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