Commit | Line | Data |
---|---|---|
f4ed5b19 MB |
1 | ;;; help-mode.el --- `help-mode' used by *Help* buffers |
2 | ||
0728ab11 | 3 | ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002, 2004 |
f4ed5b19 MB |
4 | ;; Free Software Foundation, Inc. |
5 | ||
6 | ;; Maintainer: FSF | |
7 | ;; Keywords: help, internal | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
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 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
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. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
25 | ||
26 | ;;; Commentary: | |
27 | ||
28 | ;; Defines `help-mode', which is the mode used by *Help* buffers, and | |
29 | ;; associated support machinery, such as adding hyperlinks, etc., | |
30 | ||
31 | ;;; Code: | |
32 | ||
33 | (require 'button) | |
410e58b5 | 34 | (require 'view) |
f4ed5b19 MB |
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 | ||
89f5b33f | 41 | (define-key help-mode-map [mouse-2] 'help-follow-mouse) |
f4ed5b19 MB |
42 | (define-key help-mode-map "\C-c\C-b" 'help-go-back) |
43 | (define-key help-mode-map "\C-c\C-c" 'help-follow) | |
44 | ;; Documentation only, since we use minor-mode-overriding-map-alist. | |
45 | (define-key help-mode-map "\r" 'help-follow) | |
46 | ||
47 | (defvar help-xref-stack nil | |
48 | "A stack of ways by which to return to help buffers after following xrefs. | |
49 | Used by `help-follow' and `help-xref-go-back'. | |
89f5b33f SM |
50 | An element looks like (POSITION FUNCTION ARGS...). |
51 | To use the element, do (apply FUNCTION ARGS) then goto the point.") | |
f4ed5b19 | 52 | (put 'help-xref-stack 'permanent-local t) |
89f5b33f | 53 | (make-variable-buffer-local 'help-xref-stack) |
f4ed5b19 MB |
54 | |
55 | (defvar help-xref-stack-item nil | |
56 | "An item for `help-follow' in this buffer to push onto `help-xref-stack'. | |
57 | The format is (FUNCTION ARGS...).") | |
58 | (put 'help-xref-stack-item 'permanent-local t) | |
89f5b33f | 59 | (make-variable-buffer-local 'help-xref-stack-item) |
f4ed5b19 MB |
60 | |
61 | (setq-default help-xref-stack nil help-xref-stack-item nil) | |
62 | ||
1700f41d RS |
63 | (defcustom help-mode-hook nil |
64 | "Hook run by `help-mode'." | |
65 | :type 'hook | |
66 | :group 'help) | |
f4ed5b19 MB |
67 | \f |
68 | ;; Button types used by help | |
69 | ||
6e881567 MB |
70 | (define-button-type 'help-xref |
71 | 'action #'help-button-action) | |
72 | ||
73 | (defun help-button-action (button) | |
74 | "Call BUTTON's help function." | |
75 | (help-do-xref (button-start button) | |
76 | (button-get button 'help-function) | |
77 | (button-get button 'help-args))) | |
78 | ||
7e2a83df RS |
79 | ;; These 6 calls to define-button-type were generated in a dolist |
80 | ;; loop, but that is bad because it means these button types don't | |
81 | ;; have an easily found definition. | |
82 | ||
83 | (define-button-type 'help-function | |
84 | :supertype 'help-xref | |
85 | 'help-function 'describe-function | |
86 | 'help-echo (purecopy "mouse-2, RET: describe this function")) | |
87 | ||
88 | (define-button-type 'help-variable | |
89 | :supertype 'help-xref | |
90 | 'help-function 'describe-variable | |
91 | 'help-echo (purecopy "mouse-2, RET: describe this variable")) | |
92 | ||
93 | (define-button-type 'help-face | |
94 | :supertype 'help-xref | |
95 | 'help-function 'describe-face | |
96 | 'help-echo (purecopy "mouse-2, RET: describe this face")) | |
97 | ||
98 | (define-button-type 'help-coding-system | |
99 | :supertype 'help-xref | |
100 | 'help-function 'describe-coding-system | |
101 | 'help-echo (purecopy "mouse-2, RET: describe this coding system")) | |
102 | ||
103 | (define-button-type 'help-input-method | |
104 | :supertype 'help-xref | |
105 | 'help-function 'describe-input-method | |
106 | 'help-echo (purecopy "mouse-2, RET: describe this input method")) | |
107 | ||
108 | (define-button-type 'help-character-set | |
109 | :supertype 'help-xref | |
110 | 'help-function 'describe-character-set | |
111 | 'help-echo (purecopy "mouse-2, RET: describe this character set")) | |
f4ed5b19 MB |
112 | |
113 | ;; make some more ideosyncratic button types | |
114 | ||
115 | (define-button-type 'help-symbol | |
6e881567 | 116 | :supertype 'help-xref |
f4ed5b19 | 117 | 'help-function #'help-xref-interned |
6e881567 | 118 | 'help-echo (purecopy "mouse-2, RET: describe this symbol")) |
f4ed5b19 MB |
119 | |
120 | (define-button-type 'help-back | |
6e881567 | 121 | :supertype 'help-xref |
f4ed5b19 | 122 | 'help-function #'help-xref-go-back |
6e881567 | 123 | 'help-echo (purecopy "mouse-2, RET: go back to previous help buffer")) |
f4ed5b19 MB |
124 | |
125 | (define-button-type 'help-info | |
6e881567 | 126 | :supertype 'help-xref |
f4ed5b19 | 127 | 'help-function #'info |
6e881567 | 128 | 'help-echo (purecopy"mouse-2, RET: read this Info node")) |
f4ed5b19 MB |
129 | |
130 | (define-button-type 'help-customize-variable | |
6e881567 | 131 | :supertype 'help-xref |
f4ed5b19 MB |
132 | 'help-function (lambda (v) |
133 | (if help-xref-stack | |
134 | (pop help-xref-stack)) | |
135 | (customize-variable v)) | |
6e881567 | 136 | 'help-echo (purecopy "mouse-2, RET: customize variable")) |
f4ed5b19 | 137 | |
07f904a3 | 138 | (define-button-type 'help-customize-face |
6e881567 | 139 | :supertype 'help-xref |
07f904a3 MB |
140 | 'help-function (lambda (v) |
141 | (if help-xref-stack | |
142 | (pop help-xref-stack)) | |
143 | (customize-face v)) | |
6e881567 | 144 | 'help-echo (purecopy "mouse-2, RET: customize face")) |
07f904a3 | 145 | |
f4ed5b19 | 146 | (define-button-type 'help-function-def |
6e881567 | 147 | :supertype 'help-xref |
f4ed5b19 MB |
148 | 'help-function (lambda (fun file) |
149 | (require 'find-func) | |
2ea0f8fd SM |
150 | (when (eq file 'C-source) |
151 | (setq file | |
152 | (help-C-file-name (indirect-function fun) 'fun))) | |
410e58b5 | 153 | ;; Don't use find-function-noselect because it follows |
f4ed5b19 | 154 | ;; aliases (which fails for built-in functions). |
410e58b5 | 155 | (let ((location |
2ea0f8fd | 156 | (find-function-search-for-symbol fun nil file))) |
f4ed5b19 MB |
157 | (pop-to-buffer (car location)) |
158 | (goto-char (cdr location)))) | |
6e881567 | 159 | 'help-echo (purecopy "mouse-2, RET: find function's definition")) |
f4ed5b19 MB |
160 | |
161 | (define-button-type 'help-variable-def | |
6e881567 | 162 | :supertype 'help-xref |
f4ed5b19 | 163 | 'help-function (lambda (var &optional file) |
2ea0f8fd SM |
164 | (when (eq file 'C-source) |
165 | (setq file (help-C-file-name var 'var))) | |
166 | (let ((location (find-variable-noselect var file))) | |
f4ed5b19 MB |
167 | (pop-to-buffer (car location)) |
168 | (goto-char (cdr location)))) | |
6e881567 | 169 | 'help-echo (purecopy"mouse-2, RET: find variable's definition")) |
f4ed5b19 MB |
170 | |
171 | \f | |
172 | ;;;###autoload | |
1700f41d | 173 | (defun help-mode () |
f4ed5b19 MB |
174 | "Major mode for viewing help text and navigating references in it. |
175 | Entry to this mode runs the normal hook `help-mode-hook'. | |
176 | Commands: | |
177 | \\{help-mode-map}" | |
1700f41d RS |
178 | (interactive) |
179 | (kill-all-local-variables) | |
180 | (use-local-map help-mode-map) | |
181 | (setq mode-name "Help") | |
182 | (setq major-mode 'help-mode) | |
f4ed5b19 MB |
183 | (view-mode) |
184 | (make-local-variable 'view-no-disable-on-exit) | |
1700f41d RS |
185 | (setq view-no-disable-on-exit t) |
186 | (run-hooks 'help-mode-hook)) | |
f4ed5b19 MB |
187 | |
188 | ;;;###autoload | |
189 | (defun help-mode-setup () | |
190 | (help-mode) | |
191 | (setq buffer-read-only nil)) | |
192 | ||
193 | ;;;###autoload | |
194 | (defun help-mode-finish () | |
69e73dd3 RS |
195 | (let ((entry (assq (selected-window) view-return-to-alist))) |
196 | (if entry (setcdr entry (cons (selected-window) | |
197 | help-return-method)) | |
198 | (setq view-return-to-alist | |
199 | (cons (cons (selected-window) help-return-method) | |
200 | view-return-to-alist)))) | |
f4ed5b19 MB |
201 | (when (eq major-mode 'help-mode) |
202 | ;; View mode's read-only status of existing *Help* buffer is lost | |
203 | ;; by with-output-to-temp-buffer. | |
204 | (toggle-read-only 1) | |
69e73dd3 | 205 | (help-make-xrefs (current-buffer)))) |
f4ed5b19 | 206 | \f |
410e58b5 SM |
207 | ;; Grokking cross-reference information in doc strings and |
208 | ;; hyperlinking it. | |
f4ed5b19 MB |
209 | |
210 | ;; This may have some scope for extension and the same or something | |
211 | ;; similar should be done for widget doc strings, which currently use | |
212 | ;; another mechanism. | |
213 | ||
f4ed5b19 MB |
214 | (defvar help-back-label (purecopy "[back]") |
215 | "Label to use by `help-make-xrefs' for the go-back reference.") | |
216 | ||
217 | (defconst help-xref-symbol-regexp | |
218 | (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|" | |
219 | "\\(function\\|command\\)\\|" | |
220 | "\\(face\\)\\|" | |
221 | "\\(symbol\\)\\|" | |
040b2fa3 LT |
222 | "\\(source \\(?:code \\)?\\(?:of\\|for\\)\\)\\)" |
223 | "[ \t\n]+\\)?" | |
f4ed5b19 MB |
224 | ;; Note starting with word-syntax character: |
225 | "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")) | |
226 | "Regexp matching doc string references to symbols. | |
227 | ||
228 | The words preceding the quoted symbol can be used in doc strings to | |
229 | distinguish references to variables, functions and symbols.") | |
230 | ||
410e58b5 | 231 | (defvar help-xref-mule-regexp nil |
f4ed5b19 MB |
232 | "Regexp matching doc string references to MULE-related keywords. |
233 | ||
234 | It is usually nil, and is temporarily bound to an appropriate regexp | |
235 | when help commands related to multilingual environment (e.g., | |
236 | `describe-coding-system') are invoked.") | |
237 | ||
238 | ||
239 | (defconst help-xref-info-regexp | |
2e10efeb | 240 | (purecopy "\\<[Ii]nfo[ \t\n]+\\(node\\|anchor\\)[ \t\n]+`\\([^']+\\)'") |
f4ed5b19 MB |
241 | "Regexp matching doc string references to an Info node.") |
242 | ||
243 | ;;;###autoload | |
244 | (defun help-setup-xref (item interactive-p) | |
245 | "Invoked from commands using the \"*Help*\" buffer to install some xref info. | |
246 | ||
247 | ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help | |
248 | buffer after following a reference. INTERACTIVE-P is non-nil if the | |
249 | calling command was invoked interactively. In this case the stack of | |
89f5b33f SM |
250 | items for help buffer \"back\" buttons is cleared. |
251 | ||
252 | This should be called very early, before the output buffer is cleared, | |
253 | because we want to record the \"previous\" position of point so we can | |
254 | restore it properly when going back." | |
255 | (with-current-buffer (help-buffer) | |
410e58b5 SM |
256 | (when help-xref-stack-item |
257 | (push (cons (point) help-xref-stack-item) help-xref-stack)) | |
258 | (when interactive-p | |
259 | (let ((tail (nthcdr 10 help-xref-stack))) | |
260 | ;; Truncate the stack. | |
261 | (if tail (setcdr tail nil)))) | |
89f5b33f | 262 | (setq help-xref-stack-item item))) |
f4ed5b19 MB |
263 | |
264 | (defvar help-xref-following nil | |
265 | "Non-nil when following a help cross-reference.") | |
266 | ||
89f5b33f | 267 | (defun help-buffer () |
89f5b33f SM |
268 | (buffer-name ;for with-output-to-temp-buffer |
269 | (if help-xref-following | |
270 | (current-buffer) | |
271 | (get-buffer-create "*Help*")))) | |
272 | ||
410e58b5 SM |
273 | (defvar help-xref-override-view-map |
274 | (let ((map (make-sparse-keymap))) | |
275 | (set-keymap-parent map view-mode-map) | |
276 | (define-key map "\r" nil) | |
277 | map) | |
278 | "Replacement keymap for `view-mode' in help buffers.") | |
279 | ||
f4ed5b19 MB |
280 | ;;;###autoload |
281 | (defun help-make-xrefs (&optional buffer) | |
282 | "Parse and hyperlink documentation cross-references in the given BUFFER. | |
283 | ||
d1282401 CW |
284 | Find cross-reference information in a buffer and activate such cross |
285 | references for selection with `help-follow'. Cross-references have | |
286 | the canonical form `...' and the type of reference may be | |
287 | disambiguated by the preceding word(s) used in | |
040b2fa3 LT |
288 | `help-xref-symbol-regexp'. Faces only get cross-referenced if |
289 | preceded or followed by the word `face'. Variables without | |
290 | variable documentation do not get cross-referenced, unless | |
9315fc34 | 291 | preceded by the word `variable' or `option'. |
f4ed5b19 MB |
292 | |
293 | If the variable `help-xref-mule-regexp' is non-nil, find also | |
294 | cross-reference information related to multilingual environment | |
295 | \(e.g., coding-systems). This variable is also used to disambiguate | |
296 | the type of reference as the same way as `help-xref-symbol-regexp'. | |
297 | ||
298 | A special reference `back' is made to return back through a stack of | |
299 | help buffers. Variable `help-back-label' specifies the text for | |
300 | that." | |
301 | (interactive "b") | |
302 | (save-excursion | |
303 | (set-buffer (or buffer (current-buffer))) | |
304 | (goto-char (point-min)) | |
305 | ;; Skip the header-type info, though it might be useful to parse | |
306 | ;; it at some stage (e.g. "function in `library'"). | |
307 | (forward-paragraph) | |
308 | (let ((old-modified (buffer-modified-p))) | |
309 | (let ((stab (syntax-table)) | |
310 | (case-fold-search t) | |
311 | (inhibit-read-only t)) | |
312 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
313 | ;; The following should probably be abstracted out. | |
314 | (unwind-protect | |
315 | (progn | |
316 | ;; Info references | |
317 | (save-excursion | |
318 | (while (re-search-forward help-xref-info-regexp nil t) | |
2e10efeb | 319 | (let ((data (match-string 2))) |
f4ed5b19 MB |
320 | (save-match-data |
321 | (unless (string-match "^([^)]+)" data) | |
322 | (setq data (concat "(emacs)" data)))) | |
2e10efeb | 323 | (help-xref-button 2 'help-info data)))) |
f4ed5b19 MB |
324 | ;; Mule related keywords. Do this before trying |
325 | ;; `help-xref-symbol-regexp' because some of Mule | |
326 | ;; keywords have variable or function definitions. | |
327 | (if help-xref-mule-regexp | |
328 | (save-excursion | |
329 | (while (re-search-forward help-xref-mule-regexp nil t) | |
330 | (let* ((data (match-string 7)) | |
331 | (sym (intern-soft data))) | |
332 | (cond | |
333 | ((match-string 3) ; coding system | |
334 | (and sym (coding-system-p sym) | |
335 | (help-xref-button 6 'help-coding-system sym))) | |
336 | ((match-string 4) ; input method | |
337 | (and (assoc data input-method-alist) | |
338 | (help-xref-button 7 'help-input-method data))) | |
339 | ((or (match-string 5) (match-string 6)) ; charset | |
340 | (and sym (charsetp sym) | |
341 | (help-xref-button 7 'help-character-set sym))) | |
342 | ((assoc data input-method-alist) | |
343 | (help-xref-button 7 'help-character-set data)) | |
344 | ((and sym (coding-system-p sym)) | |
345 | (help-xref-button 7 'help-coding-system sym)) | |
346 | ((and sym (charsetp sym)) | |
347 | (help-xref-button 7 'help-character-set sym))))))) | |
348 | ;; Quoted symbols | |
349 | (save-excursion | |
350 | (while (re-search-forward help-xref-symbol-regexp nil t) | |
351 | (let* ((data (match-string 8)) | |
352 | (sym (intern-soft data))) | |
353 | (if sym | |
354 | (cond | |
040b2fa3 | 355 | ((match-string 3) ; `variable' &c |
f4ed5b19 MB |
356 | (and (boundp sym) ; `variable' doesn't ensure |
357 | ; it's actually bound | |
358 | (help-xref-button 8 'help-variable sym))) | |
040b2fa3 | 359 | ((match-string 4) ; `function' &c |
f4ed5b19 MB |
360 | (and (fboundp sym) ; similarly |
361 | (help-xref-button 8 'help-function sym))) | |
362 | ((match-string 5) ; `face' | |
363 | (and (facep sym) | |
364 | (help-xref-button 8 'help-face sym))) | |
365 | ((match-string 6)) ; nothing for `symbol' | |
366 | ((match-string 7) | |
040b2fa3 LT |
367 | ;;; this used: |
368 | ;;; #'(lambda (arg) | |
369 | ;;; (let ((location | |
370 | ;;; (find-function-noselect arg))) | |
371 | ;;; (pop-to-buffer (car location)) | |
372 | ;;; (goto-char (cdr location)))) | |
f4ed5b19 | 373 | (help-xref-button 8 'help-function-def sym)) |
b1664339 LT |
374 | ((facep sym) |
375 | (if (save-match-data (looking-at "[ \t\n]+face\\W")) | |
376 | (help-xref-button 8 'help-face sym))) | |
f4ed5b19 MB |
377 | ((and (boundp sym) (fboundp sym)) |
378 | ;; We can't intuit whether to use the | |
379 | ;; variable or function doc -- supply both. | |
380 | (help-xref-button 8 'help-symbol sym)) | |
040b2fa3 LT |
381 | ((and |
382 | (boundp sym) | |
1d0a6ebb JH |
383 | (or |
384 | (documentation-property | |
385 | sym 'variable-documentation) | |
386 | (condition-case nil | |
387 | (documentation-property | |
388 | (indirect-variable sym) | |
389 | 'variable-documentation) | |
390 | (cyclic-variable-indirection nil)))) | |
f4ed5b19 MB |
391 | (help-xref-button 8 'help-variable sym)) |
392 | ((fboundp sym) | |
b1664339 | 393 | (help-xref-button 8 'help-function sym))))))) |
f4ed5b19 MB |
394 | ;; An obvious case of a key substitution: |
395 | (save-excursion | |
396 | (while (re-search-forward | |
397 | ;; Assume command name is only word characters | |
398 | ;; and dashes to get things like `use M-x foo.'. | |
399 | "\\<M-x\\s-+\\(\\sw\\(\\sw\\|-\\)+\\)" nil t) | |
400 | (let ((sym (intern-soft (match-string 1)))) | |
401 | (if (fboundp sym) | |
402 | (help-xref-button 1 'help-function sym))))) | |
403 | ;; Look for commands in whole keymap substitutions: | |
404 | (save-excursion | |
405 | ;; Make sure to find the first keymap. | |
406 | (goto-char (point-min)) | |
407 | ;; Find a header and the column at which the command | |
408 | ;; name will be found. | |
ad4888e4 RS |
409 | |
410 | ;; If the keymap substitution isn't the last thing in | |
411 | ;; the doc string, and if there is anything on the | |
412 | ;; same line after it, this code won't recognize the end of it. | |
f4ed5b19 MB |
413 | (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n" |
414 | nil t) | |
415 | (let ((col (- (match-end 1) (match-beginning 1)))) | |
416 | (while | |
ad4888e4 RS |
417 | (and (not (eobp)) |
418 | ;; Stop at a pair of blank lines. | |
419 | (not (looking-at "\n\\s-*\n"))) | |
420 | ;; Skip a single blank line. | |
421 | (and (eolp) (forward-line)) | |
422 | (end-of-line) | |
423 | (skip-chars-backward "^\t\n") | |
424 | (if (and (>= (current-column) col) | |
425 | (looking-at "\\(\\sw\\|-\\)+$")) | |
426 | (let ((sym (intern-soft (match-string 0)))) | |
427 | (if (fboundp sym) | |
428 | (help-xref-button 0 'help-function sym)))) | |
043dcdee JPW |
429 | (forward-line)))))) |
430 | (set-syntax-table stab)) | |
f4ed5b19 MB |
431 | ;; Delete extraneous newlines at the end of the docstring |
432 | (goto-char (point-max)) | |
433 | (while (and (not (bobp)) (bolp)) | |
434 | (delete-char -1)) | |
478eb46b | 435 | (insert "\n") |
f4ed5b19 | 436 | ;; Make a back-reference in this buffer if appropriate. |
89f5b33f | 437 | (when help-xref-stack |
478eb46b | 438 | (insert "\n") |
f4ed5b19 | 439 | (help-insert-xref-button help-back-label 'help-back |
478eb46b JL |
440 | (current-buffer)) |
441 | (insert "\n"))) | |
f4ed5b19 MB |
442 | ;; View mode steals RET from us. |
443 | (set (make-local-variable 'minor-mode-overriding-map-alist) | |
410e58b5 | 444 | (list (cons 'view-mode help-xref-override-view-map))) |
f4ed5b19 MB |
445 | (set-buffer-modified-p old-modified)))) |
446 | ||
447 | ;;;###autoload | |
448 | (defun help-xref-button (match-number type &rest args) | |
449 | "Make a hyperlink for cross-reference text previously matched. | |
450 | MATCH-NUMBER is the subexpression of interest in the last matched | |
451 | regexp. TYPE is the type of button to use. Any remaining arguments are | |
452 | passed to the button's help-function when it is invoked. | |
453 | See `help-make-xrefs'." | |
454 | ;; Don't mung properties we've added specially in some instances. | |
455 | (unless (button-at (match-beginning match-number)) | |
456 | (make-text-button (match-beginning match-number) | |
457 | (match-end match-number) | |
458 | 'type type 'help-args args))) | |
459 | ||
460 | ;;;###autoload | |
461 | (defun help-insert-xref-button (string type &rest args) | |
462 | "Insert STRING and make a hyperlink from cross-reference text on it. | |
463 | TYPE is the type of button to use. Any remaining arguments are passed | |
464 | to the button's help-function when it is invoked. | |
465 | See `help-make-xrefs'." | |
466 | (unless (button-at (point)) | |
467 | (insert-text-button string 'type type 'help-args args))) | |
468 | ||
469 | ;;;###autoload | |
470 | (defun help-xref-on-pp (from to) | |
471 | "Add xrefs for symbols in `pp's output between FROM and TO." | |
22f5d492 SM |
472 | (if (> (- to from) 5000) nil |
473 | (with-syntax-table emacs-lisp-mode-syntax-table | |
474 | (save-excursion | |
475 | (save-restriction | |
476 | (narrow-to-region from to) | |
477 | (goto-char (point-min)) | |
478 | (condition-case nil | |
479 | (while (not (eobp)) | |
480 | (cond | |
481 | ((looking-at "\"") (forward-sexp 1)) | |
482 | ((looking-at "#<") (search-forward ">" nil 'move)) | |
483 | ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)") | |
484 | (let* ((sym (intern-soft (match-string 1))) | |
485 | (type (cond ((fboundp sym) 'help-function) | |
486 | ((or (memq sym '(t nil)) | |
487 | (keywordp sym)) | |
488 | nil) | |
489 | ((and sym (boundp sym)) | |
490 | 'help-variable)))) | |
491 | (when type (help-xref-button 1 type sym))) | |
492 | (goto-char (match-end 1))) | |
493 | (t (forward-char 1)))) | |
494 | (error nil))))))) | |
f4ed5b19 MB |
495 | |
496 | \f | |
497 | ;; Additional functions for (re-)creating types of help buffers. | |
498 | (defun help-xref-interned (symbol) | |
499 | "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL. | |
89f5b33f | 500 | Both variable, function and face documentation are extracted into a single |
f4ed5b19 | 501 | help buffer." |
89f5b33f SM |
502 | (with-current-buffer (help-buffer) |
503 | ;; Push the previous item on the stack before clobbering the output buffer. | |
504 | (help-setup-xref nil nil) | |
505 | (let ((facedoc (when (facep symbol) | |
506 | ;; Don't record the current entry in the stack. | |
507 | (setq help-xref-stack-item nil) | |
508 | (describe-face symbol))) | |
509 | (fdoc (when (fboundp symbol) | |
510 | ;; Don't record the current entry in the stack. | |
511 | (setq help-xref-stack-item nil) | |
512 | (describe-function symbol))) | |
513 | (sdoc (when (boundp symbol) | |
514 | ;; Don't record the current entry in the stack. | |
515 | (setq help-xref-stack-item nil) | |
516 | (describe-variable symbol)))) | |
517 | (cond | |
518 | (sdoc | |
519 | ;; We now have a help buffer on the variable. | |
520 | ;; Insert the function and face text before it. | |
ea127bf4 | 521 | (when (or fdoc facedoc) |
f4ed5b19 MB |
522 | (goto-char (point-min)) |
523 | (let ((inhibit-read-only t)) | |
524 | (when fdoc | |
89f5b33f | 525 | (insert fdoc "\n\n") |
ea127bf4 RS |
526 | (when facedoc |
527 | (insert (make-string 30 ?-) "\n\n" (symbol-name symbol) | |
89f5b33f SM |
528 | " is also a " "face." "\n\n"))) |
529 | (when facedoc | |
530 | (insert facedoc "\n\n")) | |
f4ed5b19 MB |
531 | (insert (make-string 30 ?-) "\n\n" (symbol-name symbol) |
532 | " is also a " "variable." "\n\n")) | |
89f5b33f SM |
533 | ;; Don't record the `describe-variable' item in the stack. |
534 | (setq help-xref-stack-item nil) | |
535 | (help-setup-xref (list #'help-xref-interned symbol) nil))) | |
536 | (fdoc | |
537 | ;; We now have a help buffer on the function. | |
538 | ;; Insert face text before it. | |
539 | (when facedoc | |
540 | (goto-char (point-max)) | |
541 | (let ((inhibit-read-only t)) | |
542 | (insert "\n\n" (make-string 30 ?-) "\n\n" (symbol-name symbol) | |
543 | " is also a " "face." "\n\n" facedoc)) | |
544 | ;; Don't record the `describe-function' item in the stack. | |
545 | (setq help-xref-stack-item nil) | |
546 | (help-setup-xref (list #'help-xref-interned symbol) nil))))))) | |
f4ed5b19 MB |
547 | |
548 | \f | |
410e58b5 | 549 | ;; Navigation/hyperlinking with xrefs |
f4ed5b19 | 550 | |
89f5b33f SM |
551 | (defun help-follow-mouse (click) |
552 | "Follow the cross-reference that you CLICK on." | |
553 | (interactive "e") | |
554 | (let* ((start (event-start click)) | |
555 | (window (car start)) | |
556 | (pos (car (cdr start)))) | |
557 | (with-current-buffer (window-buffer window) | |
558 | (help-follow pos)))) | |
559 | ||
f4ed5b19 MB |
560 | (defun help-xref-go-back (buffer) |
561 | "From BUFFER, go back to previous help buffer text using `help-xref-stack'." | |
562 | (let (item position method args) | |
563 | (with-current-buffer buffer | |
564 | (when help-xref-stack | |
f4ed5b19 | 565 | (setq item (pop help-xref-stack) |
89f5b33f SM |
566 | ;; Clear the current item so that it won't get pushed |
567 | ;; by the function we're about to call. TODO: We could also | |
568 | ;; push it onto a "forward" stack and add a `forw' button. | |
569 | help-xref-stack-item nil | |
f4ed5b19 MB |
570 | position (car item) |
571 | method (cadr item) | |
572 | args (cddr item)))) | |
573 | (apply method args) | |
77144ebc RS |
574 | (with-current-buffer buffer |
575 | (if (get-buffer-window buffer) | |
576 | (set-window-point (get-buffer-window buffer) position) | |
577 | (goto-char position))))) | |
f4ed5b19 MB |
578 | |
579 | (defun help-go-back () | |
933cd61e | 580 | "Go back to previous topic in this help buffer." |
f4ed5b19 | 581 | (interactive) |
933cd61e SM |
582 | (if help-xref-stack |
583 | (help-xref-go-back (current-buffer)) | |
fdeadcd1 | 584 | (error "No previous help buffer"))) |
f4ed5b19 MB |
585 | |
586 | (defun help-do-xref (pos function args) | |
587 | "Call the help cross-reference function FUNCTION with args ARGS. | |
588 | Things are set up properly so that the resulting help-buffer has | |
589 | a proper [back] button." | |
f4ed5b19 MB |
590 | ;; There is a reference at point. Follow it. |
591 | (let ((help-xref-following t)) | |
592 | (apply function args))) | |
593 | ||
594 | (defun help-follow (&optional pos) | |
595 | "Follow cross-reference at POS, defaulting to point. | |
596 | ||
597 | For the cross-reference format, see `help-make-xrefs'." | |
598 | (interactive "d") | |
599 | (unless pos | |
600 | (setq pos (point))) | |
601 | (unless (push-button pos) | |
602 | ;; check if the symbol under point is a function or variable | |
603 | (let ((sym | |
604 | (intern | |
605 | (save-excursion | |
606 | (goto-char pos) (skip-syntax-backward "w_") | |
607 | (buffer-substring (point) | |
608 | (progn (skip-syntax-forward "w_") | |
609 | (point))))))) | |
89f5b33f | 610 | (when (or (boundp sym) (fboundp sym) (facep sym)) |
f4ed5b19 MB |
611 | (help-do-xref pos #'help-xref-interned (list sym)))))) |
612 | ||
613 | ||
614 | (provide 'help-mode) | |
615 | ||
ab5796a9 | 616 | ;;; arch-tag: 850954ae-3725-4cb4-8e91-0bf6d52d6b0b |
f4ed5b19 | 617 | ;;; help-mode.el ends here |