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 300 input keystrokes.
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)
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)))
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)
470 ;; jidanni wants to see the last keystrokes immediately.
471 (set-marker help-window-point-marker (point)))))
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
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."
517 (let ((fn (function-called-at-point))
518 (enable-recursive-minibuffers t)
520 (setq val (completing-read
522 (format "Where is command (default %s): " fn)
523 "Where is command: ")
524 obarray 'commandp t nil nil
525 (and fn (symbol-name fn))))
526 (list (unless (equal val "") (intern val))
527 current-prefix-arg)))
528 (unless definition (error "No command"))
529 (let ((func (indirect-function definition))
531 (standard-output (if insert (current-buffer) standard-output)))
532 ;; In DEFS, find all symbols that are aliases for DEFINITION.
533 (mapatoms (lambda (symbol)
534 (and (fboundp symbol)
535 (not (eq symbol definition))
536 (eq func (condition-case ()
537 (indirect-function symbol)
539 (push symbol defs))))
540 ;; Look at all the symbols--first DEFINITION,
542 (dolist (symbol (cons definition defs))
543 (let* ((remapped (command-remapping symbol))
544 (keys (where-is-internal
545 symbol overriding-local-map nil nil remapped))
546 (keys (mapconcat 'key-description keys ", "))
550 (if (> (length keys) 0)
552 (format "%s (%s) (remapped from %s)"
553 keys remapped symbol)
554 (format "%s (%s)" keys symbol))
555 (format "M-x %s RET" symbol))
556 (if (> (length keys) 0)
558 (format "%s is remapped to %s which is on %s"
559 symbol remapped keys)
560 (format "%s is on %s" symbol keys))
561 ;; If this is the command the user asked about,
562 ;; and it is not on any key, say so.
563 ;; For other symbols, its aliases, say nothing
564 ;; about them unless they are on keys.
565 (if (eq symbol definition)
566 (format "%s is not on any key" symbol)))))
568 (unless (eq symbol definition)
569 (princ ";\n its alias "))
573 (defun help-key-description (key untranslated)
574 (let ((string (key-description key)))
575 (if (or (not untranslated)
576 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
578 (let ((otherstring (key-description untranslated)))
579 (if (equal string otherstring)
581 (format "%s (translated from %s)" string otherstring))))))
583 (defun describe-key-briefly (&optional key insert untranslated)
584 "Print the name of the function KEY invokes. KEY is a string.
585 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
586 If non-nil, UNTRANSLATED is a vector of the untranslated events.
587 It can also be a number in which case the untranslated events from
588 the last key hit are used.
590 If KEY is a menu item or a tool-bar button that is disabled, this command
591 temporarily enables it to allow getting help on disabled items and buttons."
593 (let ((enable-disabled-menus-and-buttons t)
594 (cursor-in-echo-area t)
598 ;; If yank-menu is empty, populate it temporarily, so that
599 ;; "Select and Paste" menu can generate a complete event.
600 (when (null (cdr yank-menu))
601 (setq saved-yank-menu (copy-sequence yank-menu))
602 (menu-bar-update-yank-menu "(any string)" nil))
603 (setq key (read-key-sequence "Describe key (or click or menu item): "))
604 ;; Clear the echo area message (Bug#7014).
606 ;; If KEY is a down-event, read and discard the
607 ;; corresponding up-event. Note that there are also
608 ;; down-events on scroll bars and mode lines: the actual
609 ;; event then is in the second element of the vector.
611 (let ((last-idx (1- (length key))))
612 (and (eventp (aref key last-idx))
613 (memq 'down (event-modifiers (aref key last-idx)))))
617 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
619 ;; Put yank-menu back as it was, if we changed it.
620 (when saved-yank-menu
621 (setq yank-menu (copy-sequence saved-yank-menu))
622 (fset 'yank-menu (cons 'keymap yank-menu))))))
623 (if (numberp untranslated)
624 (setq untranslated (this-single-command-raw-keys)))
625 (let* ((event (if (and (symbolp (aref key 0))
627 (consp (aref key 1)))
630 (modifiers (event-modifiers event))
631 (standard-output (if insert (current-buffer) standard-output))
632 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
633 (memq 'drag modifiers)) " at that spot" ""))
634 (defn (key-binding key t))
636 ;; Handle the case where we faked an entry in "Select and Paste" menu.
637 (if (and (eq defn nil)
638 (stringp (aref key (1- (length key))))
639 (eq (key-binding (substring key 0 -1)) 'yank-menu))
640 (setq defn 'menu-bar-select-yank))
641 ;; Don't bother user with strings from (e.g.) the select-paste menu.
642 (if (stringp (aref key (1- (length key))))
643 (aset key (1- (length key)) "(any string)"))
644 (if (and (> (length untranslated) 0)
645 (stringp (aref untranslated (1- (length untranslated)))))
646 (aset untranslated (1- (length untranslated)) "(any string)"))
647 ;; Now describe the key, perhaps as changed.
648 (setq key-desc (help-key-description key untranslated))
649 (if (or (null defn) (integerp defn) (equal defn 'undefined))
650 (princ (format "%s%s is undefined" key-desc mouse-msg))
651 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
653 (defun help--key-binding-keymap (key &optional accept-default no-remap position)
654 "Return a keymap holding a binding for KEY within current keymaps.
655 The effect of the arguments KEY, ACCEPT-DEFAULT, NO-REMAP and
656 POSITION is as documented in the function `key-binding'."
657 (let* ((active-maps (current-active-maps t position))
659 ;; We loop over active maps like key-binding does.
662 (setq map (pop active-maps)))
663 (setq found (lookup-key map key accept-default))
664 (when (integerp found)
665 ;; The first `found' characters of KEY were found but not the
669 (if (and (symbolp found)
671 (command-remapping found))
672 ;; The user might want to know in which map the binding is
673 ;; found, or in which map the remapping is found. The
674 ;; default is to show the latter.
675 (help--key-binding-keymap (vector 'remap found))
678 (defun help--binding-locus (key position)
679 "Describe in which keymap KEY is defined.
680 Return a symbol pointing to that keymap if one exists ; otherwise
682 (let ((map (help--key-binding-keymap key t nil position)))
685 (let ((advertised-syms (nconc
686 (list 'overriding-terminal-local-map
687 'overriding-local-map)
690 (lambda (mode-and-map)
691 (let ((mode (car mode-and-map)))
692 (when (symbol-value mode)
694 (format "%s-map" mode)))))
695 minor-mode-map-alist))
697 (intern-soft (format "%s-map" major-mode)))))
699 ;; Look into these advertised symbols first.
700 (dolist (sym advertised-syms)
703 (eq map (symbol-value sym)))
705 ;; Only look in other symbols otherwise.
708 (when (and (boundp x)
709 ;; Avoid let-bound symbols.
710 (special-variable-p x)
711 (eq (symbol-value x) map))
715 (defun describe-key (&optional key untranslated up-event)
716 "Display documentation of the function invoked by KEY.
717 KEY can be any kind of a key sequence; it can include keyboard events,
718 mouse events, and/or menu events. When calling from a program,
719 pass KEY as a string or a vector.
721 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
722 It can also be a number, in which case the untranslated events from
723 the last key sequence entered are used.
724 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
726 If KEY is a menu item or a tool-bar button that is disabled, this command
727 temporarily enables it to allow getting help on disabled items and buttons."
729 (let ((enable-disabled-menus-and-buttons t)
730 (cursor-in-echo-area t)
734 ;; If yank-menu is empty, populate it temporarily, so that
735 ;; "Select and Paste" menu can generate a complete event.
736 (when (null (cdr yank-menu))
737 (setq saved-yank-menu (copy-sequence yank-menu))
738 (menu-bar-update-yank-menu "(any string)" nil))
739 (setq key (read-key-sequence "Describe key (or click or menu item): "))
742 (prefix-numeric-value current-prefix-arg)
743 ;; If KEY is a down-event, read and include the
744 ;; corresponding up-event. Note that there are also
745 ;; down-events on scroll bars and mode lines: the actual
746 ;; event then is in the second element of the vector.
748 (let ((last-idx (1- (length key))))
749 (and (eventp (aref key last-idx))
750 (memq 'down (event-modifiers (aref key last-idx)))))
751 (or (and (eventp (aref key 0))
752 (memq 'down (event-modifiers (aref key 0)))
753 ;; However, for the C-down-mouse-2 popup
754 ;; menu, there is no subsequent up-event. In
755 ;; this case, the up-event is the next
756 ;; element in the supplied vector.
758 (and (> (length key) 1)
759 (eventp (aref key 1))
760 (memq 'down (event-modifiers (aref key 1)))))
762 ;; Put yank-menu back as it was, if we changed it.
763 (when saved-yank-menu
764 (setq yank-menu (copy-sequence saved-yank-menu))
765 (fset 'yank-menu (cons 'keymap yank-menu))))))
766 (if (numberp untranslated)
767 (setq untranslated (this-single-command-raw-keys)))
768 (let* ((event (aref key (if (and (symbolp (aref key 0))
770 (consp (aref key 1)))
773 (modifiers (event-modifiers event))
774 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
775 (memq 'drag modifiers)) " at that spot" ""))
776 (defn (key-binding key t))
777 key-locus key-locus-up key-locus-up-tricky
778 defn-up defn-up-tricky ev-type
779 mouse-1-remapped mouse-1-tricky)
781 ;; Handle the case where we faked an entry in "Select and Paste" menu.
782 (when (and (eq defn nil)
783 (stringp (aref key (1- (length key))))
784 (eq (key-binding (substring key 0 -1)) 'yank-menu))
785 (setq defn 'menu-bar-select-yank))
786 (if (or (null defn) (integerp defn) (equal defn 'undefined))
787 (message "%s%s is undefined"
788 (help-key-description key untranslated) mouse-msg)
789 (help-setup-xref (list #'describe-function defn)
790 (called-interactively-p 'interactive))
791 ;; Don't bother user with strings from (e.g.) the select-paste menu.
792 (when (stringp (aref key (1- (length key))))
793 (aset key (1- (length key)) "(any string)"))
794 (when (and untranslated
795 (stringp (aref untranslated (1- (length untranslated)))))
796 (aset untranslated (1- (length untranslated))
798 ;; Need to do this before erasing *Help* buffer in case event
799 ;; is a mouse click in an existing *Help* buffer.
801 (setq ev-type (event-basic-type up-event))
802 (let ((sequence (vector up-event)))
803 (when (and (eq ev-type 'mouse-1)
804 mouse-1-click-follows-link
805 (not (eq mouse-1-click-follows-link 'double))
806 (setq mouse-1-remapped
807 (mouse-on-link-p (event-start up-event))))
808 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
809 (> mouse-1-click-follows-link 0)))
810 (cond ((stringp mouse-1-remapped)
811 (setq sequence mouse-1-remapped))
812 ((vectorp mouse-1-remapped)
813 (setcar up-event (elt mouse-1-remapped 0)))
814 (t (setcar up-event 'mouse-2))))
815 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
816 (setq key-locus-up (help--binding-locus sequence (event-start up-event)))
818 (setq sequence (vector up-event))
819 (aset sequence 0 'mouse-1)
820 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event)))
821 (setq key-locus-up-tricky (help--binding-locus sequence (event-start up-event))))))
822 (setq key-locus (help--binding-locus key (event-start event)))
823 (with-help-window (help-buffer)
824 (princ (help-key-description key untranslated))
825 (princ (format "%s runs the command %S%s, which is "
826 mouse-msg defn (if key-locus
827 (format " (found in %s)" key-locus)
829 (describe-function-1 defn)
831 (unless (or (null defn-up)
833 (equal defn-up 'undefined))
836 ----------------- up-event %s----------------
838 %s%s%s runs the command %S%s, which is "
839 (if mouse-1-tricky "(short click) " "")
840 (key-description (vector up-event))
843 " is remapped to <mouse-2>, which" "")
844 defn-up (if key-locus-up
845 (format " (found in %s)" key-locus-up)
847 (describe-function-1 defn-up))
848 (unless (or (null defn-up-tricky)
849 (integerp defn-up-tricky)
850 (eq defn-up-tricky 'undefined))
853 ----------------- up-event (long click) ----------------
855 Pressing <%S>%s for longer than %d milli-seconds
856 runs the command %S%s, which is "
858 mouse-1-click-follows-link
859 defn-up-tricky (if key-locus-up-tricky
860 (format " (found in %s)" key-locus-up-tricky)
862 (describe-function-1 defn-up-tricky)))))))
864 (defun describe-mode (&optional buffer)
865 "Display documentation of current major mode and minor modes.
866 A brief summary of the minor modes comes first, followed by the
867 major mode description. This is followed by detailed
868 descriptions of the minor modes, each on a separate page.
870 For this to work correctly for a minor mode, the mode's indicator
871 variable \(listed in `minor-mode-alist') must also be a function
872 whose documentation describes the minor mode.
874 If called from Lisp with a non-nil BUFFER argument, display
875 documentation for the major and minor modes of that buffer."
877 (unless buffer (setq buffer (current-buffer)))
878 (help-setup-xref (list #'describe-mode buffer)
879 (called-interactively-p 'interactive))
880 ;; For the sake of help-do-xref and help-xref-go-back,
881 ;; don't switch buffers before calling `help-buffer'.
882 (with-help-window (help-buffer)
883 (with-current-buffer buffer
885 ;; Older packages do not register in minor-mode-list but only in
887 (dolist (x minor-mode-alist)
889 (unless (memq x minor-mode-list)
890 (push x minor-mode-list)))
891 ;; Find enabled minor mode we will want to mention.
892 (dolist (mode minor-mode-list)
893 ;; Document a minor mode if it is listed in minor-mode-alist,
894 ;; non-nil, and has a function definition.
895 (let ((fmode (or (get mode :minor-mode-function) mode)))
896 (and (boundp mode) (symbol-value mode)
898 (let ((pretty-minor-mode
899 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
902 (substring (symbol-name fmode)
903 0 (match-beginning 0)))
905 (push (list fmode pretty-minor-mode
906 (format-mode-line (assq mode minor-mode-alist)))
910 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
912 (princ "Enabled minor modes:\n")
913 (make-local-variable 'help-button-cache)
914 (with-current-buffer standard-output
915 (dolist (mode minor-modes)
916 (let ((mode-function (nth 0 mode))
917 (pretty-minor-mode (nth 1 mode))
918 (indicator (nth 2 mode)))
919 (add-text-properties 0 (length pretty-minor-mode)
920 '(face bold) pretty-minor-mode)
922 (goto-char (point-max))
924 (push (point-marker) help-button-cache)
925 ;; Document the minor modes fully.
926 (insert pretty-minor-mode)
927 (princ (format " minor mode (%s):\n"
928 (if (zerop (length indicator))
930 (format "indicator%s"
932 (princ (documentation mode-function)))
933 (insert-button pretty-minor-mode
934 'action (car help-button-cache)
936 'help-echo "mouse-2, RET: show full information")
942 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
943 ;; Document the major mode.
944 (let ((mode mode-name))
945 (with-current-buffer standard-output
946 (let ((start (point)))
947 (insert (format-mode-line mode nil nil buffer))
948 (add-text-properties start (point) '(face bold)))))
950 (let* ((mode major-mode)
951 (file-name (find-lisp-object-file-name mode nil)))
953 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
954 ;; Make a hyperlink to the library.
955 (with-current-buffer standard-output
957 (re-search-backward "`\\([^`']+\\)'" nil t)
958 (help-xref-button 1 'help-function-def mode file-name)))))
960 (princ (documentation major-mode)))))
961 ;; For the sake of IELM and maybe others
965 (defun describe-minor-mode (minor-mode)
966 "Display documentation of a minor mode given as MINOR-MODE.
967 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
968 appeared on the mode-line."
969 (interactive (list (completing-read
972 (describe-minor-mode-completion-table-for-symbol)
973 (describe-minor-mode-completion-table-for-indicator)
975 (if (symbolp minor-mode)
976 (setq minor-mode (symbol-name minor-mode)))
977 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
978 (indicators (describe-minor-mode-completion-table-for-indicator)))
980 ((member minor-mode symbols)
981 (describe-minor-mode-from-symbol (intern minor-mode)))
982 ((member minor-mode indicators)
983 (describe-minor-mode-from-indicator minor-mode))
985 (error "No such minor mode: %s" minor-mode)))))
988 (defun describe-minor-mode-completion-table-for-symbol ()
989 ;; In order to list up all minor modes, minor-mode-list
990 ;; is used here instead of minor-mode-alist.
991 (delq nil (mapcar 'symbol-name minor-mode-list)))
993 (defun describe-minor-mode-from-symbol (symbol)
994 "Display documentation of a minor mode given as a symbol, SYMBOL"
995 (interactive (list (intern (completing-read
996 "Minor mode symbol: "
997 (describe-minor-mode-completion-table-for-symbol)))))
999 (describe-function symbol)
1000 (describe-variable symbol)))
1003 (defun describe-minor-mode-completion-table-for-indicator ()
1006 (let ((i (format-mode-line x)))
1007 ;; remove first space if existed
1011 ((eq (aref i 0) ?\s)
1017 (defun describe-minor-mode-from-indicator (indicator)
1018 "Display documentation of a minor mode specified by INDICATOR.
1019 If you call this function interactively, you can give indicator which
1020 is currently activated with completion."
1023 "Minor mode indicator: "
1024 (describe-minor-mode-completion-table-for-indicator))))
1025 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
1027 (describe-minor-mode-from-symbol minor-mode)
1028 (error "Cannot find minor mode for `%s'" indicator))))
1030 (defun lookup-minor-mode-from-indicator (indicator)
1031 "Return a minor mode symbol from its indicator on the mode line."
1032 ;; remove first space if existed
1033 (if (and (< 0 (length indicator))
1034 (eq (aref indicator 0) ?\s))
1035 (setq indicator (substring indicator 1)))
1036 (let ((minor-modes minor-mode-alist)
1039 (let* ((minor-mode (car (car minor-modes)))
1040 (anindicator (format-mode-line
1041 (car (cdr (car minor-modes))))))
1042 ;; remove first space if existed
1043 (if (and (stringp anindicator)
1044 (> (length anindicator) 0)
1045 (eq (aref anindicator 0) ?\s))
1046 (setq anindicator (substring anindicator 1)))
1047 (if (equal indicator anindicator)
1048 (setq result minor-mode
1050 (setq minor-modes (cdr minor-modes)))))
1053 ;;; Automatic resizing of temporary buffers.
1054 (defcustom temp-buffer-max-height
1056 (if (eq (selected-window) (frame-root-window))
1057 (/ (x-display-pixel-height) (frame-char-height) 2)
1058 (/ (- (frame-height) 2) 2)))
1059 "Maximum height of a window displaying a temporary buffer.
1060 This is effective only when Temp Buffer Resize mode is enabled.
1061 The value is the maximum height (in lines) which
1062 `resize-temp-buffer-window' will give to a window displaying a
1063 temporary buffer. It can also be a function to be called to
1064 choose the height for such a buffer. It gets one argument, the
1065 buffer, and should return a positive integer. At the time the
1066 function is called, the window to be resized is selected."
1067 :type '(choice integer function)
1071 (defcustom temp-buffer-max-width
1073 (if (eq (selected-window) (frame-root-window))
1074 (/ (x-display-pixel-width) (frame-char-width) 2)
1075 (/ (- (frame-width) 2) 2)))
1076 "Maximum width of a window displaying a temporary buffer.
1077 This is effective only when Temp Buffer Resize mode is enabled.
1078 The value is the maximum width (in columns) which
1079 `resize-temp-buffer-window' will give to a window displaying a
1080 temporary buffer. It can also be a function to be called to
1081 choose the width for such a buffer. It gets one argument, the
1082 buffer, and should return a positive integer. At the time the
1083 function is called, the window to be resized is selected."
1084 :type '(choice integer function)
1088 (define-minor-mode temp-buffer-resize-mode
1089 "Toggle auto-resizing temporary buffer windows (Temp Buffer Resize Mode).
1090 With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
1091 is positive, and disable it otherwise. If called from Lisp,
1092 enable the mode if ARG is omitted or nil.
1094 When Temp Buffer Resize mode is enabled, the windows in which we
1095 show a temporary buffer are automatically resized in height to
1096 fit the buffer's contents, but never more than
1097 `temp-buffer-max-height' nor less than `window-min-height'.
1099 A window is resized only if it has been specially created for the
1100 buffer. Windows that have shown another buffer before are not
1101 resized. A frame is resized only if `fit-frame-to-buffer' is
1104 This mode is used by `help', `apropos' and `completion' buffers,
1106 :global t :group 'help
1107 (if temp-buffer-resize-mode
1108 ;; `help-make-xrefs' may add a `back' button and thus increase the
1109 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1110 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1111 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
1113 (defun resize-temp-buffer-window (&optional window)
1114 "Resize WINDOW to fit its contents.
1115 WINDOW must be a live window and defaults to the selected one.
1116 Do not resize if WINDOW was not created by `display-buffer'.
1118 If WINDOW is part of a vertical combination, restrain its new
1119 size by `temp-buffer-max-height' and do not resize if its minimum
1120 accessible position is scrolled out of view. If WINDOW is part
1121 of a horizontal combination, restrain its new size by
1122 `temp-buffer-max-width'. In both cases, the value of the option
1123 `fit-window-to-buffer-horizontally' can inhibit resizing.
1125 If WINDOW is the root window of its frame, resize the frame
1126 provided `fit-frame-to-buffer' is non-nil."
1127 (setq window (window-normalize-window window t))
1128 (let ((height (if (functionp temp-buffer-max-height)
1129 (with-selected-window window
1130 (funcall temp-buffer-max-height (window-buffer)))
1131 temp-buffer-max-height))
1132 (width (if (functionp temp-buffer-max-width)
1133 (with-selected-window window
1134 (funcall temp-buffer-max-width (window-buffer)))
1135 temp-buffer-max-width))
1136 (quit-cadr (cadr (window-parameter window 'quit-restore))))
1137 ;; Resize WINDOW iff it was made by `display-buffer'.
1138 (when (or (and (eq quit-cadr 'window)
1139 (or (and (window-combined-p window)
1140 (not (eq fit-window-to-buffer-horizontally
1142 (pos-visible-in-window-p (point-min) window))
1143 (and (window-combined-p window t)
1144 fit-window-to-buffer-horizontally)))
1145 (and (eq quit-cadr 'frame)
1147 (eq window (frame-root-window window))))
1148 (fit-window-to-buffer window height nil width))))
1151 (defcustom help-window-select 'other
1152 "Non-nil means select help window for viewing.
1154 never (nil) Select help window only if there is no other window
1156 other Select help window unless the selected window is the
1157 only other window on the help window's frame.
1158 always (t) Always select the help window.
1160 This option has effect if and only if the help window was created
1161 by `with-help-window'"
1162 :type '(choice (const :tag "never (nil)" nil)
1163 (const :tag "other" other)
1164 (const :tag "always (t)" t))
1168 (defcustom help-enable-auto-load t
1169 "Whether Help commands can perform autoloading.
1170 If non-nil, whenever \\[describe-function] is called for an
1171 autoloaded function whose docstring contains any key substitution
1172 construct (see `substitute-command-keys'), the library is loaded,
1173 so that the documentation can show the right key bindings."
1178 (defun help-window-display-message (quit-part window &optional scroll)
1179 "Display message telling how to quit and scroll help window.
1180 QUIT-PART is a string telling how to quit the help window WINDOW.
1181 Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1182 SCROLL equal `other' means tell how to scroll the \"other\"
1186 ;; If we don't have QUIT-PART we probably reuse a window
1187 ;; showing the same buffer so we don't show any message.
1188 ((not quit-part) nil)
1189 ((pos-visible-in-window-p
1190 (with-current-buffer (window-buffer window)
1191 (point-max)) window t)
1192 ;; Buffer end is at least partially visible, no need to talk
1196 ", \\[scroll-other-window] to scroll help.")
1197 (scroll ", \\[scroll-up] to scroll help."))))
1199 (substitute-command-keys (concat quit-part scroll-part)))))
1201 (defun help-window-setup (window &optional value)
1202 "Set up help window WINDOW for `with-help-window'.
1203 WINDOW is the window used for displaying the help buffer.
1205 (let* ((help-buffer (when (window-live-p window)
1206 (window-buffer window)))
1207 (help-setup (when (window-live-p window)
1208 (car (window-parameter window 'quit-restore)))))
1210 ;; Handle `help-window-point-marker'.
1211 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1212 (set-window-point window help-window-point-marker)
1213 ;; Reset `help-window-point-marker'.
1214 (set-marker help-window-point-marker nil))
1217 ((or (eq window (selected-window))
1218 (and (or (eq help-window-select t)
1219 (eq help-setup 'frame)
1220 (and (eq help-window-select 'other)
1221 (eq (window-frame window) (selected-frame))
1222 (> (length (window-list nil 'no-mini)) 2)))
1223 (select-window window)))
1224 ;; The help window is or gets selected ...
1225 (help-window-display-message
1227 ((eq help-setup 'window)
1228 ;; ... and is new, ...
1229 "Type \"q\" to delete help window")
1230 ((eq help-setup 'frame)
1231 "Type \"q\" to quit the help frame")
1232 ((eq help-setup 'other)
1233 ;; ... or displayed some other buffer before.
1234 "Type \"q\" to restore previous buffer"))
1236 ((and (eq (window-frame window) (selected-frame))
1237 (= (length (window-list nil 'no-mini)) 2))
1238 ;; There are two windows on the help window's frame and the
1239 ;; other one is the selected one.
1240 (help-window-display-message
1242 ((eq help-setup 'window)
1243 "Type \\[delete-other-windows] to delete the help window")
1244 ((eq help-setup 'other)
1245 "Type \"q\" in help window to restore its previous buffer"))
1248 ;; The help window is not selected ...
1249 (help-window-display-message
1251 ((eq help-setup 'window)
1252 ;; ... and is new, ...
1253 "Type \"q\" in help window to delete it")
1254 ((eq help-setup 'other)
1255 ;; ... or displayed some other buffer before.
1256 "Type \"q\" in help window to restore previous buffer"))
1261 ;; `with-help-window' is a wrapper for `with-temp-buffer-window'
1262 ;; providing the following additional twists:
1264 ;; (1) It puts the buffer in `help-mode' (via `help-mode-setup') and
1265 ;; adds cross references (via `help-mode-finish').
1267 ;; (2) It issues a message telling how to scroll and quit the help
1268 ;; window (via `help-window-setup').
1270 ;; (3) An option (customizable via `help-window-select') to select the
1271 ;; help window automatically.
1273 ;; (4) A marker (`help-window-point-marker') to move point in the help
1274 ;; window to an arbitrary buffer position.
1275 (defmacro with-help-window (buffer-name &rest body)
1276 "Display buffer named BUFFER-NAME in a help window.
1277 Evaluate the forms in BODY with standard output bound to a buffer
1278 called BUFFER-NAME (creating it if it does not exist), put that
1279 buffer in `help-mode', display the buffer in a window (see
1280 `with-temp-buffer-window' for details) and issue a message how to
1281 deal with that \"help\" window when it's no more needed. Select
1282 the help window if the current value of the user option
1283 `help-window-select' says so. Return last value in BODY."
1284 (declare (indent 1) (debug t))
1286 ;; Make `help-window-point-marker' point nowhere. The only place
1287 ;; where this should be set to a buffer position is within BODY.
1288 (set-marker help-window-point-marker nil)
1289 (let ((temp-buffer-window-setup-hook
1290 (cons 'help-mode-setup temp-buffer-window-setup-hook))
1291 (temp-buffer-window-show-hook
1292 (cons 'help-mode-finish temp-buffer-window-show-hook)))
1293 (with-temp-buffer-window
1294 ,buffer-name nil 'help-window-setup (progn ,@body)))))
1296 ;; Called from C, on encountering `help-char' when reading a char.
1297 ;; Don't print to *Help*; that would clobber Help history.
1298 (defun help-form-show ()
1299 "Display the output of a non-nil `help-form'."
1300 (let ((msg (eval help-form)))
1302 (with-output-to-temp-buffer " *Char Help*"
1306 ;; The following functions used to be in help-fns.el, which is not preloaded.
1307 ;; But for various reasons, they are more widely needed, so they were
1308 ;; moved to this file, which is preloaded. http://debbugs.gnu.org/17001
1310 (defun help-split-fundoc (docstring def)
1311 "Split a function DOCSTRING into the actual doc and the usage info.
1312 Return (USAGE . DOC) or nil if there's no usage info, where USAGE info
1313 is a string describing the argument list of DEF, such as
1314 \"(apply FUNCTION &rest ARGUMENTS)\".
1315 DEF is the function whose usage we're looking for in DOCSTRING."
1316 ;; Functions can get the calling sequence at the end of the doc string.
1317 ;; In cases where `function' has been fset to a subr we can't search for
1318 ;; function's name in the doc string so we use `fn' as the anonymous
1319 ;; function name instead.
1320 (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring))
1321 (cons (format "(%s%s"
1322 ;; Replace `fn' with the actual function name.
1323 (if (symbolp def) def "anonymous")
1324 (match-string 1 docstring))
1325 (unless (zerop (match-beginning 0))
1326 (substring docstring 0 (match-beginning 0))))))
1328 (defun help-add-fundoc-usage (docstring arglist)
1329 "Add the usage info to DOCSTRING.
1330 If DOCSTRING already has a usage info, then just return it unchanged.
1331 The usage info is built from ARGLIST. DOCSTRING can be nil.
1332 ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
1333 (unless (stringp docstring) (setq docstring ""))
1334 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring)
1338 (if (string-match "\n?\n\\'" docstring)
1339 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
1341 (if (and (stringp arglist)
1342 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist))
1343 (concat "(fn" (match-string 1 arglist) ")")
1344 (format "%S" (help-make-usage 'fn arglist))))))
1346 (defun help-function-arglist (def &optional preserve-names)
1347 "Return a formal argument list for the function DEF.
1348 IF PRESERVE-NAMES is non-nil, return a formal arglist that uses
1349 the same names as used in the original source code, when possible."
1350 ;; Handle symbols aliased to other symbols.
1351 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
1352 ;; If definition is a macro, find the function inside it.
1353 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
1355 ((and (byte-code-function-p def) (listp (aref def 0))) (aref def 0))
1356 ((eq (car-safe def) 'lambda) (nth 1 def))
1357 ((eq (car-safe def) 'closure) (nth 2 def))
1358 ((or (and (byte-code-function-p def) (integerp (aref def 0)))
1360 (or (when preserve-names
1361 (let* ((doc (condition-case nil (documentation def) (error nil)))
1362 (docargs (if doc (car (help-split-fundoc doc nil))))
1363 (arglist (if docargs
1364 (cdar (read-from-string (downcase docargs)))))
1367 (dolist (arg arglist)
1368 (unless (and (symbolp arg)
1369 (let ((name (symbol-name arg)))
1370 (if (eq (aref name 0) ?&)
1371 (memq arg '(&rest &optional))
1372 (not (string-match "\\." name)))))
1374 (when valid arglist)))
1375 (let* ((args-desc (if (not (subrp def))
1377 (let ((a (subr-arity def)))
1379 (if (numberp (cdr a))
1382 (max (lsh args-desc -8))
1383 (min (logand args-desc 127))
1384 (rest (logand args-desc 128))
1387 (push (intern (concat "arg" (number-to-string (1+ i)))) arglist))
1389 (push '&optional arglist)
1390 (dotimes (i (- max min))
1391 (push (intern (concat "arg" (number-to-string (+ 1 i min))))
1393 (unless (zerop rest) (push '&rest arglist) (push 'rest arglist))
1394 (nreverse arglist))))
1395 ((and (autoloadp def) (not (eq (nth 4 def) 'keymap)))
1396 "[Arg list not available until function definition is loaded.]")
1399 (defun help-make-usage (function arglist)
1400 (cons (if (symbolp function) function 'anonymous)
1401 (mapcar (lambda (arg)
1402 (if (not (symbolp arg)) arg
1403 (let ((name (symbol-name arg)))
1405 ((string-match "\\`&" name) arg)
1406 ((string-match "\\`_" name)
1407 (intern (upcase (substring name 1))))
1408 (t (intern (upcase name)))))))
1414 ;;; help.el ends here