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