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