]> code.delx.au - gnu-emacs/blob - lisp/help.el
Merge from emacs-24; up to 2014-05-08T06:58:46Z!rgm@gnu.org
[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))
521 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
522 (unless definition (error "No command"))
523 (let ((func (indirect-function definition))
524 (defs nil)
525 (standard-output (if insert (current-buffer) standard-output)))
526 ;; In DEFS, find all symbols that are aliases for DEFINITION.
527 (mapatoms (lambda (symbol)
528 (and (fboundp symbol)
529 (not (eq symbol definition))
530 (eq func (condition-case ()
531 (indirect-function symbol)
532 (error symbol)))
533 (push symbol defs))))
534 ;; Look at all the symbols--first DEFINITION,
535 ;; then its aliases.
536 (dolist (symbol (cons definition defs))
537 (let* ((remapped (command-remapping symbol))
538 (keys (where-is-internal
539 symbol overriding-local-map nil nil remapped))
540 (keys (mapconcat 'key-description keys ", "))
541 string)
542 (setq string
543 (if insert
544 (if (> (length keys) 0)
545 (if remapped
546 (format "%s (%s) (remapped from %s)"
547 keys remapped symbol)
548 (format "%s (%s)" keys symbol))
549 (format "M-x %s RET" symbol))
550 (if (> (length keys) 0)
551 (if remapped
552 (format "%s is remapped to %s which is on %s"
553 symbol remapped keys)
554 (format "%s is on %s" symbol keys))
555 ;; If this is the command the user asked about,
556 ;; and it is not on any key, say so.
557 ;; For other symbols, its aliases, say nothing
558 ;; about them unless they are on keys.
559 (if (eq symbol definition)
560 (format "%s is not on any key" symbol)))))
561 (when string
562 (unless (eq symbol definition)
563 (princ ";\n its alias "))
564 (princ string)))))
565 nil)
566
567 (defun help-key-description (key untranslated)
568 (let ((string (key-description key)))
569 (if (or (not untranslated)
570 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
571 string
572 (let ((otherstring (key-description untranslated)))
573 (if (equal string otherstring)
574 string
575 (format "%s (translated from %s)" string otherstring))))))
576
577 (defun describe-key-briefly (&optional key insert untranslated)
578 "Print the name of the function KEY invokes. KEY is a string.
579 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
580 If non-nil, UNTRANSLATED is a vector of the untranslated events.
581 It can also be a number in which case the untranslated events from
582 the last key hit are used.
583
584 If KEY is a menu item or a tool-bar button that is disabled, this command
585 temporarily enables it to allow getting help on disabled items and buttons."
586 (interactive
587 (let ((enable-disabled-menus-and-buttons t)
588 (cursor-in-echo-area t)
589 saved-yank-menu)
590 (unwind-protect
591 (let (key)
592 ;; If yank-menu is empty, populate it temporarily, so that
593 ;; "Select and Paste" menu can generate a complete event.
594 (when (null (cdr yank-menu))
595 (setq saved-yank-menu (copy-sequence yank-menu))
596 (menu-bar-update-yank-menu "(any string)" nil))
597 (setq key (read-key-sequence "Describe key (or click or menu item): "))
598 ;; Clear the echo area message (Bug#7014).
599 (message nil)
600 ;; If KEY is a down-event, read and discard the
601 ;; corresponding up-event. Note that there are also
602 ;; down-events on scroll bars and mode lines: the actual
603 ;; event then is in the second element of the vector.
604 (and (vectorp key)
605 (let ((last-idx (1- (length key))))
606 (and (eventp (aref key last-idx))
607 (memq 'down (event-modifiers (aref key last-idx)))))
608 (read-event))
609 (list
610 key
611 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
612 1))
613 ;; Put yank-menu back as it was, if we changed it.
614 (when saved-yank-menu
615 (setq yank-menu (copy-sequence saved-yank-menu))
616 (fset 'yank-menu (cons 'keymap yank-menu))))))
617 (if (numberp untranslated)
618 (setq untranslated (this-single-command-raw-keys)))
619 (let* ((event (if (and (symbolp (aref key 0))
620 (> (length key) 1)
621 (consp (aref key 1)))
622 (aref key 1)
623 (aref key 0)))
624 (modifiers (event-modifiers event))
625 (standard-output (if insert (current-buffer) standard-output))
626 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
627 (memq 'drag modifiers)) " at that spot" ""))
628 (defn (key-binding key t))
629 key-desc)
630 ;; Handle the case where we faked an entry in "Select and Paste" menu.
631 (if (and (eq defn nil)
632 (stringp (aref key (1- (length key))))
633 (eq (key-binding (substring key 0 -1)) 'yank-menu))
634 (setq defn 'menu-bar-select-yank))
635 ;; Don't bother user with strings from (e.g.) the select-paste menu.
636 (if (stringp (aref key (1- (length key))))
637 (aset key (1- (length key)) "(any string)"))
638 (if (and (> (length untranslated) 0)
639 (stringp (aref untranslated (1- (length untranslated)))))
640 (aset untranslated (1- (length untranslated)) "(any string)"))
641 ;; Now describe the key, perhaps as changed.
642 (setq key-desc (help-key-description key untranslated))
643 (if (or (null defn) (integerp defn) (equal defn 'undefined))
644 (princ (format "%s%s is undefined" key-desc mouse-msg))
645 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
646
647 (defun describe-key (&optional key untranslated up-event)
648 "Display documentation of the function invoked by KEY.
649 KEY can be any kind of a key sequence; it can include keyboard events,
650 mouse events, and/or menu events. When calling from a program,
651 pass KEY as a string or a vector.
652
653 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
654 It can also be a number, in which case the untranslated events from
655 the last key sequence entered are used.
656 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
657
658 If KEY is a menu item or a tool-bar button that is disabled, this command
659 temporarily enables it to allow getting help on disabled items and buttons."
660 (interactive
661 (let ((enable-disabled-menus-and-buttons t)
662 (cursor-in-echo-area t)
663 saved-yank-menu)
664 (unwind-protect
665 (let (key)
666 ;; If yank-menu is empty, populate it temporarily, so that
667 ;; "Select and Paste" menu can generate a complete event.
668 (when (null (cdr yank-menu))
669 (setq saved-yank-menu (copy-sequence yank-menu))
670 (menu-bar-update-yank-menu "(any string)" nil))
671 (setq key (read-key-sequence "Describe key (or click or menu item): "))
672 (list
673 key
674 (prefix-numeric-value current-prefix-arg)
675 ;; If KEY is a down-event, read and include the
676 ;; corresponding up-event. Note that there are also
677 ;; down-events on scroll bars and mode lines: the actual
678 ;; event then is in the second element of the vector.
679 (and (vectorp key)
680 (let ((last-idx (1- (length key))))
681 (and (eventp (aref key last-idx))
682 (memq 'down (event-modifiers (aref key last-idx)))))
683 (or (and (eventp (aref key 0))
684 (memq 'down (event-modifiers (aref key 0)))
685 ;; However, for the C-down-mouse-2 popup
686 ;; menu, there is no subsequent up-event. In
687 ;; this case, the up-event is the next
688 ;; element in the supplied vector.
689 (= (length key) 1))
690 (and (> (length key) 1)
691 (eventp (aref key 1))
692 (memq 'down (event-modifiers (aref key 1)))))
693 (read-event))))
694 ;; Put yank-menu back as it was, if we changed it.
695 (when saved-yank-menu
696 (setq yank-menu (copy-sequence saved-yank-menu))
697 (fset 'yank-menu (cons 'keymap yank-menu))))))
698 (if (numberp untranslated)
699 (setq untranslated (this-single-command-raw-keys)))
700 (let* ((event (aref key (if (and (symbolp (aref key 0))
701 (> (length key) 1)
702 (consp (aref key 1)))
703 1
704 0)))
705 (modifiers (event-modifiers event))
706 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
707 (memq 'drag modifiers)) " at that spot" ""))
708 (defn (key-binding key t))
709 defn-up defn-up-tricky ev-type
710 mouse-1-remapped mouse-1-tricky)
711
712 ;; Handle the case where we faked an entry in "Select and Paste" menu.
713 (when (and (eq defn nil)
714 (stringp (aref key (1- (length key))))
715 (eq (key-binding (substring key 0 -1)) 'yank-menu))
716 (setq defn 'menu-bar-select-yank))
717 (if (or (null defn) (integerp defn) (equal defn 'undefined))
718 (message "%s%s is undefined"
719 (help-key-description key untranslated) mouse-msg)
720 (help-setup-xref (list #'describe-function defn)
721 (called-interactively-p 'interactive))
722 ;; Don't bother user with strings from (e.g.) the select-paste menu.
723 (when (stringp (aref key (1- (length key))))
724 (aset key (1- (length key)) "(any string)"))
725 (when (and untranslated
726 (stringp (aref untranslated (1- (length untranslated)))))
727 (aset untranslated (1- (length untranslated))
728 "(any string)"))
729 ;; Need to do this before erasing *Help* buffer in case event
730 ;; is a mouse click in an existing *Help* buffer.
731 (when up-event
732 (setq ev-type (event-basic-type up-event))
733 (let ((sequence (vector up-event)))
734 (when (and (eq ev-type 'mouse-1)
735 mouse-1-click-follows-link
736 (not (eq mouse-1-click-follows-link 'double))
737 (setq mouse-1-remapped
738 (mouse-on-link-p (event-start up-event))))
739 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
740 (> mouse-1-click-follows-link 0)))
741 (cond ((stringp mouse-1-remapped)
742 (setq sequence mouse-1-remapped))
743 ((vectorp mouse-1-remapped)
744 (setcar up-event (elt mouse-1-remapped 0)))
745 (t (setcar up-event 'mouse-2))))
746 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
747 (when mouse-1-tricky
748 (setq sequence (vector up-event))
749 (aset sequence 0 'mouse-1)
750 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
751 (with-help-window (help-buffer)
752 (princ (help-key-description key untranslated))
753 (princ (format "\
754 %s runs the command %S, which is "
755 mouse-msg defn))
756 (describe-function-1 defn)
757 (when up-event
758 (unless (or (null defn-up)
759 (integerp defn-up)
760 (equal defn-up 'undefined))
761 (princ (format "
762
763 ----------------- up-event %s----------------
764
765 %s%s%s runs the command %S, which is "
766 (if mouse-1-tricky "(short click) " "")
767 (key-description (vector up-event))
768 mouse-msg
769 (if mouse-1-remapped
770 " is remapped to <mouse-2>, which" "")
771 defn-up))
772 (describe-function-1 defn-up))
773 (unless (or (null defn-up-tricky)
774 (integerp defn-up-tricky)
775 (eq defn-up-tricky 'undefined))
776 (princ (format "
777
778 ----------------- up-event (long click) ----------------
779
780 Pressing <%S>%s for longer than %d milli-seconds
781 runs the command %S, which is "
782 ev-type mouse-msg
783 mouse-1-click-follows-link
784 defn-up-tricky))
785 (describe-function-1 defn-up-tricky)))))))
786 \f
787 (defun describe-mode (&optional buffer)
788 "Display documentation of current major mode and minor modes.
789 A brief summary of the minor modes comes first, followed by the
790 major mode description. This is followed by detailed
791 descriptions of the minor modes, each on a separate page.
792
793 For this to work correctly for a minor mode, the mode's indicator
794 variable \(listed in `minor-mode-alist') must also be a function
795 whose documentation describes the minor mode.
796
797 If called from Lisp with a non-nil BUFFER argument, display
798 documentation for the major and minor modes of that buffer."
799 (interactive "@")
800 (unless buffer (setq buffer (current-buffer)))
801 (help-setup-xref (list #'describe-mode buffer)
802 (called-interactively-p 'interactive))
803 ;; For the sake of help-do-xref and help-xref-go-back,
804 ;; don't switch buffers before calling `help-buffer'.
805 (with-help-window (help-buffer)
806 (with-current-buffer buffer
807 (let (minor-modes)
808 ;; Older packages do not register in minor-mode-list but only in
809 ;; minor-mode-alist.
810 (dolist (x minor-mode-alist)
811 (setq x (car x))
812 (unless (memq x minor-mode-list)
813 (push x minor-mode-list)))
814 ;; Find enabled minor mode we will want to mention.
815 (dolist (mode minor-mode-list)
816 ;; Document a minor mode if it is listed in minor-mode-alist,
817 ;; non-nil, and has a function definition.
818 (let ((fmode (or (get mode :minor-mode-function) mode)))
819 (and (boundp mode) (symbol-value mode)
820 (fboundp fmode)
821 (let ((pretty-minor-mode
822 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
823 (symbol-name fmode))
824 (capitalize
825 (substring (symbol-name fmode)
826 0 (match-beginning 0)))
827 fmode)))
828 (push (list fmode pretty-minor-mode
829 (format-mode-line (assq mode minor-mode-alist)))
830 minor-modes)))))
831 (setq minor-modes
832 (sort minor-modes
833 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
834 (when minor-modes
835 (princ "Enabled minor modes:\n")
836 (make-local-variable 'help-button-cache)
837 (with-current-buffer standard-output
838 (dolist (mode minor-modes)
839 (let ((mode-function (nth 0 mode))
840 (pretty-minor-mode (nth 1 mode))
841 (indicator (nth 2 mode)))
842 (add-text-properties 0 (length pretty-minor-mode)
843 '(face bold) pretty-minor-mode)
844 (save-excursion
845 (goto-char (point-max))
846 (princ "\n\f\n")
847 (push (point-marker) help-button-cache)
848 ;; Document the minor modes fully.
849 (insert pretty-minor-mode)
850 (princ (format " minor mode (%s):\n"
851 (if (zerop (length indicator))
852 "no indicator"
853 (format "indicator%s"
854 indicator))))
855 (princ (documentation mode-function)))
856 (insert-button pretty-minor-mode
857 'action (car help-button-cache)
858 'follow-link t
859 'help-echo "mouse-2, RET: show full information")
860 (newline)))
861 (forward-line -1)
862 (fill-paragraph nil)
863 (forward-line 1))
864
865 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
866 ;; Document the major mode.
867 (let ((mode mode-name))
868 (with-current-buffer standard-output
869 (let ((start (point)))
870 (insert (format-mode-line mode nil nil buffer))
871 (add-text-properties start (point) '(face bold)))))
872 (princ " mode")
873 (let* ((mode major-mode)
874 (file-name (find-lisp-object-file-name mode nil)))
875 (when file-name
876 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
877 ;; Make a hyperlink to the library.
878 (with-current-buffer standard-output
879 (save-excursion
880 (re-search-backward "`\\([^`']+\\)'" nil t)
881 (help-xref-button 1 'help-function-def mode file-name)))))
882 (princ ":\n")
883 (princ (documentation major-mode)))))
884 ;; For the sake of IELM and maybe others
885 nil)
886
887
888 (defun describe-minor-mode (minor-mode)
889 "Display documentation of a minor mode given as MINOR-MODE.
890 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
891 appeared on the mode-line."
892 (interactive (list (completing-read
893 "Minor mode: "
894 (nconc
895 (describe-minor-mode-completion-table-for-symbol)
896 (describe-minor-mode-completion-table-for-indicator)
897 ))))
898 (if (symbolp minor-mode)
899 (setq minor-mode (symbol-name minor-mode)))
900 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
901 (indicators (describe-minor-mode-completion-table-for-indicator)))
902 (cond
903 ((member minor-mode symbols)
904 (describe-minor-mode-from-symbol (intern minor-mode)))
905 ((member minor-mode indicators)
906 (describe-minor-mode-from-indicator minor-mode))
907 (t
908 (error "No such minor mode: %s" minor-mode)))))
909
910 ;; symbol
911 (defun describe-minor-mode-completion-table-for-symbol ()
912 ;; In order to list up all minor modes, minor-mode-list
913 ;; is used here instead of minor-mode-alist.
914 (delq nil (mapcar 'symbol-name minor-mode-list)))
915
916 (defun describe-minor-mode-from-symbol (symbol)
917 "Display documentation of a minor mode given as a symbol, SYMBOL"
918 (interactive (list (intern (completing-read
919 "Minor mode symbol: "
920 (describe-minor-mode-completion-table-for-symbol)))))
921 (if (fboundp symbol)
922 (describe-function symbol)
923 (describe-variable symbol)))
924
925 ;; indicator
926 (defun describe-minor-mode-completion-table-for-indicator ()
927 (delq nil
928 (mapcar (lambda (x)
929 (let ((i (format-mode-line x)))
930 ;; remove first space if existed
931 (cond
932 ((= 0 (length i))
933 nil)
934 ((eq (aref i 0) ?\s)
935 (substring i 1))
936 (t
937 i))))
938 minor-mode-alist)))
939
940 (defun describe-minor-mode-from-indicator (indicator)
941 "Display documentation of a minor mode specified by INDICATOR.
942 If you call this function interactively, you can give indicator which
943 is currently activated with completion."
944 (interactive (list
945 (completing-read
946 "Minor mode indicator: "
947 (describe-minor-mode-completion-table-for-indicator))))
948 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
949 (if minor-mode
950 (describe-minor-mode-from-symbol minor-mode)
951 (error "Cannot find minor mode for `%s'" indicator))))
952
953 (defun lookup-minor-mode-from-indicator (indicator)
954 "Return a minor mode symbol from its indicator on the mode line."
955 ;; remove first space if existed
956 (if (and (< 0 (length indicator))
957 (eq (aref indicator 0) ?\s))
958 (setq indicator (substring indicator 1)))
959 (let ((minor-modes minor-mode-alist)
960 result)
961 (while minor-modes
962 (let* ((minor-mode (car (car minor-modes)))
963 (anindicator (format-mode-line
964 (car (cdr (car minor-modes))))))
965 ;; remove first space if existed
966 (if (and (stringp anindicator)
967 (> (length anindicator) 0)
968 (eq (aref anindicator 0) ?\s))
969 (setq anindicator (substring anindicator 1)))
970 (if (equal indicator anindicator)
971 (setq result minor-mode
972 minor-modes nil)
973 (setq minor-modes (cdr minor-modes)))))
974 result))
975 \f
976 ;;; Automatic resizing of temporary buffers.
977 (defcustom temp-buffer-max-height
978 (lambda (buffer)
979 (if (eq (selected-window) (frame-root-window))
980 (/ (x-display-pixel-height) (frame-char-height) 2)
981 (/ (- (frame-height) 2) 2)))
982 "Maximum height of a window displaying a temporary buffer.
983 This is effective only when Temp Buffer Resize mode is enabled.
984 The value is the maximum height (in lines) which
985 `resize-temp-buffer-window' will give to a window displaying a
986 temporary buffer. It can also be a function to be called to
987 choose the height for such a buffer. It gets one argument, the
988 buffer, and should return a positive integer. At the time the
989 function is called, the window to be resized is selected."
990 :type '(choice integer function)
991 :group 'help
992 :version "24.3")
993
994 (defcustom temp-buffer-max-width
995 (lambda (buffer)
996 (if (eq (selected-window) (frame-root-window))
997 (/ (x-display-pixel-width) (frame-char-width) 2)
998 (/ (- (frame-width) 2) 2)))
999 "Maximum width of a window displaying a temporary buffer.
1000 This is effective only when Temp Buffer Resize mode is enabled.
1001 The value is the maximum width (in columns) which
1002 `resize-temp-buffer-window' will give to a window displaying a
1003 temporary buffer. It can also be a function to be called to
1004 choose the width for such a buffer. It gets one argument, the
1005 buffer, and should return a positive integer. At the time the
1006 function is called, the window to be resized is selected."
1007 :type '(choice integer function)
1008 :group 'help
1009 :version "24.4")
1010
1011 (define-minor-mode temp-buffer-resize-mode
1012 "Toggle auto-resizing temporary buffer windows (Temp Buffer Resize Mode).
1013 With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
1014 is positive, and disable it otherwise. If called from Lisp,
1015 enable the mode if ARG is omitted or nil.
1016
1017 When Temp Buffer Resize mode is enabled, the windows in which we
1018 show a temporary buffer are automatically resized in height to
1019 fit the buffer's contents, but never more than
1020 `temp-buffer-max-height' nor less than `window-min-height'.
1021
1022 A window is resized only if it has been specially created for the
1023 buffer. Windows that have shown another buffer before are not
1024 resized. A frame is resized only if `fit-frame-to-buffer' is
1025 non-nil.
1026
1027 This mode is used by `help', `apropos' and `completion' buffers,
1028 and some others."
1029 :global t :group 'help
1030 (if temp-buffer-resize-mode
1031 ;; `help-make-xrefs' may add a `back' button and thus increase the
1032 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1033 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1034 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
1035
1036 (defun resize-temp-buffer-window (&optional window)
1037 "Resize WINDOW to fit its contents.
1038 WINDOW must be a live window and defaults to the selected one.
1039 Do not resize if WINDOW was not created by `display-buffer'.
1040
1041 If WINDOW is part of a vertical combination, restrain its new
1042 size by `temp-buffer-max-height' and do not resize if its minimum
1043 accessible position is scrolled out of view. If WINDOW is part
1044 of a horizontal combination, restrain its new size by
1045 `temp-buffer-max-width'. In both cases, the value of the option
1046 `fit-window-to-buffer-horizontally' can inhibit resizing.
1047
1048 If WINDOW is the root window of its frame, resize the frame
1049 provided `fit-frame-to-buffer' is non-nil."
1050 (setq window (window-normalize-window window t))
1051 (let ((height (if (functionp temp-buffer-max-height)
1052 (with-selected-window window
1053 (funcall temp-buffer-max-height (window-buffer)))
1054 temp-buffer-max-height))
1055 (width (if (functionp temp-buffer-max-width)
1056 (with-selected-window window
1057 (funcall temp-buffer-max-width (window-buffer)))
1058 temp-buffer-max-width))
1059 (quit-cadr (cadr (window-parameter window 'quit-restore))))
1060 ;; Resize WINDOW iff it was made by `display-buffer'.
1061 (when (or (and (eq quit-cadr 'window)
1062 (or (and (window-combined-p window)
1063 (not (eq fit-window-to-buffer-horizontally
1064 'only))
1065 (pos-visible-in-window-p (point-min) window))
1066 (and (window-combined-p window t)
1067 fit-window-to-buffer-horizontally)))
1068 (and (eq quit-cadr 'frame)
1069 fit-frame-to-buffer
1070 (eq window (frame-root-window window))))
1071 (fit-window-to-buffer window height nil width))))
1072
1073 ;;; Help windows.
1074 (defcustom help-window-select 'other
1075 "Non-nil means select help window for viewing.
1076 Choices are:
1077 never (nil) Select help window only if there is no other window
1078 on its frame.
1079 other Select help window unless the selected window is the
1080 only other window on the help window's frame.
1081 always (t) Always select the help window.
1082
1083 This option has effect if and only if the help window was created
1084 by `with-help-window'"
1085 :type '(choice (const :tag "never (nil)" nil)
1086 (const :tag "other" other)
1087 (const :tag "always (t)" t))
1088 :group 'help
1089 :version "23.1")
1090
1091 (defcustom help-enable-auto-load t
1092 "Whether Help commands can perform autoloading.
1093 If non-nil, whenever \\[describe-function] is called for an
1094 autoloaded function whose docstring contains any key substitution
1095 construct (see `substitute-command-keys'), the library is loaded,
1096 so that the documentation can show the right key bindings."
1097 :type 'boolean
1098 :group 'help
1099 :version "24.3")
1100
1101 (defun help-window-display-message (quit-part window &optional scroll)
1102 "Display message telling how to quit and scroll help window.
1103 QUIT-PART is a string telling how to quit the help window WINDOW.
1104 Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1105 SCROLL equal `other' means tell how to scroll the \"other\"
1106 window."
1107 (let ((scroll-part
1108 (cond
1109 ;; If we don't have QUIT-PART we probably reuse a window
1110 ;; showing the same buffer so we don't show any message.
1111 ((not quit-part) nil)
1112 ((pos-visible-in-window-p
1113 (with-current-buffer (window-buffer window)
1114 (point-max)) window t)
1115 ;; Buffer end is at least partially visible, no need to talk
1116 ;; about scrolling.
1117 ".")
1118 ((eq scroll 'other)
1119 ", \\[scroll-other-window] to scroll help.")
1120 (scroll ", \\[scroll-up] to scroll help."))))
1121 (message "%s"
1122 (substitute-command-keys (concat quit-part scroll-part)))))
1123
1124 (defun help-window-setup (window &optional value)
1125 "Set up help window WINDOW for `with-help-window'.
1126 WINDOW is the window used for displaying the help buffer.
1127 Return VALUE."
1128 (let* ((help-buffer (when (window-live-p window)
1129 (window-buffer window)))
1130 (help-setup (when (window-live-p window)
1131 (car (window-parameter window 'quit-restore)))))
1132 (when help-buffer
1133 ;; Handle `help-window-point-marker'.
1134 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1135 (set-window-point window help-window-point-marker)
1136 ;; Reset `help-window-point-marker'.
1137 (set-marker help-window-point-marker nil))
1138
1139 (cond
1140 ((or (eq window (selected-window))
1141 (and (or (eq help-window-select t)
1142 (eq help-setup 'frame)
1143 (and (eq help-window-select 'other)
1144 (eq (window-frame window) (selected-frame))
1145 (> (length (window-list nil 'no-mini)) 2)))
1146 (select-window window)))
1147 ;; The help window is or gets selected ...
1148 (help-window-display-message
1149 (cond
1150 ((eq help-setup 'window)
1151 ;; ... and is new, ...
1152 "Type \"q\" to delete help window")
1153 ((eq help-setup 'frame)
1154 "Type \"q\" to quit the help frame")
1155 ((eq help-setup 'other)
1156 ;; ... or displayed some other buffer before.
1157 "Type \"q\" to restore previous buffer"))
1158 window t))
1159 ((and (eq (window-frame window) (selected-frame))
1160 (= (length (window-list nil 'no-mini)) 2))
1161 ;; There are two windows on the help window's frame and the
1162 ;; other one is the selected one.
1163 (help-window-display-message
1164 (cond
1165 ((eq help-setup 'window)
1166 "Type \\[delete-other-windows] to delete the help window")
1167 ((eq help-setup 'other)
1168 "Type \"q\" in help window to restore its previous buffer"))
1169 window 'other))
1170 (t
1171 ;; The help window is not selected ...
1172 (help-window-display-message
1173 (cond
1174 ((eq help-setup 'window)
1175 ;; ... and is new, ...
1176 "Type \"q\" in help window to delete it")
1177 ((eq help-setup 'other)
1178 ;; ... or displayed some other buffer before.
1179 "Type \"q\" in help window to restore previous buffer"))
1180 window))))
1181 ;; Return VALUE.
1182 value))
1183
1184 ;; `with-help-window' is a wrapper for `with-temp-buffer-window'
1185 ;; providing the following additional twists:
1186
1187 ;; (1) It puts the buffer in `help-mode' (via `help-mode-setup') and
1188 ;; adds cross references (via `help-mode-finish').
1189
1190 ;; (2) It issues a message telling how to scroll and quit the help
1191 ;; window (via `help-window-setup').
1192
1193 ;; (3) An option (customizable via `help-window-select') to select the
1194 ;; help window automatically.
1195
1196 ;; (4) A marker (`help-window-point-marker') to move point in the help
1197 ;; window to an arbitrary buffer position.
1198 (defmacro with-help-window (buffer-name &rest body)
1199 "Display buffer named BUFFER-NAME in a help window.
1200 Evaluate the forms in BODY with standard output bound to a buffer
1201 called BUFFER-NAME (creating it if it does not exist), put that
1202 buffer in `help-mode', display the buffer in a window (see
1203 `with-temp-buffer-window' for details) and issue a message how to
1204 deal with that \"help\" window when it's no more needed. Select
1205 the help window if the current value of the user option
1206 `help-window-select' says so. Return last value in BODY."
1207 (declare (indent 1) (debug t))
1208 `(progn
1209 ;; Make `help-window-point-marker' point nowhere. The only place
1210 ;; where this should be set to a buffer position is within BODY.
1211 (set-marker help-window-point-marker nil)
1212 (let ((temp-buffer-window-setup-hook
1213 (cons 'help-mode-setup temp-buffer-window-setup-hook))
1214 (temp-buffer-window-show-hook
1215 (cons 'help-mode-finish temp-buffer-window-show-hook)))
1216 (with-temp-buffer-window
1217 ,buffer-name nil 'help-window-setup (progn ,@body)))))
1218
1219 ;; Called from C, on encountering `help-char' when reading a char.
1220 ;; Don't print to *Help*; that would clobber Help history.
1221 (defun help-form-show ()
1222 "Display the output of a non-nil `help-form'."
1223 (let ((msg (eval help-form)))
1224 (if (stringp msg)
1225 (with-output-to-temp-buffer " *Char Help*"
1226 (princ msg)))))
1227
1228 \f
1229 ;; The following functions used to be in help-fns.el, which is not preloaded.
1230 ;; But for various reasons, they are more widely needed, so they were
1231 ;; moved to this file, which is preloaded. http://debbugs.gnu.org/17001
1232
1233 (defun help-split-fundoc (docstring def)
1234 "Split a function DOCSTRING into the actual doc and the usage info.
1235 Return (USAGE . DOC) or nil if there's no usage info, where USAGE info
1236 is a string describing the argument list of DEF, such as
1237 \"(apply FUNCTION &rest ARGUMENTS)\".
1238 DEF is the function whose usage we're looking for in DOCSTRING."
1239 ;; Functions can get the calling sequence at the end of the doc string.
1240 ;; In cases where `function' has been fset to a subr we can't search for
1241 ;; function's name in the doc string so we use `fn' as the anonymous
1242 ;; function name instead.
1243 (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring))
1244 (cons (format "(%s%s"
1245 ;; Replace `fn' with the actual function name.
1246 (if (symbolp def) def "anonymous")
1247 (match-string 1 docstring))
1248 (unless (zerop (match-beginning 0))
1249 (substring docstring 0 (match-beginning 0))))))
1250
1251 (defun help-add-fundoc-usage (docstring arglist)
1252 "Add the usage info to DOCSTRING.
1253 If DOCSTRING already has a usage info, then just return it unchanged.
1254 The usage info is built from ARGLIST. DOCSTRING can be nil.
1255 ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
1256 (unless (stringp docstring) (setq docstring ""))
1257 (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring)
1258 (eq arglist t))
1259 docstring
1260 (concat docstring
1261 (if (string-match "\n?\n\\'" docstring)
1262 (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
1263 "\n\n")
1264 (if (and (stringp arglist)
1265 (string-match "\\`([^ ]+\\(.*\\))\\'" arglist))
1266 (concat "(fn" (match-string 1 arglist) ")")
1267 (format "%S" (help-make-usage 'fn arglist))))))
1268
1269 (defun help-function-arglist (def &optional preserve-names)
1270 "Return a formal argument list for the function DEF.
1271 IF PRESERVE-NAMES is non-nil, return a formal arglist that uses
1272 the same names as used in the original source code, when possible."
1273 ;; Handle symbols aliased to other symbols.
1274 (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
1275 ;; If definition is a macro, find the function inside it.
1276 (if (eq (car-safe def) 'macro) (setq def (cdr def)))
1277 (cond
1278 ((and (byte-code-function-p def) (listp (aref def 0))) (aref def 0))
1279 ((eq (car-safe def) 'lambda) (nth 1 def))
1280 ((eq (car-safe def) 'closure) (nth 2 def))
1281 ((or (and (byte-code-function-p def) (integerp (aref def 0)))
1282 (subrp def))
1283 (or (when preserve-names
1284 (let* ((doc (condition-case nil (documentation def) (error nil)))
1285 (docargs (if doc (car (help-split-fundoc doc nil))))
1286 (arglist (if docargs
1287 (cdar (read-from-string (downcase docargs)))))
1288 (valid t))
1289 ;; Check validity.
1290 (dolist (arg arglist)
1291 (unless (and (symbolp arg)
1292 (let ((name (symbol-name arg)))
1293 (if (eq (aref name 0) ?&)
1294 (memq arg '(&rest &optional))
1295 (not (string-match "\\." name)))))
1296 (setq valid nil)))
1297 (when valid arglist)))
1298 (let* ((args-desc (if (not (subrp def))
1299 (aref def 0)
1300 (let ((a (subr-arity def)))
1301 (logior (car a)
1302 (if (numberp (cdr a))
1303 (lsh (cdr a) 8)
1304 (lsh 1 7))))))
1305 (max (lsh args-desc -8))
1306 (min (logand args-desc 127))
1307 (rest (logand args-desc 128))
1308 (arglist ()))
1309 (dotimes (i min)
1310 (push (intern (concat "arg" (number-to-string (1+ i)))) arglist))
1311 (when (> max min)
1312 (push '&optional arglist)
1313 (dotimes (i (- max min))
1314 (push (intern (concat "arg" (number-to-string (+ 1 i min))))
1315 arglist)))
1316 (unless (zerop rest) (push '&rest arglist) (push 'rest arglist))
1317 (nreverse arglist))))
1318 ((and (autoloadp def) (not (eq (nth 4 def) 'keymap)))
1319 "[Arg list not available until function definition is loaded.]")
1320 (t t)))
1321
1322 (defun help-make-usage (function arglist)
1323 (cons (if (symbolp function) function 'anonymous)
1324 (mapcar (lambda (arg)
1325 (if (not (symbolp arg)) arg
1326 (let ((name (symbol-name arg)))
1327 (cond
1328 ((string-match "\\`&" name) arg)
1329 ((string-match "\\`_" name)
1330 (intern (upcase (substring name 1))))
1331 (t (intern (upcase name)))))))
1332 arglist)))
1333
1334 \f
1335 (provide 'help)
1336
1337 ;;; help.el ends here