1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
7 ;; Keywords: help, internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This code implements GNU Emacs' on-line help system, the one invoked by
29 ;; `M-x help-for-help'.
33 ;; Get the macro make-help-screen when this is compiled,
34 ;; or run interpreted, but not when the compiled code is loaded.
35 (eval-when-compile (require 'help-macro
))
37 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
38 (add-hook 'temp-buffer-setup-hook
'help-mode-setup
)
39 (add-hook 'temp-buffer-show-hook
'help-mode-finish
)
41 ;; The variable `help-window' below is used by `help-mode-finish' to
42 ;; communicate the window displaying help (the "help window") to the
43 ;; macro `with-help-window'. The latter sets `help-window' to t before
44 ;; invoking `with-output-to-temp-buffer'. If and only if `help-window'
45 ;; is eq to t, `help-mode-finish' (called by `temp-buffer-setup-hook')
46 ;; sets `help-window' to the window selected by `display-buffer'.
47 ;; Exiting `with-help-window' and calling `print-help-return-message'
48 ;; reset `help-window' to nil.
49 (defvar help-window nil
50 "Window chosen for displaying help.")
52 ;; `help-window-point-marker' is a marker you can move to a valid
53 ;; position of the buffer shown in the help window in order to override
54 ;; the standard positioning mechanism (`point-min') chosen by
55 ;; `with-output-to-temp-buffer'. `with-help-window' has this point
56 ;; nowhere before exiting. Currently used by `view-lossage' to assert
57 ;; that the last keystrokes are always visible.
58 (defvar help-window-point-marker
(make-marker)
59 "Marker to override default `window-point' of `help-window'.")
62 (let ((map (make-sparse-keymap)))
63 (define-key map
(char-to-string help-char
) 'help-for-help
)
64 (define-key map
[help] 'help-for-help)
65 (define-key map [f1] 'help-for-help)
66 (define-key map "." 'display-local-help)
67 (define-key map "?" 'help-for-help)
69 (define-key map "\C-a" 'about-emacs)
70 (define-key map "\C-c" 'describe-copying)
71 (define-key map "\C-d" 'view-emacs-debugging)
72 (define-key map "\C-e" 'view-external-packages)
73 (define-key map "\C-f" 'view-emacs-FAQ)
74 (define-key map "\C-m" 'view-order-manuals)
75 (define-key map "\C-n" 'view-emacs-news)
76 (define-key map "\C-o" 'describe-distribution)
77 (define-key map "\C-p" 'view-emacs-problems)
78 (define-key map "\C-t" 'view-emacs-todo)
79 (define-key map "\C-w" 'describe-no-warranty)
81 ;; This does not fit the pattern, but it is natural given the C-\ command.
82 (define-key map "\C-\\" 'describe-input-method)
84 (define-key map "C" 'describe-coding-system)
85 (define-key map "F" 'Info-goto-emacs-command-node)
86 (define-key map "I" 'describe-input-method)
87 (define-key map "K" 'Info-goto-emacs-key-command-node)
88 (define-key map "L" 'describe-language-environment)
89 (define-key map "S" 'info-lookup-symbol)
91 (define-key map "a" 'apropos-command)
92 (define-key map "b" 'describe-bindings)
93 (define-key map "c" 'describe-key-briefly)
94 (define-key map "d" 'apropos-documentation)
95 (define-key map "e" 'view-echo-area-messages)
96 (define-key map "f" 'describe-function)
97 (define-key map "g" 'describe-gnu-project)
98 (define-key map "h" 'view-hello-file)
100 (define-key map "i" 'info)
101 (define-key map "4i" 'info-other-window)
103 (define-key map "k" 'describe-key)
104 (define-key map "l" 'view-lossage)
105 (define-key map "m" 'describe-mode)
106 (define-key map "n" 'view-emacs-news)
107 (define-key map "p" 'finder-by-keyword)
108 (define-key map "r" 'info-emacs-manual)
109 (define-key map "s" 'describe-syntax)
110 (define-key map "t" 'help-with-tutorial)
111 (define-key map "w" 'where-is)
112 (define-key map "v" 'describe-variable)
113 (define-key map "q" 'help-quit)
115 "Keymap for characters following the Help key.")
117 (define-key global-map (char-to-string help-char) 'help-command)
118 (define-key global-map [help] 'help-command
)
119 (define-key global-map
[f1] 'help-command)
120 (fset 'help-command help-map)
122 (autoload 'finder-by-keyword "finder"
123 "Find packages matching a given keyword." t)
125 ;; insert-button makes the action nil if it is not store somewhere
126 (defvar help-button-cache nil)
130 "Just exit from the Help command's command loop."
134 (defvar help-return-method nil
135 "What to do to \"exit\" the help buffer.
137 (WINDOW . t) delete the selected window (and possibly its frame,
138 see `quit-window' and `View-quit'), go to WINDOW.
139 (WINDOW . quit-window) do quit-window, then select WINDOW.
140 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
142 (defun print-help-return-message (&optional function)
143 "Display or return message saying how to restore windows after help command.
144 This function assumes that `standard-output' is the help buffer.
145 It computes a message, and applies the optional argument FUNCTION to it.
146 If FUNCTION is nil, it applies `message', thus displaying the message.
147 In addition, this function sets up `help-return-method', which see, that
148 specifies what to do when the user exits the help buffer."
149 ;; Reset `help-window' here to avoid confusing `help-mode-finish'.
150 (setq help-window nil)
151 (and (not (get-buffer-window standard-output))
155 (special-display-p (buffer-name standard-output)))
156 (setq help-return-method (cons (selected-window) t))
157 ;; If the help output buffer is a special display buffer,
158 ;; don't say anything about how to get rid of it.
159 ;; First of all, the user will do that with the window
160 ;; manager, not with Emacs.
161 ;; Secondly, the buffer has not been displayed yet,
162 ;; so we don't know whether its frame will be selected.
164 (display-buffer-reuse-frames
165 (setq help-return-method (cons (selected-window)
168 ((not (one-window-p t))
169 (setq help-return-method
170 (cons (selected-window) 'quit-window))
171 "Type \\[display-buffer] RET to restore the other window.")
173 (setq help-return-method (cons (selected-window) t))
174 "Type \\[delete-other-windows] to remove help window.")
176 (setq help-return-method
177 (list (selected-window) (window-buffer)
178 (window-start) (window-point)))
179 "Type \\[switch-to-buffer] RET to remove help window."))))
180 (funcall (or function 'message)
183 (substitute-command-keys first-message))
184 (if first-message " ")
185 ;; If the help buffer will go in a separate frame,
186 ;; it's no use mentioning a command to scroll, so don't.
187 (if (or pop-up-windows
188 (special-display-p (buffer-name standard-output)))
190 (if (same-window-p (buffer-name standard-output))
191 ;; Say how to scroll this window.
192 (substitute-command-keys
193 "\\[scroll-up] to scroll the help.")
194 ;; Say how to scroll some other window.
195 (substitute-command-keys
196 "\\[scroll-other-window] to scroll the help."))))))))
198 ;; So keyboard macro definitions are documented correctly
199 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
201 (defalias 'help 'help-for-help-internal)
202 ;; find-function can find this.
203 (defalias 'help-for-help 'help-for-help-internal)
204 ;; It can't find this, but nobody will look.
205 (make-help-screen help-for-help-internal
206 "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?"
207 "You have typed %THIS-KEY%, the help character. Type a Help option:
208 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
210 a PATTERN Show commands whose name matches the PATTERN (a list of words
211 or a regexp). See also the `apropos' command.
212 b Display all key bindings.
213 c KEYS Display the command name run by the given key sequence.
214 C CODING Describe the given coding system, or RET for current ones.
215 d PATTERN Show a list of functions, variables, and other items whose
216 documentation matches the PATTERN (a list of words or a regexp).
217 e Go to the *Messages* buffer which logs echo-area messages.
218 f FUNCTION Display documentation for the given function.
219 F COMMAND Show the on-line manual's section that describes the command.
220 g Display information about the GNU project.
221 h Display the HELLO file which illustrates various scripts.
222 i Start the Info documentation reader: read on-line manuals.
223 I METHOD Describe a specific input method, or RET for current.
224 k KEYS Display the full documentation for the key sequence.
225 K KEYS Show the on-line manual's section for the command bound to KEYS.
226 l Show last 100 characters you typed (lossage).
227 L LANG-ENV Describes a specific language environment, or RET for current.
228 m Display documentation of current minor modes and current major mode,
229 including their special commands.
230 n Display news of recent Emacs changes.
231 p TOPIC Find packages matching a given topic keyword.
232 r Display the Emacs manual in Info mode.
233 s Display contents of current syntax table, plus explanations.
234 S SYMBOL Show the section for the given symbol in the on-line manual
235 for the programming language used in this buffer.
236 t Start the Emacs learn-by-doing tutorial.
237 v VARIABLE Display the given variable's documentation and value.
238 w COMMAND Display which keystrokes invoke the given command (where-is).
239 . Display any available local help at point in the echo area.
241 C-a Information about Emacs.
242 C-c Emacs copying permission (GNU General Public License).
243 C-d Instructions for debugging GNU Emacs.
244 C-e External packages and information about Emacs.
246 C-m How to order printed Emacs manuals.
247 C-n News of recent Emacs changes.
248 C-o Emacs ordering and distribution information.
249 C-p Info about known Emacs problems.
251 C-w Information on absence of warranty for GNU Emacs."
256 (defun function-called-at-point ()
257 "Return a function around point or else called by the list containing point.
258 If that doesn't give a function, return nil."
259 (or (with-syntax-table emacs-lisp-mode-syntax-table
260 (or (condition-case ()
262 (or (not (zerop (skip-syntax-backward "_w")))
263 (eq (char-syntax (following-char)) ?w)
264 (eq (char-syntax (following-char)) ?_)
266 (skip-chars-forward "'")
267 (let ((obj (read (current-buffer))))
268 (and (symbolp obj) (fboundp obj) obj)))
273 (narrow-to-region (max (point-min)
274 (- (point) 1000)) (point-max))
275 ;; Move up to surrounding paren, then after the open.
278 ;; If there is space here, this is probably something
279 ;; other than a real Lisp function call, so ignore it.
280 (if (looking-at "[ \t]")
281 (error "Probably not a Lisp function call"))
282 (let ((obj (read (current-buffer))))
283 (and (symbolp obj) (fboundp obj) obj))))
285 (let* ((str (find-tag-default))
286 (sym (if str (intern-soft str))))
287 (if (and sym (fboundp sym))
290 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
291 (setq sym (intern-soft (match-string 1 str)))
292 (and (fboundp sym) sym)))))))
295 ;;; `User' help functions
297 (defun view-help-file (file &optional dir)
298 (view-file (expand-file-name file (or dir data-directory)))
300 (goto-char (point-min)))
302 (defun describe-distribution ()
303 "Display info on how to obtain the latest version of GNU Emacs."
305 (view-help-file "DISTRIB"))
307 (defun describe-copying ()
308 "Display info on how you may redistribute copies of GNU Emacs."
310 (view-help-file "COPYING"))
312 (defalias 'describe-project 'describe-gnu-project)
313 (defun describe-gnu-project ()
314 "Display info on the GNU project."
316 (view-help-file "THE-GNU-PROJECT"))
318 (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 (defalias 'view-todo 'view-emacs-todo)
416 (defun view-emacs-todo (&optional arg)
417 "Display the Emacs TODO list."
419 (view-help-file "TODO"))
421 (defun view-echo-area-messages ()
422 "View the log of recent echo-area messages: the `*Messages*' buffer.
423 The number of messages retained in that buffer
424 is specified by the variable `message-log-max'."
426 (switch-to-buffer (get-buffer-create "*Messages*")))
428 (defun view-order-manuals ()
429 "Display the Emacs ORDERS file."
431 (view-help-file "ORDERS"))
433 (defun view-emacs-FAQ ()
434 "Display the Emacs Frequently Asked Questions (FAQ) file."
436 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
439 (defun view-emacs-problems ()
440 "Display info on known problems with Emacs and possible workarounds."
442 (view-help-file "PROBLEMS"))
444 (defun view-emacs-debugging ()
445 "Display info on how to debug Emacs problems."
447 (view-help-file "DEBUG"))
449 (defun view-external-packages ()
450 "Display external packages and information about Emacs."
452 (view-help-file "MORE.STUFF"))
454 (defun view-lossage ()
455 "Display last 100 input keystrokes.
457 To record all your input on a file, use `open-dribble-file'."
459 (help-setup-xref (list #'view-lossage) (interactive-p))
460 (with-help-window (help-buffer)
461 (princ (mapconcat (lambda (key)
462 (if (or (integerp key) (symbolp key) (listp key))
463 (single-key-description key)
464 (prin1-to-string key nil)))
467 (with-current-buffer standard-output
468 (goto-char (point-min))
469 (while (progn (move-to-column 50) (not (eobp)))
470 (when (search-forward " " nil t)
473 ;; jidanni wants to see the last keystrokes immediately.
474 (set-marker help-window-point-marker (point)))))
479 (defun describe-bindings (&optional prefix buffer)
480 "Show a list of all defined keys, and their definitions.
481 We put that list in a buffer, and display the buffer.
483 The optional argument PREFIX, if non-nil, should be a key sequence;
484 then we display only bindings that start with that prefix.
485 The optional argument BUFFER specifies which buffer's bindings
486 to display (default, the current buffer). BUFFER can be a buffer
489 (or buffer (setq buffer (current-buffer)))
490 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
491 (with-current-buffer buffer
492 (describe-bindings-internal nil prefix)))
494 ;; This function used to be in keymap.c.
495 (defun describe-bindings-internal (&optional menus prefix)
496 "Show a list of all defined keys, and their definitions.
497 We put that list in a buffer, and display the buffer.
499 The optional argument MENUS, if non-nil, says to mention menu bindings.
500 \(Ordinarily these are omitted from the output.)
501 The optional argument PREFIX, if non-nil, should be a key sequence;
502 then we display only bindings that start with that prefix."
504 (let ((buf (current-buffer)))
505 (with-help-window "*Help*"
506 (with-current-buffer standard-output
507 (describe-buffer-bindings buf prefix menus)))))
509 (defun where-is (definition &optional insert)
510 "Print message listing key sequences that invoke the command DEFINITION.
511 Argument is a command definition, usually a symbol with a function definition.
512 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
514 (let ((fn (function-called-at-point))
515 (enable-recursive-minibuffers t)
517 (setq val (completing-read
519 (format "Where is command (default %s): " fn)
520 "Where is command: ")
521 obarray 'commandp t))
522 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
523 (unless definition (error "No command"))
524 (let ((func (indirect-function definition))
526 (standard-output (if insert (current-buffer) t)))
527 ;; In DEFS, find all symbols that are aliases for DEFINITION.
528 (mapatoms (lambda (symbol)
529 (and (fboundp symbol)
530 (not (eq symbol definition))
531 (eq func (condition-case ()
532 (indirect-function symbol)
534 (push symbol defs))))
535 ;; Look at all the symbols--first DEFINITION,
537 (dolist (symbol (cons definition defs))
538 (let* ((remapped (command-remapping symbol))
539 (keys (where-is-internal
540 symbol overriding-local-map nil nil remapped))
541 (keys (mapconcat 'key-description keys ", "))
545 (if (> (length keys) 0)
547 (format "%s (%s) (remapped from %s)"
548 keys remapped symbol)
549 (format "%s (%s)" keys symbol))
550 (format "M-x %s RET" symbol))
551 (if (> (length keys) 0)
553 (format "%s is remapped to %s which is on %s"
554 symbol remapped keys)
555 (format "%s is on %s" symbol keys))
556 ;; If this is the command the user asked about,
557 ;; and it is not on any key, say so.
558 ;; For other symbols, its aliases, say nothing
559 ;; about them unless they are on keys.
560 (if (eq symbol definition)
561 (format "%s is not on any key" symbol)))))
563 (unless (eq symbol definition)
564 (princ ";\n its alias "))
568 (defun help-key-description (key untranslated)
569 (let ((string (key-description key)))
570 (if (or (not untranslated)
571 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
573 (let ((otherstring (key-description untranslated)))
574 (if (equal string otherstring)
576 (format "%s (translated from %s)" string otherstring))))))
578 (defun describe-key-briefly (&optional key insert untranslated)
579 "Print the name of the function KEY invokes. KEY is a string.
580 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
581 If non-nil, UNTRANSLATED is a vector of the untranslated events.
582 It can also be a number in which case the untranslated events from
583 the last key hit are used.
585 If KEY is a menu item or a tool-bar button that is disabled, this command
586 temporarily enables it to allow getting help on disabled items and buttons."
588 (let ((enable-disabled-menus-and-buttons t)
589 (cursor-in-echo-area t)
593 ;; If yank-menu is empty, populate it temporarily, so that
594 ;; "Select and Paste" menu can generate a complete event.
595 (when (null (cdr yank-menu))
596 (setq saved-yank-menu (copy-sequence yank-menu))
597 (menu-bar-update-yank-menu "(any string)" nil))
598 (setq key (read-key-sequence "Describe key (or click or menu item): "))
599 ;; If KEY is a down-event, read and discard the
600 ;; corresponding up-event. Note that there are also
601 ;; down-events on scroll bars and mode lines: the actual
602 ;; event then is in the second element of the vector.
604 (let ((last-idx (1- (length key))))
605 (and (eventp (aref key last-idx))
606 (memq 'down (event-modifiers (aref key last-idx)))))
610 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
612 ;; Put yank-menu back as it was, if we changed it.
613 (when saved-yank-menu
614 (setq yank-menu (copy-sequence saved-yank-menu))
615 (fset 'yank-menu (cons 'keymap yank-menu))))))
616 (if (numberp untranslated)
617 (setq untranslated (this-single-command-raw-keys)))
618 (let* ((event (if (and (symbolp (aref key 0))
620 (consp (aref key 1)))
623 (modifiers (event-modifiers event))
624 (standard-output (if insert (current-buffer) t))
625 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
626 (memq 'drag modifiers)) " at that spot" ""))
627 (defn (key-binding key t))
629 ;; Handle the case where we faked an entry in "Select and Paste" menu.
630 (if (and (eq defn nil)
631 (stringp (aref key (1- (length key))))
632 (eq (key-binding (substring key 0 -1)) 'yank-menu))
633 (setq defn 'menu-bar-select-yank))
634 ;; Don't bother user with strings from (e.g.) the select-paste menu.
635 (if (stringp (aref key (1- (length key))))
636 (aset key (1- (length key)) "(any string)"))
637 (if (and (> (length untranslated) 0)
638 (stringp (aref untranslated (1- (length untranslated)))))
639 (aset untranslated (1- (length untranslated)) "(any string)"))
640 ;; Now describe the key, perhaps as changed.
641 (setq key-desc (help-key-description key untranslated))
642 (if (or (null defn) (integerp defn) (equal defn 'undefined))
643 (princ (format "%s%s is undefined" key-desc mouse-msg))
644 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
646 (defun describe-key (&optional key untranslated up-event)
647 "Display documentation of the function invoked by KEY.
648 KEY can be any kind of a key sequence; it can include keyboard events,
649 mouse events, and/or menu events. When calling from a program,
650 pass KEY as a string or a vector.
652 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
653 It can also be a number, in which case the untranslated events from
654 the last key sequence entered are used.
655 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
657 If KEY is a menu item or a tool-bar button that is disabled, this command
658 temporarily enables it to allow getting help on disabled items and buttons."
660 (let ((enable-disabled-menus-and-buttons t)
661 (cursor-in-echo-area t)
665 ;; If yank-menu is empty, populate it temporarily, so that
666 ;; "Select and Paste" menu can generate a complete event.
667 (when (null (cdr yank-menu))
668 (setq saved-yank-menu (copy-sequence yank-menu))
669 (menu-bar-update-yank-menu "(any string)" nil))
670 (setq key (read-key-sequence "Describe key (or click or menu item): "))
673 (prefix-numeric-value current-prefix-arg)
674 ;; If KEY is a down-event, read and include the
675 ;; corresponding up-event. Note that there are also
676 ;; down-events on scroll bars and mode lines: the actual
677 ;; event then is in the second element of the vector.
679 (let ((last-idx (1- (length key))))
680 (and (eventp (aref key last-idx))
681 (memq 'down (event-modifiers (aref key last-idx)))))
682 (or (and (eventp (aref key 0))
683 (memq 'down (event-modifiers (aref key 0)))
684 ;; However, for the C-down-mouse-2 popup
685 ;; menu, there is no subsequent up-event. In
686 ;; this case, the up-event is the next
687 ;; element in the supplied vector.
689 (and (> (length key) 1)
690 (eventp (aref key 1))
691 (memq 'down (event-modifiers (aref key 1)))))
693 ;; Put yank-menu back as it was, if we changed it.
694 (when saved-yank-menu
695 (setq yank-menu (copy-sequence saved-yank-menu))
696 (fset 'yank-menu (cons 'keymap yank-menu))))))
697 (if (numberp untranslated)
698 (setq untranslated (this-single-command-raw-keys)))
699 (let* ((event (aref key (if (and (symbolp (aref key 0))
701 (consp (aref key 1)))
704 (modifiers (event-modifiers event))
705 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
706 (memq 'drag modifiers)) " at that spot" ""))
707 (defn (key-binding key t))
708 defn-up defn-up-tricky ev-type
709 mouse-1-remapped mouse-1-tricky)
711 ;; Handle the case where we faked an entry in "Select and Paste" menu.
712 (when (and (eq defn nil)
713 (stringp (aref key (1- (length key))))
714 (eq (key-binding (substring key 0 -1)) 'yank-menu))
715 (setq defn 'menu-bar-select-yank))
716 (if (or (null defn) (integerp defn) (equal defn 'undefined))
717 (message "%s%s is undefined"
718 (help-key-description key untranslated) mouse-msg)
719 (help-setup-xref (list #'describe-function defn) (interactive-p))
720 ;; Don't bother user with strings from (e.g.) the select-paste menu.
721 (when (stringp (aref key (1- (length key))))
722 (aset key (1- (length key)) "(any string)"))
723 (when (and untranslated
724 (stringp (aref untranslated (1- (length untranslated)))))
725 (aset untranslated (1- (length untranslated))
727 ;; Need to do this before erasing *Help* buffer in case event
728 ;; is a mouse click in an existing *Help* buffer.
730 (setq ev-type (event-basic-type up-event))
731 (let ((sequence (vector up-event)))
732 (when (and (eq ev-type 'mouse-1)
733 mouse-1-click-follows-link
734 (not (eq mouse-1-click-follows-link 'double))
735 (setq mouse-1-remapped
736 (mouse-on-link-p (event-start up-event))))
737 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
738 (> mouse-1-click-follows-link 0)))
739 (cond ((stringp mouse-1-remapped)
740 (setq sequence mouse-1-remapped))
741 ((vectorp mouse-1-remapped)
742 (setcar up-event (elt mouse-1-remapped 0)))
743 (t (setcar up-event 'mouse-2))))
744 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
746 (setq sequence (vector up-event))
747 (aset sequence 0 'mouse-1)
748 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
749 (with-help-window (help-buffer)
750 (princ (help-key-description key untranslated))
752 %s runs the command %S, which is "
754 (describe-function-1 defn)
756 (unless (or (null defn-up)
758 (equal defn-up 'undefined))
761 ----------------- up-event %s----------------
763 <%S>%s%s runs the command %S, which is "
764 (if mouse-1-tricky "(short click) " "")
767 " is remapped to <mouse-2>, which" "")
769 (describe-function-1 defn-up))
770 (unless (or (null defn-up-tricky)
771 (integerp defn-up-tricky)
772 (eq defn-up-tricky 'undefined))
775 ----------------- up-event (long click) ----------------
777 Pressing <%S>%s for longer than %d milli-seconds
778 runs the command %S, which is "
780 mouse-1-click-follows-link
782 (describe-function-1 defn-up-tricky)))))))
784 (defun describe-mode (&optional buffer)
785 "Display documentation of current major mode and minor modes.
786 A brief summary of the minor modes comes first, followed by the
787 major mode description. This is followed by detailed
788 descriptions of the minor modes, each on a separate page.
790 For this to work correctly for a minor mode, the mode's indicator
791 variable \(listed in `minor-mode-alist') must also be a function
792 whose documentation describes the minor mode."
794 (unless buffer (setq buffer (current-buffer)))
795 (help-setup-xref (list #'describe-mode buffer)
797 ;; For the sake of help-do-xref and help-xref-go-back,
798 ;; don't switch buffers before calling `help-buffer'.
799 (with-help-window (help-buffer)
800 (with-current-buffer buffer
802 ;; Older packages do not register in minor-mode-list but only in
804 (dolist (x minor-mode-alist)
806 (unless (memq x minor-mode-list)
807 (push x minor-mode-list)))
808 ;; Find enabled minor mode we will want to mention.
809 (dolist (mode minor-mode-list)
810 ;; Document a minor mode if it is listed in minor-mode-alist,
811 ;; non-nil, and has a function definition.
812 (let ((fmode (or (get mode :minor-mode-function) mode)))
813 (and (boundp mode) (symbol-value mode)
815 (let ((pretty-minor-mode
816 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
819 (substring (symbol-name fmode)
820 0 (match-beginning 0)))
822 (push (list fmode pretty-minor-mode
823 (format-mode-line (assq mode minor-mode-alist)))
827 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
829 (princ "Enabled minor modes:\n")
830 (make-local-variable 'help-button-cache)
831 (with-current-buffer standard-output
832 (dolist (mode minor-modes)
833 (let ((mode-function (nth 0 mode))
834 (pretty-minor-mode (nth 1 mode))
835 (indicator (nth 2 mode)))
836 (add-text-properties 0 (length pretty-minor-mode)
837 '(face bold) pretty-minor-mode)
839 (goto-char (point-max))
841 (push (point-marker) help-button-cache)
842 ;; Document the minor modes fully.
843 (insert pretty-minor-mode)
844 (princ (format " minor mode (%s):\n"
845 (if (zerop (length indicator))
847 (format "indicator%s"
849 (princ (documentation mode-function)))
850 (insert-button pretty-minor-mode
851 'action (car help-button-cache)
853 'help-echo "mouse-2, RET: show full information")
859 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
860 ;; Document the major mode.
861 (let ((mode mode-name))
862 (with-current-buffer standard-output
863 (let ((start (point)))
864 (insert (format-mode-line mode nil nil buffer))
865 (add-text-properties start (point) '(face bold)))))
867 (princ (documentation major-mode))))))
870 (defun describe-minor-mode (minor-mode)
871 "Display documentation of a minor mode given as MINOR-MODE.
872 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
873 appeared on the mode-line."
874 (interactive (list (completing-read
877 (describe-minor-mode-completion-table-for-symbol)
878 (describe-minor-mode-completion-table-for-indicator)
880 (if (symbolp minor-mode)
881 (setq minor-mode (symbol-name minor-mode)))
882 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
883 (indicators (describe-minor-mode-completion-table-for-indicator)))
885 ((member minor-mode symbols)
886 (describe-minor-mode-from-symbol (intern minor-mode)))
887 ((member minor-mode indicators)
888 (describe-minor-mode-from-indicator minor-mode))
890 (error "No such minor mode: %s" minor-mode)))))
893 (defun describe-minor-mode-completion-table-for-symbol ()
894 ;; In order to list up all minor modes, minor-mode-list
895 ;; is used here instead of minor-mode-alist.
896 (delq nil (mapcar 'symbol-name minor-mode-list)))
897 (defun describe-minor-mode-from-symbol (symbol)
898 "Display documentation of a minor mode given as a symbol, SYMBOL"
899 (interactive (list (intern (completing-read
900 "Minor mode symbol: "
901 (describe-minor-mode-completion-table-for-symbol)))))
903 (describe-function symbol)
904 (describe-variable symbol)))
907 (defun describe-minor-mode-completion-table-for-indicator ()
910 (let ((i (format-mode-line x)))
911 ;; remove first space if existed
920 (defun describe-minor-mode-from-indicator (indicator)
921 "Display documentation of a minor mode specified by INDICATOR.
922 If you call this function interactively, you can give indicator which
923 is currently activated with completion."
926 "Minor mode indicator: "
927 (describe-minor-mode-completion-table-for-indicator))))
928 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
930 (describe-minor-mode-from-symbol minor-mode)
931 (error "Cannot find minor mode for `%s'" indicator))))
933 (defun lookup-minor-mode-from-indicator (indicator)
934 "Return a minor mode symbol from its indicator on the modeline."
935 ;; remove first space if existed
936 (if (and (< 0 (length indicator))
937 (eq (aref indicator 0) ?\s))
938 (setq indicator (substring indicator 1)))
939 (let ((minor-modes minor-mode-alist)
942 (let* ((minor-mode (car (car minor-modes)))
943 (anindicator (format-mode-line
944 (car (cdr (car minor-modes))))))
945 ;; remove first space if existed
946 (if (and (stringp anindicator)
947 (> (length anindicator) 0)
948 (eq (aref anindicator 0) ?\s))
949 (setq anindicator (substring anindicator 1)))
950 (if (equal indicator anindicator)
951 (setq result minor-mode
953 (setq minor-modes (cdr minor-modes)))))
957 ;;; Automatic resizing of temporary buffers.
959 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
960 "Maximum height of a window displaying a temporary buffer.
961 This is effective only when Temp Buffer Resize mode is enabled.
962 The value is the maximum height (in lines) which `resize-temp-buffer-window'
963 will give to a window displaying a temporary buffer.
964 It can also be a function to be called to choose the height for such a buffer.
965 It gets one argumemt, the buffer, and should return a positive integer."
966 :type '(choice integer function)
970 (define-minor-mode temp-buffer-resize-mode
971 "Toggle the mode which makes windows smaller for temporary buffers.
972 With prefix argument ARG, turn the resizing of windows displaying temporary
973 buffers on if ARG is positive or off otherwise.
974 This makes the window the right height for its contents, but never
975 more than `temp-buffer-max-height' nor less than `window-min-height'.
976 This applies to `help', `apropos' and `completion' buffers, and some others."
977 :global t :group 'help
978 (if temp-buffer-resize-mode
979 ;; `help-make-xrefs' may add a `back' button and thus increase the
980 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
981 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
982 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
984 (defun resize-temp-buffer-window ()
985 "Resize the selected window to fit its contents.
986 Will not make it higher than `temp-buffer-max-height' nor smaller than
987 `window-min-height'. Do nothing if it is the only window on its frame, if it
988 is not as wide as the frame or if some of the window's contents are scrolled
990 (unless (or (one-window-p 'nomini)
991 (not (pos-visible-in-window-p (point-min)))
992 (not (window-full-width-p)))
993 (fit-window-to-buffer
995 (if (functionp temp-buffer-max-height)
996 (funcall temp-buffer-max-height (current-buffer))
997 temp-buffer-max-height))))
1002 (defcustom help-window-select 'other
1003 "Non-nil means select help window for viewing.
1005 never (nil) Select help window only if there is no other window
1007 other Select help window unless the selected window is the
1008 only other window on its frame.
1009 always (t) Always select the help window.
1011 This option has effect if and only if the help window was created
1012 by `with-help-window'"
1013 :type '(choice (const :tag "never (nil)" nil)
1014 (const :tag "other" other)
1015 (const :tag "always (t)" t))
1019 (defun help-window-display-message (quit-part window &optional other)
1020 "Display message telling how to quit and scroll help window.
1021 QUIT-PART is a string telling how to quit the help window WINDOW.
1022 Optional argument OTHER non-nil means return text telling how to
1023 scroll the \"other\" window."
1026 ((pos-visible-in-window-p
1027 (with-current-buffer (window-buffer window)
1028 (point-max)) window)
1029 ;; Buffer end is visible.
1031 (other ", \\[scroll-other-window] to scroll help.")
1032 (t ", \\[scroll-up] to scroll help."))))
1034 (substitute-command-keys (concat quit-part scroll-part)))))
1036 (defun help-window-setup-finish (window &optional reuse keep-frame)
1037 "Finish setting up help window WINDOW.
1038 Select WINDOW according to the value of `help-window-select'.
1039 Display message telling how to scroll and eventually quit WINDOW.
1041 Optional argument REUSE non-nil means WINDOW has been reused \(by
1042 `display-buffer'\) for displaying help. Optional argument
1043 KEEP-FRAME non-nil means that quitting must no delete the frame
1045 (let ((number-of-windows
1046 (length (window-list (window-frame window) 'no-mini window))))
1048 ((eq window (selected-window))
1049 ;; The help window is the selected window, probably the
1050 ;; `pop-up-windows' nil case.
1051 (help-window-display-message
1053 "Type \"q\" to restore this window"
1054 ;; This should not be taken.
1055 "Type \"q\" to quit") window))
1056 ((= number-of-windows 1)
1057 ;; The help window is alone on a frame and not the selected
1058 ;; window, could be the `pop-up-frames' t case.
1059 (help-window-display-message
1061 (keep-frame "Type \"q\" to delete this window")
1062 (reuse "Type \"q\" to restore this window")
1063 (view-remove-frame-by-deleting "Type \"q\" to delete this frame")
1064 (t "Type \"q\" to iconify this frame"))
1066 ((and (= number-of-windows 2)
1067 (eq (window-frame window) (window-frame (selected-window))))
1068 ;; There are two windows on the help window's frame and the other
1069 ;; window is the selected one.
1070 (if (memq help-window-select '(nil other))
1071 ;; Do not select the help window.
1072 (help-window-display-message
1074 ;; Offer `display-buffer' for consistency with
1075 ;; `print-help-return-message'. This is hardly TRT when
1076 ;; the other window and the selected window display the
1077 ;; same buffer but has been handled this way ever since.
1078 "Type \\[display-buffer] RET to restore the other window"
1079 ;; The classic "two windows" configuration.
1080 "Type \\[delete-other-windows] to delete the help window")
1082 ;; Select help window and tell how to quit.
1083 (select-window window)
1084 (help-window-display-message
1086 "Type \"q\" to restore this window"
1087 "Type \"q\" to delete this window") window)))
1089 ;; Issuing a message with 3 or more windows on the same frame
1090 ;; without selecting the help window doesn't make any sense.
1091 (select-window window)
1092 (help-window-display-message
1094 "Type \"q\" to restore this window"
1095 "Type \"q\" to delete this window") window)))))
1097 (defun help-window-setup (list-of-frames list-of-window-tuples)
1098 "Set up help window.
1099 LIST-OF-FRAMES and LIST-OF-WINDOW-TUPLES are the lists of frames
1100 and window quadruples built by `with-help-window'. The help
1101 window itself is specified by the variable `help-window'."
1102 (let* ((help-buffer (window-buffer help-window))
1103 ;; `help-buffer' now denotes the help window's buffer.
1106 (buffer-local-value 'view-return-to-alist help-buffer)))
1107 (help-entry (assq help-window list-of-window-tuples)))
1109 ;; Handle `help-window-point-marker'.
1110 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1111 (set-window-point help-window help-window-point-marker)
1112 ;; Reset `help-window-point-marker'.
1113 (set-marker help-window-point-marker nil))
1117 ;; `view-return-to-alist' has an entry for the help window.
1119 ((eq help-window (selected-window))
1120 ;; The help window is the selected window, probably because the
1121 ;; user followed a backward/forward button or a cross reference.
1122 ;; In this case just purge stale entries from
1123 ;; `view-return-to-alist' but leave the entry alone and don't
1124 ;; display a message.
1125 (view-return-to-alist-update help-buffer))
1126 ((and help-entry (eq (cadr help-entry) help-buffer))
1127 ;; The help window was not selected but displayed the help
1128 ;; buffer. In this case reuse existing exit information but try
1129 ;; to get back to the selected window when quitting. Don't
1130 ;; display a message since the user must have seen one before.
1131 (view-return-to-alist-update
1132 help-buffer (cons help-window
1133 (cons (selected-window) (cddr view-entry)))))
1135 ;; The help window was not selected, did display the help buffer
1136 ;; earlier, but displayed another buffer when help was invoked.
1137 ;; Set up things so that quitting will show that buffer again.
1138 (view-return-to-alist-update
1139 help-buffer (cons help-window
1140 (cons (selected-window) (cdr help-entry))))
1141 (help-window-setup-finish help-window t))
1143 ;; The help window is new but `view-return-to-alist' had an
1144 ;; entry for it. This should never happen.
1145 (view-return-to-alist-update
1146 help-buffer (cons help-window
1147 (cons (selected-window) 'quit-window)))
1148 (help-window-setup-finish help-window t))))
1150 ;; `view-return-to-alist' does not have an entry for help window
1151 ;; but `list-of-window-tuples' does. Hence `display-buffer' must
1152 ;; have reused an existing window.
1153 (if (eq (cadr help-entry) help-buffer)
1154 ;; The help window displayed `help-buffer' before but no
1155 ;; `view-return-to-alist' entry was found probably because the
1156 ;; user manually switched to the help buffer. Set up things
1157 ;; for `quit-window' although `view-exit-action' should be
1158 ;; able to handle this case all by itself.
1160 (view-return-to-alist-update
1161 help-buffer (cons help-window
1162 (cons (selected-window) 'quit-window)))
1163 (help-window-setup-finish help-window t))
1164 ;; The help window displayed another buffer before. Set up
1165 ;; things in a way that quitting can orderly show that buffer
1166 ;; again. The window-start and window-point information from
1167 ;; `list-of-window-tuples' provide the necessary information.
1168 (view-return-to-alist-update
1169 help-buffer (cons help-window
1170 (cons (selected-window) (cdr help-entry))))
1171 (help-window-setup-finish help-window t)))
1172 ((memq (window-frame help-window) list-of-frames)
1173 ;; The help window is a new window on an existing frame. This
1174 ;; case must be handled specially by `help-window-setup-finish'
1175 ;; and `view-mode-exit' to ascertain that quitting does _not_
1176 ;; inadvertently delete the frame.
1177 (view-return-to-alist-update
1178 help-buffer (cons help-window
1179 (cons (selected-window) 'keep-frame)))
1180 (help-window-setup-finish help-window nil t))
1182 ;; The help window is shown on a new frame. In this case quitting
1183 ;; shall handle both, the help window _and_ its frame. We changed
1184 ;; the default of `view-remove-frame-by-deleting' to t in order to
1185 ;; intuitively DTRT here.
1186 (view-return-to-alist-update
1187 help-buffer (cons help-window (cons (selected-window) t)))
1188 (help-window-setup-finish help-window)))))
1190 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1191 ;; providing the following additional twists:
1193 ;; (1) Issue more accurate messages telling how to scroll and quit the
1196 ;; (2) Make `view-mode-exit' DTRT in more cases.
1198 ;; (3) An option (customizable via `help-window-select') to select the
1199 ;; help window automatically.
1201 ;; (4) A marker (`help-window-point-marker') to move point in the help
1202 ;; window to an arbitrary buffer position.
1204 ;; Note: It's usually always wrong to use `print-help-return-message' in
1205 ;; the body of `with-help-window'.
1206 (defmacro with-help-window (buffer-name &rest body)
1207 "Display buffer BUFFER-NAME in a help window evaluating BODY.
1208 Select help window if the actual value of the user option
1209 `help-window-select' says so."
1210 (declare (indent 1) (debug t))
1211 ;; Bind list-of-frames to `frame-list' and list-of-window-tuples to a
1212 ;; list of one <window window-buffer window-start window-point> tuple
1213 ;; for each live window.
1214 `(let ((list-of-frames (frame-list))
1215 (list-of-window-tuples
1219 (push (list window (window-buffer window)
1220 (window-start window) (window-point window))
1224 ;; We set `help-window' to t in order to trigger `help-mode-finish'
1225 ;; to set `help-window' to the actual help window.
1226 (setq help-window t)
1227 ;; Make `help-window-point-marker' point nowhere (the only place
1228 ;; where this should be set to a buffer position is within BODY).
1229 (set-marker help-window-point-marker nil)
1231 (with-output-to-temp-buffer ,buffer-name
1234 (when (windowp help-window)
1235 ;; Set up help window.
1236 (help-window-setup list-of-frames list-of-window-tuples))
1238 ;; Reset `help-window' to nil to avoid confusing future calls of
1239 ;; `help-mode-finish' by "plain" `with-output-to-temp-buffer'.
1240 (setq help-window nil)))
1244 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1245 ;;; help.el ends here