]> code.delx.au - gnu-emacs/blob - lisp/help.el
Merge from emacs-23
[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, 2010, 2011 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
8 ;; Package: emacs
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This code implements GNU Emacs' on-line help system, the one invoked by
28 ;; `M-x help-for-help'.
29
30 ;;; Code:
31
32 ;; Get the macro make-help-screen when this is compiled,
33 ;; or run interpreted, but not when the compiled code is loaded.
34 (eval-when-compile (require 'help-macro))
35
36 ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
37 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
38 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
39
40 ;; The variable `help-window' below is used by `help-mode-finish' to
41 ;; communicate the window displaying help (the "help window") to the
42 ;; macro `with-help-window'. The latter sets `help-window' to t before
43 ;; invoking `with-output-to-temp-buffer'. If and only if `help-window'
44 ;; is eq to t, `help-mode-finish' (called by `temp-buffer-setup-hook')
45 ;; sets `help-window' to the window selected by `display-buffer'.
46 ;; Exiting `with-help-window' and calling `help-print-return-message'
47 ;; reset `help-window' to nil.
48 (defvar help-window nil
49 "Window chosen for displaying help.")
50
51 ;; `help-window-point-marker' is a marker you can move to a valid
52 ;; position of the buffer shown in the help window in order to override
53 ;; the standard positioning mechanism (`point-min') chosen by
54 ;; `with-output-to-temp-buffer'. `with-help-window' has this point
55 ;; nowhere before exiting. Currently used by `view-lossage' to assert
56 ;; that the last keystrokes are always visible.
57 (defvar help-window-point-marker (make-marker)
58 "Marker to override default `window-point' of `help-window'.")
59
60 (defvar help-map
61 (let ((map (make-sparse-keymap)))
62 (define-key map (char-to-string help-char) 'help-for-help)
63 (define-key map [help] 'help-for-help)
64 (define-key map [f1] 'help-for-help)
65 (define-key map "." 'display-local-help)
66 (define-key map "?" 'help-for-help)
67
68 (define-key map "\C-a" 'about-emacs)
69 (define-key map "\C-c" 'describe-copying)
70 (define-key map "\C-d" 'view-emacs-debugging)
71 (define-key map "\C-e" 'view-external-packages)
72 (define-key map "\C-f" 'view-emacs-FAQ)
73 (define-key map "\C-m" 'view-order-manuals)
74 (define-key map "\C-n" 'view-emacs-news)
75 (define-key map "\C-o" 'describe-distribution)
76 (define-key map "\C-p" 'view-emacs-problems)
77 (define-key map "\C-t" 'view-emacs-todo)
78 (define-key map "\C-w" 'describe-no-warranty)
79
80 ;; This does not fit the pattern, but it is natural given the C-\ command.
81 (define-key map "\C-\\" 'describe-input-method)
82
83 (define-key map "C" 'describe-coding-system)
84 (define-key map "F" 'Info-goto-emacs-command-node)
85 (define-key map "I" 'describe-input-method)
86 (define-key map "K" 'Info-goto-emacs-key-command-node)
87 (define-key map "L" 'describe-language-environment)
88 (define-key map "S" 'info-lookup-symbol)
89
90 (define-key map "a" 'apropos-command)
91 (define-key map "b" 'describe-bindings)
92 (define-key map "c" 'describe-key-briefly)
93 (define-key map "d" 'apropos-documentation)
94 (define-key map "e" 'view-echo-area-messages)
95 (define-key map "f" 'describe-function)
96 (define-key map "g" 'describe-gnu-project)
97 (define-key map "h" 'view-hello-file)
98
99 (define-key map "i" 'info)
100 (define-key map "4i" 'info-other-window)
101
102 (define-key map "k" 'describe-key)
103 (define-key map "l" 'view-lossage)
104 (define-key map "m" 'describe-mode)
105 (define-key map "n" 'view-emacs-news)
106 (define-key map "p" 'finder-by-keyword)
107 (define-key map "P" 'describe-package)
108 (define-key map "r" 'info-emacs-manual)
109 (define-key map "s" 'describe-syntax)
110 (define-key map "t" 'help-with-tutorial)
111 (define-key map "w" 'where-is)
112 (define-key map "v" 'describe-variable)
113 (define-key map "q" 'help-quit)
114 map)
115 "Keymap for characters following the Help key.")
116
117 (define-key global-map (char-to-string help-char) 'help-command)
118 (define-key global-map [help] 'help-command)
119 (define-key global-map [f1] 'help-command)
120 (fset 'help-command help-map)
121
122 ;; insert-button makes the action nil if it is not store somewhere
123 (defvar help-button-cache nil)
124
125 \f
126 (defun help-quit ()
127 "Just exit from the Help command's command loop."
128 (interactive)
129 nil)
130
131 (defvar help-return-method nil
132 "What to do to \"exit\" the help buffer.
133 This is a list
134 (WINDOW . t) delete the selected window (and possibly its frame,
135 see `quit-window' and `View-quit'), go to WINDOW.
136 (WINDOW . quit-window) do quit-window, then select WINDOW.
137 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
138
139 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
140 (defun help-print-return-message (&optional function)
141 "Display or return message saying how to restore windows after help command.
142 This function assumes that `standard-output' is the help buffer.
143 It computes a message, and applies the optional argument FUNCTION to it.
144 If FUNCTION is nil, it applies `message', thus displaying the message.
145 In addition, this function sets up `help-return-method', which see, that
146 specifies what to do when the user exits the help buffer."
147 ;; Reset `help-window' here to avoid confusing `help-mode-finish'.
148 (setq help-window nil)
149 (and (not (get-buffer-window standard-output))
150 (let ((first-message
151 (cond ((or
152 pop-up-frames
153 (special-display-p (buffer-name standard-output)))
154 (setq help-return-method (cons (selected-window) t))
155 ;; If the help output buffer is a special display buffer,
156 ;; don't say anything about how to get rid of it.
157 ;; First of all, the user will do that with the window
158 ;; manager, not with Emacs.
159 ;; Secondly, the buffer has not been displayed yet,
160 ;; so we don't know whether its frame will be selected.
161 nil)
162 (display-buffer-reuse-frames
163 (setq help-return-method (cons (selected-window)
164 'quit-window))
165 nil)
166 ((not (one-window-p t))
167 (setq help-return-method
168 (cons (selected-window) 'quit-window))
169 "Type \\[display-buffer] RET to restore the other window.")
170 (pop-up-windows
171 (setq help-return-method (cons (selected-window) t))
172 "Type \\[delete-other-windows] to remove help window.")
173 (t
174 (setq help-return-method
175 (list (selected-window) (window-buffer)
176 (window-start) (window-point)))
177 "Type \\[switch-to-buffer] RET to remove help window."))))
178 (funcall (or function 'message)
179 (concat
180 (if first-message
181 (substitute-command-keys first-message))
182 (if first-message " ")
183 ;; If the help buffer will go in a separate frame,
184 ;; it's no use mentioning a command to scroll, so don't.
185 (if (or pop-up-windows
186 (special-display-p (buffer-name standard-output)))
187 nil
188 (if (same-window-p (buffer-name standard-output))
189 ;; Say how to scroll this window.
190 (substitute-command-keys
191 "\\[scroll-up] to scroll the help.")
192 ;; Say how to scroll some other window.
193 (substitute-command-keys
194 "\\[scroll-other-window] to scroll the help."))))))))
195
196 ;; So keyboard macro definitions are documented correctly
197 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
198
199 (defalias 'help 'help-for-help-internal)
200 ;; find-function can find this.
201 (defalias 'help-for-help 'help-for-help-internal)
202 ;; It can't find this, but nobody will look.
203 (make-help-screen help-for-help-internal
204 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
205 ;; Don't purecopy this one, because it's not evaluated (it's
206 ;; directly used as a docstring in a function definition, so it'll
207 ;; be moved to the DOC file anyway: no need for purecopying it).
208 "You have typed %THIS-KEY%, the help character. Type a Help option:
209 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
210
211 a PATTERN Show commands whose name matches the PATTERN (a list of words
212 or a regexp). See also the `apropos' command.
213 b Display all key bindings.
214 c KEYS Display the command name run by the given key sequence.
215 C CODING Describe the given coding system, or RET for current ones.
216 d PATTERN Show a list of functions, variables, and other items whose
217 documentation matches the PATTERN (a list of words or a regexp).
218 e Go to the *Messages* buffer which logs echo-area messages.
219 f FUNCTION Display documentation for the given function.
220 F COMMAND Show the on-line manual's section that describes the command.
221 g Display information about the GNU project.
222 h Display the HELLO file which illustrates various scripts.
223 i Start the Info documentation reader: read on-line manuals.
224 I METHOD Describe a specific input method, or RET for current.
225 k KEYS Display the full documentation for the key sequence.
226 K KEYS Show the on-line manual's section for the command bound to KEYS.
227 l Show last 300 input keystrokes (lossage).
228 L LANG-ENV Describes a specific language environment, or RET for current.
229 m Display documentation of current minor modes and current major mode,
230 including their special commands.
231 n Display news of recent Emacs changes.
232 p TOPIC Find packages matching a given topic keyword.
233 r Display the Emacs manual in Info mode.
234 s Display contents of current syntax table, plus explanations.
235 S SYMBOL Show the section for the given symbol in the on-line manual
236 for the programming language used in this buffer.
237 t Start the Emacs learn-by-doing tutorial.
238 v VARIABLE Display the given variable's documentation and value.
239 w COMMAND Display which keystrokes invoke the given command (where-is).
240 . Display any available local help at point in the echo area.
241
242 C-a Information about Emacs.
243 C-c Emacs copying permission (GNU General Public License).
244 C-d Instructions for debugging GNU Emacs.
245 C-e External packages and information about Emacs.
246 C-f Emacs FAQ.
247 C-m How to order printed Emacs manuals.
248 C-n News of recent Emacs changes.
249 C-o Emacs ordering and distribution information.
250 C-p Info about known Emacs problems.
251 C-t Emacs TODO list.
252 C-w Information on absence of warranty for GNU Emacs."
253 help-map)
254
255 \f
256
257 (defun function-called-at-point ()
258 "Return a function around point or else called by the list containing point.
259 If that doesn't give a function, return nil."
260 (with-syntax-table emacs-lisp-mode-syntax-table
261 (or (condition-case ()
262 (save-excursion
263 (or (not (zerop (skip-syntax-backward "_w")))
264 (eq (char-syntax (following-char)) ?w)
265 (eq (char-syntax (following-char)) ?_)
266 (forward-sexp -1))
267 (skip-chars-forward "'")
268 (let ((obj (read (current-buffer))))
269 (and (symbolp obj) (fboundp obj) obj)))
270 (error nil))
271 (condition-case ()
272 (save-excursion
273 (save-restriction
274 (narrow-to-region (max (point-min)
275 (- (point) 1000)) (point-max))
276 ;; Move up to surrounding paren, then after the open.
277 (backward-up-list 1)
278 (forward-char 1)
279 ;; If there is space here, this is probably something
280 ;; other than a real Lisp function call, so ignore it.
281 (if (looking-at "[ \t]")
282 (error "Probably not a Lisp function call"))
283 (let ((obj (read (current-buffer))))
284 (and (symbolp obj) (fboundp obj) obj))))
285 (error nil))
286 (let* ((str (find-tag-default))
287 (sym (if str (intern-soft str))))
288 (if (and sym (fboundp sym))
289 sym
290 (save-match-data
291 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
292 (setq sym (intern-soft (match-string 1 str)))
293 (and (fboundp sym) sym))))))))
294
295 \f
296 ;;; `User' help functions
297
298 (defun view-help-file (file &optional dir)
299 (view-file (expand-file-name file (or dir data-directory)))
300 (goto-address-mode 1)
301 (goto-char (point-min)))
302
303 (defun describe-distribution ()
304 "Display info on how to obtain the latest version of GNU Emacs."
305 (interactive)
306 (view-help-file "DISTRIB"))
307
308 (defun describe-copying ()
309 "Display info on how you may redistribute copies of GNU Emacs."
310 (interactive)
311 (view-help-file "COPYING"))
312
313 (defun describe-gnu-project ()
314 "Display info on the GNU project."
315 (interactive)
316 (view-help-file "THE-GNU-PROJECT"))
317
318 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
319
320 (defun describe-no-warranty ()
321 "Display info on all the kinds of warranty Emacs does NOT have."
322 (interactive)
323 (describe-copying)
324 (let (case-fold-search)
325 (search-forward "Disclaimer of Warranty")
326 (forward-line 0)
327 (recenter 0)))
328
329 (defun describe-prefix-bindings ()
330 "Describe the bindings of the prefix used to reach this command.
331 The prefix described consists of all but the last event
332 of the key sequence that ran this command."
333 (interactive)
334 (let ((key (this-command-keys)))
335 (describe-bindings
336 (if (stringp key)
337 (substring key 0 (1- (length key)))
338 (let ((prefix (make-vector (1- (length key)) nil))
339 (i 0))
340 (while (< i (length prefix))
341 (aset prefix i (aref key i))
342 (setq i (1+ i)))
343 prefix)))))
344 ;; Make C-h after a prefix, when not specifically bound,
345 ;; run describe-prefix-bindings.
346 (setq prefix-help-command 'describe-prefix-bindings)
347
348 (defun view-emacs-news (&optional version)
349 "Display info on recent changes to Emacs.
350 With argument, display info only for the selected version."
351 (interactive "P")
352 (unless version
353 (setq version emacs-major-version))
354 (when (consp version)
355 (let* ((all-versions
356 (let (res)
357 (mapc
358 (lambda (file)
359 (with-temp-buffer
360 (insert-file-contents
361 (expand-file-name file data-directory))
362 (while (re-search-forward
363 (if (member file '("NEWS.18" "NEWS.1-17"))
364 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
365 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
366 (setq res (cons (match-string-no-properties 1) res)))))
367 (cons "NEWS"
368 (directory-files data-directory nil
369 "^NEWS\\.[0-9][-0-9]*$" nil)))
370 (sort (delete-dups res) (lambda (a b) (string< b a)))))
371 (current (car all-versions)))
372 (setq version (completing-read
373 (format "Read NEWS for the version (default %s): " current)
374 all-versions nil nil nil nil current))
375 (if (integerp (string-to-number version))
376 (setq version (string-to-number version))
377 (unless (or (member version all-versions)
378 (<= (string-to-number version) (string-to-number current)))
379 (error "No news about version %s" version)))))
380 (when (integerp version)
381 (cond ((<= version 12)
382 (setq version (format "1.%d" version)))
383 ((<= version 18)
384 (setq version (format "%d" version)))
385 ((> version emacs-major-version)
386 (error "No news about Emacs %d (yet)" version))))
387 (let* ((vn (if (stringp version)
388 (string-to-number version)
389 version))
390 (file (cond
391 ((>= vn emacs-major-version) "NEWS")
392 ((< vn 18) "NEWS.1-17")
393 (t (format "NEWS.%d" vn))))
394 res)
395 (view-file (expand-file-name file data-directory))
396 (widen)
397 (goto-char (point-min))
398 (when (stringp version)
399 (when (re-search-forward
400 (concat (if (< vn 19)
401 "Changes in Emacs[ \t]*"
402 "^\* [^0-9\n]*") version "$")
403 nil t)
404 (beginning-of-line)
405 (narrow-to-region
406 (point)
407 (save-excursion
408 (while (and (setq res
409 (re-search-forward
410 (if (< vn 19)
411 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
412 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
413 (equal (match-string-no-properties 1) version)))
414 (or res (goto-char (point-max)))
415 (beginning-of-line)
416 (point)))))))
417
418 (defun view-emacs-todo (&optional arg)
419 "Display the Emacs TODO list."
420 (interactive "P")
421 (view-help-file "TODO"))
422
423 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
424
425
426 (defun view-echo-area-messages ()
427 "View the log of recent echo-area messages: the `*Messages*' buffer.
428 The number of messages retained in that buffer
429 is specified by the variable `message-log-max'."
430 (interactive)
431 (switch-to-buffer (get-buffer-create "*Messages*")))
432
433 (defun view-order-manuals ()
434 "Display the Emacs ORDERS file."
435 (interactive)
436 (view-help-file "ORDERS"))
437
438 (defun view-emacs-FAQ ()
439 "Display the Emacs Frequently Asked Questions (FAQ) file."
440 (interactive)
441 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
442 (info "(efaq)"))
443
444 (defun view-emacs-problems ()
445 "Display info on known problems with Emacs and possible workarounds."
446 (interactive)
447 (view-help-file "PROBLEMS"))
448
449 (defun view-emacs-debugging ()
450 "Display info on how to debug Emacs problems."
451 (interactive)
452 (view-help-file "DEBUG"))
453
454 (defun view-external-packages ()
455 "Display external packages and information about Emacs."
456 (interactive)
457 (view-help-file "MORE.STUFF"))
458
459 (defun view-lossage ()
460 "Display last 300 input keystrokes.
461
462 To record all your input on a file, use `open-dribble-file'."
463 (interactive)
464 (help-setup-xref (list #'view-lossage)
465 (called-interactively-p 'interactive))
466 (with-help-window (help-buffer)
467 (princ (mapconcat (lambda (key)
468 (if (or (integerp key) (symbolp key) (listp key))
469 (single-key-description key)
470 (prin1-to-string key nil)))
471 (recent-keys)
472 " "))
473 (with-current-buffer standard-output
474 (goto-char (point-min))
475 (while (progn (move-to-column 50) (not (eobp)))
476 (when (search-forward " " nil t)
477 (delete-char -1))
478 (insert "\n"))
479 ;; jidanni wants to see the last keystrokes immediately.
480 (set-marker help-window-point-marker (point)))))
481
482 \f
483 ;; Key bindings
484
485 (defun describe-bindings (&optional prefix buffer)
486 "Show a list of all defined keys, and their definitions.
487 We put that list in a buffer, and display the buffer.
488
489 The optional argument PREFIX, if non-nil, should be a key sequence;
490 then we display only bindings that start with that prefix.
491 The optional argument BUFFER specifies which buffer's bindings
492 to display (default, the current buffer). BUFFER can be a buffer
493 or a buffer name."
494 (interactive)
495 (or buffer (setq buffer (current-buffer)))
496 (help-setup-xref (list #'describe-bindings prefix buffer)
497 (called-interactively-p 'interactive))
498 (with-current-buffer buffer
499 (describe-bindings-internal nil prefix)))
500
501 ;; This function used to be in keymap.c.
502 (defun describe-bindings-internal (&optional menus prefix)
503 "Show a list of all defined keys, and their definitions.
504 We put that list in a buffer, and display the buffer.
505
506 The optional argument MENUS, if non-nil, says to mention menu bindings.
507 \(Ordinarily these are omitted from the output.)
508 The optional argument PREFIX, if non-nil, should be a key sequence;
509 then we display only bindings that start with that prefix."
510 (let ((buf (current-buffer)))
511 (with-help-window "*Help*"
512 (with-current-buffer standard-output
513 (describe-buffer-bindings buf prefix menus)))))
514
515 (defun where-is (definition &optional insert)
516 "Print message listing key sequences that invoke the command DEFINITION.
517 Argument is a command definition, usually a symbol with a function definition.
518 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
519 (interactive
520 (let ((fn (function-called-at-point))
521 (enable-recursive-minibuffers t)
522 val)
523 (setq val (completing-read
524 (if fn
525 (format "Where is command (default %s): " fn)
526 "Where is command: ")
527 obarray 'commandp t))
528 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
529 (unless definition (error "No command"))
530 (let ((func (indirect-function definition))
531 (defs nil)
532 (standard-output (if insert (current-buffer) t)))
533 ;; In DEFS, find all symbols that are aliases for DEFINITION.
534 (mapatoms (lambda (symbol)
535 (and (fboundp symbol)
536 (not (eq symbol definition))
537 (eq func (condition-case ()
538 (indirect-function symbol)
539 (error symbol)))
540 (push symbol defs))))
541 ;; Look at all the symbols--first DEFINITION,
542 ;; then its aliases.
543 (dolist (symbol (cons definition defs))
544 (let* ((remapped (command-remapping symbol))
545 (keys (where-is-internal
546 symbol overriding-local-map nil nil remapped))
547 (keys (mapconcat 'key-description keys ", "))
548 string)
549 (setq string
550 (if insert
551 (if (> (length keys) 0)
552 (if remapped
553 (format "%s (%s) (remapped from %s)"
554 keys remapped symbol)
555 (format "%s (%s)" keys symbol))
556 (format "M-x %s RET" symbol))
557 (if (> (length keys) 0)
558 (if remapped
559 (format "%s is remapped to %s which is on %s"
560 symbol remapped keys)
561 (format "%s is on %s" symbol keys))
562 ;; If this is the command the user asked about,
563 ;; and it is not on any key, say so.
564 ;; For other symbols, its aliases, say nothing
565 ;; about them unless they are on keys.
566 (if (eq symbol definition)
567 (format "%s is not on any key" symbol)))))
568 (when string
569 (unless (eq symbol definition)
570 (princ ";\n its alias "))
571 (princ string)))))
572 nil)
573
574 (defun help-key-description (key untranslated)
575 (let ((string (key-description key)))
576 (if (or (not untranslated)
577 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
578 string
579 (let ((otherstring (key-description untranslated)))
580 (if (equal string otherstring)
581 string
582 (format "%s (translated from %s)" string otherstring))))))
583
584 (defun describe-key-briefly (&optional key insert untranslated)
585 "Print the name of the function KEY invokes. KEY is a string.
586 If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
587 If non-nil, UNTRANSLATED is a vector of the untranslated events.
588 It can also be a number in which case the untranslated events from
589 the last key hit are used.
590
591 If KEY is a menu item or a tool-bar button that is disabled, this command
592 temporarily enables it to allow getting help on disabled items and buttons."
593 (interactive
594 (let ((enable-disabled-menus-and-buttons t)
595 (cursor-in-echo-area t)
596 saved-yank-menu)
597 (unwind-protect
598 (let (key)
599 ;; If yank-menu is empty, populate it temporarily, so that
600 ;; "Select and Paste" menu can generate a complete event.
601 (when (null (cdr yank-menu))
602 (setq saved-yank-menu (copy-sequence yank-menu))
603 (menu-bar-update-yank-menu "(any string)" nil))
604 (setq key (read-key-sequence "Describe key (or click or menu item): "))
605 ;; If KEY is a down-event, read and discard the
606 ;; corresponding up-event. Note that there are also
607 ;; down-events on scroll bars and mode lines: the actual
608 ;; event then is in the second element of the vector.
609 (and (vectorp key)
610 (let ((last-idx (1- (length key))))
611 (and (eventp (aref key last-idx))
612 (memq 'down (event-modifiers (aref key last-idx)))))
613 (read-event))
614 (list
615 key
616 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
617 1))
618 ;; Put yank-menu back as it was, if we changed it.
619 (when saved-yank-menu
620 (setq yank-menu (copy-sequence saved-yank-menu))
621 (fset 'yank-menu (cons 'keymap yank-menu))))))
622 (if (numberp untranslated)
623 (setq untranslated (this-single-command-raw-keys)))
624 (let* ((event (if (and (symbolp (aref key 0))
625 (> (length key) 1)
626 (consp (aref key 1)))
627 (aref key 1)
628 (aref key 0)))
629 (modifiers (event-modifiers event))
630 (standard-output (if insert (current-buffer) t))
631 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
632 (memq 'drag modifiers)) " at that spot" ""))
633 (defn (key-binding key t))
634 key-desc)
635 ;; Handle the case where we faked an entry in "Select and Paste" menu.
636 (if (and (eq defn nil)
637 (stringp (aref key (1- (length key))))
638 (eq (key-binding (substring key 0 -1)) 'yank-menu))
639 (setq defn 'menu-bar-select-yank))
640 ;; Don't bother user with strings from (e.g.) the select-paste menu.
641 (if (stringp (aref key (1- (length key))))
642 (aset key (1- (length key)) "(any string)"))
643 (if (and (> (length untranslated) 0)
644 (stringp (aref untranslated (1- (length untranslated)))))
645 (aset untranslated (1- (length untranslated)) "(any string)"))
646 ;; Now describe the key, perhaps as changed.
647 (setq key-desc (help-key-description key untranslated))
648 (if (or (null defn) (integerp defn) (equal defn 'undefined))
649 (princ (format "%s%s is undefined" key-desc mouse-msg))
650 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
651
652 (defun describe-key (&optional key untranslated up-event)
653 "Display documentation of the function invoked by KEY.
654 KEY can be any kind of a key sequence; it can include keyboard events,
655 mouse events, and/or menu events. When calling from a program,
656 pass KEY as a string or a vector.
657
658 If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
659 It can also be a number, in which case the untranslated events from
660 the last key sequence entered are used.
661 UP-EVENT is the up-event that was discarded by reading KEY, or nil.
662
663 If KEY is a menu item or a tool-bar button that is disabled, this command
664 temporarily enables it to allow getting help on disabled items and buttons."
665 (interactive
666 (let ((enable-disabled-menus-and-buttons t)
667 (cursor-in-echo-area t)
668 saved-yank-menu)
669 (unwind-protect
670 (let (key)
671 ;; If yank-menu is empty, populate it temporarily, so that
672 ;; "Select and Paste" menu can generate a complete event.
673 (when (null (cdr yank-menu))
674 (setq saved-yank-menu (copy-sequence yank-menu))
675 (menu-bar-update-yank-menu "(any string)" nil))
676 (setq key (read-key-sequence "Describe key (or click or menu item): "))
677 (list
678 key
679 (prefix-numeric-value current-prefix-arg)
680 ;; If KEY is a down-event, read and include the
681 ;; corresponding up-event. Note that there are also
682 ;; down-events on scroll bars and mode lines: the actual
683 ;; event then is in the second element of the vector.
684 (and (vectorp key)
685 (let ((last-idx (1- (length key))))
686 (and (eventp (aref key last-idx))
687 (memq 'down (event-modifiers (aref key last-idx)))))
688 (or (and (eventp (aref key 0))
689 (memq 'down (event-modifiers (aref key 0)))
690 ;; However, for the C-down-mouse-2 popup
691 ;; menu, there is no subsequent up-event. In
692 ;; this case, the up-event is the next
693 ;; element in the supplied vector.
694 (= (length key) 1))
695 (and (> (length key) 1)
696 (eventp (aref key 1))
697 (memq 'down (event-modifiers (aref key 1)))))
698 (read-event))))
699 ;; Put yank-menu back as it was, if we changed it.
700 (when saved-yank-menu
701 (setq yank-menu (copy-sequence saved-yank-menu))
702 (fset 'yank-menu (cons 'keymap yank-menu))))))
703 (if (numberp untranslated)
704 (setq untranslated (this-single-command-raw-keys)))
705 (let* ((event (aref key (if (and (symbolp (aref key 0))
706 (> (length key) 1)
707 (consp (aref key 1)))
708 1
709 0)))
710 (modifiers (event-modifiers event))
711 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
712 (memq 'drag modifiers)) " at that spot" ""))
713 (defn (key-binding key t))
714 defn-up defn-up-tricky ev-type
715 mouse-1-remapped mouse-1-tricky)
716
717 ;; Handle the case where we faked an entry in "Select and Paste" menu.
718 (when (and (eq defn nil)
719 (stringp (aref key (1- (length key))))
720 (eq (key-binding (substring key 0 -1)) 'yank-menu))
721 (setq defn 'menu-bar-select-yank))
722 (if (or (null defn) (integerp defn) (equal defn 'undefined))
723 (message "%s%s is undefined"
724 (help-key-description key untranslated) mouse-msg)
725 (help-setup-xref (list #'describe-function defn)
726 (called-interactively-p 'interactive))
727 ;; Don't bother user with strings from (e.g.) the select-paste menu.
728 (when (stringp (aref key (1- (length key))))
729 (aset key (1- (length key)) "(any string)"))
730 (when (and untranslated
731 (stringp (aref untranslated (1- (length untranslated)))))
732 (aset untranslated (1- (length untranslated))
733 "(any string)"))
734 ;; Need to do this before erasing *Help* buffer in case event
735 ;; is a mouse click in an existing *Help* buffer.
736 (when up-event
737 (setq ev-type (event-basic-type up-event))
738 (let ((sequence (vector up-event)))
739 (when (and (eq ev-type 'mouse-1)
740 mouse-1-click-follows-link
741 (not (eq mouse-1-click-follows-link 'double))
742 (setq mouse-1-remapped
743 (mouse-on-link-p (event-start up-event))))
744 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
745 (> mouse-1-click-follows-link 0)))
746 (cond ((stringp mouse-1-remapped)
747 (setq sequence mouse-1-remapped))
748 ((vectorp mouse-1-remapped)
749 (setcar up-event (elt mouse-1-remapped 0)))
750 (t (setcar up-event 'mouse-2))))
751 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
752 (when mouse-1-tricky
753 (setq sequence (vector up-event))
754 (aset sequence 0 'mouse-1)
755 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
756 (with-help-window (help-buffer)
757 (princ (help-key-description key untranslated))
758 (princ (format "\
759 %s runs the command %S, which is "
760 mouse-msg defn))
761 (describe-function-1 defn)
762 (when up-event
763 (unless (or (null defn-up)
764 (integerp defn-up)
765 (equal defn-up 'undefined))
766 (princ (format "
767
768 ----------------- up-event %s----------------
769
770 %s%s%s runs the command %S, which is "
771 (if mouse-1-tricky "(short click) " "")
772 (key-description (vector up-event))
773 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 "
782
783 ----------------- up-event (long click) ----------------
784
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)))))))
791 \f
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.
797
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))
866
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)
878
879
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))
899 (t
900 (error "No such minor mode: %s" minor-mode)))))
901
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)))
915
916 ;; indicator
917 (defun describe-minor-mode-completion-table-for-indicator ()
918 (delq nil
919 (mapcar (lambda (x)
920 (let ((i (format-mode-line x)))
921 ;; remove first space if existed
922 (cond
923 ((= 0 (length i))
924 nil)
925 ((eq (aref i 0) ?\s)
926 (substring i 1))
927 (t
928 i))))
929 minor-mode-alist)))
930 (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))))
942
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))
965
966 \f
967 ;;; Automatic resizing of temporary buffers.
968
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")
979
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)))
993
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))))
1008
1009 \f
1010 ;;; help-window
1011
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.
1020
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")
1028
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)))))
1045
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.
1050
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)))))
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 ;;; help.el ends here