Print test timings unconditionally
[emacs.git] / lisp / help.el
blob0830dc5d3cf7384314c86835b2a79abe11c73e20
1 ;;; help.el --- help commands for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2018 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 <https://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 "https://www.gnu.org/gnu/thegnuproject.html"))
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) #'string>)))
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 (defun view-echo-area-messages ()
415 "View the log of recent echo-area messages: the `*Messages*' buffer.
416 The number of messages retained in that buffer
417 is specified by the variable `message-log-max'."
418 (interactive)
419 (with-current-buffer (messages-buffer)
420 (goto-char (point-max))
421 (display-buffer (current-buffer))))
423 (defun view-order-manuals ()
424 "Display information on how to buy printed copies of Emacs manuals."
425 (interactive)
426 ;; (view-help-file "ORDERS")
427 (info "(emacs)Printed Books"))
429 (defun view-emacs-FAQ ()
430 "Display the Emacs Frequently Asked Questions (FAQ) file."
431 (interactive)
432 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
433 (info "(efaq)"))
435 (defun view-emacs-problems ()
436 "Display info on known problems with Emacs and possible workarounds."
437 (interactive)
438 (view-help-file "PROBLEMS"))
440 (defun view-emacs-debugging ()
441 "Display info on how to debug Emacs problems."
442 (interactive)
443 (view-help-file "DEBUG"))
445 ;; This used to visit MORE.STUFF; maybe it should just be removed.
446 (defun view-external-packages ()
447 "Display info on where to get more Emacs packages."
448 (interactive)
449 (info "(efaq)Packages that do not come with Emacs"))
451 (defun view-lossage ()
452 "Display last few input keystrokes and the commands run.
453 For convenience this uses the same format as
454 `edit-last-kbd-macro'.
456 To record all your input, use `open-dribble-file'."
457 (interactive)
458 (help-setup-xref (list #'view-lossage)
459 (called-interactively-p 'interactive))
460 (with-help-window (help-buffer)
461 (princ " ")
462 (princ (mapconcat (lambda (key)
463 (cond
464 ((and (consp key) (null (car key)))
465 (format ";; %s\n" (if (symbolp (cdr key)) (cdr key)
466 "anonymous-command")))
467 ((or (integerp key) (symbolp key) (listp key))
468 (single-key-description key))
470 (prin1-to-string key nil))))
471 (recent-keys 'include-cmds)
472 " "))
473 (with-current-buffer standard-output
474 (goto-char (point-min))
475 (let ((comment-start ";; ")
476 (comment-column 24))
477 (while (not (eobp))
478 (comment-indent)
479 (forward-line 1)))
480 ;; jidanni wants to see the last keystrokes immediately.
481 (set-marker help-window-point-marker (point)))))
484 ;; Key bindings
486 (defun describe-bindings (&optional prefix buffer)
487 "Display a buffer showing a list of all defined keys, and their definitions.
488 The keys are displayed in order of precedence.
490 The optional argument PREFIX, if non-nil, should be a key sequence;
491 then we display only bindings that start with that prefix.
492 The optional argument BUFFER specifies which buffer's bindings
493 to display (default, the current buffer). BUFFER can be a buffer
494 or a buffer name."
495 (interactive)
496 (or buffer (setq buffer (current-buffer)))
497 (help-setup-xref (list #'describe-bindings prefix buffer)
498 (called-interactively-p 'interactive))
499 (with-help-window (help-buffer)
500 ;; Be aware that `describe-buffer-bindings' puts its output into
501 ;; the current buffer.
502 (with-current-buffer (help-buffer)
503 (describe-buffer-bindings buffer prefix))))
505 ;; This function used to be in keymap.c.
506 (defun describe-bindings-internal (&optional menus prefix)
507 "Show a list of all defined keys, and their definitions.
508 We put that list in a buffer, and display the buffer.
510 The optional argument MENUS, if non-nil, says to mention menu bindings.
511 \(Ordinarily these are omitted from the output.)
512 The optional argument PREFIX, if non-nil, should be a key sequence;
513 then we display only bindings that start with that prefix."
514 (declare (obsolete describe-buffer-bindings "24.4"))
515 (let ((buf (current-buffer)))
516 (with-help-window (help-buffer)
517 ;; Be aware that `describe-buffer-bindings' puts its output into
518 ;; the current buffer.
519 (with-current-buffer (help-buffer)
520 (describe-buffer-bindings buf prefix menus)))))
522 (defun where-is (definition &optional insert)
523 "Print message listing key sequences that invoke the command DEFINITION.
524 Argument is a command definition, usually a symbol with a function definition.
525 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
526 (interactive
527 (let ((fn (function-called-at-point))
528 (enable-recursive-minibuffers t)
529 val)
530 (setq val (completing-read
531 (if fn
532 (format "Where is command (default %s): " fn)
533 "Where is command: ")
534 obarray 'commandp t nil nil
535 (and fn (symbol-name fn))))
536 (list (unless (equal val "") (intern val))
537 current-prefix-arg)))
538 (unless definition (error "No command"))
539 (let ((func (indirect-function definition))
540 (defs nil)
541 (standard-output (if insert (current-buffer) standard-output)))
542 ;; In DEFS, find all symbols that are aliases for DEFINITION.
543 (mapatoms (lambda (symbol)
544 (and (fboundp symbol)
545 (not (eq symbol definition))
546 (eq func (condition-case ()
547 (indirect-function symbol)
548 (error symbol)))
549 (push symbol defs))))
550 ;; Look at all the symbols--first DEFINITION,
551 ;; then its aliases.
552 (dolist (symbol (cons definition defs))
553 (let* ((remapped (command-remapping symbol))
554 (keys (where-is-internal
555 symbol overriding-local-map nil nil remapped))
556 (keys (mapconcat 'key-description keys ", "))
557 string)
558 (setq string
559 (if insert
560 (if (> (length keys) 0)
561 (if remapped
562 (format "%s (%s) (remapped from %s)"
563 keys remapped symbol)
564 (format "%s (%s)" keys symbol))
565 (format "M-x %s RET" symbol))
566 (if (> (length keys) 0)
567 (if remapped
568 (format "%s is remapped to %s which is on %s"
569 symbol remapped keys)
570 (format "%s is on %s" symbol keys))
571 ;; If this is the command the user asked about,
572 ;; and it is not on any key, say so.
573 ;; For other symbols, its aliases, say nothing
574 ;; about them unless they are on keys.
575 (if (eq symbol definition)
576 (format "%s is not on any key" symbol)))))
577 (when string
578 (unless (eq symbol definition)
579 (princ ";\n its alias "))
580 (princ string)))))
581 nil)
583 (defun help-key-description (key untranslated)
584 (let ((string (key-description key)))
585 (if (or (not untranslated)
586 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
587 string
588 (let ((otherstring (key-description untranslated)))
589 (if (equal string otherstring)
590 string
591 (format "%s (translated from %s)" string otherstring))))))
593 (defun help--binding-undefined-p (defn)
594 (or (null defn) (integerp defn) (equal defn 'undefined)))
596 (defun help--analyze-key (key untranslated)
597 "Get information about KEY its corresponding UNTRANSLATED events.
598 Returns a list of the form (BRIEF-DESC DEFN EVENT MOUSE-MSG)."
599 (if (numberp untranslated)
600 (error "Missing `untranslated'!"))
601 (let* ((event (when (> (length key) 0)
602 (aref key (if (and (symbolp (aref key 0))
603 (> (length key) 1)
604 (consp (aref key 1)))
605 ;; Look at the second event when the first
606 ;; is a pseudo-event like `mode-line' or
607 ;; `left-fringe'.
609 0))))
610 (modifiers (event-modifiers event))
611 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
612 (memq 'drag modifiers))
613 " at that spot" ""))
614 (defn (key-binding key t)))
615 ;; Handle the case where we faked an entry in "Select and Paste" menu.
616 (when (and (eq defn nil)
617 (stringp (aref key (1- (length key))))
618 (eq (key-binding (substring key 0 -1)) 'yank-menu))
619 (setq defn 'menu-bar-select-yank))
620 ;; Don't bother user with strings from (e.g.) the select-paste menu.
621 (when (stringp (aref key (1- (length key))))
622 (aset key (1- (length key)) "(any string)"))
623 (when (and untranslated
624 (stringp (aref untranslated (1- (length untranslated)))))
625 (aset untranslated (1- (length untranslated)) "(any string)"))
626 (list
627 ;; Now describe the key, perhaps as changed.
628 (let ((key-desc (help-key-description key untranslated)))
629 (if (help--binding-undefined-p defn)
630 (format "%s%s is undefined" key-desc mouse-msg)
631 (format "%s%s runs the command %S" key-desc mouse-msg defn)))
632 defn event mouse-msg)))
634 (defun help--filter-info-list (info-list i)
635 "Drop the undefined keys."
637 ;; Remove all `undefined' keys.
638 (delq nil (mapcar (lambda (x)
639 (unless (help--binding-undefined-p (nth i x)) x))
640 info-list))
641 ;; If nothing left, then keep one (the last one).
642 (last info-list)))
644 (defun describe-key-briefly (&optional key-list insert untranslated)
645 "Print the name of the functions KEY-LIST invokes.
646 KEY-LIST is a list of pairs (SEQ . RAW-SEQ) of key sequences, where
647 RAW-SEQ is the untranslated form of the key sequence SEQ.
648 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
650 While reading KEY-LIST interactively, this command temporarily enables
651 menu items or tool-bar buttons that are disabled to allow getting help
652 on them."
653 (declare (advertised-calling-convention (key-list &optional insert) "27.1"))
654 (interactive
655 ;; Ignore mouse movement events because it's too easy to miss the
656 ;; message while moving the mouse.
657 (let ((key-list (help--read-key-sequence 'no-mouse-movement)))
658 `(,key-list ,current-prefix-arg)))
659 (when (arrayp key-list)
660 ;; Old calling convention, changed
661 (setq key-list (list (cons key-list
662 (if (numberp untranslated)
663 (this-single-command-raw-keys)
664 untranslated)))))
665 (let* ((info-list (mapcar (lambda (kr)
666 (help--analyze-key (car kr) (cdr kr)))
667 key-list))
668 (msg (mapconcat #'car (help--filter-info-list info-list 1) "\n")))
669 (if insert (insert msg) (message "%s" msg))))
671 (defun help--key-binding-keymap (key &optional accept-default no-remap position)
672 "Return a keymap holding a binding for KEY within current keymaps.
673 The effect of the arguments KEY, ACCEPT-DEFAULT, NO-REMAP and
674 POSITION is as documented in the function `key-binding'."
675 (let* ((active-maps (current-active-maps t position))
676 map found)
677 ;; We loop over active maps like key-binding does.
678 (while (and
679 (not found)
680 (setq map (pop active-maps)))
681 (setq found (lookup-key map key accept-default))
682 (when (integerp found)
683 ;; The first `found' characters of KEY were found but not the
684 ;; whole sequence.
685 (setq found nil)))
686 (when found
687 (if (and (symbolp found)
688 (not no-remap)
689 (command-remapping found))
690 ;; The user might want to know in which map the binding is
691 ;; found, or in which map the remapping is found. The
692 ;; default is to show the latter.
693 (help--key-binding-keymap (vector 'remap found))
694 map))))
696 (defun help--binding-locus (key position)
697 "Describe in which keymap KEY is defined.
698 Return a symbol pointing to that keymap if one exists ; otherwise
699 return nil. The argument POSITION is as documented in the
700 function `key-binding'."
701 (let ((map (help--key-binding-keymap key t nil position)))
702 (when map
703 (catch 'found
704 (let ((advertised-syms (nconc
705 (list 'overriding-terminal-local-map
706 'overriding-local-map)
707 (delq nil
708 (mapcar
709 (lambda (mode-and-map)
710 (let ((mode (car mode-and-map)))
711 (when (symbol-value mode)
712 (intern-soft
713 (format "%s-map" mode)))))
714 minor-mode-map-alist))
715 (list 'global-map
716 (intern-soft (format "%s-map" major-mode))))))
717 ;; Look into these advertised symbols first.
718 (dolist (sym advertised-syms)
719 (when (and
720 (boundp sym)
721 (eq map (symbol-value sym)))
722 (throw 'found sym)))
723 ;; Only look in other symbols otherwise.
724 (mapatoms
725 (lambda (x)
726 (when (and (boundp x)
727 ;; Avoid let-bound symbols.
728 (special-variable-p x)
729 (eq (symbol-value x) map))
730 (throw 'found x))))
731 nil)))))
733 (defun help--read-key-sequence (&optional no-mouse-movement)
734 "Read a key sequence from the user.
735 Usually reads a single key sequence, except when that sequence might
736 hide another one (e.g. a down event, where the user is interested
737 in getting info about the up event, or a click event, where the user
738 wants to get info about the double click).
739 Return a list of elements of the form (SEQ . RAW-SEQ), where SEQ is a key
740 sequence, and RAW-SEQ is its untranslated form.
741 If NO-MOUSE-MOVEMENT is non-nil, ignore key sequences starting
742 with `mouse-movement' events."
743 (let ((enable-disabled-menus-and-buttons t)
744 (cursor-in-echo-area t)
745 saved-yank-menu)
746 (unwind-protect
747 (let (last-modifiers key-list)
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 (while
754 ;; Read at least one key-sequence.
755 (or (null key-list)
756 ;; After a down event, also read the (presumably) following
757 ;; up-event.
758 (memq 'down last-modifiers)
759 ;; After a click, see if a double click is on the way.
760 (and (memq 'click last-modifiers)
761 (not (sit-for (/ double-click-time 1000.0) t))))
762 (let* ((seq (read-key-sequence "\
763 Describe the following key, mouse click, or menu item: "))
764 (raw-seq (this-single-command-raw-keys))
765 (keyn (when (> (length seq) 0)
766 (aref seq (1- (length seq)))))
767 (base (event-basic-type keyn))
768 (modifiers (event-modifiers keyn)))
769 (cond
770 ((zerop (length seq))) ;FIXME: Can this happen?
771 ((and no-mouse-movement (eq base 'mouse-movement)) nil)
772 ((eq base 'help-echo) nil)
774 (setq last-modifiers modifiers)
775 (push (cons seq raw-seq) key-list)))))
776 (nreverse key-list))
777 ;; Put yank-menu back as it was, if we changed it.
778 (when saved-yank-menu
779 (setq yank-menu (copy-sequence saved-yank-menu))
780 (fset 'yank-menu (cons 'keymap yank-menu))))))
782 (defun describe-key (&optional key-list buffer up-event)
783 "Display documentation of the function invoked by KEY-LIST.
784 KEY-LIST can be any kind of a key sequence; it can include keyboard events,
785 mouse events, and/or menu events. When calling from a program,
786 pass KEY-LIST as a list of elements (SEQ . RAW-SEQ) where SEQ is
787 a key-sequence and RAW-SEQ is its untranslated form.
789 While reading KEY-LIST interactively, this command temporarily enables
790 menu items or tool-bar buttons that are disabled to allow getting help
791 on them.
793 BUFFER is the buffer in which to lookup those keys; it defaults to the
794 current buffer."
795 (declare (advertised-calling-convention (key-list &optional buffer) "27.1"))
796 (interactive (list (help--read-key-sequence)))
797 (when (arrayp key-list)
798 ;; Compatibility with old calling convention.
799 (setq key-list (cons (list key-list) (if up-event (list up-event))))
800 (when buffer
801 (let ((raw (if (numberp buffer) (this-single-command-raw-keys) buffer)))
802 (setf (cdar (last key-list)) raw)))
803 (setq buffer nil))
804 (let* ((buf (or buffer (current-buffer)))
805 (on-link
806 (mapcar (lambda (kr)
807 (let ((raw (cdr kr)))
808 (and (not (memq mouse-1-click-follows-link '(nil double)))
809 (> (length raw) 0)
810 (eq (car-safe (aref raw 0)) 'mouse-1)
811 (with-current-buffer buf
812 (mouse-on-link-p (event-start (aref raw 0)))))))
813 key-list))
814 (info-list
815 (help--filter-info-list
816 (with-current-buffer buf
817 (mapcar (lambda (x)
818 (pcase-let* ((`(,seq . ,raw-seq) x)
819 (`(,brief-desc ,defn ,event ,_mouse-msg)
820 (help--analyze-key seq raw-seq))
821 (locus
822 (help--binding-locus
823 seq (event-start event))))
824 `(,seq ,brief-desc ,defn ,locus)))
825 key-list))
826 2)))
827 (help-setup-xref (list (lambda (key-list buf)
828 (describe-key key-list
829 (if (buffer-live-p buf) buf)))
830 key-list buf)
831 (called-interactively-p 'interactive))
832 (if (and (<= (length info-list) 1)
833 (help--binding-undefined-p (nth 2 (car info-list))))
834 (message "%s" (nth 1 (car info-list)))
835 (with-help-window (help-buffer)
836 (when (> (length info-list) 1)
837 ;; FIXME: Make this into clickable hyperlinks.
838 (princ "There were several key-sequences:\n\n")
839 (princ (mapconcat (lambda (info)
840 (pcase-let ((`(,_seq ,brief-desc ,_defn ,_locus)
841 info))
842 (concat " " brief-desc)))
843 info-list
844 "\n"))
845 (when (delq nil on-link)
846 (princ "\n\nThose are influenced by `mouse-1-click-follows-link'"))
847 (princ "\n\nThey're all described below."))
848 (pcase-dolist (`(,_seq ,brief-desc ,defn ,locus)
849 info-list)
850 (when defn
851 (when (> (length info-list) 1)
852 (with-current-buffer standard-output
853 (insert "\n\n"
854 ;; FIXME: Can't use eval-when-compile because purified
855 ;; strings lose their text properties :-(
856 (propertize "\n" 'face '(:height 0.1 :inverse-video t))
857 "\n")))
859 (princ brief-desc)
860 (when locus
861 (princ (format " (found in %s)" locus)))
862 (princ ", which is ")
863 (describe-function-1 defn)))))))
865 (defun describe-mode (&optional buffer)
866 "Display documentation of current major mode and minor modes.
867 A brief summary of the minor modes comes first, followed by the
868 major mode description. This is followed by detailed
869 descriptions of the minor modes, each on a separate page.
871 For this to work correctly for a minor mode, the mode's indicator
872 variable \(listed in `minor-mode-alist') must also be a function
873 whose documentation describes the minor mode.
875 If called from Lisp with a non-nil BUFFER argument, display
876 documentation for the major and minor modes of that buffer."
877 (interactive "@")
878 (unless buffer (setq buffer (current-buffer)))
879 (help-setup-xref (list #'describe-mode buffer)
880 (called-interactively-p 'interactive))
881 ;; For the sake of help-do-xref and help-xref-go-back,
882 ;; don't switch buffers before calling `help-buffer'.
883 (with-help-window (help-buffer)
884 (with-current-buffer buffer
885 (let (minor-modes)
886 ;; Older packages do not register in minor-mode-list but only in
887 ;; minor-mode-alist.
888 (dolist (x minor-mode-alist)
889 (setq x (car x))
890 (unless (memq x minor-mode-list)
891 (push x minor-mode-list)))
892 ;; Find enabled minor mode we will want to mention.
893 (dolist (mode minor-mode-list)
894 ;; Document a minor mode if it is listed in minor-mode-alist,
895 ;; non-nil, and has a function definition.
896 (let ((fmode (or (get mode :minor-mode-function) mode)))
897 (and (boundp mode) (symbol-value mode)
898 (fboundp fmode)
899 (let ((pretty-minor-mode
900 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
901 (symbol-name fmode))
902 (capitalize
903 (substring (symbol-name fmode)
904 0 (match-beginning 0)))
905 fmode)))
906 (push (list fmode pretty-minor-mode
907 (format-mode-line (assq mode minor-mode-alist)))
908 minor-modes)))))
909 (setq minor-modes
910 (sort minor-modes
911 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
912 (when minor-modes
913 (princ "Enabled minor modes:\n")
914 (make-local-variable 'help-button-cache)
915 (with-current-buffer standard-output
916 (dolist (mode minor-modes)
917 (let ((mode-function (nth 0 mode))
918 (pretty-minor-mode (nth 1 mode))
919 (indicator (nth 2 mode)))
920 (save-excursion
921 (goto-char (point-max))
922 (princ "\n\f\n")
923 (push (point-marker) help-button-cache)
924 ;; Document the minor modes fully.
925 (insert-text-button
926 pretty-minor-mode 'type 'help-function
927 'help-args (list mode-function)
928 'button '(t))
929 (princ (format " minor mode (%s):\n"
930 (if (zerop (length indicator))
931 "no indicator"
932 (format "indicator%s"
933 indicator))))
934 (princ (documentation mode-function)))
935 (insert-button pretty-minor-mode
936 'action (car help-button-cache)
937 'follow-link t
938 'help-echo "mouse-2, RET: show full information")
939 (newline)))
940 (forward-line -1)
941 (fill-paragraph nil)
942 (forward-line 1))
944 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
945 ;; Document the major mode.
946 (let ((mode mode-name))
947 (with-current-buffer standard-output
948 (let ((start (point)))
949 (insert (format-mode-line mode nil nil buffer))
950 (add-text-properties start (point) '(face bold)))))
951 (princ " mode")
952 (let* ((mode major-mode)
953 (file-name (find-lisp-object-file-name mode nil)))
954 (when file-name
955 (princ (format-message " defined in `%s'"
956 (file-name-nondirectory file-name)))
957 ;; Make a hyperlink to the library.
958 (with-current-buffer standard-output
959 (save-excursion
960 (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
961 nil t)
962 (help-xref-button 1 'help-function-def mode file-name)))))
963 (princ ":\n")
964 (princ (documentation major-mode)))))
965 ;; For the sake of IELM and maybe others
966 nil)
969 (defun describe-minor-mode (minor-mode)
970 "Display documentation of a minor mode given as MINOR-MODE.
971 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
972 appeared on the mode-line."
973 (interactive (list (completing-read
974 "Minor mode: "
975 (nconc
976 (describe-minor-mode-completion-table-for-symbol)
977 (describe-minor-mode-completion-table-for-indicator)
978 ))))
979 (if (symbolp minor-mode)
980 (setq minor-mode (symbol-name minor-mode)))
981 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
982 (indicators (describe-minor-mode-completion-table-for-indicator)))
983 (cond
984 ((member minor-mode symbols)
985 (describe-minor-mode-from-symbol (intern minor-mode)))
986 ((member minor-mode indicators)
987 (describe-minor-mode-from-indicator minor-mode))
989 (error "No such minor mode: %s" minor-mode)))))
991 ;; symbol
992 (defun describe-minor-mode-completion-table-for-symbol ()
993 ;; In order to list up all minor modes, minor-mode-list
994 ;; is used here instead of minor-mode-alist.
995 (delq nil (mapcar 'symbol-name minor-mode-list)))
997 (defun describe-minor-mode-from-symbol (symbol)
998 "Display documentation of a minor mode given as a symbol, SYMBOL"
999 (interactive (list (intern (completing-read
1000 "Minor mode symbol: "
1001 (describe-minor-mode-completion-table-for-symbol)))))
1002 (if (fboundp symbol)
1003 (describe-function symbol)
1004 (describe-variable symbol)))
1006 ;; indicator
1007 (defun describe-minor-mode-completion-table-for-indicator ()
1008 (delq nil
1009 (mapcar (lambda (x)
1010 (let ((i (format-mode-line x)))
1011 ;; remove first space if existed
1012 (cond
1013 ((= 0 (length i))
1014 nil)
1015 ((eq (aref i 0) ?\s)
1016 (substring i 1))
1018 i))))
1019 minor-mode-alist)))
1021 (defun describe-minor-mode-from-indicator (indicator)
1022 "Display documentation of a minor mode specified by INDICATOR.
1023 If you call this function interactively, you can give indicator which
1024 is currently activated with completion."
1025 (interactive (list
1026 (completing-read
1027 "Minor mode indicator: "
1028 (describe-minor-mode-completion-table-for-indicator))))
1029 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
1030 (if minor-mode
1031 (describe-minor-mode-from-symbol minor-mode)
1032 (error "Cannot find minor mode for `%s'" indicator))))
1034 (defun lookup-minor-mode-from-indicator (indicator)
1035 "Return a minor mode symbol from its indicator on the mode line."
1036 ;; remove first space if existed
1037 (if (and (< 0 (length indicator))
1038 (eq (aref indicator 0) ?\s))
1039 (setq indicator (substring indicator 1)))
1040 (let ((minor-modes minor-mode-alist)
1041 result)
1042 (while minor-modes
1043 (let* ((minor-mode (car (car minor-modes)))
1044 (anindicator (format-mode-line
1045 (car (cdr (car minor-modes))))))
1046 ;; remove first space if existed
1047 (if (and (stringp anindicator)
1048 (> (length anindicator) 0)
1049 (eq (aref anindicator 0) ?\s))
1050 (setq anindicator (substring anindicator 1)))
1051 (if (equal indicator anindicator)
1052 (setq result minor-mode
1053 minor-modes nil)
1054 (setq minor-modes (cdr minor-modes)))))
1055 result))
1057 (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
1058 (declare-function x-display-pixel-width "xfns.c" (&optional terminal))
1060 ;;; Automatic resizing of temporary buffers.
1061 (defcustom temp-buffer-max-height
1062 (lambda (_buffer)
1063 (if (and (display-graphic-p) (eq (selected-window) (frame-root-window)))
1064 (/ (x-display-pixel-height) (frame-char-height) 2)
1065 (/ (- (frame-height) 2) 2)))
1066 "Maximum height of a window displaying a temporary buffer.
1067 This is effective only when Temp Buffer Resize mode is enabled.
1068 The value is the maximum height (in lines) which
1069 `resize-temp-buffer-window' will give to a window displaying a
1070 temporary buffer. It can also be a function to be called to
1071 choose the height for such a buffer. It gets one argument, the
1072 buffer, and should return a positive integer. At the time the
1073 function is called, the window to be resized is selected."
1074 :type '(choice integer function)
1075 :group 'help
1076 :version "24.3")
1078 (defcustom temp-buffer-max-width
1079 (lambda (_buffer)
1080 (if (and (display-graphic-p) (eq (selected-window) (frame-root-window)))
1081 (/ (x-display-pixel-width) (frame-char-width) 2)
1082 (/ (- (frame-width) 2) 2)))
1083 "Maximum width of a window displaying a temporary buffer.
1084 This is effective only when Temp Buffer Resize mode is enabled.
1085 The value is the maximum width (in columns) which
1086 `resize-temp-buffer-window' will give to a window displaying a
1087 temporary buffer. It can also be a function to be called to
1088 choose the width for such a buffer. It gets one argument, the
1089 buffer, and should return a positive integer. At the time the
1090 function is called, the window to be resized is selected."
1091 :type '(choice integer function)
1092 :group 'help
1093 :version "24.4")
1095 (define-minor-mode temp-buffer-resize-mode
1096 "Toggle auto-resizing temporary buffer windows (Temp Buffer Resize Mode).
1097 With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
1098 is positive, and disable it otherwise. If called from Lisp,
1099 enable the mode if ARG is omitted or nil.
1101 When Temp Buffer Resize mode is enabled, the windows in which we
1102 show a temporary buffer are automatically resized in height to
1103 fit the buffer's contents, but never more than
1104 `temp-buffer-max-height' nor less than `window-min-height'.
1106 A window is resized only if it has been specially created for the
1107 buffer. Windows that have shown another buffer before are not
1108 resized. A frame is resized only if `fit-frame-to-buffer' is
1109 non-nil.
1111 This mode is used by `help', `apropos' and `completion' buffers,
1112 and some others."
1113 :global t :group 'help
1114 (if temp-buffer-resize-mode
1115 ;; `help-make-xrefs' may add a `back' button and thus increase the
1116 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1117 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1118 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
1120 (defun resize-temp-buffer-window (&optional window)
1121 "Resize WINDOW to fit its contents.
1122 WINDOW must be a live window and defaults to the selected one.
1123 Do not resize if WINDOW was not created by `display-buffer'.
1125 If WINDOW is part of a vertical combination, restrain its new
1126 size by `temp-buffer-max-height' and do not resize if its minimum
1127 accessible position is scrolled out of view. If WINDOW is part
1128 of a horizontal combination, restrain its new size by
1129 `temp-buffer-max-width'. In both cases, the value of the option
1130 `fit-window-to-buffer-horizontally' can inhibit resizing.
1132 If WINDOW is the root window of its frame, resize the frame
1133 provided `fit-frame-to-buffer' is non-nil.
1135 This function may call `preserve-window-size' to preserve the
1136 size of WINDOW."
1137 (setq window (window-normalize-window window t))
1138 (let ((height (if (functionp temp-buffer-max-height)
1139 (with-selected-window window
1140 (funcall temp-buffer-max-height (window-buffer)))
1141 temp-buffer-max-height))
1142 (width (if (functionp temp-buffer-max-width)
1143 (with-selected-window window
1144 (funcall temp-buffer-max-width (window-buffer)))
1145 temp-buffer-max-width))
1146 (quit-cadr (cadr (window-parameter window 'quit-restore))))
1147 ;; Resize WINDOW iff it was made by `display-buffer'.
1148 (when (or (and (eq quit-cadr 'window)
1149 (or (and (window-combined-p window)
1150 (not (eq fit-window-to-buffer-horizontally
1151 'only))
1152 (pos-visible-in-window-p (point-min) window))
1153 (and (window-combined-p window t)
1154 fit-window-to-buffer-horizontally)))
1155 (and (eq quit-cadr 'frame)
1156 fit-frame-to-buffer
1157 (eq window (frame-root-window window))))
1158 (fit-window-to-buffer window height nil width nil t))))
1160 ;;; Help windows.
1161 (defcustom help-window-select nil
1162 "Non-nil means select help window for viewing.
1163 Choices are:
1165 never (nil) Select help window only if there is no other window
1166 on its frame.
1168 other Select help window if and only if it appears on the
1169 previously selected frame, that frame contains at
1170 least two other windows and the help window is
1171 either new or showed a different buffer before.
1173 always (t) Always select the help window.
1175 If this option is non-nil and the help window appears on another
1176 frame, then give that frame input focus too. Note also that if
1177 the help window appears on another frame, it may get selected and
1178 its frame get input focus even if this option is nil.
1180 This option has effect if and only if the help window was created
1181 by `with-help-window'."
1182 :type '(choice (const :tag "never (nil)" nil)
1183 (const :tag "other" other)
1184 (const :tag "always (t)" t))
1185 :group 'help
1186 :version "23.1")
1188 (defcustom help-enable-auto-load t
1189 "Whether Help commands can perform autoloading.
1190 If non-nil, whenever \\[describe-function] is called for an
1191 autoloaded function whose docstring contains any key substitution
1192 construct (see `substitute-command-keys'), the library is loaded,
1193 so that the documentation can show the right key bindings."
1194 :type 'boolean
1195 :group 'help
1196 :version "24.3")
1198 (defun help-window-display-message (quit-part window &optional scroll)
1199 "Display message telling how to quit and scroll help window.
1200 QUIT-PART is a string telling how to quit the help window WINDOW.
1201 Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1202 SCROLL equal `other' means tell how to scroll the \"other\"
1203 window."
1204 (let ((scroll-part
1205 (cond
1206 ;; If we don't have QUIT-PART we probably reuse a window
1207 ;; showing the same buffer so we don't show any message.
1208 ((not quit-part) nil)
1209 ((pos-visible-in-window-p
1210 (with-current-buffer (window-buffer window)
1211 (point-max)) window t)
1212 ;; Buffer end is at least partially visible, no need to talk
1213 ;; about scrolling.
1214 ".")
1215 ((eq scroll 'other)
1216 ", \\[scroll-other-window] to scroll help.")
1217 (scroll ", \\[scroll-up] to scroll help."))))
1218 (message "%s"
1219 (substitute-command-keys (concat quit-part scroll-part)))))
1221 (defun help-window-setup (window &optional value)
1222 "Set up help window WINDOW for `with-help-window'.
1223 WINDOW is the window used for displaying the help buffer.
1224 Return VALUE."
1225 (let* ((help-buffer (when (window-live-p window)
1226 (window-buffer window)))
1227 (help-setup (when (window-live-p window)
1228 (car (window-parameter window 'quit-restore))))
1229 (frame (window-frame window)))
1231 (when help-buffer
1232 ;; Handle `help-window-point-marker'.
1233 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1234 (set-window-point window help-window-point-marker)
1235 ;; Reset `help-window-point-marker'.
1236 (set-marker help-window-point-marker nil))
1238 ;; If the help window appears on another frame, select it if
1239 ;; `help-window-select' is non-nil and give that frame input focus
1240 ;; too. See also Bug#19012.
1241 (when (and help-window-select
1242 (frame-live-p help-window-old-frame)
1243 (not (eq frame help-window-old-frame)))
1244 (select-window window)
1245 (select-frame-set-input-focus frame))
1247 (cond
1248 ((or (eq window (selected-window))
1249 ;; If the help window is on the selected frame, select
1250 ;; it if `help-window-select' is t or `help-window-select'
1251 ;; is 'other, the frame contains at least three windows, and
1252 ;; the help window did show another buffer before. See also
1253 ;; Bug#11039.
1254 (and (eq frame (selected-frame))
1255 (or (eq help-window-select t)
1256 (and (eq help-window-select 'other)
1257 (> (length (window-list nil 'no-mini)) 2)
1258 (not (eq help-setup 'same))))
1259 (select-window window)))
1260 ;; The help window is or gets selected ...
1261 (help-window-display-message
1262 (cond
1263 ((eq help-setup 'window)
1264 ;; ... and is new, ...
1265 "Type \"q\" to delete help window")
1266 ((eq help-setup 'frame)
1267 ;; ... on a new frame, ...
1268 "Type \"q\" to quit the help frame")
1269 ((eq help-setup 'other)
1270 ;; ... or displayed some other buffer before.
1271 "Type \"q\" to restore previous buffer"))
1272 window t))
1273 ((and (eq (window-frame window) help-window-old-frame)
1274 (= (length (window-list nil 'no-mini)) 2))
1275 ;; There are two windows on the help window's frame and the
1276 ;; other one is the selected one.
1277 (help-window-display-message
1278 (cond
1279 ((eq help-setup 'window)
1280 "Type \\[delete-other-windows] to delete the help window")
1281 ((eq help-setup 'other)
1282 "Type \"q\" in help window to restore its previous buffer"))
1283 window 'other))
1285 ;; The help window is not selected ...
1286 (help-window-display-message
1287 (cond
1288 ((eq help-setup 'window)
1289 ;; ... and is new, ...
1290 "Type \"q\" in help window to delete it")
1291 ((eq help-setup 'other)
1292 ;; ... or displayed some other buffer before.
1293 "Type \"q\" in help window to restore previous buffer"))
1294 window))))
1295 ;; Return VALUE.
1296 value))
1298 ;; `with-help-window' is a wrapper for `with-temp-buffer-window'
1299 ;; providing the following additional twists:
1301 ;; (1) It puts the buffer in `help-mode' (via `help-mode-setup') and
1302 ;; adds cross references (via `help-mode-finish').
1304 ;; (2) It issues a message telling how to scroll and quit the help
1305 ;; window (via `help-window-setup').
1307 ;; (3) An option (customizable via `help-window-select') to select the
1308 ;; help window automatically.
1310 ;; (4) A marker (`help-window-point-marker') to move point in the help
1311 ;; window to an arbitrary buffer position.
1312 (defmacro with-help-window (buffer-name &rest body)
1313 "Display buffer named BUFFER-NAME in a help window.
1314 Evaluate the forms in BODY with standard output bound to a buffer
1315 called BUFFER-NAME (creating it if it does not exist), put that
1316 buffer in `help-mode', display the buffer in a window (see
1317 `with-temp-buffer-window' for details) and issue a message how to
1318 deal with that \"help\" window when it's no more needed. Select
1319 the help window if the current value of the user option
1320 `help-window-select' says so. Return last value in BODY."
1321 (declare (indent 1) (debug t))
1322 `(progn
1323 ;; Make `help-window-point-marker' point nowhere. The only place
1324 ;; where this should be set to a buffer position is within BODY.
1325 (set-marker help-window-point-marker nil)
1326 (let ((temp-buffer-window-setup-hook
1327 (cons 'help-mode-setup temp-buffer-window-setup-hook))
1328 (temp-buffer-window-show-hook
1329 (cons 'help-mode-finish temp-buffer-window-show-hook)))
1330 (setq help-window-old-frame (selected-frame))
1331 (with-temp-buffer-window
1332 ,buffer-name nil 'help-window-setup (progn ,@body)))))
1334 ;; Called from C, on encountering `help-char' when reading a char.
1335 ;; Don't print to *Help*; that would clobber Help history.
1336 (defun help-form-show ()
1337 "Display the output of a non-nil `help-form'."
1338 (let ((msg (eval help-form)))
1339 (if (stringp msg)
1340 (with-output-to-temp-buffer " *Char Help*"
1341 (princ msg)))))
1344 (defun help--docstring-quote (string)
1345 "Return a doc string that represents STRING.
1346 The result, when formatted by `substitute-command-keys', should equal STRING."
1347 (replace-regexp-in-string "['\\`‘’]" "\\\\=\\&" string))
1349 ;; The following functions used to be in help-fns.el, which is not preloaded.
1350 ;; But for various reasons, they are more widely needed, so they were
1351 ;; moved to this file, which is preloaded. https://debbugs.gnu.org/17001
1353 (defun help-split-fundoc (docstring def)
1354 "Split a function DOCSTRING into the actual doc and the usage info.
1355 Return (USAGE . DOC) or nil if there's no usage info, where USAGE info
1356 is a string describing the argument list of DEF, such as
1357 \"(apply FUNCTION &rest ARGUMENTS)\".
1358 DEF is the function whose usage we're looking for in DOCSTRING."
1359 ;; Functions can get the calling sequence at the end of the doc string.
1360 ;; In cases where `function' has been fset to a subr we can't search for
1361 ;; function's name in the doc string so we use `fn' as the anonymous
1362 ;; function name instead.
1363 (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring))
1364 (let ((doc (unless (zerop (match-beginning 0))
1365 (substring docstring 0 (match-beginning 0))))
1366 (usage-tail (match-string 1 docstring)))
1367 (cons (format "(%s%s"
1368 ;; Replace `fn' with the actual function name.
1369 (if (symbolp def)
1370 (help--docstring-quote (format "%S" def))
1371 'anonymous)
1372 usage-tail)
1373 doc))))
1375 (defun help-add-fundoc-usage (docstring arglist)
1376 "Add the usage info to DOCSTRING.
1377 If DOCSTRING already has a usage info, then just return it unchanged.
1378 The usage info is built from ARGLIST. DOCSTRING can be nil.
1379 ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
1380 (unless (stringp docstring) (setq docstring ""))
1381 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring)
1382 (eq arglist t))
1383 docstring
1384 (concat docstring
1385 (if (string-match "\n?\n\\'" docstring)
1386 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
1387 "\n\n")
1388 (if (stringp arglist)
1389 (if (string-match "\\`[^ ]+\\(.*\\))\\'" arglist)
1390 (concat "(fn" (match-string 1 arglist) ")")
1391 (error "Unrecognized usage format"))
1392 (help--make-usage-docstring 'fn arglist)))))
1394 (defun help-function-arglist (def &optional preserve-names)
1395 "Return a formal argument list for the function DEF.
1396 If PRESERVE-NAMES is non-nil, return a formal arglist that uses
1397 the same names as used in the original source code, when possible."
1398 ;; Handle symbols aliased to other symbols.
1399 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
1400 ;; Advice wrappers have "catch all" args, so fetch the actual underlying
1401 ;; function to find the real arguments.
1402 (while (advice--p def) (setq def (advice--cdr def)))
1403 ;; If definition is a macro, find the function inside it.
1404 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
1405 (cond
1406 ((and (byte-code-function-p def) (listp (aref def 0))) (aref def 0))
1407 ((eq (car-safe def) 'lambda) (nth 1 def))
1408 ((eq (car-safe def) 'closure) (nth 2 def))
1409 ((or (and (byte-code-function-p def) (integerp (aref def 0)))
1410 (subrp def) (module-function-p def))
1411 (or (when preserve-names
1412 (let* ((doc (condition-case nil (documentation def) (error nil)))
1413 (docargs (if doc (car (help-split-fundoc doc nil))))
1414 (arglist (if docargs
1415 (cdar (read-from-string (downcase docargs)))))
1416 (valid t))
1417 ;; Check validity.
1418 (dolist (arg arglist)
1419 (unless (and (symbolp arg)
1420 (let ((name (symbol-name arg)))
1421 (if (eq (aref name 0) ?&)
1422 (memq arg '(&rest &optional))
1423 (not (string-match "\\." name)))))
1424 (setq valid nil)))
1425 (when valid arglist)))
1426 (let* ((arity (func-arity def))
1427 (max (cdr arity))
1428 (min (car arity))
1429 (arglist ()))
1430 (dotimes (i min)
1431 (push (intern (concat "arg" (number-to-string (1+ i)))) arglist))
1432 (when (and (integerp max) (> max min))
1433 (push '&optional arglist)
1434 (dotimes (i (- max min))
1435 (push (intern (concat "arg" (number-to-string (+ 1 i min))))
1436 arglist)))
1437 (unless (integerp max) (push '&rest arglist) (push 'rest arglist))
1438 (nreverse arglist))))
1439 ((and (autoloadp def) (not (eq (nth 4 def) 'keymap)))
1440 "[Arg list not available until function definition is loaded.]")
1441 (t t)))
1443 (defun help--make-usage (function arglist)
1444 (cons (if (symbolp function) function 'anonymous)
1445 (mapcar (lambda (arg)
1446 (if (not (symbolp arg)) arg
1447 (let ((name (symbol-name arg)))
1448 (cond
1449 ((string-match "\\`&" name) arg)
1450 ((string-match "\\`_." name)
1451 (intern (upcase (substring name 1))))
1452 (t (intern (upcase name)))))))
1453 arglist)))
1455 (define-obsolete-function-alias 'help-make-usage 'help--make-usage "25.1")
1457 (defun help--make-usage-docstring (fn arglist)
1458 (let ((print-escape-newlines t))
1459 (help--docstring-quote (format "%S" (help--make-usage fn arglist)))))
1462 (provide 'help)
1464 ;;; help.el ends here