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