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