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