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