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