]> code.delx.au - gnu-emacs/blob - lisp/help.el
(help-xref-on-pp): New function.
[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 help-xref-on-pp (from to)
755 "Add xrefs for symbols in `pp's output between FROM and TO."
756 (let ((ost (syntax-table)))
757 (unwind-protect
758 (save-excursion
759 (save-restriction
760 (set-syntax-table emacs-lisp-mode-syntax-table)
761 (narrow-to-region from to)
762 (goto-char (point-min))
763 (while (not (eobp))
764 (cond
765 ((looking-at "\"") (forward-sexp 1))
766 ((looking-at "#<") (search-forward ">" nil 'move))
767 ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)")
768 (let* ((sym (intern-soft
769 (buffer-substring (match-beginning 1) (match-end 1))))
770 (fn (cond ((fboundp sym) #'describe-function)
771 ((and sym (boundp sym)) #'describe-variable))))
772 (when fn (help-xref-button 1 fn sym)))
773 (goto-char (match-end 1)))
774 (t (forward-char 1))))))
775 (set-syntax-table ost))))
776
777 (defun describe-variable (variable)
778 "Display the full documentation of VARIABLE (a symbol).
779 Returns the documentation as a string, also."
780 (interactive
781 (let ((v (variable-at-point))
782 (enable-recursive-minibuffers t)
783 val)
784 (setq val (completing-read (if (symbolp v)
785 (format "Describe variable (default %s): " v)
786 "Describe variable: ")
787 obarray 'boundp t nil nil
788 (if (symbolp v) (symbol-name v))))
789 (list (if (equal val "")
790 v (intern val)))))
791 (if (symbolp variable)
792 (let (valvoid)
793 (with-output-to-temp-buffer "*Help*"
794 (prin1 variable)
795 (if (not (boundp variable))
796 (progn
797 (princ " is void")
798 (setq valvoid t))
799 (let ((val (symbol-value variable)))
800 (with-current-buffer standard-output
801 (princ "'s value is ")
802 (terpri)
803 (let ((from (point)))
804 (pp val)
805 (help-xref-on-pp from (point))))))
806 (terpri)
807 (if (local-variable-p variable)
808 (progn
809 (princ (format "Local in buffer %s; " (buffer-name)))
810 (if (not (default-boundp variable))
811 (princ "globally void")
812 (let ((val (default-value variable)))
813 (with-current-buffer standard-output
814 (princ "global value is ")
815 (terpri)
816 (let ((from (point)))
817 (pp val)
818 (help-xref-on-pp from (point))))))
819 (terpri)))
820 (terpri)
821 (save-current-buffer
822 (set-buffer standard-output)
823 (if (> (count-lines (point-min) (point-max)) 10)
824 (progn
825 (set-syntax-table emacs-lisp-mode-syntax-table)
826 (goto-char (point-min))
827 (if valvoid
828 (forward-line 1)
829 (forward-sexp 1)
830 (delete-region (point) (progn (end-of-line) (point)))
831 (insert "'s value is shown below.\n\n")
832 (save-excursion
833 (insert "\n\nValue:"))))))
834 (princ "Documentation:")
835 (terpri)
836 (let ((doc (documentation-property variable 'variable-documentation)))
837 (princ (or doc "not documented as a variable.")))
838 (help-setup-xref (list #'describe-variable variable) (interactive-p))
839
840 ;; Make a link to customize if this variable can be customized.
841 ;; Note, it is not reliable to test only for a custom-type property
842 ;; because those are only present after the var's definition
843 ;; has been loaded.
844 (if (or (get variable 'custom-type) ; after defcustom
845 (get variable 'custom-loads) ; from loaddefs.el
846 (get variable 'standard-value)) ; from cus-start.el
847 (let ((customize-label "customize"))
848 (terpri)
849 (terpri)
850 (princ (concat "You can " customize-label " this variable."))
851 (with-current-buffer "*Help*"
852 (save-excursion
853 (re-search-backward
854 (concat "\\(" customize-label "\\)") nil t)
855 (help-xref-button 1 #'(lambda (v)
856 (customize-variable v))
857 variable
858 "mouse-2, RET: customize variable")
859 ))))
860 ;; Make a hyperlink to the library if appropriate. (Don't
861 ;; change the format of the buffer's initial line in case
862 ;; anything expects the current format.)
863 (let ((file-name (symbol-file variable)))
864 (when file-name
865 (princ "\n\nDefined in `")
866 (princ file-name)
867 (princ "'.")
868 (with-current-buffer "*Help*"
869 (save-excursion
870 (re-search-backward "`\\([^`']+\\)'" nil t)
871 (help-xref-button
872 1 (lambda (arg)
873 (let ((location
874 (find-variable-noselect arg)))
875 (pop-to-buffer (car location))
876 (goto-char (cdr location))))
877 variable "mouse-2, RET: find variable's definition")))))
878
879 (print-help-return-message)
880 (save-excursion
881 (set-buffer standard-output)
882 ;; Return the text we displayed.
883 (buffer-string))))
884 (message "You did not specify a variable")))
885
886 (defun describe-bindings (&optional prefix buffer)
887 "Show a list of all defined keys, and their definitions.
888 We put that list in a buffer, and display the buffer.
889
890 The optional argument PREFIX, if non-nil, should be a key sequence;
891 then we display only bindings that start with that prefix.
892 The optional argument BUFFER specifies which buffer's bindings
893 to display (default, the current buffer)."
894 (interactive "P")
895 (or buffer (setq buffer (current-buffer)))
896 (with-current-buffer buffer
897 (describe-bindings-internal nil prefix))
898 (with-current-buffer "*Help*"
899 (help-setup-xref (list #'describe-bindings prefix buffer)
900 (interactive-p))))
901
902 (defun where-is (definition &optional insert)
903 "Print message listing key sequences that invoke the command DEFINITION.
904 Argument is a command definition, usually a symbol with a function definition.
905 If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
906 (interactive
907 (let ((fn (function-called-at-point))
908 (enable-recursive-minibuffers t)
909 val)
910 (setq val (completing-read (if fn
911 (format "Where is command (default %s): " fn)
912 "Where is command: ")
913 obarray 'commandp t))
914 (list (if (equal val "")
915 fn (intern val))
916 current-prefix-arg)))
917 (let* ((keys (where-is-internal definition overriding-local-map nil nil))
918 (keys1 (mapconcat 'key-description keys ", "))
919 (standard-output (if insert (current-buffer) t)))
920 (if insert
921 (if (> (length keys1) 0)
922 (princ (format "%s (%s)" keys1 definition))
923 (princ (format "M-x %s RET" definition)))
924 (if (> (length keys1) 0)
925 (princ (format "%s is on %s" definition keys1))
926 (princ (format "%s is not on any key" definition)))))
927 nil)
928
929 (defun locate-library (library &optional nosuffix path interactive-call)
930 "Show the precise file name of Emacs library LIBRARY.
931 This command searches the directories in `load-path' like `M-x load-library'
932 to find the file that `M-x load-library RET LIBRARY RET' would load.
933 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
934 to the specified name LIBRARY.
935
936 If the optional third arg PATH is specified, that list of directories
937 is used instead of `load-path'.
938
939 When called from a program, the file name is normaly returned as a
940 string. When run interactively, the argument INTERACTIVE-CALL is t,
941 and the file name is displayed in the echo area."
942 (interactive (list (read-string "Locate library: ")
943 nil nil
944 t))
945 (let (result)
946 (catch 'answer
947 (mapcar
948 (lambda (dir)
949 (mapcar
950 (lambda (suf)
951 (let ((try (expand-file-name (concat library suf) dir)))
952 (and (file-readable-p try)
953 (null (file-directory-p try))
954 (progn
955 (setq result try)
956 (throw 'answer try)))))
957 (if nosuffix
958 '("")
959 '(".elc" ".el" "")
960 ;;; load doesn't handle this yet.
961 ;;; (let ((basic '(".elc" ".el" ""))
962 ;;; (compressed '(".Z" ".gz" "")))
963 ;;; ;; If autocompression mode is on,
964 ;;; ;; consider all combinations of library suffixes
965 ;;; ;; and compression suffixes.
966 ;;; (if (rassq 'jka-compr-handler file-name-handler-alist)
967 ;;; (apply 'nconc
968 ;;; (mapcar (lambda (compelt)
969 ;;; (mapcar (lambda (baselt)
970 ;;; (concat baselt compelt))
971 ;;; basic))
972 ;;; compressed))
973 ;;; basic))
974 )))
975 (or path load-path)))
976 (and interactive-call
977 (if result
978 (message "Library is file %s" result)
979 (message "No library %s in search path" library)))
980 result))
981
982 \f
983 ;;; Grokking cross-reference information in doc strings and
984 ;;; hyperlinking it.
985
986 ;; This may have some scope for extension and the same or something
987 ;; similar should be done for widget doc strings, which currently use
988 ;; another mechanism.
989
990 (defcustom help-highlight-p t
991 "*If non-nil, `help-make-xrefs' highlight cross-references.
992 Under a window system it highlights them with face defined by
993 `help-highlight-face'."
994 :group 'help
995 :version "20.3"
996 :type 'boolean)
997
998 (defcustom help-highlight-face 'underline
999 "Face used by `help-make-xrefs' to highlight cross-references.
1000 Must be previously-defined."
1001 :group 'help
1002 :version "20.3"
1003 :type 'face)
1004
1005 (defvar help-back-label (purecopy "[back]")
1006 "Label to use by `help-make-xrefs' for the go-back reference.")
1007
1008 (defconst help-xref-symbol-regexp
1009 (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
1010 "\\(function\\|command\\)\\|"
1011 "\\(symbol\\)\\)\\s-+\\)?"
1012 ;; Note starting with word-syntax character:
1013 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'"))
1014 "Regexp matching doc string references to symbols.
1015
1016 The words preceding the quoted symbol can be used in doc strings to
1017 distinguish references to variables, functions and symbols.")
1018
1019 (defconst help-xref-info-regexp
1020 (purecopy "\\<[Ii]nfo[ \t\n]+node[ \t\n]+`\\([^']+\\)'")
1021 "Regexp matching doc string references to an Info node.")
1022
1023 (defun help-setup-xref (item interactive-p)
1024 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
1025
1026 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
1027 buffer after following a reference. INTERACTIVE-P is non-nil if the
1028 calling command was invoked interactively. In this case the stack of
1029 items for help buffer \"back\" buttons is cleared."
1030 (if interactive-p
1031 (setq help-xref-stack nil))
1032 (setq help-xref-stack-item item))
1033
1034 (defun help-make-xrefs (&optional buffer)
1035 "Parse and hyperlink documentation cross-references in the given BUFFER.
1036
1037 Find cross-reference information in a buffer and, if
1038 `help-highlight-p' is non-nil, highlight it with face defined by
1039 `help-highlight-face'; activate such cross references for selection
1040 with `help-follow'. Cross-references have the canonical form `...'
1041 and the type of reference may be disambiguated by the preceding
1042 word(s) used in `help-xref-symbol-regexp'.
1043
1044 A special reference `back' is made to return back through a stack of
1045 help buffers. Variable `help-back-label' specifies the text for
1046 that."
1047 (interactive "b")
1048 (save-excursion
1049 (set-buffer (or buffer (current-buffer)))
1050 (goto-char (point-min))
1051 ;; Skip the header-type info, though it might be useful to parse
1052 ;; it at some stage (e.g. "function in `library'").
1053 (forward-paragraph)
1054 (let ((old-modified (buffer-modified-p)))
1055 (let ((stab (syntax-table))
1056 (case-fold-search t)
1057 (inhibit-read-only t))
1058 (set-syntax-table emacs-lisp-mode-syntax-table)
1059 ;; The following should probably be abstracted out.
1060 (unwind-protect
1061 (progn
1062 ;; Info references
1063 (save-excursion
1064 (while (re-search-forward help-xref-info-regexp nil t)
1065 (let ((data (match-string 1)))
1066 (save-match-data
1067 (unless (string-match "^([^)]+)" data)
1068 (setq data (concat "(emacs)" data))))
1069 (help-xref-button 1 #'info data
1070 "mouse-2, RET: read this Info node"))))
1071 ;; Quoted symbols
1072 (save-excursion
1073 (while (re-search-forward help-xref-symbol-regexp nil t)
1074 (let* ((data (match-string 6))
1075 (sym (intern-soft data)))
1076 (if sym
1077 (cond
1078 ((match-string 3) ; `variable' &c
1079 (and (boundp sym) ; `variable' doesn't ensure
1080 ; it's actually bound
1081 (help-xref-button
1082 6 #'describe-variable sym
1083 "mouse-2, RET: describe this variable")))
1084 ((match-string 4) ; `function' &c
1085 (and (fboundp sym) ; similarly
1086 (help-xref-button
1087 6 #'describe-function sym
1088 "mouse-2, RET: describe this function")))
1089 ((match-string 5)) ; nothing for symbol
1090 ((and (boundp sym) (fboundp sym))
1091 ;; We can't intuit whether to use the
1092 ;; variable or function doc -- supply both.
1093 (help-xref-button
1094 6 #'help-xref-interned sym
1095 "mouse-2, RET: describe this symbol"))
1096 ((boundp sym)
1097 (help-xref-button
1098 6 #'describe-variable sym
1099 "mouse-2, RET: describe this variable"))
1100 ((fboundp sym)
1101 (help-xref-button
1102 6 #'describe-function sym
1103 "mouse-2, RET: describe this function")))))))
1104 ;; An obvious case of a key substitution:
1105 (save-excursion
1106 (while (re-search-forward
1107 ;; Assume command name is only word characters
1108 ;; and dashes to get things like `use M-x foo.'.
1109 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|-\\)+\\)" nil t)
1110 (let ((sym (intern-soft (match-string 1))))
1111 (if (fboundp sym)
1112 (help-xref-button
1113 1 #'describe-function sym
1114 "mouse-2, RET: describe this command")))))
1115 ;; Look for commands in whole keymap substitutions:
1116 (save-excursion
1117 ;; Make sure to find the first keymap.
1118 (goto-char (point-min))
1119 ;; Find a header and the column at which the command
1120 ;; name will be found.
1121 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
1122 nil t)
1123 (let ((col (- (match-end 1) (match-beginning 1))))
1124 (while
1125 ;; Ignore single blank lines in table, but not
1126 ;; double ones, which should terminate it.
1127 (and (not (looking-at "\n\\s-*\n"))
1128 (progn
1129 (and (eolp) (forward-line))
1130 (end-of-line)
1131 (skip-chars-backward "^\t\n")
1132 (if (and (>= (current-column) col)
1133 (looking-at "\\(\\sw\\|-\\)+$"))
1134 (let ((sym (intern-soft (match-string 0))))
1135 (if (fboundp sym)
1136 (help-xref-button
1137 0 #'describe-function sym
1138 "mouse-2, RET: describe this function"))))
1139 (zerop (forward-line)))))))))
1140 (set-syntax-table stab))
1141 ;; Make a back-reference in this buffer if appropriate.
1142 (when help-xref-stack
1143 (goto-char (point-max))
1144 (save-excursion
1145 (insert "\n\n" help-back-label))
1146 ;; Just to provide the match data:
1147 (looking-at (concat "\n\n\\(" (regexp-quote help-back-label) "\\)"))
1148 (help-xref-button 1 #'help-xref-go-back (current-buffer))))
1149 ;; View mode steals RET from us.
1150 (set (make-local-variable 'minor-mode-overriding-map-alist)
1151 (list (cons 'view-mode
1152 (let ((map (make-sparse-keymap)))
1153 (set-keymap-parent map view-mode-map)
1154 (define-key map "\r" 'help-follow)
1155 map))))
1156 (set-buffer-modified-p old-modified))))
1157
1158 (defun help-xref-button (match-number function data &optional help-echo)
1159 "Make a hyperlink for cross-reference text previously matched.
1160
1161 MATCH-NUMBER is the subexpression of interest in the last matched
1162 regexp. FUNCTION is a function to invoke when the button is
1163 activated, applied to DATA. DATA may be a single value or a list.
1164 See `help-make-xrefs'.
1165 If optional arg HELP-ECHO is supplied, it is used as a help string."
1166 ;; Don't mung properties we've added specially in some instances.
1167 (unless (get-text-property (match-beginning match-number) 'help-xref)
1168 (add-text-properties (match-beginning match-number)
1169 (match-end match-number)
1170 (list 'mouse-face 'highlight
1171 'help-xref (cons function
1172 (if (listp data)
1173 data
1174 (list data)))))
1175 (if help-echo
1176 (put-text-property (match-beginning match-number)
1177 (match-end match-number)
1178 'help-echo help-echo))
1179 (if help-highlight-p
1180 (put-text-property (match-beginning match-number)
1181 (match-end match-number)
1182 'face help-highlight-face))))
1183
1184 \f
1185 ;; Additional functions for (re-)creating types of help buffers.
1186 (defun help-xref-interned (symbol)
1187 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
1188
1189 Both variable and function documentation are extracted into a single
1190 help buffer."
1191 (let ((fdoc (when (fboundp symbol) (describe-function symbol))))
1192 (when (or (boundp symbol) (not fdoc))
1193 (describe-variable symbol)
1194 ;; We now have a help buffer on the variable. Insert the function
1195 ;; text before it.
1196 (when fdoc
1197 (with-current-buffer "*Help*"
1198 (goto-char (point-min))
1199 (let ((inhibit-read-only t))
1200 (insert fdoc "\n\n" (symbol-name symbol) " is also a variable.\n\n"))
1201 (help-setup-xref (list #'help-xref-interned symbol) nil))))))
1202
1203 (defun help-xref-mode (buffer)
1204 "Do a `describe-mode' for the specified BUFFER."
1205 (save-excursion
1206 (set-buffer buffer)
1207 (describe-mode)))
1208 \f
1209 ;;; Navigation/hyperlinking with xrefs
1210
1211 (defun help-follow-mouse (click)
1212 "Follow the cross-reference that you click on."
1213 (interactive "e")
1214 (let* ((start (event-start click))
1215 (window (car start))
1216 (pos (car (cdr start))))
1217 (with-current-buffer (window-buffer window)
1218 (help-follow pos))))
1219
1220 (defun help-xref-go-back (buffer)
1221 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
1222 (let (item position method args)
1223 (with-current-buffer buffer
1224 (when help-xref-stack
1225 (setq help-xref-stack (cdr help-xref-stack)) ; due to help-follow
1226 (setq item (car help-xref-stack)
1227 position (car item)
1228 method (cadr item)
1229 args (cddr item))
1230 (setq help-xref-stack (cdr help-xref-stack))))
1231 (apply method args)
1232 ;; We're not in the right buffer to do this, and we don't actually
1233 ;; know which we should be in.
1234 ;;(goto-char position)
1235 ))
1236
1237 (defun help-go-back ()
1238 "Invoke the [back] button (if any) in the Help mode buffer."
1239 (interactive)
1240 (help-follow (1- (point-max))))
1241
1242 (defun help-follow (&optional pos)
1243 "Follow cross-reference at POS, defaulting to point.
1244
1245 For the cross-reference format, see `help-make-xrefs'."
1246 (interactive "d")
1247 (unless pos
1248 (setq pos (point)))
1249 (let* ((help-data
1250 (or (and (not (= pos (point-max)))
1251 (get-text-property pos 'help-xref))
1252 (and (not (= pos (point-min)))
1253 (get-text-property (1- pos) 'help-xref))
1254 ;; check if the symbol under point is a function or variable
1255 (let ((sym
1256 (intern
1257 (save-excursion
1258 (goto-char pos) (skip-syntax-backward "w_")
1259 (buffer-substring (point)
1260 (progn (skip-syntax-forward "w_")
1261 (point)))))))
1262 (when (or (boundp sym) (fboundp sym))
1263 (list #'help-xref-interned sym)))))
1264 (method (car help-data))
1265 (args (cdr help-data)))
1266 (when help-data
1267 (setq help-xref-stack (cons (cons (point) help-xref-stack-item)
1268 help-xref-stack))
1269 (setq help-xref-stack-item nil)
1270 ;; There is a reference at point. Follow it.
1271 (apply method args))))
1272
1273 ;; For tabbing through buffer.
1274 (defun help-next-ref ()
1275 "Find the next help cross-reference in the buffer."
1276 (interactive)
1277 (let (pos)
1278 (while (not pos)
1279 (if (get-text-property (point) 'help-xref) ; move off reference
1280 (goto-char (or (next-single-property-change (point) 'help-xref)
1281 (point))))
1282 (cond ((setq pos (next-single-property-change (point) 'help-xref))
1283 (if pos (goto-char pos)))
1284 ((bobp)
1285 (message "No cross references in the buffer.")
1286 (setq pos t))
1287 (t ; be circular
1288 (goto-char (point-min)))))))
1289
1290 (defun help-previous-ref ()
1291 "Find the previous help cross-reference in the buffer."
1292 (interactive)
1293 (let (pos)
1294 (while (not pos)
1295 (if (get-text-property (point) 'help-xref) ; move off reference
1296 (goto-char (or (previous-single-property-change (point) 'help-xref)
1297 (point))))
1298 (cond ((setq pos (previous-single-property-change (point) 'help-xref))
1299 (if pos (goto-char pos)))
1300 ((bobp)
1301 (message "No cross references in the buffer.")
1302 (setq pos t))
1303 (t ; be circular
1304 (goto-char (point-max)))))))
1305
1306 \f
1307 ;;; Automatic resizing of temporary buffers.
1308
1309 (defcustom temp-buffer-resize-mode nil
1310 "Non-nil means resize windows displaying temporary buffers.
1311 This makes the window the right height for its contents, but never
1312 more than `temp-buffer-max-height' nor less than `window-min-height'.
1313 This applies to `help', `apropos' and `completion' buffers, and some others.
1314
1315 Setting this variable directly does not take effect;
1316 use either \\[customize] or the function `temp-buffer-resize-mode'."
1317 :get (lambda (symbol)
1318 (and (memq 'resize-temp-buffer-window temp-buffer-show-hook) t))
1319 :set (lambda (symbol value)
1320 (temp-buffer-resize-mode (if value 1 -1)))
1321 :initialize 'custom-initialize-default
1322 :type 'boolean
1323 :group 'help
1324 :version "20.4")
1325
1326 (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
1327 "*Maximum height of a window displaying a temporary buffer.
1328 This is the maximum height (in text lines) which `resize-temp-buffer-window'
1329 will give to a window displaying a temporary buffer.
1330 It can also be a function which will be called with the object corresponding
1331 to the buffer to be displayed as argument and should return an integer
1332 positive number."
1333 :type '(choice integer function)
1334 :group 'help
1335 :version "20.4")
1336
1337 (defun temp-buffer-resize-mode (arg)
1338 "Toggle the mode which that makes windows smaller for temporary buffers.
1339 With prefix argument ARG, turn the resizing of windows displaying temporary
1340 buffers on if ARG is positive or off otherwise.
1341 See the documentation of the variable `temp-buffer-resize-mode' for
1342 more information."
1343 (interactive "P")
1344 (let ((turn-it-on
1345 (if (null arg)
1346 (not (memq 'resize-temp-buffer-window temp-buffer-show-hook))
1347 (> (prefix-numeric-value arg) 0))))
1348 (if turn-it-on
1349 (progn
1350 ;; `help-mode-maybe' may add a `back' button and thus increase the
1351 ;; text size, so `resize-temp-buffer-window' must be run *after* it.
1352 (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
1353 (setq temp-buffer-resize-mode t))
1354 (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)
1355 (setq temp-buffer-resize-mode nil))))
1356
1357 (defun resize-temp-buffer-window ()
1358 "Resize the current window to fit its contents.
1359 Will not make it higher than `temp-buffer-max-height' nor smaller than
1360 `window-min-height'. Do nothing if it is the only window on its frame, if it
1361 is not as wide as the frame or if some of the window's contents are scrolled
1362 out of view."
1363 (unless (or (one-window-p 'nomini)
1364 (not (pos-visible-in-window-p (point-min)))
1365 (/= (frame-width) (window-width)))
1366 (let* ((max-height (if (functionp temp-buffer-max-height)
1367 (funcall temp-buffer-max-height (current-buffer))
1368 temp-buffer-max-height))
1369 (win-height (1- (window-height)))
1370 (min-height (1- window-min-height))
1371 (text-height (count-screen-lines))
1372 (new-height (max (min text-height max-height) min-height)))
1373 (enlarge-window (- new-height win-height)))))
1374
1375 ;; `help-manyarg-func-alist' is defined primitively (in doc.c).
1376 ;; New primitives with `MANY' or `UNEVALLED' arglists should be added
1377 ;; to this alist.
1378 ;; The parens and function name are redundant, but it's messy to add
1379 ;; them in `documentation'.
1380 (defconst help-manyarg-func-alist
1381 (purecopy
1382 '((list . "(list &rest OBJECTS)")
1383 (vector . "(vector &rest OBJECTS)")
1384 (make-byte-code . "(make-byte-code &rest ELEMENTS)")
1385 (call-process
1386 . "(call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)")
1387 (string . "(string &rest CHARACTERS)")
1388 (+ . "(+ &rest NUMBERS-OR-MARKERS)")
1389 (- . "(- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS)")
1390 (* . "(* &rest NUMBERS-OR-MARKERS)")
1391 (/ . "(/ DIVIDEND DIVISOR &rest DIVISORS)")
1392 (max . "(max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS)")
1393 (min . "(min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS)")
1394 (logand . "(logand &rest INTS-OR-MARKERS)")
1395 (logior . "(logior &rest INTS-OR-MARKERS)")
1396 (logxor . "(logxor &rest INTS-OR-MARKERS)")
1397 (encode-time
1398 . "(encode-time SECOND MINUTE HOUR DAY MONTH YEAR &optional ZONE)")
1399 (insert . "(insert &rest ARGS)")
1400 (insert-before-markers . "(insert-before-markers &rest ARGS)")
1401 (message . "(message STRING &rest ARGUMENTS)")
1402 (message-box . "(message-box STRING &rest ARGUMENTS)")
1403 (message-or-box . "(message-or-box STRING &rest ARGUMENTS)")
1404 (propertize . "(propertize STRING &rest PROPERTIES)")
1405 (format . "(format STRING &rest OBJECTS)")
1406 (apply . "(apply FUNCTION &rest ARGUMENTS)")
1407 (run-hooks . "(run-hooks &rest HOOKS)")
1408 (run-hook-with-args . "(run-hook-with-args HOOK &rest ARGS)")
1409 (run-hook-with-args-until-failure
1410 . "(run-hook-with-args-until-failure HOOK &rest ARGS)")
1411 (run-hook-with-args-until-success
1412 . "(run-hook-with-args-until-success HOOK &rest ARGS)")
1413 (funcall . "(funcall FUNCTION &rest ARGUMENTS)")
1414 (append . "(append &rest SEQUENCES)")
1415 (concat . "(concat &rest SEQUENCES)")
1416 (vconcat . "(vconcat vconcat)")
1417 (nconc . "(nconc &rest LISTS)")
1418 (widget-apply . "(widget-apply WIDGET PROPERTY &rest ARGS)")
1419 (make-hash-table . "(make-hash-table &rest KEYWORD-ARGS)")
1420 (insert-string . "(insert-string &rest ARGS)")
1421 (start-process . "(start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)")
1422 (setq-default . "(setq-default SYMBOL VALUE [SYMBOL VALUE...])")
1423 (save-excursion . "(save-excursion &rest BODY)")
1424 (save-current-buffer . "(save-current-buffer &rest BODY)")
1425 (save-restriction . "(save-restriction &rest BODY)")
1426 (or . "(or CONDITIONS ...)")
1427 (and . "(and CONDITIONS ...)")
1428 (if . "(if COND THEN ELSE...)")
1429 (cond . "(cond CLAUSES...)")
1430 (progn . "(progn BODY ...)")
1431 (prog1 . "(prog1 FIRST BODY...)")
1432 (prog2 . "(prog2 X Y BODY...)")
1433 (setq . "(setq SYM VAL SYM VAL ...)")
1434 (quote . "(quote ARG)")
1435 (function . "(function ARG)")
1436 (defun . "(defun NAME ARGLIST [DOCSTRING] BODY...)")
1437 (defmacro . "(defmacro NAME ARGLIST [DOCSTRING] BODY...)")
1438 (defvar . "(defvar SYMBOL [INITVALUE DOCSTRING])")
1439 (defconst . "(defconst SYMBOL INITVALUE [DOCSTRING])")
1440 (let* . "(let* VARLIST BODY...)")
1441 (let . "(let VARLIST BODY...)")
1442 (while . "(while TEST BODY...)")
1443 (catch . "(catch TAG BODY...)")
1444 (unwind-protect . "(unwind-protect BODYFORM UNWINDFORMS...)")
1445 (condition-case . "(condition-case VAR BODYFORM HANDLERS...)")
1446 (track-mouse . "(track-mouse BOFY ...)")
1447 (ml-if . "(ml-if COND THEN ELSE...)")
1448 (ml-provide-prefix-argument . "(ml-provide-prefix-argument ARG1 ARG2)")
1449 (with-output-to-temp-buffer
1450 . "(with-output-to-temp-buffer BUFFNAME BODY ...)")
1451 (save-window-excursion . "(save-window-excursion BODY ...)"))))
1452
1453 ;;; help.el ends here