* cedet/ede/speedbar.el (ede-speedbar-file-setup): Add autoload.
[bpt/emacs.git] / lisp / help.el
CommitLineData
1a06eabd
ER
1;;; help.el --- help commands for Emacs
2
0d30b337 3;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002,
ae940284 4;; 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
3a801d0c 5
e5167999 6;; Maintainer: FSF
fd7fa35a 7;; Keywords: help, internal
e5167999 8
433ae6f6
RS
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
433ae6f6 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
433ae6f6
RS
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
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
433ae6f6 23
d9ecc911
ER
24;;; Commentary:
25
a1c9f209 26;; This code implements GNU Emacs' on-line help system, the one invoked by
95ac0a6f 27;; `M-x help-for-help'.
d9ecc911 28
e5167999
ER
29;;; Code:
30
8aa3a187
RS
31;; Get the macro make-help-screen when this is compiled,
32;; or run interpreted, but not when the compiled code is loaded.
b1fe9304 33(eval-when-compile (require 'help-macro))
788d62cf
MB
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)
41b8542b 38
cde56121
MR
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'.
b3d8e4a0 45;; Exiting `with-help-window' and calling `help-print-return-message'
cde56121
MR
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.
7b01c8d7 56(defvar help-window-point-marker (make-marker)
cde56121
MR
57 "Marker to override default `window-point' of `help-window'.")
58
604aa5f0
SM
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
d4a18332 67 (define-key map "\C-a" 'about-emacs)
604aa5f0 68 (define-key map "\C-c" 'describe-copying)
7b01c8d7
KS
69 (define-key map "\C-d" 'view-emacs-debugging)
70 (define-key map "\C-e" 'view-external-packages)
604aa5f0
SM
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)
7b01c8d7
KS
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)
604aa5f0
SM
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)
7b01c8d7 95 (define-key map "g" 'describe-gnu-project)
604aa5f0
SM
96 (define-key map "h" 'view-hello-file)
97
98 (define-key map "i" 'info)
99 (define-key map "4i" 'info-other-window)
100
101 (define-key map "k" 'describe-key)
102 (define-key map "l" 'view-lossage)
103 (define-key map "m" 'describe-mode)
104 (define-key map "n" 'view-emacs-news)
105 (define-key map "p" 'finder-by-keyword)
106 (define-key map "r" 'info-emacs-manual)
107 (define-key map "s" 'describe-syntax)
108 (define-key map "t" 'help-with-tutorial)
109 (define-key map "w" 'where-is)
110 (define-key map "v" 'describe-variable)
111 (define-key map "q" 'help-quit)
112 map)
433ae6f6
RS
113 "Keymap for characters following the Help key.")
114
e17d2fd1 115(define-key global-map (char-to-string help-char) 'help-command)
0af3df1c
RS
116(define-key global-map [help] 'help-command)
117(define-key global-map [f1] 'help-command)
433ae6f6
RS
118(fset 'help-command help-map)
119
3e9c095d
RS
120(autoload 'finder-by-keyword "finder"
121 "Find packages matching a given keyword." t)
06b98c51 122
e25e90b4
DP
123;; insert-button makes the action nil if it is not store somewhere
124(defvar help-button-cache nil)
125
0cf0d828 126\f
2fc9d9f4 127(defun help-quit ()
3120a677 128 "Just exit from the Help command's command loop."
2fc9d9f4
RS
129 (interactive)
130 nil)
131
01364a75
RS
132(defvar help-return-method nil
133 "What to do to \"exit\" the help buffer.
134This is a list
a8e7142c
EZ
135 (WINDOW . t) delete the selected window (and possibly its frame,
136 see `quit-window' and `View-quit'), go to WINDOW.
01364a75
RS
137 (WINDOW . quit-window) do quit-window, then select WINDOW.
138 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
139
b3d8e4a0
SM
140(define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
141(defun help-print-return-message (&optional function)
433ae6f6 142 "Display or return message saying how to restore windows after help command.
d009b6e9
RS
143This function assumes that `standard-output' is the help buffer.
144It computes a message, and applies the optional argument FUNCTION to it.
a8e7142c
EZ
145If FUNCTION is nil, it applies `message', thus displaying the message.
146In addition, this function sets up `help-return-method', which see, that
147specifies what to do when the user exits the help buffer."
cde56121
MR
148 ;; Reset `help-window' here to avoid confusing `help-mode-finish'.
149 (setq help-window nil)
433ae6f6 150 (and (not (get-buffer-window standard-output))
d536293f 151 (let ((first-message
a8e7142c
EZ
152 (cond ((or
153 pop-up-frames
154 (special-display-p (buffer-name standard-output)))
01364a75 155 (setq help-return-method (cons (selected-window) t))
d536293f
RS
156 ;; If the help output buffer is a special display buffer,
157 ;; don't say anything about how to get rid of it.
158 ;; First of all, the user will do that with the window
159 ;; manager, not with Emacs.
160 ;; Secondly, the buffer has not been displayed yet,
161 ;; so we don't know whether its frame will be selected.
d536293f 162 nil)
f3ad2fc8
GM
163 (display-buffer-reuse-frames
164 (setq help-return-method (cons (selected-window)
165 'quit-window))
166 nil)
d536293f 167 ((not (one-window-p t))
01364a75
RS
168 (setq help-return-method
169 (cons (selected-window) 'quit-window))
cb0b6766 170 "Type \\[display-buffer] RET to restore the other window.")
d536293f 171 (pop-up-windows
01364a75 172 (setq help-return-method (cons (selected-window) t))
d536293f
RS
173 "Type \\[delete-other-windows] to remove help window.")
174 (t
01364a75
RS
175 (setq help-return-method
176 (list (selected-window) (window-buffer)
177 (window-start) (window-point)))
d536293f
RS
178 "Type \\[switch-to-buffer] RET to remove help window."))))
179 (funcall (or function 'message)
180 (concat
181 (if first-message
376b2a24
DL
182 (substitute-command-keys first-message))
183 (if first-message " ")
125a8d70
RS
184 ;; If the help buffer will go in a separate frame,
185 ;; it's no use mentioning a command to scroll, so don't.
7b057a3d
EZ
186 (if (or pop-up-windows
187 (special-display-p (buffer-name standard-output)))
125a8d70 188 nil
a1c9f209 189 (if (same-window-p (buffer-name standard-output))
125a8d70
RS
190 ;; Say how to scroll this window.
191 (substitute-command-keys
192 "\\[scroll-up] to scroll the help.")
193 ;; Say how to scroll some other window.
6e7f5182 194 (substitute-command-keys
125a8d70 195 "\\[scroll-other-window] to scroll the help."))))))))
433ae6f6 196
433ae6f6
RS
197;; So keyboard macro definitions are documented correctly
198(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
199
a4bdcdf3
RS
200(defalias 'help 'help-for-help-internal)
201;; find-function can find this.
202(defalias 'help-for-help 'help-for-help-internal)
203;; It can't find this, but nobody will look.
204(make-help-screen help-for-help-internal
7b01c8d7 205 "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?"
788d62cf
MB
206 "You have typed %THIS-KEY%, the help character. Type a Help option:
207\(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
208
7b01c8d7
KS
209a PATTERN Show commands whose name matches the PATTERN (a list of words
210 or a regexp). See also the `apropos' command.
211b Display all key bindings.
212c KEYS Display the command name run by the given key sequence.
213C CODING Describe the given coding system, or RET for current ones.
214d PATTERN Show a list of functions, variables, and other items whose
215 documentation matches the PATTERN (a list of words or a regexp).
216e Go to the *Messages* buffer which logs echo-area messages.
217f FUNCTION Display documentation for the given function.
218F COMMAND Show the on-line manual's section that describes the command.
219g Display information about the GNU project.
220h Display the HELLO file which illustrates various scripts.
221i Start the Info documentation reader: read on-line manuals.
222I METHOD Describe a specific input method, or RET for current.
223k KEYS Display the full documentation for the key sequence.
224K KEYS Show the on-line manual's section for the command bound to KEYS.
90ad3541 225l Show last 300 input keystrokes (lossage).
7b01c8d7
KS
226L LANG-ENV Describes a specific language environment, or RET for current.
227m Display documentation of current minor modes and current major mode,
228 including their special commands.
229n Display news of recent Emacs changes.
230p TOPIC Find packages matching a given topic keyword.
231r Display the Emacs manual in Info mode.
232s Display contents of current syntax table, plus explanations.
233S SYMBOL Show the section for the given symbol in the on-line manual
234 for the programming language used in this buffer.
235t Start the Emacs learn-by-doing tutorial.
236v VARIABLE Display the given variable's documentation and value.
237w COMMAND Display which keystrokes invoke the given command (where-is).
238. Display any available local help at point in the echo area.
239
240C-a Information about Emacs.
241C-c Emacs copying permission (GNU General Public License).
242C-d Instructions for debugging GNU Emacs.
243C-e External packages and information about Emacs.
244C-f Emacs FAQ.
245C-m How to order printed Emacs manuals.
246C-n News of recent Emacs changes.
247C-o Emacs ordering and distribution information.
248C-p Info about known Emacs problems.
249C-t Emacs TODO list.
250C-w Information on absence of warranty for GNU Emacs."
788d62cf
MB
251 help-map)
252
253\f
254
255(defun function-called-at-point ()
256 "Return a function around point or else called by the list containing point.
257If that doesn't give a function, return nil."
2e6750c8
SM
258 (with-syntax-table emacs-lisp-mode-syntax-table
259 (or (condition-case ()
260 (save-excursion
261 (or (not (zerop (skip-syntax-backward "_w")))
262 (eq (char-syntax (following-char)) ?w)
263 (eq (char-syntax (following-char)) ?_)
264 (forward-sexp -1))
265 (skip-chars-forward "'")
266 (let ((obj (read (current-buffer))))
267 (and (symbolp obj) (fboundp obj) obj)))
268 (error nil))
269 (condition-case ()
270 (save-excursion
271 (save-restriction
272 (narrow-to-region (max (point-min)
273 (- (point) 1000)) (point-max))
274 ;; Move up to surrounding paren, then after the open.
275 (backward-up-list 1)
276 (forward-char 1)
277 ;; If there is space here, this is probably something
278 ;; other than a real Lisp function call, so ignore it.
279 (if (looking-at "[ \t]")
280 (error "Probably not a Lisp function call"))
281 (let ((obj (read (current-buffer))))
282 (and (symbolp obj) (fboundp obj) obj))))
283 (error nil))
284 (let* ((str (find-tag-default))
285 (sym (if str (intern-soft str))))
286 (if (and sym (fboundp sym))
287 sym
288 (save-match-data
289 (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
290 (setq sym (intern-soft (match-string 1 str)))
291 (and (fboundp sym) sym))))))))
788d62cf
MB
292
293\f
294;;; `User' help functions
295
7b01c8d7
KS
296(defun view-help-file (file &optional dir)
297 (view-file (expand-file-name file (or dir data-directory)))
3ffb948c 298 (goto-address-mode 1)
7b01c8d7
KS
299 (goto-char (point-min)))
300
433ae6f6
RS
301(defun describe-distribution ()
302 "Display info on how to obtain the latest version of GNU Emacs."
303 (interactive)
7b01c8d7 304 (view-help-file "DISTRIB"))
433ae6f6
RS
305
306(defun describe-copying ()
307 "Display info on how you may redistribute copies of GNU Emacs."
308 (interactive)
7b01c8d7 309 (view-help-file "COPYING"))
433ae6f6 310
7b01c8d7 311(defun describe-gnu-project ()
76766f2d
RS
312 "Display info on the GNU project."
313 (interactive)
7b01c8d7 314 (view-help-file "THE-GNU-PROJECT"))
76766f2d 315
4e44f5ce
KS
316(define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
317
433ae6f6
RS
318(defun describe-no-warranty ()
319 "Display info on all the kinds of warranty Emacs does NOT have."
320 (interactive)
321 (describe-copying)
322 (let (case-fold-search)
323 (search-forward "NO WARRANTY")
324 (recenter 0)))
325
61c6b658 326(defun describe-prefix-bindings ()
c7cba9cb
RS
327 "Describe the bindings of the prefix used to reach this command.
328The prefix described consists of all but the last event
329of the key sequence that ran this command."
61c6b658 330 (interactive)
0f101663 331 (let ((key (this-command-keys)))
ccc06dcc
KH
332 (describe-bindings
333 (if (stringp key)
334 (substring key 0 (1- (length key)))
335 (let ((prefix (make-vector (1- (length key)) nil))
336 (i 0))
337 (while (< i (length prefix))
338 (aset prefix i (aref key i))
339 (setq i (1+ i)))
340 prefix)))))
788d62cf 341;; Make C-h after a prefix, when not specifically bound,
c7cba9cb 342;; run describe-prefix-bindings.
61c6b658
RS
343(setq prefix-help-command 'describe-prefix-bindings)
344
e38cc268 345(defun view-emacs-news (&optional version)
382d018a 346 "Display info on recent changes to Emacs.
598ea453 347With argument, display info only for the selected version."
382d018a 348 (interactive "P")
e38cc268
KS
349 (unless version
350 (setq version emacs-major-version))
351 (when (consp version)
352 (let* ((all-versions
353 (let (res)
cc6650af 354 (mapc
e38cc268
KS
355 (lambda (file)
356 (with-temp-buffer
357 (insert-file-contents
358 (expand-file-name file data-directory))
359 (while (re-search-forward
360 (if (member file '("NEWS.18" "NEWS.1-17"))
361 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
362 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
363 (setq res (cons (match-string-no-properties 1) res)))))
364 (cons "NEWS"
365 (directory-files data-directory nil
366 "^NEWS\\.[0-9][-0-9]*$" nil)))
367 (sort (delete-dups res) (lambda (a b) (string< b a)))))
8c9fc4be 368 (current (car all-versions)))
e38cc268
KS
369 (setq version (completing-read
370 (format "Read NEWS for the version (default %s): " current)
371 all-versions nil nil nil nil current))
372 (if (integerp (string-to-number version))
373 (setq version (string-to-number version))
374 (unless (or (member version all-versions)
375 (<= (string-to-number version) (string-to-number current)))
376 (error "No news about version %s" version)))))
377 (when (integerp version)
378 (cond ((<= version 12)
379 (setq version (format "1.%d" version)))
380 ((<= version 18)
381 (setq version (format "%d" version)))
382 ((> version emacs-major-version)
e08b54f2 383 (error "No news about Emacs %d (yet)" version))))
e38cc268
KS
384 (let* ((vn (if (stringp version)
385 (string-to-number version)
386 version))
387 (file (cond
388 ((>= vn emacs-major-version) "NEWS")
389 ((< vn 18) "NEWS.1-17")
8c9fc4be
KS
390 (t (format "NEWS.%d" vn))))
391 res)
e38cc268
KS
392 (view-file (expand-file-name file data-directory))
393 (widen)
394 (goto-char (point-min))
395 (when (stringp version)
396 (when (re-search-forward
397 (concat (if (< vn 19)
398 "Changes in Emacs[ \t]*"
399 "^\* [^0-9\n]*") version "$")
400 nil t)
401 (beginning-of-line)
402 (narrow-to-region
403 (point)
404 (save-excursion
405 (while (and (setq res
406 (re-search-forward
407 (if (< vn 19)
408 "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
409 "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
410 (equal (match-string-no-properties 1) version)))
411 (or res (goto-char (point-max)))
412 (beginning-of-line)
413 (point)))))))
414
7b01c8d7 415(defun view-emacs-todo (&optional arg)
1ebc1f01
RS
416 "Display the Emacs TODO list."
417 (interactive "P")
7b01c8d7 418 (view-help-file "TODO"))
1ebc1f01 419
4e44f5ce
KS
420(define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
421
422
4f16ea85
RS
423(defun view-echo-area-messages ()
424 "View the log of recent echo-area messages: the `*Messages*' buffer.
425The number of messages retained in that buffer
426is specified by the variable `message-log-max'."
427 (interactive)
428 (switch-to-buffer (get-buffer-create "*Messages*")))
429
754084bb
GM
430(defun view-order-manuals ()
431 "Display the Emacs ORDERS file."
432 (interactive)
7b01c8d7 433 (view-help-file "ORDERS"))
754084bb 434
7ee71cf1
RS
435(defun view-emacs-FAQ ()
436 "Display the Emacs Frequently Asked Questions (FAQ) file."
437 (interactive)
94ea540b 438 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
279b772d 439 (info "(efaq)"))
7ee71cf1 440
4cbff657
DL
441(defun view-emacs-problems ()
442 "Display info on known problems with Emacs and possible workarounds."
443 (interactive)
7b01c8d7
KS
444 (view-help-file "PROBLEMS"))
445
446(defun view-emacs-debugging ()
447 "Display info on how to debug Emacs problems."
448 (interactive)
449 (view-help-file "DEBUG"))
450
451(defun view-external-packages ()
452 "Display external packages and information about Emacs."
453 (interactive)
454 (view-help-file "MORE.STUFF"))
4cbff657 455
433ae6f6 456(defun view-lossage ()
6b8d1c72 457 "Display last 300 input keystrokes.
50b57199
EZ
458
459To record all your input on a file, use `open-dribble-file'."
433ae6f6 460 (interactive)
3e5929af 461 (help-setup-xref (list #'view-lossage) (interactive-p))
cde56121 462 (with-help-window (help-buffer)
02dfca16
SM
463 (princ (mapconcat (lambda (key)
464 (if (or (integerp key) (symbolp key) (listp key))
465 (single-key-description key)
466 (prin1-to-string key nil)))
298a7c8c
RS
467 (recent-keys)
468 " "))
b0fbf754 469 (with-current-buffer standard-output
433ae6f6
RS
470 (goto-char (point-min))
471 (while (progn (move-to-column 50) (not (eobp)))
e5fe3a6c
JB
472 (when (search-forward " " nil t)
473 (delete-char -1))
cde56121
MR
474 (insert "\n"))
475 ;; jidanni wants to see the last keystrokes immediately.
476 (set-marker help-window-point-marker (point)))))
433ae6f6 477
788d62cf
MB
478\f
479;; Key bindings
433ae6f6 480
4c45295b 481(defun describe-bindings (&optional prefix buffer)
a8ad43aa
RS
482 "Show a list of all defined keys, and their definitions.
483We put that list in a buffer, and display the buffer.
484
485The optional argument PREFIX, if non-nil, should be a key sequence;
4c45295b
KH
486then we display only bindings that start with that prefix.
487The optional argument BUFFER specifies which buffer's bindings
abca4ad7
LT
488to display (default, the current buffer). BUFFER can be a buffer
489or a buffer name."
3e5929af 490 (interactive)
4c45295b 491 (or buffer (setq buffer (current-buffer)))
3e5929af 492 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
4c45295b 493 (with-current-buffer buffer
3e5929af 494 (describe-bindings-internal nil prefix)))
a8ad43aa 495
94ea540b
SM
496;; This function used to be in keymap.c.
497(defun describe-bindings-internal (&optional menus prefix)
498 "Show a list of all defined keys, and their definitions.
499We put that list in a buffer, and display the buffer.
500
501The optional argument MENUS, if non-nil, says to mention menu bindings.
502\(Ordinarily these are omitted from the output.)
503The optional argument PREFIX, if non-nil, should be a key sequence;
504then we display only bindings that start with that prefix."
505 (interactive)
506 (let ((buf (current-buffer)))
cde56121 507 (with-help-window "*Help*"
94ea540b
SM
508 (with-current-buffer standard-output
509 (describe-buffer-bindings buf prefix menus)))))
510
e88a2c59 511(defun where-is (definition &optional insert)
b2c85790 512 "Print message listing key sequences that invoke the command DEFINITION.
e88a2c59
RS
513Argument is a command definition, usually a symbol with a function definition.
514If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
54c0b967
RS
515 (interactive
516 (let ((fn (function-called-at-point))
788d62cf 517 (enable-recursive-minibuffers t)
54c0b967 518 val)
3829bcc5
SM
519 (setq val (completing-read
520 (if fn
521 (format "Where is command (default %s): " fn)
522 "Where is command: ")
523 obarray 'commandp t))
524 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
463d75ac 525 (unless definition (error "No command"))
7a698dc1 526 (let ((func (indirect-function definition))
3829bcc5 527 (defs nil)
7a698dc1 528 (standard-output (if insert (current-buffer) t)))
740b479c 529 ;; In DEFS, find all symbols that are aliases for DEFINITION.
3829bcc5
SM
530 (mapatoms (lambda (symbol)
531 (and (fboundp symbol)
532 (not (eq symbol definition))
d92c2757
RS
533 (eq func (condition-case ()
534 (indirect-function symbol)
535 (error symbol)))
3829bcc5 536 (push symbol defs))))
740b479c
RS
537 ;; Look at all the symbols--first DEFINITION,
538 ;; then its aliases.
539 (dolist (symbol (cons definition defs))
540 (let* ((remapped (command-remapping symbol))
541 (keys (where-is-internal
542 symbol overriding-local-map nil nil remapped))
543 (keys (mapconcat 'key-description keys ", "))
544 string)
545 (setq string
546 (if insert
547 (if (> (length keys) 0)
548 (if remapped
549 (format "%s (%s) (remapped from %s)"
550 keys remapped symbol)
551 (format "%s (%s)" keys symbol))
552 (format "M-x %s RET" symbol))
553 (if (> (length keys) 0)
554 (if remapped
555 (format "%s is remapped to %s which is on %s"
a5f43550 556 symbol remapped keys)
740b479c
RS
557 (format "%s is on %s" symbol keys))
558 ;; If this is the command the user asked about,
559 ;; and it is not on any key, say so.
560 ;; For other symbols, its aliases, say nothing
561 ;; about them unless they are on keys.
562 (if (eq symbol definition)
563 (format "%s is not on any key" symbol)))))
564 (when string
565 (unless (eq symbol definition)
566 (princ ";\n its alias "))
567 (princ string)))))
54c0b967
RS
568 nil)
569
02dfca16
SM
570(defun help-key-description (key untranslated)
571 (let ((string (key-description key)))
ae1bb8ac
SM
572 (if (or (not untranslated)
573 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
02dfca16
SM
574 string
575 (let ((otherstring (key-description untranslated)))
576 (if (equal string otherstring)
577 string
578 (format "%s (translated from %s)" string otherstring))))))
71296446 579
6527c983
EZ
580(defun describe-key-briefly (&optional key insert untranslated)
581 "Print the name of the function KEY invokes. KEY is a string.
582If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
583If non-nil, UNTRANSLATED is a vector of the untranslated events.
584It can also be a number in which case the untranslated events from
585the last key hit are used.
586
587If KEY is a menu item or a tool-bar button that is disabled, this command
8ee320fc 588temporarily enables it to allow getting help on disabled items and buttons."
2c8ed538
RS
589 (interactive
590 (let ((enable-disabled-menus-and-buttons t)
591 (cursor-in-echo-area t)
592 saved-yank-menu)
593 (unwind-protect
594 (let (key)
595 ;; If yank-menu is empty, populate it temporarily, so that
596 ;; "Select and Paste" menu can generate a complete event.
597 (when (null (cdr yank-menu))
598 (setq saved-yank-menu (copy-sequence yank-menu))
599 (menu-bar-update-yank-menu "(any string)" nil))
600 (setq key (read-key-sequence "Describe key (or click or menu item): "))
46d91fa0 601 ;; If KEY is a down-event, read and discard the
91a2acb2
DK
602 ;; corresponding up-event. Note that there are also
603 ;; down-events on scroll bars and mode lines: the actual
604 ;; event then is in the second element of the vector.
605 (and (vectorp key)
badf89ea
RS
606 (let ((last-idx (1- (length key))))
607 (and (eventp (aref key last-idx))
608 (memq 'down (event-modifiers (aref key last-idx)))))
91a2acb2 609 (read-event))
2c8ed538
RS
610 (list
611 key
774a814f
RS
612 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
613 1))
2c8ed538
RS
614 ;; Put yank-menu back as it was, if we changed it.
615 (when saved-yank-menu
616 (setq yank-menu (copy-sequence saved-yank-menu))
617 (fset 'yank-menu (cons 'keymap yank-menu))))))
02dfca16
SM
618 (if (numberp untranslated)
619 (setq untranslated (this-single-command-raw-keys)))
91a2acb2
DK
620 (let* ((event (if (and (symbolp (aref key 0))
621 (> (length key) 1)
622 (consp (aref key 1)))
623 (aref key 1)
624 (aref key 0)))
625 (modifiers (event-modifiers event))
626 (standard-output (if insert (current-buffer) t))
24a27882
KS
627 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
628 (memq 'drag modifiers)) " at that spot" ""))
629 (defn (key-binding key t))
630 key-desc)
631 ;; Handle the case where we faked an entry in "Select and Paste" menu.
632 (if (and (eq defn nil)
633 (stringp (aref key (1- (length key))))
634 (eq (key-binding (substring key 0 -1)) 'yank-menu))
635 (setq defn 'menu-bar-select-yank))
636 ;; Don't bother user with strings from (e.g.) the select-paste menu.
637 (if (stringp (aref key (1- (length key))))
638 (aset key (1- (length key)) "(any string)"))
639 (if (and (> (length untranslated) 0)
640 (stringp (aref untranslated (1- (length untranslated)))))
641 (aset untranslated (1- (length untranslated)) "(any string)"))
642 ;; Now describe the key, perhaps as changed.
643 (setq key-desc (help-key-description key untranslated))
644 (if (or (null defn) (integerp defn) (equal defn 'undefined))
645 (princ (format "%s%s is undefined" key-desc mouse-msg))
646 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
96ede6b2 647
6527c983
EZ
648(defun describe-key (&optional key untranslated up-event)
649 "Display documentation of the function invoked by KEY.
650KEY can be any kind of a key sequence; it can include keyboard events,
651mouse events, and/or menu events. When calling from a program,
652pass KEY as a string or a vector.
653
654If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
655It can also be a number, in which case the untranslated events from
656the last key sequence entered are used.
657UP-EVENT is the up-event that was discarded by reading KEY, or nil.
658
659If KEY is a menu item or a tool-bar button that is disabled, this command
8ee320fc 660temporarily enables it to allow getting help on disabled items and buttons."
2c8ed538
RS
661 (interactive
662 (let ((enable-disabled-menus-and-buttons t)
663 (cursor-in-echo-area t)
664 saved-yank-menu)
665 (unwind-protect
666 (let (key)
667 ;; If yank-menu is empty, populate it temporarily, so that
668 ;; "Select and Paste" menu can generate a complete event.
669 (when (null (cdr yank-menu))
670 (setq saved-yank-menu (copy-sequence yank-menu))
671 (menu-bar-update-yank-menu "(any string)" nil))
672 (setq key (read-key-sequence "Describe key (or click or menu item): "))
673 (list
674 key
675 (prefix-numeric-value current-prefix-arg)
c3f82997 676 ;; If KEY is a down-event, read and include the
badf89ea
RS
677 ;; corresponding up-event. Note that there are also
678 ;; down-events on scroll bars and mode lines: the actual
679 ;; event then is in the second element of the vector.
91a2acb2 680 (and (vectorp key)
badf89ea
RS
681 (let ((last-idx (1- (length key))))
682 (and (eventp (aref key last-idx))
683 (memq 'down (event-modifiers (aref key last-idx)))))
91a2acb2 684 (or (and (eventp (aref key 0))
98da283b
CY
685 (memq 'down (event-modifiers (aref key 0)))
686 ;; However, for the C-down-mouse-2 popup
687 ;; menu, there is no subsequent up-event. In
688 ;; this case, the up-event is the next
689 ;; element in the supplied vector.
690 (= (length key) 1))
91a2acb2
DK
691 (and (> (length key) 1)
692 (eventp (aref key 1))
693 (memq 'down (event-modifiers (aref key 1)))))
694 (read-event))))
2c8ed538
RS
695 ;; Put yank-menu back as it was, if we changed it.
696 (when saved-yank-menu
697 (setq yank-menu (copy-sequence saved-yank-menu))
698 (fset 'yank-menu (cons 'keymap yank-menu))))))
02dfca16
SM
699 (if (numberp untranslated)
700 (setq untranslated (this-single-command-raw-keys)))
05ca18a8
KS
701 (let* ((event (aref key (if (and (symbolp (aref key 0))
702 (> (length key) 1)
703 (consp (aref key 1)))
704 1
705 0)))
91a2acb2 706 (modifiers (event-modifiers event))
24a27882
KS
707 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
708 (memq 'drag modifiers)) " at that spot" ""))
05ca18a8
KS
709 (defn (key-binding key t))
710 defn-up defn-up-tricky ev-type
711 mouse-1-remapped mouse-1-tricky)
91a2acb2 712
05ca18a8 713 ;; Handle the case where we faked an entry in "Select and Paste" menu.
24a27882 714 (when (and (eq defn nil)
91a2acb2
DK
715 (stringp (aref key (1- (length key))))
716 (eq (key-binding (substring key 0 -1)) 'yank-menu))
24a27882
KS
717 (setq defn 'menu-bar-select-yank))
718 (if (or (null defn) (integerp defn) (equal defn 'undefined))
719 (message "%s%s is undefined"
720 (help-key-description key untranslated) mouse-msg)
721 (help-setup-xref (list #'describe-function defn) (interactive-p))
722 ;; Don't bother user with strings from (e.g.) the select-paste menu.
723 (when (stringp (aref key (1- (length key))))
724 (aset key (1- (length key)) "(any string)"))
725 (when (and untranslated
91a2acb2 726 (stringp (aref untranslated (1- (length untranslated)))))
24a27882
KS
727 (aset untranslated (1- (length untranslated))
728 "(any string)"))
729 ;; Need to do this before erasing *Help* buffer in case event
730 ;; is a mouse click in an existing *Help* buffer.
731 (when up-event
732 (setq ev-type (event-basic-type up-event))
733 (let ((sequence (vector up-event)))
734 (when (and (eq ev-type 'mouse-1)
735 mouse-1-click-follows-link
736 (not (eq mouse-1-click-follows-link 'double))
737 (setq mouse-1-remapped
738 (mouse-on-link-p (event-start up-event))))
739 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
740 (> mouse-1-click-follows-link 0)))
741 (cond ((stringp mouse-1-remapped)
742 (setq sequence mouse-1-remapped))
743 ((vectorp mouse-1-remapped)
744 (setcar up-event (elt mouse-1-remapped 0)))
745 (t (setcar up-event 'mouse-2))))
746 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
747 (when mouse-1-tricky
748 (setq sequence (vector up-event))
749 (aset sequence 0 'mouse-1)
750 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
cde56121 751 (with-help-window (help-buffer)
24a27882
KS
752 (princ (help-key-description key untranslated))
753 (princ (format "\
b96817c3 754%s runs the command %S, which is "
24a27882
KS
755 mouse-msg defn))
756 (describe-function-1 defn)
05ca18a8 757 (when up-event
24a27882
KS
758 (unless (or (null defn-up)
759 (integerp defn-up)
760 (equal defn-up 'undefined))
761 (princ (format "
762
763----------------- up-event %s----------------
764
b96817c3 765<%S>%s%s runs the command %S, which is "
24a27882
KS
766 (if mouse-1-tricky "(short click) " "")
767 ev-type mouse-msg
768 (if mouse-1-remapped
b96817c3 769 " is remapped to <mouse-2>, which" "")
24a27882
KS
770 defn-up))
771 (describe-function-1 defn-up))
772 (unless (or (null defn-up-tricky)
773 (integerp defn-up-tricky)
774 (eq defn-up-tricky 'undefined))
775 (princ (format "
776
777----------------- up-event (long click) ----------------
778
779Pressing <%S>%s for longer than %d milli-seconds
b96817c3 780runs the command %S, which is "
24a27882
KS
781 ev-type mouse-msg
782 mouse-1-click-follows-link
783 defn-up-tricky))
cde56121 784 (describe-function-1 defn-up-tricky)))))))
400a1b1f 785\f
788d62cf
MB
786(defun describe-mode (&optional buffer)
787 "Display documentation of current major mode and minor modes.
efde809a
JPW
788A brief summary of the minor modes comes first, followed by the
789major mode description. This is followed by detailed
790descriptions of the minor modes, each on a separate page.
791
792For this to work correctly for a minor mode, the mode's indicator
793variable \(listed in `minor-mode-alist') must also be a function
794whose documentation describes the minor mode."
f3b5dd74 795 (interactive "@")
dd39c336
SM
796 (unless buffer (setq buffer (current-buffer)))
797 (help-setup-xref (list #'describe-mode buffer)
9639be74
RS
798 (interactive-p))
799 ;; For the sake of help-do-xref and help-xref-go-back,
800 ;; don't switch buffers before calling `help-buffer'.
cde56121 801 (with-help-window (help-buffer)
dd39c336 802 (with-current-buffer buffer
f6c57ef6 803 (let (minor-modes)
dd39c336
SM
804 ;; Older packages do not register in minor-mode-list but only in
805 ;; minor-mode-alist.
806 (dolist (x minor-mode-alist)
807 (setq x (car x))
808 (unless (memq x minor-mode-list)
809 (push x minor-mode-list)))
f6c57ef6
RS
810 ;; Find enabled minor mode we will want to mention.
811 (dolist (mode minor-mode-list)
812 ;; Document a minor mode if it is listed in minor-mode-alist,
813 ;; non-nil, and has a function definition.
af5f4483
SM
814 (let ((fmode (or (get mode :minor-mode-function) mode)))
815 (and (boundp mode) (symbol-value mode)
816 (fboundp fmode)
817 (let ((pretty-minor-mode
818 (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
819 (symbol-name fmode))
820 (capitalize
821 (substring (symbol-name fmode)
822 0 (match-beginning 0)))
823 fmode)))
824 (push (list fmode pretty-minor-mode
825 (format-mode-line (assq mode minor-mode-alist)))
826 minor-modes)))))
f6c57ef6
RS
827 (setq minor-modes
828 (sort minor-modes
af5f4483 829 (lambda (a b) (string-lessp (cadr a) (cadr b)))))
f6c57ef6 830 (when minor-modes
71723367 831 (princ "Enabled minor modes:\n")
e25e90b4
DP
832 (make-local-variable 'help-button-cache)
833 (with-current-buffer standard-output
834 (dolist (mode minor-modes)
af5f4483
SM
835 (let ((mode-function (nth 0 mode))
836 (pretty-minor-mode (nth 1 mode))
e25e90b4
DP
837 (indicator (nth 2 mode)))
838 (add-text-properties 0 (length pretty-minor-mode)
839 '(face bold) pretty-minor-mode)
840 (save-excursion
841 (goto-char (point-max))
842 (princ "\n\f\n")
843 (push (point-marker) help-button-cache)
844 ;; Document the minor modes fully.
845 (insert pretty-minor-mode)
71723367
RS
846 (princ (format " minor mode (%s):\n"
847 (if (zerop (length indicator))
848 "no indicator"
849 (format "indicator%s"
d8a869ea 850 indicator))))
e25e90b4 851 (princ (documentation mode-function)))
e25e90b4
DP
852 (insert-button pretty-minor-mode
853 'action (car help-button-cache)
72b64ad5 854 'follow-link t
e25e90b4 855 'help-echo "mouse-2, RET: show full information")
71723367
RS
856 (newline)))
857 (forward-line -1)
858 (fill-paragraph nil)
859 (forward-line 1))
860
861 (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
f6c57ef6 862 ;; Document the major mode.
e25e90b4
DP
863 (let ((mode mode-name))
864 (with-current-buffer standard-output
4e6d3170 865 (let ((start (point)))
145fe412 866 (insert (format-mode-line mode nil nil buffer))
4e6d3170 867 (add-text-properties start (point) '(face bold)))))
f6c57ef6 868 (princ " mode:\n")
cde56121 869 (princ (documentation major-mode))))))
400a1b1f 870
f6c57ef6 871
8e864068 872(defun describe-minor-mode (minor-mode)
335028c3
MY
873 "Display documentation of a minor mode given as MINOR-MODE.
874MINOR-MODE can be a minor mode symbol or a minor mode indicator string
875appeared on the mode-line."
7ada28ac 876 (interactive (list (completing-read
335028c3
MY
877 "Minor mode: "
878 (nconc
879 (describe-minor-mode-completion-table-for-symbol)
880 (describe-minor-mode-completion-table-for-indicator)
881 ))))
882 (if (symbolp minor-mode)
883 (setq minor-mode (symbol-name minor-mode)))
884 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
885 (indicators (describe-minor-mode-completion-table-for-indicator)))
886 (cond
887 ((member minor-mode symbols)
888 (describe-minor-mode-from-symbol (intern minor-mode)))
889 ((member minor-mode indicators)
890 (describe-minor-mode-from-indicator minor-mode))
891 (t
892 (error "No such minor mode: %s" minor-mode)))))
893
7ada28ac 894;; symbol
335028c3
MY
895(defun describe-minor-mode-completion-table-for-symbol ()
896 ;; In order to list up all minor modes, minor-mode-list
897 ;; is used here instead of minor-mode-alist.
898 (delq nil (mapcar 'symbol-name minor-mode-list)))
899(defun describe-minor-mode-from-symbol (symbol)
900 "Display documentation of a minor mode given as a symbol, SYMBOL"
7ada28ac 901 (interactive (list (intern (completing-read
335028c3
MY
902 "Minor mode symbol: "
903 (describe-minor-mode-completion-table-for-symbol)))))
904 (if (fboundp symbol)
905 (describe-function symbol)
906 (describe-variable symbol)))
907
908;; indicator
909(defun describe-minor-mode-completion-table-for-indicator ()
7ada28ac 910 (delq nil
335028c3
MY
911 (mapcar (lambda (x)
912 (let ((i (format-mode-line x)))
913 ;; remove first space if existed
914 (cond
915 ((= 0 (length i))
916 nil)
94318c8a 917 ((eq (aref i 0) ?\s)
335028c3 918 (substring i 1))
7ada28ac 919 (t
335028c3
MY
920 i))))
921 minor-mode-alist)))
8e864068 922(defun describe-minor-mode-from-indicator (indicator)
335028c3
MY
923 "Display documentation of a minor mode specified by INDICATOR.
924If you call this function interactively, you can give indicator which
925is currently activated with completion."
7ada28ac
LT
926 (interactive (list
927 (completing-read
8e864068 928 "Minor mode indicator: "
335028c3 929 (describe-minor-mode-completion-table-for-indicator))))
8e864068
JB
930 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
931 (if minor-mode
335028c3 932 (describe-minor-mode-from-symbol minor-mode)
8e864068
JB
933 (error "Cannot find minor mode for `%s'" indicator))))
934
935(defun lookup-minor-mode-from-indicator (indicator)
936 "Return a minor mode symbol from its indicator on the modeline."
335028c3 937 ;; remove first space if existed
7ada28ac 938 (if (and (< 0 (length indicator))
94318c8a 939 (eq (aref indicator 0) ?\s))
335028c3 940 (setq indicator (substring indicator 1)))
8e864068
JB
941 (let ((minor-modes minor-mode-alist)
942 result)
943 (while minor-modes
944 (let* ((minor-mode (car (car minor-modes)))
7ada28ac 945 (anindicator (format-mode-line
335028c3
MY
946 (car (cdr (car minor-modes))))))
947 ;; remove first space if existed
7ada28ac 948 (if (and (stringp anindicator)
335028c3 949 (> (length anindicator) 0)
94318c8a 950 (eq (aref anindicator 0) ?\s))
335028c3
MY
951 (setq anindicator (substring anindicator 1)))
952 (if (equal indicator anindicator)
8e864068
JB
953 (setq result minor-mode
954 minor-modes nil)
955 (setq minor-modes (cdr minor-modes)))))
956 result))
957
48ce3c22
RS
958\f
959;;; Automatic resizing of temporary buffers.
960
4483ddc5 961(defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
4e6d3170 962 "Maximum height of a window displaying a temporary buffer.
90e357ae
RS
963This is effective only when Temp Buffer Resize mode is enabled.
964The value is the maximum height (in lines) which `resize-temp-buffer-window'
48ce3c22 965will give to a window displaying a temporary buffer.
90e357ae
RS
966It can also be a function to be called to choose the height for such a buffer.
967It gets one argumemt, the buffer, and should return a positive integer."
48ce3c22
RS
968 :type '(choice integer function)
969 :group 'help
970 :version "20.4")
971
4e1ede6c
SM
972(define-minor-mode temp-buffer-resize-mode
973 "Toggle the mode which makes windows smaller for temporary buffers.
48ce3c22
RS
974With prefix argument ARG, turn the resizing of windows displaying temporary
975buffers on if ARG is positive or off otherwise.
4e1ede6c
SM
976This makes the window the right height for its contents, but never
977more than `temp-buffer-max-height' nor less than `window-min-height'.
978This applies to `help', `apropos' and `completion' buffers, and some others."
b0fbf754 979 :global t :group 'help
4e1ede6c 980 (if temp-buffer-resize-mode
57f43907 981 ;; `help-make-xrefs' may add a `back' button and thus increase the
4e1ede6c
SM
982 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
983 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
8304a3bb 984 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
48ce3c22
RS
985
986(defun resize-temp-buffer-window ()
2a5f11a2 987 "Resize the selected window to fit its contents.
4483ddc5 988Will not make it higher than `temp-buffer-max-height' nor smaller than
b2c85790 989`window-min-height'. Do nothing if it is the only window on its frame, if it
48ce3c22
RS
990is not as wide as the frame or if some of the window's contents are scrolled
991out of view."
992 (unless (or (one-window-p 'nomini)
993 (not (pos-visible-in-window-p (point-min)))
2a5f11a2 994 (not (window-full-width-p)))
d9c30bdf
MB
995 (fit-window-to-buffer
996 (selected-window)
997 (if (functionp temp-buffer-max-height)
998 (funcall temp-buffer-max-height (current-buffer))
999 temp-buffer-max-height))))
48ce3c22 1000
172892e3 1001\f
cde56121
MR
1002;;; help-window
1003
1004(defcustom help-window-select 'other
1005 "Non-nil means select help window for viewing.
1006Choices are:
1007 never (nil) Select help window only if there is no other window
1008 on its frame.
1009 other Select help window unless the selected window is the
1010 only other window on its frame.
1011 always (t) Always select the help window.
1012
1013This option has effect if and only if the help window was created
1014by `with-help-window'"
1015 :type '(choice (const :tag "never (nil)" nil)
1016 (const :tag "other" other)
1017 (const :tag "always (t)" t))
1018 :group 'help
1019 :version "23.1")
1020
1021(defun help-window-display-message (quit-part window &optional other)
1022 "Display message telling how to quit and scroll help window.
1023QUIT-PART is a string telling how to quit the help window WINDOW.
1024Optional argument OTHER non-nil means return text telling how to
1025scroll the \"other\" window."
1026 (let ((scroll-part
1027 (cond
1028 ((pos-visible-in-window-p
1029 (with-current-buffer (window-buffer window)
1030 (point-max)) window)
1031 ;; Buffer end is visible.
1032 ".")
1033 (other ", \\[scroll-other-window] to scroll help.")
1034 (t ", \\[scroll-up] to scroll help."))))
f6e7ec02 1035 (message "%s"
cde56121
MR
1036 (substitute-command-keys (concat quit-part scroll-part)))))
1037
1038(defun help-window-setup-finish (window &optional reuse keep-frame)
1039 "Finish setting up help window WINDOW.
1040Select WINDOW according to the value of `help-window-select'.
1041Display message telling how to scroll and eventually quit WINDOW.
1042
1043Optional argument REUSE non-nil means WINDOW has been reused \(by
1044`display-buffer'\) for displaying help. Optional argument
1045KEEP-FRAME non-nil means that quitting must no delete the frame
1046of WINDOW."
1047 (let ((number-of-windows
1048 (length (window-list (window-frame window) 'no-mini window))))
1049 (cond
1050 ((eq window (selected-window))
1051 ;; The help window is the selected window, probably the
1052 ;; `pop-up-windows' nil case.
1053 (help-window-display-message
1054 (if reuse
1055 "Type \"q\" to restore this window"
1056 ;; This should not be taken.
1057 "Type \"q\" to quit") window))
1058 ((= number-of-windows 1)
1059 ;; The help window is alone on a frame and not the selected
1060 ;; window, could be the `pop-up-frames' t case.
1061 (help-window-display-message
1062 (cond
1063 (keep-frame "Type \"q\" to delete this window")
1064 (reuse "Type \"q\" to restore this window")
1065 (view-remove-frame-by-deleting "Type \"q\" to delete this frame")
1066 (t "Type \"q\" to iconify this frame"))
1067 window))
1068 ((and (= number-of-windows 2)
1069 (eq (window-frame window) (window-frame (selected-window))))
1070 ;; There are two windows on the help window's frame and the other
1071 ;; window is the selected one.
1072 (if (memq help-window-select '(nil other))
1073 ;; Do not select the help window.
1074 (help-window-display-message
1075 (if reuse
1076 ;; Offer `display-buffer' for consistency with
b3d8e4a0 1077 ;; `help-print-return-message'. This is hardly TRT when
cde56121
MR
1078 ;; the other window and the selected window display the
1079 ;; same buffer but has been handled this way ever since.
1080 "Type \\[display-buffer] RET to restore the other window"
1081 ;; The classic "two windows" configuration.
1082 "Type \\[delete-other-windows] to delete the help window")
1083 window t)
1084 ;; Select help window and tell how to quit.
1085 (select-window window)
1086 (help-window-display-message
1087 (if reuse
1088 "Type \"q\" to restore this window"
1089 "Type \"q\" to delete this window") window)))
1090 (help-window-select
1091 ;; Issuing a message with 3 or more windows on the same frame
1092 ;; without selecting the help window doesn't make any sense.
1093 (select-window window)
1094 (help-window-display-message
1095 (if reuse
1096 "Type \"q\" to restore this window"
1097 "Type \"q\" to delete this window") window)))))
1098
1099(defun help-window-setup (list-of-frames list-of-window-tuples)
1100 "Set up help window.
1101LIST-OF-FRAMES and LIST-OF-WINDOW-TUPLES are the lists of frames
1102and window quadruples built by `with-help-window'. The help
1103window itself is specified by the variable `help-window'."
1104 (let* ((help-buffer (window-buffer help-window))
1105 ;; `help-buffer' now denotes the help window's buffer.
1106 (view-entry
1107 (assq help-window
1108 (buffer-local-value 'view-return-to-alist help-buffer)))
1109 (help-entry (assq help-window list-of-window-tuples)))
1110
1111 ;; Handle `help-window-point-marker'.
1112 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1113 (set-window-point help-window help-window-point-marker)
1114 ;; Reset `help-window-point-marker'.
1115 (set-marker help-window-point-marker nil))
1116
1117 (cond
1118 (view-entry
1119 ;; `view-return-to-alist' has an entry for the help window.
1120 (cond
1121 ((eq help-window (selected-window))
1122 ;; The help window is the selected window, probably because the
1123 ;; user followed a backward/forward button or a cross reference.
1124 ;; In this case just purge stale entries from
1125 ;; `view-return-to-alist' but leave the entry alone and don't
1126 ;; display a message.
1127 (view-return-to-alist-update help-buffer))
1128 ((and help-entry (eq (cadr help-entry) help-buffer))
1129 ;; The help window was not selected but displayed the help
1130 ;; buffer. In this case reuse existing exit information but try
1131 ;; to get back to the selected window when quitting. Don't
1132 ;; display a message since the user must have seen one before.
1133 (view-return-to-alist-update
1134 help-buffer (cons help-window
1135 (cons (selected-window) (cddr view-entry)))))
1136 (help-entry
1137 ;; The help window was not selected, did display the help buffer
1138 ;; earlier, but displayed another buffer when help was invoked.
1139 ;; Set up things so that quitting will show that buffer again.
1140 (view-return-to-alist-update
1141 help-buffer (cons help-window
1142 (cons (selected-window) (cdr help-entry))))
1143 (help-window-setup-finish help-window t))
1144 (t
1145 ;; The help window is new but `view-return-to-alist' had an
1146 ;; entry for it. This should never happen.
1147 (view-return-to-alist-update
1148 help-buffer (cons help-window
1149 (cons (selected-window) 'quit-window)))
1150 (help-window-setup-finish help-window t))))
1151 (help-entry
1152 ;; `view-return-to-alist' does not have an entry for help window
1153 ;; but `list-of-window-tuples' does. Hence `display-buffer' must
1154 ;; have reused an existing window.
1155 (if (eq (cadr help-entry) help-buffer)
1156 ;; The help window displayed `help-buffer' before but no
1157 ;; `view-return-to-alist' entry was found probably because the
1158 ;; user manually switched to the help buffer. Set up things
1159 ;; for `quit-window' although `view-exit-action' should be
1160 ;; able to handle this case all by itself.
1161 (progn
1162 (view-return-to-alist-update
1163 help-buffer (cons help-window
1164 (cons (selected-window) 'quit-window)))
1165 (help-window-setup-finish help-window t))
1166 ;; The help window displayed another buffer before. Set up
1167 ;; things in a way that quitting can orderly show that buffer
1168 ;; again. The window-start and window-point information from
1169 ;; `list-of-window-tuples' provide the necessary information.
1170 (view-return-to-alist-update
1171 help-buffer (cons help-window
1172 (cons (selected-window) (cdr help-entry))))
1173 (help-window-setup-finish help-window t)))
1174 ((memq (window-frame help-window) list-of-frames)
1175 ;; The help window is a new window on an existing frame. This
1176 ;; case must be handled specially by `help-window-setup-finish'
1177 ;; and `view-mode-exit' to ascertain that quitting does _not_
1178 ;; inadvertently delete the frame.
1179 (view-return-to-alist-update
1180 help-buffer (cons help-window
1181 (cons (selected-window) 'keep-frame)))
1182 (help-window-setup-finish help-window nil t))
1183 (t
1184 ;; The help window is shown on a new frame. In this case quitting
1185 ;; shall handle both, the help window _and_ its frame. We changed
1186 ;; the default of `view-remove-frame-by-deleting' to t in order to
1187 ;; intuitively DTRT here.
1188 (view-return-to-alist-update
1189 help-buffer (cons help-window (cons (selected-window) t)))
1190 (help-window-setup-finish help-window)))))
1191
1192;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1193;; providing the following additional twists:
1194
1195;; (1) Issue more accurate messages telling how to scroll and quit the
1196;; help window.
1197
1198;; (2) Make `view-mode-exit' DTRT in more cases.
1199
1200;; (3) An option (customizable via `help-window-select') to select the
1201;; help window automatically.
1202
1203;; (4) A marker (`help-window-point-marker') to move point in the help
1204;; window to an arbitrary buffer position.
1205
b3d8e4a0 1206;; Note: It's usually always wrong to use `help-print-return-message' in
cde56121
MR
1207;; the body of `with-help-window'.
1208(defmacro with-help-window (buffer-name &rest body)
1209 "Display buffer BUFFER-NAME in a help window evaluating BODY.
1210Select help window if the actual value of the user option
eff26424 1211`help-window-select' says so. Return last value in BODY."
cde56121
MR
1212 (declare (indent 1) (debug t))
1213 ;; Bind list-of-frames to `frame-list' and list-of-window-tuples to a
1214 ;; list of one <window window-buffer window-start window-point> tuple
1215 ;; for each live window.
1216 `(let ((list-of-frames (frame-list))
1217 (list-of-window-tuples
1218 (let (list)
1219 (walk-windows
1220 (lambda (window)
1221 (push (list window (window-buffer window)
1222 (window-start window) (window-point window))
1223 list))
1224 'no-mini t)
1225 list)))
eff26424
MR
1226 ;; Make `help-window' t to trigger `help-mode-finish' to set
1227 ;; `help-window' to the actual help window.
cde56121
MR
1228 (setq help-window t)
1229 ;; Make `help-window-point-marker' point nowhere (the only place
1230 ;; where this should be set to a buffer position is within BODY).
1231 (set-marker help-window-point-marker nil)
eff26424
MR
1232 (prog1
1233 ;; Return value returned by `with-output-to-temp-buffer'.
1234 (with-output-to-temp-buffer ,buffer-name
1235 (progn ,@body))
1236 (when (windowp help-window)
1237 ;; Set up help window.
1238 (help-window-setup list-of-frames list-of-window-tuples))
1239 ;; Reset `help-window' to nil to avoid confusing future calls of
1240 ;; `help-mode-finish' with plain `with-output-to-temp-buffer'.
1241 (setq help-window nil))))
cde56121 1242\f
172892e3
JB
1243(provide 'help)
1244
dd39c336 1245;; arch-tag: cf427352-27e9-49b7-9a6f-741ebab02423
1a06eabd 1246;;; help.el ends here