New feature optionally to accelerate auto-repeated scrolling.
[emacs.git] / lisp / help.el
blob44716a5d0a8db184fd81537d193992db5df50e19
1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2014 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
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 built-in 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-window-old-frame nil
50 "Frame selected at the time `with-help-window' is invoked.")
52 (defvar help-map
53 (let ((map (make-sparse-keymap)))
54 (define-key map (char-to-string help-char) 'help-for-help)
55 (define-key map [help] 'help-for-help)
56 (define-key map [f1] 'help-for-help)
57 (define-key map "." 'display-local-help)
58 (define-key map "?" 'help-for-help)
60 (define-key map "\C-a" 'about-emacs)
61 (define-key map "\C-c" 'describe-copying)
62 (define-key map "\C-d" 'view-emacs-debugging)
63 (define-key map "\C-e" 'view-external-packages)
64 (define-key map "\C-f" 'view-emacs-FAQ)
65 (define-key map "\C-m" 'view-order-manuals)
66 (define-key map "\C-n" 'view-emacs-news)
67 (define-key map "\C-o" 'describe-distribution)
68 (define-key map "\C-p" 'view-emacs-problems)
69 (define-key map "\C-t" 'view-emacs-todo)
70 (define-key map "\C-w" 'describe-no-warranty)
72 ;; This does not fit the pattern, but it is natural given the C-\ command.
73 (define-key map "\C-\\" 'describe-input-method)
75 (define-key map "C" 'describe-coding-system)
76 (define-key map "F" 'Info-goto-emacs-command-node)
77 (define-key map "I" 'describe-input-method)
78 (define-key map "K" 'Info-goto-emacs-key-command-node)
79 (define-key map "L" 'describe-language-environment)
80 (define-key map "S" 'info-lookup-symbol)
82 (define-key map "a" 'apropos-command)
83 (define-key map "b" 'describe-bindings)
84 (define-key map "c" 'describe-key-briefly)
85 (define-key map "d" 'apropos-documentation)
86 (define-key map "e" 'view-echo-area-messages)
87 (define-key map "f" 'describe-function)
88 (define-key map "g" 'describe-gnu-project)
89 (define-key map "h" 'view-hello-file)
91 (define-key map "i" 'info)
92 (define-key map "4i" 'info-other-window)
94 (define-key map "k" 'describe-key)
95 (define-key map "l" 'view-lossage)
96 (define-key map "m" 'describe-mode)
97 (define-key map "n" 'view-emacs-news)
98 (define-key map "p" 'finder-by-keyword)
99 (define-key map "P" 'describe-package)
100 (define-key map "r" 'info-emacs-manual)
101 (define-key map "s" 'describe-syntax)
102 (define-key map "t" 'help-with-tutorial)
103 (define-key map "w" 'where-is)
104 (define-key map "v" 'describe-variable)
105 (define-key map "q" 'help-quit)
106 map)
107 "Keymap for characters following the Help key.")
109 (define-key global-map (char-to-string help-char) 'help-command)
110 (define-key global-map [help] 'help-command)
111 (define-key global-map [f1] 'help-command)
112 (fset 'help-command help-map)
114 ;; insert-button makes the action nil if it is not store somewhere
115 (defvar help-button-cache nil)
118 (defun help-quit ()
119 "Just exit from the Help command's command loop."
120 (interactive)
121 nil)
123 (defvar help-return-method nil
124 "What to do to \"exit\" the help buffer.
125 This is a list
126 (WINDOW . t) delete the selected window (and possibly its frame,
127 see `quit-window'), go to WINDOW.
128 (WINDOW . quit-window) do quit-window, then select WINDOW.
129 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
131 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
132 (defun help-print-return-message (&optional function)
133 "Display or return message saying how to restore windows after help command.
134 This function assumes that `standard-output' is the help buffer.
135 It computes a message, and applies the optional argument FUNCTION to it.
136 If FUNCTION is nil, it applies `message', thus displaying the message.
137 In addition, this function sets up `help-return-method', which see, that
138 specifies what to do when the user exits the help buffer.
140 Do not call this in the scope of `with-help-window'."
141 (and (not (get-buffer-window standard-output))
142 (let ((first-message
143 (cond ((or
144 pop-up-frames
145 (special-display-p (buffer-name standard-output)))
146 (setq help-return-method (cons (selected-window) t))
147 ;; If the help output buffer is a special display buffer,
148 ;; don't say anything about how to get rid of it.
149 ;; First of all, the user will do that with the window
150 ;; manager, not with Emacs.
151 ;; Secondly, the buffer has not been displayed yet,
152 ;; so we don't know whether its frame will be selected.
153 nil)
154 ((not (one-window-p t))
155 (setq help-return-method
156 (cons (selected-window) 'quit-window))
157 "Type \\[display-buffer] RET to restore the other window.")
158 (pop-up-windows
159 (setq help-return-method (cons (selected-window) t))
160 "Type \\[delete-other-windows] to remove help window.")
162 (setq help-return-method
163 (list (selected-window) (window-buffer)
164 (window-start) (window-point)))
165 "Type \\[switch-to-buffer] RET to remove help window."))))
166 (funcall (or function 'message)
167 (concat
168 (if first-message
169 (substitute-command-keys first-message))
170 (if first-message " ")
171 ;; If the help buffer will go in a separate frame,
172 ;; it's no use mentioning a command to scroll, so don't.
173 (if (or pop-up-windows
174 (special-display-p (buffer-name standard-output)))
176 (if (same-window-p (buffer-name standard-output))
177 ;; Say how to scroll this window.
178 (substitute-command-keys
179 "\\[scroll-up] to scroll the help.")
180 ;; Say how to scroll some other window.
181 (substitute-command-keys
182 "\\[scroll-other-window] to scroll the help."))))))))
184 ;; So keyboard macro definitions are documented correctly
185 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
187 (defalias 'help 'help-for-help-internal)
188 ;; find-function can find this.
189 (defalias 'help-for-help 'help-for-help-internal)
190 ;; It can't find this, but nobody will look.
191 (make-help-screen help-for-help-internal
192 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
193 ;; Don't purecopy this one, because it's not evaluated (it's
194 ;; directly used as a docstring in a function definition, so it'll
195 ;; be moved to the DOC file anyway: no need for purecopying it).
196 "You have typed %THIS-KEY%, the help character. Type a Help option:
197 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
199 a PATTERN Show commands whose name matches the PATTERN (a list of words
200 or a regexp). See also the `apropos' command.
201 b Display all key bindings.
202 c KEYS Display the command name run by the given key sequence.
203 C CODING Describe the given coding system, or RET for current ones.
204 d PATTERN Show a list of functions, variables, and other items whose
205 documentation matches the PATTERN (a list of words or a regexp).
206 e Go to the *Messages* buffer which logs echo-area messages.
207 f FUNCTION Display documentation for the given function.
208 F COMMAND Show the Emacs manual's section that describes the command.
209 g Display information about the GNU project.
210 h Display the HELLO file which illustrates various scripts.
211 i Start the Info documentation reader: read included manuals.
212 I METHOD Describe a specific input method, or RET for current.
213 k KEYS Display the full documentation for the key sequence.
214 K KEYS Show the Emacs manual's section for the command bound to KEYS.
215 l Show last 300 input keystrokes (lossage).
216 L LANG-ENV Describes a specific language environment, or RET for current.
217 m Display documentation of current minor modes and current major mode,
218 including their special commands.
219 n Display news of recent Emacs changes.
220 p TOPIC Find packages matching a given topic keyword.
221 P PACKAGE Describe the given Emacs Lisp package.
222 r Display the Emacs manual in Info mode.
223 s Display contents of current syntax table, plus explanations.
224 S SYMBOL Show the section for the given symbol in the Info manual
225 for the programming language used in this buffer.
226 t Start the Emacs learn-by-doing tutorial.
227 v VARIABLE Display the given variable's documentation and value.
228 w COMMAND Display which keystrokes invoke the given command (where-is).
229 . Display any available local help at point in the echo area.
231 C-a Information about Emacs.
232 C-c Emacs copying permission (GNU General Public License).
233 C-d Instructions for debugging GNU Emacs.
234 C-e External packages and information about Emacs.
235 C-f Emacs FAQ.
236 C-m How to order printed Emacs manuals.
237 C-n News of recent Emacs changes.
238 C-o Emacs ordering and distribution information.
239 C-p Info about known Emacs problems.
240 C-t Emacs TODO list.
241 C-w Information on absence of warranty for GNU Emacs."
242 help-map)
246 (defun function-called-at-point ()
247 "Return a function around point or else called by the list containing point.
248 If that doesn't give a function, return nil."
249 (with-syntax-table emacs-lisp-mode-syntax-table
250 (or (condition-case ()
251 (save-excursion
252 (or (not (zerop (skip-syntax-backward "_w")))
253 (eq (char-syntax (following-char)) ?w)
254 (eq (char-syntax (following-char)) ?_)
255 (forward-sexp -1))
256 (skip-chars-forward "'")
257 (let ((obj (read (current-buffer))))
258 (and (symbolp obj) (fboundp obj) obj)))
259 (error nil))
260 (condition-case ()
261 (save-excursion
262 (save-restriction
263 (narrow-to-region (max (point-min)
264 (- (point) 1000)) (point-max))
265 ;; Move up to surrounding paren, then after the open.
266 (backward-up-list 1)
267 (forward-char 1)
268 ;; If there is space here, this is probably something
269 ;; other than a real Lisp function call, so ignore it.
270 (if (looking-at "[ \t]")
271 (error "Probably not a Lisp function call"))
272 (let ((obj (read (current-buffer))))
273 (and (symbolp obj) (fboundp obj) obj))))
274 (error nil))
275 (let* ((str (find-tag-default))
276 (sym (if str (intern-soft str))))
277 (if (and sym (fboundp sym))
279 (save-match-data
280 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
281 (setq sym (intern-soft (match-string 1 str)))
282 (and (fboundp sym) sym))))))))
285 ;;; `User' help functions
287 (defun view-help-file (file &optional dir)
288 (view-file (expand-file-name file (or dir data-directory)))
289 (goto-address-mode 1)
290 (goto-char (point-min)))
292 (defun describe-distribution ()
293 "Display info on how to obtain the latest version of GNU Emacs."
294 (interactive)
295 (view-help-file "DISTRIB"))
297 (defun describe-copying ()
298 "Display info on how you may redistribute copies of GNU Emacs."
299 (interactive)
300 (view-help-file "COPYING"))
302 ;; Maybe this command should just be removed.
303 (defun describe-gnu-project ()
304 "Browse online information on the GNU project."
305 (interactive)
306 (browse-url "http://www.gnu.org/gnu/thegnuproject.html"))
308 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
310 (defun describe-no-warranty ()
311 "Display info on all the kinds of warranty Emacs does NOT have."
312 (interactive)
313 (describe-copying)
314 (let (case-fold-search)
315 (search-forward "Disclaimer of Warranty")
316 (forward-line 0)
317 (recenter 0)))
319 (defun describe-prefix-bindings ()
320 "Describe the bindings of the prefix used to reach this command.
321 The prefix described consists of all but the last event
322 of the key sequence that ran this command."
323 (interactive)
324 (let ((key (this-command-keys)))
325 (describe-bindings
326 (if (stringp key)
327 (substring key 0 (1- (length key)))
328 (let ((prefix (make-vector (1- (length key)) nil))
329 (i 0))
330 (while (< i (length prefix))
331 (aset prefix i (aref key i))
332 (setq i (1+ i)))
333 prefix)))))
334 ;; Make C-h after a prefix, when not specifically bound,
335 ;; run describe-prefix-bindings.
336 (setq prefix-help-command 'describe-prefix-bindings)
338 (defun view-emacs-news (&optional version)
339 "Display info on recent changes to Emacs.
340 With argument, display info only for the selected version."
341 (interactive "P")
342 (unless version
343 (setq version emacs-major-version))
344 (when (consp version)
345 (let* ((all-versions
346 (let (res)
347 (mapc
348 (lambda (file)
349 (with-temp-buffer
350 (insert-file-contents
351 (expand-file-name file data-directory))
352 (while (re-search-forward
353 (if (member file '("NEWS.18" "NEWS.1-17"))
354 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
355 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
356 (setq res (cons (match-string-no-properties 1) res)))))
357 (cons "NEWS"
358 (directory-files data-directory nil
359 "^NEWS\\.[0-9][-0-9]*$" nil)))
360 (sort (delete-dups res) (lambda (a b) (string< b a)))))
361 (current (car all-versions)))
362 (setq version (completing-read
363 (format "Read NEWS for the version (default %s): " current)
364 all-versions nil nil nil nil current))
365 (if (integerp (string-to-number version))
366 (setq version (string-to-number version))
367 (unless (or (member version all-versions)
368 (<= (string-to-number version) (string-to-number current)))
369 (error "No news about version %s" version)))))
370 (when (integerp version)
371 (cond ((<= version 12)
372 (setq version (format "1.%d" version)))
373 ((<= version 18)
374 (setq version (format "%d" version)))
375 ((> version emacs-major-version)
376 (error "No news about Emacs %d (yet)" version))))
377 (let* ((vn (if (stringp version)
378 (string-to-number version)
379 version))
380 (file (cond
381 ((>= vn emacs-major-version) "NEWS")
382 ((< vn 18) "NEWS.1-17")
383 (t (format "NEWS.%d" vn))))
384 res)
385 (view-file (expand-file-name file data-directory))
386 (widen)
387 (goto-char (point-min))
388 (when (stringp version)
389 (when (re-search-forward
390 (concat (if (< vn 19)
391 "Changes in Emacs[ \t]*"
392 "^\* [^0-9\n]*") version "$")
393 nil t)
394 (beginning-of-line)
395 (narrow-to-region
396 (point)
397 (save-excursion
398 (while (and (setq res
399 (re-search-forward
400 (if (< vn 19)
401 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
402 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
403 (equal (match-string-no-properties 1) version)))
404 (or res (goto-char (point-max)))
405 (beginning-of-line)
406 (point)))))))
408 (defun view-emacs-todo (&optional _arg)
409 "Display the Emacs TODO list."
410 (interactive "P")
411 (view-help-file "TODO"))
413 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
416 (defun view-echo-area-messages ()
417 "View the log of recent echo-area messages: the `*Messages*' buffer.
418 The number of messages retained in that buffer
419 is specified by the variable `message-log-max'."
420 (interactive)
421 (with-current-buffer (messages-buffer)
422 (goto-char (point-max))
423 (display-buffer (current-buffer))))
425 (defun view-order-manuals ()
426 "Display information on how to buy printed copies of Emacs manuals."
427 (interactive)
428 ;; (view-help-file "ORDERS")
429 (info "(emacs)Printed Books"))
431 (defun view-emacs-FAQ ()
432 "Display the Emacs Frequently Asked Questions (FAQ) file."
433 (interactive)
434 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
435 (info "(efaq)"))
437 (defun view-emacs-problems ()
438 "Display info on known problems with Emacs and possible workarounds."
439 (interactive)
440 (view-help-file "PROBLEMS"))
442 (defun view-emacs-debugging ()
443 "Display info on how to debug Emacs problems."
444 (interactive)
445 (view-help-file "DEBUG"))
447 ;; This used to visit MORE.STUFF; maybe it should just be removed.
448 (defun view-external-packages ()
449 "Display info on where to get more Emacs packages."
450 (interactive)
451 (info "(efaq)Packages that do not come with Emacs"))
453 (defun view-lossage ()
454 "Display last few input keystrokes and the commands run.
456 To record all your input, use `open-dribble-file'."
457 (interactive)
458 (help-setup-xref (list #'view-lossage)
459 (called-interactively-p 'interactive))
460 (with-help-window (help-buffer)
461 (princ " ")
462 (princ (mapconcat (lambda (key)
463 (cond
464 ((and (consp key) (null (car key)))
465 (format "[%s]\n" (if (symbolp (cdr key)) (cdr key)
466 "anonymous-command")))
467 ((or (integerp key) (symbolp key) (listp key))
468 (single-key-description key))
470 (prin1-to-string key nil))))
471 (recent-keys 'include-cmds)
472 " "))
473 (with-current-buffer standard-output
474 (goto-char (point-min))
475 (while (not (eobp))
476 (move-to-column 50)
477 (unless (eolp)
478 (fill-region (line-beginning-position) (line-end-position)))
479 (forward-line 1))
480 ;; jidanni wants to see the last keystrokes immediately.
481 (set-marker help-window-point-marker (point)))))
484 ;; Key bindings
486 (defun describe-bindings (&optional prefix buffer)
487 "Display a buffer showing a list of all defined keys, and their definitions.
488 The keys are displayed in order of precedence.
490 The optional argument PREFIX, if non-nil, should be a key sequence;
491 then we display only bindings that start with that prefix.
492 The optional argument BUFFER specifies which buffer's bindings
493 to display (default, the current buffer). BUFFER can be a buffer
494 or a buffer name."
495 (interactive)
496 (or buffer (setq buffer (current-buffer)))
497 (help-setup-xref (list #'describe-bindings prefix buffer)
498 (called-interactively-p 'interactive))
499 (with-help-window (help-buffer)
500 ;; Be aware that `describe-buffer-bindings' puts its output into
501 ;; the current buffer.
502 (with-current-buffer (help-buffer)
503 (describe-buffer-bindings buffer prefix))))
505 ;; This function used to be in keymap.c.
506 (defun describe-bindings-internal (&optional menus prefix)
507 "Show a list of all defined keys, and their definitions.
508 We put that list in a buffer, and display the buffer.
510 The optional argument MENUS, if non-nil, says to mention menu bindings.
511 \(Ordinarily these are omitted from the output.)
512 The optional argument PREFIX, if non-nil, should be a key sequence;
513 then we display only bindings that start with that prefix."
514 (declare (obsolete describe-buffer-bindings "24.4"))
515 (let ((buf (current-buffer)))
516 (with-help-window (help-buffer)
517 ;; Be aware that `describe-buffer-bindings' puts its output into
518 ;; the current buffer.
519 (with-current-buffer (help-buffer)
520 (describe-buffer-bindings buf prefix menus)))))
522 (defun where-is (definition &optional insert)
523 "Print message listing key sequences that invoke the command DEFINITION.
524 Argument is a command definition, usually a symbol with a function definition.
525 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
526 (interactive
527 (let ((fn (function-called-at-point))
528 (enable-recursive-minibuffers t)
529 val)
530 (setq val (completing-read
531 (if fn
532 (format "Where is command (default %s): " fn)
533 "Where is command: ")
534 obarray 'commandp t nil nil
535 (and fn (symbol-name fn))))
536 (list (unless (equal val "") (intern val))
537 current-prefix-arg)))
538 (unless definition (error "No command"))
539 (let ((func (indirect-function definition))
540 (defs nil)
541 (standard-output (if insert (current-buffer) standard-output)))
542 ;; In DEFS, find all symbols that are aliases for DEFINITION.
543 (mapatoms (lambda (symbol)
544 (and (fboundp symbol)
545 (not (eq symbol definition))
546 (eq func (condition-case ()
547 (indirect-function symbol)
548 (error symbol)))
549 (push symbol defs))))
550 ;; Look at all the symbols--first DEFINITION,
551 ;; then its aliases.
552 (dolist (symbol (cons definition defs))
553 (let* ((remapped (command-remapping symbol))
554 (keys (where-is-internal
555 symbol overriding-local-map nil nil remapped))
556 (keys (mapconcat 'key-description keys ", "))
557 string)
558 (setq string
559 (if insert
560 (if (> (length keys) 0)
561 (if remapped
562 (format "%s (%s) (remapped from %s)"
563 keys remapped symbol)
564 (format "%s (%s)" keys symbol))
565 (format "M-x %s RET" symbol))
566 (if (> (length keys) 0)
567 (if remapped
568 (format "%s is remapped to %s which is on %s"
569 symbol remapped keys)
570 (format "%s is on %s" symbol keys))
571 ;; If this is the command the user asked about,
572 ;; and it is not on any key, say so.
573 ;; For other symbols, its aliases, say nothing
574 ;; about them unless they are on keys.
575 (if (eq symbol definition)
576 (format "%s is not on any key" symbol)))))
577 (when string
578 (unless (eq symbol definition)
579 (princ ";\n its alias "))
580 (princ string)))))
581 nil)
583 (defun help-key-description (key untranslated)
584 (let ((string (key-description key)))
585 (if (or (not untranslated)
586 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
587 string
588 (let ((otherstring (key-description untranslated)))
589 (if (equal string otherstring)
590 string
591 (format "%s (translated from %s)" string otherstring))))))
593 (defun describe-key-briefly (&optional key insert untranslated)
594 "Print the name of the function KEY invokes. KEY is a string.
595 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
596 If non-nil, UNTRANSLATED is a vector of the untranslated events.
597 It can also be a number in which case the untranslated events from
598 the last key hit are used.
600 If KEY is a menu item or a tool-bar button that is disabled, this command
601 temporarily enables it to allow getting help on disabled items and buttons."
602 (interactive
603 (let ((enable-disabled-menus-and-buttons t)
604 (cursor-in-echo-area t)
605 saved-yank-menu)
606 (unwind-protect
607 (let (key)
608 ;; If yank-menu is empty, populate it temporarily, so that
609 ;; "Select and Paste" menu can generate a complete event.
610 (when (null (cdr yank-menu))
611 (setq saved-yank-menu (copy-sequence yank-menu))
612 (menu-bar-update-yank-menu "(any string)" nil))
613 (setq key (read-key-sequence "Describe key (or click or menu item): "))
614 ;; Clear the echo area message (Bug#7014).
615 (message nil)
616 ;; If KEY is a down-event, read and discard the
617 ;; corresponding up-event. Note that there are also
618 ;; down-events on scroll bars and mode lines: the actual
619 ;; event then is in the second element of the vector.
620 (and (vectorp key)
621 (let ((last-idx (1- (length key))))
622 (and (eventp (aref key last-idx))
623 (memq 'down (event-modifiers (aref key last-idx)))))
624 (read-event))
625 (list
627 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
629 ;; Put yank-menu back as it was, if we changed it.
630 (when saved-yank-menu
631 (setq yank-menu (copy-sequence saved-yank-menu))
632 (fset 'yank-menu (cons 'keymap yank-menu))))))
633 (if (numberp untranslated)
634 (setq untranslated (this-single-command-raw-keys)))
635 (let* ((event (if (and (symbolp (aref key 0))
636 (> (length key) 1)
637 (consp (aref key 1)))
638 (aref key 1)
639 (aref key 0)))
640 (modifiers (event-modifiers event))
641 (standard-output (if insert (current-buffer) standard-output))
642 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
643 (memq 'drag modifiers)) " at that spot" ""))
644 (defn (key-binding key t))
645 key-desc)
646 ;; Handle the case where we faked an entry in "Select and Paste" menu.
647 (if (and (eq defn nil)
648 (stringp (aref key (1- (length key))))
649 (eq (key-binding (substring key 0 -1)) 'yank-menu))
650 (setq defn 'menu-bar-select-yank))
651 ;; Don't bother user with strings from (e.g.) the select-paste menu.
652 (if (stringp (aref key (1- (length key))))
653 (aset key (1- (length key)) "(any string)"))
654 (if (and (> (length untranslated) 0)
655 (stringp (aref untranslated (1- (length untranslated)))))
656 (aset untranslated (1- (length untranslated)) "(any string)"))
657 ;; Now describe the key, perhaps as changed.
658 (setq key-desc (help-key-description key untranslated))
659 (if (or (null defn) (integerp defn) (equal defn 'undefined))
660 (princ (format "%s%s is undefined" key-desc mouse-msg))
661 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
663 (defun help--key-binding-keymap (key &optional accept-default no-remap position)
664 "Return a keymap holding a binding for KEY within current keymaps.
665 The effect of the arguments KEY, ACCEPT-DEFAULT, NO-REMAP and
666 POSITION is as documented in the function `key-binding'."
667 (let* ((active-maps (current-active-maps t position))
668 map found)
669 ;; We loop over active maps like key-binding does.
670 (while (and
671 (not found)
672 (setq map (pop active-maps)))
673 (setq found (lookup-key map key accept-default))
674 (when (integerp found)
675 ;; The first `found' characters of KEY were found but not the
676 ;; whole sequence.
677 (setq found nil)))
678 (when found
679 (if (and (symbolp found)
680 (not no-remap)
681 (command-remapping found))
682 ;; The user might want to know in which map the binding is
683 ;; found, or in which map the remapping is found. The
684 ;; default is to show the latter.
685 (help--key-binding-keymap (vector 'remap found))
686 map))))
688 (defun help--binding-locus (key position)
689 "Describe in which keymap KEY is defined.
690 Return a symbol pointing to that keymap if one exists ; otherwise
691 return nil."
692 (let ((map (help--key-binding-keymap key t nil position)))
693 (when map
694 (catch 'found
695 (let ((advertised-syms (nconc
696 (list 'overriding-terminal-local-map
697 'overriding-local-map)
698 (delq nil
699 (mapcar
700 (lambda (mode-and-map)
701 (let ((mode (car mode-and-map)))
702 (when (symbol-value mode)
703 (intern-soft
704 (format "%s-map" mode)))))
705 minor-mode-map-alist))
706 (list 'global-map
707 (intern-soft (format "%s-map" major-mode)))))
708 found)
709 ;; Look into these advertised symbols first.
710 (dolist (sym advertised-syms)
711 (when (and
712 (boundp sym)
713 (eq map (symbol-value sym)))
714 (throw 'found sym)))
715 ;; Only look in other symbols otherwise.
716 (mapatoms
717 (lambda (x)
718 (when (and (boundp x)
719 ;; Avoid let-bound symbols.
720 (special-variable-p x)
721 (eq (symbol-value x) map))
722 (throw 'found x))))
723 nil)))))
725 (defun describe-key (&optional key untranslated up-event)
726 "Display documentation of the function invoked by KEY.
727 KEY can be any kind of a key sequence; it can include keyboard events,
728 mouse events, and/or menu events. When calling from a program,
729 pass KEY as a string or a vector.
731 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
732 It can also be a number, in which case the untranslated events from
733 the last key sequence entered are used.
734 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
736 If KEY is a menu item or a tool-bar button that is disabled, this command
737 temporarily enables it to allow getting help on disabled items and buttons."
738 (interactive
739 (let ((enable-disabled-menus-and-buttons t)
740 (cursor-in-echo-area t)
741 saved-yank-menu)
742 (unwind-protect
743 (let (key)
744 ;; If yank-menu is empty, populate it temporarily, so that
745 ;; "Select and Paste" menu can generate a complete event.
746 (when (null (cdr yank-menu))
747 (setq saved-yank-menu (copy-sequence yank-menu))
748 (menu-bar-update-yank-menu "(any string)" nil))
749 (setq key (read-key-sequence "Describe key (or click or menu item): "))
750 (list
752 (prefix-numeric-value current-prefix-arg)
753 ;; If KEY is a down-event, read and include the
754 ;; corresponding up-event. Note that there are also
755 ;; down-events on scroll bars and mode lines: the actual
756 ;; event then is in the second element of the vector.
757 (and (vectorp key)
758 (let ((last-idx (1- (length key))))
759 (and (eventp (aref key last-idx))
760 (memq 'down (event-modifiers (aref key last-idx)))))
761 (or (and (eventp (aref key 0))
762 (memq 'down (event-modifiers (aref key 0)))
763 ;; However, for the C-down-mouse-2 popup
764 ;; menu, there is no subsequent up-event. In
765 ;; this case, the up-event is the next
766 ;; element in the supplied vector.
767 (= (length key) 1))
768 (and (> (length key) 1)
769 (eventp (aref key 1))
770 (memq 'down (event-modifiers (aref key 1)))))
771 (read-event))))
772 ;; Put yank-menu back as it was, if we changed it.
773 (when saved-yank-menu
774 (setq yank-menu (copy-sequence saved-yank-menu))
775 (fset 'yank-menu (cons 'keymap yank-menu))))))
776 (if (numberp untranslated)
777 (setq untranslated (this-single-command-raw-keys)))
778 (let* ((event (aref key (if (and (symbolp (aref key 0))
779 (> (length key) 1)
780 (consp (aref key 1)))
782 0)))
783 (modifiers (event-modifiers event))
784 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
785 (memq 'drag modifiers)) " at that spot" ""))
786 (defn (key-binding key t))
787 key-locus key-locus-up key-locus-up-tricky
788 defn-up defn-up-tricky ev-type
789 mouse-1-remapped mouse-1-tricky)
791 ;; Handle the case where we faked an entry in "Select and Paste" menu.
792 (when (and (eq defn nil)
793 (stringp (aref key (1- (length key))))
794 (eq (key-binding (substring key 0 -1)) 'yank-menu))
795 (setq defn 'menu-bar-select-yank))
796 (if (or (null defn) (integerp defn) (equal defn 'undefined))
797 (message "%s%s is undefined"
798 (help-key-description key untranslated) mouse-msg)
799 (help-setup-xref (list #'describe-function defn)
800 (called-interactively-p 'interactive))
801 ;; Don't bother user with strings from (e.g.) the select-paste menu.
802 (when (stringp (aref key (1- (length key))))
803 (aset key (1- (length key)) "(any string)"))
804 (when (and untranslated
805 (stringp (aref untranslated (1- (length untranslated)))))
806 (aset untranslated (1- (length untranslated))
807 "(any string)"))
808 ;; Need to do this before erasing *Help* buffer in case event
809 ;; is a mouse click in an existing *Help* buffer.
810 (when up-event
811 (setq ev-type (event-basic-type up-event))
812 (let ((sequence (vector up-event)))
813 (when (and (eq ev-type 'mouse-1)
814 mouse-1-click-follows-link
815 (not (eq mouse-1-click-follows-link 'double))
816 (setq mouse-1-remapped
817 (mouse-on-link-p (event-start up-event))))
818 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
819 (> mouse-1-click-follows-link 0)))
820 (cond ((stringp mouse-1-remapped)
821 (setq sequence mouse-1-remapped))
822 ((vectorp mouse-1-remapped)
823 (setcar up-event (elt mouse-1-remapped 0)))
824 (t (setcar up-event 'mouse-2))))
825 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
826 (setq key-locus-up (help--binding-locus sequence (event-start up-event)))
827 (when mouse-1-tricky
828 (setq sequence (vector up-event))
829 (aset sequence 0 'mouse-1)
830 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event)))
831 (setq key-locus-up-tricky (help--binding-locus sequence (event-start up-event))))))
832 (setq key-locus (help--binding-locus key (event-start event)))
833 (with-help-window (help-buffer)
834 (princ (help-key-description key untranslated))
835 (princ (format "%s runs the command %S%s, which is "
836 mouse-msg defn (if key-locus
837 (format " (found in %s)" key-locus)
838 "")))
839 (describe-function-1 defn)
840 (when up-event
841 (unless (or (null defn-up)
842 (integerp defn-up)
843 (equal defn-up 'undefined))
844 (princ (format "
846 ----------------- up-event %s----------------
848 %s%s%s runs the command %S%s, which is "
849 (if mouse-1-tricky "(short click) " "")
850 (key-description (vector up-event))
851 mouse-msg
852 (if mouse-1-remapped
853 " is remapped to <mouse-2>, which" "")
854 defn-up (if key-locus-up
855 (format " (found in %s)" key-locus-up)
856 "")))
857 (describe-function-1 defn-up))
858 (unless (or (null defn-up-tricky)
859 (integerp defn-up-tricky)
860 (eq defn-up-tricky 'undefined))
861 (princ (format "
863 ----------------- up-event (long click) ----------------
865 Pressing <%S>%s for longer than %d milli-seconds
866 runs the command %S%s, which is "
867 ev-type mouse-msg
868 mouse-1-click-follows-link
869 defn-up-tricky (if key-locus-up-tricky
870 (format " (found in %s)" key-locus-up-tricky)
871 "")))
872 (describe-function-1 defn-up-tricky)))))))
874 (defun describe-mode (&optional buffer)
875 "Display documentation of current major mode and minor modes.
876 A brief summary of the minor modes comes first, followed by the
877 major mode description. This is followed by detailed
878 descriptions of the minor modes, each on a separate page.
880 For this to work correctly for a minor mode, the mode's indicator
881 variable \(listed in `minor-mode-alist') must also be a function
882 whose documentation describes the minor mode.
884 If called from Lisp with a non-nil BUFFER argument, display
885 documentation for the major and minor modes of that buffer."
886 (interactive "@")
887 (unless buffer (setq buffer (current-buffer)))
888 (help-setup-xref (list #'describe-mode buffer)
889 (called-interactively-p 'interactive))
890 ;; For the sake of help-do-xref and help-xref-go-back,
891 ;; don't switch buffers before calling `help-buffer'.
892 (with-help-window (help-buffer)
893 (with-current-buffer buffer
894 (let (minor-modes)
895 ;; Older packages do not register in minor-mode-list but only in
896 ;; minor-mode-alist.
897 (dolist (x minor-mode-alist)
898 (setq x (car x))
899 (unless (memq x minor-mode-list)
900 (push x minor-mode-list)))
901 ;; Find enabled minor mode we will want to mention.
902 (dolist (mode minor-mode-list)
903 ;; Document a minor mode if it is listed in minor-mode-alist,
904 ;; non-nil, and has a function definition.
905 (let ((fmode (or (get mode :minor-mode-function) mode)))
906 (and (boundp mode) (symbol-value mode)
907 (fboundp fmode)
908 (let ((pretty-minor-mode
909 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
910 (symbol-name fmode))
911 (capitalize
912 (substring (symbol-name fmode)
913 0 (match-beginning 0)))
914 fmode)))
915 (push (list fmode pretty-minor-mode
916 (format-mode-line (assq mode minor-mode-alist)))
917 minor-modes)))))
918 (setq minor-modes
919 (sort minor-modes
920 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
921 (when minor-modes
922 (princ "Enabled minor modes:\n")
923 (make-local-variable 'help-button-cache)
924 (with-current-buffer standard-output
925 (dolist (mode minor-modes)
926 (let ((mode-function (nth 0 mode))
927 (pretty-minor-mode (nth 1 mode))
928 (indicator (nth 2 mode)))
929 (add-text-properties 0 (length pretty-minor-mode)
930 '(face bold) pretty-minor-mode)
931 (save-excursion
932 (goto-char (point-max))
933 (princ "\n\f\n")
934 (push (point-marker) help-button-cache)
935 ;; Document the minor modes fully.
936 (insert pretty-minor-mode)
937 (princ (format " minor mode (%s):\n"
938 (if (zerop (length indicator))
939 "no indicator"
940 (format "indicator%s"
941 indicator))))
942 (princ (documentation mode-function)))
943 (insert-button pretty-minor-mode
944 'action (car help-button-cache)
945 'follow-link t
946 'help-echo "mouse-2, RET: show full information")
947 (newline)))
948 (forward-line -1)
949 (fill-paragraph nil)
950 (forward-line 1))
952 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
953 ;; Document the major mode.
954 (let ((mode mode-name))
955 (with-current-buffer standard-output
956 (let ((start (point)))
957 (insert (format-mode-line mode nil nil buffer))
958 (add-text-properties start (point) '(face bold)))))
959 (princ " mode")
960 (let* ((mode major-mode)
961 (file-name (find-lisp-object-file-name mode nil)))
962 (when file-name
963 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
964 ;; Make a hyperlink to the library.
965 (with-current-buffer standard-output
966 (save-excursion
967 (re-search-backward "`\\([^`']+\\)'" nil t)
968 (help-xref-button 1 'help-function-def mode file-name)))))
969 (princ ":\n")
970 (princ (documentation major-mode)))))
971 ;; For the sake of IELM and maybe others
972 nil)
975 (defun describe-minor-mode (minor-mode)
976 "Display documentation of a minor mode given as MINOR-MODE.
977 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
978 appeared on the mode-line."
979 (interactive (list (completing-read
980 "Minor mode: "
981 (nconc
982 (describe-minor-mode-completion-table-for-symbol)
983 (describe-minor-mode-completion-table-for-indicator)
984 ))))
985 (if (symbolp minor-mode)
986 (setq minor-mode (symbol-name minor-mode)))
987 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
988 (indicators (describe-minor-mode-completion-table-for-indicator)))
989 (cond
990 ((member minor-mode symbols)
991 (describe-minor-mode-from-symbol (intern minor-mode)))
992 ((member minor-mode indicators)
993 (describe-minor-mode-from-indicator minor-mode))
995 (error "No such minor mode: %s" minor-mode)))))
997 ;; symbol
998 (defun describe-minor-mode-completion-table-for-symbol ()
999 ;; In order to list up all minor modes, minor-mode-list
1000 ;; is used here instead of minor-mode-alist.
1001 (delq nil (mapcar 'symbol-name minor-mode-list)))
1003 (defun describe-minor-mode-from-symbol (symbol)
1004 "Display documentation of a minor mode given as a symbol, SYMBOL"
1005 (interactive (list (intern (completing-read
1006 "Minor mode symbol: "
1007 (describe-minor-mode-completion-table-for-symbol)))))
1008 (if (fboundp symbol)
1009 (describe-function symbol)
1010 (describe-variable symbol)))
1012 ;; indicator
1013 (defun describe-minor-mode-completion-table-for-indicator ()
1014 (delq nil
1015 (mapcar (lambda (x)
1016 (let ((i (format-mode-line x)))
1017 ;; remove first space if existed
1018 (cond
1019 ((= 0 (length i))
1020 nil)
1021 ((eq (aref i 0) ?\s)
1022 (substring i 1))
1024 i))))
1025 minor-mode-alist)))
1027 (defun describe-minor-mode-from-indicator (indicator)
1028 "Display documentation of a minor mode specified by INDICATOR.
1029 If you call this function interactively, you can give indicator which
1030 is currently activated with completion."
1031 (interactive (list
1032 (completing-read
1033 "Minor mode indicator: "
1034 (describe-minor-mode-completion-table-for-indicator))))
1035 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
1036 (if minor-mode
1037 (describe-minor-mode-from-symbol minor-mode)
1038 (error "Cannot find minor mode for `%s'" indicator))))
1040 (defun lookup-minor-mode-from-indicator (indicator)
1041 "Return a minor mode symbol from its indicator on the mode line."
1042 ;; remove first space if existed
1043 (if (and (< 0 (length indicator))
1044 (eq (aref indicator 0) ?\s))
1045 (setq indicator (substring indicator 1)))
1046 (let ((minor-modes minor-mode-alist)
1047 result)
1048 (while minor-modes
1049 (let* ((minor-mode (car (car minor-modes)))
1050 (anindicator (format-mode-line
1051 (car (cdr (car minor-modes))))))
1052 ;; remove first space if existed
1053 (if (and (stringp anindicator)
1054 (> (length anindicator) 0)
1055 (eq (aref anindicator 0) ?\s))
1056 (setq anindicator (substring anindicator 1)))
1057 (if (equal indicator anindicator)
1058 (setq result minor-mode
1059 minor-modes nil)
1060 (setq minor-modes (cdr minor-modes)))))
1061 result))
1063 ;;; Automatic resizing of temporary buffers.
1064 (defcustom temp-buffer-max-height
1065 (lambda (buffer)
1066 (if (eq (selected-window) (frame-root-window))
1067 (/ (x-display-pixel-height) (frame-char-height) 2)
1068 (/ (- (frame-height) 2) 2)))
1069 "Maximum height of a window displaying a temporary buffer.
1070 This is effective only when Temp Buffer Resize mode is enabled.
1071 The value is the maximum height (in lines) which
1072 `resize-temp-buffer-window' will give to a window displaying a
1073 temporary buffer. It can also be a function to be called to
1074 choose the height for such a buffer. It gets one argument, the
1075 buffer, and should return a positive integer. At the time the
1076 function is called, the window to be resized is selected."
1077 :type '(choice integer function)
1078 :group 'help
1079 :version "24.3")
1081 (defcustom temp-buffer-max-width
1082 (lambda (buffer)
1083 (if (eq (selected-window) (frame-root-window))
1084 (/ (x-display-pixel-width) (frame-char-width) 2)
1085 (/ (- (frame-width) 2) 2)))
1086 "Maximum width of a window displaying a temporary buffer.
1087 This is effective only when Temp Buffer Resize mode is enabled.
1088 The value is the maximum width (in columns) which
1089 `resize-temp-buffer-window' will give to a window displaying a
1090 temporary buffer. It can also be a function to be called to
1091 choose the width for such a buffer. It gets one argument, the
1092 buffer, and should return a positive integer. At the time the
1093 function is called, the window to be resized is selected."
1094 :type '(choice integer function)
1095 :group 'help
1096 :version "24.4")
1098 (define-minor-mode temp-buffer-resize-mode
1099 "Toggle auto-resizing temporary buffer windows (Temp Buffer Resize Mode).
1100 With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
1101 is positive, and disable it otherwise. If called from Lisp,
1102 enable the mode if ARG is omitted or nil.
1104 When Temp Buffer Resize mode is enabled, the windows in which we
1105 show a temporary buffer are automatically resized in height to
1106 fit the buffer's contents, but never more than
1107 `temp-buffer-max-height' nor less than `window-min-height'.
1109 A window is resized only if it has been specially created for the
1110 buffer. Windows that have shown another buffer before are not
1111 resized. A frame is resized only if `fit-frame-to-buffer' is
1112 non-nil.
1114 This mode is used by `help', `apropos' and `completion' buffers,
1115 and some others."
1116 :global t :group 'help
1117 (if temp-buffer-resize-mode
1118 ;; `help-make-xrefs' may add a `back' button and thus increase the
1119 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1120 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1121 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
1123 (defun resize-temp-buffer-window (&optional window)
1124 "Resize WINDOW to fit its contents.
1125 WINDOW must be a live window and defaults to the selected one.
1126 Do not resize if WINDOW was not created by `display-buffer'.
1128 If WINDOW is part of a vertical combination, restrain its new
1129 size by `temp-buffer-max-height' and do not resize if its minimum
1130 accessible position is scrolled out of view. If WINDOW is part
1131 of a horizontal combination, restrain its new size by
1132 `temp-buffer-max-width'. In both cases, the value of the option
1133 `fit-window-to-buffer-horizontally' can inhibit resizing.
1135 If WINDOW is the root window of its frame, resize the frame
1136 provided `fit-frame-to-buffer' is non-nil."
1137 (setq window (window-normalize-window window t))
1138 (let ((height (if (functionp temp-buffer-max-height)
1139 (with-selected-window window
1140 (funcall temp-buffer-max-height (window-buffer)))
1141 temp-buffer-max-height))
1142 (width (if (functionp temp-buffer-max-width)
1143 (with-selected-window window
1144 (funcall temp-buffer-max-width (window-buffer)))
1145 temp-buffer-max-width))
1146 (quit-cadr (cadr (window-parameter window 'quit-restore))))
1147 ;; Resize WINDOW iff it was made by `display-buffer'.
1148 (when (or (and (eq quit-cadr 'window)
1149 (or (and (window-combined-p window)
1150 (not (eq fit-window-to-buffer-horizontally
1151 'only))
1152 (pos-visible-in-window-p (point-min) window))
1153 (and (window-combined-p window t)
1154 fit-window-to-buffer-horizontally)))
1155 (and (eq quit-cadr 'frame)
1156 fit-frame-to-buffer
1157 (eq window (frame-root-window window))))
1158 (fit-window-to-buffer window height nil width))))
1160 ;;; Help windows.
1161 (defcustom help-window-select nil
1162 "Non-nil means select help window for viewing.
1163 Choices are:
1165 never (nil) Select help window only if there is no other window
1166 on its frame.
1168 other Select help window if and only if it appears on the
1169 previously selected frame, that frame contains at
1170 least two other windows and the help window is
1171 either new or showed a different buffer before.
1173 always (t) Always select the help window.
1175 If this option is non-nil and the help window appears on another
1176 frame, then give that frame input focus too. Note also that if
1177 the help window appears on another frame, it may get selected and
1178 its frame get input focus even if this option is nil.
1180 This option has effect if and only if the help window was created
1181 by `with-help-window'."
1182 :type '(choice (const :tag "never (nil)" nil)
1183 (const :tag "other" other)
1184 (const :tag "always (t)" t))
1185 :group 'help
1186 :version "23.1")
1188 (defcustom help-enable-auto-load t
1189 "Whether Help commands can perform autoloading.
1190 If non-nil, whenever \\[describe-function] is called for an
1191 autoloaded function whose docstring contains any key substitution
1192 construct (see `substitute-command-keys'), the library is loaded,
1193 so that the documentation can show the right key bindings."
1194 :type 'boolean
1195 :group 'help
1196 :version "24.3")
1198 (defun help-window-display-message (quit-part window &optional scroll)
1199 "Display message telling how to quit and scroll help window.
1200 QUIT-PART is a string telling how to quit the help window WINDOW.
1201 Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1202 SCROLL equal `other' means tell how to scroll the \"other\"
1203 window."
1204 (let ((scroll-part
1205 (cond
1206 ;; If we don't have QUIT-PART we probably reuse a window
1207 ;; showing the same buffer so we don't show any message.
1208 ((not quit-part) nil)
1209 ((pos-visible-in-window-p
1210 (with-current-buffer (window-buffer window)
1211 (point-max)) window t)
1212 ;; Buffer end is at least partially visible, no need to talk
1213 ;; about scrolling.
1214 ".")
1215 ((eq scroll 'other)
1216 ", \\[scroll-other-window] to scroll help.")
1217 (scroll ", \\[scroll-up] to scroll help."))))
1218 (message "%s"
1219 (substitute-command-keys (concat quit-part scroll-part)))))
1221 (defun help-window-setup (window &optional value)
1222 "Set up help window WINDOW for `with-help-window'.
1223 WINDOW is the window used for displaying the help buffer.
1224 Return VALUE."
1225 (let* ((help-buffer (when (window-live-p window)
1226 (window-buffer window)))
1227 (help-setup (when (window-live-p window)
1228 (car (window-parameter window 'quit-restore))))
1229 (frame (window-frame window)))
1231 (when help-buffer
1232 ;; Handle `help-window-point-marker'.
1233 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1234 (set-window-point window help-window-point-marker)
1235 ;; Reset `help-window-point-marker'.
1236 (set-marker help-window-point-marker nil))
1238 ;; If the help window appears on another frame, select it if
1239 ;; `help-window-select' is non-nil and give that frame input focus
1240 ;; too. See also Bug#19012.
1241 (when (and help-window-select
1242 (frame-live-p help-window-old-frame)
1243 (not (eq frame help-window-old-frame)))
1244 (select-window window)
1245 (select-frame-set-input-focus frame))
1247 (cond
1248 ((or (eq window (selected-window))
1249 ;; If the help window is on the selected frame, select
1250 ;; it if `help-window-select' is t or `help-window-select'
1251 ;; is 'other, the frame contains at least three windows, and
1252 ;; the help window did show another buffer before. See also
1253 ;; Bug#11039.
1254 (and (eq frame (selected-frame))
1255 (or (eq help-window-select t)
1256 (and (eq help-window-select 'other)
1257 (> (length (window-list nil 'no-mini)) 2)
1258 (not (eq help-setup 'same))))
1259 (select-window window)))
1260 ;; The help window is or gets selected ...
1261 (help-window-display-message
1262 (cond
1263 ((eq help-setup 'window)
1264 ;; ... and is new, ...
1265 "Type \"q\" to delete help window")
1266 ((eq help-setup 'frame)
1267 ;; ... on a new frame, ...
1268 "Type \"q\" to quit the help frame")
1269 ((eq help-setup 'other)
1270 ;; ... or displayed some other buffer before.
1271 "Type \"q\" to restore previous buffer"))
1272 window t))
1273 ((and (eq (window-frame window) help-window-old-frame)
1274 (= (length (window-list nil 'no-mini)) 2))
1275 ;; There are two windows on the help window's frame and the
1276 ;; other one is the selected one.
1277 (help-window-display-message
1278 (cond
1279 ((eq help-setup 'window)
1280 "Type \\[delete-other-windows] to delete the help window")
1281 ((eq help-setup 'other)
1282 "Type \"q\" in help window to restore its previous buffer"))
1283 window 'other))
1285 ;; The help window is not selected ...
1286 (help-window-display-message
1287 (cond
1288 ((eq help-setup 'window)
1289 ;; ... and is new, ...
1290 "Type \"q\" in help window to delete it")
1291 ((eq help-setup 'other)
1292 ;; ... or displayed some other buffer before.
1293 "Type \"q\" in help window to restore previous buffer"))
1294 window))))
1295 ;; Return VALUE.
1296 value))
1298 ;; `with-help-window' is a wrapper for `with-temp-buffer-window'
1299 ;; providing the following additional twists:
1301 ;; (1) It puts the buffer in `help-mode' (via `help-mode-setup') and
1302 ;; adds cross references (via `help-mode-finish').
1304 ;; (2) It issues a message telling how to scroll and quit the help
1305 ;; window (via `help-window-setup').
1307 ;; (3) An option (customizable via `help-window-select') to select the
1308 ;; help window automatically.
1310 ;; (4) A marker (`help-window-point-marker') to move point in the help
1311 ;; window to an arbitrary buffer position.
1312 (defmacro with-help-window (buffer-name &rest body)
1313 "Display buffer named BUFFER-NAME in a help window.
1314 Evaluate the forms in BODY with standard output bound to a buffer
1315 called BUFFER-NAME (creating it if it does not exist), put that
1316 buffer in `help-mode', display the buffer in a window (see
1317 `with-temp-buffer-window' for details) and issue a message how to
1318 deal with that \"help\" window when it's no more needed. Select
1319 the help window if the current value of the user option
1320 `help-window-select' says so. Return last value in BODY."
1321 (declare (indent 1) (debug t))
1322 `(progn
1323 ;; Make `help-window-point-marker' point nowhere. The only place
1324 ;; where this should be set to a buffer position is within BODY.
1325 (set-marker help-window-point-marker nil)
1326 (let ((temp-buffer-window-setup-hook
1327 (cons 'help-mode-setup temp-buffer-window-setup-hook))
1328 (temp-buffer-window-show-hook
1329 (cons 'help-mode-finish temp-buffer-window-show-hook)))
1330 (setq help-window-old-frame (selected-frame))
1331 (with-temp-buffer-window
1332 ,buffer-name nil 'help-window-setup (progn ,@body)))))
1334 ;; Called from C, on encountering `help-char' when reading a char.
1335 ;; Don't print to *Help*; that would clobber Help history.
1336 (defun help-form-show ()
1337 "Display the output of a non-nil `help-form'."
1338 (let ((msg (eval help-form)))
1339 (if (stringp msg)
1340 (with-output-to-temp-buffer " *Char Help*"
1341 (princ msg)))))
1344 ;; The following functions used to be in help-fns.el, which is not preloaded.
1345 ;; But for various reasons, they are more widely needed, so they were
1346 ;; moved to this file, which is preloaded. http://debbugs.gnu.org/17001
1348 (defun help-split-fundoc (docstring def)
1349 "Split a function DOCSTRING into the actual doc and the usage info.
1350 Return (USAGE . DOC) or nil if there's no usage info, where USAGE info
1351 is a string describing the argument list of DEF, such as
1352 \"(apply FUNCTION &rest ARGUMENTS)\".
1353 DEF is the function whose usage we're looking for in DOCSTRING."
1354 ;; Functions can get the calling sequence at the end of the doc string.
1355 ;; In cases where `function' has been fset to a subr we can't search for
1356 ;; function's name in the doc string so we use `fn' as the anonymous
1357 ;; function name instead.
1358 (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring))
1359 (cons (format "(%s%s"
1360 ;; Replace `fn' with the actual function name.
1361 (if (symbolp def) def "anonymous")
1362 (match-string 1 docstring))
1363 (unless (zerop (match-beginning 0))
1364 (substring docstring 0 (match-beginning 0))))))
1366 (defun help-add-fundoc-usage (docstring arglist)
1367 "Add the usage info to DOCSTRING.
1368 If DOCSTRING already has a usage info, then just return it unchanged.
1369 The usage info is built from ARGLIST. DOCSTRING can be nil.
1370 ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
1371 (unless (stringp docstring) (setq docstring ""))
1372 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring)
1373 (eq arglist t))
1374 docstring
1375 (concat docstring
1376 (if (string-match "\n?\n\\'" docstring)
1377 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
1378 "\n\n")
1379 (if (and (stringp arglist)
1380 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist))
1381 (concat "(fn" (match-string 1 arglist) ")")
1382 (format "%S" (help-make-usage 'fn arglist))))))
1384 (defun help-function-arglist (def &optional preserve-names)
1385 "Return a formal argument list for the function DEF.
1386 IF PRESERVE-NAMES is non-nil, return a formal arglist that uses
1387 the same names as used in the original source code, when possible."
1388 ;; Handle symbols aliased to other symbols.
1389 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
1390 ;; If definition is a macro, find the function inside it.
1391 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
1392 (cond
1393 ((and (byte-code-function-p def) (listp (aref def 0))) (aref def 0))
1394 ((eq (car-safe def) 'lambda) (nth 1 def))
1395 ((eq (car-safe def) 'closure) (nth 2 def))
1396 ((or (and (byte-code-function-p def) (integerp (aref def 0)))
1397 (subrp def))
1398 (or (when preserve-names
1399 (let* ((doc (condition-case nil (documentation def) (error nil)))
1400 (docargs (if doc (car (help-split-fundoc doc nil))))
1401 (arglist (if docargs
1402 (cdar (read-from-string (downcase docargs)))))
1403 (valid t))
1404 ;; Check validity.
1405 (dolist (arg arglist)
1406 (unless (and (symbolp arg)
1407 (let ((name (symbol-name arg)))
1408 (if (eq (aref name 0) ?&)
1409 (memq arg '(&rest &optional))
1410 (not (string-match "\\." name)))))
1411 (setq valid nil)))
1412 (when valid arglist)))
1413 (let* ((args-desc (if (not (subrp def))
1414 (aref def 0)
1415 (let ((a (subr-arity def)))
1416 (logior (car a)
1417 (if (numberp (cdr a))
1418 (lsh (cdr a) 8)
1419 (lsh 1 7))))))
1420 (max (lsh args-desc -8))
1421 (min (logand args-desc 127))
1422 (rest (logand args-desc 128))
1423 (arglist ()))
1424 (dotimes (i min)
1425 (push (intern (concat "arg" (number-to-string (1+ i)))) arglist))
1426 (when (> max min)
1427 (push '&optional arglist)
1428 (dotimes (i (- max min))
1429 (push (intern (concat "arg" (number-to-string (+ 1 i min))))
1430 arglist)))
1431 (unless (zerop rest) (push '&rest arglist) (push 'rest arglist))
1432 (nreverse arglist))))
1433 ((and (autoloadp def) (not (eq (nth 4 def) 'keymap)))
1434 "[Arg list not available until function definition is loaded.]")
1435 (t t)))
1437 (defun help-make-usage (function arglist)
1438 (cons (if (symbolp function) function 'anonymous)
1439 (mapcar (lambda (arg)
1440 (if (not (symbolp arg)) arg
1441 (let ((name (symbol-name arg)))
1442 (cond
1443 ((string-match "\\`&" name) arg)
1444 ((string-match "\\`_" name)
1445 (intern (upcase (substring name 1))))
1446 (t (intern (upcase name)))))))
1447 arglist)))
1450 (provide 'help)
1452 ;;; help.el ends here