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