Checked anti.texi, errors.texi, and maps.texi.
[emacs.git] / lisp / help.el
blobb5c778b1df445f72101f20ab9f73b06023fb0429
1 ;;; help.el --- help commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This code implements GNU Emacs' on-line help system, the one invoked by
27 ;; `M-x help-for-help'.
29 ;;; Code:
31 ;; Get the macro make-help-screen when this is compiled,
32 ;; or run interpreted, but not when the compiled code is loaded.
33 (eval-when-compile (require 'help-macro))
35 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
36 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
37 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
39 ;; The variable `help-window' below is used by `help-mode-finish' to
40 ;; communicate the window displaying help (the "help window") to the
41 ;; macro `with-help-window'. The latter sets `help-window' to t before
42 ;; invoking `with-output-to-temp-buffer'. If and only if `help-window'
43 ;; is eq to t, `help-mode-finish' (called by `temp-buffer-setup-hook')
44 ;; sets `help-window' to the window selected by `display-buffer'.
45 ;; Exiting `with-help-window' and calling `print-help-return-message'
46 ;; reset `help-window' to nil.
47 (defvar help-window nil
48 "Window chosen for displaying help.")
50 ;; `help-window-point-marker' is a marker you can move to a valid
51 ;; position of the buffer shown in the help window in order to override
52 ;; the standard positioning mechanism (`point-min') chosen by
53 ;; `with-output-to-temp-buffer'. `with-help-window' has this point
54 ;; nowhere before exiting. Currently used by `view-lossage' to assert
55 ;; that the last keystrokes are always visible.
56 (defvar help-window-point-marker (make-marker)
57 "Marker to override default `window-point' of `help-window'.")
59 (defvar help-map
60 (let ((map (make-sparse-keymap)))
61 (define-key map (char-to-string help-char) 'help-for-help)
62 (define-key map [help] 'help-for-help)
63 (define-key map [f1] 'help-for-help)
64 (define-key map "." 'display-local-help)
65 (define-key map "?" 'help-for-help)
67 (define-key map "\C-a" 'about-emacs)
68 (define-key map "\C-c" 'describe-copying)
69 (define-key map "\C-d" 'view-emacs-debugging)
70 (define-key map "\C-e" 'view-external-packages)
71 (define-key map "\C-f" 'view-emacs-FAQ)
72 (define-key map "\C-m" 'view-order-manuals)
73 (define-key map "\C-n" 'view-emacs-news)
74 (define-key map "\C-o" 'describe-distribution)
75 (define-key map "\C-p" 'view-emacs-problems)
76 (define-key map "\C-t" 'view-emacs-todo)
77 (define-key map "\C-w" 'describe-no-warranty)
79 ;; This does not fit the pattern, but it is natural given the C-\ command.
80 (define-key map "\C-\\" 'describe-input-method)
82 (define-key map "C" 'describe-coding-system)
83 (define-key map "F" 'Info-goto-emacs-command-node)
84 (define-key map "I" 'describe-input-method)
85 (define-key map "K" 'Info-goto-emacs-key-command-node)
86 (define-key map "L" 'describe-language-environment)
87 (define-key map "S" 'info-lookup-symbol)
89 (define-key map "a" 'apropos-command)
90 (define-key map "b" 'describe-bindings)
91 (define-key map "c" 'describe-key-briefly)
92 (define-key map "d" 'apropos-documentation)
93 (define-key map "e" 'view-echo-area-messages)
94 (define-key map "f" 'describe-function)
95 (define-key map "g" 'describe-gnu-project)
96 (define-key map "h" 'view-hello-file)
98 (define-key map "i" 'info)
99 (define-key map "4i" 'info-other-window)
101 (define-key map "k" 'describe-key)
102 (define-key map "l" 'view-lossage)
103 (define-key map "m" 'describe-mode)
104 (define-key map "n" 'view-emacs-news)
105 (define-key map "p" 'finder-by-keyword)
106 (define-key map "r" 'info-emacs-manual)
107 (define-key map "s" 'describe-syntax)
108 (define-key map "t" 'help-with-tutorial)
109 (define-key map "w" 'where-is)
110 (define-key map "v" 'describe-variable)
111 (define-key map "q" 'help-quit)
112 map)
113 "Keymap for characters following the Help key.")
115 (define-key global-map (char-to-string help-char) 'help-command)
116 (define-key global-map [help] 'help-command)
117 (define-key global-map [f1] 'help-command)
118 (fset 'help-command help-map)
120 (autoload 'finder-by-keyword "finder"
121 "Find packages matching a given keyword." t)
123 ;; insert-button makes the action nil if it is not store somewhere
124 (defvar help-button-cache nil)
127 (defun help-quit ()
128 "Just exit from the Help command's command loop."
129 (interactive)
130 nil)
132 (defvar help-return-method nil
133 "What to do to \"exit\" the help buffer.
134 This is a list
135 (WINDOW . t) delete the selected window (and possibly its frame,
136 see `quit-window' and `View-quit'), go to WINDOW.
137 (WINDOW . quit-window) do quit-window, then select WINDOW.
138 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
140 (defun print-help-return-message (&optional function)
141 "Display or return message saying how to restore windows after help command.
142 This function assumes that `standard-output' is the help buffer.
143 It computes a message, and applies the optional argument FUNCTION to it.
144 If FUNCTION is nil, it applies `message', thus displaying the message.
145 In addition, this function sets up `help-return-method', which see, that
146 specifies what to do when the user exits the help buffer."
147 ;; Reset `help-window' here to avoid confusing `help-mode-finish'.
148 (setq help-window nil)
149 (and (not (get-buffer-window standard-output))
150 (let ((first-message
151 (cond ((or
152 pop-up-frames
153 (special-display-p (buffer-name standard-output)))
154 (setq help-return-method (cons (selected-window) t))
155 ;; If the help output buffer is a special display buffer,
156 ;; don't say anything about how to get rid of it.
157 ;; First of all, the user will do that with the window
158 ;; manager, not with Emacs.
159 ;; Secondly, the buffer has not been displayed yet,
160 ;; so we don't know whether its frame will be selected.
161 nil)
162 (display-buffer-reuse-frames
163 (setq help-return-method (cons (selected-window)
164 'quit-window))
165 nil)
166 ((not (one-window-p t))
167 (setq help-return-method
168 (cons (selected-window) 'quit-window))
169 "Type \\[display-buffer] RET to restore the other window.")
170 (pop-up-windows
171 (setq help-return-method (cons (selected-window) t))
172 "Type \\[delete-other-windows] to remove help window.")
174 (setq help-return-method
175 (list (selected-window) (window-buffer)
176 (window-start) (window-point)))
177 "Type \\[switch-to-buffer] RET to remove help window."))))
178 (funcall (or function 'message)
179 (concat
180 (if first-message
181 (substitute-command-keys first-message))
182 (if first-message " ")
183 ;; If the help buffer will go in a separate frame,
184 ;; it's no use mentioning a command to scroll, so don't.
185 (if (or pop-up-windows
186 (special-display-p (buffer-name standard-output)))
188 (if (same-window-p (buffer-name standard-output))
189 ;; Say how to scroll this window.
190 (substitute-command-keys
191 "\\[scroll-up] to scroll the help.")
192 ;; Say how to scroll some other window.
193 (substitute-command-keys
194 "\\[scroll-other-window] to scroll the help."))))))))
196 ;; So keyboard macro definitions are documented correctly
197 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
199 (defalias 'help 'help-for-help-internal)
200 ;; find-function can find this.
201 (defalias 'help-for-help 'help-for-help-internal)
202 ;; It can't find this, but nobody will look.
203 (make-help-screen help-for-help-internal
204 "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?"
205 "You have typed %THIS-KEY%, the help character. Type a Help option:
206 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
208 a PATTERN Show commands whose name matches the PATTERN (a list of words
209 or a regexp). See also the `apropos' command.
210 b Display all key bindings.
211 c KEYS Display the command name run by the given key sequence.
212 C CODING Describe the given coding system, or RET for current ones.
213 d PATTERN Show a list of functions, variables, and other items whose
214 documentation matches the PATTERN (a list of words or a regexp).
215 e Go to the *Messages* buffer which logs echo-area messages.
216 f FUNCTION Display documentation for the given function.
217 F COMMAND Show the on-line manual's section that describes the command.
218 g Display information about the GNU project.
219 h Display the HELLO file which illustrates various scripts.
220 i Start the Info documentation reader: read on-line manuals.
221 I METHOD Describe a specific input method, or RET for current.
222 k KEYS Display the full documentation for the key sequence.
223 K KEYS Show the on-line manual's section for the command bound to KEYS.
224 l Show last 300 input keystrokes (lossage).
225 L LANG-ENV Describes a specific language environment, or RET for current.
226 m Display documentation of current minor modes and current major mode,
227 including their special commands.
228 n Display news of recent Emacs changes.
229 p TOPIC Find packages matching a given topic keyword.
230 r Display the Emacs manual in Info mode.
231 s Display contents of current syntax table, plus explanations.
232 S SYMBOL Show the section for the given symbol in the on-line manual
233 for the programming language used in this buffer.
234 t Start the Emacs learn-by-doing tutorial.
235 v VARIABLE Display the given variable's documentation and value.
236 w COMMAND Display which keystrokes invoke the given command (where-is).
237 . Display any available local help at point in the echo area.
239 C-a Information about Emacs.
240 C-c Emacs copying permission (GNU General Public License).
241 C-d Instructions for debugging GNU Emacs.
242 C-e External packages and information about Emacs.
243 C-f Emacs FAQ.
244 C-m How to order printed Emacs manuals.
245 C-n News of recent Emacs changes.
246 C-o Emacs ordering and distribution information.
247 C-p Info about known Emacs problems.
248 C-t Emacs TODO list.
249 C-w Information on absence of warranty for GNU Emacs."
250 help-map)
254 (defun function-called-at-point ()
255 "Return a function around point or else called by the list containing point.
256 If that doesn't give a function, return nil."
257 (with-syntax-table emacs-lisp-mode-syntax-table
258 (or (condition-case ()
259 (save-excursion
260 (or (not (zerop (skip-syntax-backward "_w")))
261 (eq (char-syntax (following-char)) ?w)
262 (eq (char-syntax (following-char)) ?_)
263 (forward-sexp -1))
264 (skip-chars-forward "'")
265 (let ((obj (read (current-buffer))))
266 (and (symbolp obj) (fboundp obj) obj)))
267 (error nil))
268 (condition-case ()
269 (save-excursion
270 (save-restriction
271 (narrow-to-region (max (point-min)
272 (- (point) 1000)) (point-max))
273 ;; Move up to surrounding paren, then after the open.
274 (backward-up-list 1)
275 (forward-char 1)
276 ;; If there is space here, this is probably something
277 ;; other than a real Lisp function call, so ignore it.
278 (if (looking-at "[ \t]")
279 (error "Probably not a Lisp function call"))
280 (let ((obj (read (current-buffer))))
281 (and (symbolp obj) (fboundp obj) obj))))
282 (error nil))
283 (let* ((str (find-tag-default))
284 (sym (if str (intern-soft str))))
285 (if (and sym (fboundp sym))
287 (save-match-data
288 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
289 (setq sym (intern-soft (match-string 1 str)))
290 (and (fboundp sym) sym))))))))
293 ;;; `User' help functions
295 (defun view-help-file (file &optional dir)
296 (view-file (expand-file-name file (or dir data-directory)))
297 (goto-address-mode 1)
298 (goto-char (point-min)))
300 (defun describe-distribution ()
301 "Display info on how to obtain the latest version of GNU Emacs."
302 (interactive)
303 (view-help-file "DISTRIB"))
305 (defun describe-copying ()
306 "Display info on how you may redistribute copies of GNU Emacs."
307 (interactive)
308 (view-help-file "COPYING"))
310 (defun describe-gnu-project ()
311 "Display info on the GNU project."
312 (interactive)
313 (view-help-file "THE-GNU-PROJECT"))
315 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
317 (defun describe-no-warranty ()
318 "Display info on all the kinds of warranty Emacs does NOT have."
319 (interactive)
320 (describe-copying)
321 (let (case-fold-search)
322 (search-forward "NO WARRANTY")
323 (recenter 0)))
325 (defun describe-prefix-bindings ()
326 "Describe the bindings of the prefix used to reach this command.
327 The prefix described consists of all but the last event
328 of the key sequence that ran this command."
329 (interactive)
330 (let ((key (this-command-keys)))
331 (describe-bindings
332 (if (stringp key)
333 (substring key 0 (1- (length key)))
334 (let ((prefix (make-vector (1- (length key)) nil))
335 (i 0))
336 (while (< i (length prefix))
337 (aset prefix i (aref key i))
338 (setq i (1+ i)))
339 prefix)))))
340 ;; Make C-h after a prefix, when not specifically bound,
341 ;; run describe-prefix-bindings.
342 (setq prefix-help-command 'describe-prefix-bindings)
344 (defun view-emacs-news (&optional version)
345 "Display info on recent changes to Emacs.
346 With argument, display info only for the selected version."
347 (interactive "P")
348 (unless version
349 (setq version emacs-major-version))
350 (when (consp version)
351 (let* ((all-versions
352 (let (res)
353 (mapc
354 (lambda (file)
355 (with-temp-buffer
356 (insert-file-contents
357 (expand-file-name file data-directory))
358 (while (re-search-forward
359 (if (member file '("NEWS.18" "NEWS.1-17"))
360 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
361 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
362 (setq res (cons (match-string-no-properties 1) res)))))
363 (cons "NEWS"
364 (directory-files data-directory nil
365 "^NEWS\\.[0-9][-0-9]*$" nil)))
366 (sort (delete-dups res) (lambda (a b) (string< b a)))))
367 (current (car all-versions)))
368 (setq version (completing-read
369 (format "Read NEWS for the version (default %s): " current)
370 all-versions nil nil nil nil current))
371 (if (integerp (string-to-number version))
372 (setq version (string-to-number version))
373 (unless (or (member version all-versions)
374 (<= (string-to-number version) (string-to-number current)))
375 (error "No news about version %s" version)))))
376 (when (integerp version)
377 (cond ((<= version 12)
378 (setq version (format "1.%d" version)))
379 ((<= version 18)
380 (setq version (format "%d" version)))
381 ((> version emacs-major-version)
382 (error "No news about Emacs %d (yet)" version))))
383 (let* ((vn (if (stringp version)
384 (string-to-number version)
385 version))
386 (file (cond
387 ((>= vn emacs-major-version) "NEWS")
388 ((< vn 18) "NEWS.1-17")
389 (t (format "NEWS.%d" vn))))
390 res)
391 (view-file (expand-file-name file data-directory))
392 (widen)
393 (goto-char (point-min))
394 (when (stringp version)
395 (when (re-search-forward
396 (concat (if (< vn 19)
397 "Changes in Emacs[ \t]*"
398 "^\* [^0-9\n]*") version "$")
399 nil t)
400 (beginning-of-line)
401 (narrow-to-region
402 (point)
403 (save-excursion
404 (while (and (setq res
405 (re-search-forward
406 (if (< vn 19)
407 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
408 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
409 (equal (match-string-no-properties 1) version)))
410 (or res (goto-char (point-max)))
411 (beginning-of-line)
412 (point)))))))
414 (defun view-emacs-todo (&optional arg)
415 "Display the Emacs TODO list."
416 (interactive "P")
417 (view-help-file "TODO"))
419 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
422 (defun view-echo-area-messages ()
423 "View the log of recent echo-area messages: the `*Messages*' buffer.
424 The number of messages retained in that buffer
425 is specified by the variable `message-log-max'."
426 (interactive)
427 (switch-to-buffer (get-buffer-create "*Messages*")))
429 (defun view-order-manuals ()
430 "Display the Emacs ORDERS file."
431 (interactive)
432 (view-help-file "ORDERS"))
434 (defun view-emacs-FAQ ()
435 "Display the Emacs Frequently Asked Questions (FAQ) file."
436 (interactive)
437 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
438 (info "(efaq)"))
440 (defun view-emacs-problems ()
441 "Display info on known problems with Emacs and possible workarounds."
442 (interactive)
443 (view-help-file "PROBLEMS"))
445 (defun view-emacs-debugging ()
446 "Display info on how to debug Emacs problems."
447 (interactive)
448 (view-help-file "DEBUG"))
450 (defun view-external-packages ()
451 "Display external packages and information about Emacs."
452 (interactive)
453 (view-help-file "MORE.STUFF"))
455 (defun view-lossage ()
456 "Display last 300 input keystrokes.
458 To record all your input on a file, use `open-dribble-file'."
459 (interactive)
460 (help-setup-xref (list #'view-lossage) (interactive-p))
461 (with-help-window (help-buffer)
462 (princ (mapconcat (lambda (key)
463 (if (or (integerp key) (symbolp key) (listp key))
464 (single-key-description key)
465 (prin1-to-string key nil)))
466 (recent-keys)
467 " "))
468 (with-current-buffer standard-output
469 (goto-char (point-min))
470 (while (progn (move-to-column 50) (not (eobp)))
471 (when (search-forward " " nil t)
472 (delete-char -1))
473 (insert "\n"))
474 ;; jidanni wants to see the last keystrokes immediately.
475 (set-marker help-window-point-marker (point)))))
478 ;; Key bindings
480 (defun describe-bindings (&optional prefix buffer)
481 "Show a list of all defined keys, and their definitions.
482 We put that list in a buffer, and display the buffer.
484 The optional argument PREFIX, if non-nil, should be a key sequence;
485 then we display only bindings that start with that prefix.
486 The optional argument BUFFER specifies which buffer's bindings
487 to display (default, the current buffer). BUFFER can be a buffer
488 or a buffer name."
489 (interactive)
490 (or buffer (setq buffer (current-buffer)))
491 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
492 (with-current-buffer buffer
493 (describe-bindings-internal nil prefix)))
495 ;; This function used to be in keymap.c.
496 (defun describe-bindings-internal (&optional menus prefix)
497 "Show a list of all defined keys, and their definitions.
498 We put that list in a buffer, and display the buffer.
500 The optional argument MENUS, if non-nil, says to mention menu bindings.
501 \(Ordinarily these are omitted from the output.)
502 The optional argument PREFIX, if non-nil, should be a key sequence;
503 then we display only bindings that start with that prefix."
504 (interactive)
505 (let ((buf (current-buffer)))
506 (with-help-window "*Help*"
507 (with-current-buffer standard-output
508 (describe-buffer-bindings buf prefix menus)))))
510 (defun where-is (definition &optional insert)
511 "Print message listing key sequences that invoke the command DEFINITION.
512 Argument is a command definition, usually a symbol with a function definition.
513 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
514 (interactive
515 (let ((fn (function-called-at-point))
516 (enable-recursive-minibuffers t)
517 val)
518 (setq val (completing-read
519 (if fn
520 (format "Where is command (default %s): " fn)
521 "Where is command: ")
522 obarray 'commandp t))
523 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
524 (unless definition (error "No command"))
525 (let ((func (indirect-function definition))
526 (defs nil)
527 (standard-output (if insert (current-buffer) t)))
528 ;; In DEFS, find all symbols that are aliases for DEFINITION.
529 (mapatoms (lambda (symbol)
530 (and (fboundp symbol)
531 (not (eq symbol definition))
532 (eq func (condition-case ()
533 (indirect-function symbol)
534 (error symbol)))
535 (push symbol defs))))
536 ;; Look at all the symbols--first DEFINITION,
537 ;; then its aliases.
538 (dolist (symbol (cons definition defs))
539 (let* ((remapped (command-remapping symbol))
540 (keys (where-is-internal
541 symbol overriding-local-map nil nil remapped))
542 (keys (mapconcat 'key-description keys ", "))
543 string)
544 (setq string
545 (if insert
546 (if (> (length keys) 0)
547 (if remapped
548 (format "%s (%s) (remapped from %s)"
549 keys remapped symbol)
550 (format "%s (%s)" keys symbol))
551 (format "M-x %s RET" symbol))
552 (if (> (length keys) 0)
553 (if remapped
554 (format "%s is remapped to %s which is on %s"
555 symbol remapped keys)
556 (format "%s is on %s" symbol keys))
557 ;; If this is the command the user asked about,
558 ;; and it is not on any key, say so.
559 ;; For other symbols, its aliases, say nothing
560 ;; about them unless they are on keys.
561 (if (eq symbol definition)
562 (format "%s is not on any key" symbol)))))
563 (when string
564 (unless (eq symbol definition)
565 (princ ";\n its alias "))
566 (princ string)))))
567 nil)
569 (defun help-key-description (key untranslated)
570 (let ((string (key-description key)))
571 (if (or (not untranslated)
572 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
573 string
574 (let ((otherstring (key-description untranslated)))
575 (if (equal string otherstring)
576 string
577 (format "%s (translated from %s)" string otherstring))))))
579 (defun describe-key-briefly (&optional key insert untranslated)
580 "Print the name of the function KEY invokes. KEY is a string.
581 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
582 If non-nil, UNTRANSLATED is a vector of the untranslated events.
583 It can also be a number in which case the untranslated events from
584 the last key hit are used.
586 If KEY is a menu item or a tool-bar button that is disabled, this command
587 temporarily enables it to allow getting help on disabled items and buttons."
588 (interactive
589 (let ((enable-disabled-menus-and-buttons t)
590 (cursor-in-echo-area t)
591 saved-yank-menu)
592 (unwind-protect
593 (let (key)
594 ;; If yank-menu is empty, populate it temporarily, so that
595 ;; "Select and Paste" menu can generate a complete event.
596 (when (null (cdr yank-menu))
597 (setq saved-yank-menu (copy-sequence yank-menu))
598 (menu-bar-update-yank-menu "(any string)" nil))
599 (setq key (read-key-sequence "Describe key (or click or menu item): "))
600 ;; If KEY is a down-event, read and discard the
601 ;; corresponding up-event. Note that there are also
602 ;; down-events on scroll bars and mode lines: the actual
603 ;; event then is in the second element of the vector.
604 (and (vectorp key)
605 (let ((last-idx (1- (length key))))
606 (and (eventp (aref key last-idx))
607 (memq 'down (event-modifiers (aref key last-idx)))))
608 (read-event))
609 (list
611 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
613 ;; Put yank-menu back as it was, if we changed it.
614 (when saved-yank-menu
615 (setq yank-menu (copy-sequence saved-yank-menu))
616 (fset 'yank-menu (cons 'keymap yank-menu))))))
617 (if (numberp untranslated)
618 (setq untranslated (this-single-command-raw-keys)))
619 (let* ((event (if (and (symbolp (aref key 0))
620 (> (length key) 1)
621 (consp (aref key 1)))
622 (aref key 1)
623 (aref key 0)))
624 (modifiers (event-modifiers event))
625 (standard-output (if insert (current-buffer) t))
626 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
627 (memq 'drag modifiers)) " at that spot" ""))
628 (defn (key-binding key t))
629 key-desc)
630 ;; Handle the case where we faked an entry in "Select and Paste" menu.
631 (if (and (eq defn nil)
632 (stringp (aref key (1- (length key))))
633 (eq (key-binding (substring key 0 -1)) 'yank-menu))
634 (setq defn 'menu-bar-select-yank))
635 ;; Don't bother user with strings from (e.g.) the select-paste menu.
636 (if (stringp (aref key (1- (length key))))
637 (aset key (1- (length key)) "(any string)"))
638 (if (and (> (length untranslated) 0)
639 (stringp (aref untranslated (1- (length untranslated)))))
640 (aset untranslated (1- (length untranslated)) "(any string)"))
641 ;; Now describe the key, perhaps as changed.
642 (setq key-desc (help-key-description key untranslated))
643 (if (or (null defn) (integerp defn) (equal defn 'undefined))
644 (princ (format "%s%s is undefined" key-desc mouse-msg))
645 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
647 (defun describe-key (&optional key untranslated up-event)
648 "Display documentation of the function invoked by KEY.
649 KEY can be any kind of a key sequence; it can include keyboard events,
650 mouse events, and/or menu events. When calling from a program,
651 pass KEY as a string or a vector.
653 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
654 It can also be a number, in which case the untranslated events from
655 the last key sequence entered are used.
656 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
658 If KEY is a menu item or a tool-bar button that is disabled, this command
659 temporarily enables it to allow getting help on disabled items and buttons."
660 (interactive
661 (let ((enable-disabled-menus-and-buttons t)
662 (cursor-in-echo-area t)
663 saved-yank-menu)
664 (unwind-protect
665 (let (key)
666 ;; If yank-menu is empty, populate it temporarily, so that
667 ;; "Select and Paste" menu can generate a complete event.
668 (when (null (cdr yank-menu))
669 (setq saved-yank-menu (copy-sequence yank-menu))
670 (menu-bar-update-yank-menu "(any string)" nil))
671 (setq key (read-key-sequence "Describe key (or click or menu item): "))
672 (list
674 (prefix-numeric-value current-prefix-arg)
675 ;; If KEY is a down-event, read and include the
676 ;; corresponding up-event. Note that there are also
677 ;; down-events on scroll bars and mode lines: the actual
678 ;; event then is in the second element of the vector.
679 (and (vectorp key)
680 (let ((last-idx (1- (length key))))
681 (and (eventp (aref key last-idx))
682 (memq 'down (event-modifiers (aref key last-idx)))))
683 (or (and (eventp (aref key 0))
684 (memq 'down (event-modifiers (aref key 0)))
685 ;; However, for the C-down-mouse-2 popup
686 ;; menu, there is no subsequent up-event. In
687 ;; this case, the up-event is the next
688 ;; element in the supplied vector.
689 (= (length key) 1))
690 (and (> (length key) 1)
691 (eventp (aref key 1))
692 (memq 'down (event-modifiers (aref key 1)))))
693 (read-event))))
694 ;; Put yank-menu back as it was, if we changed it.
695 (when saved-yank-menu
696 (setq yank-menu (copy-sequence saved-yank-menu))
697 (fset 'yank-menu (cons 'keymap yank-menu))))))
698 (if (numberp untranslated)
699 (setq untranslated (this-single-command-raw-keys)))
700 (let* ((event (aref key (if (and (symbolp (aref key 0))
701 (> (length key) 1)
702 (consp (aref key 1)))
704 0)))
705 (modifiers (event-modifiers event))
706 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
707 (memq 'drag modifiers)) " at that spot" ""))
708 (defn (key-binding key t))
709 defn-up defn-up-tricky ev-type
710 mouse-1-remapped mouse-1-tricky)
712 ;; Handle the case where we faked an entry in "Select and Paste" menu.
713 (when (and (eq defn nil)
714 (stringp (aref key (1- (length key))))
715 (eq (key-binding (substring key 0 -1)) 'yank-menu))
716 (setq defn 'menu-bar-select-yank))
717 (if (or (null defn) (integerp defn) (equal defn 'undefined))
718 (message "%s%s is undefined"
719 (help-key-description key untranslated) mouse-msg)
720 (help-setup-xref (list #'describe-function defn) (interactive-p))
721 ;; Don't bother user with strings from (e.g.) the select-paste menu.
722 (when (stringp (aref key (1- (length key))))
723 (aset key (1- (length key)) "(any string)"))
724 (when (and untranslated
725 (stringp (aref untranslated (1- (length untranslated)))))
726 (aset untranslated (1- (length untranslated))
727 "(any string)"))
728 ;; Need to do this before erasing *Help* buffer in case event
729 ;; is a mouse click in an existing *Help* buffer.
730 (when up-event
731 (setq ev-type (event-basic-type up-event))
732 (let ((sequence (vector up-event)))
733 (when (and (eq ev-type 'mouse-1)
734 mouse-1-click-follows-link
735 (not (eq mouse-1-click-follows-link 'double))
736 (setq mouse-1-remapped
737 (mouse-on-link-p (event-start up-event))))
738 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
739 (> mouse-1-click-follows-link 0)))
740 (cond ((stringp mouse-1-remapped)
741 (setq sequence mouse-1-remapped))
742 ((vectorp mouse-1-remapped)
743 (setcar up-event (elt mouse-1-remapped 0)))
744 (t (setcar up-event 'mouse-2))))
745 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
746 (when mouse-1-tricky
747 (setq sequence (vector up-event))
748 (aset sequence 0 'mouse-1)
749 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
750 (with-help-window (help-buffer)
751 (princ (help-key-description key untranslated))
752 (princ (format "\
753 %s runs the command %S, which is "
754 mouse-msg defn))
755 (describe-function-1 defn)
756 (when up-event
757 (unless (or (null defn-up)
758 (integerp defn-up)
759 (equal defn-up 'undefined))
760 (princ (format "
762 ----------------- up-event %s----------------
764 <%S>%s%s runs the command %S, which is "
765 (if mouse-1-tricky "(short click) " "")
766 ev-type mouse-msg
767 (if mouse-1-remapped
768 " is remapped to <mouse-2>, which" "")
769 defn-up))
770 (describe-function-1 defn-up))
771 (unless (or (null defn-up-tricky)
772 (integerp defn-up-tricky)
773 (eq defn-up-tricky 'undefined))
774 (princ (format "
776 ----------------- up-event (long click) ----------------
778 Pressing <%S>%s for longer than %d milli-seconds
779 runs the command %S, which is "
780 ev-type mouse-msg
781 mouse-1-click-follows-link
782 defn-up-tricky))
783 (describe-function-1 defn-up-tricky)))))))
785 (defun describe-mode (&optional buffer)
786 "Display documentation of current major mode and minor modes.
787 A brief summary of the minor modes comes first, followed by the
788 major mode description. This is followed by detailed
789 descriptions of the minor modes, each on a separate page.
791 For this to work correctly for a minor mode, the mode's indicator
792 variable \(listed in `minor-mode-alist') must also be a function
793 whose documentation describes the minor mode."
794 (interactive "@")
795 (unless buffer (setq buffer (current-buffer)))
796 (help-setup-xref (list #'describe-mode buffer)
797 (interactive-p))
798 ;; For the sake of help-do-xref and help-xref-go-back,
799 ;; don't switch buffers before calling `help-buffer'.
800 (with-help-window (help-buffer)
801 (with-current-buffer buffer
802 (let (minor-modes)
803 ;; Older packages do not register in minor-mode-list but only in
804 ;; minor-mode-alist.
805 (dolist (x minor-mode-alist)
806 (setq x (car x))
807 (unless (memq x minor-mode-list)
808 (push x minor-mode-list)))
809 ;; Find enabled minor mode we will want to mention.
810 (dolist (mode minor-mode-list)
811 ;; Document a minor mode if it is listed in minor-mode-alist,
812 ;; non-nil, and has a function definition.
813 (let ((fmode (or (get mode :minor-mode-function) mode)))
814 (and (boundp mode) (symbol-value mode)
815 (fboundp fmode)
816 (let ((pretty-minor-mode
817 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
818 (symbol-name fmode))
819 (capitalize
820 (substring (symbol-name fmode)
821 0 (match-beginning 0)))
822 fmode)))
823 (push (list fmode pretty-minor-mode
824 (format-mode-line (assq mode minor-mode-alist)))
825 minor-modes)))))
826 (setq minor-modes
827 (sort minor-modes
828 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
829 (when minor-modes
830 (princ "Enabled minor modes:\n")
831 (make-local-variable 'help-button-cache)
832 (with-current-buffer standard-output
833 (dolist (mode minor-modes)
834 (let ((mode-function (nth 0 mode))
835 (pretty-minor-mode (nth 1 mode))
836 (indicator (nth 2 mode)))
837 (add-text-properties 0 (length pretty-minor-mode)
838 '(face bold) pretty-minor-mode)
839 (save-excursion
840 (goto-char (point-max))
841 (princ "\n\f\n")
842 (push (point-marker) help-button-cache)
843 ;; Document the minor modes fully.
844 (insert pretty-minor-mode)
845 (princ (format " minor mode (%s):\n"
846 (if (zerop (length indicator))
847 "no indicator"
848 (format "indicator%s"
849 indicator))))
850 (princ (documentation mode-function)))
851 (insert-button pretty-minor-mode
852 'action (car help-button-cache)
853 'follow-link t
854 'help-echo "mouse-2, RET: show full information")
855 (newline)))
856 (forward-line -1)
857 (fill-paragraph nil)
858 (forward-line 1))
860 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
861 ;; Document the major mode.
862 (let ((mode mode-name))
863 (with-current-buffer standard-output
864 (let ((start (point)))
865 (insert (format-mode-line mode nil nil buffer))
866 (add-text-properties start (point) '(face bold)))))
867 (princ " mode:\n")
868 (princ (documentation major-mode))))))
871 (defun describe-minor-mode (minor-mode)
872 "Display documentation of a minor mode given as MINOR-MODE.
873 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
874 appeared on the mode-line."
875 (interactive (list (completing-read
876 "Minor mode: "
877 (nconc
878 (describe-minor-mode-completion-table-for-symbol)
879 (describe-minor-mode-completion-table-for-indicator)
880 ))))
881 (if (symbolp minor-mode)
882 (setq minor-mode (symbol-name minor-mode)))
883 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
884 (indicators (describe-minor-mode-completion-table-for-indicator)))
885 (cond
886 ((member minor-mode symbols)
887 (describe-minor-mode-from-symbol (intern minor-mode)))
888 ((member minor-mode indicators)
889 (describe-minor-mode-from-indicator minor-mode))
891 (error "No such minor mode: %s" minor-mode)))))
893 ;; symbol
894 (defun describe-minor-mode-completion-table-for-symbol ()
895 ;; In order to list up all minor modes, minor-mode-list
896 ;; is used here instead of minor-mode-alist.
897 (delq nil (mapcar 'symbol-name minor-mode-list)))
898 (defun describe-minor-mode-from-symbol (symbol)
899 "Display documentation of a minor mode given as a symbol, SYMBOL"
900 (interactive (list (intern (completing-read
901 "Minor mode symbol: "
902 (describe-minor-mode-completion-table-for-symbol)))))
903 (if (fboundp symbol)
904 (describe-function symbol)
905 (describe-variable symbol)))
907 ;; indicator
908 (defun describe-minor-mode-completion-table-for-indicator ()
909 (delq nil
910 (mapcar (lambda (x)
911 (let ((i (format-mode-line x)))
912 ;; remove first space if existed
913 (cond
914 ((= 0 (length i))
915 nil)
916 ((eq (aref i 0) ?\s)
917 (substring i 1))
919 i))))
920 minor-mode-alist)))
921 (defun describe-minor-mode-from-indicator (indicator)
922 "Display documentation of a minor mode specified by INDICATOR.
923 If you call this function interactively, you can give indicator which
924 is currently activated with completion."
925 (interactive (list
926 (completing-read
927 "Minor mode indicator: "
928 (describe-minor-mode-completion-table-for-indicator))))
929 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
930 (if minor-mode
931 (describe-minor-mode-from-symbol minor-mode)
932 (error "Cannot find minor mode for `%s'" indicator))))
934 (defun lookup-minor-mode-from-indicator (indicator)
935 "Return a minor mode symbol from its indicator on the modeline."
936 ;; remove first space if existed
937 (if (and (< 0 (length indicator))
938 (eq (aref indicator 0) ?\s))
939 (setq indicator (substring indicator 1)))
940 (let ((minor-modes minor-mode-alist)
941 result)
942 (while minor-modes
943 (let* ((minor-mode (car (car minor-modes)))
944 (anindicator (format-mode-line
945 (car (cdr (car minor-modes))))))
946 ;; remove first space if existed
947 (if (and (stringp anindicator)
948 (> (length anindicator) 0)
949 (eq (aref anindicator 0) ?\s))
950 (setq anindicator (substring anindicator 1)))
951 (if (equal indicator anindicator)
952 (setq result minor-mode
953 minor-modes nil)
954 (setq minor-modes (cdr minor-modes)))))
955 result))
958 ;;; Automatic resizing of temporary buffers.
960 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
961 "Maximum height of a window displaying a temporary buffer.
962 This is effective only when Temp Buffer Resize mode is enabled.
963 The value is the maximum height (in lines) which `resize-temp-buffer-window'
964 will give to a window displaying a temporary buffer.
965 It can also be a function to be called to choose the height for such a buffer.
966 It gets one argumemt, the buffer, and should return a positive integer."
967 :type '(choice integer function)
968 :group 'help
969 :version "20.4")
971 (define-minor-mode temp-buffer-resize-mode
972 "Toggle the mode which makes windows smaller for temporary buffers.
973 With prefix argument ARG, turn the resizing of windows displaying temporary
974 buffers on if ARG is positive or off otherwise.
975 This makes the window the right height for its contents, but never
976 more than `temp-buffer-max-height' nor less than `window-min-height'.
977 This applies to `help', `apropos' and `completion' buffers, and some others."
978 :global t :group 'help
979 (if temp-buffer-resize-mode
980 ;; `help-make-xrefs' may add a `back' button and thus increase the
981 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
982 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
983 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
985 (defun resize-temp-buffer-window ()
986 "Resize the selected window to fit its contents.
987 Will not make it higher than `temp-buffer-max-height' nor smaller than
988 `window-min-height'. Do nothing if it is the only window on its frame, if it
989 is not as wide as the frame or if some of the window's contents are scrolled
990 out of view."
991 (unless (or (one-window-p 'nomini)
992 (not (pos-visible-in-window-p (point-min)))
993 (not (window-full-width-p)))
994 (fit-window-to-buffer
995 (selected-window)
996 (if (functionp temp-buffer-max-height)
997 (funcall temp-buffer-max-height (current-buffer))
998 temp-buffer-max-height))))
1001 ;;; help-window
1003 (defcustom help-window-select 'other
1004 "Non-nil means select help window for viewing.
1005 Choices are:
1006 never (nil) Select help window only if there is no other window
1007 on its frame.
1008 other Select help window unless the selected window is the
1009 only other window on its frame.
1010 always (t) Always select the help window.
1012 This option has effect if and only if the help window was created
1013 by `with-help-window'"
1014 :type '(choice (const :tag "never (nil)" nil)
1015 (const :tag "other" other)
1016 (const :tag "always (t)" t))
1017 :group 'help
1018 :version "23.1")
1020 (defun help-window-display-message (quit-part window &optional other)
1021 "Display message telling how to quit and scroll help window.
1022 QUIT-PART is a string telling how to quit the help window WINDOW.
1023 Optional argument OTHER non-nil means return text telling how to
1024 scroll the \"other\" window."
1025 (let ((scroll-part
1026 (cond
1027 ((pos-visible-in-window-p
1028 (with-current-buffer (window-buffer window)
1029 (point-max)) window)
1030 ;; Buffer end is visible.
1031 ".")
1032 (other ", \\[scroll-other-window] to scroll help.")
1033 (t ", \\[scroll-up] to scroll help."))))
1034 (message "%s"
1035 (substitute-command-keys (concat quit-part scroll-part)))))
1037 (defun help-window-setup-finish (window &optional reuse keep-frame)
1038 "Finish setting up help window WINDOW.
1039 Select WINDOW according to the value of `help-window-select'.
1040 Display message telling how to scroll and eventually quit WINDOW.
1042 Optional argument REUSE non-nil means WINDOW has been reused \(by
1043 `display-buffer'\) for displaying help. Optional argument
1044 KEEP-FRAME non-nil means that quitting must no delete the frame
1045 of WINDOW."
1046 (let ((number-of-windows
1047 (length (window-list (window-frame window) 'no-mini window))))
1048 (cond
1049 ((eq window (selected-window))
1050 ;; The help window is the selected window, probably the
1051 ;; `pop-up-windows' nil case.
1052 (help-window-display-message
1053 (if reuse
1054 "Type \"q\" to restore this window"
1055 ;; This should not be taken.
1056 "Type \"q\" to quit") window))
1057 ((= number-of-windows 1)
1058 ;; The help window is alone on a frame and not the selected
1059 ;; window, could be the `pop-up-frames' t case.
1060 (help-window-display-message
1061 (cond
1062 (keep-frame "Type \"q\" to delete this window")
1063 (reuse "Type \"q\" to restore this window")
1064 (view-remove-frame-by-deleting "Type \"q\" to delete this frame")
1065 (t "Type \"q\" to iconify this frame"))
1066 window))
1067 ((and (= number-of-windows 2)
1068 (eq (window-frame window) (window-frame (selected-window))))
1069 ;; There are two windows on the help window's frame and the other
1070 ;; window is the selected one.
1071 (if (memq help-window-select '(nil other))
1072 ;; Do not select the help window.
1073 (help-window-display-message
1074 (if reuse
1075 ;; Offer `display-buffer' for consistency with
1076 ;; `print-help-return-message'. This is hardly TRT when
1077 ;; the other window and the selected window display the
1078 ;; same buffer but has been handled this way ever since.
1079 "Type \\[display-buffer] RET to restore the other window"
1080 ;; The classic "two windows" configuration.
1081 "Type \\[delete-other-windows] to delete the help window")
1082 window t)
1083 ;; Select help window and tell how to quit.
1084 (select-window window)
1085 (help-window-display-message
1086 (if reuse
1087 "Type \"q\" to restore this window"
1088 "Type \"q\" to delete this window") window)))
1089 (help-window-select
1090 ;; Issuing a message with 3 or more windows on the same frame
1091 ;; without selecting the help window doesn't make any sense.
1092 (select-window window)
1093 (help-window-display-message
1094 (if reuse
1095 "Type \"q\" to restore this window"
1096 "Type \"q\" to delete this window") window)))))
1098 (defun help-window-setup (list-of-frames list-of-window-tuples)
1099 "Set up help window.
1100 LIST-OF-FRAMES and LIST-OF-WINDOW-TUPLES are the lists of frames
1101 and window quadruples built by `with-help-window'. The help
1102 window itself is specified by the variable `help-window'."
1103 (let* ((help-buffer (window-buffer help-window))
1104 ;; `help-buffer' now denotes the help window's buffer.
1105 (view-entry
1106 (assq help-window
1107 (buffer-local-value 'view-return-to-alist help-buffer)))
1108 (help-entry (assq help-window list-of-window-tuples)))
1110 ;; Handle `help-window-point-marker'.
1111 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1112 (set-window-point help-window help-window-point-marker)
1113 ;; Reset `help-window-point-marker'.
1114 (set-marker help-window-point-marker nil))
1116 (cond
1117 (view-entry
1118 ;; `view-return-to-alist' has an entry for the help window.
1119 (cond
1120 ((eq help-window (selected-window))
1121 ;; The help window is the selected window, probably because the
1122 ;; user followed a backward/forward button or a cross reference.
1123 ;; In this case just purge stale entries from
1124 ;; `view-return-to-alist' but leave the entry alone and don't
1125 ;; display a message.
1126 (view-return-to-alist-update help-buffer))
1127 ((and help-entry (eq (cadr help-entry) help-buffer))
1128 ;; The help window was not selected but displayed the help
1129 ;; buffer. In this case reuse existing exit information but try
1130 ;; to get back to the selected window when quitting. Don't
1131 ;; display a message since the user must have seen one before.
1132 (view-return-to-alist-update
1133 help-buffer (cons help-window
1134 (cons (selected-window) (cddr view-entry)))))
1135 (help-entry
1136 ;; The help window was not selected, did display the help buffer
1137 ;; earlier, but displayed another buffer when help was invoked.
1138 ;; Set up things so that quitting will show that buffer again.
1139 (view-return-to-alist-update
1140 help-buffer (cons help-window
1141 (cons (selected-window) (cdr help-entry))))
1142 (help-window-setup-finish help-window t))
1144 ;; The help window is new but `view-return-to-alist' had an
1145 ;; entry for it. This should never happen.
1146 (view-return-to-alist-update
1147 help-buffer (cons help-window
1148 (cons (selected-window) 'quit-window)))
1149 (help-window-setup-finish help-window t))))
1150 (help-entry
1151 ;; `view-return-to-alist' does not have an entry for help window
1152 ;; but `list-of-window-tuples' does. Hence `display-buffer' must
1153 ;; have reused an existing window.
1154 (if (eq (cadr help-entry) help-buffer)
1155 ;; The help window displayed `help-buffer' before but no
1156 ;; `view-return-to-alist' entry was found probably because the
1157 ;; user manually switched to the help buffer. Set up things
1158 ;; for `quit-window' although `view-exit-action' should be
1159 ;; able to handle this case all by itself.
1160 (progn
1161 (view-return-to-alist-update
1162 help-buffer (cons help-window
1163 (cons (selected-window) 'quit-window)))
1164 (help-window-setup-finish help-window t))
1165 ;; The help window displayed another buffer before. Set up
1166 ;; things in a way that quitting can orderly show that buffer
1167 ;; again. The window-start and window-point information from
1168 ;; `list-of-window-tuples' provide the necessary information.
1169 (view-return-to-alist-update
1170 help-buffer (cons help-window
1171 (cons (selected-window) (cdr help-entry))))
1172 (help-window-setup-finish help-window t)))
1173 ((memq (window-frame help-window) list-of-frames)
1174 ;; The help window is a new window on an existing frame. This
1175 ;; case must be handled specially by `help-window-setup-finish'
1176 ;; and `view-mode-exit' to ascertain that quitting does _not_
1177 ;; inadvertently delete the frame.
1178 (view-return-to-alist-update
1179 help-buffer (cons help-window
1180 (cons (selected-window) 'keep-frame)))
1181 (help-window-setup-finish help-window nil t))
1183 ;; The help window is shown on a new frame. In this case quitting
1184 ;; shall handle both, the help window _and_ its frame. We changed
1185 ;; the default of `view-remove-frame-by-deleting' to t in order to
1186 ;; intuitively DTRT here.
1187 (view-return-to-alist-update
1188 help-buffer (cons help-window (cons (selected-window) t)))
1189 (help-window-setup-finish help-window)))))
1191 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1192 ;; providing the following additional twists:
1194 ;; (1) Issue more accurate messages telling how to scroll and quit the
1195 ;; help window.
1197 ;; (2) Make `view-mode-exit' DTRT in more cases.
1199 ;; (3) An option (customizable via `help-window-select') to select the
1200 ;; help window automatically.
1202 ;; (4) A marker (`help-window-point-marker') to move point in the help
1203 ;; window to an arbitrary buffer position.
1205 ;; Note: It's usually always wrong to use `print-help-return-message' in
1206 ;; the body of `with-help-window'.
1207 (defmacro with-help-window (buffer-name &rest body)
1208 "Display buffer BUFFER-NAME in a help window evaluating BODY.
1209 Select help window if the actual value of the user option
1210 `help-window-select' says so. Return last value in BODY."
1211 (declare (indent 1) (debug t))
1212 ;; Bind list-of-frames to `frame-list' and list-of-window-tuples to a
1213 ;; list of one <window window-buffer window-start window-point> tuple
1214 ;; for each live window.
1215 `(let ((list-of-frames (frame-list))
1216 (list-of-window-tuples
1217 (let (list)
1218 (walk-windows
1219 (lambda (window)
1220 (push (list window (window-buffer window)
1221 (window-start window) (window-point window))
1222 list))
1223 'no-mini t)
1224 list)))
1225 ;; Make `help-window' t to trigger `help-mode-finish' to set
1226 ;; `help-window' to the actual help window.
1227 (setq help-window t)
1228 ;; Make `help-window-point-marker' point nowhere (the only place
1229 ;; where this should be set to a buffer position is within BODY).
1230 (set-marker help-window-point-marker nil)
1231 (prog1
1232 ;; Return value returned by `with-output-to-temp-buffer'.
1233 (with-output-to-temp-buffer ,buffer-name
1234 (progn ,@body))
1235 (when (windowp help-window)
1236 ;; Set up help window.
1237 (help-window-setup list-of-frames list-of-window-tuples))
1238 ;; Reset `help-window' to nil to avoid confusing future calls of
1239 ;; `help-mode-finish' with plain `with-output-to-temp-buffer'.
1240 (setq help-window nil))))
1242 (provide 'help)
1244 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1245 ;;; help.el ends here