* lisp/subr.el (internal--called-interactively-p--get-frame): Find aliases
[emacs.git] / lisp / help.el
blob4219dd86986fac3903085cc2a98f257447663cbc
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 (switch-to-buffer (get-buffer-create "*Messages*")))
417 (defun view-order-manuals ()
418 "Display the Emacs ORDERS file."
419 (interactive)
420 (view-help-file "ORDERS"))
422 (defun view-emacs-FAQ ()
423 "Display the Emacs Frequently Asked Questions (FAQ) file."
424 (interactive)
425 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
426 (info "(efaq)"))
428 (defun view-emacs-problems ()
429 "Display info on known problems with Emacs and possible workarounds."
430 (interactive)
431 (view-help-file "PROBLEMS"))
433 (defun view-emacs-debugging ()
434 "Display info on how to debug Emacs problems."
435 (interactive)
436 (view-help-file "DEBUG"))
438 (defun view-external-packages ()
439 "Display external packages and information about Emacs."
440 (interactive)
441 (view-help-file "MORE.STUFF"))
443 (defun view-lossage ()
444 "Display last 300 input keystrokes.
446 To record all your input on a file, use `open-dribble-file'."
447 (interactive)
448 (help-setup-xref (list #'view-lossage)
449 (called-interactively-p 'interactive))
450 (with-help-window (help-buffer)
451 (princ (mapconcat (lambda (key)
452 (if (or (integerp key) (symbolp key) (listp key))
453 (single-key-description key)
454 (prin1-to-string key nil)))
455 (recent-keys)
456 " "))
457 (with-current-buffer standard-output
458 (goto-char (point-min))
459 (while (progn (move-to-column 50) (not (eobp)))
460 (when (search-forward " " nil t)
461 (delete-char -1))
462 (insert "\n"))
463 ;; jidanni wants to see the last keystrokes immediately.
464 (set-marker help-window-point-marker (point)))))
467 ;; Key bindings
469 (defun describe-bindings (&optional prefix buffer)
470 "Show a list of all defined keys, and their definitions.
471 We put that list in a buffer, and display the buffer.
473 The optional argument PREFIX, if non-nil, should be a key sequence;
474 then we display only bindings that start with that prefix.
475 The optional argument BUFFER specifies which buffer's bindings
476 to display (default, the current buffer). BUFFER can be a buffer
477 or a buffer name."
478 (interactive)
479 (or buffer (setq buffer (current-buffer)))
480 (help-setup-xref (list #'describe-bindings prefix buffer)
481 (called-interactively-p 'interactive))
482 (with-current-buffer buffer
483 (describe-bindings-internal nil prefix)))
485 ;; This function used to be in keymap.c.
486 (defun describe-bindings-internal (&optional menus prefix)
487 "Show a list of all defined keys, and their definitions.
488 We put that list in a buffer, and display the buffer.
490 The optional argument MENUS, if non-nil, says to mention menu bindings.
491 \(Ordinarily these are omitted from the output.)
492 The optional argument PREFIX, if non-nil, should be a key sequence;
493 then we display only bindings that start with that prefix."
494 (let ((buf (current-buffer)))
495 (with-help-window "*Help*"
496 (with-current-buffer standard-output
497 (describe-buffer-bindings buf prefix menus)))))
499 (defun where-is (definition &optional insert)
500 "Print message listing key sequences that invoke the command DEFINITION.
501 Argument is a command definition, usually a symbol with a function definition.
502 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
503 (interactive
504 (let ((fn (function-called-at-point))
505 (enable-recursive-minibuffers t)
506 val)
507 (setq val (completing-read
508 (if fn
509 (format "Where is command (default %s): " fn)
510 "Where is command: ")
511 obarray 'commandp t))
512 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
513 (unless definition (error "No command"))
514 (let ((func (indirect-function definition))
515 (defs nil)
516 (standard-output (if insert (current-buffer) standard-output)))
517 ;; In DEFS, find all symbols that are aliases for DEFINITION.
518 (mapatoms (lambda (symbol)
519 (and (fboundp symbol)
520 (not (eq symbol definition))
521 (eq func (condition-case ()
522 (indirect-function symbol)
523 (error symbol)))
524 (push symbol defs))))
525 ;; Look at all the symbols--first DEFINITION,
526 ;; then its aliases.
527 (dolist (symbol (cons definition defs))
528 (let* ((remapped (command-remapping symbol))
529 (keys (where-is-internal
530 symbol overriding-local-map nil nil remapped))
531 (keys (mapconcat 'key-description keys ", "))
532 string)
533 (setq string
534 (if insert
535 (if (> (length keys) 0)
536 (if remapped
537 (format "%s (%s) (remapped from %s)"
538 keys remapped symbol)
539 (format "%s (%s)" keys symbol))
540 (format "M-x %s RET" symbol))
541 (if (> (length keys) 0)
542 (if remapped
543 (format "%s is remapped to %s which is on %s"
544 symbol remapped keys)
545 (format "%s is on %s" symbol keys))
546 ;; If this is the command the user asked about,
547 ;; and it is not on any key, say so.
548 ;; For other symbols, its aliases, say nothing
549 ;; about them unless they are on keys.
550 (if (eq symbol definition)
551 (format "%s is not on any key" symbol)))))
552 (when string
553 (unless (eq symbol definition)
554 (princ ";\n its alias "))
555 (princ string)))))
556 nil)
558 (defun help-key-description (key untranslated)
559 (let ((string (key-description key)))
560 (if (or (not untranslated)
561 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
562 string
563 (let ((otherstring (key-description untranslated)))
564 (if (equal string otherstring)
565 string
566 (format "%s (translated from %s)" string otherstring))))))
568 (defun describe-key-briefly (&optional key insert untranslated)
569 "Print the name of the function KEY invokes. KEY is a string.
570 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
571 If non-nil, UNTRANSLATED is a vector of the untranslated events.
572 It can also be a number in which case the untranslated events from
573 the last key hit are used.
575 If KEY is a menu item or a tool-bar button that is disabled, this command
576 temporarily enables it to allow getting help on disabled items and buttons."
577 (interactive
578 (let ((enable-disabled-menus-and-buttons t)
579 (cursor-in-echo-area t)
580 saved-yank-menu)
581 (unwind-protect
582 (let (key)
583 ;; If yank-menu is empty, populate it temporarily, so that
584 ;; "Select and Paste" menu can generate a complete event.
585 (when (null (cdr yank-menu))
586 (setq saved-yank-menu (copy-sequence yank-menu))
587 (menu-bar-update-yank-menu "(any string)" nil))
588 (setq key (read-key-sequence "Describe key (or click or menu item): "))
589 ;; Clear the echo area message (Bug#7014).
590 (message nil)
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.
788 If called from Lisp with a non-nil BUFFER argument, display
789 documentation for the major and minor modes of that buffer."
790 (interactive "@")
791 (unless buffer (setq buffer (current-buffer)))
792 (help-setup-xref (list #'describe-mode buffer)
793 (called-interactively-p 'interactive))
794 ;; For the sake of help-do-xref and help-xref-go-back,
795 ;; don't switch buffers before calling `help-buffer'.
796 (with-help-window (help-buffer)
797 (with-current-buffer buffer
798 (let (minor-modes)
799 ;; Older packages do not register in minor-mode-list but only in
800 ;; minor-mode-alist.
801 (dolist (x minor-mode-alist)
802 (setq x (car x))
803 (unless (memq x minor-mode-list)
804 (push x minor-mode-list)))
805 ;; Find enabled minor mode we will want to mention.
806 (dolist (mode minor-mode-list)
807 ;; Document a minor mode if it is listed in minor-mode-alist,
808 ;; non-nil, and has a function definition.
809 (let ((fmode (or (get mode :minor-mode-function) mode)))
810 (and (boundp mode) (symbol-value mode)
811 (fboundp fmode)
812 (let ((pretty-minor-mode
813 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
814 (symbol-name fmode))
815 (capitalize
816 (substring (symbol-name fmode)
817 0 (match-beginning 0)))
818 fmode)))
819 (push (list fmode pretty-minor-mode
820 (format-mode-line (assq mode minor-mode-alist)))
821 minor-modes)))))
822 (setq minor-modes
823 (sort minor-modes
824 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
825 (when minor-modes
826 (princ "Enabled minor modes:\n")
827 (make-local-variable 'help-button-cache)
828 (with-current-buffer standard-output
829 (dolist (mode minor-modes)
830 (let ((mode-function (nth 0 mode))
831 (pretty-minor-mode (nth 1 mode))
832 (indicator (nth 2 mode)))
833 (add-text-properties 0 (length pretty-minor-mode)
834 '(face bold) pretty-minor-mode)
835 (save-excursion
836 (goto-char (point-max))
837 (princ "\n\f\n")
838 (push (point-marker) help-button-cache)
839 ;; Document the minor modes fully.
840 (insert pretty-minor-mode)
841 (princ (format " minor mode (%s):\n"
842 (if (zerop (length indicator))
843 "no indicator"
844 (format "indicator%s"
845 indicator))))
846 (princ (documentation mode-function)))
847 (insert-button pretty-minor-mode
848 'action (car help-button-cache)
849 'follow-link t
850 'help-echo "mouse-2, RET: show full information")
851 (newline)))
852 (forward-line -1)
853 (fill-paragraph nil)
854 (forward-line 1))
856 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
857 ;; Document the major mode.
858 (let ((mode mode-name))
859 (with-current-buffer standard-output
860 (let ((start (point)))
861 (insert (format-mode-line mode nil nil buffer))
862 (add-text-properties start (point) '(face bold)))))
863 (princ " mode")
864 (let* ((mode major-mode)
865 (file-name (find-lisp-object-file-name mode nil)))
866 (when file-name
867 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
868 ;; Make a hyperlink to the library.
869 (with-current-buffer standard-output
870 (save-excursion
871 (re-search-backward "`\\([^`']+\\)'" nil t)
872 (help-xref-button 1 'help-function-def mode file-name)))))
873 (princ ":\n")
874 (princ (documentation major-mode)))))
875 ;; For the sake of IELM and maybe others
876 nil)
879 (defun describe-minor-mode (minor-mode)
880 "Display documentation of a minor mode given as MINOR-MODE.
881 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
882 appeared on the mode-line."
883 (interactive (list (completing-read
884 "Minor mode: "
885 (nconc
886 (describe-minor-mode-completion-table-for-symbol)
887 (describe-minor-mode-completion-table-for-indicator)
888 ))))
889 (if (symbolp minor-mode)
890 (setq minor-mode (symbol-name minor-mode)))
891 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
892 (indicators (describe-minor-mode-completion-table-for-indicator)))
893 (cond
894 ((member minor-mode symbols)
895 (describe-minor-mode-from-symbol (intern minor-mode)))
896 ((member minor-mode indicators)
897 (describe-minor-mode-from-indicator minor-mode))
899 (error "No such minor mode: %s" minor-mode)))))
901 ;; symbol
902 (defun describe-minor-mode-completion-table-for-symbol ()
903 ;; In order to list up all minor modes, minor-mode-list
904 ;; is used here instead of minor-mode-alist.
905 (delq nil (mapcar 'symbol-name minor-mode-list)))
907 (defun describe-minor-mode-from-symbol (symbol)
908 "Display documentation of a minor mode given as a symbol, SYMBOL"
909 (interactive (list (intern (completing-read
910 "Minor mode symbol: "
911 (describe-minor-mode-completion-table-for-symbol)))))
912 (if (fboundp symbol)
913 (describe-function symbol)
914 (describe-variable symbol)))
916 ;; indicator
917 (defun describe-minor-mode-completion-table-for-indicator ()
918 (delq nil
919 (mapcar (lambda (x)
920 (let ((i (format-mode-line x)))
921 ;; remove first space if existed
922 (cond
923 ((= 0 (length i))
924 nil)
925 ((eq (aref i 0) ?\s)
926 (substring i 1))
928 i))))
929 minor-mode-alist)))
931 (defun describe-minor-mode-from-indicator (indicator)
932 "Display documentation of a minor mode specified by INDICATOR.
933 If you call this function interactively, you can give indicator which
934 is currently activated with completion."
935 (interactive (list
936 (completing-read
937 "Minor mode indicator: "
938 (describe-minor-mode-completion-table-for-indicator))))
939 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
940 (if minor-mode
941 (describe-minor-mode-from-symbol minor-mode)
942 (error "Cannot find minor mode for `%s'" indicator))))
944 (defun lookup-minor-mode-from-indicator (indicator)
945 "Return a minor mode symbol from its indicator on the mode line."
946 ;; remove first space if existed
947 (if (and (< 0 (length indicator))
948 (eq (aref indicator 0) ?\s))
949 (setq indicator (substring indicator 1)))
950 (let ((minor-modes minor-mode-alist)
951 result)
952 (while minor-modes
953 (let* ((minor-mode (car (car minor-modes)))
954 (anindicator (format-mode-line
955 (car (cdr (car minor-modes))))))
956 ;; remove first space if existed
957 (if (and (stringp anindicator)
958 (> (length anindicator) 0)
959 (eq (aref anindicator 0) ?\s))
960 (setq anindicator (substring anindicator 1)))
961 (if (equal indicator anindicator)
962 (setq result minor-mode
963 minor-modes nil)
964 (setq minor-modes (cdr minor-modes)))))
965 result))
967 ;;; Automatic resizing of temporary buffers.
968 (defcustom temp-buffer-max-height
969 (lambda (buffer)
970 (if (eq (selected-window) (frame-root-window))
971 (/ (x-display-pixel-height) (frame-char-height) 2)
972 (/ (- (frame-height) 2) 2)))
973 "Maximum height of a window displaying a temporary buffer.
974 This is effective only when Temp Buffer Resize mode is enabled.
975 The value is the maximum height (in lines) which
976 `resize-temp-buffer-window' will give to a window displaying a
977 temporary buffer. It can also be a function to be called to
978 choose the height for such a buffer. It gets one argument, the
979 buffer, and should return a positive integer. At the time the
980 function is called, the window to be resized is selected."
981 :type '(choice integer function)
982 :group 'help
983 :version "24.3")
985 (define-minor-mode temp-buffer-resize-mode
986 "Toggle auto-resizing temporary buffer windows (Temp Buffer Resize Mode).
987 With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
988 is positive, and disable it otherwise. If called from Lisp,
989 enable the mode if ARG is omitted or nil.
991 When Temp Buffer Resize mode is enabled, the windows in which we
992 show a temporary buffer are automatically resized in height to
993 fit the buffer's contents, but never more than
994 `temp-buffer-max-height' nor less than `window-min-height'.
996 A window is resized only if it has been specially created for the
997 buffer. Windows that have shown another buffer before are not
998 resized. A frame is resized only if `fit-frame-to-buffer' is
999 non-nil.
1001 This mode is used by `help', `apropos' and `completion' buffers,
1002 and some others."
1003 :global t :group 'help
1004 (if temp-buffer-resize-mode
1005 ;; `help-make-xrefs' may add a `back' button and thus increase the
1006 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1007 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1008 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
1010 (defun resize-temp-buffer-window (&optional window)
1011 "Resize WINDOW to fit its contents.
1012 WINDOW can be any live window and defaults to the selected one.
1014 Do not make WINDOW higher than `temp-buffer-max-height' nor
1015 smaller than `window-min-height'. Do nothing if WINDOW is not
1016 vertically combined, some of its contents are scrolled out of
1017 view, or WINDOW was not created by `display-buffer'."
1018 (setq window (window-normalize-window window t))
1019 (let ((buffer-name (buffer-name (window-buffer window))))
1020 (let ((height (if (functionp temp-buffer-max-height)
1021 (with-selected-window window
1022 (funcall temp-buffer-max-height (window-buffer)))
1023 temp-buffer-max-height))
1024 (quit-cadr (cadr (window-parameter window 'quit-restore))))
1025 (cond
1026 ;; Resize WINDOW iff it was split off by `display-buffer'.
1027 ((and (eq quit-cadr 'window)
1028 (pos-visible-in-window-p (point-min) window)
1029 (window-combined-p window))
1030 (fit-window-to-buffer window height))
1031 ;; Resize FRAME iff it was created by `display-buffer'.
1032 ((and fit-frame-to-buffer
1033 (eq quit-cadr 'frame)
1034 (eq window (frame-root-window window)))
1035 (let ((frame (window-frame window)))
1036 (fit-frame-to-buffer
1037 frame (+ (frame-height frame)
1038 (- (window-total-size window))
1039 height))))))))
1041 ;;; Help windows.
1042 (defcustom help-window-select 'other
1043 "Non-nil means select help window for viewing.
1044 Choices are:
1045 never (nil) Select help window only if there is no other window
1046 on its frame.
1047 other Select help window unless the selected window is the
1048 only other window on the help window's frame.
1049 always (t) Always select the help window.
1051 This option has effect if and only if the help window was created
1052 by `with-help-window'"
1053 :type '(choice (const :tag "never (nil)" nil)
1054 (const :tag "other" other)
1055 (const :tag "always (t)" t))
1056 :group 'help
1057 :version "23.1")
1059 (defcustom help-enable-auto-load t
1060 "Whether Help commands can perform autoloading.
1061 If non-nil, whenever \\[describe-function] is called for an
1062 autoloaded function whose docstring contains any key substitution
1063 construct (see `substitute-command-keys'), the library is loaded,
1064 so that the documentation can show the right key bindings."
1065 :type 'boolean
1066 :group 'help
1067 :version "24.3")
1069 (defun help-window-display-message (quit-part window &optional scroll)
1070 "Display message telling how to quit and scroll help window.
1071 QUIT-PART is a string telling how to quit the help window WINDOW.
1072 Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1073 SCROLL equal `other' means tell how to scroll the \"other\"
1074 window."
1075 (let ((scroll-part
1076 (cond
1077 ;; If we don't have QUIT-PART we probably reuse a window
1078 ;; showing the same buffer so we don't show any message.
1079 ((not quit-part) nil)
1080 ((pos-visible-in-window-p
1081 (with-current-buffer (window-buffer window)
1082 (point-max)) window t)
1083 ;; Buffer end is at least partially visible, no need to talk
1084 ;; about scrolling.
1085 ".")
1086 ((eq scroll 'other)
1087 ", \\[scroll-other-window] to scroll help.")
1088 (scroll ", \\[scroll-up] to scroll help."))))
1089 (message "%s"
1090 (substitute-command-keys (concat quit-part scroll-part)))))
1092 (defun help-window-setup (help-window)
1093 "Set up help window for `with-help-window'.
1094 HELP-WINDOW is the window used for displaying the help buffer."
1095 (let* ((help-buffer (when (window-live-p help-window)
1096 (window-buffer help-window)))
1097 (help-setup (when (window-live-p help-window)
1098 (car (window-parameter help-window 'quit-restore)))))
1099 (when help-buffer
1100 ;; Handle `help-window-point-marker'.
1101 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1102 (set-window-point help-window help-window-point-marker)
1103 ;; Reset `help-window-point-marker'.
1104 (set-marker help-window-point-marker nil))
1106 (cond
1107 ((or (eq help-window (selected-window))
1108 (and (or (eq help-window-select t)
1109 (eq help-setup 'frame)
1110 (and (eq help-window-select 'other)
1111 (eq (window-frame help-window) (selected-frame))
1112 (> (length (window-list nil 'no-mini)) 2)))
1113 (select-window help-window)))
1114 ;; The help window is or gets selected ...
1115 (help-window-display-message
1116 (cond
1117 ((eq help-setup 'window)
1118 ;; ... and is new, ...
1119 "Type \"q\" to delete help window")
1120 ((eq help-setup 'frame)
1121 "Type \"q\" to delete help frame")
1122 ((eq help-setup 'other)
1123 ;; ... or displayed some other buffer before.
1124 "Type \"q\" to restore previous buffer"))
1125 help-window t))
1126 ((and (eq (window-frame help-window) (selected-frame))
1127 (= (length (window-list nil 'no-mini)) 2))
1128 ;; There are two windows on the help window's frame and the
1129 ;; other one is the selected one.
1130 (help-window-display-message
1131 (cond
1132 ((eq help-setup 'window)
1133 "Type \\[delete-other-windows] to delete the help window")
1134 ((eq help-setup 'other)
1135 "Type \"q\" in help window to restore its previous buffer"))
1136 help-window 'other))
1138 ;; The help window is not selected ...
1139 (help-window-display-message
1140 (cond
1141 ((eq help-setup 'window)
1142 ;; ... and is new, ...
1143 "Type \"q\" in help window to delete it")
1144 ((eq help-setup 'other)
1145 ;; ... or displayed some other buffer before.
1146 "Type \"q\" in help window to restore previous buffer"))
1147 help-window))))))
1149 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1150 ;; providing the following additional twists:
1152 ;; (1) Issue more accurate messages telling how to scroll and quit the
1153 ;; help window.
1155 ;; (2) An option (customizable via `help-window-select') to select the
1156 ;; help window automatically.
1158 ;; (3) A marker (`help-window-point-marker') to move point in the help
1159 ;; window to an arbitrary buffer position.
1161 ;; Note: It's usually always wrong to use `help-print-return-message' in
1162 ;; the body of `with-help-window'.
1163 (defmacro with-help-window (buffer-name &rest body)
1164 "Display buffer with name BUFFER-NAME in a help window evaluating BODY.
1165 Select help window if the actual value of the user option
1166 `help-window-select' says so. Return last value in BODY."
1167 (declare (indent 1) (debug t))
1168 `(progn
1169 ;; Make `help-window-point-marker' point nowhere. The only place
1170 ;; where this should be set to a buffer position is within BODY.
1171 (set-marker help-window-point-marker nil)
1172 (let* (help-window
1173 (temp-buffer-show-hook
1174 (cons (lambda () (setq help-window (selected-window)))
1175 temp-buffer-show-hook)))
1176 ;; Return value returned by `with-output-to-temp-buffer'.
1177 (prog1
1178 (with-output-to-temp-buffer ,buffer-name
1179 (progn ,@body))
1180 (help-window-setup help-window)))))
1182 ;; Called from C, on encountering `help-char' when reading a char.
1183 ;; Don't print to *Help*; that would clobber Help history.
1184 (defun help-form-show ()
1185 "Display the output of a non-nil `help-form'."
1186 (let ((msg (eval help-form)))
1187 (if (stringp msg)
1188 (with-output-to-temp-buffer " *Char Help*"
1189 (princ msg)))))
1191 (provide 'help)
1193 ;;; help.el ends here