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, which is "
750 (describe-function-1 defn)
752 (unless (or (null defn-up)
754 (equal defn-up 'undefined))
757 ----------------- up-event %s----------------
759 <%S>%s%s runs the command %S, which is "
760 (if mouse-1-tricky "(short click) " "")
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)
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))))))
866 (defun describe-minor-mode (minor-mode)
867 "Display documentation of a minor mode given as MINOR-MODE.
868 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
869 appeared on the mode-line."
870 (interactive (list (completing-read
873 (describe-minor-mode-completion-table-for-symbol)
874 (describe-minor-mode-completion-table-for-indicator)
876 (if (symbolp minor-mode)
877 (setq minor-mode (symbol-name minor-mode)))
878 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
879 (indicators (describe-minor-mode-completion-table-for-indicator)))
881 ((member minor-mode symbols)
882 (describe-minor-mode-from-symbol (intern minor-mode)))
883 ((member minor-mode indicators)
884 (describe-minor-mode-from-indicator minor-mode))
886 (error "No such minor mode: %s" minor-mode)))))
889 (defun describe-minor-mode-completion-table-for-symbol ()
890 ;; In order to list up all minor modes, minor-mode-list
891 ;; is used here instead of minor-mode-alist.
892 (delq nil (mapcar 'symbol-name minor-mode-list)))
893 (defun describe-minor-mode-from-symbol (symbol)
894 "Display documentation of a minor mode given as a symbol, SYMBOL"
895 (interactive (list (intern (completing-read
896 "Minor mode symbol: "
897 (describe-minor-mode-completion-table-for-symbol)))))
899 (describe-function symbol)
900 (describe-variable symbol)))
903 (defun describe-minor-mode-completion-table-for-indicator ()
906 (let ((i (format-mode-line x)))
907 ;; remove first space if existed
916 (defun describe-minor-mode-from-indicator (indicator)
917 "Display documentation of a minor mode specified by INDICATOR.
918 If you call this function interactively, you can give indicator which
919 is currently activated with completion."
922 "Minor mode indicator: "
923 (describe-minor-mode-completion-table-for-indicator))))
924 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
926 (describe-minor-mode-from-symbol minor-mode)
927 (error "Cannot find minor mode for `%s'" indicator))))
929 (defun lookup-minor-mode-from-indicator (indicator)
930 "Return a minor mode symbol from its indicator on the modeline."
931 ;; remove first space if existed
932 (if (and (< 0 (length indicator))
933 (eq (aref indicator 0) ?\s))
934 (setq indicator (substring indicator 1)))
935 (let ((minor-modes minor-mode-alist)
938 (let* ((minor-mode (car (car minor-modes)))
939 (anindicator (format-mode-line
940 (car (cdr (car minor-modes))))))
941 ;; remove first space if existed
942 (if (and (stringp anindicator)
943 (> (length anindicator) 0)
944 (eq (aref anindicator 0) ?\s))
945 (setq anindicator (substring anindicator 1)))
946 (if (equal indicator anindicator)
947 (setq result minor-mode
949 (setq minor-modes (cdr minor-modes)))))
953 ;;; Automatic resizing of temporary buffers.
955 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
956 "Maximum height of a window displaying a temporary buffer.
957 This is effective only when Temp Buffer Resize mode is enabled.
958 The value is the maximum height (in lines) which `resize-temp-buffer-window'
959 will give to a window displaying a temporary buffer.
960 It can also be a function to be called to choose the height for such a buffer.
961 It gets one argumemt, the buffer, and should return a positive integer."
962 :type '(choice integer function)
966 (define-minor-mode temp-buffer-resize-mode
967 "Toggle the mode which makes windows smaller for temporary buffers.
968 With prefix argument ARG, turn the resizing of windows displaying temporary
969 buffers on if ARG is positive or off otherwise.
970 This makes the window the right height for its contents, but never
971 more than `temp-buffer-max-height' nor less than `window-min-height'.
972 This applies to `help', `apropos' and `completion' buffers, and some others."
973 :global t :group 'help
974 (if temp-buffer-resize-mode
975 ;; `help-make-xrefs' may add a `back' button and thus increase the
976 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
977 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
978 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
980 (defun resize-temp-buffer-window ()
981 "Resize the selected window to fit its contents.
982 Will not make it higher than `temp-buffer-max-height' nor smaller than
983 `window-min-height'. Do nothing if it is the only window on its frame, if it
984 is not as wide as the frame or if some of the window's contents are scrolled
986 (unless (or (one-window-p 'nomini)
987 (not (pos-visible-in-window-p (point-min)))
988 (not (window-full-width-p)))
989 (fit-window-to-buffer
991 (if (functionp temp-buffer-max-height)
992 (funcall temp-buffer-max-height (current-buffer))
993 temp-buffer-max-height))))
998 (defcustom help-window-select 'other
999 "Non-nil means select help window for viewing.
1001 never (nil) Select help window only if there is no other window
1003 other Select help window unless the selected window is the
1004 only other window on its frame.
1005 always (t) Always select the help window.
1007 This option has effect if and only if the help window was created
1008 by `with-help-window'"
1009 :type '(choice (const :tag "never (nil)" nil)
1010 (const :tag "other" other)
1011 (const :tag "always (t)" t))
1015 (defun help-window-display-message (quit-part window &optional other)
1016 "Display message telling how to quit and scroll help window.
1017 QUIT-PART is a string telling how to quit the help window WINDOW.
1018 Optional argument OTHER non-nil means return text telling how to
1019 scroll the \"other\" window."
1022 ((pos-visible-in-window-p
1023 (with-current-buffer (window-buffer window)
1024 (point-max)) window)
1025 ;; Buffer end is visible.
1027 (other ", \\[scroll-other-window] to scroll help.")
1028 (t ", \\[scroll-up] to scroll help."))))
1030 (substitute-command-keys (concat quit-part scroll-part)))))
1032 (defun help-window-setup-finish (window &optional reuse keep-frame)
1033 "Finish setting up help window WINDOW.
1034 Select WINDOW according to the value of `help-window-select'.
1035 Display message telling how to scroll and eventually quit WINDOW.
1037 Optional argument REUSE non-nil means WINDOW has been reused \(by
1038 `display-buffer'\) for displaying help. Optional argument
1039 KEEP-FRAME non-nil means that quitting must no delete the frame
1041 (let ((number-of-windows
1042 (length (window-list (window-frame window) 'no-mini window))))
1044 ((eq window (selected-window))
1045 ;; The help window is the selected window, probably the
1046 ;; `pop-up-windows' nil case.
1047 (help-window-display-message
1049 "Type \"q\" to restore this window"
1050 ;; This should not be taken.
1051 "Type \"q\" to quit") window))
1052 ((= number-of-windows 1)
1053 ;; The help window is alone on a frame and not the selected
1054 ;; window, could be the `pop-up-frames' t case.
1055 (help-window-display-message
1057 (keep-frame "Type \"q\" to delete this window")
1058 (reuse "Type \"q\" to restore this window")
1059 (view-remove-frame-by-deleting "Type \"q\" to delete this frame")
1060 (t "Type \"q\" to iconify this frame"))
1062 ((and (= number-of-windows 2)
1063 (eq (window-frame window) (window-frame (selected-window))))
1064 ;; There are two windows on the help window's frame and the other
1065 ;; window is the selected one.
1066 (if (memq help-window-select '(nil other))
1067 ;; Do not select the help window.
1068 (help-window-display-message
1070 ;; Offer `display-buffer' for consistency with
1071 ;; `print-help-return-message'. This is hardly TRT when
1072 ;; the other window and the selected window display the
1073 ;; same buffer but has been handled this way ever since.
1074 "Type \\[display-buffer] RET to restore the other window"
1075 ;; The classic "two windows" configuration.
1076 "Type \\[delete-other-windows] to delete the help window")
1078 ;; Select help window and tell how to quit.
1079 (select-window window)
1080 (help-window-display-message
1082 "Type \"q\" to restore this window"
1083 "Type \"q\" to delete this window") window)))
1085 ;; Issuing a message with 3 or more windows on the same frame
1086 ;; without selecting the help window doesn't make any sense.
1087 (select-window window)
1088 (help-window-display-message
1090 "Type \"q\" to restore this window"
1091 "Type \"q\" to delete this window") window)))))
1093 (defun help-window-setup (list-of-frames list-of-window-tuples)
1094 "Set up help window.
1095 LIST-OF-FRAMES and LIST-OF-WINDOW-TUPLES are the lists of frames
1096 and window quadruples built by `with-help-window'. The help
1097 window itself is specified by the variable `help-window'."
1098 (let* ((help-buffer (window-buffer help-window))
1099 ;; `help-buffer' now denotes the help window's buffer.
1102 (buffer-local-value 'view-return-to-alist help-buffer)))
1103 (help-entry (assq help-window list-of-window-tuples)))
1105 ;; Handle `help-window-point-marker'.
1106 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1107 (set-window-point help-window help-window-point-marker)
1108 ;; Reset `help-window-point-marker'.
1109 (set-marker help-window-point-marker nil))
1113 ;; `view-return-to-alist' has an entry for the help window.
1115 ((eq help-window (selected-window))
1116 ;; The help window is the selected window, probably because the
1117 ;; user followed a backward/forward button or a cross reference.
1118 ;; In this case just purge stale entries from
1119 ;; `view-return-to-alist' but leave the entry alone and don't
1120 ;; display a message.
1121 (view-return-to-alist-update help-buffer))
1122 ((and help-entry (eq (cadr help-entry) help-buffer))
1123 ;; The help window was not selected but displayed the help
1124 ;; buffer. In this case reuse existing exit information but try
1125 ;; to get back to the selected window when quitting. Don't
1126 ;; display a message since the user must have seen one before.
1127 (view-return-to-alist-update
1128 help-buffer (cons help-window
1129 (cons (selected-window) (cddr view-entry)))))
1131 ;; The help window was not selected, did display the help buffer
1132 ;; earlier, but displayed another buffer when help was invoked.
1133 ;; Set up things so that quitting will show that buffer again.
1134 (view-return-to-alist-update
1135 help-buffer (cons help-window
1136 (cons (selected-window) (cdr help-entry))))
1137 (help-window-setup-finish help-window t))
1139 ;; The help window is new but `view-return-to-alist' had an
1140 ;; entry for it. This should never happen.
1141 (view-return-to-alist-update
1142 help-buffer (cons help-window
1143 (cons (selected-window) 'quit-window)))
1144 (help-window-setup-finish help-window t))))
1146 ;; `view-return-to-alist' does not have an entry for help window
1147 ;; but `list-of-window-tuples' does. Hence `display-buffer' must
1148 ;; have reused an existing window.
1149 (if (eq (cadr help-entry) help-buffer)
1150 ;; The help window displayed `help-buffer' before but no
1151 ;; `view-return-to-alist' entry was found probably because the
1152 ;; user manually switched to the help buffer. Set up things
1153 ;; for `quit-window' although `view-exit-action' should be
1154 ;; able to handle this case all by itself.
1156 (view-return-to-alist-update
1157 help-buffer (cons help-window
1158 (cons (selected-window) 'quit-window)))
1159 (help-window-setup-finish help-window t))
1160 ;; The help window displayed another buffer before. Set up
1161 ;; things in a way that quitting can orderly show that buffer
1162 ;; again. The window-start and window-point information from
1163 ;; `list-of-window-tuples' provide the necessary information.
1164 (view-return-to-alist-update
1165 help-buffer (cons help-window
1166 (cons (selected-window) (cdr help-entry))))
1167 (help-window-setup-finish help-window t)))
1168 ((memq (window-frame help-window) list-of-frames)
1169 ;; The help window is a new window on an existing frame. This
1170 ;; case must be handled specially by `help-window-setup-finish'
1171 ;; and `view-mode-exit' to ascertain that quitting does _not_
1172 ;; inadvertently delete the frame.
1173 (view-return-to-alist-update
1174 help-buffer (cons help-window
1175 (cons (selected-window) 'keep-frame)))
1176 (help-window-setup-finish help-window nil t))
1178 ;; The help window is shown on a new frame. In this case quitting
1179 ;; shall handle both, the help window _and_ its frame. We changed
1180 ;; the default of `view-remove-frame-by-deleting' to t in order to
1181 ;; intuitively DTRT here.
1182 (view-return-to-alist-update
1183 help-buffer (cons help-window (cons (selected-window) t)))
1184 (help-window-setup-finish help-window)))))
1186 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1187 ;; providing the following additional twists:
1189 ;; (1) Issue more accurate messages telling how to scroll and quit the
1192 ;; (2) Make `view-mode-exit' DTRT in more cases.
1194 ;; (3) An option (customizable via `help-window-select') to select the
1195 ;; help window automatically.
1197 ;; (4) A marker (`help-window-point-marker') to move point in the help
1198 ;; window to an arbitrary buffer position.
1200 ;; Note: It's usually always wrong to use `print-help-return-message' in
1201 ;; the body of `with-help-window'.
1202 (defmacro with-help-window (buffer-name &rest body)
1203 "Display buffer BUFFER-NAME in a help window evaluating BODY.
1204 Select help window if the actual value of the user option
1205 `help-window-select' says so."
1206 (declare (indent 1) (debug t))
1207 ;; Bind list-of-frames to `frame-list' and list-of-window-tuples to a
1208 ;; list of one <window window-buffer window-start window-point> tuple
1209 ;; for each live window.
1210 `(let ((list-of-frames (frame-list))
1211 (list-of-window-tuples
1215 (push (list window (window-buffer window)
1216 (window-start window) (window-point window))
1220 ;; We set `help-window' to t in order to trigger `help-mode-finish'
1221 ;; to set `help-window' to the actual help window.
1222 (setq help-window t)
1223 ;; Make `help-window-point-marker' point nowhere (the only place
1224 ;; where this should be set to a buffer position is within BODY).
1225 (set-marker help-window-point-marker nil)
1227 (with-output-to-temp-buffer ,buffer-name
1230 (when (windowp help-window)
1231 ;; Set up help window.
1232 (help-window-setup list-of-frames list-of-window-tuples))
1234 ;; Reset `help-window' to nil to avoid confusing future calls of
1235 ;; `help-mode-finish' by "plain" `with-output-to-temp-buffer'.
1236 (setq help-window nil)))
1240 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1241 ;;; help.el ends here