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