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