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