1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2014 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
6 ;; Keywords: help, internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This code implements GNU Emacs's built-in help system, the one invoked by
27 ;; `M-x help-for-help'.
31 ;; Get the macro make-help-screen when this is compiled,
32 ;; or run interpreted, but not when the compiled code is loaded.
33 (eval-when-compile (require 'help-macro
))
35 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
36 (add-hook 'temp-buffer-setup-hook
'help-mode-setup
)
37 (add-hook 'temp-buffer-show-hook
'help-mode-finish
)
39 ;; `help-window-point-marker' is a marker you can move to a valid
40 ;; position of the buffer shown in the help window in order to override
41 ;; the standard positioning mechanism (`point-min') chosen by
42 ;; `with-output-to-temp-buffer' and `with-temp-buffer-window'.
43 ;; `with-help-window' has this point nowhere before exiting. Currently
44 ;; used by `view-lossage' to assert that the last keystrokes are always
46 (defvar help-window-point-marker
(make-marker)
47 "Marker to override default `window-point' in help windows.")
50 (let ((map (make-sparse-keymap)))
51 (define-key map
(char-to-string help-char
) 'help-for-help
)
52 (define-key map
[help] 'help-for-help)
53 (define-key map [f1] 'help-for-help)
54 (define-key map "." 'display-local-help)
55 (define-key map "?" 'help-for-help)
57 (define-key map "\C-a" 'about-emacs)
58 (define-key map "\C-c" 'describe-copying)
59 (define-key map "\C-d" 'view-emacs-debugging)
60 (define-key map "\C-e" 'view-external-packages)
61 (define-key map "\C-f" 'view-emacs-FAQ)
62 (define-key map "\C-m" 'view-order-manuals)
63 (define-key map "\C-n" 'view-emacs-news)
64 (define-key map "\C-o" 'describe-distribution)
65 (define-key map "\C-p" 'view-emacs-problems)
66 (define-key map "\C-t" 'view-emacs-todo)
67 (define-key map "\C-w" 'describe-no-warranty)
69 ;; This does not fit the pattern, but it is natural given the C-\ command.
70 (define-key map "\C-\\" 'describe-input-method)
72 (define-key map "C" 'describe-coding-system)
73 (define-key map "F" 'Info-goto-emacs-command-node)
74 (define-key map "I" 'describe-input-method)
75 (define-key map "K" 'Info-goto-emacs-key-command-node)
76 (define-key map "L" 'describe-language-environment)
77 (define-key map "S" 'info-lookup-symbol)
79 (define-key map "a" 'apropos-command)
80 (define-key map "b" 'describe-bindings)
81 (define-key map "c" 'describe-key-briefly)
82 (define-key map "d" 'apropos-documentation)
83 (define-key map "e" 'view-echo-area-messages)
84 (define-key map "f" 'describe-function)
85 (define-key map "g" 'describe-gnu-project)
86 (define-key map "h" 'view-hello-file)
88 (define-key map "i" 'info)
89 (define-key map "4i" 'info-other-window)
91 (define-key map "k" 'describe-key)
92 (define-key map "l" 'view-lossage)
93 (define-key map "m" 'describe-mode)
94 (define-key map "n" 'view-emacs-news)
95 (define-key map "p" 'finder-by-keyword)
96 (define-key map "P" 'describe-package)
97 (define-key map "r" 'info-emacs-manual)
98 (define-key map "s" 'describe-syntax)
99 (define-key map "t" 'help-with-tutorial)
100 (define-key map "w" 'where-is)
101 (define-key map "v" 'describe-variable)
102 (define-key map "q" 'help-quit)
104 "Keymap for characters following the Help key.")
106 (define-key global-map (char-to-string help-char) 'help-command)
107 (define-key global-map [help] 'help-command
)
108 (define-key global-map
[f1] 'help-command)
109 (fset 'help-command help-map)
111 ;; insert-button makes the action nil if it is not store somewhere
112 (defvar help-button-cache nil)
116 "Just exit from the Help command's command loop."
120 (defvar help-return-method nil
121 "What to do to \"exit\" the help buffer.
123 (WINDOW . t) delete the selected window (and possibly its frame,
124 see `quit-window'), go to WINDOW.
125 (WINDOW . quit-window) do quit-window, then select WINDOW.
126 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
128 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
129 (defun help-print-return-message (&optional function)
130 "Display or return message saying how to restore windows after help command.
131 This function assumes that `standard-output' is the help buffer.
132 It computes a message, and applies the optional argument FUNCTION to it.
133 If FUNCTION is nil, it applies `message', thus displaying the message.
134 In addition, this function sets up `help-return-method', which see, that
135 specifies what to do when the user exits the help buffer.
137 Do not call this in the scope of `with-help-window'."
138 (and (not (get-buffer-window standard-output))
142 (special-display-p (buffer-name standard-output)))
143 (setq help-return-method (cons (selected-window) t))
144 ;; If the help output buffer is a special display buffer,
145 ;; don't say anything about how to get rid of it.
146 ;; First of all, the user will do that with the window
147 ;; manager, not with Emacs.
148 ;; Secondly, the buffer has not been displayed yet,
149 ;; so we don't know whether its frame will be selected.
151 ((not (one-window-p t))
152 (setq help-return-method
153 (cons (selected-window) 'quit-window))
154 "Type \\[display-buffer] RET to restore the other window.")
156 (setq help-return-method (cons (selected-window) t))
157 "Type \\[delete-other-windows] to remove help window.")
159 (setq help-return-method
160 (list (selected-window) (window-buffer)
161 (window-start) (window-point)))
162 "Type \\[switch-to-buffer] RET to remove help window."))))
163 (funcall (or function 'message)
166 (substitute-command-keys first-message))
167 (if first-message " ")
168 ;; If the help buffer will go in a separate frame,
169 ;; it's no use mentioning a command to scroll, so don't.
170 (if (or pop-up-windows
171 (special-display-p (buffer-name standard-output)))
173 (if (same-window-p (buffer-name standard-output))
174 ;; Say how to scroll this window.
175 (substitute-command-keys
176 "\\[scroll-up] to scroll the help.")
177 ;; Say how to scroll some other window.
178 (substitute-command-keys
179 "\\[scroll-other-window] to scroll the help."))))))))
181 ;; So keyboard macro definitions are documented correctly
182 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
184 (defalias 'help 'help-for-help-internal)
185 ;; find-function can find this.
186 (defalias 'help-for-help 'help-for-help-internal)
187 ;; It can't find this, but nobody will look.
188 (make-help-screen help-for-help-internal
189 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
190 ;; Don't purecopy this one, because it's not evaluated (it's
191 ;; directly used as a docstring in a function definition, so it'll
192 ;; be moved to the DOC file anyway: no need for purecopying it).
193 "You have typed %THIS-KEY%, the help character. Type a Help option:
194 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
196 a PATTERN Show commands whose name matches the PATTERN (a list of words
197 or a regexp). See also the `apropos' command.
198 b Display all key bindings.
199 c KEYS Display the command name run by the given key sequence.
200 C CODING Describe the given coding system, or RET for current ones.
201 d PATTERN Show a list of functions, variables, and other items whose
202 documentation matches the PATTERN (a list of words or a regexp).
203 e Go to the *Messages* buffer which logs echo-area messages.
204 f FUNCTION Display documentation for the given function.
205 F COMMAND Show the Emacs manual's section that describes the command.
206 g Display information about the GNU project.
207 h Display the HELLO file which illustrates various scripts.
208 i Start the Info documentation reader: read included manuals.
209 I METHOD Describe a specific input method, or RET for current.
210 k KEYS Display the full documentation for the key sequence.
211 K KEYS Show the Emacs manual's section for the command bound to KEYS.
212 l Show last 300 input keystrokes (lossage).
213 L LANG-ENV Describes a specific language environment, or RET for current.
214 m Display documentation of current minor modes and current major mode,
215 including their special commands.
216 n Display news of recent Emacs changes.
217 p TOPIC Find packages matching a given topic keyword.
218 P PACKAGE Describe the given Emacs Lisp package.
219 r Display the Emacs manual in Info mode.
220 s Display contents of current syntax table, plus explanations.
221 S SYMBOL Show the section for the given symbol in the Info manual
222 for the programming language used in this buffer.
223 t Start the Emacs learn-by-doing tutorial.
224 v VARIABLE Display the given variable's documentation and value.
225 w COMMAND Display which keystrokes invoke the given command (where-is).
226 . Display any available local help at point in the echo area.
228 C-a Information about Emacs.
229 C-c Emacs copying permission (GNU General Public License).
230 C-d Instructions for debugging GNU Emacs.
231 C-e External packages and information about Emacs.
233 C-m How to order printed Emacs manuals.
234 C-n News of recent Emacs changes.
235 C-o Emacs ordering and distribution information.
236 C-p Info about known Emacs problems.
238 C-w Information on absence of warranty for GNU Emacs."
243 (defun function-called-at-point ()
244 "Return a function around point or else called by the list containing point.
245 If that doesn't give a function, return nil."
246 (with-syntax-table emacs-lisp-mode-syntax-table
247 (or (condition-case ()
249 (or (not (zerop (skip-syntax-backward "_w")))
250 (eq (char-syntax (following-char)) ?w)
251 (eq (char-syntax (following-char)) ?_)
253 (skip-chars-forward "'")
254 (let ((obj (read (current-buffer))))
255 (and (symbolp obj) (fboundp obj) obj)))
260 (narrow-to-region (max (point-min)
261 (- (point) 1000)) (point-max))
262 ;; Move up to surrounding paren, then after the open.
265 ;; If there is space here, this is probably something
266 ;; other than a real Lisp function call, so ignore it.
267 (if (looking-at "[ \t]")
268 (error "Probably not a Lisp function call"))
269 (let ((obj (read (current-buffer))))
270 (and (symbolp obj) (fboundp obj) obj))))
272 (let* ((str (find-tag-default))
273 (sym (if str (intern-soft str))))
274 (if (and sym (fboundp sym))
277 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
278 (setq sym (intern-soft (match-string 1 str)))
279 (and (fboundp sym) sym))))))))
282 ;;; `User' help functions
284 (defun view-help-file (file &optional dir)
285 (view-file (expand-file-name file (or dir data-directory)))
286 (goto-address-mode 1)
287 (goto-char (point-min)))
289 (defun describe-distribution ()
290 "Display info on how to obtain the latest version of GNU Emacs."
292 (view-help-file "DISTRIB"))
294 (defun describe-copying ()
295 "Display info on how you may redistribute copies of GNU Emacs."
297 (view-help-file "COPYING"))
299 ;; Maybe this command should just be removed.
300 (defun describe-gnu-project ()
301 "Browse online information on the GNU project."
303 (browse-url "http://www.gnu.org/gnu/thegnuproject.html"))
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."
311 (let (case-fold-search)
312 (search-forward "Disclaimer of Warranty")
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."
321 (let ((key (this-command-keys)))
324 (substring key 0 (1- (length key)))
325 (let ((prefix (make-vector (1- (length key)) nil))
327 (while (< i (length prefix))
328 (aset prefix i (aref key i))
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."
340 (setq version emacs-major-version))
341 (when (consp version)
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)))))
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)))
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)
378 ((>= vn emacs-major-version) "NEWS")
379 ((< vn 18) "NEWS.1-17")
380 (t (format "NEWS.%d" vn))))
382 (view-file (expand-file-name file data-directory))
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 "$")
395 (while (and (setq res
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)))
405 (defun view-emacs-todo (&optional _arg)
406 "Display the Emacs TODO list."
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'."
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."
425 ;; (view-help-file "ORDERS")
426 (info "(emacs)Printed Books"))
428 (defun view-emacs-FAQ ()
429 "Display the Emacs Frequently Asked Questions (FAQ) file."
431 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
434 (defun view-emacs-problems ()
435 "Display info on known problems with Emacs and possible workarounds."
437 (view-help-file "PROBLEMS"))
439 (defun view-emacs-debugging ()
440 "Display info on how to debug Emacs problems."
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."
448 (info "(efaq)Packages that do not come with Emacs"))
450 (defun view-lossage ()
451 "Display last few input keystrokes and the commands run.
453 To record all your input, use `open-dribble-file'."
455 (help-setup-xref (list #'view-lossage)
456 (called-interactively-p 'interactive))
457 (with-help-window (help-buffer)
459 (princ (mapconcat (lambda (key)
461 ((and (consp key) (null (car key)))
462 (format "[%s]\n" (if (symbolp (cdr key)) (cdr key)
463 "anonymous-command")))
464 ((or (integerp key) (symbolp key) (listp key))
465 (single-key-description key))
467 (prin1-to-string key nil))))
468 (recent-keys 'include-cmds)
470 (with-current-buffer standard-output
471 (goto-char (point-min))
475 (fill-region (line-beginning-position) (line-end-position)))
477 ;; jidanni wants to see the last keystrokes immediately.
478 (set-marker help-window-point-marker (point)))))
483 (defun describe-bindings (&optional prefix buffer)
484 "Display a buffer showing a list of all defined keys, and their definitions.
485 The keys are displayed in order of precedence.
487 The optional argument PREFIX, if non-nil, should be a key sequence;
488 then we display only bindings that start with that prefix.
489 The optional argument BUFFER specifies which buffer's bindings
490 to display (default, the current buffer). BUFFER can be a buffer
493 (or buffer (setq buffer (current-buffer)))
494 (help-setup-xref (list #'describe-bindings prefix buffer)
495 (called-interactively-p 'interactive))
496 (with-help-window (help-buffer)
497 ;; Be aware that `describe-buffer-bindings' puts its output into
498 ;; the current buffer.
499 (with-current-buffer (help-buffer)
500 (describe-buffer-bindings buffer prefix))))
502 ;; This function used to be in keymap.c.
503 (defun describe-bindings-internal (&optional menus prefix)
504 "Show a list of all defined keys, and their definitions.
505 We put that list in a buffer, and display the buffer.
507 The optional argument MENUS, if non-nil, says to mention menu bindings.
508 \(Ordinarily these are omitted from the output.)
509 The optional argument PREFIX, if non-nil, should be a key sequence;
510 then we display only bindings that start with that prefix."
511 (declare (obsolete describe-buffer-bindings "24.4"))
512 (let ((buf (current-buffer)))
513 (with-help-window (help-buffer)
514 ;; Be aware that `describe-buffer-bindings' puts its output into
515 ;; the current buffer.
516 (with-current-buffer (help-buffer)
517 (describe-buffer-bindings buf prefix menus)))))
519 (defun where-is (definition &optional insert)
520 "Print message listing key sequences that invoke the command DEFINITION.
521 Argument is a command definition, usually a symbol with a function definition.
522 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
524 (let ((fn (function-called-at-point))
525 (enable-recursive-minibuffers t)
527 (setq val (completing-read
529 (format "Where is command (default %s): " fn)
530 "Where is command: ")
531 obarray 'commandp t nil nil
532 (and fn (symbol-name fn))))
533 (list (unless (equal val "") (intern val))
534 current-prefix-arg)))
535 (unless definition (error "No command"))
536 (let ((func (indirect-function definition))
538 (standard-output (if insert (current-buffer) standard-output)))
539 ;; In DEFS, find all symbols that are aliases for DEFINITION.
540 (mapatoms (lambda (symbol)
541 (and (fboundp symbol)
542 (not (eq symbol definition))
543 (eq func (condition-case ()
544 (indirect-function symbol)
546 (push symbol defs))))
547 ;; Look at all the symbols--first DEFINITION,
549 (dolist (symbol (cons definition defs))
550 (let* ((remapped (command-remapping symbol))
551 (keys (where-is-internal
552 symbol overriding-local-map nil nil remapped))
553 (keys (mapconcat 'key-description keys ", "))
557 (if (> (length keys) 0)
559 (format "%s (%s) (remapped from %s)"
560 keys remapped symbol)
561 (format "%s (%s)" keys symbol))
562 (format "M-x %s RET" symbol))
563 (if (> (length keys) 0)
565 (format "%s is remapped to %s which is on %s"
566 symbol remapped keys)
567 (format "%s is on %s" symbol keys))
568 ;; If this is the command the user asked about,
569 ;; and it is not on any key, say so.
570 ;; For other symbols, its aliases, say nothing
571 ;; about them unless they are on keys.
572 (if (eq symbol definition)
573 (format "%s is not on any key" symbol)))))
575 (unless (eq symbol definition)
576 (princ ";\n its alias "))
580 (defun help-key-description (key untranslated)
581 (let ((string (key-description key)))
582 (if (or (not untranslated)
583 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
585 (let ((otherstring (key-description untranslated)))
586 (if (equal string otherstring)
588 (format "%s (translated from %s)" string otherstring))))))
590 (defun describe-key-briefly (&optional key insert untranslated)
591 "Print the name of the function KEY invokes. KEY is a string.
592 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
593 If non-nil, UNTRANSLATED is a vector of the untranslated events.
594 It can also be a number in which case the untranslated events from
595 the last key hit are used.
597 If KEY is a menu item or a tool-bar button that is disabled, this command
598 temporarily enables it to allow getting help on disabled items and buttons."
600 (let ((enable-disabled-menus-and-buttons t)
601 (cursor-in-echo-area t)
605 ;; If yank-menu is empty, populate it temporarily, so that
606 ;; "Select and Paste" menu can generate a complete event.
607 (when (null (cdr yank-menu))
608 (setq saved-yank-menu (copy-sequence yank-menu))
609 (menu-bar-update-yank-menu "(any string)" nil))
610 (setq key (read-key-sequence "Describe key (or click or menu item): "))
611 ;; Clear the echo area message (Bug#7014).
613 ;; If KEY is a down-event, read and discard the
614 ;; corresponding up-event. Note that there are also
615 ;; down-events on scroll bars and mode lines: the actual
616 ;; event then is in the second element of the vector.
618 (let ((last-idx (1- (length key))))
619 (and (eventp (aref key last-idx))
620 (memq 'down (event-modifiers (aref key last-idx)))))
624 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
626 ;; Put yank-menu back as it was, if we changed it.
627 (when saved-yank-menu
628 (setq yank-menu (copy-sequence saved-yank-menu))
629 (fset 'yank-menu (cons 'keymap yank-menu))))))
630 (if (numberp untranslated)
631 (setq untranslated (this-single-command-raw-keys)))
632 (let* ((event (if (and (symbolp (aref key 0))
634 (consp (aref key 1)))
637 (modifiers (event-modifiers event))
638 (standard-output (if insert (current-buffer) standard-output))
639 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
640 (memq 'drag modifiers)) " at that spot" ""))
641 (defn (key-binding key t))
643 ;; Handle the case where we faked an entry in "Select and Paste" menu.
644 (if (and (eq defn nil)
645 (stringp (aref key (1- (length key))))
646 (eq (key-binding (substring key 0 -1)) 'yank-menu))
647 (setq defn 'menu-bar-select-yank))
648 ;; Don't bother user with strings from (e.g.) the select-paste menu.
649 (if (stringp (aref key (1- (length key))))
650 (aset key (1- (length key)) "(any string)"))
651 (if (and (> (length untranslated) 0)
652 (stringp (aref untranslated (1- (length untranslated)))))
653 (aset untranslated (1- (length untranslated)) "(any string)"))
654 ;; Now describe the key, perhaps as changed.
655 (setq key-desc (help-key-description key untranslated))
656 (if (or (null defn) (integerp defn) (equal defn 'undefined))
657 (princ (format "%s%s is undefined" key-desc mouse-msg))
658 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
660 (defun help--key-binding-keymap (key &optional accept-default no-remap position)
661 "Return a keymap holding a binding for KEY within current keymaps.
662 The effect of the arguments KEY, ACCEPT-DEFAULT, NO-REMAP and
663 POSITION is as documented in the function `key-binding'."
664 (let* ((active-maps (current-active-maps t position))
666 ;; We loop over active maps like key-binding does.
669 (setq map (pop active-maps)))
670 (setq found (lookup-key map key accept-default))
671 (when (integerp found)
672 ;; The first `found' characters of KEY were found but not the
676 (if (and (symbolp found)
678 (command-remapping found))
679 ;; The user might want to know in which map the binding is
680 ;; found, or in which map the remapping is found. The
681 ;; default is to show the latter.
682 (help--key-binding-keymap (vector 'remap found))
685 (defun help--binding-locus (key position)
686 "Describe in which keymap KEY is defined.
687 Return a symbol pointing to that keymap if one exists ; otherwise
689 (let ((map (help--key-binding-keymap key t nil position)))
692 (let ((advertised-syms (nconc
693 (list 'overriding-terminal-local-map
694 'overriding-local-map)
697 (lambda (mode-and-map)
698 (let ((mode (car mode-and-map)))
699 (when (symbol-value mode)
701 (format "%s-map" mode)))))
702 minor-mode-map-alist))
704 (intern-soft (format "%s-map" major-mode)))))
706 ;; Look into these advertised symbols first.
707 (dolist (sym advertised-syms)
710 (eq map (symbol-value sym)))
712 ;; Only look in other symbols otherwise.
715 (when (and (boundp x)
716 ;; Avoid let-bound symbols.
717 (special-variable-p x)
718 (eq (symbol-value x) map))
722 (defun describe-key (&optional key untranslated up-event)
723 "Display documentation of the function invoked by KEY.
724 KEY can be any kind of a key sequence; it can include keyboard events,
725 mouse events, and/or menu events. When calling from a program,
726 pass KEY as a string or a vector.
728 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
729 It can also be a number, in which case the untranslated events from
730 the last key sequence entered are used.
731 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
733 If KEY is a menu item or a tool-bar button that is disabled, this command
734 temporarily enables it to allow getting help on disabled items and buttons."
736 (let ((enable-disabled-menus-and-buttons t)
737 (cursor-in-echo-area t)
741 ;; If yank-menu is empty, populate it temporarily, so that
742 ;; "Select and Paste" menu can generate a complete event.
743 (when (null (cdr yank-menu))
744 (setq saved-yank-menu (copy-sequence yank-menu))
745 (menu-bar-update-yank-menu "(any string)" nil))
746 (setq key (read-key-sequence "Describe key (or click or menu item): "))
749 (prefix-numeric-value current-prefix-arg)
750 ;; If KEY is a down-event, read and include the
751 ;; corresponding up-event. Note that there are also
752 ;; down-events on scroll bars and mode lines: the actual
753 ;; event then is in the second element of the vector.
755 (let ((last-idx (1- (length key))))
756 (and (eventp (aref key last-idx))
757 (memq 'down (event-modifiers (aref key last-idx)))))
758 (or (and (eventp (aref key 0))
759 (memq 'down (event-modifiers (aref key 0)))
760 ;; However, for the C-down-mouse-2 popup
761 ;; menu, there is no subsequent up-event. In
762 ;; this case, the up-event is the next
763 ;; element in the supplied vector.
765 (and (> (length key) 1)
766 (eventp (aref key 1))
767 (memq 'down (event-modifiers (aref key 1)))))
769 ;; Put yank-menu back as it was, if we changed it.
770 (when saved-yank-menu
771 (setq yank-menu (copy-sequence saved-yank-menu))
772 (fset 'yank-menu (cons 'keymap yank-menu))))))
773 (if (numberp untranslated)
774 (setq untranslated (this-single-command-raw-keys)))
775 (let* ((event (aref key (if (and (symbolp (aref key 0))
777 (consp (aref key 1)))
780 (modifiers (event-modifiers event))
781 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
782 (memq 'drag modifiers)) " at that spot" ""))
783 (defn (key-binding key t))
784 key-locus key-locus-up key-locus-up-tricky
785 defn-up defn-up-tricky ev-type
786 mouse-1-remapped mouse-1-tricky)
788 ;; Handle the case where we faked an entry in "Select and Paste" menu.
789 (when (and (eq defn nil)
790 (stringp (aref key (1- (length key))))
791 (eq (key-binding (substring key 0 -1)) 'yank-menu))
792 (setq defn 'menu-bar-select-yank))
793 (if (or (null defn) (integerp defn) (equal defn 'undefined))
794 (message "%s%s is undefined"
795 (help-key-description key untranslated) mouse-msg)
796 (help-setup-xref (list #'describe-function defn)
797 (called-interactively-p 'interactive))
798 ;; Don't bother user with strings from (e.g.) the select-paste menu.
799 (when (stringp (aref key (1- (length key))))
800 (aset key (1- (length key)) "(any string)"))
801 (when (and untranslated
802 (stringp (aref untranslated (1- (length untranslated)))))
803 (aset untranslated (1- (length untranslated))
805 ;; Need to do this before erasing *Help* buffer in case event
806 ;; is a mouse click in an existing *Help* buffer.
808 (setq ev-type (event-basic-type up-event))
809 (let ((sequence (vector up-event)))
810 (when (and (eq ev-type 'mouse-1)
811 mouse-1-click-follows-link
812 (not (eq mouse-1-click-follows-link 'double))
813 (setq mouse-1-remapped
814 (mouse-on-link-p (event-start up-event))))
815 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
816 (> mouse-1-click-follows-link 0)))
817 (cond ((stringp mouse-1-remapped)
818 (setq sequence mouse-1-remapped))
819 ((vectorp mouse-1-remapped)
820 (setcar up-event (elt mouse-1-remapped 0)))
821 (t (setcar up-event 'mouse-2))))
822 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
823 (setq key-locus-up (help--binding-locus sequence (event-start up-event)))
825 (setq sequence (vector up-event))
826 (aset sequence 0 'mouse-1)
827 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event)))
828 (setq key-locus-up-tricky (help--binding-locus sequence (event-start up-event))))))
829 (setq key-locus (help--binding-locus key (event-start event)))
830 (with-help-window (help-buffer)
831 (princ (help-key-description key untranslated))
832 (princ (format "%s runs the command %S%s, which is "
833 mouse-msg defn (if key-locus
834 (format " (found in %s)" key-locus)
836 (describe-function-1 defn)
838 (unless (or (null defn-up)
840 (equal defn-up 'undefined))
843 ----------------- up-event %s----------------
845 %s%s%s runs the command %S%s, which is "
846 (if mouse-1-tricky "(short click) " "")
847 (key-description (vector up-event))
850 " is remapped to <mouse-2>, which" "")
851 defn-up (if key-locus-up
852 (format " (found in %s)" key-locus-up)
854 (describe-function-1 defn-up))
855 (unless (or (null defn-up-tricky)
856 (integerp defn-up-tricky)
857 (eq defn-up-tricky 'undefined))
860 ----------------- up-event (long click) ----------------
862 Pressing <%S>%s for longer than %d milli-seconds
863 runs the command %S%s, which is "
865 mouse-1-click-follows-link
866 defn-up-tricky (if key-locus-up-tricky
867 (format " (found in %s)" key-locus-up-tricky)
869 (describe-function-1 defn-up-tricky)))))))
871 (defun describe-mode (&optional buffer)
872 "Display documentation of current major mode and minor modes.
873 A brief summary of the minor modes comes first, followed by the
874 major mode description. This is followed by detailed
875 descriptions of the minor modes, each on a separate page.
877 For this to work correctly for a minor mode, the mode's indicator
878 variable \(listed in `minor-mode-alist') must also be a function
879 whose documentation describes the minor mode.
881 If called from Lisp with a non-nil BUFFER argument, display
882 documentation for the major and minor modes of that buffer."
884 (unless buffer (setq buffer (current-buffer)))
885 (help-setup-xref (list #'describe-mode buffer)
886 (called-interactively-p 'interactive))
887 ;; For the sake of help-do-xref and help-xref-go-back,
888 ;; don't switch buffers before calling `help-buffer'.
889 (with-help-window (help-buffer)
890 (with-current-buffer buffer
892 ;; Older packages do not register in minor-mode-list but only in
894 (dolist (x minor-mode-alist)
896 (unless (memq x minor-mode-list)
897 (push x minor-mode-list)))
898 ;; Find enabled minor mode we will want to mention.
899 (dolist (mode minor-mode-list)
900 ;; Document a minor mode if it is listed in minor-mode-alist,
901 ;; non-nil, and has a function definition.
902 (let ((fmode (or (get mode :minor-mode-function) mode)))
903 (and (boundp mode) (symbol-value mode)
905 (let ((pretty-minor-mode
906 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
909 (substring (symbol-name fmode)
910 0 (match-beginning 0)))
912 (push (list fmode pretty-minor-mode
913 (format-mode-line (assq mode minor-mode-alist)))
917 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
919 (princ "Enabled minor modes:\n")
920 (make-local-variable 'help-button-cache)
921 (with-current-buffer standard-output
922 (dolist (mode minor-modes)
923 (let ((mode-function (nth 0 mode))
924 (pretty-minor-mode (nth 1 mode))
925 (indicator (nth 2 mode)))
926 (add-text-properties 0 (length pretty-minor-mode)
927 '(face bold) pretty-minor-mode)
929 (goto-char (point-max))
931 (push (point-marker) help-button-cache)
932 ;; Document the minor modes fully.
933 (insert pretty-minor-mode)
934 (princ (format " minor mode (%s):\n"
935 (if (zerop (length indicator))
937 (format "indicator%s"
939 (princ (documentation mode-function)))
940 (insert-button pretty-minor-mode
941 'action (car help-button-cache)
943 'help-echo "mouse-2, RET: show full information")
949 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
950 ;; Document the major mode.
951 (let ((mode mode-name))
952 (with-current-buffer standard-output
953 (let ((start (point)))
954 (insert (format-mode-line mode nil nil buffer))
955 (add-text-properties start (point) '(face bold)))))
957 (let* ((mode major-mode)
958 (file-name (find-lisp-object-file-name mode nil)))
960 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
961 ;; Make a hyperlink to the library.
962 (with-current-buffer standard-output
964 (re-search-backward "`\\([^`']+\\)'" nil t)
965 (help-xref-button 1 'help-function-def mode file-name)))))
967 (princ (documentation major-mode)))))
968 ;; For the sake of IELM and maybe others
972 (defun describe-minor-mode (minor-mode)
973 "Display documentation of a minor mode given as MINOR-MODE.
974 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
975 appeared on the mode-line."
976 (interactive (list (completing-read
979 (describe-minor-mode-completion-table-for-symbol)
980 (describe-minor-mode-completion-table-for-indicator)
982 (if (symbolp minor-mode)
983 (setq minor-mode (symbol-name minor-mode)))
984 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
985 (indicators (describe-minor-mode-completion-table-for-indicator)))
987 ((member minor-mode symbols)
988 (describe-minor-mode-from-symbol (intern minor-mode)))
989 ((member minor-mode indicators)
990 (describe-minor-mode-from-indicator minor-mode))
992 (error "No such minor mode: %s" minor-mode)))))
995 (defun describe-minor-mode-completion-table-for-symbol ()
996 ;; In order to list up all minor modes, minor-mode-list
997 ;; is used here instead of minor-mode-alist.
998 (delq nil (mapcar 'symbol-name minor-mode-list)))
1000 (defun describe-minor-mode-from-symbol (symbol)
1001 "Display documentation of a minor mode given as a symbol, SYMBOL"
1002 (interactive (list (intern (completing-read
1003 "Minor mode symbol: "
1004 (describe-minor-mode-completion-table-for-symbol)))))
1005 (if (fboundp symbol)
1006 (describe-function symbol)
1007 (describe-variable symbol)))
1010 (defun describe-minor-mode-completion-table-for-indicator ()
1013 (let ((i (format-mode-line x)))
1014 ;; remove first space if existed
1018 ((eq (aref i 0) ?\s)
1024 (defun describe-minor-mode-from-indicator (indicator)
1025 "Display documentation of a minor mode specified by INDICATOR.
1026 If you call this function interactively, you can give indicator which
1027 is currently activated with completion."
1030 "Minor mode indicator: "
1031 (describe-minor-mode-completion-table-for-indicator))))
1032 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
1034 (describe-minor-mode-from-symbol minor-mode)
1035 (error "Cannot find minor mode for `%s'" indicator))))
1037 (defun lookup-minor-mode-from-indicator (indicator)
1038 "Return a minor mode symbol from its indicator on the mode line."
1039 ;; remove first space if existed
1040 (if (and (< 0 (length indicator))
1041 (eq (aref indicator 0) ?\s))
1042 (setq indicator (substring indicator 1)))
1043 (let ((minor-modes minor-mode-alist)
1046 (let* ((minor-mode (car (car minor-modes)))
1047 (anindicator (format-mode-line
1048 (car (cdr (car minor-modes))))))
1049 ;; remove first space if existed
1050 (if (and (stringp anindicator)
1051 (> (length anindicator) 0)
1052 (eq (aref anindicator 0) ?\s))
1053 (setq anindicator (substring anindicator 1)))
1054 (if (equal indicator anindicator)
1055 (setq result minor-mode
1057 (setq minor-modes (cdr minor-modes)))))
1060 ;;; Automatic resizing of temporary buffers.
1061 (defcustom temp-buffer-max-height
1063 (if (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)
1078 (defcustom temp-buffer-max-width
1080 (if (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)
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
1111 This mode is used by `help', `apropos' and `completion' buffers,
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."
1134 (setq window (window-normalize-window window t))
1135 (let ((height (if (functionp temp-buffer-max-height)
1136 (with-selected-window window
1137 (funcall temp-buffer-max-height (window-buffer)))
1138 temp-buffer-max-height))
1139 (width (if (functionp temp-buffer-max-width)
1140 (with-selected-window window
1141 (funcall temp-buffer-max-width (window-buffer)))
1142 temp-buffer-max-width))
1143 (quit-cadr (cadr (window-parameter window 'quit-restore))))
1144 ;; Resize WINDOW iff it was made by `display-buffer'.
1145 (when (or (and (eq quit-cadr 'window)
1146 (or (and (window-combined-p window)
1147 (not (eq fit-window-to-buffer-horizontally
1149 (pos-visible-in-window-p (point-min) window))
1150 (and (window-combined-p window t)
1151 fit-window-to-buffer-horizontally)))
1152 (and (eq quit-cadr 'frame)
1154 (eq window (frame-root-window window))))
1155 (fit-window-to-buffer window height nil width))))
1158 (defcustom help-window-select 'other
1159 "Non-nil means select help window for viewing.
1161 never (nil) Select help window only if there is no other window
1163 other Select help window unless the selected window is the
1164 only other window on the help window's frame.
1165 always (t) Always select the help window.
1167 This option has effect if and only if the help window was created
1168 by `with-help-window'"
1169 :type '(choice (const :tag "never (nil)" nil)
1170 (const :tag "other" other)
1171 (const :tag "always (t)" t))
1175 (defcustom help-enable-auto-load t
1176 "Whether Help commands can perform autoloading.
1177 If non-nil, whenever \\[describe-function] is called for an
1178 autoloaded function whose docstring contains any key substitution
1179 construct (see `substitute-command-keys'), the library is loaded,
1180 so that the documentation can show the right key bindings."
1185 (defun help-window-display-message (quit-part window &optional scroll)
1186 "Display message telling how to quit and scroll help window.
1187 QUIT-PART is a string telling how to quit the help window WINDOW.
1188 Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1189 SCROLL equal `other' means tell how to scroll the \"other\"
1193 ;; If we don't have QUIT-PART we probably reuse a window
1194 ;; showing the same buffer so we don't show any message.
1195 ((not quit-part) nil)
1196 ((pos-visible-in-window-p
1197 (with-current-buffer (window-buffer window)
1198 (point-max)) window t)
1199 ;; Buffer end is at least partially visible, no need to talk
1203 ", \\[scroll-other-window] to scroll help.")
1204 (scroll ", \\[scroll-up] to scroll help."))))
1206 (substitute-command-keys (concat quit-part scroll-part)))))
1208 (defun help-window-setup (window &optional value)
1209 "Set up help window WINDOW for `with-help-window'.
1210 WINDOW is the window used for displaying the help buffer.
1212 (let* ((help-buffer (when (window-live-p window)
1213 (window-buffer window)))
1214 (help-setup (when (window-live-p window)
1215 (car (window-parameter window 'quit-restore)))))
1217 ;; Handle `help-window-point-marker'.
1218 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1219 (set-window-point window help-window-point-marker)
1220 ;; Reset `help-window-point-marker'.
1221 (set-marker help-window-point-marker nil))
1224 ((or (eq window (selected-window))
1225 (and (or (eq help-window-select t)
1226 (eq help-setup 'frame)
1227 (and (eq help-window-select 'other)
1228 (eq (window-frame window) (selected-frame))
1229 (> (length (window-list nil 'no-mini)) 2)))
1230 (select-window window)))
1231 ;; The help window is or gets selected ...
1232 (help-window-display-message
1234 ((eq help-setup 'window)
1235 ;; ... and is new, ...
1236 "Type \"q\" to delete help window")
1237 ((eq help-setup 'frame)
1238 "Type \"q\" to quit the help frame")
1239 ((eq help-setup 'other)
1240 ;; ... or displayed some other buffer before.
1241 "Type \"q\" to restore previous buffer"))
1243 ((and (eq (window-frame window) (selected-frame))
1244 (= (length (window-list nil 'no-mini)) 2))
1245 ;; There are two windows on the help window's frame and the
1246 ;; other one is the selected one.
1247 (help-window-display-message
1249 ((eq help-setup 'window)
1250 "Type \\[delete-other-windows] to delete the help window")
1251 ((eq help-setup 'other)
1252 "Type \"q\" in help window to restore its previous buffer"))
1255 ;; The help window is not selected ...
1256 (help-window-display-message
1258 ((eq help-setup 'window)
1259 ;; ... and is new, ...
1260 "Type \"q\" in help window to delete it")
1261 ((eq help-setup 'other)
1262 ;; ... or displayed some other buffer before.
1263 "Type \"q\" in help window to restore previous buffer"))
1268 ;; `with-help-window' is a wrapper for `with-temp-buffer-window'
1269 ;; providing the following additional twists:
1271 ;; (1) It puts the buffer in `help-mode' (via `help-mode-setup') and
1272 ;; adds cross references (via `help-mode-finish').
1274 ;; (2) It issues a message telling how to scroll and quit the help
1275 ;; window (via `help-window-setup').
1277 ;; (3) An option (customizable via `help-window-select') to select the
1278 ;; help window automatically.
1280 ;; (4) A marker (`help-window-point-marker') to move point in the help
1281 ;; window to an arbitrary buffer position.
1282 (defmacro with-help-window (buffer-name &rest body)
1283 "Display buffer named BUFFER-NAME in a help window.
1284 Evaluate the forms in BODY with standard output bound to a buffer
1285 called BUFFER-NAME (creating it if it does not exist), put that
1286 buffer in `help-mode', display the buffer in a window (see
1287 `with-temp-buffer-window' for details) and issue a message how to
1288 deal with that \"help\" window when it's no more needed. Select
1289 the help window if the current value of the user option
1290 `help-window-select' says so. Return last value in BODY."
1291 (declare (indent 1) (debug t))
1293 ;; Make `help-window-point-marker' point nowhere. The only place
1294 ;; where this should be set to a buffer position is within BODY.
1295 (set-marker help-window-point-marker nil)
1296 (let ((temp-buffer-window-setup-hook
1297 (cons 'help-mode-setup temp-buffer-window-setup-hook))
1298 (temp-buffer-window-show-hook
1299 (cons 'help-mode-finish temp-buffer-window-show-hook)))
1300 (with-temp-buffer-window
1301 ,buffer-name nil 'help-window-setup (progn ,@body)))))
1303 ;; Called from C, on encountering `help-char' when reading a char.
1304 ;; Don't print to *Help*; that would clobber Help history.
1305 (defun help-form-show ()
1306 "Display the output of a non-nil `help-form'."
1307 (let ((msg (eval help-form)))
1309 (with-output-to-temp-buffer " *Char Help*"
1313 ;; The following functions used to be in help-fns.el, which is not preloaded.
1314 ;; But for various reasons, they are more widely needed, so they were
1315 ;; moved to this file, which is preloaded. http://debbugs.gnu.org/17001
1317 (defun help-split-fundoc (docstring def)
1318 "Split a function DOCSTRING into the actual doc and the usage info.
1319 Return (USAGE . DOC) or nil if there's no usage info, where USAGE info
1320 is a string describing the argument list of DEF, such as
1321 \"(apply FUNCTION &rest ARGUMENTS)\".
1322 DEF is the function whose usage we're looking for in DOCSTRING."
1323 ;; Functions can get the calling sequence at the end of the doc string.
1324 ;; In cases where `function' has been fset to a subr we can't search for
1325 ;; function's name in the doc string so we use `fn' as the anonymous
1326 ;; function name instead.
1327 (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring))
1328 (cons (format "(%s%s"
1329 ;; Replace `fn' with the actual function name.
1330 (if (symbolp def) def "anonymous")
1331 (match-string 1 docstring))
1332 (unless (zerop (match-beginning 0))
1333 (substring docstring 0 (match-beginning 0))))))
1335 (defun help-add-fundoc-usage (docstring arglist)
1336 "Add the usage info to DOCSTRING.
1337 If DOCSTRING already has a usage info, then just return it unchanged.
1338 The usage info is built from ARGLIST. DOCSTRING can be nil.
1339 ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
1340 (unless (stringp docstring) (setq docstring ""))
1341 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring)
1345 (if (string-match "\n?\n\\'" docstring)
1346 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
1348 (if (and (stringp arglist)
1349 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist))
1350 (concat "(fn" (match-string 1 arglist) ")")
1351 (format "%S" (help-make-usage 'fn arglist))))))
1353 (defun help-function-arglist (def &optional preserve-names)
1354 "Return a formal argument list for the function DEF.
1355 IF PRESERVE-NAMES is non-nil, return a formal arglist that uses
1356 the same names as used in the original source code, when possible."
1357 ;; Handle symbols aliased to other symbols.
1358 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
1359 ;; If definition is a macro, find the function inside it.
1360 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
1362 ((and (byte-code-function-p def) (listp (aref def 0))) (aref def 0))
1363 ((eq (car-safe def) 'lambda) (nth 1 def))
1364 ((eq (car-safe def) 'closure) (nth 2 def))
1365 ((or (and (byte-code-function-p def) (integerp (aref def 0)))
1367 (or (when preserve-names
1368 (let* ((doc (condition-case nil (documentation def) (error nil)))
1369 (docargs (if doc (car (help-split-fundoc doc nil))))
1370 (arglist (if docargs
1371 (cdar (read-from-string (downcase docargs)))))
1374 (dolist (arg arglist)
1375 (unless (and (symbolp arg)
1376 (let ((name (symbol-name arg)))
1377 (if (eq (aref name 0) ?&)
1378 (memq arg '(&rest &optional))
1379 (not (string-match "\\." name)))))
1381 (when valid arglist)))
1382 (let* ((args-desc (if (not (subrp def))
1384 (let ((a (subr-arity def)))
1386 (if (numberp (cdr a))
1389 (max (lsh args-desc -8))
1390 (min (logand args-desc 127))
1391 (rest (logand args-desc 128))
1394 (push (intern (concat "arg" (number-to-string (1+ i)))) arglist))
1396 (push '&optional arglist)
1397 (dotimes (i (- max min))
1398 (push (intern (concat "arg" (number-to-string (+ 1 i min))))
1400 (unless (zerop rest) (push '&rest arglist) (push 'rest arglist))
1401 (nreverse arglist))))
1402 ((and (autoloadp def) (not (eq (nth 4 def) 'keymap)))
1403 "[Arg list not available until function definition is loaded.]")
1406 (defun help-make-usage (function arglist)
1407 (cons (if (symbolp function) function 'anonymous)
1408 (mapcar (lambda (arg)
1409 (if (not (symbolp arg)) arg
1410 (let ((name (symbol-name arg)))
1412 ((string-match "\\`&" name) arg)
1413 ((string-match "\\`_" name)
1414 (intern (upcase (substring name 1))))
1415 (t (intern (upcase name)))))))
1421 ;;; help.el ends here