2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2012
4 @c Free Software Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @node Documentation, Files, Modes, Top
8 @cindex documentation strings
10 GNU Emacs has convenient built-in help facilities, most of which
11 derive their information from documentation strings associated with
12 functions and variables. This chapter describes how to access
13 documentation strings in Lisp programs. @xref{Documentation Tips},
14 for how to write good documentation strings.
16 Note that the documentation strings for Emacs are not the same thing
17 as the Emacs manual. Manuals have their own source files, written in
18 the Texinfo language; documentation strings are specified in the
19 definitions of the functions and variables they apply to. A collection
20 of documentation strings is not sufficient as a manual because a good
21 manual is not organized in that fashion; it is organized in terms of
24 For commands to display documentation strings, see @ref{Help, ,
25 Help, emacs, The GNU Emacs Manual}.
28 * Documentation Basics:: Where doc strings are defined and stored.
29 * Accessing Documentation:: How Lisp programs can access doc strings.
30 * Keys in Documentation:: Substituting current key bindings.
31 * Describing Characters:: Making printable descriptions of
32 non-printing characters and key sequences.
33 * Help Functions:: Subroutines used by Emacs help facilities.
36 @node Documentation Basics
37 @comment node-name, next, previous, up
38 @section Documentation Basics
39 @cindex documentation conventions
40 @cindex writing a documentation string
41 @cindex string, writing a doc string
43 A documentation string is written using the Lisp syntax for strings,
44 with double-quote characters surrounding the text of the string. This
45 is because it really is a Lisp string object. The string serves as
46 documentation when it is written in the proper place in the definition
47 of a function or variable. In a function definition, the documentation
48 string follows the argument list. In a variable definition, the
49 documentation string follows the initial value of the variable.
51 When you write a documentation string, make the first line a
52 complete sentence (or two complete sentences) that briefly describes
53 what the function or variable does. Some commands, such as
54 @code{apropos}, show only the first line of a multi-line documentation
55 string. Also, you should not indent the second line of a
56 documentation string, if it has one, because that looks odd when you
57 use @kbd{C-h f} (@code{describe-function}) or @kbd{C-h v}
58 (@code{describe-variable}) to view the documentation string. There
59 are many other conventions for documentation strings; see
60 @ref{Documentation Tips}.
62 Documentation strings can contain several special substrings, which
63 stand for key bindings to be looked up in the current keymaps when the
64 documentation is displayed. This allows documentation strings to refer
65 to the keys for related commands and be accurate even when a user
66 rearranges the key bindings. (@xref{Keys in Documentation}.)
68 @vindex emacs-lisp-docstring-fill-column
69 Emacs Lisp mode fills documentation strings to the width
70 specified by @code{emacs-lisp-docstring-fill-column}.
72 Exactly where a documentation string is stored depends on how its
73 function or variable was defined or loaded into memory:
77 @kindex function-documentation
78 When you define a function (@pxref{Lambda Expressions}, and
79 @pxref{Function Documentation}), the documentation string is stored in
80 the function definition itself. You can also put function
81 documentation in the @code{function-documentation} property of a
82 function name. That is useful for function definitions which can't
83 hold a documentation string, such as keyboard macros.
86 @kindex variable-documentation
87 When you define a variable with a @code{defvar} or related form
88 (@pxref{Defining Variables}), the documentation is stored in the
89 variable's @code{variable-documentation} property.
91 @cindex @file{DOC-@var{version}} (documentation) file
93 To save memory, the documentation for preloaded functions and
94 variables (including primitive functions and autoloaded functions) is
95 not kept in memory, but in the file
96 @file{emacs/etc/DOC-@var{version}}, where @var{version} is the Emacs
97 version number (@pxref{Version Info}).
100 When a function or variable is loaded from a byte-compiled file during
101 the Emacs session, its documentation string is not loaded into memory.
102 Instead, Emacs looks it up in the byte-compiled file as needed.
103 @xref{Docs and Compilation}.
107 Regardless of where the documentation string is stored, you can
108 retrieve it using the @code{documentation} or
109 @code{documentation-property} function, described in the next section.
111 @node Accessing Documentation
112 @section Access to Documentation Strings
114 @defun documentation-property symbol property &optional verbatim
115 This function returns the documentation string recorded in
116 @var{symbol}'s property list under property @var{property}. It is
117 most often used to look up the documentation strings of variables, for
118 which @var{property} is @code{variable-documentation}. However, it
119 can also be used to look up other kinds of documentation, such as for
120 customization groups (but for function documentation, use the
121 @code{documentation} command, below).
123 If the value recorded in the property list refers to a documentation
124 string stored in a @file{DOC-@var{version}} file or a byte-compiled
125 file, it looks up that string and returns it. If the property value
126 isn't @code{nil}, isn't a string, and doesn't refer to text in a file,
127 then it is evaluated as a Lisp expression to obtain a string.
129 The last thing this function does is pass the string through
130 @code{substitute-command-keys} to substitute actual key bindings
131 (@pxref{Keys in Documentation}). However, it skips this step if
132 @var{verbatim} is non-@code{nil}.
136 (documentation-property 'command-line-processed
137 'variable-documentation)
138 @result{} "Non-nil once command line has been processed"
141 (symbol-plist 'command-line-processed)
142 @result{} (variable-documentation 188902)
145 (documentation-property 'emacs 'group-documentation)
146 @result{} "Customization of the One True Editor."
151 @defun documentation function &optional verbatim
152 This function returns the documentation string of @var{function}. It
153 handles macros, named keyboard macros, and special forms, as well as
156 If @var{function} is a symbol, this function first looks for the
157 @code{function-documentation} property of that symbol; if that has a
158 non-@code{nil} value, the documentation comes from that value (if the
159 value is not a string, it is evaluated). If @var{function} is not a
160 symbol, or if it has no @code{function-documentation} property, then
161 @code{documentation} extracts the documentation string from the actual
162 function definition, reading it from a file if called for.
164 Finally, unless @var{verbatim} is non-@code{nil}, it calls
165 @code{substitute-command-keys} so as to return a value containing the
166 actual (current) key bindings.
168 The function @code{documentation} signals a @code{void-function} error
169 if @var{function} has no function definition. However, it is OK if
170 the function definition has no documentation string. In that case,
171 @code{documentation} returns @code{nil}.
174 @defun face-documentation face
175 This function returns the documentation string of @var{face} as a
179 @c Wordy to prevent overfull hboxes. --rjc 15mar92
180 Here is an example of using the two functions, @code{documentation} and
181 @code{documentation-property}, to display the documentation strings for
182 several symbols in a @file{*Help*} buffer.
184 @anchor{describe-symbols example}
187 (defun describe-symbols (pattern)
188 "Describe the Emacs Lisp symbols matching PATTERN.
189 All symbols that have PATTERN in their name are described
190 in the `*Help*' buffer."
191 (interactive "sDescribe symbols matching: ")
197 ;; @r{Print description of symbol.}
198 (if (fboundp s) ; @r{It is a function.}
200 (format "%s\t%s\n%s\n\n" s
202 (let ((keys (where-is-internal s)))
206 (mapconcat 'key-description
212 (or (documentation s)
215 (if (boundp s) ; @r{It is a variable.}
219 (format "%s\t%s\n%s\n\n" s
220 (if (custom-variable-p s)
221 "Option " "Variable")
224 (or (documentation-property
225 s 'variable-documentation)
226 "not documented")))))))
231 ;; @r{Build a list of symbols that match pattern.}
234 (if (string-match pattern (symbol-name sym))
235 (setq sym-list (cons sym sym-list))))))
239 ;; @r{Display the data.}
240 (help-setup-xref (list 'describe-symbols pattern) (interactive-p))
241 (with-help-window (help-buffer)
242 (mapcar describe-func (sort sym-list 'string<)))))
246 The @code{describe-symbols} function works like @code{apropos},
247 but provides more information.
251 (describe-symbols "goal")
253 ---------- Buffer: *Help* ----------
255 Semipermanent goal column for vertical motion, as set by @dots{}
257 @c Do not blithely break or fill these lines.
258 @c That makes them incorrect.
261 set-goal-column Keys: C-x C-n
262 Set the current horizontal position as a goal for C-n and C-p.
264 @c DO NOT put a blank line here! That is factually inaccurate!
266 Those commands will move to this position in the line moved to
267 rather than trying to keep the same horizontal position.
268 With a non-nil argument, clears out the goal column
269 so that C-n and C-p resume vertical motion.
270 The goal column is stored in the variable `goal-column'.
274 temporary-goal-column Variable
275 Current goal column for vertical motion.
276 It is the column where point was
277 at the start of current run of vertical motion commands.
278 When the `track-eol' feature is doing its job, the value is 9999.
279 ---------- Buffer: *Help* ----------
283 @anchor{Definition of Snarf-documentation}
284 @defun Snarf-documentation filename
285 This function is used when building Emacs, just before the runnable
286 Emacs is dumped. It finds the positions of the documentation strings
287 stored in the file @var{filename}, and records those positions into
288 memory in the function definitions and variable property lists.
289 @xref{Building Emacs}.
291 Emacs reads the file @var{filename} from the @file{emacs/etc} directory.
292 When the dumped Emacs is later executed, the same file will be looked
293 for in the directory @code{doc-directory}. Usually @var{filename} is
294 @code{"DOC-@var{version}"}.
297 @defvar doc-directory
298 This variable holds the name of the directory which should contain the
299 file @code{"DOC-@var{version}"} that contains documentation strings for
300 built-in and preloaded functions and variables.
302 In most cases, this is the same as @code{data-directory}. They may be
303 different when you run Emacs from the directory where you built it,
304 without actually installing it. @xref{Definition of data-directory}.
307 @node Keys in Documentation
308 @section Substituting Key Bindings in Documentation
309 @cindex documentation, keys in
310 @cindex keys in documentation strings
311 @cindex substituting keys in documentation
313 When documentation strings refer to key sequences, they should use the
314 current, actual key bindings. They can do so using certain special text
315 sequences described below. Accessing documentation strings in the usual
316 way substitutes current key binding information for these special
317 sequences. This works by calling @code{substitute-command-keys}. You
318 can also call that function yourself.
320 Here is a list of the special sequences and what they mean:
323 @item \[@var{command}]
324 stands for a key sequence that will invoke @var{command}, or @samp{M-x
325 @var{command}} if @var{command} has no key bindings.
327 @item \@{@var{mapvar}@}
328 stands for a summary of the keymap which is the value of the variable
329 @var{mapvar}. The summary is made using @code{describe-bindings}.
331 @item \<@var{mapvar}>
332 stands for no text itself. It is used only for a side effect: it
333 specifies @var{mapvar}'s value as the keymap for any following
334 @samp{\[@var{command}]} sequences in this documentation string.
337 quotes the following character and is discarded; thus, @samp{\=\[} puts
338 @samp{\[} into the output, and @samp{\=\=} puts @samp{\=} into the
342 @strong{Please note:} Each @samp{\} must be doubled when written in a
343 string in Emacs Lisp.
345 @defun substitute-command-keys string
346 This function scans @var{string} for the above special sequences and
347 replaces them by what they stand for, returning the result as a string.
348 This permits display of documentation that refers accurately to the
349 user's own customized key bindings.
351 @cindex advertised binding
352 If a command has multiple bindings, this function normally uses the
353 first one it finds. You can specify one particular key binding by
354 assigning an @code{:advertised-binding} symbol property to the
358 (put 'undo :advertised-binding [?\C-/])
362 The @code{:advertised-binding} property also affects the binding shown
363 in menu items (@pxref{Menu Bar}). The property is ignored if it
364 specifies a key binding that the command does not actually have.
367 Here are examples of the special sequences:
371 (substitute-command-keys
372 "To abort recursive edit, type: \\[abort-recursive-edit]")
373 @result{} "To abort recursive edit, type: C-]"
377 (substitute-command-keys
378 "The keys that are defined for the minibuffer here are:
379 \\@{minibuffer-local-must-match-map@}")
380 @result{} "The keys that are defined for the minibuffer here are:
383 ? minibuffer-completion-help
384 SPC minibuffer-complete-word
385 TAB minibuffer-complete
386 C-j minibuffer-complete-and-exit
387 RET minibuffer-complete-and-exit
388 C-g abort-recursive-edit
392 (substitute-command-keys
393 "To abort a recursive edit from the minibuffer, type\
394 \\<minibuffer-local-must-match-map>\\[abort-recursive-edit].")
395 @result{} "To abort a recursive edit from the minibuffer, type C-g."
399 There are other special conventions for the text in documentation
400 strings---for instance, you can refer to functions, variables, and
401 sections of this manual. @xref{Documentation Tips}, for details.
403 @node Describing Characters
404 @section Describing Characters for Help Messages
405 @cindex describe characters and events
407 These functions convert events, key sequences, or characters to
408 textual descriptions. These descriptions are useful for including
409 arbitrary text characters or key sequences in messages, because they
410 convert non-printing and whitespace characters to sequences of printing
411 characters. The description of a non-whitespace printing character is
412 the character itself.
414 @defun key-description sequence &optional prefix
415 @cindex Emacs event standard notation
416 This function returns a string containing the Emacs standard notation
417 for the input events in @var{sequence}. If @var{prefix} is
418 non-@code{nil}, it is a sequence of input events leading up to
419 @var{sequence} and is included in the return value. Both arguments
420 may be strings, vectors or lists. @xref{Input Events}, for more
421 information about valid events.
425 (key-description [?\M-3 delete])
426 @result{} "M-3 <delete>"
429 (key-description [delete] "\M-3")
430 @result{} "M-3 <delete>"
434 See also the examples for @code{single-key-description}, below.
437 @defun single-key-description event &optional no-angles
438 @cindex event printing
439 @cindex character printing
440 @cindex control character printing
441 @cindex meta character printing
442 This function returns a string describing @var{event} in the standard
443 Emacs notation for keyboard input. A normal printing character
444 appears as itself, but a control character turns into a string
445 starting with @samp{C-}, a meta character turns into a string starting
446 with @samp{M-}, and space, tab, etc.@: appear as @samp{SPC},
447 @samp{TAB}, etc. A function key symbol appears inside angle brackets
448 @samp{<@dots{}>}. An event that is a list appears as the name of the
449 symbol in the @sc{car} of the list, inside angle brackets.
451 If the optional argument @var{no-angles} is non-@code{nil}, the angle
452 brackets around function keys and event symbols are omitted; this is
453 for compatibility with old versions of Emacs which didn't use the
458 (single-key-description ?\C-x)
462 (key-description "\C-x \M-y \n \t \r \f123")
463 @result{} "C-x SPC M-y SPC C-j SPC TAB SPC RET SPC C-l 1 2 3"
466 (single-key-description 'delete)
470 (single-key-description 'C-mouse-1)
471 @result{} "<C-mouse-1>"
474 (single-key-description 'C-mouse-1 t)
475 @result{} "C-mouse-1"
480 @defun text-char-description character
481 This function returns a string describing @var{character} in the
482 standard Emacs notation for characters that appear in text---like
483 @code{single-key-description}, except that control characters are
484 represented with a leading caret (which is how control characters in
485 Emacs buffers are usually displayed). Another difference is that
486 @code{text-char-description} recognizes the 2**7 bit as the Meta
487 character, whereas @code{single-key-description} uses the 2**27 bit
492 (text-char-description ?\C-c)
496 (text-char-description ?\M-m)
500 (text-char-description ?\C-\M-m)
504 (text-char-description (+ 128 ?m))
508 (text-char-description (+ 128 ?\C-m))
514 @defun read-kbd-macro string &optional need-vector
515 This function is used mainly for operating on keyboard macros, but it
516 can also be used as a rough inverse for @code{key-description}. You
517 call it with a string containing key descriptions, separated by spaces;
518 it returns a string or vector containing the corresponding events.
519 (This may or may not be a single valid key sequence, depending on what
520 events you use; @pxref{Key Sequences}.) If @var{need-vector} is
521 non-@code{nil}, the return value is always a vector.
525 @section Help Functions
527 Emacs provides a variety of on-line help functions, all accessible to
528 the user as subcommands of the prefix @kbd{C-h}. For more information
529 about them, see @ref{Help, , Help, emacs, The GNU Emacs Manual}. Here
530 we describe some program-level interfaces to the same information.
532 @deffn Command apropos pattern &optional do-all
533 This function finds all ``meaningful'' symbols whose names contain a
534 match for the apropos pattern @var{pattern}. An apropos pattern is
535 either a word to match, a space-separated list of words of which at
536 least two must match, or a regular expression (if any special regular
537 expression characters occur). A symbol is ``meaningful'' if it has a
538 definition as a function, variable, or face, or has properties.
540 The function returns a list of elements that look like this:
543 (@var{symbol} @var{score} @var{function-doc} @var{variable-doc}
544 @var{plist-doc} @var{widget-doc} @var{face-doc} @var{group-doc})
547 Here, @var{score} is an integer measure of how important the symbol
548 seems to be as a match. Each of the remaining elements is a
549 documentation string, or @code{nil}, for @var{symbol} as a function,
552 It also displays the symbols in a buffer named @file{*Apropos*}, each
553 with a one-line description taken from the beginning of its
554 documentation string.
556 If @var{do-all} is non-@code{nil}, or if the user option
557 @code{apropos-do-all} is non-@code{nil}, then @code{apropos} also
558 shows key bindings for the functions that are found; it also shows
559 @emph{all} interned symbols, not just meaningful ones (and it lists
560 them in the return value as well).
564 The value of this variable is a local keymap for characters following the
568 @deffn {Prefix Command} help-command
569 This symbol is not a function; its function definition cell holds the
570 keymap known as @code{help-map}. It is defined in @file{help.el} as
575 (define-key global-map (string help-char) 'help-command)
576 (fset 'help-command help-map)
582 The value of this variable is the help character---the character that
583 Emacs recognizes as meaning Help. By default, its value is 8, which
584 stands for @kbd{C-h}. When Emacs reads this character, if
585 @code{help-form} is a non-@code{nil} Lisp expression, it evaluates that
586 expression, and displays the result in a window if it is a string.
588 Usually the value of @code{help-form} is @code{nil}. Then the
589 help character has no special meaning at the level of command input, and
590 it becomes part of a key sequence in the normal way. The standard key
591 binding of @kbd{C-h} is a prefix key for several general-purpose help
594 The help character is special after prefix keys, too. If it has no
595 binding as a subcommand of the prefix key, it runs
596 @code{describe-prefix-bindings}, which displays a list of all the
597 subcommands of the prefix key.
600 @defopt help-event-list
601 The value of this variable is a list of event types that serve as
602 alternative ``help characters''. These events are handled just like the
603 event specified by @code{help-char}.
607 If this variable is non-@code{nil}, its value is a form to evaluate
608 whenever the character @code{help-char} is read. If evaluating the form
609 produces a string, that string is displayed.
611 A command that calls @code{read-event}, @code{read-char-choice}, or
612 @code{read-char} probably should bind @code{help-form} to a
613 non-@code{nil} expression while it does input. (The time when you
614 should not do this is when @kbd{C-h} has some other meaning.)
615 Evaluating this expression should result in a string that explains
616 what the input is for and how to enter it properly.
618 Entry to the minibuffer binds this variable to the value of
619 @code{minibuffer-help-form} (@pxref{Definition of minibuffer-help-form}).
622 @defvar prefix-help-command
623 This variable holds a function to print help for a prefix key. The
624 function is called when the user types a prefix key followed by the help
625 character, and the help character has no binding after that prefix. The
626 variable's default value is @code{describe-prefix-bindings}.
629 @defun describe-prefix-bindings
630 This function calls @code{describe-bindings} to display a list of all
631 the subcommands of the prefix key of the most recent key sequence. The
632 prefix described consists of all but the last event of that key
633 sequence. (The last event is, presumably, the help character.)
636 The following two functions are meant for modes that want to provide
637 help without relinquishing control, such as the ``electric'' modes.
638 Their names begin with @samp{Helper} to distinguish them from the
639 ordinary help functions.
641 @deffn Command Helper-describe-bindings
642 This command pops up a window displaying a help buffer containing a
643 listing of all of the key bindings from both the local and global keymaps.
644 It works by calling @code{describe-bindings}.
647 @deffn Command Helper-help
648 This command provides help for the current mode. It prompts the user
649 in the minibuffer with the message @samp{Help (Type ? for further
650 options)}, and then provides assistance in finding out what the key
651 bindings are, and what the mode is intended for. It returns @code{nil}.
653 @vindex Helper-help-map
654 This can be customized by changing the map @code{Helper-help-map}.
657 @defvar data-directory
658 @anchor{Definition of data-directory}
659 This variable holds the name of the directory in which Emacs finds
660 certain documentation and text files that come with Emacs.
664 This function returns the name of the help buffer, which is normally
665 @file{*Help*}; if such a buffer does not exist, it is first created.
668 @defmac with-help-window buffer-name body@dots{}
669 This macro evaluates the @var{body} forms, inserting any output they
670 produce into a buffer named @var{buffer-name} like
671 @code{with-output-to-temp-buffer} (@pxref{Temporary Displays}).
672 (Usually, @var{buffer-name} should be the value returned by the
673 function @code{help-buffer}.) It also puts the specified buffer into
674 Help mode and displays a message telling the user how to quit and
675 scroll the help window.
678 @defun help-setup-xref item interactive-p
679 This function updates the cross reference data in the @file{*Help*}
680 buffer, which is used to regenerate the help information when the user
681 clicks on the @samp{Back} or @samp{Forward} buttons. Most commands
682 that use the @file{*Help*} buffer should invoke this function before
683 clearing the buffer. The @var{item} argument should have the form
684 @code{(@var{function} . @var{args})}, where @var{function} is a function
685 to call, with argument list @var{args}, to regenerate the help buffer.
686 The @var{interactive-p} argument is non-@code{nil} if the calling
687 command was invoked interactively; in that case, the stack of items
688 for the @file{*Help*} buffer's @samp{Back} buttons is cleared.
691 @xref{describe-symbols example}, for an example of using
692 @code{help-buffer}, @code{with-help-window}, and
693 @code{help-setup-xref}.
695 @defmac make-help-screen fname help-line help-text help-map
696 This macro defines a help command named @var{fname} that acts like a
697 prefix key that shows a list of the subcommands it offers.
699 When invoked, @var{fname} displays @var{help-text} in a window, then
700 reads and executes a key sequence according to @var{help-map}. The
701 string @var{help-text} should describe the bindings available in
704 The command @var{fname} is defined to handle a few events itself, by
705 scrolling the display of @var{help-text}. When @var{fname} reads one of
706 those special events, it does the scrolling and then reads another
707 event. When it reads an event that is not one of those few, and which
708 has a binding in @var{help-map}, it executes that key's binding and
711 The argument @var{help-line} should be a single-line summary of the
712 alternatives in @var{help-map}. In the current version of Emacs, this
713 argument is used only if you set the option @code{three-step-help} to
716 This macro is used in the command @code{help-for-help} which is the
717 binding of @kbd{C-h C-h}.
720 @defopt three-step-help
721 If this variable is non-@code{nil}, commands defined with
722 @code{make-help-screen} display their @var{help-line} strings in the
723 echo area at first, and display the longer @var{help-text} strings only
724 if the user types the help character again.