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