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