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