]> code.delx.au - gnu-emacs/blob - lisp/help-mode.el
(help-buffer): Add docstring.
[gnu-emacs] / lisp / help-mode.el
1 ;;; help-mode.el --- `help-mode' used by *Help* buffers
2
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Defines `help-mode', which is the mode used by *Help* buffers, and
27 ;; associated support machinery, such as adding hyperlinks, etc.,
28
29 ;;; Code:
30
31 (require 'button)
32 (require 'view)
33 (eval-when-compile (require 'easymenu))
34
35 (defvar help-mode-map (make-sparse-keymap)
36 "Keymap for help mode.")
37
38 (set-keymap-parent help-mode-map button-buffer-map)
39
40 (define-key help-mode-map [mouse-2] 'help-follow-mouse)
41 (define-key help-mode-map "\C-c\C-b" 'help-go-back)
42 (define-key help-mode-map "\C-c\C-f" 'help-go-forward)
43 (define-key help-mode-map "\C-c\C-c" 'help-follow-symbol)
44 ;; Documentation only, since we use minor-mode-overriding-map-alist.
45 (define-key help-mode-map "\r" 'help-follow)
46
47 (easy-menu-define help-mode-menu help-mode-map
48 "Menu for Help Mode."
49 '("Help-Mode"
50 ["Show Help for Symbol" help-follow-symbol
51 :help "Show the docs for the symbol at point"]
52 ["Previous Topic" help-go-back
53 :help "Go back to previous topic in this help buffer"]
54 ["Next Topic" help-go-forward
55 :help "Go back to next topic in this help buffer"]
56 ["Move to Previous Button" backward-button
57 :help "Move to the Next Button in the help buffer"]
58 ["Move to Next Button" forward-button
59 :help "Move to the Next Button in the help buffer"]))
60
61 (defvar help-xref-stack nil
62 "A stack of ways by which to return to help buffers after following xrefs.
63 Used by `help-follow' and `help-xref-go-back'.
64 An element looks like (POSITION FUNCTION ARGS...).
65 To use the element, do (apply FUNCTION ARGS) then goto the point.")
66 (put 'help-xref-stack 'permanent-local t)
67 (make-variable-buffer-local 'help-xref-stack)
68
69 (defvar help-xref-forward-stack nil
70 "A stack used to navigate help forwards after using the back button.
71 Used by `help-follow' and `help-xref-go-forward'.
72 An element looks like (POSITION FUNCTION ARGS...).
73 To use the element, do (apply FUNCTION ARGS) then goto the point.")
74 (put 'help-xref-forward-stack 'permanent-local t)
75 (make-variable-buffer-local 'help-xref-forward-stack)
76
77 (defvar help-xref-stack-item nil
78 "An item for `help-follow' in this buffer to push onto `help-xref-stack'.
79 The format is (FUNCTION ARGS...).")
80 (put 'help-xref-stack-item 'permanent-local t)
81 (make-variable-buffer-local 'help-xref-stack-item)
82
83 (defvar help-xref-stack-forward-item nil
84 "An item for `help-go-back' to push onto `help-xref-forward-stack'.
85 The format is (FUNCTION ARGS...).")
86 (put 'help-xref-stack-forward-item 'permanent-local t)
87 (make-variable-buffer-local 'help-xref-stack-forward-item)
88
89 (setq-default help-xref-stack nil help-xref-stack-item nil)
90 (setq-default help-xref-forward-stack nil help-xref-forward-stack-item nil)
91
92 (defcustom help-mode-hook nil
93 "Hook run by `help-mode'."
94 :type 'hook
95 :group 'help)
96 \f
97 ;; Button types used by help
98
99 (define-button-type 'help-xref
100 'follow-link t
101 'action #'help-button-action)
102
103 (defun help-button-action (button)
104 "Call BUTTON's help function."
105 (help-do-xref (button-start button)
106 (button-get button 'help-function)
107 (button-get button 'help-args)))
108
109 ;; These 6 calls to define-button-type were generated in a dolist
110 ;; loop, but that is bad because it means these button types don't
111 ;; have an easily found definition.
112
113 (define-button-type 'help-function
114 :supertype 'help-xref
115 'help-function 'describe-function
116 'help-echo (purecopy "mouse-2, RET: describe this function"))
117
118 (define-button-type 'help-variable
119 :supertype 'help-xref
120 'help-function 'describe-variable
121 'help-echo (purecopy "mouse-2, RET: describe this variable"))
122
123 (define-button-type 'help-face
124 :supertype 'help-xref
125 'help-function 'describe-face
126 'help-echo (purecopy "mouse-2, RET: describe this face"))
127
128 (define-button-type 'help-coding-system
129 :supertype 'help-xref
130 'help-function 'describe-coding-system
131 'help-echo (purecopy "mouse-2, RET: describe this coding system"))
132
133 (define-button-type 'help-input-method
134 :supertype 'help-xref
135 'help-function 'describe-input-method
136 'help-echo (purecopy "mouse-2, RET: describe this input method"))
137
138 (define-button-type 'help-character-set
139 :supertype 'help-xref
140 'help-function 'describe-character-set
141 'help-echo (purecopy "mouse-2, RET: describe this character set"))
142
143 ;; make some more ideosyncratic button types
144
145 (define-button-type 'help-symbol
146 :supertype 'help-xref
147 'help-function #'help-xref-interned
148 'help-echo (purecopy "mouse-2, RET: describe this symbol"))
149
150 (define-button-type 'help-back
151 :supertype 'help-xref
152 'help-function #'help-xref-go-back
153 'help-echo (purecopy "mouse-2, RET: go back to previous help buffer"))
154
155 (define-button-type 'help-forward
156 :supertype 'help-xref
157 'help-function #'help-xref-go-forward
158 'help-echo (purecopy "mouse-2, RET: move forward to next help buffer"))
159
160 (define-button-type 'help-info-variable
161 :supertype 'help-xref
162 ;; the name of the variable is put before the argument to Info
163 'help-function (lambda (a v) (info v))
164 'help-echo (purecopy "mouse-2, RET: read this Info node"))
165
166 (define-button-type 'help-info
167 :supertype 'help-xref
168 'help-function #'info
169 'help-echo (purecopy "mouse-2, RET: read this Info node"))
170
171 (define-button-type 'help-url
172 :supertype 'help-xref
173 'help-function #'browse-url
174 'help-echo (purecopy "mouse-2, RET: view this URL in a browser"))
175
176 (define-button-type 'help-customize-variable
177 :supertype 'help-xref
178 'help-function (lambda (v)
179 (customize-variable v))
180 'help-echo (purecopy "mouse-2, RET: customize variable"))
181
182 (define-button-type 'help-customize-face
183 :supertype 'help-xref
184 'help-function (lambda (v)
185 (customize-face v))
186 'help-echo (purecopy "mouse-2, RET: customize face"))
187
188 (define-button-type 'help-function-def
189 :supertype 'help-xref
190 'help-function (lambda (fun file)
191 (require 'find-func)
192 (when (eq file 'C-source)
193 (setq file
194 (help-C-file-name (indirect-function fun) 'fun)))
195 ;; Don't use find-function-noselect because it follows
196 ;; aliases (which fails for built-in functions).
197 (let ((location
198 (find-function-search-for-symbol fun nil file)))
199 (pop-to-buffer (car location))
200 (if (cdr location)
201 (goto-char (cdr location))
202 (message "Unable to find location in file"))))
203 'help-echo (purecopy "mouse-2, RET: find function's definition"))
204
205 (define-button-type 'help-variable-def
206 :supertype 'help-xref
207 'help-function (lambda (var &optional file)
208 (when (eq file 'C-source)
209 (setq file (help-C-file-name var 'var)))
210 (let ((location (find-variable-noselect var file)))
211 (pop-to-buffer (car location))
212 (if (cdr location)
213 (goto-char (cdr location))
214 (message "Unable to find location in file"))))
215 'help-echo (purecopy "mouse-2, RET: find variable's definition"))
216
217 (define-button-type 'help-face-def
218 :supertype 'help-xref
219 'help-function (lambda (fun file)
220 (require 'find-func)
221 ;; Don't use find-function-noselect because it follows
222 ;; aliases (which fails for built-in functions).
223 (let ((location
224 (find-function-search-for-symbol fun 'defface file)))
225 (pop-to-buffer (car location))
226 (if (cdr location)
227 (goto-char (cdr location))
228 (message "Unable to find location in file"))))
229 'help-echo (purecopy "mouse-2, RET: find face's definition"))
230
231 \f
232 ;;;###autoload
233 (defun help-mode ()
234 "Major mode for viewing help text and navigating references in it.
235 Entry to this mode runs the normal hook `help-mode-hook'.
236 Commands:
237 \\{help-mode-map}"
238 (interactive)
239 (kill-all-local-variables)
240 (use-local-map help-mode-map)
241 (setq mode-name "Help")
242 (setq major-mode 'help-mode)
243
244 (view-mode)
245 (set (make-local-variable 'view-no-disable-on-exit) t)
246 ;; With Emacs 22 `view-exit-action' could delete the selected window
247 ;; disregarding whether the help buffer was shown in that window at
248 ;; all. Since `view-exit-action' is called with the help buffer as
249 ;; argument it seems more appropriate to have it work on the buffer
250 ;; only and leave it to `view-mode-exit' to delete any associated
251 ;; window(s).
252 (setq view-exit-action
253 (lambda (buffer)
254 ;; Use `with-current-buffer' to make sure that `bury-buffer'
255 ;; also removes BUFFER from the selected window.
256 (with-current-buffer buffer
257 (bury-buffer))))
258
259 (run-mode-hooks 'help-mode-hook))
260
261 ;;;###autoload
262 (defun help-mode-setup ()
263 (help-mode)
264 (setq buffer-read-only nil))
265
266 ;;;###autoload
267 (defun help-mode-finish ()
268 (if (eq help-window t)
269 ;; If `help-window' is t, `view-return-to-alist' is handled by
270 ;; `with-help-window'. In this case set `help-window' to the
271 ;; selected window since now is the only moment where we can
272 ;; unambiguously identify it.
273 (setq help-window (selected-window))
274 (let ((entry (assq (selected-window) view-return-to-alist)))
275 (if entry
276 ;; When entering Help mode from the Help window,
277 ;; such as by following a link, preserve the same
278 ;; meaning for the q command.
279 ;; (setcdr entry (cons (selected-window) help-return-method))
280 nil
281 (setq view-return-to-alist
282 (cons (cons (selected-window) help-return-method)
283 view-return-to-alist)))))
284
285 (when (eq major-mode 'help-mode)
286 ;; View mode's read-only status of existing *Help* buffer is lost
287 ;; by with-output-to-temp-buffer.
288 (toggle-read-only 1)
289 (help-make-xrefs (current-buffer))))
290 \f
291 ;; Grokking cross-reference information in doc strings and
292 ;; hyperlinking it.
293
294 ;; This may have some scope for extension and the same or something
295 ;; similar should be done for widget doc strings, which currently use
296 ;; another mechanism.
297
298 (defvar help-back-label (purecopy "[back]")
299 "Label to use by `help-make-xrefs' for the go-back reference.")
300
301 (defvar help-forward-label (purecopy "[forward]")
302 "Label to use by `help-make-xrefs' for the go-forward reference.")
303
304 (defconst help-xref-symbol-regexp
305 (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|" ; Link to var
306 "\\(function\\|command\\)\\|" ; Link to function
307 "\\(face\\)\\|" ; Link to face
308 "\\(symbol\\|program\\|property\\)\\|" ; Don't link
309 "\\(source \\(?:code \\)?\\(?:of\\|for\\)\\)\\)"
310 "[ \t\n]+\\)?"
311 ;; Note starting with word-syntax character:
312 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'"))
313 "Regexp matching doc string references to symbols.
314
315 The words preceding the quoted symbol can be used in doc strings to
316 distinguish references to variables, functions and symbols.")
317
318 (defvar help-xref-mule-regexp nil
319 "Regexp matching doc string references to MULE-related keywords.
320
321 It is usually nil, and is temporarily bound to an appropriate regexp
322 when help commands related to multilingual environment (e.g.,
323 `describe-coding-system') are invoked.")
324
325
326 (defconst help-xref-info-regexp
327 (purecopy "\\<[Ii]nfo[ \t\n]+\\(node\\|anchor\\)[ \t\n]+`\\([^']+\\)'")
328 "Regexp matching doc string references to an Info node.")
329
330 (defconst help-xref-url-regexp
331 (purecopy "\\<[Uu][Rr][Ll][ \t\n]+`\\([^']+\\)'")
332 "Regexp matching doc string references to a URL.")
333
334 ;;;###autoload
335 (defun help-setup-xref (item interactive-p)
336 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
337
338 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
339 buffer after following a reference. INTERACTIVE-P is non-nil if the
340 calling command was invoked interactively. In this case the stack of
341 items for help buffer \"back\" buttons is cleared.
342
343 This should be called very early, before the output buffer is cleared,
344 because we want to record the \"previous\" position of point so we can
345 restore it properly when going back."
346 (with-current-buffer (help-buffer)
347 (when help-xref-stack-item
348 (push (cons (point) help-xref-stack-item) help-xref-stack)
349 (setq help-xref-forward-stack nil))
350 (when interactive-p
351 (let ((tail (nthcdr 10 help-xref-stack)))
352 ;; Truncate the stack.
353 (if tail (setcdr tail nil))))
354 (setq help-xref-stack-item item)))
355
356 (defvar help-xref-following nil
357 "Non-nil when following a help cross-reference.")
358
359 ;;;###autoload
360 (defun help-buffer ()
361 "Return a buffer for inserting help.
362 If `help-xref-following' is non-nil, this is the current buffer.
363 Otherwise, it is the buffer named *Help*. If no such buffer
364 currently exists, it is created."
365 (buffer-name ;for with-output-to-temp-buffer
366 (if help-xref-following
367 (current-buffer)
368 (get-buffer-create "*Help*"))))
369
370 (defvar help-xref-override-view-map
371 (let ((map (make-sparse-keymap)))
372 (set-keymap-parent map view-mode-map)
373 (define-key map "\r" nil)
374 map)
375 "Replacement keymap for `view-mode' in help buffers.")
376
377 ;;;###autoload
378 (defun help-make-xrefs (&optional buffer)
379 "Parse and hyperlink documentation cross-references in the given BUFFER.
380
381 Find cross-reference information in a buffer and activate such cross
382 references for selection with `help-follow'. Cross-references have
383 the canonical form `...' and the type of reference may be
384 disambiguated by the preceding word(s) used in
385 `help-xref-symbol-regexp'. Faces only get cross-referenced if
386 preceded or followed by the word `face'. Variables without
387 variable documentation do not get cross-referenced, unless
388 preceded by the word `variable' or `option'.
389
390 If the variable `help-xref-mule-regexp' is non-nil, find also
391 cross-reference information related to multilingual environment
392 \(e.g., coding-systems). This variable is also used to disambiguate
393 the type of reference as the same way as `help-xref-symbol-regexp'.
394
395 A special reference `back' is made to return back through a stack of
396 help buffers. Variable `help-back-label' specifies the text for
397 that."
398 (interactive "b")
399 (save-excursion
400 (set-buffer (or buffer (current-buffer)))
401 (goto-char (point-min))
402 ;; Skip the header-type info, though it might be useful to parse
403 ;; it at some stage (e.g. "function in `library'").
404 (forward-paragraph)
405 (let ((old-modified (buffer-modified-p)))
406 (let ((stab (syntax-table))
407 (case-fold-search t)
408 (inhibit-read-only t))
409 (set-syntax-table emacs-lisp-mode-syntax-table)
410 ;; The following should probably be abstracted out.
411 (unwind-protect
412 (progn
413 ;; Info references
414 (save-excursion
415 (while (re-search-forward help-xref-info-regexp nil t)
416 (let ((data (match-string 2)))
417 (save-match-data
418 (unless (string-match "^([^)]+)" data)
419 (setq data (concat "(emacs)" data))))
420 (help-xref-button 2 'help-info data))))
421 ;; URLs
422 (save-excursion
423 (while (re-search-forward help-xref-url-regexp nil t)
424 (let ((data (match-string 1)))
425 (help-xref-button 1 'help-url data))))
426 ;; Mule related keywords. Do this before trying
427 ;; `help-xref-symbol-regexp' because some of Mule
428 ;; keywords have variable or function definitions.
429 (if help-xref-mule-regexp
430 (save-excursion
431 (while (re-search-forward help-xref-mule-regexp nil t)
432 (let* ((data (match-string 7))
433 (sym (intern-soft data)))
434 (cond
435 ((match-string 3) ; coding system
436 (and sym (coding-system-p sym)
437 (help-xref-button 6 'help-coding-system sym)))
438 ((match-string 4) ; input method
439 (and (assoc data input-method-alist)
440 (help-xref-button 7 'help-input-method data)))
441 ((or (match-string 5) (match-string 6)) ; charset
442 (and sym (charsetp sym)
443 (help-xref-button 7 'help-character-set sym)))
444 ((assoc data input-method-alist)
445 (help-xref-button 7 'help-character-set data))
446 ((and sym (coding-system-p sym))
447 (help-xref-button 7 'help-coding-system sym))
448 ((and sym (charsetp sym))
449 (help-xref-button 7 'help-character-set sym)))))))
450 ;; Quoted symbols
451 (save-excursion
452 (while (re-search-forward help-xref-symbol-regexp nil t)
453 (let* ((data (match-string 8))
454 (sym (intern-soft data)))
455 (if sym
456 (cond
457 ((match-string 3) ; `variable' &c
458 (and (or (boundp sym) ; `variable' doesn't ensure
459 ; it's actually bound
460 (get sym 'variable-documentation))
461 (help-xref-button 8 'help-variable sym)))
462 ((match-string 4) ; `function' &c
463 (and (fboundp sym) ; similarly
464 (help-xref-button 8 'help-function sym)))
465 ((match-string 5) ; `face'
466 (and (facep sym)
467 (help-xref-button 8 'help-face sym)))
468 ((match-string 6)) ; nothing for `symbol'
469 ((match-string 7)
470 ;;; this used:
471 ;;; #'(lambda (arg)
472 ;;; (let ((location
473 ;;; (find-function-noselect arg)))
474 ;;; (pop-to-buffer (car location))
475 ;;; (goto-char (cdr location))))
476 (help-xref-button 8 'help-function-def sym))
477 ((and
478 (facep sym)
479 (save-match-data (looking-at "[ \t\n]+face\\W")))
480 (help-xref-button 8 'help-face sym))
481 ((and (or (boundp sym)
482 (get sym 'variable-documentation))
483 (fboundp sym))
484 ;; We can't intuit whether to use the
485 ;; variable or function doc -- supply both.
486 (help-xref-button 8 'help-symbol sym))
487 ((and
488 (or (boundp sym)
489 (get sym 'variable-documentation))
490 (or
491 (documentation-property
492 sym 'variable-documentation)
493 (condition-case nil
494 (documentation-property
495 (indirect-variable sym)
496 'variable-documentation)
497 (cyclic-variable-indirection nil))))
498 (help-xref-button 8 'help-variable sym))
499 ((fboundp sym)
500 (help-xref-button 8 'help-function sym)))))))
501 ;; An obvious case of a key substitution:
502 (save-excursion
503 (while (re-search-forward
504 ;; Assume command name is only word and symbol
505 ;; characters to get things like `use M-x foo->bar'.
506 ;; Command required to end with word constituent
507 ;; to avoid `.' at end of a sentence.
508 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|\\s_\\)*\\sw\\)" nil t)
509 (let ((sym (intern-soft (match-string 1))))
510 (if (fboundp sym)
511 (help-xref-button 1 'help-function sym)))))
512 ;; Look for commands in whole keymap substitutions:
513 (save-excursion
514 ;; Make sure to find the first keymap.
515 (goto-char (point-min))
516 ;; Find a header and the column at which the command
517 ;; name will be found.
518
519 ;; If the keymap substitution isn't the last thing in
520 ;; the doc string, and if there is anything on the
521 ;; same line after it, this code won't recognize the end of it.
522 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
523 nil t)
524 (let ((col (- (match-end 1) (match-beginning 1))))
525 (while
526 (and (not (eobp))
527 ;; Stop at a pair of blank lines.
528 (not (looking-at "\n\\s-*\n")))
529 ;; Skip a single blank line.
530 (and (eolp) (forward-line))
531 (end-of-line)
532 (skip-chars-backward "^ \t\n")
533 (if (and (>= (current-column) col)
534 (looking-at "\\(\\sw\\|\\s_\\)+$"))
535 (let ((sym (intern-soft (match-string 0))))
536 (if (fboundp sym)
537 (help-xref-button 0 'help-function sym))))
538 (forward-line))))))
539 (set-syntax-table stab))
540 ;; Delete extraneous newlines at the end of the docstring
541 (goto-char (point-max))
542 (while (and (not (bobp)) (bolp))
543 (delete-char -1))
544 (insert "\n")
545 (when (or help-xref-stack help-xref-forward-stack)
546 (insert "\n"))
547 ;; Make a back-reference in this buffer if appropriate.
548 (when help-xref-stack
549 (help-insert-xref-button help-back-label 'help-back
550 (current-buffer)))
551 ;; Make a forward-reference in this buffer if appropriate.
552 (when help-xref-forward-stack
553 (when help-xref-stack
554 (insert "\t"))
555 (help-insert-xref-button help-forward-label 'help-forward
556 (current-buffer)))
557 (when (or help-xref-stack help-xref-forward-stack)
558 (insert "\n")))
559 ;; View mode steals RET from us.
560 (set (make-local-variable 'minor-mode-overriding-map-alist)
561 (list (cons 'view-mode help-xref-override-view-map)))
562 (set-buffer-modified-p old-modified))))
563
564 ;;;###autoload
565 (defun help-xref-button (match-number type &rest args)
566 "Make a hyperlink for cross-reference text previously matched.
567 MATCH-NUMBER is the subexpression of interest in the last matched
568 regexp. TYPE is the type of button to use. Any remaining arguments are
569 passed to the button's help-function when it is invoked.
570 See `help-make-xrefs'."
571 ;; Don't mung properties we've added specially in some instances.
572 (unless (button-at (match-beginning match-number))
573 (make-text-button (match-beginning match-number)
574 (match-end match-number)
575 'type type 'help-args args)))
576
577 ;;;###autoload
578 (defun help-insert-xref-button (string type &rest args)
579 "Insert STRING and make a hyperlink from cross-reference text on it.
580 TYPE is the type of button to use. Any remaining arguments are passed
581 to the button's help-function when it is invoked.
582 See `help-make-xrefs'."
583 (unless (button-at (point))
584 (insert-text-button string 'type type 'help-args args)))
585
586 ;;;###autoload
587 (defun help-xref-on-pp (from to)
588 "Add xrefs for symbols in `pp's output between FROM and TO."
589 (if (> (- to from) 5000) nil
590 (with-syntax-table emacs-lisp-mode-syntax-table
591 (save-excursion
592 (save-restriction
593 (narrow-to-region from to)
594 (goto-char (point-min))
595 (condition-case nil
596 (while (not (eobp))
597 (cond
598 ((looking-at "\"") (forward-sexp 1))
599 ((looking-at "#<") (search-forward ">" nil 'move))
600 ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)")
601 (let* ((sym (intern-soft (match-string 1)))
602 (type (cond ((fboundp sym) 'help-function)
603 ((or (memq sym '(t nil))
604 (keywordp sym))
605 nil)
606 ((and sym
607 (or (boundp sym)
608 (get sym
609 'variable-documentation)))
610 'help-variable))))
611 (when type (help-xref-button 1 type sym)))
612 (goto-char (match-end 1)))
613 (t (forward-char 1))))
614 (error nil)))))))
615
616 \f
617 ;; Additional functions for (re-)creating types of help buffers.
618 (defun help-xref-interned (symbol)
619 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
620 Both variable, function and face documentation are extracted into a single
621 help buffer."
622 (with-current-buffer (help-buffer)
623 ;; Push the previous item on the stack before clobbering the output buffer.
624 (help-setup-xref nil nil)
625 (let ((facedoc (when (facep symbol)
626 ;; Don't record the current entry in the stack.
627 (setq help-xref-stack-item nil)
628 (describe-face symbol)))
629 (fdoc (when (fboundp symbol)
630 ;; Don't record the current entry in the stack.
631 (setq help-xref-stack-item nil)
632 (describe-function symbol)))
633 (sdoc (when (or (boundp symbol)
634 (get symbol 'variable-documentation))
635 ;; Don't record the current entry in the stack.
636 (setq help-xref-stack-item nil)
637 (describe-variable symbol))))
638 (cond
639 (sdoc
640 ;; We now have a help buffer on the variable.
641 ;; Insert the function and face text before it.
642 (when (or fdoc facedoc)
643 (goto-char (point-min))
644 (let ((inhibit-read-only t))
645 (when fdoc
646 (insert fdoc "\n\n")
647 (when facedoc
648 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
649 " is also a " "face." "\n\n")))
650 (when facedoc
651 (insert facedoc "\n\n"))
652 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
653 " is also a " "variable." "\n\n"))
654 ;; Don't record the `describe-variable' item in the stack.
655 (setq help-xref-stack-item nil)
656 (help-setup-xref (list #'help-xref-interned symbol) nil)))
657 (fdoc
658 ;; We now have a help buffer on the function.
659 ;; Insert face text before it.
660 (when facedoc
661 (goto-char (point-max))
662 (let ((inhibit-read-only t))
663 (insert "\n\n" (make-string 30 ?-) "\n\n" (symbol-name symbol)
664 " is also a " "face." "\n\n" facedoc))
665 ;; Don't record the `describe-function' item in the stack.
666 (setq help-xref-stack-item nil)
667 (help-setup-xref (list #'help-xref-interned symbol) nil)))))))
668
669 \f
670 ;; Navigation/hyperlinking with xrefs
671
672 (defun help-xref-go-back (buffer)
673 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
674 (let (item position method args)
675 (with-current-buffer buffer
676 (push (cons (point) help-xref-stack-item) help-xref-forward-stack)
677 (when help-xref-stack
678 (setq item (pop help-xref-stack)
679 ;; Clear the current item so that it won't get pushed
680 ;; by the function we're about to call. TODO: We could also
681 ;; push it onto a "forward" stack and add a `forw' button.
682 help-xref-stack-item nil
683 position (car item)
684 method (cadr item)
685 args (cddr item))))
686 (apply method args)
687 (with-current-buffer buffer
688 (if (get-buffer-window buffer)
689 (set-window-point (get-buffer-window buffer) position)
690 (goto-char position)))))
691
692 (defun help-xref-go-forward (buffer)
693 "From BUFFER, go forward to next help buffer."
694 (let (item position method args)
695 (with-current-buffer buffer
696 (push (cons (point) help-xref-stack-item) help-xref-stack)
697 (when help-xref-forward-stack
698 (setq item (pop help-xref-forward-stack)
699 ;; Clear the current item so that it won't get pushed
700 ;; by the function we're about to call. TODO: We could also
701 ;; push it onto a "forward" stack and add a `forw' button.
702 help-xref-stack-item nil
703 position (car item)
704 method (cadr item)
705 args (cddr item))))
706 (apply method args)
707 (with-current-buffer buffer
708 (if (get-buffer-window buffer)
709 (set-window-point (get-buffer-window buffer) position)
710 (goto-char position)))))
711
712 (defun help-go-back ()
713 "Go back to previous topic in this help buffer."
714 (interactive)
715 (if help-xref-stack
716 (help-xref-go-back (current-buffer))
717 (error "No previous help buffer")))
718
719 (defun help-go-forward ()
720 "Go back to next topic in this help buffer."
721 (interactive)
722 (if help-xref-forward-stack
723 (help-xref-go-forward (current-buffer))
724 (error "No next help buffer")))
725
726 (defun help-do-xref (pos function args)
727 "Call the help cross-reference function FUNCTION with args ARGS.
728 Things are set up properly so that the resulting help-buffer has
729 a proper [back] button."
730 ;; There is a reference at point. Follow it.
731 (let ((help-xref-following t))
732 (apply function args)))
733
734 ;; The doc string is meant to explain what buttons do.
735 (defun help-follow-mouse ()
736 "Follow the cross-reference that you click on."
737 (interactive)
738 (error "No cross-reference here"))
739
740 ;; The doc string is meant to explain what buttons do.
741 (defun help-follow ()
742 "Follow cross-reference at point.
743
744 For the cross-reference format, see `help-make-xrefs'."
745 (interactive)
746 (error "No cross-reference here"))
747
748 (defun help-follow-symbol (&optional pos)
749 "In help buffer, show docs for symbol at POS, defaulting to point.
750 Show all docs for that symbol as either a variable, function or face."
751 (interactive "d")
752 (unless pos
753 (setq pos (point)))
754 ;; check if the symbol under point is a function, variable or face
755 (let ((sym
756 (intern
757 (save-excursion
758 (goto-char pos) (skip-syntax-backward "w_")
759 (buffer-substring (point)
760 (progn (skip-syntax-forward "w_")
761 (point)))))))
762 (when (or (boundp sym)
763 (get sym 'variable-documentation)
764 (fboundp sym) (facep sym))
765 (help-do-xref pos #'help-xref-interned (list sym)))))
766
767 (defun help-insert-string (string)
768 "Insert STRING to the help buffer and install xref info for it.
769 This function can be used to restore the old contents of the help buffer
770 when going back to the previous topic in the xref stack. It is needed
771 in case when it is impossible to recompute the old contents of the
772 help buffer by other means."
773 (setq help-xref-stack-item (list #'help-insert-string string))
774 (with-output-to-temp-buffer (help-buffer)
775 (insert string)))
776
777 (provide 'help-mode)
778
779 ;; arch-tag: 850954ae-3725-4cb4-8e91-0bf6d52d6b0b
780 ;;; help-mode.el ends here