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, 2010 Free Software Foundation, Inc.
7 ;; Keywords: help, internal
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This code implements GNU Emacs' on-line help system, the one invoked by
28 ;; `M-x help-for-help'.
32 ;; Get the macro make-help-screen when this is compiled,
33 ;; or run interpreted, but not when the compiled code is loaded.
34 (eval-when-compile (require 'help-macro
))
36 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
37 (add-hook 'temp-buffer-setup-hook
'help-mode-setup
)
38 (add-hook 'temp-buffer-show-hook
'help-mode-finish
)
40 ;; The variable `help-window' below is used by `help-mode-finish' to
41 ;; communicate the window displaying help (the "help window") to the
42 ;; macro `with-help-window'. The latter sets `help-window' to t before
43 ;; invoking `with-output-to-temp-buffer'. If and only if `help-window'
44 ;; is eq to t, `help-mode-finish' (called by `temp-buffer-setup-hook')
45 ;; sets `help-window' to the window selected by `display-buffer'.
46 ;; Exiting `with-help-window' and calling `help-print-return-message'
47 ;; reset `help-window' to nil.
48 (defvar help-window nil
49 "Window chosen for displaying help.")
51 ;; `help-window-point-marker' is a marker you can move to a valid
52 ;; position of the buffer shown in the help window in order to override
53 ;; the standard positioning mechanism (`point-min') chosen by
54 ;; `with-output-to-temp-buffer'. `with-help-window' has this point
55 ;; nowhere before exiting. Currently used by `view-lossage' to assert
56 ;; that the last keystrokes are always visible.
57 (defvar help-window-point-marker
(make-marker)
58 "Marker to override default `window-point' of `help-window'.")
61 (let ((map (make-sparse-keymap)))
62 (define-key map
(char-to-string help-char
) 'help-for-help
)
63 (define-key map
[help] 'help-for-help)
64 (define-key map [f1] 'help-for-help)
65 (define-key map "." 'display-local-help)
66 (define-key map "?" 'help-for-help)
68 (define-key map "\C-a" 'about-emacs)
69 (define-key map "\C-c" 'describe-copying)
70 (define-key map "\C-d" 'view-emacs-debugging)
71 (define-key map "\C-e" 'view-external-packages)
72 (define-key map "\C-f" 'view-emacs-FAQ)
73 (define-key map "\C-m" 'view-order-manuals)
74 (define-key map "\C-n" 'view-emacs-news)
75 (define-key map "\C-o" 'describe-distribution)
76 (define-key map "\C-p" 'view-emacs-problems)
77 (define-key map "\C-t" 'view-emacs-todo)
78 (define-key map "\C-w" 'describe-no-warranty)
80 ;; This does not fit the pattern, but it is natural given the C-\ command.
81 (define-key map "\C-\\" 'describe-input-method)
83 (define-key map "C" 'describe-coding-system)
84 (define-key map "F" 'Info-goto-emacs-command-node)
85 (define-key map "I" 'describe-input-method)
86 (define-key map "K" 'Info-goto-emacs-key-command-node)
87 (define-key map "L" 'describe-language-environment)
88 (define-key map "S" 'info-lookup-symbol)
90 (define-key map "a" 'apropos-command)
91 (define-key map "b" 'describe-bindings)
92 (define-key map "c" 'describe-key-briefly)
93 (define-key map "d" 'apropos-documentation)
94 (define-key map "e" 'view-echo-area-messages)
95 (define-key map "f" 'describe-function)
96 (define-key map "g" 'describe-gnu-project)
97 (define-key map "h" 'view-hello-file)
99 (define-key map "i" 'info)
100 (define-key map "4i" 'info-other-window)
102 (define-key map "k" 'describe-key)
103 (define-key map "l" 'view-lossage)
104 (define-key map "m" 'describe-mode)
105 (define-key map "n" 'view-emacs-news)
106 (define-key map "p" 'finder-by-keyword)
107 (define-key map "P" 'describe-package)
108 (define-key map "r" 'info-emacs-manual)
109 (define-key map "s" 'describe-syntax)
110 (define-key map "t" 'help-with-tutorial)
111 (define-key map "w" 'where-is)
112 (define-key map "v" 'describe-variable)
113 (define-key map "q" 'help-quit)
115 "Keymap for characters following the Help key.")
117 (define-key global-map (char-to-string help-char) 'help-command)
118 (define-key global-map [help] 'help-command
)
119 (define-key global-map
[f1] 'help-command)
120 (fset 'help-command help-map)
122 (autoload 'finder-by-keyword "finder"
123 "Find packages matching a given keyword." t)
125 ;; insert-button makes the action nil if it is not store somewhere
126 (defvar help-button-cache nil)
130 "Just exit from the Help command's command loop."
134 (defvar help-return-method nil
135 "What to do to \"exit\" the help buffer.
137 (WINDOW . t) delete the selected window (and possibly its frame,
138 see `quit-window' and `View-quit'), go to WINDOW.
139 (WINDOW . quit-window) do quit-window, then select WINDOW.
140 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
142 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
143 (defun help-print-return-message (&optional function)
144 "Display or return message saying how to restore windows after help command.
145 This function assumes that `standard-output' is the help buffer.
146 It computes a message, and applies the optional argument FUNCTION to it.
147 If FUNCTION is nil, it applies `message', thus displaying the message.
148 In addition, this function sets up `help-return-method', which see, that
149 specifies what to do when the user exits the help buffer."
150 ;; Reset `help-window' here to avoid confusing `help-mode-finish'.
151 (setq help-window nil)
152 (and (not (get-buffer-window standard-output))
156 (special-display-p (buffer-name standard-output)))
157 (setq help-return-method (cons (selected-window) t))
158 ;; If the help output buffer is a special display buffer,
159 ;; don't say anything about how to get rid of it.
160 ;; First of all, the user will do that with the window
161 ;; manager, not with Emacs.
162 ;; Secondly, the buffer has not been displayed yet,
163 ;; so we don't know whether its frame will be selected.
165 (display-buffer-reuse-frames
166 (setq help-return-method (cons (selected-window)
169 ((not (one-window-p t))
170 (setq help-return-method
171 (cons (selected-window) 'quit-window))
172 "Type \\[display-buffer] RET to restore the other window.")
174 (setq help-return-method (cons (selected-window) t))
175 "Type \\[delete-other-windows] to remove help window.")
177 (setq help-return-method
178 (list (selected-window) (window-buffer)
179 (window-start) (window-point)))
180 "Type \\[switch-to-buffer] RET to remove help window."))))
181 (funcall (or function 'message)
184 (substitute-command-keys first-message))
185 (if first-message " ")
186 ;; If the help buffer will go in a separate frame,
187 ;; it's no use mentioning a command to scroll, so don't.
188 (if (or pop-up-windows
189 (special-display-p (buffer-name standard-output)))
191 (if (same-window-p (buffer-name standard-output))
192 ;; Say how to scroll this window.
193 (substitute-command-keys
194 "\\[scroll-up] to scroll the help.")
195 ;; Say how to scroll some other window.
196 (substitute-command-keys
197 "\\[scroll-other-window] to scroll the help."))))))))
199 ;; So keyboard macro definitions are documented correctly
200 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
202 (defalias 'help 'help-for-help-internal)
203 ;; find-function can find this.
204 (defalias 'help-for-help 'help-for-help-internal)
205 ;; It can't find this, but nobody will look.
206 (make-help-screen help-for-help-internal
207 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
208 ;; Don't purecopy this one, because it's not evaluated (it's
209 ;; directly used as a docstring in a function definition, so it'll
210 ;; be moved to the DOC file anyway: no need for purecopying it).
211 "You have typed %THIS-KEY%, the help character. Type a Help option:
212 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
214 a PATTERN Show commands whose name matches the PATTERN (a list of words
215 or a regexp). See also the `apropos' command.
216 b Display all key bindings.
217 c KEYS Display the command name run by the given key sequence.
218 C CODING Describe the given coding system, or RET for current ones.
219 d PATTERN Show a list of functions, variables, and other items whose
220 documentation matches the PATTERN (a list of words or a regexp).
221 e Go to the *Messages* buffer which logs echo-area messages.
222 f FUNCTION Display documentation for the given function.
223 F COMMAND Show the on-line manual's section that describes the command.
224 g Display information about the GNU project.
225 h Display the HELLO file which illustrates various scripts.
226 i Start the Info documentation reader: read on-line manuals.
227 I METHOD Describe a specific input method, or RET for current.
228 k KEYS Display the full documentation for the key sequence.
229 K KEYS Show the on-line manual's section for the command bound to KEYS.
230 l Show last 300 input keystrokes (lossage).
231 L LANG-ENV Describes a specific language environment, or RET for current.
232 m Display documentation of current minor modes and current major mode,
233 including their special commands.
234 n Display news of recent Emacs changes.
235 p TOPIC Find packages matching a given topic keyword.
236 r Display the Emacs manual in Info mode.
237 s Display contents of current syntax table, plus explanations.
238 S SYMBOL Show the section for the given symbol in the on-line manual
239 for the programming language used in this buffer.
240 t Start the Emacs learn-by-doing tutorial.
241 v VARIABLE Display the given variable's documentation and value.
242 w COMMAND Display which keystrokes invoke the given command (where-is).
243 . Display any available local help at point in the echo area.
245 C-a Information about Emacs.
246 C-c Emacs copying permission (GNU General Public License).
247 C-d Instructions for debugging GNU Emacs.
248 C-e External packages and information about Emacs.
250 C-m How to order printed Emacs manuals.
251 C-n News of recent Emacs changes.
252 C-o Emacs ordering and distribution information.
253 C-p Info about known Emacs problems.
255 C-w Information on absence of warranty for GNU Emacs."
260 (defun function-called-at-point ()
261 "Return a function around point or else called by the list containing point.
262 If that doesn't give a function, return nil."
263 (with-syntax-table emacs-lisp-mode-syntax-table
264 (or (condition-case ()
266 (or (not (zerop (skip-syntax-backward "_w")))
267 (eq (char-syntax (following-char)) ?w)
268 (eq (char-syntax (following-char)) ?_)
270 (skip-chars-forward "'")
271 (let ((obj (read (current-buffer))))
272 (and (symbolp obj) (fboundp obj) obj)))
277 (narrow-to-region (max (point-min)
278 (- (point) 1000)) (point-max))
279 ;; Move up to surrounding paren, then after the open.
282 ;; If there is space here, this is probably something
283 ;; other than a real Lisp function call, so ignore it.
284 (if (looking-at "[ \t]")
285 (error "Probably not a Lisp function call"))
286 (let ((obj (read (current-buffer))))
287 (and (symbolp obj) (fboundp obj) obj))))
289 (let* ((str (find-tag-default))
290 (sym (if str (intern-soft str))))
291 (if (and sym (fboundp sym))
294 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
295 (setq sym (intern-soft (match-string 1 str)))
296 (and (fboundp sym) sym))))))))
299 ;;; `User' help functions
301 (defun view-help-file (file &optional dir)
302 (view-file (expand-file-name file (or dir data-directory)))
303 (goto-address-mode 1)
304 (goto-char (point-min)))
306 (defun describe-distribution ()
307 "Display info on how to obtain the latest version of GNU Emacs."
309 (view-help-file "DISTRIB"))
311 (defun describe-copying ()
312 "Display info on how you may redistribute copies of GNU Emacs."
314 (view-help-file "COPYING"))
316 (defun describe-gnu-project ()
317 "Display info on the GNU project."
319 (view-help-file "THE-GNU-PROJECT"))
321 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
323 (defun describe-no-warranty ()
324 "Display info on all the kinds of warranty Emacs does NOT have."
327 (let (case-fold-search)
328 (search-forward "Disclaimer of Warranty")
332 (defun describe-prefix-bindings ()
333 "Describe the bindings of the prefix used to reach this command.
334 The prefix described consists of all but the last event
335 of the key sequence that ran this command."
337 (let ((key (this-command-keys)))
340 (substring key 0 (1- (length key)))
341 (let ((prefix (make-vector (1- (length key)) nil))
343 (while (< i (length prefix))
344 (aset prefix i (aref key i))
347 ;; Make C-h after a prefix, when not specifically bound,
348 ;; run describe-prefix-bindings.
349 (setq prefix-help-command 'describe-prefix-bindings)
351 (defun view-emacs-news (&optional version)
352 "Display info on recent changes to Emacs.
353 With argument, display info only for the selected version."
356 (setq version emacs-major-version))
357 (when (consp version)
363 (insert-file-contents
364 (expand-file-name file data-directory))
365 (while (re-search-forward
366 (if (member file '("NEWS.18" "NEWS.1-17"))
367 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
368 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
369 (setq res (cons (match-string-no-properties 1) res)))))
371 (directory-files data-directory nil
372 "^NEWS\\.[0-9][-0-9]*$" nil)))
373 (sort (delete-dups res) (lambda (a b) (string< b a)))))
374 (current (car all-versions)))
375 (setq version (completing-read
376 (format "Read NEWS for the version (default %s): " current)
377 all-versions nil nil nil nil current))
378 (if (integerp (string-to-number version))
379 (setq version (string-to-number version))
380 (unless (or (member version all-versions)
381 (<= (string-to-number version) (string-to-number current)))
382 (error "No news about version %s" version)))))
383 (when (integerp version)
384 (cond ((<= version 12)
385 (setq version (format "1.%d" version)))
387 (setq version (format "%d" version)))
388 ((> version emacs-major-version)
389 (error "No news about Emacs %d (yet)" version))))
390 (let* ((vn (if (stringp version)
391 (string-to-number version)
394 ((>= vn emacs-major-version) "NEWS")
395 ((< vn 18) "NEWS.1-17")
396 (t (format "NEWS.%d" vn))))
398 (view-file (expand-file-name file data-directory))
400 (goto-char (point-min))
401 (when (stringp version)
402 (when (re-search-forward
403 (concat (if (< vn 19)
404 "Changes in Emacs[ \t]*"
405 "^\* [^0-9\n]*") version "$")
411 (while (and (setq res
414 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
415 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
416 (equal (match-string-no-properties 1) version)))
417 (or res (goto-char (point-max)))
421 (defun view-emacs-todo (&optional arg)
422 "Display the Emacs TODO list."
424 (view-help-file "TODO"))
426 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
429 (defun view-echo-area-messages ()
430 "View the log of recent echo-area messages: the `*Messages*' buffer.
431 The number of messages retained in that buffer
432 is specified by the variable `message-log-max'."
434 (switch-to-buffer (get-buffer-create "*Messages*")))
436 (defun view-order-manuals ()
437 "Display the Emacs ORDERS file."
439 (view-help-file "ORDERS"))
441 (defun view-emacs-FAQ ()
442 "Display the Emacs Frequently Asked Questions (FAQ) file."
444 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
447 (defun view-emacs-problems ()
448 "Display info on known problems with Emacs and possible workarounds."
450 (view-help-file "PROBLEMS"))
452 (defun view-emacs-debugging ()
453 "Display info on how to debug Emacs problems."
455 (view-help-file "DEBUG"))
457 (defun view-external-packages ()
458 "Display external packages and information about Emacs."
460 (view-help-file "MORE.STUFF"))
462 (defun view-lossage ()
463 "Display last 300 input keystrokes.
465 To record all your input on a file, use `open-dribble-file'."
467 (help-setup-xref (list #'view-lossage)
468 (called-interactively-p 'interactive))
469 (with-help-window (help-buffer)
470 (princ (mapconcat (lambda (key)
471 (if (or (integerp key) (symbolp key) (listp key))
472 (single-key-description key)
473 (prin1-to-string key nil)))
476 (with-current-buffer standard-output
477 (goto-char (point-min))
478 (while (progn (move-to-column 50) (not (eobp)))
479 (when (search-forward " " nil t)
482 ;; jidanni wants to see the last keystrokes immediately.
483 (set-marker help-window-point-marker (point)))))
488 (defun describe-bindings (&optional prefix buffer)
489 "Show a list of all defined keys, and their definitions.
490 We put that list in a buffer, and display the buffer.
492 The optional argument PREFIX, if non-nil, should be a key sequence;
493 then we display only bindings that start with that prefix.
494 The optional argument BUFFER specifies which buffer's bindings
495 to display (default, the current buffer). BUFFER can be a buffer
498 (or buffer (setq buffer (current-buffer)))
499 (help-setup-xref (list #'describe-bindings prefix buffer)
500 (called-interactively-p 'interactive))
501 (with-current-buffer buffer
502 (describe-bindings-internal nil prefix)))
504 ;; This function used to be in keymap.c.
505 (defun describe-bindings-internal (&optional menus prefix)
506 "Show a list of all defined keys, and their definitions.
507 We put that list in a buffer, and display the buffer.
509 The optional argument MENUS, if non-nil, says to mention menu bindings.
510 \(Ordinarily these are omitted from the output.)
511 The optional argument PREFIX, if non-nil, should be a key sequence;
512 then we display only bindings that start with that prefix."
513 (let ((buf (current-buffer)))
514 (with-help-window "*Help*"
515 (with-current-buffer standard-output
516 (describe-buffer-bindings buf prefix menus)))))
518 (defun where-is (definition &optional insert)
519 "Print message listing key sequences that invoke the command DEFINITION.
520 Argument is a command definition, usually a symbol with a function definition.
521 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
523 (let ((fn (function-called-at-point))
524 (enable-recursive-minibuffers t)
526 (setq val (completing-read
528 (format "Where is command (default %s): " fn)
529 "Where is command: ")
530 obarray 'commandp t))
531 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
532 (unless definition (error "No command"))
533 (let ((func (indirect-function definition))
535 (standard-output (if insert (current-buffer) t)))
536 ;; In DEFS, find all symbols that are aliases for DEFINITION.
537 (mapatoms (lambda (symbol)
538 (and (fboundp symbol)
539 (not (eq symbol definition))
540 (eq func (condition-case ()
541 (indirect-function symbol)
543 (push symbol defs))))
544 ;; Look at all the symbols--first DEFINITION,
546 (dolist (symbol (cons definition defs))
547 (let* ((remapped (command-remapping symbol))
548 (keys (where-is-internal
549 symbol overriding-local-map nil nil remapped))
550 (keys (mapconcat 'key-description keys ", "))
554 (if (> (length keys) 0)
556 (format "%s (%s) (remapped from %s)"
557 keys remapped symbol)
558 (format "%s (%s)" keys symbol))
559 (format "M-x %s RET" symbol))
560 (if (> (length keys) 0)
562 (format "%s is remapped to %s which is on %s"
563 symbol remapped keys)
564 (format "%s is on %s" symbol keys))
565 ;; If this is the command the user asked about,
566 ;; and it is not on any key, say so.
567 ;; For other symbols, its aliases, say nothing
568 ;; about them unless they are on keys.
569 (if (eq symbol definition)
570 (format "%s is not on any key" symbol)))))
572 (unless (eq symbol definition)
573 (princ ";\n its alias "))
577 (defun help-key-description (key untranslated)
578 (let ((string (key-description key)))
579 (if (or (not untranslated)
580 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
582 (let ((otherstring (key-description untranslated)))
583 (if (equal string otherstring)
585 (format "%s (translated from %s)" string otherstring))))))
587 (defun describe-key-briefly (&optional key insert untranslated)
588 "Print the name of the function KEY invokes. KEY is a string.
589 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
590 If non-nil, UNTRANSLATED is a vector of the untranslated events.
591 It can also be a number in which case the untranslated events from
592 the last key hit are used.
594 If KEY is a menu item or a tool-bar button that is disabled, this command
595 temporarily enables it to allow getting help on disabled items and buttons."
597 (let ((enable-disabled-menus-and-buttons t)
598 (cursor-in-echo-area t)
602 ;; If yank-menu is empty, populate it temporarily, so that
603 ;; "Select and Paste" menu can generate a complete event.
604 (when (null (cdr yank-menu))
605 (setq saved-yank-menu (copy-sequence yank-menu))
606 (menu-bar-update-yank-menu "(any string)" nil))
607 (setq key (read-key-sequence "Describe key (or click or menu item): "))
608 ;; If KEY is a down-event, read and discard the
609 ;; corresponding up-event. Note that there are also
610 ;; down-events on scroll bars and mode lines: the actual
611 ;; event then is in the second element of the vector.
613 (let ((last-idx (1- (length key))))
614 (and (eventp (aref key last-idx))
615 (memq 'down (event-modifiers (aref key last-idx)))))
619 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
621 ;; Put yank-menu back as it was, if we changed it.
622 (when saved-yank-menu
623 (setq yank-menu (copy-sequence saved-yank-menu))
624 (fset 'yank-menu (cons 'keymap yank-menu))))))
625 (if (numberp untranslated)
626 (setq untranslated (this-single-command-raw-keys)))
627 (let* ((event (if (and (symbolp (aref key 0))
629 (consp (aref key 1)))
632 (modifiers (event-modifiers event))
633 (standard-output (if insert (current-buffer) t))
634 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
635 (memq 'drag modifiers)) " at that spot" ""))
636 (defn (key-binding key t))
638 ;; Handle the case where we faked an entry in "Select and Paste" menu.
639 (if (and (eq defn nil)
640 (stringp (aref key (1- (length key))))
641 (eq (key-binding (substring key 0 -1)) 'yank-menu))
642 (setq defn 'menu-bar-select-yank))
643 ;; Don't bother user with strings from (e.g.) the select-paste menu.
644 (if (stringp (aref key (1- (length key))))
645 (aset key (1- (length key)) "(any string)"))
646 (if (and (> (length untranslated) 0)
647 (stringp (aref untranslated (1- (length untranslated)))))
648 (aset untranslated (1- (length untranslated)) "(any string)"))
649 ;; Now describe the key, perhaps as changed.
650 (setq key-desc (help-key-description key untranslated))
651 (if (or (null defn) (integerp defn) (equal defn 'undefined))
652 (princ (format "%s%s is undefined" key-desc mouse-msg))
653 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
655 (defun describe-key (&optional key untranslated up-event)
656 "Display documentation of the function invoked by KEY.
657 KEY can be any kind of a key sequence; it can include keyboard events,
658 mouse events, and/or menu events. When calling from a program,
659 pass KEY as a string or a vector.
661 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
662 It can also be a number, in which case the untranslated events from
663 the last key sequence entered are used.
664 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
666 If KEY is a menu item or a tool-bar button that is disabled, this command
667 temporarily enables it to allow getting help on disabled items and buttons."
669 (let ((enable-disabled-menus-and-buttons t)
670 (cursor-in-echo-area t)
674 ;; If yank-menu is empty, populate it temporarily, so that
675 ;; "Select and Paste" menu can generate a complete event.
676 (when (null (cdr yank-menu))
677 (setq saved-yank-menu (copy-sequence yank-menu))
678 (menu-bar-update-yank-menu "(any string)" nil))
679 (setq key (read-key-sequence "Describe key (or click or menu item): "))
682 (prefix-numeric-value current-prefix-arg)
683 ;; If KEY is a down-event, read and include the
684 ;; corresponding up-event. Note that there are also
685 ;; down-events on scroll bars and mode lines: the actual
686 ;; event then is in the second element of the vector.
688 (let ((last-idx (1- (length key))))
689 (and (eventp (aref key last-idx))
690 (memq 'down (event-modifiers (aref key last-idx)))))
691 (or (and (eventp (aref key 0))
692 (memq 'down (event-modifiers (aref key 0)))
693 ;; However, for the C-down-mouse-2 popup
694 ;; menu, there is no subsequent up-event. In
695 ;; this case, the up-event is the next
696 ;; element in the supplied vector.
698 (and (> (length key) 1)
699 (eventp (aref key 1))
700 (memq 'down (event-modifiers (aref key 1)))))
702 ;; Put yank-menu back as it was, if we changed it.
703 (when saved-yank-menu
704 (setq yank-menu (copy-sequence saved-yank-menu))
705 (fset 'yank-menu (cons 'keymap yank-menu))))))
706 (if (numberp untranslated)
707 (setq untranslated (this-single-command-raw-keys)))
708 (let* ((event (aref key (if (and (symbolp (aref key 0))
710 (consp (aref key 1)))
713 (modifiers (event-modifiers event))
714 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
715 (memq 'drag modifiers)) " at that spot" ""))
716 (defn (key-binding key t))
717 defn-up defn-up-tricky ev-type
718 mouse-1-remapped mouse-1-tricky)
720 ;; Handle the case where we faked an entry in "Select and Paste" menu.
721 (when (and (eq defn nil)
722 (stringp (aref key (1- (length key))))
723 (eq (key-binding (substring key 0 -1)) 'yank-menu))
724 (setq defn 'menu-bar-select-yank))
725 (if (or (null defn) (integerp defn) (equal defn 'undefined))
726 (message "%s%s is undefined"
727 (help-key-description key untranslated) mouse-msg)
728 (help-setup-xref (list #'describe-function defn)
729 (called-interactively-p 'interactive))
730 ;; Don't bother user with strings from (e.g.) the select-paste menu.
731 (when (stringp (aref key (1- (length key))))
732 (aset key (1- (length key)) "(any string)"))
733 (when (and untranslated
734 (stringp (aref untranslated (1- (length untranslated)))))
735 (aset untranslated (1- (length untranslated))
737 ;; Need to do this before erasing *Help* buffer in case event
738 ;; is a mouse click in an existing *Help* buffer.
740 (setq ev-type (event-basic-type up-event))
741 (let ((sequence (vector up-event)))
742 (when (and (eq ev-type 'mouse-1)
743 mouse-1-click-follows-link
744 (not (eq mouse-1-click-follows-link 'double))
745 (setq mouse-1-remapped
746 (mouse-on-link-p (event-start up-event))))
747 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
748 (> mouse-1-click-follows-link 0)))
749 (cond ((stringp mouse-1-remapped)
750 (setq sequence mouse-1-remapped))
751 ((vectorp mouse-1-remapped)
752 (setcar up-event (elt mouse-1-remapped 0)))
753 (t (setcar up-event 'mouse-2))))
754 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
756 (setq sequence (vector up-event))
757 (aset sequence 0 'mouse-1)
758 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
759 (with-help-window (help-buffer)
760 (princ (help-key-description key untranslated))
762 %s runs the command %S, which is "
764 (describe-function-1 defn)
766 (unless (or (null defn-up)
768 (equal defn-up 'undefined))
771 ----------------- up-event %s----------------
773 %s%s%s runs the command %S, which is "
774 (if mouse-1-tricky "(short click) " "")
775 (key-description (vector up-event))
778 " is remapped to <mouse-2>, which" "")
780 (describe-function-1 defn-up))
781 (unless (or (null defn-up-tricky)
782 (integerp defn-up-tricky)
783 (eq defn-up-tricky 'undefined))
786 ----------------- up-event (long click) ----------------
788 Pressing <%S>%s for longer than %d milli-seconds
789 runs the command %S, which is "
791 mouse-1-click-follows-link
793 (describe-function-1 defn-up-tricky)))))))
795 (defun describe-mode (&optional buffer)
796 "Display documentation of current major mode and minor modes.
797 A brief summary of the minor modes comes first, followed by the
798 major mode description. This is followed by detailed
799 descriptions of the minor modes, each on a separate page.
801 For this to work correctly for a minor mode, the mode's indicator
802 variable \(listed in `minor-mode-alist') must also be a function
803 whose documentation describes the minor mode."
805 (unless buffer (setq buffer (current-buffer)))
806 (help-setup-xref (list #'describe-mode buffer)
807 (called-interactively-p 'interactive))
808 ;; For the sake of help-do-xref and help-xref-go-back,
809 ;; don't switch buffers before calling `help-buffer'.
810 (with-help-window (help-buffer)
811 (with-current-buffer buffer
813 ;; Older packages do not register in minor-mode-list but only in
815 (dolist (x minor-mode-alist)
817 (unless (memq x minor-mode-list)
818 (push x minor-mode-list)))
819 ;; Find enabled minor mode we will want to mention.
820 (dolist (mode minor-mode-list)
821 ;; Document a minor mode if it is listed in minor-mode-alist,
822 ;; non-nil, and has a function definition.
823 (let ((fmode (or (get mode :minor-mode-function) mode)))
824 (and (boundp mode) (symbol-value mode)
826 (let ((pretty-minor-mode
827 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
830 (substring (symbol-name fmode)
831 0 (match-beginning 0)))
833 (push (list fmode pretty-minor-mode
834 (format-mode-line (assq mode minor-mode-alist)))
838 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
840 (princ "Enabled minor modes:\n")
841 (make-local-variable 'help-button-cache)
842 (with-current-buffer standard-output
843 (dolist (mode minor-modes)
844 (let ((mode-function (nth 0 mode))
845 (pretty-minor-mode (nth 1 mode))
846 (indicator (nth 2 mode)))
847 (add-text-properties 0 (length pretty-minor-mode)
848 '(face bold) pretty-minor-mode)
850 (goto-char (point-max))
852 (push (point-marker) help-button-cache)
853 ;; Document the minor modes fully.
854 (insert pretty-minor-mode)
855 (princ (format " minor mode (%s):\n"
856 (if (zerop (length indicator))
858 (format "indicator%s"
860 (princ (documentation mode-function)))
861 (insert-button pretty-minor-mode
862 'action (car help-button-cache)
864 'help-echo "mouse-2, RET: show full information")
870 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
871 ;; Document the major mode.
872 (let ((mode mode-name))
873 (with-current-buffer standard-output
874 (let ((start (point)))
875 (insert (format-mode-line mode nil nil buffer))
876 (add-text-properties start (point) '(face bold)))))
878 (princ (documentation major-mode)))))
879 ;; For the sake of IELM and maybe others
883 (defun describe-minor-mode (minor-mode)
884 "Display documentation of a minor mode given as MINOR-MODE.
885 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
886 appeared on the mode-line."
887 (interactive (list (completing-read
890 (describe-minor-mode-completion-table-for-symbol)
891 (describe-minor-mode-completion-table-for-indicator)
893 (if (symbolp minor-mode)
894 (setq minor-mode (symbol-name minor-mode)))
895 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
896 (indicators (describe-minor-mode-completion-table-for-indicator)))
898 ((member minor-mode symbols)
899 (describe-minor-mode-from-symbol (intern minor-mode)))
900 ((member minor-mode indicators)
901 (describe-minor-mode-from-indicator minor-mode))
903 (error "No such minor mode: %s" minor-mode)))))
906 (defun describe-minor-mode-completion-table-for-symbol ()
907 ;; In order to list up all minor modes, minor-mode-list
908 ;; is used here instead of minor-mode-alist.
909 (delq nil (mapcar 'symbol-name minor-mode-list)))
910 (defun describe-minor-mode-from-symbol (symbol)
911 "Display documentation of a minor mode given as a symbol, SYMBOL"
912 (interactive (list (intern (completing-read
913 "Minor mode symbol: "
914 (describe-minor-mode-completion-table-for-symbol)))))
916 (describe-function symbol)
917 (describe-variable symbol)))
920 (defun describe-minor-mode-completion-table-for-indicator ()
923 (let ((i (format-mode-line x)))
924 ;; remove first space if existed
933 (defun describe-minor-mode-from-indicator (indicator)
934 "Display documentation of a minor mode specified by INDICATOR.
935 If you call this function interactively, you can give indicator which
936 is currently activated with completion."
939 "Minor mode indicator: "
940 (describe-minor-mode-completion-table-for-indicator))))
941 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
943 (describe-minor-mode-from-symbol minor-mode)
944 (error "Cannot find minor mode for `%s'" indicator))))
946 (defun lookup-minor-mode-from-indicator (indicator)
947 "Return a minor mode symbol from its indicator on the modeline."
948 ;; remove first space if existed
949 (if (and (< 0 (length indicator))
950 (eq (aref indicator 0) ?\s))
951 (setq indicator (substring indicator 1)))
952 (let ((minor-modes minor-mode-alist)
955 (let* ((minor-mode (car (car minor-modes)))
956 (anindicator (format-mode-line
957 (car (cdr (car minor-modes))))))
958 ;; remove first space if existed
959 (if (and (stringp anindicator)
960 (> (length anindicator) 0)
961 (eq (aref anindicator 0) ?\s))
962 (setq anindicator (substring anindicator 1)))
963 (if (equal indicator anindicator)
964 (setq result minor-mode
966 (setq minor-modes (cdr minor-modes)))))
970 ;;; Automatic resizing of temporary buffers.
972 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
973 "Maximum height of a window displaying a temporary buffer.
974 This is effective only when Temp Buffer Resize mode is enabled.
975 The value is the maximum height (in lines) which `resize-temp-buffer-window'
976 will give to a window displaying a temporary buffer.
977 It can also be a function to be called to choose the height for such a buffer.
978 It gets one argumemt, the buffer, and should return a positive integer."
979 :type '(choice integer function)
983 (define-minor-mode temp-buffer-resize-mode
984 "Toggle the mode which makes windows smaller for temporary buffers.
985 With prefix argument ARG, turn the resizing of windows displaying temporary
986 buffers on if ARG is positive or off otherwise.
987 This makes the window the right height for its contents, but never
988 more than `temp-buffer-max-height' nor less than `window-min-height'.
989 This applies to `help', `apropos' and `completion' buffers, and some others."
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 than
1000 `window-min-height'. Do nothing if it is the only window on its frame, if it
1001 is not as wide as the frame or if some of the window's contents are scrolled
1003 (unless (or (one-window-p 'nomini)
1004 (not (pos-visible-in-window-p (point-min)))
1005 (not (window-full-width-p)))
1006 (fit-window-to-buffer
1008 (if (functionp temp-buffer-max-height)
1009 (funcall temp-buffer-max-height (current-buffer))
1010 temp-buffer-max-height))))
1015 (defcustom help-window-select 'other
1016 "Non-nil means select help window for viewing.
1018 never (nil) Select help window only if there is no other window
1020 other Select help window unless the selected window is the
1021 only other window on its frame.
1022 always (t) Always select the help window.
1024 This option has effect if and only if the help window was created
1025 by `with-help-window'"
1026 :type '(choice (const :tag "never (nil)" nil)
1027 (const :tag "other" other)
1028 (const :tag "always (t)" t))
1032 (defun help-window-display-message (quit-part window &optional other)
1033 "Display message telling how to quit and scroll help window.
1034 QUIT-PART is a string telling how to quit the help window WINDOW.
1035 Optional argument OTHER non-nil means return text telling how to
1036 scroll the \"other\" window."
1039 ((pos-visible-in-window-p
1040 (with-current-buffer (window-buffer window)
1041 (point-max)) window)
1042 ;; Buffer end is visible.
1044 (other ", \\[scroll-other-window] to scroll help.")
1045 (t ", \\[scroll-up] to scroll help."))))
1047 (substitute-command-keys (concat quit-part scroll-part)))))
1049 (defun help-window-setup-finish (window &optional reuse keep-frame)
1050 "Finish setting up help window WINDOW.
1051 Select WINDOW according to the value of `help-window-select'.
1052 Display message telling how to scroll and eventually quit WINDOW.
1054 Optional argument REUSE non-nil means WINDOW has been reused by
1055 `display-buffer'. Optional argument KEEP-FRAME non-nil means
1056 that quitting should not delete WINDOW's frame."
1057 (let ((number-of-windows
1058 (length (window-list (window-frame window) 'no-mini window))))
1060 ((eq window (selected-window))
1061 ;; The help window is the selected window, probably the
1062 ;; `pop-up-windows' nil case.
1063 (help-window-display-message
1065 "Type \"q\" to restore this window"
1066 ;; This should not be taken.
1067 "Type \"q\" to quit") window))
1068 ((= number-of-windows 1)
1069 ;; The help window is alone on a frame and not the selected
1070 ;; window, could be the `pop-up-frames' t case.
1071 (help-window-display-message
1073 (keep-frame "Type \"q\" to delete this window")
1074 (reuse "Type \"q\" to restore this window")
1075 (view-remove-frame-by-deleting "Type \"q\" to delete this frame")
1076 (t "Type \"q\" to iconify this frame"))
1078 ((and (= number-of-windows 2)
1079 (eq (window-frame window) (window-frame (selected-window))))
1080 ;; There are two windows on the help window's frame and the other
1081 ;; window is the selected one.
1082 (if (memq help-window-select '(nil other))
1083 ;; Do not select the help window.
1084 (help-window-display-message
1086 ;; Offer `display-buffer' for consistency with
1087 ;; `help-print-return-message'. This is hardly TRT when
1088 ;; the other window and the selected window display the
1089 ;; same buffer but has been handled this way ever since.
1090 "Type \\[display-buffer] RET to restore the other window"
1091 ;; The classic "two windows" configuration.
1092 "Type \\[delete-other-windows] to delete the help window")
1094 ;; Select help window and tell how to quit.
1095 (select-window window)
1096 (help-window-display-message
1098 "Type \"q\" to restore this window"
1099 "Type \"q\" to delete this window") window)))
1101 ;; Issuing a message with 3 or more windows on the same frame
1102 ;; without selecting the help window doesn't make any sense.
1103 (select-window window)
1104 (help-window-display-message
1106 "Type \"q\" to restore this window"
1107 "Type \"q\" to delete this window") window)))))
1109 (defun help-window-setup (list-of-frames list-of-window-tuples)
1110 "Set up help window.
1111 LIST-OF-FRAMES and LIST-OF-WINDOW-TUPLES are the lists of frames
1112 and window quadruples built by `with-help-window'. The help
1113 window itself is specified by the variable `help-window'."
1114 (let* ((help-buffer (window-buffer help-window))
1115 ;; `help-buffer' now denotes the help window's buffer.
1118 (buffer-local-value 'view-return-to-alist help-buffer)))
1119 (help-entry (assq help-window list-of-window-tuples)))
1121 ;; Handle `help-window-point-marker'.
1122 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1123 (set-window-point help-window help-window-point-marker)
1124 ;; Reset `help-window-point-marker'.
1125 (set-marker help-window-point-marker nil))
1129 ;; `view-return-to-alist' has an entry for the help window.
1131 ((eq help-window (selected-window))
1132 ;; The help window is the selected window, probably because the
1133 ;; user followed a backward/forward button or a cross reference.
1134 ;; In this case just purge stale entries from
1135 ;; `view-return-to-alist' but leave the entry alone and don't
1136 ;; display a message.
1137 (view-return-to-alist-update help-buffer))
1138 ((and help-entry (eq (cadr help-entry) help-buffer))
1139 ;; The help window was not selected but displayed the help
1140 ;; buffer. In this case reuse existing exit information but try
1141 ;; to get back to the selected window when quitting. Don't
1142 ;; display a message since the user must have seen one before.
1143 (view-return-to-alist-update
1144 help-buffer (cons help-window
1145 (cons (selected-window) (cddr view-entry)))))
1147 ;; The help window was not selected, did display the help buffer
1148 ;; earlier, but displayed another buffer when help was invoked.
1149 ;; Set up things so that quitting will show that buffer again.
1150 (view-return-to-alist-update
1151 help-buffer (cons help-window
1152 (cons (selected-window) (cdr help-entry))))
1153 (help-window-setup-finish help-window t))
1155 ;; The help window is new but `view-return-to-alist' had an
1156 ;; entry for it. This should never happen.
1157 (view-return-to-alist-update
1158 help-buffer (cons help-window
1159 (cons (selected-window) 'quit-window)))
1160 (help-window-setup-finish help-window t))))
1162 ;; `view-return-to-alist' does not have an entry for help window
1163 ;; but `list-of-window-tuples' does. Hence `display-buffer' must
1164 ;; have reused an existing window.
1165 (if (eq (cadr help-entry) help-buffer)
1166 ;; The help window displayed `help-buffer' before but no
1167 ;; `view-return-to-alist' entry was found probably because the
1168 ;; user manually switched to the help buffer. Set up things
1169 ;; for `quit-window' although `view-exit-action' should be
1170 ;; able to handle this case all by itself.
1172 (view-return-to-alist-update
1173 help-buffer (cons help-window
1174 (cons (selected-window) 'quit-window)))
1175 (help-window-setup-finish help-window t))
1176 ;; The help window displayed another buffer before. Set up
1177 ;; things in a way that quitting can orderly show that buffer
1178 ;; again. The window-start and window-point information from
1179 ;; `list-of-window-tuples' provide the necessary information.
1180 (view-return-to-alist-update
1181 help-buffer (cons help-window
1182 (cons (selected-window) (cdr help-entry))))
1183 (help-window-setup-finish help-window t)))
1184 ((memq (window-frame help-window) list-of-frames)
1185 ;; The help window is a new window on an existing frame. This
1186 ;; case must be handled specially by `help-window-setup-finish'
1187 ;; and `view-mode-exit' to ascertain that quitting does _not_
1188 ;; inadvertently delete the frame.
1189 (view-return-to-alist-update
1190 help-buffer (cons help-window
1191 (cons (selected-window) 'keep-frame)))
1192 (help-window-setup-finish help-window nil t))
1194 ;; The help window is shown on a new frame. In this case quitting
1195 ;; shall handle both, the help window _and_ its frame. We changed
1196 ;; the default of `view-remove-frame-by-deleting' to t in order to
1197 ;; intuitively DTRT here.
1198 (view-return-to-alist-update
1199 help-buffer (cons help-window (cons (selected-window) t)))
1200 (help-window-setup-finish help-window)))))
1202 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1203 ;; providing the following additional twists:
1205 ;; (1) Issue more accurate messages telling how to scroll and quit the
1208 ;; (2) Make `view-mode-exit' DTRT in more cases.
1210 ;; (3) An option (customizable via `help-window-select') to select the
1211 ;; help window automatically.
1213 ;; (4) A marker (`help-window-point-marker') to move point in the help
1214 ;; window to an arbitrary buffer position.
1216 ;; Note: It's usually always wrong to use `help-print-return-message' in
1217 ;; the body of `with-help-window'.
1218 (defmacro with-help-window (buffer-name &rest body)
1219 "Display buffer BUFFER-NAME in a help window evaluating BODY.
1220 Select help window if the actual value of the user option
1221 `help-window-select' says so. Return last value in BODY."
1222 (declare (indent 1) (debug t))
1223 ;; Bind list-of-frames to `frame-list' and list-of-window-tuples to a
1224 ;; list of one <window window-buffer window-start window-point> tuple
1225 ;; for each live window.
1226 `(let ((list-of-frames (frame-list))
1227 (list-of-window-tuples
1231 (push (list window (window-buffer window)
1232 (window-start window) (window-point window))
1236 ;; Make `help-window' t to trigger `help-mode-finish' to set
1237 ;; `help-window' to the actual help window.
1238 (setq help-window t)
1239 ;; Make `help-window-point-marker' point nowhere (the only place
1240 ;; where this should be set to a buffer position is within BODY).
1241 (set-marker help-window-point-marker nil)
1243 ;; Return value returned by `with-output-to-temp-buffer'.
1244 (with-output-to-temp-buffer ,buffer-name
1246 (when (windowp help-window)
1247 ;; Set up help window.
1248 (help-window-setup list-of-frames list-of-window-tuples))
1249 ;; Reset `help-window' to nil to avoid confusing future calls of
1250 ;; `help-mode-finish' with plain `with-output-to-temp-buffer'.
1251 (setq help-window nil))))
1255 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1256 ;;; help.el ends here