tips.texi 41 KB

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