]> code.delx.au - gnu-emacs/blob - lisp/help.el
(byte-compile-insert-header): Drop test for Emacs 19. (Bug#1531)
[gnu-emacs] / lisp / help.el
1 ;;; help.el --- help commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This code implements GNU Emacs' on-line help system, the one invoked by
27 ;; `M-x help-for-help'.
28
29 ;;; Code:
30
31 ;; Get the macro make-help-screen when this is compiled,
32 ;; or run interpreted, but not when the compiled code is loaded.
33 (eval-when-compile (require 'help-macro))
34
35 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
36 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
37 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
38
39 ;; 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.")
49
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'.")
58
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)
66
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)
78
79 ;; This does not fit the pattern, but it is natural given the C-\ command.
80 (define-key map "\C-\\" 'describe-input-method)
81
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)
88
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)
97
98 (define-key map "i" 'info)
99 (define-key map "4i" 'info-other-window)
100
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.")
114
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)
119
120 (autoload 'finder-by-keyword "finder"
121 "Find packages matching a given keyword." t)
122
123 ;; insert-button makes the action nil if it is not store somewhere
124 (defvar help-button-cache nil)
125
126 \f
127 (defun help-quit ()
128 "Just exit from the Help command's command loop."
129 (interactive)
130 nil)
131
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.")
139
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.")
174 (t
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)))
188 nil
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."))))))))
196
197 ;; So keyboard macro definitions are documented correctly
198 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
199
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.)
211
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.
242
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)
255
256 \f
257
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))
290 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))))))))
295
296 \f
297 ;;; `User' help functions
298
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)))
303
304 (defun describe-distribution ()
305 "Display info on how to obtain the latest version of GNU Emacs."
306 (interactive)
307 (view-help-file "DISTRIB"))
308
309 (defun describe-copying ()
310 "Display info on how you may redistribute copies of GNU Emacs."
311 (interactive)
312 (view-help-file "COPYING"))
313
314 (defun describe-gnu-project ()
315 "Display info on the GNU project."
316 (interactive)
317 (view-help-file "THE-GNU-PROJECT"))
318
319 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
320
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)))
329
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)
348
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)))))))
418
419 (defun view-emacs-todo (&optional arg)
420 "Display the Emacs TODO list."
421 (interactive "P")
422 (view-help-file "TODO"))
423
424 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
425
426
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*")))
433
434 (defun view-order-manuals ()
435 "Display the Emacs ORDERS file."
436 (interactive)
437 (view-help-file "ORDERS"))
438
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)"))
444
445 (defun view-emacs-problems ()
446 "Display info on known problems with Emacs and possible workarounds."
447 (interactive)
448 (view-help-file "PROBLEMS"))
449
450 (defun view-emacs-debugging ()
451 "Display info on how to debug Emacs problems."
452 (interactive)
453 (view-help-file "DEBUG"))
454
455 (defun view-external-packages ()
456 "Display external packages and information about Emacs."
457 (interactive)
458 (view-help-file "MORE.STUFF"))
459
460 (defun view-lossage ()
461 "Display last 300 input keystrokes.
462
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)))))
482
483 \f
484 ;; Key bindings
485
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.
489
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)))
501
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.
506
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 (interactive)
512 (let ((buf (current-buffer)))
513 (with-help-window "*Help*"
514 (with-current-buffer standard-output
515 (describe-buffer-bindings buf prefix menus)))))
516
517 (defun where-is (definition &optional insert)
518 "Print message listing key sequences that invoke the command DEFINITION.
519 Argument is a command definition, usually a symbol with a function definition.
520 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
521 (interactive
522 (let ((fn (function-called-at-point))
523 (enable-recursive-minibuffers t)
524 val)
525 (setq val (completing-read
526 (if fn
527 (format "Where is command (default %s): " fn)
528 "Where is command: ")
529 obarray 'commandp t))
530 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
531 (unless definition (error "No command"))
532 (let ((func (indirect-function definition))
533 (defs nil)
534 (standard-output (if insert (current-buffer) t)))
535 ;; In DEFS, find all symbols that are aliases for DEFINITION.
536 (mapatoms (lambda (symbol)
537 (and (fboundp symbol)
538 (not (eq symbol definition))
539 (eq func (condition-case ()
540 (indirect-function symbol)
541 (error symbol)))
542 (push symbol defs))))
543 ;; Look at all the symbols--first DEFINITION,
544 ;; then its aliases.
545 (dolist (symbol (cons definition defs))
546 (let* ((remapped (command-remapping symbol))
547 (keys (where-is-internal
548 symbol overriding-local-map nil nil remapped))
549 (keys (mapconcat 'key-description keys ", "))
550 string)
551 (setq string
552 (if insert
553 (if (> (length keys) 0)
554 (if remapped
555 (format "%s (%s) (remapped from %s)"
556 keys remapped symbol)
557 (format "%s (%s)" keys symbol))
558 (format "M-x %s RET" symbol))
559 (if (> (length keys) 0)
560 (if remapped
561 (format "%s is remapped to %s which is on %s"
562 symbol remapped keys)
563 (format "%s is on %s" symbol keys))
564 ;; If this is the command the user asked about,
565 ;; and it is not on any key, say so.
566 ;; For other symbols, its aliases, say nothing
567 ;; about them unless they are on keys.
568 (if (eq symbol definition)
569 (format "%s is not on any key" symbol)))))
570 (when string
571 (unless (eq symbol definition)
572 (princ ";\n its alias "))
573 (princ string)))))
574 nil)
575
576 (defun help-key-description (key untranslated)
577 (let ((string (key-description key)))
578 (if (or (not untranslated)
579 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
580 string
581 (let ((otherstring (key-description untranslated)))
582 (if (equal string otherstring)
583 string
584 (format "%s (translated from %s)" string otherstring))))))
585
586 (defun describe-key-briefly (&optional key insert untranslated)
587 "Print the name of the function KEY invokes. KEY is a string.
588 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
589 If non-nil, UNTRANSLATED is a vector of the untranslated events.
590 It can also be a number in which case the untranslated events from
591 the last key hit are used.
592
593 If KEY is a menu item or a tool-bar button that is disabled, this command
594 temporarily enables it to allow getting help on disabled items and buttons."
595 (interactive
596 (let ((enable-disabled-menus-and-buttons t)
597 (cursor-in-echo-area t)
598 saved-yank-menu)
599 (unwind-protect
600 (let (key)
601 ;; If yank-menu is empty, populate it temporarily, so that
602 ;; "Select and Paste" menu can generate a complete event.
603 (when (null (cdr yank-menu))
604 (setq saved-yank-menu (copy-sequence yank-menu))
605 (menu-bar-update-yank-menu "(any string)" nil))
606 (setq key (read-key-sequence "Describe key (or click or menu item): "))
607 ;; If KEY is a down-event, read and discard the
608 ;; corresponding up-event. Note that there are also
609 ;; down-events on scroll bars and mode lines: the actual
610 ;; event then is in the second element of the vector.
611 (and (vectorp key)
612 (let ((last-idx (1- (length key))))
613 (and (eventp (aref key last-idx))
614 (memq 'down (event-modifiers (aref key last-idx)))))
615 (read-event))
616 (list
617 key
618 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
619 1))
620 ;; Put yank-menu back as it was, if we changed it.
621 (when saved-yank-menu
622 (setq yank-menu (copy-sequence saved-yank-menu))
623 (fset 'yank-menu (cons 'keymap yank-menu))))))
624 (if (numberp untranslated)
625 (setq untranslated (this-single-command-raw-keys)))
626 (let* ((event (if (and (symbolp (aref key 0))
627 (> (length key) 1)
628 (consp (aref key 1)))
629 (aref key 1)
630 (aref key 0)))
631 (modifiers (event-modifiers event))
632 (standard-output (if insert (current-buffer) t))
633 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
634 (memq 'drag modifiers)) " at that spot" ""))
635 (defn (key-binding key t))
636 key-desc)
637 ;; Handle the case where we faked an entry in "Select and Paste" menu.
638 (if (and (eq defn nil)
639 (stringp (aref key (1- (length key))))
640 (eq (key-binding (substring key 0 -1)) 'yank-menu))
641 (setq defn 'menu-bar-select-yank))
642 ;; Don't bother user with strings from (e.g.) the select-paste menu.
643 (if (stringp (aref key (1- (length key))))
644 (aset key (1- (length key)) "(any string)"))
645 (if (and (> (length untranslated) 0)
646 (stringp (aref untranslated (1- (length untranslated)))))
647 (aset untranslated (1- (length untranslated)) "(any string)"))
648 ;; Now describe the key, perhaps as changed.
649 (setq key-desc (help-key-description key untranslated))
650 (if (or (null defn) (integerp defn) (equal defn 'undefined))
651 (princ (format "%s%s is undefined" key-desc mouse-msg))
652 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
653
654 (defun describe-key (&optional key untranslated up-event)
655 "Display documentation of the function invoked by KEY.
656 KEY can be any kind of a key sequence; it can include keyboard events,
657 mouse events, and/or menu events. When calling from a program,
658 pass KEY as a string or a vector.
659
660 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
661 It can also be a number, in which case the untranslated events from
662 the last key sequence entered are used.
663 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
664
665 If KEY is a menu item or a tool-bar button that is disabled, this command
666 temporarily enables it to allow getting help on disabled items and buttons."
667 (interactive
668 (let ((enable-disabled-menus-and-buttons t)
669 (cursor-in-echo-area t)
670 saved-yank-menu)
671 (unwind-protect
672 (let (key)
673 ;; If yank-menu is empty, populate it temporarily, so that
674 ;; "Select and Paste" menu can generate a complete event.
675 (when (null (cdr yank-menu))
676 (setq saved-yank-menu (copy-sequence yank-menu))
677 (menu-bar-update-yank-menu "(any string)" nil))
678 (setq key (read-key-sequence "Describe key (or click or menu item): "))
679 (list
680 key
681 (prefix-numeric-value current-prefix-arg)
682 ;; If KEY is a down-event, read and include the
683 ;; corresponding up-event. Note that there are also
684 ;; down-events on scroll bars and mode lines: the actual
685 ;; event then is in the second element of the vector.
686 (and (vectorp key)
687 (let ((last-idx (1- (length key))))
688 (and (eventp (aref key last-idx))
689 (memq 'down (event-modifiers (aref key last-idx)))))
690 (or (and (eventp (aref key 0))
691 (memq 'down (event-modifiers (aref key 0)))
692 ;; However, for the C-down-mouse-2 popup
693 ;; menu, there is no subsequent up-event. In
694 ;; this case, the up-event is the next
695 ;; element in the supplied vector.
696 (= (length key) 1))
697 (and (> (length key) 1)
698 (eventp (aref key 1))
699 (memq 'down (event-modifiers (aref key 1)))))
700 (read-event))))
701 ;; Put yank-menu back as it was, if we changed it.
702 (when saved-yank-menu
703 (setq yank-menu (copy-sequence saved-yank-menu))
704 (fset 'yank-menu (cons 'keymap yank-menu))))))
705 (if (numberp untranslated)
706 (setq untranslated (this-single-command-raw-keys)))
707 (let* ((event (aref key (if (and (symbolp (aref key 0))
708 (> (length key) 1)
709 (consp (aref key 1)))
710 1
711 0)))
712 (modifiers (event-modifiers event))
713 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
714 (memq 'drag modifiers)) " at that spot" ""))
715 (defn (key-binding key t))
716 defn-up defn-up-tricky ev-type
717 mouse-1-remapped mouse-1-tricky)
718
719 ;; Handle the case where we faked an entry in "Select and Paste" menu.
720 (when (and (eq defn nil)
721 (stringp (aref key (1- (length key))))
722 (eq (key-binding (substring key 0 -1)) 'yank-menu))
723 (setq defn 'menu-bar-select-yank))
724 (if (or (null defn) (integerp defn) (equal defn 'undefined))
725 (message "%s%s is undefined"
726 (help-key-description key untranslated) mouse-msg)
727 (help-setup-xref (list #'describe-function defn)
728 (called-interactively-p 'interactive))
729 ;; Don't bother user with strings from (e.g.) the select-paste menu.
730 (when (stringp (aref key (1- (length key))))
731 (aset key (1- (length key)) "(any string)"))
732 (when (and untranslated
733 (stringp (aref untranslated (1- (length untranslated)))))
734 (aset untranslated (1- (length untranslated))
735 "(any string)"))
736 ;; Need to do this before erasing *Help* buffer in case event
737 ;; is a mouse click in an existing *Help* buffer.
738 (when up-event
739 (setq ev-type (event-basic-type up-event))
740 (let ((sequence (vector up-event)))
741 (when (and (eq ev-type 'mouse-1)
742 mouse-1-click-follows-link
743 (not (eq mouse-1-click-follows-link 'double))
744 (setq mouse-1-remapped
745 (mouse-on-link-p (event-start up-event))))
746 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
747 (> mouse-1-click-follows-link 0)))
748 (cond ((stringp mouse-1-remapped)
749 (setq sequence mouse-1-remapped))
750 ((vectorp mouse-1-remapped)
751 (setcar up-event (elt mouse-1-remapped 0)))
752 (t (setcar up-event 'mouse-2))))
753 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
754 (when mouse-1-tricky
755 (setq sequence (vector up-event))
756 (aset sequence 0 'mouse-1)
757 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
758 (with-help-window (help-buffer)
759 (princ (help-key-description key untranslated))
760 (princ (format "\
761 %s runs the command %S, which is "
762 mouse-msg defn))
763 (describe-function-1 defn)
764 (when up-event
765 (unless (or (null defn-up)
766 (integerp defn-up)
767 (equal defn-up 'undefined))
768 (princ (format "
769
770 ----------------- up-event %s----------------
771
772 <%S>%s%s runs the command %S, which is "
773 (if mouse-1-tricky "(short click) " "")
774 ev-type 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 "
783
784 ----------------- up-event (long click) ----------------
785
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)))))))
792 \f
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.
798
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))
867
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
878
879 (defun describe-minor-mode (minor-mode)
880 "Display documentation of a minor mode given as MINOR-MODE.
881 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
882 appeared on the mode-line."
883 (interactive (list (completing-read
884 "Minor mode: "
885 (nconc
886 (describe-minor-mode-completion-table-for-symbol)
887 (describe-minor-mode-completion-table-for-indicator)
888 ))))
889 (if (symbolp minor-mode)
890 (setq minor-mode (symbol-name minor-mode)))
891 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
892 (indicators (describe-minor-mode-completion-table-for-indicator)))
893 (cond
894 ((member minor-mode symbols)
895 (describe-minor-mode-from-symbol (intern minor-mode)))
896 ((member minor-mode indicators)
897 (describe-minor-mode-from-indicator minor-mode))
898 (t
899 (error "No such minor mode: %s" minor-mode)))))
900
901 ;; symbol
902 (defun describe-minor-mode-completion-table-for-symbol ()
903 ;; In order to list up all minor modes, minor-mode-list
904 ;; is used here instead of minor-mode-alist.
905 (delq nil (mapcar 'symbol-name minor-mode-list)))
906 (defun describe-minor-mode-from-symbol (symbol)
907 "Display documentation of a minor mode given as a symbol, SYMBOL"
908 (interactive (list (intern (completing-read
909 "Minor mode symbol: "
910 (describe-minor-mode-completion-table-for-symbol)))))
911 (if (fboundp symbol)
912 (describe-function symbol)
913 (describe-variable symbol)))
914
915 ;; indicator
916 (defun describe-minor-mode-completion-table-for-indicator ()
917 (delq nil
918 (mapcar (lambda (x)
919 (let ((i (format-mode-line x)))
920 ;; remove first space if existed
921 (cond
922 ((= 0 (length i))
923 nil)
924 ((eq (aref i 0) ?\s)
925 (substring i 1))
926 (t
927 i))))
928 minor-mode-alist)))
929 (defun describe-minor-mode-from-indicator (indicator)
930 "Display documentation of a minor mode specified by INDICATOR.
931 If you call this function interactively, you can give indicator which
932 is currently activated with completion."
933 (interactive (list
934 (completing-read
935 "Minor mode indicator: "
936 (describe-minor-mode-completion-table-for-indicator))))
937 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
938 (if minor-mode
939 (describe-minor-mode-from-symbol minor-mode)
940 (error "Cannot find minor mode for `%s'" indicator))))
941
942 (defun lookup-minor-mode-from-indicator (indicator)
943 "Return a minor mode symbol from its indicator on the modeline."
944 ;; remove first space if existed
945 (if (and (< 0 (length indicator))
946 (eq (aref indicator 0) ?\s))
947 (setq indicator (substring indicator 1)))
948 (let ((minor-modes minor-mode-alist)
949 result)
950 (while minor-modes
951 (let* ((minor-mode (car (car minor-modes)))
952 (anindicator (format-mode-line
953 (car (cdr (car minor-modes))))))
954 ;; remove first space if existed
955 (if (and (stringp anindicator)
956 (> (length anindicator) 0)
957 (eq (aref anindicator 0) ?\s))
958 (setq anindicator (substring anindicator 1)))
959 (if (equal indicator anindicator)
960 (setq result minor-mode
961 minor-modes nil)
962 (setq minor-modes (cdr minor-modes)))))
963 result))
964
965 \f
966 ;;; Automatic resizing of temporary buffers.
967
968 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
969 "Maximum height of a window displaying a temporary buffer.
970 This is effective only when Temp Buffer Resize mode is enabled.
971 The value is the maximum height (in lines) which `resize-temp-buffer-window'
972 will give to a window displaying a temporary buffer.
973 It can also be a function to be called to choose the height for such a buffer.
974 It gets one argumemt, the buffer, and should return a positive integer."
975 :type '(choice integer function)
976 :group 'help
977 :version "20.4")
978
979 (define-minor-mode temp-buffer-resize-mode
980 "Toggle the mode which makes windows smaller for temporary buffers.
981 With prefix argument ARG, turn the resizing of windows displaying temporary
982 buffers on if ARG is positive or off otherwise.
983 This makes the window the right height for its contents, but never
984 more than `temp-buffer-max-height' nor less than `window-min-height'.
985 This applies to `help', `apropos' and `completion' buffers, and some others."
986 :global t :group 'help
987 (if temp-buffer-resize-mode
988 ;; `help-make-xrefs' may add a `back' button and thus increase the
989 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
990 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
991 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
992
993 (defun resize-temp-buffer-window ()
994 "Resize the selected window to fit its contents.
995 Will not make it higher than `temp-buffer-max-height' nor smaller than
996 `window-min-height'. Do nothing if it is the only window on its frame, if it
997 is not as wide as the frame or if some of the window's contents are scrolled
998 out of view."
999 (unless (or (one-window-p 'nomini)
1000 (not (pos-visible-in-window-p (point-min)))
1001 (not (window-full-width-p)))
1002 (fit-window-to-buffer
1003 (selected-window)
1004 (if (functionp temp-buffer-max-height)
1005 (funcall temp-buffer-max-height (current-buffer))
1006 temp-buffer-max-height))))
1007
1008 \f
1009 ;;; help-window
1010
1011 (defcustom help-window-select 'other
1012 "Non-nil means select help window for viewing.
1013 Choices are:
1014 never (nil) Select help window only if there is no other window
1015 on its frame.
1016 other Select help window unless the selected window is the
1017 only other window on its frame.
1018 always (t) Always select the help window.
1019
1020 This option has effect if and only if the help window was created
1021 by `with-help-window'"
1022 :type '(choice (const :tag "never (nil)" nil)
1023 (const :tag "other" other)
1024 (const :tag "always (t)" t))
1025 :group 'help
1026 :version "23.1")
1027
1028 (defun help-window-display-message (quit-part window &optional other)
1029 "Display message telling how to quit and scroll help window.
1030 QUIT-PART is a string telling how to quit the help window WINDOW.
1031 Optional argument OTHER non-nil means return text telling how to
1032 scroll the \"other\" window."
1033 (let ((scroll-part
1034 (cond
1035 ((pos-visible-in-window-p
1036 (with-current-buffer (window-buffer window)
1037 (point-max)) window)
1038 ;; Buffer end is visible.
1039 ".")
1040 (other ", \\[scroll-other-window] to scroll help.")
1041 (t ", \\[scroll-up] to scroll help."))))
1042 (message "%s"
1043 (substitute-command-keys (concat quit-part scroll-part)))))
1044
1045 (defun help-window-setup-finish (window &optional reuse keep-frame)
1046 "Finish setting up help window WINDOW.
1047 Select WINDOW according to the value of `help-window-select'.
1048 Display message telling how to scroll and eventually quit WINDOW.
1049
1050 Optional argument REUSE non-nil means WINDOW has been reused \(by
1051 `display-buffer'\) for displaying help. Optional argument
1052 KEEP-FRAME non-nil means that quitting must no delete the frame
1053 of WINDOW."
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)))))
1105
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)))
1117
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))
1123
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))
1151 (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))
1190 (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)))))
1198
1199 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1200 ;; providing the following additional twists:
1201
1202 ;; (1) Issue more accurate messages telling how to scroll and quit the
1203 ;; help window.
1204
1205 ;; (2) Make `view-mode-exit' DTRT in more cases.
1206
1207 ;; (3) An option (customizable via `help-window-select') to select the
1208 ;; help window automatically.
1209
1210 ;; (4) A marker (`help-window-point-marker') to move point in the help
1211 ;; window to an arbitrary buffer position.
1212
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))))
1249 \f
1250 (provide 'help)
1251
1252 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1253 ;;; help.el ends here