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