2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2015 Free Software
5 @c See the file elisp.texi for copying conditions.
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.
15 The contents of a documentation string should follow certain
16 conventions. In particular, its first line should be a complete
17 sentence (or two complete sentences) that briefly describes what the
18 function or variable does. @xref{Documentation Tips}, for how to
19 write good documentation strings.
21 Note that the documentation strings for Emacs are not the same thing
22 as the Emacs manual. Manuals have their own source files, written in
23 the Texinfo language; documentation strings are specified in the
24 definitions of the functions and variables they apply to. A collection
25 of documentation strings is not sufficient as a manual because a good
26 manual is not organized in that fashion; it is organized in terms of
29 For commands to display documentation strings, see @ref{Help, ,
30 Help, emacs, The GNU Emacs Manual}.
33 * Documentation Basics:: Where doc strings are defined and stored.
34 * Accessing Documentation:: How Lisp programs can access doc strings.
35 * Keys in Documentation:: Substituting current key bindings.
36 * Describing Characters:: Making printable descriptions of
37 non-printing characters and key sequences.
38 * Help Functions:: Subroutines used by Emacs help facilities.
41 @node Documentation Basics
42 @section Documentation Basics
43 @cindex documentation conventions
44 @cindex writing a documentation string
45 @cindex string, writing a doc string
47 A documentation string is written using the Lisp syntax for strings,
48 with double-quote characters surrounding the text. It is, in fact, an
49 actual Lisp string. When the string appears in the proper place in a
50 function or variable definition, it serves as the function's or
51 variable's documentation.
53 @cindex @code{function-documentation} property
54 In a function definition (a @code{lambda} or @code{defun} form), the
55 documentation string is specified after the argument list, and is
56 normally stored directly in the function object. @xref{Function
57 Documentation}. You can also put function documentation in the
58 @code{function-documentation} property of a function name
59 (@pxref{Accessing Documentation}).
61 @cindex @code{variable-documentation} property
62 In a variable definition (a @code{defvar} form), the documentation
63 string is specified after the initial value. @xref{Defining
64 Variables}. The string is stored in the variable's
65 @code{variable-documentation} property.
67 @cindex @file{DOC} (documentation) file
68 Sometimes, Emacs does not keep documentation strings in memory.
69 There are two such circumstances. Firstly, to save memory, the
70 documentation for preloaded functions and variables (including
71 primitives) is kept in a file named @file{DOC}, in the directory
72 specified by @code{doc-directory} (@pxref{Accessing Documentation}).
73 Secondly, when a function or variable is loaded from a byte-compiled
74 file, Emacs avoids loading its documentation string (@pxref{Docs and
75 Compilation}). In both cases, Emacs looks up the documentation string
76 from the file only when needed, such as when the user calls @kbd{C-h
77 f} (@code{describe-function}) for a function.
79 Documentation strings can contain special @dfn{key substitution
80 sequences}, referring to key bindings which are looked up only when
81 the user views the documentation. This allows the help commands to
82 display the correct keys even if a user rearranges the default key
83 bindings. @xref{Keys in Documentation}.
85 In the documentation string of an autoloaded command
86 (@pxref{Autoload}), these key-substitution sequences have an
87 additional special effect: they cause @kbd{C-h f} on the command to
88 trigger autoloading. (This is needed for correctly setting up the
89 hyperlinks in the @file{*Help*} buffer.)
91 @node Accessing Documentation
92 @section Access to Documentation Strings
93 @cindex accessing documentation strings
95 @defun documentation-property symbol property &optional verbatim
96 This function returns the documentation string recorded in
97 @var{symbol}'s property list under property @var{property}. It is
98 most often used to look up the documentation strings of variables, for
99 which @var{property} is @code{variable-documentation}. However, it
100 can also be used to look up other kinds of documentation, such as for
101 customization groups (but for function documentation, use the
102 @code{documentation} function, below).
104 If the property value refers to a documentation string stored in the
105 @file{DOC} file or a byte-compiled file, this function looks up that
106 string and returns it.
108 If the property value isn't @code{nil}, isn't a string, and doesn't
109 refer to text in a file, then it is evaluated as a Lisp expression to
112 Finally, this function passes the string through
113 @code{substitute-command-keys} to substitute key bindings (@pxref{Keys
114 in Documentation}). It skips this step if @var{verbatim} is
119 (documentation-property 'command-line-processed
120 'variable-documentation)
121 @result{} "Non-nil once command line has been processed"
124 (symbol-plist 'command-line-processed)
125 @result{} (variable-documentation 188902)
128 (documentation-property 'emacs 'group-documentation)
129 @result{} "Customization of the One True Editor."
134 @defun documentation function &optional verbatim
135 This function returns the documentation string of @var{function}. It
136 handles macros, named keyboard macros, and special forms, as well as
139 If @var{function} is a symbol, this function first looks for the
140 @code{function-documentation} property of that symbol; if that has a
141 non-@code{nil} value, the documentation comes from that value (if the
142 value is not a string, it is evaluated).
144 If @var{function} is not a symbol, or if it has no
145 @code{function-documentation} property, then @code{documentation}
146 extracts the documentation string from the actual function definition,
147 reading it from a file if called for.
149 Finally, unless @var{verbatim} is non-@code{nil}, this function calls
150 @code{substitute-command-keys}. The result is the documentation
153 The @code{documentation} function signals a @code{void-function} error
154 if @var{function} has no function definition. However, it is OK if
155 the function definition has no documentation string. In that case,
156 @code{documentation} returns @code{nil}.
159 @defun face-documentation face
160 This function returns the documentation string of @var{face} as a
164 Here is an example of using the two functions, @code{documentation} and
165 @code{documentation-property}, to display the documentation strings for
166 several symbols in a @file{*Help*} buffer.
168 @anchor{describe-symbols example}
171 (defun describe-symbols (pattern)
172 "Describe the Emacs Lisp symbols matching PATTERN.
173 All symbols that have PATTERN in their name are described
174 in the *Help* buffer."
175 (interactive "sDescribe symbols matching: ")
181 ;; @r{Print description of symbol.}
182 (if (fboundp s) ; @r{It is a function.}
184 (format "%s\t%s\n%s\n\n" s
186 (let ((keys (where-is-internal s)))
190 (mapconcat 'key-description
196 (or (documentation s)
199 (if (boundp s) ; @r{It is a variable.}
203 (format "%s\t%s\n%s\n\n" s
204 (if (custom-variable-p s)
205 "Option " "Variable")
208 (or (documentation-property
209 s 'variable-documentation)
210 "not documented")))))))
215 ;; @r{Build a list of symbols that match pattern.}
218 (if (string-match pattern (symbol-name sym))
219 (setq sym-list (cons sym sym-list))))))
223 ;; @r{Display the data.}
224 (help-setup-xref (list 'describe-symbols pattern) (interactive-p))
225 (with-help-window (help-buffer)
226 (mapcar describe-func (sort sym-list 'string<)))))
230 The @code{describe-symbols} function works like @code{apropos},
231 but provides more information.
235 (describe-symbols "goal")
237 ---------- Buffer: *Help* ----------
239 Semipermanent goal column for vertical motion, as set by @dots{}
241 @c Do not blithely break or fill these lines.
242 @c That makes them incorrect.
245 set-goal-column Keys: C-x C-n
246 Set the current horizontal position as a goal for C-n and C-p.
248 @c DO NOT put a blank line here! That is factually inaccurate!
250 Those commands will move to this position in the line moved to
251 rather than trying to keep the same horizontal position.
252 With a non-nil argument, clears out the goal column
253 so that C-n and C-p resume vertical motion.
254 The goal column is stored in the variable `goal-column'.
258 temporary-goal-column Variable
259 Current goal column for vertical motion.
260 It is the column where point was
261 at the start of current run of vertical motion commands.
262 When the `track-eol' feature is doing its job, the value is 9999.
263 ---------- Buffer: *Help* ----------
267 @anchor{Definition of Snarf-documentation}
268 @defun Snarf-documentation filename
269 This function is used when building Emacs, just before the runnable
270 Emacs is dumped. It finds the positions of the documentation strings
271 stored in the file @var{filename}, and records those positions into
272 memory in the function definitions and variable property lists.
273 @xref{Building Emacs}.
275 Emacs reads the file @var{filename} from the @file{emacs/etc} directory.
276 When the dumped Emacs is later executed, the same file will be looked
277 for in the directory @code{doc-directory}. Usually @var{filename} is
281 @defvar doc-directory
282 This variable holds the name of the directory which should contain the
283 file @code{"DOC"} that contains documentation strings for
284 built-in and preloaded functions and variables.
286 In most cases, this is the same as @code{data-directory}. They may be
287 different when you run Emacs from the directory where you built it,
288 without actually installing it. @xref{Definition of data-directory}.
291 @node Keys in Documentation
292 @section Substituting Key Bindings in Documentation
293 @cindex documentation, keys in
294 @cindex keys in documentation strings
295 @cindex substituting keys in documentation
296 @cindex key substitution sequence
298 When documentation strings refer to key sequences, they should use the
299 current, actual key bindings. They can do so using certain special text
300 sequences described below. Accessing documentation strings in the usual
301 way substitutes current key binding information for these special
302 sequences. This works by calling @code{substitute-command-keys}. You
303 can also call that function yourself.
305 Here is a list of the special sequences and what they mean:
308 @item \[@var{command}]
309 stands for a key sequence that will invoke @var{command}, or @samp{M-x
310 @var{command}} if @var{command} has no key bindings.
312 @item \@{@var{mapvar}@}
313 stands for a summary of the keymap which is the value of the variable
314 @var{mapvar}. The summary is made using @code{describe-bindings}.
316 @item \<@var{mapvar}>
317 stands for no text itself. It is used only for a side effect: it
318 specifies @var{mapvar}'s value as the keymap for any following
319 @samp{\[@var{command}]} sequences in this documentation string.
322 (grave accent) stands for a left quote, and alters the interpretation
323 of the next unmatched apostrophe.
326 (apostrophe) stands for a right quote if preceded by grave accent and
327 there are no intervening apostrophes. Otherwise, apostrophe stands
331 (left single quotation mark) stands for a left quote.
334 (right single quotation mark) stands for a right quote.
337 (apostrophe) stands for a right quote if
338 preceded by grave accent and there are no intervening apostrophes.
339 Otherwise, apostrophe stands for itself.
342 quotes the following character and is discarded; thus, @samp{\=`} puts
343 @samp{`} into the output, @samp{\=\[} puts @samp{\[} into the output,
344 and @samp{\=\=} puts @samp{\=} into the output.
347 @strong{Please note:} Each @samp{\} must be doubled when written in a
348 string in Emacs Lisp.
350 @defvar text-quoting-style
351 @cindex curved quotes
353 The value of this variable specifies the style
354 @code{substitute-command-keys} uses when generating left and right
355 quotes. If the variable's value is @code{curve}, the style is
356 @t{‘like this’} with curved single quotes. If the value is
357 @code{straight}, the style is @t{'like this'} with straight
358 apostrophes. If the value is @code{grave}, the style is @t{`like
359 this'} with grave accent and apostrophe. The default value @code{nil}
360 acts like @code{curve} if curved single quotes are displayable, and
361 like @code{grave} otherwise.
364 @defun substitute-command-keys string
365 This function scans @var{string} for the above special sequences and
366 replaces them by what they stand for, returning the result as a string.
367 This permits display of documentation that refers accurately to the
368 user's own customized key bindings.
370 @cindex advertised binding
371 If a command has multiple bindings, this function normally uses the
372 first one it finds. You can specify one particular key binding by
373 assigning an @code{:advertised-binding} symbol property to the
377 (put 'undo :advertised-binding [?\C-/])
381 The @code{:advertised-binding} property also affects the binding shown
382 in menu items (@pxref{Menu Bar}). The property is ignored if it
383 specifies a key binding that the command does not actually have.
386 Here are examples of the special sequences:
390 (substitute-command-keys
391 "To abort recursive edit, type ‘\\[abort-recursive-edit]’.")
392 @result{} "To abort recursive edit, type ‘C-]’."
396 (substitute-command-keys
397 "The keys that are defined for the minibuffer here are:
398 \\@{minibuffer-local-must-match-map@}")
399 @result{} "The keys that are defined for the minibuffer here are:
402 ? minibuffer-completion-help
403 SPC minibuffer-complete-word
404 TAB minibuffer-complete
405 C-j minibuffer-complete-and-exit
406 RET minibuffer-complete-and-exit
407 C-g abort-recursive-edit
411 (substitute-command-keys
412 "To abort a recursive edit from the minibuffer, type\
413 `\\<minibuffer-local-must-match-map>\\[abort-recursive-edit]'.")
414 @result{} "To abort a recursive edit from the minibuffer, type ‘C-g’."
418 There are other special conventions for the text in documentation
419 strings---for instance, you can refer to functions, variables, and
420 sections of this manual. @xref{Documentation Tips}, for details.
422 @node Describing Characters
423 @section Describing Characters for Help Messages
424 @cindex describe characters and events
426 These functions convert events, key sequences, or characters to
427 textual descriptions. These descriptions are useful for including
428 arbitrary text characters or key sequences in messages, because they
429 convert non-printing and whitespace characters to sequences of printing
430 characters. The description of a non-whitespace printing character is
431 the character itself.
433 @defun key-description sequence &optional prefix
434 @cindex Emacs event standard notation
435 This function returns a string containing the Emacs standard notation
436 for the input events in @var{sequence}. If @var{prefix} is
437 non-@code{nil}, it is a sequence of input events leading up to
438 @var{sequence} and is included in the return value. Both arguments
439 may be strings, vectors or lists. @xref{Input Events}, for more
440 information about valid events.
444 (key-description [?\M-3 delete])
445 @result{} "M-3 <delete>"
448 (key-description [delete] "\M-3")
449 @result{} "M-3 <delete>"
453 See also the examples for @code{single-key-description}, below.
456 @defun single-key-description event &optional no-angles
457 @cindex event printing
458 @cindex character printing
459 @cindex control character printing
460 @cindex meta character printing
461 This function returns a string describing @var{event} in the standard
462 Emacs notation for keyboard input. A normal printing character
463 appears as itself, but a control character turns into a string
464 starting with @samp{C-}, a meta character turns into a string starting
465 with @samp{M-}, and space, tab, etc., appear as @samp{SPC},
466 @samp{TAB}, etc. A function key symbol appears inside angle brackets
467 @samp{<@dots{}>}. An event that is a list appears as the name of the
468 symbol in the @sc{car} of the list, inside angle brackets.
470 If the optional argument @var{no-angles} is non-@code{nil}, the angle
471 brackets around function keys and event symbols are omitted; this is
472 for compatibility with old versions of Emacs which didn't use the
477 (single-key-description ?\C-x)
481 (key-description "\C-x \M-y \n \t \r \f123")
482 @result{} "C-x SPC M-y SPC C-j SPC TAB SPC RET SPC C-l 1 2 3"
485 (single-key-description 'delete)
489 (single-key-description 'C-mouse-1)
490 @result{} "<C-mouse-1>"
493 (single-key-description 'C-mouse-1 t)
494 @result{} "C-mouse-1"
499 @defun text-char-description character
500 This function returns a string describing @var{character} in the
501 standard Emacs notation for characters that appear in text---like
502 @code{single-key-description}, except that control characters are
503 represented with a leading caret (which is how control characters in
504 Emacs buffers are usually displayed). Another difference is that
505 @code{text-char-description} recognizes the 2**7 bit as the Meta
506 character, whereas @code{single-key-description} uses the 2**27 bit
511 (text-char-description ?\C-c)
515 (text-char-description ?\M-m)
519 (text-char-description ?\C-\M-m)
523 (text-char-description (+ 128 ?m))
527 (text-char-description (+ 128 ?\C-m))
533 @deffn Command read-kbd-macro string &optional need-vector
534 This function is used mainly for operating on keyboard macros, but it
535 can also be used as a rough inverse for @code{key-description}. You
536 call it with a string containing key descriptions, separated by spaces;
537 it returns a string or vector containing the corresponding events.
538 (This may or may not be a single valid key sequence, depending on what
539 events you use; @pxref{Key Sequences}.) If @var{need-vector} is
540 non-@code{nil}, the return value is always a vector.
544 @section Help Functions
545 @cindex help functions
547 Emacs provides a variety of built-in help functions, all accessible to
548 the user as subcommands of the prefix @kbd{C-h}. For more information
549 about them, see @ref{Help, , Help, emacs, The GNU Emacs Manual}. Here
550 we describe some program-level interfaces to the same information.
552 @deffn Command apropos pattern &optional do-all
553 This function finds all ``meaningful'' symbols whose names contain a
554 match for the apropos pattern @var{pattern}. An apropos pattern is
555 either a word to match, a space-separated list of words of which at
556 least two must match, or a regular expression (if any special regular
557 expression characters occur). A symbol is ``meaningful'' if it has a
558 definition as a function, variable, or face, or has properties.
560 The function returns a list of elements that look like this:
563 (@var{symbol} @var{score} @var{function-doc} @var{variable-doc}
564 @var{plist-doc} @var{widget-doc} @var{face-doc} @var{group-doc})
567 Here, @var{score} is an integer measure of how important the symbol
568 seems to be as a match. Each of the remaining elements is a
569 documentation string, or @code{nil}, for @var{symbol} as a function,
572 It also displays the symbols in a buffer named @file{*Apropos*}, each
573 with a one-line description taken from the beginning of its
574 documentation string.
576 If @var{do-all} is non-@code{nil}, or if the user option
577 @code{apropos-do-all} is non-@code{nil}, then @code{apropos} also
578 shows key bindings for the functions that are found; it also shows
579 @emph{all} interned symbols, not just meaningful ones (and it lists
580 them in the return value as well).
584 The value of this variable is a local keymap for characters following the
588 @deffn {Prefix Command} help-command
589 This symbol is not a function; its function definition cell holds the
590 keymap known as @code{help-map}. It is defined in @file{help.el} as
595 (define-key global-map (string help-char) 'help-command)
596 (fset 'help-command help-map)
602 The value of this variable is the help character---the character that
603 Emacs recognizes as meaning Help. By default, its value is 8, which
604 stands for @kbd{C-h}. When Emacs reads this character, if
605 @code{help-form} is a non-@code{nil} Lisp expression, it evaluates that
606 expression, and displays the result in a window if it is a string.
608 Usually the value of @code{help-form} is @code{nil}. Then the
609 help character has no special meaning at the level of command input, and
610 it becomes part of a key sequence in the normal way. The standard key
611 binding of @kbd{C-h} is a prefix key for several general-purpose help
614 The help character is special after prefix keys, too. If it has no
615 binding as a subcommand of the prefix key, it runs
616 @code{describe-prefix-bindings}, which displays a list of all the
617 subcommands of the prefix key.
620 @defopt help-event-list
621 The value of this variable is a list of event types that serve as
622 alternative ``help characters''. These events are handled just like the
623 event specified by @code{help-char}.
627 If this variable is non-@code{nil}, its value is a form to evaluate
628 whenever the character @code{help-char} is read. If evaluating the form
629 produces a string, that string is displayed.
631 A command that calls @code{read-event}, @code{read-char-choice}, or
632 @code{read-char} probably should bind @code{help-form} to a
633 non-@code{nil} expression while it does input. (The time when you
634 should not do this is when @kbd{C-h} has some other meaning.)
635 Evaluating this expression should result in a string that explains
636 what the input is for and how to enter it properly.
638 Entry to the minibuffer binds this variable to the value of
639 @code{minibuffer-help-form} (@pxref{Definition of minibuffer-help-form}).
642 @defvar prefix-help-command
643 This variable holds a function to print help for a prefix key. The
644 function is called when the user types a prefix key followed by the help
645 character, and the help character has no binding after that prefix. The
646 variable's default value is @code{describe-prefix-bindings}.
649 @deffn Command describe-prefix-bindings
650 This function calls @code{describe-bindings} to display a list of all
651 the subcommands of the prefix key of the most recent key sequence. The
652 prefix described consists of all but the last event of that key
653 sequence. (The last event is, presumably, the help character.)
656 The following two functions are meant for modes that want to provide
657 help without relinquishing control, such as the ``electric'' modes.
658 Their names begin with @samp{Helper} to distinguish them from the
659 ordinary help functions.
661 @deffn Command Helper-describe-bindings
662 This command pops up a window displaying a help buffer containing a
663 listing of all of the key bindings from both the local and global keymaps.
664 It works by calling @code{describe-bindings}.
667 @deffn Command Helper-help
668 This command provides help for the current mode. It prompts the user
669 in the minibuffer with the message @samp{Help (Type ? for further
670 options)}, and then provides assistance in finding out what the key
671 bindings are, and what the mode is intended for. It returns @code{nil}.
673 @vindex Helper-help-map
674 This can be customized by changing the map @code{Helper-help-map}.
677 @defvar data-directory
678 @anchor{Definition of data-directory}
679 This variable holds the name of the directory in which Emacs finds
680 certain documentation and text files that come with Emacs.
684 This function returns the name of the help buffer, which is normally
685 @file{*Help*}; if such a buffer does not exist, it is first created.
688 @vindex help-window-select
689 @defmac with-help-window buffer-name body@dots{}
690 This macro evaluates @var{body} like @code{with-output-to-temp-buffer}
691 (@pxref{Temporary Displays}), inserting any output produced by its forms
692 into a buffer named @var{buffer-name}. (Usually, @var{buffer-name}
693 should be the value returned by the function @code{help-buffer}.) It
694 also puts the specified buffer into Help mode and displays a message
695 telling the user how to quit and scroll the help window. It selects the
696 help window if the current value of the user option
697 @code{help-window-select} has been set accordingly. It returns the last
701 @defun help-setup-xref item interactive-p
702 This function updates the cross reference data in the @file{*Help*}
703 buffer, which is used to regenerate the help information when the user
704 clicks on the @samp{Back} or @samp{Forward} buttons. Most commands
705 that use the @file{*Help*} buffer should invoke this function before
706 clearing the buffer. The @var{item} argument should have the form
707 @code{(@var{function} . @var{args})}, where @var{function} is a function
708 to call, with argument list @var{args}, to regenerate the help buffer.
709 The @var{interactive-p} argument is non-@code{nil} if the calling
710 command was invoked interactively; in that case, the stack of items
711 for the @file{*Help*} buffer's @samp{Back} buttons is cleared.
714 @xref{describe-symbols example}, for an example of using
715 @code{help-buffer}, @code{with-help-window}, and
716 @code{help-setup-xref}.
718 @defmac make-help-screen fname help-line help-text help-map
719 This macro defines a help command named @var{fname} that acts like a
720 prefix key that shows a list of the subcommands it offers.
722 When invoked, @var{fname} displays @var{help-text} in a window, then
723 reads and executes a key sequence according to @var{help-map}. The
724 string @var{help-text} should describe the bindings available in
727 The command @var{fname} is defined to handle a few events itself, by
728 scrolling the display of @var{help-text}. When @var{fname} reads one of
729 those special events, it does the scrolling and then reads another
730 event. When it reads an event that is not one of those few, and which
731 has a binding in @var{help-map}, it executes that key's binding and
734 The argument @var{help-line} should be a single-line summary of the
735 alternatives in @var{help-map}. In the current version of Emacs, this
736 argument is used only if you set the option @code{three-step-help} to
739 This macro is used in the command @code{help-for-help} which is the
740 binding of @kbd{C-h C-h}.
743 @defopt three-step-help
744 If this variable is non-@code{nil}, commands defined with
745 @code{make-help-screen} display their @var{help-line} strings in the
746 echo area at first, and display the longer @var{help-text} strings only
747 if the user types the help character again.