1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2011
4 ;; Free Software Foundation, Inc.
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' 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 ;; The variable `help-window' below is used by `help-mode-finish' to
41 ;; communicate the window displaying help (the "help window") to the
42 ;; macro `with-help-window'. The latter sets `help-window' to t before
43 ;; invoking `with-output-to-temp-buffer'. If and only if `help-window'
44 ;; is eq to t, `help-mode-finish' (called by `temp-buffer-setup-hook')
45 ;; sets `help-window' to the window selected by `display-buffer'.
46 ;; Exiting `with-help-window' and calling `help-print-return-message'
47 ;; reset `help-window' to nil.
48 (defvar help-window nil
49 "Window chosen for displaying help.")
51 ;; `help-window-point-marker' is a marker you can move to a valid
52 ;; position of the buffer shown in the help window in order to override
53 ;; the standard positioning mechanism (`point-min') chosen by
54 ;; `with-output-to-temp-buffer'. `with-help-window' has this point
55 ;; nowhere before exiting. Currently used by `view-lossage' to assert
56 ;; that the last keystrokes are always visible.
57 (defvar help-window-point-marker
(make-marker)
58 "Marker to override default `window-point' of `help-window'.")
61 (let ((map (make-sparse-keymap)))
62 (define-key map
(char-to-string help-char
) 'help-for-help
)
63 (define-key map
[help] 'help-for-help)
64 (define-key map [f1] 'help-for-help)
65 (define-key map "." 'display-local-help)
66 (define-key map "?" 'help-for-help)
68 (define-key map "\C-a" 'about-emacs)
69 (define-key map "\C-c" 'describe-copying)
70 (define-key map "\C-d" 'view-emacs-debugging)
71 (define-key map "\C-e" 'view-external-packages)
72 (define-key map "\C-f" 'view-emacs-FAQ)
73 (define-key map "\C-m" 'view-order-manuals)
74 (define-key map "\C-n" 'view-emacs-news)
75 (define-key map "\C-o" 'describe-distribution)
76 (define-key map "\C-p" 'view-emacs-problems)
77 (define-key map "\C-t" 'view-emacs-todo)
78 (define-key map "\C-w" 'describe-no-warranty)
80 ;; This does not fit the pattern, but it is natural given the C-\ command.
81 (define-key map "\C-\\" 'describe-input-method)
83 (define-key map "C" 'describe-coding-system)
84 (define-key map "F" 'Info-goto-emacs-command-node)
85 (define-key map "I" 'describe-input-method)
86 (define-key map "K" 'Info-goto-emacs-key-command-node)
87 (define-key map "L" 'describe-language-environment)
88 (define-key map "S" 'info-lookup-symbol)
90 (define-key map "a" 'apropos-command)
91 (define-key map "b" 'describe-bindings)
92 (define-key map "c" 'describe-key-briefly)
93 (define-key map "d" 'apropos-documentation)
94 (define-key map "e" 'view-echo-area-messages)
95 (define-key map "f" 'describe-function)
96 (define-key map "g" 'describe-gnu-project)
97 (define-key map "h" 'view-hello-file)
99 (define-key map "i" 'info)
100 (define-key map "4i" 'info-other-window)
102 (define-key map "k" 'describe-key)
103 (define-key map "l" 'view-lossage)
104 (define-key map "m" 'describe-mode)
105 (define-key map "n" 'view-emacs-news)
106 (define-key map "p" 'finder-by-keyword)
107 (define-key map "P" 'describe-package)
108 (define-key map "r" 'info-emacs-manual)
109 (define-key map "s" 'describe-syntax)
110 (define-key map "t" 'help-with-tutorial)
111 (define-key map "w" 'where-is)
112 (define-key map "v" 'describe-variable)
113 (define-key map "q" 'help-quit)
115 "Keymap for characters following the Help key.")
117 (define-key global-map (char-to-string help-char) 'help-command)
118 (define-key global-map [help] 'help-command
)
119 (define-key global-map
[f1] 'help-command)
120 (fset 'help-command help-map)
122 ;; insert-button makes the action nil if it is not store somewhere
123 (defvar help-button-cache nil)
127 "Just exit from the Help command's command loop."
131 (defvar help-return-method nil
132 "What to do to \"exit\" the help buffer.
134 (WINDOW . t) delete the selected window (and possibly its frame,
135 see `quit-window' and `View-quit'), go to WINDOW.
136 (WINDOW . quit-window) do quit-window, then select WINDOW.
137 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
139 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
140 (defun help-print-return-message (&optional function)
141 "Display or return message saying how to restore windows after help command.
142 This function assumes that `standard-output' is the help buffer.
143 It computes a message, and applies the optional argument FUNCTION to it.
144 If FUNCTION is nil, it applies `message', thus displaying the message.
145 In addition, this function sets up `help-return-method', which see, that
146 specifies what to do when the user exits the help buffer."
147 ;; Reset `help-window' here to avoid confusing `help-mode-finish'.
148 (setq help-window nil)
149 (and (not (get-buffer-window standard-output))
153 (special-display-p (buffer-name standard-output)))
154 (setq help-return-method (cons (selected-window) t))
155 ;; If the help output buffer is a special display buffer,
156 ;; don't say anything about how to get rid of it.
157 ;; First of all, the user will do that with the window
158 ;; manager, not with Emacs.
159 ;; Secondly, the buffer has not been displayed yet,
160 ;; so we don't know whether its frame will be selected.
162 (display-buffer-reuse-frames
163 (setq help-return-method (cons (selected-window)
166 ((not (one-window-p t))
167 (setq help-return-method
168 (cons (selected-window) 'quit-window))
169 "Type \\[display-buffer] RET to restore the other window.")
171 (setq help-return-method (cons (selected-window) t))
172 "Type \\[delete-other-windows] to remove help window.")
174 (setq help-return-method
175 (list (selected-window) (window-buffer)
176 (window-start) (window-point)))
177 "Type \\[switch-to-buffer] RET to remove help window."))))
178 (funcall (or function 'message)
181 (substitute-command-keys first-message))
182 (if first-message " ")
183 ;; If the help buffer will go in a separate frame,
184 ;; it's no use mentioning a command to scroll, so don't.
185 (if (or pop-up-windows
186 (special-display-p (buffer-name standard-output)))
188 (if (same-window-p (buffer-name standard-output))
189 ;; Say how to scroll this window.
190 (substitute-command-keys
191 "\\[scroll-up] to scroll the help.")
192 ;; Say how to scroll some other window.
193 (substitute-command-keys
194 "\\[scroll-other-window] to scroll the help."))))))))
196 ;; So keyboard macro definitions are documented correctly
197 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
199 (defalias 'help 'help-for-help-internal)
200 ;; find-function can find this.
201 (defalias 'help-for-help 'help-for-help-internal)
202 ;; It can't find this, but nobody will look.
203 (make-help-screen help-for-help-internal
204 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
205 ;; Don't purecopy this one, because it's not evaluated (it's
206 ;; directly used as a docstring in a function definition, so it'll
207 ;; be moved to the DOC file anyway: no need for purecopying it).
208 "You have typed %THIS-KEY%, the help character. Type a Help option:
209 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
211 a PATTERN Show commands whose name matches the PATTERN (a list of words
212 or a regexp). See also the `apropos' command.
213 b Display all key bindings.
214 c KEYS Display the command name run by the given key sequence.
215 C CODING Describe the given coding system, or RET for current ones.
216 d PATTERN Show a list of functions, variables, and other items whose
217 documentation matches the PATTERN (a list of words or a regexp).
218 e Go to the *Messages* buffer which logs echo-area messages.
219 f FUNCTION Display documentation for the given function.
220 F COMMAND Show the on-line manual's section that describes the command.
221 g Display information about the GNU project.
222 h Display the HELLO file which illustrates various scripts.
223 i Start the Info documentation reader: read on-line manuals.
224 I METHOD Describe a specific input method, or RET for current.
225 k KEYS Display the full documentation for the key sequence.
226 K KEYS Show the on-line manual's section for the command bound to KEYS.
227 l Show last 300 input keystrokes (lossage).
228 L LANG-ENV Describes a specific language environment, or RET for current.
229 m Display documentation of current minor modes and current major mode,
230 including their special commands.
231 n Display news of recent Emacs changes.
232 p TOPIC Find packages matching a given topic keyword.
233 r Display the Emacs manual in Info mode.
234 s Display contents of current syntax table, plus explanations.
235 S SYMBOL Show the section for the given symbol in the on-line manual
236 for the programming language used in this buffer.
237 t Start the Emacs learn-by-doing tutorial.
238 v VARIABLE Display the given variable's documentation and value.
239 w COMMAND Display which keystrokes invoke the given command (where-is).
240 . Display any available local help at point in the echo area.
242 C-a Information about Emacs.
243 C-c Emacs copying permission (GNU General Public License).
244 C-d Instructions for debugging GNU Emacs.
245 C-e External packages and information about Emacs.
247 C-m How to order printed Emacs manuals.
248 C-n News of recent Emacs changes.
249 C-o Emacs ordering and distribution information.
250 C-p Info about known Emacs problems.
252 C-w Information on absence of warranty for GNU Emacs."
257 (defun function-called-at-point ()
258 "Return a function around point or else called by the list containing point.
259 If that doesn't give a function, return nil."
260 (with-syntax-table emacs-lisp-mode-syntax-table
261 (or (condition-case ()
263 (or (not (zerop (skip-syntax-backward "_w")))
264 (eq (char-syntax (following-char)) ?w)
265 (eq (char-syntax (following-char)) ?_)
267 (skip-chars-forward "'")
268 (let ((obj (read (current-buffer))))
269 (and (symbolp obj) (fboundp obj) obj)))
274 (narrow-to-region (max (point-min)
275 (- (point) 1000)) (point-max))
276 ;; Move up to surrounding paren, then after the open.
279 ;; If there is space here, this is probably something
280 ;; other than a real Lisp function call, so ignore it.
281 (if (looking-at "[ \t]")
282 (error "Probably not a Lisp function call"))
283 (let ((obj (read (current-buffer))))
284 (and (symbolp obj) (fboundp obj) obj))))
286 (let* ((str (find-tag-default))
287 (sym (if str (intern-soft str))))
288 (if (and sym (fboundp sym))
291 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
292 (setq sym (intern-soft (match-string 1 str)))
293 (and (fboundp sym) sym))))))))
296 ;;; `User' help functions
298 (defun view-help-file (file &optional dir)
299 (view-file (expand-file-name file (or dir data-directory)))
300 (goto-address-mode 1)
301 (goto-char (point-min)))
303 (defun describe-distribution ()
304 "Display info on how to obtain the latest version of GNU Emacs."
306 (view-help-file "DISTRIB"))
308 (defun describe-copying ()
309 "Display info on how you may redistribute copies of GNU Emacs."
311 (view-help-file "COPYING"))
313 (defun describe-gnu-project ()
314 "Display info on the GNU project."
316 (view-help-file "THE-GNU-PROJECT"))
318 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
320 (defun describe-no-warranty ()
321 "Display info on all the kinds of warranty Emacs does NOT have."
324 (let (case-fold-search)
325 (search-forward "Disclaimer of Warranty")
329 (defun describe-prefix-bindings ()
330 "Describe the bindings of the prefix used to reach this command.
331 The prefix described consists of all but the last event
332 of the key sequence that ran this command."
334 (let ((key (this-command-keys)))
337 (substring key 0 (1- (length key)))
338 (let ((prefix (make-vector (1- (length key)) nil))
340 (while (< i (length prefix))
341 (aset prefix i (aref key i))
344 ;; Make C-h after a prefix, when not specifically bound,
345 ;; run describe-prefix-bindings.
346 (setq prefix-help-command 'describe-prefix-bindings)
348 (defun view-emacs-news (&optional version)
349 "Display info on recent changes to Emacs.
350 With argument, display info only for the selected version."
353 (setq version emacs-major-version))
354 (when (consp version)
360 (insert-file-contents
361 (expand-file-name file data-directory))
362 (while (re-search-forward
363 (if (member file '("NEWS.18" "NEWS.1-17"))
364 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
365 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
366 (setq res (cons (match-string-no-properties 1) res)))))
368 (directory-files data-directory nil
369 "^NEWS\\.[0-9][-0-9]*$" nil)))
370 (sort (delete-dups res) (lambda (a b) (string< b a)))))
371 (current (car all-versions)))
372 (setq version (completing-read
373 (format "Read NEWS for the version (default %s): " current)
374 all-versions nil nil nil nil current))
375 (if (integerp (string-to-number version))
376 (setq version (string-to-number version))
377 (unless (or (member version all-versions)
378 (<= (string-to-number version) (string-to-number current)))
379 (error "No news about version %s" version)))))
380 (when (integerp version)
381 (cond ((<= version 12)
382 (setq version (format "1.%d" version)))
384 (setq version (format "%d" version)))
385 ((> version emacs-major-version)
386 (error "No news about Emacs %d (yet)" version))))
387 (let* ((vn (if (stringp version)
388 (string-to-number version)
391 ((>= vn emacs-major-version) "NEWS")
392 ((< vn 18) "NEWS.1-17")
393 (t (format "NEWS.%d" vn))))
395 (view-file (expand-file-name file data-directory))
397 (goto-char (point-min))
398 (when (stringp version)
399 (when (re-search-forward
400 (concat (if (< vn 19)
401 "Changes in Emacs[ \t]*"
402 "^\* [^0-9\n]*") version "$")
408 (while (and (setq res
411 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
412 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
413 (equal (match-string-no-properties 1) version)))
414 (or res (goto-char (point-max)))
418 (defun view-emacs-todo (&optional arg)
419 "Display the Emacs TODO list."
421 (view-help-file "TODO"))
423 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
426 (defun view-echo-area-messages ()
427 "View the log of recent echo-area messages: the `*Messages*' buffer.
428 The number of messages retained in that buffer
429 is specified by the variable `message-log-max'."
431 (switch-to-buffer (get-buffer-create "*Messages*")))
433 (defun view-order-manuals ()
434 "Display the Emacs ORDERS file."
436 (view-help-file "ORDERS"))
438 (defun view-emacs-FAQ ()
439 "Display the Emacs Frequently Asked Questions (FAQ) file."
441 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
444 (defun view-emacs-problems ()
445 "Display info on known problems with Emacs and possible workarounds."
447 (view-help-file "PROBLEMS"))
449 (defun view-emacs-debugging ()
450 "Display info on how to debug Emacs problems."
452 (view-help-file "DEBUG"))
454 (defun view-external-packages ()
455 "Display external packages and information about Emacs."
457 (view-help-file "MORE.STUFF"))
459 (defun view-lossage ()
460 "Display last 300 input keystrokes.
462 To record all your input on a file, use `open-dribble-file'."
464 (help-setup-xref (list #'view-lossage)
465 (called-interactively-p 'interactive))
466 (with-help-window (help-buffer)
467 (princ (mapconcat (lambda (key)
468 (if (or (integerp key) (symbolp key) (listp key))
469 (single-key-description key)
470 (prin1-to-string key nil)))
473 (with-current-buffer standard-output
474 (goto-char (point-min))
475 (while (progn (move-to-column 50) (not (eobp)))
476 (when (search-forward " " nil t)
479 ;; jidanni wants to see the last keystrokes immediately.
480 (set-marker help-window-point-marker (point)))))
485 (defun describe-bindings (&optional prefix buffer)
486 "Show a list of all defined keys, and their definitions.
487 We put that list in a buffer, and display the buffer.
489 The optional argument PREFIX, if non-nil, should be a key sequence;
490 then we display only bindings that start with that prefix.
491 The optional argument BUFFER specifies which buffer's bindings
492 to display (default, the current buffer). BUFFER can be a buffer
495 (or buffer (setq buffer (current-buffer)))
496 (help-setup-xref (list #'describe-bindings prefix buffer)
497 (called-interactively-p 'interactive))
498 (with-current-buffer buffer
499 (describe-bindings-internal nil prefix)))
501 ;; This function used to be in keymap.c.
502 (defun describe-bindings-internal (&optional menus prefix)
503 "Show a list of all defined keys, and their definitions.
504 We put that list in a buffer, and display the buffer.
506 The optional argument MENUS, if non-nil, says to mention menu bindings.
507 \(Ordinarily these are omitted from the output.)
508 The optional argument PREFIX, if non-nil, should be a key sequence;
509 then we display only bindings that start with that prefix."
510 (let ((buf (current-buffer)))
511 (with-help-window "*Help*"
512 (with-current-buffer standard-output
513 (describe-buffer-bindings buf prefix menus)))))
515 (defun where-is (definition &optional insert)
516 "Print message listing key sequences that invoke the command DEFINITION.
517 Argument is a command definition, usually a symbol with a function definition.
518 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
520 (let ((fn (function-called-at-point))
521 (enable-recursive-minibuffers t)
523 (setq val (completing-read
525 (format "Where is command (default %s): " fn)
526 "Where is command: ")
527 obarray 'commandp t))
528 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
529 (unless definition (error "No command"))
530 (let ((func (indirect-function definition))
532 (standard-output (if insert (current-buffer) t)))
533 ;; In DEFS, find all symbols that are aliases for DEFINITION.
534 (mapatoms (lambda (symbol)
535 (and (fboundp symbol)
536 (not (eq symbol definition))
537 (eq func (condition-case ()
538 (indirect-function symbol)
540 (push symbol defs))))
541 ;; Look at all the symbols--first DEFINITION,
543 (dolist (symbol (cons definition defs))
544 (let* ((remapped (command-remapping symbol))
545 (keys (where-is-internal
546 symbol overriding-local-map nil nil remapped))
547 (keys (mapconcat 'key-description keys ", "))
551 (if (> (length keys) 0)
553 (format "%s (%s) (remapped from %s)"
554 keys remapped symbol)
555 (format "%s (%s)" keys symbol))
556 (format "M-x %s RET" symbol))
557 (if (> (length keys) 0)
559 (format "%s is remapped to %s which is on %s"
560 symbol remapped keys)
561 (format "%s is on %s" symbol keys))
562 ;; If this is the command the user asked about,
563 ;; and it is not on any key, say so.
564 ;; For other symbols, its aliases, say nothing
565 ;; about them unless they are on keys.
566 (if (eq symbol definition)
567 (format "%s is not on any key" symbol)))))
569 (unless (eq symbol definition)
570 (princ ";\n its alias "))
574 (defun help-key-description (key untranslated)
575 (let ((string (key-description key)))
576 (if (or (not untranslated)
577 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
579 (let ((otherstring (key-description untranslated)))
580 (if (equal string otherstring)
582 (format "%s (translated from %s)" string otherstring))))))
584 (defun describe-key-briefly (&optional key insert untranslated)
585 "Print the name of the function KEY invokes. KEY is a string.
586 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
587 If non-nil, UNTRANSLATED is a vector of the untranslated events.
588 It can also be a number in which case the untranslated events from
589 the last key hit are used.
591 If KEY is a menu item or a tool-bar button that is disabled, this command
592 temporarily enables it to allow getting help on disabled items and buttons."
594 (let ((enable-disabled-menus-and-buttons t)
595 (cursor-in-echo-area t)
599 ;; If yank-menu is empty, populate it temporarily, so that
600 ;; "Select and Paste" menu can generate a complete event.
601 (when (null (cdr yank-menu))
602 (setq saved-yank-menu (copy-sequence yank-menu))
603 (menu-bar-update-yank-menu "(any string)" nil))
604 (setq key (read-key-sequence "Describe key (or click or menu item): "))
605 ;; If KEY is a down-event, read and discard the
606 ;; corresponding up-event. Note that there are also
607 ;; down-events on scroll bars and mode lines: the actual
608 ;; event then is in the second element of the vector.
610 (let ((last-idx (1- (length key))))
611 (and (eventp (aref key last-idx))
612 (memq 'down (event-modifiers (aref key last-idx)))))
616 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
618 ;; Put yank-menu back as it was, if we changed it.
619 (when saved-yank-menu
620 (setq yank-menu (copy-sequence saved-yank-menu))
621 (fset 'yank-menu (cons 'keymap yank-menu))))))
622 (if (numberp untranslated)
623 (setq untranslated (this-single-command-raw-keys)))
624 (let* ((event (if (and (symbolp (aref key 0))
626 (consp (aref key 1)))
629 (modifiers (event-modifiers event))
630 (standard-output (if insert (current-buffer) t))
631 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
632 (memq 'drag modifiers)) " at that spot" ""))
633 (defn (key-binding key t))
635 ;; Handle the case where we faked an entry in "Select and Paste" menu.
636 (if (and (eq defn nil)
637 (stringp (aref key (1- (length key))))
638 (eq (key-binding (substring key 0 -1)) 'yank-menu))
639 (setq defn 'menu-bar-select-yank))
640 ;; Don't bother user with strings from (e.g.) the select-paste menu.
641 (if (stringp (aref key (1- (length key))))
642 (aset key (1- (length key)) "(any string)"))
643 (if (and (> (length untranslated) 0)
644 (stringp (aref untranslated (1- (length untranslated)))))
645 (aset untranslated (1- (length untranslated)) "(any string)"))
646 ;; Now describe the key, perhaps as changed.
647 (setq key-desc (help-key-description key untranslated))
648 (if (or (null defn) (integerp defn) (equal defn 'undefined))
649 (princ (format "%s%s is undefined" key-desc mouse-msg))
650 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
652 (defun describe-key (&optional key untranslated up-event)
653 "Display documentation of the function invoked by KEY.
654 KEY can be any kind of a key sequence; it can include keyboard events,
655 mouse events, and/or menu events. When calling from a program,
656 pass KEY as a string or a vector.
658 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
659 It can also be a number, in which case the untranslated events from
660 the last key sequence entered are used.
661 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
663 If KEY is a menu item or a tool-bar button that is disabled, this command
664 temporarily enables it to allow getting help on disabled items and buttons."
666 (let ((enable-disabled-menus-and-buttons t)
667 (cursor-in-echo-area t)
671 ;; If yank-menu is empty, populate it temporarily, so that
672 ;; "Select and Paste" menu can generate a complete event.
673 (when (null (cdr yank-menu))
674 (setq saved-yank-menu (copy-sequence yank-menu))
675 (menu-bar-update-yank-menu "(any string)" nil))
676 (setq key (read-key-sequence "Describe key (or click or menu item): "))
679 (prefix-numeric-value current-prefix-arg)
680 ;; If KEY is a down-event, read and include the
681 ;; corresponding up-event. Note that there are also
682 ;; down-events on scroll bars and mode lines: the actual
683 ;; event then is in the second element of the vector.
685 (let ((last-idx (1- (length key))))
686 (and (eventp (aref key last-idx))
687 (memq 'down (event-modifiers (aref key last-idx)))))
688 (or (and (eventp (aref key 0))
689 (memq 'down (event-modifiers (aref key 0)))
690 ;; However, for the C-down-mouse-2 popup
691 ;; menu, there is no subsequent up-event. In
692 ;; this case, the up-event is the next
693 ;; element in the supplied vector.
695 (and (> (length key) 1)
696 (eventp (aref key 1))
697 (memq 'down (event-modifiers (aref key 1)))))
699 ;; Put yank-menu back as it was, if we changed it.
700 (when saved-yank-menu
701 (setq yank-menu (copy-sequence saved-yank-menu))
702 (fset 'yank-menu (cons 'keymap yank-menu))))))
703 (if (numberp untranslated)
704 (setq untranslated (this-single-command-raw-keys)))
705 (let* ((event (aref key (if (and (symbolp (aref key 0))
707 (consp (aref key 1)))
710 (modifiers (event-modifiers event))
711 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
712 (memq 'drag modifiers)) " at that spot" ""))
713 (defn (key-binding key t))
714 defn-up defn-up-tricky ev-type
715 mouse-1-remapped mouse-1-tricky)
717 ;; Handle the case where we faked an entry in "Select and Paste" menu.
718 (when (and (eq defn nil)
719 (stringp (aref key (1- (length key))))
720 (eq (key-binding (substring key 0 -1)) 'yank-menu))
721 (setq defn 'menu-bar-select-yank))
722 (if (or (null defn) (integerp defn) (equal defn 'undefined))
723 (message "%s%s is undefined"
724 (help-key-description key untranslated) mouse-msg)
725 (help-setup-xref (list #'describe-function defn)
726 (called-interactively-p 'interactive))
727 ;; Don't bother user with strings from (e.g.) the select-paste menu.
728 (when (stringp (aref key (1- (length key))))
729 (aset key (1- (length key)) "(any string)"))
730 (when (and untranslated
731 (stringp (aref untranslated (1- (length untranslated)))))
732 (aset untranslated (1- (length untranslated))
734 ;; Need to do this before erasing *Help* buffer in case event
735 ;; is a mouse click in an existing *Help* buffer.
737 (setq ev-type (event-basic-type up-event))
738 (let ((sequence (vector up-event)))
739 (when (and (eq ev-type 'mouse-1)
740 mouse-1-click-follows-link
741 (not (eq mouse-1-click-follows-link 'double))
742 (setq mouse-1-remapped
743 (mouse-on-link-p (event-start up-event))))
744 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
745 (> mouse-1-click-follows-link 0)))
746 (cond ((stringp mouse-1-remapped)
747 (setq sequence mouse-1-remapped))
748 ((vectorp mouse-1-remapped)
749 (setcar up-event (elt mouse-1-remapped 0)))
750 (t (setcar up-event 'mouse-2))))
751 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
753 (setq sequence (vector up-event))
754 (aset sequence 0 'mouse-1)
755 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
756 (with-help-window (help-buffer)
757 (princ (help-key-description key untranslated))
759 %s runs the command %S, which is "
761 (describe-function-1 defn)
763 (unless (or (null defn-up)
765 (equal defn-up 'undefined))
768 ----------------- up-event %s----------------
770 %s%s%s runs the command %S, which is "
771 (if mouse-1-tricky "(short click) " "")
772 (key-description (vector up-event))
775 " is remapped to <mouse-2>, which" "")
777 (describe-function-1 defn-up))
778 (unless (or (null defn-up-tricky)
779 (integerp defn-up-tricky)
780 (eq defn-up-tricky 'undefined))
783 ----------------- up-event (long click) ----------------
785 Pressing <%S>%s for longer than %d milli-seconds
786 runs the command %S, which is "
788 mouse-1-click-follows-link
790 (describe-function-1 defn-up-tricky)))))))
792 (defun describe-mode (&optional buffer)
793 "Display documentation of current major mode and minor modes.
794 A brief summary of the minor modes comes first, followed by the
795 major mode description. This is followed by detailed
796 descriptions of the minor modes, each on a separate page.
798 For this to work correctly for a minor mode, the mode's indicator
799 variable \(listed in `minor-mode-alist') must also be a function
800 whose documentation describes the minor mode."
802 (unless buffer (setq buffer (current-buffer)))
803 (help-setup-xref (list #'describe-mode buffer)
804 (called-interactively-p 'interactive))
805 ;; For the sake of help-do-xref and help-xref-go-back,
806 ;; don't switch buffers before calling `help-buffer'.
807 (with-help-window (help-buffer)
808 (with-current-buffer buffer
810 ;; Older packages do not register in minor-mode-list but only in
812 (dolist (x minor-mode-alist)
814 (unless (memq x minor-mode-list)
815 (push x minor-mode-list)))
816 ;; Find enabled minor mode we will want to mention.
817 (dolist (mode minor-mode-list)
818 ;; Document a minor mode if it is listed in minor-mode-alist,
819 ;; non-nil, and has a function definition.
820 (let ((fmode (or (get mode :minor-mode-function) mode)))
821 (and (boundp mode) (symbol-value mode)
823 (let ((pretty-minor-mode
824 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
827 (substring (symbol-name fmode)
828 0 (match-beginning 0)))
830 (push (list fmode pretty-minor-mode
831 (format-mode-line (assq mode minor-mode-alist)))
835 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
837 (princ "Enabled minor modes:\n")
838 (make-local-variable 'help-button-cache)
839 (with-current-buffer standard-output
840 (dolist (mode minor-modes)
841 (let ((mode-function (nth 0 mode))
842 (pretty-minor-mode (nth 1 mode))
843 (indicator (nth 2 mode)))
844 (add-text-properties 0 (length pretty-minor-mode)
845 '(face bold) pretty-minor-mode)
847 (goto-char (point-max))
849 (push (point-marker) help-button-cache)
850 ;; Document the minor modes fully.
851 (insert pretty-minor-mode)
852 (princ (format " minor mode (%s):\n"
853 (if (zerop (length indicator))
855 (format "indicator%s"
857 (princ (documentation mode-function)))
858 (insert-button pretty-minor-mode
859 'action (car help-button-cache)
861 'help-echo "mouse-2, RET: show full information")
867 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
868 ;; Document the major mode.
869 (let ((mode mode-name))
870 (with-current-buffer standard-output
871 (let ((start (point)))
872 (insert (format-mode-line mode nil nil buffer))
873 (add-text-properties start (point) '(face bold)))))
875 (let* ((mode major-mode)
876 (file-name (find-lisp-object-file-name mode nil)))
878 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
879 ;; Make a hyperlink to the library.
880 (with-current-buffer standard-output
882 (re-search-backward "`\\([^`']+\\)'" nil t)
883 (help-xref-button 1 'help-function-def mode file-name)))))
885 (princ (documentation major-mode)))))
886 ;; For the sake of IELM and maybe others
890 (defun describe-minor-mode (minor-mode)
891 "Display documentation of a minor mode given as MINOR-MODE.
892 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
893 appeared on the mode-line."
894 (interactive (list (completing-read
897 (describe-minor-mode-completion-table-for-symbol)
898 (describe-minor-mode-completion-table-for-indicator)
900 (if (symbolp minor-mode)
901 (setq minor-mode (symbol-name minor-mode)))
902 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
903 (indicators (describe-minor-mode-completion-table-for-indicator)))
905 ((member minor-mode symbols)
906 (describe-minor-mode-from-symbol (intern minor-mode)))
907 ((member minor-mode indicators)
908 (describe-minor-mode-from-indicator minor-mode))
910 (error "No such minor mode: %s" minor-mode)))))
913 (defun describe-minor-mode-completion-table-for-symbol ()
914 ;; In order to list up all minor modes, minor-mode-list
915 ;; is used here instead of minor-mode-alist.
916 (delq nil (mapcar 'symbol-name minor-mode-list)))
917 (defun describe-minor-mode-from-symbol (symbol)
918 "Display documentation of a minor mode given as a symbol, SYMBOL"
919 (interactive (list (intern (completing-read
920 "Minor mode symbol: "
921 (describe-minor-mode-completion-table-for-symbol)))))
923 (describe-function symbol)
924 (describe-variable symbol)))
927 (defun describe-minor-mode-completion-table-for-indicator ()
930 (let ((i (format-mode-line x)))
931 ;; remove first space if existed
940 (defun describe-minor-mode-from-indicator (indicator)
941 "Display documentation of a minor mode specified by INDICATOR.
942 If you call this function interactively, you can give indicator which
943 is currently activated with completion."
946 "Minor mode indicator: "
947 (describe-minor-mode-completion-table-for-indicator))))
948 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
950 (describe-minor-mode-from-symbol minor-mode)
951 (error "Cannot find minor mode for `%s'" indicator))))
953 (defun lookup-minor-mode-from-indicator (indicator)
954 "Return a minor mode symbol from its indicator on the modeline."
955 ;; remove first space if existed
956 (if (and (< 0 (length indicator))
957 (eq (aref indicator 0) ?\s))
958 (setq indicator (substring indicator 1)))
959 (let ((minor-modes minor-mode-alist)
962 (let* ((minor-mode (car (car minor-modes)))
963 (anindicator (format-mode-line
964 (car (cdr (car minor-modes))))))
965 ;; remove first space if existed
966 (if (and (stringp anindicator)
967 (> (length anindicator) 0)
968 (eq (aref anindicator 0) ?\s))
969 (setq anindicator (substring anindicator 1)))
970 (if (equal indicator anindicator)
971 (setq result minor-mode
973 (setq minor-modes (cdr minor-modes)))))
977 ;;; Automatic resizing of temporary buffers.
979 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
980 "Maximum height of a window displaying a temporary buffer.
981 This is effective only when Temp Buffer Resize mode is enabled.
982 The value is the maximum height (in lines) which `resize-temp-buffer-window'
983 will give to a window displaying a temporary buffer.
984 It can also be a function to be called to choose the height for such a buffer.
985 It gets one argumemt, the buffer, and should return a positive integer."
986 :type '(choice integer function)
990 (define-minor-mode temp-buffer-resize-mode
991 "Toggle the mode which makes windows smaller for temporary buffers.
992 With prefix argument ARG, turn the resizing of windows displaying temporary
993 buffers on if ARG is positive or off otherwise.
994 This makes the window the right height for its contents, but never
995 more than `temp-buffer-max-height' nor less than `window-min-height'.
996 This applies to `help', `apropos' and `completion' buffers, and some others."
997 :global t :group 'help
998 (if temp-buffer-resize-mode
999 ;; `help-make-xrefs' may add a `back' button and thus increase the
1000 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1001 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1002 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
1004 (defun resize-temp-buffer-window ()
1005 "Resize the selected window to fit its contents.
1006 Will not make it higher than `temp-buffer-max-height' nor smaller than
1007 `window-min-height'. Do nothing if it is the only window on its frame, if it
1008 is not as wide as the frame or if some of the window's contents are scrolled
1010 (unless (or (one-window-p 'nomini)
1011 (not (pos-visible-in-window-p (point-min)))
1012 (not (window-full-width-p)))
1013 (fit-window-to-buffer
1015 (if (functionp temp-buffer-max-height)
1016 (funcall temp-buffer-max-height (current-buffer))
1017 temp-buffer-max-height))))
1022 (defcustom help-window-select 'other
1023 "Non-nil means select help window for viewing.
1025 never (nil) Select help window only if there is no other window
1027 other Select help window unless the selected window is the
1028 only other window on its frame.
1029 always (t) Always select the help window.
1031 This option has effect if and only if the help window was created
1032 by `with-help-window'"
1033 :type '(choice (const :tag "never (nil)" nil)
1034 (const :tag "other" other)
1035 (const :tag "always (t)" t))
1039 (defun help-window-display-message (quit-part window &optional other)
1040 "Display message telling how to quit and scroll help window.
1041 QUIT-PART is a string telling how to quit the help window WINDOW.
1042 Optional argument OTHER non-nil means return text telling how to
1043 scroll the \"other\" window."
1046 ((pos-visible-in-window-p
1047 (with-current-buffer (window-buffer window)
1048 (point-max)) window)
1049 ;; Buffer end is visible.
1051 (other ", \\[scroll-other-window] to scroll help.")
1052 (t ", \\[scroll-up] to scroll help."))))
1054 (substitute-command-keys (concat quit-part scroll-part)))))
1056 (defun help-window-setup-finish (window &optional reuse keep-frame)
1057 "Finish setting up help window WINDOW.
1058 Select WINDOW according to the value of `help-window-select'.
1059 Display message telling how to scroll and eventually quit WINDOW.
1061 Optional argument REUSE non-nil means WINDOW has been reused by
1062 `display-buffer'. Optional argument KEEP-FRAME non-nil means
1063 that quitting should not delete WINDOW's frame."
1064 (let ((number-of-windows
1065 (length (window-list (window-frame window) 'no-mini window))))
1067 ((eq window (selected-window))
1068 ;; The help window is the selected window, probably the
1069 ;; `pop-up-windows' nil case.
1070 (help-window-display-message
1072 "Type \"q\" to restore this window"
1073 ;; This should not be taken.
1074 "Type \"q\" to quit") window))
1075 ((= number-of-windows 1)
1076 ;; The help window is alone on a frame and not the selected
1077 ;; window, could be the `pop-up-frames' t case.
1078 (help-window-display-message
1080 (keep-frame "Type \"q\" to delete this window")
1081 (reuse "Type \"q\" to restore this window")
1082 (view-remove-frame-by-deleting "Type \"q\" to delete this frame")
1083 (t "Type \"q\" to iconify this frame"))
1085 ((and (= number-of-windows 2)
1086 (eq (window-frame window) (window-frame (selected-window))))
1087 ;; There are two windows on the help window's frame and the other
1088 ;; window is the selected one.
1089 (if (memq help-window-select '(nil other))
1090 ;; Do not select the help window.
1091 (help-window-display-message
1093 ;; Offer `display-buffer' for consistency with
1094 ;; `help-print-return-message'. This is hardly TRT when
1095 ;; the other window and the selected window display the
1096 ;; same buffer but has been handled this way ever since.
1097 "Type \\[display-buffer] RET to restore the other window"
1098 ;; The classic "two windows" configuration.
1099 "Type \\[delete-other-windows] to delete the help window")
1101 ;; Select help window and tell how to quit.
1102 (select-window window)
1103 (help-window-display-message
1105 "Type \"q\" to restore this window"
1106 "Type \"q\" to delete this window") window)))
1108 ;; Issuing a message with 3 or more windows on the same frame
1109 ;; without selecting the help window doesn't make any sense.
1110 (select-window window)
1111 (help-window-display-message
1113 "Type \"q\" to restore this window"
1114 "Type \"q\" to delete this window") window)))))
1116 (defun help-window-setup (list-of-frames list-of-window-tuples)
1117 "Set up help window.
1118 LIST-OF-FRAMES and LIST-OF-WINDOW-TUPLES are the lists of frames
1119 and window quadruples built by `with-help-window'. The help
1120 window itself is specified by the variable `help-window'."
1121 (let* ((help-buffer (window-buffer help-window))
1122 ;; `help-buffer' now denotes the help window's buffer.
1125 (buffer-local-value 'view-return-to-alist help-buffer)))
1126 (help-entry (assq help-window list-of-window-tuples)))
1128 ;; Handle `help-window-point-marker'.
1129 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1130 (set-window-point help-window help-window-point-marker)
1131 ;; Reset `help-window-point-marker'.
1132 (set-marker help-window-point-marker nil))
1136 ;; `view-return-to-alist' has an entry for the help window.
1138 ((eq help-window (selected-window))
1139 ;; The help window is the selected window, probably because the
1140 ;; user followed a backward/forward button or a cross reference.
1141 ;; In this case just purge stale entries from
1142 ;; `view-return-to-alist' but leave the entry alone and don't
1143 ;; display a message.
1144 (view-return-to-alist-update help-buffer))
1145 ((and help-entry (eq (cadr help-entry) help-buffer))
1146 ;; The help window was not selected but displayed the help
1147 ;; buffer. In this case reuse existing exit information but try
1148 ;; to get back to the selected window when quitting. Don't
1149 ;; display a message since the user must have seen one before.
1150 (view-return-to-alist-update
1151 help-buffer (cons help-window
1152 (cons (selected-window) (cddr view-entry)))))
1154 ;; The help window was not selected, did display the help buffer
1155 ;; earlier, but displayed another buffer when help was invoked.
1156 ;; Set up things so that quitting will show that buffer again.
1157 (view-return-to-alist-update
1158 help-buffer (cons help-window
1159 (cons (selected-window) (cdr help-entry))))
1160 (help-window-setup-finish help-window t))
1162 ;; The help window is new but `view-return-to-alist' had an
1163 ;; entry for it. This should never happen.
1164 (view-return-to-alist-update
1165 help-buffer (cons help-window
1166 (cons (selected-window) 'quit-window)))
1167 (help-window-setup-finish help-window t))))
1169 ;; `view-return-to-alist' does not have an entry for help window
1170 ;; but `list-of-window-tuples' does. Hence `display-buffer' must
1171 ;; have reused an existing window.
1172 (if (eq (cadr help-entry) help-buffer)
1173 ;; The help window displayed `help-buffer' before but no
1174 ;; `view-return-to-alist' entry was found probably because the
1175 ;; user manually switched to the help buffer. Set up things
1176 ;; for `quit-window' although `view-exit-action' should be
1177 ;; able to handle this case all by itself.
1179 (view-return-to-alist-update
1180 help-buffer (cons help-window
1181 (cons (selected-window) 'quit-window)))
1182 (help-window-setup-finish help-window t))
1183 ;; The help window displayed another buffer before. Set up
1184 ;; things in a way that quitting can orderly show that buffer
1185 ;; again. The window-start and window-point information from
1186 ;; `list-of-window-tuples' provide the necessary information.
1187 (view-return-to-alist-update
1188 help-buffer (cons help-window
1189 (cons (selected-window) (cdr help-entry))))
1190 (help-window-setup-finish help-window t)))
1191 ((memq (window-frame help-window) list-of-frames)
1192 ;; The help window is a new window on an existing frame. This
1193 ;; case must be handled specially by `help-window-setup-finish'
1194 ;; and `view-mode-exit' to ascertain that quitting does _not_
1195 ;; inadvertently delete the frame.
1196 (view-return-to-alist-update
1197 help-buffer (cons help-window
1198 (cons (selected-window) 'keep-frame)))
1199 (help-window-setup-finish help-window nil t))
1201 ;; The help window is shown on a new frame. In this case quitting
1202 ;; shall handle both, the help window _and_ its frame. We changed
1203 ;; the default of `view-remove-frame-by-deleting' to t in order to
1204 ;; intuitively DTRT here.
1205 (view-return-to-alist-update
1206 help-buffer (cons help-window (cons (selected-window) t)))
1207 (help-window-setup-finish help-window)))))
1209 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1210 ;; providing the following additional twists:
1212 ;; (1) Issue more accurate messages telling how to scroll and quit the
1215 ;; (2) Make `view-mode-exit' DTRT in more cases.
1217 ;; (3) An option (customizable via `help-window-select') to select the
1218 ;; help window automatically.
1220 ;; (4) A marker (`help-window-point-marker') to move point in the help
1221 ;; window to an arbitrary buffer position.
1223 ;; Note: It's usually always wrong to use `help-print-return-message' in
1224 ;; the body of `with-help-window'.
1225 (defmacro with-help-window (buffer-name &rest body)
1226 "Display buffer BUFFER-NAME in a help window evaluating BODY.
1227 Select help window if the actual value of the user option
1228 `help-window-select' says so. Return last value in BODY."
1229 (declare (indent 1) (debug t))
1230 ;; Bind list-of-frames to `frame-list' and list-of-window-tuples to a
1231 ;; list of one <window window-buffer window-start window-point> tuple
1232 ;; for each live window.
1233 `(let ((list-of-frames (frame-list))
1234 (list-of-window-tuples
1238 (push (list window (window-buffer window)
1239 (window-start window) (window-point window))
1243 ;; Make `help-window' t to trigger `help-mode-finish' to set
1244 ;; `help-window' to the actual help window.
1245 (setq help-window t)
1246 ;; Make `help-window-point-marker' point nowhere (the only place
1247 ;; where this should be set to a buffer position is within BODY).
1248 (set-marker help-window-point-marker nil)
1250 ;; Return value returned by `with-output-to-temp-buffer'.
1251 (with-output-to-temp-buffer ,buffer-name
1253 (when (windowp help-window)
1254 ;; Set up help window.
1255 (help-window-setup list-of-frames list-of-window-tuples))
1256 ;; Reset `help-window' to nil to avoid confusing future calls of
1257 ;; `help-mode-finish' with plain `with-output-to-temp-buffer'.
1258 (setq help-window nil))))
1262 ;;; help.el ends here