]> code.delx.au - gnu-emacs/blob - lisp/help.el
(resize-temp-buffer-window): Use count-screen-lines.
[gnu-emacs] / lisp / help.el
1 ;;; help.el --- help commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: help, internal
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This code implements GNU Emacs' on-line help system, the one invoked by
28 ;; `M-x help-for-help'.
29
30 ;;; Code:
31
32 ;; Get the macro make-help-screen when this is compiled,
33 ;; or run interpreted, but not when the compiled code is loaded.
34 (eval-when-compile (require 'help-macro))
35 (eval-when-compile (require 'view))
36
37 (defvar help-map (make-sparse-keymap)
38 "Keymap for characters following the Help key.")
39
40 (defvar help-mode-map (make-sparse-keymap)
41 "Keymap for help mode.")
42
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)
47
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)
52
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)
58
59 (define-key help-map "b" 'describe-bindings)
60
61 (define-key help-map "c" 'describe-key-briefly)
62 (define-key help-map "k" 'describe-key)
63
64 (define-key help-map "d" 'describe-function)
65 (define-key help-map "f" 'describe-function)
66
67 (define-key help-map "F" 'view-emacs-FAQ)
68
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)
74
75 (define-key help-map "l" 'view-lossage)
76
77 (define-key help-map "m" 'describe-mode)
78
79 (define-key help-map "\C-n" 'view-emacs-news)
80 (define-key help-map "n" 'view-emacs-news)
81
82 (define-key help-map "p" 'finder-by-keyword)
83 (autoload 'finder-by-keyword "finder"
84 "Find packages matching a given keyword." t)
85
86 (define-key help-map "P" 'view-emacs-problems)
87
88 (define-key help-map "s" 'describe-syntax)
89
90 (define-key help-map "t" 'help-with-tutorial)
91
92 (define-key help-map "w" 'where-is)
93
94 (define-key help-map "v" 'describe-variable)
95
96 (define-key help-map "q" 'help-quit)
97
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)
106
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)
113
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)
118
119 (setq-default help-xref-stack nil help-xref-stack-item nil)
120
121 (defcustom help-mode-hook nil
122 "Hook run by `help-mode'."
123 :type 'hook
124 :group 'help)
125
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))
144
145 (defun help-mode-setup ()
146 (help-mode)
147 (setq buffer-read-only nil))
148
149 (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
150
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))))
159
160 (add-hook 'temp-buffer-show-hook 'help-mode-finish)
161
162 (defun help-quit ()
163 "Just exit from the Help command's command loop."
164 (interactive)
165 nil)
166
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))))
206
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))
219
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)))))))))
252
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.")
259
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.")
282 (t
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)
291 "")
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))
296 nil
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."))))))))
304
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)))))))
334
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.
339
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)))
380
381 ;; So keyboard macro definitions are documented correctly
382 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
383
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)))
389
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)))
396
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)))
403
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)))
411
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)
430
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))))
439
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)"))
445
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)))
450
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)))
472
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.)
478
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.
514
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)
523
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))))
556
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.")
561
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))
585
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")))
611
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"))
642 ))
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 "mouse-2, RET: describe this function")))))
666 (or file-name
667 (setq file-name (symbol-file function)))
668 (if file-name
669 (progn
670 (princ " in `")
671 ;; We used to add .el to the file name,
672 ;; but that's completely wrong when the user used load-file.
673 (princ file-name)
674 (princ "'")
675 ;; Make a hyperlink to the library.
676 (with-current-buffer "*Help*"
677 (save-excursion
678 (re-search-backward "`\\([^`']+\\)'" nil t)
679 (help-xref-button 1 #'(lambda (arg)
680 (let ((location
681 (find-function-noselect arg)))
682 (pop-to-buffer (car location))
683 (goto-char (cdr location))))
684 function
685 "mouse-2, RET: find function's definition")))))
686 (if need-close (princ ")"))
687 (princ ".")
688 (terpri)
689 ;; Handle symbols aliased to other symbols.
690 (setq def (indirect-function def))
691 ;; If definition is a macro, find the function inside it.
692 (if (eq (car-safe def) 'macro)
693 (setq def (cdr def)))
694 (let ((arglist (cond ((byte-code-function-p def)
695 (car (append def nil)))
696 ((eq (car-safe def) 'lambda)
697 (nth 1 def))
698 ((and (eq (car-safe def) 'autoload)
699 (not (eq (nth 4 def) 'keymap)))
700 (concat "[Arg list not available until "
701 "function definition is loaded.]"))
702 (t t))))
703 (cond ((listp arglist)
704 (princ (cons (if (symbolp function) function "anonymous")
705 (mapcar (lambda (arg)
706 (if (memq arg '(&optional &rest))
707 arg
708 (intern (upcase (symbol-name arg)))))
709 arglist)))
710 (terpri))
711 ((stringp arglist)
712 (princ arglist)
713 (terpri))))
714 (let ((doc (documentation function)))
715 (if doc
716 (progn (terpri)
717 (princ doc)
718 (with-current-buffer standard-output
719 (beginning-of-line)
720 ;; Builtins get the calling sequence at the end of
721 ;; the doc string. Move it to the same place as
722 ;; for other functions.
723 (when (looking-at (format "(%S[ )]" function))
724 (let ((start (point-marker)))
725 (goto-char (point-min))
726 (forward-paragraph)
727 (insert-buffer-substring (current-buffer) start)
728 (insert ?\n)
729 (delete-region (1- start) (point-max))
730 (goto-char (point-max)))))
731 (help-setup-xref (list #'describe-function function)
732 interactive-p))
733 (princ "not documented")))))
734
735 (defun variable-at-point ()
736 "Return the bound variable symbol found around point.
737 Return 0 if there is no such symbol."
738 (condition-case ()
739 (let ((stab (syntax-table)))
740 (unwind-protect
741 (save-excursion
742 (set-syntax-table emacs-lisp-mode-syntax-table)
743 (or (not (zerop (skip-syntax-backward "_w")))
744 (eq (char-syntax (following-char)) ?w)
745 (eq (char-syntax (following-char)) ?_)
746 (forward-sexp -1))
747 (skip-chars-forward "'")
748 (let ((obj (read (current-buffer))))
749 (or (and (symbolp obj) (boundp obj) obj)
750 0)))
751 (set-syntax-table stab)))
752 (error 0)))
753
754 (defun describe-variable (variable)
755 "Display the full documentation of VARIABLE (a symbol).
756 Returns the documentation as a string, also."
757 (interactive
758 (let ((v (variable-at-point))
759 (enable-recursive-minibuffers t)
760 val)
761 (setq val (completing-read (if (symbolp v)
762 (format "Describe variable (default %s): " v)
763 "Describe variable: ")
764 obarray 'boundp t nil nil
765 (if (symbolp v) (symbol-name v))))
766 (list (if (equal val "")
767 v (intern val)))))
768 (if (symbolp variable)
769 (let (valvoid)
770 (with-output-to-temp-buffer "*Help*"
771 (prin1 variable)
772 (if (not (boundp variable))
773 (progn
774 (princ " is void")
775 (terpri)
776 (setq valvoid t))
777 (princ "'s value is ")
778 (terpri)
779 (pp (symbol-value variable))
780 (terpri))
781 (if (local-variable-p variable)
782 (progn
783 (princ (format "Local in buffer %s; " (buffer-name)))
784 (if (not (default-boundp variable))
785 (princ "globally void")
786 (princ "global value is ")
787 (terpri)
788 (pp (default-value variable)))
789 (terpri)))
790 (terpri)
791 (save-current-buffer
792 (set-buffer standard-output)
793 (if (> (count-lines (point-min) (point-max)) 10)
794 (progn
795 (set-syntax-table emacs-lisp-mode-syntax-table)
796 (goto-char (point-min))
797 (if valvoid
798 (forward-line 1)
799 (forward-sexp 1)
800 (delete-region (point) (progn (end-of-line) (point)))
801 (insert "'s value is shown below.\n\n")
802 (save-excursion
803 (insert "\n\nValue:"))))))
804 (princ "Documentation:")
805 (terpri)
806 (let ((doc (documentation-property variable 'variable-documentation)))
807 (princ (or doc "not documented as a variable.")))
808 (help-setup-xref (list #'describe-variable variable) (interactive-p))
809
810 ;; Make a link to customize if this variable can be customized.
811 ;; Note, it is not reliable to test only for a custom-type property
812 ;; because those are only present after the var's definition
813 ;; has been loaded.
814 (if (or (get variable 'custom-type) ; after defcustom
815 (get variable 'custom-loads) ; from loaddefs.el
816 (get variable 'standard-value)) ; from cus-start.el
817 (let ((customize-label "customize"))
818 (terpri)
819 (terpri)
820 (princ (concat "You can " customize-label " this variable."))
821 (with-current-buffer "*Help*"
822 (save-excursion
823 (re-search-backward
824 (concat "\\(" customize-label "\\)") nil t)
825 (help-xref-button 1 #'(lambda (v)
826 (customize-variable v))
827 variable
828 "mouse-2, RET: customize variable")
829 ))))
830 ;; Make a hyperlink to the library if appropriate. (Don't
831 ;; change the format of the buffer's initial line in case
832 ;; anything expects the current format.)
833 (let ((file-name (symbol-file variable)))
834 (when file-name
835 (princ "\n\nDefined in `")
836 (princ file-name)
837 (princ "'.")
838 (with-current-buffer "*Help*"
839 (save-excursion
840 (re-search-backward "`\\([^`']+\\)'" nil t)
841 (help-xref-button
842 1 (lambda (arg)
843 (let ((location
844 (find-variable-noselect arg)))
845 (pop-to-buffer (car location))
846 (goto-char (cdr location))))
847 variable "mouse-2, RET: find variable's definition")))))
848
849 (print-help-return-message)
850 (save-excursion
851 (set-buffer standard-output)
852 ;; Return the text we displayed.
853 (buffer-string))))
854 (message "You did not specify a variable")))
855
856 (defun describe-bindings (&optional prefix buffer)
857 "Show a list of all defined keys, and their definitions.
858 We put that list in a buffer, and display the buffer.
859
860 The optional argument PREFIX, if non-nil, should be a key sequence;
861 then we display only bindings that start with that prefix.
862 The optional argument BUFFER specifies which buffer's bindings
863 to display (default, the current buffer)."
864 (interactive "P")
865 (or buffer (setq buffer (current-buffer)))
866 (with-current-buffer buffer
867 (describe-bindings-internal nil prefix))
868 (with-current-buffer "*Help*"
869 (help-setup-xref (list #'describe-bindings prefix buffer)
870 (interactive-p))))
871
872 (defun where-is (definition &optional insert)
873 "Print message listing key sequences that invoke the command DEFINITION.
874 Argument is a command definition, usually a symbol with a function definition.
875 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
876 (interactive
877 (let ((fn (function-called-at-point))
878 (enable-recursive-minibuffers t)
879 val)
880 (setq val (completing-read (if fn
881 (format "Where is command (default %s): " fn)
882 "Where is command: ")
883 obarray 'commandp t))
884 (list (if (equal val "")
885 fn (intern val))
886 current-prefix-arg)))
887 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
888 (keys1 (mapconcat 'key-description keys ", "))
889 (standard-output (if insert (current-buffer) t)))
890 (if insert
891 (if (> (length keys1) 0)
892 (princ (format "%s (%s)" keys1 definition))
893 (princ (format "M-x %s RET" definition)))
894 (if (> (length keys1) 0)
895 (princ (format "%s is on %s" definition keys1))
896 (princ (format "%s is not on any key" definition)))))
897 nil)
898
899 (defun locate-library (library &optional nosuffix path interactive-call)
900 "Show the precise file name of Emacs library LIBRARY.
901 This command searches the directories in `load-path' like `M-x load-library'
902 to find the file that `M-x load-library RET LIBRARY RET' would load.
903 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
904 to the specified name LIBRARY.
905
906 If the optional third arg PATH is specified, that list of directories
907 is used instead of `load-path'.
908
909 When called from a program, the file name is normaly returned as a
910 string. When run interactively, the argument INTERACTIVE-CALL is t,
911 and the file name is displayed in the echo area."
912 (interactive (list (read-string "Locate library: ")
913 nil nil
914 t))
915 (let (result)
916 (catch 'answer
917 (mapcar
918 (lambda (dir)
919 (mapcar
920 (lambda (suf)
921 (let ((try (expand-file-name (concat library suf) dir)))
922 (and (file-readable-p try)
923 (null (file-directory-p try))
924 (progn
925 (setq result try)
926 (throw 'answer try)))))
927 (if nosuffix
928 '("")
929 '(".elc" ".el" "")
930 ;;; load doesn't handle this yet.
931 ;;; (let ((basic '(".elc" ".el" ""))
932 ;;; (compressed '(".Z" ".gz" "")))
933 ;;; ;; If autocompression mode is on,
934 ;;; ;; consider all combinations of library suffixes
935 ;;; ;; and compression suffixes.
936 ;;; (if (rassq 'jka-compr-handler file-name-handler-alist)
937 ;;; (apply 'nconc
938 ;;; (mapcar (lambda (compelt)
939 ;;; (mapcar (lambda (baselt)
940 ;;; (concat baselt compelt))
941 ;;; basic))
942 ;;; compressed))
943 ;;; basic))
944 )))
945 (or path load-path)))
946 (and interactive-call
947 (if result
948 (message "Library is file %s" result)
949 (message "No library %s in search path" library)))
950 result))
951
952 \f
953 ;;; Grokking cross-reference information in doc strings and
954 ;;; hyperlinking it.
955
956 ;; This may have some scope for extension and the same or something
957 ;; similar should be done for widget doc strings, which currently use
958 ;; another mechanism.
959
960 (defcustom help-highlight-p t
961 "*If non-nil, `help-make-xrefs' highlight cross-references.
962 Under a window system it highlights them with face defined by
963 `help-highlight-face'."
964 :group 'help
965 :version "20.3"
966 :type 'boolean)
967
968 (defcustom help-highlight-face 'underline
969 "Face used by `help-make-xrefs' to highlight cross-references.
970 Must be previously-defined."
971 :group 'help
972 :version "20.3"
973 :type 'face)
974
975 (defvar help-back-label (purecopy "[back]")
976 "Label to use by `help-make-xrefs' for the go-back reference.")
977
978 (defconst help-xref-symbol-regexp
979 (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
980 "\\(function\\|command\\)\\|"
981 "\\(symbol\\)\\)\\s-+\\)?"
982 ;; Note starting with word-syntax character:
983 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'"))
984 "Regexp matching doc string references to symbols.
985
986 The words preceding the quoted symbol can be used in doc strings to
987 distinguish references to variables, functions and symbols.")
988
989 (defconst help-xref-info-regexp
990 (purecopy "\\<[Ii]nfo[ \t\n]+node[ \t\n]+`\\([^']+\\)'")
991 "Regexp matching doc string references to an Info node.")
992
993 (defun help-setup-xref (item interactive-p)
994 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
995
996 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
997 buffer after following a reference. INTERACTIVE-P is non-nil if the
998 calling command was invoked interactively. In this case the stack of
999 items for help buffer \"back\" buttons is cleared."
1000 (if interactive-p
1001 (setq help-xref-stack nil))
1002 (setq help-xref-stack-item item))
1003
1004 (defun help-make-xrefs (&optional buffer)
1005 "Parse and hyperlink documentation cross-references in the given BUFFER.
1006
1007 Find cross-reference information in a buffer and, if
1008 `help-highlight-p' is non-nil, highlight it with face defined by
1009 `help-highlight-face'; activate such cross references for selection
1010 with `help-follow'. Cross-references have the canonical form `...'
1011 and the type of reference may be disambiguated by the preceding
1012 word(s) used in `help-xref-symbol-regexp'.
1013
1014 A special reference `back' is made to return back through a stack of
1015 help buffers. Variable `help-back-label' specifies the text for
1016 that."
1017 (interactive "b")
1018 (save-excursion
1019 (set-buffer (or buffer (current-buffer)))
1020 (goto-char (point-min))
1021 ;; Skip the header-type info, though it might be useful to parse
1022 ;; it at some stage (e.g. "function in `library'").
1023 (forward-paragraph)
1024 (let ((old-modified (buffer-modified-p)))
1025 (let ((stab (syntax-table))
1026 (case-fold-search t)
1027 (inhibit-read-only t))
1028 (set-syntax-table emacs-lisp-mode-syntax-table)
1029 ;; The following should probably be abstracted out.
1030 (unwind-protect
1031 (progn
1032 ;; Info references
1033 (save-excursion
1034 (while (re-search-forward help-xref-info-regexp nil t)
1035 (let ((data (match-string 1)))
1036 (save-match-data
1037 (unless (string-match "^([^)]+)" data)
1038 (setq data (concat "(emacs)" data))))
1039 (help-xref-button 1 #'info data
1040 "mouse-2, RET: read this Info node"))))
1041 ;; Quoted symbols
1042 (save-excursion
1043 (while (re-search-forward help-xref-symbol-regexp nil t)
1044 (let* ((data (match-string 6))
1045 (sym (intern-soft data)))
1046 (if sym
1047 (cond
1048 ((match-string 3) ; `variable' &c
1049 (and (boundp sym) ; `variable' doesn't ensure
1050 ; it's actually bound
1051 (help-xref-button
1052 6 #'describe-variable sym
1053 "mouse-2, RET: describe this variable")))
1054 ((match-string 4) ; `function' &c
1055 (and (fboundp sym) ; similarly
1056 (help-xref-button
1057 6 #'describe-function sym
1058 "mouse-2, RET: describe this function")))
1059 ((match-string 5)) ; nothing for symbol
1060 ((and (boundp sym) (fboundp sym))
1061 ;; We can't intuit whether to use the
1062 ;; variable or function doc -- supply both.
1063 (help-xref-button
1064 6 #'help-xref-interned sym
1065 "mouse-2, RET: describe this symbol"))
1066 ((boundp sym)
1067 (help-xref-button
1068 6 #'describe-variable sym
1069 "mouse-2, RET: describe this variable"))
1070 ((fboundp sym)
1071 (help-xref-button
1072 6 #'describe-function sym
1073 "mouse-2, RET: describe this function")))))))
1074 ;; An obvious case of a key substitution:
1075 (save-excursion
1076 (while (re-search-forward
1077 ;; Assume command name is only word characters
1078 ;; and dashes to get things like `use M-x foo.'.
1079 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|-\\)+\\)" nil t)
1080 (let ((sym (intern-soft (match-string 1))))
1081 (if (fboundp sym)
1082 (help-xref-button
1083 1 #'describe-function sym
1084 "mouse-2, RET: describe this command")))))
1085 ;; Look for commands in whole keymap substitutions:
1086 (save-excursion
1087 ;; Make sure to find the first keymap.
1088 (goto-char (point-min))
1089 ;; Find a header and the column at which the command
1090 ;; name will be found.
1091 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
1092 nil t)
1093 (let ((col (- (match-end 1) (match-beginning 1))))
1094 (while
1095 ;; Ignore single blank lines in table, but not
1096 ;; double ones, which should terminate it.
1097 (and (not (looking-at "\n\\s-*\n"))
1098 (progn
1099 (and (eolp) (forward-line))
1100 (end-of-line)
1101 (skip-chars-backward "^\t\n")
1102 (if (and (>= (current-column) col)
1103 (looking-at "\\(\\sw\\|-\\)+$"))
1104 (let ((sym (intern-soft (match-string 0))))
1105 (if (fboundp sym)
1106 (help-xref-button
1107 0 #'describe-function sym
1108 "mouse-2, RET: describe this function"))))
1109 (zerop (forward-line)))))))))
1110 (set-syntax-table stab))
1111 ;; Make a back-reference in this buffer if appropriate.
1112 (when help-xref-stack
1113 (goto-char (point-max))
1114 (save-excursion
1115 (insert "\n\n" help-back-label))
1116 ;; Just to provide the match data:
1117 (looking-at (concat "\n\n\\(" (regexp-quote help-back-label) "\\)"))
1118 (help-xref-button 1 #'help-xref-go-back (current-buffer))))
1119 ;; View mode steals RET from us.
1120 (set (make-local-variable 'minor-mode-overriding-map-alist)
1121 (list (cons 'view-mode
1122 (let ((map (make-sparse-keymap)))
1123 (set-keymap-parent map view-mode-map)
1124 (define-key map "\r" 'help-follow)
1125 map))))
1126 (set-buffer-modified-p old-modified))))
1127
1128 (defun help-xref-button (match-number function data &optional help-echo)
1129 "Make a hyperlink for cross-reference text previously matched.
1130
1131 MATCH-NUMBER is the subexpression of interest in the last matched
1132 regexp. FUNCTION is a function to invoke when the button is
1133 activated, applied to DATA. DATA may be a single value or a list.
1134 See `help-make-xrefs'.
1135 If optional arg HELP-ECHO is supplied, it is used as a help string."
1136 ;; Don't mung properties we've added specially in some instances.
1137 (unless (get-text-property (match-beginning match-number) 'help-xref)
1138 (add-text-properties (match-beginning match-number)
1139 (match-end match-number)
1140 (list 'mouse-face 'highlight
1141 'help-xref (cons function
1142 (if (listp data)
1143 data
1144 (list data)))))
1145 (if help-echo
1146 (put-text-property (match-beginning match-number)
1147 (match-end match-number)
1148 'help-echo help-echo))
1149 (if help-highlight-p
1150 (put-text-property (match-beginning match-number)
1151 (match-end match-number)
1152 'face help-highlight-face))))
1153
1154 \f
1155 ;; Additional functions for (re-)creating types of help buffers.
1156 (defun help-xref-interned (symbol)
1157 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
1158
1159 Both variable and function documentation are extracted into a single
1160 help buffer."
1161 (let ((fdoc (when (fboundp symbol) (describe-function symbol))))
1162 (when (or (boundp symbol) (not fdoc))
1163 (describe-variable symbol)
1164 ;; We now have a help buffer on the variable. Insert the function
1165 ;; text before it.
1166 (when fdoc
1167 (with-current-buffer "*Help*"
1168 (goto-char (point-min))
1169 (let ((inhibit-read-only t))
1170 (insert fdoc "\n\n" (symbol-name symbol) " is also a variable.\n\n"))
1171 (help-setup-xref (list #'help-xref-interned symbol) nil))))))
1172
1173 (defun help-xref-mode (buffer)
1174 "Do a `describe-mode' for the specified BUFFER."
1175 (save-excursion
1176 (set-buffer buffer)
1177 (describe-mode)))
1178 \f
1179 ;;; Navigation/hyperlinking with xrefs
1180
1181 (defun help-follow-mouse (click)
1182 "Follow the cross-reference that you click on."
1183 (interactive "e")
1184 (let* ((start (event-start click))
1185 (window (car start))
1186 (pos (car (cdr start))))
1187 (with-current-buffer (window-buffer window)
1188 (help-follow pos))))
1189
1190 (defun help-xref-go-back (buffer)
1191 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
1192 (let (item position method args)
1193 (with-current-buffer buffer
1194 (when help-xref-stack
1195 (setq help-xref-stack (cdr help-xref-stack)) ; due to help-follow
1196 (setq item (car help-xref-stack)
1197 position (car item)
1198 method (cadr item)
1199 args (cddr item))
1200 (setq help-xref-stack (cdr help-xref-stack))))
1201 (apply method args)
1202 ;; We're not in the right buffer to do this, and we don't actually
1203 ;; know which we should be in.
1204 ;;(goto-char position)
1205 ))
1206
1207 (defun help-go-back ()
1208 "Invoke the [back] button (if any) in the Help mode buffer."
1209 (interactive)
1210 (help-follow (1- (point-max))))
1211
1212 (defun help-follow (&optional pos)
1213 "Follow cross-reference at POS, defaulting to point.
1214
1215 For the cross-reference format, see `help-make-xrefs'."
1216 (interactive "d")
1217 (unless pos
1218 (setq pos (point)))
1219 (let* ((help-data
1220 (or (and (not (= pos (point-max)))
1221 (get-text-property pos 'help-xref))
1222 (and (not (= pos (point-min)))
1223 (get-text-property (1- pos) 'help-xref))
1224 ;; check if the symbol under point is a function or variable
1225 (let ((sym
1226 (intern
1227 (save-excursion
1228 (goto-char pos) (skip-syntax-backward "w_")
1229 (buffer-substring (point)
1230 (progn (skip-syntax-forward "w_")
1231 (point)))))))
1232 (when (or (boundp sym) (fboundp sym))
1233 (list #'help-xref-interned sym)))))
1234 (method (car help-data))
1235 (args (cdr help-data)))
1236 (when help-data
1237 (setq help-xref-stack (cons (cons (point) help-xref-stack-item)
1238 help-xref-stack))
1239 (setq help-xref-stack-item nil)
1240 ;; There is a reference at point. Follow it.
1241 (apply method args))))
1242
1243 ;; For tabbing through buffer.
1244 (defun help-next-ref ()
1245 "Find the next help cross-reference in the buffer."
1246 (interactive)
1247 (let (pos)
1248 (while (not pos)
1249 (if (get-text-property (point) 'help-xref) ; move off reference
1250 (goto-char (or (next-single-property-change (point) 'help-xref)
1251 (point))))
1252 (cond ((setq pos (next-single-property-change (point) 'help-xref))
1253 (if pos (goto-char pos)))
1254 ((bobp)
1255 (message "No cross references in the buffer.")
1256 (setq pos t))
1257 (t ; be circular
1258 (goto-char (point-min)))))))
1259
1260 (defun help-previous-ref ()
1261 "Find the previous help cross-reference in the buffer."
1262 (interactive)
1263 (let (pos)
1264 (while (not pos)
1265 (if (get-text-property (point) 'help-xref) ; move off reference
1266 (goto-char (or (previous-single-property-change (point) 'help-xref)
1267 (point))))
1268 (cond ((setq pos (previous-single-property-change (point) 'help-xref))
1269 (if pos (goto-char pos)))
1270 ((bobp)
1271 (message "No cross references in the buffer.")
1272 (setq pos t))
1273 (t ; be circular
1274 (goto-char (point-max)))))))
1275
1276 \f
1277 ;;; Automatic resizing of temporary buffers.
1278
1279 (defcustom temp-buffer-resize-mode nil
1280 "Non-nil means resize windows displaying temporary buffers.
1281 This makes the window the right height for its contents, but never
1282 more than `temp-buffer-max-height' nor less than `window-min-height'.
1283 This applies to `help', `apropos' and `completion' buffers, and some others.
1284
1285 Setting this variable directly does not take effect;
1286 use either \\[customize] or the function `temp-buffer-resize-mode'."
1287 :get (lambda (symbol)
1288 (and (memq 'resize-temp-buffer-window temp-buffer-show-hook) t))
1289 :set (lambda (symbol value)
1290 (temp-buffer-resize-mode (if value 1 -1)))
1291 :initialize 'custom-initialize-default
1292 :type 'boolean
1293 :group 'help
1294 :version "20.4")
1295
1296 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
1297 "*Maximum height of a window displaying a temporary buffer.
1298 This is the maximum height (in text lines) which `resize-temp-buffer-window'
1299 will give to a window displaying a temporary buffer.
1300 It can also be a function which will be called with the object corresponding
1301 to the buffer to be displayed as argument and should return an integer
1302 positive number."
1303 :type '(choice integer function)
1304 :group 'help
1305 :version "20.4")
1306
1307 (defun temp-buffer-resize-mode (arg)
1308 "Toggle the mode which that makes windows smaller for temporary buffers.
1309 With prefix argument ARG, turn the resizing of windows displaying temporary
1310 buffers on if ARG is positive or off otherwise.
1311 See the documentation of the variable `temp-buffer-resize-mode' for
1312 more information."
1313 (interactive "P")
1314 (let ((turn-it-on
1315 (if (null arg)
1316 (not (memq 'resize-temp-buffer-window temp-buffer-show-hook))
1317 (> (prefix-numeric-value arg) 0))))
1318 (if turn-it-on
1319 (progn
1320 ;; `help-mode-maybe' may add a `back' button and thus increase the
1321 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1322 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1323 (setq temp-buffer-resize-mode t))
1324 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)
1325 (setq temp-buffer-resize-mode nil))))
1326
1327 (defun resize-temp-buffer-window ()
1328 "Resize the current window to fit its contents.
1329 Will not make it higher than `temp-buffer-max-height' nor smaller than
1330 `window-min-height'. Do nothing if it is the only window on its frame, if it
1331 is not as wide as the frame or if some of the window's contents are scrolled
1332 out of view."
1333 (unless (or (one-window-p 'nomini)
1334 (not (pos-visible-in-window-p (point-min)))
1335 (/= (frame-width) (window-width)))
1336 (let* ((max-height (if (functionp temp-buffer-max-height)
1337 (funcall temp-buffer-max-height (current-buffer))
1338 temp-buffer-max-height))
1339 (win-height (1- (window-height)))
1340 (min-height (1- window-min-height))
1341 (text-height (count-screen-lines))
1342 (new-height (max (min text-height max-height) min-height)))
1343 (enlarge-window (- new-height win-height)))))
1344
1345 ;; `help-manyarg-func-alist' is defined primitively (in doc.c).
1346 ;; New primitives with `MANY' or `UNEVALLED' arglists should be added
1347 ;; to this alist.
1348 ;; The parens and function name are redundant, but it's messy to add
1349 ;; them in `documentation'.
1350 (defconst help-manyarg-func-alist
1351 (purecopy
1352 '((list . "(list &rest OBJECTS)")
1353 (vector . "(vector &rest OBJECTS)")
1354 (make-byte-code . "(make-byte-code &rest ELEMENTS)")
1355 (call-process
1356 . "(call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)")
1357 (string . "(string &rest CHARACTERS)")
1358 (+ . "(+ &rest NUMBERS-OR-MARKERS)")
1359 (- . "(- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS)")
1360 (* . "(* &rest NUMBERS-OR-MARKERS)")
1361 (/ . "(/ DIVIDEND DIVISOR &rest DIVISORS)")
1362 (max . "(max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS)")
1363 (min . "(min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS)")
1364 (logand . "(logand &rest INTS-OR-MARKERS)")
1365 (logior . "(logior &rest INTS-OR-MARKERS)")
1366 (logxor . "(logxor &rest INTS-OR-MARKERS)")
1367 (encode-time
1368 . "(encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE)")
1369 (insert . "(insert &rest ARGS)")
1370 (insert-before-markers . "(insert-before-markers &rest ARGS)")
1371 (message . "(message STRING &rest ARGUMENTS)")
1372 (message-box . "(message-box STRING &rest ARGUMENTS)")
1373 (message-or-box . "(message-or-box STRING &rest ARGUMENTS)")
1374 (propertize . "(propertize STRING &rest PROPERTIES)")
1375 (format . "(format STRING &rest OBJECTS)")
1376 (apply . "(apply FUNCTION &rest ARGUMENTS)")
1377 (run-hooks . "(run-hooks &rest HOOKS)")
1378 (run-hook-with-args . "(run-hook-with-args HOOK &rest ARGS)")
1379 (run-hook-with-args-until-failure
1380 . "(run-hook-with-args-until-failure HOOK &rest ARGS)")
1381 (run-hook-with-args-until-success
1382 . "(run-hook-with-args-until-success HOOK &rest ARGS)")
1383 (funcall . "(funcall FUNCTION &rest ARGUMENTS)")
1384 (append . "(append &rest SEQUENCES)")
1385 (concat . "(concat &rest SEQUENCES)")
1386 (vconcat . "(vconcat vconcat)")
1387 (nconc . "(nconc &rest LISTS)")
1388 (widget-apply . "(widget-apply WIDGET PROPERTY &rest ARGS)")
1389 (make-hash-table . "(make-hash-table &rest KEYWORD-ARGS)")
1390 (insert-string . "(insert-string &rest ARGS)")
1391 (start-process . "(start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)")
1392 (setq-default . "(setq-default SYMBOL VALUE [SYMBOL VALUE...])")
1393 (save-excursion . "(save-excursion &rest BODY)")
1394 (save-current-buffer . "(save-current-buffer &rest BODY)")
1395 (save-restriction . "(save-restriction &rest BODY)")
1396 (or . "(or CONDITIONS ...)")
1397 (and . "(and CONDITIONS ...)")
1398 (if . "(if COND THEN ELSE...)")
1399 (cond . "(cond CLAUSES...)")
1400 (progn . "(progn BODY ...)")
1401 (prog1 . "(prog1 FIRST BODY...)")
1402 (prog2 . "(prog2 X Y BODY...)")
1403 (setq . "(setq SYM VAL SYM VAL ...)")
1404 (quote . "(quote ARG)")
1405 (function . "(function ARG)")
1406 (defun . "(defun NAME ARGLIST [DOCSTRING] BODY...)")
1407 (defmacro . "(defmacro NAME ARGLIST [DOCSTRING] BODY...)")
1408 (defvar . "(defvar SYMBOL [INITVALUE DOCSTRING])")
1409 (defconst . "(defconst SYMBOL INITVALUE [DOCSTRING])")
1410 (let* . "(let* VARLIST BODY...)")
1411 (let . "(let VARLIST BODY...)")
1412 (while . "(while TEST BODY...)")
1413 (catch . "(catch TAG BODY...)")
1414 (unwind-protect . "(unwind-protect BODYFORM UNWINDFORMS...)")
1415 (condition-case . "(condition-case VAR BODYFORM HANDLERS...)")
1416 (track-mouse . "(track-mouse BOFY ...)")
1417 (ml-if . "(ml-if COND THEN ELSE...)")
1418 (ml-provide-prefix-argument . "(ml-provide-prefix-argument ARG1 ARG2)")
1419 (with-output-to-temp-buffer
1420 . "(with-output-to-temp-buffer BUFFNAME BODY ...)")
1421 (save-window-excursion . "(save-window-excursion BODY ...)"))))
1422
1423 ;;; help.el ends here