1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
7 ;; Keywords: help, internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 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' 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 ;; The variable `help-window' below is used by `help-mode-finish' to
40 ;; communicate the window displaying help (the "help window") to the
41 ;; macro `with-help-window'. The latter sets `help-window' to t before
42 ;; invoking `with-output-to-temp-buffer'. If and only if `help-window'
43 ;; is eq to t, `help-mode-finish' (called by `temp-buffer-setup-hook')
44 ;; sets `help-window' to the window selected by `display-buffer'.
45 ;; Exiting `with-help-window' and calling `help-print-return-message'
46 ;; reset `help-window' to nil.
47 (defvar help-window nil
48 "Window chosen for displaying help.")
50 ;; `help-window-point-marker' is a marker you can move to a valid
51 ;; position of the buffer shown in the help window in order to override
52 ;; the standard positioning mechanism (`point-min') chosen by
53 ;; `with-output-to-temp-buffer'. `with-help-window' has this point
54 ;; nowhere before exiting. Currently used by `view-lossage' to assert
55 ;; that the last keystrokes are always visible.
56 (defvar help-window-point-marker
(make-marker)
57 "Marker to override default `window-point' of `help-window'.")
60 (let ((map (make-sparse-keymap)))
61 (define-key map
(char-to-string help-char
) 'help-for-help
)
62 (define-key map
[help] 'help-for-help)
63 (define-key map [f1] 'help-for-help)
64 (define-key map "." 'display-local-help)
65 (define-key map "?" 'help-for-help)
67 (define-key map "\C-a" 'about-emacs)
68 (define-key map "\C-c" 'describe-copying)
69 (define-key map "\C-d" 'view-emacs-debugging)
70 (define-key map "\C-e" 'view-external-packages)
71 (define-key map "\C-f" 'view-emacs-FAQ)
72 (define-key map "\C-m" 'view-order-manuals)
73 (define-key map "\C-n" 'view-emacs-news)
74 (define-key map "\C-o" 'describe-distribution)
75 (define-key map "\C-p" 'view-emacs-problems)
76 (define-key map "\C-t" 'view-emacs-todo)
77 (define-key map "\C-w" 'describe-no-warranty)
79 ;; This does not fit the pattern, but it is natural given the C-\ command.
80 (define-key map "\C-\\" 'describe-input-method)
82 (define-key map "C" 'describe-coding-system)
83 (define-key map "F" 'Info-goto-emacs-command-node)
84 (define-key map "I" 'describe-input-method)
85 (define-key map "K" 'Info-goto-emacs-key-command-node)
86 (define-key map "L" 'describe-language-environment)
87 (define-key map "S" 'info-lookup-symbol)
89 (define-key map "a" 'apropos-command)
90 (define-key map "b" 'describe-bindings)
91 (define-key map "c" 'describe-key-briefly)
92 (define-key map "d" 'apropos-documentation)
93 (define-key map "e" 'view-echo-area-messages)
94 (define-key map "f" 'describe-function)
95 (define-key map "g" 'describe-gnu-project)
96 (define-key map "h" 'view-hello-file)
98 (define-key map "i" 'info)
99 (define-key map "4i" 'info-other-window)
101 (define-key map "k" 'describe-key)
102 (define-key map "l" 'view-lossage)
103 (define-key map "m" 'describe-mode)
104 (define-key map "n" 'view-emacs-news)
105 (define-key map "p" 'finder-by-keyword)
106 (define-key map "r" 'info-emacs-manual)
107 (define-key map "s" 'describe-syntax)
108 (define-key map "t" 'help-with-tutorial)
109 (define-key map "w" 'where-is)
110 (define-key map "v" 'describe-variable)
111 (define-key map "q" 'help-quit)
113 "Keymap for characters following the Help key.")
115 (define-key global-map (char-to-string help-char) 'help-command)
116 (define-key global-map [help] 'help-command
)
117 (define-key global-map
[f1] 'help-command)
118 (fset 'help-command help-map)
120 (autoload 'finder-by-keyword "finder"
121 "Find packages matching a given keyword." t)
123 ;; insert-button makes the action nil if it is not store somewhere
124 (defvar help-button-cache nil)
128 "Just exit from the Help command's command loop."
132 (defvar help-return-method nil
133 "What to do to \"exit\" the help buffer.
135 (WINDOW . t) delete the selected window (and possibly its frame,
136 see `quit-window' and `View-quit'), go to WINDOW.
137 (WINDOW . quit-window) do quit-window, then select WINDOW.
138 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
140 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
141 (defun help-print-return-message (&optional function)
142 "Display or return message saying how to restore windows after help command.
143 This function assumes that `standard-output' is the help buffer.
144 It computes a message, and applies the optional argument FUNCTION to it.
145 If FUNCTION is nil, it applies `message', thus displaying the message.
146 In addition, this function sets up `help-return-method', which see, that
147 specifies what to do when the user exits the help buffer."
148 ;; Reset `help-window' here to avoid confusing `help-mode-finish'.
149 (setq help-window nil)
150 (and (not (get-buffer-window standard-output))
154 (special-display-p (buffer-name standard-output)))
155 (setq help-return-method (cons (selected-window) t))
156 ;; If the help output buffer is a special display buffer,
157 ;; don't say anything about how to get rid of it.
158 ;; First of all, the user will do that with the window
159 ;; manager, not with Emacs.
160 ;; Secondly, the buffer has not been displayed yet,
161 ;; so we don't know whether its frame will be selected.
163 (display-buffer-reuse-frames
164 (setq help-return-method (cons (selected-window)
167 ((not (one-window-p t))
168 (setq help-return-method
169 (cons (selected-window) 'quit-window))
170 "Type \\[display-buffer] RET to restore the other window.")
172 (setq help-return-method (cons (selected-window) t))
173 "Type \\[delete-other-windows] to remove help window.")
175 (setq help-return-method
176 (list (selected-window) (window-buffer)
177 (window-start) (window-point)))
178 "Type \\[switch-to-buffer] RET to remove help window."))))
179 (funcall (or function 'message)
182 (substitute-command-keys first-message))
183 (if first-message " ")
184 ;; If the help buffer will go in a separate frame,
185 ;; it's no use mentioning a command to scroll, so don't.
186 (if (or pop-up-windows
187 (special-display-p (buffer-name standard-output)))
189 (if (same-window-p (buffer-name standard-output))
190 ;; Say how to scroll this window.
191 (substitute-command-keys
192 "\\[scroll-up] to scroll the help.")
193 ;; Say how to scroll some other window.
194 (substitute-command-keys
195 "\\[scroll-other-window] to scroll the help."))))))))
197 ;; So keyboard macro definitions are documented correctly
198 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
200 (defalias 'help 'help-for-help-internal)
201 ;; find-function can find this.
202 (defalias 'help-for-help 'help-for-help-internal)
203 ;; It can't find this, but nobody will look.
204 (make-help-screen help-for-help-internal
205 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
206 ;; Don't purecopy this one, because it's not evaluated (it's
207 ;; directly used as a docstring in a function definition, so it'll
208 ;; be moved to the DOC file anyway: no need for purecopying it).
209 "You have typed %THIS-KEY%, the help character. Type a Help option:
210 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
212 a PATTERN Show commands whose name matches the PATTERN (a list of words
213 or a regexp). See also the `apropos' command.
214 b Display all key bindings.
215 c KEYS Display the command name run by the given key sequence.
216 C CODING Describe the given coding system, or RET for current ones.
217 d PATTERN Show a list of functions, variables, and other items whose
218 documentation matches the PATTERN (a list of words or a regexp).
219 e Go to the *Messages* buffer which logs echo-area messages.
220 f FUNCTION Display documentation for the given function.
221 F COMMAND Show the on-line manual's section that describes the command.
222 g Display information about the GNU project.
223 h Display the HELLO file which illustrates various scripts.
224 i Start the Info documentation reader: read on-line manuals.
225 I METHOD Describe a specific input method, or RET for current.
226 k KEYS Display the full documentation for the key sequence.
227 K KEYS Show the on-line manual's section for the command bound to KEYS.
228 l Show last 300 input keystrokes (lossage).
229 L LANG-ENV Describes a specific language environment, or RET for current.
230 m Display documentation of current minor modes and current major mode,
231 including their special commands.
232 n Display news of recent Emacs changes.
233 p TOPIC Find packages matching a given topic keyword.
234 r Display the Emacs manual in Info mode.
235 s Display contents of current syntax table, plus explanations.
236 S SYMBOL Show the section for the given symbol in the on-line manual
237 for the programming language used in this buffer.
238 t Start the Emacs learn-by-doing tutorial.
239 v VARIABLE Display the given variable's documentation and value.
240 w COMMAND Display which keystrokes invoke the given command (where-is).
241 . Display any available local help at point in the echo area.
243 C-a Information about Emacs.
244 C-c Emacs copying permission (GNU General Public License).
245 C-d Instructions for debugging GNU Emacs.
246 C-e External packages and information about Emacs.
248 C-m How to order printed Emacs manuals.
249 C-n News of recent Emacs changes.
250 C-o Emacs ordering and distribution information.
251 C-p Info about known Emacs problems.
253 C-w Information on absence of warranty for GNU Emacs."
258 (defun function-called-at-point ()
259 "Return a function around point or else called by the list containing point.
260 If that doesn't give a function, return nil."
261 (with-syntax-table emacs-lisp-mode-syntax-table
262 (or (condition-case ()
264 (or (not (zerop (skip-syntax-backward "_w")))
265 (eq (char-syntax (following-char)) ?w)
266 (eq (char-syntax (following-char)) ?_)
268 (skip-chars-forward "'")
269 (let ((obj (read (current-buffer))))
270 (and (symbolp obj) (fboundp obj) obj)))
275 (narrow-to-region (max (point-min)
276 (- (point) 1000)) (point-max))
277 ;; Move up to surrounding paren, then after the open.
280 ;; If there is space here, this is probably something
281 ;; other than a real Lisp function call, so ignore it.
282 (if (looking-at "[ \t]")
283 (error "Probably not a Lisp function call"))
284 (let ((obj (read (current-buffer))))
285 (and (symbolp obj) (fboundp obj) obj))))
287 (let* ((str (find-tag-default))
288 (sym (if str (intern-soft str))))
289 (if (and sym (fboundp sym))
292 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
293 (setq sym (intern-soft (match-string 1 str)))
294 (and (fboundp sym) sym))))))))
297 ;;; `User' help functions
299 (defun view-help-file (file &optional dir)
300 (view-file (expand-file-name file (or dir data-directory)))
301 (goto-address-mode 1)
302 (goto-char (point-min)))
304 (defun describe-distribution ()
305 "Display info on how to obtain the latest version of GNU Emacs."
307 (view-help-file "DISTRIB"))
309 (defun describe-copying ()
310 "Display info on how you may redistribute copies of GNU Emacs."
312 (view-help-file "COPYING"))
314 (defun describe-gnu-project ()
315 "Display info on the GNU project."
317 (view-help-file "THE-GNU-PROJECT"))
319 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
321 (defun describe-no-warranty ()
322 "Display info on all the kinds of warranty Emacs does NOT have."
325 (let (case-fold-search)
326 (search-forward "Disclaimer of Warranty")
330 (defun describe-prefix-bindings ()
331 "Describe the bindings of the prefix used to reach this command.
332 The prefix described consists of all but the last event
333 of the key sequence that ran this command."
335 (let ((key (this-command-keys)))
338 (substring key 0 (1- (length key)))
339 (let ((prefix (make-vector (1- (length key)) nil))
341 (while (< i (length prefix))
342 (aset prefix i (aref key i))
345 ;; Make C-h after a prefix, when not specifically bound,
346 ;; run describe-prefix-bindings.
347 (setq prefix-help-command 'describe-prefix-bindings)
349 (defun view-emacs-news (&optional version)
350 "Display info on recent changes to Emacs.
351 With argument, display info only for the selected version."
354 (setq version emacs-major-version))
355 (when (consp version)
361 (insert-file-contents
362 (expand-file-name file data-directory))
363 (while (re-search-forward
364 (if (member file '("NEWS.18" "NEWS.1-17"))
365 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
366 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
367 (setq res (cons (match-string-no-properties 1) res)))))
369 (directory-files data-directory nil
370 "^NEWS\\.[0-9][-0-9]*$" nil)))
371 (sort (delete-dups res) (lambda (a b) (string< b a)))))
372 (current (car all-versions)))
373 (setq version (completing-read
374 (format "Read NEWS for the version (default %s): " current)
375 all-versions nil nil nil nil current))
376 (if (integerp (string-to-number version))
377 (setq version (string-to-number version))
378 (unless (or (member version all-versions)
379 (<= (string-to-number version) (string-to-number current)))
380 (error "No news about version %s" version)))))
381 (when (integerp version)
382 (cond ((<= version 12)
383 (setq version (format "1.%d" version)))
385 (setq version (format "%d" version)))
386 ((> version emacs-major-version)
387 (error "No news about Emacs %d (yet)" version))))
388 (let* ((vn (if (stringp version)
389 (string-to-number version)
392 ((>= vn emacs-major-version) "NEWS")
393 ((< vn 18) "NEWS.1-17")
394 (t (format "NEWS.%d" vn))))
396 (view-file (expand-file-name file data-directory))
398 (goto-char (point-min))
399 (when (stringp version)
400 (when (re-search-forward
401 (concat (if (< vn 19)
402 "Changes in Emacs[ \t]*"
403 "^\* [^0-9\n]*") version "$")
409 (while (and (setq res
412 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
413 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
414 (equal (match-string-no-properties 1) version)))
415 (or res (goto-char (point-max)))
419 (defun view-emacs-todo (&optional arg)
420 "Display the Emacs TODO list."
422 (view-help-file "TODO"))
424 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
427 (defun view-echo-area-messages ()
428 "View the log of recent echo-area messages: the `*Messages*' buffer.
429 The number of messages retained in that buffer
430 is specified by the variable `message-log-max'."
432 (switch-to-buffer (get-buffer-create "*Messages*")))
434 (defun view-order-manuals ()
435 "Display the Emacs ORDERS file."
437 (view-help-file "ORDERS"))
439 (defun view-emacs-FAQ ()
440 "Display the Emacs Frequently Asked Questions (FAQ) file."
442 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
445 (defun view-emacs-problems ()
446 "Display info on known problems with Emacs and possible workarounds."
448 (view-help-file "PROBLEMS"))
450 (defun view-emacs-debugging ()
451 "Display info on how to debug Emacs problems."
453 (view-help-file "DEBUG"))
455 (defun view-external-packages ()
456 "Display external packages and information about Emacs."
458 (view-help-file "MORE.STUFF"))
460 (defun view-lossage ()
461 "Display last 300 input keystrokes.
463 To record all your input on a file, use `open-dribble-file'."
465 (help-setup-xref (list #'view-lossage)
466 (called-interactively-p 'interactive))
467 (with-help-window (help-buffer)
468 (princ (mapconcat (lambda (key)
469 (if (or (integerp key) (symbolp key) (listp key))
470 (single-key-description key)
471 (prin1-to-string key nil)))
474 (with-current-buffer standard-output
475 (goto-char (point-min))
476 (while (progn (move-to-column 50) (not (eobp)))
477 (when (search-forward " " nil t)
480 ;; jidanni wants to see the last keystrokes immediately.
481 (set-marker help-window-point-marker (point)))))
486 (defun describe-bindings (&optional prefix buffer)
487 "Show a list of all defined keys, and their definitions.
488 We put that list in a buffer, and display the buffer.
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 The optional argument BUFFER specifies which buffer's bindings
493 to display (default, the current buffer). BUFFER can be a buffer
496 (or buffer (setq buffer (current-buffer)))
497 (help-setup-xref (list #'describe-bindings prefix buffer)
498 (called-interactively-p 'interactive))
499 (with-current-buffer buffer
500 (describe-bindings-internal nil prefix)))
502 ;; This function used to be in keymap.c.
503 (defun describe-bindings-internal (&optional menus prefix)
504 "Show a list of all defined keys, and their definitions.
505 We put that list in a buffer, and display the buffer.
507 The optional argument MENUS, if non-nil, says to mention menu bindings.
508 \(Ordinarily these are omitted from the output.)
509 The optional argument PREFIX, if non-nil, should be a key sequence;
510 then we display only bindings that start with that prefix."
511 (let ((buf (current-buffer)))
512 (with-help-window "*Help*"
513 (with-current-buffer standard-output
514 (describe-buffer-bindings buf prefix menus)))))
516 (defun where-is (definition &optional insert)
517 "Print message listing key sequences that invoke the command DEFINITION.
518 Argument is a command definition, usually a symbol with a function definition.
519 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
521 (let ((fn (function-called-at-point))
522 (enable-recursive-minibuffers t)
524 (setq val (completing-read
526 (format "Where is command (default %s): " fn)
527 "Where is command: ")
528 obarray 'commandp t))
529 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
530 (unless definition (error "No command"))
531 (let ((func (indirect-function definition))
533 (standard-output (if insert (current-buffer) t)))
534 ;; In DEFS, find all symbols that are aliases for DEFINITION.
535 (mapatoms (lambda (symbol)
536 (and (fboundp symbol)
537 (not (eq symbol definition))
538 (eq func (condition-case ()
539 (indirect-function symbol)
541 (push symbol defs))))
542 ;; Look at all the symbols--first DEFINITION,
544 (dolist (symbol (cons definition defs))
545 (let* ((remapped (command-remapping symbol))
546 (keys (where-is-internal
547 symbol overriding-local-map nil nil remapped))
548 (keys (mapconcat 'key-description keys ", "))
552 (if (> (length keys) 0)
554 (format "%s (%s) (remapped from %s)"
555 keys remapped symbol)
556 (format "%s (%s)" keys symbol))
557 (format "M-x %s RET" symbol))
558 (if (> (length keys) 0)
560 (format "%s is remapped to %s which is on %s"
561 symbol remapped keys)
562 (format "%s is on %s" symbol keys))
563 ;; If this is the command the user asked about,
564 ;; and it is not on any key, say so.
565 ;; For other symbols, its aliases, say nothing
566 ;; about them unless they are on keys.
567 (if (eq symbol definition)
568 (format "%s is not on any key" symbol)))))
570 (unless (eq symbol definition)
571 (princ ";\n its alias "))
575 (defun help-key-description (key untranslated)
576 (let ((string (key-description key)))
577 (if (or (not untranslated)
578 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
580 (let ((otherstring (key-description untranslated)))
581 (if (equal string otherstring)
583 (format "%s (translated from %s)" string otherstring))))))
585 (defun describe-key-briefly (&optional key insert untranslated)
586 "Print the name of the function KEY invokes. KEY is a string.
587 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
588 If non-nil, UNTRANSLATED is a vector of the untranslated events.
589 It can also be a number in which case the untranslated events from
590 the last key hit are used.
592 If KEY is a menu item or a tool-bar button that is disabled, this command
593 temporarily enables it to allow getting help on disabled items and buttons."
595 (let ((enable-disabled-menus-and-buttons t)
596 (cursor-in-echo-area t)
600 ;; If yank-menu is empty, populate it temporarily, so that
601 ;; "Select and Paste" menu can generate a complete event.
602 (when (null (cdr yank-menu))
603 (setq saved-yank-menu (copy-sequence yank-menu))
604 (menu-bar-update-yank-menu "(any string)" nil))
605 (setq key (read-key-sequence "Describe key (or click or menu item): "))
606 ;; If KEY is a down-event, read and discard the
607 ;; corresponding up-event. Note that there are also
608 ;; down-events on scroll bars and mode lines: the actual
609 ;; event then is in the second element of the vector.
611 (let ((last-idx (1- (length key))))
612 (and (eventp (aref key last-idx))
613 (memq 'down (event-modifiers (aref key last-idx)))))
617 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
619 ;; Put yank-menu back as it was, if we changed it.
620 (when saved-yank-menu
621 (setq yank-menu (copy-sequence saved-yank-menu))
622 (fset 'yank-menu (cons 'keymap yank-menu))))))
623 (if (numberp untranslated)
624 (setq untranslated (this-single-command-raw-keys)))
625 (let* ((event (if (and (symbolp (aref key 0))
627 (consp (aref key 1)))
630 (modifiers (event-modifiers event))
631 (standard-output (if insert (current-buffer) t))
632 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
633 (memq 'drag modifiers)) " at that spot" ""))
634 (defn (key-binding key t))
636 ;; Handle the case where we faked an entry in "Select and Paste" menu.
637 (if (and (eq defn nil)
638 (stringp (aref key (1- (length key))))
639 (eq (key-binding (substring key 0 -1)) 'yank-menu))
640 (setq defn 'menu-bar-select-yank))
641 ;; Don't bother user with strings from (e.g.) the select-paste menu.
642 (if (stringp (aref key (1- (length key))))
643 (aset key (1- (length key)) "(any string)"))
644 (if (and (> (length untranslated) 0)
645 (stringp (aref untranslated (1- (length untranslated)))))
646 (aset untranslated (1- (length untranslated)) "(any string)"))
647 ;; Now describe the key, perhaps as changed.
648 (setq key-desc (help-key-description key untranslated))
649 (if (or (null defn) (integerp defn) (equal defn 'undefined))
650 (princ (format "%s%s is undefined" key-desc mouse-msg))
651 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
653 (defun describe-key (&optional key untranslated up-event)
654 "Display documentation of the function invoked by KEY.
655 KEY can be any kind of a key sequence; it can include keyboard events,
656 mouse events, and/or menu events. When calling from a program,
657 pass KEY as a string or a vector.
659 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
660 It can also be a number, in which case the untranslated events from
661 the last key sequence entered are used.
662 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
664 If KEY is a menu item or a tool-bar button that is disabled, this command
665 temporarily enables it to allow getting help on disabled items and buttons."
667 (let ((enable-disabled-menus-and-buttons t)
668 (cursor-in-echo-area t)
672 ;; If yank-menu is empty, populate it temporarily, so that
673 ;; "Select and Paste" menu can generate a complete event.
674 (when (null (cdr yank-menu))
675 (setq saved-yank-menu (copy-sequence yank-menu))
676 (menu-bar-update-yank-menu "(any string)" nil))
677 (setq key (read-key-sequence "Describe key (or click or menu item): "))
680 (prefix-numeric-value current-prefix-arg)
681 ;; If KEY is a down-event, read and include the
682 ;; corresponding up-event. Note that there are also
683 ;; down-events on scroll bars and mode lines: the actual
684 ;; event then is in the second element of the vector.
686 (let ((last-idx (1- (length key))))
687 (and (eventp (aref key last-idx))
688 (memq 'down (event-modifiers (aref key last-idx)))))
689 (or (and (eventp (aref key 0))
690 (memq 'down (event-modifiers (aref key 0)))
691 ;; However, for the C-down-mouse-2 popup
692 ;; menu, there is no subsequent up-event. In
693 ;; this case, the up-event is the next
694 ;; element in the supplied vector.
696 (and (> (length key) 1)
697 (eventp (aref key 1))
698 (memq 'down (event-modifiers (aref key 1)))))
700 ;; Put yank-menu back as it was, if we changed it.
701 (when saved-yank-menu
702 (setq yank-menu (copy-sequence saved-yank-menu))
703 (fset 'yank-menu (cons 'keymap yank-menu))))))
704 (if (numberp untranslated)
705 (setq untranslated (this-single-command-raw-keys)))
706 (let* ((event (aref key (if (and (symbolp (aref key 0))
708 (consp (aref key 1)))
711 (modifiers (event-modifiers event))
712 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
713 (memq 'drag modifiers)) " at that spot" ""))
714 (defn (key-binding key t))
715 defn-up defn-up-tricky ev-type
716 mouse-1-remapped mouse-1-tricky)
718 ;; Handle the case where we faked an entry in "Select and Paste" menu.
719 (when (and (eq defn nil)
720 (stringp (aref key (1- (length key))))
721 (eq (key-binding (substring key 0 -1)) 'yank-menu))
722 (setq defn 'menu-bar-select-yank))
723 (if (or (null defn) (integerp defn) (equal defn 'undefined))
724 (message "%s%s is undefined"
725 (help-key-description key untranslated) mouse-msg)
726 (help-setup-xref (list #'describe-function defn)
727 (called-interactively-p 'interactive))
728 ;; Don't bother user with strings from (e.g.) the select-paste menu.
729 (when (stringp (aref key (1- (length key))))
730 (aset key (1- (length key)) "(any string)"))
731 (when (and untranslated
732 (stringp (aref untranslated (1- (length untranslated)))))
733 (aset untranslated (1- (length untranslated))
735 ;; Need to do this before erasing *Help* buffer in case event
736 ;; is a mouse click in an existing *Help* buffer.
738 (setq ev-type (event-basic-type up-event))
739 (let ((sequence (vector up-event)))
740 (when (and (eq ev-type 'mouse-1)
741 mouse-1-click-follows-link
742 (not (eq mouse-1-click-follows-link 'double))
743 (setq mouse-1-remapped
744 (mouse-on-link-p (event-start up-event))))
745 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
746 (> mouse-1-click-follows-link 0)))
747 (cond ((stringp mouse-1-remapped)
748 (setq sequence mouse-1-remapped))
749 ((vectorp mouse-1-remapped)
750 (setcar up-event (elt mouse-1-remapped 0)))
751 (t (setcar up-event 'mouse-2))))
752 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
754 (setq sequence (vector up-event))
755 (aset sequence 0 'mouse-1)
756 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
757 (with-help-window (help-buffer)
758 (princ (help-key-description key untranslated))
760 %s runs the command %S, which is "
762 (describe-function-1 defn)
764 (unless (or (null defn-up)
766 (equal defn-up 'undefined))
769 ----------------- up-event %s----------------
771 <%S>%s%s runs the command %S, which is "
772 (if mouse-1-tricky "(short click) " "")
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 (princ (documentation major-mode))))))
878 (defun describe-minor-mode (minor-mode)
879 "Display documentation of a minor mode given as MINOR-MODE.
880 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
881 appeared on the mode-line."
882 (interactive (list (completing-read
885 (describe-minor-mode-completion-table-for-symbol)
886 (describe-minor-mode-completion-table-for-indicator)
888 (if (symbolp minor-mode)
889 (setq minor-mode (symbol-name minor-mode)))
890 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
891 (indicators (describe-minor-mode-completion-table-for-indicator)))
893 ((member minor-mode symbols)
894 (describe-minor-mode-from-symbol (intern minor-mode)))
895 ((member minor-mode indicators)
896 (describe-minor-mode-from-indicator minor-mode))
898 (error "No such minor mode: %s" minor-mode)))))
901 (defun describe-minor-mode-completion-table-for-symbol ()
902 ;; In order to list up all minor modes, minor-mode-list
903 ;; is used here instead of minor-mode-alist.
904 (delq nil (mapcar 'symbol-name minor-mode-list)))
905 (defun describe-minor-mode-from-symbol (symbol)
906 "Display documentation of a minor mode given as a symbol, SYMBOL"
907 (interactive (list (intern (completing-read
908 "Minor mode symbol: "
909 (describe-minor-mode-completion-table-for-symbol)))))
911 (describe-function symbol)
912 (describe-variable symbol)))
915 (defun describe-minor-mode-completion-table-for-indicator ()
918 (let ((i (format-mode-line x)))
919 ;; remove first space if existed
928 (defun describe-minor-mode-from-indicator (indicator)
929 "Display documentation of a minor mode specified by INDICATOR.
930 If you call this function interactively, you can give indicator which
931 is currently activated with completion."
934 "Minor mode indicator: "
935 (describe-minor-mode-completion-table-for-indicator))))
936 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
938 (describe-minor-mode-from-symbol minor-mode)
939 (error "Cannot find minor mode for `%s'" indicator))))
941 (defun lookup-minor-mode-from-indicator (indicator)
942 "Return a minor mode symbol from its indicator on the modeline."
943 ;; remove first space if existed
944 (if (and (< 0 (length indicator))
945 (eq (aref indicator 0) ?\s))
946 (setq indicator (substring indicator 1)))
947 (let ((minor-modes minor-mode-alist)
950 (let* ((minor-mode (car (car minor-modes)))
951 (anindicator (format-mode-line
952 (car (cdr (car minor-modes))))))
953 ;; remove first space if existed
954 (if (and (stringp anindicator)
955 (> (length anindicator) 0)
956 (eq (aref anindicator 0) ?\s))
957 (setq anindicator (substring anindicator 1)))
958 (if (equal indicator anindicator)
959 (setq result minor-mode
961 (setq minor-modes (cdr minor-modes)))))
965 ;;; Automatic resizing of temporary buffers.
967 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
968 "Maximum height of a window displaying a temporary buffer.
969 This is effective only when Temp Buffer Resize mode is enabled.
970 The value is the maximum height (in lines) which `resize-temp-buffer-window'
971 will give to a window displaying a temporary buffer.
972 It can also be a function to be called to choose the height for such a buffer.
973 It gets one argumemt, the buffer, and should return a positive integer."
974 :type '(choice integer function)
978 (define-minor-mode temp-buffer-resize-mode
979 "Toggle the mode which makes windows smaller for temporary buffers.
980 With prefix argument ARG, turn the resizing of windows displaying temporary
981 buffers on if ARG is positive or off otherwise.
982 This makes the window the right height for its contents, but never
983 more than `temp-buffer-max-height' nor less than `window-min-height'.
984 This applies to `help', `apropos' and `completion' buffers, and some others."
985 :global t :group 'help
986 (if temp-buffer-resize-mode
987 ;; `help-make-xrefs' may add a `back' button and thus increase the
988 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
989 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
990 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
992 (defun resize-temp-buffer-window ()
993 "Resize the selected window to fit its contents.
994 Will not make it higher than `temp-buffer-max-height' nor smaller than
995 `window-min-height'. Do nothing if it is the only window on its frame, if it
996 is not as wide as the frame or if some of the window's contents are scrolled
998 (unless (or (one-window-p 'nomini)
999 (not (pos-visible-in-window-p (point-min)))
1000 (not (window-full-width-p)))
1001 (fit-window-to-buffer
1003 (if (functionp temp-buffer-max-height)
1004 (funcall temp-buffer-max-height (current-buffer))
1005 temp-buffer-max-height))))
1010 (defcustom help-window-select 'other
1011 "Non-nil means select help window for viewing.
1013 never (nil) Select help window only if there is no other window
1015 other Select help window unless the selected window is the
1016 only other window on its frame.
1017 always (t) Always select the help window.
1019 This option has effect if and only if the help window was created
1020 by `with-help-window'"
1021 :type '(choice (const :tag "never (nil)" nil)
1022 (const :tag "other" other)
1023 (const :tag "always (t)" t))
1027 (defun help-window-display-message (quit-part window &optional other)
1028 "Display message telling how to quit and scroll help window.
1029 QUIT-PART is a string telling how to quit the help window WINDOW.
1030 Optional argument OTHER non-nil means return text telling how to
1031 scroll the \"other\" window."
1034 ((pos-visible-in-window-p
1035 (with-current-buffer (window-buffer window)
1036 (point-max)) window)
1037 ;; Buffer end is visible.
1039 (other ", \\[scroll-other-window] to scroll help.")
1040 (t ", \\[scroll-up] to scroll help."))))
1042 (substitute-command-keys (concat quit-part scroll-part)))))
1044 (defun help-window-setup-finish (window &optional reuse keep-frame)
1045 "Finish setting up help window WINDOW.
1046 Select WINDOW according to the value of `help-window-select'.
1047 Display message telling how to scroll and eventually quit WINDOW.
1049 Optional argument REUSE non-nil means WINDOW has been reused \(by
1050 `display-buffer'\) for displaying help. Optional argument
1051 KEEP-FRAME non-nil means that quitting must no delete the frame
1053 (let ((number-of-windows
1054 (length (window-list (window-frame window) 'no-mini window))))
1056 ((eq window (selected-window))
1057 ;; The help window is the selected window, probably the
1058 ;; `pop-up-windows' nil case.
1059 (help-window-display-message
1061 "Type \"q\" to restore this window"
1062 ;; This should not be taken.
1063 "Type \"q\" to quit") window))
1064 ((= number-of-windows 1)
1065 ;; The help window is alone on a frame and not the selected
1066 ;; window, could be the `pop-up-frames' t case.
1067 (help-window-display-message
1069 (keep-frame "Type \"q\" to delete this window")
1070 (reuse "Type \"q\" to restore this window")
1071 (view-remove-frame-by-deleting "Type \"q\" to delete this frame")
1072 (t "Type \"q\" to iconify this frame"))
1074 ((and (= number-of-windows 2)
1075 (eq (window-frame window) (window-frame (selected-window))))
1076 ;; There are two windows on the help window's frame and the other
1077 ;; window is the selected one.
1078 (if (memq help-window-select '(nil other))
1079 ;; Do not select the help window.
1080 (help-window-display-message
1082 ;; Offer `display-buffer' for consistency with
1083 ;; `help-print-return-message'. This is hardly TRT when
1084 ;; the other window and the selected window display the
1085 ;; same buffer but has been handled this way ever since.
1086 "Type \\[display-buffer] RET to restore the other window"
1087 ;; The classic "two windows" configuration.
1088 "Type \\[delete-other-windows] to delete the help window")
1090 ;; Select help window and tell how to quit.
1091 (select-window window)
1092 (help-window-display-message
1094 "Type \"q\" to restore this window"
1095 "Type \"q\" to delete this window") window)))
1097 ;; Issuing a message with 3 or more windows on the same frame
1098 ;; without selecting the help window doesn't make any sense.
1099 (select-window window)
1100 (help-window-display-message
1102 "Type \"q\" to restore this window"
1103 "Type \"q\" to delete this window") window)))))
1105 (defun help-window-setup (list-of-frames list-of-window-tuples)
1106 "Set up help window.
1107 LIST-OF-FRAMES and LIST-OF-WINDOW-TUPLES are the lists of frames
1108 and window quadruples built by `with-help-window'. The help
1109 window itself is specified by the variable `help-window'."
1110 (let* ((help-buffer (window-buffer help-window))
1111 ;; `help-buffer' now denotes the help window's buffer.
1114 (buffer-local-value 'view-return-to-alist help-buffer)))
1115 (help-entry (assq help-window list-of-window-tuples)))
1117 ;; Handle `help-window-point-marker'.
1118 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1119 (set-window-point help-window help-window-point-marker)
1120 ;; Reset `help-window-point-marker'.
1121 (set-marker help-window-point-marker nil))
1125 ;; `view-return-to-alist' has an entry for the help window.
1127 ((eq help-window (selected-window))
1128 ;; The help window is the selected window, probably because the
1129 ;; user followed a backward/forward button or a cross reference.
1130 ;; In this case just purge stale entries from
1131 ;; `view-return-to-alist' but leave the entry alone and don't
1132 ;; display a message.
1133 (view-return-to-alist-update help-buffer))
1134 ((and help-entry (eq (cadr help-entry) help-buffer))
1135 ;; The help window was not selected but displayed the help
1136 ;; buffer. In this case reuse existing exit information but try
1137 ;; to get back to the selected window when quitting. Don't
1138 ;; display a message since the user must have seen one before.
1139 (view-return-to-alist-update
1140 help-buffer (cons help-window
1141 (cons (selected-window) (cddr view-entry)))))
1143 ;; The help window was not selected, did display the help buffer
1144 ;; earlier, but displayed another buffer when help was invoked.
1145 ;; Set up things so that quitting will show that buffer again.
1146 (view-return-to-alist-update
1147 help-buffer (cons help-window
1148 (cons (selected-window) (cdr help-entry))))
1149 (help-window-setup-finish help-window t))
1151 ;; The help window is new but `view-return-to-alist' had an
1152 ;; entry for it. This should never happen.
1153 (view-return-to-alist-update
1154 help-buffer (cons help-window
1155 (cons (selected-window) 'quit-window)))
1156 (help-window-setup-finish help-window t))))
1158 ;; `view-return-to-alist' does not have an entry for help window
1159 ;; but `list-of-window-tuples' does. Hence `display-buffer' must
1160 ;; have reused an existing window.
1161 (if (eq (cadr help-entry) help-buffer)
1162 ;; The help window displayed `help-buffer' before but no
1163 ;; `view-return-to-alist' entry was found probably because the
1164 ;; user manually switched to the help buffer. Set up things
1165 ;; for `quit-window' although `view-exit-action' should be
1166 ;; able to handle this case all by itself.
1168 (view-return-to-alist-update
1169 help-buffer (cons help-window
1170 (cons (selected-window) 'quit-window)))
1171 (help-window-setup-finish help-window t))
1172 ;; The help window displayed another buffer before. Set up
1173 ;; things in a way that quitting can orderly show that buffer
1174 ;; again. The window-start and window-point information from
1175 ;; `list-of-window-tuples' provide the necessary information.
1176 (view-return-to-alist-update
1177 help-buffer (cons help-window
1178 (cons (selected-window) (cdr help-entry))))
1179 (help-window-setup-finish help-window t)))
1180 ((memq (window-frame help-window) list-of-frames)
1181 ;; The help window is a new window on an existing frame. This
1182 ;; case must be handled specially by `help-window-setup-finish'
1183 ;; and `view-mode-exit' to ascertain that quitting does _not_
1184 ;; inadvertently delete the frame.
1185 (view-return-to-alist-update
1186 help-buffer (cons help-window
1187 (cons (selected-window) 'keep-frame)))
1188 (help-window-setup-finish help-window nil t))
1190 ;; The help window is shown on a new frame. In this case quitting
1191 ;; shall handle both, the help window _and_ its frame. We changed
1192 ;; the default of `view-remove-frame-by-deleting' to t in order to
1193 ;; intuitively DTRT here.
1194 (view-return-to-alist-update
1195 help-buffer (cons help-window (cons (selected-window) t)))
1196 (help-window-setup-finish help-window)))))
1198 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1199 ;; providing the following additional twists:
1201 ;; (1) Issue more accurate messages telling how to scroll and quit the
1204 ;; (2) Make `view-mode-exit' DTRT in more cases.
1206 ;; (3) An option (customizable via `help-window-select') to select the
1207 ;; help window automatically.
1209 ;; (4) A marker (`help-window-point-marker') to move point in the help
1210 ;; window to an arbitrary buffer position.
1212 ;; Note: It's usually always wrong to use `help-print-return-message' in
1213 ;; the body of `with-help-window'.
1214 (defmacro with-help-window (buffer-name &rest body)
1215 "Display buffer BUFFER-NAME in a help window evaluating BODY.
1216 Select help window if the actual value of the user option
1217 `help-window-select' says so. Return last value in BODY."
1218 (declare (indent 1) (debug t))
1219 ;; Bind list-of-frames to `frame-list' and list-of-window-tuples to a
1220 ;; list of one <window window-buffer window-start window-point> tuple
1221 ;; for each live window.
1222 `(let ((list-of-frames (frame-list))
1223 (list-of-window-tuples
1227 (push (list window (window-buffer window)
1228 (window-start window) (window-point window))
1232 ;; Make `help-window' t to trigger `help-mode-finish' to set
1233 ;; `help-window' to the actual help window.
1234 (setq help-window t)
1235 ;; Make `help-window-point-marker' point nowhere (the only place
1236 ;; where this should be set to a buffer position is within BODY).
1237 (set-marker help-window-point-marker nil)
1239 ;; Return value returned by `with-output-to-temp-buffer'.
1240 (with-output-to-temp-buffer ,buffer-name
1242 (when (windowp help-window)
1243 ;; Set up help window.
1244 (help-window-setup list-of-frames list-of-window-tuples))
1245 ;; Reset `help-window' to nil to avoid confusing future calls of
1246 ;; `help-mode-finish' with plain `with-output-to-temp-buffer'.
1247 (setq help-window nil))))
1251 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1252 ;;; help.el ends here