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