* mark.texi (Mark): Clarify introduction.
[emacs.git] / lisp / help-mode.el
blob91fb4f9925754d9e3695ff209af4516015f05c9e
1 ;;; help-mode.el --- `help-mode' used by *Help* buffers
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
26 ;; Defines `help-mode', which is the mode used by *Help* buffers, and
27 ;; associated support machinery, such as adding hyperlinks, etc.,
29 ;;; Code:
31 (require 'button)
32 (require 'view)
33 (eval-when-compile (require 'easymenu))
35 (defvar help-mode-map (make-sparse-keymap)
36 "Keymap for help mode.")
38 (set-keymap-parent help-mode-map button-buffer-map)
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)
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"]))
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)
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)
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)
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)
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)
92 (defcustom help-mode-hook nil
93 "Hook run by `help-mode'."
94 :type 'hook
95 :group 'help)
97 ;; Button types used by help
99 (define-button-type 'help-xref
100 'follow-link t
101 'action #'help-button-action)
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)))
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.
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"))
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"))
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"))
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"))
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"))
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"))
143 ;; make some more ideosyncratic button types
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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)
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))))
259 (run-mode-hooks 'help-mode-hook))
261 ;;;###autoload
262 (defun help-mode-setup ()
263 (help-mode)
264 (setq buffer-read-only nil))
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))
281 (setq view-return-to-alist
282 (cons (cons (selected-window) help-return-method)
283 view-return-to-alist)))))
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))))
291 ;; Grokking cross-reference information in doc strings and
292 ;; hyperlinking it.
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.
298 (defvar help-back-label (purecopy "[back]")
299 "Label to use by `help-make-xrefs' for the go-back reference.")
301 (defvar help-forward-label (purecopy "[forward]")
302 "Label to use by `help-make-xrefs' for the go-forward reference.")
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.
315 The words preceding the quoted symbol can be used in doc strings to
316 distinguish references to variables, functions and symbols.")
318 (defvar help-xref-mule-regexp nil
319 "Regexp matching doc string references to MULE-related keywords.
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.")
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.")
330 (defconst help-xref-url-regexp
331 (purecopy "\\<[Uu][Rr][Ll][ \t\n]+`\\([^']+\\)'")
332 "Regexp matching doc string references to a URL.")
334 ;;;###autoload
335 (defun help-setup-xref (item interactive-p)
336 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
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.
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)))
356 (defvar help-xref-following nil
357 "Non-nil when following a help cross-reference.")
359 ;;;###autoload
360 (defun help-buffer ()
361 "Return the name of a buffer for inserting help.
362 If `help-xref-following' is non-nil, this is the name of the
363 current buffer.
364 Otherwise, it is *Help*; if no buffer with that name currently
365 exists, it is created."
366 (buffer-name ;for with-output-to-temp-buffer
367 (if help-xref-following
368 (current-buffer)
369 (get-buffer-create "*Help*"))))
371 (defvar help-xref-override-view-map
372 (let ((map (make-sparse-keymap)))
373 (set-keymap-parent map view-mode-map)
374 (define-key map "\r" nil)
375 map)
376 "Replacement keymap for `view-mode' in help buffers.")
378 ;;;###autoload
379 (defun help-make-xrefs (&optional buffer)
380 "Parse and hyperlink documentation cross-references in the given BUFFER.
382 Find cross-reference information in a buffer and activate such cross
383 references for selection with `help-follow'. Cross-references have
384 the canonical form `...' and the type of reference may be
385 disambiguated by the preceding word(s) used in
386 `help-xref-symbol-regexp'. Faces only get cross-referenced if
387 preceded or followed by the word `face'. Variables without
388 variable documentation do not get cross-referenced, unless
389 preceded by the word `variable' or `option'.
391 If the variable `help-xref-mule-regexp' is non-nil, find also
392 cross-reference information related to multilingual environment
393 \(e.g., coding-systems). This variable is also used to disambiguate
394 the type of reference as the same way as `help-xref-symbol-regexp'.
396 A special reference `back' is made to return back through a stack of
397 help buffers. Variable `help-back-label' specifies the text for
398 that."
399 (interactive "b")
400 (save-excursion
401 (set-buffer (or buffer (current-buffer)))
402 (goto-char (point-min))
403 ;; Skip the header-type info, though it might be useful to parse
404 ;; it at some stage (e.g. "function in `library'").
405 (forward-paragraph)
406 (let ((old-modified (buffer-modified-p)))
407 (let ((stab (syntax-table))
408 (case-fold-search t)
409 (inhibit-read-only t))
410 (set-syntax-table emacs-lisp-mode-syntax-table)
411 ;; The following should probably be abstracted out.
412 (unwind-protect
413 (progn
414 ;; Info references
415 (save-excursion
416 (while (re-search-forward help-xref-info-regexp nil t)
417 (let ((data (match-string 2)))
418 (save-match-data
419 (unless (string-match "^([^)]+)" data)
420 (setq data (concat "(emacs)" data))))
421 (help-xref-button 2 'help-info data))))
422 ;; URLs
423 (save-excursion
424 (while (re-search-forward help-xref-url-regexp nil t)
425 (let ((data (match-string 1)))
426 (help-xref-button 1 'help-url data))))
427 ;; Mule related keywords. Do this before trying
428 ;; `help-xref-symbol-regexp' because some of Mule
429 ;; keywords have variable or function definitions.
430 (if help-xref-mule-regexp
431 (save-excursion
432 (while (re-search-forward help-xref-mule-regexp nil t)
433 (let* ((data (match-string 7))
434 (sym (intern-soft data)))
435 (cond
436 ((match-string 3) ; coding system
437 (and sym (coding-system-p sym)
438 (help-xref-button 6 'help-coding-system sym)))
439 ((match-string 4) ; input method
440 (and (assoc data input-method-alist)
441 (help-xref-button 7 'help-input-method data)))
442 ((or (match-string 5) (match-string 6)) ; charset
443 (and sym (charsetp sym)
444 (help-xref-button 7 'help-character-set sym)))
445 ((assoc data input-method-alist)
446 (help-xref-button 7 'help-character-set data))
447 ((and sym (coding-system-p sym))
448 (help-xref-button 7 'help-coding-system sym))
449 ((and sym (charsetp sym))
450 (help-xref-button 7 'help-character-set sym)))))))
451 ;; Quoted symbols
452 (save-excursion
453 (while (re-search-forward help-xref-symbol-regexp nil t)
454 (let* ((data (match-string 8))
455 (sym (intern-soft data)))
456 (if sym
457 (cond
458 ((match-string 3) ; `variable' &c
459 (and (or (boundp sym) ; `variable' doesn't ensure
460 ; it's actually bound
461 (get sym 'variable-documentation))
462 (help-xref-button 8 'help-variable sym)))
463 ((match-string 4) ; `function' &c
464 (and (fboundp sym) ; similarly
465 (help-xref-button 8 'help-function sym)))
466 ((match-string 5) ; `face'
467 (and (facep sym)
468 (help-xref-button 8 'help-face sym)))
469 ((match-string 6)) ; nothing for `symbol'
470 ((match-string 7)
471 ;;; this used:
472 ;;; #'(lambda (arg)
473 ;;; (let ((location
474 ;;; (find-function-noselect arg)))
475 ;;; (pop-to-buffer (car location))
476 ;;; (goto-char (cdr location))))
477 (help-xref-button 8 'help-function-def sym))
478 ((and
479 (facep sym)
480 (save-match-data (looking-at "[ \t\n]+face\\W")))
481 (help-xref-button 8 'help-face sym))
482 ((and (or (boundp sym)
483 (get sym 'variable-documentation))
484 (fboundp sym))
485 ;; We can't intuit whether to use the
486 ;; variable or function doc -- supply both.
487 (help-xref-button 8 'help-symbol sym))
488 ((and
489 (or (boundp sym)
490 (get sym 'variable-documentation))
492 (documentation-property
493 sym 'variable-documentation)
494 (condition-case nil
495 (documentation-property
496 (indirect-variable sym)
497 'variable-documentation)
498 (cyclic-variable-indirection nil))))
499 (help-xref-button 8 'help-variable sym))
500 ((fboundp sym)
501 (help-xref-button 8 'help-function sym)))))))
502 ;; An obvious case of a key substitution:
503 (save-excursion
504 (while (re-search-forward
505 ;; Assume command name is only word and symbol
506 ;; characters to get things like `use M-x foo->bar'.
507 ;; Command required to end with word constituent
508 ;; to avoid `.' at end of a sentence.
509 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|\\s_\\)*\\sw\\)" nil t)
510 (let ((sym (intern-soft (match-string 1))))
511 (if (fboundp sym)
512 (help-xref-button 1 'help-function sym)))))
513 ;; Look for commands in whole keymap substitutions:
514 (save-excursion
515 ;; Make sure to find the first keymap.
516 (goto-char (point-min))
517 ;; Find a header and the column at which the command
518 ;; name will be found.
520 ;; If the keymap substitution isn't the last thing in
521 ;; the doc string, and if there is anything on the
522 ;; same line after it, this code won't recognize the end of it.
523 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
524 nil t)
525 (let ((col (- (match-end 1) (match-beginning 1))))
526 (while
527 (and (not (eobp))
528 ;; Stop at a pair of blank lines.
529 (not (looking-at "\n\\s-*\n")))
530 ;; Skip a single blank line.
531 (and (eolp) (forward-line))
532 (end-of-line)
533 (skip-chars-backward "^ \t\n")
534 (if (and (>= (current-column) col)
535 (looking-at "\\(\\sw\\|\\s_\\)+$"))
536 (let ((sym (intern-soft (match-string 0))))
537 (if (fboundp sym)
538 (help-xref-button 0 'help-function sym))))
539 (forward-line))))))
540 (set-syntax-table stab))
541 ;; Delete extraneous newlines at the end of the docstring
542 (goto-char (point-max))
543 (while (and (not (bobp)) (bolp))
544 (delete-char -1))
545 (insert "\n")
546 (when (or help-xref-stack help-xref-forward-stack)
547 (insert "\n"))
548 ;; Make a back-reference in this buffer if appropriate.
549 (when help-xref-stack
550 (help-insert-xref-button help-back-label 'help-back
551 (current-buffer)))
552 ;; Make a forward-reference in this buffer if appropriate.
553 (when help-xref-forward-stack
554 (when help-xref-stack
555 (insert "\t"))
556 (help-insert-xref-button help-forward-label 'help-forward
557 (current-buffer)))
558 (when (or help-xref-stack help-xref-forward-stack)
559 (insert "\n")))
560 ;; View mode steals RET from us.
561 (set (make-local-variable 'minor-mode-overriding-map-alist)
562 (list (cons 'view-mode help-xref-override-view-map)))
563 (set-buffer-modified-p old-modified))))
565 ;;;###autoload
566 (defun help-xref-button (match-number type &rest args)
567 "Make a hyperlink for cross-reference text previously matched.
568 MATCH-NUMBER is the subexpression of interest in the last matched
569 regexp. TYPE is the type of button to use. Any remaining arguments are
570 passed to the button's help-function when it is invoked.
571 See `help-make-xrefs'."
572 ;; Don't mung properties we've added specially in some instances.
573 (unless (button-at (match-beginning match-number))
574 (make-text-button (match-beginning match-number)
575 (match-end match-number)
576 'type type 'help-args args)))
578 ;;;###autoload
579 (defun help-insert-xref-button (string type &rest args)
580 "Insert STRING and make a hyperlink from cross-reference text on it.
581 TYPE is the type of button to use. Any remaining arguments are passed
582 to the button's help-function when it is invoked.
583 See `help-make-xrefs'."
584 (unless (button-at (point))
585 (insert-text-button string 'type type 'help-args args)))
587 ;;;###autoload
588 (defun help-xref-on-pp (from to)
589 "Add xrefs for symbols in `pp's output between FROM and TO."
590 (if (> (- to from) 5000) nil
591 (with-syntax-table emacs-lisp-mode-syntax-table
592 (save-excursion
593 (save-restriction
594 (narrow-to-region from to)
595 (goto-char (point-min))
596 (condition-case nil
597 (while (not (eobp))
598 (cond
599 ((looking-at "\"") (forward-sexp 1))
600 ((looking-at "#<") (search-forward ">" nil 'move))
601 ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)")
602 (let* ((sym (intern-soft (match-string 1)))
603 (type (cond ((fboundp sym) 'help-function)
604 ((or (memq sym '(t nil))
605 (keywordp sym))
606 nil)
607 ((and sym
608 (or (boundp sym)
609 (get sym
610 'variable-documentation)))
611 'help-variable))))
612 (when type (help-xref-button 1 type sym)))
613 (goto-char (match-end 1)))
614 (t (forward-char 1))))
615 (error nil)))))))
618 ;; Additional functions for (re-)creating types of help buffers.
619 (defun help-xref-interned (symbol)
620 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
621 Both variable, function and face documentation are extracted into a single
622 help buffer."
623 (with-current-buffer (help-buffer)
624 ;; Push the previous item on the stack before clobbering the output buffer.
625 (help-setup-xref nil nil)
626 (let ((facedoc (when (facep symbol)
627 ;; Don't record the current entry in the stack.
628 (setq help-xref-stack-item nil)
629 (describe-face symbol)))
630 (fdoc (when (fboundp symbol)
631 ;; Don't record the current entry in the stack.
632 (setq help-xref-stack-item nil)
633 (describe-function symbol)))
634 (sdoc (when (or (boundp symbol)
635 (get symbol 'variable-documentation))
636 ;; Don't record the current entry in the stack.
637 (setq help-xref-stack-item nil)
638 (describe-variable symbol))))
639 (cond
640 (sdoc
641 ;; We now have a help buffer on the variable.
642 ;; Insert the function and face text before it.
643 (when (or fdoc facedoc)
644 (goto-char (point-min))
645 (let ((inhibit-read-only t))
646 (when fdoc
647 (insert fdoc "\n\n")
648 (when facedoc
649 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
650 " is also a " "face." "\n\n")))
651 (when facedoc
652 (insert facedoc "\n\n"))
653 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
654 " is also a " "variable." "\n\n"))
655 ;; Don't record the `describe-variable' item in the stack.
656 (setq help-xref-stack-item nil)
657 (help-setup-xref (list #'help-xref-interned symbol) nil)))
658 (fdoc
659 ;; We now have a help buffer on the function.
660 ;; Insert face text before it.
661 (when facedoc
662 (goto-char (point-max))
663 (let ((inhibit-read-only t))
664 (insert "\n\n" (make-string 30 ?-) "\n\n" (symbol-name symbol)
665 " is also a " "face." "\n\n" facedoc))
666 ;; Don't record the `describe-function' item in the stack.
667 (setq help-xref-stack-item nil)
668 (help-setup-xref (list #'help-xref-interned symbol) nil)))))))
671 ;; Navigation/hyperlinking with xrefs
673 (defun help-xref-go-back (buffer)
674 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
675 (let (item position method args)
676 (with-current-buffer buffer
677 (push (cons (point) help-xref-stack-item) help-xref-forward-stack)
678 (when help-xref-stack
679 (setq item (pop help-xref-stack)
680 ;; Clear the current item so that it won't get pushed
681 ;; by the function we're about to call. TODO: We could also
682 ;; push it onto a "forward" stack and add a `forw' button.
683 help-xref-stack-item nil
684 position (car item)
685 method (cadr item)
686 args (cddr item))))
687 (apply method args)
688 (with-current-buffer buffer
689 (if (get-buffer-window buffer)
690 (set-window-point (get-buffer-window buffer) position)
691 (goto-char position)))))
693 (defun help-xref-go-forward (buffer)
694 "From BUFFER, go forward to next help buffer."
695 (let (item position method args)
696 (with-current-buffer buffer
697 (push (cons (point) help-xref-stack-item) help-xref-stack)
698 (when help-xref-forward-stack
699 (setq item (pop help-xref-forward-stack)
700 ;; Clear the current item so that it won't get pushed
701 ;; by the function we're about to call. TODO: We could also
702 ;; push it onto a "forward" stack and add a `forw' button.
703 help-xref-stack-item nil
704 position (car item)
705 method (cadr item)
706 args (cddr item))))
707 (apply method args)
708 (with-current-buffer buffer
709 (if (get-buffer-window buffer)
710 (set-window-point (get-buffer-window buffer) position)
711 (goto-char position)))))
713 (defun help-go-back ()
714 "Go back to previous topic in this help buffer."
715 (interactive)
716 (if help-xref-stack
717 (help-xref-go-back (current-buffer))
718 (error "No previous help buffer")))
720 (defun help-go-forward ()
721 "Go back to next topic in this help buffer."
722 (interactive)
723 (if help-xref-forward-stack
724 (help-xref-go-forward (current-buffer))
725 (error "No next help buffer")))
727 (defun help-do-xref (pos function args)
728 "Call the help cross-reference function FUNCTION with args ARGS.
729 Things are set up properly so that the resulting help-buffer has
730 a proper [back] button."
731 ;; There is a reference at point. Follow it.
732 (let ((help-xref-following t))
733 (apply function args)))
735 ;; The doc string is meant to explain what buttons do.
736 (defun help-follow-mouse ()
737 "Follow the cross-reference that you click on."
738 (interactive)
739 (error "No cross-reference here"))
741 ;; The doc string is meant to explain what buttons do.
742 (defun help-follow ()
743 "Follow cross-reference at point.
745 For the cross-reference format, see `help-make-xrefs'."
746 (interactive)
747 (error "No cross-reference here"))
749 (defun help-follow-symbol (&optional pos)
750 "In help buffer, show docs for symbol at POS, defaulting to point.
751 Show all docs for that symbol as either a variable, function or face."
752 (interactive "d")
753 (unless pos
754 (setq pos (point)))
755 ;; check if the symbol under point is a function, variable or face
756 (let ((sym
757 (intern
758 (save-excursion
759 (goto-char pos) (skip-syntax-backward "w_")
760 (buffer-substring (point)
761 (progn (skip-syntax-forward "w_")
762 (point)))))))
763 (when (or (boundp sym)
764 (get sym 'variable-documentation)
765 (fboundp sym) (facep sym))
766 (help-do-xref pos #'help-xref-interned (list sym)))))
768 (defun help-insert-string (string)
769 "Insert STRING to the help buffer and install xref info for it.
770 This function can be used to restore the old contents of the help buffer
771 when going back to the previous topic in the xref stack. It is needed
772 in case when it is impossible to recompute the old contents of the
773 help buffer by other means."
774 (setq help-xref-stack-item (list #'help-insert-string string))
775 (with-output-to-temp-buffer (help-buffer)
776 (insert string)))
778 (provide 'help-mode)
780 ;; arch-tag: 850954ae-3725-4cb4-8e91-0bf6d52d6b0b
781 ;;; help-mode.el ends here