Following these conventions will make your program fit better into Emacs when it runs.
next-line
or previous-line
in programs; nearly
always, forward-line
is more convenient as well as more
predictable and robust. See Motion by Text Lines.
In particular, don’t use any of these functions:
beginning-of-buffer
, end-of-buffer
replace-string
, replace-regexp
insert-file
, insert-buffer
If you just want to move point, or replace a certain string, or insert a file or buffer’s contents, 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 The Echo Area.
error
(or signal
). The function error
does not return.
See How to Signal an Error.
Don’t use message
, throw
, sleep-for
, or
beep
to report errors.
It is occasionally useful to tell the user where an error originated,
even if debug-on-error
is nil
. In such cases, a
lower-case Lisp symbol can be prepended to the error message. For
example, the error message “Invalid input” could be extended to say
“some-function: Invalid input”.
yes-or-no-p
or
y-or-n-p
should start with a capital letter and end with
‘?’.
Enter the answer (default 42):
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.