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