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