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