1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002
4 ;; Free Software Foundation, Inc.
7 ;; Keywords: help, internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This code implements GNU Emacs' on-line help system, the one invoked by
29 ;; `M-x help-for-help'.
33 ;; Get the macro make-help-screen when this is compiled,
34 ;; or run interpreted, but not when the compiled code is loaded.
35 (eval-when-compile (require 'help-macro
))
37 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
38 (add-hook 'temp-buffer-setup-hook
'help-mode-setup
)
39 (add-hook 'temp-buffer-show-hook
'help-mode-finish
)
41 (defvar help-map
(make-sparse-keymap)
42 "Keymap for characters following the Help key.")
44 (define-key global-map
(char-to-string help-char
) 'help-command
)
45 (define-key global-map
[help] 'help-command)
46 (define-key global-map [f1] 'help-command)
47 (fset 'help-command help-map)
49 (define-key help-map (char-to-string help-char) 'help-for-help)
50 (define-key help-map [help] 'help-for-help
)
51 (define-key help-map
[f1] 'help-for-help)
52 (define-key help-map "?" 'help-for-help)
54 (define-key help-map "\C-c" 'describe-copying)
55 (define-key help-map "\C-d" 'describe-distribution)
56 (define-key help-map "\C-e" 'view-emacs-problems)
57 (define-key help-map "\C-f" 'view-emacs-FAQ)
58 (define-key help-map "\C-m" 'view-order-manuals)
59 (define-key help-map "\C-n" 'view-emacs-news)
60 (define-key help-map "\C-p" 'describe-project)
61 (define-key help-map "\C-t" 'view-todo)
62 (define-key help-map "\C-w" 'describe-no-warranty)
64 ;; This does not fit the pattern, but it is natural given the C-\ command.
65 (define-key help-map "\C-\\" 'describe-input-method)
67 (define-key help-map "C" 'describe-coding-system)
68 (define-key help-map "F" 'Info-goto-emacs-command-node)
69 (define-key help-map "I" 'describe-input-method)
70 (define-key help-map "K" 'Info-goto-emacs-key-command-node)
71 (define-key help-map "L" 'describe-language-environment)
72 (define-key help-map "S" 'info-lookup-symbol)
74 (define-key help-map "a" 'apropos-command)
76 (define-key help-map "b" 'describe-bindings)
78 (define-key help-map "c" 'describe-key-briefly)
80 (define-key help-map "e" 'view-echo-area-messages)
82 (define-key help-map "f" 'describe-function)
84 (define-key help-map "h" 'view-hello-file)
86 (define-key help-map "i" 'info)
87 (define-key help-map "4i" 'info-other-window)
89 (define-key help-map "k" 'describe-key)
91 (define-key help-map "l" 'view-lossage)
93 (define-key help-map "m" 'describe-mode)
95 (define-key help-map "n" 'view-emacs-news)
97 (define-key help-map "p" 'finder-by-keyword)
98 (autoload 'finder-by-keyword "finder"
99 "Find packages matching a given keyword." t)
101 (define-key help-map "r" 'info-emacs-manual)
103 (define-key help-map "s" 'describe-syntax)
105 (define-key help-map "t" 'help-with-tutorial)
107 (define-key help-map "w" 'where-is)
109 (define-key help-map "v" 'describe-variable)
111 (define-key help-map "q" 'help-quit)
115 "Just exit from the Help command's command loop."
119 (defvar help-return-method nil
120 "What to do to \"exit\" the help buffer.
122 (WINDOW . t) delete the selected window, go to WINDOW.
123 (WINDOW . quit-window) do quit-window, then select WINDOW.
124 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
126 (defun print-help-return-message (&optional function)
127 "Display or return message saying how to restore windows after help command.
128 This function assumes that `standard-output' is the help buffer.
129 It computes a message, and applies the optional argument FUNCTION to it.
130 If FUNCTION is nil, it applies `message', thus displaying the message."
131 (and (not (get-buffer-window standard-output))
133 (cond ((special-display-p (buffer-name standard-output))
134 (setq help-return-method (cons (selected-window) t))
135 ;; If the help output buffer is a special display buffer,
136 ;; don't say anything about how to get rid of it.
137 ;; First of all, the user will do that with the window
138 ;; manager, not with Emacs.
139 ;; Secondly, the buffer has not been displayed yet,
140 ;; so we don't know whether its frame will be selected.
142 (display-buffer-reuse-frames
143 (setq help-return-method (cons (selected-window)
146 ((not (one-window-p t))
147 (setq help-return-method
148 (cons (selected-window) 'quit-window))
149 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
151 (setq help-return-method (cons (selected-window) t))
152 "Type \\[delete-other-windows] to remove help window.")
154 (setq help-return-method
155 (list (selected-window) (window-buffer)
156 (window-start) (window-point)))
157 "Type \\[switch-to-buffer] RET to remove help window."))))
158 (funcall (or function 'message)
161 (substitute-command-keys first-message))
162 (if first-message " ")
163 ;; If the help buffer will go in a separate frame,
164 ;; it's no use mentioning a command to scroll, so don't.
165 (if (special-display-p (buffer-name standard-output))
167 (if (same-window-p (buffer-name standard-output))
168 ;; Say how to scroll this window.
169 (substitute-command-keys
170 "\\[scroll-up] to scroll the help.")
171 ;; Say how to scroll some other window.
172 (substitute-command-keys
173 "\\[scroll-other-window] to scroll the help."))))))))
175 ;; So keyboard macro definitions are documented correctly
176 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
178 (defalias 'help 'help-for-help)
179 (make-help-screen help-for-help
180 "a b c C e f F i I k C-k l L m p s t v w C-c C-d C-f C-n C-p C-t C-w or ? :"
181 "You have typed %THIS-KEY%, the help character. Type a Help option:
182 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
184 a command-apropos. Give a substring, and see a list of commands
185 (functions interactively callable) that contain
186 that substring. See also the apropos command.
187 b describe-bindings. Display table of all key bindings.
188 c describe-key-briefly. Type a command key sequence;
189 it prints the function name that sequence runs.
190 C describe-coding-system. This describes either a specific coding system
191 (if you type its name) or the coding systems currently in use
192 (if you type just RET).
193 e view-echo-area-messages. Show the `*Messages*' buffer.
194 f describe-function. Type a function name and get documentation of it.
195 F Info-goto-emacs-command-node. Type a function name;
196 it takes you to the Info node for that command.
197 h Display the HELLO file which illustrates various scripts.
198 i info. The info documentation reader.
199 I describe-input-method. Describe a specific input method (if you type
200 its name) or the current input method (if you type just RET).
201 k describe-key. Type a command key sequence;
202 it displays the full documentation.
203 K Info-goto-emacs-key-command-node. Type a command key sequence;
204 it takes you to the Info node for the command bound to that key.
205 l view-lossage. Show last 100 characters you typed.
206 L describe-language-environment. This describes either a
207 specific language environment (if you type its name)
208 or the current language environment (if you type just RET).
209 m describe-mode. Print documentation of current minor modes,
210 and the current major mode, including their special commands.
211 p finder-by-keyword. Find packages matching a given topic keyword.
212 s describe-syntax. Display contents of syntax table, plus explanations.
213 S info-lookup-symbol. Display the definition of a specific symbol
214 as found in the manual for the language this buffer is written in.
215 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
216 v describe-variable. Type name of a variable;
217 it displays the variable's documentation and value.
218 w where-is. Type command name; it prints which keystrokes
221 C-c Display Emacs copying permission (GNU General Public License).
222 C-d Display Emacs ordering information.
223 C-e Display info about Emacs problems.
224 C-f Display the Emacs FAQ.
225 C-m Display how to order printed Emacs manuals.
226 C-n Display news of recent Emacs changes.
227 C-p Display information about the GNU project.
228 C-t Display the Emacs TODO list.
229 C-w Display information on absence of warranty for GNU Emacs."
234 (defun function-called-at-point ()
235 "Return a function around point or else called by the list containing point.
236 If that doesn't give a function, return nil."
237 (with-syntax-table emacs-lisp-mode-syntax-table
238 (or (condition-case ()
240 (or (not (zerop (skip-syntax-backward "_w")))
241 (eq (char-syntax (following-char)) ?w)
242 (eq (char-syntax (following-char)) ?_)
244 (skip-chars-forward "'")
245 (let ((obj (read (current-buffer))))
246 (and (symbolp obj) (fboundp obj) obj)))
251 (narrow-to-region (max (point-min)
252 (- (point) 1000)) (point-max))
253 ;; Move up to surrounding paren, then after the open.
256 ;; If there is space here, this is probably something
257 ;; other than a real Lisp function call, so ignore it.
258 (if (looking-at "[ \t]")
259 (error "Probably not a Lisp function call"))
260 (let ((obj (read (current-buffer))))
261 (and (symbolp obj) (fboundp obj) obj))))
265 ;;; `User' help functions
267 (defun describe-distribution ()
268 "Display info on how to obtain the latest version of GNU Emacs."
270 (view-file (expand-file-name "DISTRIB" data-directory)))
272 (defun describe-copying ()
273 "Display info on how you may redistribute copies of GNU Emacs."
275 (view-file (expand-file-name "COPYING" data-directory))
276 (goto-char (point-min)))
278 (defun describe-project ()
279 "Display info on the GNU project."
281 (view-file (expand-file-name "THE-GNU-PROJECT" data-directory))
282 (goto-char (point-min)))
284 (defun describe-no-warranty ()
285 "Display info on all the kinds of warranty Emacs does NOT have."
288 (let (case-fold-search)
289 (search-forward "NO WARRANTY")
292 (defun describe-prefix-bindings ()
293 "Describe the bindings of the prefix used to reach this command.
294 The prefix described consists of all but the last event
295 of the key sequence that ran this command."
297 (let* ((key (this-command-keys)))
300 (substring key 0 (1- (length key)))
301 (let ((prefix (make-vector (1- (length key)) nil))
303 (while (< i (length prefix))
304 (aset prefix i (aref key i))
307 ;; Make C-h after a prefix, when not specifically bound,
308 ;; run describe-prefix-bindings.
309 (setq prefix-help-command 'describe-prefix-bindings)
311 (defun view-emacs-news (&optional arg)
312 "Display info on recent changes to Emacs.
313 With numeric argument, display information on correspondingly older changes."
315 (let* ((arg (if arg (prefix-numeric-value arg) 0))
316 (file (cond ((eq arg 0) "NEWS")
320 (nreverse (directory-files data-directory
321 nil "^ONEWS\\.[0-9]+$"
324 (view-file (expand-file-name file data-directory))
325 (error "No such old news"))))
327 (defun view-todo (&optional arg)
328 "Display the Emacs TODO list."
330 (view-file (expand-file-name "TODO" data-directory)))
332 (defun view-echo-area-messages ()
333 "View the log of recent echo-area messages: the `*Messages*' buffer.
334 The number of messages retained in that buffer
335 is specified by the variable `message-log-max'."
337 (switch-to-buffer (get-buffer-create "*Messages*")))
339 (defun view-order-manuals ()
340 "Display the Emacs ORDERS file."
342 (view-file (expand-file-name "ORDERS" data-directory))
345 (defun view-emacs-FAQ ()
346 "Display the Emacs Frequently Asked Questions (FAQ) file."
348 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
351 (defun view-emacs-problems ()
352 "Display info on known problems with Emacs and possible workarounds."
354 (view-file (expand-file-name "PROBLEMS" data-directory)))
356 (defun view-lossage ()
357 "Display last 100 input keystrokes.
359 To record all your input on a file, use `open-dribble-file'."
361 (help-setup-xref (list #'view-lossage) (interactive-p))
362 (with-output-to-temp-buffer (help-buffer)
363 (princ (mapconcat (lambda (key)
364 (if (or (integerp key) (symbolp key) (listp key))
365 (single-key-description key)
366 (prin1-to-string key nil)))
369 (with-current-buffer standard-output
370 (goto-char (point-min))
371 (while (progn (move-to-column 50) (not (eobp)))
372 (search-forward " " nil t)
374 (print-help-return-message)))
379 (defun describe-bindings (&optional prefix buffer)
380 "Show a list of all defined keys, and their definitions.
381 We put that list in a buffer, and display the buffer.
383 The optional argument PREFIX, if non-nil, should be a key sequence;
384 then we display only bindings that start with that prefix.
385 The optional argument BUFFER specifies which buffer's bindings
386 to display (default, the current buffer)."
388 (or buffer (setq buffer (current-buffer)))
389 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
390 (with-current-buffer buffer
391 (describe-bindings-internal nil prefix)))
393 ;; This function used to be in keymap.c.
394 (defun describe-bindings-internal (&optional menus prefix)
395 "Show a list of all defined keys, and their definitions.
396 We put that list in a buffer, and display the buffer.
398 The optional argument MENUS, if non-nil, says to mention menu bindings.
399 \(Ordinarily these are omitted from the output.)
400 The optional argument PREFIX, if non-nil, should be a key sequence;
401 then we display only bindings that start with that prefix."
403 (let ((buf (current-buffer)))
404 (with-output-to-temp-buffer "*Help*"
405 (with-current-buffer standard-output
406 (describe-buffer-bindings buf prefix menus)))))
408 (defun where-is (definition &optional insert)
409 "Print message listing key sequences that invoke the command DEFINITION.
410 Argument is a command definition, usually a symbol with a function definition.
411 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
413 (let ((fn (function-called-at-point))
414 (enable-recursive-minibuffers t)
416 (setq val (completing-read
418 (format "Where is command (default %s): " fn)
419 "Where is command: ")
420 obarray 'commandp t))
421 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
422 (let ((func (indirect-function definition))
424 (standard-output (if insert (current-buffer) t)))
425 (mapatoms (lambda (symbol)
426 (and (fboundp symbol)
427 (not (eq symbol definition))
428 (eq func (condition-case ()
429 (indirect-function symbol)
431 (push symbol defs))))
434 (let* ((remapped (remap-command symbol))
435 (keys (where-is-internal
436 symbol overriding-local-map nil nil remapped))
437 (keys (mapconcat 'key-description keys ", ")))
439 (if (> (length keys) 0)
441 (format "%s (%s) (remapped from %s)"
442 keys remapped symbol)
443 (format "%s (%s)" keys symbol))
444 (format "M-x %s RET" symbol))
445 (if (> (length keys) 0)
447 (format "%s is remapped to %s which is on %s"
448 definition symbol keys)
449 (format "%s is on %s" symbol keys))
450 (format "%s is not on any key" symbol)))))
451 (cons definition defs)
455 (defun string-key-binding (key)
456 "Value is the binding of KEY in a string.
457 If KEY is an event on a string, and that string has a `local-map'
458 or `keymap' property, return the binding of KEY in the string's keymap."
460 (start (when (vectorp key)
461 (if (memq (aref key 0)
462 '(mode-line header-line left-margin right-margin))
463 (event-start (aref key 1))
464 (and (consp (aref key 0))
465 (event-start (aref key 0))))))
466 (string-info (and (consp start) (nth 4 start))))
468 (let* ((string (car string-info))
469 (pos (cdr string-info))
470 (local-map (and (>= pos 0)
471 (< pos (length string))
472 (or (get-text-property pos 'local-map string)
473 (get-text-property pos 'keymap string)))))
474 (setq defn (and local-map (lookup-key local-map key)))))
477 (defun help-key-description (key untranslated)
478 (let ((string (key-description key)))
479 (if (or (not untranslated)
480 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
482 (let ((otherstring (key-description untranslated)))
483 (if (equal string otherstring)
485 (format "%s (translated from %s)" string otherstring))))))
487 (defun describe-key-briefly (key &optional insert untranslated)
488 "Print the name of the function KEY invokes. KEY is a string.
489 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
490 If non-nil UNTRANSLATED is a vector of the untranslated events.
491 It can also be a number in which case the untranslated events from
492 the last key hit are used."
493 (interactive "kDescribe key briefly: \nP\np")
494 (if (numberp untranslated)
495 (setq untranslated (this-single-command-raw-keys)))
497 (let ((modifiers (event-modifiers (aref key 0)))
498 (standard-output (if insert (current-buffer) t))
500 ;; For a mouse button event, go to the button it applies to
501 ;; to get the right key bindings. And go to the right place
502 ;; in case the keymap depends on where you clicked.
503 (if (or (memq 'click modifiers) (memq 'down modifiers)
504 (memq 'drag modifiers))
505 (setq window (posn-window (event-start (aref key 0)))
506 position (posn-point (event-start (aref key 0)))))
509 (set-buffer (window-buffer window))
510 (goto-char position)))
511 ;; Ok, now look up the key and name the command.
512 (let ((defn (or (string-key-binding key)
514 (key-desc (help-key-description key untranslated)))
515 (if (or (null defn) (integerp defn) (equal defn 'undefined))
516 (princ (format "%s is undefined" key-desc))
517 (princ (format (if (windowp window)
518 "%s at that spot runs the command %s"
519 "%s runs the command %s")
521 (if (symbolp defn) defn (prin1-to-string defn)))))))))
524 (defun describe-key (key &optional untranslated)
525 "Display documentation of the function invoked by KEY.
526 KEY should be a key sequence--when calling from a program,
527 pass a string or a vector.
528 If non-nil UNTRANSLATED is a vector of the untranslated events.
529 It can also be a number in which case the untranslated events from
530 the last key hit are used."
531 (interactive "kDescribe key: \np")
532 (if (numberp untranslated)
533 (setq untranslated (this-single-command-raw-keys)))
535 (let ((modifiers (event-modifiers (aref key 0)))
537 ;; For a mouse button event, go to the button it applies to
538 ;; to get the right key bindings. And go to the right place
539 ;; in case the keymap depends on where you clicked.
540 (if (or (memq 'click modifiers) (memq 'down modifiers)
541 (memq 'drag modifiers))
542 (setq window (posn-window (event-start (aref key 0)))
543 position (posn-point (event-start (aref key 0)))))
544 (when (windowp window)
545 (set-buffer (window-buffer window))
546 (goto-char position))
547 (let ((defn (or (string-key-binding key) (key-binding key))))
548 (if (or (null defn) (integerp defn) (equal defn 'undefined))
549 (message "%s is undefined" (help-key-description key untranslated))
550 (help-setup-xref (list #'describe-function defn) (interactive-p))
551 (with-output-to-temp-buffer (help-buffer)
552 (princ (help-key-description key untranslated))
554 (princ " at that spot"))
555 (princ " runs the command ")
557 (princ "\n which is ")
558 (describe-function-1 defn)
559 (print-help-return-message)))))))
562 (defun describe-mode (&optional buffer)
563 "Display documentation of current major mode and minor modes.
564 The major mode description comes first, followed by the minor modes,
565 each on a separate page.
566 For this to work correctly for a minor mode, the mode's indicator variable
567 \(listed in `minor-mode-alist') must also be a function whose documentation
568 describes the minor mode."
570 (help-setup-xref (list #'describe-mode (or buffer (current-buffer)))
572 ;; For the sake of help-do-xref and help-xref-go-back,
573 ;; don't switch buffers before calling `help-buffer'.
574 (with-output-to-temp-buffer (help-buffer)
576 (when buffer (set-buffer buffer))
577 (when minor-mode-alist
578 (princ "The major mode is described first.
579 For minor modes, see following pages.\n\n"))
582 (princ (documentation major-mode))
583 (let ((minor-modes minor-mode-alist))
585 (let* ((minor-mode (car (car minor-modes)))
586 (indicator (car (cdr (car minor-modes)))))
587 ;; Document a minor mode if it is listed in minor-mode-alist,
588 ;; bound locally in this buffer, non-nil, and has a function
590 (if (and (boundp minor-mode)
591 (symbol-value minor-mode)
592 (fboundp minor-mode))
593 (let ((pretty-minor-mode minor-mode))
594 (if (string-match "\\(-minor\\)?-mode\\'"
595 (symbol-name minor-mode))
596 (setq pretty-minor-mode
598 (substring (symbol-name minor-mode)
599 0 (match-beginning 0)))))
600 (while (and indicator (symbolp indicator)
602 (not (eq indicator (symbol-value indicator))))
603 (setq indicator (symbol-value indicator)))
605 (princ (format "%s minor mode (%s):\n"
608 (format "indicator%s" indicator)
610 (princ (documentation minor-mode)))))
611 (setq minor-modes (cdr minor-modes))))
612 (print-help-return-message))))
615 ;;; Automatic resizing of temporary buffers.
617 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
618 "*Maximum height of a window displaying a temporary buffer.
619 This is the maximum height (in text lines) which `resize-temp-buffer-window'
620 will give to a window displaying a temporary buffer.
621 It can also be a function which will be called with the object corresponding
622 to the buffer to be displayed as argument and should return an integer
624 :type '(choice integer function)
628 (define-minor-mode temp-buffer-resize-mode
629 "Toggle the mode which makes windows smaller for temporary buffers.
630 With prefix argument ARG, turn the resizing of windows displaying temporary
631 buffers on if ARG is positive or off otherwise.
632 This makes the window the right height for its contents, but never
633 more than `temp-buffer-max-height' nor less than `window-min-height'.
634 This applies to `help', `apropos' and `completion' buffers, and some others."
635 :global t :group 'help
636 (if temp-buffer-resize-mode
637 ;; `help-make-xrefs' may add a `back' button and thus increase the
638 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
639 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
640 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
642 (defun resize-temp-buffer-window ()
643 "Resize the current window to fit its contents.
644 Will not make it higher than `temp-buffer-max-height' nor smaller than
645 `window-min-height'. Do nothing if it is the only window on its frame, if it
646 is not as wide as the frame or if some of the window's contents are scrolled
648 (unless (or (one-window-p 'nomini)
649 (not (pos-visible-in-window-p (point-min)))
650 (/= (frame-width) (window-width)))
651 (fit-window-to-buffer
653 (if (functionp temp-buffer-max-height)
654 (funcall temp-buffer-max-height (current-buffer))
655 temp-buffer-max-height))))
657 ;; Provide this for the sake of define-minor-mode which generates
658 ;; defcustoms which require 'help'.
661 ;;; help.el ends here