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