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