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