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