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