Merge from emacs-23; up to 2010-06-12T10:58:54Z!romain@orebokech.com.
[bpt/emacs.git] / lisp / help.el
1 ;;; help.el --- help commands for Emacs
2
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2011
4 ;; 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")
875 (let* ((mode major-mode)
876 (file-name (find-lisp-object-file-name mode nil)))
877 (when file-name
878 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
879 ;; Make a hyperlink to the library.
880 (with-current-buffer standard-output
881 (save-excursion
882 (re-search-backward "`\\([^`']+\\)'" nil t)
883 (help-xref-button 1 'help-function-def mode file-name)))))
884 (princ ":\n")
885 (princ (documentation major-mode)))))
886 ;; For the sake of IELM and maybe others
887 nil)
888
889
890 (defun describe-minor-mode (minor-mode)
891 "Display documentation of a minor mode given as MINOR-MODE.
892 MINOR-MODE can be a minor mode symbol or a minor mode indicator string
893 appeared on the mode-line."
894 (interactive (list (completing-read
895 "Minor mode: "
896 (nconc
897 (describe-minor-mode-completion-table-for-symbol)
898 (describe-minor-mode-completion-table-for-indicator)
899 ))))
900 (if (symbolp minor-mode)
901 (setq minor-mode (symbol-name minor-mode)))
902 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
903 (indicators (describe-minor-mode-completion-table-for-indicator)))
904 (cond
905 ((member minor-mode symbols)
906 (describe-minor-mode-from-symbol (intern minor-mode)))
907 ((member minor-mode indicators)
908 (describe-minor-mode-from-indicator minor-mode))
909 (t
910 (error "No such minor mode: %s" minor-mode)))))
911
912 ;; symbol
913 (defun describe-minor-mode-completion-table-for-symbol ()
914 ;; In order to list up all minor modes, minor-mode-list
915 ;; is used here instead of minor-mode-alist.
916 (delq nil (mapcar 'symbol-name minor-mode-list)))
917 (defun describe-minor-mode-from-symbol (symbol)
918 "Display documentation of a minor mode given as a symbol, SYMBOL"
919 (interactive (list (intern (completing-read
920 "Minor mode symbol: "
921 (describe-minor-mode-completion-table-for-symbol)))))
922 (if (fboundp symbol)
923 (describe-function symbol)
924 (describe-variable symbol)))
925
926 ;; indicator
927 (defun describe-minor-mode-completion-table-for-indicator ()
928 (delq nil
929 (mapcar (lambda (x)
930 (let ((i (format-mode-line x)))
931 ;; remove first space if existed
932 (cond
933 ((= 0 (length i))
934 nil)
935 ((eq (aref i 0) ?\s)
936 (substring i 1))
937 (t
938 i))))
939 minor-mode-alist)))
940 (defun describe-minor-mode-from-indicator (indicator)
941 "Display documentation of a minor mode specified by INDICATOR.
942 If you call this function interactively, you can give indicator which
943 is currently activated with completion."
944 (interactive (list
945 (completing-read
946 "Minor mode indicator: "
947 (describe-minor-mode-completion-table-for-indicator))))
948 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
949 (if minor-mode
950 (describe-minor-mode-from-symbol minor-mode)
951 (error "Cannot find minor mode for `%s'" indicator))))
952
953 (defun lookup-minor-mode-from-indicator (indicator)
954 "Return a minor mode symbol from its indicator on the modeline."
955 ;; remove first space if existed
956 (if (and (< 0 (length indicator))
957 (eq (aref indicator 0) ?\s))
958 (setq indicator (substring indicator 1)))
959 (let ((minor-modes minor-mode-alist)
960 result)
961 (while minor-modes
962 (let* ((minor-mode (car (car minor-modes)))
963 (anindicator (format-mode-line
964 (car (cdr (car minor-modes))))))
965 ;; remove first space if existed
966 (if (and (stringp anindicator)
967 (> (length anindicator) 0)
968 (eq (aref anindicator 0) ?\s))
969 (setq anindicator (substring anindicator 1)))
970 (if (equal indicator anindicator)
971 (setq result minor-mode
972 minor-modes nil)
973 (setq minor-modes (cdr minor-modes)))))
974 result))
975
976 \f
977 ;;; Automatic resizing of temporary buffers.
978
979 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
980 "Maximum height of a window displaying a temporary buffer.
981 This is effective only when Temp Buffer Resize mode is enabled.
982 The value is the maximum height (in lines) which `resize-temp-buffer-window'
983 will give to a window displaying a temporary buffer.
984 It can also be a function to be called to choose the height for such a buffer.
985 It gets one argumemt, the buffer, and should return a positive integer."
986 :type '(choice integer function)
987 :group 'help
988 :version "20.4")
989
990 (define-minor-mode temp-buffer-resize-mode
991 "Toggle the mode which makes windows smaller for temporary buffers.
992 With prefix argument ARG, turn the resizing of windows displaying temporary
993 buffers on if ARG is positive or off otherwise.
994 This makes the window the right height for its contents, but never
995 more than `temp-buffer-max-height' nor less than `window-min-height'.
996 This applies to `help', `apropos' and `completion' buffers, and some others."
997 :global t :group 'help
998 (if temp-buffer-resize-mode
999 ;; `help-make-xrefs' may add a `back' button and thus increase the
1000 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1001 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1002 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
1003
1004 (defun resize-temp-buffer-window ()
1005 "Resize the selected window to fit its contents.
1006 Will not make it higher than `temp-buffer-max-height' nor smaller than
1007 `window-min-height'. Do nothing if it is the only window on its frame, if it
1008 is not as wide as the frame or if some of the window's contents are scrolled
1009 out of view."
1010 (unless (or (one-window-p 'nomini)
1011 (not (pos-visible-in-window-p (point-min)))
1012 (not (window-full-width-p)))
1013 (fit-window-to-buffer
1014 (selected-window)
1015 (if (functionp temp-buffer-max-height)
1016 (funcall temp-buffer-max-height (current-buffer))
1017 temp-buffer-max-height))))
1018
1019 \f
1020 ;;; help-window
1021
1022 (defcustom help-window-select 'other
1023 "Non-nil means select help window for viewing.
1024 Choices are:
1025 never (nil) Select help window only if there is no other window
1026 on its frame.
1027 other Select help window unless the selected window is the
1028 only other window on its frame.
1029 always (t) Always select the help window.
1030
1031 This option has effect if and only if the help window was created
1032 by `with-help-window'"
1033 :type '(choice (const :tag "never (nil)" nil)
1034 (const :tag "other" other)
1035 (const :tag "always (t)" t))
1036 :group 'help
1037 :version "23.1")
1038
1039 (defun help-window-display-message (quit-part window &optional other)
1040 "Display message telling how to quit and scroll help window.
1041 QUIT-PART is a string telling how to quit the help window WINDOW.
1042 Optional argument OTHER non-nil means return text telling how to
1043 scroll the \"other\" window."
1044 (let ((scroll-part
1045 (cond
1046 ((pos-visible-in-window-p
1047 (with-current-buffer (window-buffer window)
1048 (point-max)) window)
1049 ;; Buffer end is visible.
1050 ".")
1051 (other ", \\[scroll-other-window] to scroll help.")
1052 (t ", \\[scroll-up] to scroll help."))))
1053 (message "%s"
1054 (substitute-command-keys (concat quit-part scroll-part)))))
1055
1056 (defun help-window-setup-finish (window &optional reuse keep-frame)
1057 "Finish setting up help window WINDOW.
1058 Select WINDOW according to the value of `help-window-select'.
1059 Display message telling how to scroll and eventually quit WINDOW.
1060
1061 Optional argument REUSE non-nil means WINDOW has been reused by
1062 `display-buffer'. Optional argument KEEP-FRAME non-nil means
1063 that quitting should not delete WINDOW's frame."
1064 (let ((number-of-windows
1065 (length (window-list (window-frame window) 'no-mini window))))
1066 (cond
1067 ((eq window (selected-window))
1068 ;; The help window is the selected window, probably the
1069 ;; `pop-up-windows' nil case.
1070 (help-window-display-message
1071 (if reuse
1072 "Type \"q\" to restore this window"
1073 ;; This should not be taken.
1074 "Type \"q\" to quit") window))
1075 ((= number-of-windows 1)
1076 ;; The help window is alone on a frame and not the selected
1077 ;; window, could be the `pop-up-frames' t case.
1078 (help-window-display-message
1079 (cond
1080 (keep-frame "Type \"q\" to delete this window")
1081 (reuse "Type \"q\" to restore this window")
1082 (view-remove-frame-by-deleting "Type \"q\" to delete this frame")
1083 (t "Type \"q\" to iconify this frame"))
1084 window))
1085 ((and (= number-of-windows 2)
1086 (eq (window-frame window) (window-frame (selected-window))))
1087 ;; There are two windows on the help window's frame and the other
1088 ;; window is the selected one.
1089 (if (memq help-window-select '(nil other))
1090 ;; Do not select the help window.
1091 (help-window-display-message
1092 (if reuse
1093 ;; Offer `display-buffer' for consistency with
1094 ;; `help-print-return-message'. This is hardly TRT when
1095 ;; the other window and the selected window display the
1096 ;; same buffer but has been handled this way ever since.
1097 "Type \\[display-buffer] RET to restore the other window"
1098 ;; The classic "two windows" configuration.
1099 "Type \\[delete-other-windows] to delete the help window")
1100 window t)
1101 ;; Select help window and tell how to quit.
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 (help-window-select
1108 ;; Issuing a message with 3 or more windows on the same frame
1109 ;; without selecting the help window doesn't make any sense.
1110 (select-window window)
1111 (help-window-display-message
1112 (if reuse
1113 "Type \"q\" to restore this window"
1114 "Type \"q\" to delete this window") window)))))
1115
1116 (defun help-window-setup (list-of-frames list-of-window-tuples)
1117 "Set up help window.
1118 LIST-OF-FRAMES and LIST-OF-WINDOW-TUPLES are the lists of frames
1119 and window quadruples built by `with-help-window'. The help
1120 window itself is specified by the variable `help-window'."
1121 (let* ((help-buffer (window-buffer help-window))
1122 ;; `help-buffer' now denotes the help window's buffer.
1123 (view-entry
1124 (assq help-window
1125 (buffer-local-value 'view-return-to-alist help-buffer)))
1126 (help-entry (assq help-window list-of-window-tuples)))
1127
1128 ;; Handle `help-window-point-marker'.
1129 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1130 (set-window-point help-window help-window-point-marker)
1131 ;; Reset `help-window-point-marker'.
1132 (set-marker help-window-point-marker nil))
1133
1134 (cond
1135 (view-entry
1136 ;; `view-return-to-alist' has an entry for the help window.
1137 (cond
1138 ((eq help-window (selected-window))
1139 ;; The help window is the selected window, probably because the
1140 ;; user followed a backward/forward button or a cross reference.
1141 ;; In this case just purge stale entries from
1142 ;; `view-return-to-alist' but leave the entry alone and don't
1143 ;; display a message.
1144 (view-return-to-alist-update help-buffer))
1145 ((and help-entry (eq (cadr help-entry) help-buffer))
1146 ;; The help window was not selected but displayed the help
1147 ;; buffer. In this case reuse existing exit information but try
1148 ;; to get back to the selected window when quitting. Don't
1149 ;; display a message since the user must have seen one before.
1150 (view-return-to-alist-update
1151 help-buffer (cons help-window
1152 (cons (selected-window) (cddr view-entry)))))
1153 (help-entry
1154 ;; The help window was not selected, did display the help buffer
1155 ;; earlier, but displayed another buffer when help was invoked.
1156 ;; Set up things so that quitting will show that buffer again.
1157 (view-return-to-alist-update
1158 help-buffer (cons help-window
1159 (cons (selected-window) (cdr help-entry))))
1160 (help-window-setup-finish help-window t))
1161 (t
1162 ;; The help window is new but `view-return-to-alist' had an
1163 ;; entry for it. This should never happen.
1164 (view-return-to-alist-update
1165 help-buffer (cons help-window
1166 (cons (selected-window) 'quit-window)))
1167 (help-window-setup-finish help-window t))))
1168 (help-entry
1169 ;; `view-return-to-alist' does not have an entry for help window
1170 ;; but `list-of-window-tuples' does. Hence `display-buffer' must
1171 ;; have reused an existing window.
1172 (if (eq (cadr help-entry) help-buffer)
1173 ;; The help window displayed `help-buffer' before but no
1174 ;; `view-return-to-alist' entry was found probably because the
1175 ;; user manually switched to the help buffer. Set up things
1176 ;; for `quit-window' although `view-exit-action' should be
1177 ;; able to handle this case all by itself.
1178 (progn
1179 (view-return-to-alist-update
1180 help-buffer (cons help-window
1181 (cons (selected-window) 'quit-window)))
1182 (help-window-setup-finish help-window t))
1183 ;; The help window displayed another buffer before. Set up
1184 ;; things in a way that quitting can orderly show that buffer
1185 ;; again. The window-start and window-point information from
1186 ;; `list-of-window-tuples' provide the necessary information.
1187 (view-return-to-alist-update
1188 help-buffer (cons help-window
1189 (cons (selected-window) (cdr help-entry))))
1190 (help-window-setup-finish help-window t)))
1191 ((memq (window-frame help-window) list-of-frames)
1192 ;; The help window is a new window on an existing frame. This
1193 ;; case must be handled specially by `help-window-setup-finish'
1194 ;; and `view-mode-exit' to ascertain that quitting does _not_
1195 ;; inadvertently delete the frame.
1196 (view-return-to-alist-update
1197 help-buffer (cons help-window
1198 (cons (selected-window) 'keep-frame)))
1199 (help-window-setup-finish help-window nil t))
1200 (t
1201 ;; The help window is shown on a new frame. In this case quitting
1202 ;; shall handle both, the help window _and_ its frame. We changed
1203 ;; the default of `view-remove-frame-by-deleting' to t in order to
1204 ;; intuitively DTRT here.
1205 (view-return-to-alist-update
1206 help-buffer (cons help-window (cons (selected-window) t)))
1207 (help-window-setup-finish help-window)))))
1208
1209 ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1210 ;; providing the following additional twists:
1211
1212 ;; (1) Issue more accurate messages telling how to scroll and quit the
1213 ;; help window.
1214
1215 ;; (2) Make `view-mode-exit' DTRT in more cases.
1216
1217 ;; (3) An option (customizable via `help-window-select') to select the
1218 ;; help window automatically.
1219
1220 ;; (4) A marker (`help-window-point-marker') to move point in the help
1221 ;; window to an arbitrary buffer position.
1222
1223 ;; Note: It's usually always wrong to use `help-print-return-message' in
1224 ;; the body of `with-help-window'.
1225 (defmacro with-help-window (buffer-name &rest body)
1226 "Display buffer BUFFER-NAME in a help window evaluating BODY.
1227 Select help window if the actual value of the user option
1228 `help-window-select' says so. Return last value in BODY."
1229 (declare (indent 1) (debug t))
1230 ;; Bind list-of-frames to `frame-list' and list-of-window-tuples to a
1231 ;; list of one <window window-buffer window-start window-point> tuple
1232 ;; for each live window.
1233 `(let ((list-of-frames (frame-list))
1234 (list-of-window-tuples
1235 (let (list)
1236 (walk-windows
1237 (lambda (window)
1238 (push (list window (window-buffer window)
1239 (window-start window) (window-point window))
1240 list))
1241 'no-mini t)
1242 list)))
1243 ;; Make `help-window' t to trigger `help-mode-finish' to set
1244 ;; `help-window' to the actual help window.
1245 (setq help-window t)
1246 ;; Make `help-window-point-marker' point nowhere (the only place
1247 ;; where this should be set to a buffer position is within BODY).
1248 (set-marker help-window-point-marker nil)
1249 (prog1
1250 ;; Return value returned by `with-output-to-temp-buffer'.
1251 (with-output-to-temp-buffer ,buffer-name
1252 (progn ,@body))
1253 (when (windowp help-window)
1254 ;; Set up help window.
1255 (help-window-setup list-of-frames list-of-window-tuples))
1256 ;; Reset `help-window' to nil to avoid confusing future calls of
1257 ;; `help-mode-finish' with plain `with-output-to-temp-buffer'.
1258 (setq help-window nil))))
1259
1260 ;; Called from C, on encountering `help-char' when reading a char.
1261 ;; Don't print to *Help*; that would clobber Help history.
1262 (defun help-form-show ()
1263 "Display the output of a non-nil `help-form'."
1264 (let ((msg (eval help-form)))
1265 (if (stringp msg)
1266 (with-output-to-temp-buffer " *Char Help*"
1267 (princ msg)))))
1268 \f
1269 (provide 'help)
1270
1271 ;;; help.el ends here