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