* emacs-lisp/byte-run.el (defmacro): Use same argument parsing as
[bpt/emacs.git] / doc / lispref / tips.texi
CommitLineData
b8d4c8d0
GM
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
acaf905b 3@c Copyright (C) 1990-1993, 1995, 1998-1999, 2001-2012
d24880de 4@c Free Software Foundation, Inc.
b8d4c8d0 5@c See the file elisp.texi for copying conditions.
ecc6530d 6@node Tips
b8d4c8d0
GM
7@appendix Tips and Conventions
8@cindex tips for writing Lisp
9@cindex standards of coding style
10@cindex coding standards
11
12 This chapter describes no additional features of Emacs Lisp. Instead
13it gives advice on making effective use of the features described in the
14previous chapters, and describes conventions Emacs Lisp programmers
15should follow.
16
17 You can automatically check some of the conventions described below by
18running the command @kbd{M-x checkdoc RET} when visiting a Lisp file.
19It cannot check all of the conventions, and not all the warnings it
20gives necessarily correspond to problems, but it is worth examining them
21all.
22
23@menu
24* Coding Conventions:: Conventions for clean and robust programs.
25* Key Binding Conventions:: Which keys should be bound by which programs.
26* Programming Tips:: Making Emacs code fit smoothly in Emacs.
27* Compilation Tips:: Making compiled code run fast.
28* Warning Tips:: Turning off compiler warnings.
29* Documentation Tips:: Writing readable documentation strings.
d24880de 30* Comment Tips:: Conventions for writing comments.
b8d4c8d0
GM
31* Library Headers:: Standard headers for library packages.
32@end menu
33
34@node Coding Conventions
35@section Emacs Lisp Coding Conventions
36
37@cindex coding conventions in Emacs Lisp
38 Here are conventions that you should follow when writing Emacs Lisp
39code intended for widespread use:
40
41@itemize @bullet
42@item
4f1e25e2 43Simply loading a package should not change Emacs's editing behavior.
b8d4c8d0
GM
44Include a command or commands to enable and disable the feature,
45or to invoke it.
46
47This convention is mandatory for any file that includes custom
48definitions. If fixing such a file to follow this convention requires
49an incompatible change, go ahead and make the incompatible change;
50don't postpone it.
51
52@item
4f1e25e2
CY
53You should choose a short word to distinguish your program from other
54Lisp programs. The names of all global variables, constants, and
55functions in your program should begin with that chosen prefix.
56Separate the prefix from the rest of the name with a hyphen, @samp{-}.
57This practice helps avoid name conflicts, since all global variables
58in Emacs Lisp share the same name space, and all functions share
59another name space@footnote{The benefits of a Common Lisp-style
b3134b95 60package system are considered not to outweigh the costs.}.
b8d4c8d0
GM
61
62Occasionally, for a command name intended for users to use, it is more
63convenient if some words come before the package's name prefix. And
64constructs that define functions, variables, etc., work better if they
65start with @samp{defun} or @samp{defvar}, so put the name prefix later
66on in the name.
67
68This recommendation applies even to names for traditional Lisp
69primitives that are not primitives in Emacs Lisp---such as
70@code{copy-list}. Believe it or not, there is more than one plausible
71way to define @code{copy-list}. Play it safe; append your name prefix
72to produce a name like @code{foo-copy-list} or @code{mylib-copy-list}
73instead.
74
75If you write a function that you think ought to be added to Emacs under
76a certain name, such as @code{twiddle-files}, don't call it by that name
77in your program. Call it @code{mylib-twiddle-files} in your program,
78and send mail to @samp{bug-gnu-emacs@@gnu.org} suggesting we add
79it to Emacs. If and when we do, we can change the name easily enough.
80
81If one prefix is insufficient, your package can use two or three
82alternative common prefixes, so long as they make sense.
83
b8d4c8d0
GM
84@item
85Put a call to @code{provide} at the end of each separate Lisp file.
4f1e25e2 86@xref{Named Features}.
b8d4c8d0
GM
87
88@item
89If a file requires certain other Lisp programs to be loaded
90beforehand, then the comments at the beginning of the file should say
91so. Also, use @code{require} to make sure they are loaded.
da0bbbc4 92@xref{Named Features}.
b8d4c8d0
GM
93
94@item
4f1e25e2
CY
95If a file @var{foo} uses a macro defined in another file @var{bar},
96but does not use any functions or variables defined in @var{bar}, then
97@var{foo} should contain the following expression:
b8d4c8d0
GM
98
99@example
100(eval-when-compile (require '@var{bar}))
101@end example
102
103@noindent
4f1e25e2
CY
104This tells Emacs to load @var{bar} just before byte-compiling
105@var{foo}, so that the macro definition is available during
106compilation. Using @code{eval-when-compile} avoids loading @var{bar}
107when the compiled version of @var{foo} is @emph{used}. It should be
108called before the first use of the macro in the file. @xref{Compiling
109Macros}.
b8d4c8d0 110
b3134b95
GM
111@item
112Avoid loading additional libraries at run time unless they are really
113needed. If your file simply cannot work without some other library,
114then just @code{require} that library at the top-level and be done
115with it. But if your file contains several independent features, and
116only one or two require the extra library, then consider putting
117@code{require} statements inside the relevant functions rather than at
118the top-level. Or use @code{autoload} statements to load the extra
119library when needed. This way people who don't use those aspects of
120your file do not need to load the extra library.
121
b8d4c8d0 122@item
5fb904b0
GM
123If you need Common Lisp extensions, use the @code{cl-lib} library
124rather than the old @code{cl} library. The latter does not
125use a clean namespace (i.e., its definitions do not
126start with a @samp{cl-} prefix). If your package loads @code{cl} at
127run time, that could cause name clashes for users who don't use that
128package.
129
130There is no problem with using the @code{cl} package at @emph{compile}
131time, with @code{(eval-when-compile (require 'cl))}. That's
b8d4c8d0 132sufficient for using the macros in the @code{cl} package, because the
5fb904b0
GM
133compiler expands them before generating the byte-code. It is still
134better to use the more modern @code{cl-lib} in this case, though.
b8d4c8d0
GM
135
136@item
137When defining a major mode, please follow the major mode
138conventions. @xref{Major Mode Conventions}.
139
140@item
141When defining a minor mode, please follow the minor mode
142conventions. @xref{Minor Mode Conventions}.
143
144@item
4f1e25e2
CY
145If the purpose of a function is to tell you whether a certain
146condition is true or false, give the function a name that ends in
147@samp{p} (which stands for ``predicate''). If the name is one word,
148add just @samp{p}; if the name is multiple words, add @samp{-p}.
149Examples are @code{framep} and @code{frame-live-p}.
b8d4c8d0 150
b8d4c8d0
GM
151@item
152If the purpose of a variable is to store a single function, give it a
153name that ends in @samp{-function}. If the purpose of a variable is
154to store a list of functions (i.e., the variable is a hook), please
155follow the naming conventions for hooks. @xref{Hooks}.
156
157@item
158@cindex unloading packages, preparing for
159If loading the file adds functions to hooks, define a function
160@code{@var{feature}-unload-hook}, where @var{feature} is the name of
161the feature the package provides, and make it undo any such changes.
162Using @code{unload-feature} to unload the file will run this function.
163@xref{Unloading}.
164
165@item
166It is a bad idea to define aliases for the Emacs primitives. Normally
167you should use the standard names instead. The case where an alias
168may be useful is where it facilitates backwards compatibility or
169portability.
170
171@item
172If a package needs to define an alias or a new function for
173compatibility with some other version of Emacs, name it with the package
174prefix, not with the raw name with which it occurs in the other version.
175Here is an example from Gnus, which provides many examples of such
176compatibility issues.
177
178@example
179(defalias 'gnus-point-at-bol
180 (if (fboundp 'point-at-bol)
181 'point-at-bol
182 'line-beginning-position))
183@end example
184
185@item
4f1e25e2 186Redefining or advising an Emacs primitive is a bad idea. It may do
b8d4c8d0 187the right thing for a particular program, but there is no telling what
4f1e25e2 188other programs might break as a result.
b8d4c8d0
GM
189
190@item
4f1e25e2
CY
191It is likewise a bad idea for one Lisp package to advise a function in
192another Lisp package (@pxref{Advising Functions}).
b8d4c8d0
GM
193
194@item
4f1e25e2
CY
195Avoid using @code{eval-after-load} in libraries and packages
196(@pxref{Hooks for Loading}). This feature is meant for personal
197customizations; using it in a Lisp program is unclean, because it
198modifies the behavior of another Lisp file in a way that's not visible
199in that file. This is an obstacle for debugging, much like advising a
200function in the other package.
b8d4c8d0
GM
201
202@item
4f1e25e2
CY
203If a file does replace any of the standard functions or library
204programs of Emacs, prominent comments at the beginning of the file
205should say which functions are replaced, and how the behavior of the
b8d4c8d0
GM
206replacements differs from that of the originals.
207
208@item
209Constructs that define a function or variable should be macros,
b3134b95
GM
210not functions, and their names should start with @samp{define-}.
211The macro should receive the name to be
b8d4c8d0
GM
212defined as the first argument. That will help various tools find the
213definition automatically. Avoid constructing the names in the macro
214itself, since that would confuse these tools.
215
b8d4c8d0
GM
216@item
217In some other systems there is a convention of choosing variable names
218that begin and end with @samp{*}. We don't use that convention in Emacs
219Lisp, so please don't use it in your programs. (Emacs uses such names
b3134b95 220only for special-purpose buffers.) People will find Emacs more
b8d4c8d0
GM
221coherent if all libraries use the same conventions.
222
223@item
224If your program contains non-ASCII characters in string or character
225constants, you should make sure Emacs always decodes these characters
4f1e25e2
CY
226the same way, regardless of the user's settings. The easiest way to
227do this is to use the coding system @code{utf-8-emacs} (@pxref{Coding
228System Basics}), and specify that coding in the @samp{-*-} line or the
b3134b95 229local variables list. @xref{File Variables, , Local Variables in
4f1e25e2 230Files, emacs, The GNU Emacs Manual}.
b8d4c8d0
GM
231
232@example
4f1e25e2 233;; XXX.el -*- coding: utf-8-emacs; -*-
b8d4c8d0 234@end example
b8d4c8d0
GM
235
236@item
b3134b95 237Indent the file using the default indentation parameters.
b8d4c8d0
GM
238
239@item
4f1e25e2
CY
240Don't make a habit of putting close-parentheses on lines by
241themselves; Lisp programmers find this disconcerting.
b8d4c8d0
GM
242
243@item
244Please put a copyright notice and copying permission notice on the
b3134b95 245file if you distribute copies. @xref{Library Headers}.
352c8b4a 246
b8d4c8d0
GM
247@end itemize
248
249@node Key Binding Conventions
250@section Key Binding Conventions
251@cindex key binding, conventions for
252
253@itemize @bullet
254@item
255@cindex mouse-2
256@cindex references, following
4f1e25e2
CY
257Many special major modes, like Dired, Info, Compilation, and Occur,
258are designed to handle read-only text that contains @dfn{hyper-links}.
259Such a major mode should redefine @kbd{mouse-2} and @key{RET} to
260follow the links. It should also set up a @code{follow-link}
261condition, so that the link obeys @code{mouse-1-click-follows-link}.
262@xref{Clickable Text}. @xref{Buttons}, for an easy method of
263implementing such clickable links.
b8d4c8d0
GM
264
265@item
266@cindex reserved keys
267@cindex keys, reserved
4f1e25e2 268Don't define @kbd{C-c @var{letter}} as a key in Lisp programs.
b8d4c8d0
GM
269Sequences consisting of @kbd{C-c} and a letter (either upper or lower
270case) are reserved for users; they are the @strong{only} sequences
271reserved for users, so do not block them.
272
273Changing all the Emacs major modes to respect this convention was a
274lot of work; abandoning this convention would make that work go to
275waste, and inconvenience users. Please comply with it.
276
277@item
278Function keys @key{F5} through @key{F9} without modifier keys are
279also reserved for users to define.
280
b8d4c8d0
GM
281@item
282Sequences consisting of @kbd{C-c} followed by a control character or a
283digit are reserved for major modes.
284
285@item
286Sequences consisting of @kbd{C-c} followed by @kbd{@{}, @kbd{@}},
287@kbd{<}, @kbd{>}, @kbd{:} or @kbd{;} are also reserved for major modes.
288
289@item
290Sequences consisting of @kbd{C-c} followed by any other punctuation
291character are allocated for minor modes. Using them in a major mode is
292not absolutely prohibited, but if you do that, the major mode binding
293may be shadowed from time to time by minor modes.
294
295@item
4f1e25e2
CY
296Don't bind @kbd{C-h} following any prefix character (including
297@kbd{C-c}). If you don't bind @kbd{C-h}, it is automatically
298available as a help character for listing the subcommands of the
299prefix character.
b8d4c8d0
GM
300
301@item
4f1e25e2
CY
302Don't bind a key sequence ending in @key{ESC} except following another
303@key{ESC}. (That is, it is OK to bind a sequence ending in
b8d4c8d0
GM
304@kbd{@key{ESC} @key{ESC}}.)
305
306The reason for this rule is that a non-prefix binding for @key{ESC} in
307any context prevents recognition of escape sequences as function keys in
308that context.
309
b8f0a954
CY
310@item
311Similarly, don't bind a key sequence ending in @key{C-g}, since that
312is commonly used to cancel a key sequence.
313
b8d4c8d0 314@item
b3134b95 315Anything that acts like a temporary mode or state that the user can
b8d4c8d0
GM
316enter and leave should define @kbd{@key{ESC} @key{ESC}} or
317@kbd{@key{ESC} @key{ESC} @key{ESC}} as a way to escape.
318
b3134b95 319For a state that accepts ordinary Emacs commands, or more generally any
b8d4c8d0
GM
320kind of state in which @key{ESC} followed by a function key or arrow key
321is potentially meaningful, then you must not define @kbd{@key{ESC}
322@key{ESC}}, since that would preclude recognizing an escape sequence
323after @key{ESC}. In these states, you should define @kbd{@key{ESC}
324@key{ESC} @key{ESC}} as the way to escape. Otherwise, define
325@kbd{@key{ESC} @key{ESC}} instead.
326@end itemize
327
328@node Programming Tips
329@section Emacs Programming Tips
330@cindex programming conventions
331
332 Following these conventions will make your program fit better
333into Emacs when it runs.
334
335@itemize @bullet
336@item
337Don't use @code{next-line} or @code{previous-line} in programs; nearly
338always, @code{forward-line} is more convenient as well as more
339predictable and robust. @xref{Text Lines}.
340
341@item
342Don't call functions that set the mark, unless setting the mark is one
343of the intended features of your program. The mark is a user-level
344feature, so it is incorrect to change the mark except to supply a value
345for the user's benefit. @xref{The Mark}.
346
347In particular, don't use any of these functions:
348
349@itemize @bullet
350@item
351@code{beginning-of-buffer}, @code{end-of-buffer}
352@item
353@code{replace-string}, @code{replace-regexp}
354@item
355@code{insert-file}, @code{insert-buffer}
356@end itemize
357
358If you just want to move point, or replace a certain string, or insert
359a file or buffer's contents, without any of the other features
360intended for interactive users, you can replace these functions with
361one or two lines of simple Lisp code.
362
363@item
364Use lists rather than vectors, except when there is a particular reason
365to use a vector. Lisp has more facilities for manipulating lists than
366for vectors, and working with lists is usually more convenient.
367
368Vectors are advantageous for tables that are substantial in size and are
369accessed in random order (not searched front to back), provided there is
370no need to insert or delete elements (only lists allow that).
371
372@item
373The recommended way to show a message in the echo area is with
374the @code{message} function, not @code{princ}. @xref{The Echo Area}.
375
376@item
377When you encounter an error condition, call the function @code{error}
378(or @code{signal}). The function @code{error} does not return.
379@xref{Signaling Errors}.
380
4f1e25e2
CY
381Don't use @code{message}, @code{throw}, @code{sleep-for}, or
382@code{beep} to report errors.
b8d4c8d0
GM
383
384@item
385An error message should start with a capital letter but should not end
386with a period.
387
388@item
b3134b95
GM
389A question asked in the minibuffer with @code{yes-or-no-p} or
390@code{y-or-n-p} should start with a capital letter and end with
b8d4c8d0
GM
391@samp{? }.
392
393@item
394When you mention a default value in a minibuffer prompt,
395put it and the word @samp{default} inside parentheses.
396It should look like this:
397
398@example
399Enter the answer (default 42):
400@end example
401
402@item
403In @code{interactive}, if you use a Lisp expression to produce a list
404of arguments, don't try to provide the ``correct'' default values for
405region or position arguments. Instead, provide @code{nil} for those
406arguments if they were not specified, and have the function body
407compute the default value when the argument is @code{nil}. For
408instance, write this:
409
410@example
411(defun foo (pos)
412 (interactive
413 (list (if @var{specified} @var{specified-pos})))
414 (unless pos (setq pos @var{default-pos}))
415 ...)
416@end example
417
418@noindent
419rather than this:
420
421@example
422(defun foo (pos)
423 (interactive
424 (list (if @var{specified} @var{specified-pos}
425 @var{default-pos})))
426 ...)
427@end example
428
429@noindent
430This is so that repetition of the command will recompute
431these defaults based on the current circumstances.
432
433You do not need to take such precautions when you use interactive
434specs @samp{d}, @samp{m} and @samp{r}, because they make special
435arrangements to recompute the argument values on repetition of the
436command.
437
438@item
439Many commands that take a long time to execute display a message that
4f1e25e2
CY
440says something like @samp{Operating...} when they start, and change it
441to @samp{Operating...done} when they finish. Please keep the style of
b8d4c8d0 442these messages uniform: @emph{no} space around the ellipsis, and
4f1e25e2
CY
443@emph{no} period after @samp{done}. @xref{Progress}, for an easy way
444to generate such messages.
b8d4c8d0
GM
445
446@item
447Try to avoid using recursive edits. Instead, do what the Rmail @kbd{e}
b3134b95
GM
448command does: use a new local keymap that contains a command defined
449to switch back to the old local keymap. Or simply switch to another
450buffer and let the user switch back at will. @xref{Recursive Editing}.
b8d4c8d0
GM
451@end itemize
452
453@node Compilation Tips
454@section Tips for Making Compiled Code Fast
455@cindex execution speed
456@cindex speedups
457
458 Here are ways of improving the execution speed of byte-compiled
459Lisp programs.
460
461@itemize @bullet
462@item
5b776637
GM
463Profile your program, to find out where the time is being spent.
464@xref{Profiling}.
b8d4c8d0
GM
465
466@item
467Use iteration rather than recursion whenever possible.
468Function calls are slow in Emacs Lisp even when a compiled function
469is calling another compiled function.
470
471@item
472Using the primitive list-searching functions @code{memq}, @code{member},
473@code{assq}, or @code{assoc} is even faster than explicit iteration. It
474can be worth rearranging a data structure so that one of these primitive
475search functions can be used.
476
477@item
478Certain built-in functions are handled specially in byte-compiled code,
479avoiding the need for an ordinary function call. It is a good idea to
480use these functions rather than alternatives. To see whether a function
481is handled specially by the compiler, examine its @code{byte-compile}
482property. If the property is non-@code{nil}, then the function is
483handled specially.
484
485For example, the following input will show you that @code{aref} is
486compiled specially (@pxref{Array Functions}):
487
488@example
489@group
490(get 'aref 'byte-compile)
491 @result{} byte-compile-two-args
492@end group
493@end example
494
b3134b95
GM
495@noindent
496Note that in this case (and many others), you must first load the
497@file{bytecomp} library, which defines the @code{byte-compile} property.
498
b8d4c8d0
GM
499@item
500If calling a small function accounts for a substantial part of your
501program's running time, make the function inline. This eliminates
502the function call overhead. Since making a function inline reduces
503the flexibility of changing the program, don't do it unless it gives
504a noticeable speedup in something slow enough that users care about
505the speed. @xref{Inline Functions}.
506@end itemize
507
508@node Warning Tips
509@section Tips for Avoiding Compiler Warnings
510@cindex byte compiler warnings, how to avoid
511
512@itemize @bullet
513@item
514Try to avoid compiler warnings about undefined free variables, by adding
515dummy @code{defvar} definitions for these variables, like this:
516
517@example
518(defvar foo)
519@end example
520
521Such a definition has no effect except to tell the compiler
522not to warn about uses of the variable @code{foo} in this file.
523
b3134b95
GM
524@item
525Similarly, to avoid a compiler warning about an undefined function
526that you know @emph{will} be defined, use a @code{declare-function}
527statement (@pxref{Declaring Functions}).
528
b8d4c8d0
GM
529@item
530If you use many functions and variables from a certain file, you can
531add a @code{require} for that package to avoid compilation warnings
532for them. For instance,
533
534@example
535(eval-when-compile
536 (require 'foo))
537@end example
538
539@item
540If you bind a variable in one function, and use it or set it in
541another function, the compiler warns about the latter function unless
542the variable has a definition. But adding a definition would be
543unclean if the variable has a short name, since Lisp packages should
544not define short variable names. The right thing to do is to rename
545this variable to start with the name prefix used for the other
546functions and variables in your package.
547
548@item
549The last resort for avoiding a warning, when you want to do something
b3134b95
GM
550that is usually a mistake but you know is not a mistake in your usage,
551is to put it inside @code{with-no-warnings}. @xref{Compiler Errors}.
b8d4c8d0
GM
552@end itemize
553
554@node Documentation Tips
555@section Tips for Documentation Strings
556@cindex documentation strings, conventions and tips
557
558@findex checkdoc-minor-mode
559 Here are some tips and conventions for the writing of documentation
560strings. You can check many of these conventions by running the command
561@kbd{M-x checkdoc-minor-mode}.
562
563@itemize @bullet
564@item
565Every command, function, or variable intended for users to know about
566should have a documentation string.
567
568@item
b3134b95
GM
569An internal variable or subroutine of a Lisp program might as well
570have a documentation string. Documentation strings take up very
571little space in a running Emacs.
b8d4c8d0
GM
572
573@item
574Format the documentation string so that it fits in an Emacs window on an
57580-column screen. It is a good idea for most lines to be no wider than
57660 characters. The first line should not be wider than 67 characters
577or it will look bad in the output of @code{apropos}.
578
579You can fill the text if that looks good. However, rather than blindly
580filling the entire documentation string, you can often make it much more
581readable by choosing certain line breaks with care. Use blank lines
b3134b95 582between sections if the documentation string is long.
b8d4c8d0
GM
583
584@item
585The first line of the documentation string should consist of one or two
586complete sentences that stand on their own as a summary. @kbd{M-x
587apropos} displays just the first line, and if that line's contents don't
588stand on their own, the result looks bad. In particular, start the
b3134b95 589first line with a capital letter and end it with a period.
b8d4c8d0
GM
590
591For a function, the first line should briefly answer the question,
592``What does this function do?'' For a variable, the first line should
593briefly answer the question, ``What does this value mean?''
594
595Don't limit the documentation string to one line; use as many lines as
596you need to explain the details of how to use the function or
597variable. Please use complete sentences for the rest of the text too.
598
599@item
600When the user tries to use a disabled command, Emacs displays just the
601first paragraph of its documentation string---everything through the
602first blank line. If you wish, you can choose which information to
603include before the first blank line so as to make this display useful.
604
605@item
606The first line should mention all the important arguments of the
607function, and should mention them in the order that they are written
608in a function call. If the function has many arguments, then it is
609not feasible to mention them all in the first line; in that case, the
610first line should mention the first few arguments, including the most
611important arguments.
612
613@item
614When a function's documentation string mentions the value of an argument
615of the function, use the argument name in capital letters as if it were
616a name for that value. Thus, the documentation string of the function
b3134b95 617@code{eval} refers to its first argument as @samp{FORM}, because the
b8d4c8d0
GM
618actual argument name is @code{form}:
619
620@example
621Evaluate FORM and return its value.
622@end example
623
624Also write metasyntactic variables in capital letters, such as when you
625show the decomposition of a list or vector into subunits, some of which
626may vary. @samp{KEY} and @samp{VALUE} in the following example
627illustrate this practice:
628
629@example
630The argument TABLE should be an alist whose elements
631have the form (KEY . VALUE). Here, KEY is ...
632@end example
633
634@item
635Never change the case of a Lisp symbol when you mention it in a doc
16152b76 636string. If the symbol's name is @code{foo}, write ``foo'', not
b8d4c8d0
GM
637``Foo'' (which is a different symbol).
638
639This might appear to contradict the policy of writing function
640argument values, but there is no real contradiction; the argument
b3134b95 641@emph{value} is not the same thing as the @emph{symbol} that the
b8d4c8d0
GM
642function uses to hold the value.
643
644If this puts a lower-case letter at the beginning of a sentence
645and that annoys you, rewrite the sentence so that the symbol
646is not at the start of it.
647
648@item
649Do not start or end a documentation string with whitespace.
650
651@item
652@strong{Do not} indent subsequent lines of a documentation string so
653that the text is lined up in the source code with the text of the first
654line. This looks nice in the source code, but looks bizarre when users
655view the documentation. Remember that the indentation before the
656starting double-quote is not part of the string!
657
658@anchor{Docstring hyperlinks}
659@item
660@iftex
661When a documentation string refers to a Lisp symbol, write it as it
662would be printed (which usually means in lower case), with single-quotes
663around it. For example: @samp{`lambda'}. There are two exceptions:
664write @code{t} and @code{nil} without single-quotes.
665@end iftex
666@ifnottex
667When a documentation string refers to a Lisp symbol, write it as it
668would be printed (which usually means in lower case), with single-quotes
669around it. For example: @samp{lambda}. There are two exceptions: write
670t and nil without single-quotes. (In this manual, we use a different
671convention, with single-quotes for all symbols.)
672@end ifnottex
673
674@cindex hyperlinks in documentation strings
675Help mode automatically creates a hyperlink when a documentation string
676uses a symbol name inside single quotes, if the symbol has either a
677function or a variable definition. You do not need to do anything
678special to make use of this feature. However, when a symbol has both a
679function definition and a variable definition, and you want to refer to
680just one of them, you can specify which one by writing one of the words
681@samp{variable}, @samp{option}, @samp{function}, or @samp{command},
682immediately before the symbol name. (Case makes no difference in
683recognizing these indicator words.) For example, if you write
684
685@example
686This function sets the variable `buffer-file-name'.
687@end example
688
689@noindent
690then the hyperlink will refer only to the variable documentation of
691@code{buffer-file-name}, and not to its function documentation.
692
693If a symbol has a function definition and/or a variable definition, but
694those are irrelevant to the use of the symbol that you are documenting,
695you can write the words @samp{symbol} or @samp{program} before the
696symbol name to prevent making any hyperlink. For example,
697
698@example
699If the argument KIND-OF-RESULT is the symbol `list',
700this function returns a list of all the objects
701that satisfy the criterion.
702@end example
703
704@noindent
705does not make a hyperlink to the documentation, irrelevant here, of the
706function @code{list}.
707
708Normally, no hyperlink is made for a variable without variable
709documentation. You can force a hyperlink for such variables by
710preceding them with one of the words @samp{variable} or
711@samp{option}.
712
713Hyperlinks for faces are only made if the face name is preceded or
714followed by the word @samp{face}. In that case, only the face
715documentation will be shown, even if the symbol is also defined as a
716variable or as a function.
717
718To make a hyperlink to Info documentation, write the name of the Info
719node (or anchor) in single quotes, preceded by @samp{info node},
720@samp{Info node}, @samp{info anchor} or @samp{Info anchor}. The Info
721file name defaults to @samp{emacs}. For example,
722
723@smallexample
724See Info node `Font Lock' and Info node `(elisp)Font Lock Basics'.
725@end smallexample
726
727Finally, to create a hyperlink to URLs, write the URL in single
728quotes, preceded by @samp{URL}. For example,
729
730@smallexample
731The home page for the GNU project has more information (see URL
732`http://www.gnu.org/').
733@end smallexample
734
735@item
736Don't write key sequences directly in documentation strings. Instead,
737use the @samp{\\[@dots{}]} construct to stand for them. For example,
738instead of writing @samp{C-f}, write the construct
739@samp{\\[forward-char]}. When Emacs displays the documentation string,
740it substitutes whatever key is currently bound to @code{forward-char}.
741(This is normally @samp{C-f}, but it may be some other character if the
742user has moved key bindings.) @xref{Keys in Documentation}.
743
744@item
745In documentation strings for a major mode, you will want to refer to the
746key bindings of that mode's local map, rather than global ones.
747Therefore, use the construct @samp{\\<@dots{}>} once in the
748documentation string to specify which key map to use. Do this before
749the first use of @samp{\\[@dots{}]}. The text inside the
750@samp{\\<@dots{}>} should be the name of the variable containing the
751local keymap for the major mode.
752
753It is not practical to use @samp{\\[@dots{}]} very many times, because
754display of the documentation string will become slow. So use this to
755describe the most important commands in your major mode, and then use
756@samp{\\@{@dots{}@}} to display the rest of the mode's keymap.
757
758@item
759For consistency, phrase the verb in the first sentence of a function's
760documentation string as an imperative---for instance, use ``Return the
1df7defd 761cons of A and B.@:'' in preference to ``Returns the cons of A and B@.''
b8d4c8d0
GM
762Usually it looks good to do likewise for the rest of the first
763paragraph. Subsequent paragraphs usually look better if each sentence
764is indicative and has a proper subject.
765
766@item
767The documentation string for a function that is a yes-or-no predicate
16152b76
GM
768should start with words such as ``Return t if'', to indicate
769explicitly what constitutes ``truth''. The word ``return'' avoids
770starting the sentence with lower-case ``t'', which could be somewhat
b8d4c8d0
GM
771distracting.
772
773@item
774If a line in a documentation string begins with an open-parenthesis,
775write a backslash before the open-parenthesis, like this:
776
777@example
778The argument FOO can be either a number
779\(a buffer position) or a string (a file name).
780@end example
781
782This prevents the open-parenthesis from being treated as the start of a
783defun (@pxref{Defuns,, Defuns, emacs, The GNU Emacs Manual}).
784
785@item
786Write documentation strings in the active voice, not the passive, and in
787the present tense, not the future. For instance, use ``Return a list
1df7defd 788containing A and B.@:'' instead of ``A list containing A and B will be
b8d4c8d0
GM
789returned.''
790
791@item
792Avoid using the word ``cause'' (or its equivalents) unnecessarily.
16152b76
GM
793Instead of, ``Cause Emacs to display text in boldface'', write just
794``Display text in boldface''.
b8d4c8d0
GM
795
796@item
797Avoid using ``iff'' (a mathematics term meaning ``if and only if''),
798since many people are unfamiliar with it and mistake it for a typo. In
799most cases, the meaning is clear with just ``if''. Otherwise, try to
800find an alternate phrasing that conveys the meaning.
801
802@item
803When a command is meaningful only in a certain mode or situation,
804do mention that in the documentation string. For example,
805the documentation of @code{dired-find-file} is:
806
807@example
808In Dired, visit the file or directory named on this line.
809@end example
810
811@item
b3134b95
GM
812When you define a variable that represents an option users might want
813to set, use @code{defcustom}. @xref{Defining Variables}.
b8d4c8d0
GM
814
815@item
816The documentation string for a variable that is a yes-or-no flag should
16152b76 817start with words such as ``Non-nil means'', to make it clear that
b8d4c8d0
GM
818all non-@code{nil} values are equivalent and indicate explicitly what
819@code{nil} and non-@code{nil} mean.
820@end itemize
821
822@node Comment Tips
823@section Tips on Writing Comments
824@cindex comments, Lisp convention for
825
b3134b95 826 We recommend these conventions for comments:
b8d4c8d0
GM
827
828@table @samp
829@item ;
830Comments that start with a single semicolon, @samp{;}, should all be
831aligned to the same column on the right of the source code. Such
b3134b95
GM
832comments usually explain how the code on that line does its job.
833For example:
b8d4c8d0
GM
834
835@smallexample
836@group
837(setq base-version-list ; there was a base
838 (assoc (substring fn 0 start-vn) ; version to which
839 file-version-assoc-list)) ; this looks like
840 ; a subversion
841@end group
842@end smallexample
843
844@item ;;
845Comments that start with two semicolons, @samp{;;}, should be aligned to
846the same level of indentation as the code. Such comments usually
847describe the purpose of the following lines or the state of the program
848at that point. For example:
849
850@smallexample
851@group
852(prog1 (setq auto-fill-function
853 @dots{}
854 @dots{}
b3134b95 855 ;; Update mode line.
b8d4c8d0
GM
856 (force-mode-line-update)))
857@end group
858@end smallexample
859
860We also normally use two semicolons for comments outside functions.
861
862@smallexample
863@group
b3134b95
GM
864;; This Lisp code is run in Emacs when it is to operate as
865;; a server for other processes.
b8d4c8d0
GM
866@end group
867@end smallexample
868
b3134b95
GM
869If a function has no documentation string, it should instead have a
870two-semicolon comment right before the function, explaining what the
871function does and how to call it properly. Explain precisely what
872each argument means and how the function interprets its possible
873values. It is much better to convert such comments to documentation
874strings, though.
b8d4c8d0
GM
875
876@item ;;;
877Comments that start with three semicolons, @samp{;;;}, should start at
878the left margin. These are used, occasionally, for comments within
879functions that should start at the margin. We also use them sometimes
880for comments that are between functions---whether to use two or three
881semicolons depends on whether the comment should be considered a
882``heading'' by Outline minor mode. By default, comments starting with
883at least three semicolons (followed by a single space and a
884non-whitespace character) are considered headings, comments starting
b3134b95 885with two or fewer are not.
b8d4c8d0
GM
886
887Another use for triple-semicolon comments is for commenting out lines
888within a function. We use three semicolons for this precisely so that
889they remain at the left margin. By default, Outline minor mode does
890not consider a comment to be a heading (even if it starts with at
891least three semicolons) if the semicolons are followed by at least two
892spaces. Thus, if you add an introductory comment to the commented out
893code, make sure to indent it by at least two spaces after the three
894semicolons.
895
896@smallexample
897(defun foo (a)
898;;; This is no longer necessary.
899;;; (force-mode-line-update)
900 (message "Finished with %s" a))
901@end smallexample
902
903When commenting out entire functions, use two semicolons.
904
905@item ;;;;
906Comments that start with four semicolons, @samp{;;;;}, should be aligned
907to the left margin and are used for headings of major sections of a
908program. For example:
909
910@smallexample
911;;;; The kill ring
912@end smallexample
913@end table
914
915@noindent
b3134b95
GM
916Generally speaking, the @kbd{M-;} (@code{comment-dwim}) command
917automatically starts a comment of the appropriate type; or indents an
918existing comment to the right place, depending on the number of
919semicolons.
920@xref{Comments,, Manipulating Comments, emacs, The GNU Emacs Manual}.
b8d4c8d0
GM
921
922@node Library Headers
923@section Conventional Headers for Emacs Libraries
924@cindex header comments
925@cindex library header comments
926
927 Emacs has conventions for using special comments in Lisp libraries
928to divide them into sections and give information such as who wrote
b3134b95
GM
929them. Using a standard format for these items makes it easier for
930tools (and people) to extract the relevant information. This section
931explains these conventions, starting with an example:
b8d4c8d0
GM
932
933@smallexample
934@group
b3134b95 935;;; foo.el --- Support for the Foo programming language
b8d4c8d0 936
b3134b95 937;; Copyright (C) 2010-2012 Your Name
b8d4c8d0
GM
938@end group
939
b3134b95
GM
940;; Author: Your Name <yourname@@example.com>
941;; Maintainer: Someone Else <someone@@example.com>
942;; Created: 14 Jul 2010
b8d4c8d0 943@group
b3134b95 944;; Keywords: languages
b8d4c8d0 945
b3134b95
GM
946;; This file is not part of GNU Emacs.
947
948;; This file is free software@dots{}
b8d4c8d0 949@dots{}
b3134b95 950;; along with this file. If not, see <http://www.gnu.org/licenses/>.
b8d4c8d0
GM
951@end group
952@end smallexample
953
954 The very first line should have this format:
955
956@example
957;;; @var{filename} --- @var{description}
958@end example
959
960@noindent
b3134b95 961The description should be contained in one line. If the file
b8d4c8d0 962needs a @samp{-*-} specification, put it after @var{description}.
b3134b95
GM
963If this would make the first line too long, use a Local Variables
964section at the end of the file.
965
966 The copyright notice usually lists your name (if you wrote the
967file). If you have an employer who claims copyright on your work, you
968might need to list them instead. Do not say that the copyright holder
969is the Free Software Foundation (or that the file is part of GNU
970Emacs) unless your file has been accepted into the Emacs distribution.
971For more information on the form of copyright and license notices, see
972@uref{http://www.gnu.org/licenses/gpl-howto.html, the guide on the GNU
973website}.
b8d4c8d0
GM
974
975 After the copyright notice come several @dfn{header comment} lines,
976each beginning with @samp{;; @var{header-name}:}. Here is a table of
977the conventional possibilities for @var{header-name}:
978
979@table @samp
980@item Author
b3134b95
GM
981This line states the name and email address of at least the principal
982author of the library. If there are multiple authors, list them on
983continuation lines led by @code{;;} and whitespace (this is easier
984for tools to parse than having more than one author on one line).
985We recommend including a contact email address, of the form
986@samp{<@dots{}>}. For example:
b8d4c8d0
GM
987
988@smallexample
989@group
b3134b95
GM
990;; Author: Your Name <yourname@@example.com>
991;; Someone Else <someone@@example.com>
992;; Another Person <another@@example.com>
b8d4c8d0
GM
993@end group
994@end smallexample
995
996@item Maintainer
b3134b95
GM
997This header has the same format as the Author header. It lists the
998person(s) who currently maintain(s) the file (respond to bug reports,
999etc.).
b8d4c8d0 1000
b3134b95
GM
1001If there is no maintainer line, the person(s) in the Author field
1002is/are presumed to be the maintainers. Some files in Emacs use
1003@samp{FSF} for the maintainer. This means that the original author is
1004no longer responsible for the file, and that it is maintained as part
1005of Emacs.
b8d4c8d0
GM
1006
1007@item Created
b3134b95
GM
1008This optional line gives the original creation date of the file, and
1009is for historical interest only.
b8d4c8d0
GM
1010
1011@item Version
b3134b95
GM
1012If you wish to record version numbers for the individual Lisp program,
1013put them in this line. Lisp files distributed with Emacs generally do
1014not have a @samp{Version} header, since the version number of Emacs
1015itself serves the same purpose. If you are distributing a collection
1016of multiple files, we recommend not writing the version in every file,
1017but only the main one.
b8d4c8d0
GM
1018
1019@item Keywords
1020This line lists keywords for the @code{finder-by-keyword} help command.
1021Please use that command to see a list of the meaningful keywords.
1022
b3134b95
GM
1023This field is how people will find your package when they're looking
1024for things by topic. To separate the keywords, you can use spaces,
1025commas, or both.
1026
1027The name of this field is unfortunate, since people often assume it is
1028the place to write arbitrary keywords that describe their package,
1029rather than just the relevant Finder keywords.
fdc76236
TT
1030
1031@item Package-Version
1032If @samp{Version} is not suitable for use by the package manager, then
1033a package can define @samp{Package-Version}; it will be used instead.
1034This is handy if @samp{Version} is an RCS id or something else that
1035cannot be parsed by @code{version-to-list}. @xref{Packaging Basics}.
1036
1037@item Package-Requires
1038If this exists, it names packages on which the current package depends
1039for proper operation. @xref{Packaging Basics}. This is used by the
1040package manager both at download time (to ensure that a complete set
1041of packages is downloaded) and at activation time (to ensure that a
b3134b95 1042package is only activated if all its dependencies have been).
fdc76236
TT
1043
1044Its format is a list of lists. The @code{car} of each sub-list is the
1045name of a package, as a symbol. The @code{cadr} of each sub-list is
1046the minimum acceptable version number, as a string. For instance:
1047
1048@smallexample
1049;; Package-Requires: ((gnus "1.0") (bubbles "2.7.2"))
1050@end smallexample
1051
1052The package code automatically defines a package named @samp{emacs}
1053with the version number of the currently running Emacs. This can be
1054used to require a minimal version of Emacs for a package.
b8d4c8d0
GM
1055@end table
1056
1057 Just about every Lisp library ought to have the @samp{Author} and
1058@samp{Keywords} header comment lines. Use the others if they are
1059appropriate. You can also put in header lines with other header
1060names---they have no standard meanings, so they can't do any harm.
1061
1062 We use additional stylized comments to subdivide the contents of the
b3134b95
GM
1063library file. These should be separated from anything else by blank
1064lines. Here is a table of them:
b8d4c8d0 1065
9800c5b0 1066@cindex commentary, in a Lisp library
b8d4c8d0
GM
1067@table @samp
1068@item ;;; Commentary:
1069This begins introductory comments that explain how the library works.
1070It should come right after the copying permissions, terminated by a
1071@samp{Change Log}, @samp{History} or @samp{Code} comment line. This
1072text is used by the Finder package, so it should make sense in that
1073context.
1074
b8d4c8d0 1075@item ;;; Change Log:
b3134b95
GM
1076This begins an optional log of changes to the file over time. Don't
1077put too much information in this section---it is better to keep the
1078detailed logs in a separate @file{ChangeLog} file (as Emacs does),
1079and/or to use a version control system. @samp{History} is an
1080alternative to @samp{Change Log}.
b8d4c8d0
GM
1081
1082@item ;;; Code:
1083This begins the actual code of the program.
1084
1085@item ;;; @var{filename} ends here
1086This is the @dfn{footer line}; it appears at the very end of the file.
1087Its purpose is to enable people to detect truncated versions of the file
1088from the lack of a footer line.
1089@end table