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' in help windows.")
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 (and (not (get-buffer-window standard-output))
151 (special-display-p (buffer-name standard-output)))
152 (setq help-return-method (cons (selected-window) t))
153 ;; If the help output buffer is a special display buffer,
154 ;; don't say anything about how to get rid of it.
155 ;; First of all, the user will do that with the window
156 ;; manager, not with Emacs.
157 ;; Secondly, the buffer has not been displayed yet,
158 ;; so we don't know whether its frame will be selected.
160 (display-buffer-reuse-frames
161 (setq help-return-method (cons (selected-window)
164 ((not (one-window-p t))
165 (setq help-return-method
166 (cons (selected-window) 'quit-window))
167 "Type \\[display-buffer] RET to restore the other window.")
169 (setq help-return-method (cons (selected-window) t))
170 "Type \\[delete-other-windows] to remove help window.")
172 (setq help-return-method
173 (list (selected-window) (window-buffer)
174 (window-start) (window-point)))
175 "Type \\[switch-to-buffer] RET to remove help window."))))
176 (funcall (or function 'message)
179 (substitute-command-keys first-message))
180 (if first-message " ")
181 ;; If the help buffer will go in a separate frame,
182 ;; it's no use mentioning a command to scroll, so don't.
183 (if (or pop-up-windows
184 (special-display-p (buffer-name standard-output)))
186 (if (same-window-p (buffer-name standard-output))
187 ;; Say how to scroll this window.
188 (substitute-command-keys
189 "\\[scroll-up] to scroll the help.")
190 ;; Say how to scroll some other window.
191 (substitute-command-keys
192 "\\[scroll-other-window] to scroll the help."))))))))
194 ;; So keyboard macro definitions are documented correctly
195 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
197 (defalias 'help 'help-for-help-internal)
198 ;; find-function can find this.
199 (defalias 'help-for-help 'help-for-help-internal)
200 ;; It can't find this, but nobody will look.
201 (make-help-screen help-for-help-internal
202 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
203 ;; Don't purecopy this one, because it's not evaluated (it's
204 ;; directly used as a docstring in a function definition, so it'll
205 ;; be moved to the DOC file anyway: no need for purecopying it).
206 "You have typed %THIS-KEY%, the help character. Type a Help option:
207 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
209 a PATTERN Show commands whose name matches the PATTERN (a list of words
210 or a regexp). See also the `apropos' command.
211 b Display all key bindings.
212 c KEYS Display the command name run by the given key sequence.
213 C CODING Describe the given coding system, or RET for current ones.
214 d PATTERN Show a list of functions, variables, and other items whose
215 documentation matches the PATTERN (a list of words or a regexp).
216 e Go to the *Messages* buffer which logs echo-area messages.
217 f FUNCTION Display documentation for the given function.
218 F COMMAND Show the on-line manual's section that describes the command.
219 g Display information about the GNU project.
220 h Display the HELLO file which illustrates various scripts.
221 i Start the Info documentation reader: read on-line manuals.
222 I METHOD Describe a specific input method, or RET for current.
223 k KEYS Display the full documentation for the key sequence.
224 K KEYS Show the on-line manual's section for the command bound to KEYS.
225 l Show last 300 input keystrokes (lossage).
226 L LANG-ENV Describes a specific language environment, or RET for current.
227 m Display documentation of current minor modes and current major mode,
228 including their special commands.
229 n Display news of recent Emacs changes.
230 p TOPIC Find packages matching a given topic keyword.
231 r Display the Emacs manual in Info mode.
232 s Display contents of current syntax table, plus explanations.
233 S SYMBOL Show the section for the given symbol in the on-line manual
234 for the programming language used in this buffer.
235 t Start the Emacs learn-by-doing tutorial.
236 v VARIABLE Display the given variable's documentation and value.
237 w COMMAND Display which keystrokes invoke the given command (where-is).
238 . Display any available local help at point in the echo area.
240 C-a Information about Emacs.
241 C-c Emacs copying permission (GNU General Public License).
242 C-d Instructions for debugging GNU Emacs.
243 C-e External packages and information about Emacs.
245 C-m How to order printed Emacs manuals.
246 C-n News of recent Emacs changes.
247 C-o Emacs ordering and distribution information.
248 C-p Info about known Emacs problems.
250 C-w Information on absence of warranty for GNU Emacs."
255 (defun function-called-at-point ()
256 "Return a function around point or else called by the list containing point.
257 If that doesn't give a function, return nil."
258 (with-syntax-table emacs-lisp-mode-syntax-table
259 (or (condition-case ()
261 (or (not (zerop (skip-syntax-backward "_w")))
262 (eq (char-syntax (following-char)) ?w)
263 (eq (char-syntax (following-char)) ?_)
265 (skip-chars-forward "'")
266 (let ((obj (read (current-buffer))))
267 (and (symbolp obj) (fboundp obj) obj)))
272 (narrow-to-region (max (point-min)
273 (- (point) 1000)) (point-max))
274 ;; Move up to surrounding paren, then after the open.
277 ;; If there is space here, this is probably something
278 ;; other than a real Lisp function call, so ignore it.
279 (if (looking-at "[ \t]")
280 (error "Probably not a Lisp function call"))
281 (let ((obj (read (current-buffer))))
282 (and (symbolp obj) (fboundp obj) obj))))
284 (let* ((str (find-tag-default))
285 (sym (if str (intern-soft str))))
286 (if (and sym (fboundp sym))
289 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
290 (setq sym (intern-soft (match-string 1 str)))
291 (and (fboundp sym) sym))))))))
294 ;;; `User' help functions
296 (defun view-help-file (file &optional dir)
297 (view-file (expand-file-name file (or dir data-directory)))
298 (goto-address-mode 1)
299 (goto-char (point-min)))
301 (defun describe-distribution ()
302 "Display info on how to obtain the latest version of GNU Emacs."
304 (view-help-file "DISTRIB"))
306 (defun describe-copying ()
307 "Display info on how you may redistribute copies of GNU Emacs."
309 (view-help-file "COPYING"))
311 (defun describe-gnu-project ()
312 "Display info on the GNU project."
314 (view-help-file "THE-GNU-PROJECT"))
316 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
318 (defun describe-no-warranty ()
319 "Display info on all the kinds of warranty Emacs does NOT have."
322 (let (case-fold-search)
323 (search-forward "Disclaimer of Warranty")
327 (defun describe-prefix-bindings ()
328 "Describe the bindings of the prefix used to reach this command.
329 The prefix described consists of all but the last event
330 of the key sequence that ran this command."
332 (let ((key (this-command-keys)))
335 (substring key 0 (1- (length key)))
336 (let ((prefix (make-vector (1- (length key)) nil))
338 (while (< i (length prefix))
339 (aset prefix i (aref key i))
342 ;; Make C-h after a prefix, when not specifically bound,
343 ;; run describe-prefix-bindings.
344 (setq prefix-help-command 'describe-prefix-bindings)
346 (defun view-emacs-news (&optional version)
347 "Display info on recent changes to Emacs.
348 With argument, display info only for the selected version."
351 (setq version emacs-major-version))
352 (when (consp version)
358 (insert-file-contents
359 (expand-file-name file data-directory))
360 (while (re-search-forward
361 (if (member file '("NEWS.18" "NEWS.1-17"))
362 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
363 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
364 (setq res (cons (match-string-no-properties 1) res)))))
366 (directory-files data-directory nil
367 "^NEWS\\.[0-9][-0-9]*$" nil)))
368 (sort (delete-dups res) (lambda (a b) (string< b a)))))
369 (current (car all-versions)))
370 (setq version (completing-read
371 (format "Read NEWS for the version (default %s): " current)
372 all-versions nil nil nil nil current))
373 (if (integerp (string-to-number version))
374 (setq version (string-to-number version))
375 (unless (or (member version all-versions)
376 (<= (string-to-number version) (string-to-number current)))
377 (error "No news about version %s" version)))))
378 (when (integerp version)
379 (cond ((<= version 12)
380 (setq version (format "1.%d" version)))
382 (setq version (format "%d" version)))
383 ((> version emacs-major-version)
384 (error "No news about Emacs %d (yet)" version))))
385 (let* ((vn (if (stringp version)
386 (string-to-number version)
389 ((>= vn emacs-major-version) "NEWS")
390 ((< vn 18) "NEWS.1-17")
391 (t (format "NEWS.%d" vn))))
393 (view-file (expand-file-name file data-directory))
395 (goto-char (point-min))
396 (when (stringp version)
397 (when (re-search-forward
398 (concat (if (< vn 19)
399 "Changes in Emacs[ \t]*"
400 "^\* [^0-9\n]*") version "$")
406 (while (and (setq res
409 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
410 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
411 (equal (match-string-no-properties 1) version)))
412 (or res (goto-char (point-max)))
416 (defun view-emacs-todo (&optional _arg)
417 "Display the Emacs TODO list."
419 (view-help-file "TODO"))
421 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
424 (defun view-echo-area-messages ()
425 "View the log of recent echo-area messages: the `*Messages*' buffer.
426 The number of messages retained in that buffer
427 is specified by the variable `message-log-max'."
429 (switch-to-buffer (get-buffer-create "*Messages*")))
431 (defun view-order-manuals ()
432 "Display the Emacs ORDERS file."
434 (view-help-file "ORDERS"))
436 (defun view-emacs-FAQ ()
437 "Display the Emacs Frequently Asked Questions (FAQ) file."
439 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
442 (defun view-emacs-problems ()
443 "Display info on known problems with Emacs and possible workarounds."
445 (view-help-file "PROBLEMS"))
447 (defun view-emacs-debugging ()
448 "Display info on how to debug Emacs problems."
450 (view-help-file "DEBUG"))
452 (defun view-external-packages ()
453 "Display external packages and information about Emacs."
455 (view-help-file "MORE.STUFF"))
457 (defun view-lossage ()
458 "Display last 300 input keystrokes.
460 To record all your input on a file, use `open-dribble-file'."
462 (help-setup-xref (list #'view-lossage)
463 (called-interactively-p 'interactive))
464 (with-help-window (help-buffer)
465 (princ (mapconcat (lambda (key)
466 (if (or (integerp key) (symbolp key) (listp key))
467 (single-key-description key)
468 (prin1-to-string key nil)))
471 (with-current-buffer standard-output
472 (goto-char (point-min))
473 (while (progn (move-to-column 50) (not (eobp)))
474 (when (search-forward " " nil t)
477 ;; jidanni wants to see the last keystrokes immediately.
478 (set-marker help-window-point-marker (point)))))
483 (defun describe-bindings (&optional prefix buffer)
484 "Show a list of all defined keys, and their definitions.
485 We put that list in a buffer, and display the buffer.
487 The optional argument PREFIX, if non-nil, should be a key sequence;
488 then we display only bindings that start with that prefix.
489 The optional argument BUFFER specifies which buffer's bindings
490 to display (default, the current buffer). BUFFER can be a buffer
493 (or buffer (setq buffer (current-buffer)))
494 (help-setup-xref (list #'describe-bindings prefix buffer)
495 (called-interactively-p 'interactive))
496 (with-current-buffer buffer
497 (describe-bindings-internal nil prefix)))
499 ;; This function used to be in keymap.c.
500 (defun describe-bindings-internal (&optional menus prefix)
501 "Show a list of all defined keys, and their definitions.
502 We put that list in a buffer, and display the buffer.
504 The optional argument MENUS, if non-nil, says to mention menu bindings.
505 \(Ordinarily these are omitted from the output.)
506 The optional argument PREFIX, if non-nil, should be a key sequence;
507 then we display only bindings that start with that prefix."
508 (let ((buf (current-buffer)))
509 (with-help-window "*Help*"
510 (with-current-buffer standard-output
511 (describe-buffer-bindings buf prefix menus)))))
513 (defun where-is (definition &optional insert)
514 "Print message listing key sequences that invoke the command DEFINITION.
515 Argument is a command definition, usually a symbol with a function definition.
516 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
518 (let ((fn (function-called-at-point))
519 (enable-recursive-minibuffers t)
521 (setq val (completing-read
523 (format "Where is command (default %s): " fn)
524 "Where is command: ")
525 obarray 'commandp t))
526 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
527 (unless definition (error "No command"))
528 (let ((func (indirect-function definition))
530 (standard-output (if insert (current-buffer) t)))
531 ;; In DEFS, find all symbols that are aliases for DEFINITION.
532 (mapatoms (lambda (symbol)
533 (and (fboundp symbol)
534 (not (eq symbol definition))
535 (eq func (condition-case ()
536 (indirect-function symbol)
538 (push symbol defs))))
539 ;; Look at all the symbols--first DEFINITION,
541 (dolist (symbol (cons definition defs))
542 (let* ((remapped (command-remapping symbol))
543 (keys (where-is-internal
544 symbol overriding-local-map nil nil remapped))
545 (keys (mapconcat 'key-description keys ", "))
549 (if (> (length keys) 0)
551 (format "%s (%s) (remapped from %s)"
552 keys remapped symbol)
553 (format "%s (%s)" keys symbol))
554 (format "M-x %s RET" symbol))
555 (if (> (length keys) 0)
557 (format "%s is remapped to %s which is on %s"
558 symbol remapped keys)
559 (format "%s is on %s" symbol keys))
560 ;; If this is the command the user asked about,
561 ;; and it is not on any key, say so.
562 ;; For other symbols, its aliases, say nothing
563 ;; about them unless they are on keys.
564 (if (eq symbol definition)
565 (format "%s is not on any key" symbol)))))
567 (unless (eq symbol definition)
568 (princ ";\n its alias "))
572 (defun help-key-description (key untranslated)
573 (let ((string (key-description key)))
574 (if (or (not untranslated)
575 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
577 (let ((otherstring (key-description untranslated)))
578 (if (equal string otherstring)
580 (format "%s (translated from %s)" string otherstring))))))
582 (defun describe-key-briefly (&optional key insert untranslated)
583 "Print the name of the function KEY invokes. KEY is a string.
584 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
585 If non-nil, UNTRANSLATED is a vector of the untranslated events.
586 It can also be a number in which case the untranslated events from
587 the last key hit are used.
589 If KEY is a menu item or a tool-bar button that is disabled, this command
590 temporarily enables it to allow getting help on disabled items and buttons."
592 (let ((enable-disabled-menus-and-buttons t)
593 (cursor-in-echo-area t)
597 ;; If yank-menu is empty, populate it temporarily, so that
598 ;; "Select and Paste" menu can generate a complete event.
599 (when (null (cdr yank-menu))
600 (setq saved-yank-menu (copy-sequence yank-menu))
601 (menu-bar-update-yank-menu "(any string)" nil))
602 (setq key (read-key-sequence "Describe key (or click or menu item): "))
603 ;; If KEY is a down-event, read and discard the
604 ;; corresponding up-event. Note that there are also
605 ;; down-events on scroll bars and mode lines: the actual
606 ;; event then is in the second element of the vector.
608 (let ((last-idx (1- (length key))))
609 (and (eventp (aref key last-idx))
610 (memq 'down (event-modifiers (aref key last-idx)))))
614 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
616 ;; Put yank-menu back as it was, if we changed it.
617 (when saved-yank-menu
618 (setq yank-menu (copy-sequence saved-yank-menu))
619 (fset 'yank-menu (cons 'keymap yank-menu))))))
620 (if (numberp untranslated)
621 (setq untranslated (this-single-command-raw-keys)))
622 (let* ((event (if (and (symbolp (aref key 0))
624 (consp (aref key 1)))
627 (modifiers (event-modifiers event))
628 (standard-output (if insert (current-buffer) t))
629 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
630 (memq 'drag modifiers)) " at that spot" ""))
631 (defn (key-binding key t))
633 ;; Handle the case where we faked an entry in "Select and Paste" menu.
634 (if (and (eq defn nil)
635 (stringp (aref key (1- (length key))))
636 (eq (key-binding (substring key 0 -1)) 'yank-menu))
637 (setq defn 'menu-bar-select-yank))
638 ;; Don't bother user with strings from (e.g.) the select-paste menu.
639 (if (stringp (aref key (1- (length key))))
640 (aset key (1- (length key)) "(any string)"))
641 (if (and (> (length untranslated) 0)
642 (stringp (aref untranslated (1- (length untranslated)))))
643 (aset untranslated (1- (length untranslated)) "(any string)"))
644 ;; Now describe the key, perhaps as changed.
645 (setq key-desc (help-key-description key untranslated))
646 (if (or (null defn) (integerp defn) (equal defn 'undefined))
647 (princ (format "%s%s is undefined" key-desc mouse-msg))
648 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
650 (defun describe-key (&optional key untranslated up-event)
651 "Display documentation of the function invoked by KEY.
652 KEY can be any kind of a key sequence; it can include keyboard events,
653 mouse events, and/or menu events. When calling from a program,
654 pass KEY as a string or a vector.
656 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
657 It can also be a number, in which case the untranslated events from
658 the last key sequence entered are used.
659 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
661 If KEY is a menu item or a tool-bar button that is disabled, this command
662 temporarily enables it to allow getting help on disabled items and buttons."
664 (let ((enable-disabled-menus-and-buttons t)
665 (cursor-in-echo-area t)
669 ;; If yank-menu is empty, populate it temporarily, so that
670 ;; "Select and Paste" menu can generate a complete event.
671 (when (null (cdr yank-menu))
672 (setq saved-yank-menu (copy-sequence yank-menu))
673 (menu-bar-update-yank-menu "(any string)" nil))
674 (setq key (read-key-sequence "Describe key (or click or menu item): "))
677 (prefix-numeric-value current-prefix-arg)
678 ;; If KEY is a down-event, read and include the
679 ;; corresponding up-event. Note that there are also
680 ;; down-events on scroll bars and mode lines: the actual
681 ;; event then is in the second element of the vector.
683 (let ((last-idx (1- (length key))))
684 (and (eventp (aref key last-idx))
685 (memq 'down (event-modifiers (aref key last-idx)))))
686 (or (and (eventp (aref key 0))
687 (memq 'down (event-modifiers (aref key 0)))
688 ;; However, for the C-down-mouse-2 popup
689 ;; menu, there is no subsequent up-event. In
690 ;; this case, the up-event is the next
691 ;; element in the supplied vector.
693 (and (> (length key) 1)
694 (eventp (aref key 1))
695 (memq 'down (event-modifiers (aref key 1)))))
697 ;; Put yank-menu back as it was, if we changed it.
698 (when saved-yank-menu
699 (setq yank-menu (copy-sequence saved-yank-menu))
700 (fset 'yank-menu (cons 'keymap yank-menu))))))
701 (if (numberp untranslated)
702 (setq untranslated (this-single-command-raw-keys)))
703 (let* ((event (aref key (if (and (symbolp (aref key 0))
705 (consp (aref key 1)))
708 (modifiers (event-modifiers event))
709 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
710 (memq 'drag modifiers)) " at that spot" ""))
711 (defn (key-binding key t))
712 defn-up defn-up-tricky ev-type
713 mouse-1-remapped mouse-1-tricky)
715 ;; Handle the case where we faked an entry in "Select and Paste" menu.
716 (when (and (eq defn nil)
717 (stringp (aref key (1- (length key))))
718 (eq (key-binding (substring key 0 -1)) 'yank-menu))
719 (setq defn 'menu-bar-select-yank))
720 (if (or (null defn) (integerp defn) (equal defn 'undefined))
721 (message "%s%s is undefined"
722 (help-key-description key untranslated) mouse-msg)
723 (help-setup-xref (list #'describe-function defn)
724 (called-interactively-p 'interactive))
725 ;; Don't bother user with strings from (e.g.) the select-paste menu.
726 (when (stringp (aref key (1- (length key))))
727 (aset key (1- (length key)) "(any string)"))
728 (when (and untranslated
729 (stringp (aref untranslated (1- (length untranslated)))))
730 (aset untranslated (1- (length untranslated))
732 ;; Need to do this before erasing *Help* buffer in case event
733 ;; is a mouse click in an existing *Help* buffer.
735 (setq ev-type (event-basic-type up-event))
736 (let ((sequence (vector up-event)))
737 (when (and (eq ev-type 'mouse-1)
738 mouse-1-click-follows-link
739 (not (eq mouse-1-click-follows-link 'double))
740 (setq mouse-1-remapped
741 (mouse-on-link-p (event-start up-event))))
742 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
743 (> mouse-1-click-follows-link 0)))
744 (cond ((stringp mouse-1-remapped)
745 (setq sequence mouse-1-remapped))
746 ((vectorp mouse-1-remapped)
747 (setcar up-event (elt mouse-1-remapped 0)))
748 (t (setcar up-event 'mouse-2))))
749 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
751 (setq sequence (vector up-event))
752 (aset sequence 0 'mouse-1)
753 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
754 (with-help-window (help-buffer)
755 (princ (help-key-description key untranslated))
757 %s runs the command %S, which is "
759 (describe-function-1 defn)
761 (unless (or (null defn-up)
763 (equal defn-up 'undefined))
766 ----------------- up-event %s----------------
768 %s%s%s runs the command %S, which is "
769 (if mouse-1-tricky "(short click) " "")
770 (key-description (vector up-event))
773 " is remapped to <mouse-2>, which" "")
775 (describe-function-1 defn-up))
776 (unless (or (null defn-up-tricky)
777 (integerp defn-up-tricky)
778 (eq defn-up-tricky 'undefined))
781 ----------------- up-event (long click) ----------------
783 Pressing <%S>%s for longer than %d milli-seconds
784 runs the command %S, which is "
786 mouse-1-click-follows-link
788 (describe-function-1 defn-up-tricky)))))))
790 (defun describe-mode (&optional buffer)
791 "Display documentation of current major mode and minor modes.
792 A brief summary of the minor modes comes first, followed by the
793 major mode description. This is followed by detailed
794 descriptions of the minor modes, each on a separate page.
796 For this to work correctly for a minor mode, the mode's indicator
797 variable \(listed in `minor-mode-alist') must also be a function
798 whose documentation describes the minor mode."
800 (unless buffer (setq buffer (current-buffer)))
801 (help-setup-xref (list #'describe-mode buffer)
802 (called-interactively-p 'interactive))
803 ;; For the sake of help-do-xref and help-xref-go-back,
804 ;; don't switch buffers before calling `help-buffer'.
805 (with-help-window (help-buffer)
806 (with-current-buffer buffer
808 ;; Older packages do not register in minor-mode-list but only in
810 (dolist (x minor-mode-alist)
812 (unless (memq x minor-mode-list)
813 (push x minor-mode-list)))
814 ;; Find enabled minor mode we will want to mention.
815 (dolist (mode minor-mode-list)
816 ;; Document a minor mode if it is listed in minor-mode-alist,
817 ;; non-nil, and has a function definition.
818 (let ((fmode (or (get mode :minor-mode-function) mode)))
819 (and (boundp mode) (symbol-value mode)
821 (let ((pretty-minor-mode
822 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
825 (substring (symbol-name fmode)
826 0 (match-beginning 0)))
828 (push (list fmode pretty-minor-mode
829 (format-mode-line (assq mode minor-mode-alist)))
833 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
835 (princ "Enabled minor modes:\n")
836 (make-local-variable 'help-button-cache)
837 (with-current-buffer standard-output
838 (dolist (mode minor-modes)
839 (let ((mode-function (nth 0 mode))
840 (pretty-minor-mode (nth 1 mode))
841 (indicator (nth 2 mode)))
842 (add-text-properties 0 (length pretty-minor-mode)
843 '(face bold) pretty-minor-mode)
845 (goto-char (point-max))
847 (push (point-marker) help-button-cache)
848 ;; Document the minor modes fully.
849 (insert pretty-minor-mode)
850 (princ (format " minor mode (%s):\n"
851 (if (zerop (length indicator))
853 (format "indicator%s"
855 (princ (documentation mode-function)))
856 (insert-button pretty-minor-mode
857 'action (car help-button-cache)
859 'help-echo "mouse-2, RET: show full information")
865 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
866 ;; Document the major mode.
867 (let ((mode mode-name))
868 (with-current-buffer standard-output
869 (let ((start (point)))
870 (insert (format-mode-line mode nil nil buffer))
871 (add-text-properties start (point) '(face bold)))))
873 (let* ((mode major-mode)
874 (file-name (find-lisp-object-file-name mode nil)))
876 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
877 ;; Make a hyperlink to the library.
878 (with-current-buffer standard-output
880 (re-search-backward "`\\([^`']+\\)'" nil t)
881 (help-xref-button 1 'help-function-def mode file-name)))))
883 (princ (documentation major-mode)))))
884 ;; For the sake of IELM and maybe others
888 (defun describe-minor-mode (minor-mode)
889 "Display documentation of a minor mode given as MINOR-MODE.
890 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
891 appeared on the mode-line."
892 (interactive (list (completing-read
895 (describe-minor-mode-completion-table-for-symbol)
896 (describe-minor-mode-completion-table-for-indicator)
898 (if (symbolp minor-mode)
899 (setq minor-mode (symbol-name minor-mode)))
900 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
901 (indicators (describe-minor-mode-completion-table-for-indicator)))
903 ((member minor-mode symbols)
904 (describe-minor-mode-from-symbol (intern minor-mode)))
905 ((member minor-mode indicators)
906 (describe-minor-mode-from-indicator minor-mode))
908 (error "No such minor mode: %s" minor-mode)))))
911 (defun describe-minor-mode-completion-table-for-symbol ()
912 ;; In order to list up all minor modes, minor-mode-list
913 ;; is used here instead of minor-mode-alist.
914 (delq nil (mapcar 'symbol-name minor-mode-list)))
916 (defun describe-minor-mode-from-symbol (symbol)
917 "Display documentation of a minor mode given as a symbol, SYMBOL"
918 (interactive (list (intern (completing-read
919 "Minor mode symbol: "
920 (describe-minor-mode-completion-table-for-symbol)))))
922 (describe-function symbol)
923 (describe-variable symbol)))
926 (defun describe-minor-mode-completion-table-for-indicator ()
929 (let ((i (format-mode-line x)))
930 ;; 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)))))
976 ;;; Automatic resizing of temporary buffers.
977 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
978 "Maximum height of a window displaying a temporary buffer.
979 This is effective only when Temp Buffer Resize mode is enabled.
980 The value is the maximum height (in lines) which
981 `resize-temp-buffer-window' will give to a window displaying a
982 temporary buffer. It can also be a function to be called to
983 choose the height for such a buffer. It gets one argumemt, the
984 buffer, and should return a positive integer. At the time the
985 function is called, the window to be resized is selected."
986 :type '(choice integer function)
990 (define-minor-mode temp-buffer-resize-mode
991 "Toggle mode which makes windows smaller for temporary buffers.
992 With prefix argument ARG, turn the resizing of windows displaying
993 temporary buffers on if ARG is positive or off otherwise.
995 This mode makes a window the right height for its contents, but
996 never more than `temp-buffer-max-height' nor less than
999 This mode is used by `help', `apropos' and `completion' buffers,
1001 :global t :group 'help
1002 (if temp-buffer-resize-mode
1003 ;; `help-make-xrefs' may add a `back' button and thus increase the
1004 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1005 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1006 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
1008 (defun resize-temp-buffer-window ()
1009 "Resize the selected window to fit its contents.
1010 Will not make it higher than `temp-buffer-max-height' nor smaller
1011 than `window-min-height'. Do nothing if the selected window is
1012 not vertically combined or some of its contents are scrolled out
1014 (when (and (pos-visible-in-window-p (point-min))
1015 (window-iso-combined-p))
1016 (fit-window-to-buffer
1018 (if (functionp temp-buffer-max-height)
1019 (funcall temp-buffer-max-height (window-buffer))
1020 temp-buffer-max-height))))
1023 (defcustom help-window-select 'other
1024 "Non-nil means select help window for viewing.
1026 never (nil) Select help window only if there is no other window
1028 other Select help window unless the selected window is the
1029 only other window on the help window's frame.
1030 always (t) Always select the help window.
1032 This option has effect if and only if the help window was created
1033 by `with-help-window'"
1034 :type '(choice (const :tag "never (nil)" nil)
1035 (const :tag "other" other)
1036 (const :tag "always (t)" t))
1040 (defun help-window-display-message (quit-part window &optional scroll)
1041 "Display message telling how to quit and scroll help window.
1042 QUIT-PART is a string telling how to quit the help window WINDOW.
1043 Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1044 SCROLL equal `other' means tell how to scroll the \"other\"
1048 ;; If we don't have QUIT-PART we probably reuse a window
1049 ;; showing the same buffer so we don't show any message.
1050 ((not quit-part) nil)
1051 ((pos-visible-in-window-p
1052 (with-current-buffer (window-buffer window)
1053 (point-max)) window t)
1054 ;; Buffer end is at least partially visible, no need to talk
1058 ", \\[scroll-other-window] to scroll help.")
1059 (scroll ", \\[scroll-up] to scroll help."))))
1061 (substitute-command-keys (concat quit-part scroll-part)))))
1063 (defun help-window-setup ()
1064 "Set up help window for `with-help-window'.
1065 This relies on `display-buffer-window' being correctly set up by
1067 (let* ((help-window (car-safe display-buffer-window))
1068 (help-buffer (when (window-live-p help-window)
1069 (window-buffer help-window)))
1070 (help-value (cdr-safe display-buffer-window)))
1072 ;; Handle `help-window-point-marker'.
1073 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1074 (set-window-point help-window help-window-point-marker)
1075 ;; Reset `help-window-point-marker'.
1076 (set-marker help-window-point-marker nil))
1079 ((or (eq help-window (selected-window))
1080 (and (or (eq help-window-select t)
1081 (and (eq help-window-select 'other)
1082 (eq (window-frame help-window) (selected-frame))
1083 (> (length (window-list nil 'no-mini)) 2)))
1084 (select-window help-window)))
1085 ;; The help window is or gets selected ...
1086 (help-window-display-message
1088 ((eq help-value 'new-window)
1089 ;; ... and is new, ...
1090 "Type \"q\" to delete this window")
1091 ((eq help-value 'new-frame)
1092 ;; ... is on a new frame ...
1093 "Type \"q\" to delete this frame")
1094 ((eq help-value 'reuse-other-window)
1095 ;; ... or displayed some other buffer before.
1096 "Type \"q\" to restore previous buffer"))
1098 ((and (eq (window-frame help-window) (selected-frame))
1099 (= (length (window-list nil 'no-mini)) 2))
1100 ;; There are two windows on the help window's frame and the
1101 ;; other one is the selected one.
1102 (help-window-display-message
1104 ((eq help-value 'new-window)
1105 "Type \\[delete-other-windows] to delete the help window")
1106 ((eq help-value 'reuse-other-window)
1107 "Type \"q\" in other window to quit"))
1108 help-window 'other))
1110 ;; Not much to say here.
1111 (help-window-display-message
1112 "Type \"q\" in help window to quit" help-window))))))
1114 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1115 ;; providing the following additional twists:
1117 ;; (1) Issue more accurate messages telling how to scroll and quit the
1120 ;; (2) An option (customizable via `help-window-select') to select the
1121 ;; help window automatically.
1123 ;; (3) A marker (`help-window-point-marker') to move point in the help
1124 ;; window to an arbitrary buffer position.
1126 ;; Note: It's usually always wrong to use `help-print-return-message' in
1127 ;; the body of `with-help-window'.
1128 (defmacro with-help-window (buffer-name &rest body)
1129 "Display buffer with name BUFFER-NAME in a help window evaluating BODY.
1130 Select help window if the actual value of the user option
1131 `help-window-select' says so. Return last value in BODY.
1133 You can specify where and how to show the buffer by binding the
1134 variable `temp-buffer-show-specifiers' to an appropriate value."
1135 (declare (indent 1) (debug t))
1137 ;; Reset `display-buffer-window': `display-buffer' is
1138 ;; supposed to set this to the window displaying the buffer plus
1139 ;; some additional information.
1140 (setq display-buffer-window nil)
1141 ;; Make `help-window-point-marker' point nowhere. The only place
1142 ;; where this should be set to a buffer position is within BODY.
1143 (set-marker help-window-point-marker nil)
1145 ;; Return value returned by `with-output-to-temp-buffer'.
1146 (with-output-to-temp-buffer ,buffer-name
1148 (when display-buffer-window (help-window-setup)))))
1150 ;; Called from C, on encountering `help-char' when reading a char.
1151 ;; Don't print to *Help*; that would clobber Help history.
1152 (defun help-form-show ()
1153 "Display the output of a non-nil `help-form'."
1154 (let ((msg (eval help-form)))
1156 (with-output-to-temp-buffer " *Char Help*"
1161 ;;; help.el ends here