Change
[bpt/emacs.git] / lispref / help.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/help
6 @node Documentation, Files, Modes, Top
7 @chapter Documentation
8 @cindex documentation strings
9
10 GNU Emacs Lisp has convenient on-line help facilities, most of which
11 derive their information from the documentation strings associated with
12 functions and variables. This chapter describes how to write good
13 documentation strings for your Lisp programs, as well as how to write
14 programs to access documentation.
15
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
22 topics of discussion.
23
24 @menu
25 * Documentation Basics:: Good style for doc strings.
26 Where to put them. How Emacs stores them.
27 * Accessing Documentation:: How Lisp programs can access doc strings.
28 * Keys in Documentation:: Substituting current key bindings.
29 * Describing Characters:: Making printable descriptions of
30 non-printing characters and key sequences.
31 * Help Functions:: Subroutines used by Emacs help facilities.
32 @end menu
33
34 @node Documentation Basics
35 @comment node-name, next, previous, up
36 @section Documentation Basics
37 @cindex documentation conventions
38 @cindex writing a documentation string
39 @cindex string, writing a doc string
40
41 A documentation string is written using the Lisp syntax for strings,
42 with double-quote characters surrounding the text of the string. This
43 is because it really is a Lisp string object. The string serves as
44 documentation when it is written in the proper place in the definition
45 of a function or variable. In a function definition, the documentation
46 string follows the argument list. In a variable definition, the
47 documentation string follows the initial value of the variable.
48
49 When you write a documentation string, make the first line a complete
50 sentence (or two complete sentences) since some commands, such as
51 @code{apropos}, show only the first line of a multi-line documentation
52 string. Also, you should not indent the second line of a documentation
53 string, if it has one, because that looks odd when you use @kbd{C-h f}
54 (@code{describe-function}) or @kbd{C-h v} (@code{describe-variable}) to
55 view the documentation string. @xref{Documentation Tips}.
56
57 Documentation strings can contain several special substrings, which
58 stand for key bindings to be looked up in the current keymaps when the
59 documentation is displayed. This allows documentation strings to refer
60 to the keys for related commands and be accurate even when a user
61 rearranges the key bindings. (@xref{Accessing Documentation}.)
62
63 In Emacs Lisp, a documentation string is accessible through the
64 function or variable that it describes:
65
66 @itemize @bullet
67 @item
68 The documentation for a function is stored in the function definition
69 itself (@pxref{Lambda Expressions}). The function @code{documentation}
70 knows how to extract it.
71
72 @item
73 @kindex variable-documentation
74 The documentation for a variable is stored in the variable's property
75 list under the property name @code{variable-documentation}. The
76 function @code{documentation-property} knows how to retrieve it.
77 @end itemize
78
79 @cindex @file{DOC} (documentation) file
80 @cindex @file{emacs/etc/DOC-@var{version}}
81 @cindex @file{etc/DOC-@var{version}}
82 To save space, the documentation for preloaded functions and variables
83 (including primitive functions and autoloaded functions) is stored in
84 the file @file{emacs/etc/DOC-@var{version}}---not inside Emacs. The
85 documentation strings for functions and variables loaded during the
86 Emacs session from byte-compiled files are stored in those files
87 (@pxref{Docs and Compilation}).
88
89 The data structure inside Emacs has an integer offset into the file, or
90 a list containing a file name and an integer, in place of the
91 documentation string. The functions @code{documentation} and
92 @code{documentation-property} use that information to fetch the
93 documentation string from the appropriate file; this is transparent to
94 the user.
95
96 For information on the uses of documentation strings, see @ref{Help, ,
97 Help, emacs, The GNU Emacs Manual}.
98
99 @c Wordy to prevent overfull hbox. --rjc 15mar92
100 The @file{emacs/lib-src} directory contains two utilities that you can
101 use to print nice-looking hardcopy for the file
102 @file{emacs/etc/DOC-@var{version}}. These are @file{sorted-doc} and
103 @file{digest-doc}.
104
105 @node Accessing Documentation
106 @section Access to Documentation Strings
107
108 @defun documentation-property symbol property &optional verbatim
109 This function returns the documentation string that is recorded
110 in @var{symbol}'s property list under property @var{property}. It
111 retrieves the text from a file if necessary, and runs
112 @code{substitute-command-keys} to substitute actual key bindings. (This
113 substitution is not done if @var{verbatim} is non-@code{nil}.)
114
115 @smallexample
116 @group
117 (documentation-property 'command-line-processed
118 'variable-documentation)
119 @result{} "Non-nil once command line has been processed"
120 @end group
121 @group
122 (symbol-plist 'command-line-processed)
123 @result{} (variable-documentation 188902)
124 @end group
125 @end smallexample
126 @end defun
127
128 @defun documentation function &optional verbatim
129 This function returns the documentation string of @var{function}. It
130 reads the text from a file if necessary. Then (unless @var{verbatim} is
131 non-@code{nil}) it calls @code{substitute-command-keys}, to return a
132 value containing the actual (current) key bindings.
133
134 The function @code{documentation} signals a @code{void-function} error
135 if @var{function} has no function definition. However, it is OK if
136 the function definition has no documentation string. In that case,
137 @code{documentation} returns @code{nil}.
138 @end defun
139
140 @c Wordy to prevent overfull hboxes. --rjc 15mar92
141 Here is an example of using the two functions, @code{documentation} and
142 @code{documentation-property}, to display the documentation strings for
143 several symbols in a @samp{*Help*} buffer.
144
145 @smallexample
146 @group
147 (defun describe-symbols (pattern)
148 "Describe the Emacs Lisp symbols matching PATTERN.
149 All symbols that have PATTERN in their name are described
150 in the `*Help*' buffer."
151 (interactive "sDescribe symbols matching: ")
152 (let ((describe-func
153 (function
154 (lambda (s)
155 @end group
156 @group
157 ;; @r{Print description of symbol.}
158 (if (fboundp s) ; @r{It is a function.}
159 (princ
160 (format "%s\t%s\n%s\n\n" s
161 (if (commandp s)
162 (let ((keys (where-is-internal s)))
163 (if keys
164 (concat
165 "Keys: "
166 (mapconcat 'key-description
167 keys " "))
168 "Keys: none"))
169 "Function")
170 @end group
171 @group
172 (or (documentation s)
173 "not documented"))))
174
175 (if (boundp s) ; @r{It is a variable.}
176 @end group
177 @group
178 (princ
179 (format "%s\t%s\n%s\n\n" s
180 (if (user-variable-p s)
181 "Option " "Variable")
182 @end group
183 @group
184 (or (documentation-property
185 s 'variable-documentation)
186 "not documented")))))))
187 sym-list)
188 @end group
189
190 @group
191 ;; @r{Build a list of symbols that match pattern.}
192 (mapatoms (function
193 (lambda (sym)
194 (if (string-match pattern (symbol-name sym))
195 (setq sym-list (cons sym sym-list))))))
196 @end group
197
198 @group
199 ;; @r{Display the data.}
200 (with-output-to-temp-buffer "*Help*"
201 (mapcar describe-func (sort sym-list 'string<))
202 (print-help-return-message))))
203 @end group
204 @end smallexample
205
206 The @code{describe-symbols} function works like @code{apropos},
207 but provides more information.
208
209 @smallexample
210 @group
211 (describe-symbols "goal")
212
213 ---------- Buffer: *Help* ----------
214 goal-column Option
215 *Semipermanent goal column for vertical motion, as set by @dots{}
216 @end group
217 @c Do not blithely break or fill these lines.
218 @c That makes them incorrect.
219
220 @group
221 set-goal-column Keys: C-x C-n
222 Set the current horizontal position as a goal for C-n and C-p.
223 @end group
224 @c DO NOT put a blank line here! That is factually inaccurate!
225 @group
226 Those commands will move to this position in the line moved to
227 rather than trying to keep the same horizontal position.
228 With a non-nil argument, clears out the goal column
229 so that C-n and C-p resume vertical motion.
230 The goal column is stored in the variable `goal-column'.
231 @end group
232
233 @group
234 temporary-goal-column Variable
235 Current goal column for vertical motion.
236 It is the column where point was
237 at the start of current run of vertical motion commands.
238 When the `track-eol' feature is doing its job, the value is 9999.
239 ---------- Buffer: *Help* ----------
240 @end group
241 @end smallexample
242
243 @defun Snarf-documentation filename
244 This function is used only during Emacs initialization, just before
245 the runnable Emacs is dumped. It finds the file offsets of the
246 documentation strings stored in the file @var{filename}, and records
247 them in the in-core function definitions and variable property lists in
248 place of the actual strings. @xref{Building Emacs}.
249
250 Emacs reads the file @var{filename} from the @file{emacs/etc} directory.
251 When the dumped Emacs is later executed, the same file will be looked
252 for in the directory @code{doc-directory}. Usually @var{filename} is
253 @code{"DOC-@var{version}"}.
254 @end defun
255
256 @c Emacs 19 feature
257 @defvar doc-directory
258 This variable holds the name of the directory which should contain the
259 file @code{"DOC-@var{version}"} that contains documentation strings for
260 built-in and preloaded functions and variables.
261
262 In most cases, this is the same as @code{data-directory}. They may be
263 different when you run Emacs from the directory where you built it,
264 without actually installing it. See @code{data-directory} in @ref{Help
265 Functions}.
266
267 In older Emacs versions, @code{exec-directory} was used for this.
268 @end defvar
269
270 @node Keys in Documentation
271 @section Substituting Key Bindings in Documentation
272 @cindex documentation, keys in
273 @cindex keys in documentation strings
274 @cindex substituting keys in documentation
275
276 When documentation strings refer to key sequences, they should use the
277 current, actual key bindings. They can do so using certain special text
278 sequences described below. Accessing documentation strings in the usual
279 way substitutes current key binding information for these special
280 sequences. This works by calling @code{substitute-command-keys}. You
281 can also call that function yourself.
282
283 Here is a list of the special sequences and what they mean:
284
285 @table @code
286 @item \[@var{command}]
287 stands for a key sequence that will invoke @var{command}, or @samp{M-x
288 @var{command}} if @var{command} has no key bindings.
289
290 @item \@{@var{mapvar}@}
291 stands for a summary of the keymap which is the value of the variable
292 @var{mapvar}. The summary is made using @code{describe-bindings}.
293
294 @item \<@var{mapvar}>
295 stands for no text itself. It is used only for a side effect: it
296 specifies @var{mapvar}'s value as the keymap for any following
297 @samp{\[@var{command}]} sequences in this documentation string.
298
299 @item \=
300 quotes the following character and is discarded; thus, @samp{\=\[} puts
301 @samp{\[} into the output, and @samp{\=\=} puts @samp{\=} into the
302 output.
303 @end table
304
305 @strong{Please note:} Each @samp{\} must be doubled when written in a
306 string in Emacs Lisp.
307
308 @defun substitute-command-keys string
309 This function scans @var{string} for the above special sequences and
310 replaces them by what they stand for, returning the result as a string.
311 This permits display of documentation that refers accurately to the
312 user's own customized key bindings.
313 @end defun
314
315 Here are examples of the special sequences:
316
317 @smallexample
318 @group
319 (substitute-command-keys
320 "To abort recursive edit, type: \\[abort-recursive-edit]")
321 @result{} "To abort recursive edit, type: C-]"
322 @end group
323
324 @group
325 (substitute-command-keys
326 "The keys that are defined for the minibuffer here are:
327 \\@{minibuffer-local-must-match-map@}")
328 @result{} "The keys that are defined for the minibuffer here are:
329 @end group
330
331 ? minibuffer-completion-help
332 SPC minibuffer-complete-word
333 TAB minibuffer-complete
334 C-j minibuffer-complete-and-exit
335 RET minibuffer-complete-and-exit
336 C-g abort-recursive-edit
337 "
338
339 @group
340 (substitute-command-keys
341 "To abort a recursive edit from the minibuffer, type\
342 \\<minibuffer-local-must-match-map>\\[abort-recursive-edit].")
343 @result{} "To abort a recursive edit from the minibuffer, type C-g."
344 @end group
345 @end smallexample
346
347 @node Describing Characters
348 @section Describing Characters for Help Messages
349
350 These functions convert events, key sequences, or characters to
351 textual descriptions. These descriptions are useful for including
352 arbitrary text characters or key sequences in messages, because they
353 convert non-printing and whitespace characters to sequences of printing
354 characters. The description of a non-whitespace printing character is
355 the character itself.
356
357 @defun key-description sequence
358 @cindex Emacs event standard notation
359 This function returns a string containing the Emacs standard notation
360 for the input events in @var{sequence}. The argument @var{sequence} may
361 be a string, vector or list. @xref{Input Events}, for more information
362 about valid events. See also the examples for
363 @code{single-key-description}, below.
364 @end defun
365
366 @defun single-key-description event
367 @cindex event printing
368 @cindex character printing
369 @cindex control character printing
370 @cindex meta character printing
371 This function returns a string describing @var{event} in the standard
372 Emacs notation for keyboard input. A normal printing character appears
373 as itself, but a control character turns into a string starting with
374 @samp{C-}, a meta character turns into a string starting with @samp{M-},
375 and space, tab, etc.@: appear as @samp{SPC}, @samp{TAB}, etc. A
376 function key symbol appears as itself. An event that is a list appears
377 as the name of the symbol in the @sc{car} of the list.
378
379 @smallexample
380 @group
381 (single-key-description ?\C-x)
382 @result{} "C-x"
383 @end group
384 @group
385 (key-description "\C-x \M-y \n \t \r \f123")
386 @result{} "C-x SPC M-y SPC C-j SPC TAB SPC RET SPC C-l 1 2 3"
387 @end group
388 @group
389 (single-key-description 'C-mouse-1)
390 @result{} "C-mouse-1"
391 @end group
392 @end smallexample
393 @end defun
394
395 @defun text-char-description character
396 This function returns a string describing @var{character} in the
397 standard Emacs notation for characters that appear in text---like
398 @code{single-key-description}, except that control characters are
399 represented with a leading caret (which is how control characters in
400 Emacs buffers are usually displayed).
401
402 @smallexample
403 @group
404 (text-char-description ?\C-c)
405 @result{} "^C"
406 @end group
407 @group
408 (text-char-description ?\M-m)
409 @result{} "M-m"
410 @end group
411 @group
412 (text-char-description ?\C-\M-m)
413 @result{} "M-^M"
414 @end group
415 @end smallexample
416 @end defun
417
418 @defun read-kbd-macro string
419 This function is used mainly for operating on keyboard macros, but it
420 can also be used as a rough inverse for @code{key-description}. You
421 call it with a string containing key descriptions, separated by spaces;
422 it returns a string or vector containing the corresponding events.
423 (This may or may not be a single valid key sequence, depending on what
424 events you use; @pxref{Keymap Terminology}.)
425 @end defun
426
427 @node Help Functions
428 @section Help Functions
429
430 Emacs provides a variety of on-line help functions, all accessible to
431 the user as subcommands of the prefix @kbd{C-h}. For more information
432 about them, see @ref{Help, , Help, emacs, The GNU Emacs Manual}. Here
433 we describe some program-level interfaces to the same information.
434
435 @deffn Command apropos regexp &optional do-all
436 This function finds all symbols whose names contain a match for the
437 regular expression @var{regexp}, and returns a list of them
438 (@pxref{Regular Expressions}). It also displays the symbols in a buffer
439 named @samp{*Help*}, each with a one-line description taken from the
440 beginning of its documentation string.
441
442 @c Emacs 19 feature
443 If @var{do-all} is non-@code{nil}, then @code{apropos} also shows key
444 bindings for the functions that are found; it also shows all symbols,
445 even those that are neither functions nor variables.
446
447 In the first of the following examples, @code{apropos} finds all the
448 symbols with names containing @samp{exec}. (We don't show here the
449 output that results in the @samp{*Help*} buffer.)
450
451 @smallexample
452 @group
453 (apropos "exec")
454 @result{} (Buffer-menu-execute command-execute exec-directory
455 exec-path execute-extended-command execute-kbd-macro
456 executing-kbd-macro executing-macro)
457 @end group
458 @end smallexample
459 @end deffn
460
461 @defvar help-map
462 The value of this variable is a local keymap for characters following the
463 Help key, @kbd{C-h}.
464 @end defvar
465
466 @deffn {Prefix Command} help-command
467 This symbol is not a function; its function definition cell holds the
468 keymap known as @code{help-map}. It is defined in @file{help.el} as
469 follows:
470
471 @smallexample
472 @group
473 (define-key global-map "\C-h" 'help-command)
474 (fset 'help-command help-map)
475 @end group
476 @end smallexample
477 @end deffn
478
479 @defun print-help-return-message &optional function
480 This function builds a string that explains how to restore the previous
481 state of the windows after a help command. After building the message,
482 it applies @var{function} to it if @var{function} is non-@code{nil}.
483 Otherwise it calls @code{message} to display it in the echo area.
484
485 This function expects to be called inside a
486 @code{with-output-to-temp-buffer} special form, and expects
487 @code{standard-output} to have the value bound by that special form.
488 For an example of its use, see the long example in @ref{Accessing
489 Documentation}.
490 @end defun
491
492 @defvar help-char
493 The value of this variable is the help character---the character that
494 Emacs recognizes as meaning Help. By default, its value is 8, which
495 stands for @kbd{C-h}. When Emacs reads this character, if
496 @code{help-form} is a non-@code{nil} Lisp expression, it evaluates that
497 expression, and displays the result in a window if it is a string.
498
499 Usually the value of @code{help-form} is @code{nil}. Then the
500 help character has no special meaning at the level of command input, and
501 it becomes part of a key sequence in the normal way. The standard key
502 binding of @kbd{C-h} is a prefix key for several general-purpose help
503 features.
504
505 The help character is special after prefix keys, too. If it has no
506 binding as a subcommand of the prefix key, it runs
507 @code{describe-prefix-bindings}, which displays a list of all the
508 subcommands of the prefix key.
509 @end defvar
510
511 @defvar help-event-list
512 @tindex help-event-list
513 The value of this variable is a list of event types that serve as
514 alternative ``help characters.'' These events are handled just like the
515 event specified by @code{help-char}.
516 @end defvar
517
518 @defvar help-form
519 If this variable is non-@code{nil}, its value is a form to evaluate
520 whenever the character @code{help-char} is read. If evaluating the form
521 produces a string, that string is displayed.
522
523 A command that calls @code{read-event} or @code{read-char} probably
524 should bind @code{help-form} to a non-@code{nil} expression while it
525 does input. (The time when you should not do this is when @kbd{C-h} has
526 some other meaning.) Evaluating this expression should result in a
527 string that explains what the input is for and how to enter it properly.
528
529 Entry to the minibuffer binds this variable to the value of
530 @code{minibuffer-help-form} (@pxref{Minibuffer Misc}).
531 @end defvar
532
533 @defvar prefix-help-command
534 This variable holds a function to print help for a prefix key. The
535 function is called when the user types a prefix key followed by the help
536 character, and the help character has no binding after that prefix. The
537 variable's default value is @code{describe-prefix-bindings}.
538 @end defvar
539
540 @defun describe-prefix-bindings
541 This function calls @code{describe-bindings} to display a list of all
542 the subcommands of the prefix key of the most recent key sequence. The
543 prefix described consists of all but the last event of that key
544 sequence. (The last event is, presumably, the help character.)
545 @end defun
546
547 The following two functions are meant for modes that want to provide
548 help without relinquishing control, such as the ``electric'' modes.
549 Their names begin with @samp{Helper} to distinguish them from the
550 ordinary help functions.
551
552 @deffn Command Helper-describe-bindings
553 This command pops up a window displaying a help buffer containing a
554 listing of all of the key bindings from both the local and global keymaps.
555 It works by calling @code{describe-bindings}.
556 @end deffn
557
558 @deffn Command Helper-help
559 This command provides help for the current mode. It prompts the user
560 in the minibuffer with the message @samp{Help (Type ? for further
561 options)}, and then provides assistance in finding out what the key
562 bindings are, and what the mode is intended for. It returns @code{nil}.
563
564 This can be customized by changing the map @code{Helper-help-map}.
565 @end deffn
566
567 @c Emacs 19 feature
568 @defvar data-directory
569 This variable holds the name of the directory in which Emacs finds
570 certain documentation and text files that come with Emacs. In older
571 Emacs versions, @code{exec-directory} was used for this.
572 @end defvar
573
574 @c Emacs 19 feature
575 @defmac make-help-screen fname help-line help-text help-map
576 This macro defines a help command named @var{fname} that acts like a
577 prefix key that shows a list of the subcommands it offers.
578
579 When invoked, @var{fname} displays @var{help-text} in a window, then
580 reads and executes a key sequence according to @var{help-map}. The
581 string @var{help-text} should describe the bindings available in
582 @var{help-map}.
583
584 The command @var{fname} is defined to handle a few events itself, by
585 scrolling the display of @var{help-text}. When @var{fname} reads one of
586 those special events, it does the scrolling and then reads another
587 event. When it reads an event that is not one of those few, and which
588 has a binding in @var{help-map}, it executes that key's binding and
589 then returns.
590
591 The argument @var{help-line} should be a single-line summary of the
592 alternatives in @var{help-map}. In the current version of Emacs, this
593 argument is used only if you set the option @code{three-step-help} to
594 @code{t}.
595
596 This macro is used in the command @code{help-for-help} which is the
597 binding of @kbd{C-h C-h}.
598 @end defmac
599
600 @defopt three-step-help
601 If this variable is non-@code{nil}, commands defined with
602 @code{make-help-screen} display their @var{help-line} strings in the
603 echo area at first, and display the longer @var{help-text} strings only
604 if the user types the help character again.
605 @end defopt