]> code.delx.au - gnu-emacs/blob - lisp/help.el
(sql-accumulate-and-indent): Instead of testing
[gnu-emacs] / lisp / help.el
1 ;;; help.el --- help commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: help, internal
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This code implements GNU Emacs' on-line help system, the one invoked by
28 ;;`M-x help-for-help'.
29
30 ;;; Code:
31
32 ;; Get the macro make-help-screen when this is compiled,
33 ;; or run interpreted, but not when the compiled code is loaded.
34 (eval-when-compile (require 'help-macro))
35
36 (defvar help-map (make-sparse-keymap)
37 "Keymap for characters following the Help key.")
38
39 (defvar help-mode-map (make-sparse-keymap)
40 "Keymap for help mode.")
41
42 (define-key global-map (char-to-string help-char) 'help-command)
43 (define-key global-map [help] 'help-command)
44 (define-key global-map [f1] 'help-command)
45 (fset 'help-command help-map)
46
47 (define-key help-map (char-to-string help-char) 'help-for-help)
48 (define-key help-map [help] 'help-for-help)
49 (define-key help-map [f1] 'help-for-help)
50 (define-key help-map "?" 'help-for-help)
51
52 (define-key help-map "\C-c" 'describe-copying)
53 (define-key help-map "\C-d" 'describe-distribution)
54 (define-key help-map "\C-w" 'describe-no-warranty)
55 (define-key help-map "\C-p" 'describe-project)
56 (define-key help-map "a" 'apropos-command)
57
58 (define-key help-map "b" 'describe-bindings)
59
60 (define-key help-map "c" 'describe-key-briefly)
61 (define-key help-map "k" 'describe-key)
62
63 (define-key help-map "d" 'describe-function)
64 (define-key help-map "f" 'describe-function)
65
66 (define-key help-map "F" 'view-emacs-FAQ)
67
68 (define-key help-map "i" 'info)
69 (define-key help-map "4i" 'info-other-window)
70 (define-key help-map "\C-f" 'Info-goto-emacs-command-node)
71 (define-key help-map "\C-k" 'Info-goto-emacs-key-command-node)
72 (define-key help-map "\C-i" 'info-lookup-symbol)
73
74 (define-key help-map "l" 'view-lossage)
75
76 (define-key help-map "m" 'describe-mode)
77
78 (define-key help-map "\C-n" 'view-emacs-news)
79 (define-key help-map "n" 'view-emacs-news)
80
81 (define-key help-map "p" 'finder-by-keyword)
82 (autoload 'finder-by-keyword "finder"
83 "Find packages matching a given keyword." t)
84
85 (define-key help-map "s" 'describe-syntax)
86
87 (define-key help-map "t" 'help-with-tutorial)
88
89 (define-key help-map "w" 'where-is)
90
91 (define-key help-map "v" 'describe-variable)
92
93 (define-key help-map "q" 'help-quit)
94
95 (define-key help-mode-map [mouse-2] 'help-follow-mouse)
96 (define-key help-mode-map "\C-c\C-b" 'help-go-back)
97 (define-key help-mode-map "\C-c\C-c" 'help-follow)
98 (define-key help-mode-map "\t" 'help-next-ref)
99 (define-key help-mode-map [backtab] 'help-previous-ref)
100 (define-key help-mode-map [(shift tab)] 'help-previous-ref)
101 ;; Documentation only, since we use minor-mode-overriding-map-alist.
102 (define-key help-mode-map "\r" 'help-follow)
103
104 ;; Font-locking is incompatible with the new xref stuff.
105 ;(defvar help-font-lock-keywords
106 ; (eval-when-compile
107 ; (let ((name-char "[-+a-zA-Z0-9_*]") (sym-char "[-+a-zA-Z0-9_:*]"))
108 ; (list
109 ; ;;
110 ; ;; The symbol itself.
111 ; (list (concat "\\`\\(" name-char "+\\)\\(\\(:\\)\\|\\('\\)\\)")
112 ; '(1 (if (match-beginning 3)
113 ; font-lock-function-name-face
114 ; font-lock-variable-name-face)))
115 ; ;;
116 ; ;; Words inside `' which tend to be symbol names.
117 ; (list (concat "`\\(" sym-char sym-char "+\\)'")
118 ; 1 'font-lock-constant-face t)
119 ; ;;
120 ; ;; CLisp `:' keywords as references.
121 ; (list (concat "\\<:" sym-char "+\\>") 0 'font-lock-builtin-face t))))
122 ; "Default expressions to highlight in Help mode.")
123
124 (defvar help-xref-stack nil
125 "A stack of ways by which to return to help buffers after following xrefs.
126 Used by `help-follow' and `help-xref-go-back'.
127 An element looks like (POSITION FUNCTION ARGS...).
128 To use the element, do (apply FUNCTION ARGS) then (goto-char POSITION).")
129 (put 'help-xref-stack 'permanent-local t)
130
131 (defvar help-xref-stack-item nil
132 "An item for `help-follow' in this buffer to push onto `help-xref-stack'.
133 The format is (FUNCTION ARGS...).")
134 (put 'help-xref-stack-item 'permanent-local t)
135
136 (setq-default help-xref-stack nil help-xref-stack-item nil)
137
138 (defun help-mode ()
139 "Major mode for viewing help text and navigating references in it.
140 Entry to this mode runs the normal hook `help-mode-hook'.
141 Commands:
142 \\{help-mode-map}"
143 (interactive)
144 (kill-all-local-variables)
145 (use-local-map help-mode-map)
146 (setq mode-name "Help")
147 (setq major-mode 'help-mode)
148 (make-local-variable 'font-lock-defaults)
149 (setq font-lock-defaults nil) ; font-lock would defeat xref
150 (view-mode)
151 (make-local-variable 'view-no-disable-on-exit)
152 (setq view-no-disable-on-exit t)
153 ;; `help-make-xrefs' would be run here if not invoked from
154 ;; `help-mode-maybe'.
155 (run-hooks 'help-mode-hook))
156
157 (defun help-mode-setup ()
158 (help-mode)
159 (setq buffer-read-only nil))
160
161 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
162
163 (defun help-mode-finish ()
164 (when (eq major-mode 'help-mode)
165 ;; View mode's read-only status of existing *Help* buffer is lost
166 ;; by with-output-to-temp-buffer.
167 (toggle-read-only 1)
168 (help-make-xrefs (current-buffer)))
169 (setq view-return-to-alist
170 (list (cons (selected-window) help-return-method))))
171
172 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
173
174 (defun help-quit ()
175 "Just exit from the Help command's command loop."
176 (interactive)
177 nil)
178
179 (defun help-with-tutorial (&optional arg)
180 "Select the Emacs learn-by-doing tutorial.
181 If there is a tutorial version written in the language
182 of the selected language environment, that version is used.
183 If there's no tutorial in that language, `TUTORIAL' is selected.
184 With arg, you are asked to choose which language."
185 (interactive "P")
186 (let ((lang (if arg
187 (read-language-name 'tutorial "Language: " "English")
188 (if (get-language-info current-language-environment 'tutorial)
189 current-language-environment
190 "English")))
191 file filename)
192 (setq filename (get-language-info lang 'tutorial))
193 (setq file (expand-file-name (concat "~/" filename)))
194 (delete-other-windows)
195 (if (get-file-buffer file)
196 (switch-to-buffer (get-file-buffer file))
197 (switch-to-buffer (create-file-buffer file))
198 (setq buffer-file-name file)
199 (setq default-directory (expand-file-name "~/"))
200 (setq buffer-auto-save-file-name nil)
201 (insert-file-contents (expand-file-name filename data-directory))
202 (goto-char (point-min))
203 (search-forward "\n<<")
204 (beginning-of-line)
205 (delete-region (point) (progn (end-of-line) (point)))
206 (let ((n (- (window-height (selected-window))
207 (count-lines (point-min) (point))
208 6)))
209 (if (< n 12)
210 (newline n)
211 ;; Some people get confused by the large gap.
212 (newline (/ n 2))
213 (insert "[Middle of page left blank for didactic purposes. "
214 "Text continues below]")
215 (newline (- n (/ n 2)))))
216 (goto-char (point-min))
217 (set-buffer-modified-p nil))))
218
219 (defun mode-line-key-binding (key)
220 "Value is the binding of KEY in the mode line or nil if none."
221 (let (string-info defn)
222 (when (and (eq 'mode-line (aref key 0))
223 (consp (setq string-info (nth 4 (event-start (aref key 1))))))
224 (let* ((string (car string-info))
225 (pos (cdr string-info))
226 (local-map (and (> pos 0)
227 (< pos (length string))
228 (get-text-property pos 'local-map string))))
229 (setq defn (and local-map (lookup-key local-map key)))))
230 defn))
231
232 (defun describe-key-briefly (key &optional insert)
233 "Print the name of the function KEY invokes. KEY is a string.
234 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
235 (interactive "kDescribe key briefly: \nP")
236 (save-excursion
237 (let ((modifiers (event-modifiers (aref key 0)))
238 (standard-output (if insert (current-buffer) t))
239 window position)
240 ;; For a mouse button event, go to the button it applies to
241 ;; to get the right key bindings. And go to the right place
242 ;; in case the keymap depends on where you clicked.
243 (if (or (memq 'click modifiers) (memq 'down modifiers)
244 (memq 'drag modifiers))
245 (setq window (posn-window (event-start (aref key 0)))
246 position (posn-point (event-start (aref key 0)))))
247 (if (windowp window)
248 (progn
249 (set-buffer (window-buffer window))
250 (goto-char position)))
251 ;; Ok, now look up the key and name the command.
252 (let ((defn (or (mode-line-key-binding key)
253 (key-binding key)))
254 (key-desc (key-description key)))
255 (if (or (null defn) (integerp defn))
256 (princ (format "%s is undefined" key-desc))
257 (princ (format (if insert
258 "`%s' (`%s')"
259 (if (windowp window)
260 "%s at that spot runs the command %s"
261 "%s runs the command %s"))
262 key-desc
263 (if (symbolp defn) defn (prin1-to-string defn)))))))))
264
265 (defvar help-return-method nil
266 "What to do to \"exit\" the help buffer.
267 This is a list
268 (WINDOW . t) delete the selected window, go to WINDOW.
269 (WINDOW . quit-window) do quit-window, then select WINDOW.
270 (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
271
272 (defun print-help-return-message (&optional function)
273 "Display or return message saying how to restore windows after help command.
274 Computes a message and applies the optional argument FUNCTION to it.
275 If FUNCTION is nil, applies `message' to it, thus printing it."
276 (and (not (get-buffer-window standard-output))
277 (let ((first-message
278 (cond ((special-display-p (buffer-name standard-output))
279 (setq help-return-method (cons (selected-window) t))
280 ;; If the help output buffer is a special display buffer,
281 ;; don't say anything about how to get rid of it.
282 ;; First of all, the user will do that with the window
283 ;; manager, not with Emacs.
284 ;; Secondly, the buffer has not been displayed yet,
285 ;; so we don't know whether its frame will be selected.
286 nil)
287 ((not (one-window-p t))
288 (setq help-return-method
289 (cons (selected-window) 'quit-window))
290 "Type \\[switch-to-buffer-other-window] RET to restore the other window.")
291 (pop-up-windows
292 (setq help-return-method (cons (selected-window) t))
293 "Type \\[delete-other-windows] to remove help window.")
294 (t
295 (setq help-return-method
296 (list (selected-window) (window-buffer)
297 (window-start) (window-point)))
298 "Type \\[switch-to-buffer] RET to remove help window."))))
299 (funcall (or function 'message)
300 (concat
301 (if first-message
302 (substitute-command-keys first-message)
303 "")
304 (if first-message " " "")
305 ;; If the help buffer will go in a separate frame,
306 ;; it's no use mentioning a command to scroll, so don't.
307 (if (special-display-p (buffer-name standard-output))
308 nil
309 (if (same-window-p (buffer-name standard-output))
310 ;; Say how to scroll this window.
311 (substitute-command-keys
312 "\\[scroll-up] to scroll the help.")
313 ;; Say how to scroll some other window.
314 (substitute-command-keys
315 "\\[scroll-other-window] to scroll the help."))))))))
316
317 (defun describe-key (key)
318 "Display documentation of the function invoked by KEY. KEY is a string."
319 (interactive "kDescribe key: ")
320 (save-excursion
321 (let ((modifiers (event-modifiers (aref key 0)))
322 window position)
323 ;; For a mouse button event, go to the button it applies to
324 ;; to get the right key bindings. And go to the right place
325 ;; in case the keymap depends on where you clicked.
326 (if (or (memq 'click modifiers) (memq 'down modifiers)
327 (memq 'drag modifiers))
328 (setq window (posn-window (event-start (aref key 0)))
329 position (posn-point (event-start (aref key 0)))))
330 (if (windowp window)
331 (progn
332 (set-buffer (window-buffer window))
333 (goto-char position)))
334 (let ((defn (or (mode-line-key-binding key) (key-binding key))))
335 (if (or (null defn) (integerp defn))
336 (message "%s is undefined" (key-description key))
337 (with-output-to-temp-buffer "*Help*"
338 (princ (key-description key))
339 (if (windowp window)
340 (princ " at that spot"))
341 (princ " runs the command ")
342 (prin1 defn)
343 (princ "\n which is ")
344 (describe-function-1 defn nil (interactive-p))
345 (print-help-return-message)))))))
346
347 (defun describe-mode ()
348 "Display documentation of current major mode and minor modes.
349 The major mode description comes first, followed by the minor modes,
350 each on a separate page.
351
352 For this to work correctly for a minor mode, the mode's indicator variable
353 \(listed in `minor-mode-alist') must also be a function whose documentation
354 describes the minor mode."
355 (interactive)
356 (with-output-to-temp-buffer "*Help*"
357 (when minor-mode-alist
358 (princ "The major mode is described first.
359 For minor modes, see following pages.\n\n"))
360 (princ mode-name)
361 (princ " mode:\n")
362 (princ (documentation major-mode))
363 (help-setup-xref (list #'help-xref-mode (current-buffer)) (interactive-p))
364 (let ((minor-modes minor-mode-alist))
365 (while minor-modes
366 (let* ((minor-mode (car (car minor-modes)))
367 (indicator (car (cdr (car minor-modes)))))
368 ;; Document a minor mode if it is listed in minor-mode-alist,
369 ;; bound locally in this buffer, non-nil, and has a function
370 ;; definition.
371 (if (and (symbol-value minor-mode)
372 (fboundp minor-mode))
373 (let ((pretty-minor-mode minor-mode))
374 (if (string-match "-mode$" (symbol-name minor-mode))
375 (setq pretty-minor-mode
376 (capitalize
377 (substring (symbol-name minor-mode)
378 0 (match-beginning 0)))))
379 (while (and indicator (symbolp indicator)
380 (boundp indicator)
381 (not (eq indicator (symbol-value indicator))))
382 (setq indicator (symbol-value indicator)))
383 (princ "\n\f\n")
384 (princ (format "%s minor mode (%s):\n"
385 pretty-minor-mode
386 (if indicator
387 (format "indicator%s" indicator)
388 "no indicator")))
389 (princ (documentation minor-mode)))))
390 (setq minor-modes (cdr minor-modes))))
391 (print-help-return-message)))
392
393 ;; So keyboard macro definitions are documented correctly
394 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
395
396 (defun describe-distribution ()
397 "Display info on how to obtain the latest version of GNU Emacs."
398 (interactive)
399 (find-file-read-only
400 (expand-file-name "DISTRIB" data-directory)))
401
402 (defun describe-copying ()
403 "Display info on how you may redistribute copies of GNU Emacs."
404 (interactive)
405 (find-file-read-only
406 (expand-file-name "COPYING" data-directory))
407 (goto-char (point-min)))
408
409 (defun describe-project ()
410 "Display info on the GNU project."
411 (interactive)
412 (find-file-read-only
413 (expand-file-name "GNU" data-directory))
414 (goto-char (point-min)))
415
416 (defun describe-no-warranty ()
417 "Display info on all the kinds of warranty Emacs does NOT have."
418 (interactive)
419 (describe-copying)
420 (let (case-fold-search)
421 (search-forward "NO WARRANTY")
422 (recenter 0)))
423
424 (defun describe-prefix-bindings ()
425 "Describe the bindings of the prefix used to reach this command.
426 The prefix described consists of all but the last event
427 of the key sequence that ran this command."
428 (interactive)
429 (let* ((key (this-command-keys)))
430 (describe-bindings
431 (if (stringp key)
432 (substring key 0 (1- (length key)))
433 (let ((prefix (make-vector (1- (length key)) nil))
434 (i 0))
435 (while (< i (length prefix))
436 (aset prefix i (aref key i))
437 (setq i (1+ i)))
438 prefix)))))
439 ;; Make C-h after a prefix, when not specifically bound,
440 ;; run describe-prefix-bindings.
441 (setq prefix-help-command 'describe-prefix-bindings)
442
443 (defun view-emacs-news (&optional arg)
444 "Display info on recent changes to Emacs.
445 With numeric argument display information on correspondingly older changes."
446 (interactive "P")
447 (let* ((arg (if arg (prefix-numeric-value arg) 0)))
448 (find-file-read-only
449 (expand-file-name (concat (make-string arg ?O) "NEWS")
450 data-directory))))
451
452 (defun view-emacs-FAQ ()
453 "Display the Emacs Frequently Asked Questions (FAQ) file."
454 (interactive)
455 (find-file-read-only (expand-file-name "FAQ" data-directory)))
456
457 (defun view-lossage ()
458 "Display last 100 input keystrokes."
459 (interactive)
460 (with-output-to-temp-buffer "*Help*"
461 (princ (mapconcat (function (lambda (key)
462 (if (or (integerp key)
463 (symbolp key)
464 (listp key))
465 (single-key-description key)
466 (prin1-to-string key nil))))
467 (recent-keys)
468 " "))
469 (save-excursion
470 (set-buffer standard-output)
471 (goto-char (point-min))
472 (while (progn (move-to-column 50) (not (eobp)))
473 (search-forward " " nil t)
474 (insert "\n"))
475 (setq help-xref-stack nil
476 help-xref-stack-item nil))
477 (print-help-return-message)))
478
479 (defalias 'help 'help-for-help)
480 (make-help-screen help-for-help
481 "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:"
482 "You have typed %THIS-KEY%, the help character. Type a Help option:
483 \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
484
485 a command-apropos. Give a substring, and see a list of commands
486 (functions interactively callable) that contain
487 that substring. See also the apropos command.
488 b describe-bindings. Display table of all key bindings.
489 c describe-key-briefly. Type a command key sequence;
490 it prints the function name that sequence runs.
491 C describe-coding-system. This describes either a specific coding system
492 (if you type its name) or the coding systems currently in use
493 (if you type just RET).
494 f describe-function. Type a function name and get documentation of it.
495 C-f Info-goto-emacs-command-node. Type a function name;
496 it takes you to the Info node for that command.
497 i info. The info documentation reader.
498 I describe-input-method. Describe a specific input method (if you type
499 its name) or the current input method (if you type just RET).
500 C-i info-lookup-symbol. Display the definition of a specific symbol
501 as found in the manual for the language this buffer is written in.
502 k describe-key. Type a command key sequence;
503 it displays the full documentation.
504 C-k Info-goto-emacs-key-command-node. Type a command key sequence;
505 it takes you to the Info node for the command bound to that key.
506 l view-lossage. Show last 100 characters you typed.
507 L describe-language-environment. This describes either the a
508 specific language environment (if you type its name)
509 or the current language environment (if you type just RET).
510 m describe-mode. Print documentation of current minor modes,
511 and the current major mode, including their special commands.
512 n view-emacs-news. Display news of recent Emacs changes.
513 p finder-by-keyword. Find packages matching a given topic keyword.
514 s describe-syntax. Display contents of syntax table, plus explanations
515 t help-with-tutorial. Select the Emacs learn-by-doing tutorial.
516 v describe-variable. Type name of a variable;
517 it displays the variable's documentation and value.
518 w where-is. Type command name; it prints which keystrokes
519 invoke that command.
520
521 F Display the frequently asked questions file.
522 h Display the HELLO file which illustrates various scripts.
523 C-c Display Emacs copying permission (General Public License).
524 C-d Display Emacs ordering information.
525 C-n Display news of recent Emacs changes.
526 C-p Display information about the GNU project.
527 C-w Display information on absence of warranty for GNU Emacs."
528 help-map)
529
530 (defun function-called-at-point ()
531 "Return a function around point or else called by the list containing point.
532 If that doesn't give a function, return nil."
533 (let ((stab (syntax-table)))
534 (set-syntax-table emacs-lisp-mode-syntax-table)
535 (unwind-protect
536 (or (condition-case ()
537 (save-excursion
538 (or (not (zerop (skip-syntax-backward "_w")))
539 (eq (char-syntax (following-char)) ?w)
540 (eq (char-syntax (following-char)) ?_)
541 (forward-sexp -1))
542 (skip-chars-forward "'")
543 (let ((obj (read (current-buffer))))
544 (and (symbolp obj) (fboundp obj) obj)))
545 (error nil))
546 (condition-case ()
547 (save-excursion
548 (save-restriction
549 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
550 ;; Move up to surrounding paren, then after the open.
551 (backward-up-list 1)
552 (forward-char 1)
553 ;; If there is space here, this is probably something
554 ;; other than a real Lisp function call, so ignore it.
555 (if (looking-at "[ \t]")
556 (error "Probably not a Lisp function call"))
557 (let (obj)
558 (setq obj (read (current-buffer)))
559 (and (symbolp obj) (fboundp obj) obj))))
560 (error nil)))
561 (set-syntax-table stab))))
562
563 (defvar symbol-file-load-history-loaded nil
564 "Non-nil means we have loaded the file `fns-VERSION.el' in `exec-directory'.
565 That file records the part of `load-history' for preloaded files,
566 which is cleared out before dumping to make Emacs smaller.")
567
568 (defun symbol-file (function)
569 "Return the input source from which FUNCTION was loaded.
570 The value is normally a string that was passed to `load':
571 either an absolute file name, or a library name
572 \(with no directory name and no `.el' or `.elc' at the end).
573 It can also be nil, if the definition is not associated with any file."
574 (unless symbol-file-load-history-loaded
575 (load (expand-file-name
576 ;; fns-XX.YY.ZZ.el does not work on DOS filesystem.
577 (if (eq system-type 'ms-dos)
578 "fns.el"
579 (format "fns-%s.el" emacs-version))
580 exec-directory)
581 ;; The file name fns-%s.el already has a .el extension.
582 nil nil t)
583 (setq symbol-file-load-history-loaded t))
584 (let ((files load-history)
585 file functions)
586 (while files
587 (if (memq function (cdr (car files)))
588 (setq file (car (car files)) files nil))
589 (setq files (cdr files)))
590 file))
591
592 (defun describe-function (function)
593 "Display the full documentation of FUNCTION (a symbol)."
594 (interactive
595 (let ((fn (function-called-at-point))
596 (enable-recursive-minibuffers t)
597 val)
598 (setq val (completing-read (if fn
599 (format "Describe function (default %s): " fn)
600 "Describe function: ")
601 obarray 'fboundp t nil nil (symbol-name fn)))
602 (list (if (equal val "")
603 fn (intern val)))))
604 (if function
605 (with-output-to-temp-buffer "*Help*"
606 (prin1 function)
607 ;; Use " is " instead of a colon so that
608 ;; it is easier to get out the function name using forward-sexp.
609 (princ " is ")
610 (describe-function-1 function nil (interactive-p))
611 (print-help-return-message)
612 (save-excursion
613 (set-buffer standard-output)
614 ;; Return the text we displayed.
615 (buffer-string)))
616 (message "You didn't specify a function")))
617
618 (defun describe-function-1 (function parens interactive-p)
619 (let* ((def (if (symbolp function)
620 (symbol-function function)
621 function))
622 file-name string need-close
623 (beg (if (commandp def) "an interactive " "a ")))
624 (setq string
625 (cond ((or (stringp def)
626 (vectorp def))
627 "a keyboard macro")
628 ((subrp def)
629 (concat beg "built-in function"))
630 ((byte-code-function-p def)
631 (concat beg "compiled Lisp function"))
632 ((symbolp def)
633 (while (symbolp (symbol-function def))
634 (setq def (symbol-function def)))
635 (format "an alias for `%s'" def))
636 ((eq (car-safe def) 'lambda)
637 (concat beg "Lisp function"))
638 ((eq (car-safe def) 'macro)
639 "a Lisp macro")
640 ((eq (car-safe def) 'mocklisp)
641 "a mocklisp function")
642 ((eq (car-safe def) 'autoload)
643 (setq file-name (nth 1 def))
644 (format "%s autoloaded %s"
645 (if (commandp def) "an interactive" "an")
646 (if (eq (nth 4 def) 'keymap) "keymap"
647 (if (nth 4 def) "Lisp macro" "Lisp function"))
648 ))
649 (t "")))
650 (when (and parens (not (equal string "")))
651 (setq need-close t)
652 (princ "("))
653 (princ string)
654 (with-current-buffer "*Help*"
655 (save-excursion
656 (save-match-data
657 (if (re-search-backward "alias for `\\([^`']+\\)'" nil t)
658 (help-xref-button 1 #'describe-function def)))))
659 (or file-name
660 (setq file-name (symbol-file function)))
661 (if file-name
662 (progn
663 (princ " in `")
664 ;; We used to add .el to the file name,
665 ;; but that's completely wrong when the user used load-file.
666 (princ file-name)
667 (princ "'")
668 ;; Make a hyperlink to the library.
669 (with-current-buffer "*Help*"
670 (save-excursion
671 (re-search-backward "`\\([^`']+\\)'" nil t)
672 (help-xref-button 1 #'(lambda (arg)
673 (let ((location
674 (find-function-noselect arg)))
675 (pop-to-buffer (car location))
676 (goto-char (cdr location))))
677 function)))))
678 (if need-close (princ ")"))
679 (princ ".")
680 (terpri)
681 ;; Handle symbols aliased to other symbols.
682 (setq def (indirect-function def))
683 ;; If definition is a macro, find the function inside it.
684 (if (eq (car-safe def) 'macro)
685 (setq def (cdr def)))
686 (let ((arglist (cond ((byte-code-function-p def)
687 (car (append def nil)))
688 ((eq (car-safe def) 'lambda)
689 (nth 1 def))
690 (t t))))
691 (if (listp arglist)
692 (progn
693 (princ (cons (if (symbolp function) function "anonymous")
694 (mapcar (lambda (arg)
695 (if (memq arg '(&optional &rest))
696 arg
697 (intern (upcase (symbol-name arg)))))
698 arglist)))
699 (terpri))))
700 (let ((doc (documentation function)))
701 (if doc
702 (progn (terpri)
703 (princ doc)
704 (help-setup-xref (list #'describe-function function) interactive-p))
705 (princ "not documented")))))
706
707 (defun variable-at-point ()
708 "Return the bound variable symbol found around point.
709 Return 0 if there is no such symbol."
710 (condition-case ()
711 (let ((stab (syntax-table)))
712 (unwind-protect
713 (save-excursion
714 (set-syntax-table emacs-lisp-mode-syntax-table)
715 (or (not (zerop (skip-syntax-backward "_w")))
716 (eq (char-syntax (following-char)) ?w)
717 (eq (char-syntax (following-char)) ?_)
718 (forward-sexp -1))
719 (skip-chars-forward "'")
720 (let ((obj (read (current-buffer))))
721 (or (and (symbolp obj) (boundp obj) obj)
722 0)))
723 (set-syntax-table stab)))
724 (error 0)))
725
726 (defun describe-variable (variable)
727 "Display the full documentation of VARIABLE (a symbol).
728 Returns the documentation as a string, also."
729 (interactive
730 (let ((v (variable-at-point))
731 (enable-recursive-minibuffers t)
732 val)
733 (setq val (completing-read (if (symbolp v)
734 (format "Describe variable (default %s): " v)
735 "Describe variable: ")
736 obarray 'boundp t nil nil
737 (if (symbolp v) (symbol-name v))))
738 (list (if (equal val "")
739 v (intern val)))))
740 (if (symbolp variable)
741 (let (valvoid)
742 (with-output-to-temp-buffer "*Help*"
743 (prin1 variable)
744 (if (not (boundp variable))
745 (progn
746 (princ " is void")
747 (terpri)
748 (setq valvoid t))
749 (princ "'s value is ")
750 (terpri)
751 (pp (symbol-value variable))
752 (terpri))
753 (if (local-variable-p variable)
754 (progn
755 (princ (format "Local in buffer %s; " (buffer-name)))
756 (if (not (default-boundp variable))
757 (princ "globally void")
758 (princ "global value is ")
759 (terpri)
760 (pp (default-value variable)))
761 (terpri)))
762 (terpri)
763 (save-current-buffer
764 (set-buffer standard-output)
765 (if (> (count-lines (point-min) (point-max)) 10)
766 (progn
767 (goto-char (point-min))
768 (if valvoid
769 (forward-line 1)
770 (forward-sexp 1)
771 (delete-region (point) (progn (end-of-line) (point)))
772 (insert "'s value is shown below.\n\n")
773 (save-excursion
774 (insert "\n\nValue:"))))))
775 (princ "Documentation:")
776 (terpri)
777 (let ((doc (documentation-property variable 'variable-documentation)))
778 (princ (or doc "not documented as a variable.")))
779 (help-setup-xref (list #'describe-variable variable) (interactive-p))
780
781 ;; Make a link to customize if this variable can be customized.
782 ;; Note, it is not reliable to test only for a custom-type property
783 ;; because those are only present after the var's definition
784 ;; has been loaded.
785 (if (or (get variable 'custom-type) ; after defcustom
786 (get variable 'custom-loads) ; from loaddefs.el
787 (get variable 'standard-value)) ; from cus-start.el
788 (let ((customize-label "customize"))
789 (terpri)
790 (terpri)
791 (princ (concat "You can " customize-label " this variable."))
792 (with-current-buffer "*Help*"
793 (save-excursion
794 (re-search-backward
795 (concat "\\(" customize-label "\\)") nil t)
796 (help-xref-button 1 #'(lambda (v)
797 (customize-variable v)) variable)
798 ))))
799 ;; Make a hyperlink to the library if appropriate. (Don't
800 ;; change the format of the buffer's initial line in case
801 ;; anything expects the current format.)
802 (let ((file-name (symbol-file variable)))
803 (when file-name
804 (princ "\n\nDefined in `")
805 (princ file-name)
806 (princ "'.")
807 (with-current-buffer "*Help*"
808 (save-excursion
809 (re-search-backward "`\\([^`']+\\)'" nil t)
810 (help-xref-button 1 (lambda (arg)
811 (let ((location
812 (find-variable-noselect arg)))
813 (pop-to-buffer (car location))
814 (goto-char (cdr location))))
815 variable)))))
816
817 (print-help-return-message)
818 (save-excursion
819 (set-buffer standard-output)
820 ;; Return the text we displayed.
821 (buffer-string))))
822 (message "You did not specify a variable")))
823
824 (defun describe-bindings (&optional prefix buffer)
825 "Show a list of all defined keys, and their definitions.
826 We put that list in a buffer, and display the buffer.
827
828 The optional argument PREFIX, if non-nil, should be a key sequence;
829 then we display only bindings that start with that prefix.
830 The optional argument BUFFER specifies which buffer's bindings
831 to display (default, the current buffer)."
832 (interactive "P")
833 (or buffer (setq buffer (current-buffer)))
834 (with-current-buffer buffer
835 (describe-bindings-internal nil prefix))
836 (with-current-buffer "*Help*"
837 (help-setup-xref (list #'describe-bindings prefix buffer)
838 (interactive-p))))
839
840 (defun where-is (definition &optional insert)
841 "Print message listing key sequences that invoke the command DEFINITION.
842 Argument is a command definition, usually a symbol with a function definition.
843 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
844 (interactive
845 (let ((fn (function-called-at-point))
846 (enable-recursive-minibuffers t)
847 val)
848 (setq val (completing-read (if fn
849 (format "Where is command (default %s): " fn)
850 "Where is command: ")
851 obarray 'fboundp t))
852 (list (if (equal val "")
853 fn (intern val))
854 current-prefix-arg)))
855 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
856 (keys1 (mapconcat 'key-description keys ", "))
857 (standard-output (if insert (current-buffer) t)))
858 (if insert
859 (if (> (length keys1) 0)
860 (princ (format "%s (%s)" keys1 definition))
861 (princ (format "M-x %s RET" definition)))
862 (if (> (length keys1) 0)
863 (princ (format "%s is on %s" definition keys1))
864 (princ (format "%s is not on any key" definition)))))
865 nil)
866
867 (defun locate-library (library &optional nosuffix path interactive-call)
868 "Show the precise file name of Emacs library LIBRARY.
869 This command searches the directories in `load-path' like `M-x load-library'
870 to find the file that `M-x load-library RET LIBRARY RET' would load.
871 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
872 to the specified name LIBRARY.
873
874 If the optional third arg PATH is specified, that list of directories
875 is used instead of `load-path'.
876
877 When called from a program, the file name is normaly returned as a
878 string. When run interactively, the argument INTERACTIVE-CALL is t,
879 and the file name is displayed in the echo area."
880 (interactive (list (read-string "Locate library: ")
881 nil nil
882 t))
883 (let (result)
884 (catch 'answer
885 (mapcar
886 (lambda (dir)
887 (mapcar
888 (lambda (suf)
889 (let ((try (expand-file-name (concat library suf) dir)))
890 (and (file-readable-p try)
891 (null (file-directory-p try))
892 (progn
893 (setq result try)
894 (throw 'answer try)))))
895 (if nosuffix
896 '("")
897 '(".elc" ".el" "")
898 ;;; load doesn't handle this yet.
899 ;;; (let ((basic '(".elc" ".el" ""))
900 ;;; (compressed '(".Z" ".gz" "")))
901 ;;; ;; If autocompression mode is on,
902 ;;; ;; consider all combinations of library suffixes
903 ;;; ;; and compression suffixes.
904 ;;; (if (rassq 'jka-compr-handler file-name-handler-alist)
905 ;;; (apply 'nconc
906 ;;; (mapcar (lambda (compelt)
907 ;;; (mapcar (lambda (baselt)
908 ;;; (concat baselt compelt))
909 ;;; basic))
910 ;;; compressed))
911 ;;; basic))
912 )))
913 (or path load-path)))
914 (and interactive-call
915 (if result
916 (message "Library is file %s" result)
917 (message "No library %s in search path" library)))
918 result))
919
920 \f
921 ;;; Grokking cross-reference information in doc strings and
922 ;;; hyperlinking it.
923
924 ;; This may have some scope for extension and the same or something
925 ;; similar should be done for widget doc strings, which currently use
926 ;; another mechanism.
927
928 (defcustom help-highlight-p t
929 "*If non-nil, `help-make-xrefs' highlight cross-references.
930 Under a window system it highlights them with face defined by
931 `help-highlight-face'. On a character terminal highlighted
932 references look like cross-references in info mode."
933 :group 'help
934 :version "20.3"
935 :type 'boolean)
936
937 (defcustom help-highlight-face 'underline
938 "Face used by `help-make-xrefs' to highlight cross-references.
939 Must be previously-defined."
940 :group 'help
941 :version "20.3"
942 :type 'face)
943
944 (defvar help-back-label "[back]"
945 "Label to use by `help-make-xrefs' for the go-back reference.")
946
947 (defvar help-xref-symbol-regexp
948 (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
949 "\\(function\\|command\\)\\|"
950 "\\(symbol\\)\\)\\s-+\\)?"
951 ;; Note starting with word-syntax character:
952 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")
953 "Regexp matching doc string references to symbols.
954
955 The words preceding the quoted symbol can be used in doc strings to
956 distinguish references to variables, functions and symbols.")
957
958 (defvar help-xref-info-regexp
959 "\\<[Ii]nfo[ \t\n]+node[ \t\n]+`\\([^']+\\)'"
960 "Regexp matching doc string references to an Info node.")
961
962 (defun help-setup-xref (item interactive-p)
963 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
964
965 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
966 buffer after following a reference. INTERACTIVE-P is non-nil if the
967 calling command was invoked interactively. In this case the stack of
968 items for help buffer \"back\" buttons is cleared."
969 (if interactive-p
970 (setq help-xref-stack nil))
971 (setq help-xref-stack-item item))
972
973 (defun help-make-xrefs (&optional buffer)
974 "Parse and hyperlink documentation cross-references in the given BUFFER.
975
976 Find cross-reference information in a buffer and, if
977 `help-highlight-p' is non-nil, highlight it with face defined by
978 `help-highlight-face'; activate such cross references for selection
979 with `help-follow'. Cross-references have the canonical form `...'
980 and the type of reference may be disambiguated by the preceding
981 word(s) used in `help-xref-symbol-regexp'.
982
983 A special reference `back' is made to return back through a stack of
984 help buffers. Variable `help-back-label' specifies the text for
985 that."
986 (interactive "b")
987 (save-excursion
988 (set-buffer (or buffer (current-buffer)))
989 (goto-char (point-min))
990 ;; Skip the header-type info, though it might be useful to parse
991 ;; it at some stage (e.g. "function in `library'").
992 (forward-paragraph)
993 (let ((old-modified (buffer-modified-p)))
994 (let ((stab (syntax-table))
995 (case-fold-search t)
996 (inhibit-read-only t))
997 (set-syntax-table emacs-lisp-mode-syntax-table)
998 ;; The following should probably be abstracted out.
999 (unwind-protect
1000 (progn
1001 ;; Info references
1002 (save-excursion
1003 (while (re-search-forward help-xref-info-regexp nil t)
1004 (let ((data (match-string 1)))
1005 (save-match-data
1006 (unless (string-match "^([^)]+)" data)
1007 (setq data (concat "(emacs)" data))))
1008 (help-xref-button 1 #'info data))))
1009 ;; Quoted symbols
1010 (save-excursion
1011 (while (re-search-forward help-xref-symbol-regexp nil t)
1012 (let* ((data (match-string 6))
1013 (sym (intern-soft data)))
1014 (if sym
1015 (cond
1016 ((match-string 3) ; `variable' &c
1017 (and (boundp sym) ; `variable' doesn't ensure
1018 ; it's actually bound
1019 (help-xref-button 6 #'describe-variable sym)))
1020 ((match-string 4) ; `function' &c
1021 (and (fboundp sym) ; similarly
1022 (help-xref-button 6 #'describe-function sym)))
1023 ((match-string 5)) ; nothing for symbol
1024 ((and (boundp sym) (fboundp sym))
1025 ;; We can't intuit whether to use the
1026 ;; variable or function doc -- supply both.
1027 (help-xref-button 6 #'help-xref-interned sym))
1028 ((boundp sym)
1029 (help-xref-button 6 #'describe-variable sym))
1030 ((fboundp sym)
1031 (help-xref-button 6 #'describe-function sym)))))))
1032 ;; An obvious case of a key substitution:
1033 (save-excursion
1034 (while (re-search-forward
1035 ;; Assume command name is only word characters
1036 ;; and dashes to get things like `use M-x foo.'.
1037 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|-\\)+\\)" nil t)
1038 (let ((sym (intern-soft (match-string 1))))
1039 (if (fboundp sym)
1040 (help-xref-button 1 #'describe-function sym)))))
1041 ;; Look for commands in whole keymap substitutions:
1042 (save-excursion
1043 ;; Make sure to find the first keymap.
1044 (goto-char (point-min))
1045 ;; Find a header and the column at which the command
1046 ;; name will be found.
1047 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
1048 nil t)
1049 (let ((col (- (match-end 1) (match-beginning 1))))
1050 (while
1051 ;; Ignore single blank lines in table, but not
1052 ;; double ones, which should terminate it.
1053 (and (not (looking-at "\n\n"))
1054 (progn
1055 (and (eolp) (forward-line))
1056 (end-of-line)
1057 (skip-chars-backward "^\t\n")
1058 (if (and (>= (current-column) col)
1059 (looking-at "\\(\\sw\\|-\\)+$"))
1060 (let ((sym (intern-soft (match-string 0))))
1061 (if (fboundp sym)
1062 (help-xref-button
1063 0 #'describe-function sym))))
1064 (zerop (forward-line)))))))))
1065 (set-syntax-table stab))
1066 ;; Make a back-reference in this buffer if appropriate.
1067 (when help-xref-stack
1068 (goto-char (point-max))
1069 (save-excursion
1070 (insert "\n\n" help-back-label))
1071 ;; Just to provide the match data:
1072 (looking-at (concat "\n\n\\(" (regexp-quote help-back-label) "\\)"))
1073 (help-xref-button 1 #'help-xref-go-back (current-buffer))))
1074 ;; View mode steals RET from us.
1075 (set (make-local-variable 'minor-mode-overriding-map-alist)
1076 (list (cons 'view-mode
1077 (let ((map (make-sparse-keymap)))
1078 (set-keymap-parent map view-mode-map)
1079 (define-key map "\r" 'help-follow)
1080 map))))
1081 (set-buffer-modified-p old-modified))))
1082
1083 (defun help-xref-button (match-number function data)
1084 "Make a hyperlink for cross-reference text previously matched.
1085
1086 MATCH-NUMBER is the subexpression of interest in the last matched
1087 regexp. FUNCTION is a function to invoke when the button is
1088 activated, applied to DATA. DATA may be a single value or a list.
1089 See `help-make-xrefs'."
1090 ;; Don't mung properties we've added specially in some instances.
1091 (unless (get-text-property (match-beginning match-number) 'help-xref)
1092 (add-text-properties (match-beginning match-number)
1093 (match-end match-number)
1094 (list 'mouse-face 'highlight
1095 'help-xref (cons function
1096 (if (listp data)
1097 data
1098 (list data)))))
1099 (if help-highlight-p
1100 (put-text-property (match-beginning match-number)
1101 (match-end match-number)
1102 'face help-highlight-face))))
1103
1104 \f
1105 ;; Additional functions for (re-)creating types of help buffers.
1106 (defun help-xref-interned (symbol)
1107 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
1108
1109 Both variable and function documentation are extracted into a single
1110 help buffer."
1111 (let ((fdoc (describe-function symbol)))
1112 (describe-variable symbol)
1113 ;; We now have a help buffer on the variable. Insert the function
1114 ;; text before it.
1115 (with-current-buffer "*Help*"
1116 (goto-char (point-min))
1117 (let ((inhibit-read-only t))
1118 (insert fdoc "\n\n" (symbol-name symbol) " is also a variable.\n\n"))
1119 (help-setup-xref (list #'help-xref-interned symbol) nil))))
1120
1121 (defun help-xref-mode (buffer)
1122 "Do a `describe-mode' for the specified BUFFER."
1123 (save-excursion
1124 (set-buffer buffer)
1125 (describe-mode)))
1126 \f
1127 ;;; Navigation/hyperlinking with xrefs
1128
1129 (defun help-follow-mouse (click)
1130 "Follow the cross-reference that you click on."
1131 (interactive "e")
1132 (let* ((start (event-start click))
1133 (window (car start))
1134 (pos (car (cdr start))))
1135 (with-current-buffer (window-buffer window)
1136 (help-follow pos))))
1137
1138 (defun help-xref-go-back (buffer)
1139 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
1140 (let (item position method args)
1141 (with-current-buffer buffer
1142 (when help-xref-stack
1143 (setq help-xref-stack (cdr help-xref-stack)) ; due to help-follow
1144 (setq item (car help-xref-stack)
1145 position (car item)
1146 method (cadr item)
1147 args (cddr item))
1148 (setq help-xref-stack (cdr help-xref-stack))))
1149 (apply method args)
1150 (goto-char position)))
1151
1152 (defun help-go-back ()
1153 "Invoke the [back] button (if any) in the Help mode buffer."
1154 (interactive)
1155 (help-follow (1- (point-max))))
1156
1157 (defun help-follow (&optional pos)
1158 "Follow cross-reference at POS, defaulting to point.
1159
1160 For the cross-reference format, see `help-make-xrefs'."
1161 (interactive "d")
1162 (let* ((help-data (or (and (not (= pos (point-max)))
1163 (get-text-property pos 'help-xref))
1164 (and (not (= pos (point-min)))
1165 (get-text-property (1- pos) 'help-xref))))
1166 (method (car help-data))
1167 (args (cdr help-data)))
1168 (setq help-xref-stack (cons (cons (point) help-xref-stack-item)
1169 help-xref-stack))
1170 (setq help-xref-stack-item nil)
1171 (when help-data
1172 ;; There is a reference at point. Follow it.
1173 (apply method args))))
1174
1175 ;; For tabbing through buffer.
1176 (defun help-next-ref ()
1177 "Find the next help cross-reference in the buffer."
1178 (interactive)
1179 (let (pos)
1180 (while (not pos)
1181 (if (get-text-property (point) 'help-xref) ; move off reference
1182 (goto-char (or (next-single-property-change (point) 'help-xref)
1183 (point))))
1184 (cond ((setq pos (next-single-property-change (point) 'help-xref))
1185 (if pos (goto-char pos)))
1186 ((bobp)
1187 (message "No cross references in the buffer.")
1188 (setq pos t))
1189 (t ; be circular
1190 (goto-char (point-min)))))))
1191
1192 (defun help-previous-ref ()
1193 "Find the previous help cross-reference in the buffer."
1194 (interactive)
1195 (let (pos)
1196 (while (not pos)
1197 (if (get-text-property (point) 'help-xref) ; move off reference
1198 (goto-char (or (previous-single-property-change (point) 'help-xref)
1199 (point))))
1200 (cond ((setq pos (previous-single-property-change (point) 'help-xref))
1201 (if pos (goto-char pos)))
1202 ((bobp)
1203 (message "No cross references in the buffer.")
1204 (setq pos t))
1205 (t ; be circular
1206 (goto-char (point-max)))))))
1207
1208 \f
1209 ;;; Automatic resizing of temporary buffers.
1210
1211 (defcustom temp-buffer-resize-mode nil
1212 "Non-nil means resize windows displaying temporary buffers.
1213 This makes the window the right height for its contents, but never
1214 more than `temp-buffer-max-height' nor less than `window-min-height'.
1215 This applies to `help', `apropos' and `completion' buffers, and some others.
1216
1217 Setting this variable directly does not take effect;
1218 use either \\[customize] or the function `temp-buffer-resize-mode'."
1219 :get (lambda (symbol)
1220 (and (memq 'resize-temp-buffer-window temp-buffer-show-hook) t))
1221 :set (lambda (symbol value)
1222 (temp-buffer-resize-mode (if value 1 -1)))
1223 :initialize 'custom-initialize-default
1224 :type 'boolean
1225 :group 'help
1226 :version "20.4")
1227
1228 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
1229 "*Maximum height of a window displaying a temporary buffer.
1230 This is the maximum height (in text lines) which `resize-temp-buffer-window'
1231 will give to a window displaying a temporary buffer.
1232 It can also be a function which will be called with the object corresponding
1233 to the buffer to be displayed as argument and should return an integer
1234 positive number."
1235 :type '(choice integer function)
1236 :group 'help
1237 :version "20.4")
1238
1239 (defun temp-buffer-resize-mode (arg)
1240 "Toggle the mode which that makes windows smaller for temporary buffers.
1241 With prefix argument ARG, turn the resizing of windows displaying temporary
1242 buffers on if ARG is positive or off otherwise.
1243 See the documentation of the variable `temp-buffer-resize-mode' for
1244 more information."
1245 (interactive "P")
1246 (let ((turn-it-on
1247 (if (null arg)
1248 (not (memq 'resize-temp-buffer-window temp-buffer-show-hook))
1249 (> (prefix-numeric-value arg) 0))))
1250 (if turn-it-on
1251 (progn
1252 ;; `help-mode-maybe' may add a `back' button and thus increase the
1253 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1254 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1255 (setq temp-buffer-resize-mode t))
1256 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)
1257 (setq temp-buffer-resize-mode nil))))
1258
1259 (defun resize-temp-buffer-window ()
1260 "Resize the current window to fit its contents.
1261 Will not make it higher than `temp-buffer-max-height' nor smaller than
1262 `window-min-height'. Do nothing if it is the only window on its frame, if it
1263 is not as wide as the frame or if some of the window's contents are scrolled
1264 out of view."
1265 (unless (or (one-window-p 'nomini)
1266 (not (pos-visible-in-window-p (point-min)))
1267 (/= (frame-width) (window-width)))
1268 (let* ((max-height (if (functionp temp-buffer-max-height)
1269 (funcall temp-buffer-max-height (current-buffer))
1270 temp-buffer-max-height))
1271 (win-height (1- (window-height)))
1272 (min-height (1- window-min-height))
1273 (text-height (window-buffer-height(selected-window)))
1274 (new-height (max (min text-height max-height) min-height)))
1275 (enlarge-window (- new-height win-height)))))
1276
1277 ;;; help.el ends here