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