(cvs-vc-command-advice): Fix a typo in code (file->files).
[emacs.git] / lisp / help-mode.el
blob1435eb019ec6d393c0e2dc20d460300de1839e4e
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 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, or (at your option)
14 ;; 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; 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.
26 ;;; Commentary:
28 ;; Defines `help-mode', which is the mode used by *Help* buffers, and
29 ;; associated support machinery, such as adding hyperlinks, etc.,
31 ;;; Code:
33 (require 'button)
34 (require 'view)
36 (defvar help-mode-map (make-sparse-keymap)
37 "Keymap for help mode.")
39 (set-keymap-parent help-mode-map button-buffer-map)
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)
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)
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)
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)
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)
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)
79 (defcustom help-mode-hook nil
80 "Hook run by `help-mode'."
81 :type 'hook
82 :group 'help)
84 ;; Button types used by help
86 (define-button-type 'help-xref
87 'follow-link t
88 'action #'help-button-action)
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)))
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.
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"))
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"))
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"))
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"))
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"))
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"))
130 ;; make some more ideosyncratic button types
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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 (view-mode)
225 (make-local-variable 'view-no-disable-on-exit)
226 (setq view-no-disable-on-exit t)
227 (setq view-exit-action (lambda (buffer)
228 (or (window-minibuffer-p (selected-window))
229 (one-window-p t)
230 (delete-window))))
231 (run-mode-hooks 'help-mode-hook))
233 ;;;###autoload
234 (defun help-mode-setup ()
235 (help-mode)
236 (setq buffer-read-only nil))
238 ;;;###autoload
239 (defun help-mode-finish ()
240 (let ((entry (assq (selected-window) view-return-to-alist)))
241 (if entry
242 ;; When entering Help mode from the Help window,
243 ;; such as by following a link, preserve the same
244 ;; meaning for the q command.
245 ;; (setcdr entry (cons (selected-window) help-return-method))
247 (setq view-return-to-alist
248 (cons (cons (selected-window) help-return-method)
249 view-return-to-alist))))
250 (when (eq major-mode 'help-mode)
251 ;; View mode's read-only status of existing *Help* buffer is lost
252 ;; by with-output-to-temp-buffer.
253 (toggle-read-only 1)
254 (help-make-xrefs (current-buffer))))
256 ;; Grokking cross-reference information in doc strings and
257 ;; hyperlinking it.
259 ;; This may have some scope for extension and the same or something
260 ;; similar should be done for widget doc strings, which currently use
261 ;; another mechanism.
263 (defvar help-back-label (purecopy "[back]")
264 "Label to use by `help-make-xrefs' for the go-back reference.")
266 (defvar help-forward-label (purecopy "[forward]")
267 "Label to use by `help-make-xrefs' for the go-forward reference.")
269 (defconst help-xref-symbol-regexp
270 (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|" ; Link to var
271 "\\(function\\|command\\)\\|" ; Link to function
272 "\\(face\\)\\|" ; Link to face
273 "\\(symbol\\|program\\|property\\)\\|" ; Don't link
274 "\\(source \\(?:code \\)?\\(?:of\\|for\\)\\)\\)"
275 "[ \t\n]+\\)?"
276 ;; Note starting with word-syntax character:
277 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'"))
278 "Regexp matching doc string references to symbols.
280 The words preceding the quoted symbol can be used in doc strings to
281 distinguish references to variables, functions and symbols.")
283 (defvar help-xref-mule-regexp nil
284 "Regexp matching doc string references to MULE-related keywords.
286 It is usually nil, and is temporarily bound to an appropriate regexp
287 when help commands related to multilingual environment (e.g.,
288 `describe-coding-system') are invoked.")
291 (defconst help-xref-info-regexp
292 (purecopy "\\<[Ii]nfo[ \t\n]+\\(node\\|anchor\\)[ \t\n]+`\\([^']+\\)'")
293 "Regexp matching doc string references to an Info node.")
295 (defconst help-xref-url-regexp
296 (purecopy "\\<[Uu][Rr][Ll][ \t\n]+`\\([^']+\\)'")
297 "Regexp matching doc string references to a URL.")
299 ;;;###autoload
300 (defun help-setup-xref (item interactive-p)
301 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
303 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
304 buffer after following a reference. INTERACTIVE-P is non-nil if the
305 calling command was invoked interactively. In this case the stack of
306 items for help buffer \"back\" buttons is cleared.
308 This should be called very early, before the output buffer is cleared,
309 because we want to record the \"previous\" position of point so we can
310 restore it properly when going back."
311 (with-current-buffer (help-buffer)
312 (when help-xref-stack-item
313 (push (cons (point) help-xref-stack-item) help-xref-stack)
314 (setq help-xref-forward-stack nil))
315 (when interactive-p
316 (let ((tail (nthcdr 10 help-xref-stack)))
317 ;; Truncate the stack.
318 (if tail (setcdr tail nil))))
319 (setq help-xref-stack-item item)))
321 (defvar help-xref-following nil
322 "Non-nil when following a help cross-reference.")
324 (defun help-buffer ()
325 (buffer-name ;for with-output-to-temp-buffer
326 (if help-xref-following
327 (current-buffer)
328 (get-buffer-create "*Help*"))))
330 (defvar help-xref-override-view-map
331 (let ((map (make-sparse-keymap)))
332 (set-keymap-parent map view-mode-map)
333 (define-key map "\r" nil)
334 map)
335 "Replacement keymap for `view-mode' in help buffers.")
337 ;;;###autoload
338 (defun help-make-xrefs (&optional buffer)
339 "Parse and hyperlink documentation cross-references in the given BUFFER.
341 Find cross-reference information in a buffer and activate such cross
342 references for selection with `help-follow'. Cross-references have
343 the canonical form `...' and the type of reference may be
344 disambiguated by the preceding word(s) used in
345 `help-xref-symbol-regexp'. Faces only get cross-referenced if
346 preceded or followed by the word `face'. Variables without
347 variable documentation do not get cross-referenced, unless
348 preceded by the word `variable' or `option'.
350 If the variable `help-xref-mule-regexp' is non-nil, find also
351 cross-reference information related to multilingual environment
352 \(e.g., coding-systems). This variable is also used to disambiguate
353 the type of reference as the same way as `help-xref-symbol-regexp'.
355 A special reference `back' is made to return back through a stack of
356 help buffers. Variable `help-back-label' specifies the text for
357 that."
358 (interactive "b")
359 (save-excursion
360 (set-buffer (or buffer (current-buffer)))
361 (goto-char (point-min))
362 ;; Skip the header-type info, though it might be useful to parse
363 ;; it at some stage (e.g. "function in `library'").
364 (forward-paragraph)
365 (let ((old-modified (buffer-modified-p)))
366 (let ((stab (syntax-table))
367 (case-fold-search t)
368 (inhibit-read-only t))
369 (set-syntax-table emacs-lisp-mode-syntax-table)
370 ;; The following should probably be abstracted out.
371 (unwind-protect
372 (progn
373 ;; Info references
374 (save-excursion
375 (while (re-search-forward help-xref-info-regexp nil t)
376 (let ((data (match-string 2)))
377 (save-match-data
378 (unless (string-match "^([^)]+)" data)
379 (setq data (concat "(emacs)" data))))
380 (help-xref-button 2 'help-info data))))
381 ;; URLs
382 (save-excursion
383 (while (re-search-forward help-xref-url-regexp nil t)
384 (let ((data (match-string 1)))
385 (help-xref-button 1 'help-url data))))
386 ;; Mule related keywords. Do this before trying
387 ;; `help-xref-symbol-regexp' because some of Mule
388 ;; keywords have variable or function definitions.
389 (if help-xref-mule-regexp
390 (save-excursion
391 (while (re-search-forward help-xref-mule-regexp nil t)
392 (let* ((data (match-string 7))
393 (sym (intern-soft data)))
394 (cond
395 ((match-string 3) ; coding system
396 (and sym (coding-system-p sym)
397 (help-xref-button 6 'help-coding-system sym)))
398 ((match-string 4) ; input method
399 (and (assoc data input-method-alist)
400 (help-xref-button 7 'help-input-method data)))
401 ((or (match-string 5) (match-string 6)) ; charset
402 (and sym (charsetp sym)
403 (help-xref-button 7 'help-character-set sym)))
404 ((assoc data input-method-alist)
405 (help-xref-button 7 'help-character-set data))
406 ((and sym (coding-system-p sym))
407 (help-xref-button 7 'help-coding-system sym))
408 ((and sym (charsetp sym))
409 (help-xref-button 7 'help-character-set sym)))))))
410 ;; Quoted symbols
411 (save-excursion
412 (while (re-search-forward help-xref-symbol-regexp nil t)
413 (let* ((data (match-string 8))
414 (sym (intern-soft data)))
415 (if sym
416 (cond
417 ((match-string 3) ; `variable' &c
418 (and (or (boundp sym) ; `variable' doesn't ensure
419 ; it's actually bound
420 (get sym 'variable-documentation))
421 (help-xref-button 8 'help-variable sym)))
422 ((match-string 4) ; `function' &c
423 (and (fboundp sym) ; similarly
424 (help-xref-button 8 'help-function sym)))
425 ((match-string 5) ; `face'
426 (and (facep sym)
427 (help-xref-button 8 'help-face sym)))
428 ((match-string 6)) ; nothing for `symbol'
429 ((match-string 7)
430 ;;; this used:
431 ;;; #'(lambda (arg)
432 ;;; (let ((location
433 ;;; (find-function-noselect arg)))
434 ;;; (pop-to-buffer (car location))
435 ;;; (goto-char (cdr location))))
436 (help-xref-button 8 'help-function-def sym))
437 ((and
438 (facep sym)
439 (save-match-data (looking-at "[ \t\n]+face\\W")))
440 (help-xref-button 8 'help-face sym))
441 ((and (or (boundp sym)
442 (get sym 'variable-documentation))
443 (fboundp sym))
444 ;; We can't intuit whether to use the
445 ;; variable or function doc -- supply both.
446 (help-xref-button 8 'help-symbol sym))
447 ((and
448 (or (boundp sym)
449 (get sym 'variable-documentation))
451 (documentation-property
452 sym 'variable-documentation)
453 (condition-case nil
454 (documentation-property
455 (indirect-variable sym)
456 'variable-documentation)
457 (cyclic-variable-indirection nil))))
458 (help-xref-button 8 'help-variable sym))
459 ((fboundp sym)
460 (help-xref-button 8 'help-function sym)))))))
461 ;; An obvious case of a key substitution:
462 (save-excursion
463 (while (re-search-forward
464 ;; Assume command name is only word and symbol
465 ;; characters to get things like `use M-x foo->bar'.
466 ;; Command required to end with word constituent
467 ;; to avoid `.' at end of a sentence.
468 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|\\s_\\)*\\sw\\)" nil t)
469 (let ((sym (intern-soft (match-string 1))))
470 (if (fboundp sym)
471 (help-xref-button 1 'help-function sym)))))
472 ;; Look for commands in whole keymap substitutions:
473 (save-excursion
474 ;; Make sure to find the first keymap.
475 (goto-char (point-min))
476 ;; Find a header and the column at which the command
477 ;; name will be found.
479 ;; If the keymap substitution isn't the last thing in
480 ;; the doc string, and if there is anything on the
481 ;; same line after it, this code won't recognize the end of it.
482 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
483 nil t)
484 (let ((col (- (match-end 1) (match-beginning 1))))
485 (while
486 (and (not (eobp))
487 ;; Stop at a pair of blank lines.
488 (not (looking-at "\n\\s-*\n")))
489 ;; Skip a single blank line.
490 (and (eolp) (forward-line))
491 (end-of-line)
492 (skip-chars-backward "^ \t\n")
493 (if (and (>= (current-column) col)
494 (looking-at "\\(\\sw\\|\\s_\\)+$"))
495 (let ((sym (intern-soft (match-string 0))))
496 (if (fboundp sym)
497 (help-xref-button 0 'help-function sym))))
498 (forward-line))))))
499 (set-syntax-table stab))
500 ;; Delete extraneous newlines at the end of the docstring
501 (goto-char (point-max))
502 (while (and (not (bobp)) (bolp))
503 (delete-char -1))
504 (insert "\n")
505 (when (or help-xref-stack help-xref-forward-stack)
506 (insert "\n"))
507 ;; Make a back-reference in this buffer if appropriate.
508 (when help-xref-stack
509 (help-insert-xref-button help-back-label 'help-back
510 (current-buffer)))
511 ;; Make a forward-reference in this buffer if appropriate.
512 (when help-xref-forward-stack
513 (when help-xref-stack
514 (insert "\t"))
515 (help-insert-xref-button help-forward-label 'help-forward
516 (current-buffer)))
517 (when (or help-xref-stack help-xref-forward-stack)
518 (insert "\n")))
519 ;; View mode steals RET from us.
520 (set (make-local-variable 'minor-mode-overriding-map-alist)
521 (list (cons 'view-mode help-xref-override-view-map)))
522 (set-buffer-modified-p old-modified))))
524 ;;;###autoload
525 (defun help-xref-button (match-number type &rest args)
526 "Make a hyperlink for cross-reference text previously matched.
527 MATCH-NUMBER is the subexpression of interest in the last matched
528 regexp. TYPE is the type of button to use. Any remaining arguments are
529 passed to the button's help-function when it is invoked.
530 See `help-make-xrefs'."
531 ;; Don't mung properties we've added specially in some instances.
532 (unless (button-at (match-beginning match-number))
533 (make-text-button (match-beginning match-number)
534 (match-end match-number)
535 'type type 'help-args args)))
537 ;;;###autoload
538 (defun help-insert-xref-button (string type &rest args)
539 "Insert STRING and make a hyperlink from cross-reference text on it.
540 TYPE is the type of button to use. Any remaining arguments are passed
541 to the button's help-function when it is invoked.
542 See `help-make-xrefs'."
543 (unless (button-at (point))
544 (insert-text-button string 'type type 'help-args args)))
546 ;;;###autoload
547 (defun help-xref-on-pp (from to)
548 "Add xrefs for symbols in `pp's output between FROM and TO."
549 (if (> (- to from) 5000) nil
550 (with-syntax-table emacs-lisp-mode-syntax-table
551 (save-excursion
552 (save-restriction
553 (narrow-to-region from to)
554 (goto-char (point-min))
555 (condition-case nil
556 (while (not (eobp))
557 (cond
558 ((looking-at "\"") (forward-sexp 1))
559 ((looking-at "#<") (search-forward ">" nil 'move))
560 ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)")
561 (let* ((sym (intern-soft (match-string 1)))
562 (type (cond ((fboundp sym) 'help-function)
563 ((or (memq sym '(t nil))
564 (keywordp sym))
565 nil)
566 ((and sym
567 (or (boundp sym)
568 (get sym
569 'variable-documentation)))
570 'help-variable))))
571 (when type (help-xref-button 1 type sym)))
572 (goto-char (match-end 1)))
573 (t (forward-char 1))))
574 (error nil)))))))
577 ;; Additional functions for (re-)creating types of help buffers.
578 (defun help-xref-interned (symbol)
579 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
580 Both variable, function and face documentation are extracted into a single
581 help buffer."
582 (with-current-buffer (help-buffer)
583 ;; Push the previous item on the stack before clobbering the output buffer.
584 (help-setup-xref nil nil)
585 (let ((facedoc (when (facep symbol)
586 ;; Don't record the current entry in the stack.
587 (setq help-xref-stack-item nil)
588 (describe-face symbol)))
589 (fdoc (when (fboundp symbol)
590 ;; Don't record the current entry in the stack.
591 (setq help-xref-stack-item nil)
592 (describe-function symbol)))
593 (sdoc (when (or (boundp symbol)
594 (get symbol 'variable-documentation))
595 ;; Don't record the current entry in the stack.
596 (setq help-xref-stack-item nil)
597 (describe-variable symbol))))
598 (cond
599 (sdoc
600 ;; We now have a help buffer on the variable.
601 ;; Insert the function and face text before it.
602 (when (or fdoc facedoc)
603 (goto-char (point-min))
604 (let ((inhibit-read-only t))
605 (when fdoc
606 (insert fdoc "\n\n")
607 (when facedoc
608 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
609 " is also a " "face." "\n\n")))
610 (when facedoc
611 (insert facedoc "\n\n"))
612 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
613 " is also a " "variable." "\n\n"))
614 ;; Don't record the `describe-variable' item in the stack.
615 (setq help-xref-stack-item nil)
616 (help-setup-xref (list #'help-xref-interned symbol) nil)))
617 (fdoc
618 ;; We now have a help buffer on the function.
619 ;; Insert face text before it.
620 (when facedoc
621 (goto-char (point-max))
622 (let ((inhibit-read-only t))
623 (insert "\n\n" (make-string 30 ?-) "\n\n" (symbol-name symbol)
624 " is also a " "face." "\n\n" facedoc))
625 ;; Don't record the `describe-function' item in the stack.
626 (setq help-xref-stack-item nil)
627 (help-setup-xref (list #'help-xref-interned symbol) nil)))))))
630 ;; Navigation/hyperlinking with xrefs
632 (defun help-xref-go-back (buffer)
633 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
634 (let (item position method args)
635 (with-current-buffer buffer
636 (push (cons (point) help-xref-stack-item) help-xref-forward-stack)
637 (when help-xref-stack
638 (setq item (pop help-xref-stack)
639 ;; Clear the current item so that it won't get pushed
640 ;; by the function we're about to call. TODO: We could also
641 ;; push it onto a "forward" stack and add a `forw' button.
642 help-xref-stack-item nil
643 position (car item)
644 method (cadr item)
645 args (cddr item))))
646 (apply method args)
647 (with-current-buffer buffer
648 (if (get-buffer-window buffer)
649 (set-window-point (get-buffer-window buffer) position)
650 (goto-char position)))))
652 (defun help-xref-go-forward (buffer)
653 "From BUFFER, go forward to next help buffer."
654 (let (item position method args)
655 (with-current-buffer buffer
656 (push (cons (point) help-xref-stack-item) help-xref-stack)
657 (when help-xref-forward-stack
658 (setq item (pop help-xref-forward-stack)
659 ;; Clear the current item so that it won't get pushed
660 ;; by the function we're about to call. TODO: We could also
661 ;; push it onto a "forward" stack and add a `forw' button.
662 help-xref-stack-item nil
663 position (car item)
664 method (cadr item)
665 args (cddr item))))
666 (apply method args)
667 (with-current-buffer buffer
668 (if (get-buffer-window buffer)
669 (set-window-point (get-buffer-window buffer) position)
670 (goto-char position)))))
672 (defun help-go-back ()
673 "Go back to previous topic in this help buffer."
674 (interactive)
675 (if help-xref-stack
676 (help-xref-go-back (current-buffer))
677 (error "No previous help buffer")))
679 (defun help-go-forward ()
680 "Go back to next topic in this help buffer."
681 (interactive)
682 (if help-xref-forward-stack
683 (help-xref-go-forward (current-buffer))
684 (error "No next help buffer")))
686 (defun help-do-xref (pos function args)
687 "Call the help cross-reference function FUNCTION with args ARGS.
688 Things are set up properly so that the resulting help-buffer has
689 a proper [back] button."
690 ;; There is a reference at point. Follow it.
691 (let ((help-xref-following t))
692 (apply function args)))
694 ;; The doc string is meant to explain what buttons do.
695 (defun help-follow-mouse ()
696 "Follow the cross-reference that you click on."
697 (interactive)
698 (error "No cross-reference here"))
700 ;; The doc string is meant to explain what buttons do.
701 (defun help-follow ()
702 "Follow cross-reference at point.
704 For the cross-reference format, see `help-make-xrefs'."
705 (interactive)
706 (error "No cross-reference here"))
708 (defun help-follow-symbol (&optional pos)
709 "In help buffer, show docs for symbol at POS, defaulting to point.
710 Show all docs for that symbol as either a variable, function or face."
711 (interactive "d")
712 (unless pos
713 (setq pos (point)))
714 ;; check if the symbol under point is a function, variable or face
715 (let ((sym
716 (intern
717 (save-excursion
718 (goto-char pos) (skip-syntax-backward "w_")
719 (buffer-substring (point)
720 (progn (skip-syntax-forward "w_")
721 (point)))))))
722 (when (or (boundp sym)
723 (get sym 'variable-documentation)
724 (fboundp sym) (facep sym))
725 (help-do-xref pos #'help-xref-interned (list sym)))))
727 (defun help-insert-string (string)
728 "Insert STRING to the help buffer and install xref info for it.
729 This function can be used to restore the old contents of the help buffer
730 when going back to the previous topic in the xref stack. It is needed
731 in case when it is impossible to recompute the old contents of the
732 help buffer by other means."
733 (setq help-xref-stack-item (list #'help-insert-string string))
734 (with-output-to-temp-buffer (help-buffer)
735 (insert string)))
737 (provide 'help-mode)
739 ;; arch-tag: 850954ae-3725-4cb4-8e91-0bf6d52d6b0b
740 ;;; help-mode.el ends here