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