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
5 ;; Free Software Foundation, Inc.
8 ;; Keywords: help, internal
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This code implements GNU Emacs' on-line help system, the one invoked by
29 ;; `M-x help-for-help'.
33 ;; Get the macro make-help-screen when this is compiled,
34 ;; or run interpreted, but not when the compiled code is loaded.
35 (eval-when-compile (require 'help-macro
))
37 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
38 (add-hook 'temp-buffer-setup-hook
'help-mode-setup
)
39 (add-hook 'temp-buffer-show-hook
'help-mode-finish
)
41 ;; `help-window-point-marker' is a marker you can move to a valid
42 ;; position of the buffer shown in the help window in order to override
43 ;; the standard positioning mechanism (`point-min') chosen by
44 ;; `with-output-to-temp-buffer'. `with-help-window' has this point
45 ;; nowhere before exiting. Currently used by `view-lossage' to assert
46 ;; that the last keystrokes are always visible.
47 (defvar help-window-point-marker
(make-marker)
48 "Marker to override default `window-point' in help windows.")
51 (let ((map (make-sparse-keymap)))
52 (define-key map
(char-to-string help-char
) 'help-for-help
)
53 (define-key map
[help] 'help-for-help)
54 (define-key map [f1] 'help-for-help)
55 (define-key map "." 'display-local-help)
56 (define-key map "?" 'help-for-help)
58 (define-key map "\C-a" 'about-emacs)
59 (define-key map "\C-c" 'describe-copying)
60 (define-key map "\C-d" 'view-emacs-debugging)
61 (define-key map "\C-e" 'view-external-packages)
62 (define-key map "\C-f" 'view-emacs-FAQ)
63 (define-key map "\C-m" 'view-order-manuals)
64 (define-key map "\C-n" 'view-emacs-news)
65 (define-key map "\C-o" 'describe-distribution)
66 (define-key map "\C-p" 'view-emacs-problems)
67 (define-key map "\C-t" 'view-emacs-todo)
68 (define-key map "\C-w" 'describe-no-warranty)
70 ;; This does not fit the pattern, but it is natural given the C-\ command.
71 (define-key map "\C-\\" 'describe-input-method)
73 (define-key map "C" 'describe-coding-system)
74 (define-key map "F" 'Info-goto-emacs-command-node)
75 (define-key map "I" 'describe-input-method)
76 (define-key map "K" 'Info-goto-emacs-key-command-node)
77 (define-key map "L" 'describe-language-environment)
78 (define-key map "S" 'info-lookup-symbol)
80 (define-key map "a" 'apropos-command)
81 (define-key map "b" 'describe-bindings)
82 (define-key map "c" 'describe-key-briefly)
83 (define-key map "d" 'apropos-documentation)
84 (define-key map "e" 'view-echo-area-messages)
85 (define-key map "f" 'describe-function)
86 (define-key map "g" 'describe-gnu-project)
87 (define-key map "h" 'view-hello-file)
89 (define-key map "i" 'info)
90 (define-key map "4i" 'info-other-window)
92 (define-key map "k" 'describe-key)
93 (define-key map "l" 'view-lossage)
94 (define-key map "m" 'describe-mode)
95 (define-key map "n" 'view-emacs-news)
96 (define-key map "p" 'finder-by-keyword)
97 (define-key map "P" 'describe-package)
98 (define-key map "r" 'info-emacs-manual)
99 (define-key map "s" 'describe-syntax)
100 (define-key map "t" 'help-with-tutorial)
101 (define-key map "w" 'where-is)
102 (define-key map "v" 'describe-variable)
103 (define-key map "q" 'help-quit)
105 "Keymap for characters following the Help key.")
107 (define-key global-map (char-to-string help-char) 'help-command)
108 (define-key global-map [help] 'help-command
)
109 (define-key global-map
[f1] 'help-command)
110 (fset 'help-command help-map)
112 ;; insert-button makes the action nil if it is not store somewhere
113 (defvar help-button-cache nil)
117 "Just exit from the Help command's command loop."
121 (defvar help-return-method nil
122 "What to do to \"exit\" the help buffer.
124 (WINDOW . t) delete the selected window (and possibly its frame,
125 see `quit-window' and `View-quit'), go to WINDOW.
126 (WINDOW . quit-window) do quit-window, then select WINDOW.
127 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
129 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
130 (defun help-print-return-message (&optional function)
131 "Display or return message saying how to restore windows after help command.
132 This function assumes that `standard-output' is the help buffer.
133 It computes a message, and applies the optional argument FUNCTION to it.
134 If FUNCTION is nil, it applies `message', thus displaying the message.
135 In addition, this function sets up `help-return-method', which see, that
136 specifies what to do when the user exits the help buffer."
137 (and (not (get-buffer-window standard-output))
141 (special-display-p (buffer-name standard-output)))
142 (setq help-return-method (cons (selected-window) t))
143 ;; If the help output buffer is a special display buffer,
144 ;; don't say anything about how to get rid of it.
145 ;; First of all, the user will do that with the window
146 ;; manager, not with Emacs.
147 ;; Secondly, the buffer has not been displayed yet,
148 ;; so we don't know whether its frame will be selected.
150 (display-buffer-reuse-frames
151 (setq help-return-method (cons (selected-window)
154 ((not (one-window-p t))
155 (setq help-return-method
156 (cons (selected-window) 'quit-window))
157 "Type \\[display-buffer] RET to restore the other window.")
159 (setq help-return-method (cons (selected-window) t))
160 "Type \\[delete-other-windows] to remove help window.")
162 (setq help-return-method
163 (list (selected-window) (window-buffer)
164 (window-start) (window-point)))
165 "Type \\[switch-to-buffer] RET to remove help window."))))
166 (funcall (or function 'message)
169 (substitute-command-keys first-message))
170 (if first-message " ")
171 ;; If the help buffer will go in a separate frame,
172 ;; it's no use mentioning a command to scroll, so don't.
173 (if (or pop-up-windows
174 (special-display-p (buffer-name standard-output)))
176 (if (same-window-p (buffer-name standard-output))
177 ;; Say how to scroll this window.
178 (substitute-command-keys
179 "\\[scroll-up] to scroll the help.")
180 ;; Say how to scroll some other window.
181 (substitute-command-keys
182 "\\[scroll-other-window] to scroll the help."))))))))
184 ;; So keyboard macro definitions are documented correctly
185 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
187 (defalias 'help 'help-for-help-internal)
188 ;; find-function can find this.
189 (defalias 'help-for-help 'help-for-help-internal)
190 ;; It can't find this, but nobody will look.
191 (make-help-screen help-for-help-internal
192 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
193 ;; Don't purecopy this one, because it's not evaluated (it's
194 ;; directly used as a docstring in a function definition, so it'll
195 ;; be moved to the DOC file anyway: no need for purecopying it).
196 "You have typed %THIS-KEY%, the help character. Type a Help option:
197 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
199 a PATTERN Show commands whose name matches the PATTERN (a list of words
200 or a regexp). See also the `apropos' command.
201 b Display all key bindings.
202 c KEYS Display the command name run by the given key sequence.
203 C CODING Describe the given coding system, or RET for current ones.
204 d PATTERN Show a list of functions, variables, and other items whose
205 documentation matches the PATTERN (a list of words or a regexp).
206 e Go to the *Messages* buffer which logs echo-area messages.
207 f FUNCTION Display documentation for the given function.
208 F COMMAND Show the on-line manual's section that describes the command.
209 g Display information about the GNU project.
210 h Display the HELLO file which illustrates various scripts.
211 i Start the Info documentation reader: read on-line manuals.
212 I METHOD Describe a specific input method, or RET for current.
213 k KEYS Display the full documentation for the key sequence.
214 K KEYS Show the on-line manual's section for the command bound to KEYS.
215 l Show last 300 input keystrokes (lossage).
216 L LANG-ENV Describes a specific language environment, or RET for current.
217 m Display documentation of current minor modes and current major mode,
218 including their special commands.
219 n Display news of recent Emacs changes.
220 p TOPIC Find packages matching a given topic keyword.
221 r Display the Emacs manual in Info mode.
222 s Display contents of current syntax table, plus explanations.
223 S SYMBOL Show the section for the given symbol in the on-line manual
224 for the programming language used in this buffer.
225 t Start the Emacs learn-by-doing tutorial.
226 v VARIABLE Display the given variable's documentation and value.
227 w COMMAND Display which keystrokes invoke the given command (where-is).
228 . Display any available local help at point in the echo area.
230 C-a Information about Emacs.
231 C-c Emacs copying permission (GNU General Public License).
232 C-d Instructions for debugging GNU Emacs.
233 C-e External packages and information about Emacs.
235 C-m How to order printed Emacs manuals.
236 C-n News of recent Emacs changes.
237 C-o Emacs ordering and distribution information.
238 C-p Info about known Emacs problems.
240 C-w Information on absence of warranty for GNU Emacs."
245 (defun function-called-at-point ()
246 "Return a function around point or else called by the list containing point.
247 If that doesn't give a function, return nil."
248 (with-syntax-table emacs-lisp-mode-syntax-table
249 (or (condition-case ()
251 (or (not (zerop (skip-syntax-backward "_w")))
252 (eq (char-syntax (following-char)) ?w)
253 (eq (char-syntax (following-char)) ?_)
255 (skip-chars-forward "'")
256 (let ((obj (read (current-buffer))))
257 (and (symbolp obj) (fboundp obj) obj)))
262 (narrow-to-region (max (point-min)
263 (- (point) 1000)) (point-max))
264 ;; Move up to surrounding paren, then after the open.
267 ;; If there is space here, this is probably something
268 ;; other than a real Lisp function call, so ignore it.
269 (if (looking-at "[ \t]")
270 (error "Probably not a Lisp function call"))
271 (let ((obj (read (current-buffer))))
272 (and (symbolp obj) (fboundp obj) obj))))
274 (let* ((str (find-tag-default))
275 (sym (if str (intern-soft str))))
276 (if (and sym (fboundp sym))
279 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
280 (setq sym (intern-soft (match-string 1 str)))
281 (and (fboundp sym) sym))))))))
284 ;;; `User' help functions
286 (defun view-help-file (file &optional dir)
287 (view-file (expand-file-name file (or dir data-directory)))
288 (goto-address-mode 1)
289 (goto-char (point-min)))
291 (defun describe-distribution ()
292 "Display info on how to obtain the latest version of GNU Emacs."
294 (view-help-file "DISTRIB"))
296 (defun describe-copying ()
297 "Display info on how you may redistribute copies of GNU Emacs."
299 (view-help-file "COPYING"))
301 (defun describe-gnu-project ()
302 "Display info on the GNU project."
304 (view-help-file "THE-GNU-PROJECT"))
306 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
308 (defun describe-no-warranty ()
309 "Display info on all the kinds of warranty Emacs does NOT have."
312 (let (case-fold-search)
313 (search-forward "Disclaimer of Warranty")
317 (defun describe-prefix-bindings ()
318 "Describe the bindings of the prefix used to reach this command.
319 The prefix described consists of all but the last event
320 of the key sequence that ran this command."
322 (let ((key (this-command-keys)))
325 (substring key 0 (1- (length key)))
326 (let ((prefix (make-vector (1- (length key)) nil))
328 (while (< i (length prefix))
329 (aset prefix i (aref key i))
332 ;; Make C-h after a prefix, when not specifically bound,
333 ;; run describe-prefix-bindings.
334 (setq prefix-help-command 'describe-prefix-bindings)
336 (defun view-emacs-news (&optional version)
337 "Display info on recent changes to Emacs.
338 With argument, display info only for the selected version."
341 (setq version emacs-major-version))
342 (when (consp version)
348 (insert-file-contents
349 (expand-file-name file data-directory))
350 (while (re-search-forward
351 (if (member file '("NEWS.18" "NEWS.1-17"))
352 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
353 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
354 (setq res (cons (match-string-no-properties 1) res)))))
356 (directory-files data-directory nil
357 "^NEWS\\.[0-9][-0-9]*$" nil)))
358 (sort (delete-dups res) (lambda (a b) (string< b a)))))
359 (current (car all-versions)))
360 (setq version (completing-read
361 (format "Read NEWS for the version (default %s): " current)
362 all-versions nil nil nil nil current))
363 (if (integerp (string-to-number version))
364 (setq version (string-to-number version))
365 (unless (or (member version all-versions)
366 (<= (string-to-number version) (string-to-number current)))
367 (error "No news about version %s" version)))))
368 (when (integerp version)
369 (cond ((<= version 12)
370 (setq version (format "1.%d" version)))
372 (setq version (format "%d" version)))
373 ((> version emacs-major-version)
374 (error "No news about Emacs %d (yet)" version))))
375 (let* ((vn (if (stringp version)
376 (string-to-number version)
379 ((>= vn emacs-major-version) "NEWS")
380 ((< vn 18) "NEWS.1-17")
381 (t (format "NEWS.%d" vn))))
383 (view-file (expand-file-name file data-directory))
385 (goto-char (point-min))
386 (when (stringp version)
387 (when (re-search-forward
388 (concat (if (< vn 19)
389 "Changes in Emacs[ \t]*"
390 "^\* [^0-9\n]*") version "$")
396 (while (and (setq res
399 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
400 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
401 (equal (match-string-no-properties 1) version)))
402 (or res (goto-char (point-max)))
406 (defun view-emacs-todo (&optional arg)
407 "Display the Emacs TODO list."
409 (view-help-file "TODO"))
411 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
414 (defun view-echo-area-messages ()
415 "View the log of recent echo-area messages: the `*Messages*' buffer.
416 The number of messages retained in that buffer
417 is specified by the variable `message-log-max'."
419 (switch-to-buffer (get-buffer-create "*Messages*")))
421 (defun view-order-manuals ()
422 "Display the Emacs ORDERS file."
424 (view-help-file "ORDERS"))
426 (defun view-emacs-FAQ ()
427 "Display the Emacs Frequently Asked Questions (FAQ) file."
429 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
432 (defun view-emacs-problems ()
433 "Display info on known problems with Emacs and possible workarounds."
435 (view-help-file "PROBLEMS"))
437 (defun view-emacs-debugging ()
438 "Display info on how to debug Emacs problems."
440 (view-help-file "DEBUG"))
442 (defun view-external-packages ()
443 "Display external packages and information about Emacs."
445 (view-help-file "MORE.STUFF"))
447 (defun view-lossage ()
448 "Display last 300 input keystrokes.
450 To record all your input on a file, use `open-dribble-file'."
452 (help-setup-xref (list #'view-lossage)
453 (called-interactively-p 'interactive))
454 (with-help-window (help-buffer)
455 (princ (mapconcat (lambda (key)
456 (if (or (integerp key) (symbolp key) (listp key))
457 (single-key-description key)
458 (prin1-to-string key nil)))
461 (with-current-buffer standard-output
462 (goto-char (point-min))
463 (while (progn (move-to-column 50) (not (eobp)))
464 (when (search-forward " " nil t)
467 ;; jidanni wants to see the last keystrokes immediately.
468 (set-marker help-window-point-marker (point)))))
473 (defun describe-bindings (&optional prefix buffer)
474 "Show a list of all defined keys, and their definitions.
475 We put that list in a buffer, and display the buffer.
477 The optional argument PREFIX, if non-nil, should be a key sequence;
478 then we display only bindings that start with that prefix.
479 The optional argument BUFFER specifies which buffer's bindings
480 to display (default, the current buffer). BUFFER can be a buffer
483 (or buffer (setq buffer (current-buffer)))
484 (help-setup-xref (list #'describe-bindings prefix buffer)
485 (called-interactively-p 'interactive))
486 (with-current-buffer buffer
487 (describe-bindings-internal nil prefix)))
489 ;; This function used to be in keymap.c.
490 (defun describe-bindings-internal (&optional menus prefix)
491 "Show a list of all defined keys, and their definitions.
492 We put that list in a buffer, and display the buffer.
494 The optional argument MENUS, if non-nil, says to mention menu bindings.
495 \(Ordinarily these are omitted from the output.)
496 The optional argument PREFIX, if non-nil, should be a key sequence;
497 then we display only bindings that start with that prefix."
498 (let ((buf (current-buffer)))
499 (with-help-window "*Help*"
500 (with-current-buffer standard-output
501 (describe-buffer-bindings buf prefix menus)))))
503 (defun where-is (definition &optional insert)
504 "Print message listing key sequences that invoke the command DEFINITION.
505 Argument is a command definition, usually a symbol with a function definition.
506 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
508 (let ((fn (function-called-at-point))
509 (enable-recursive-minibuffers t)
511 (setq val (completing-read
513 (format "Where is command (default %s): " fn)
514 "Where is command: ")
515 obarray 'commandp t))
516 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
517 (unless definition (error "No command"))
518 (let ((func (indirect-function definition))
520 (standard-output (if insert (current-buffer) t)))
521 ;; In DEFS, find all symbols that are aliases for DEFINITION.
522 (mapatoms (lambda (symbol)
523 (and (fboundp symbol)
524 (not (eq symbol definition))
525 (eq func (condition-case ()
526 (indirect-function symbol)
528 (push symbol defs))))
529 ;; Look at all the symbols--first DEFINITION,
531 (dolist (symbol (cons definition defs))
532 (let* ((remapped (command-remapping symbol))
533 (keys (where-is-internal
534 symbol overriding-local-map nil nil remapped))
535 (keys (mapconcat 'key-description keys ", "))
539 (if (> (length keys) 0)
541 (format "%s (%s) (remapped from %s)"
542 keys remapped symbol)
543 (format "%s (%s)" keys symbol))
544 (format "M-x %s RET" symbol))
545 (if (> (length keys) 0)
547 (format "%s is remapped to %s which is on %s"
548 symbol remapped keys)
549 (format "%s is on %s" symbol keys))
550 ;; If this is the command the user asked about,
551 ;; and it is not on any key, say so.
552 ;; For other symbols, its aliases, say nothing
553 ;; about them unless they are on keys.
554 (if (eq symbol definition)
555 (format "%s is not on any key" symbol)))))
557 (unless (eq symbol definition)
558 (princ ";\n its alias "))
562 (defun help-key-description (key untranslated)
563 (let ((string (key-description key)))
564 (if (or (not untranslated)
565 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
567 (let ((otherstring (key-description untranslated)))
568 (if (equal string otherstring)
570 (format "%s (translated from %s)" string otherstring))))))
572 (defun describe-key-briefly (&optional key insert untranslated)
573 "Print the name of the function KEY invokes. KEY is a string.
574 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
575 If non-nil, UNTRANSLATED is a vector of the untranslated events.
576 It can also be a number in which case the untranslated events from
577 the last key hit are used.
579 If KEY is a menu item or a tool-bar button that is disabled, this command
580 temporarily enables it to allow getting help on disabled items and buttons."
582 (let ((enable-disabled-menus-and-buttons t)
583 (cursor-in-echo-area t)
587 ;; If yank-menu is empty, populate it temporarily, so that
588 ;; "Select and Paste" menu can generate a complete event.
589 (when (null (cdr yank-menu))
590 (setq saved-yank-menu (copy-sequence yank-menu))
591 (menu-bar-update-yank-menu "(any string)" nil))
592 (setq key (read-key-sequence "Describe key (or click or menu item): "))
593 ;; If KEY is a down-event, read and discard the
594 ;; corresponding up-event. Note that there are also
595 ;; down-events on scroll bars and mode lines: the actual
596 ;; event then is in the second element of the vector.
598 (let ((last-idx (1- (length key))))
599 (and (eventp (aref key last-idx))
600 (memq 'down (event-modifiers (aref key last-idx)))))
604 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
606 ;; Put yank-menu back as it was, if we changed it.
607 (when saved-yank-menu
608 (setq yank-menu (copy-sequence saved-yank-menu))
609 (fset 'yank-menu (cons 'keymap yank-menu))))))
610 (if (numberp untranslated)
611 (setq untranslated (this-single-command-raw-keys)))
612 (let* ((event (if (and (symbolp (aref key 0))
614 (consp (aref key 1)))
617 (modifiers (event-modifiers event))
618 (standard-output (if insert (current-buffer) t))
619 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
620 (memq 'drag modifiers)) " at that spot" ""))
621 (defn (key-binding key t))
623 ;; Handle the case where we faked an entry in "Select and Paste" menu.
624 (if (and (eq defn nil)
625 (stringp (aref key (1- (length key))))
626 (eq (key-binding (substring key 0 -1)) 'yank-menu))
627 (setq defn 'menu-bar-select-yank))
628 ;; Don't bother user with strings from (e.g.) the select-paste menu.
629 (if (stringp (aref key (1- (length key))))
630 (aset key (1- (length key)) "(any string)"))
631 (if (and (> (length untranslated) 0)
632 (stringp (aref untranslated (1- (length untranslated)))))
633 (aset untranslated (1- (length untranslated)) "(any string)"))
634 ;; Now describe the key, perhaps as changed.
635 (setq key-desc (help-key-description key untranslated))
636 (if (or (null defn) (integerp defn) (equal defn 'undefined))
637 (princ (format "%s%s is undefined" key-desc mouse-msg))
638 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
640 (defun describe-key (&optional key untranslated up-event)
641 "Display documentation of the function invoked by KEY.
642 KEY can be any kind of a key sequence; it can include keyboard events,
643 mouse events, and/or menu events. When calling from a program,
644 pass KEY as a string or a vector.
646 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
647 It can also be a number, in which case the untranslated events from
648 the last key sequence entered are used.
649 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
651 If KEY is a menu item or a tool-bar button that is disabled, this command
652 temporarily enables it to allow getting help on disabled items and buttons."
654 (let ((enable-disabled-menus-and-buttons t)
655 (cursor-in-echo-area t)
659 ;; If yank-menu is empty, populate it temporarily, so that
660 ;; "Select and Paste" menu can generate a complete event.
661 (when (null (cdr yank-menu))
662 (setq saved-yank-menu (copy-sequence yank-menu))
663 (menu-bar-update-yank-menu "(any string)" nil))
664 (setq key (read-key-sequence "Describe key (or click or menu item): "))
667 (prefix-numeric-value current-prefix-arg)
668 ;; If KEY is a down-event, read and include the
669 ;; corresponding up-event. Note that there are also
670 ;; down-events on scroll bars and mode lines: the actual
671 ;; event then is in the second element of the vector.
673 (let ((last-idx (1- (length key))))
674 (and (eventp (aref key last-idx))
675 (memq 'down (event-modifiers (aref key last-idx)))))
676 (or (and (eventp (aref key 0))
677 (memq 'down (event-modifiers (aref key 0)))
678 ;; However, for the C-down-mouse-2 popup
679 ;; menu, there is no subsequent up-event. In
680 ;; this case, the up-event is the next
681 ;; element in the supplied vector.
683 (and (> (length key) 1)
684 (eventp (aref key 1))
685 (memq 'down (event-modifiers (aref key 1)))))
687 ;; Put yank-menu back as it was, if we changed it.
688 (when saved-yank-menu
689 (setq yank-menu (copy-sequence saved-yank-menu))
690 (fset 'yank-menu (cons 'keymap yank-menu))))))
691 (if (numberp untranslated)
692 (setq untranslated (this-single-command-raw-keys)))
693 (let* ((event (aref key (if (and (symbolp (aref key 0))
695 (consp (aref key 1)))
698 (modifiers (event-modifiers event))
699 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
700 (memq 'drag modifiers)) " at that spot" ""))
701 (defn (key-binding key t))
702 defn-up defn-up-tricky ev-type
703 mouse-1-remapped mouse-1-tricky)
705 ;; Handle the case where we faked an entry in "Select and Paste" menu.
706 (when (and (eq defn nil)
707 (stringp (aref key (1- (length key))))
708 (eq (key-binding (substring key 0 -1)) 'yank-menu))
709 (setq defn 'menu-bar-select-yank))
710 (if (or (null defn) (integerp defn) (equal defn 'undefined))
711 (message "%s%s is undefined"
712 (help-key-description key untranslated) mouse-msg)
713 (help-setup-xref (list #'describe-function defn)
714 (called-interactively-p 'interactive))
715 ;; Don't bother user with strings from (e.g.) the select-paste menu.
716 (when (stringp (aref key (1- (length key))))
717 (aset key (1- (length key)) "(any string)"))
718 (when (and untranslated
719 (stringp (aref untranslated (1- (length untranslated)))))
720 (aset untranslated (1- (length untranslated))
722 ;; Need to do this before erasing *Help* buffer in case event
723 ;; is a mouse click in an existing *Help* buffer.
725 (setq ev-type (event-basic-type up-event))
726 (let ((sequence (vector up-event)))
727 (when (and (eq ev-type 'mouse-1)
728 mouse-1-click-follows-link
729 (not (eq mouse-1-click-follows-link 'double))
730 (setq mouse-1-remapped
731 (mouse-on-link-p (event-start up-event))))
732 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
733 (> mouse-1-click-follows-link 0)))
734 (cond ((stringp mouse-1-remapped)
735 (setq sequence mouse-1-remapped))
736 ((vectorp mouse-1-remapped)
737 (setcar up-event (elt mouse-1-remapped 0)))
738 (t (setcar up-event 'mouse-2))))
739 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
741 (setq sequence (vector up-event))
742 (aset sequence 0 'mouse-1)
743 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
744 (with-help-window (help-buffer)
745 (princ (help-key-description key untranslated))
747 %s runs the command %S, which is "
749 (describe-function-1 defn)
751 (unless (or (null defn-up)
753 (equal defn-up 'undefined))
756 ----------------- up-event %s----------------
758 %s%s%s runs the command %S, which is "
759 (if mouse-1-tricky "(short click) " "")
760 (key-description (vector up-event))
763 " is remapped to <mouse-2>, which" "")
765 (describe-function-1 defn-up))
766 (unless (or (null defn-up-tricky)
767 (integerp defn-up-tricky)
768 (eq defn-up-tricky 'undefined))
771 ----------------- up-event (long click) ----------------
773 Pressing <%S>%s for longer than %d milli-seconds
774 runs the command %S, which is "
776 mouse-1-click-follows-link
778 (describe-function-1 defn-up-tricky)))))))
780 (defun describe-mode (&optional buffer)
781 "Display documentation of current major mode and minor modes.
782 A brief summary of the minor modes comes first, followed by the
783 major mode description. This is followed by detailed
784 descriptions of the minor modes, each on a separate page.
786 For this to work correctly for a minor mode, the mode's indicator
787 variable \(listed in `minor-mode-alist') must also be a function
788 whose documentation describes the minor mode."
790 (unless buffer (setq buffer (current-buffer)))
791 (help-setup-xref (list #'describe-mode buffer)
792 (called-interactively-p 'interactive))
793 ;; For the sake of help-do-xref and help-xref-go-back,
794 ;; don't switch buffers before calling `help-buffer'.
795 (with-help-window (help-buffer)
796 (with-current-buffer buffer
798 ;; Older packages do not register in minor-mode-list but only in
800 (dolist (x minor-mode-alist)
802 (unless (memq x minor-mode-list)
803 (push x minor-mode-list)))
804 ;; Find enabled minor mode we will want to mention.
805 (dolist (mode minor-mode-list)
806 ;; Document a minor mode if it is listed in minor-mode-alist,
807 ;; non-nil, and has a function definition.
808 (let ((fmode (or (get mode :minor-mode-function) mode)))
809 (and (boundp mode) (symbol-value mode)
811 (let ((pretty-minor-mode
812 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
815 (substring (symbol-name fmode)
816 0 (match-beginning 0)))
818 (push (list fmode pretty-minor-mode
819 (format-mode-line (assq mode minor-mode-alist)))
823 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
825 (princ "Enabled minor modes:\n")
826 (make-local-variable 'help-button-cache)
827 (with-current-buffer standard-output
828 (dolist (mode minor-modes)
829 (let ((mode-function (nth 0 mode))
830 (pretty-minor-mode (nth 1 mode))
831 (indicator (nth 2 mode)))
832 (add-text-properties 0 (length pretty-minor-mode)
833 '(face bold) pretty-minor-mode)
835 (goto-char (point-max))
837 (push (point-marker) help-button-cache)
838 ;; Document the minor modes fully.
839 (insert pretty-minor-mode)
840 (princ (format " minor mode (%s):\n"
841 (if (zerop (length indicator))
843 (format "indicator%s"
845 (princ (documentation mode-function)))
846 (insert-button pretty-minor-mode
847 'action (car help-button-cache)
849 'help-echo "mouse-2, RET: show full information")
855 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
856 ;; Document the major mode.
857 (let ((mode mode-name))
858 (with-current-buffer standard-output
859 (let ((start (point)))
860 (insert (format-mode-line mode nil nil buffer))
861 (add-text-properties start (point) '(face bold)))))
863 (princ (documentation major-mode)))))
864 ;; For the sake of IELM and maybe others
868 (defun describe-minor-mode (minor-mode)
869 "Display documentation of a minor mode given as MINOR-MODE.
870 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
871 appeared on the mode-line."
872 (interactive (list (completing-read
875 (describe-minor-mode-completion-table-for-symbol)
876 (describe-minor-mode-completion-table-for-indicator)
878 (if (symbolp minor-mode)
879 (setq minor-mode (symbol-name minor-mode)))
880 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
881 (indicators (describe-minor-mode-completion-table-for-indicator)))
883 ((member minor-mode symbols)
884 (describe-minor-mode-from-symbol (intern minor-mode)))
885 ((member minor-mode indicators)
886 (describe-minor-mode-from-indicator minor-mode))
888 (error "No such minor mode: %s" minor-mode)))))
891 (defun describe-minor-mode-completion-table-for-symbol ()
892 ;; In order to list up all minor modes, minor-mode-list
893 ;; is used here instead of minor-mode-alist.
894 (delq nil (mapcar 'symbol-name minor-mode-list)))
896 (defun describe-minor-mode-from-symbol (symbol)
897 "Display documentation of a minor mode given as a symbol, SYMBOL"
898 (interactive (list (intern (completing-read
899 "Minor mode symbol: "
900 (describe-minor-mode-completion-table-for-symbol)))))
902 (describe-function symbol)
903 (describe-variable symbol)))
906 (defun describe-minor-mode-completion-table-for-indicator ()
909 (let ((i (format-mode-line x)))
910 ;; remove first space if existed
920 (defun describe-minor-mode-from-indicator (indicator)
921 "Display documentation of a minor mode specified by INDICATOR.
922 If you call this function interactively, you can give indicator which
923 is currently activated with completion."
926 "Minor mode indicator: "
927 (describe-minor-mode-completion-table-for-indicator))))
928 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
930 (describe-minor-mode-from-symbol minor-mode)
931 (error "Cannot find minor mode for `%s'" indicator))))
933 (defun lookup-minor-mode-from-indicator (indicator)
934 "Return a minor mode symbol from its indicator on the modeline."
935 ;; remove first space if existed
936 (if (and (< 0 (length indicator))
937 (eq (aref indicator 0) ?\s))
938 (setq indicator (substring indicator 1)))
939 (let ((minor-modes minor-mode-alist)
942 (let* ((minor-mode (car (car minor-modes)))
943 (anindicator (format-mode-line
944 (car (cdr (car minor-modes))))))
945 ;; remove first space if existed
946 (if (and (stringp anindicator)
947 (> (length anindicator) 0)
948 (eq (aref anindicator 0) ?\s))
949 (setq anindicator (substring anindicator 1)))
950 (if (equal indicator anindicator)
951 (setq result minor-mode
953 (setq minor-modes (cdr minor-modes)))))
956 ;;; Automatic resizing of temporary buffers.
957 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
958 "Maximum height of a window displaying a temporary buffer.
959 This is effective only when Temp Buffer Resize mode is enabled.
960 The value is the maximum height (in lines) which
961 `resize-temp-buffer-window' will give to a window displaying a
962 temporary buffer. It can also be a function to be called to
963 choose the height for such a buffer. It gets one argumemt, the
964 buffer, and should return a positive integer. At the time the
965 function is called, the window to be resized is selected."
966 :type '(choice integer function)
970 (define-minor-mode temp-buffer-resize-mode
971 "Toggle mode which makes windows smaller for temporary buffers.
972 With prefix argument ARG, turn the resizing of windows displaying
973 temporary buffers on if ARG is positive or off otherwise.
975 This mode makes a window the right height for its contents, but
976 never more than `temp-buffer-max-height' nor less than
979 This mode is used by `help', `apropos' and `completion' buffers,
981 :global t :group 'help
982 (if temp-buffer-resize-mode
983 ;; `help-make-xrefs' may add a `back' button and thus increase the
984 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
985 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
986 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
988 (defun resize-temp-buffer-window ()
989 "Resize the selected window to fit its contents.
990 Will not make it higher than `temp-buffer-max-height' nor smaller
991 than `window-min-height'. Do nothing if the selected window is
992 not vertically combined or some of its contents are scrolled out
994 (when (and (pos-visible-in-window-p (point-min))
995 (window-iso-combined-p))
996 (fit-window-to-buffer
998 (if (functionp temp-buffer-max-height)
999 (funcall temp-buffer-max-height (window-buffer))
1000 temp-buffer-max-height))))
1003 (defcustom help-window-select 'other
1004 "Non-nil means select help window for viewing.
1006 never (nil) Select help window only if there is no other window
1008 other Select help window unless the selected window is the
1009 only other window on the help window's frame.
1010 always (t) Always select the help window.
1012 This option has effect if and only if the help window was created
1013 by `with-help-window'"
1014 :type '(choice (const :tag "never (nil)" nil)
1015 (const :tag "other" other)
1016 (const :tag "always (t)" t))
1020 (defun help-window-display-message (quit-part window &optional scroll)
1021 "Display message telling how to quit and scroll help window.
1022 QUIT-PART is a string telling how to quit the help window WINDOW.
1023 Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1024 SCROLL equal `other' means tell how to scroll the \"other\"
1028 ((pos-visible-in-window-p
1029 (with-current-buffer (window-buffer window)
1030 (point-max)) window)
1031 ;; Buffer end is visible.
1034 ", \\[scroll-other-window] to scroll help.")
1035 (scroll ", \\[scroll-up] to scroll help."))))
1037 (substitute-command-keys (concat quit-part scroll-part)))))
1039 (defun help-window-setup ()
1040 "Set up help window for `with-help-window'.
1041 This relies on `display-buffer-window' being correctly set up by
1043 (let* ((help-window (car-safe display-buffer-window))
1044 (help-buffer (when (window-live-p help-window)
1045 (window-buffer help-window)))
1046 (help-value (cdr-safe display-buffer-window)))
1048 ;; Handle `help-window-point-marker'.
1049 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1050 (set-window-point help-window help-window-point-marker)
1051 ;; Reset `help-window-point-marker'.
1052 (set-marker help-window-point-marker nil))
1055 ((or (eq help-window (selected-window))
1056 (and (or (eq help-window-select t)
1057 (and (eq help-window-select 'other)
1058 (eq (window-frame help-window) (selected-frame))
1059 (= (length (window-list nil 'no-mini)) 2)))
1060 (select-window help-window)))
1061 ;; The help window is or gets selected ...
1062 (help-window-display-message
1064 ((eq help-value 'new-window)
1065 ;; ... and is new, ...
1066 "Type \"q\" to delete this window")
1067 ((eq help-value 'new-frame)
1068 ;; ... is on a new frame ...
1069 "Type \"q\" to delete this frame")
1070 ((eq help-value 'reuse-other-window)
1071 ;; ... or displayed some other buffer before.
1072 "Type \"q\" to restore previous buffer"))
1074 ((and (eq (window-frame help-window) (selected-frame))
1075 (= (length (window-list nil 'no-mini)) 2))
1076 ;; There are two windows on the help window's frame and the
1077 ;; other one is the selected one.
1078 (help-window-display-message
1080 ((eq help-value 'new-window)
1081 "Type \\[delete-other-windows] to delete the help window")
1082 ((eq help-value 'reuse-other-window)
1083 "Type \\[switch-to-prev-buffer] RET to restore previous buffer"))
1084 help-window 'other))
1086 ;; Not much to say here.
1087 (help-window-display-message
1088 "Type \"q\" in help window to quit" help-window))))))
1090 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1091 ;; providing the following additional twists:
1093 ;; (1) Issue more accurate messages telling how to scroll and quit the
1096 ;; (2) An option (customizable via `help-window-select') to select the
1097 ;; help window automatically.
1099 ;; (3) A marker (`help-window-point-marker') to move point in the help
1100 ;; window to an arbitrary buffer position.
1102 ;; Note: It's usually always wrong to use `help-print-return-message' in
1103 ;; the body of `with-help-window'.
1104 (defmacro with-help-window (buffer-name &rest body)
1105 "Display buffer with name BUFFER-NAME in a help window evaluating BODY.
1106 Select help window if the actual value of the user option
1107 `help-window-select' says so. Return last value in BODY.
1109 You can specify where and how to show the buffer by binding the
1110 variable `temp-buffer-show-specifiers' to an appropriate value."
1111 (declare (indent 1) (debug t))
1113 ;; Reset `display-buffer-window': `display-buffer' is
1114 ;; supposed to set this to the window displaying the buffer plus
1115 ;; some additional information.
1116 (setq display-buffer-window nil)
1117 ;; Make `help-window-point-marker' point nowhere. The only place
1118 ;; where this should be set to a buffer position is within BODY.
1119 (set-marker help-window-point-marker nil)
1121 ;; Return value returned by `with-output-to-temp-buffer'.
1122 (with-output-to-temp-buffer ,buffer-name
1124 (when display-buffer-window (help-window-setup)))))
1128 ;;; help.el ends here