tips.texi 40 KB

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