* s/netbsd.h (AMPERSAND_FULL_NAME): Remove (defined in AH_BOTTOM).
[emacs.git] / lisp / help.el
blob899547aa0a17c242c1b8d935d3a70d5e56847204
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 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 `help-print-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 (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 ev-type mouse-msg
774 (if mouse-1-remapped
775 " is remapped to <mouse-2>, which" "")
776 defn-up))
777 (describe-function-1 defn-up))
778 (unless (or (null defn-up-tricky)
779 (integerp defn-up-tricky)
780 (eq defn-up-tricky 'undefined))
781 (princ (format "
783 ----------------- up-event (long click) ----------------
785 Pressing <%S>%s for longer than %d milli-seconds
786 runs the command %S, which is "
787 ev-type mouse-msg
788 mouse-1-click-follows-link
789 defn-up-tricky))
790 (describe-function-1 defn-up-tricky)))))))
792 (defun describe-mode (&optional buffer)
793 "Display documentation of current major mode and minor modes.
794 A brief summary of the minor modes comes first, followed by the
795 major mode description. This is followed by detailed
796 descriptions of the minor modes, each on a separate page.
798 For this to work correctly for a minor mode, the mode's indicator
799 variable \(listed in `minor-mode-alist') must also be a function
800 whose documentation describes the minor mode."
801 (interactive "@")
802 (unless buffer (setq buffer (current-buffer)))
803 (help-setup-xref (list #'describe-mode buffer)
804 (called-interactively-p 'interactive))
805 ;; For the sake of help-do-xref and help-xref-go-back,
806 ;; don't switch buffers before calling `help-buffer'.
807 (with-help-window (help-buffer)
808 (with-current-buffer buffer
809 (let (minor-modes)
810 ;; Older packages do not register in minor-mode-list but only in
811 ;; minor-mode-alist.
812 (dolist (x minor-mode-alist)
813 (setq x (car x))
814 (unless (memq x minor-mode-list)
815 (push x minor-mode-list)))
816 ;; Find enabled minor mode we will want to mention.
817 (dolist (mode minor-mode-list)
818 ;; Document a minor mode if it is listed in minor-mode-alist,
819 ;; non-nil, and has a function definition.
820 (let ((fmode (or (get mode :minor-mode-function) mode)))
821 (and (boundp mode) (symbol-value mode)
822 (fboundp fmode)
823 (let ((pretty-minor-mode
824 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
825 (symbol-name fmode))
826 (capitalize
827 (substring (symbol-name fmode)
828 0 (match-beginning 0)))
829 fmode)))
830 (push (list fmode pretty-minor-mode
831 (format-mode-line (assq mode minor-mode-alist)))
832 minor-modes)))))
833 (setq minor-modes
834 (sort minor-modes
835 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
836 (when minor-modes
837 (princ "Enabled minor modes:\n")
838 (make-local-variable 'help-button-cache)
839 (with-current-buffer standard-output
840 (dolist (mode minor-modes)
841 (let ((mode-function (nth 0 mode))
842 (pretty-minor-mode (nth 1 mode))
843 (indicator (nth 2 mode)))
844 (add-text-properties 0 (length pretty-minor-mode)
845 '(face bold) pretty-minor-mode)
846 (save-excursion
847 (goto-char (point-max))
848 (princ "\n\f\n")
849 (push (point-marker) help-button-cache)
850 ;; Document the minor modes fully.
851 (insert pretty-minor-mode)
852 (princ (format " minor mode (%s):\n"
853 (if (zerop (length indicator))
854 "no indicator"
855 (format "indicator%s"
856 indicator))))
857 (princ (documentation mode-function)))
858 (insert-button pretty-minor-mode
859 'action (car help-button-cache)
860 'follow-link t
861 'help-echo "mouse-2, RET: show full information")
862 (newline)))
863 (forward-line -1)
864 (fill-paragraph nil)
865 (forward-line 1))
867 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
868 ;; Document the major mode.
869 (let ((mode mode-name))
870 (with-current-buffer standard-output
871 (let ((start (point)))
872 (insert (format-mode-line mode nil nil buffer))
873 (add-text-properties start (point) '(face bold)))))
874 (princ " mode:\n")
875 (princ (documentation major-mode)))))
876 ;; For the sake of IELM and maybe others
877 nil)
880 (defun describe-minor-mode (minor-mode)
881 "Display documentation of a minor mode given as MINOR-MODE.
882 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
883 appeared on the mode-line."
884 (interactive (list (completing-read
885 "Minor mode: "
886 (nconc
887 (describe-minor-mode-completion-table-for-symbol)
888 (describe-minor-mode-completion-table-for-indicator)
889 ))))
890 (if (symbolp minor-mode)
891 (setq minor-mode (symbol-name minor-mode)))
892 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
893 (indicators (describe-minor-mode-completion-table-for-indicator)))
894 (cond
895 ((member minor-mode symbols)
896 (describe-minor-mode-from-symbol (intern minor-mode)))
897 ((member minor-mode indicators)
898 (describe-minor-mode-from-indicator minor-mode))
900 (error "No such minor mode: %s" minor-mode)))))
902 ;; symbol
903 (defun describe-minor-mode-completion-table-for-symbol ()
904 ;; In order to list up all minor modes, minor-mode-list
905 ;; is used here instead of minor-mode-alist.
906 (delq nil (mapcar 'symbol-name minor-mode-list)))
907 (defun describe-minor-mode-from-symbol (symbol)
908 "Display documentation of a minor mode given as a symbol, SYMBOL"
909 (interactive (list (intern (completing-read
910 "Minor mode symbol: "
911 (describe-minor-mode-completion-table-for-symbol)))))
912 (if (fboundp symbol)
913 (describe-function symbol)
914 (describe-variable symbol)))
916 ;; indicator
917 (defun describe-minor-mode-completion-table-for-indicator ()
918 (delq nil
919 (mapcar (lambda (x)
920 (let ((i (format-mode-line x)))
921 ;; remove first space if existed
922 (cond
923 ((= 0 (length i))
924 nil)
925 ((eq (aref i 0) ?\s)
926 (substring i 1))
928 i))))
929 minor-mode-alist)))
930 (defun describe-minor-mode-from-indicator (indicator)
931 "Display documentation of a minor mode specified by INDICATOR.
932 If you call this function interactively, you can give indicator which
933 is currently activated with completion."
934 (interactive (list
935 (completing-read
936 "Minor mode indicator: "
937 (describe-minor-mode-completion-table-for-indicator))))
938 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
939 (if minor-mode
940 (describe-minor-mode-from-symbol minor-mode)
941 (error "Cannot find minor mode for `%s'" indicator))))
943 (defun lookup-minor-mode-from-indicator (indicator)
944 "Return a minor mode symbol from its indicator on the modeline."
945 ;; remove first space if existed
946 (if (and (< 0 (length indicator))
947 (eq (aref indicator 0) ?\s))
948 (setq indicator (substring indicator 1)))
949 (let ((minor-modes minor-mode-alist)
950 result)
951 (while minor-modes
952 (let* ((minor-mode (car (car minor-modes)))
953 (anindicator (format-mode-line
954 (car (cdr (car minor-modes))))))
955 ;; remove first space if existed
956 (if (and (stringp anindicator)
957 (> (length anindicator) 0)
958 (eq (aref anindicator 0) ?\s))
959 (setq anindicator (substring anindicator 1)))
960 (if (equal indicator anindicator)
961 (setq result minor-mode
962 minor-modes nil)
963 (setq minor-modes (cdr minor-modes)))))
964 result))
967 ;;; Automatic resizing of temporary buffers.
969 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
970 "Maximum height of a window displaying a temporary buffer.
971 This is effective only when Temp Buffer Resize mode is enabled.
972 The value is the maximum height (in lines) which `resize-temp-buffer-window'
973 will give to a window displaying a temporary buffer.
974 It can also be a function to be called to choose the height for such a buffer.
975 It gets one argumemt, the buffer, and should return a positive integer."
976 :type '(choice integer function)
977 :group 'help
978 :version "20.4")
980 (define-minor-mode temp-buffer-resize-mode
981 "Toggle the mode which makes windows smaller for temporary buffers.
982 With prefix argument ARG, turn the resizing of windows displaying temporary
983 buffers on if ARG is positive or off otherwise.
984 This makes the window the right height for its contents, but never
985 more than `temp-buffer-max-height' nor less than `window-min-height'.
986 This applies to `help', `apropos' and `completion' buffers, and some others."
987 :global t :group 'help
988 (if temp-buffer-resize-mode
989 ;; `help-make-xrefs' may add a `back' button and thus increase the
990 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
991 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
992 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
994 (defun resize-temp-buffer-window ()
995 "Resize the selected window to fit its contents.
996 Will not make it higher than `temp-buffer-max-height' nor smaller than
997 `window-min-height'. Do nothing if it is the only window on its frame, if it
998 is not as wide as the frame or if some of the window's contents are scrolled
999 out of view."
1000 (unless (or (one-window-p 'nomini)
1001 (not (pos-visible-in-window-p (point-min)))
1002 (not (window-full-width-p)))
1003 (fit-window-to-buffer
1004 (selected-window)
1005 (if (functionp temp-buffer-max-height)
1006 (funcall temp-buffer-max-height (current-buffer))
1007 temp-buffer-max-height))))
1010 ;;; help-window
1012 (defcustom help-window-select 'other
1013 "Non-nil means select help window for viewing.
1014 Choices are:
1015 never (nil) Select help window only if there is no other window
1016 on its frame.
1017 other Select help window unless the selected window is the
1018 only other window on its frame.
1019 always (t) Always select the help window.
1021 This option has effect if and only if the help window was created
1022 by `with-help-window'"
1023 :type '(choice (const :tag "never (nil)" nil)
1024 (const :tag "other" other)
1025 (const :tag "always (t)" t))
1026 :group 'help
1027 :version "23.1")
1029 (defun help-window-display-message (quit-part window &optional other)
1030 "Display message telling how to quit and scroll help window.
1031 QUIT-PART is a string telling how to quit the help window WINDOW.
1032 Optional argument OTHER non-nil means return text telling how to
1033 scroll the \"other\" window."
1034 (let ((scroll-part
1035 (cond
1036 ((pos-visible-in-window-p
1037 (with-current-buffer (window-buffer window)
1038 (point-max)) window)
1039 ;; Buffer end is visible.
1040 ".")
1041 (other ", \\[scroll-other-window] to scroll help.")
1042 (t ", \\[scroll-up] to scroll help."))))
1043 (message "%s"
1044 (substitute-command-keys (concat quit-part scroll-part)))))
1046 (defun help-window-setup-finish (window &optional reuse keep-frame)
1047 "Finish setting up help window WINDOW.
1048 Select WINDOW according to the value of `help-window-select'.
1049 Display message telling how to scroll and eventually quit WINDOW.
1051 Optional argument REUSE non-nil means WINDOW has been reused by
1052 `display-buffer'. Optional argument KEEP-FRAME non-nil means
1053 that quitting should not delete WINDOW's frame."
1054 (let ((number-of-windows
1055 (length (window-list (window-frame window) 'no-mini window))))
1056 (cond
1057 ((eq window (selected-window))
1058 ;; The help window is the selected window, probably the
1059 ;; `pop-up-windows' nil case.
1060 (help-window-display-message
1061 (if reuse
1062 "Type \"q\" to restore this window"
1063 ;; This should not be taken.
1064 "Type \"q\" to quit") window))
1065 ((= number-of-windows 1)
1066 ;; The help window is alone on a frame and not the selected
1067 ;; window, could be the `pop-up-frames' t case.
1068 (help-window-display-message
1069 (cond
1070 (keep-frame "Type \"q\" to delete this window")
1071 (reuse "Type \"q\" to restore this window")
1072 (view-remove-frame-by-deleting "Type \"q\" to delete this frame")
1073 (t "Type \"q\" to iconify this frame"))
1074 window))
1075 ((and (= number-of-windows 2)
1076 (eq (window-frame window) (window-frame (selected-window))))
1077 ;; There are two windows on the help window's frame and the other
1078 ;; window is the selected one.
1079 (if (memq help-window-select '(nil other))
1080 ;; Do not select the help window.
1081 (help-window-display-message
1082 (if reuse
1083 ;; Offer `display-buffer' for consistency with
1084 ;; `help-print-return-message'. This is hardly TRT when
1085 ;; the other window and the selected window display the
1086 ;; same buffer but has been handled this way ever since.
1087 "Type \\[display-buffer] RET to restore the other window"
1088 ;; The classic "two windows" configuration.
1089 "Type \\[delete-other-windows] to delete the help window")
1090 window t)
1091 ;; Select help window and tell how to quit.
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)))
1097 (help-window-select
1098 ;; Issuing a message with 3 or more windows on the same frame
1099 ;; without selecting the help window doesn't make any sense.
1100 (select-window window)
1101 (help-window-display-message
1102 (if reuse
1103 "Type \"q\" to restore this window"
1104 "Type \"q\" to delete this window") window)))))
1106 (defun help-window-setup (list-of-frames list-of-window-tuples)
1107 "Set up help window.
1108 LIST-OF-FRAMES and LIST-OF-WINDOW-TUPLES are the lists of frames
1109 and window quadruples built by `with-help-window'. The help
1110 window itself is specified by the variable `help-window'."
1111 (let* ((help-buffer (window-buffer help-window))
1112 ;; `help-buffer' now denotes the help window's buffer.
1113 (view-entry
1114 (assq help-window
1115 (buffer-local-value 'view-return-to-alist help-buffer)))
1116 (help-entry (assq help-window list-of-window-tuples)))
1118 ;; Handle `help-window-point-marker'.
1119 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1120 (set-window-point help-window help-window-point-marker)
1121 ;; Reset `help-window-point-marker'.
1122 (set-marker help-window-point-marker nil))
1124 (cond
1125 (view-entry
1126 ;; `view-return-to-alist' has an entry for the help window.
1127 (cond
1128 ((eq help-window (selected-window))
1129 ;; The help window is the selected window, probably because the
1130 ;; user followed a backward/forward button or a cross reference.
1131 ;; In this case just purge stale entries from
1132 ;; `view-return-to-alist' but leave the entry alone and don't
1133 ;; display a message.
1134 (view-return-to-alist-update help-buffer))
1135 ((and help-entry (eq (cadr help-entry) help-buffer))
1136 ;; The help window was not selected but displayed the help
1137 ;; buffer. In this case reuse existing exit information but try
1138 ;; to get back to the selected window when quitting. Don't
1139 ;; display a message since the user must have seen one before.
1140 (view-return-to-alist-update
1141 help-buffer (cons help-window
1142 (cons (selected-window) (cddr view-entry)))))
1143 (help-entry
1144 ;; The help window was not selected, did display the help buffer
1145 ;; earlier, but displayed another buffer when help was invoked.
1146 ;; Set up things so that quitting will show that buffer again.
1147 (view-return-to-alist-update
1148 help-buffer (cons help-window
1149 (cons (selected-window) (cdr help-entry))))
1150 (help-window-setup-finish help-window t))
1152 ;; The help window is new but `view-return-to-alist' had an
1153 ;; entry for it. This should never happen.
1154 (view-return-to-alist-update
1155 help-buffer (cons help-window
1156 (cons (selected-window) 'quit-window)))
1157 (help-window-setup-finish help-window t))))
1158 (help-entry
1159 ;; `view-return-to-alist' does not have an entry for help window
1160 ;; but `list-of-window-tuples' does. Hence `display-buffer' must
1161 ;; have reused an existing window.
1162 (if (eq (cadr help-entry) help-buffer)
1163 ;; The help window displayed `help-buffer' before but no
1164 ;; `view-return-to-alist' entry was found probably because the
1165 ;; user manually switched to the help buffer. Set up things
1166 ;; for `quit-window' although `view-exit-action' should be
1167 ;; able to handle this case all by itself.
1168 (progn
1169 (view-return-to-alist-update
1170 help-buffer (cons help-window
1171 (cons (selected-window) 'quit-window)))
1172 (help-window-setup-finish help-window t))
1173 ;; The help window displayed another buffer before. Set up
1174 ;; things in a way that quitting can orderly show that buffer
1175 ;; again. The window-start and window-point information from
1176 ;; `list-of-window-tuples' provide the necessary information.
1177 (view-return-to-alist-update
1178 help-buffer (cons help-window
1179 (cons (selected-window) (cdr help-entry))))
1180 (help-window-setup-finish help-window t)))
1181 ((memq (window-frame help-window) list-of-frames)
1182 ;; The help window is a new window on an existing frame. This
1183 ;; case must be handled specially by `help-window-setup-finish'
1184 ;; and `view-mode-exit' to ascertain that quitting does _not_
1185 ;; inadvertently delete the frame.
1186 (view-return-to-alist-update
1187 help-buffer (cons help-window
1188 (cons (selected-window) 'keep-frame)))
1189 (help-window-setup-finish help-window nil t))
1191 ;; The help window is shown on a new frame. In this case quitting
1192 ;; shall handle both, the help window _and_ its frame. We changed
1193 ;; the default of `view-remove-frame-by-deleting' to t in order to
1194 ;; intuitively DTRT here.
1195 (view-return-to-alist-update
1196 help-buffer (cons help-window (cons (selected-window) t)))
1197 (help-window-setup-finish help-window)))))
1199 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1200 ;; providing the following additional twists:
1202 ;; (1) Issue more accurate messages telling how to scroll and quit the
1203 ;; help window.
1205 ;; (2) Make `view-mode-exit' DTRT in more cases.
1207 ;; (3) An option (customizable via `help-window-select') to select the
1208 ;; help window automatically.
1210 ;; (4) A marker (`help-window-point-marker') to move point in the help
1211 ;; window to an arbitrary buffer position.
1213 ;; Note: It's usually always wrong to use `help-print-return-message' in
1214 ;; the body of `with-help-window'.
1215 (defmacro with-help-window (buffer-name &rest body)
1216 "Display buffer BUFFER-NAME in a help window evaluating BODY.
1217 Select help window if the actual value of the user option
1218 `help-window-select' says so. Return last value in BODY."
1219 (declare (indent 1) (debug t))
1220 ;; Bind list-of-frames to `frame-list' and list-of-window-tuples to a
1221 ;; list of one <window window-buffer window-start window-point> tuple
1222 ;; for each live window.
1223 `(let ((list-of-frames (frame-list))
1224 (list-of-window-tuples
1225 (let (list)
1226 (walk-windows
1227 (lambda (window)
1228 (push (list window (window-buffer window)
1229 (window-start window) (window-point window))
1230 list))
1231 'no-mini t)
1232 list)))
1233 ;; Make `help-window' t to trigger `help-mode-finish' to set
1234 ;; `help-window' to the actual help window.
1235 (setq help-window t)
1236 ;; Make `help-window-point-marker' point nowhere (the only place
1237 ;; where this should be set to a buffer position is within BODY).
1238 (set-marker help-window-point-marker nil)
1239 (prog1
1240 ;; Return value returned by `with-output-to-temp-buffer'.
1241 (with-output-to-temp-buffer ,buffer-name
1242 (progn ,@body))
1243 (when (windowp help-window)
1244 ;; Set up help window.
1245 (help-window-setup list-of-frames list-of-window-tuples))
1246 ;; Reset `help-window' to nil to avoid confusing future calls of
1247 ;; `help-mode-finish' with plain `with-output-to-temp-buffer'.
1248 (setq help-window nil))))
1250 (provide 'help)
1252 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1253 ;;; help.el ends here