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