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" 'describe-distribution)
72 (define-key map "\C-e" 'view-emacs-problems)
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-p" 'describe-project)
77 (define-key map "\C-t" 'view-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 "h" 'view-hello-file)
98 (define-key map "i" 'info)
99 (define-key map "4i" 'info-other-window)
101 (define-key map "k" 'describe-key)
102 (define-key map "l" 'view-lossage)
103 (define-key map "m" 'describe-mode)
104 (define-key map "n" 'view-emacs-news)
105 (define-key map "p" 'finder-by-keyword)
106 (define-key map "r" 'info-emacs-manual)
107 (define-key map "s" 'describe-syntax)
108 (define-key map "t" 'help-with-tutorial)
109 (define-key map "w" 'where-is)
110 (define-key map "v" 'describe-variable)
111 (define-key map "q" 'help-quit)
113 "Keymap for characters following the Help key.")
115 (define-key global-map (char-to-string help-char) 'help-command)
116 (define-key global-map [help] 'help-command
)
117 (define-key global-map
[f1] 'help-command)
118 (fset 'help-command help-map)
120 (autoload 'finder-by-keyword "finder"
121 "Find packages matching a given keyword." t)
123 ;; insert-button makes the action nil if it is not store somewhere
124 (defvar help-button-cache nil)
128 "Just exit from the Help command's command loop."
132 (defvar help-return-method nil
133 "What to do to \"exit\" the help buffer.
135 (WINDOW . t) delete the selected window (and possibly its frame,
136 see `quit-window' and `View-quit'), go to WINDOW.
137 (WINDOW . quit-window) do quit-window, then select WINDOW.
138 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
140 (defun print-help-return-message (&optional function)
141 "Display or return message saying how to restore windows after help command.
142 This function assumes that `standard-output' is the help buffer.
143 It computes a message, and applies the optional argument FUNCTION to it.
144 If FUNCTION is nil, it applies `message', thus displaying the message.
145 In addition, this function sets up `help-return-method', which see, that
146 specifies what to do when the user exits the help buffer."
147 ;; Reset `help-window' here to avoid confusing `help-mode-finish'.
148 (setq help-window nil)
149 (and (not (get-buffer-window standard-output))
153 (special-display-p (buffer-name standard-output)))
154 (setq help-return-method (cons (selected-window) t))
155 ;; If the help output buffer is a special display buffer,
156 ;; don't say anything about how to get rid of it.
157 ;; First of all, the user will do that with the window
158 ;; manager, not with Emacs.
159 ;; Secondly, the buffer has not been displayed yet,
160 ;; so we don't know whether its frame will be selected.
162 (display-buffer-reuse-frames
163 (setq help-return-method (cons (selected-window)
166 ((not (one-window-p t))
167 (setq help-return-method
168 (cons (selected-window) 'quit-window))
169 "Type \\[display-buffer] RET to restore the other window.")
171 (setq help-return-method (cons (selected-window) t))
172 "Type \\[delete-other-windows] to remove help window.")
174 (setq help-return-method
175 (list (selected-window) (window-buffer)
176 (window-start) (window-point)))
177 "Type \\[switch-to-buffer] RET to remove help window."))))
178 (funcall (or function 'message)
181 (substitute-command-keys first-message))
182 (if first-message " ")
183 ;; If the help buffer will go in a separate frame,
184 ;; it's no use mentioning a command to scroll, so don't.
185 (if (or pop-up-windows
186 (special-display-p (buffer-name standard-output)))
188 (if (same-window-p (buffer-name standard-output))
189 ;; Say how to scroll this window.
190 (substitute-command-keys
191 "\\[scroll-up] to scroll the help.")
192 ;; Say how to scroll some other window.
193 (substitute-command-keys
194 "\\[scroll-other-window] to scroll the help."))))))))
196 ;; So keyboard macro definitions are documented correctly
197 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
199 (defalias 'help 'help-for-help-internal)
200 ;; find-function can find this.
201 (defalias 'help-for-help 'help-for-help-internal)
202 ;; It can't find this, but nobody will look.
203 (make-help-screen help-for-help-internal
204 "a b c C e f F i I k C-k l L m p r s t v w C-c C-d C-f C-n C-p C-t C-w . or ? :"
205 "You have typed %THIS-KEY%, the help character. Type a Help option:
206 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
208 a command-apropos. Type a list of words or a regexp; it shows a list of
209 commands whose names match. See also the apropos command.
210 b describe-bindings. Display a table of all key bindings.
211 c describe-key-briefly. Type a key sequence;
212 it displays the command name run by that key sequence.
213 C describe-coding-system. Type the name of the coding system to describe,
214 or just RET to describe the ones currently in use.
215 d apropos-documentation. Type a pattern (a list of words or a regexp), and
216 it shows a list of functions, variables, and other items whose
217 documentation matches that pattern. See also the apropos command.
218 e view-echo-area-messages. Go to the buffer that logs echo-area messages.
219 f describe-function. Type a function name and you see its documentation.
220 F Info-goto-emacs-command-node. Type a command name;
221 it goes to the on-line manual's section that describes the command.
222 h Display the HELLO file which illustrates various scripts.
223 i info. The Info documentation reader: read on-line manuals.
224 I describe-input-method. Describe a specific input method (if you type
225 its name) or the current input method (if you type just RET).
226 k describe-key. Type a key sequence;
227 it displays the full documentation for that key sequence.
228 K Info-goto-emacs-key-command-node. Type a key sequence;
229 it goes to the on-line manual's section that describes
230 the command bound to that key.
231 l view-lossage. Show last 100 characters you typed.
232 L describe-language-environment. This describes either a
233 specific language environment (if you type its name)
234 or the current language environment (if you type just RET).
235 m describe-mode. Display documentation of current minor modes,
236 and the current major mode, including their special commands.
237 n view-emacs-news. Display news of recent Emacs changes.
238 p finder-by-keyword. Find packages matching a given topic keyword.
239 r info-emacs-manual. Display the Emacs manual in Info mode.
240 s describe-syntax. Display contents of syntax table, plus explanations.
241 S info-lookup-symbol. Type a symbol; it goes to that symbol in the
242 on-line manual for the programming language used in this buffer.
243 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
244 v describe-variable. Type name of a variable;
245 it displays the variable's documentation and value.
246 w where-is. Type a command name; it displays which keystrokes
248 . display-local-help. Display any available local help at point
251 C-a Display information about Emacs.
252 C-c Display Emacs copying permission (GNU General Public License).
253 C-d Display Emacs ordering information.
254 C-e Display info about Emacs problems.
255 C-f Display the Emacs FAQ.
256 C-m Display how to order printed Emacs manuals.
257 C-n Display news of recent Emacs changes.
258 C-p Display information about the GNU project.
259 C-t Display the Emacs TODO list.
260 C-w Display information on absence of warranty for GNU Emacs."
265 (defun function-called-at-point ()
266 "Return a function around point or else called by the list containing point.
267 If that doesn't give a function, return nil."
268 (or (with-syntax-table emacs-lisp-mode-syntax-table
269 (or (condition-case ()
271 (or (not (zerop (skip-syntax-backward "_w")))
272 (eq (char-syntax (following-char)) ?w)
273 (eq (char-syntax (following-char)) ?_)
275 (skip-chars-forward "'")
276 (let ((obj (read (current-buffer))))
277 (and (symbolp obj) (fboundp obj) obj)))
282 (narrow-to-region (max (point-min)
283 (- (point) 1000)) (point-max))
284 ;; Move up to surrounding paren, then after the open.
287 ;; If there is space here, this is probably something
288 ;; other than a real Lisp function call, so ignore it.
289 (if (looking-at "[ \t]")
290 (error "Probably not a Lisp function call"))
291 (let ((obj (read (current-buffer))))
292 (and (symbolp obj) (fboundp obj) obj))))
294 (let* ((str (find-tag-default))
295 (sym (if str (intern-soft str))))
296 (if (and sym (fboundp sym))
299 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
300 (setq sym (intern-soft (match-string 1 str)))
301 (and (fboundp sym) sym)))))))
304 ;;; `User' help functions
306 (defun describe-distribution ()
307 "Display info on how to obtain the latest version of GNU Emacs."
309 (view-file (expand-file-name "DISTRIB" data-directory)))
311 (defun describe-copying ()
312 "Display info on how you may redistribute copies of GNU Emacs."
314 (view-file (expand-file-name "COPYING" data-directory))
315 (goto-char (point-min)))
317 (defun describe-project ()
318 "Display info on the GNU project."
320 (view-file (expand-file-name "THE-GNU-PROJECT" data-directory))
321 (goto-char (point-min)))
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 "NO WARRANTY")
331 (defun describe-prefix-bindings ()
332 "Describe the bindings of the prefix used to reach this command.
333 The prefix described consists of all but the last event
334 of the key sequence that ran this command."
336 (let ((key (this-command-keys)))
339 (substring key 0 (1- (length key)))
340 (let ((prefix (make-vector (1- (length key)) nil))
342 (while (< i (length prefix))
343 (aset prefix i (aref key i))
346 ;; Make C-h after a prefix, when not specifically bound,
347 ;; run describe-prefix-bindings.
348 (setq prefix-help-command 'describe-prefix-bindings)
350 (defun view-emacs-news (&optional version)
351 "Display info on recent changes to Emacs.
352 With argument, display info only for the selected version."
355 (setq version emacs-major-version))
356 (when (consp version)
362 (insert-file-contents
363 (expand-file-name file data-directory))
364 (while (re-search-forward
365 (if (member file '("NEWS.18" "NEWS.1-17"))
366 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
367 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
368 (setq res (cons (match-string-no-properties 1) res)))))
370 (directory-files data-directory nil
371 "^NEWS\\.[0-9][-0-9]*$" nil)))
372 (sort (delete-dups res) (lambda (a b) (string< b a)))))
373 (current (car all-versions)))
374 (setq version (completing-read
375 (format "Read NEWS for the version (default %s): " current)
376 all-versions nil nil nil nil current))
377 (if (integerp (string-to-number version))
378 (setq version (string-to-number version))
379 (unless (or (member version all-versions)
380 (<= (string-to-number version) (string-to-number current)))
381 (error "No news about version %s" version)))))
382 (when (integerp version)
383 (cond ((<= version 12)
384 (setq version (format "1.%d" version)))
386 (setq version (format "%d" version)))
387 ((> version emacs-major-version)
388 (error "No news about Emacs %d (yet)" version))))
389 (let* ((vn (if (stringp version)
390 (string-to-number version)
393 ((>= vn emacs-major-version) "NEWS")
394 ((< vn 18) "NEWS.1-17")
395 (t (format "NEWS.%d" vn))))
397 (view-file (expand-file-name file data-directory))
399 (goto-char (point-min))
400 (when (stringp version)
401 (when (re-search-forward
402 (concat (if (< vn 19)
403 "Changes in Emacs[ \t]*"
404 "^\* [^0-9\n]*") version "$")
410 (while (and (setq res
413 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
414 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
415 (equal (match-string-no-properties 1) version)))
416 (or res (goto-char (point-max)))
421 (defun view-todo (&optional arg)
422 "Display the Emacs TODO list."
424 (view-file (expand-file-name "TODO" data-directory)))
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-file (expand-file-name "ORDERS" data-directory))
439 (defun view-emacs-FAQ ()
440 "Display the Emacs Frequently Asked Questions (FAQ) file."
442 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
445 (defun view-emacs-problems ()
446 "Display info on known problems with Emacs and possible workarounds."
448 (view-file (expand-file-name "PROBLEMS" data-directory)))
450 (defun view-lossage ()
451 "Display last 100 input keystrokes.
453 To record all your input on a file, use `open-dribble-file'."
455 (help-setup-xref (list #'view-lossage) (interactive-p))
456 (with-help-window (help-buffer)
457 (princ (mapconcat (lambda (key)
458 (if (or (integerp key) (symbolp key) (listp key))
459 (single-key-description key)
460 (prin1-to-string key nil)))
463 (with-current-buffer standard-output
464 (goto-char (point-min))
465 (while (progn (move-to-column 50) (not (eobp)))
466 (when (search-forward " " nil t)
469 ;; jidanni wants to see the last keystrokes immediately.
470 (set-marker help-window-point-marker (point)))))
475 (defun describe-bindings (&optional prefix buffer)
476 "Show a list of all defined keys, and their definitions.
477 We put that list in a buffer, and display the buffer.
479 The optional argument PREFIX, if non-nil, should be a key sequence;
480 then we display only bindings that start with that prefix.
481 The optional argument BUFFER specifies which buffer's bindings
482 to display (default, the current buffer). BUFFER can be a buffer
485 (or buffer (setq buffer (current-buffer)))
486 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
487 (with-current-buffer buffer
488 (describe-bindings-internal nil prefix)))
490 ;; This function used to be in keymap.c.
491 (defun describe-bindings-internal (&optional menus prefix)
492 "Show a list of all defined keys, and their definitions.
493 We put that list in a buffer, and display the buffer.
495 The optional argument MENUS, if non-nil, says to mention menu bindings.
496 \(Ordinarily these are omitted from the output.)
497 The optional argument PREFIX, if non-nil, should be a key sequence;
498 then we display only bindings that start with that prefix."
500 (let ((buf (current-buffer)))
501 (with-help-window "*Help*"
502 (with-current-buffer standard-output
503 (describe-buffer-bindings buf prefix menus)))))
505 (defun where-is (definition &optional insert)
506 "Print message listing key sequences that invoke the command DEFINITION.
507 Argument is a command definition, usually a symbol with a function definition.
508 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
510 (let ((fn (function-called-at-point))
511 (enable-recursive-minibuffers t)
513 (setq val (completing-read
515 (format "Where is command (default %s): " fn)
516 "Where is command: ")
517 obarray 'commandp t))
518 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
519 (unless definition (error "No command"))
520 (let ((func (indirect-function definition))
522 (standard-output (if insert (current-buffer) t)))
523 ;; In DEFS, find all symbols that are aliases for DEFINITION.
524 (mapatoms (lambda (symbol)
525 (and (fboundp symbol)
526 (not (eq symbol definition))
527 (eq func (condition-case ()
528 (indirect-function symbol)
530 (push symbol defs))))
531 ;; Look at all the symbols--first DEFINITION,
533 (dolist (symbol (cons definition defs))
534 (let* ((remapped (command-remapping symbol))
535 (keys (where-is-internal
536 symbol overriding-local-map nil nil remapped))
537 (keys (mapconcat 'key-description keys ", "))
541 (if (> (length keys) 0)
543 (format "%s (%s) (remapped from %s)"
544 keys remapped symbol)
545 (format "%s (%s)" keys symbol))
546 (format "M-x %s RET" symbol))
547 (if (> (length keys) 0)
549 (format "%s is remapped to %s which is on %s"
550 symbol remapped keys)
551 (format "%s is on %s" symbol keys))
552 ;; If this is the command the user asked about,
553 ;; and it is not on any key, say so.
554 ;; For other symbols, its aliases, say nothing
555 ;; about them unless they are on keys.
556 (if (eq symbol definition)
557 (format "%s is not on any key" symbol)))))
559 (unless (eq symbol definition)
560 (princ ";\n its alias "))
564 (defun help-key-description (key untranslated)
565 (let ((string (key-description key)))
566 (if (or (not untranslated)
567 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
569 (let ((otherstring (key-description untranslated)))
570 (if (equal string otherstring)
572 (format "%s (translated from %s)" string otherstring))))))
574 (defun describe-key-briefly (&optional key insert untranslated)
575 "Print the name of the function KEY invokes. KEY is a string.
576 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
577 If non-nil, UNTRANSLATED is a vector of the untranslated events.
578 It can also be a number in which case the untranslated events from
579 the last key hit are used.
581 If KEY is a menu item or a tool-bar button that is disabled, this command
582 temporarily enables it to allow getting help on disabled items and buttons."
584 (let ((enable-disabled-menus-and-buttons t)
585 (cursor-in-echo-area t)
589 ;; If yank-menu is empty, populate it temporarily, so that
590 ;; "Select and Paste" menu can generate a complete event.
591 (when (null (cdr yank-menu))
592 (setq saved-yank-menu (copy-sequence yank-menu))
593 (menu-bar-update-yank-menu "(any string)" nil))
594 (setq key (read-key-sequence "Describe key (or click or menu item): "))
595 ;; If KEY is a down-event, read and discard the
596 ;; corresponding up-event. Note that there are also
597 ;; down-events on scroll bars and mode lines: the actual
598 ;; event then is in the second element of the vector.
600 (let ((last-idx (1- (length key))))
601 (and (eventp (aref key last-idx))
602 (memq 'down (event-modifiers (aref key last-idx)))))
606 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
608 ;; Put yank-menu back as it was, if we changed it.
609 (when saved-yank-menu
610 (setq yank-menu (copy-sequence saved-yank-menu))
611 (fset 'yank-menu (cons 'keymap yank-menu))))))
612 (if (numberp untranslated)
613 (setq untranslated (this-single-command-raw-keys)))
614 (let* ((event (if (and (symbolp (aref key 0))
616 (consp (aref key 1)))
619 (modifiers (event-modifiers event))
620 (standard-output (if insert (current-buffer) t))
621 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
622 (memq 'drag modifiers)) " at that spot" ""))
623 (defn (key-binding key t))
625 ;; Handle the case where we faked an entry in "Select and Paste" menu.
626 (if (and (eq defn nil)
627 (stringp (aref key (1- (length key))))
628 (eq (key-binding (substring key 0 -1)) 'yank-menu))
629 (setq defn 'menu-bar-select-yank))
630 ;; Don't bother user with strings from (e.g.) the select-paste menu.
631 (if (stringp (aref key (1- (length key))))
632 (aset key (1- (length key)) "(any string)"))
633 (if (and (> (length untranslated) 0)
634 (stringp (aref untranslated (1- (length untranslated)))))
635 (aset untranslated (1- (length untranslated)) "(any string)"))
636 ;; Now describe the key, perhaps as changed.
637 (setq key-desc (help-key-description key untranslated))
638 (if (or (null defn) (integerp defn) (equal defn 'undefined))
639 (princ (format "%s%s is undefined" key-desc mouse-msg))
640 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
642 (defun describe-key (&optional key untranslated up-event)
643 "Display documentation of the function invoked by KEY.
644 KEY can be any kind of a key sequence; it can include keyboard events,
645 mouse events, and/or menu events. When calling from a program,
646 pass KEY as a string or a vector.
648 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
649 It can also be a number, in which case the untranslated events from
650 the last key sequence entered are used.
651 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
653 If KEY is a menu item or a tool-bar button that is disabled, this command
654 temporarily enables it to allow getting help on disabled items and buttons."
656 (let ((enable-disabled-menus-and-buttons t)
657 (cursor-in-echo-area t)
661 ;; If yank-menu is empty, populate it temporarily, so that
662 ;; "Select and Paste" menu can generate a complete event.
663 (when (null (cdr yank-menu))
664 (setq saved-yank-menu (copy-sequence yank-menu))
665 (menu-bar-update-yank-menu "(any string)" nil))
666 (setq key (read-key-sequence "Describe key (or click or menu item): "))
669 (prefix-numeric-value current-prefix-arg)
670 ;; If KEY is a down-event, read and include the
671 ;; corresponding up-event. Note that there are also
672 ;; down-events on scroll bars and mode lines: the actual
673 ;; event then is in the second element of the vector.
675 (let ((last-idx (1- (length key))))
676 (and (eventp (aref key last-idx))
677 (memq 'down (event-modifiers (aref key last-idx)))))
678 (or (and (eventp (aref key 0))
679 (memq 'down (event-modifiers (aref key 0)))
680 ;; However, for the C-down-mouse-2 popup
681 ;; menu, there is no subsequent up-event. In
682 ;; this case, the up-event is the next
683 ;; element in the supplied vector.
685 (and (> (length key) 1)
686 (eventp (aref key 1))
687 (memq 'down (event-modifiers (aref key 1)))))
689 ;; Put yank-menu back as it was, if we changed it.
690 (when saved-yank-menu
691 (setq yank-menu (copy-sequence saved-yank-menu))
692 (fset 'yank-menu (cons 'keymap yank-menu))))))
693 (if (numberp untranslated)
694 (setq untranslated (this-single-command-raw-keys)))
695 (let* ((event (aref key (if (and (symbolp (aref key 0))
697 (consp (aref key 1)))
700 (modifiers (event-modifiers event))
701 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
702 (memq 'drag modifiers)) " at that spot" ""))
703 (defn (key-binding key t))
704 defn-up defn-up-tricky ev-type
705 mouse-1-remapped mouse-1-tricky)
707 ;; Handle the case where we faked an entry in "Select and Paste" menu.
708 (when (and (eq defn nil)
709 (stringp (aref key (1- (length key))))
710 (eq (key-binding (substring key 0 -1)) 'yank-menu))
711 (setq defn 'menu-bar-select-yank))
712 (if (or (null defn) (integerp defn) (equal defn 'undefined))
713 (message "%s%s is undefined"
714 (help-key-description key untranslated) mouse-msg)
715 (help-setup-xref (list #'describe-function defn) (interactive-p))
716 ;; Don't bother user with strings from (e.g.) the select-paste menu.
717 (when (stringp (aref key (1- (length key))))
718 (aset key (1- (length key)) "(any string)"))
719 (when (and untranslated
720 (stringp (aref untranslated (1- (length untranslated)))))
721 (aset untranslated (1- (length untranslated))
723 ;; Need to do this before erasing *Help* buffer in case event
724 ;; is a mouse click in an existing *Help* buffer.
726 (setq ev-type (event-basic-type up-event))
727 (let ((sequence (vector up-event)))
728 (when (and (eq ev-type 'mouse-1)
729 mouse-1-click-follows-link
730 (not (eq mouse-1-click-follows-link 'double))
731 (setq mouse-1-remapped
732 (mouse-on-link-p (event-start up-event))))
733 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
734 (> mouse-1-click-follows-link 0)))
735 (cond ((stringp mouse-1-remapped)
736 (setq sequence mouse-1-remapped))
737 ((vectorp mouse-1-remapped)
738 (setcar up-event (elt mouse-1-remapped 0)))
739 (t (setcar up-event 'mouse-2))))
740 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
742 (setq sequence (vector up-event))
743 (aset sequence 0 'mouse-1)
744 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
745 (with-help-window (help-buffer)
746 (princ (help-key-description key untranslated))
748 %s runs the command %S
751 (describe-function-1 defn)
753 (unless (or (null defn-up)
755 (equal defn-up 'undefined))
758 ----------------- up-event %s----------------
760 <%S>%s%s runs the command %S
762 (if mouse-1-tricky "(short click) " "")
765 " is remapped to <mouse-2>\nwhich" "")
767 (describe-function-1 defn-up))
768 (unless (or (null defn-up-tricky)
769 (integerp defn-up-tricky)
770 (eq defn-up-tricky 'undefined))
773 ----------------- up-event (long click) ----------------
775 Pressing <%S>%s for longer than %d milli-seconds
779 mouse-1-click-follows-link
781 (describe-function-1 defn-up-tricky)))))))
783 (defun describe-mode (&optional buffer)
784 "Display documentation of current major mode and minor modes.
785 A brief summary of the minor modes comes first, followed by the
786 major mode description. This is followed by detailed
787 descriptions of the minor modes, each on a separate page.
789 For this to work correctly for a minor mode, the mode's indicator
790 variable \(listed in `minor-mode-alist') must also be a function
791 whose documentation describes the minor mode."
793 (unless buffer (setq buffer (current-buffer)))
794 (help-setup-xref (list #'describe-mode buffer)
796 ;; For the sake of help-do-xref and help-xref-go-back,
797 ;; don't switch buffers before calling `help-buffer'.
798 (with-help-window (help-buffer)
799 (with-current-buffer buffer
801 ;; Older packages do not register in minor-mode-list but only in
803 (dolist (x minor-mode-alist)
805 (unless (memq x minor-mode-list)
806 (push x minor-mode-list)))
807 ;; Find enabled minor mode we will want to mention.
808 (dolist (mode minor-mode-list)
809 ;; Document a minor mode if it is listed in minor-mode-alist,
810 ;; non-nil, and has a function definition.
811 (let ((fmode (or (get mode :minor-mode-function) mode)))
812 (and (boundp mode) (symbol-value mode)
814 (let ((pretty-minor-mode
815 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
818 (substring (symbol-name fmode)
819 0 (match-beginning 0)))
821 (push (list fmode pretty-minor-mode
822 (format-mode-line (assq mode minor-mode-alist)))
826 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
828 (princ "Enabled minor modes:\n")
829 (make-local-variable 'help-button-cache)
830 (with-current-buffer standard-output
831 (dolist (mode minor-modes)
832 (let ((mode-function (nth 0 mode))
833 (pretty-minor-mode (nth 1 mode))
834 (indicator (nth 2 mode)))
835 (add-text-properties 0 (length pretty-minor-mode)
836 '(face bold) pretty-minor-mode)
838 (goto-char (point-max))
840 (push (point-marker) help-button-cache)
841 ;; Document the minor modes fully.
842 (insert pretty-minor-mode)
843 (princ (format " minor mode (%s):\n"
844 (if (zerop (length indicator))
846 (format "indicator%s"
848 (princ (documentation mode-function)))
849 (insert-button pretty-minor-mode
850 'action (car help-button-cache)
852 'help-echo "mouse-2, RET: show full information")
858 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
859 ;; Document the major mode.
860 (let ((mode mode-name))
861 (with-current-buffer standard-output
862 (let ((start (point)))
863 (insert (format-mode-line mode nil nil buffer))
864 (add-text-properties start (point) '(face bold)))))
866 (princ (documentation major-mode))))))
869 (defun describe-minor-mode (minor-mode)
870 "Display documentation of a minor mode given as MINOR-MODE.
871 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
872 appeared on the mode-line."
873 (interactive (list (completing-read
876 (describe-minor-mode-completion-table-for-symbol)
877 (describe-minor-mode-completion-table-for-indicator)
879 (if (symbolp minor-mode)
880 (setq minor-mode (symbol-name minor-mode)))
881 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
882 (indicators (describe-minor-mode-completion-table-for-indicator)))
884 ((member minor-mode symbols)
885 (describe-minor-mode-from-symbol (intern minor-mode)))
886 ((member minor-mode indicators)
887 (describe-minor-mode-from-indicator minor-mode))
889 (error "No such minor mode: %s" minor-mode)))))
892 (defun describe-minor-mode-completion-table-for-symbol ()
893 ;; In order to list up all minor modes, minor-mode-list
894 ;; is used here instead of minor-mode-alist.
895 (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
919 (defun describe-minor-mode-from-indicator (indicator)
920 "Display documentation of a minor mode specified by INDICATOR.
921 If you call this function interactively, you can give indicator which
922 is currently activated with completion."
925 "Minor mode indicator: "
926 (describe-minor-mode-completion-table-for-indicator))))
927 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
929 (describe-minor-mode-from-symbol minor-mode)
930 (error "Cannot find minor mode for `%s'" indicator))))
932 (defun lookup-minor-mode-from-indicator (indicator)
933 "Return a minor mode symbol from its indicator on the modeline."
934 ;; remove first space if existed
935 (if (and (< 0 (length indicator))
936 (eq (aref indicator 0) ?\s))
937 (setq indicator (substring indicator 1)))
938 (let ((minor-modes minor-mode-alist)
941 (let* ((minor-mode (car (car minor-modes)))
942 (anindicator (format-mode-line
943 (car (cdr (car minor-modes))))))
944 ;; remove first space if existed
945 (if (and (stringp anindicator)
946 (> (length anindicator) 0)
947 (eq (aref anindicator 0) ?\s))
948 (setq anindicator (substring anindicator 1)))
949 (if (equal indicator anindicator)
950 (setq result minor-mode
952 (setq minor-modes (cdr minor-modes)))))
956 ;;; Automatic resizing of temporary buffers.
958 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
959 "Maximum height of a window displaying a temporary buffer.
960 This is effective only when Temp Buffer Resize mode is enabled.
961 The value is the maximum height (in lines) which `resize-temp-buffer-window'
962 will give to a window displaying a temporary buffer.
963 It can also be a function to be called to choose the height for such a buffer.
964 It gets one argumemt, the buffer, and should return a positive integer."
965 :type '(choice integer function)
969 (define-minor-mode temp-buffer-resize-mode
970 "Toggle the mode which makes windows smaller for temporary buffers.
971 With prefix argument ARG, turn the resizing of windows displaying temporary
972 buffers on if ARG is positive or off otherwise.
973 This makes the window the right height for its contents, but never
974 more than `temp-buffer-max-height' nor less than `window-min-height'.
975 This applies to `help', `apropos' and `completion' buffers, and some others."
976 :global t :group 'help
977 (if temp-buffer-resize-mode
978 ;; `help-make-xrefs' may add a `back' button and thus increase the
979 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
980 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
981 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
983 (defun resize-temp-buffer-window ()
984 "Resize the selected window to fit its contents.
985 Will not make it higher than `temp-buffer-max-height' nor smaller than
986 `window-min-height'. Do nothing if it is the only window on its frame, if it
987 is not as wide as the frame or if some of the window's contents are scrolled
989 (unless (or (one-window-p 'nomini)
990 (not (pos-visible-in-window-p (point-min)))
991 (not (window-full-width-p)))
992 (fit-window-to-buffer
994 (if (functionp temp-buffer-max-height)
995 (funcall temp-buffer-max-height (current-buffer))
996 temp-buffer-max-height))))
1001 (defcustom help-window-select 'other
1002 "Non-nil means select help window for viewing.
1004 never (nil) Select help window only if there is no other window
1006 other Select help window unless the selected window is the
1007 only other window on its frame.
1008 always (t) Always select the help window.
1010 This option has effect if and only if the help window was created
1011 by `with-help-window'"
1012 :type '(choice (const :tag "never (nil)" nil)
1013 (const :tag "other" other)
1014 (const :tag "always (t)" t))
1018 (defun help-window-display-message (quit-part window &optional other)
1019 "Display message telling how to quit and scroll help window.
1020 QUIT-PART is a string telling how to quit the help window WINDOW.
1021 Optional argument OTHER non-nil means return text telling how to
1022 scroll the \"other\" window."
1025 ((pos-visible-in-window-p
1026 (with-current-buffer (window-buffer window)
1027 (point-max)) window)
1028 ;; Buffer end is visible.
1030 (other ", \\[scroll-other-window] to scroll help.")
1031 (t ", \\[scroll-up] to scroll help."))))
1033 (substitute-command-keys (concat quit-part scroll-part)))))
1035 (defun help-window-setup-finish (window &optional reuse keep-frame)
1036 "Finish setting up help window WINDOW.
1037 Select WINDOW according to the value of `help-window-select'.
1038 Display message telling how to scroll and eventually quit WINDOW.
1040 Optional argument REUSE non-nil means WINDOW has been reused \(by
1041 `display-buffer'\) for displaying help. Optional argument
1042 KEEP-FRAME non-nil means that quitting must no delete the frame
1044 (let ((number-of-windows
1045 (length (window-list (window-frame window) 'no-mini window))))
1047 ((eq window (selected-window))
1048 ;; The help window is the selected window, probably the
1049 ;; `pop-up-windows' nil case.
1050 (help-window-display-message
1052 "Type \"q\" to restore this window"
1053 ;; This should not be taken.
1054 "Type \"q\" to quit") window))
1055 ((= number-of-windows 1)
1056 ;; The help window is alone on a frame and not the selected
1057 ;; window, could be the `pop-up-frames' t case.
1058 (help-window-display-message
1060 (keep-frame "Type \"q\" to delete this window")
1061 (reuse "Type \"q\" to restore this window")
1062 (view-remove-frame-by-deleting "Type \"q\" to delete this frame")
1063 (t "Type \"q\" to iconify this frame"))
1065 ((and (= number-of-windows 2)
1066 (eq (window-frame window) (window-frame (selected-window))))
1067 ;; There are two windows on the help window's frame and the other
1068 ;; window is the selected one.
1069 (if (memq help-window-select '(nil other))
1070 ;; Do not select the help window.
1071 (help-window-display-message
1073 ;; Offer `display-buffer' for consistency with
1074 ;; `print-help-return-message'. This is hardly TRT when
1075 ;; the other window and the selected window display the
1076 ;; same buffer but has been handled this way ever since.
1077 "Type \\[display-buffer] RET to restore the other window"
1078 ;; The classic "two windows" configuration.
1079 "Type \\[delete-other-windows] to delete the help window")
1081 ;; Select help window and tell how to quit.
1082 (select-window window)
1083 (help-window-display-message
1085 "Type \"q\" to restore this window"
1086 "Type \"q\" to delete this window") window)))
1088 ;; Issuing a message with 3 or more windows on the same frame
1089 ;; without selecting the help window doesn't make any sense.
1090 (select-window window)
1091 (help-window-display-message
1093 "Type \"q\" to restore this window"
1094 "Type \"q\" to delete this window") window)))))
1096 (defun help-window-setup (list-of-frames list-of-window-tuples)
1097 "Set up help window.
1098 LIST-OF-FRAMES and LIST-OF-WINDOW-TUPLES are the lists of frames
1099 and window quadruples built by `with-help-window'. The help
1100 window itself is specified by the variable `help-window'."
1101 (let* ((help-buffer (window-buffer help-window))
1102 ;; `help-buffer' now denotes the help window's buffer.
1105 (buffer-local-value 'view-return-to-alist help-buffer)))
1106 (help-entry (assq help-window list-of-window-tuples)))
1108 ;; Handle `help-window-point-marker'.
1109 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1110 (set-window-point help-window help-window-point-marker)
1111 ;; Reset `help-window-point-marker'.
1112 (set-marker help-window-point-marker nil))
1116 ;; `view-return-to-alist' has an entry for the help window.
1118 ((eq help-window (selected-window))
1119 ;; The help window is the selected window, probably because the
1120 ;; user followed a backward/forward button or a cross reference.
1121 ;; In this case just purge stale entries from
1122 ;; `view-return-to-alist' but leave the entry alone and don't
1123 ;; display a message.
1124 (view-return-to-alist-update help-buffer))
1125 ((and help-entry (eq (cadr help-entry) help-buffer))
1126 ;; The help window was not selected but displayed the help
1127 ;; buffer. In this case reuse existing exit information but try
1128 ;; to get back to the selected window when quitting. Don't
1129 ;; display a message since the user must have seen one before.
1130 (view-return-to-alist-update
1131 help-buffer (cons help-window
1132 (cons (selected-window) (cddr view-entry)))))
1134 ;; The help window was not selected, did display the help buffer
1135 ;; earlier, but displayed another buffer when help was invoked.
1136 ;; Set up things so that quitting will show that buffer again.
1137 (view-return-to-alist-update
1138 help-buffer (cons help-window
1139 (cons (selected-window) (cdr help-entry))))
1140 (help-window-setup-finish help-window t))
1142 ;; The help window is new but `view-return-to-alist' had an
1143 ;; entry for it. This should never happen.
1144 (view-return-to-alist-update
1145 help-buffer (cons help-window
1146 (cons (selected-window) 'quit-window)))
1147 (help-window-setup-finish help-window t))))
1149 ;; `view-return-to-alist' does not have an entry for help window
1150 ;; but `list-of-window-tuples' does. Hence `display-buffer' must
1151 ;; have reused an existing window.
1152 (if (eq (cadr help-entry) help-buffer)
1153 ;; The help window displayed `help-buffer' before but no
1154 ;; `view-return-to-alist' entry was found probably because the
1155 ;; user manually switched to the help buffer. Set up things
1156 ;; for `quit-window' although `view-exit-action' should be
1157 ;; able to handle this case all by itself.
1159 (view-return-to-alist-update
1160 help-buffer (cons help-window
1161 (cons (selected-window) 'quit-window)))
1162 (help-window-setup-finish help-window t))
1163 ;; The help window displayed another buffer before. Set up
1164 ;; things in a way that quitting can orderly show that buffer
1165 ;; again. The window-start and window-point information from
1166 ;; `list-of-window-tuples' provide the necessary information.
1167 (view-return-to-alist-update
1168 help-buffer (cons help-window
1169 (cons (selected-window) (cdr help-entry))))
1170 (help-window-setup-finish help-window t)))
1171 ((memq (window-frame help-window) list-of-frames)
1172 ;; The help window is a new window on an existing frame. This
1173 ;; case must be handled specially by `help-window-setup-finish'
1174 ;; and `view-mode-exit' to ascertain that quitting does _not_
1175 ;; inadvertently delete the frame.
1176 (view-return-to-alist-update
1177 help-buffer (cons help-window
1178 (cons (selected-window) 'keep-frame)))
1179 (help-window-setup-finish help-window nil t))
1181 ;; The help window is shown on a new frame. In this case quitting
1182 ;; shall handle both, the help window _and_ its frame. We changed
1183 ;; the default of `view-remove-frame-by-deleting' to t in order to
1184 ;; intuitively DTRT here.
1185 (view-return-to-alist-update
1186 help-buffer (cons help-window (cons (selected-window) t)))
1187 (help-window-setup-finish help-window)))))
1189 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1190 ;; providing the following additional twists:
1192 ;; (1) Issue more accurate messages telling how to scroll and quit the
1195 ;; (2) Make `view-mode-exit' DTRT in more cases.
1197 ;; (3) An option (customizable via `help-window-select') to select the
1198 ;; help window automatically.
1200 ;; (4) A marker (`help-window-point-marker') to move point in the help
1201 ;; window to an arbitrary buffer position.
1203 ;; Note: It's usually always wrong to use `print-help-return-message' in
1204 ;; the body of `with-help-window'.
1205 (defmacro with-help-window (buffer-name &rest body)
1206 "Display buffer BUFFER-NAME in a help window evaluating BODY.
1207 Select help window if the actual value of the user option
1208 `help-window-select' says so."
1209 (declare (indent 1) (debug t))
1210 ;; Bind list-of-frames to `frame-list' and list-of-window-tuples to a
1211 ;; list of one <window window-buffer window-start window-point> tuple
1212 ;; for each live window.
1213 `(let ((list-of-frames (frame-list))
1214 (list-of-window-tuples
1218 (push (list window (window-buffer window)
1219 (window-start window) (window-point window))
1223 ;; We set `help-window' to t in order to trigger `help-mode-finish'
1224 ;; to set `help-window' to the actual help window.
1225 (setq help-window t)
1226 ;; Make `help-window-point-marker' point nowhere (the only place
1227 ;; where this should be set to a buffer position is within BODY).
1228 (set-marker help-window-point-marker nil)
1230 (with-output-to-temp-buffer ,buffer-name
1233 (when (windowp help-window)
1234 ;; Set up help window.
1235 (help-window-setup list-of-frames list-of-window-tuples))
1237 ;; Reset `help-window' to nil to avoid confusing future calls of
1238 ;; `help-mode-finish' by "plain" `with-output-to-temp-buffer'.
1239 (setq help-window nil)))
1243 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1244 ;;; help.el ends here