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