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