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