[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here are conventions that you should follow when writing Emacs Lisp code intended for widespread use:
This recommendation applies even to names for traditional Lisp
primitives that are not primitives in Emacs Lisp--even to
copy-list
. Believe it or not, there is more than one plausible
way to define copy-list
. Play it safe; append your name prefix
to produce a name like foo-copy-list
or mylib-copy-list
instead.
If you write a function that you think ought to be added to Emacs under
a certain name, such as twiddle-files
, don't call it by that name
in your program. Call it mylib-twiddle-files
in your program,
and send mail to `bug-gnu-emacs@gnu.org' suggesting we add
it to Emacs. If and when we do, we can change the name easily enough.
If one prefix is insufficient, your package may use two or three alternative common prefixes, so long as they make sense.
Separate the prefix from the rest of the symbol name with a hyphen, `-'. This will be consistent with Emacs itself and with most Emacs Lisp programs.
provide
in each separate
library program, at least if there is more than one entry point to the
program.
require
to make sure they are loaded.
(eval-when-compile (require 'bar)) |
(And the library bar should contain (provide 'bar)
,
to make the require
work.) This will cause bar to be
loaded when you byte-compile foo. Otherwise, you risk compiling
foo without the necessary macro loaded, and that would produce
compiled code that won't work right. See section 13.3 Macros and Byte Compilation.
Using eval-when-compile
avoids loading bar when
the compiled version of foo is used.
cl
package of Common Lisp extensions at
run time. Use of this package is optional, and it is not part of the
standard Emacs namespace. If your package loads cl
at run time,
that could cause name clashes for users who don't use that package.
However, there is no problem with using the cl
package at compile
time, for the sake of macros. You do that like this:
(eval-when-compile (require 'cl)) |
framep
and frame-live-p
.
Instead, define sequences consisting of C-c followed by a control character, a digit, or certain punctuation characters. These sequences are reserved for major modes.
Changing all the Emacs major modes to follow this convention was a lot of work. Abandoning this convention would make that work go to waste, and inconvenience users.
The reason for this rule is that a non-prefix binding for ESC in any context prevents recognition of escape sequences as function keys in that context.
For a state which accepts ordinary Emacs commands, or more generally any kind of state in which ESC followed by a function key or arrow key is potentially meaningful, then you must not define ESC ESC, since that would preclude recognizing an escape sequence after ESC. In these states, you should define ESC ESC ESC as the way to escape. Otherwise, define ESC ESC instead.
whatever-mode
which turns the feature on or
off, and make it autoload (see section 15.4 Autoload). Design the package so
that simply loading it has no visible effect--that should not enable
the feature.(11) Users will request the feature by
invoking the command.
(defalias 'gnus-point-at-bol (if (fboundp 'point-at-bol) 'point-at-bol 'line-beginning-position)) |
next-line
or previous-line
in programs; nearly
always, forward-line
is more convenient as well as more
predictable and robust. See section 30.2.4 Motion by Text Lines.
In particular, don't use any of these functions:
beginning-of-buffer
, end-of-buffer
replace-string
, replace-regexp
If you just want to move point, or replace a certain string, without any of the other features intended for interactive users, you can replace these functions with one or two lines of simple Lisp code.
Vectors are advantageous for tables that are substantial in size and are accessed in random order (not searched front to back), provided there is no need to insert or delete elements (only lists allow that).
message
function, not princ
. See section 38.4 The Echo Area.
error
(or signal
). The function error
does not return.
See section 10.5.3.1 How to Signal an Error.
Do not use message
, throw
, sleep-for
,
or beep
to report errors.
interactive
, if you use a Lisp expression to produce a list
of arguments, don't try to provide the "correct" default values for
region or position arguments. Instead, provide nil
for those
arguments if they were not specified, and have the function body
compute the default value when the argument is nil
. For
instance, write this:
(defun foo (pos) (interactive (list (if specified specified-pos))) (unless pos (setq pos default-pos)) ...) |
rather than this:
(defun foo (pos) (interactive (list (if specified specified-pos default-pos))) ...) |
This is so that repetition of the command will recompute these defaults based on the current circumstances.
You do not need to take such precautions when you use interactive specs `d', `m' and `r', because they make special arrangements to recompute the argument values on repetition of the command.
edit-options
command does: switch to another buffer and let the
user switch back at will. See section 21.12 Recursive Editing.
defvar
definitions for these variables.
Sometimes adding a require
for another package is useful to avoid
compilation warnings for variables and functions defined in that
package. If you do this, often it is better if the require
acts
only at compile time. Here's how to do that:
(eval-when-compile (require 'foo) (defvar bar-baz)) |
If you bind a variable in one function, and use it or set it in another function, the compiler warns about the latter function unless the variable has a definition. But often these variables have short names, and it is not clean for Lisp packages to define such variable names. Therefore, you should rename the variable to start with the name prefix used for the other functions and variables in your package.
indent-sexp
) using the
default indentation parameters.
;; Copyright (C) year name ;; This program is free software; you can redistribute it and/or ;; modify it under the terms of the GNU General Public License as ;; published by the Free Software Foundation; either version 2 of ;; the License, or (at your option) any later version. ;; This program is distributed in the hope that it will be ;; useful, but WITHOUT ANY WARRANTY; without even the implied ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ;; PURPOSE. See the GNU General Public License for more details. ;; You should have received a copy of the GNU General Public ;; License along with this program; if not, write to the Free ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, ;; MA 02111-1307 USA |
If you have signed papers to assign the copyright to the Foundation, then use `Free Software Foundation, Inc.' as name. Otherwise, use your name.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |