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