lisp/*.el: Silence lexical-binding warnings.
[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)
123ecb68
CY
415 (with-current-buffer (get-buffer-create "*Messages*")
416 (goto-char (point-max))
417 (display-buffer (current-buffer))))
4f16ea85 418
754084bb
GM
419(defun view-order-manuals ()
420 "Display the Emacs ORDERS file."
421 (interactive)
7b01c8d7 422 (view-help-file "ORDERS"))
754084bb 423
7ee71cf1
RS
424(defun view-emacs-FAQ ()
425 "Display the Emacs Frequently Asked Questions (FAQ) file."
426 (interactive)
94ea540b 427 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
279b772d 428 (info "(efaq)"))
7ee71cf1 429
4cbff657
DL
430(defun view-emacs-problems ()
431 "Display info on known problems with Emacs and possible workarounds."
432 (interactive)
7b01c8d7
KS
433 (view-help-file "PROBLEMS"))
434
435(defun view-emacs-debugging ()
436 "Display info on how to debug Emacs problems."
437 (interactive)
438 (view-help-file "DEBUG"))
439
440(defun view-external-packages ()
441 "Display external packages and information about Emacs."
442 (interactive)
443 (view-help-file "MORE.STUFF"))
4cbff657 444
433ae6f6 445(defun view-lossage ()
6b8d1c72 446 "Display last 300 input keystrokes.
50b57199
EZ
447
448To record all your input on a file, use `open-dribble-file'."
433ae6f6 449 (interactive)
32226619
JB
450 (help-setup-xref (list #'view-lossage)
451 (called-interactively-p 'interactive))
cde56121 452 (with-help-window (help-buffer)
02dfca16
SM
453 (princ (mapconcat (lambda (key)
454 (if (or (integerp key) (symbolp key) (listp key))
455 (single-key-description key)
456 (prin1-to-string key nil)))
298a7c8c
RS
457 (recent-keys)
458 " "))
b0fbf754 459 (with-current-buffer standard-output
433ae6f6
RS
460 (goto-char (point-min))
461 (while (progn (move-to-column 50) (not (eobp)))
e5fe3a6c
JB
462 (when (search-forward " " nil t)
463 (delete-char -1))
cde56121
MR
464 (insert "\n"))
465 ;; jidanni wants to see the last keystrokes immediately.
466 (set-marker help-window-point-marker (point)))))
433ae6f6 467
788d62cf
MB
468\f
469;; Key bindings
433ae6f6 470
4c45295b 471(defun describe-bindings (&optional prefix buffer)
a8ad43aa
RS
472 "Show a list of all defined keys, and their definitions.
473We put that list in a buffer, and display the buffer.
474
475The optional argument PREFIX, if non-nil, should be a key sequence;
4c45295b
KH
476then we display only bindings that start with that prefix.
477The optional argument BUFFER specifies which buffer's bindings
abca4ad7
LT
478to display (default, the current buffer). BUFFER can be a buffer
479or a buffer name."
3e5929af 480 (interactive)
4c45295b 481 (or buffer (setq buffer (current-buffer)))
32226619
JB
482 (help-setup-xref (list #'describe-bindings prefix buffer)
483 (called-interactively-p 'interactive))
4c45295b 484 (with-current-buffer buffer
3e5929af 485 (describe-bindings-internal nil prefix)))
a8ad43aa 486
94ea540b
SM
487;; This function used to be in keymap.c.
488(defun describe-bindings-internal (&optional menus prefix)
489 "Show a list of all defined keys, and their definitions.
490We put that list in a buffer, and display the buffer.
491
492The optional argument MENUS, if non-nil, says to mention menu bindings.
493\(Ordinarily these are omitted from the output.)
494The optional argument PREFIX, if non-nil, should be a key sequence;
495then we display only bindings that start with that prefix."
94ea540b 496 (let ((buf (current-buffer)))
cde56121 497 (with-help-window "*Help*"
94ea540b
SM
498 (with-current-buffer standard-output
499 (describe-buffer-bindings buf prefix menus)))))
500
e88a2c59 501(defun where-is (definition &optional insert)
b2c85790 502 "Print message listing key sequences that invoke the command DEFINITION.
e88a2c59
RS
503Argument is a command definition, usually a symbol with a function definition.
504If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
54c0b967
RS
505 (interactive
506 (let ((fn (function-called-at-point))
788d62cf 507 (enable-recursive-minibuffers t)
54c0b967 508 val)
3829bcc5
SM
509 (setq val (completing-read
510 (if fn
511 (format "Where is command (default %s): " fn)
512 "Where is command: ")
513 obarray 'commandp t))
514 (list (if (equal val "") fn (intern val)) current-prefix-arg)))
463d75ac 515 (unless definition (error "No command"))
7a698dc1 516 (let ((func (indirect-function definition))
3829bcc5 517 (defs nil)
e3ce671f 518 (standard-output (if insert (current-buffer) standard-output)))
740b479c 519 ;; In DEFS, find all symbols that are aliases for DEFINITION.
3829bcc5
SM
520 (mapatoms (lambda (symbol)
521 (and (fboundp symbol)
522 (not (eq symbol definition))
d92c2757
RS
523 (eq func (condition-case ()
524 (indirect-function symbol)
525 (error symbol)))
3829bcc5 526 (push symbol defs))))
740b479c
RS
527 ;; Look at all the symbols--first DEFINITION,
528 ;; then its aliases.
529 (dolist (symbol (cons definition defs))
530 (let* ((remapped (command-remapping symbol))
531 (keys (where-is-internal
532 symbol overriding-local-map nil nil remapped))
533 (keys (mapconcat 'key-description keys ", "))
534 string)
535 (setq string
536 (if insert
537 (if (> (length keys) 0)
538 (if remapped
539 (format "%s (%s) (remapped from %s)"
540 keys remapped symbol)
541 (format "%s (%s)" keys symbol))
542 (format "M-x %s RET" symbol))
543 (if (> (length keys) 0)
544 (if remapped
545 (format "%s is remapped to %s which is on %s"
a5f43550 546 symbol remapped keys)
740b479c
RS
547 (format "%s is on %s" symbol keys))
548 ;; If this is the command the user asked about,
549 ;; and it is not on any key, say so.
550 ;; For other symbols, its aliases, say nothing
551 ;; about them unless they are on keys.
552 (if (eq symbol definition)
553 (format "%s is not on any key" symbol)))))
554 (when string
555 (unless (eq symbol definition)
556 (princ ";\n its alias "))
557 (princ string)))))
54c0b967
RS
558 nil)
559
02dfca16
SM
560(defun help-key-description (key untranslated)
561 (let ((string (key-description key)))
ae1bb8ac
SM
562 (if (or (not untranslated)
563 (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
02dfca16
SM
564 string
565 (let ((otherstring (key-description untranslated)))
566 (if (equal string otherstring)
567 string
568 (format "%s (translated from %s)" string otherstring))))))
71296446 569
6527c983
EZ
570(defun describe-key-briefly (&optional key insert untranslated)
571 "Print the name of the function KEY invokes. KEY is a string.
572If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
573If non-nil, UNTRANSLATED is a vector of the untranslated events.
574It can also be a number in which case the untranslated events from
575the last key hit are used.
576
577If KEY is a menu item or a tool-bar button that is disabled, this command
8ee320fc 578temporarily enables it to allow getting help on disabled items and buttons."
2c8ed538
RS
579 (interactive
580 (let ((enable-disabled-menus-and-buttons t)
581 (cursor-in-echo-area t)
582 saved-yank-menu)
583 (unwind-protect
584 (let (key)
585 ;; If yank-menu is empty, populate it temporarily, so that
586 ;; "Select and Paste" menu can generate a complete event.
587 (when (null (cdr yank-menu))
588 (setq saved-yank-menu (copy-sequence yank-menu))
589 (menu-bar-update-yank-menu "(any string)" nil))
590 (setq key (read-key-sequence "Describe key (or click or menu item): "))
74c582e6
CY
591 ;; Clear the echo area message (Bug#7014).
592 (message nil)
46d91fa0 593 ;; If KEY is a down-event, read and discard the
91a2acb2
DK
594 ;; corresponding up-event. Note that there are also
595 ;; down-events on scroll bars and mode lines: the actual
596 ;; event then is in the second element of the vector.
597 (and (vectorp key)
badf89ea
RS
598 (let ((last-idx (1- (length key))))
599 (and (eventp (aref key last-idx))
600 (memq 'down (event-modifiers (aref key last-idx)))))
91a2acb2 601 (read-event))
2c8ed538
RS
602 (list
603 key
774a814f
RS
604 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
605 1))
2c8ed538
RS
606 ;; Put yank-menu back as it was, if we changed it.
607 (when saved-yank-menu
608 (setq yank-menu (copy-sequence saved-yank-menu))
609 (fset 'yank-menu (cons 'keymap yank-menu))))))
02dfca16
SM
610 (if (numberp untranslated)
611 (setq untranslated (this-single-command-raw-keys)))
91a2acb2
DK
612 (let* ((event (if (and (symbolp (aref key 0))
613 (> (length key) 1)
614 (consp (aref key 1)))
615 (aref key 1)
616 (aref key 0)))
617 (modifiers (event-modifiers event))
b2cba41e 618 (standard-output (if insert (current-buffer) standard-output))
24a27882
KS
619 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
620 (memq 'drag modifiers)) " at that spot" ""))
621 (defn (key-binding key t))
622 key-desc)
623 ;; Handle the case where we faked an entry in "Select and Paste" menu.
624 (if (and (eq defn nil)
625 (stringp (aref key (1- (length key))))
626 (eq (key-binding (substring key 0 -1)) 'yank-menu))
627 (setq defn 'menu-bar-select-yank))
628 ;; Don't bother user with strings from (e.g.) the select-paste menu.
629 (if (stringp (aref key (1- (length key))))
630 (aset key (1- (length key)) "(any string)"))
631 (if (and (> (length untranslated) 0)
632 (stringp (aref untranslated (1- (length untranslated)))))
633 (aset untranslated (1- (length untranslated)) "(any string)"))
634 ;; Now describe the key, perhaps as changed.
635 (setq key-desc (help-key-description key untranslated))
636 (if (or (null defn) (integerp defn) (equal defn 'undefined))
637 (princ (format "%s%s is undefined" key-desc mouse-msg))
638 (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
96ede6b2 639
6527c983
EZ
640(defun describe-key (&optional key untranslated up-event)
641 "Display documentation of the function invoked by KEY.
642KEY can be any kind of a key sequence; it can include keyboard events,
643mouse events, and/or menu events. When calling from a program,
644pass KEY as a string or a vector.
645
646If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
647It can also be a number, in which case the untranslated events from
648the last key sequence entered are used.
649UP-EVENT is the up-event that was discarded by reading KEY, or nil.
650
651If KEY is a menu item or a tool-bar button that is disabled, this command
8ee320fc 652temporarily enables it to allow getting help on disabled items and buttons."
2c8ed538
RS
653 (interactive
654 (let ((enable-disabled-menus-and-buttons t)
655 (cursor-in-echo-area t)
656 saved-yank-menu)
657 (unwind-protect
658 (let (key)
659 ;; If yank-menu is empty, populate it temporarily, so that
660 ;; "Select and Paste" menu can generate a complete event.
661 (when (null (cdr yank-menu))
662 (setq saved-yank-menu (copy-sequence yank-menu))
663 (menu-bar-update-yank-menu "(any string)" nil))
664 (setq key (read-key-sequence "Describe key (or click or menu item): "))
665 (list
666 key
667 (prefix-numeric-value current-prefix-arg)
c3f82997 668 ;; If KEY is a down-event, read and include the
badf89ea
RS
669 ;; corresponding up-event. Note that there are also
670 ;; down-events on scroll bars and mode lines: the actual
671 ;; event then is in the second element of the vector.
91a2acb2 672 (and (vectorp key)
badf89ea
RS
673 (let ((last-idx (1- (length key))))
674 (and (eventp (aref key last-idx))
675 (memq 'down (event-modifiers (aref key last-idx)))))
91a2acb2 676 (or (and (eventp (aref key 0))
98da283b
CY
677 (memq 'down (event-modifiers (aref key 0)))
678 ;; However, for the C-down-mouse-2 popup
679 ;; menu, there is no subsequent up-event. In
680 ;; this case, the up-event is the next
681 ;; element in the supplied vector.
682 (= (length key) 1))
91a2acb2
DK
683 (and (> (length key) 1)
684 (eventp (aref key 1))
685 (memq 'down (event-modifiers (aref key 1)))))
686 (read-event))))
2c8ed538
RS
687 ;; Put yank-menu back as it was, if we changed it.
688 (when saved-yank-menu
689 (setq yank-menu (copy-sequence saved-yank-menu))
690 (fset 'yank-menu (cons 'keymap yank-menu))))))
02dfca16
SM
691 (if (numberp untranslated)
692 (setq untranslated (this-single-command-raw-keys)))
05ca18a8
KS
693 (let* ((event (aref key (if (and (symbolp (aref key 0))
694 (> (length key) 1)
695 (consp (aref key 1)))
696 1
697 0)))
91a2acb2 698 (modifiers (event-modifiers event))
24a27882
KS
699 (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
700 (memq 'drag modifiers)) " at that spot" ""))
05ca18a8
KS
701 (defn (key-binding key t))
702 defn-up defn-up-tricky ev-type
703 mouse-1-remapped mouse-1-tricky)
91a2acb2 704
05ca18a8 705 ;; Handle the case where we faked an entry in "Select and Paste" menu.
24a27882 706 (when (and (eq defn nil)
91a2acb2
DK
707 (stringp (aref key (1- (length key))))
708 (eq (key-binding (substring key 0 -1)) 'yank-menu))
24a27882
KS
709 (setq defn 'menu-bar-select-yank))
710 (if (or (null defn) (integerp defn) (equal defn 'undefined))
711 (message "%s%s is undefined"
712 (help-key-description key untranslated) mouse-msg)
32226619
JB
713 (help-setup-xref (list #'describe-function defn)
714 (called-interactively-p 'interactive))
24a27882
KS
715 ;; Don't bother user with strings from (e.g.) the select-paste menu.
716 (when (stringp (aref key (1- (length key))))
717 (aset key (1- (length key)) "(any string)"))
718 (when (and untranslated
91a2acb2 719 (stringp (aref untranslated (1- (length untranslated)))))
24a27882
KS
720 (aset untranslated (1- (length untranslated))
721 "(any string)"))
722 ;; Need to do this before erasing *Help* buffer in case event
723 ;; is a mouse click in an existing *Help* buffer.
724 (when up-event
725 (setq ev-type (event-basic-type up-event))
726 (let ((sequence (vector up-event)))
727 (when (and (eq ev-type 'mouse-1)
728 mouse-1-click-follows-link
729 (not (eq mouse-1-click-follows-link 'double))
730 (setq mouse-1-remapped
731 (mouse-on-link-p (event-start up-event))))
732 (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
733 (> mouse-1-click-follows-link 0)))
734 (cond ((stringp mouse-1-remapped)
735 (setq sequence mouse-1-remapped))
736 ((vectorp mouse-1-remapped)
737 (setcar up-event (elt mouse-1-remapped 0)))
738 (t (setcar up-event 'mouse-2))))
739 (setq defn-up (key-binding sequence nil nil (event-start up-event)))
740 (when mouse-1-tricky
741 (setq sequence (vector up-event))
742 (aset sequence 0 'mouse-1)
743 (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
cde56121 744 (with-help-window (help-buffer)
24a27882
KS
745 (princ (help-key-description key untranslated))
746 (princ (format "\
b96817c3 747%s runs the command %S, which is "
24a27882
KS
748 mouse-msg defn))
749 (describe-function-1 defn)
05ca18a8 750 (when up-event
24a27882
KS
751 (unless (or (null defn-up)
752 (integerp defn-up)
753 (equal defn-up 'undefined))
754 (princ (format "
755
756----------------- up-event %s----------------
757
e8579ebc 758%s%s%s runs the command %S, which is "
24a27882 759 (if mouse-1-tricky "(short click) " "")
e8579ebc
CY
760 (key-description (vector up-event))
761 mouse-msg
24a27882 762 (if mouse-1-remapped
b96817c3 763 " is remapped to <mouse-2>, which" "")
24a27882
KS
764 defn-up))
765 (describe-function-1 defn-up))
766 (unless (or (null defn-up-tricky)
767 (integerp defn-up-tricky)
768 (eq defn-up-tricky 'undefined))
769 (princ (format "
770
771----------------- up-event (long click) ----------------
772
773Pressing <%S>%s for longer than %d milli-seconds
b96817c3 774runs the command %S, which is "
24a27882
KS
775 ev-type mouse-msg
776 mouse-1-click-follows-link
777 defn-up-tricky))
cde56121 778 (describe-function-1 defn-up-tricky)))))))
400a1b1f 779\f
788d62cf
MB
780(defun describe-mode (&optional buffer)
781 "Display documentation of current major mode and minor modes.
efde809a
JPW
782A brief summary of the minor modes comes first, followed by the
783major mode description. This is followed by detailed
784descriptions of the minor modes, each on a separate page.
785
786For this to work correctly for a minor mode, the mode's indicator
787variable \(listed in `minor-mode-alist') must also be a function
27d1f87a
CY
788whose documentation describes the minor mode.
789
790If called from Lisp with a non-nil BUFFER argument, display
791documentation for the major and minor modes of that buffer."
f3b5dd74 792 (interactive "@")
dd39c336
SM
793 (unless buffer (setq buffer (current-buffer)))
794 (help-setup-xref (list #'describe-mode buffer)
32226619 795 (called-interactively-p 'interactive))
9639be74
RS
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 862 (let ((start (point)))
145fe412 863 (insert (format-mode-line mode nil nil buffer))
4e6d3170 864 (add-text-properties start (point) '(face bold)))))
9d05d1ba
JB
865 (princ " mode")
866 (let* ((mode major-mode)
867 (file-name (find-lisp-object-file-name mode nil)))
868 (when file-name
869 (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
870 ;; Make a hyperlink to the library.
871 (with-current-buffer standard-output
872 (save-excursion
873 (re-search-backward "`\\([^`']+\\)'" nil t)
874 (help-xref-button 1 'help-function-def mode file-name)))))
875 (princ ":\n")
309d5b43 876 (princ (documentation major-mode)))))
6460e534 877 ;; For the sake of IELM and maybe others
309d5b43 878 nil)
400a1b1f 879
f6c57ef6 880
8e864068 881(defun describe-minor-mode (minor-mode)
335028c3
MY
882 "Display documentation of a minor mode given as MINOR-MODE.
883MINOR-MODE can be a minor mode symbol or a minor mode indicator string
884appeared on the mode-line."
7ada28ac 885 (interactive (list (completing-read
335028c3
MY
886 "Minor mode: "
887 (nconc
888 (describe-minor-mode-completion-table-for-symbol)
889 (describe-minor-mode-completion-table-for-indicator)
890 ))))
891 (if (symbolp minor-mode)
892 (setq minor-mode (symbol-name minor-mode)))
893 (let ((symbols (describe-minor-mode-completion-table-for-symbol))
894 (indicators (describe-minor-mode-completion-table-for-indicator)))
895 (cond
896 ((member minor-mode symbols)
897 (describe-minor-mode-from-symbol (intern minor-mode)))
898 ((member minor-mode indicators)
899 (describe-minor-mode-from-indicator minor-mode))
900 (t
901 (error "No such minor mode: %s" minor-mode)))))
902
7ada28ac 903;; symbol
335028c3
MY
904(defun describe-minor-mode-completion-table-for-symbol ()
905 ;; In order to list up all minor modes, minor-mode-list
906 ;; is used here instead of minor-mode-alist.
907 (delq nil (mapcar 'symbol-name minor-mode-list)))
357f93d2 908
335028c3
MY
909(defun describe-minor-mode-from-symbol (symbol)
910 "Display documentation of a minor mode given as a symbol, SYMBOL"
7ada28ac 911 (interactive (list (intern (completing-read
335028c3
MY
912 "Minor mode symbol: "
913 (describe-minor-mode-completion-table-for-symbol)))))
914 (if (fboundp symbol)
915 (describe-function symbol)
916 (describe-variable symbol)))
917
918;; indicator
919(defun describe-minor-mode-completion-table-for-indicator ()
7ada28ac 920 (delq nil
335028c3
MY
921 (mapcar (lambda (x)
922 (let ((i (format-mode-line x)))
923 ;; remove first space if existed
924 (cond
925 ((= 0 (length i))
926 nil)
94318c8a 927 ((eq (aref i 0) ?\s)
335028c3 928 (substring i 1))
7ada28ac 929 (t
335028c3
MY
930 i))))
931 minor-mode-alist)))
357f93d2 932
8e864068 933(defun describe-minor-mode-from-indicator (indicator)
335028c3
MY
934 "Display documentation of a minor mode specified by INDICATOR.
935If you call this function interactively, you can give indicator which
936is currently activated with completion."
7ada28ac
LT
937 (interactive (list
938 (completing-read
8e864068 939 "Minor mode indicator: "
335028c3 940 (describe-minor-mode-completion-table-for-indicator))))
8e864068
JB
941 (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
942 (if minor-mode
335028c3 943 (describe-minor-mode-from-symbol minor-mode)
8e864068
JB
944 (error "Cannot find minor mode for `%s'" indicator))))
945
946(defun lookup-minor-mode-from-indicator (indicator)
37269466 947 "Return a minor mode symbol from its indicator on the mode line."
335028c3 948 ;; remove first space if existed
7ada28ac 949 (if (and (< 0 (length indicator))
94318c8a 950 (eq (aref indicator 0) ?\s))
335028c3 951 (setq indicator (substring indicator 1)))
8e864068
JB
952 (let ((minor-modes minor-mode-alist)
953 result)
954 (while minor-modes
955 (let* ((minor-mode (car (car minor-modes)))
7ada28ac 956 (anindicator (format-mode-line
335028c3
MY
957 (car (cdr (car minor-modes))))))
958 ;; remove first space if existed
7ada28ac 959 (if (and (stringp anindicator)
335028c3 960 (> (length anindicator) 0)
94318c8a 961 (eq (aref anindicator 0) ?\s))
335028c3
MY
962 (setq anindicator (substring anindicator 1)))
963 (if (equal indicator anindicator)
8e864068
JB
964 (setq result minor-mode
965 minor-modes nil)
966 (setq minor-modes (cdr minor-modes)))))
967 result))
48ce3c22
RS
968\f
969;;; Automatic resizing of temporary buffers.
ef654460
MR
970(defcustom temp-buffer-max-height
971 (lambda (buffer)
972 (if (eq (selected-window) (frame-root-window))
973 (/ (x-display-pixel-height) (frame-char-height) 2)
974 (/ (- (frame-height) 2) 2)))
4e6d3170 975 "Maximum height of a window displaying a temporary buffer.
90e357ae 976This is effective only when Temp Buffer Resize mode is enabled.
357f93d2
MR
977The value is the maximum height (in lines) which
978`resize-temp-buffer-window' will give to a window displaying a
979temporary buffer. It can also be a function to be called to
e4920bc9 980choose the height for such a buffer. It gets one argument, the
357f93d2
MR
981buffer, and should return a positive integer. At the time the
982function is called, the window to be resized is selected."
48ce3c22
RS
983 :type '(choice integer function)
984 :group 'help
6ba6a3e5 985 :version "24.3")
ef654460 986
4e1ede6c 987(define-minor-mode temp-buffer-resize-mode
c5e28e39 988 "Toggle auto-resizing temporary buffer windows (Temp Buffer Resize Mode).
06e21633
CY
989With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
990is positive, and disable it otherwise. If called from Lisp,
991enable the mode if ARG is omitted or nil.
992
993When Temp Buffer Resize mode is enabled, the windows in which we
c5e28e39 994show a temporary buffer are automatically resized in height to
06e21633
CY
995fit the buffer's contents, but never more than
996`temp-buffer-max-height' nor less than `window-min-height'.
357f93d2 997
8e17c9ba
MR
998A window is resized only if it has been specially created for the
999buffer. Windows that have shown another buffer before are not
5938d519
MR
1000resized. A frame is resized only if `fit-frame-to-buffer' is
1001non-nil.
8e17c9ba 1002
357f93d2
MR
1003This mode is used by `help', `apropos' and `completion' buffers,
1004and some others."
b0fbf754 1005 :global t :group 'help
4e1ede6c 1006 (if temp-buffer-resize-mode
57f43907 1007 ;; `help-make-xrefs' may add a `back' button and thus increase the
4e1ede6c
SM
1008 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1009 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
8304a3bb 1010 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
48ce3c22 1011
c5e28e39
MR
1012(defun resize-temp-buffer-window (&optional window)
1013 "Resize WINDOW to fit its contents.
1014WINDOW can be any live window and defaults to the selected one.
1015
1016Do not make WINDOW higher than `temp-buffer-max-height' nor
1017smaller than `window-min-height'. Do nothing if WINDOW is not
8c7727c3
MR
1018vertically combined, some of its contents are scrolled out of
1019view, or WINDOW was not created by `display-buffer'."
c5e28e39 1020 (setq window (window-normalize-window window t))
9d3aa82c
JB
1021 (let ((height (if (functionp temp-buffer-max-height)
1022 (with-selected-window window
1023 (funcall temp-buffer-max-height (window-buffer)))
1024 temp-buffer-max-height))
1025 (quit-cadr (cadr (window-parameter window 'quit-restore))))
1026 (cond
1027 ;; Resize WINDOW iff it was split off by `display-buffer'.
1028 ((and (eq quit-cadr 'window)
1029 (pos-visible-in-window-p (point-min) window)
1030 (window-combined-p window))
1031 (fit-window-to-buffer window height))
1032 ;; Resize FRAME iff it was created by `display-buffer'.
1033 ((and fit-frame-to-buffer
1034 (eq quit-cadr 'frame)
1035 (eq window (frame-root-window window)))
1036 (let ((frame (window-frame window)))
1037 (fit-frame-to-buffer
1038 frame (+ (frame-height frame)
1039 (- (window-total-size window))
1040 height)))))))
48ce3c22 1041
357f93d2 1042;;; Help windows.
cde56121
MR
1043(defcustom help-window-select 'other
1044 "Non-nil means select help window for viewing.
1045Choices are:
1046 never (nil) Select help window only if there is no other window
1047 on its frame.
1048 other Select help window unless the selected window is the
357f93d2 1049 only other window on the help window's frame.
cde56121
MR
1050 always (t) Always select the help window.
1051
1052This option has effect if and only if the help window was created
1053by `with-help-window'"
1054 :type '(choice (const :tag "never (nil)" nil)
1055 (const :tag "other" other)
1056 (const :tag "always (t)" t))
1057 :group 'help
1058 :version "23.1")
1059
c89926a5
CY
1060(defcustom help-enable-auto-load t
1061 "Whether Help commands can perform autoloading.
1062If non-nil, whenever \\[describe-function] is called for an
1063autoloaded function whose docstring contains any key substitution
1064construct (see `substitute-command-keys'), the library is loaded,
1065so that the documentation can show the right key bindings."
1066 :type 'boolean
1067 :group 'help
2a1e2476 1068 :version "24.3")
c89926a5 1069
357f93d2 1070(defun help-window-display-message (quit-part window &optional scroll)
cde56121
MR
1071 "Display message telling how to quit and scroll help window.
1072QUIT-PART is a string telling how to quit the help window WINDOW.
357f93d2
MR
1073Optional argument SCROLL non-nil means tell how to scroll WINDOW.
1074SCROLL equal `other' means tell how to scroll the \"other\"
1075window."
cde56121
MR
1076 (let ((scroll-part
1077 (cond
357f93d2
MR
1078 ;; If we don't have QUIT-PART we probably reuse a window
1079 ;; showing the same buffer so we don't show any message.
1080 ((not quit-part) nil)
cde56121
MR
1081 ((pos-visible-in-window-p
1082 (with-current-buffer (window-buffer window)
357f93d2
MR
1083 (point-max)) window t)
1084 ;; Buffer end is at least partially visible, no need to talk
1085 ;; about scrolling.
cde56121 1086 ".")
357f93d2
MR
1087 ((eq scroll 'other)
1088 ", \\[scroll-other-window] to scroll help.")
1089 (scroll ", \\[scroll-up] to scroll help."))))
f6e7ec02 1090 (message "%s"
cde56121
MR
1091 (substitute-command-keys (concat quit-part scroll-part)))))
1092
f678e0b6 1093(defun help-window-setup (help-window)
357f93d2 1094 "Set up help window for `with-help-window'.
f678e0b6
MR
1095HELP-WINDOW is the window used for displaying the help buffer."
1096 (let* ((help-buffer (when (window-live-p help-window)
357f93d2 1097 (window-buffer help-window)))
f678e0b6 1098 (help-setup (when (window-live-p help-window)
cf4eacfd 1099 (car (window-parameter help-window 'quit-restore)))))
357f93d2
MR
1100 (when help-buffer
1101 ;; Handle `help-window-point-marker'.
1102 (when (eq (marker-buffer help-window-point-marker) help-buffer)
1103 (set-window-point help-window help-window-point-marker)
1104 ;; Reset `help-window-point-marker'.
1105 (set-marker help-window-point-marker nil))
cde56121 1106
cde56121 1107 (cond
357f93d2
MR
1108 ((or (eq help-window (selected-window))
1109 (and (or (eq help-window-select t)
cf4eacfd 1110 (eq help-setup 'frame)
357f93d2
MR
1111 (and (eq help-window-select 'other)
1112 (eq (window-frame help-window) (selected-frame))
1113 (> (length (window-list nil 'no-mini)) 2)))
1114 (select-window help-window)))
1115 ;; The help window is or gets selected ...
1116 (help-window-display-message
1117 (cond
cf4eacfd 1118 ((eq help-setup 'window)
357f93d2 1119 ;; ... and is new, ...
f678e0b6 1120 "Type \"q\" to delete help window")
cf4eacfd 1121 ((eq help-setup 'frame)
f678e0b6 1122 "Type \"q\" to delete help frame")
cf4eacfd 1123 ((eq help-setup 'other)
357f93d2
MR
1124 ;; ... or displayed some other buffer before.
1125 "Type \"q\" to restore previous buffer"))
1126 help-window t))
1127 ((and (eq (window-frame help-window) (selected-frame))
1128 (= (length (window-list nil 'no-mini)) 2))
1129 ;; There are two windows on the help window's frame and the
1130 ;; other one is the selected one.
1131 (help-window-display-message
1132 (cond
cf4eacfd 1133 ((eq help-setup 'window)
357f93d2 1134 "Type \\[delete-other-windows] to delete the help window")
cf4eacfd 1135 ((eq help-setup 'other)
f678e0b6 1136 "Type \"q\" in help window to restore its previous buffer"))
357f93d2 1137 help-window 'other))
cde56121 1138 (t
f678e0b6 1139 ;; The help window is not selected ...
357f93d2 1140 (help-window-display-message
f678e0b6 1141 (cond
cf4eacfd 1142 ((eq help-setup 'window)
f678e0b6
MR
1143 ;; ... and is new, ...
1144 "Type \"q\" in help window to delete it")
cf4eacfd 1145 ((eq help-setup 'other)
f678e0b6
MR
1146 ;; ... or displayed some other buffer before.
1147 "Type \"q\" in help window to restore previous buffer"))
1148 help-window))))))
cde56121
MR
1149
1150;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
1151;; providing the following additional twists:
1152
1153;; (1) Issue more accurate messages telling how to scroll and quit the
1154;; help window.
1155
357f93d2 1156;; (2) An option (customizable via `help-window-select') to select the
cde56121
MR
1157;; help window automatically.
1158
357f93d2 1159;; (3) A marker (`help-window-point-marker') to move point in the help
cde56121
MR
1160;; window to an arbitrary buffer position.
1161
b3d8e4a0 1162;; Note: It's usually always wrong to use `help-print-return-message' in
cde56121
MR
1163;; the body of `with-help-window'.
1164(defmacro with-help-window (buffer-name &rest body)
357f93d2 1165 "Display buffer with name BUFFER-NAME in a help window evaluating BODY.
cde56121 1166Select help window if the actual value of the user option
4ee88440 1167`help-window-select' says so. Return last value in BODY."
cde56121 1168 (declare (indent 1) (debug t))
357f93d2 1169 `(progn
357f93d2
MR
1170 ;; Make `help-window-point-marker' point nowhere. The only place
1171 ;; where this should be set to a buffer position is within BODY.
cde56121 1172 (set-marker help-window-point-marker nil)
f678e0b6
MR
1173 (let* (help-window
1174 (temp-buffer-show-hook
1175 (cons (lambda () (setq help-window (selected-window)))
1176 temp-buffer-show-hook)))
1177 ;; Return value returned by `with-output-to-temp-buffer'.
1178 (prog1
1179 (with-output-to-temp-buffer ,buffer-name
1180 (progn ,@body))
1181 (help-window-setup help-window)))))
cbb59342
CY
1182
1183;; Called from C, on encountering `help-char' when reading a char.
1184;; Don't print to *Help*; that would clobber Help history.
1185(defun help-form-show ()
1186 "Display the output of a non-nil `help-form'."
1187 (let ((msg (eval help-form)))
1188 (if (stringp msg)
1189 (with-output-to-temp-buffer " *Char Help*"
1190 (princ msg)))))
cde56121 1191\f
172892e3
JB
1192(provide 'help)
1193
1a06eabd 1194;;; help.el ends here