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