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