1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2012 Free Software Foundation, Inc.
6 ;; 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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
26 ;; This code implements GNU Emacs's on-line help system, the one invoked by
27 ;; `M-x help-for-help'.
31 ;; Get the macro make-help-screen when this is compiled,
32 ;; or run interpreted, but not when the compiled code is loaded.
33 (eval-when-compile (require 'help-macro
))
35 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
36 (add-hook 'temp-buffer-setup-hook
'help-mode-setup
)
37 (add-hook 'temp-buffer-show-hook
'help-mode-finish
)
39 ;; `help-window-point-marker' is a marker you can move to a valid
40 ;; position of the buffer shown in the help window in order to override
41 ;; the standard positioning mechanism (`point-min') chosen by
42 ;; `with-output-to-temp-buffer'. `with-help-window' has this point
43 ;; nowhere before exiting. Currently used by `view-lossage' to assert
44 ;; that the last keystrokes are always visible.
45 (defvar help-window-point-marker
(make-marker)
46 "Marker to override default `window-point' in help windows.")
49 (let ((map (make-sparse-keymap)))
50 (define-key map
(char-to-string help-char
) 'help-for-help
)
51 (define-key map
[help] 'help-for-help)
52 (define-key map [f1] 'help-for-help)
53 (define-key map "." 'display-local-help)
54 (define-key map "?" 'help-for-help)
56 (define-key map "\C-a" 'about-emacs)
57 (define-key map "\C-c" 'describe-copying)
58 (define-key map "\C-d" 'view-emacs-debugging)
59 (define-key map "\C-e" 'view-external-packages)
60 (define-key map "\C-f" 'view-emacs-FAQ)
61 (define-key map "\C-m" 'view-order-manuals)
62 (define-key map "\C-n" 'view-emacs-news)
63 (define-key map "\C-o" 'describe-distribution)
64 (define-key map "\C-p" 'view-emacs-problems)
65 (define-key map "\C-t" 'view-emacs-todo)
66 (define-key map "\C-w" 'describe-no-warranty)
68 ;; This does not fit the pattern, but it is natural given the C-\ command.
69 (define-key map "\C-\\" 'describe-input-method)
71 (define-key map "C" 'describe-coding-system)
72 (define-key map "F" 'Info-goto-emacs-command-node)
73 (define-key map "I" 'describe-input-method)
74 (define-key map "K" 'Info-goto-emacs-key-command-node)
75 (define-key map "L" 'describe-language-environment)
76 (define-key map "S" 'info-lookup-symbol)
78 (define-key map "a" 'apropos-command)
79 (define-key map "b" 'describe-bindings)
80 (define-key map "c" 'describe-key-briefly)
81 (define-key map "d" 'apropos-documentation)
82 (define-key map "e" 'view-echo-area-messages)
83 (define-key map "f" 'describe-function)
84 (define-key map "g" 'describe-gnu-project)
85 (define-key map "h" 'view-hello-file)
87 (define-key map "i" 'info)
88 (define-key map "4i" 'info-other-window)
90 (define-key map "k" 'describe-key)
91 (define-key map "l" 'view-lossage)
92 (define-key map "m" 'describe-mode)
93 (define-key map "n" 'view-emacs-news)
94 (define-key map "p" 'finder-by-keyword)
95 (define-key map "P" 'describe-package)
96 (define-key map "r" 'info-emacs-manual)
97 (define-key map "s" 'describe-syntax)
98 (define-key map "t" 'help-with-tutorial)
99 (define-key map "w" 'where-is)
100 (define-key map "v" 'describe-variable)
101 (define-key map "q" 'help-quit)
103 "Keymap for characters following the Help key.")
105 (define-key global-map (char-to-string help-char) 'help-command)
106 (define-key global-map [help] 'help-command
)
107 (define-key global-map
[f1] 'help-command)
108 (fset 'help-command help-map)
110 ;; insert-button makes the action nil if it is not store somewhere
111 (defvar help-button-cache nil)
115 "Just exit from the Help command's command loop."
119 (defvar help-return-method nil
120 "What to do to \"exit\" the help buffer.
122 (WINDOW . t) delete the selected window (and possibly its frame,
123 see `quit-window'), go to WINDOW.
124 (WINDOW . quit-window) do quit-window, then select WINDOW.
125 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
127 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
128 (defun help-print-return-message (&optional function)
129 "Display or return message saying how to restore windows after help command.
130 This function assumes that `standard-output' is the help buffer.
131 It computes a message, and applies the optional argument FUNCTION to it.
132 If FUNCTION is nil, it applies `message', thus displaying the message.
133 In addition, this function sets up `help-return-method', which see, that
134 specifies what to do when the user exits the help buffer."
135 (and (not (get-buffer-window standard-output))
139 (special-display-p (buffer-name standard-output)))
140 (setq help-return-method (cons (selected-window) t))
141 ;; If the help output buffer is a special display buffer,
142 ;; don't say anything about how to get rid of it.
143 ;; First of all, the user will do that with the window
144 ;; manager, not with Emacs.
145 ;; Secondly, the buffer has not been displayed yet,
146 ;; so we don't know whether its frame will be selected.
148 ((not (one-window-p t))
149 (setq help-return-method
150 (cons (selected-window) 'quit-window))
151 "Type \\[display-buffer] RET to restore the other window.")
153 (setq help-return-method (cons (selected-window) t))
154 "Type \\[delete-other-windows] to remove help window.")
156 (setq help-return-method
157 (list (selected-window) (window-buffer)
158 (window-start) (window-point)))
159 "Type \\[switch-to-buffer] RET to remove help window."))))
160 (funcall (or function 'message)
163 (substitute-command-keys first-message))
164 (if first-message " ")
165 ;; If the help buffer will go in a separate frame,
166 ;; it's no use mentioning a command to scroll, so don't.
167 (if (or pop-up-windows
168 (special-display-p (buffer-name standard-output)))
170 (if (same-window-p (buffer-name standard-output))
171 ;; Say how to scroll this window.
172 (substitute-command-keys
173 "\\[scroll-up] to scroll the help.")
174 ;; Say how to scroll some other window.
175 (substitute-command-keys
176 "\\[scroll-other-window] to scroll the help."))))))))
178 ;; So keyboard macro definitions are documented correctly
179 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
181 (defalias 'help 'help-for-help-internal)
182 ;; find-function can find this.
183 (defalias 'help-for-help 'help-for-help-internal)
184 ;; It can't find this, but nobody will look.
185 (make-help-screen help-for-help-internal
186 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
187 ;; Don't purecopy this one, because it's not evaluated (it's
188 ;; directly used as a docstring in a function definition, so it'll
189 ;; be moved to the DOC file anyway: no need for purecopying it).
190 "You have typed %THIS-KEY%, the help character. Type a Help option:
191 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
193 a PATTERN Show commands whose name matches the PATTERN (a list of words
194 or a regexp). See also the `apropos' command.
195 b Display all key bindings.
196 c KEYS Display the command name run by the given key sequence.
197 C CODING Describe the given coding system, or RET for current ones.
198 d PATTERN Show a list of functions, variables, and other items whose
199 documentation matches the PATTERN (a list of words or a regexp).
200 e Go to the *Messages* buffer which logs echo-area messages.
201 f FUNCTION Display documentation for the given function.
202 F COMMAND Show the on-line manual's section that describes the command.
203 g Display information about the GNU project.
204 h Display the HELLO file which illustrates various scripts.
205 i Start the Info documentation reader: read on-line manuals.
206 I METHOD Describe a specific input method, or RET for current.
207 k KEYS Display the full documentation for the key sequence.
208 K KEYS Show the on-line manual's section for the command bound to KEYS.
209 l Show last 300 input keystrokes (lossage).
210 L LANG-ENV Describes a specific language environment, or RET for current.
211 m Display documentation of current minor modes and current major mode,
212 including their special commands.
213 n Display news of recent Emacs changes.
214 p TOPIC Find packages matching a given topic keyword.
215 r Display the Emacs manual in Info mode.
216 s Display contents of current syntax table, plus explanations.
217 S SYMBOL Show the section for the given symbol in the on-line manual
218 for the programming language used in this buffer.
219 t Start the Emacs learn-by-doing tutorial.
220 v VARIABLE Display the given variable's documentation and value.
221 w COMMAND Display which keystrokes invoke the given command (where-is).
222 . Display any available local help at point in the echo area.
224 C-a Information about Emacs.
225 C-c Emacs copying permission (GNU General Public License).
226 C-d Instructions for debugging GNU Emacs.
227 C-e External packages and information about Emacs.
229 C-m How to order printed Emacs manuals.
230 C-n News of recent Emacs changes.
231 C-o Emacs ordering and distribution information.
232 C-p Info about known Emacs problems.
234 C-w Information on absence of warranty for GNU Emacs."
239 (defun function-called-at-point ()
240 "Return a function around point or else called by the list containing point.
241 If that doesn't give a function, return nil."
242 (with-syntax-table emacs-lisp-mode-syntax-table
243 (or (condition-case ()
245 (or (not (zerop (skip-syntax-backward "_w")))
246 (eq (char-syntax (following-char)) ?w)
247 (eq (char-syntax (following-char)) ?_)
249 (skip-chars-forward "'")
250 (let ((obj (read (current-buffer))))
251 (and (symbolp obj) (fboundp obj) obj)))
256 (narrow-to-region (max (point-min)
257 (- (point) 1000)) (point-max))
258 ;; Move up to surrounding paren, then after the open.
261 ;; If there is space here, this is probably something
262 ;; other than a real Lisp function call, so ignore it.
263 (if (looking-at "[ \t]")
264 (error "Probably not a Lisp function call"))
265 (let ((obj (read (current-buffer))))
266 (and (symbolp obj) (fboundp obj) obj))))
268 (let* ((str (find-tag-default))
269 (sym (if str (intern-soft str))))
270 (if (and sym (fboundp sym))
273 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
274 (setq sym (intern-soft (match-string 1 str)))
275 (and (fboundp sym) sym))))))))
278 ;;; `User' help functions
280 (defun view-help-file (file &optional dir)
281 (view-file (expand-file-name file (or dir data-directory)))
282 (goto-address-mode 1)
283 (goto-char (point-min)))
285 (defun describe-distribution ()
286 "Display info on how to obtain the latest version of GNU Emacs."
288 (view-help-file "DISTRIB"))
290 (defun describe-copying ()
291 "Display info on how you may redistribute copies of GNU Emacs."
293 (view-help-file "COPYING"))
295 (defun describe-gnu-project ()
296 "Display info on the GNU project."
298 (view-help-file "THE-GNU-PROJECT"))
300 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
302 (defun describe-no-warranty ()
303 "Display info on all the kinds of warranty Emacs does NOT have."
306 (let (case-fold-search)
307 (search-forward "Disclaimer of Warranty")
311 (defun describe-prefix-bindings ()
312 "Describe the bindings of the prefix used to reach this command.
313 The prefix described consists of all but the last event
314 of the key sequence that ran this command."
316 (let ((key (this-command-keys)))
319 (substring key 0 (1- (length key)))
320 (let ((prefix (make-vector (1- (length key)) nil))
322 (while (< i (length prefix))
323 (aset prefix i (aref key i))
326 ;; Make C-h after a prefix, when not specifically bound,
327 ;; run describe-prefix-bindings.
328 (setq prefix-help-command 'describe-prefix-bindings)
330 (defun view-emacs-news (&optional version)
331 "Display info on recent changes to Emacs.
332 With argument, display info only for the selected version."
335 (setq version emacs-major-version))
336 (when (consp version)
342 (insert-file-contents
343 (expand-file-name file data-directory))
344 (while (re-search-forward
345 (if (member file '("NEWS.18" "NEWS.1-17"))
346 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
347 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
348 (setq res (cons (match-string-no-properties 1) res)))))
350 (directory-files data-directory nil
351 "^NEWS\\.[0-9][-0-9]*$" nil)))
352 (sort (delete-dups res) (lambda (a b) (string< b a)))))
353 (current (car all-versions)))
354 (setq version (completing-read
355 (format "Read NEWS for the version (default %s): " current)
356 all-versions nil nil nil nil current))
357 (if (integerp (string-to-number version))
358 (setq version (string-to-number version))
359 (unless (or (member version all-versions)
360 (<= (string-to-number version) (string-to-number current)))
361 (error "No news about version %s" version)))))
362 (when (integerp version)
363 (cond ((<= version 12)
364 (setq version (format "1.%d" version)))
366 (setq version (format "%d" version)))
367 ((> version emacs-major-version)
368 (error "No news about Emacs %d (yet)" version))))
369 (let* ((vn (if (stringp version)
370 (string-to-number version)
373 ((>= vn emacs-major-version) "NEWS")
374 ((< vn 18) "NEWS.1-17")
375 (t (format "NEWS.%d" vn))))
377 (view-file (expand-file-name file data-directory))
379 (goto-char (point-min))
380 (when (stringp version)
381 (when (re-search-forward
382 (concat (if (< vn 19)
383 "Changes in Emacs[ \t]*"
384 "^\* [^0-9\n]*") version "$")
390 (while (and (setq res
393 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
394 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
395 (equal (match-string-no-properties 1) version)))
396 (or res (goto-char (point-max)))
400 (defun view-emacs-todo (&optional _arg)
401 "Display the Emacs TODO list."
403 (view-help-file "TODO"))
405 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
408 (defun view-echo-area-messages ()
409 "View the log of recent echo-area messages: the `*Messages*' buffer.
410 The number of messages retained in that buffer
411 is specified by the variable `message-log-max'."
413 (switch-to-buffer (get-buffer-create "*Messages*")))
415 (defun view-order-manuals ()
416 "Display the Emacs ORDERS file."
418 (view-help-file "ORDERS"))
420 (defun view-emacs-FAQ ()
421 "Display the Emacs Frequently Asked Questions (FAQ) file."
423 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
426 (defun view-emacs-problems ()
427 "Display info on known problems with Emacs and possible workarounds."
429 (view-help-file "PROBLEMS"))
431 (defun view-emacs-debugging ()
432 "Display info on how to debug Emacs problems."
434 (view-help-file "DEBUG"))
436 (defun view-external-packages ()
437 "Display external packages and information about Emacs."
439 (view-help-file "MORE.STUFF"))
441 (defun view-lossage ()
442 "Display last 300 input keystrokes.
444 To record all your input on a file, use `open-dribble-file'."
446 (help-setup-xref (list #'view-lossage)
447 (called-interactively-p 'interactive))
448 (with-help-window (help-buffer)
449 (princ (mapconcat (lambda (key)
450 (if (or (integerp key) (symbolp key) (listp key))
451 (single-key-description key)
452 (prin1-to-string key nil)))
455 (with-current-buffer standard-output
456 (goto-char (point-min))
457 (while (progn (move-to-column 50) (not (eobp)))
458 (when (search-forward " " nil t)
461 ;; jidanni wants to see the last keystrokes immediately.
462 (set-marker help-window-point-marker (point)))))
467 (defun describe-bindings (&optional prefix buffer)
468 "Show a list of all defined keys, and their definitions.
469 We put that list in a buffer, and display the buffer.
471 The optional argument PREFIX, if non-nil, should be a key sequence;
472 then we display only bindings that start with that prefix.
473 The optional argument BUFFER specifies which buffer's bindings
474 to display (default, the current buffer). BUFFER can be a buffer
477 (or buffer (setq buffer (current-buffer)))
478 (help-setup-xref (list #'describe-bindings prefix buffer)
479 (called-interactively-p 'interactive))
480 (with-current-buffer buffer
481 (describe-bindings-internal nil prefix)))
483 ;; This function used to be in keymap.c.
484 (defun describe-bindings-internal (&optional menus prefix)
485 "Show a list of all defined keys, and their definitions.
486 We put that list in a buffer, and display the buffer.
488 The optional argument MENUS, if non-nil, says to mention menu bindings.
489 \(Ordinarily these are omitted from the output.)
490 The optional argument PREFIX, if non-nil, should be a key sequence;
491 then we display only bindings that start with that prefix."
492 (let ((buf (current-buffer)))
493 (with-help-window "*Help*"
494 (with-current-buffer standard-output
495 (describe-buffer-bindings buf prefix menus)))))
497 (defun where-is (definition &optional insert)
498 "Print message listing key sequences that invoke the command DEFINITION.
499 Argument is a command definition, usually a symbol with a function definition.
500 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
502 (let ((fn (function-called-at-point))
503 (enable-recursive-minibuffers t)
505 (setq val (completing-read
507 (format "Where is command (default %s): " fn)
508 "Where is command: ")
509 obarray 'commandp t))
510 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
511 (unless definition (error "No command"))
512 (let ((func (indirect-function definition))
514 (standard-output (if insert (current-buffer) standard-output)))
515 ;; In DEFS, find all symbols that are aliases for DEFINITION.
516 (mapatoms (lambda (symbol)
517 (and (fboundp symbol)
518 (not (eq symbol definition))
519 (eq func (condition-case ()
520 (indirect-function symbol)
522 (push symbol defs))))
523 ;; Look at all the symbols--first DEFINITION,
525 (dolist (symbol (cons definition defs))
526 (let* ((remapped (command-remapping symbol))
527 (keys (where-is-internal
528 symbol overriding-local-map nil nil remapped))
529 (keys (mapconcat 'key-description keys ", "))
533 (if (> (length keys) 0)
535 (format "%s (%s) (remapped from %s)"
536 keys remapped symbol)
537 (format "%s (%s)" keys symbol))
538 (format "M-x %s RET" symbol))
539 (if (> (length keys) 0)
541 (format "%s is remapped to %s which is on %s"
542 symbol remapped keys)
543 (format "%s is on %s" symbol keys))
544 ;; If this is the command the user asked about,
545 ;; and it is not on any key, say so.
546 ;; For other symbols, its aliases, say nothing
547 ;; about them unless they are on keys.
548 (if (eq symbol definition)
549 (format "%s is not on any key" symbol)))))
551 (unless (eq symbol definition)
552 (princ ";\n its alias "))
556 (defun help-key-description (key untranslated)
557 (let ((string (key-description key)))
558 (if (or (not untranslated)
559 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
561 (let ((otherstring (key-description untranslated)))
562 (if (equal string otherstring)
564 (format "%s (translated from %s)" string otherstring))))))
566 (defun describe-key-briefly (&optional key insert untranslated)
567 "Print the name of the function KEY invokes. KEY is a string.
568 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
569 If non-nil, UNTRANSLATED is a vector of the untranslated events.
570 It can also be a number in which case the untranslated events from
571 the last key hit are used.
573 If KEY is a menu item or a tool-bar button that is disabled, this command
574 temporarily enables it to allow getting help on disabled items and buttons."
576 (let ((enable-disabled-menus-and-buttons t)
577 (cursor-in-echo-area t)
581 ;; If yank-menu is empty, populate it temporarily, so that
582 ;; "Select and Paste" menu can generate a complete event.
583 (when (null (cdr yank-menu))
584 (setq saved-yank-menu (copy-sequence yank-menu))
585 (menu-bar-update-yank-menu "(any string)" nil))
586 (setq key (read-key-sequence "Describe key (or click or menu item): "))
587 ;; If KEY is a down-event, read and discard the
588 ;; corresponding up-event. Note that there are also
589 ;; down-events on scroll bars and mode lines: the actual
590 ;; event then is in the second element of the vector.
592 (let ((last-idx (1- (length key))))
593 (and (eventp (aref key last-idx))
594 (memq 'down (event-modifiers (aref key last-idx)))))
598 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
600 ;; Put yank-menu back as it was, if we changed it.
601 (when saved-yank-menu
602 (setq yank-menu (copy-sequence saved-yank-menu))
603 (fset 'yank-menu (cons 'keymap yank-menu))))))
604 (if (numberp untranslated)
605 (setq untranslated (this-single-command-raw-keys)))
606 (let* ((event (if (and (symbolp (aref key 0))
608 (consp (aref key 1)))
611 (modifiers (event-modifiers event))
612 (standard-output (if insert (current-buffer) standard-output))
613 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
614 (memq 'drag modifiers)) " at that spot" ""))
615 (defn (key-binding key t))
617 ;; Handle the case where we faked an entry in "Select and Paste" menu.
618 (if (and (eq defn nil)
619 (stringp (aref key (1- (length key))))
620 (eq (key-binding (substring key 0 -1)) 'yank-menu))
621 (setq defn 'menu-bar-select-yank))
622 ;; Don't bother user with strings from (e.g.) the select-paste menu.
623 (if (stringp (aref key (1- (length key))))
624 (aset key (1- (length key)) "(any string)"))
625 (if (and (> (length untranslated) 0)
626 (stringp (aref untranslated (1- (length untranslated)))))
627 (aset untranslated (1- (length untranslated)) "(any string)"))
628 ;; Now describe the key, perhaps as changed.
629 (setq key-desc (help-key-description key untranslated))
630 (if (or (null defn) (integerp defn) (equal defn 'undefined))
631 (princ (format "%s%s is undefined" key-desc mouse-msg))
632 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
634 (defun describe-key (&optional key untranslated up-event)
635 "Display documentation of the function invoked by KEY.
636 KEY can be any kind of a key sequence; it can include keyboard events,
637 mouse events, and/or menu events. When calling from a program,
638 pass KEY as a string or a vector.
640 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
641 It can also be a number, in which case the untranslated events from
642 the last key sequence entered are used.
643 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
645 If KEY is a menu item or a tool-bar button that is disabled, this command
646 temporarily enables it to allow getting help on disabled items and buttons."
648 (let ((enable-disabled-menus-and-buttons t)
649 (cursor-in-echo-area t)
653 ;; If yank-menu is empty, populate it temporarily, so that
654 ;; "Select and Paste" menu can generate a complete event.
655 (when (null (cdr yank-menu))
656 (setq saved-yank-menu (copy-sequence yank-menu))
657 (menu-bar-update-yank-menu "(any string)" nil))
658 (setq key (read-key-sequence "Describe key (or click or menu item): "))
661 (prefix-numeric-value current-prefix-arg)
662 ;; If KEY is a down-event, read and include the
663 ;; corresponding up-event. Note that there are also
664 ;; down-events on scroll bars and mode lines: the actual
665 ;; event then is in the second element of the vector.
667 (let ((last-idx (1- (length key))))
668 (and (eventp (aref key last-idx))
669 (memq 'down (event-modifiers (aref key last-idx)))))
670 (or (and (eventp (aref key 0))
671 (memq 'down (event-modifiers (aref key 0)))
672 ;; However, for the C-down-mouse-2 popup
673 ;; menu, there is no subsequent up-event. In
674 ;; this case, the up-event is the next
675 ;; element in the supplied vector.
677 (and (> (length key) 1)
678 (eventp (aref key 1))
679 (memq 'down (event-modifiers (aref key 1)))))
681 ;; Put yank-menu back as it was, if we changed it.
682 (when saved-yank-menu
683 (setq yank-menu (copy-sequence saved-yank-menu))
684 (fset 'yank-menu (cons 'keymap yank-menu))))))
685 (if (numberp untranslated)
686 (setq untranslated (this-single-command-raw-keys)))
687 (let* ((event (aref key (if (and (symbolp (aref key 0))
689 (consp (aref key 1)))
692 (modifiers (event-modifiers event))
693 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
694 (memq 'drag modifiers)) " at that spot" ""))
695 (defn (key-binding key t))
696 defn-up defn-up-tricky ev-type
697 mouse-1-remapped mouse-1-tricky)
699 ;; Handle the case where we faked an entry in "Select and Paste" menu.
700 (when (and (eq defn nil)
701 (stringp (aref key (1- (length key))))
702 (eq (key-binding (substring key 0 -1)) 'yank-menu))
703 (setq defn 'menu-bar-select-yank))
704 (if (or (null defn) (integerp defn) (equal defn 'undefined))
705 (message "%s%s is undefined"
706 (help-key-description key untranslated) mouse-msg)
707 (help-setup-xref (list #'describe-function defn)
708 (called-interactively-p 'interactive))
709 ;; Don't bother user with strings from (e.g.) the select-paste menu.
710 (when (stringp (aref key (1- (length key))))
711 (aset key (1- (length key)) "(any string)"))
712 (when (and untranslated
713 (stringp (aref untranslated (1- (length untranslated)))))
714 (aset untranslated (1- (length untranslated))
716 ;; Need to do this before erasing *Help* buffer in case event
717 ;; is a mouse click in an existing *Help* buffer.
719 (setq ev-type (event-basic-type up-event))
720 (let ((sequence (vector up-event)))
721 (when (and (eq ev-type 'mouse-1)
722 mouse-1-click-follows-link
723 (not (eq mouse-1-click-follows-link 'double))
724 (setq mouse-1-remapped
725 (mouse-on-link-p (event-start up-event))))
726 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
727 (> mouse-1-click-follows-link 0)))
728 (cond ((stringp mouse-1-remapped)
729 (setq sequence mouse-1-remapped))
730 ((vectorp mouse-1-remapped)
731 (setcar up-event (elt mouse-1-remapped 0)))
732 (t (setcar up-event 'mouse-2))))
733 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
735 (setq sequence (vector up-event))
736 (aset sequence 0 'mouse-1)
737 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
738 (with-help-window (help-buffer)
739 (princ (help-key-description key untranslated))
741 %s runs the command %S, which is "
743 (describe-function-1 defn)
745 (unless (or (null defn-up)
747 (equal defn-up 'undefined))
750 ----------------- up-event %s----------------
752 %s%s%s runs the command %S, which is "
753 (if mouse-1-tricky "(short click) " "")
754 (key-description (vector up-event))
757 " is remapped to <mouse-2>, which" "")
759 (describe-function-1 defn-up))
760 (unless (or (null defn-up-tricky)
761 (integerp defn-up-tricky)
762 (eq defn-up-tricky 'undefined))
765 ----------------- up-event (long click) ----------------
767 Pressing <%S>%s for longer than %d milli-seconds
768 runs the command %S, which is "
770 mouse-1-click-follows-link
772 (describe-function-1 defn-up-tricky)))))))
774 (defun describe-mode (&optional buffer)
775 "Display documentation of current major mode and minor modes.
776 A brief summary of the minor modes comes first, followed by the
777 major mode description. This is followed by detailed
778 descriptions of the minor modes, each on a separate page.
780 For this to work correctly for a minor mode, the mode's indicator
781 variable \(listed in `minor-mode-alist') must also be a function
782 whose documentation describes the minor mode.
784 If called from Lisp with a non-nil BUFFER argument, display
785 documentation for the major and minor modes of that buffer."
787 (unless buffer (setq buffer (current-buffer)))
788 (help-setup-xref (list #'describe-mode buffer)
789 (called-interactively-p 'interactive))
790 ;; For the sake of help-do-xref and help-xref-go-back,
791 ;; don't switch buffers before calling `help-buffer'.
792 (with-help-window (help-buffer)
793 (with-current-buffer buffer
795 ;; Older packages do not register in minor-mode-list but only in
797 (dolist (x minor-mode-alist)
799 (unless (memq x minor-mode-list)
800 (push x minor-mode-list)))
801 ;; Find enabled minor mode we will want to mention.
802 (dolist (mode minor-mode-list)
803 ;; Document a minor mode if it is listed in minor-mode-alist,
804 ;; non-nil, and has a function definition.
805 (let ((fmode (or (get mode :minor-mode-function) mode)))
806 (and (boundp mode) (symbol-value mode)
808 (let ((pretty-minor-mode
809 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
812 (substring (symbol-name fmode)
813 0 (match-beginning 0)))
815 (push (list fmode pretty-minor-mode
816 (format-mode-line (assq mode minor-mode-alist)))
820 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
822 (princ "Enabled minor modes:\n")
823 (make-local-variable 'help-button-cache)
824 (with-current-buffer standard-output
825 (dolist (mode minor-modes)
826 (let ((mode-function (nth 0 mode))
827 (pretty-minor-mode (nth 1 mode))
828 (indicator (nth 2 mode)))
829 (add-text-properties 0 (length pretty-minor-mode)
830 '(face bold) pretty-minor-mode)
832 (goto-char (point-max))
834 (push (point-marker) help-button-cache)
835 ;; Document the minor modes fully.
836 (insert pretty-minor-mode)
837 (princ (format " minor mode (%s):\n"
838 (if (zerop (length indicator))
840 (format "indicator%s"
842 (princ (documentation mode-function)))
843 (insert-button pretty-minor-mode
844 'action (car help-button-cache)
846 'help-echo "mouse-2, RET: show full information")
852 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
853 ;; Document the major mode.
854 (let ((mode mode-name))
855 (with-current-buffer standard-output
856 (let ((start (point)))
857 (insert (format-mode-line mode nil nil buffer))
858 (add-text-properties start (point) '(face bold)))))
860 (let* ((mode major-mode)
861 (file-name (find-lisp-object-file-name mode nil)))
863 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
864 ;; Make a hyperlink to the library.
865 (with-current-buffer standard-output
867 (re-search-backward "`\\([^`']+\\)'" nil t)
868 (help-xref-button 1 'help-function-def mode file-name)))))
870 (princ (documentation major-mode)))))
871 ;; For the sake of IELM and maybe others
875 (defun describe-minor-mode (minor-mode)
876 "Display documentation of a minor mode given as MINOR-MODE.
877 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
878 appeared on the mode-line."
879 (interactive (list (completing-read
882 (describe-minor-mode-completion-table-for-symbol)
883 (describe-minor-mode-completion-table-for-indicator)
885 (if (symbolp minor-mode)
886 (setq minor-mode (symbol-name minor-mode)))
887 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
888 (indicators (describe-minor-mode-completion-table-for-indicator)))
890 ((member minor-mode symbols)
891 (describe-minor-mode-from-symbol (intern minor-mode)))
892 ((member minor-mode indicators)
893 (describe-minor-mode-from-indicator minor-mode))
895 (error "No such minor mode: %s" minor-mode)))))
898 (defun describe-minor-mode-completion-table-for-symbol ()
899 ;; In order to list up all minor modes, minor-mode-list
900 ;; is used here instead of minor-mode-alist.
901 (delq nil (mapcar 'symbol-name minor-mode-list)))
903 (defun describe-minor-mode-from-symbol (symbol)
904 "Display documentation of a minor mode given as a symbol, SYMBOL"
905 (interactive (list (intern (completing-read
906 "Minor mode symbol: "
907 (describe-minor-mode-completion-table-for-symbol)))))
909 (describe-function symbol)
910 (describe-variable symbol)))
913 (defun describe-minor-mode-completion-table-for-indicator ()
916 (let ((i (format-mode-line x)))
917 ;; remove first space if existed
927 (defun describe-minor-mode-from-indicator (indicator)
928 "Display documentation of a minor mode specified by INDICATOR.
929 If you call this function interactively, you can give indicator which
930 is currently activated with completion."
933 "Minor mode indicator: "
934 (describe-minor-mode-completion-table-for-indicator))))
935 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
937 (describe-minor-mode-from-symbol minor-mode)
938 (error "Cannot find minor mode for `%s'" indicator))))
940 (defun lookup-minor-mode-from-indicator (indicator)
941 "Return a minor mode symbol from its indicator on the mode line."
942 ;; remove first space if existed
943 (if (and (< 0 (length indicator))
944 (eq (aref indicator 0) ?\s))
945 (setq indicator (substring indicator 1)))
946 (let ((minor-modes minor-mode-alist)
949 (let* ((minor-mode (car (car minor-modes)))
950 (anindicator (format-mode-line
951 (car (cdr (car minor-modes))))))
952 ;; remove first space if existed
953 (if (and (stringp anindicator)
954 (> (length anindicator) 0)
955 (eq (aref anindicator 0) ?\s))
956 (setq anindicator (substring anindicator 1)))
957 (if (equal indicator anindicator)
958 (setq result minor-mode
960 (setq minor-modes (cdr minor-modes)))))
963 ;;; Automatic resizing of temporary buffers.
964 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
965 "Maximum height of a window displaying a temporary buffer.
966 This is effective only when Temp Buffer Resize mode is enabled.
967 The value is the maximum height (in lines) which
968 `resize-temp-buffer-window' will give to a window displaying a
969 temporary buffer. It can also be a function to be called to
970 choose the height for such a buffer. It gets one argument, the
971 buffer, and should return a positive integer. At the time the
972 function is called, the window to be resized is selected."
973 :type '(choice integer function)
977 (define-minor-mode temp-buffer-resize-mode
978 "Toggle auto-shrinking temp buffer windows (Temp Buffer Resize mode).
979 With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
980 is positive, and disable it otherwise. If called from Lisp,
981 enable the mode if ARG is omitted or nil.
983 When Temp Buffer Resize mode is enabled, the windows in which we
984 show a temporary buffer are automatically reduced in height to
985 fit the buffer's contents, but never more than
986 `temp-buffer-max-height' nor less than `window-min-height'.
988 This mode is used by `help', `apropos' and `completion' buffers,
990 :global t :group 'help
991 (if temp-buffer-resize-mode
992 ;; `help-make-xrefs' may add a `back' button and thus increase the
993 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
994 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
995 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
997 (defun resize-temp-buffer-window ()
998 "Resize the selected window to fit its contents.
999 Will not make it higher than `temp-buffer-max-height' nor smaller
1000 than `window-min-height'. Do nothing if the selected window is
1001 not vertically combined or some of its contents are scrolled out
1003 (when (and (pos-visible-in-window-p (point-min))
1004 (window-combined-p))
1005 (fit-window-to-buffer
1007 (if (functionp temp-buffer-max-height)
1008 (funcall temp-buffer-max-height (window-buffer))
1009 temp-buffer-max-height))))
1012 (defcustom help-window-select 'other
1013 "Non-nil means select help window for viewing.
1015 never (nil) Select help window only if there is no other window
1017 other Select help window unless the selected window is the
1018 only other window on the help window's frame.
1019 always (t) Always select the help window.
1021 This option has effect if and only if the help window was created
1022 by `with-help-window'"
1023 :type '(choice (const :tag "never (nil)" nil)
1024 (const :tag "other" other)
1025 (const :tag "always (t)" t))
1029 (defcustom help-enable-auto-load t
1030 "Whether Help commands can perform autoloading.
1031 If non-nil, whenever \\[describe-function] is called for an
1032 autoloaded function whose docstring contains any key substitution
1033 construct (see `substitute-command-keys'), the library is loaded,
1034 so that the documentation can show the right key bindings."
1039 (defun help-window-display-message (quit-part window &optional scroll)
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 SCROLL non-nil means tell how to scroll WINDOW.
1043 SCROLL equal `other' means tell how to scroll the \"other\"
1047 ;; If we don't have QUIT-PART we probably reuse a window
1048 ;; showing the same buffer so we don't show any message.
1049 ((not quit-part) nil)
1050 ((pos-visible-in-window-p
1051 (with-current-buffer (window-buffer window)
1052 (point-max)) window t)
1053 ;; Buffer end is at least partially visible, no need to talk
1057 ", \\[scroll-other-window] to scroll help.")
1058 (scroll ", \\[scroll-up] to scroll help."))))
1060 (substitute-command-keys (concat quit-part scroll-part)))))
1062 (defun help-window-setup (help-window)
1063 "Set up help window for `with-help-window'.
1064 HELP-WINDOW is the window used for displaying the help buffer."
1065 (let* ((help-buffer (when (window-live-p help-window)
1066 (window-buffer help-window)))
1067 (help-setup (when (window-live-p help-window)
1068 (car (window-parameter help-window 'quit-restore)))))
1070 ;; Handle `help-window-point-marker'.
1071 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1072 (set-window-point help-window help-window-point-marker)
1073 ;; Reset `help-window-point-marker'.
1074 (set-marker help-window-point-marker nil))
1077 ((or (eq help-window (selected-window))
1078 (and (or (eq help-window-select t)
1079 (eq help-setup 'frame)
1080 (and (eq help-window-select 'other)
1081 (eq (window-frame help-window) (selected-frame))
1082 (> (length (window-list nil 'no-mini)) 2)))
1083 (select-window help-window)))
1084 ;; The help window is or gets selected ...
1085 (help-window-display-message
1087 ((eq help-setup 'window)
1088 ;; ... and is new, ...
1089 "Type \"q\" to delete help window")
1090 ((eq help-setup 'frame)
1091 "Type \"q\" to delete help frame")
1092 ((eq help-setup 'other)
1093 ;; ... or displayed some other buffer before.
1094 "Type \"q\" to restore previous buffer"))
1096 ((and (eq (window-frame help-window) (selected-frame))
1097 (= (length (window-list nil 'no-mini)) 2))
1098 ;; There are two windows on the help window's frame and the
1099 ;; other one is the selected one.
1100 (help-window-display-message
1102 ((eq help-setup 'window)
1103 "Type \\[delete-other-windows] to delete the help window")
1104 ((eq help-setup 'other)
1105 "Type \"q\" in help window to restore its previous buffer"))
1106 help-window 'other))
1108 ;; The help window is not selected ...
1109 (help-window-display-message
1111 ((eq help-setup 'window)
1112 ;; ... and is new, ...
1113 "Type \"q\" in help window to delete it")
1114 ((eq help-setup 'other)
1115 ;; ... or displayed some other buffer before.
1116 "Type \"q\" in help window to restore previous buffer"))
1119 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1120 ;; providing the following additional twists:
1122 ;; (1) Issue more accurate messages telling how to scroll and quit the
1125 ;; (2) An option (customizable via `help-window-select') to select the
1126 ;; help window automatically.
1128 ;; (3) A marker (`help-window-point-marker') to move point in the help
1129 ;; window to an arbitrary buffer position.
1131 ;; Note: It's usually always wrong to use `help-print-return-message' in
1132 ;; the body of `with-help-window'.
1133 (defmacro with-help-window (buffer-name &rest body)
1134 "Display buffer with name BUFFER-NAME in a help window evaluating BODY.
1135 Select help window if the actual value of the user option
1136 `help-window-select' says so. Return last value in BODY."
1137 (declare (indent 1) (debug t))
1139 ;; Make `help-window-point-marker' point nowhere. The only place
1140 ;; where this should be set to a buffer position is within BODY.
1141 (set-marker help-window-point-marker nil)
1143 (temp-buffer-show-hook
1144 (cons (lambda () (setq help-window (selected-window)))
1145 temp-buffer-show-hook)))
1146 ;; Return value returned by `with-output-to-temp-buffer'.
1148 (with-output-to-temp-buffer ,buffer-name
1150 (help-window-setup help-window)))))
1152 ;; Called from C, on encountering `help-char' when reading a char.
1153 ;; Don't print to *Help*; that would clobber Help history.
1154 (defun help-form-show ()
1155 "Display the output of a non-nil `help-form'."
1156 (let ((msg (eval help-form)))
1158 (with-output-to-temp-buffer " *Char Help*"
1163 ;;; help.el ends here