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