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