* ibuffer.el (ibuffer-redisplay): Remove another gratuitous error.
[emacs.git] / lisp / help.el
blob1a6aa9d13d283c9063a7b5af715ee5e93e8e089e
1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2012 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: help, internal
7 ;; Package: emacs
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/>.
24 ;;; Commentary:
26 ;; This code implements GNU Emacs's on-line help system, the one invoked by
27 ;; `M-x help-for-help'.
29 ;;; Code:
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 ;; `help-window-point-marker' is a marker you can move to a valid
40 ;; position of the buffer shown in the help window in order to override
41 ;; the standard positioning mechanism (`point-min') chosen by
42 ;; `with-output-to-temp-buffer'. `with-help-window' has this point
43 ;; nowhere before exiting. Currently used by `view-lossage' to assert
44 ;; that the last keystrokes are always visible.
45 (defvar help-window-point-marker (make-marker)
46 "Marker to override default `window-point' in help windows.")
48 (defvar help-map
49 (let ((map (make-sparse-keymap)))
50 (define-key map (char-to-string help-char) 'help-for-help)
51 (define-key map [help] 'help-for-help)
52 (define-key map [f1] 'help-for-help)
53 (define-key map "." 'display-local-help)
54 (define-key map "?" 'help-for-help)
56 (define-key map "\C-a" 'about-emacs)
57 (define-key map "\C-c" 'describe-copying)
58 (define-key map "\C-d" 'view-emacs-debugging)
59 (define-key map "\C-e" 'view-external-packages)
60 (define-key map "\C-f" 'view-emacs-FAQ)
61 (define-key map "\C-m" 'view-order-manuals)
62 (define-key map "\C-n" 'view-emacs-news)
63 (define-key map "\C-o" 'describe-distribution)
64 (define-key map "\C-p" 'view-emacs-problems)
65 (define-key map "\C-t" 'view-emacs-todo)
66 (define-key map "\C-w" 'describe-no-warranty)
68 ;; This does not fit the pattern, but it is natural given the C-\ command.
69 (define-key map "\C-\\" 'describe-input-method)
71 (define-key map "C" 'describe-coding-system)
72 (define-key map "F" 'Info-goto-emacs-command-node)
73 (define-key map "I" 'describe-input-method)
74 (define-key map "K" 'Info-goto-emacs-key-command-node)
75 (define-key map "L" 'describe-language-environment)
76 (define-key map "S" 'info-lookup-symbol)
78 (define-key map "a" 'apropos-command)
79 (define-key map "b" 'describe-bindings)
80 (define-key map "c" 'describe-key-briefly)
81 (define-key map "d" 'apropos-documentation)
82 (define-key map "e" 'view-echo-area-messages)
83 (define-key map "f" 'describe-function)
84 (define-key map "g" 'describe-gnu-project)
85 (define-key map "h" 'view-hello-file)
87 (define-key map "i" 'info)
88 (define-key map "4i" 'info-other-window)
90 (define-key map "k" 'describe-key)
91 (define-key map "l" 'view-lossage)
92 (define-key map "m" 'describe-mode)
93 (define-key map "n" 'view-emacs-news)
94 (define-key map "p" 'finder-by-keyword)
95 (define-key map "P" 'describe-package)
96 (define-key map "r" 'info-emacs-manual)
97 (define-key map "s" 'describe-syntax)
98 (define-key map "t" 'help-with-tutorial)
99 (define-key map "w" 'where-is)
100 (define-key map "v" 'describe-variable)
101 (define-key map "q" 'help-quit)
102 map)
103 "Keymap for characters following the Help key.")
105 (define-key global-map (char-to-string help-char) 'help-command)
106 (define-key global-map [help] 'help-command)
107 (define-key global-map [f1] 'help-command)
108 (fset 'help-command help-map)
110 ;; insert-button makes the action nil if it is not store somewhere
111 (defvar help-button-cache nil)
114 (defun help-quit ()
115 "Just exit from the Help command's command loop."
116 (interactive)
117 nil)
119 (defvar help-return-method nil
120 "What to do to \"exit\" the help buffer.
121 This is a list
122 (WINDOW . t) delete the selected window (and possibly its frame,
123 see `quit-window'), go to WINDOW.
124 (WINDOW . quit-window) do quit-window, then select WINDOW.
125 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
127 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
128 (defun help-print-return-message (&optional function)
129 "Display or return message saying how to restore windows after help command.
130 This function assumes that `standard-output' is the help buffer.
131 It computes a message, and applies the optional argument FUNCTION to it.
132 If FUNCTION is nil, it applies `message', thus displaying the message.
133 In addition, this function sets up `help-return-method', which see, that
134 specifies what to do when the user exits the help buffer."
135 (and (not (get-buffer-window standard-output))
136 (let ((first-message
137 (cond ((or
138 pop-up-frames
139 (special-display-p (buffer-name standard-output)))
140 (setq help-return-method (cons (selected-window) t))
141 ;; If the help output buffer is a special display buffer,
142 ;; don't say anything about how to get rid of it.
143 ;; First of all, the user will do that with the window
144 ;; manager, not with Emacs.
145 ;; Secondly, the buffer has not been displayed yet,
146 ;; so we don't know whether its frame will be selected.
147 nil)
148 (display-buffer-reuse-frames
149 (setq help-return-method (cons (selected-window)
150 'quit-window))
151 nil)
152 ((not (one-window-p t))
153 (setq help-return-method
154 (cons (selected-window) 'quit-window))
155 "Type \\[display-buffer] RET to restore the other window.")
156 (pop-up-windows
157 (setq help-return-method (cons (selected-window) t))
158 "Type \\[delete-other-windows] to remove help window.")
160 (setq help-return-method
161 (list (selected-window) (window-buffer)
162 (window-start) (window-point)))
163 "Type \\[switch-to-buffer] RET to remove help window."))))
164 (funcall (or function 'message)
165 (concat
166 (if first-message
167 (substitute-command-keys first-message))
168 (if first-message " ")
169 ;; If the help buffer will go in a separate frame,
170 ;; it's no use mentioning a command to scroll, so don't.
171 (if (or pop-up-windows
172 (special-display-p (buffer-name standard-output)))
174 (if (same-window-p (buffer-name standard-output))
175 ;; Say how to scroll this window.
176 (substitute-command-keys
177 "\\[scroll-up] to scroll the help.")
178 ;; Say how to scroll some other window.
179 (substitute-command-keys
180 "\\[scroll-other-window] to scroll the help."))))))))
182 ;; So keyboard macro definitions are documented correctly
183 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
185 (defalias 'help 'help-for-help-internal)
186 ;; find-function can find this.
187 (defalias 'help-for-help 'help-for-help-internal)
188 ;; It can't find this, but nobody will look.
189 (make-help-screen help-for-help-internal
190 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
191 ;; Don't purecopy this one, because it's not evaluated (it's
192 ;; directly used as a docstring in a function definition, so it'll
193 ;; be moved to the DOC file anyway: no need for purecopying it).
194 "You have typed %THIS-KEY%, the help character. Type a Help option:
195 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
197 a PATTERN Show commands whose name matches the PATTERN (a list of words
198 or a regexp). See also the `apropos' command.
199 b Display all key bindings.
200 c KEYS Display the command name run by the given key sequence.
201 C CODING Describe the given coding system, or RET for current ones.
202 d PATTERN Show a list of functions, variables, and other items whose
203 documentation matches the PATTERN (a list of words or a regexp).
204 e Go to the *Messages* buffer which logs echo-area messages.
205 f FUNCTION Display documentation for the given function.
206 F COMMAND Show the on-line manual's section that describes the command.
207 g Display information about the GNU project.
208 h Display the HELLO file which illustrates various scripts.
209 i Start the Info documentation reader: read on-line manuals.
210 I METHOD Describe a specific input method, or RET for current.
211 k KEYS Display the full documentation for the key sequence.
212 K KEYS Show the on-line manual's section for the command bound to KEYS.
213 l Show last 300 input keystrokes (lossage).
214 L LANG-ENV Describes a specific language environment, or RET for current.
215 m Display documentation of current minor modes and current major mode,
216 including their special commands.
217 n Display news of recent Emacs changes.
218 p TOPIC Find packages matching a given topic keyword.
219 r Display the Emacs manual in Info mode.
220 s Display contents of current syntax table, plus explanations.
221 S SYMBOL Show the section for the given symbol in the on-line manual
222 for the programming language used in this buffer.
223 t Start the Emacs learn-by-doing tutorial.
224 v VARIABLE Display the given variable's documentation and value.
225 w COMMAND Display which keystrokes invoke the given command (where-is).
226 . Display any available local help at point in the echo area.
228 C-a Information about Emacs.
229 C-c Emacs copying permission (GNU General Public License).
230 C-d Instructions for debugging GNU Emacs.
231 C-e External packages and information about Emacs.
232 C-f Emacs FAQ.
233 C-m How to order printed Emacs manuals.
234 C-n News of recent Emacs changes.
235 C-o Emacs ordering and distribution information.
236 C-p Info about known Emacs problems.
237 C-t Emacs TODO list.
238 C-w Information on absence of warranty for GNU Emacs."
239 help-map)
243 (defun function-called-at-point ()
244 "Return a function around point or else called by the list containing point.
245 If that doesn't give a function, return nil."
246 (with-syntax-table emacs-lisp-mode-syntax-table
247 (or (condition-case ()
248 (save-excursion
249 (or (not (zerop (skip-syntax-backward "_w")))
250 (eq (char-syntax (following-char)) ?w)
251 (eq (char-syntax (following-char)) ?_)
252 (forward-sexp -1))
253 (skip-chars-forward "'")
254 (let ((obj (read (current-buffer))))
255 (and (symbolp obj) (fboundp obj) obj)))
256 (error nil))
257 (condition-case ()
258 (save-excursion
259 (save-restriction
260 (narrow-to-region (max (point-min)
261 (- (point) 1000)) (point-max))
262 ;; Move up to surrounding paren, then after the open.
263 (backward-up-list 1)
264 (forward-char 1)
265 ;; If there is space here, this is probably something
266 ;; other than a real Lisp function call, so ignore it.
267 (if (looking-at "[ \t]")
268 (error "Probably not a Lisp function call"))
269 (let ((obj (read (current-buffer))))
270 (and (symbolp obj) (fboundp obj) obj))))
271 (error nil))
272 (let* ((str (find-tag-default))
273 (sym (if str (intern-soft str))))
274 (if (and sym (fboundp sym))
276 (save-match-data
277 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
278 (setq sym (intern-soft (match-string 1 str)))
279 (and (fboundp sym) sym))))))))
282 ;;; `User' help functions
284 (defun view-help-file (file &optional dir)
285 (view-file (expand-file-name file (or dir data-directory)))
286 (goto-address-mode 1)
287 (goto-char (point-min)))
289 (defun describe-distribution ()
290 "Display info on how to obtain the latest version of GNU Emacs."
291 (interactive)
292 (view-help-file "DISTRIB"))
294 (defun describe-copying ()
295 "Display info on how you may redistribute copies of GNU Emacs."
296 (interactive)
297 (view-help-file "COPYING"))
299 (defun describe-gnu-project ()
300 "Display info on the GNU project."
301 (interactive)
302 (view-help-file "THE-GNU-PROJECT"))
304 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
306 (defun describe-no-warranty ()
307 "Display info on all the kinds of warranty Emacs does NOT have."
308 (interactive)
309 (describe-copying)
310 (let (case-fold-search)
311 (search-forward "Disclaimer of Warranty")
312 (forward-line 0)
313 (recenter 0)))
315 (defun describe-prefix-bindings ()
316 "Describe the bindings of the prefix used to reach this command.
317 The prefix described consists of all but the last event
318 of the key sequence that ran this command."
319 (interactive)
320 (let ((key (this-command-keys)))
321 (describe-bindings
322 (if (stringp key)
323 (substring key 0 (1- (length key)))
324 (let ((prefix (make-vector (1- (length key)) nil))
325 (i 0))
326 (while (< i (length prefix))
327 (aset prefix i (aref key i))
328 (setq i (1+ i)))
329 prefix)))))
330 ;; Make C-h after a prefix, when not specifically bound,
331 ;; run describe-prefix-bindings.
332 (setq prefix-help-command 'describe-prefix-bindings)
334 (defun view-emacs-news (&optional version)
335 "Display info on recent changes to Emacs.
336 With argument, display info only for the selected version."
337 (interactive "P")
338 (unless version
339 (setq version emacs-major-version))
340 (when (consp version)
341 (let* ((all-versions
342 (let (res)
343 (mapc
344 (lambda (file)
345 (with-temp-buffer
346 (insert-file-contents
347 (expand-file-name file data-directory))
348 (while (re-search-forward
349 (if (member file '("NEWS.18" "NEWS.1-17"))
350 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
351 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
352 (setq res (cons (match-string-no-properties 1) res)))))
353 (cons "NEWS"
354 (directory-files data-directory nil
355 "^NEWS\\.[0-9][-0-9]*$" nil)))
356 (sort (delete-dups res) (lambda (a b) (string< b a)))))
357 (current (car all-versions)))
358 (setq version (completing-read
359 (format "Read NEWS for the version (default %s): " current)
360 all-versions nil nil nil nil current))
361 (if (integerp (string-to-number version))
362 (setq version (string-to-number version))
363 (unless (or (member version all-versions)
364 (<= (string-to-number version) (string-to-number current)))
365 (error "No news about version %s" version)))))
366 (when (integerp version)
367 (cond ((<= version 12)
368 (setq version (format "1.%d" version)))
369 ((<= version 18)
370 (setq version (format "%d" version)))
371 ((> version emacs-major-version)
372 (error "No news about Emacs %d (yet)" version))))
373 (let* ((vn (if (stringp version)
374 (string-to-number version)
375 version))
376 (file (cond
377 ((>= vn emacs-major-version) "NEWS")
378 ((< vn 18) "NEWS.1-17")
379 (t (format "NEWS.%d" vn))))
380 res)
381 (view-file (expand-file-name file data-directory))
382 (widen)
383 (goto-char (point-min))
384 (when (stringp version)
385 (when (re-search-forward
386 (concat (if (< vn 19)
387 "Changes in Emacs[ \t]*"
388 "^\* [^0-9\n]*") version "$")
389 nil t)
390 (beginning-of-line)
391 (narrow-to-region
392 (point)
393 (save-excursion
394 (while (and (setq res
395 (re-search-forward
396 (if (< vn 19)
397 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
398 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
399 (equal (match-string-no-properties 1) version)))
400 (or res (goto-char (point-max)))
401 (beginning-of-line)
402 (point)))))))
404 (defun view-emacs-todo (&optional _arg)
405 "Display the Emacs TODO list."
406 (interactive "P")
407 (view-help-file "TODO"))
409 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
412 (defun view-echo-area-messages ()
413 "View the log of recent echo-area messages: the `*Messages*' buffer.
414 The number of messages retained in that buffer
415 is specified by the variable `message-log-max'."
416 (interactive)
417 (switch-to-buffer (get-buffer-create "*Messages*")))
419 (defun view-order-manuals ()
420 "Display the Emacs ORDERS file."
421 (interactive)
422 (view-help-file "ORDERS"))
424 (defun view-emacs-FAQ ()
425 "Display the Emacs Frequently Asked Questions (FAQ) file."
426 (interactive)
427 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
428 (info "(efaq)"))
430 (defun view-emacs-problems ()
431 "Display info on known problems with Emacs and possible workarounds."
432 (interactive)
433 (view-help-file "PROBLEMS"))
435 (defun view-emacs-debugging ()
436 "Display info on how to debug Emacs problems."
437 (interactive)
438 (view-help-file "DEBUG"))
440 (defun view-external-packages ()
441 "Display external packages and information about Emacs."
442 (interactive)
443 (view-help-file "MORE.STUFF"))
445 (defun view-lossage ()
446 "Display last 300 input keystrokes.
448 To record all your input on a file, use `open-dribble-file'."
449 (interactive)
450 (help-setup-xref (list #'view-lossage)
451 (called-interactively-p 'interactive))
452 (with-help-window (help-buffer)
453 (princ (mapconcat (lambda (key)
454 (if (or (integerp key) (symbolp key) (listp key))
455 (single-key-description key)
456 (prin1-to-string key nil)))
457 (recent-keys)
458 " "))
459 (with-current-buffer standard-output
460 (goto-char (point-min))
461 (while (progn (move-to-column 50) (not (eobp)))
462 (when (search-forward " " nil t)
463 (delete-char -1))
464 (insert "\n"))
465 ;; jidanni wants to see the last keystrokes immediately.
466 (set-marker help-window-point-marker (point)))))
469 ;; Key bindings
471 (defun describe-bindings (&optional prefix buffer)
472 "Show a list of all defined keys, and their definitions.
473 We put that list in a buffer, and display the buffer.
475 The optional argument PREFIX, if non-nil, should be a key sequence;
476 then we display only bindings that start with that prefix.
477 The optional argument BUFFER specifies which buffer's bindings
478 to display (default, the current buffer). BUFFER can be a buffer
479 or a buffer name."
480 (interactive)
481 (or buffer (setq buffer (current-buffer)))
482 (help-setup-xref (list #'describe-bindings prefix buffer)
483 (called-interactively-p 'interactive))
484 (with-current-buffer buffer
485 (describe-bindings-internal nil prefix)))
487 ;; This function used to be in keymap.c.
488 (defun describe-bindings-internal (&optional menus prefix)
489 "Show a list of all defined keys, and their definitions.
490 We put that list in a buffer, and display the buffer.
492 The optional argument MENUS, if non-nil, says to mention menu bindings.
493 \(Ordinarily these are omitted from the output.)
494 The optional argument PREFIX, if non-nil, should be a key sequence;
495 then we display only bindings that start with that prefix."
496 (let ((buf (current-buffer)))
497 (with-help-window "*Help*"
498 (with-current-buffer standard-output
499 (describe-buffer-bindings buf prefix menus)))))
501 (defun where-is (definition &optional insert)
502 "Print message listing key sequences that invoke the command DEFINITION.
503 Argument is a command definition, usually a symbol with a function definition.
504 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
505 (interactive
506 (let ((fn (function-called-at-point))
507 (enable-recursive-minibuffers t)
508 val)
509 (setq val (completing-read
510 (if fn
511 (format "Where is command (default %s): " fn)
512 "Where is command: ")
513 obarray 'commandp t))
514 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
515 (unless definition (error "No command"))
516 (let ((func (indirect-function definition))
517 (defs nil)
518 (standard-output (if insert (current-buffer) standard-output)))
519 ;; In DEFS, find all symbols that are aliases for DEFINITION.
520 (mapatoms (lambda (symbol)
521 (and (fboundp symbol)
522 (not (eq symbol definition))
523 (eq func (condition-case ()
524 (indirect-function symbol)
525 (error symbol)))
526 (push symbol defs))))
527 ;; Look at all the symbols--first DEFINITION,
528 ;; then its aliases.
529 (dolist (symbol (cons definition defs))
530 (let* ((remapped (command-remapping symbol))
531 (keys (where-is-internal
532 symbol overriding-local-map nil nil remapped))
533 (keys (mapconcat 'key-description keys ", "))
534 string)
535 (setq string
536 (if insert
537 (if (> (length keys) 0)
538 (if remapped
539 (format "%s (%s) (remapped from %s)"
540 keys remapped symbol)
541 (format "%s (%s)" keys symbol))
542 (format "M-x %s RET" symbol))
543 (if (> (length keys) 0)
544 (if remapped
545 (format "%s is remapped to %s which is on %s"
546 symbol remapped keys)
547 (format "%s is on %s" symbol keys))
548 ;; If this is the command the user asked about,
549 ;; and it is not on any key, say so.
550 ;; For other symbols, its aliases, say nothing
551 ;; about them unless they are on keys.
552 (if (eq symbol definition)
553 (format "%s is not on any key" symbol)))))
554 (when string
555 (unless (eq symbol definition)
556 (princ ";\n its alias "))
557 (princ string)))))
558 nil)
560 (defun help-key-description (key untranslated)
561 (let ((string (key-description key)))
562 (if (or (not untranslated)
563 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
564 string
565 (let ((otherstring (key-description untranslated)))
566 (if (equal string otherstring)
567 string
568 (format "%s (translated from %s)" string otherstring))))))
570 (defun describe-key-briefly (&optional key insert untranslated)
571 "Print the name of the function KEY invokes. KEY is a string.
572 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
573 If non-nil, UNTRANSLATED is a vector of the untranslated events.
574 It can also be a number in which case the untranslated events from
575 the last key hit are used.
577 If KEY is a menu item or a tool-bar button that is disabled, this command
578 temporarily enables it to allow getting help on disabled items and buttons."
579 (interactive
580 (let ((enable-disabled-menus-and-buttons t)
581 (cursor-in-echo-area t)
582 saved-yank-menu)
583 (unwind-protect
584 (let (key)
585 ;; If yank-menu is empty, populate it temporarily, so that
586 ;; "Select and Paste" menu can generate a complete event.
587 (when (null (cdr yank-menu))
588 (setq saved-yank-menu (copy-sequence yank-menu))
589 (menu-bar-update-yank-menu "(any string)" nil))
590 (setq key (read-key-sequence "Describe key (or click or menu item): "))
591 ;; If KEY is a down-event, read and discard the
592 ;; corresponding up-event. Note that there are also
593 ;; down-events on scroll bars and mode lines: the actual
594 ;; event then is in the second element of the vector.
595 (and (vectorp key)
596 (let ((last-idx (1- (length key))))
597 (and (eventp (aref key last-idx))
598 (memq 'down (event-modifiers (aref key last-idx)))))
599 (read-event))
600 (list
602 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
604 ;; Put yank-menu back as it was, if we changed it.
605 (when saved-yank-menu
606 (setq yank-menu (copy-sequence saved-yank-menu))
607 (fset 'yank-menu (cons 'keymap yank-menu))))))
608 (if (numberp untranslated)
609 (setq untranslated (this-single-command-raw-keys)))
610 (let* ((event (if (and (symbolp (aref key 0))
611 (> (length key) 1)
612 (consp (aref key 1)))
613 (aref key 1)
614 (aref key 0)))
615 (modifiers (event-modifiers event))
616 (standard-output (if insert (current-buffer) standard-output))
617 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
618 (memq 'drag modifiers)) " at that spot" ""))
619 (defn (key-binding key t))
620 key-desc)
621 ;; Handle the case where we faked an entry in "Select and Paste" menu.
622 (if (and (eq defn nil)
623 (stringp (aref key (1- (length key))))
624 (eq (key-binding (substring key 0 -1)) 'yank-menu))
625 (setq defn 'menu-bar-select-yank))
626 ;; Don't bother user with strings from (e.g.) the select-paste menu.
627 (if (stringp (aref key (1- (length key))))
628 (aset key (1- (length key)) "(any string)"))
629 (if (and (> (length untranslated) 0)
630 (stringp (aref untranslated (1- (length untranslated)))))
631 (aset untranslated (1- (length untranslated)) "(any string)"))
632 ;; Now describe the key, perhaps as changed.
633 (setq key-desc (help-key-description key untranslated))
634 (if (or (null defn) (integerp defn) (equal defn 'undefined))
635 (princ (format "%s%s is undefined" key-desc mouse-msg))
636 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
638 (defun describe-key (&optional key untranslated up-event)
639 "Display documentation of the function invoked by KEY.
640 KEY can be any kind of a key sequence; it can include keyboard events,
641 mouse events, and/or menu events. When calling from a program,
642 pass KEY as a string or a vector.
644 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
645 It can also be a number, in which case the untranslated events from
646 the last key sequence entered are used.
647 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
649 If KEY is a menu item or a tool-bar button that is disabled, this command
650 temporarily enables it to allow getting help on disabled items and buttons."
651 (interactive
652 (let ((enable-disabled-menus-and-buttons t)
653 (cursor-in-echo-area t)
654 saved-yank-menu)
655 (unwind-protect
656 (let (key)
657 ;; If yank-menu is empty, populate it temporarily, so that
658 ;; "Select and Paste" menu can generate a complete event.
659 (when (null (cdr yank-menu))
660 (setq saved-yank-menu (copy-sequence yank-menu))
661 (menu-bar-update-yank-menu "(any string)" nil))
662 (setq key (read-key-sequence "Describe key (or click or menu item): "))
663 (list
665 (prefix-numeric-value current-prefix-arg)
666 ;; If KEY is a down-event, read and include the
667 ;; corresponding up-event. Note that there are also
668 ;; down-events on scroll bars and mode lines: the actual
669 ;; event then is in the second element of the vector.
670 (and (vectorp key)
671 (let ((last-idx (1- (length key))))
672 (and (eventp (aref key last-idx))
673 (memq 'down (event-modifiers (aref key last-idx)))))
674 (or (and (eventp (aref key 0))
675 (memq 'down (event-modifiers (aref key 0)))
676 ;; However, for the C-down-mouse-2 popup
677 ;; menu, there is no subsequent up-event. In
678 ;; this case, the up-event is the next
679 ;; element in the supplied vector.
680 (= (length key) 1))
681 (and (> (length key) 1)
682 (eventp (aref key 1))
683 (memq 'down (event-modifiers (aref key 1)))))
684 (read-event))))
685 ;; Put yank-menu back as it was, if we changed it.
686 (when saved-yank-menu
687 (setq yank-menu (copy-sequence saved-yank-menu))
688 (fset 'yank-menu (cons 'keymap yank-menu))))))
689 (if (numberp untranslated)
690 (setq untranslated (this-single-command-raw-keys)))
691 (let* ((event (aref key (if (and (symbolp (aref key 0))
692 (> (length key) 1)
693 (consp (aref key 1)))
695 0)))
696 (modifiers (event-modifiers event))
697 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
698 (memq 'drag modifiers)) " at that spot" ""))
699 (defn (key-binding key t))
700 defn-up defn-up-tricky ev-type
701 mouse-1-remapped mouse-1-tricky)
703 ;; Handle the case where we faked an entry in "Select and Paste" menu.
704 (when (and (eq defn nil)
705 (stringp (aref key (1- (length key))))
706 (eq (key-binding (substring key 0 -1)) 'yank-menu))
707 (setq defn 'menu-bar-select-yank))
708 (if (or (null defn) (integerp defn) (equal defn 'undefined))
709 (message "%s%s is undefined"
710 (help-key-description key untranslated) mouse-msg)
711 (help-setup-xref (list #'describe-function defn)
712 (called-interactively-p 'interactive))
713 ;; Don't bother user with strings from (e.g.) the select-paste menu.
714 (when (stringp (aref key (1- (length key))))
715 (aset key (1- (length key)) "(any string)"))
716 (when (and untranslated
717 (stringp (aref untranslated (1- (length untranslated)))))
718 (aset untranslated (1- (length untranslated))
719 "(any string)"))
720 ;; Need to do this before erasing *Help* buffer in case event
721 ;; is a mouse click in an existing *Help* buffer.
722 (when up-event
723 (setq ev-type (event-basic-type up-event))
724 (let ((sequence (vector up-event)))
725 (when (and (eq ev-type 'mouse-1)
726 mouse-1-click-follows-link
727 (not (eq mouse-1-click-follows-link 'double))
728 (setq mouse-1-remapped
729 (mouse-on-link-p (event-start up-event))))
730 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
731 (> mouse-1-click-follows-link 0)))
732 (cond ((stringp mouse-1-remapped)
733 (setq sequence mouse-1-remapped))
734 ((vectorp mouse-1-remapped)
735 (setcar up-event (elt mouse-1-remapped 0)))
736 (t (setcar up-event 'mouse-2))))
737 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
738 (when mouse-1-tricky
739 (setq sequence (vector up-event))
740 (aset sequence 0 'mouse-1)
741 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
742 (with-help-window (help-buffer)
743 (princ (help-key-description key untranslated))
744 (princ (format "\
745 %s runs the command %S, which is "
746 mouse-msg defn))
747 (describe-function-1 defn)
748 (when up-event
749 (unless (or (null defn-up)
750 (integerp defn-up)
751 (equal defn-up 'undefined))
752 (princ (format "
754 ----------------- up-event %s----------------
756 %s%s%s runs the command %S, which is "
757 (if mouse-1-tricky "(short click) " "")
758 (key-description (vector up-event))
759 mouse-msg
760 (if mouse-1-remapped
761 " is remapped to <mouse-2>, which" "")
762 defn-up))
763 (describe-function-1 defn-up))
764 (unless (or (null defn-up-tricky)
765 (integerp defn-up-tricky)
766 (eq defn-up-tricky 'undefined))
767 (princ (format "
769 ----------------- up-event (long click) ----------------
771 Pressing <%S>%s for longer than %d milli-seconds
772 runs the command %S, which is "
773 ev-type mouse-msg
774 mouse-1-click-follows-link
775 defn-up-tricky))
776 (describe-function-1 defn-up-tricky)))))))
778 (defun describe-mode (&optional buffer)
779 "Display documentation of current major mode and minor modes.
780 A brief summary of the minor modes comes first, followed by the
781 major mode description. This is followed by detailed
782 descriptions of the minor modes, each on a separate page.
784 For this to work correctly for a minor mode, the mode's indicator
785 variable \(listed in `minor-mode-alist') must also be a function
786 whose documentation describes the minor mode."
787 (interactive "@")
788 (unless buffer (setq buffer (current-buffer)))
789 (help-setup-xref (list #'describe-mode buffer)
790 (called-interactively-p 'interactive))
791 ;; For the sake of help-do-xref and help-xref-go-back,
792 ;; don't switch buffers before calling `help-buffer'.
793 (with-help-window (help-buffer)
794 (with-current-buffer buffer
795 (let (minor-modes)
796 ;; Older packages do not register in minor-mode-list but only in
797 ;; minor-mode-alist.
798 (dolist (x minor-mode-alist)
799 (setq x (car x))
800 (unless (memq x minor-mode-list)
801 (push x minor-mode-list)))
802 ;; Find enabled minor mode we will want to mention.
803 (dolist (mode minor-mode-list)
804 ;; Document a minor mode if it is listed in minor-mode-alist,
805 ;; non-nil, and has a function definition.
806 (let ((fmode (or (get mode :minor-mode-function) mode)))
807 (and (boundp mode) (symbol-value mode)
808 (fboundp fmode)
809 (let ((pretty-minor-mode
810 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
811 (symbol-name fmode))
812 (capitalize
813 (substring (symbol-name fmode)
814 0 (match-beginning 0)))
815 fmode)))
816 (push (list fmode pretty-minor-mode
817 (format-mode-line (assq mode minor-mode-alist)))
818 minor-modes)))))
819 (setq minor-modes
820 (sort minor-modes
821 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
822 (when minor-modes
823 (princ "Enabled minor modes:\n")
824 (make-local-variable 'help-button-cache)
825 (with-current-buffer standard-output
826 (dolist (mode minor-modes)
827 (let ((mode-function (nth 0 mode))
828 (pretty-minor-mode (nth 1 mode))
829 (indicator (nth 2 mode)))
830 (add-text-properties 0 (length pretty-minor-mode)
831 '(face bold) pretty-minor-mode)
832 (save-excursion
833 (goto-char (point-max))
834 (princ "\n\f\n")
835 (push (point-marker) help-button-cache)
836 ;; Document the minor modes fully.
837 (insert pretty-minor-mode)
838 (princ (format " minor mode (%s):\n"
839 (if (zerop (length indicator))
840 "no indicator"
841 (format "indicator%s"
842 indicator))))
843 (princ (documentation mode-function)))
844 (insert-button pretty-minor-mode
845 'action (car help-button-cache)
846 'follow-link t
847 'help-echo "mouse-2, RET: show full information")
848 (newline)))
849 (forward-line -1)
850 (fill-paragraph nil)
851 (forward-line 1))
853 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
854 ;; Document the major mode.
855 (let ((mode mode-name))
856 (with-current-buffer standard-output
857 (let ((start (point)))
858 (insert (format-mode-line mode nil nil buffer))
859 (add-text-properties start (point) '(face bold)))))
860 (princ " mode")
861 (let* ((mode major-mode)
862 (file-name (find-lisp-object-file-name mode nil)))
863 (when file-name
864 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
865 ;; Make a hyperlink to the library.
866 (with-current-buffer standard-output
867 (save-excursion
868 (re-search-backward "`\\([^`']+\\)'" nil t)
869 (help-xref-button 1 'help-function-def mode file-name)))))
870 (princ ":\n")
871 (princ (documentation major-mode)))))
872 ;; For the sake of IELM and maybe others
873 nil)
876 (defun describe-minor-mode (minor-mode)
877 "Display documentation of a minor mode given as MINOR-MODE.
878 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
879 appeared on the mode-line."
880 (interactive (list (completing-read
881 "Minor mode: "
882 (nconc
883 (describe-minor-mode-completion-table-for-symbol)
884 (describe-minor-mode-completion-table-for-indicator)
885 ))))
886 (if (symbolp minor-mode)
887 (setq minor-mode (symbol-name minor-mode)))
888 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
889 (indicators (describe-minor-mode-completion-table-for-indicator)))
890 (cond
891 ((member minor-mode symbols)
892 (describe-minor-mode-from-symbol (intern minor-mode)))
893 ((member minor-mode indicators)
894 (describe-minor-mode-from-indicator minor-mode))
896 (error "No such minor mode: %s" minor-mode)))))
898 ;; symbol
899 (defun describe-minor-mode-completion-table-for-symbol ()
900 ;; In order to list up all minor modes, minor-mode-list
901 ;; is used here instead of minor-mode-alist.
902 (delq nil (mapcar 'symbol-name minor-mode-list)))
904 (defun describe-minor-mode-from-symbol (symbol)
905 "Display documentation of a minor mode given as a symbol, SYMBOL"
906 (interactive (list (intern (completing-read
907 "Minor mode symbol: "
908 (describe-minor-mode-completion-table-for-symbol)))))
909 (if (fboundp symbol)
910 (describe-function symbol)
911 (describe-variable symbol)))
913 ;; indicator
914 (defun describe-minor-mode-completion-table-for-indicator ()
915 (delq nil
916 (mapcar (lambda (x)
917 (let ((i (format-mode-line x)))
918 ;; remove first space if existed
919 (cond
920 ((= 0 (length i))
921 nil)
922 ((eq (aref i 0) ?\s)
923 (substring i 1))
925 i))))
926 minor-mode-alist)))
928 (defun describe-minor-mode-from-indicator (indicator)
929 "Display documentation of a minor mode specified by INDICATOR.
930 If you call this function interactively, you can give indicator which
931 is currently activated with completion."
932 (interactive (list
933 (completing-read
934 "Minor mode indicator: "
935 (describe-minor-mode-completion-table-for-indicator))))
936 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
937 (if minor-mode
938 (describe-minor-mode-from-symbol minor-mode)
939 (error "Cannot find minor mode for `%s'" indicator))))
941 (defun lookup-minor-mode-from-indicator (indicator)
942 "Return a minor mode symbol from its indicator on the modeline."
943 ;; remove first space if existed
944 (if (and (< 0 (length indicator))
945 (eq (aref indicator 0) ?\s))
946 (setq indicator (substring indicator 1)))
947 (let ((minor-modes minor-mode-alist)
948 result)
949 (while minor-modes
950 (let* ((minor-mode (car (car minor-modes)))
951 (anindicator (format-mode-line
952 (car (cdr (car minor-modes))))))
953 ;; remove first space if existed
954 (if (and (stringp anindicator)
955 (> (length anindicator) 0)
956 (eq (aref anindicator 0) ?\s))
957 (setq anindicator (substring anindicator 1)))
958 (if (equal indicator anindicator)
959 (setq result minor-mode
960 minor-modes nil)
961 (setq minor-modes (cdr minor-modes)))))
962 result))
964 ;;; Automatic resizing of temporary buffers.
965 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
966 "Maximum height of a window displaying a temporary buffer.
967 This is effective only when Temp Buffer Resize mode is enabled.
968 The value is the maximum height (in lines) which
969 `resize-temp-buffer-window' will give to a window displaying a
970 temporary buffer. It can also be a function to be called to
971 choose the height for such a buffer. It gets one argument, the
972 buffer, and should return a positive integer. At the time the
973 function is called, the window to be resized is selected."
974 :type '(choice integer function)
975 :group 'help
976 :version "20.4")
978 (define-minor-mode temp-buffer-resize-mode
979 "Toggle auto-shrinking temp buffer windows (Temp Buffer Resize mode).
980 With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
981 is positive, and disable it otherwise. If called from Lisp,
982 enable the mode if ARG is omitted or nil.
984 When Temp Buffer Resize mode is enabled, the windows in which we
985 show a temporary buffer are automatically reduced in height to
986 fit the buffer's contents, but never more than
987 `temp-buffer-max-height' nor less than `window-min-height'.
989 This mode is used by `help', `apropos' and `completion' buffers,
990 and some others."
991 :global t :group 'help
992 (if temp-buffer-resize-mode
993 ;; `help-make-xrefs' may add a `back' button and thus increase the
994 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
995 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
996 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
998 (defun resize-temp-buffer-window ()
999 "Resize the selected window to fit its contents.
1000 Will not make it higher than `temp-buffer-max-height' nor smaller
1001 than `window-min-height'. Do nothing if the selected window is
1002 not vertically combined or some of its contents are scrolled out
1003 of view."
1004 (when (and (pos-visible-in-window-p (point-min))
1005 (window-combined-p))
1006 (fit-window-to-buffer
1008 (if (functionp temp-buffer-max-height)
1009 (funcall temp-buffer-max-height (window-buffer))
1010 temp-buffer-max-height))))
1012 ;;; Help windows.
1013 (defcustom help-window-select 'other
1014 "Non-nil means select help window for viewing.
1015 Choices are:
1016 never (nil) Select help window only if there is no other window
1017 on its frame.
1018 other Select help window unless the selected window is the
1019 only other window on the help window's frame.
1020 always (t) Always select the help window.
1022 This option has effect if and only if the help window was created
1023 by `with-help-window'"
1024 :type '(choice (const :tag "never (nil)" nil)
1025 (const :tag "other" other)
1026 (const :tag "always (t)" t))
1027 :group 'help
1028 :version "23.1")
1030 (defun help-window-display-message (quit-part window &optional scroll)
1031 "Display message telling how to quit and scroll help window.
1032 QUIT-PART is a string telling how to quit the help window WINDOW.
1033 Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1034 SCROLL equal `other' means tell how to scroll the \"other\"
1035 window."
1036 (let ((scroll-part
1037 (cond
1038 ;; If we don't have QUIT-PART we probably reuse a window
1039 ;; showing the same buffer so we don't show any message.
1040 ((not quit-part) nil)
1041 ((pos-visible-in-window-p
1042 (with-current-buffer (window-buffer window)
1043 (point-max)) window t)
1044 ;; Buffer end is at least partially visible, no need to talk
1045 ;; about scrolling.
1046 ".")
1047 ((eq scroll 'other)
1048 ", \\[scroll-other-window] to scroll help.")
1049 (scroll ", \\[scroll-up] to scroll help."))))
1050 (message "%s"
1051 (substitute-command-keys (concat quit-part scroll-part)))))
1053 (defun help-window-setup (help-window)
1054 "Set up help window for `with-help-window'.
1055 HELP-WINDOW is the window used for displaying the help buffer."
1056 (let* ((help-buffer (when (window-live-p help-window)
1057 (window-buffer help-window)))
1058 (help-setup (when (window-live-p help-window)
1059 (car (window-parameter help-window 'quit-restore)))))
1060 (when help-buffer
1061 ;; Handle `help-window-point-marker'.
1062 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1063 (set-window-point help-window help-window-point-marker)
1064 ;; Reset `help-window-point-marker'.
1065 (set-marker help-window-point-marker nil))
1067 (cond
1068 ((or (eq help-window (selected-window))
1069 (and (or (eq help-window-select t)
1070 (eq help-setup 'frame)
1071 (and (eq help-window-select 'other)
1072 (eq (window-frame help-window) (selected-frame))
1073 (> (length (window-list nil 'no-mini)) 2)))
1074 (select-window help-window)))
1075 ;; The help window is or gets selected ...
1076 (help-window-display-message
1077 (cond
1078 ((eq help-setup 'window)
1079 ;; ... and is new, ...
1080 "Type \"q\" to delete help window")
1081 ((eq help-setup 'frame)
1082 "Type \"q\" to delete help frame")
1083 ((eq help-setup 'other)
1084 ;; ... or displayed some other buffer before.
1085 "Type \"q\" to restore previous buffer"))
1086 help-window t))
1087 ((and (eq (window-frame help-window) (selected-frame))
1088 (= (length (window-list nil 'no-mini)) 2))
1089 ;; There are two windows on the help window's frame and the
1090 ;; other one is the selected one.
1091 (help-window-display-message
1092 (cond
1093 ((eq help-setup 'window)
1094 "Type \\[delete-other-windows] to delete the help window")
1095 ((eq help-setup 'other)
1096 "Type \"q\" in help window to restore its previous buffer"))
1097 help-window 'other))
1099 ;; The help window is not selected ...
1100 (help-window-display-message
1101 (cond
1102 ((eq help-setup 'window)
1103 ;; ... and is new, ...
1104 "Type \"q\" in help window to delete it")
1105 ((eq help-setup 'other)
1106 ;; ... or displayed some other buffer before.
1107 "Type \"q\" in help window to restore previous buffer"))
1108 help-window))))))
1110 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1111 ;; providing the following additional twists:
1113 ;; (1) Issue more accurate messages telling how to scroll and quit the
1114 ;; help window.
1116 ;; (2) An option (customizable via `help-window-select') to select the
1117 ;; help window automatically.
1119 ;; (3) A marker (`help-window-point-marker') to move point in the help
1120 ;; window to an arbitrary buffer position.
1122 ;; Note: It's usually always wrong to use `help-print-return-message' in
1123 ;; the body of `with-help-window'.
1124 (defmacro with-help-window (buffer-name &rest body)
1125 "Display buffer with name BUFFER-NAME in a help window evaluating BODY.
1126 Select help window if the actual value of the user option
1127 `help-window-select' says so. Return last value in BODY."
1128 (declare (indent 1) (debug t))
1129 `(progn
1130 ;; Make `help-window-point-marker' point nowhere. The only place
1131 ;; where this should be set to a buffer position is within BODY.
1132 (set-marker help-window-point-marker nil)
1133 (let* (help-window
1134 (temp-buffer-show-hook
1135 (cons (lambda () (setq help-window (selected-window)))
1136 temp-buffer-show-hook)))
1137 ;; Return value returned by `with-output-to-temp-buffer'.
1138 (prog1
1139 (with-output-to-temp-buffer ,buffer-name
1140 (progn ,@body))
1141 (help-window-setup help-window)))))
1143 ;; Called from C, on encountering `help-char' when reading a char.
1144 ;; Don't print to *Help*; that would clobber Help history.
1145 (defun help-form-show ()
1146 "Display the output of a non-nil `help-form'."
1147 (let ((msg (eval help-form)))
1148 (if (stringp msg)
1149 (with-output-to-temp-buffer " *Char Help*"
1150 (princ msg)))))
1152 (provide 'help)
1154 ;;; help.el ends here