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