1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2014 Free Software
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: help, internal
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This code implements GNU Emacs's on-line help system, the one invoked by
28 ;; `M-x help-for-help'.
32 ;; Get the macro make-help-screen when this is compiled,
33 ;; or run interpreted, but not when the compiled code is loaded.
34 (eval-when-compile (require 'help-macro
))
36 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
37 (add-hook 'temp-buffer-setup-hook
'help-mode-setup
)
38 (add-hook 'temp-buffer-show-hook
'help-mode-finish
)
40 ;; `help-window-point-marker' is a marker you can move to a valid
41 ;; position of the buffer shown in the help window in order to override
42 ;; the standard positioning mechanism (`point-min') chosen by
43 ;; `with-output-to-temp-buffer' and `with-temp-buffer-window'.
44 ;; `with-help-window' has this point nowhere before exiting. Currently
45 ;; used by `view-lossage' to assert that the last keystrokes are always
47 (defvar help-window-point-marker
(make-marker)
48 "Marker to override default `window-point' in help windows.")
51 (let ((map (make-sparse-keymap)))
52 (define-key map
(char-to-string help-char
) 'help-for-help
)
53 (define-key map
[help] 'help-for-help)
54 (define-key map [f1] 'help-for-help)
55 (define-key map "." 'display-local-help)
56 (define-key map "?" 'help-for-help)
58 (define-key map "\C-a" 'about-emacs)
59 (define-key map "\C-c" 'describe-copying)
60 (define-key map "\C-d" 'view-emacs-debugging)
61 (define-key map "\C-e" 'view-external-packages)
62 (define-key map "\C-f" 'view-emacs-FAQ)
63 (define-key map "\C-m" 'view-order-manuals)
64 (define-key map "\C-n" 'view-emacs-news)
65 (define-key map "\C-o" 'describe-distribution)
66 (define-key map "\C-p" 'view-emacs-problems)
67 (define-key map "\C-t" 'view-emacs-todo)
68 (define-key map "\C-w" 'describe-no-warranty)
70 ;; This does not fit the pattern, but it is natural given the C-\ command.
71 (define-key map "\C-\\" 'describe-input-method)
73 (define-key map "C" 'describe-coding-system)
74 (define-key map "F" 'Info-goto-emacs-command-node)
75 (define-key map "I" 'describe-input-method)
76 (define-key map "K" 'Info-goto-emacs-key-command-node)
77 (define-key map "L" 'describe-language-environment)
78 (define-key map "S" 'info-lookup-symbol)
80 (define-key map "a" 'apropos-command)
81 (define-key map "b" 'describe-bindings)
82 (define-key map "c" 'describe-key-briefly)
83 (define-key map "d" 'apropos-documentation)
84 (define-key map "e" 'view-echo-area-messages)
85 (define-key map "f" 'describe-function)
86 (define-key map "g" 'describe-gnu-project)
87 (define-key map "h" 'view-hello-file)
89 (define-key map "i" 'info)
90 (define-key map "4i" 'info-other-window)
92 (define-key map "k" 'describe-key)
93 (define-key map "l" 'view-lossage)
94 (define-key map "m" 'describe-mode)
95 (define-key map "n" 'view-emacs-news)
96 (define-key map "p" 'finder-by-keyword)
97 (define-key map "P" 'describe-package)
98 (define-key map "r" 'info-emacs-manual)
99 (define-key map "s" 'describe-syntax)
100 (define-key map "t" 'help-with-tutorial)
101 (define-key map "w" 'where-is)
102 (define-key map "v" 'describe-variable)
103 (define-key map "q" 'help-quit)
105 "Keymap for characters following the Help key.")
107 (define-key global-map (char-to-string help-char) 'help-command)
108 (define-key global-map [help] 'help-command
)
109 (define-key global-map
[f1] 'help-command)
110 (fset 'help-command help-map)
112 ;; insert-button makes the action nil if it is not store somewhere
113 (defvar help-button-cache nil)
117 "Just exit from the Help command's command loop."
121 (defvar help-return-method nil
122 "What to do to \"exit\" the help buffer.
124 (WINDOW . t) delete the selected window (and possibly its frame,
125 see `quit-window'), go to WINDOW.
126 (WINDOW . quit-window) do quit-window, then select WINDOW.
127 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
129 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
130 (defun help-print-return-message (&optional function)
131 "Display or return message saying how to restore windows after help command.
132 This function assumes that `standard-output' is the help buffer.
133 It computes a message, and applies the optional argument FUNCTION to it.
134 If FUNCTION is nil, it applies `message', thus displaying the message.
135 In addition, this function sets up `help-return-method', which see, that
136 specifies what to do when the user exits the help buffer."
137 (and (not (get-buffer-window standard-output))
141 (special-display-p (buffer-name standard-output)))
142 (setq help-return-method (cons (selected-window) t))
143 ;; If the help output buffer is a special display buffer,
144 ;; don't say anything about how to get rid of it.
145 ;; First of all, the user will do that with the window
146 ;; manager, not with Emacs.
147 ;; Secondly, the buffer has not been displayed yet,
148 ;; so we don't know whether its frame will be selected.
150 ((not (one-window-p t))
151 (setq help-return-method
152 (cons (selected-window) 'quit-window))
153 "Type \\[display-buffer] RET to restore the other window.")
155 (setq help-return-method (cons (selected-window) t))
156 "Type \\[delete-other-windows] to remove help window.")
158 (setq help-return-method
159 (list (selected-window) (window-buffer)
160 (window-start) (window-point)))
161 "Type \\[switch-to-buffer] RET to remove help window."))))
162 (funcall (or function 'message)
165 (substitute-command-keys first-message))
166 (if first-message " ")
167 ;; If the help buffer will go in a separate frame,
168 ;; it's no use mentioning a command to scroll, so don't.
169 (if (or pop-up-windows
170 (special-display-p (buffer-name standard-output)))
172 (if (same-window-p (buffer-name standard-output))
173 ;; Say how to scroll this window.
174 (substitute-command-keys
175 "\\[scroll-up] to scroll the help.")
176 ;; Say how to scroll some other window.
177 (substitute-command-keys
178 "\\[scroll-other-window] to scroll the help."))))))))
180 ;; So keyboard macro definitions are documented correctly
181 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
183 (defalias 'help 'help-for-help-internal)
184 ;; find-function can find this.
185 (defalias 'help-for-help 'help-for-help-internal)
186 ;; It can't find this, but nobody will look.
187 (make-help-screen help-for-help-internal
188 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
189 ;; Don't purecopy this one, because it's not evaluated (it's
190 ;; directly used as a docstring in a function definition, so it'll
191 ;; be moved to the DOC file anyway: no need for purecopying it).
192 "You have typed %THIS-KEY%, the help character. Type a Help option:
193 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
195 a PATTERN Show commands whose name matches the PATTERN (a list of words
196 or a regexp). See also the `apropos' command.
197 b Display all key bindings.
198 c KEYS Display the command name run by the given key sequence.
199 C CODING Describe the given coding system, or RET for current ones.
200 d PATTERN Show a list of functions, variables, and other items whose
201 documentation matches the PATTERN (a list of words or a regexp).
202 e Go to the *Messages* buffer which logs echo-area messages.
203 f FUNCTION Display documentation for the given function.
204 F COMMAND Show the on-line manual's section that describes the command.
205 g Display information about the GNU project.
206 h Display the HELLO file which illustrates various scripts.
207 i Start the Info documentation reader: read on-line manuals.
208 I METHOD Describe a specific input method, or RET for current.
209 k KEYS Display the full documentation for the key sequence.
210 K KEYS Show the on-line manual's section for the command bound to KEYS.
211 l Show last 300 input keystrokes (lossage).
212 L LANG-ENV Describes a specific language environment, or RET for current.
213 m Display documentation of current minor modes and current major mode,
214 including their special commands.
215 n Display news of recent Emacs changes.
216 p TOPIC Find packages matching a given topic keyword.
217 P PACKAGE Describe the given Emacs Lisp package.
218 r Display the Emacs manual in Info mode.
219 s Display contents of current syntax table, plus explanations.
220 S SYMBOL Show the section for the given symbol in the on-line manual
221 for the programming language used in this buffer.
222 t Start the Emacs learn-by-doing tutorial.
223 v VARIABLE Display the given variable's documentation and value.
224 w COMMAND Display which keystrokes invoke the given command (where-is).
225 . Display any available local help at point in the echo area.
227 C-a Information about Emacs.
228 C-c Emacs copying permission (GNU General Public License).
229 C-d Instructions for debugging GNU Emacs.
230 C-e External packages and information about Emacs.
232 C-m How to order printed Emacs manuals.
233 C-n News of recent Emacs changes.
234 C-o Emacs ordering and distribution information.
235 C-p Info about known Emacs problems.
237 C-w Information on absence of warranty for GNU Emacs."
242 (defun function-called-at-point ()
243 "Return a function around point or else called by the list containing point.
244 If that doesn't give a function, return nil."
245 (with-syntax-table emacs-lisp-mode-syntax-table
246 (or (condition-case ()
248 (or (not (zerop (skip-syntax-backward "_w")))
249 (eq (char-syntax (following-char)) ?w)
250 (eq (char-syntax (following-char)) ?_)
252 (skip-chars-forward "'")
253 (let ((obj (read (current-buffer))))
254 (and (symbolp obj) (fboundp obj) obj)))
259 (narrow-to-region (max (point-min)
260 (- (point) 1000)) (point-max))
261 ;; Move up to surrounding paren, then after the open.
264 ;; If there is space here, this is probably something
265 ;; other than a real Lisp function call, so ignore it.
266 (if (looking-at "[ \t]")
267 (error "Probably not a Lisp function call"))
268 (let ((obj (read (current-buffer))))
269 (and (symbolp obj) (fboundp obj) obj))))
271 (let* ((str (find-tag-default))
272 (sym (if str (intern-soft str))))
273 (if (and sym (fboundp sym))
276 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
277 (setq sym (intern-soft (match-string 1 str)))
278 (and (fboundp sym) sym))))))))
281 ;;; `User' help functions
283 (defun view-help-file (file &optional dir)
284 (view-file (expand-file-name file (or dir data-directory)))
285 (goto-address-mode 1)
286 (goto-char (point-min)))
288 (defun describe-distribution ()
289 "Display info on how to obtain the latest version of GNU Emacs."
291 (view-help-file "DISTRIB"))
293 (defun describe-copying ()
294 "Display info on how you may redistribute copies of GNU Emacs."
296 (view-help-file "COPYING"))
298 (defun describe-gnu-project ()
299 "Display info on the GNU project."
301 (view-help-file "THE-GNU-PROJECT"))
303 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
305 (defun describe-no-warranty ()
306 "Display info on all the kinds of warranty Emacs does NOT have."
309 (let (case-fold-search)
310 (search-forward "Disclaimer of Warranty")
314 (defun describe-prefix-bindings ()
315 "Describe the bindings of the prefix used to reach this command.
316 The prefix described consists of all but the last event
317 of the key sequence that ran this command."
319 (let ((key (this-command-keys)))
322 (substring key 0 (1- (length key)))
323 (let ((prefix (make-vector (1- (length key)) nil))
325 (while (< i (length prefix))
326 (aset prefix i (aref key i))
329 ;; Make C-h after a prefix, when not specifically bound,
330 ;; run describe-prefix-bindings.
331 (setq prefix-help-command 'describe-prefix-bindings)
333 (defun view-emacs-news (&optional version)
334 "Display info on recent changes to Emacs.
335 With argument, display info only for the selected version."
338 (setq version emacs-major-version))
339 (when (consp version)
345 (insert-file-contents
346 (expand-file-name file data-directory))
347 (while (re-search-forward
348 (if (member file '("NEWS.18" "NEWS.1-17"))
349 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
350 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
351 (setq res (cons (match-string-no-properties 1) res)))))
353 (directory-files data-directory nil
354 "^NEWS\\.[0-9][-0-9]*$" nil)))
355 (sort (delete-dups res) (lambda (a b) (string< b a)))))
356 (current (car all-versions)))
357 (setq version (completing-read
358 (format "Read NEWS for the version (default %s): " current)
359 all-versions nil nil nil nil current))
360 (if (integerp (string-to-number version))
361 (setq version (string-to-number version))
362 (unless (or (member version all-versions)
363 (<= (string-to-number version) (string-to-number current)))
364 (error "No news about version %s" version)))))
365 (when (integerp version)
366 (cond ((<= version 12)
367 (setq version (format "1.%d" version)))
369 (setq version (format "%d" version)))
370 ((> version emacs-major-version)
371 (error "No news about Emacs %d (yet)" version))))
372 (let* ((vn (if (stringp version)
373 (string-to-number version)
376 ((>= vn emacs-major-version) "NEWS")
377 ((< vn 18) "NEWS.1-17")
378 (t (format "NEWS.%d" vn))))
380 (view-file (expand-file-name file data-directory))
382 (goto-char (point-min))
383 (when (stringp version)
384 (when (re-search-forward
385 (concat (if (< vn 19)
386 "Changes in Emacs[ \t]*"
387 "^\* [^0-9\n]*") version "$")
393 (while (and (setq res
396 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
397 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
398 (equal (match-string-no-properties 1) version)))
399 (or res (goto-char (point-max)))
403 (defun view-emacs-todo (&optional _arg)
404 "Display the Emacs TODO list."
406 (view-help-file "TODO"))
408 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
411 (defun view-echo-area-messages ()
412 "View the log of recent echo-area messages: the `*Messages*' buffer.
413 The number of messages retained in that buffer
414 is specified by the variable `message-log-max'."
416 (with-current-buffer (messages-buffer)
417 (goto-char (point-max))
418 (display-buffer (current-buffer))))
420 (defun view-order-manuals ()
421 "Display information on how to buy printed copies of Emacs manuals."
423 ;; (view-help-file "ORDERS")
424 (info "(emacs)Printed Books"))
426 (defun view-emacs-FAQ ()
427 "Display the Emacs Frequently Asked Questions (FAQ) file."
429 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
432 (defun view-emacs-problems ()
433 "Display info on known problems with Emacs and possible workarounds."
435 (view-help-file "PROBLEMS"))
437 (defun view-emacs-debugging ()
438 "Display info on how to debug Emacs problems."
440 (view-help-file "DEBUG"))
442 ;; This used to visit MORE.STUFF; maybe it should just be removed.
443 (defun view-external-packages ()
444 "Display info on where to get more Emacs packages."
446 (info "(efaq)Packages that do not come with Emacs"))
448 (defun view-lossage ()
449 "Display last 300 input keystrokes.
451 To record all your input on a file, use `open-dribble-file'."
453 (help-setup-xref (list #'view-lossage)
454 (called-interactively-p 'interactive))
455 (with-help-window (help-buffer)
456 (princ (mapconcat (lambda (key)
457 (if (or (integerp key) (symbolp key) (listp key))
458 (single-key-description key)
459 (prin1-to-string key nil)))
462 (with-current-buffer standard-output
463 (goto-char (point-min))
464 (while (progn (move-to-column 50) (not (eobp)))
465 (when (search-forward " " nil t)
468 ;; jidanni wants to see the last keystrokes immediately.
469 (set-marker help-window-point-marker (point)))))
474 (defun describe-bindings (&optional prefix buffer)
475 "Display a buffer showing a list of all defined keys, and their definitions.
476 The keys are displayed in order of precedence.
478 The optional argument PREFIX, if non-nil, should be a key sequence;
479 then we display only bindings that start with that prefix.
480 The optional argument BUFFER specifies which buffer's bindings
481 to display (default, the current buffer). BUFFER can be a buffer
484 (or buffer (setq buffer (current-buffer)))
485 (help-setup-xref (list #'describe-bindings prefix buffer)
486 (called-interactively-p 'interactive))
487 (with-current-buffer buffer
488 (describe-bindings-internal nil prefix)))
490 ;; This function used to be in keymap.c.
491 (defun describe-bindings-internal (&optional menus prefix)
492 "Show a list of all defined keys, and their definitions.
493 We put that list in a buffer, and display the buffer.
495 The optional argument MENUS, if non-nil, says to mention menu bindings.
496 \(Ordinarily these are omitted from the output.)
497 The optional argument PREFIX, if non-nil, should be a key sequence;
498 then we display only bindings that start with that prefix."
499 (let ((buf (current-buffer)))
500 (with-help-window (help-buffer)
501 (describe-buffer-bindings buf prefix menus))))
503 (defun where-is (definition &optional insert)
504 "Print message listing key sequences that invoke the command DEFINITION.
505 Argument is a command definition, usually a symbol with a function definition.
506 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
508 (let ((fn (function-called-at-point))
509 (enable-recursive-minibuffers t)
511 (setq val (completing-read
513 (format "Where is command (default %s): " fn)
514 "Where is command: ")
515 obarray 'commandp t))
516 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
517 (unless definition (error "No command"))
518 (let ((func (indirect-function definition))
520 (standard-output (if insert (current-buffer) standard-output)))
521 ;; In DEFS, find all symbols that are aliases for DEFINITION.
522 (mapatoms (lambda (symbol)
523 (and (fboundp symbol)
524 (not (eq symbol definition))
525 (eq func (condition-case ()
526 (indirect-function symbol)
528 (push symbol defs))))
529 ;; Look at all the symbols--first DEFINITION,
531 (dolist (symbol (cons definition defs))
532 (let* ((remapped (command-remapping symbol))
533 (keys (where-is-internal
534 symbol overriding-local-map nil nil remapped))
535 (keys (mapconcat 'key-description keys ", "))
539 (if (> (length keys) 0)
541 (format "%s (%s) (remapped from %s)"
542 keys remapped symbol)
543 (format "%s (%s)" keys symbol))
544 (format "M-x %s RET" symbol))
545 (if (> (length keys) 0)
547 (format "%s is remapped to %s which is on %s"
548 symbol remapped keys)
549 (format "%s is on %s" symbol keys))
550 ;; If this is the command the user asked about,
551 ;; and it is not on any key, say so.
552 ;; For other symbols, its aliases, say nothing
553 ;; about them unless they are on keys.
554 (if (eq symbol definition)
555 (format "%s is not on any key" symbol)))))
557 (unless (eq symbol definition)
558 (princ ";\n its alias "))
562 (defun help-key-description (key untranslated)
563 (let ((string (key-description key)))
564 (if (or (not untranslated)
565 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
567 (let ((otherstring (key-description untranslated)))
568 (if (equal string otherstring)
570 (format "%s (translated from %s)" string otherstring))))))
572 (defun describe-key-briefly (&optional key insert untranslated)
573 "Print the name of the function KEY invokes. KEY is a string.
574 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
575 If non-nil, UNTRANSLATED is a vector of the untranslated events.
576 It can also be a number in which case the untranslated events from
577 the last key hit are used.
579 If KEY is a menu item or a tool-bar button that is disabled, this command
580 temporarily enables it to allow getting help on disabled items and buttons."
582 (let ((enable-disabled-menus-and-buttons t)
583 (cursor-in-echo-area t)
587 ;; If yank-menu is empty, populate it temporarily, so that
588 ;; "Select and Paste" menu can generate a complete event.
589 (when (null (cdr yank-menu))
590 (setq saved-yank-menu (copy-sequence yank-menu))
591 (menu-bar-update-yank-menu "(any string)" nil))
592 (setq key (read-key-sequence "Describe key (or click or menu item): "))
593 ;; Clear the echo area message (Bug#7014).
595 ;; If KEY is a down-event, read and discard the
596 ;; corresponding up-event. Note that there are also
597 ;; down-events on scroll bars and mode lines: the actual
598 ;; event then is in the second element of the vector.
600 (let ((last-idx (1- (length key))))
601 (and (eventp (aref key last-idx))
602 (memq 'down (event-modifiers (aref key last-idx)))))
606 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
608 ;; Put yank-menu back as it was, if we changed it.
609 (when saved-yank-menu
610 (setq yank-menu (copy-sequence saved-yank-menu))
611 (fset 'yank-menu (cons 'keymap yank-menu))))))
612 (if (numberp untranslated)
613 (setq untranslated (this-single-command-raw-keys)))
614 (let* ((event (if (and (symbolp (aref key 0))
616 (consp (aref key 1)))
619 (modifiers (event-modifiers event))
620 (standard-output (if insert (current-buffer) standard-output))
621 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
622 (memq 'drag modifiers)) " at that spot" ""))
623 (defn (key-binding key t))
625 ;; Handle the case where we faked an entry in "Select and Paste" menu.
626 (if (and (eq defn nil)
627 (stringp (aref key (1- (length key))))
628 (eq (key-binding (substring key 0 -1)) 'yank-menu))
629 (setq defn 'menu-bar-select-yank))
630 ;; Don't bother user with strings from (e.g.) the select-paste menu.
631 (if (stringp (aref key (1- (length key))))
632 (aset key (1- (length key)) "(any string)"))
633 (if (and (> (length untranslated) 0)
634 (stringp (aref untranslated (1- (length untranslated)))))
635 (aset untranslated (1- (length untranslated)) "(any string)"))
636 ;; Now describe the key, perhaps as changed.
637 (setq key-desc (help-key-description key untranslated))
638 (if (or (null defn) (integerp defn) (equal defn 'undefined))
639 (princ (format "%s%s is undefined" key-desc mouse-msg))
640 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
642 (defun describe-key (&optional key untranslated up-event)
643 "Display documentation of the function invoked by KEY.
644 KEY can be any kind of a key sequence; it can include keyboard events,
645 mouse events, and/or menu events. When calling from a program,
646 pass KEY as a string or a vector.
648 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
649 It can also be a number, in which case the untranslated events from
650 the last key sequence entered are used.
651 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
653 If KEY is a menu item or a tool-bar button that is disabled, this command
654 temporarily enables it to allow getting help on disabled items and buttons."
656 (let ((enable-disabled-menus-and-buttons t)
657 (cursor-in-echo-area t)
661 ;; If yank-menu is empty, populate it temporarily, so that
662 ;; "Select and Paste" menu can generate a complete event.
663 (when (null (cdr yank-menu))
664 (setq saved-yank-menu (copy-sequence yank-menu))
665 (menu-bar-update-yank-menu "(any string)" nil))
666 (setq key (read-key-sequence "Describe key (or click or menu item): "))
669 (prefix-numeric-value current-prefix-arg)
670 ;; If KEY is a down-event, read and include the
671 ;; corresponding up-event. Note that there are also
672 ;; down-events on scroll bars and mode lines: the actual
673 ;; event then is in the second element of the vector.
675 (let ((last-idx (1- (length key))))
676 (and (eventp (aref key last-idx))
677 (memq 'down (event-modifiers (aref key last-idx)))))
678 (or (and (eventp (aref key 0))
679 (memq 'down (event-modifiers (aref key 0)))
680 ;; However, for the C-down-mouse-2 popup
681 ;; menu, there is no subsequent up-event. In
682 ;; this case, the up-event is the next
683 ;; element in the supplied vector.
685 (and (> (length key) 1)
686 (eventp (aref key 1))
687 (memq 'down (event-modifiers (aref key 1)))))
689 ;; Put yank-menu back as it was, if we changed it.
690 (when saved-yank-menu
691 (setq yank-menu (copy-sequence saved-yank-menu))
692 (fset 'yank-menu (cons 'keymap yank-menu))))))
693 (if (numberp untranslated)
694 (setq untranslated (this-single-command-raw-keys)))
695 (let* ((event (aref key (if (and (symbolp (aref key 0))
697 (consp (aref key 1)))
700 (modifiers (event-modifiers event))
701 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
702 (memq 'drag modifiers)) " at that spot" ""))
703 (defn (key-binding key t))
704 defn-up defn-up-tricky ev-type
705 mouse-1-remapped mouse-1-tricky)
707 ;; Handle the case where we faked an entry in "Select and Paste" menu.
708 (when (and (eq defn nil)
709 (stringp (aref key (1- (length key))))
710 (eq (key-binding (substring key 0 -1)) 'yank-menu))
711 (setq defn 'menu-bar-select-yank))
712 (if (or (null defn) (integerp defn) (equal defn 'undefined))
713 (message "%s%s is undefined"
714 (help-key-description key untranslated) mouse-msg)
715 (help-setup-xref (list #'describe-function defn)
716 (called-interactively-p 'interactive))
717 ;; Don't bother user with strings from (e.g.) the select-paste menu.
718 (when (stringp (aref key (1- (length key))))
719 (aset key (1- (length key)) "(any string)"))
720 (when (and untranslated
721 (stringp (aref untranslated (1- (length untranslated)))))
722 (aset untranslated (1- (length untranslated))
724 ;; Need to do this before erasing *Help* buffer in case event
725 ;; is a mouse click in an existing *Help* buffer.
727 (setq ev-type (event-basic-type up-event))
728 (let ((sequence (vector up-event)))
729 (when (and (eq ev-type 'mouse-1)
730 mouse-1-click-follows-link
731 (not (eq mouse-1-click-follows-link 'double))
732 (setq mouse-1-remapped
733 (mouse-on-link-p (event-start up-event))))
734 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
735 (> mouse-1-click-follows-link 0)))
736 (cond ((stringp mouse-1-remapped)
737 (setq sequence mouse-1-remapped))
738 ((vectorp mouse-1-remapped)
739 (setcar up-event (elt mouse-1-remapped 0)))
740 (t (setcar up-event 'mouse-2))))
741 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
743 (setq sequence (vector up-event))
744 (aset sequence 0 'mouse-1)
745 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
746 (with-help-window (help-buffer)
747 (princ (help-key-description key untranslated))
749 %s runs the command %S, which is "
751 (describe-function-1 defn)
753 (unless (or (null defn-up)
755 (equal defn-up 'undefined))
758 ----------------- up-event %s----------------
760 %s%s%s runs the command %S, which is "
761 (if mouse-1-tricky "(short click) " "")
762 (key-description (vector up-event))
765 " is remapped to <mouse-2>, which" "")
767 (describe-function-1 defn-up))
768 (unless (or (null defn-up-tricky)
769 (integerp defn-up-tricky)
770 (eq defn-up-tricky 'undefined))
773 ----------------- up-event (long click) ----------------
775 Pressing <%S>%s for longer than %d milli-seconds
776 runs the command %S, which is "
778 mouse-1-click-follows-link
780 (describe-function-1 defn-up-tricky)))))))
782 (defun describe-mode (&optional buffer)
783 "Display documentation of current major mode and minor modes.
784 A brief summary of the minor modes comes first, followed by the
785 major mode description. This is followed by detailed
786 descriptions of the minor modes, each on a separate page.
788 For this to work correctly for a minor mode, the mode's indicator
789 variable \(listed in `minor-mode-alist') must also be a function
790 whose documentation describes the minor mode.
792 If called from Lisp with a non-nil BUFFER argument, display
793 documentation for the major and minor modes of that buffer."
795 (unless buffer (setq buffer (current-buffer)))
796 (help-setup-xref (list #'describe-mode buffer)
797 (called-interactively-p 'interactive))
798 ;; For the sake of help-do-xref and help-xref-go-back,
799 ;; don't switch buffers before calling `help-buffer'.
800 (with-help-window (help-buffer)
801 (with-current-buffer buffer
803 ;; Older packages do not register in minor-mode-list but only in
805 (dolist (x minor-mode-alist)
807 (unless (memq x minor-mode-list)
808 (push x minor-mode-list)))
809 ;; Find enabled minor mode we will want to mention.
810 (dolist (mode minor-mode-list)
811 ;; Document a minor mode if it is listed in minor-mode-alist,
812 ;; non-nil, and has a function definition.
813 (let ((fmode (or (get mode :minor-mode-function) mode)))
814 (and (boundp mode) (symbol-value mode)
816 (let ((pretty-minor-mode
817 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
820 (substring (symbol-name fmode)
821 0 (match-beginning 0)))
823 (push (list fmode pretty-minor-mode
824 (format-mode-line (assq mode minor-mode-alist)))
828 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
830 (princ "Enabled minor modes:\n")
831 (make-local-variable 'help-button-cache)
832 (with-current-buffer standard-output
833 (dolist (mode minor-modes)
834 (let ((mode-function (nth 0 mode))
835 (pretty-minor-mode (nth 1 mode))
836 (indicator (nth 2 mode)))
837 (add-text-properties 0 (length pretty-minor-mode)
838 '(face bold) pretty-minor-mode)
840 (goto-char (point-max))
842 (push (point-marker) help-button-cache)
843 ;; Document the minor modes fully.
844 (insert pretty-minor-mode)
845 (princ (format " minor mode (%s):\n"
846 (if (zerop (length indicator))
848 (format "indicator%s"
850 (princ (documentation mode-function)))
851 (insert-button pretty-minor-mode
852 'action (car help-button-cache)
854 'help-echo "mouse-2, RET: show full information")
860 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
861 ;; Document the major mode.
862 (let ((mode mode-name))
863 (with-current-buffer standard-output
864 (let ((start (point)))
865 (insert (format-mode-line mode nil nil buffer))
866 (add-text-properties start (point) '(face bold)))))
868 (let* ((mode major-mode)
869 (file-name (find-lisp-object-file-name mode nil)))
871 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
872 ;; Make a hyperlink to the library.
873 (with-current-buffer standard-output
875 (re-search-backward "`\\([^`']+\\)'" nil t)
876 (help-xref-button 1 'help-function-def mode file-name)))))
878 (princ (documentation major-mode)))))
879 ;; For the sake of IELM and maybe others
883 (defun describe-minor-mode (minor-mode)
884 "Display documentation of a minor mode given as MINOR-MODE.
885 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
886 appeared on the mode-line."
887 (interactive (list (completing-read
890 (describe-minor-mode-completion-table-for-symbol)
891 (describe-minor-mode-completion-table-for-indicator)
893 (if (symbolp minor-mode)
894 (setq minor-mode (symbol-name minor-mode)))
895 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
896 (indicators (describe-minor-mode-completion-table-for-indicator)))
898 ((member minor-mode symbols)
899 (describe-minor-mode-from-symbol (intern minor-mode)))
900 ((member minor-mode indicators)
901 (describe-minor-mode-from-indicator minor-mode))
903 (error "No such minor mode: %s" minor-mode)))))
906 (defun describe-minor-mode-completion-table-for-symbol ()
907 ;; In order to list up all minor modes, minor-mode-list
908 ;; is used here instead of minor-mode-alist.
909 (delq nil (mapcar 'symbol-name minor-mode-list)))
911 (defun describe-minor-mode-from-symbol (symbol)
912 "Display documentation of a minor mode given as a symbol, SYMBOL"
913 (interactive (list (intern (completing-read
914 "Minor mode symbol: "
915 (describe-minor-mode-completion-table-for-symbol)))))
917 (describe-function symbol)
918 (describe-variable symbol)))
921 (defun describe-minor-mode-completion-table-for-indicator ()
924 (let ((i (format-mode-line x)))
925 ;; remove first space if existed
935 (defun describe-minor-mode-from-indicator (indicator)
936 "Display documentation of a minor mode specified by INDICATOR.
937 If you call this function interactively, you can give indicator which
938 is currently activated with completion."
941 "Minor mode indicator: "
942 (describe-minor-mode-completion-table-for-indicator))))
943 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
945 (describe-minor-mode-from-symbol minor-mode)
946 (error "Cannot find minor mode for `%s'" indicator))))
948 (defun lookup-minor-mode-from-indicator (indicator)
949 "Return a minor mode symbol from its indicator on the mode line."
950 ;; remove first space if existed
951 (if (and (< 0 (length indicator))
952 (eq (aref indicator 0) ?\s))
953 (setq indicator (substring indicator 1)))
954 (let ((minor-modes minor-mode-alist)
957 (let* ((minor-mode (car (car minor-modes)))
958 (anindicator (format-mode-line
959 (car (cdr (car minor-modes))))))
960 ;; remove first space if existed
961 (if (and (stringp anindicator)
962 (> (length anindicator) 0)
963 (eq (aref anindicator 0) ?\s))
964 (setq anindicator (substring anindicator 1)))
965 (if (equal indicator anindicator)
966 (setq result minor-mode
968 (setq minor-modes (cdr minor-modes)))))
971 ;;; Automatic resizing of temporary buffers.
972 (defcustom temp-buffer-max-height
974 (if (eq (selected-window) (frame-root-window))
975 (/ (x-display-pixel-height) (frame-char-height) 2)
976 (/ (- (frame-height) 2) 2)))
977 "Maximum height of a window displaying a temporary buffer.
978 This is effective only when Temp Buffer Resize mode is enabled.
979 The value is the maximum height (in lines) which
980 `resize-temp-buffer-window' will give to a window displaying a
981 temporary buffer. It can also be a function to be called to
982 choose the height for such a buffer. It gets one argument, the
983 buffer, and should return a positive integer. At the time the
984 function is called, the window to be resized is selected."
985 :type '(choice integer function)
989 (defcustom temp-buffer-max-width
991 (if (eq (selected-window) (frame-root-window))
992 (/ (x-display-pixel-width) (frame-char-width) 2)
993 (/ (- (frame-width) 2) 2)))
994 "Maximum width of a window displaying a temporary buffer.
995 This is effective only when Temp Buffer Resize mode is enabled.
996 The value is the maximum width (in columns) which
997 `resize-temp-buffer-window' will give to a window displaying a
998 temporary buffer. It can also be a function to be called to
999 choose the width for such a buffer. It gets one argument, the
1000 buffer, and should return a positive integer. At the time the
1001 function is called, the window to be resized is selected."
1002 :type '(choice integer function)
1006 (define-minor-mode temp-buffer-resize-mode
1007 "Toggle auto-resizing temporary buffer windows (Temp Buffer Resize Mode).
1008 With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
1009 is positive, and disable it otherwise. If called from Lisp,
1010 enable the mode if ARG is omitted or nil.
1012 When Temp Buffer Resize mode is enabled, the windows in which we
1013 show a temporary buffer are automatically resized in height to
1014 fit the buffer's contents, but never more than
1015 `temp-buffer-max-height' nor less than `window-min-height'.
1017 A window is resized only if it has been specially created for the
1018 buffer. Windows that have shown another buffer before are not
1019 resized. A frame is resized only if `fit-frame-to-buffer' is
1022 This mode is used by `help', `apropos' and `completion' buffers,
1024 :global t :group 'help
1025 (if temp-buffer-resize-mode
1026 ;; `help-make-xrefs' may add a `back' button and thus increase the
1027 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1028 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1029 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
1031 (defun resize-temp-buffer-window (&optional window)
1032 "Resize WINDOW to fit its contents.
1033 WINDOW must be a live window and defaults to the selected one.
1034 Do not resize if WINDOW was not created by `display-buffer'.
1036 If WINDOW is part of a vertical combination, restrain its new
1037 size by `temp-buffer-max-height' and do not resize if its minimum
1038 accessible position is scrolled out of view. If WINDOW is part
1039 of a horizontal combination, restrain its new size by
1040 `temp-buffer-max-width'. In both cases, the value of the option
1041 `fit-window-to-buffer-horizontally' can inhibit resizing.
1043 If WINDOW is the root window of its frame, resize the frame
1044 provided `fit-frame-to-buffer' is non-nil."
1045 (setq window (window-normalize-window window t))
1046 (let ((height (if (functionp temp-buffer-max-height)
1047 (with-selected-window window
1048 (funcall temp-buffer-max-height (window-buffer)))
1049 temp-buffer-max-height))
1050 (width (if (functionp temp-buffer-max-width)
1051 (with-selected-window window
1052 (funcall temp-buffer-max-width (window-buffer)))
1053 temp-buffer-max-width))
1054 (quit-cadr (cadr (window-parameter window 'quit-restore))))
1055 ;; Resize WINDOW iff it was made by `display-buffer'.
1056 (when (or (and (eq quit-cadr 'window)
1057 (or (and (window-combined-p window)
1058 (not (eq fit-window-to-buffer-horizontally
1060 (pos-visible-in-window-p (point-min) window))
1061 (and (window-combined-p window t)
1062 fit-window-to-buffer-horizontally)))
1063 (and (eq quit-cadr 'frame)
1065 (eq window (frame-root-window window))))
1066 (fit-window-to-buffer window height nil width))))
1069 (defcustom help-window-select 'other
1070 "Non-nil means select help window for viewing.
1072 never (nil) Select help window only if there is no other window
1074 other Select help window unless the selected window is the
1075 only other window on the help window's frame.
1076 always (t) Always select the help window.
1078 This option has effect if and only if the help window was created
1079 by `with-help-window'"
1080 :type '(choice (const :tag "never (nil)" nil)
1081 (const :tag "other" other)
1082 (const :tag "always (t)" t))
1086 (defcustom help-enable-auto-load t
1087 "Whether Help commands can perform autoloading.
1088 If non-nil, whenever \\[describe-function] is called for an
1089 autoloaded function whose docstring contains any key substitution
1090 construct (see `substitute-command-keys'), the library is loaded,
1091 so that the documentation can show the right key bindings."
1096 (defun help-window-display-message (quit-part window &optional scroll)
1097 "Display message telling how to quit and scroll help window.
1098 QUIT-PART is a string telling how to quit the help window WINDOW.
1099 Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1100 SCROLL equal `other' means tell how to scroll the \"other\"
1104 ;; If we don't have QUIT-PART we probably reuse a window
1105 ;; showing the same buffer so we don't show any message.
1106 ((not quit-part) nil)
1107 ((pos-visible-in-window-p
1108 (with-current-buffer (window-buffer window)
1109 (point-max)) window t)
1110 ;; Buffer end is at least partially visible, no need to talk
1114 ", \\[scroll-other-window] to scroll help.")
1115 (scroll ", \\[scroll-up] to scroll help."))))
1117 (substitute-command-keys (concat quit-part scroll-part)))))
1119 (defun help-window-setup (window &optional value)
1120 "Set up help window WINDOW for `with-help-window'.
1121 WINDOW is the window used for displaying the help buffer.
1123 (let* ((help-buffer (when (window-live-p window)
1124 (window-buffer window)))
1125 (help-setup (when (window-live-p window)
1126 (car (window-parameter window 'quit-restore)))))
1128 ;; Handle `help-window-point-marker'.
1129 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1130 (set-window-point window help-window-point-marker)
1131 ;; Reset `help-window-point-marker'.
1132 (set-marker help-window-point-marker nil))
1135 ((or (eq window (selected-window))
1136 (and (or (eq help-window-select t)
1137 (eq help-setup 'frame)
1138 (and (eq help-window-select 'other)
1139 (eq (window-frame window) (selected-frame))
1140 (> (length (window-list nil 'no-mini)) 2)))
1141 (select-window window)))
1142 ;; The help window is or gets selected ...
1143 (help-window-display-message
1145 ((eq help-setup 'window)
1146 ;; ... and is new, ...
1147 "Type \"q\" to delete help window")
1148 ((eq help-setup 'frame)
1149 "Type \"q\" to quit the help frame")
1150 ((eq help-setup 'other)
1151 ;; ... or displayed some other buffer before.
1152 "Type \"q\" to restore previous buffer"))
1154 ((and (eq (window-frame window) (selected-frame))
1155 (= (length (window-list nil 'no-mini)) 2))
1156 ;; There are two windows on the help window's frame and the
1157 ;; other one is the selected one.
1158 (help-window-display-message
1160 ((eq help-setup 'window)
1161 "Type \\[delete-other-windows] to delete the help window")
1162 ((eq help-setup 'other)
1163 "Type \"q\" in help window to restore its previous buffer"))
1166 ;; The help window is not selected ...
1167 (help-window-display-message
1169 ((eq help-setup 'window)
1170 ;; ... and is new, ...
1171 "Type \"q\" in help window to delete it")
1172 ((eq help-setup 'other)
1173 ;; ... or displayed some other buffer before.
1174 "Type \"q\" in help window to restore previous buffer"))
1179 ;; `with-help-window' is a wrapper for `with-temp-buffer-window'
1180 ;; providing the following additional twists:
1182 ;; (1) It puts the buffer in `help-mode' (via `help-mode-setup') and
1183 ;; adds cross references (via `help-mode-finish').
1185 ;; (2) It issues a message telling how to scroll and quit the help
1186 ;; window (via `help-window-setup').
1188 ;; (3) An option (customizable via `help-window-select') to select the
1189 ;; help window automatically.
1191 ;; (4) A marker (`help-window-point-marker') to move point in the help
1192 ;; window to an arbitrary buffer position.
1194 ;; Note: It's usually always wrong to use `help-print-return-message' in
1195 ;; the body of `with-help-window'.
1196 (defmacro with-help-window (buffer-name &rest body)
1197 "Display buffer with name BUFFER-NAME in a help window evaluating BODY.
1198 Select help window if the current value of the user option
1199 `help-window-select' says so. Return last value in BODY."
1200 (declare (indent 1) (debug t))
1202 ;; Make `help-window-point-marker' point nowhere. The only place
1203 ;; where this should be set to a buffer position is within BODY.
1204 (set-marker help-window-point-marker nil)
1205 (let ((temp-buffer-window-setup-hook
1206 (cons 'help-mode-setup temp-buffer-window-setup-hook))
1207 (temp-buffer-window-show-hook
1208 (cons 'help-mode-finish temp-buffer-window-show-hook)))
1209 (with-temp-buffer-window
1210 ,buffer-name nil 'help-window-setup (progn ,@body)))))
1212 ;; Called from C, on encountering `help-char' when reading a char.
1213 ;; Don't print to *Help*; that would clobber Help history.
1214 (defun help-form-show ()
1215 "Display the output of a non-nil `help-form'."
1216 (let ((msg (eval help-form)))
1218 (with-output-to-temp-buffer " *Char Help*"
1223 ;;; help.el ends here