* doc/emacs/maintaining.texi (VC Directory Commands): Copyedit.
[emacs.git] / lisp / help.el
blob25bc9c4b7c625c430f93c44d5afcb084f3b5028b
1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2013 Free Software
4 ;; Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This code implements GNU Emacs's on-line help system, the one invoked by
28 ;; `M-x help-for-help'.
30 ;;; Code:
32 ;; Get the macro make-help-screen when this is compiled,
33 ;; or run interpreted, but not when the compiled code is loaded.
34 (eval-when-compile (require 'help-macro))
36 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
37 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
38 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
40 ;; `help-window-point-marker' is a marker you can move to a valid
41 ;; position of the buffer shown in the help window in order to override
42 ;; the standard positioning mechanism (`point-min') chosen by
43 ;; `with-output-to-temp-buffer' and `with-temp-buffer-window'.
44 ;; `with-help-window' has this point nowhere before exiting. Currently
45 ;; used by `view-lossage' to assert that the last keystrokes are always
46 ;; visible.
47 (defvar help-window-point-marker (make-marker)
48 "Marker to override default `window-point' in help windows.")
50 (defvar help-map
51 (let ((map (make-sparse-keymap)))
52 (define-key map (char-to-string help-char) 'help-for-help)
53 (define-key map [help] 'help-for-help)
54 (define-key map [f1] 'help-for-help)
55 (define-key map "." 'display-local-help)
56 (define-key map "?" 'help-for-help)
58 (define-key map "\C-a" 'about-emacs)
59 (define-key map "\C-c" 'describe-copying)
60 (define-key map "\C-d" 'view-emacs-debugging)
61 (define-key map "\C-e" 'view-external-packages)
62 (define-key map "\C-f" 'view-emacs-FAQ)
63 (define-key map "\C-m" 'view-order-manuals)
64 (define-key map "\C-n" 'view-emacs-news)
65 (define-key map "\C-o" 'describe-distribution)
66 (define-key map "\C-p" 'view-emacs-problems)
67 (define-key map "\C-t" 'view-emacs-todo)
68 (define-key map "\C-w" 'describe-no-warranty)
70 ;; This does not fit the pattern, but it is natural given the C-\ command.
71 (define-key map "\C-\\" 'describe-input-method)
73 (define-key map "C" 'describe-coding-system)
74 (define-key map "F" 'Info-goto-emacs-command-node)
75 (define-key map "I" 'describe-input-method)
76 (define-key map "K" 'Info-goto-emacs-key-command-node)
77 (define-key map "L" 'describe-language-environment)
78 (define-key map "S" 'info-lookup-symbol)
80 (define-key map "a" 'apropos-command)
81 (define-key map "b" 'describe-bindings)
82 (define-key map "c" 'describe-key-briefly)
83 (define-key map "d" 'apropos-documentation)
84 (define-key map "e" 'view-echo-area-messages)
85 (define-key map "f" 'describe-function)
86 (define-key map "g" 'describe-gnu-project)
87 (define-key map "h" 'view-hello-file)
89 (define-key map "i" 'info)
90 (define-key map "4i" 'info-other-window)
92 (define-key map "k" 'describe-key)
93 (define-key map "l" 'view-lossage)
94 (define-key map "m" 'describe-mode)
95 (define-key map "n" 'view-emacs-news)
96 (define-key map "p" 'finder-by-keyword)
97 (define-key map "P" 'describe-package)
98 (define-key map "r" 'info-emacs-manual)
99 (define-key map "s" 'describe-syntax)
100 (define-key map "t" 'help-with-tutorial)
101 (define-key map "w" 'where-is)
102 (define-key map "v" 'describe-variable)
103 (define-key map "q" 'help-quit)
104 map)
105 "Keymap for characters following the Help key.")
107 (define-key global-map (char-to-string help-char) 'help-command)
108 (define-key global-map [help] 'help-command)
109 (define-key global-map [f1] 'help-command)
110 (fset 'help-command help-map)
112 ;; insert-button makes the action nil if it is not store somewhere
113 (defvar help-button-cache nil)
116 (defun help-quit ()
117 "Just exit from the Help command's command loop."
118 (interactive)
119 nil)
121 (defvar help-return-method nil
122 "What to do to \"exit\" the help buffer.
123 This is a list
124 (WINDOW . t) delete the selected window (and possibly its frame,
125 see `quit-window'), go to WINDOW.
126 (WINDOW . quit-window) do quit-window, then select WINDOW.
127 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
129 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
130 (defun help-print-return-message (&optional function)
131 "Display or return message saying how to restore windows after help command.
132 This function assumes that `standard-output' is the help buffer.
133 It computes a message, and applies the optional argument FUNCTION to it.
134 If FUNCTION is nil, it applies `message', thus displaying the message.
135 In addition, this function sets up `help-return-method', which see, that
136 specifies what to do when the user exits the help buffer."
137 (and (not (get-buffer-window standard-output))
138 (let ((first-message
139 (cond ((or
140 pop-up-frames
141 (special-display-p (buffer-name standard-output)))
142 (setq help-return-method (cons (selected-window) t))
143 ;; If the help output buffer is a special display buffer,
144 ;; don't say anything about how to get rid of it.
145 ;; First of all, the user will do that with the window
146 ;; manager, not with Emacs.
147 ;; Secondly, the buffer has not been displayed yet,
148 ;; so we don't know whether its frame will be selected.
149 nil)
150 ((not (one-window-p t))
151 (setq help-return-method
152 (cons (selected-window) 'quit-window))
153 "Type \\[display-buffer] RET to restore the other window.")
154 (pop-up-windows
155 (setq help-return-method (cons (selected-window) t))
156 "Type \\[delete-other-windows] to remove help window.")
158 (setq help-return-method
159 (list (selected-window) (window-buffer)
160 (window-start) (window-point)))
161 "Type \\[switch-to-buffer] RET to remove help window."))))
162 (funcall (or function 'message)
163 (concat
164 (if first-message
165 (substitute-command-keys first-message))
166 (if first-message " ")
167 ;; If the help buffer will go in a separate frame,
168 ;; it's no use mentioning a command to scroll, so don't.
169 (if (or pop-up-windows
170 (special-display-p (buffer-name standard-output)))
172 (if (same-window-p (buffer-name standard-output))
173 ;; Say how to scroll this window.
174 (substitute-command-keys
175 "\\[scroll-up] to scroll the help.")
176 ;; Say how to scroll some other window.
177 (substitute-command-keys
178 "\\[scroll-other-window] to scroll the help."))))))))
180 ;; So keyboard macro definitions are documented correctly
181 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
183 (defalias 'help 'help-for-help-internal)
184 ;; find-function can find this.
185 (defalias 'help-for-help 'help-for-help-internal)
186 ;; It can't find this, but nobody will look.
187 (make-help-screen help-for-help-internal
188 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
189 ;; Don't purecopy this one, because it's not evaluated (it's
190 ;; directly used as a docstring in a function definition, so it'll
191 ;; be moved to the DOC file anyway: no need for purecopying it).
192 "You have typed %THIS-KEY%, the help character. Type a Help option:
193 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
195 a PATTERN Show commands whose name matches the PATTERN (a list of words
196 or a regexp). See also the `apropos' command.
197 b Display all key bindings.
198 c KEYS Display the command name run by the given key sequence.
199 C CODING Describe the given coding system, or RET for current ones.
200 d PATTERN Show a list of functions, variables, and other items whose
201 documentation matches the PATTERN (a list of words or a regexp).
202 e Go to the *Messages* buffer which logs echo-area messages.
203 f FUNCTION Display documentation for the given function.
204 F COMMAND Show the on-line manual's section that describes the command.
205 g Display information about the GNU project.
206 h Display the HELLO file which illustrates various scripts.
207 i Start the Info documentation reader: read on-line manuals.
208 I METHOD Describe a specific input method, or RET for current.
209 k KEYS Display the full documentation for the key sequence.
210 K KEYS Show the on-line manual's section for the command bound to KEYS.
211 l Show last 300 input keystrokes (lossage).
212 L LANG-ENV Describes a specific language environment, or RET for current.
213 m Display documentation of current minor modes and current major mode,
214 including their special commands.
215 n Display news of recent Emacs changes.
216 p TOPIC Find packages matching a given topic keyword.
217 r Display the Emacs manual in Info mode.
218 s Display contents of current syntax table, plus explanations.
219 S SYMBOL Show the section for the given symbol in the on-line manual
220 for the programming language used in this buffer.
221 t Start the Emacs learn-by-doing tutorial.
222 v VARIABLE Display the given variable's documentation and value.
223 w COMMAND Display which keystrokes invoke the given command (where-is).
224 . Display any available local help at point in the echo area.
226 C-a Information about Emacs.
227 C-c Emacs copying permission (GNU General Public License).
228 C-d Instructions for debugging GNU Emacs.
229 C-e External packages and information about Emacs.
230 C-f Emacs FAQ.
231 C-m How to order printed Emacs manuals.
232 C-n News of recent Emacs changes.
233 C-o Emacs ordering and distribution information.
234 C-p Info about known Emacs problems.
235 C-t Emacs TODO list.
236 C-w Information on absence of warranty for GNU Emacs."
237 help-map)
241 (defun function-called-at-point ()
242 "Return a function around point or else called by the list containing point.
243 If that doesn't give a function, return nil."
244 (with-syntax-table emacs-lisp-mode-syntax-table
245 (or (condition-case ()
246 (save-excursion
247 (or (not (zerop (skip-syntax-backward "_w")))
248 (eq (char-syntax (following-char)) ?w)
249 (eq (char-syntax (following-char)) ?_)
250 (forward-sexp -1))
251 (skip-chars-forward "'")
252 (let ((obj (read (current-buffer))))
253 (and (symbolp obj) (fboundp obj) obj)))
254 (error nil))
255 (condition-case ()
256 (save-excursion
257 (save-restriction
258 (narrow-to-region (max (point-min)
259 (- (point) 1000)) (point-max))
260 ;; Move up to surrounding paren, then after the open.
261 (backward-up-list 1)
262 (forward-char 1)
263 ;; If there is space here, this is probably something
264 ;; other than a real Lisp function call, so ignore it.
265 (if (looking-at "[ \t]")
266 (error "Probably not a Lisp function call"))
267 (let ((obj (read (current-buffer))))
268 (and (symbolp obj) (fboundp obj) obj))))
269 (error nil))
270 (let* ((str (find-tag-default))
271 (sym (if str (intern-soft str))))
272 (if (and sym (fboundp sym))
274 (save-match-data
275 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
276 (setq sym (intern-soft (match-string 1 str)))
277 (and (fboundp sym) sym))))))))
280 ;;; `User' help functions
282 (defun view-help-file (file &optional dir)
283 (view-file (expand-file-name file (or dir data-directory)))
284 (goto-address-mode 1)
285 (goto-char (point-min)))
287 (defun describe-distribution ()
288 "Display info on how to obtain the latest version of GNU Emacs."
289 (interactive)
290 (view-help-file "DISTRIB"))
292 (defun describe-copying ()
293 "Display info on how you may redistribute copies of GNU Emacs."
294 (interactive)
295 (view-help-file "COPYING"))
297 (defun describe-gnu-project ()
298 "Display info on the GNU project."
299 (interactive)
300 (view-help-file "THE-GNU-PROJECT"))
302 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
304 (defun describe-no-warranty ()
305 "Display info on all the kinds of warranty Emacs does NOT have."
306 (interactive)
307 (describe-copying)
308 (let (case-fold-search)
309 (search-forward "Disclaimer of Warranty")
310 (forward-line 0)
311 (recenter 0)))
313 (defun describe-prefix-bindings ()
314 "Describe the bindings of the prefix used to reach this command.
315 The prefix described consists of all but the last event
316 of the key sequence that ran this command."
317 (interactive)
318 (let ((key (this-command-keys)))
319 (describe-bindings
320 (if (stringp key)
321 (substring key 0 (1- (length key)))
322 (let ((prefix (make-vector (1- (length key)) nil))
323 (i 0))
324 (while (< i (length prefix))
325 (aset prefix i (aref key i))
326 (setq i (1+ i)))
327 prefix)))))
328 ;; Make C-h after a prefix, when not specifically bound,
329 ;; run describe-prefix-bindings.
330 (setq prefix-help-command 'describe-prefix-bindings)
332 (defun view-emacs-news (&optional version)
333 "Display info on recent changes to Emacs.
334 With argument, display info only for the selected version."
335 (interactive "P")
336 (unless version
337 (setq version emacs-major-version))
338 (when (consp version)
339 (let* ((all-versions
340 (let (res)
341 (mapc
342 (lambda (file)
343 (with-temp-buffer
344 (insert-file-contents
345 (expand-file-name file data-directory))
346 (while (re-search-forward
347 (if (member file '("NEWS.18" "NEWS.1-17"))
348 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
349 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
350 (setq res (cons (match-string-no-properties 1) res)))))
351 (cons "NEWS"
352 (directory-files data-directory nil
353 "^NEWS\\.[0-9][-0-9]*$" nil)))
354 (sort (delete-dups res) (lambda (a b) (string< b a)))))
355 (current (car all-versions)))
356 (setq version (completing-read
357 (format "Read NEWS for the version (default %s): " current)
358 all-versions nil nil nil nil current))
359 (if (integerp (string-to-number version))
360 (setq version (string-to-number version))
361 (unless (or (member version all-versions)
362 (<= (string-to-number version) (string-to-number current)))
363 (error "No news about version %s" version)))))
364 (when (integerp version)
365 (cond ((<= version 12)
366 (setq version (format "1.%d" version)))
367 ((<= version 18)
368 (setq version (format "%d" version)))
369 ((> version emacs-major-version)
370 (error "No news about Emacs %d (yet)" version))))
371 (let* ((vn (if (stringp version)
372 (string-to-number version)
373 version))
374 (file (cond
375 ((>= vn emacs-major-version) "NEWS")
376 ((< vn 18) "NEWS.1-17")
377 (t (format "NEWS.%d" vn))))
378 res)
379 (view-file (expand-file-name file data-directory))
380 (widen)
381 (goto-char (point-min))
382 (when (stringp version)
383 (when (re-search-forward
384 (concat (if (< vn 19)
385 "Changes in Emacs[ \t]*"
386 "^\* [^0-9\n]*") version "$")
387 nil t)
388 (beginning-of-line)
389 (narrow-to-region
390 (point)
391 (save-excursion
392 (while (and (setq res
393 (re-search-forward
394 (if (< vn 19)
395 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
396 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
397 (equal (match-string-no-properties 1) version)))
398 (or res (goto-char (point-max)))
399 (beginning-of-line)
400 (point)))))))
402 (defun view-emacs-todo (&optional _arg)
403 "Display the Emacs TODO list."
404 (interactive "P")
405 (view-help-file "TODO"))
407 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
410 (defun view-echo-area-messages ()
411 "View the log of recent echo-area messages: the `*Messages*' buffer.
412 The number of messages retained in that buffer
413 is specified by the variable `message-log-max'."
414 (interactive)
415 (with-current-buffer (get-buffer-create "*Messages*")
416 (goto-char (point-max))
417 (display-buffer (current-buffer))))
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 ;; Clear the echo area message (Bug#7014).
592 (message nil)
593 ;; If KEY is a down-event, read and discard the
594 ;; corresponding up-event. Note that there are also
595 ;; down-events on scroll bars and mode lines: the actual
596 ;; event then is in the second element of the vector.
597 (and (vectorp key)
598 (let ((last-idx (1- (length key))))
599 (and (eventp (aref key last-idx))
600 (memq 'down (event-modifiers (aref key last-idx)))))
601 (read-event))
602 (list
604 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
606 ;; Put yank-menu back as it was, if we changed it.
607 (when saved-yank-menu
608 (setq yank-menu (copy-sequence saved-yank-menu))
609 (fset 'yank-menu (cons 'keymap yank-menu))))))
610 (if (numberp untranslated)
611 (setq untranslated (this-single-command-raw-keys)))
612 (let* ((event (if (and (symbolp (aref key 0))
613 (> (length key) 1)
614 (consp (aref key 1)))
615 (aref key 1)
616 (aref key 0)))
617 (modifiers (event-modifiers event))
618 (standard-output (if insert (current-buffer) standard-output))
619 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
620 (memq 'drag modifiers)) " at that spot" ""))
621 (defn (key-binding key t))
622 key-desc)
623 ;; Handle the case where we faked an entry in "Select and Paste" menu.
624 (if (and (eq defn nil)
625 (stringp (aref key (1- (length key))))
626 (eq (key-binding (substring key 0 -1)) 'yank-menu))
627 (setq defn 'menu-bar-select-yank))
628 ;; Don't bother user with strings from (e.g.) the select-paste menu.
629 (if (stringp (aref key (1- (length key))))
630 (aset key (1- (length key)) "(any string)"))
631 (if (and (> (length untranslated) 0)
632 (stringp (aref untranslated (1- (length untranslated)))))
633 (aset untranslated (1- (length untranslated)) "(any string)"))
634 ;; Now describe the key, perhaps as changed.
635 (setq key-desc (help-key-description key untranslated))
636 (if (or (null defn) (integerp defn) (equal defn 'undefined))
637 (princ (format "%s%s is undefined" key-desc mouse-msg))
638 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
640 (defun describe-key (&optional key untranslated up-event)
641 "Display documentation of the function invoked by KEY.
642 KEY can be any kind of a key sequence; it can include keyboard events,
643 mouse events, and/or menu events. When calling from a program,
644 pass KEY as a string or a vector.
646 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
647 It can also be a number, in which case the untranslated events from
648 the last key sequence entered are used.
649 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
651 If KEY is a menu item or a tool-bar button that is disabled, this command
652 temporarily enables it to allow getting help on disabled items and buttons."
653 (interactive
654 (let ((enable-disabled-menus-and-buttons t)
655 (cursor-in-echo-area t)
656 saved-yank-menu)
657 (unwind-protect
658 (let (key)
659 ;; If yank-menu is empty, populate it temporarily, so that
660 ;; "Select and Paste" menu can generate a complete event.
661 (when (null (cdr yank-menu))
662 (setq saved-yank-menu (copy-sequence yank-menu))
663 (menu-bar-update-yank-menu "(any string)" nil))
664 (setq key (read-key-sequence "Describe key (or click or menu item): "))
665 (list
667 (prefix-numeric-value current-prefix-arg)
668 ;; If KEY is a down-event, read and include the
669 ;; corresponding up-event. Note that there are also
670 ;; down-events on scroll bars and mode lines: the actual
671 ;; event then is in the second element of the vector.
672 (and (vectorp key)
673 (let ((last-idx (1- (length key))))
674 (and (eventp (aref key last-idx))
675 (memq 'down (event-modifiers (aref key last-idx)))))
676 (or (and (eventp (aref key 0))
677 (memq 'down (event-modifiers (aref key 0)))
678 ;; However, for the C-down-mouse-2 popup
679 ;; menu, there is no subsequent up-event. In
680 ;; this case, the up-event is the next
681 ;; element in the supplied vector.
682 (= (length key) 1))
683 (and (> (length key) 1)
684 (eventp (aref key 1))
685 (memq 'down (event-modifiers (aref key 1)))))
686 (read-event))))
687 ;; Put yank-menu back as it was, if we changed it.
688 (when saved-yank-menu
689 (setq yank-menu (copy-sequence saved-yank-menu))
690 (fset 'yank-menu (cons 'keymap yank-menu))))))
691 (if (numberp untranslated)
692 (setq untranslated (this-single-command-raw-keys)))
693 (let* ((event (aref key (if (and (symbolp (aref key 0))
694 (> (length key) 1)
695 (consp (aref key 1)))
697 0)))
698 (modifiers (event-modifiers event))
699 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
700 (memq 'drag modifiers)) " at that spot" ""))
701 (defn (key-binding key t))
702 defn-up defn-up-tricky ev-type
703 mouse-1-remapped mouse-1-tricky)
705 ;; Handle the case where we faked an entry in "Select and Paste" menu.
706 (when (and (eq defn nil)
707 (stringp (aref key (1- (length key))))
708 (eq (key-binding (substring key 0 -1)) 'yank-menu))
709 (setq defn 'menu-bar-select-yank))
710 (if (or (null defn) (integerp defn) (equal defn 'undefined))
711 (message "%s%s is undefined"
712 (help-key-description key untranslated) mouse-msg)
713 (help-setup-xref (list #'describe-function defn)
714 (called-interactively-p 'interactive))
715 ;; Don't bother user with strings from (e.g.) the select-paste menu.
716 (when (stringp (aref key (1- (length key))))
717 (aset key (1- (length key)) "(any string)"))
718 (when (and untranslated
719 (stringp (aref untranslated (1- (length untranslated)))))
720 (aset untranslated (1- (length untranslated))
721 "(any string)"))
722 ;; Need to do this before erasing *Help* buffer in case event
723 ;; is a mouse click in an existing *Help* buffer.
724 (when up-event
725 (setq ev-type (event-basic-type up-event))
726 (let ((sequence (vector up-event)))
727 (when (and (eq ev-type 'mouse-1)
728 mouse-1-click-follows-link
729 (not (eq mouse-1-click-follows-link 'double))
730 (setq mouse-1-remapped
731 (mouse-on-link-p (event-start up-event))))
732 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
733 (> mouse-1-click-follows-link 0)))
734 (cond ((stringp mouse-1-remapped)
735 (setq sequence mouse-1-remapped))
736 ((vectorp mouse-1-remapped)
737 (setcar up-event (elt mouse-1-remapped 0)))
738 (t (setcar up-event 'mouse-2))))
739 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
740 (when mouse-1-tricky
741 (setq sequence (vector up-event))
742 (aset sequence 0 'mouse-1)
743 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
744 (with-help-window (help-buffer)
745 (princ (help-key-description key untranslated))
746 (princ (format "\
747 %s runs the command %S, which is "
748 mouse-msg defn))
749 (describe-function-1 defn)
750 (when up-event
751 (unless (or (null defn-up)
752 (integerp defn-up)
753 (equal defn-up 'undefined))
754 (princ (format "
756 ----------------- up-event %s----------------
758 %s%s%s runs the command %S, which is "
759 (if mouse-1-tricky "(short click) " "")
760 (key-description (vector up-event))
761 mouse-msg
762 (if mouse-1-remapped
763 " is remapped to <mouse-2>, which" "")
764 defn-up))
765 (describe-function-1 defn-up))
766 (unless (or (null defn-up-tricky)
767 (integerp defn-up-tricky)
768 (eq defn-up-tricky 'undefined))
769 (princ (format "
771 ----------------- up-event (long click) ----------------
773 Pressing <%S>%s for longer than %d milli-seconds
774 runs the command %S, which is "
775 ev-type mouse-msg
776 mouse-1-click-follows-link
777 defn-up-tricky))
778 (describe-function-1 defn-up-tricky)))))))
780 (defun describe-mode (&optional buffer)
781 "Display documentation of current major mode and minor modes.
782 A brief summary of the minor modes comes first, followed by the
783 major mode description. This is followed by detailed
784 descriptions of the minor modes, each on a separate page.
786 For this to work correctly for a minor mode, the mode's indicator
787 variable \(listed in `minor-mode-alist') must also be a function
788 whose documentation describes the minor mode.
790 If called from Lisp with a non-nil BUFFER argument, display
791 documentation for the major and minor modes of that buffer."
792 (interactive "@")
793 (unless buffer (setq buffer (current-buffer)))
794 (help-setup-xref (list #'describe-mode buffer)
795 (called-interactively-p 'interactive))
796 ;; For the sake of help-do-xref and help-xref-go-back,
797 ;; don't switch buffers before calling `help-buffer'.
798 (with-help-window (help-buffer)
799 (with-current-buffer buffer
800 (let (minor-modes)
801 ;; Older packages do not register in minor-mode-list but only in
802 ;; minor-mode-alist.
803 (dolist (x minor-mode-alist)
804 (setq x (car x))
805 (unless (memq x minor-mode-list)
806 (push x minor-mode-list)))
807 ;; Find enabled minor mode we will want to mention.
808 (dolist (mode minor-mode-list)
809 ;; Document a minor mode if it is listed in minor-mode-alist,
810 ;; non-nil, and has a function definition.
811 (let ((fmode (or (get mode :minor-mode-function) mode)))
812 (and (boundp mode) (symbol-value mode)
813 (fboundp fmode)
814 (let ((pretty-minor-mode
815 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
816 (symbol-name fmode))
817 (capitalize
818 (substring (symbol-name fmode)
819 0 (match-beginning 0)))
820 fmode)))
821 (push (list fmode pretty-minor-mode
822 (format-mode-line (assq mode minor-mode-alist)))
823 minor-modes)))))
824 (setq minor-modes
825 (sort minor-modes
826 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
827 (when minor-modes
828 (princ "Enabled minor modes:\n")
829 (make-local-variable 'help-button-cache)
830 (with-current-buffer standard-output
831 (dolist (mode minor-modes)
832 (let ((mode-function (nth 0 mode))
833 (pretty-minor-mode (nth 1 mode))
834 (indicator (nth 2 mode)))
835 (add-text-properties 0 (length pretty-minor-mode)
836 '(face bold) pretty-minor-mode)
837 (save-excursion
838 (goto-char (point-max))
839 (princ "\n\f\n")
840 (push (point-marker) help-button-cache)
841 ;; Document the minor modes fully.
842 (insert pretty-minor-mode)
843 (princ (format " minor mode (%s):\n"
844 (if (zerop (length indicator))
845 "no indicator"
846 (format "indicator%s"
847 indicator))))
848 (princ (documentation mode-function)))
849 (insert-button pretty-minor-mode
850 'action (car help-button-cache)
851 'follow-link t
852 'help-echo "mouse-2, RET: show full information")
853 (newline)))
854 (forward-line -1)
855 (fill-paragraph nil)
856 (forward-line 1))
858 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
859 ;; Document the major mode.
860 (let ((mode mode-name))
861 (with-current-buffer standard-output
862 (let ((start (point)))
863 (insert (format-mode-line mode nil nil buffer))
864 (add-text-properties start (point) '(face bold)))))
865 (princ " mode")
866 (let* ((mode major-mode)
867 (file-name (find-lisp-object-file-name mode nil)))
868 (when file-name
869 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
870 ;; Make a hyperlink to the library.
871 (with-current-buffer standard-output
872 (save-excursion
873 (re-search-backward "`\\([^`']+\\)'" nil t)
874 (help-xref-button 1 'help-function-def mode file-name)))))
875 (princ ":\n")
876 (princ (documentation major-mode)))))
877 ;; For the sake of IELM and maybe others
878 nil)
881 (defun describe-minor-mode (minor-mode)
882 "Display documentation of a minor mode given as MINOR-MODE.
883 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
884 appeared on the mode-line."
885 (interactive (list (completing-read
886 "Minor mode: "
887 (nconc
888 (describe-minor-mode-completion-table-for-symbol)
889 (describe-minor-mode-completion-table-for-indicator)
890 ))))
891 (if (symbolp minor-mode)
892 (setq minor-mode (symbol-name minor-mode)))
893 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
894 (indicators (describe-minor-mode-completion-table-for-indicator)))
895 (cond
896 ((member minor-mode symbols)
897 (describe-minor-mode-from-symbol (intern minor-mode)))
898 ((member minor-mode indicators)
899 (describe-minor-mode-from-indicator minor-mode))
901 (error "No such minor mode: %s" minor-mode)))))
903 ;; symbol
904 (defun describe-minor-mode-completion-table-for-symbol ()
905 ;; In order to list up all minor modes, minor-mode-list
906 ;; is used here instead of minor-mode-alist.
907 (delq nil (mapcar 'symbol-name minor-mode-list)))
909 (defun describe-minor-mode-from-symbol (symbol)
910 "Display documentation of a minor mode given as a symbol, SYMBOL"
911 (interactive (list (intern (completing-read
912 "Minor mode symbol: "
913 (describe-minor-mode-completion-table-for-symbol)))))
914 (if (fboundp symbol)
915 (describe-function symbol)
916 (describe-variable symbol)))
918 ;; indicator
919 (defun describe-minor-mode-completion-table-for-indicator ()
920 (delq nil
921 (mapcar (lambda (x)
922 (let ((i (format-mode-line x)))
923 ;; remove first space if existed
924 (cond
925 ((= 0 (length i))
926 nil)
927 ((eq (aref i 0) ?\s)
928 (substring i 1))
930 i))))
931 minor-mode-alist)))
933 (defun describe-minor-mode-from-indicator (indicator)
934 "Display documentation of a minor mode specified by INDICATOR.
935 If you call this function interactively, you can give indicator which
936 is currently activated with completion."
937 (interactive (list
938 (completing-read
939 "Minor mode indicator: "
940 (describe-minor-mode-completion-table-for-indicator))))
941 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
942 (if minor-mode
943 (describe-minor-mode-from-symbol minor-mode)
944 (error "Cannot find minor mode for `%s'" indicator))))
946 (defun lookup-minor-mode-from-indicator (indicator)
947 "Return a minor mode symbol from its indicator on the mode line."
948 ;; remove first space if existed
949 (if (and (< 0 (length indicator))
950 (eq (aref indicator 0) ?\s))
951 (setq indicator (substring indicator 1)))
952 (let ((minor-modes minor-mode-alist)
953 result)
954 (while minor-modes
955 (let* ((minor-mode (car (car minor-modes)))
956 (anindicator (format-mode-line
957 (car (cdr (car minor-modes))))))
958 ;; remove first space if existed
959 (if (and (stringp anindicator)
960 (> (length anindicator) 0)
961 (eq (aref anindicator 0) ?\s))
962 (setq anindicator (substring anindicator 1)))
963 (if (equal indicator anindicator)
964 (setq result minor-mode
965 minor-modes nil)
966 (setq minor-modes (cdr minor-modes)))))
967 result))
969 ;;; Automatic resizing of temporary buffers.
970 (defcustom temp-buffer-max-height
971 (lambda (buffer)
972 (if (eq (selected-window) (frame-root-window))
973 (/ (x-display-pixel-height) (frame-char-height) 2)
974 (/ (- (frame-height) 2) 2)))
975 "Maximum height of a window displaying a temporary buffer.
976 This is effective only when Temp Buffer Resize mode is enabled.
977 The value is the maximum height (in lines) which
978 `resize-temp-buffer-window' will give to a window displaying a
979 temporary buffer. It can also be a function to be called to
980 choose the height for such a buffer. It gets one argument, the
981 buffer, and should return a positive integer. At the time the
982 function is called, the window to be resized is selected."
983 :type '(choice integer function)
984 :group 'help
985 :version "24.3")
987 (define-minor-mode temp-buffer-resize-mode
988 "Toggle auto-resizing temporary buffer windows (Temp Buffer Resize Mode).
989 With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
990 is positive, and disable it otherwise. If called from Lisp,
991 enable the mode if ARG is omitted or nil.
993 When Temp Buffer Resize mode is enabled, the windows in which we
994 show a temporary buffer are automatically resized in height to
995 fit the buffer's contents, but never more than
996 `temp-buffer-max-height' nor less than `window-min-height'.
998 A window is resized only if it has been specially created for the
999 buffer. Windows that have shown another buffer before are not
1000 resized. A frame is resized only if `fit-frame-to-buffer' is
1001 non-nil.
1003 This mode is used by `help', `apropos' and `completion' buffers,
1004 and some others."
1005 :global t :group 'help
1006 (if temp-buffer-resize-mode
1007 ;; `help-make-xrefs' may add a `back' button and thus increase the
1008 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1009 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1010 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
1012 (defun resize-temp-buffer-window (&optional window)
1013 "Resize WINDOW to fit its contents.
1014 WINDOW can be any live window and defaults to the selected one.
1016 Do not make WINDOW higher than `temp-buffer-max-height' nor
1017 smaller than `window-min-height'. Do nothing if WINDOW is not
1018 vertically combined, some of its contents are scrolled out of
1019 view, or WINDOW was not created by `display-buffer'."
1020 (setq window (window-normalize-window window t))
1021 (let ((buffer-name (buffer-name (window-buffer window))))
1022 (let ((height (if (functionp temp-buffer-max-height)
1023 (with-selected-window window
1024 (funcall temp-buffer-max-height (window-buffer)))
1025 temp-buffer-max-height))
1026 (quit-cadr (cadr (window-parameter window 'quit-restore))))
1027 (cond
1028 ;; Resize WINDOW iff it was split off by `display-buffer'.
1029 ((and (eq quit-cadr 'window)
1030 (pos-visible-in-window-p (point-min) window)
1031 (window-combined-p window))
1032 (fit-window-to-buffer window height))
1033 ;; Resize FRAME iff it was created by `display-buffer'.
1034 ((and fit-frame-to-buffer
1035 (eq quit-cadr 'frame)
1036 (eq window (frame-root-window window)))
1037 (let ((frame (window-frame window)))
1038 (fit-frame-to-buffer
1039 frame (+ (frame-height frame)
1040 (- (window-total-size window))
1041 height))))))))
1043 ;;; Help windows.
1044 (defcustom help-window-select 'other
1045 "Non-nil means select help window for viewing.
1046 Choices are:
1047 never (nil) Select help window only if there is no other window
1048 on its frame.
1049 other Select help window unless the selected window is the
1050 only other window on the help window's frame.
1051 always (t) Always select the help window.
1053 This option has effect if and only if the help window was created
1054 by `with-help-window'"
1055 :type '(choice (const :tag "never (nil)" nil)
1056 (const :tag "other" other)
1057 (const :tag "always (t)" t))
1058 :group 'help
1059 :version "23.1")
1061 (defcustom help-enable-auto-load t
1062 "Whether Help commands can perform autoloading.
1063 If non-nil, whenever \\[describe-function] is called for an
1064 autoloaded function whose docstring contains any key substitution
1065 construct (see `substitute-command-keys'), the library is loaded,
1066 so that the documentation can show the right key bindings."
1067 :type 'boolean
1068 :group 'help
1069 :version "24.3")
1071 (defun help-window-display-message (quit-part window &optional scroll)
1072 "Display message telling how to quit and scroll help window.
1073 QUIT-PART is a string telling how to quit the help window WINDOW.
1074 Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1075 SCROLL equal `other' means tell how to scroll the \"other\"
1076 window."
1077 (let ((scroll-part
1078 (cond
1079 ;; If we don't have QUIT-PART we probably reuse a window
1080 ;; showing the same buffer so we don't show any message.
1081 ((not quit-part) nil)
1082 ((pos-visible-in-window-p
1083 (with-current-buffer (window-buffer window)
1084 (point-max)) window t)
1085 ;; Buffer end is at least partially visible, no need to talk
1086 ;; about scrolling.
1087 ".")
1088 ((eq scroll 'other)
1089 ", \\[scroll-other-window] to scroll help.")
1090 (scroll ", \\[scroll-up] to scroll help."))))
1091 (message "%s"
1092 (substitute-command-keys (concat quit-part scroll-part)))))
1094 (defun help-window-setup (help-window)
1095 "Set up help window for `with-help-window'.
1096 HELP-WINDOW is the window used for displaying the help buffer."
1097 (let* ((help-buffer (when (window-live-p help-window)
1098 (window-buffer help-window)))
1099 (help-setup (when (window-live-p help-window)
1100 (car (window-parameter help-window 'quit-restore)))))
1101 (when help-buffer
1102 ;; Handle `help-window-point-marker'.
1103 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1104 (set-window-point help-window help-window-point-marker)
1105 ;; Reset `help-window-point-marker'.
1106 (set-marker help-window-point-marker nil))
1108 (cond
1109 ((or (eq help-window (selected-window))
1110 (and (or (eq help-window-select t)
1111 (eq help-setup 'frame)
1112 (and (eq help-window-select 'other)
1113 (eq (window-frame help-window) (selected-frame))
1114 (> (length (window-list nil 'no-mini)) 2)))
1115 (select-window help-window)))
1116 ;; The help window is or gets selected ...
1117 (help-window-display-message
1118 (cond
1119 ((eq help-setup 'window)
1120 ;; ... and is new, ...
1121 "Type \"q\" to delete help window")
1122 ((eq help-setup 'frame)
1123 "Type \"q\" to delete help frame")
1124 ((eq help-setup 'other)
1125 ;; ... or displayed some other buffer before.
1126 "Type \"q\" to restore previous buffer"))
1127 help-window t))
1128 ((and (eq (window-frame help-window) (selected-frame))
1129 (= (length (window-list nil 'no-mini)) 2))
1130 ;; There are two windows on the help window's frame and the
1131 ;; other one is the selected one.
1132 (help-window-display-message
1133 (cond
1134 ((eq help-setup 'window)
1135 "Type \\[delete-other-windows] to delete the help window")
1136 ((eq help-setup 'other)
1137 "Type \"q\" in help window to restore its previous buffer"))
1138 help-window 'other))
1140 ;; The help window is not selected ...
1141 (help-window-display-message
1142 (cond
1143 ((eq help-setup 'window)
1144 ;; ... and is new, ...
1145 "Type \"q\" in help window to delete it")
1146 ((eq help-setup 'other)
1147 ;; ... or displayed some other buffer before.
1148 "Type \"q\" in help window to restore previous buffer"))
1149 help-window))))))
1151 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1152 ;; providing the following additional twists:
1154 ;; (1) Issue more accurate messages telling how to scroll and quit the
1155 ;; help window.
1157 ;; (2) An option (customizable via `help-window-select') to select the
1158 ;; help window automatically.
1160 ;; (3) A marker (`help-window-point-marker') to move point in the help
1161 ;; window to an arbitrary buffer position.
1163 ;; Note: It's usually always wrong to use `help-print-return-message' in
1164 ;; the body of `with-help-window'.
1165 (defmacro with-help-window (buffer-name &rest body)
1166 "Display buffer with name BUFFER-NAME in a help window evaluating BODY.
1167 Select help window if the actual value of the user option
1168 `help-window-select' says so. Return last value in BODY."
1169 (declare (indent 1) (debug t))
1170 `(progn
1171 ;; Make `help-window-point-marker' point nowhere. The only place
1172 ;; where this should be set to a buffer position is within BODY.
1173 (set-marker help-window-point-marker nil)
1174 (let* (help-window
1175 (temp-buffer-show-hook
1176 (cons (lambda () (setq help-window (selected-window)))
1177 temp-buffer-show-hook)))
1178 ;; Return value returned by `with-output-to-temp-buffer'.
1179 (prog1
1180 (with-output-to-temp-buffer ,buffer-name
1181 (progn ,@body))
1182 (help-window-setup help-window)))))
1184 ;; Called from C, on encountering `help-char' when reading a char.
1185 ;; Don't print to *Help*; that would clobber Help history.
1186 (defun help-form-show ()
1187 "Display the output of a non-nil `help-form'."
1188 (let ((msg (eval help-form)))
1189 (if (stringp msg)
1190 (with-output-to-temp-buffer " *Char Help*"
1191 (princ msg)))))
1193 (provide 'help)
1195 ;;; help.el ends here