help-map bindings moved to help.el.
[bpt/emacs.git] / lisp / help.el
CommitLineData
1a06eabd
ER
1;;; help.el --- help commands for Emacs
2
754084bb
GM
3;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001
4;; Free Software Foundation, Inc.
3a801d0c 5
e5167999 6;; Maintainer: FSF
fd7fa35a 7;; Keywords: help, internal
e5167999 8
433ae6f6
RS
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
e5167999 13;; the Free Software Foundation; either version 2, or (at your option)
433ae6f6
RS
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
b578f267
EN
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
433ae6f6 25
d9ecc911
ER
26;;; Commentary:
27
a1c9f209 28;; This code implements GNU Emacs' on-line help system, the one invoked by
95ac0a6f 29;; `M-x help-for-help'.
d9ecc911 30
e5167999
ER
31;;; Code:
32
8aa3a187
RS
33;; Get the macro make-help-screen when this is compiled,
34;; or run interpreted, but not when the compiled code is loaded.
b1fe9304 35(eval-when-compile (require 'help-macro))
788d62cf
MB
36
37;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
38(add-hook 'temp-buffer-setup-hook 'help-mode-setup)
39(add-hook 'temp-buffer-show-hook 'help-mode-finish)
41b8542b 40
433ae6f6
RS
41(defvar help-map (make-sparse-keymap)
42 "Keymap for characters following the Help key.")
43
e17d2fd1 44(define-key global-map (char-to-string help-char) 'help-command)
0af3df1c
RS
45(define-key global-map [help] 'help-command)
46(define-key global-map [f1] 'help-command)
433ae6f6
RS
47(fset 'help-command help-map)
48
e17d2fd1 49(define-key help-map (char-to-string help-char) 'help-for-help)
0af3df1c
RS
50(define-key help-map [help] 'help-for-help)
51(define-key help-map [f1] 'help-for-help)
433ae6f6
RS
52(define-key help-map "?" 'help-for-help)
53
54(define-key help-map "\C-c" 'describe-copying)
55(define-key help-map "\C-d" 'describe-distribution)
56(define-key help-map "\C-w" 'describe-no-warranty)
76766f2d 57(define-key help-map "\C-p" 'describe-project)
122955bf 58(define-key help-map "a" 'apropos-command)
433ae6f6
RS
59
60(define-key help-map "b" 'describe-bindings)
61
62(define-key help-map "c" 'describe-key-briefly)
63(define-key help-map "k" 'describe-key)
64
65(define-key help-map "d" 'describe-function)
66(define-key help-map "f" 'describe-function)
67
7ee71cf1
RS
68(define-key help-map "F" 'view-emacs-FAQ)
69
433ae6f6 70(define-key help-map "i" 'info)
4b08b7ed 71(define-key help-map "4i" 'info-other-window)
e5d77022
JB
72(define-key help-map "\C-f" 'Info-goto-emacs-command-node)
73(define-key help-map "\C-k" 'Info-goto-emacs-key-command-node)
32884eab 74(define-key help-map "\C-i" 'info-lookup-symbol)
433ae6f6
RS
75
76(define-key help-map "l" 'view-lossage)
77
78(define-key help-map "m" 'describe-mode)
754084bb 79(define-key help-map "\C-m" 'view-order-manuals)
433ae6f6
RS
80
81(define-key help-map "\C-n" 'view-emacs-news)
82(define-key help-map "n" 'view-emacs-news)
83
06b98c51 84(define-key help-map "p" 'finder-by-keyword)
3e9c095d
RS
85(autoload 'finder-by-keyword "finder"
86 "Find packages matching a given keyword." t)
06b98c51 87
4cbff657
DL
88(define-key help-map "P" 'view-emacs-problems)
89
433ae6f6
RS
90(define-key help-map "s" 'describe-syntax)
91
92(define-key help-map "t" 'help-with-tutorial)
93
94(define-key help-map "w" 'where-is)
95
96(define-key help-map "v" 'describe-variable)
97
2fc9d9f4
RS
98(define-key help-map "q" 'help-quit)
99
0cf0d828 100\f
2fc9d9f4 101(defun help-quit ()
3120a677 102 "Just exit from the Help command's command loop."
2fc9d9f4
RS
103 (interactive)
104 nil)
105
01364a75
RS
106(defvar help-return-method nil
107 "What to do to \"exit\" the help buffer.
108This is a list
109 (WINDOW . t) delete the selected window, go to WINDOW.
110 (WINDOW . quit-window) do quit-window, then select WINDOW.
111 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
112
433ae6f6
RS
113(defun print-help-return-message (&optional function)
114 "Display or return message saying how to restore windows after help command.
115Computes a message and applies the optional argument FUNCTION to it.
116If FUNCTION is nil, applies `message' to it, thus printing it."
117 (and (not (get-buffer-window standard-output))
d536293f 118 (let ((first-message
a1c9f209 119 (cond ((special-display-p (buffer-name standard-output))
01364a75 120 (setq help-return-method (cons (selected-window) t))
d536293f
RS
121 ;; If the help output buffer is a special display buffer,
122 ;; don't say anything about how to get rid of it.
123 ;; First of all, the user will do that with the window
124 ;; manager, not with Emacs.
125 ;; Secondly, the buffer has not been displayed yet,
126 ;; so we don't know whether its frame will be selected.
d536293f 127 nil)
f3ad2fc8
GM
128 (display-buffer-reuse-frames
129 (setq help-return-method (cons (selected-window)
130 'quit-window))
131 nil)
d536293f 132 ((not (one-window-p t))
01364a75
RS
133 (setq help-return-method
134 (cons (selected-window) 'quit-window))
d536293f
RS
135 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
136 (pop-up-windows
01364a75 137 (setq help-return-method (cons (selected-window) t))
d536293f
RS
138 "Type \\[delete-other-windows] to remove help window.")
139 (t
01364a75
RS
140 (setq help-return-method
141 (list (selected-window) (window-buffer)
142 (window-start) (window-point)))
d536293f
RS
143 "Type \\[switch-to-buffer] RET to remove help window."))))
144 (funcall (or function 'message)
145 (concat
146 (if first-message
376b2a24
DL
147 (substitute-command-keys first-message))
148 (if first-message " ")
125a8d70
RS
149 ;; If the help buffer will go in a separate frame,
150 ;; it's no use mentioning a command to scroll, so don't.
a1c9f209 151 (if (special-display-p (buffer-name standard-output))
125a8d70 152 nil
a1c9f209 153 (if (same-window-p (buffer-name standard-output))
125a8d70
RS
154 ;; Say how to scroll this window.
155 (substitute-command-keys
156 "\\[scroll-up] to scroll the help.")
157 ;; Say how to scroll some other window.
6e7f5182 158 (substitute-command-keys
125a8d70 159 "\\[scroll-other-window] to scroll the help."))))))))
433ae6f6 160
433ae6f6
RS
161;; So keyboard macro definitions are documented correctly
162(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
163
788d62cf
MB
164(defalias 'help 'help-for-help)
165(make-help-screen help-for-help
166 "a b c C f F C-f i I k C-k l L m n p s t v w C-c C-d C-n C-p C-w; ? for help:"
167 "You have typed %THIS-KEY%, the help character. Type a Help option:
168\(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
169
170a command-apropos. Give a substring, and see a list of commands
171 (functions interactively callable) that contain
172 that substring. See also the apropos command.
173b describe-bindings. Display table of all key bindings.
174c describe-key-briefly. Type a command key sequence;
175 it prints the function name that sequence runs.
176C describe-coding-system. This describes either a specific coding system
177 (if you type its name) or the coding systems currently in use
178 (if you type just RET).
179f describe-function. Type a function name and get documentation of it.
180C-f Info-goto-emacs-command-node. Type a function name;
181 it takes you to the Info node for that command.
182i info. The info documentation reader.
183I describe-input-method. Describe a specific input method (if you type
184 its name) or the current input method (if you type just RET).
185C-i info-lookup-symbol. Display the definition of a specific symbol
186 as found in the manual for the language this buffer is written in.
187k describe-key. Type a command key sequence;
188 it displays the full documentation.
189C-k Info-goto-emacs-key-command-node. Type a command key sequence;
190 it takes you to the Info node for the command bound to that key.
191l view-lossage. Show last 100 characters you typed.
192L describe-language-environment. This describes either a
193 specific language environment (if you type its name)
194 or the current language environment (if you type just RET).
195m describe-mode. Print documentation of current minor modes,
196 and the current major mode, including their special commands.
197n view-emacs-news. Display news of recent Emacs changes.
198p finder-by-keyword. Find packages matching a given topic keyword.
199s describe-syntax. Display contents of syntax table, plus explanations.
200t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
201v describe-variable. Type name of a variable;
202 it displays the variable's documentation and value.
203w where-is. Type command name; it prints which keystrokes
204 invoke that command.
205
206F Display the frequently asked questions file.
207h Display the HELLO file which illustrates various scripts.
208C-c Display Emacs copying permission (General Public License).
209C-d Display Emacs ordering information.
210C-n Display news of recent Emacs changes.
211C-p Display information about the GNU project.
212C-w Display information on absence of warranty for GNU Emacs."
213 help-map)
214
215\f
216
217(defun function-called-at-point ()
218 "Return a function around point or else called by the list containing point.
219If that doesn't give a function, return nil."
220 (with-syntax-table emacs-lisp-mode-syntax-table
221 (or (condition-case ()
222 (save-excursion
223 (or (not (zerop (skip-syntax-backward "_w")))
224 (eq (char-syntax (following-char)) ?w)
225 (eq (char-syntax (following-char)) ?_)
226 (forward-sexp -1))
227 (skip-chars-forward "'")
228 (let ((obj (read (current-buffer))))
229 (and (symbolp obj) (fboundp obj) obj)))
230 (error nil))
231 (condition-case ()
232 (save-excursion
233 (save-restriction
234 (narrow-to-region (max (point-min)
235 (- (point) 1000)) (point-max))
236 ;; Move up to surrounding paren, then after the open.
237 (backward-up-list 1)
238 (forward-char 1)
239 ;; If there is space here, this is probably something
240 ;; other than a real Lisp function call, so ignore it.
241 (if (looking-at "[ \t]")
242 (error "Probably not a Lisp function call"))
243 (let ((obj (read (current-buffer))))
244 (and (symbolp obj) (fboundp obj) obj))))
245 (error nil)))))
246
247\f
248;;; `User' help functions
249
433ae6f6
RS
250(defun describe-distribution ()
251 "Display info on how to obtain the latest version of GNU Emacs."
252 (interactive)
64f3b7d3 253 (view-find (expand-file-name "DISTRIB" data-directory)))
433ae6f6
RS
254
255(defun describe-copying ()
256 "Display info on how you may redistribute copies of GNU Emacs."
257 (interactive)
64f3b7d3 258 (view-file (expand-file-name "COPYING" data-directory))
433ae6f6
RS
259 (goto-char (point-min)))
260
76766f2d
RS
261(defun describe-project ()
262 "Display info on the GNU project."
263 (interactive)
64f3b7d3 264 (view-file (expand-file-name "THE-GNU-PROJECT" data-directory))
76766f2d
RS
265 (goto-char (point-min)))
266
433ae6f6
RS
267(defun describe-no-warranty ()
268 "Display info on all the kinds of warranty Emacs does NOT have."
269 (interactive)
270 (describe-copying)
271 (let (case-fold-search)
272 (search-forward "NO WARRANTY")
273 (recenter 0)))
274
61c6b658 275(defun describe-prefix-bindings ()
c7cba9cb
RS
276 "Describe the bindings of the prefix used to reach this command.
277The prefix described consists of all but the last event
278of the key sequence that ran this command."
61c6b658 279 (interactive)
ccc06dcc
KH
280 (let* ((key (this-command-keys)))
281 (describe-bindings
282 (if (stringp key)
283 (substring key 0 (1- (length key)))
284 (let ((prefix (make-vector (1- (length key)) nil))
285 (i 0))
286 (while (< i (length prefix))
287 (aset prefix i (aref key i))
288 (setq i (1+ i)))
289 prefix)))))
788d62cf 290;; Make C-h after a prefix, when not specifically bound,
c7cba9cb 291;; run describe-prefix-bindings.
61c6b658
RS
292(setq prefix-help-command 'describe-prefix-bindings)
293
382d018a
RS
294(defun view-emacs-news (&optional arg)
295 "Display info on recent changes to Emacs.
f0753a5f 296With numeric argument, display information on correspondingly older changes."
382d018a 297 (interactive "P")
f0753a5f
GM
298 (let* ((arg (if arg (prefix-numeric-value arg) 0))
299 (file (cond ((eq arg 0) "NEWS")
300 ((eq arg 1) "ONEWS")
301 (t
302 (nth (- arg 2)
303 (nreverse (directory-files data-directory
975f82c9 304 nil "^ONEWS\\.[0-9]+$"
f0753a5f
GM
305 nil)))))))
306 (if file
64f3b7d3 307 (view-file (expand-file-name file data-directory))
f0753a5f 308 (error "No such old news"))))
433ae6f6 309
754084bb
GM
310(defun view-order-manuals ()
311 "Display the Emacs ORDERS file."
312 (interactive)
64f3b7d3 313 (view-file (expand-file-name "ORDERS" data-directory))
38790755 314 (goto-address))
754084bb 315
7ee71cf1
RS
316(defun view-emacs-FAQ ()
317 "Display the Emacs Frequently Asked Questions (FAQ) file."
318 (interactive)
94ea540b 319 ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
279b772d 320 (info "(efaq)"))
7ee71cf1 321
4cbff657
DL
322(defun view-emacs-problems ()
323 "Display info on known problems with Emacs and possible workarounds."
324 (interactive)
325 (view-file (expand-file-name "PROBLEMS" data-directory)))
326
433ae6f6 327(defun view-lossage ()
50b57199
EZ
328 "Display last 100 input keystrokes.
329
330To record all your input on a file, use `open-dribble-file'."
433ae6f6 331 (interactive)
3e5929af
SM
332 (help-setup-xref (list #'view-lossage) (interactive-p))
333 (with-output-to-temp-buffer (help-buffer)
298a7c8c
RS
334 (princ (mapconcat (function (lambda (key)
335 (if (or (integerp key)
336 (symbolp key)
337 (listp key))
338 (single-key-description key)
339 (prin1-to-string key nil))))
340 (recent-keys)
341 " "))
b0fbf754 342 (with-current-buffer standard-output
433ae6f6
RS
343 (goto-char (point-min))
344 (while (progn (move-to-column 50) (not (eobp)))
345 (search-forward " " nil t)
3e5929af 346 (insert "\n")))
433ae6f6
RS
347 (print-help-return-message)))
348
788d62cf
MB
349\f
350;; Key bindings
433ae6f6 351
4c45295b 352(defun describe-bindings (&optional prefix buffer)
a8ad43aa
RS
353 "Show a list of all defined keys, and their definitions.
354We put that list in a buffer, and display the buffer.
355
356The optional argument PREFIX, if non-nil, should be a key sequence;
4c45295b
KH
357then we display only bindings that start with that prefix.
358The optional argument BUFFER specifies which buffer's bindings
359to display (default, the current buffer)."
3e5929af 360 (interactive)
4c45295b 361 (or buffer (setq buffer (current-buffer)))
3e5929af 362 (help-setup-xref (list #'describe-bindings prefix buffer) (interactive-p))
4c45295b 363 (with-current-buffer buffer
3e5929af 364 (describe-bindings-internal nil prefix)))
a8ad43aa 365
94ea540b
SM
366;; This function used to be in keymap.c.
367(defun describe-bindings-internal (&optional menus prefix)
368 "Show a list of all defined keys, and their definitions.
369We put that list in a buffer, and display the buffer.
370
371The optional argument MENUS, if non-nil, says to mention menu bindings.
372\(Ordinarily these are omitted from the output.)
373The optional argument PREFIX, if non-nil, should be a key sequence;
374then we display only bindings that start with that prefix."
375 (interactive)
376 (let ((buf (current-buffer)))
377 (with-output-to-temp-buffer "*Help*"
378 (with-current-buffer standard-output
379 (describe-buffer-bindings buf prefix menus)))))
380
e88a2c59 381(defun where-is (definition &optional insert)
b2c85790 382 "Print message listing key sequences that invoke the command DEFINITION.
e88a2c59
RS
383Argument is a command definition, usually a symbol with a function definition.
384If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
54c0b967
RS
385 (interactive
386 (let ((fn (function-called-at-point))
788d62cf 387 (enable-recursive-minibuffers t)
54c0b967
RS
388 val)
389 (setq val (completing-read (if fn
390 (format "Where is command (default %s): " fn)
391 "Where is command: ")
d5f65532 392 obarray 'commandp t))
54c0b967 393 (list (if (equal val "")
e88a2c59
RS
394 fn (intern val))
395 current-prefix-arg)))
54c0b967 396 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
e88a2c59
RS
397 (keys1 (mapconcat 'key-description keys ", "))
398 (standard-output (if insert (current-buffer) t)))
399 (if insert
400 (if (> (length keys1) 0)
401 (princ (format "%s (%s)" keys1 definition))
402 (princ (format "M-x %s RET" definition)))
403 (if (> (length keys1) 0)
404 (princ (format "%s is on %s" definition keys1))
405 (princ (format "%s is not on any key" definition)))))
54c0b967
RS
406 nil)
407
788d62cf
MB
408(defun string-key-binding (key)
409 "Value is the binding of KEY in a string.
410If KEY is an event on a string, and that string has a `local-map'
411or `keymap' property, return the binding of KEY in the string's keymap."
412 (let* ((defn nil)
413 (start (when (vectorp key)
414 (if (memq (aref key 0) '(mode-line header-line))
415 (event-start (aref key 1))
416 (and (consp (aref key 0))
417 (event-start (aref key 0))))))
418 (string-info (and (consp start) (nth 4 start))))
419 (when string-info
420 (let* ((string (car string-info))
421 (pos (cdr string-info))
422 (local-map (and (> pos 0)
423 (< pos (length string))
424 (or (get-text-property pos 'local-map string)
425 (get-text-property pos 'keymap string)))))
426 (setq defn (and local-map (lookup-key local-map key)))))
427 defn))
1a06eabd 428
788d62cf
MB
429(defun describe-key-briefly (key &optional insert)
430 "Print the name of the function KEY invokes. KEY is a string.
431If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
432 (interactive "kDescribe key briefly: \nP")
400a1b1f 433 (save-excursion
788d62cf
MB
434 (let ((modifiers (event-modifiers (aref key 0)))
435 (standard-output (if insert (current-buffer) t))
436 window position)
437 ;; For a mouse button event, go to the button it applies to
438 ;; to get the right key bindings. And go to the right place
439 ;; in case the keymap depends on where you clicked.
440 (if (or (memq 'click modifiers) (memq 'down modifiers)
441 (memq 'drag modifiers))
442 (setq window (posn-window (event-start (aref key 0)))
443 position (posn-point (event-start (aref key 0)))))
444 (if (windowp window)
445 (progn
446 (set-buffer (window-buffer window))
447 (goto-char position)))
448 ;; Ok, now look up the key and name the command.
449 (let ((defn (or (string-key-binding key)
450 (key-binding key)))
451 (key-desc (key-description key)))
452 (if (or (null defn) (integerp defn))
453 (princ (format "%s is undefined" key-desc))
454 (princ (format (if insert
455 "`%s' (`%s')"
456 (if (windowp window)
457 "%s at that spot runs the command %s"
458 "%s runs the command %s"))
459 key-desc
460 (if (symbolp defn) defn (prin1-to-string defn)))))))))
96ede6b2 461
788d62cf
MB
462
463(defun describe-key (key)
a0d32c10
RS
464 "Display documentation of the function invoked by KEY.
465KEY should be a key sequence--when calling from a program,
466pass a string or a vector."
788d62cf
MB
467 (interactive "kDescribe key: ")
468 (save-excursion
469 (let ((modifiers (event-modifiers (aref key 0)))
470 window position)
471 ;; For a mouse button event, go to the button it applies to
472 ;; to get the right key bindings. And go to the right place
473 ;; in case the keymap depends on where you clicked.
474 (if (or (memq 'click modifiers) (memq 'down modifiers)
475 (memq 'drag modifiers))
476 (setq window (posn-window (event-start (aref key 0)))
477 position (posn-point (event-start (aref key 0)))))
94ea540b 478 (when (windowp window)
788d62cf 479 (set-buffer (window-buffer window))
94ea540b 480 (goto-char position))
788d62cf
MB
481 (let ((defn (or (string-key-binding key) (key-binding key))))
482 (if (or (null defn) (integerp defn))
483 (message "%s is undefined" (key-description key))
3e5929af
SM
484 (help-setup-xref (list #'describe-function defn) (interactive-p))
485 (with-output-to-temp-buffer (help-buffer)
788d62cf
MB
486 (princ (key-description key))
487 (if (windowp window)
488 (princ " at that spot"))
489 (princ " runs the command ")
490 (prin1 defn)
491 (princ "\n which is ")
3e5929af 492 (describe-function-1 defn)
788d62cf 493 (print-help-return-message)))))))
400a1b1f 494
400a1b1f 495\f
788d62cf
MB
496(defun describe-mode (&optional buffer)
497 "Display documentation of current major mode and minor modes.
498The major mode description comes first, followed by the minor modes,
499each on a separate page.
500For this to work correctly for a minor mode, the mode's indicator variable
501\(listed in `minor-mode-alist') must also be a function whose documentation
502describes the minor mode."
400a1b1f 503 (interactive)
9639be74
RS
504 (help-setup-xref (list #'describe-mode (or buffer (current-buffer)))
505 (interactive-p))
506 ;; For the sake of help-do-xref and help-xref-go-back,
507 ;; don't switch buffers before calling `help-buffer'.
3e5929af 508 (with-output-to-temp-buffer (help-buffer)
9639be74
RS
509 (save-excursion
510 (when buffer (set-buffer buffer))
511 (when minor-mode-alist
512 (princ "The major mode is described first.
788d62cf 513For minor modes, see following pages.\n\n"))
9639be74
RS
514 (princ mode-name)
515 (princ " mode:\n")
516 (princ (documentation major-mode))
517 (let ((minor-modes minor-mode-alist))
518 (while minor-modes
519 (let* ((minor-mode (car (car minor-modes)))
520 (indicator (car (cdr (car minor-modes)))))
521 ;; Document a minor mode if it is listed in minor-mode-alist,
522 ;; bound locally in this buffer, non-nil, and has a function
523 ;; definition.
524 (if (and (boundp minor-mode)
525 (symbol-value minor-mode)
526 (fboundp minor-mode))
527 (let ((pretty-minor-mode minor-mode))
528 (if (string-match "\\(-minor\\)?-mode\\'"
529 (symbol-name minor-mode))
530 (setq pretty-minor-mode
531 (capitalize
532 (substring (symbol-name minor-mode)
533 0 (match-beginning 0)))))
534 (while (and indicator (symbolp indicator)
535 (boundp indicator)
536 (not (eq indicator (symbol-value indicator))))
537 (setq indicator (symbol-value indicator)))
538 (princ "\n\f\n")
539 (princ (format "%s minor mode (%s):\n"
540 pretty-minor-mode
541 (if indicator
542 (format "indicator%s" indicator)
543 "no indicator")))
544 (princ (documentation minor-mode)))))
545 (setq minor-modes (cdr minor-modes))))
546 (print-help-return-message))))
400a1b1f 547
48ce3c22
RS
548\f
549;;; Automatic resizing of temporary buffers.
550
4483ddc5 551(defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
48ce3c22
RS
552 "*Maximum height of a window displaying a temporary buffer.
553This is the maximum height (in text lines) which `resize-temp-buffer-window'
554will give to a window displaying a temporary buffer.
555It can also be a function which will be called with the object corresponding
556to the buffer to be displayed as argument and should return an integer
557positive number."
558 :type '(choice integer function)
559 :group 'help
560 :version "20.4")
561
4e1ede6c
SM
562(define-minor-mode temp-buffer-resize-mode
563 "Toggle the mode which makes windows smaller for temporary buffers.
48ce3c22
RS
564With prefix argument ARG, turn the resizing of windows displaying temporary
565buffers on if ARG is positive or off otherwise.
4e1ede6c
SM
566This makes the window the right height for its contents, but never
567more than `temp-buffer-max-height' nor less than `window-min-height'.
568This applies to `help', `apropos' and `completion' buffers, and some others."
b0fbf754 569 :global t :group 'help
4e1ede6c 570 (if temp-buffer-resize-mode
57f43907 571 ;; `help-make-xrefs' may add a `back' button and thus increase the
4e1ede6c
SM
572 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
573 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
8304a3bb 574 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
48ce3c22
RS
575
576(defun resize-temp-buffer-window ()
577 "Resize the current window to fit its contents.
4483ddc5 578Will not make it higher than `temp-buffer-max-height' nor smaller than
b2c85790 579`window-min-height'. Do nothing if it is the only window on its frame, if it
48ce3c22
RS
580is not as wide as the frame or if some of the window's contents are scrolled
581out of view."
582 (unless (or (one-window-p 'nomini)
583 (not (pos-visible-in-window-p (point-min)))
584 (/= (frame-width) (window-width)))
d9c30bdf
MB
585 (fit-window-to-buffer
586 (selected-window)
587 (if (functionp temp-buffer-max-height)
588 (funcall temp-buffer-max-height (current-buffer))
589 temp-buffer-max-height))))
48ce3c22 590
5fbeea74
GM
591;; Provide this for the sake of define-minor-mode which generates
592;; defcustoms which require 'help'.
5fbeea74
GM
593(provide 'help)
594
1a06eabd 595;;; help.el ends here