1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
26 ;; This code implements GNU Emacs' on-line help system, the one invoked by
27 ;; `M-x help-for-help'.
31 ;; Get the macro make-help-screen when this is compiled,
32 ;; or run interpreted, but not when the compiled code is loaded.
33 (eval-when-compile (require 'help-macro
))
35 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
36 (add-hook 'temp-buffer-setup-hook
'help-mode-setup
)
37 (add-hook 'temp-buffer-show-hook
'help-mode-finish
)
39 ;; The variable `help-window' below is used by `help-mode-finish' to
40 ;; communicate the window displaying help (the "help window") to the
41 ;; macro `with-help-window'. The latter sets `help-window' to t before
42 ;; invoking `with-output-to-temp-buffer'. If and only if `help-window'
43 ;; is eq to t, `help-mode-finish' (called by `temp-buffer-setup-hook')
44 ;; sets `help-window' to the window selected by `display-buffer'.
45 ;; Exiting `with-help-window' and calling `help-print-return-message'
46 ;; reset `help-window' to nil.
47 (defvar help-window nil
48 "Window chosen for displaying help.")
50 ;; `help-window-point-marker' is a marker you can move to a valid
51 ;; position of the buffer shown in the help window in order to override
52 ;; the standard positioning mechanism (`point-min') chosen by
53 ;; `with-output-to-temp-buffer'. `with-help-window' has this point
54 ;; nowhere before exiting. Currently used by `view-lossage' to assert
55 ;; that the last keystrokes are always visible.
56 (defvar help-window-point-marker
(make-marker)
57 "Marker to override default `window-point' of `help-window'.")
60 (let ((map (make-sparse-keymap)))
61 (define-key map
(char-to-string help-char
) 'help-for-help
)
62 (define-key map
[help] 'help-for-help)
63 (define-key map [f1] 'help-for-help)
64 (define-key map "." 'display-local-help)
65 (define-key map "?" 'help-for-help)
67 (define-key map "\C-a" 'about-emacs)
68 (define-key map "\C-c" 'describe-copying)
69 (define-key map "\C-d" 'view-emacs-debugging)
70 (define-key map "\C-e" 'view-external-packages)
71 (define-key map "\C-f" 'view-emacs-FAQ)
72 (define-key map "\C-m" 'view-order-manuals)
73 (define-key map "\C-n" 'view-emacs-news)
74 (define-key map "\C-o" 'describe-distribution)
75 (define-key map "\C-p" 'view-emacs-problems)
76 (define-key map "\C-t" 'view-emacs-todo)
77 (define-key map "\C-w" 'describe-no-warranty)
79 ;; This does not fit the pattern, but it is natural given the C-\ command.
80 (define-key map "\C-\\" 'describe-input-method)
82 (define-key map "C" 'describe-coding-system)
83 (define-key map "F" 'Info-goto-emacs-command-node)
84 (define-key map "I" 'describe-input-method)
85 (define-key map "K" 'Info-goto-emacs-key-command-node)
86 (define-key map "L" 'describe-language-environment)
87 (define-key map "S" 'info-lookup-symbol)
89 (define-key map "a" 'apropos-command)
90 (define-key map "b" 'describe-bindings)
91 (define-key map "c" 'describe-key-briefly)
92 (define-key map "d" 'apropos-documentation)
93 (define-key map "e" 'view-echo-area-messages)
94 (define-key map "f" 'describe-function)
95 (define-key map "g" 'describe-gnu-project)
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 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
141 (defun help-print-return-message (&optional function)
142 "Display or return message saying how to restore windows after help command.
143 This function assumes that `standard-output' is the help buffer.
144 It computes a message, and applies the optional argument FUNCTION to it.
145 If FUNCTION is nil, it applies `message', thus displaying the message.
146 In addition, this function sets up `help-return-method', which see, that
147 specifies what to do when the user exits the help buffer."
148 ;; Reset `help-window' here to avoid confusing `help-mode-finish'.
149 (setq help-window nil)
150 (and (not (get-buffer-window standard-output))
154 (special-display-p (buffer-name standard-output)))
155 (setq help-return-method (cons (selected-window) t))
156 ;; If the help output buffer is a special display buffer,
157 ;; don't say anything about how to get rid of it.
158 ;; First of all, the user will do that with the window
159 ;; manager, not with Emacs.
160 ;; Secondly, the buffer has not been displayed yet,
161 ;; so we don't know whether its frame will be selected.
163 (display-buffer-reuse-frames
164 (setq help-return-method (cons (selected-window)
167 ((not (one-window-p t))
168 (setq help-return-method
169 (cons (selected-window) 'quit-window))
170 "Type \\[display-buffer] RET to restore the other window.")
172 (setq help-return-method (cons (selected-window) t))
173 "Type \\[delete-other-windows] to remove help window.")
175 (setq help-return-method
176 (list (selected-window) (window-buffer)
177 (window-start) (window-point)))
178 "Type \\[switch-to-buffer] RET to remove help window."))))
179 (funcall (or function 'message)
182 (substitute-command-keys first-message))
183 (if first-message " ")
184 ;; If the help buffer will go in a separate frame,
185 ;; it's no use mentioning a command to scroll, so don't.
186 (if (or pop-up-windows
187 (special-display-p (buffer-name standard-output)))
189 (if (same-window-p (buffer-name standard-output))
190 ;; Say how to scroll this window.
191 (substitute-command-keys
192 "\\[scroll-up] to scroll the help.")
193 ;; Say how to scroll some other window.
194 (substitute-command-keys
195 "\\[scroll-other-window] to scroll the help."))))))))
197 ;; So keyboard macro definitions are documented correctly
198 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
200 (defalias 'help 'help-for-help-internal)
201 ;; find-function can find this.
202 (defalias 'help-for-help 'help-for-help-internal)
203 ;; It can't find this, but nobody will look.
204 (make-help-screen help-for-help-internal
205 "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?"
206 "You have typed %THIS-KEY%, the help character. Type a Help option:
207 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
209 a PATTERN Show commands whose name matches the PATTERN (a list of words
210 or a regexp). See also the `apropos' command.
211 b Display all key bindings.
212 c KEYS Display the command name run by the given key sequence.
213 C CODING Describe the given coding system, or RET for current ones.
214 d PATTERN Show a list of functions, variables, and other items whose
215 documentation matches the PATTERN (a list of words or a regexp).
216 e Go to the *Messages* buffer which logs echo-area messages.
217 f FUNCTION Display documentation for the given function.
218 F COMMAND Show the on-line manual's section that describes the command.
219 g Display information about the GNU project.
220 h Display the HELLO file which illustrates various scripts.
221 i Start the Info documentation reader: read on-line manuals.
222 I METHOD Describe a specific input method, or RET for current.
223 k KEYS Display the full documentation for the key sequence.
224 K KEYS Show the on-line manual's section for the command bound to KEYS.
225 l Show last 300 input keystrokes (lossage).
226 L LANG-ENV Describes a specific language environment, or RET for current.
227 m Display documentation of current minor modes and current major mode,
228 including their special commands.
229 n Display news of recent Emacs changes.
230 p TOPIC Find packages matching a given topic keyword.
231 r Display the Emacs manual in Info mode.
232 s Display contents of current syntax table, plus explanations.
233 S SYMBOL Show the section for the given symbol in the on-line manual
234 for the programming language used in this buffer.
235 t Start the Emacs learn-by-doing tutorial.
236 v VARIABLE Display the given variable's documentation and value.
237 w COMMAND Display which keystrokes invoke the given command (where-is).
238 . Display any available local help at point in the echo area.
240 C-a Information about Emacs.
241 C-c Emacs copying permission (GNU General Public License).
242 C-d Instructions for debugging GNU Emacs.
243 C-e External packages and information about Emacs.
245 C-m How to order printed Emacs manuals.
246 C-n News of recent Emacs changes.
247 C-o Emacs ordering and distribution information.
248 C-p Info about known Emacs problems.
250 C-w Information on absence of warranty for GNU Emacs."
255 (defun function-called-at-point ()
256 "Return a function around point or else called by the list containing point.
257 If that doesn't give a function, return nil."
258 (with-syntax-table emacs-lisp-mode-syntax-table
259 (or (condition-case ()
261 (or (not (zerop (skip-syntax-backward "_w")))
262 (eq (char-syntax (following-char)) ?w)
263 (eq (char-syntax (following-char)) ?_)
265 (skip-chars-forward "'")
266 (let ((obj (read (current-buffer))))
267 (and (symbolp obj) (fboundp obj) obj)))
272 (narrow-to-region (max (point-min)
273 (- (point) 1000)) (point-max))
274 ;; Move up to surrounding paren, then after the open.
277 ;; If there is space here, this is probably something
278 ;; other than a real Lisp function call, so ignore it.
279 (if (looking-at "[ \t]")
280 (error "Probably not a Lisp function call"))
281 (let ((obj (read (current-buffer))))
282 (and (symbolp obj) (fboundp obj) obj))))
284 (let* ((str (find-tag-default))
285 (sym (if str (intern-soft str))))
286 (if (and sym (fboundp sym))
289 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
290 (setq sym (intern-soft (match-string 1 str)))
291 (and (fboundp sym) sym))))))))
294 ;;; `User' help functions
296 (defun view-help-file (file &optional dir)
297 (view-file (expand-file-name file (or dir data-directory)))
298 (goto-address-mode 1)
299 (goto-char (point-min)))
301 (defun describe-distribution ()
302 "Display info on how to obtain the latest version of GNU Emacs."
304 (view-help-file "DISTRIB"))
306 (defun describe-copying ()
307 "Display info on how you may redistribute copies of GNU Emacs."
309 (view-help-file "COPYING"))
311 (defun describe-gnu-project ()
312 "Display info on the GNU project."
314 (view-help-file "THE-GNU-PROJECT"))
316 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
318 (defun describe-no-warranty ()
319 "Display info on all the kinds of warranty Emacs does NOT have."
322 (let (case-fold-search)
323 (search-forward "NO WARRANTY")
326 (defun describe-prefix-bindings ()
327 "Describe the bindings of the prefix used to reach this command.
328 The prefix described consists of all but the last event
329 of the key sequence that ran this command."
331 (let ((key (this-command-keys)))
334 (substring key 0 (1- (length key)))
335 (let ((prefix (make-vector (1- (length key)) nil))
337 (while (< i (length prefix))
338 (aset prefix i (aref key i))
341 ;; Make C-h after a prefix, when not specifically bound,
342 ;; run describe-prefix-bindings.
343 (setq prefix-help-command 'describe-prefix-bindings)
345 (defun view-emacs-news (&optional version)
346 "Display info on recent changes to Emacs.
347 With argument, display info only for the selected version."
350 (setq version emacs-major-version))
351 (when (consp version)
357 (insert-file-contents
358 (expand-file-name file data-directory))
359 (while (re-search-forward
360 (if (member file '("NEWS.18" "NEWS.1-17"))
361 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
362 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
363 (setq res (cons (match-string-no-properties 1) res)))))
365 (directory-files data-directory nil
366 "^NEWS\\.[0-9][-0-9]*$" nil)))
367 (sort (delete-dups res) (lambda (a b) (string< b a)))))
368 (current (car all-versions)))
369 (setq version (completing-read
370 (format "Read NEWS for the version (default %s): " current)
371 all-versions nil nil nil nil current))
372 (if (integerp (string-to-number version))
373 (setq version (string-to-number version))
374 (unless (or (member version all-versions)
375 (<= (string-to-number version) (string-to-number current)))
376 (error "No news about version %s" version)))))
377 (when (integerp version)
378 (cond ((<= version 12)
379 (setq version (format "1.%d" version)))
381 (setq version (format "%d" version)))
382 ((> version emacs-major-version)
383 (error "No news about Emacs %d (yet)" version))))
384 (let* ((vn (if (stringp version)
385 (string-to-number version)
388 ((>= vn emacs-major-version) "NEWS")
389 ((< vn 18) "NEWS.1-17")
390 (t (format "NEWS.%d" vn))))
392 (view-file (expand-file-name file data-directory))
394 (goto-char (point-min))
395 (when (stringp version)
396 (when (re-search-forward
397 (concat (if (< vn 19)
398 "Changes in Emacs[ \t]*"
399 "^\* [^0-9\n]*") version "$")
405 (while (and (setq res
408 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
409 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
410 (equal (match-string-no-properties 1) version)))
411 (or res (goto-char (point-max)))
415 (defun view-emacs-todo (&optional arg)
416 "Display the Emacs TODO list."
418 (view-help-file "TODO"))
420 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
423 (defun view-echo-area-messages ()
424 "View the log of recent echo-area messages: the `*Messages*' buffer.
425 The number of messages retained in that buffer
426 is specified by the variable `message-log-max'."
428 (switch-to-buffer (get-buffer-create "*Messages*")))
430 (defun view-order-manuals ()
431 "Display the Emacs ORDERS file."
433 (view-help-file "ORDERS"))
435 (defun view-emacs-FAQ ()
436 "Display the Emacs Frequently Asked Questions (FAQ) file."
438 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
441 (defun view-emacs-problems ()
442 "Display info on known problems with Emacs and possible workarounds."
444 (view-help-file "PROBLEMS"))
446 (defun view-emacs-debugging ()
447 "Display info on how to debug Emacs problems."
449 (view-help-file "DEBUG"))
451 (defun view-external-packages ()
452 "Display external packages and information about Emacs."
454 (view-help-file "MORE.STUFF"))
456 (defun view-lossage ()
457 "Display last 300 input keystrokes.
459 To record all your input on a file, use `open-dribble-file'."
461 (help-setup-xref (list #'view-lossage) (interactive-p))
462 (with-help-window (help-buffer)
463 (princ (mapconcat (lambda (key)
464 (if (or (integerp key) (symbolp key) (listp key))
465 (single-key-description key)
466 (prin1-to-string key nil)))
469 (with-current-buffer standard-output
470 (goto-char (point-min))
471 (while (progn (move-to-column 50) (not (eobp)))
472 (when (search-forward " " nil t)
475 ;; jidanni wants to see the last keystrokes immediately.
476 (set-marker help-window-point-marker (point)))))
481 (defun describe-bindings (&optional prefix buffer)
482 "Show a list of all defined keys, and their definitions.
483 We put that list in a buffer, and display the buffer.
485 The optional argument PREFIX, if non-nil, should be a key sequence;
486 then we display only bindings that start with that prefix.
487 The optional argument BUFFER specifies which buffer's bindings
488 to display (default, the current buffer). BUFFER can be a buffer
491 (or buffer (setq buffer (current-buffer)))
492 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
493 (with-current-buffer buffer
494 (describe-bindings-internal nil prefix)))
496 ;; This function used to be in keymap.c.
497 (defun describe-bindings-internal (&optional menus prefix)
498 "Show a list of all defined keys, and their definitions.
499 We put that list in a buffer, and display the buffer.
501 The optional argument MENUS, if non-nil, says to mention menu bindings.
502 \(Ordinarily these are omitted from the output.)
503 The optional argument PREFIX, if non-nil, should be a key sequence;
504 then we display only bindings that start with that prefix."
506 (let ((buf (current-buffer)))
507 (with-help-window "*Help*"
508 (with-current-buffer standard-output
509 (describe-buffer-bindings buf prefix menus)))))
511 (defun where-is (definition &optional insert)
512 "Print message listing key sequences that invoke the command DEFINITION.
513 Argument is a command definition, usually a symbol with a function definition.
514 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
516 (let ((fn (function-called-at-point))
517 (enable-recursive-minibuffers t)
519 (setq val (completing-read
521 (format "Where is command (default %s): " fn)
522 "Where is command: ")
523 obarray 'commandp t))
524 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
525 (unless definition (error "No command"))
526 (let ((func (indirect-function definition))
528 (standard-output (if insert (current-buffer) t)))
529 ;; In DEFS, find all symbols that are aliases for DEFINITION.
530 (mapatoms (lambda (symbol)
531 (and (fboundp symbol)
532 (not (eq symbol definition))
533 (eq func (condition-case ()
534 (indirect-function symbol)
536 (push symbol defs))))
537 ;; Look at all the symbols--first DEFINITION,
539 (dolist (symbol (cons definition defs))
540 (let* ((remapped (command-remapping symbol))
541 (keys (where-is-internal
542 symbol overriding-local-map nil nil remapped))
543 (keys (mapconcat 'key-description keys ", "))
547 (if (> (length keys) 0)
549 (format "%s (%s) (remapped from %s)"
550 keys remapped symbol)
551 (format "%s (%s)" keys symbol))
552 (format "M-x %s RET" symbol))
553 (if (> (length keys) 0)
555 (format "%s is remapped to %s which is on %s"
556 symbol remapped keys)
557 (format "%s is on %s" symbol keys))
558 ;; If this is the command the user asked about,
559 ;; and it is not on any key, say so.
560 ;; For other symbols, its aliases, say nothing
561 ;; about them unless they are on keys.
562 (if (eq symbol definition)
563 (format "%s is not on any key" symbol)))))
565 (unless (eq symbol definition)
566 (princ ";\n its alias "))
570 (defun help-key-description (key untranslated)
571 (let ((string (key-description key)))
572 (if (or (not untranslated)
573 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
575 (let ((otherstring (key-description untranslated)))
576 (if (equal string otherstring)
578 (format "%s (translated from %s)" string otherstring))))))
580 (defun describe-key-briefly (&optional key insert untranslated)
581 "Print the name of the function KEY invokes. KEY is a string.
582 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
583 If non-nil, UNTRANSLATED is a vector of the untranslated events.
584 It can also be a number in which case the untranslated events from
585 the last key hit are used.
587 If KEY is a menu item or a tool-bar button that is disabled, this command
588 temporarily enables it to allow getting help on disabled items and buttons."
590 (let ((enable-disabled-menus-and-buttons t)
591 (cursor-in-echo-area t)
595 ;; If yank-menu is empty, populate it temporarily, so that
596 ;; "Select and Paste" menu can generate a complete event.
597 (when (null (cdr yank-menu))
598 (setq saved-yank-menu (copy-sequence yank-menu))
599 (menu-bar-update-yank-menu "(any string)" nil))
600 (setq key (read-key-sequence "Describe key (or click or menu item): "))
601 ;; If KEY is a down-event, read and discard the
602 ;; corresponding up-event. Note that there are also
603 ;; down-events on scroll bars and mode lines: the actual
604 ;; event then is in the second element of the vector.
606 (let ((last-idx (1- (length key))))
607 (and (eventp (aref key last-idx))
608 (memq 'down (event-modifiers (aref key last-idx)))))
612 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
614 ;; Put yank-menu back as it was, if we changed it.
615 (when saved-yank-menu
616 (setq yank-menu (copy-sequence saved-yank-menu))
617 (fset 'yank-menu (cons 'keymap yank-menu))))))
618 (if (numberp untranslated)
619 (setq untranslated (this-single-command-raw-keys)))
620 (let* ((event (if (and (symbolp (aref key 0))
622 (consp (aref key 1)))
625 (modifiers (event-modifiers event))
626 (standard-output (if insert (current-buffer) t))
627 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
628 (memq 'drag modifiers)) " at that spot" ""))
629 (defn (key-binding key t))
631 ;; Handle the case where we faked an entry in "Select and Paste" menu.
632 (if (and (eq defn nil)
633 (stringp (aref key (1- (length key))))
634 (eq (key-binding (substring key 0 -1)) 'yank-menu))
635 (setq defn 'menu-bar-select-yank))
636 ;; Don't bother user with strings from (e.g.) the select-paste menu.
637 (if (stringp (aref key (1- (length key))))
638 (aset key (1- (length key)) "(any string)"))
639 (if (and (> (length untranslated) 0)
640 (stringp (aref untranslated (1- (length untranslated)))))
641 (aset untranslated (1- (length untranslated)) "(any string)"))
642 ;; Now describe the key, perhaps as changed.
643 (setq key-desc (help-key-description key untranslated))
644 (if (or (null defn) (integerp defn) (equal defn 'undefined))
645 (princ (format "%s%s is undefined" key-desc mouse-msg))
646 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
648 (defun describe-key (&optional key untranslated up-event)
649 "Display documentation of the function invoked by KEY.
650 KEY can be any kind of a key sequence; it can include keyboard events,
651 mouse events, and/or menu events. When calling from a program,
652 pass KEY as a string or a vector.
654 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
655 It can also be a number, in which case the untranslated events from
656 the last key sequence entered are used.
657 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
659 If KEY is a menu item or a tool-bar button that is disabled, this command
660 temporarily enables it to allow getting help on disabled items and buttons."
662 (let ((enable-disabled-menus-and-buttons t)
663 (cursor-in-echo-area t)
667 ;; If yank-menu is empty, populate it temporarily, so that
668 ;; "Select and Paste" menu can generate a complete event.
669 (when (null (cdr yank-menu))
670 (setq saved-yank-menu (copy-sequence yank-menu))
671 (menu-bar-update-yank-menu "(any string)" nil))
672 (setq key (read-key-sequence "Describe key (or click or menu item): "))
675 (prefix-numeric-value current-prefix-arg)
676 ;; If KEY is a down-event, read and include the
677 ;; corresponding up-event. Note that there are also
678 ;; down-events on scroll bars and mode lines: the actual
679 ;; event then is in the second element of the vector.
681 (let ((last-idx (1- (length key))))
682 (and (eventp (aref key last-idx))
683 (memq 'down (event-modifiers (aref key last-idx)))))
684 (or (and (eventp (aref key 0))
685 (memq 'down (event-modifiers (aref key 0)))
686 ;; However, for the C-down-mouse-2 popup
687 ;; menu, there is no subsequent up-event. In
688 ;; this case, the up-event is the next
689 ;; element in the supplied vector.
691 (and (> (length key) 1)
692 (eventp (aref key 1))
693 (memq 'down (event-modifiers (aref key 1)))))
695 ;; Put yank-menu back as it was, if we changed it.
696 (when saved-yank-menu
697 (setq yank-menu (copy-sequence saved-yank-menu))
698 (fset 'yank-menu (cons 'keymap yank-menu))))))
699 (if (numberp untranslated)
700 (setq untranslated (this-single-command-raw-keys)))
701 (let* ((event (aref key (if (and (symbolp (aref key 0))
703 (consp (aref key 1)))
706 (modifiers (event-modifiers event))
707 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
708 (memq 'drag modifiers)) " at that spot" ""))
709 (defn (key-binding key t))
710 defn-up defn-up-tricky ev-type
711 mouse-1-remapped mouse-1-tricky)
713 ;; Handle the case where we faked an entry in "Select and Paste" menu.
714 (when (and (eq defn nil)
715 (stringp (aref key (1- (length key))))
716 (eq (key-binding (substring key 0 -1)) 'yank-menu))
717 (setq defn 'menu-bar-select-yank))
718 (if (or (null defn) (integerp defn) (equal defn 'undefined))
719 (message "%s%s is undefined"
720 (help-key-description key untranslated) mouse-msg)
721 (help-setup-xref (list #'describe-function defn) (interactive-p))
722 ;; Don't bother user with strings from (e.g.) the select-paste menu.
723 (when (stringp (aref key (1- (length key))))
724 (aset key (1- (length key)) "(any string)"))
725 (when (and untranslated
726 (stringp (aref untranslated (1- (length untranslated)))))
727 (aset untranslated (1- (length untranslated))
729 ;; Need to do this before erasing *Help* buffer in case event
730 ;; is a mouse click in an existing *Help* buffer.
732 (setq ev-type (event-basic-type up-event))
733 (let ((sequence (vector up-event)))
734 (when (and (eq ev-type 'mouse-1)
735 mouse-1-click-follows-link
736 (not (eq mouse-1-click-follows-link 'double))
737 (setq mouse-1-remapped
738 (mouse-on-link-p (event-start up-event))))
739 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
740 (> mouse-1-click-follows-link 0)))
741 (cond ((stringp mouse-1-remapped)
742 (setq sequence mouse-1-remapped))
743 ((vectorp mouse-1-remapped)
744 (setcar up-event (elt mouse-1-remapped 0)))
745 (t (setcar up-event 'mouse-2))))
746 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
748 (setq sequence (vector up-event))
749 (aset sequence 0 'mouse-1)
750 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
751 (with-help-window (help-buffer)
752 (princ (help-key-description key untranslated))
754 %s runs the command %S, which is "
756 (describe-function-1 defn)
758 (unless (or (null defn-up)
760 (equal defn-up 'undefined))
763 ----------------- up-event %s----------------
765 <%S>%s%s runs the command %S, which is "
766 (if mouse-1-tricky "(short click) " "")
769 " is remapped to <mouse-2>, which" "")
771 (describe-function-1 defn-up))
772 (unless (or (null defn-up-tricky)
773 (integerp defn-up-tricky)
774 (eq defn-up-tricky 'undefined))
777 ----------------- up-event (long click) ----------------
779 Pressing <%S>%s for longer than %d milli-seconds
780 runs the command %S, which is "
782 mouse-1-click-follows-link
784 (describe-function-1 defn-up-tricky)))))))
786 (defun describe-mode (&optional buffer)
787 "Display documentation of current major mode and minor modes.
788 A brief summary of the minor modes comes first, followed by the
789 major mode description. This is followed by detailed
790 descriptions of the minor modes, each on a separate page.
792 For this to work correctly for a minor mode, the mode's indicator
793 variable \(listed in `minor-mode-alist') must also be a function
794 whose documentation describes the minor mode."
796 (unless buffer (setq buffer (current-buffer)))
797 (help-setup-xref (list #'describe-mode buffer)
799 ;; For the sake of help-do-xref and help-xref-go-back,
800 ;; don't switch buffers before calling `help-buffer'.
801 (with-help-window (help-buffer)
802 (with-current-buffer buffer
804 ;; Older packages do not register in minor-mode-list but only in
806 (dolist (x minor-mode-alist)
808 (unless (memq x minor-mode-list)
809 (push x minor-mode-list)))
810 ;; Find enabled minor mode we will want to mention.
811 (dolist (mode minor-mode-list)
812 ;; Document a minor mode if it is listed in minor-mode-alist,
813 ;; non-nil, and has a function definition.
814 (let ((fmode (or (get mode :minor-mode-function) mode)))
815 (and (boundp mode) (symbol-value mode)
817 (let ((pretty-minor-mode
818 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
821 (substring (symbol-name fmode)
822 0 (match-beginning 0)))
824 (push (list fmode pretty-minor-mode
825 (format-mode-line (assq mode minor-mode-alist)))
829 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
831 (princ "Enabled minor modes:\n")
832 (make-local-variable 'help-button-cache)
833 (with-current-buffer standard-output
834 (dolist (mode minor-modes)
835 (let ((mode-function (nth 0 mode))
836 (pretty-minor-mode (nth 1 mode))
837 (indicator (nth 2 mode)))
838 (add-text-properties 0 (length pretty-minor-mode)
839 '(face bold) pretty-minor-mode)
841 (goto-char (point-max))
843 (push (point-marker) help-button-cache)
844 ;; Document the minor modes fully.
845 (insert pretty-minor-mode)
846 (princ (format " minor mode (%s):\n"
847 (if (zerop (length indicator))
849 (format "indicator%s"
851 (princ (documentation mode-function)))
852 (insert-button pretty-minor-mode
853 'action (car help-button-cache)
855 'help-echo "mouse-2, RET: show full information")
861 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
862 ;; Document the major mode.
863 (let ((mode mode-name))
864 (with-current-buffer standard-output
865 (let ((start (point)))
866 (insert (format-mode-line mode nil nil buffer))
867 (add-text-properties start (point) '(face bold)))))
869 (princ (documentation major-mode))))))
872 (defun describe-minor-mode (minor-mode)
873 "Display documentation of a minor mode given as MINOR-MODE.
874 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
875 appeared on the mode-line."
876 (interactive (list (completing-read
879 (describe-minor-mode-completion-table-for-symbol)
880 (describe-minor-mode-completion-table-for-indicator)
882 (if (symbolp minor-mode)
883 (setq minor-mode (symbol-name minor-mode)))
884 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
885 (indicators (describe-minor-mode-completion-table-for-indicator)))
887 ((member minor-mode symbols)
888 (describe-minor-mode-from-symbol (intern minor-mode)))
889 ((member minor-mode indicators)
890 (describe-minor-mode-from-indicator minor-mode))
892 (error "No such minor mode: %s" minor-mode)))))
895 (defun describe-minor-mode-completion-table-for-symbol ()
896 ;; In order to list up all minor modes, minor-mode-list
897 ;; is used here instead of minor-mode-alist.
898 (delq nil (mapcar 'symbol-name minor-mode-list)))
899 (defun describe-minor-mode-from-symbol (symbol)
900 "Display documentation of a minor mode given as a symbol, SYMBOL"
901 (interactive (list (intern (completing-read
902 "Minor mode symbol: "
903 (describe-minor-mode-completion-table-for-symbol)))))
905 (describe-function symbol)
906 (describe-variable symbol)))
909 (defun describe-minor-mode-completion-table-for-indicator ()
912 (let ((i (format-mode-line x)))
913 ;; remove first space if existed
922 (defun describe-minor-mode-from-indicator (indicator)
923 "Display documentation of a minor mode specified by INDICATOR.
924 If you call this function interactively, you can give indicator which
925 is currently activated with completion."
928 "Minor mode indicator: "
929 (describe-minor-mode-completion-table-for-indicator))))
930 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
932 (describe-minor-mode-from-symbol minor-mode)
933 (error "Cannot find minor mode for `%s'" indicator))))
935 (defun lookup-minor-mode-from-indicator (indicator)
936 "Return a minor mode symbol from its indicator on the modeline."
937 ;; remove first space if existed
938 (if (and (< 0 (length indicator))
939 (eq (aref indicator 0) ?\s))
940 (setq indicator (substring indicator 1)))
941 (let ((minor-modes minor-mode-alist)
944 (let* ((minor-mode (car (car minor-modes)))
945 (anindicator (format-mode-line
946 (car (cdr (car minor-modes))))))
947 ;; remove first space if existed
948 (if (and (stringp anindicator)
949 (> (length anindicator) 0)
950 (eq (aref anindicator 0) ?\s))
951 (setq anindicator (substring anindicator 1)))
952 (if (equal indicator anindicator)
953 (setq result minor-mode
955 (setq minor-modes (cdr minor-modes)))))
959 ;;; Automatic resizing of temporary buffers.
961 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
962 "Maximum height of a window displaying a temporary buffer.
963 This is effective only when Temp Buffer Resize mode is enabled.
964 The value is the maximum height (in lines) which `resize-temp-buffer-window'
965 will give to a window displaying a temporary buffer.
966 It can also be a function to be called to choose the height for such a buffer.
967 It gets one argumemt, the buffer, and should return a positive integer."
968 :type '(choice integer function)
972 (define-minor-mode temp-buffer-resize-mode
973 "Toggle the mode which makes windows smaller for temporary buffers.
974 With prefix argument ARG, turn the resizing of windows displaying temporary
975 buffers on if ARG is positive or off otherwise.
976 This makes the window the right height for its contents, but never
977 more than `temp-buffer-max-height' nor less than `window-min-height'.
978 This applies to `help', `apropos' and `completion' buffers, and some others."
979 :global t :group 'help
980 (if temp-buffer-resize-mode
981 ;; `help-make-xrefs' may add a `back' button and thus increase the
982 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
983 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
984 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
986 (defun resize-temp-buffer-window ()
987 "Resize the selected window to fit its contents.
988 Will not make it higher than `temp-buffer-max-height' nor smaller than
989 `window-min-height'. Do nothing if it is the only window on its frame, if it
990 is not as wide as the frame or if some of the window's contents are scrolled
992 (unless (or (one-window-p 'nomini)
993 (not (pos-visible-in-window-p (point-min)))
994 (not (window-full-width-p)))
995 (fit-window-to-buffer
997 (if (functionp temp-buffer-max-height)
998 (funcall temp-buffer-max-height (current-buffer))
999 temp-buffer-max-height))))
1004 (defcustom help-window-select 'other
1005 "Non-nil means select help window for viewing.
1007 never (nil) Select help window only if there is no other window
1009 other Select help window unless the selected window is the
1010 only other window on its frame.
1011 always (t) Always select the help window.
1013 This option has effect if and only if the help window was created
1014 by `with-help-window'"
1015 :type '(choice (const :tag "never (nil)" nil)
1016 (const :tag "other" other)
1017 (const :tag "always (t)" t))
1021 (defun help-window-display-message (quit-part window &optional other)
1022 "Display message telling how to quit and scroll help window.
1023 QUIT-PART is a string telling how to quit the help window WINDOW.
1024 Optional argument OTHER non-nil means return text telling how to
1025 scroll the \"other\" window."
1028 ((pos-visible-in-window-p
1029 (with-current-buffer (window-buffer window)
1030 (point-max)) window)
1031 ;; Buffer end is visible.
1033 (other ", \\[scroll-other-window] to scroll help.")
1034 (t ", \\[scroll-up] to scroll help."))))
1036 (substitute-command-keys (concat quit-part scroll-part)))))
1038 (defun help-window-setup-finish (window &optional reuse keep-frame)
1039 "Finish setting up help window WINDOW.
1040 Select WINDOW according to the value of `help-window-select'.
1041 Display message telling how to scroll and eventually quit WINDOW.
1043 Optional argument REUSE non-nil means WINDOW has been reused \(by
1044 `display-buffer'\) for displaying help. Optional argument
1045 KEEP-FRAME non-nil means that quitting must no delete the frame
1047 (let ((number-of-windows
1048 (length (window-list (window-frame window) 'no-mini window))))
1050 ((eq window (selected-window))
1051 ;; The help window is the selected window, probably the
1052 ;; `pop-up-windows' nil case.
1053 (help-window-display-message
1055 "Type \"q\" to restore this window"
1056 ;; This should not be taken.
1057 "Type \"q\" to quit") window))
1058 ((= number-of-windows 1)
1059 ;; The help window is alone on a frame and not the selected
1060 ;; window, could be the `pop-up-frames' t case.
1061 (help-window-display-message
1063 (keep-frame "Type \"q\" to delete this window")
1064 (reuse "Type \"q\" to restore this window")
1065 (view-remove-frame-by-deleting "Type \"q\" to delete this frame")
1066 (t "Type \"q\" to iconify this frame"))
1068 ((and (= number-of-windows 2)
1069 (eq (window-frame window) (window-frame (selected-window))))
1070 ;; There are two windows on the help window's frame and the other
1071 ;; window is the selected one.
1072 (if (memq help-window-select '(nil other))
1073 ;; Do not select the help window.
1074 (help-window-display-message
1076 ;; Offer `display-buffer' for consistency with
1077 ;; `help-print-return-message'. This is hardly TRT when
1078 ;; the other window and the selected window display the
1079 ;; same buffer but has been handled this way ever since.
1080 "Type \\[display-buffer] RET to restore the other window"
1081 ;; The classic "two windows" configuration.
1082 "Type \\[delete-other-windows] to delete the help window")
1084 ;; Select help window and tell how to quit.
1085 (select-window window)
1086 (help-window-display-message
1088 "Type \"q\" to restore this window"
1089 "Type \"q\" to delete this window") window)))
1091 ;; Issuing a message with 3 or more windows on the same frame
1092 ;; without selecting the help window doesn't make any sense.
1093 (select-window window)
1094 (help-window-display-message
1096 "Type \"q\" to restore this window"
1097 "Type \"q\" to delete this window") window)))))
1099 (defun help-window-setup (list-of-frames list-of-window-tuples)
1100 "Set up help window.
1101 LIST-OF-FRAMES and LIST-OF-WINDOW-TUPLES are the lists of frames
1102 and window quadruples built by `with-help-window'. The help
1103 window itself is specified by the variable `help-window'."
1104 (let* ((help-buffer (window-buffer help-window))
1105 ;; `help-buffer' now denotes the help window's buffer.
1108 (buffer-local-value 'view-return-to-alist help-buffer)))
1109 (help-entry (assq help-window list-of-window-tuples)))
1111 ;; Handle `help-window-point-marker'.
1112 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1113 (set-window-point help-window help-window-point-marker)
1114 ;; Reset `help-window-point-marker'.
1115 (set-marker help-window-point-marker nil))
1119 ;; `view-return-to-alist' has an entry for the help window.
1121 ((eq help-window (selected-window))
1122 ;; The help window is the selected window, probably because the
1123 ;; user followed a backward/forward button or a cross reference.
1124 ;; In this case just purge stale entries from
1125 ;; `view-return-to-alist' but leave the entry alone and don't
1126 ;; display a message.
1127 (view-return-to-alist-update help-buffer))
1128 ((and help-entry (eq (cadr help-entry) help-buffer))
1129 ;; The help window was not selected but displayed the help
1130 ;; buffer. In this case reuse existing exit information but try
1131 ;; to get back to the selected window when quitting. Don't
1132 ;; display a message since the user must have seen one before.
1133 (view-return-to-alist-update
1134 help-buffer (cons help-window
1135 (cons (selected-window) (cddr view-entry)))))
1137 ;; The help window was not selected, did display the help buffer
1138 ;; earlier, but displayed another buffer when help was invoked.
1139 ;; Set up things so that quitting will show that buffer again.
1140 (view-return-to-alist-update
1141 help-buffer (cons help-window
1142 (cons (selected-window) (cdr help-entry))))
1143 (help-window-setup-finish help-window t))
1145 ;; The help window is new but `view-return-to-alist' had an
1146 ;; entry for it. This should never happen.
1147 (view-return-to-alist-update
1148 help-buffer (cons help-window
1149 (cons (selected-window) 'quit-window)))
1150 (help-window-setup-finish help-window t))))
1152 ;; `view-return-to-alist' does not have an entry for help window
1153 ;; but `list-of-window-tuples' does. Hence `display-buffer' must
1154 ;; have reused an existing window.
1155 (if (eq (cadr help-entry) help-buffer)
1156 ;; The help window displayed `help-buffer' before but no
1157 ;; `view-return-to-alist' entry was found probably because the
1158 ;; user manually switched to the help buffer. Set up things
1159 ;; for `quit-window' although `view-exit-action' should be
1160 ;; able to handle this case all by itself.
1162 (view-return-to-alist-update
1163 help-buffer (cons help-window
1164 (cons (selected-window) 'quit-window)))
1165 (help-window-setup-finish help-window t))
1166 ;; The help window displayed another buffer before. Set up
1167 ;; things in a way that quitting can orderly show that buffer
1168 ;; again. The window-start and window-point information from
1169 ;; `list-of-window-tuples' provide the necessary information.
1170 (view-return-to-alist-update
1171 help-buffer (cons help-window
1172 (cons (selected-window) (cdr help-entry))))
1173 (help-window-setup-finish help-window t)))
1174 ((memq (window-frame help-window) list-of-frames)
1175 ;; The help window is a new window on an existing frame. This
1176 ;; case must be handled specially by `help-window-setup-finish'
1177 ;; and `view-mode-exit' to ascertain that quitting does _not_
1178 ;; inadvertently delete the frame.
1179 (view-return-to-alist-update
1180 help-buffer (cons help-window
1181 (cons (selected-window) 'keep-frame)))
1182 (help-window-setup-finish help-window nil t))
1184 ;; The help window is shown on a new frame. In this case quitting
1185 ;; shall handle both, the help window _and_ its frame. We changed
1186 ;; the default of `view-remove-frame-by-deleting' to t in order to
1187 ;; intuitively DTRT here.
1188 (view-return-to-alist-update
1189 help-buffer (cons help-window (cons (selected-window) t)))
1190 (help-window-setup-finish help-window)))))
1192 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1193 ;; providing the following additional twists:
1195 ;; (1) Issue more accurate messages telling how to scroll and quit the
1198 ;; (2) Make `view-mode-exit' DTRT in more cases.
1200 ;; (3) An option (customizable via `help-window-select') to select the
1201 ;; help window automatically.
1203 ;; (4) A marker (`help-window-point-marker') to move point in the help
1204 ;; window to an arbitrary buffer position.
1206 ;; Note: It's usually always wrong to use `help-print-return-message' in
1207 ;; the body of `with-help-window'.
1208 (defmacro with-help-window (buffer-name &rest body)
1209 "Display buffer BUFFER-NAME in a help window evaluating BODY.
1210 Select help window if the actual value of the user option
1211 `help-window-select' says so. Return last value in BODY."
1212 (declare (indent 1) (debug t))
1213 ;; Bind list-of-frames to `frame-list' and list-of-window-tuples to a
1214 ;; list of one <window window-buffer window-start window-point> tuple
1215 ;; for each live window.
1216 `(let ((list-of-frames (frame-list))
1217 (list-of-window-tuples
1221 (push (list window (window-buffer window)
1222 (window-start window) (window-point window))
1226 ;; Make `help-window' t to trigger `help-mode-finish' to set
1227 ;; `help-window' to the actual help window.
1228 (setq help-window t)
1229 ;; Make `help-window-point-marker' point nowhere (the only place
1230 ;; where this should be set to a buffer position is within BODY).
1231 (set-marker help-window-point-marker nil)
1233 ;; Return value returned by `with-output-to-temp-buffer'.
1234 (with-output-to-temp-buffer ,buffer-name
1236 (when (windowp help-window)
1237 ;; Set up help window.
1238 (help-window-setup list-of-frames list-of-window-tuples))
1239 ;; Reset `help-window' to nil to avoid confusing future calls of
1240 ;; `help-mode-finish' with plain `with-output-to-temp-buffer'.
1241 (setq help-window nil))))
1245 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1246 ;;; help.el ends here