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 "s" 'describe-syntax)
103 (define-key help-map "t" 'help-with-tutorial)
105 (define-key help-map "w" 'where-is)
107 (define-key help-map "v" 'describe-variable)
109 (define-key help-map "q" 'help-quit)
113 "Just exit from the Help command's command loop."
117 (defvar help-return-method nil
118 "What to do to \"exit\" the help buffer.
120 (WINDOW . t) delete the selected window, go to WINDOW.
121 (WINDOW . quit-window) do quit-window, then select WINDOW.
122 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
124 (defun print-help-return-message (&optional function)
125 "Display or return message saying how to restore windows after help command.
126 Computes a message and applies the optional argument FUNCTION to it.
127 If FUNCTION is nil, applies `message' to it, thus printing it."
128 (and (not (get-buffer-window standard-output))
130 (cond ((special-display-p (buffer-name standard-output))
131 (setq help-return-method (cons (selected-window) t))
132 ;; If the help output buffer is a special display buffer,
133 ;; don't say anything about how to get rid of it.
134 ;; First of all, the user will do that with the window
135 ;; manager, not with Emacs.
136 ;; Secondly, the buffer has not been displayed yet,
137 ;; so we don't know whether its frame will be selected.
139 (display-buffer-reuse-frames
140 (setq help-return-method (cons (selected-window)
143 ((not (one-window-p t))
144 (setq help-return-method
145 (cons (selected-window) 'quit-window))
146 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
148 (setq help-return-method (cons (selected-window) t))
149 "Type \\[delete-other-windows] to remove help window.")
151 (setq help-return-method
152 (list (selected-window) (window-buffer)
153 (window-start) (window-point)))
154 "Type \\[switch-to-buffer] RET to remove help window."))))
155 (funcall (or function 'message)
158 (substitute-command-keys first-message))
159 (if first-message " ")
160 ;; If the help buffer will go in a separate frame,
161 ;; it's no use mentioning a command to scroll, so don't.
162 (if (special-display-p (buffer-name standard-output))
164 (if (same-window-p (buffer-name standard-output))
165 ;; Say how to scroll this window.
166 (substitute-command-keys
167 "\\[scroll-up] to scroll the help.")
168 ;; Say how to scroll some other window.
169 (substitute-command-keys
170 "\\[scroll-other-window] to scroll the help."))))))))
172 ;; So keyboard macro definitions are documented correctly
173 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
175 (defalias 'help 'help-for-help)
176 (make-help-screen help-for-help
177 "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 ? :"
178 "You have typed %THIS-KEY%, the help character. Type a Help option:
179 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
181 a command-apropos. Give a substring, and see a list of commands
182 (functions interactively callable) that contain
183 that substring. See also the apropos command.
184 b describe-bindings. Display table of all key bindings.
185 c describe-key-briefly. Type a command key sequence;
186 it prints the function name that sequence runs.
187 C describe-coding-system. This describes either a specific coding system
188 (if you type its name) or the coding systems currently in use
189 (if you type just RET).
190 e view-echo-area-messages. Show the `*Messages*' buffer.
191 f describe-function. Type a function name and get documentation of it.
192 F Info-goto-emacs-command-node. Type a function name;
193 it takes you to the Info node for that command.
194 h Display the HELLO file which illustrates various scripts.
195 i info. The info documentation reader.
196 I describe-input-method. Describe a specific input method (if you type
197 its name) or the current input method (if you type just RET).
198 k describe-key. Type a command key sequence;
199 it displays the full documentation.
200 K Info-goto-emacs-key-command-node. Type a command key sequence;
201 it takes you to the Info node for the command bound to that key.
202 l view-lossage. Show last 100 characters you typed.
203 L describe-language-environment. This describes either a
204 specific language environment (if you type its name)
205 or the current language environment (if you type just RET).
206 m describe-mode. Print documentation of current minor modes,
207 and the current major mode, including their special commands.
208 p finder-by-keyword. Find packages matching a given topic keyword.
209 s describe-syntax. Display contents of syntax table, plus explanations.
210 S info-lookup-symbol. Display the definition of a specific symbol
211 as found in the manual for the language this buffer is written in.
212 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
213 v describe-variable. Type name of a variable;
214 it displays the variable's documentation and value.
215 w where-is. Type command name; it prints which keystrokes
218 C-c Display Emacs copying permission (GNU General Public License).
219 C-d Display Emacs ordering information.
220 C-e Display info about Emacs problems.
221 C-f Display the Emacs FAQ.
222 C-m Display how to order printed Emacs manuals.
223 C-n Display news of recent Emacs changes.
224 C-p Display information about the GNU project.
225 C-t Display the Emacs TODO list.
226 C-w Display information on absence of warranty for GNU Emacs."
231 (defun function-called-at-point ()
232 "Return a function around point or else called by the list containing point.
233 If that doesn't give a function, return nil."
234 (with-syntax-table emacs-lisp-mode-syntax-table
235 (or (condition-case ()
237 (or (not (zerop (skip-syntax-backward "_w")))
238 (eq (char-syntax (following-char)) ?w)
239 (eq (char-syntax (following-char)) ?_)
241 (skip-chars-forward "'")
242 (let ((obj (read (current-buffer))))
243 (and (symbolp obj) (fboundp obj) obj)))
248 (narrow-to-region (max (point-min)
249 (- (point) 1000)) (point-max))
250 ;; Move up to surrounding paren, then after the open.
253 ;; If there is space here, this is probably something
254 ;; other than a real Lisp function call, so ignore it.
255 (if (looking-at "[ \t]")
256 (error "Probably not a Lisp function call"))
257 (let ((obj (read (current-buffer))))
258 (and (symbolp obj) (fboundp obj) obj))))
262 ;;; `User' help functions
264 (defun describe-distribution ()
265 "Display info on how to obtain the latest version of GNU Emacs."
267 (view-file (expand-file-name "DISTRIB" data-directory)))
269 (defun describe-copying ()
270 "Display info on how you may redistribute copies of GNU Emacs."
272 (view-file (expand-file-name "COPYING" data-directory))
273 (goto-char (point-min)))
275 (defun describe-project ()
276 "Display info on the GNU project."
278 (view-file (expand-file-name "THE-GNU-PROJECT" data-directory))
279 (goto-char (point-min)))
281 (defun describe-no-warranty ()
282 "Display info on all the kinds of warranty Emacs does NOT have."
285 (let (case-fold-search)
286 (search-forward "NO WARRANTY")
289 (defun describe-prefix-bindings ()
290 "Describe the bindings of the prefix used to reach this command.
291 The prefix described consists of all but the last event
292 of the key sequence that ran this command."
294 (let* ((key (this-command-keys)))
297 (substring key 0 (1- (length key)))
298 (let ((prefix (make-vector (1- (length key)) nil))
300 (while (< i (length prefix))
301 (aset prefix i (aref key i))
304 ;; Make C-h after a prefix, when not specifically bound,
305 ;; run describe-prefix-bindings.
306 (setq prefix-help-command 'describe-prefix-bindings)
308 (defun view-emacs-news (&optional arg)
309 "Display info on recent changes to Emacs.
310 With numeric argument, display information on correspondingly older changes."
312 (let* ((arg (if arg (prefix-numeric-value arg) 0))
313 (file (cond ((eq arg 0) "NEWS")
317 (nreverse (directory-files data-directory
318 nil "^ONEWS\\.[0-9]+$"
321 (view-file (expand-file-name file data-directory))
322 (error "No such old news"))))
324 (defun view-todo (&optional arg)
325 "Display the Emacs TODO list."
327 (view-file (expand-file-name "TODO" data-directory)))
329 (defun view-echo-area-messages ()
330 "View the log of recent echo-area messages: the `*Messages*' buffer.
331 The number of messages retained in that buffer
332 is specified by the variable `message-log-max'."
334 (switch-to-buffer (get-buffer-create "*Messages*")))
336 (defun view-order-manuals ()
337 "Display the Emacs ORDERS file."
339 (view-file (expand-file-name "ORDERS" data-directory))
342 (defun view-emacs-FAQ ()
343 "Display the Emacs Frequently Asked Questions (FAQ) file."
345 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
348 (defun view-emacs-problems ()
349 "Display info on known problems with Emacs and possible workarounds."
351 (view-file (expand-file-name "PROBLEMS" data-directory)))
353 (defun view-lossage ()
354 "Display last 100 input keystrokes.
356 To record all your input on a file, use `open-dribble-file'."
358 (help-setup-xref (list #'view-lossage) (interactive-p))
359 (with-output-to-temp-buffer (help-buffer)
360 (princ (mapconcat (lambda (key)
361 (if (or (integerp key) (symbolp key) (listp key))
362 (single-key-description key)
363 (prin1-to-string key nil)))
366 (with-current-buffer standard-output
367 (goto-char (point-min))
368 (while (progn (move-to-column 50) (not (eobp)))
369 (search-forward " " nil t)
371 (print-help-return-message)))
376 (defun describe-bindings (&optional prefix buffer)
377 "Show a list of all defined keys, and their definitions.
378 We put that list in a buffer, and display the buffer.
380 The optional argument PREFIX, if non-nil, should be a key sequence;
381 then we display only bindings that start with that prefix.
382 The optional argument BUFFER specifies which buffer's bindings
383 to display (default, the current buffer)."
385 (or buffer (setq buffer (current-buffer)))
386 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
387 (with-current-buffer buffer
388 (describe-bindings-internal nil prefix)))
390 ;; This function used to be in keymap.c.
391 (defun describe-bindings-internal (&optional menus prefix)
392 "Show a list of all defined keys, and their definitions.
393 We put that list in a buffer, and display the buffer.
395 The optional argument MENUS, if non-nil, says to mention menu bindings.
396 \(Ordinarily these are omitted from the output.)
397 The optional argument PREFIX, if non-nil, should be a key sequence;
398 then we display only bindings that start with that prefix."
400 (let ((buf (current-buffer)))
401 (with-output-to-temp-buffer "*Help*"
402 (with-current-buffer standard-output
403 (describe-buffer-bindings buf prefix menus)))))
405 (defun where-is (definition &optional insert)
406 "Print message listing key sequences that invoke the command DEFINITION.
407 Argument is a command definition, usually a symbol with a function definition.
408 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
410 (let ((fn (function-called-at-point))
411 (enable-recursive-minibuffers t)
413 (setq val (completing-read (if fn
414 (format "Where is command (default %s): " fn)
415 "Where is command: ")
416 obarray 'commandp t))
417 (list (if (equal val "")
419 current-prefix-arg)))
420 (let* ((remapped (remap-command definition))
421 (keys (where-is-internal definition overriding-local-map nil nil remapped))
422 (keys1 (mapconcat 'key-description keys ", "))
423 (standard-output (if insert (current-buffer) t)))
425 (if (> (length keys1) 0)
427 (princ (format "%s (%s) (remapped from %s)" keys1 remapped definition))
428 (princ (format "%s (%s)" keys1 definition)))
429 (princ (format "M-x %s RET" definition)))
430 (if (> (length keys1) 0)
432 (princ (format "%s is remapped to %s which is on %s" definition remapped keys1))
433 (princ (format "%s is on %s" definition keys1)))
434 (princ (format "%s is not on any key" definition)))))
437 (defun string-key-binding (key)
438 "Value is the binding of KEY in a string.
439 If KEY is an event on a string, and that string has a `local-map'
440 or `keymap' property, return the binding of KEY in the string's keymap."
442 (start (when (vectorp key)
443 (if (memq (aref key 0)
444 '(mode-line header-line left-margin right-margin))
445 (event-start (aref key 1))
446 (and (consp (aref key 0))
447 (event-start (aref key 0))))))
448 (string-info (and (consp start) (nth 4 start))))
450 (let* ((string (car string-info))
451 (pos (cdr string-info))
452 (local-map (and (>= pos 0)
453 (< pos (length string))
454 (or (get-text-property pos 'local-map string)
455 (get-text-property pos 'keymap string)))))
456 (setq defn (and local-map (lookup-key local-map key)))))
459 (defun help-key-description (key untranslated)
460 (let ((string (key-description key)))
461 (if (or (not untranslated) (eq (aref untranslated 0) ?\e))
463 (let ((otherstring (key-description untranslated)))
464 (if (equal string otherstring)
466 (format "%s (translated from %s)" string otherstring))))))
468 (defun describe-key-briefly (key &optional insert untranslated)
469 "Print the name of the function KEY invokes. KEY is a string.
470 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
471 If non-nil UNTRANSLATED is a vector of the untranslated events.
472 It can also be a number in which case the untranslated events from
473 the last key hit are used."
474 (interactive "kDescribe key briefly: \nP\np")
475 (if (numberp untranslated)
476 (setq untranslated (this-single-command-raw-keys)))
478 (let ((modifiers (event-modifiers (aref key 0)))
479 (standard-output (if insert (current-buffer) t))
481 ;; For a mouse button event, go to the button it applies to
482 ;; to get the right key bindings. And go to the right place
483 ;; in case the keymap depends on where you clicked.
484 (if (or (memq 'click modifiers) (memq 'down modifiers)
485 (memq 'drag modifiers))
486 (setq window (posn-window (event-start (aref key 0)))
487 position (posn-point (event-start (aref key 0)))))
490 (set-buffer (window-buffer window))
491 (goto-char position)))
492 ;; Ok, now look up the key and name the command.
493 (let ((defn (or (string-key-binding key)
495 (key-desc (help-key-description key untranslated)))
496 (if (or (null defn) (integerp defn) (equal defn 'undefined))
497 (princ (format "%s is undefined" key-desc))
498 (princ (format (if (windowp window)
499 "%s at that spot runs the command %s"
500 "%s runs the command %s")
502 (if (symbolp defn) defn (prin1-to-string defn)))))))))
505 (defun describe-key (key &optional untranslated)
506 "Display documentation of the function invoked by KEY.
507 KEY should be a key sequence--when calling from a program,
508 pass a string or a vector.
509 If non-nil UNTRANSLATED is a vector of the untranslated events.
510 It can also be a number in which case the untranslated events from
511 the last key hit are used."
512 (interactive "kDescribe key: \np")
513 (if (numberp untranslated)
514 (setq untranslated (this-single-command-raw-keys)))
516 (let ((modifiers (event-modifiers (aref key 0)))
518 ;; For a mouse button event, go to the button it applies to
519 ;; to get the right key bindings. And go to the right place
520 ;; in case the keymap depends on where you clicked.
521 (if (or (memq 'click modifiers) (memq 'down modifiers)
522 (memq 'drag modifiers))
523 (setq window (posn-window (event-start (aref key 0)))
524 position (posn-point (event-start (aref key 0)))))
525 (when (windowp window)
526 (set-buffer (window-buffer window))
527 (goto-char position))
528 (let ((defn (or (string-key-binding key) (key-binding key))))
529 (if (or (null defn) (integerp defn) (equal defn 'undefined))
530 (message "%s is undefined" (help-key-description key untranslated))
531 (help-setup-xref (list #'describe-function defn) (interactive-p))
532 (with-output-to-temp-buffer (help-buffer)
533 (princ (help-key-description key untranslated))
535 (princ " at that spot"))
536 (princ " runs the command ")
538 (princ "\n which is ")
539 (describe-function-1 defn)
540 (print-help-return-message)))))))
543 (defun describe-mode (&optional buffer)
544 "Display documentation of current major mode and minor modes.
545 The major mode description comes first, followed by the minor modes,
546 each on a separate page.
547 For this to work correctly for a minor mode, the mode's indicator variable
548 \(listed in `minor-mode-alist') must also be a function whose documentation
549 describes the minor mode."
551 (help-setup-xref (list #'describe-mode (or buffer (current-buffer)))
553 ;; For the sake of help-do-xref and help-xref-go-back,
554 ;; don't switch buffers before calling `help-buffer'.
555 (with-output-to-temp-buffer (help-buffer)
557 (when buffer (set-buffer buffer))
558 (when minor-mode-alist
559 (princ "The major mode is described first.
560 For minor modes, see following pages.\n\n"))
563 (princ (documentation major-mode))
564 (let ((minor-modes minor-mode-alist))
566 (let* ((minor-mode (car (car minor-modes)))
567 (indicator (car (cdr (car minor-modes)))))
568 ;; Document a minor mode if it is listed in minor-mode-alist,
569 ;; bound locally in this buffer, non-nil, and has a function
571 (if (and (boundp minor-mode)
572 (symbol-value minor-mode)
573 (fboundp minor-mode))
574 (let ((pretty-minor-mode minor-mode))
575 (if (string-match "\\(-minor\\)?-mode\\'"
576 (symbol-name minor-mode))
577 (setq pretty-minor-mode
579 (substring (symbol-name minor-mode)
580 0 (match-beginning 0)))))
581 (while (and indicator (symbolp indicator)
583 (not (eq indicator (symbol-value indicator))))
584 (setq indicator (symbol-value indicator)))
586 (princ (format "%s minor mode (%s):\n"
589 (format "indicator%s" indicator)
591 (princ (documentation minor-mode)))))
592 (setq minor-modes (cdr minor-modes))))
593 (print-help-return-message))))
596 ;;; Automatic resizing of temporary buffers.
598 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
599 "*Maximum height of a window displaying a temporary buffer.
600 This is the maximum height (in text lines) which `resize-temp-buffer-window'
601 will give to a window displaying a temporary buffer.
602 It can also be a function which will be called with the object corresponding
603 to the buffer to be displayed as argument and should return an integer
605 :type '(choice integer function)
609 (define-minor-mode temp-buffer-resize-mode
610 "Toggle the mode which makes windows smaller for temporary buffers.
611 With prefix argument ARG, turn the resizing of windows displaying temporary
612 buffers on if ARG is positive or off otherwise.
613 This makes the window the right height for its contents, but never
614 more than `temp-buffer-max-height' nor less than `window-min-height'.
615 This applies to `help', `apropos' and `completion' buffers, and some others."
616 :global t :group 'help
617 (if temp-buffer-resize-mode
618 ;; `help-make-xrefs' may add a `back' button and thus increase the
619 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
620 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
621 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
623 (defun resize-temp-buffer-window ()
624 "Resize the current window to fit its contents.
625 Will not make it higher than `temp-buffer-max-height' nor smaller than
626 `window-min-height'. Do nothing if it is the only window on its frame, if it
627 is not as wide as the frame or if some of the window's contents are scrolled
629 (unless (or (one-window-p 'nomini)
630 (not (pos-visible-in-window-p (point-min)))
631 (/= (frame-width) (window-width)))
632 (fit-window-to-buffer
634 (if (functionp temp-buffer-max-height)
635 (funcall temp-buffer-max-height (current-buffer))
636 temp-buffer-max-height))))
638 ;; Provide this for the sake of define-minor-mode which generates
639 ;; defcustoms which require 'help'.
642 ;;; help.el ends here