]> code.delx.au - gnu-emacs/blob - lisp/help.el
Improvements to describe-package buffer.
[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 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 "P" 'describe-package)
107 (define-key map "r" 'info-emacs-manual)
108 (define-key map "s" 'describe-syntax)
109 (define-key map "t" 'help-with-tutorial)
110 (define-key map "w" 'where-is)
111 (define-key map "v" 'describe-variable)
112 (define-key map "q" 'help-quit)
113 map)
114 "Keymap for characters following the Help key.")
115
116 (define-key global-map (char-to-string help-char) 'help-command)
117 (define-key global-map [help] 'help-command)
118 (define-key global-map [f1] 'help-command)
119 (fset 'help-command help-map)
120
121 (autoload 'finder-by-keyword "finder"
122 "Find packages matching a given keyword." t)
123
124 ;; insert-button makes the action nil if it is not store somewhere
125 (defvar help-button-cache nil)
126
127 \f
128 (defun help-quit ()
129 "Just exit from the Help command's command loop."
130 (interactive)
131 nil)
132
133 (defvar help-return-method nil
134 "What to do to \"exit\" the help buffer.
135 This is a list
136 (WINDOW . t) delete the selected window (and possibly its frame,
137 see `quit-window' and `View-quit'), go to WINDOW.
138 (WINDOW . quit-window) do quit-window, then select WINDOW.
139 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
140
141 (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
142 (defun help-print-return-message (&optional function)
143 "Display or return message saying how to restore windows after help command.
144 This function assumes that `standard-output' is the help buffer.
145 It computes a message, and applies the optional argument FUNCTION to it.
146 If FUNCTION is nil, it applies `message', thus displaying the message.
147 In addition, this function sets up `help-return-method', which see, that
148 specifies what to do when the user exits the help buffer."
149 ;; Reset `help-window' here to avoid confusing `help-mode-finish'.
150 (setq help-window nil)
151 (and (not (get-buffer-window standard-output))
152 (let ((first-message
153 (cond ((or
154 pop-up-frames
155 (special-display-p (buffer-name standard-output)))
156 (setq help-return-method (cons (selected-window) t))
157 ;; If the help output buffer is a special display buffer,
158 ;; don't say anything about how to get rid of it.
159 ;; First of all, the user will do that with the window
160 ;; manager, not with Emacs.
161 ;; Secondly, the buffer has not been displayed yet,
162 ;; so we don't know whether its frame will be selected.
163 nil)
164 (display-buffer-reuse-frames
165 (setq help-return-method (cons (selected-window)
166 'quit-window))
167 nil)
168 ((not (one-window-p t))
169 (setq help-return-method
170 (cons (selected-window) 'quit-window))
171 "Type \\[display-buffer] RET to restore the other window.")
172 (pop-up-windows
173 (setq help-return-method (cons (selected-window) t))
174 "Type \\[delete-other-windows] to remove help window.")
175 (t
176 (setq help-return-method
177 (list (selected-window) (window-buffer)
178 (window-start) (window-point)))
179 "Type \\[switch-to-buffer] RET to remove help window."))))
180 (funcall (or function 'message)
181 (concat
182 (if first-message
183 (substitute-command-keys first-message))
184 (if first-message " ")
185 ;; If the help buffer will go in a separate frame,
186 ;; it's no use mentioning a command to scroll, so don't.
187 (if (or pop-up-windows
188 (special-display-p (buffer-name standard-output)))
189 nil
190 (if (same-window-p (buffer-name standard-output))
191 ;; Say how to scroll this window.
192 (substitute-command-keys
193 "\\[scroll-up] to scroll the help.")
194 ;; Say how to scroll some other window.
195 (substitute-command-keys
196 "\\[scroll-other-window] to scroll the help."))))))))
197
198 ;; So keyboard macro definitions are documented correctly
199 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
200
201 (defalias 'help 'help-for-help-internal)
202 ;; find-function can find this.
203 (defalias 'help-for-help 'help-for-help-internal)
204 ;; It can't find this, but nobody will look.
205 (make-help-screen help-for-help-internal
206 (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
207 ;; Don't purecopy this one, because it's not evaluated (it's
208 ;; directly used as a docstring in a function definition, so it'll
209 ;; be moved to the DOC file anyway: no need for purecopying it).
210 "You have typed %THIS-KEY%, the help character. Type a Help option:
211 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
212
213 a PATTERN Show commands whose name matches the PATTERN (a list of words
214 or a regexp). See also the `apropos' command.
215 b Display all key bindings.
216 c KEYS Display the command name run by the given key sequence.
217 C CODING Describe the given coding system, or RET for current ones.
218 d PATTERN Show a list of functions, variables, and other items whose
219 documentation matches the PATTERN (a list of words or a regexp).
220 e Go to the *Messages* buffer which logs echo-area messages.
221 f FUNCTION Display documentation for the given function.
222 F COMMAND Show the on-line manual's section that describes the command.
223 g Display information about the GNU project.
224 h Display the HELLO file which illustrates various scripts.
225 i Start the Info documentation reader: read on-line manuals.
226 I METHOD Describe a specific input method, or RET for current.
227 k KEYS Display the full documentation for the key sequence.
228 K KEYS Show the on-line manual's section for the command bound to KEYS.
229 l Show last 300 input keystrokes (lossage).
230 L LANG-ENV Describes a specific language environment, or RET for current.
231 m Display documentation of current minor modes and current major mode,
232 including their special commands.
233 n Display news of recent Emacs changes.
234 p TOPIC Find packages matching a given topic keyword.
235 r Display the Emacs manual in Info mode.
236 s Display contents of current syntax table, plus explanations.
237 S SYMBOL Show the section for the given symbol in the on-line manual
238 for the programming language used in this buffer.
239 t Start the Emacs learn-by-doing tutorial.
240 v VARIABLE Display the given variable's documentation and value.
241 w COMMAND Display which keystrokes invoke the given command (where-is).
242 . Display any available local help at point in the echo area.
243
244 C-a Information about Emacs.
245 C-c Emacs copying permission (GNU General Public License).
246 C-d Instructions for debugging GNU Emacs.
247 C-e External packages and information about Emacs.
248 C-f Emacs FAQ.
249 C-m How to order printed Emacs manuals.
250 C-n News of recent Emacs changes.
251 C-o Emacs ordering and distribution information.
252 C-p Info about known Emacs problems.
253 C-t Emacs TODO list.
254 C-w Information on absence of warranty for GNU Emacs."
255 help-map)
256
257 \f
258
259 (defun function-called-at-point ()
260 "Return a function around point or else called by the list containing point.
261 If that doesn't give a function, return nil."
262 (with-syntax-table emacs-lisp-mode-syntax-table
263 (or (condition-case ()
264 (save-excursion
265 (or (not (zerop (skip-syntax-backward "_w")))
266 (eq (char-syntax (following-char)) ?w)
267 (eq (char-syntax (following-char)) ?_)
268 (forward-sexp -1))
269 (skip-chars-forward "'")
270 (let ((obj (read (current-buffer))))
271 (and (symbolp obj) (fboundp obj) obj)))
272 (error nil))
273 (condition-case ()
274 (save-excursion
275 (save-restriction
276 (narrow-to-region (max (point-min)
277 (- (point) 1000)) (point-max))
278 ;; Move up to surrounding paren, then after the open.
279 (backward-up-list 1)
280 (forward-char 1)
281 ;; If there is space here, this is probably something
282 ;; other than a real Lisp function call, so ignore it.
283 (if (looking-at "[ \t]")
284 (error "Probably not a Lisp function call"))
285 (let ((obj (read (current-buffer))))
286 (and (symbolp obj) (fboundp obj) obj))))
287 (error nil))
288 (let* ((str (find-tag-default))
289 (sym (if str (intern-soft str))))
290 (if (and sym (fboundp sym))
291 sym
292 (save-match-data
293 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
294 (setq sym (intern-soft (match-string 1 str)))
295 (and (fboundp sym) sym))))))))
296
297 \f
298 ;;; `User' help functions
299
300 (defun view-help-file (file &optional dir)
301 (view-file (expand-file-name file (or dir data-directory)))
302 (goto-address-mode 1)
303 (goto-char (point-min)))
304
305 (defun describe-distribution ()
306 "Display info on how to obtain the latest version of GNU Emacs."
307 (interactive)
308 (view-help-file "DISTRIB"))
309
310 (defun describe-copying ()
311 "Display info on how you may redistribute copies of GNU Emacs."
312 (interactive)
313 (view-help-file "COPYING"))
314
315 (defun describe-gnu-project ()
316 "Display info on the GNU project."
317 (interactive)
318 (view-help-file "THE-GNU-PROJECT"))
319
320 (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
321
322 (defun describe-no-warranty ()
323 "Display info on all the kinds of warranty Emacs does NOT have."
324 (interactive)
325 (describe-copying)
326 (let (case-fold-search)
327 (search-forward "Disclaimer of Warranty")
328 (forward-line 0)
329 (recenter 0)))
330
331 (defun describe-prefix-bindings ()
332 "Describe the bindings of the prefix used to reach this command.
333 The prefix described consists of all but the last event
334 of the key sequence that ran this command."
335 (interactive)
336 (let ((key (this-command-keys)))
337 (describe-bindings
338 (if (stringp key)
339 (substring key 0 (1- (length key)))
340 (let ((prefix (make-vector (1- (length key)) nil))
341 (i 0))
342 (while (< i (length prefix))
343 (aset prefix i (aref key i))
344 (setq i (1+ i)))
345 prefix)))))
346 ;; Make C-h after a prefix, when not specifically bound,
347 ;; run describe-prefix-bindings.
348 (setq prefix-help-command 'describe-prefix-bindings)
349
350 (defun view-emacs-news (&optional version)
351 "Display info on recent changes to Emacs.
352 With argument, display info only for the selected version."
353 (interactive "P")
354 (unless version
355 (setq version emacs-major-version))
356 (when (consp version)
357 (let* ((all-versions
358 (let (res)
359 (mapc
360 (lambda (file)
361 (with-temp-buffer
362 (insert-file-contents
363 (expand-file-name file data-directory))
364 (while (re-search-forward
365 (if (member file '("NEWS.18" "NEWS.1-17"))
366 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
367 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
368 (setq res (cons (match-string-no-properties 1) res)))))
369 (cons "NEWS"
370 (directory-files data-directory nil
371 "^NEWS\\.[0-9][-0-9]*$" nil)))
372 (sort (delete-dups res) (lambda (a b) (string< b a)))))
373 (current (car all-versions)))
374 (setq version (completing-read
375 (format "Read NEWS for the version (default %s): " current)
376 all-versions nil nil nil nil current))
377 (if (integerp (string-to-number version))
378 (setq version (string-to-number version))
379 (unless (or (member version all-versions)
380 (<= (string-to-number version) (string-to-number current)))
381 (error "No news about version %s" version)))))
382 (when (integerp version)
383 (cond ((<= version 12)
384 (setq version (format "1.%d" version)))
385 ((<= version 18)
386 (setq version (format "%d" version)))
387 ((> version emacs-major-version)
388 (error "No news about Emacs %d (yet)" version))))
389 (let* ((vn (if (stringp version)
390 (string-to-number version)
391 version))
392 (file (cond
393 ((>= vn emacs-major-version) "NEWS")
394 ((< vn 18) "NEWS.1-17")
395 (t (format "NEWS.%d" vn))))
396 res)
397 (view-file (expand-file-name file data-directory))
398 (widen)
399 (goto-char (point-min))
400 (when (stringp version)
401 (when (re-search-forward
402 (concat (if (< vn 19)
403 "Changes in Emacs[ \t]*"
404 "^\* [^0-9\n]*") version "$")
405 nil t)
406 (beginning-of-line)
407 (narrow-to-region
408 (point)
409 (save-excursion
410 (while (and (setq res
411 (re-search-forward
412 (if (< vn 19)
413 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
414 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
415 (equal (match-string-no-properties 1) version)))
416 (or res (goto-char (point-max)))
417 (beginning-of-line)
418 (point)))))))
419
420 (defun view-emacs-todo (&optional arg)
421 "Display the Emacs TODO list."
422 (interactive "P")
423 (view-help-file "TODO"))
424
425 (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
426
427
428 (defun view-echo-area-messages ()
429 "View the log of recent echo-area messages: the `*Messages*' buffer.
430 The number of messages retained in that buffer
431 is specified by the variable `message-log-max'."
432 (interactive)
433 (switch-to-buffer (get-buffer-create "*Messages*")))
434
435 (defun view-order-manuals ()
436 "Display the Emacs ORDERS file."
437 (interactive)
438 (view-help-file "ORDERS"))
439
440 (defun view-emacs-FAQ ()
441 "Display the Emacs Frequently Asked Questions (FAQ) file."
442 (interactive)
443 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
444 (info "(efaq)"))
445
446 (defun view-emacs-problems ()
447 "Display info on known problems with Emacs and possible workarounds."
448 (interactive)
449 (view-help-file "PROBLEMS"))
450
451 (defun view-emacs-debugging ()
452 "Display info on how to debug Emacs problems."
453 (interactive)
454 (view-help-file "DEBUG"))
455
456 (defun view-external-packages ()
457 "Display external packages and information about Emacs."
458 (interactive)
459 (view-help-file "MORE.STUFF"))
460
461 (defun view-lossage ()
462 "Display last 300 input keystrokes.
463
464 To record all your input on a file, use `open-dribble-file'."
465 (interactive)
466 (help-setup-xref (list #'view-lossage)
467 (called-interactively-p 'interactive))
468 (with-help-window (help-buffer)
469 (princ (mapconcat (lambda (key)
470 (if (or (integerp key) (symbolp key) (listp key))
471 (single-key-description key)
472 (prin1-to-string key nil)))
473 (recent-keys)
474 " "))
475 (with-current-buffer standard-output
476 (goto-char (point-min))
477 (while (progn (move-to-column 50) (not (eobp)))
478 (when (search-forward " " nil t)
479 (delete-char -1))
480 (insert "\n"))
481 ;; jidanni wants to see the last keystrokes immediately.
482 (set-marker help-window-point-marker (point)))))
483
484 \f
485 ;; Key bindings
486
487 (defun describe-bindings (&optional prefix buffer)
488 "Show a list of all defined keys, and their definitions.
489 We put that list in a buffer, and display the buffer.
490
491 The optional argument PREFIX, if non-nil, should be a key sequence;
492 then we display only bindings that start with that prefix.
493 The optional argument BUFFER specifies which buffer's bindings
494 to display (default, the current buffer). BUFFER can be a buffer
495 or a buffer name."
496 (interactive)
497 (or buffer (setq buffer (current-buffer)))
498 (help-setup-xref (list #'describe-bindings prefix buffer)
499 (called-interactively-p 'interactive))
500 (with-current-buffer buffer
501 (describe-bindings-internal nil prefix)))
502
503 ;; This function used to be in keymap.c.
504 (defun describe-bindings-internal (&optional menus prefix)
505 "Show a list of all defined keys, and their definitions.
506 We put that list in a buffer, and display the buffer.
507
508 The optional argument MENUS, if non-nil, says to mention menu bindings.
509 \(Ordinarily these are omitted from the output.)
510 The optional argument PREFIX, if non-nil, should be a key sequence;
511 then we display only bindings that start with that prefix."
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 (key-description (vector up-event))
775 mouse-msg
776 (if mouse-1-remapped
777 " is remapped to <mouse-2>, which" "")
778 defn-up))
779 (describe-function-1 defn-up))
780 (unless (or (null defn-up-tricky)
781 (integerp defn-up-tricky)
782 (eq defn-up-tricky 'undefined))
783 (princ (format "
784
785 ----------------- up-event (long click) ----------------
786
787 Pressing <%S>%s for longer than %d milli-seconds
788 runs the command %S, which is "
789 ev-type mouse-msg
790 mouse-1-click-follows-link
791 defn-up-tricky))
792 (describe-function-1 defn-up-tricky)))))))
793 \f
794 (defun describe-mode (&optional buffer)
795 "Display documentation of current major mode and minor modes.
796 A brief summary of the minor modes comes first, followed by the
797 major mode description. This is followed by detailed
798 descriptions of the minor modes, each on a separate page.
799
800 For this to work correctly for a minor mode, the mode's indicator
801 variable \(listed in `minor-mode-alist') must also be a function
802 whose documentation describes the minor mode."
803 (interactive "@")
804 (unless buffer (setq buffer (current-buffer)))
805 (help-setup-xref (list #'describe-mode buffer)
806 (called-interactively-p 'interactive))
807 ;; For the sake of help-do-xref and help-xref-go-back,
808 ;; don't switch buffers before calling `help-buffer'.
809 (with-help-window (help-buffer)
810 (with-current-buffer buffer
811 (let (minor-modes)
812 ;; Older packages do not register in minor-mode-list but only in
813 ;; minor-mode-alist.
814 (dolist (x minor-mode-alist)
815 (setq x (car x))
816 (unless (memq x minor-mode-list)
817 (push x minor-mode-list)))
818 ;; Find enabled minor mode we will want to mention.
819 (dolist (mode minor-mode-list)
820 ;; Document a minor mode if it is listed in minor-mode-alist,
821 ;; non-nil, and has a function definition.
822 (let ((fmode (or (get mode :minor-mode-function) mode)))
823 (and (boundp mode) (symbol-value mode)
824 (fboundp fmode)
825 (let ((pretty-minor-mode
826 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
827 (symbol-name fmode))
828 (capitalize
829 (substring (symbol-name fmode)
830 0 (match-beginning 0)))
831 fmode)))
832 (push (list fmode pretty-minor-mode
833 (format-mode-line (assq mode minor-mode-alist)))
834 minor-modes)))))
835 (setq minor-modes
836 (sort minor-modes
837 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
838 (when minor-modes
839 (princ "Enabled minor modes:\n")
840 (make-local-variable 'help-button-cache)
841 (with-current-buffer standard-output
842 (dolist (mode minor-modes)
843 (let ((mode-function (nth 0 mode))
844 (pretty-minor-mode (nth 1 mode))
845 (indicator (nth 2 mode)))
846 (add-text-properties 0 (length pretty-minor-mode)
847 '(face bold) pretty-minor-mode)
848 (save-excursion
849 (goto-char (point-max))
850 (princ "\n\f\n")
851 (push (point-marker) help-button-cache)
852 ;; Document the minor modes fully.
853 (insert pretty-minor-mode)
854 (princ (format " minor mode (%s):\n"
855 (if (zerop (length indicator))
856 "no indicator"
857 (format "indicator%s"
858 indicator))))
859 (princ (documentation mode-function)))
860 (insert-button pretty-minor-mode
861 'action (car help-button-cache)
862 'follow-link t
863 'help-echo "mouse-2, RET: show full information")
864 (newline)))
865 (forward-line -1)
866 (fill-paragraph nil)
867 (forward-line 1))
868
869 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
870 ;; Document the major mode.
871 (let ((mode mode-name))
872 (with-current-buffer standard-output
873 (let ((start (point)))
874 (insert (format-mode-line mode nil nil buffer))
875 (add-text-properties start (point) '(face bold)))))
876 (princ " mode:\n")
877 (princ (documentation major-mode)))))
878 ;; For the sake of IELM and maybe others
879 nil)
880
881
882 (defun describe-minor-mode (minor-mode)
883 "Display documentation of a minor mode given as MINOR-MODE.
884 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
885 appeared on the mode-line."
886 (interactive (list (completing-read
887 "Minor mode: "
888 (nconc
889 (describe-minor-mode-completion-table-for-symbol)
890 (describe-minor-mode-completion-table-for-indicator)
891 ))))
892 (if (symbolp minor-mode)
893 (setq minor-mode (symbol-name minor-mode)))
894 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
895 (indicators (describe-minor-mode-completion-table-for-indicator)))
896 (cond
897 ((member minor-mode symbols)
898 (describe-minor-mode-from-symbol (intern minor-mode)))
899 ((member minor-mode indicators)
900 (describe-minor-mode-from-indicator minor-mode))
901 (t
902 (error "No such minor mode: %s" minor-mode)))))
903
904 ;; symbol
905 (defun describe-minor-mode-completion-table-for-symbol ()
906 ;; In order to list up all minor modes, minor-mode-list
907 ;; is used here instead of minor-mode-alist.
908 (delq nil (mapcar 'symbol-name minor-mode-list)))
909 (defun describe-minor-mode-from-symbol (symbol)
910 "Display documentation of a minor mode given as a symbol, SYMBOL"
911 (interactive (list (intern (completing-read
912 "Minor mode symbol: "
913 (describe-minor-mode-completion-table-for-symbol)))))
914 (if (fboundp symbol)
915 (describe-function symbol)
916 (describe-variable symbol)))
917
918 ;; indicator
919 (defun describe-minor-mode-completion-table-for-indicator ()
920 (delq nil
921 (mapcar (lambda (x)
922 (let ((i (format-mode-line x)))
923 ;; remove first space if existed
924 (cond
925 ((= 0 (length i))
926 nil)
927 ((eq (aref i 0) ?\s)
928 (substring i 1))
929 (t
930 i))))
931 minor-mode-alist)))
932 (defun describe-minor-mode-from-indicator (indicator)
933 "Display documentation of a minor mode specified by INDICATOR.
934 If you call this function interactively, you can give indicator which
935 is currently activated with completion."
936 (interactive (list
937 (completing-read
938 "Minor mode indicator: "
939 (describe-minor-mode-completion-table-for-indicator))))
940 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
941 (if minor-mode
942 (describe-minor-mode-from-symbol minor-mode)
943 (error "Cannot find minor mode for `%s'" indicator))))
944
945 (defun lookup-minor-mode-from-indicator (indicator)
946 "Return a minor mode symbol from its indicator on the modeline."
947 ;; remove first space if existed
948 (if (and (< 0 (length indicator))
949 (eq (aref indicator 0) ?\s))
950 (setq indicator (substring indicator 1)))
951 (let ((minor-modes minor-mode-alist)
952 result)
953 (while minor-modes
954 (let* ((minor-mode (car (car minor-modes)))
955 (anindicator (format-mode-line
956 (car (cdr (car minor-modes))))))
957 ;; remove first space if existed
958 (if (and (stringp anindicator)
959 (> (length anindicator) 0)
960 (eq (aref anindicator 0) ?\s))
961 (setq anindicator (substring anindicator 1)))
962 (if (equal indicator anindicator)
963 (setq result minor-mode
964 minor-modes nil)
965 (setq minor-modes (cdr minor-modes)))))
966 result))
967
968 \f
969 ;;; Automatic resizing of temporary buffers.
970
971 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
972 "Maximum height of a window displaying a temporary buffer.
973 This is effective only when Temp Buffer Resize mode is enabled.
974 The value is the maximum height (in lines) which `resize-temp-buffer-window'
975 will give to a window displaying a temporary buffer.
976 It can also be a function to be called to choose the height for such a buffer.
977 It gets one argumemt, the buffer, and should return a positive integer."
978 :type '(choice integer function)
979 :group 'help
980 :version "20.4")
981
982 (define-minor-mode temp-buffer-resize-mode
983 "Toggle the mode which makes windows smaller for temporary buffers.
984 With prefix argument ARG, turn the resizing of windows displaying temporary
985 buffers on if ARG is positive or off otherwise.
986 This makes the window the right height for its contents, but never
987 more than `temp-buffer-max-height' nor less than `window-min-height'.
988 This applies to `help', `apropos' and `completion' buffers, and some others."
989 :global t :group 'help
990 (if temp-buffer-resize-mode
991 ;; `help-make-xrefs' may add a `back' button and thus increase the
992 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
993 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
994 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
995
996 (defun resize-temp-buffer-window ()
997 "Resize the selected window to fit its contents.
998 Will not make it higher than `temp-buffer-max-height' nor smaller than
999 `window-min-height'. Do nothing if it is the only window on its frame, if it
1000 is not as wide as the frame or if some of the window's contents are scrolled
1001 out of view."
1002 (unless (or (one-window-p 'nomini)
1003 (not (pos-visible-in-window-p (point-min)))
1004 (not (window-full-width-p)))
1005 (fit-window-to-buffer
1006 (selected-window)
1007 (if (functionp temp-buffer-max-height)
1008 (funcall temp-buffer-max-height (current-buffer))
1009 temp-buffer-max-height))))
1010
1011 \f
1012 ;;; help-window
1013
1014 (defcustom help-window-select 'other
1015 "Non-nil means select help window for viewing.
1016 Choices are:
1017 never (nil) Select help window only if there is no other window
1018 on its frame.
1019 other Select help window unless the selected window is the
1020 only other window on its frame.
1021 always (t) Always select the help window.
1022
1023 This option has effect if and only if the help window was created
1024 by `with-help-window'"
1025 :type '(choice (const :tag "never (nil)" nil)
1026 (const :tag "other" other)
1027 (const :tag "always (t)" t))
1028 :group 'help
1029 :version "23.1")
1030
1031 (defun help-window-display-message (quit-part window &optional other)
1032 "Display message telling how to quit and scroll help window.
1033 QUIT-PART is a string telling how to quit the help window WINDOW.
1034 Optional argument OTHER non-nil means return text telling how to
1035 scroll the \"other\" window."
1036 (let ((scroll-part
1037 (cond
1038 ((pos-visible-in-window-p
1039 (with-current-buffer (window-buffer window)
1040 (point-max)) window)
1041 ;; Buffer end is visible.
1042 ".")
1043 (other ", \\[scroll-other-window] to scroll help.")
1044 (t ", \\[scroll-up] to scroll help."))))
1045 (message "%s"
1046 (substitute-command-keys (concat quit-part scroll-part)))))
1047
1048 (defun help-window-setup-finish (window &optional reuse keep-frame)
1049 "Finish setting up help window WINDOW.
1050 Select WINDOW according to the value of `help-window-select'.
1051 Display message telling how to scroll and eventually quit WINDOW.
1052
1053 Optional argument REUSE non-nil means WINDOW has been reused by
1054 `display-buffer'. Optional argument KEEP-FRAME non-nil means
1055 that quitting should not delete WINDOW's frame."
1056 (let ((number-of-windows
1057 (length (window-list (window-frame window) 'no-mini window))))
1058 (cond
1059 ((eq window (selected-window))
1060 ;; The help window is the selected window, probably the
1061 ;; `pop-up-windows' nil case.
1062 (help-window-display-message
1063 (if reuse
1064 "Type \"q\" to restore this window"
1065 ;; This should not be taken.
1066 "Type \"q\" to quit") window))
1067 ((= number-of-windows 1)
1068 ;; The help window is alone on a frame and not the selected
1069 ;; window, could be the `pop-up-frames' t case.
1070 (help-window-display-message
1071 (cond
1072 (keep-frame "Type \"q\" to delete this window")
1073 (reuse "Type \"q\" to restore this window")
1074 (view-remove-frame-by-deleting "Type \"q\" to delete this frame")
1075 (t "Type \"q\" to iconify this frame"))
1076 window))
1077 ((and (= number-of-windows 2)
1078 (eq (window-frame window) (window-frame (selected-window))))
1079 ;; There are two windows on the help window's frame and the other
1080 ;; window is the selected one.
1081 (if (memq help-window-select '(nil other))
1082 ;; Do not select the help window.
1083 (help-window-display-message
1084 (if reuse
1085 ;; Offer `display-buffer' for consistency with
1086 ;; `help-print-return-message'. This is hardly TRT when
1087 ;; the other window and the selected window display the
1088 ;; same buffer but has been handled this way ever since.
1089 "Type \\[display-buffer] RET to restore the other window"
1090 ;; The classic "two windows" configuration.
1091 "Type \\[delete-other-windows] to delete the help window")
1092 window t)
1093 ;; Select help window and tell how to quit.
1094 (select-window window)
1095 (help-window-display-message
1096 (if reuse
1097 "Type \"q\" to restore this window"
1098 "Type \"q\" to delete this window") window)))
1099 (help-window-select
1100 ;; Issuing a message with 3 or more windows on the same frame
1101 ;; without selecting the help window doesn't make any sense.
1102 (select-window window)
1103 (help-window-display-message
1104 (if reuse
1105 "Type \"q\" to restore this window"
1106 "Type \"q\" to delete this window") window)))))
1107
1108 (defun help-window-setup (list-of-frames list-of-window-tuples)
1109 "Set up help window.
1110 LIST-OF-FRAMES and LIST-OF-WINDOW-TUPLES are the lists of frames
1111 and window quadruples built by `with-help-window'. The help
1112 window itself is specified by the variable `help-window'."
1113 (let* ((help-buffer (window-buffer help-window))
1114 ;; `help-buffer' now denotes the help window's buffer.
1115 (view-entry
1116 (assq help-window
1117 (buffer-local-value 'view-return-to-alist help-buffer)))
1118 (help-entry (assq help-window list-of-window-tuples)))
1119
1120 ;; Handle `help-window-point-marker'.
1121 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1122 (set-window-point help-window help-window-point-marker)
1123 ;; Reset `help-window-point-marker'.
1124 (set-marker help-window-point-marker nil))
1125
1126 (cond
1127 (view-entry
1128 ;; `view-return-to-alist' has an entry for the help window.
1129 (cond
1130 ((eq help-window (selected-window))
1131 ;; The help window is the selected window, probably because the
1132 ;; user followed a backward/forward button or a cross reference.
1133 ;; In this case just purge stale entries from
1134 ;; `view-return-to-alist' but leave the entry alone and don't
1135 ;; display a message.
1136 (view-return-to-alist-update help-buffer))
1137 ((and help-entry (eq (cadr help-entry) help-buffer))
1138 ;; The help window was not selected but displayed the help
1139 ;; buffer. In this case reuse existing exit information but try
1140 ;; to get back to the selected window when quitting. Don't
1141 ;; display a message since the user must have seen one before.
1142 (view-return-to-alist-update
1143 help-buffer (cons help-window
1144 (cons (selected-window) (cddr view-entry)))))
1145 (help-entry
1146 ;; The help window was not selected, did display the help buffer
1147 ;; earlier, but displayed another buffer when help was invoked.
1148 ;; Set up things so that quitting will show that buffer again.
1149 (view-return-to-alist-update
1150 help-buffer (cons help-window
1151 (cons (selected-window) (cdr help-entry))))
1152 (help-window-setup-finish help-window t))
1153 (t
1154 ;; The help window is new but `view-return-to-alist' had an
1155 ;; entry for it. This should never happen.
1156 (view-return-to-alist-update
1157 help-buffer (cons help-window
1158 (cons (selected-window) 'quit-window)))
1159 (help-window-setup-finish help-window t))))
1160 (help-entry
1161 ;; `view-return-to-alist' does not have an entry for help window
1162 ;; but `list-of-window-tuples' does. Hence `display-buffer' must
1163 ;; have reused an existing window.
1164 (if (eq (cadr help-entry) help-buffer)
1165 ;; The help window displayed `help-buffer' before but no
1166 ;; `view-return-to-alist' entry was found probably because the
1167 ;; user manually switched to the help buffer. Set up things
1168 ;; for `quit-window' although `view-exit-action' should be
1169 ;; able to handle this case all by itself.
1170 (progn
1171 (view-return-to-alist-update
1172 help-buffer (cons help-window
1173 (cons (selected-window) 'quit-window)))
1174 (help-window-setup-finish help-window t))
1175 ;; The help window displayed another buffer before. Set up
1176 ;; things in a way that quitting can orderly show that buffer
1177 ;; again. The window-start and window-point information from
1178 ;; `list-of-window-tuples' provide the necessary information.
1179 (view-return-to-alist-update
1180 help-buffer (cons help-window
1181 (cons (selected-window) (cdr help-entry))))
1182 (help-window-setup-finish help-window t)))
1183 ((memq (window-frame help-window) list-of-frames)
1184 ;; The help window is a new window on an existing frame. This
1185 ;; case must be handled specially by `help-window-setup-finish'
1186 ;; and `view-mode-exit' to ascertain that quitting does _not_
1187 ;; inadvertently delete the frame.
1188 (view-return-to-alist-update
1189 help-buffer (cons help-window
1190 (cons (selected-window) 'keep-frame)))
1191 (help-window-setup-finish help-window nil t))
1192 (t
1193 ;; The help window is shown on a new frame. In this case quitting
1194 ;; shall handle both, the help window _and_ its frame. We changed
1195 ;; the default of `view-remove-frame-by-deleting' to t in order to
1196 ;; intuitively DTRT here.
1197 (view-return-to-alist-update
1198 help-buffer (cons help-window (cons (selected-window) t)))
1199 (help-window-setup-finish help-window)))))
1200
1201 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1202 ;; providing the following additional twists:
1203
1204 ;; (1) Issue more accurate messages telling how to scroll and quit the
1205 ;; help window.
1206
1207 ;; (2) Make `view-mode-exit' DTRT in more cases.
1208
1209 ;; (3) An option (customizable via `help-window-select') to select the
1210 ;; help window automatically.
1211
1212 ;; (4) A marker (`help-window-point-marker') to move point in the help
1213 ;; window to an arbitrary buffer position.
1214
1215 ;; Note: It's usually always wrong to use `help-print-return-message' in
1216 ;; the body of `with-help-window'.
1217 (defmacro with-help-window (buffer-name &rest body)
1218 "Display buffer BUFFER-NAME in a help window evaluating BODY.
1219 Select help window if the actual value of the user option
1220 `help-window-select' says so. Return last value in BODY."
1221 (declare (indent 1) (debug t))
1222 ;; Bind list-of-frames to `frame-list' and list-of-window-tuples to a
1223 ;; list of one <window window-buffer window-start window-point> tuple
1224 ;; for each live window.
1225 `(let ((list-of-frames (frame-list))
1226 (list-of-window-tuples
1227 (let (list)
1228 (walk-windows
1229 (lambda (window)
1230 (push (list window (window-buffer window)
1231 (window-start window) (window-point window))
1232 list))
1233 'no-mini t)
1234 list)))
1235 ;; Make `help-window' t to trigger `help-mode-finish' to set
1236 ;; `help-window' to the actual help window.
1237 (setq help-window t)
1238 ;; Make `help-window-point-marker' point nowhere (the only place
1239 ;; where this should be set to a buffer position is within BODY).
1240 (set-marker help-window-point-marker nil)
1241 (prog1
1242 ;; Return value returned by `with-output-to-temp-buffer'.
1243 (with-output-to-temp-buffer ,buffer-name
1244 (progn ,@body))
1245 (when (windowp help-window)
1246 ;; Set up help window.
1247 (help-window-setup list-of-frames list-of-window-tuples))
1248 ;; Reset `help-window' to nil to avoid confusing future calls of
1249 ;; `help-mode-finish' with plain `with-output-to-temp-buffer'.
1250 (setq help-window nil))))
1251 \f
1252 (provide 'help)
1253
1254 ;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1255 ;;; help.el ends here