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