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