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