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