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