Doc fixes. Remove redundant
[emacs.git] / lisp / help.el
blobfe7782d276a9493abe52b82032337928fdade27f
1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
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)
14 ;; any later version.
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.
26 ;;; Commentary:
28 ;; This code implements GNU Emacs' on-line help system, the one invoked by
29 ;; `M-x help-for-help'.
31 ;;; Code:
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-w" 'describe-no-warranty)
63 ;; This does not fit the pattern, but it is natural given the C-\ command.
64 (define-key help-map "\C-\\" 'describe-input-method)
66 (define-key help-map "C" 'describe-coding-system)
67 (define-key help-map "F" 'Info-goto-emacs-command-node)
68 (define-key help-map "I" 'describe-input-method)
69 (define-key help-map "K" 'Info-goto-emacs-key-command-node)
70 (define-key help-map "L" 'describe-language-environment)
71 (define-key help-map "S" 'info-lookup-symbol)
73 (define-key help-map "a" 'apropos-command)
75 (define-key help-map "b" 'describe-bindings)
77 (define-key help-map "c" 'describe-key-briefly)
79 (define-key help-map "e" 'view-echo-area-messages)
81 (define-key help-map "f" 'describe-function)
83 (define-key help-map "h" 'view-hello-file)
85 (define-key help-map "i" 'info)
86 (define-key help-map "4i" 'info-other-window)
88 (define-key help-map "k" 'describe-key)
90 (define-key help-map "l" 'view-lossage)
92 (define-key help-map "m" 'describe-mode)
94 (define-key help-map "n" 'view-emacs-news)
96 (define-key help-map "p" 'finder-by-keyword)
97 (autoload 'finder-by-keyword "finder"
98 "Find packages matching a given keyword." t)
100 (define-key help-map "s" 'describe-syntax)
102 (define-key help-map "t" 'help-with-tutorial)
104 (define-key help-map "w" 'where-is)
106 (define-key help-map "v" 'describe-variable)
108 (define-key help-map "q" 'help-quit)
111 (defun help-quit ()
112 "Just exit from the Help command's command loop."
113 (interactive)
114 nil)
116 (defvar help-return-method nil
117 "What to do to \"exit\" the help buffer.
118 This is a list
119 (WINDOW . t) delete the selected window, go to WINDOW.
120 (WINDOW . quit-window) do quit-window, then select WINDOW.
121 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
123 (defun print-help-return-message (&optional function)
124 "Display or return message saying how to restore windows after help command.
125 Computes a message and applies the optional argument FUNCTION to it.
126 If FUNCTION is nil, applies `message' to it, thus printing it."
127 (and (not (get-buffer-window standard-output))
128 (let ((first-message
129 (cond ((special-display-p (buffer-name standard-output))
130 (setq help-return-method (cons (selected-window) t))
131 ;; If the help output buffer is a special display buffer,
132 ;; don't say anything about how to get rid of it.
133 ;; First of all, the user will do that with the window
134 ;; manager, not with Emacs.
135 ;; Secondly, the buffer has not been displayed yet,
136 ;; so we don't know whether its frame will be selected.
137 nil)
138 (display-buffer-reuse-frames
139 (setq help-return-method (cons (selected-window)
140 'quit-window))
141 nil)
142 ((not (one-window-p t))
143 (setq help-return-method
144 (cons (selected-window) 'quit-window))
145 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
146 (pop-up-windows
147 (setq help-return-method (cons (selected-window) t))
148 "Type \\[delete-other-windows] to remove help window.")
150 (setq help-return-method
151 (list (selected-window) (window-buffer)
152 (window-start) (window-point)))
153 "Type \\[switch-to-buffer] RET to remove help window."))))
154 (funcall (or function 'message)
155 (concat
156 (if first-message
157 (substitute-command-keys first-message))
158 (if first-message " ")
159 ;; If the help buffer will go in a separate frame,
160 ;; it's no use mentioning a command to scroll, so don't.
161 (if (special-display-p (buffer-name standard-output))
163 (if (same-window-p (buffer-name standard-output))
164 ;; Say how to scroll this window.
165 (substitute-command-keys
166 "\\[scroll-up] to scroll the help.")
167 ;; Say how to scroll some other window.
168 (substitute-command-keys
169 "\\[scroll-other-window] to scroll the help."))))))))
171 ;; So keyboard macro definitions are documented correctly
172 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
174 (defalias 'help 'help-for-help)
175 (make-help-screen help-for-help
176 "a b c C e f F C-f i I k C-k l L m p s t v w C-c C-d C-n C-p C-w or ? :"
177 "You have typed %THIS-KEY%, the help character. Type a Help option:
178 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
180 a command-apropos. Give a substring, and see a list of commands
181 (functions interactively callable) that contain
182 that substring. See also the apropos command.
183 b describe-bindings. Display table of all key bindings.
184 c describe-key-briefly. Type a command key sequence;
185 it prints the function name that sequence runs.
186 C describe-coding-system. This describes either a specific coding system
187 (if you type its name) or the coding systems currently in use
188 (if you type just RET).
189 e view-echo-area-messages. Show the `*Messages*' buffer.
190 f describe-function. Type a function name and get documentation of it.
191 F Info-goto-emacs-command-node. Type a function name;
192 it takes you to the Info node for that command.
193 h Display the HELLO file which illustrates various scripts.
194 i info. The info documentation reader.
195 I describe-input-method. Describe a specific input method (if you type
196 its name) or the current input method (if you type just RET).
197 k describe-key. Type a command key sequence;
198 it displays the full documentation.
199 K Info-goto-emacs-key-command-node. Type a command key sequence;
200 it takes you to the Info node for the command bound to that key.
201 l view-lossage. Show last 100 characters you typed.
202 L describe-language-environment. This describes either a
203 specific language environment (if you type its name)
204 or the current language environment (if you type just RET).
205 m describe-mode. Print documentation of current minor modes,
206 and the current major mode, including their special commands.
207 p finder-by-keyword. Find packages matching a given topic keyword.
208 s describe-syntax. Display contents of syntax table, plus explanations.
209 S info-lookup-symbol. Display the definition of a specific symbol
210 as found in the manual for the language this buffer is written in.
211 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
212 v describe-variable. Type name of a variable;
213 it displays the variable's documentation and value.
214 w where-is. Type command name; it prints which keystrokes
215 invoke that command.
217 C-c Display Emacs copying permission (GNU General Public License).
218 C-d Display Emacs ordering information.
219 C-e Display info about Emacs problems.
220 C-f Display the Emacs FAQ.
221 C-m Display how to order printed Emacs manuals.
222 C-n Display news of recent Emacs changes.
223 C-p Display information about the GNU project.
224 C-w Display information on absence of warranty for GNU Emacs."
225 help-map)
229 (defun function-called-at-point ()
230 "Return a function around point or else called by the list containing point.
231 If that doesn't give a function, return nil."
232 (with-syntax-table emacs-lisp-mode-syntax-table
233 (or (condition-case ()
234 (save-excursion
235 (or (not (zerop (skip-syntax-backward "_w")))
236 (eq (char-syntax (following-char)) ?w)
237 (eq (char-syntax (following-char)) ?_)
238 (forward-sexp -1))
239 (skip-chars-forward "'")
240 (let ((obj (read (current-buffer))))
241 (and (symbolp obj) (fboundp obj) obj)))
242 (error nil))
243 (condition-case ()
244 (save-excursion
245 (save-restriction
246 (narrow-to-region (max (point-min)
247 (- (point) 1000)) (point-max))
248 ;; Move up to surrounding paren, then after the open.
249 (backward-up-list 1)
250 (forward-char 1)
251 ;; If there is space here, this is probably something
252 ;; other than a real Lisp function call, so ignore it.
253 (if (looking-at "[ \t]")
254 (error "Probably not a Lisp function call"))
255 (let ((obj (read (current-buffer))))
256 (and (symbolp obj) (fboundp obj) obj))))
257 (error nil)))))
260 ;;; `User' help functions
262 (defun describe-distribution ()
263 "Display info on how to obtain the latest version of GNU Emacs."
264 (interactive)
265 (view-file (expand-file-name "DISTRIB" data-directory)))
267 (defun describe-copying ()
268 "Display info on how you may redistribute copies of GNU Emacs."
269 (interactive)
270 (view-file (expand-file-name "COPYING" data-directory))
271 (goto-char (point-min)))
273 (defun describe-project ()
274 "Display info on the GNU project."
275 (interactive)
276 (view-file (expand-file-name "THE-GNU-PROJECT" data-directory))
277 (goto-char (point-min)))
279 (defun describe-no-warranty ()
280 "Display info on all the kinds of warranty Emacs does NOT have."
281 (interactive)
282 (describe-copying)
283 (let (case-fold-search)
284 (search-forward "NO WARRANTY")
285 (recenter 0)))
287 (defun describe-prefix-bindings ()
288 "Describe the bindings of the prefix used to reach this command.
289 The prefix described consists of all but the last event
290 of the key sequence that ran this command."
291 (interactive)
292 (let* ((key (this-command-keys)))
293 (describe-bindings
294 (if (stringp key)
295 (substring key 0 (1- (length key)))
296 (let ((prefix (make-vector (1- (length key)) nil))
297 (i 0))
298 (while (< i (length prefix))
299 (aset prefix i (aref key i))
300 (setq i (1+ i)))
301 prefix)))))
302 ;; Make C-h after a prefix, when not specifically bound,
303 ;; run describe-prefix-bindings.
304 (setq prefix-help-command 'describe-prefix-bindings)
306 (defun view-emacs-news (&optional arg)
307 "Display info on recent changes to Emacs.
308 With numeric argument, display information on correspondingly older changes."
309 (interactive "P")
310 (let* ((arg (if arg (prefix-numeric-value arg) 0))
311 (file (cond ((eq arg 0) "NEWS")
312 ((eq arg 1) "ONEWS")
314 (nth (- arg 2)
315 (nreverse (directory-files data-directory
316 nil "^ONEWS\\.[0-9]+$"
317 nil)))))))
318 (if file
319 (view-file (expand-file-name file data-directory))
320 (error "No such old news"))))
322 (defun view-echo-area-messages ()
323 "View the log of recent echo-area messages: the `*Messages*' buffer.
324 The number of messages retained in that buffer
325 is specified by the variable `message-log-max'."
326 (interactive)
327 (switch-to-buffer (get-buffer-create "*Messages*")))
329 (defun view-order-manuals ()
330 "Display the Emacs ORDERS file."
331 (interactive)
332 (view-file (expand-file-name "ORDERS" data-directory))
333 (goto-address))
335 (defun view-emacs-FAQ ()
336 "Display the Emacs Frequently Asked Questions (FAQ) file."
337 (interactive)
338 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
339 (info "(efaq)"))
341 (defun view-emacs-problems ()
342 "Display info on known problems with Emacs and possible workarounds."
343 (interactive)
344 (view-file (expand-file-name "PROBLEMS" data-directory)))
346 (defun view-lossage ()
347 "Display last 100 input keystrokes.
349 To record all your input on a file, use `open-dribble-file'."
350 (interactive)
351 (help-setup-xref (list #'view-lossage) (interactive-p))
352 (with-output-to-temp-buffer (help-buffer)
353 (princ (mapconcat (function (lambda (key)
354 (if (or (integerp key)
355 (symbolp key)
356 (listp key))
357 (single-key-description key)
358 (prin1-to-string key nil))))
359 (recent-keys)
360 " "))
361 (with-current-buffer standard-output
362 (goto-char (point-min))
363 (while (progn (move-to-column 50) (not (eobp)))
364 (search-forward " " nil t)
365 (insert "\n")))
366 (print-help-return-message)))
369 ;; Key bindings
371 (defun describe-bindings (&optional prefix buffer)
372 "Show a list of all defined keys, and their definitions.
373 We put that list in a buffer, and display the buffer.
375 The optional argument PREFIX, if non-nil, should be a key sequence;
376 then we display only bindings that start with that prefix.
377 The optional argument BUFFER specifies which buffer's bindings
378 to display (default, the current buffer)."
379 (interactive)
380 (or buffer (setq buffer (current-buffer)))
381 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
382 (with-current-buffer buffer
383 (describe-bindings-internal nil prefix)))
385 ;; This function used to be in keymap.c.
386 (defun describe-bindings-internal (&optional menus prefix)
387 "Show a list of all defined keys, and their definitions.
388 We put that list in a buffer, and display the buffer.
390 The optional argument MENUS, if non-nil, says to mention menu bindings.
391 \(Ordinarily these are omitted from the output.)
392 The optional argument PREFIX, if non-nil, should be a key sequence;
393 then we display only bindings that start with that prefix."
394 (interactive)
395 (let ((buf (current-buffer)))
396 (with-output-to-temp-buffer "*Help*"
397 (with-current-buffer standard-output
398 (describe-buffer-bindings buf prefix menus)))))
400 (defun where-is (definition &optional insert)
401 "Print message listing key sequences that invoke the command DEFINITION.
402 Argument is a command definition, usually a symbol with a function definition.
403 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
404 (interactive
405 (let ((fn (function-called-at-point))
406 (enable-recursive-minibuffers t)
407 val)
408 (setq val (completing-read (if fn
409 (format "Where is command (default %s): " fn)
410 "Where is command: ")
411 obarray 'commandp t))
412 (list (if (equal val "")
413 fn (intern val))
414 current-prefix-arg)))
415 (let* ((remapped (remap-command definition))
416 (keys (where-is-internal definition overriding-local-map nil nil remapped))
417 (keys1 (mapconcat 'key-description keys ", "))
418 (standard-output (if insert (current-buffer) t)))
419 (if insert
420 (if (> (length keys1) 0)
421 (if remapped
422 (princ (format "%s (%s) (remapped from %s)" keys1 remapped definition))
423 (princ (format "%s (%s)" keys1 definition)))
424 (princ (format "M-x %s RET" definition)))
425 (if (> (length keys1) 0)
426 (if remapped
427 (princ (format "%s is remapped to %s which is on %s" definition remapped keys1))
428 (princ (format "%s is on %s" definition keys1)))
429 (princ (format "%s is not on any key" definition)))))
430 nil)
432 (defun string-key-binding (key)
433 "Value is the binding of KEY in a string.
434 If KEY is an event on a string, and that string has a `local-map'
435 or `keymap' property, return the binding of KEY in the string's keymap."
436 (let* ((defn nil)
437 (start (when (vectorp key)
438 (if (memq (aref key 0) '(mode-line header-line))
439 (event-start (aref key 1))
440 (and (consp (aref key 0))
441 (event-start (aref key 0))))))
442 (string-info (and (consp start) (nth 4 start))))
443 (when string-info
444 (let* ((string (car string-info))
445 (pos (cdr string-info))
446 (local-map (and (> pos 0)
447 (< pos (length string))
448 (or (get-text-property pos 'local-map string)
449 (get-text-property pos 'keymap string)))))
450 (setq defn (and local-map (lookup-key local-map key)))))
451 defn))
453 (defun describe-key-briefly (key &optional insert)
454 "Print the name of the function KEY invokes. KEY is a string.
455 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
456 (interactive "kDescribe key briefly: \nP")
457 (save-excursion
458 (let ((modifiers (event-modifiers (aref key 0)))
459 (standard-output (if insert (current-buffer) t))
460 window position)
461 ;; For a mouse button event, go to the button it applies to
462 ;; to get the right key bindings. And go to the right place
463 ;; in case the keymap depends on where you clicked.
464 (if (or (memq 'click modifiers) (memq 'down modifiers)
465 (memq 'drag modifiers))
466 (setq window (posn-window (event-start (aref key 0)))
467 position (posn-point (event-start (aref key 0)))))
468 (if (windowp window)
469 (progn
470 (set-buffer (window-buffer window))
471 (goto-char position)))
472 ;; Ok, now look up the key and name the command.
473 (let ((defn (or (string-key-binding key)
474 (key-binding key)))
475 (key-desc (key-description key)))
476 (if (or (null defn) (integerp defn))
477 (princ (format "%s is undefined" key-desc))
478 (princ (format (if (windowp window)
479 "%s at that spot runs the command %s"
480 "%s runs the command %s")
481 key-desc
482 (if (symbolp defn) defn (prin1-to-string defn)))))))))
485 (defun describe-key (key)
486 "Display documentation of the function invoked by KEY.
487 KEY should be a key sequence--when calling from a program,
488 pass a string or a vector."
489 (interactive "kDescribe key: ")
490 (save-excursion
491 (let ((modifiers (event-modifiers (aref key 0)))
492 window position)
493 ;; For a mouse button event, go to the button it applies to
494 ;; to get the right key bindings. And go to the right place
495 ;; in case the keymap depends on where you clicked.
496 (if (or (memq 'click modifiers) (memq 'down modifiers)
497 (memq 'drag modifiers))
498 (setq window (posn-window (event-start (aref key 0)))
499 position (posn-point (event-start (aref key 0)))))
500 (when (windowp window)
501 (set-buffer (window-buffer window))
502 (goto-char position))
503 (let ((defn (or (string-key-binding key) (key-binding key))))
504 (if (or (null defn) (integerp defn))
505 (message "%s is undefined" (key-description key))
506 (help-setup-xref (list #'describe-function defn) (interactive-p))
507 (with-output-to-temp-buffer (help-buffer)
508 (princ (key-description key))
509 (if (windowp window)
510 (princ " at that spot"))
511 (princ " runs the command ")
512 (prin1 defn)
513 (princ "\n which is ")
514 (describe-function-1 defn)
515 (print-help-return-message)))))))
518 (defun describe-mode (&optional buffer)
519 "Display documentation of current major mode and minor modes.
520 The major mode description comes first, followed by the minor modes,
521 each on a separate page.
522 For this to work correctly for a minor mode, the mode's indicator variable
523 \(listed in `minor-mode-alist') must also be a function whose documentation
524 describes the minor mode."
525 (interactive)
526 (help-setup-xref (list #'describe-mode (or buffer (current-buffer)))
527 (interactive-p))
528 ;; For the sake of help-do-xref and help-xref-go-back,
529 ;; don't switch buffers before calling `help-buffer'.
530 (with-output-to-temp-buffer (help-buffer)
531 (save-excursion
532 (when buffer (set-buffer buffer))
533 (when minor-mode-alist
534 (princ "The major mode is described first.
535 For minor modes, see following pages.\n\n"))
536 (princ mode-name)
537 (princ " mode:\n")
538 (princ (documentation major-mode))
539 (let ((minor-modes minor-mode-alist))
540 (while minor-modes
541 (let* ((minor-mode (car (car minor-modes)))
542 (indicator (car (cdr (car minor-modes)))))
543 ;; Document a minor mode if it is listed in minor-mode-alist,
544 ;; bound locally in this buffer, non-nil, and has a function
545 ;; definition.
546 (if (and (boundp minor-mode)
547 (symbol-value minor-mode)
548 (fboundp minor-mode))
549 (let ((pretty-minor-mode minor-mode))
550 (if (string-match "\\(-minor\\)?-mode\\'"
551 (symbol-name minor-mode))
552 (setq pretty-minor-mode
553 (capitalize
554 (substring (symbol-name minor-mode)
555 0 (match-beginning 0)))))
556 (while (and indicator (symbolp indicator)
557 (boundp indicator)
558 (not (eq indicator (symbol-value indicator))))
559 (setq indicator (symbol-value indicator)))
560 (princ "\n\f\n")
561 (princ (format "%s minor mode (%s):\n"
562 pretty-minor-mode
563 (if indicator
564 (format "indicator%s" indicator)
565 "no indicator")))
566 (princ (documentation minor-mode)))))
567 (setq minor-modes (cdr minor-modes))))
568 (print-help-return-message))))
571 ;;; Automatic resizing of temporary buffers.
573 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
574 "*Maximum height of a window displaying a temporary buffer.
575 This is the maximum height (in text lines) which `resize-temp-buffer-window'
576 will give to a window displaying a temporary buffer.
577 It can also be a function which will be called with the object corresponding
578 to the buffer to be displayed as argument and should return an integer
579 positive number."
580 :type '(choice integer function)
581 :group 'help
582 :version "20.4")
584 (define-minor-mode temp-buffer-resize-mode
585 "Toggle the mode which makes windows smaller for temporary buffers.
586 With prefix argument ARG, turn the resizing of windows displaying temporary
587 buffers on if ARG is positive or off otherwise.
588 This makes the window the right height for its contents, but never
589 more than `temp-buffer-max-height' nor less than `window-min-height'.
590 This applies to `help', `apropos' and `completion' buffers, and some others."
591 :global t :group 'help
592 (if temp-buffer-resize-mode
593 ;; `help-make-xrefs' may add a `back' button and thus increase the
594 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
595 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
596 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
598 (defun resize-temp-buffer-window ()
599 "Resize the current window to fit its contents.
600 Will not make it higher than `temp-buffer-max-height' nor smaller than
601 `window-min-height'. Do nothing if it is the only window on its frame, if it
602 is not as wide as the frame or if some of the window's contents are scrolled
603 out of view."
604 (unless (or (one-window-p 'nomini)
605 (not (pos-visible-in-window-p (point-min)))
606 (/= (frame-width) (window-width)))
607 (fit-window-to-buffer
608 (selected-window)
609 (if (functionp temp-buffer-max-height)
610 (funcall temp-buffer-max-height (current-buffer))
611 temp-buffer-max-height))))
613 ;; Provide this for the sake of define-minor-mode which generates
614 ;; defcustoms which require 'help'.
615 (provide 'help)
617 ;;; help.el ends here