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 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, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
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 ;; The variable `help-window' below is used by `help-mode-finish' to
42 ;; communicate the window displaying help (the "help window") to the
43 ;; macro `with-help-window'. The latter sets `help-window' to t before
44 ;; invoking `with-output-to-temp-buffer'. If and only if `help-window'
45 ;; is eq to t, `help-mode-finish' (called by `temp-buffer-setup-hook')
46 ;; sets `help-window' to the window selected by `display-buffer'.
47 ;; Exiting `with-help-window' and calling `print-help-return-message'
48 ;; reset `help-window' to nil.
49 (defvar help-window nil
50 "Window chosen for displaying help.")
52 ;; `help-window-point-marker' is a marker you can move to a valid
53 ;; position of the buffer shown in the help window in order to override
54 ;; the standard positioning mechanism (`point-min') chosen by
55 ;; `with-output-to-temp-buffer'. `with-help-window' has this point
56 ;; nowhere before exiting. Currently used by `view-lossage' to assert
57 ;; that the last keystrokes are always visible.
58 (defvar help-window-point-marker
(make-marker)
59 "Marker to override default `window-point' of `help-window'.")
62 (let ((map (make-sparse-keymap)))
63 (define-key map
(char-to-string help-char
) 'help-for-help
)
64 (define-key map
[help] 'help-for-help)
65 (define-key map [f1] 'help-for-help)
66 (define-key map "." 'display-local-help)
67 (define-key map "?" 'help-for-help)
69 (define-key map "\C-a" 'about-emacs)
70 (define-key map "\C-c" 'describe-copying)
71 (define-key map "\C-d" 'view-emacs-debugging)
72 (define-key map "\C-e" 'view-external-packages)
73 (define-key map "\C-f" 'view-emacs-FAQ)
74 (define-key map "\C-m" 'view-order-manuals)
75 (define-key map "\C-n" 'view-emacs-news)
76 (define-key map "\C-o" 'describe-distribution)
77 (define-key map "\C-p" 'view-emacs-problems)
78 (define-key map "\C-t" 'view-emacs-todo)
79 (define-key map "\C-w" 'describe-no-warranty)
81 ;; This does not fit the pattern, but it is natural given the C-\ command.
82 (define-key map "\C-\\" 'describe-input-method)
84 (define-key map "C" 'describe-coding-system)
85 (define-key map "F" 'Info-goto-emacs-command-node)
86 (define-key map "I" 'describe-input-method)
87 (define-key map "K" 'Info-goto-emacs-key-command-node)
88 (define-key map "L" 'describe-language-environment)
89 (define-key map "S" 'info-lookup-symbol)
91 (define-key map "a" 'apropos-command)
92 (define-key map "b" 'describe-bindings)
93 (define-key map "c" 'describe-key-briefly)
94 (define-key map "d" 'apropos-documentation)
95 (define-key map "e" 'view-echo-area-messages)
96 (define-key map "f" 'describe-function)
97 (define-key map "g" 'describe-gnu-project)
98 (define-key map "h" 'view-hello-file)
100 (define-key map "i" 'info)
101 (define-key map "4i" 'info-other-window)
103 (define-key map "k" 'describe-key)
104 (define-key map "l" 'view-lossage)
105 (define-key map "m" 'describe-mode)
106 (define-key map "n" 'view-emacs-news)
107 (define-key map "p" 'finder-by-keyword)
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 (defun print-help-return-message (&optional function)
143 "Display or return message saying how to restore windows after help command.
144 This function assumes that `standard-output' is the help buffer.
145 It computes a message, and applies the optional argument FUNCTION to it.
146 If FUNCTION is nil, it applies `message', thus displaying the message.
147 In addition, this function sets up `help-return-method', which see, that
148 specifies what to do when the user exits the help buffer."
149 ;; Reset `help-window' here to avoid confusing `help-mode-finish'.
150 (setq help-window nil)
151 (and (not (get-buffer-window standard-output))
155 (special-display-p (buffer-name standard-output)))
156 (setq help-return-method (cons (selected-window) t))
157 ;; If the help output buffer is a special display buffer,
158 ;; don't say anything about how to get rid of it.
159 ;; First of all, the user will do that with the window
160 ;; manager, not with Emacs.
161 ;; Secondly, the buffer has not been displayed yet,
162 ;; so we don't know whether its frame will be selected.
164 (display-buffer-reuse-frames
165 (setq help-return-method (cons (selected-window)
168 ((not (one-window-p t))
169 (setq help-return-method
170 (cons (selected-window) 'quit-window))
171 "Type \\[display-buffer] RET to restore the other window.")
173 (setq help-return-method (cons (selected-window) t))
174 "Type \\[delete-other-windows] to remove help window.")
176 (setq help-return-method
177 (list (selected-window) (window-buffer)
178 (window-start) (window-point)))
179 "Type \\[switch-to-buffer] RET to remove help window."))))
180 (funcall (or function 'message)
183 (substitute-command-keys first-message))
184 (if first-message " ")
185 ;; If the help buffer will go in a separate frame,
186 ;; it's no use mentioning a command to scroll, so don't.
187 (if (or pop-up-windows
188 (special-display-p (buffer-name standard-output)))
190 (if (same-window-p (buffer-name standard-output))
191 ;; Say how to scroll this window.
192 (substitute-command-keys
193 "\\[scroll-up] to scroll the help.")
194 ;; Say how to scroll some other window.
195 (substitute-command-keys
196 "\\[scroll-other-window] to scroll the help."))))))))
198 ;; So keyboard macro definitions are documented correctly
199 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
201 (defalias 'help 'help-for-help-internal)
202 ;; find-function can find this.
203 (defalias 'help-for-help 'help-for-help-internal)
204 ;; It can't find this, but nobody will look.
205 (make-help-screen help-for-help-internal
206 "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?"
207 "You have typed %THIS-KEY%, the help character. Type a Help option:
208 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
210 a PATTERN Show commands whose name matches the PATTERN (a list of words
211 or a regexp). See also the `apropos' command.
212 b Display all key bindings.
213 c KEYS Display the command name run by the given key sequence.
214 C CODING Describe the given coding system, or RET for current ones.
215 d PATTERN Show a list of functions, variables, and other items whose
216 documentation matches the PATTERN (a list of words or a regexp).
217 e Go to the *Messages* buffer which logs echo-area messages.
218 f FUNCTION Display documentation for the given function.
219 F COMMAND Show the on-line manual's section that describes the command.
220 g Display information about the GNU project.
221 h Display the HELLO file which illustrates various scripts.
222 i Start the Info documentation reader: read on-line manuals.
223 I METHOD Describe a specific input method, or RET for current.
224 k KEYS Display the full documentation for the key sequence.
225 K KEYS Show the on-line manual's section for the command bound to KEYS.
226 l Show last 100 characters you typed (lossage).
227 L LANG-ENV Describes a specific language environment, or RET for current.
228 m Display documentation of current minor modes and current major mode,
229 including their special commands.
230 n Display news of recent Emacs changes.
231 p TOPIC Find packages matching a given topic keyword.
232 r Display the Emacs manual in Info mode.
233 s Display contents of current syntax table, plus explanations.
234 S SYMBOL Show the section for the given symbol in the on-line manual
235 for the programming language used in this buffer.
236 t Start the Emacs learn-by-doing tutorial.
237 v VARIABLE Display the given variable's documentation and value.
238 w COMMAND Display which keystrokes invoke the given command (where-is).
239 . Display any available local help at point in the echo area.
241 C-a Information about Emacs.
242 C-c Emacs copying permission (GNU General Public License).
243 C-d Instructions for debugging GNU Emacs.
244 C-e External packages and information about Emacs.
246 C-m How to order printed Emacs manuals.
247 C-n News of recent Emacs changes.
248 C-o Emacs ordering and distribution information.
249 C-p Info about known Emacs problems.
251 C-w Information on absence of warranty for GNU Emacs."
256 (defun function-called-at-point ()
257 "Return a function around point or else called by the list containing point.
258 If that doesn't give a function, return nil."
259 (or (with-syntax-table emacs-lisp-mode-syntax-table
260 (or (condition-case ()
262 (or (not (zerop (skip-syntax-backward "_w")))
263 (eq (char-syntax (following-char)) ?w)
264 (eq (char-syntax (following-char)) ?_)
266 (skip-chars-forward "'")
267 (let ((obj (read (current-buffer))))
268 (and (symbolp obj) (fboundp obj) obj)))
273 (narrow-to-region (max (point-min)
274 (- (point) 1000)) (point-max))
275 ;; Move up to surrounding paren, then after the open.
278 ;; If there is space here, this is probably something
279 ;; other than a real Lisp function call, so ignore it.
280 (if (looking-at "[ \t]")
281 (error "Probably not a Lisp function call"))
282 (let ((obj (read (current-buffer))))
283 (and (symbolp obj) (fboundp obj) obj))))
285 (let* ((str (find-tag-default))
286 (sym (if str (intern-soft str))))
287 (if (and sym (fboundp sym))
290 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
291 (setq sym (intern-soft (match-string 1 str)))
292 (and (fboundp sym) sym)))))))
295 ;;; `User' help functions
297 (defun view-help-file (file &optional dir)
298 (view-file (expand-file-name file (or dir data-directory)))
300 (goto-char (point-min)))
302 (defun describe-distribution ()
303 "Display info on how to obtain the latest version of GNU Emacs."
305 (view-help-file "DISTRIB"))
307 (defun describe-copying ()
308 "Display info on how you may redistribute copies of GNU Emacs."
310 (view-help-file "COPYING"))
312 (defalias 'describe-project 'describe-gnu-project)
313 (defun describe-gnu-project ()
314 "Display info on the GNU project."
316 (view-help-file "THE-GNU-PROJECT"))
318 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
320 (defun describe-no-warranty ()
321 "Display info on all the kinds of warranty Emacs does NOT have."
324 (let (case-fold-search)
325 (search-forward "NO WARRANTY")
328 (defun describe-prefix-bindings ()
329 "Describe the bindings of the prefix used to reach this command.
330 The prefix described consists of all but the last event
331 of the key sequence that ran this command."
333 (let ((key (this-command-keys)))
336 (substring key 0 (1- (length key)))
337 (let ((prefix (make-vector (1- (length key)) nil))
339 (while (< i (length prefix))
340 (aset prefix i (aref key i))
343 ;; Make C-h after a prefix, when not specifically bound,
344 ;; run describe-prefix-bindings.
345 (setq prefix-help-command 'describe-prefix-bindings)
347 (defun view-emacs-news (&optional version)
348 "Display info on recent changes to Emacs.
349 With argument, display info only for the selected version."
352 (setq version emacs-major-version))
353 (when (consp version)
359 (insert-file-contents
360 (expand-file-name file data-directory))
361 (while (re-search-forward
362 (if (member file '("NEWS.18" "NEWS.1-17"))
363 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
364 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
365 (setq res (cons (match-string-no-properties 1) res)))))
367 (directory-files data-directory nil
368 "^NEWS\\.[0-9][-0-9]*$" nil)))
369 (sort (delete-dups res) (lambda (a b) (string< b a)))))
370 (current (car all-versions)))
371 (setq version (completing-read
372 (format "Read NEWS for the version (default %s): " current)
373 all-versions nil nil nil nil current))
374 (if (integerp (string-to-number version))
375 (setq version (string-to-number version))
376 (unless (or (member version all-versions)
377 (<= (string-to-number version) (string-to-number current)))
378 (error "No news about version %s" version)))))
379 (when (integerp version)
380 (cond ((<= version 12)
381 (setq version (format "1.%d" version)))
383 (setq version (format "%d" version)))
384 ((> version emacs-major-version)
385 (error "No news about Emacs %d (yet)" version))))
386 (let* ((vn (if (stringp version)
387 (string-to-number version)
390 ((>= vn emacs-major-version) "NEWS")
391 ((< vn 18) "NEWS.1-17")
392 (t (format "NEWS.%d" vn))))
394 (view-file (expand-file-name file data-directory))
396 (goto-char (point-min))
397 (when (stringp version)
398 (when (re-search-forward
399 (concat (if (< vn 19)
400 "Changes in Emacs[ \t]*"
401 "^\* [^0-9\n]*") version "$")
407 (while (and (setq res
410 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
411 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
412 (equal (match-string-no-properties 1) version)))
413 (or res (goto-char (point-max)))
417 (defalias 'view-todo 'view-emacs-todo)
418 (defun view-emacs-todo (&optional arg)
419 "Display the Emacs TODO list."
421 (view-help-file "TODO"))
423 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
426 (defun view-echo-area-messages ()
427 "View the log of recent echo-area messages: the `*Messages*' buffer.
428 The number of messages retained in that buffer
429 is specified by the variable `message-log-max'."
431 (switch-to-buffer (get-buffer-create "*Messages*")))
433 (defun view-order-manuals ()
434 "Display the Emacs ORDERS file."
436 (view-help-file "ORDERS"))
438 (defun view-emacs-FAQ ()
439 "Display the Emacs Frequently Asked Questions (FAQ) file."
441 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
444 (defun view-emacs-problems ()
445 "Display info on known problems with Emacs and possible workarounds."
447 (view-help-file "PROBLEMS"))
449 (defun view-emacs-debugging ()
450 "Display info on how to debug Emacs problems."
452 (view-help-file "DEBUG"))
454 (defun view-external-packages ()
455 "Display external packages and information about Emacs."
457 (view-help-file "MORE.STUFF"))
459 (defun view-lossage ()
460 "Display last 100 input keystrokes.
462 To record all your input on a file, use `open-dribble-file'."
464 (help-setup-xref (list #'view-lossage) (interactive-p))
465 (with-help-window (help-buffer)
466 (princ (mapconcat (lambda (key)
467 (if (or (integerp key) (symbolp key) (listp key))
468 (single-key-description key)
469 (prin1-to-string key nil)))
472 (with-current-buffer standard-output
473 (goto-char (point-min))
474 (while (progn (move-to-column 50) (not (eobp)))
475 (when (search-forward " " nil t)
478 ;; jidanni wants to see the last keystrokes immediately.
479 (set-marker help-window-point-marker (point)))))
484 (defun describe-bindings (&optional prefix buffer)
485 "Show a list of all defined keys, and their definitions.
486 We put that list in a buffer, and display the buffer.
488 The optional argument PREFIX, if non-nil, should be a key sequence;
489 then we display only bindings that start with that prefix.
490 The optional argument BUFFER specifies which buffer's bindings
491 to display (default, the current buffer). BUFFER can be a buffer
494 (or buffer (setq buffer (current-buffer)))
495 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
496 (with-current-buffer buffer
497 (describe-bindings-internal nil prefix)))
499 ;; This function used to be in keymap.c.
500 (defun describe-bindings-internal (&optional menus prefix)
501 "Show a list of all defined keys, and their definitions.
502 We put that list in a buffer, and display the buffer.
504 The optional argument MENUS, if non-nil, says to mention menu bindings.
505 \(Ordinarily these are omitted from the output.)
506 The optional argument PREFIX, if non-nil, should be a key sequence;
507 then we display only bindings that start with that prefix."
509 (let ((buf (current-buffer)))
510 (with-help-window "*Help*"
511 (with-current-buffer standard-output
512 (describe-buffer-bindings buf prefix menus)))))
514 (defun where-is (definition &optional insert)
515 "Print message listing key sequences that invoke the command DEFINITION.
516 Argument is a command definition, usually a symbol with a function definition.
517 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
519 (let ((fn (function-called-at-point))
520 (enable-recursive-minibuffers t)
522 (setq val (completing-read
524 (format "Where is command (default %s): " fn)
525 "Where is command: ")
526 obarray 'commandp t))
527 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
528 (unless definition (error "No command"))
529 (let ((func (indirect-function definition))
531 (standard-output (if insert (current-buffer) t)))
532 ;; In DEFS, find all symbols that are aliases for DEFINITION.
533 (mapatoms (lambda (symbol)
534 (and (fboundp symbol)
535 (not (eq symbol definition))
536 (eq func (condition-case ()
537 (indirect-function symbol)
539 (push symbol defs))))
540 ;; Look at all the symbols--first DEFINITION,
542 (dolist (symbol (cons definition defs))
543 (let* ((remapped (command-remapping symbol))
544 (keys (where-is-internal
545 symbol overriding-local-map nil nil remapped))
546 (keys (mapconcat 'key-description keys ", "))
550 (if (> (length keys) 0)
552 (format "%s (%s) (remapped from %s)"
553 keys remapped symbol)
554 (format "%s (%s)" keys symbol))
555 (format "M-x %s RET" symbol))
556 (if (> (length keys) 0)
558 (format "%s is remapped to %s which is on %s"
559 symbol remapped keys)
560 (format "%s is on %s" symbol keys))
561 ;; If this is the command the user asked about,
562 ;; and it is not on any key, say so.
563 ;; For other symbols, its aliases, say nothing
564 ;; about them unless they are on keys.
565 (if (eq symbol definition)
566 (format "%s is not on any key" symbol)))))
568 (unless (eq symbol definition)
569 (princ ";\n its alias "))
573 (defun help-key-description (key untranslated)
574 (let ((string (key-description key)))
575 (if (or (not untranslated)
576 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
578 (let ((otherstring (key-description untranslated)))
579 (if (equal string otherstring)
581 (format "%s (translated from %s)" string otherstring))))))
583 (defun describe-key-briefly (&optional key insert untranslated)
584 "Print the name of the function KEY invokes. KEY is a string.
585 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
586 If non-nil, UNTRANSLATED is a vector of the untranslated events.
587 It can also be a number in which case the untranslated events from
588 the last key hit are used.
590 If KEY is a menu item or a tool-bar button that is disabled, this command
591 temporarily enables it to allow getting help on disabled items and buttons."
593 (let ((enable-disabled-menus-and-buttons t)
594 (cursor-in-echo-area t)
598 ;; If yank-menu is empty, populate it temporarily, so that
599 ;; "Select and Paste" menu can generate a complete event.
600 (when (null (cdr yank-menu))
601 (setq saved-yank-menu (copy-sequence yank-menu))
602 (menu-bar-update-yank-menu "(any string)" nil))
603 (setq key (read-key-sequence "Describe key (or click or menu item): "))
604 ;; If KEY is a down-event, read and discard the
605 ;; corresponding up-event. Note that there are also
606 ;; down-events on scroll bars and mode lines: the actual
607 ;; event then is in the second element of the vector.
609 (let ((last-idx (1- (length key))))
610 (and (eventp (aref key last-idx))
611 (memq 'down (event-modifiers (aref key last-idx)))))
615 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
617 ;; Put yank-menu back as it was, if we changed it.
618 (when saved-yank-menu
619 (setq yank-menu (copy-sequence saved-yank-menu))
620 (fset 'yank-menu (cons 'keymap yank-menu))))))
621 (if (numberp untranslated)
622 (setq untranslated (this-single-command-raw-keys)))
623 (let* ((event (if (and (symbolp (aref key 0))
625 (consp (aref key 1)))
628 (modifiers (event-modifiers event))
629 (standard-output (if insert (current-buffer) t))
630 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
631 (memq 'drag modifiers)) " at that spot" ""))
632 (defn (key-binding key t))
634 ;; Handle the case where we faked an entry in "Select and Paste" menu.
635 (if (and (eq defn nil)
636 (stringp (aref key (1- (length key))))
637 (eq (key-binding (substring key 0 -1)) 'yank-menu))
638 (setq defn 'menu-bar-select-yank))
639 ;; Don't bother user with strings from (e.g.) the select-paste menu.
640 (if (stringp (aref key (1- (length key))))
641 (aset key (1- (length key)) "(any string)"))
642 (if (and (> (length untranslated) 0)
643 (stringp (aref untranslated (1- (length untranslated)))))
644 (aset untranslated (1- (length untranslated)) "(any string)"))
645 ;; Now describe the key, perhaps as changed.
646 (setq key-desc (help-key-description key untranslated))
647 (if (or (null defn) (integerp defn) (equal defn 'undefined))
648 (princ (format "%s%s is undefined" key-desc mouse-msg))
649 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
651 (defun describe-key (&optional key untranslated up-event)
652 "Display documentation of the function invoked by KEY.
653 KEY can be any kind of a key sequence; it can include keyboard events,
654 mouse events, and/or menu events. When calling from a program,
655 pass KEY as a string or a vector.
657 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
658 It can also be a number, in which case the untranslated events from
659 the last key sequence entered are used.
660 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
662 If KEY is a menu item or a tool-bar button that is disabled, this command
663 temporarily enables it to allow getting help on disabled items and buttons."
665 (let ((enable-disabled-menus-and-buttons t)
666 (cursor-in-echo-area t)
670 ;; If yank-menu is empty, populate it temporarily, so that
671 ;; "Select and Paste" menu can generate a complete event.
672 (when (null (cdr yank-menu))
673 (setq saved-yank-menu (copy-sequence yank-menu))
674 (menu-bar-update-yank-menu "(any string)" nil))
675 (setq key (read-key-sequence "Describe key (or click or menu item): "))
678 (prefix-numeric-value current-prefix-arg)
679 ;; If KEY is a down-event, read and include the
680 ;; corresponding up-event. Note that there are also
681 ;; down-events on scroll bars and mode lines: the actual
682 ;; event then is in the second element of the vector.
684 (let ((last-idx (1- (length key))))
685 (and (eventp (aref key last-idx))
686 (memq 'down (event-modifiers (aref key last-idx)))))
687 (or (and (eventp (aref key 0))
688 (memq 'down (event-modifiers (aref key 0)))
689 ;; However, for the C-down-mouse-2 popup
690 ;; menu, there is no subsequent up-event. In
691 ;; this case, the up-event is the next
692 ;; element in the supplied vector.
694 (and (> (length key) 1)
695 (eventp (aref key 1))
696 (memq 'down (event-modifiers (aref key 1)))))
698 ;; Put yank-menu back as it was, if we changed it.
699 (when saved-yank-menu
700 (setq yank-menu (copy-sequence saved-yank-menu))
701 (fset 'yank-menu (cons 'keymap yank-menu))))))
702 (if (numberp untranslated)
703 (setq untranslated (this-single-command-raw-keys)))
704 (let* ((event (aref key (if (and (symbolp (aref key 0))
706 (consp (aref key 1)))
709 (modifiers (event-modifiers event))
710 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
711 (memq 'drag modifiers)) " at that spot" ""))
712 (defn (key-binding key t))
713 defn-up defn-up-tricky ev-type
714 mouse-1-remapped mouse-1-tricky)
716 ;; Handle the case where we faked an entry in "Select and Paste" menu.
717 (when (and (eq defn nil)
718 (stringp (aref key (1- (length key))))
719 (eq (key-binding (substring key 0 -1)) 'yank-menu))
720 (setq defn 'menu-bar-select-yank))
721 (if (or (null defn) (integerp defn) (equal defn 'undefined))
722 (message "%s%s is undefined"
723 (help-key-description key untranslated) mouse-msg)
724 (help-setup-xref (list #'describe-function defn) (interactive-p))
725 ;; Don't bother user with strings from (e.g.) the select-paste menu.
726 (when (stringp (aref key (1- (length key))))
727 (aset key (1- (length key)) "(any string)"))
728 (when (and untranslated
729 (stringp (aref untranslated (1- (length untranslated)))))
730 (aset untranslated (1- (length untranslated))
732 ;; Need to do this before erasing *Help* buffer in case event
733 ;; is a mouse click in an existing *Help* buffer.
735 (setq ev-type (event-basic-type up-event))
736 (let ((sequence (vector up-event)))
737 (when (and (eq ev-type 'mouse-1)
738 mouse-1-click-follows-link
739 (not (eq mouse-1-click-follows-link 'double))
740 (setq mouse-1-remapped
741 (mouse-on-link-p (event-start up-event))))
742 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
743 (> mouse-1-click-follows-link 0)))
744 (cond ((stringp mouse-1-remapped)
745 (setq sequence mouse-1-remapped))
746 ((vectorp mouse-1-remapped)
747 (setcar up-event (elt mouse-1-remapped 0)))
748 (t (setcar up-event 'mouse-2))))
749 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
751 (setq sequence (vector up-event))
752 (aset sequence 0 'mouse-1)
753 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
754 (with-help-window (help-buffer)
755 (princ (help-key-description key untranslated))
757 %s runs the command %S, which is "
759 (describe-function-1 defn)
761 (unless (or (null defn-up)
763 (equal defn-up 'undefined))
766 ----------------- up-event %s----------------
768 <%S>%s%s runs the command %S, which is "
769 (if mouse-1-tricky "(short click) " "")
772 " is remapped to <mouse-2>, which" "")
774 (describe-function-1 defn-up))
775 (unless (or (null defn-up-tricky)
776 (integerp defn-up-tricky)
777 (eq defn-up-tricky 'undefined))
780 ----------------- up-event (long click) ----------------
782 Pressing <%S>%s for longer than %d milli-seconds
783 runs the command %S, which is "
785 mouse-1-click-follows-link
787 (describe-function-1 defn-up-tricky)))))))
789 (defun describe-mode (&optional buffer)
790 "Display documentation of current major mode and minor modes.
791 A brief summary of the minor modes comes first, followed by the
792 major mode description. This is followed by detailed
793 descriptions of the minor modes, each on a separate page.
795 For this to work correctly for a minor mode, the mode's indicator
796 variable \(listed in `minor-mode-alist') must also be a function
797 whose documentation describes the minor mode."
799 (unless buffer (setq buffer (current-buffer)))
800 (help-setup-xref (list #'describe-mode buffer)
802 ;; For the sake of help-do-xref and help-xref-go-back,
803 ;; don't switch buffers before calling `help-buffer'.
804 (with-help-window (help-buffer)
805 (with-current-buffer buffer
807 ;; Older packages do not register in minor-mode-list but only in
809 (dolist (x minor-mode-alist)
811 (unless (memq x minor-mode-list)
812 (push x minor-mode-list)))
813 ;; Find enabled minor mode we will want to mention.
814 (dolist (mode minor-mode-list)
815 ;; Document a minor mode if it is listed in minor-mode-alist,
816 ;; non-nil, and has a function definition.
817 (let ((fmode (or (get mode :minor-mode-function) mode)))
818 (and (boundp mode) (symbol-value mode)
820 (let ((pretty-minor-mode
821 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
824 (substring (symbol-name fmode)
825 0 (match-beginning 0)))
827 (push (list fmode pretty-minor-mode
828 (format-mode-line (assq mode minor-mode-alist)))
832 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
834 (princ "Enabled minor modes:\n")
835 (make-local-variable 'help-button-cache)
836 (with-current-buffer standard-output
837 (dolist (mode minor-modes)
838 (let ((mode-function (nth 0 mode))
839 (pretty-minor-mode (nth 1 mode))
840 (indicator (nth 2 mode)))
841 (add-text-properties 0 (length pretty-minor-mode)
842 '(face bold) pretty-minor-mode)
844 (goto-char (point-max))
846 (push (point-marker) help-button-cache)
847 ;; Document the minor modes fully.
848 (insert pretty-minor-mode)
849 (princ (format " minor mode (%s):\n"
850 (if (zerop (length indicator))
852 (format "indicator%s"
854 (princ (documentation mode-function)))
855 (insert-button pretty-minor-mode
856 'action (car help-button-cache)
858 'help-echo "mouse-2, RET: show full information")
864 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
865 ;; Document the major mode.
866 (let ((mode mode-name))
867 (with-current-buffer standard-output
868 (let ((start (point)))
869 (insert (format-mode-line mode nil nil buffer))
870 (add-text-properties start (point) '(face bold)))))
872 (princ (documentation major-mode))))))
875 (defun describe-minor-mode (minor-mode)
876 "Display documentation of a minor mode given as MINOR-MODE.
877 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
878 appeared on the mode-line."
879 (interactive (list (completing-read
882 (describe-minor-mode-completion-table-for-symbol)
883 (describe-minor-mode-completion-table-for-indicator)
885 (if (symbolp minor-mode)
886 (setq minor-mode (symbol-name minor-mode)))
887 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
888 (indicators (describe-minor-mode-completion-table-for-indicator)))
890 ((member minor-mode symbols)
891 (describe-minor-mode-from-symbol (intern minor-mode)))
892 ((member minor-mode indicators)
893 (describe-minor-mode-from-indicator minor-mode))
895 (error "No such minor mode: %s" minor-mode)))))
898 (defun describe-minor-mode-completion-table-for-symbol ()
899 ;; In order to list up all minor modes, minor-mode-list
900 ;; is used here instead of minor-mode-alist.
901 (delq nil (mapcar 'symbol-name minor-mode-list)))
902 (defun describe-minor-mode-from-symbol (symbol)
903 "Display documentation of a minor mode given as a symbol, SYMBOL"
904 (interactive (list (intern (completing-read
905 "Minor mode symbol: "
906 (describe-minor-mode-completion-table-for-symbol)))))
908 (describe-function symbol)
909 (describe-variable symbol)))
912 (defun describe-minor-mode-completion-table-for-indicator ()
915 (let ((i (format-mode-line x)))
916 ;; remove first space if existed
925 (defun describe-minor-mode-from-indicator (indicator)
926 "Display documentation of a minor mode specified by INDICATOR.
927 If you call this function interactively, you can give indicator which
928 is currently activated with completion."
931 "Minor mode indicator: "
932 (describe-minor-mode-completion-table-for-indicator))))
933 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
935 (describe-minor-mode-from-symbol minor-mode)
936 (error "Cannot find minor mode for `%s'" indicator))))
938 (defun lookup-minor-mode-from-indicator (indicator)
939 "Return a minor mode symbol from its indicator on the modeline."
940 ;; remove first space if existed
941 (if (and (< 0 (length indicator))
942 (eq (aref indicator 0) ?\s))
943 (setq indicator (substring indicator 1)))
944 (let ((minor-modes minor-mode-alist)
947 (let* ((minor-mode (car (car minor-modes)))
948 (anindicator (format-mode-line
949 (car (cdr (car minor-modes))))))
950 ;; remove first space if existed
951 (if (and (stringp anindicator)
952 (> (length anindicator) 0)
953 (eq (aref anindicator 0) ?\s))
954 (setq anindicator (substring anindicator 1)))
955 (if (equal indicator anindicator)
956 (setq result minor-mode
958 (setq minor-modes (cdr minor-modes)))))
962 ;;; Automatic resizing of temporary buffers.
964 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
965 "Maximum height of a window displaying a temporary buffer.
966 This is effective only when Temp Buffer Resize mode is enabled.
967 The value is the maximum height (in lines) which `resize-temp-buffer-window'
968 will give to a window displaying a temporary buffer.
969 It can also be a function to be called to choose the height for such a buffer.
970 It gets one argumemt, the buffer, and should return a positive integer."
971 :type '(choice integer function)
975 (define-minor-mode temp-buffer-resize-mode
976 "Toggle the mode which makes windows smaller for temporary buffers.
977 With prefix argument ARG, turn the resizing of windows displaying temporary
978 buffers on if ARG is positive or off otherwise.
979 This makes the window the right height for its contents, but never
980 more than `temp-buffer-max-height' nor less than `window-min-height'.
981 This applies to `help', `apropos' and `completion' buffers, and some others."
982 :global t :group 'help
983 (if temp-buffer-resize-mode
984 ;; `help-make-xrefs' may add a `back' button and thus increase the
985 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
986 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
987 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
989 (defun resize-temp-buffer-window ()
990 "Resize the selected window to fit its contents.
991 Will not make it higher than `temp-buffer-max-height' nor smaller than
992 `window-min-height'. Do nothing if it is the only window on its frame, if it
993 is not as wide as the frame or if some of the window's contents are scrolled
995 (unless (or (one-window-p 'nomini)
996 (not (pos-visible-in-window-p (point-min)))
997 (not (window-full-width-p)))
998 (fit-window-to-buffer
1000 (if (functionp temp-buffer-max-height)
1001 (funcall temp-buffer-max-height (current-buffer))
1002 temp-buffer-max-height))))
1007 (defcustom help-window-select 'other
1008 "Non-nil means select help window for viewing.
1010 never (nil) Select help window only if there is no other window
1012 other Select help window unless the selected window is the
1013 only other window on its frame.
1014 always (t) Always select the help window.
1016 This option has effect if and only if the help window was created
1017 by `with-help-window'"
1018 :type '(choice (const :tag "never (nil)" nil)
1019 (const :tag "other" other)
1020 (const :tag "always (t)" t))
1024 (defun help-window-display-message (quit-part window &optional other)
1025 "Display message telling how to quit and scroll help window.
1026 QUIT-PART is a string telling how to quit the help window WINDOW.
1027 Optional argument OTHER non-nil means return text telling how to
1028 scroll the \"other\" window."
1031 ((pos-visible-in-window-p
1032 (with-current-buffer (window-buffer window)
1033 (point-max)) window)
1034 ;; Buffer end is visible.
1036 (other ", \\[scroll-other-window] to scroll help.")
1037 (t ", \\[scroll-up] to scroll help."))))
1039 (substitute-command-keys (concat quit-part scroll-part)))))
1041 (defun help-window-setup-finish (window &optional reuse keep-frame)
1042 "Finish setting up help window WINDOW.
1043 Select WINDOW according to the value of `help-window-select'.
1044 Display message telling how to scroll and eventually quit WINDOW.
1046 Optional argument REUSE non-nil means WINDOW has been reused \(by
1047 `display-buffer'\) for displaying help. Optional argument
1048 KEEP-FRAME non-nil means that quitting must no delete the frame
1050 (let ((number-of-windows
1051 (length (window-list (window-frame window) 'no-mini window))))
1053 ((eq window (selected-window))
1054 ;; The help window is the selected window, probably the
1055 ;; `pop-up-windows' nil case.
1056 (help-window-display-message
1058 "Type \"q\" to restore this window"
1059 ;; This should not be taken.
1060 "Type \"q\" to quit") window))
1061 ((= number-of-windows 1)
1062 ;; The help window is alone on a frame and not the selected
1063 ;; window, could be the `pop-up-frames' t case.
1064 (help-window-display-message
1066 (keep-frame "Type \"q\" to delete this window")
1067 (reuse "Type \"q\" to restore this window")
1068 (view-remove-frame-by-deleting "Type \"q\" to delete this frame")
1069 (t "Type \"q\" to iconify this frame"))
1071 ((and (= number-of-windows 2)
1072 (eq (window-frame window) (window-frame (selected-window))))
1073 ;; There are two windows on the help window's frame and the other
1074 ;; window is the selected one.
1075 (if (memq help-window-select '(nil other))
1076 ;; Do not select the help window.
1077 (help-window-display-message
1079 ;; Offer `display-buffer' for consistency with
1080 ;; `print-help-return-message'. This is hardly TRT when
1081 ;; the other window and the selected window display the
1082 ;; same buffer but has been handled this way ever since.
1083 "Type \\[display-buffer] RET to restore the other window"
1084 ;; The classic "two windows" configuration.
1085 "Type \\[delete-other-windows] to delete the help window")
1087 ;; Select help window and tell how to quit.
1088 (select-window window)
1089 (help-window-display-message
1091 "Type \"q\" to restore this window"
1092 "Type \"q\" to delete this window") window)))
1094 ;; Issuing a message with 3 or more windows on the same frame
1095 ;; without selecting the help window doesn't make any sense.
1096 (select-window window)
1097 (help-window-display-message
1099 "Type \"q\" to restore this window"
1100 "Type \"q\" to delete this window") window)))))
1102 (defun help-window-setup (list-of-frames list-of-window-tuples)
1103 "Set up help window.
1104 LIST-OF-FRAMES and LIST-OF-WINDOW-TUPLES are the lists of frames
1105 and window quadruples built by `with-help-window'. The help
1106 window itself is specified by the variable `help-window'."
1107 (let* ((help-buffer (window-buffer help-window))
1108 ;; `help-buffer' now denotes the help window's buffer.
1111 (buffer-local-value 'view-return-to-alist help-buffer)))
1112 (help-entry (assq help-window list-of-window-tuples)))
1114 ;; Handle `help-window-point-marker'.
1115 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1116 (set-window-point help-window help-window-point-marker)
1117 ;; Reset `help-window-point-marker'.
1118 (set-marker help-window-point-marker nil))
1122 ;; `view-return-to-alist' has an entry for the help window.
1124 ((eq help-window (selected-window))
1125 ;; The help window is the selected window, probably because the
1126 ;; user followed a backward/forward button or a cross reference.
1127 ;; In this case just purge stale entries from
1128 ;; `view-return-to-alist' but leave the entry alone and don't
1129 ;; display a message.
1130 (view-return-to-alist-update help-buffer))
1131 ((and help-entry (eq (cadr help-entry) help-buffer))
1132 ;; The help window was not selected but displayed the help
1133 ;; buffer. In this case reuse existing exit information but try
1134 ;; to get back to the selected window when quitting. Don't
1135 ;; display a message since the user must have seen one before.
1136 (view-return-to-alist-update
1137 help-buffer (cons help-window
1138 (cons (selected-window) (cddr view-entry)))))
1140 ;; The help window was not selected, did display the help buffer
1141 ;; earlier, but displayed another buffer when help was invoked.
1142 ;; Set up things so that quitting will show that buffer again.
1143 (view-return-to-alist-update
1144 help-buffer (cons help-window
1145 (cons (selected-window) (cdr help-entry))))
1146 (help-window-setup-finish help-window t))
1148 ;; The help window is new but `view-return-to-alist' had an
1149 ;; entry for it. This should never happen.
1150 (view-return-to-alist-update
1151 help-buffer (cons help-window
1152 (cons (selected-window) 'quit-window)))
1153 (help-window-setup-finish help-window t))))
1155 ;; `view-return-to-alist' does not have an entry for help window
1156 ;; but `list-of-window-tuples' does. Hence `display-buffer' must
1157 ;; have reused an existing window.
1158 (if (eq (cadr help-entry) help-buffer)
1159 ;; The help window displayed `help-buffer' before but no
1160 ;; `view-return-to-alist' entry was found probably because the
1161 ;; user manually switched to the help buffer. Set up things
1162 ;; for `quit-window' although `view-exit-action' should be
1163 ;; able to handle this case all by itself.
1165 (view-return-to-alist-update
1166 help-buffer (cons help-window
1167 (cons (selected-window) 'quit-window)))
1168 (help-window-setup-finish help-window t))
1169 ;; The help window displayed another buffer before. Set up
1170 ;; things in a way that quitting can orderly show that buffer
1171 ;; again. The window-start and window-point information from
1172 ;; `list-of-window-tuples' provide the necessary information.
1173 (view-return-to-alist-update
1174 help-buffer (cons help-window
1175 (cons (selected-window) (cdr help-entry))))
1176 (help-window-setup-finish help-window t)))
1177 ((memq (window-frame help-window) list-of-frames)
1178 ;; The help window is a new window on an existing frame. This
1179 ;; case must be handled specially by `help-window-setup-finish'
1180 ;; and `view-mode-exit' to ascertain that quitting does _not_
1181 ;; inadvertently delete the frame.
1182 (view-return-to-alist-update
1183 help-buffer (cons help-window
1184 (cons (selected-window) 'keep-frame)))
1185 (help-window-setup-finish help-window nil t))
1187 ;; The help window is shown on a new frame. In this case quitting
1188 ;; shall handle both, the help window _and_ its frame. We changed
1189 ;; the default of `view-remove-frame-by-deleting' to t in order to
1190 ;; intuitively DTRT here.
1191 (view-return-to-alist-update
1192 help-buffer (cons help-window (cons (selected-window) t)))
1193 (help-window-setup-finish help-window)))))
1195 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1196 ;; providing the following additional twists:
1198 ;; (1) Issue more accurate messages telling how to scroll and quit the
1201 ;; (2) Make `view-mode-exit' DTRT in more cases.
1203 ;; (3) An option (customizable via `help-window-select') to select the
1204 ;; help window automatically.
1206 ;; (4) A marker (`help-window-point-marker') to move point in the help
1207 ;; window to an arbitrary buffer position.
1209 ;; Note: It's usually always wrong to use `print-help-return-message' in
1210 ;; the body of `with-help-window'.
1211 (defmacro with-help-window (buffer-name &rest body)
1212 "Display buffer BUFFER-NAME in a help window evaluating BODY.
1213 Select help window if the actual value of the user option
1214 `help-window-select' says so."
1215 (declare (indent 1) (debug t))
1216 ;; Bind list-of-frames to `frame-list' and list-of-window-tuples to a
1217 ;; list of one <window window-buffer window-start window-point> tuple
1218 ;; for each live window.
1219 `(let ((list-of-frames (frame-list))
1220 (list-of-window-tuples
1224 (push (list window (window-buffer window)
1225 (window-start window) (window-point window))
1229 ;; We set `help-window' to t in order to trigger `help-mode-finish'
1230 ;; to set `help-window' to the actual help window.
1231 (setq help-window t)
1232 ;; Make `help-window-point-marker' point nowhere (the only place
1233 ;; where this should be set to a buffer position is within BODY).
1234 (set-marker help-window-point-marker nil)
1236 (with-output-to-temp-buffer ,buffer-name
1239 (when (windowp help-window)
1240 ;; Set up help window.
1241 (help-window-setup list-of-frames list-of-window-tuples))
1243 ;; Reset `help-window' to nil to avoid confusing future calls of
1244 ;; `help-mode-finish' by "plain" `with-output-to-temp-buffer'.
1245 (setq help-window nil)))
1249 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1250 ;;; help.el ends here