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