Commit | Line | Data |
---|---|---|
f4ed5b19 MB |
1 | ;;; help-mode.el --- `help-mode' used by *Help* buffers |
2 | ||
3 | ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001 | |
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) | |
34 | (eval-when-compile (require 'view)) | |
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) | |
150 | ;; Don't use find-function-noselect because it follows | |
151 | ;; aliases (which fails for built-in functions). | |
152 | (let* ((location (find-function-search-for-symbol | |
153 | fun nil file))) | |
154 | (pop-to-buffer (car location)) | |
155 | (goto-char (cdr location)))) | |
6e881567 | 156 | 'help-echo (purecopy "mouse-2, RET: find function's definition")) |
f4ed5b19 MB |
157 | |
158 | (define-button-type 'help-variable-def | |
6e881567 | 159 | :supertype 'help-xref |
f4ed5b19 MB |
160 | 'help-function (lambda (var &optional file) |
161 | (let ((location | |
162 | (find-variable-noselect var file))) | |
163 | (pop-to-buffer (car location)) | |
164 | (goto-char (cdr location)))) | |
6e881567 | 165 | 'help-echo (purecopy"mouse-2, RET: find variable's definition")) |
f4ed5b19 MB |
166 | |
167 | \f | |
168 | ;;;###autoload | |
1700f41d | 169 | (defun help-mode () |
f4ed5b19 MB |
170 | "Major mode for viewing help text and navigating references in it. |
171 | Entry to this mode runs the normal hook `help-mode-hook'. | |
172 | Commands: | |
173 | \\{help-mode-map}" | |
1700f41d RS |
174 | (interactive) |
175 | (kill-all-local-variables) | |
176 | (use-local-map help-mode-map) | |
177 | (setq mode-name "Help") | |
178 | (setq major-mode 'help-mode) | |
179 | (make-local-variable 'font-lock-defaults) | |
f4ed5b19 MB |
180 | (setq font-lock-defaults nil) ; font-lock would defeat xref |
181 | (view-mode) | |
182 | (make-local-variable 'view-no-disable-on-exit) | |
1700f41d RS |
183 | (setq view-no-disable-on-exit t) |
184 | (run-hooks 'help-mode-hook)) | |
f4ed5b19 MB |
185 | |
186 | ;;;###autoload | |
187 | (defun help-mode-setup () | |
188 | (help-mode) | |
189 | (setq buffer-read-only nil)) | |
190 | ||
191 | ;;;###autoload | |
192 | (defun help-mode-finish () | |
193 | (when (eq major-mode 'help-mode) | |
194 | ;; View mode's read-only status of existing *Help* buffer is lost | |
195 | ;; by with-output-to-temp-buffer. | |
196 | (toggle-read-only 1) | |
197 | (help-make-xrefs (current-buffer))) | |
198 | (setq view-return-to-alist | |
199 | (list (cons (selected-window) help-return-method)))) | |
200 | ||
201 | \f | |
202 | ;;; Grokking cross-reference information in doc strings and | |
203 | ;;; hyperlinking it. | |
204 | ||
205 | ;; This may have some scope for extension and the same or something | |
206 | ;; similar should be done for widget doc strings, which currently use | |
207 | ;; another mechanism. | |
208 | ||
209 | (defcustom help-highlight-p t | |
210 | "*If non-nil, `help-make-xrefs' highlight cross-references. | |
211 | Under a window system it highlights them with face defined by | |
212 | `help-highlight-face'." | |
213 | :group 'help | |
214 | :version "20.3" | |
215 | :type 'boolean) | |
216 | ||
217 | (defcustom help-highlight-face 'underline | |
218 | "Face used by `help-make-xrefs' to highlight cross-references. | |
219 | Must be previously-defined." | |
220 | :group 'help | |
221 | :version "20.3" | |
222 | :type 'face) | |
223 | ||
224 | (defvar help-back-label (purecopy "[back]") | |
225 | "Label to use by `help-make-xrefs' for the go-back reference.") | |
226 | ||
227 | (defconst help-xref-symbol-regexp | |
228 | (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|" | |
229 | "\\(function\\|command\\)\\|" | |
230 | "\\(face\\)\\|" | |
231 | "\\(symbol\\)\\|" | |
232 | "\\(source \\(?:code \\)?\\(?:of\\|for\\)\\)\\)\\s-+\\)?" | |
233 | ;; Note starting with word-syntax character: | |
234 | "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")) | |
235 | "Regexp matching doc string references to symbols. | |
236 | ||
237 | The words preceding the quoted symbol can be used in doc strings to | |
238 | distinguish references to variables, functions and symbols.") | |
239 | ||
240 | (defconst help-xref-mule-regexp nil | |
241 | "Regexp matching doc string references to MULE-related keywords. | |
242 | ||
243 | It is usually nil, and is temporarily bound to an appropriate regexp | |
244 | when help commands related to multilingual environment (e.g., | |
245 | `describe-coding-system') are invoked.") | |
246 | ||
247 | ||
248 | (defconst help-xref-info-regexp | |
249 | (purecopy "\\<[Ii]nfo[ \t\n]+node[ \t\n]+`\\([^']+\\)'") | |
250 | "Regexp matching doc string references to an Info node.") | |
251 | ||
252 | ;;;###autoload | |
253 | (defun help-setup-xref (item interactive-p) | |
254 | "Invoked from commands using the \"*Help*\" buffer to install some xref info. | |
255 | ||
256 | ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help | |
257 | buffer after following a reference. INTERACTIVE-P is non-nil if the | |
258 | calling command was invoked interactively. In this case the stack of | |
89f5b33f SM |
259 | items for help buffer \"back\" buttons is cleared. |
260 | ||
261 | This should be called very early, before the output buffer is cleared, | |
262 | because we want to record the \"previous\" position of point so we can | |
263 | restore it properly when going back." | |
264 | (with-current-buffer (help-buffer) | |
265 | (if interactive-p | |
266 | ;; Why do we want to prevent the user from going back ?? -stef | |
267 | (setq help-xref-stack nil) | |
268 | (when help-xref-stack-item | |
269 | (push (cons (point) help-xref-stack-item) help-xref-stack))) | |
270 | (setq help-xref-stack-item item))) | |
f4ed5b19 MB |
271 | |
272 | (defvar help-xref-following nil | |
273 | "Non-nil when following a help cross-reference.") | |
274 | ||
89f5b33f | 275 | (defun help-buffer () |
89f5b33f SM |
276 | (buffer-name ;for with-output-to-temp-buffer |
277 | (if help-xref-following | |
278 | (current-buffer) | |
279 | (get-buffer-create "*Help*")))) | |
280 | ||
f4ed5b19 MB |
281 | ;;;###autoload |
282 | (defun help-make-xrefs (&optional buffer) | |
283 | "Parse and hyperlink documentation cross-references in the given BUFFER. | |
284 | ||
285 | Find cross-reference information in a buffer and, if | |
286 | `help-highlight-p' is non-nil, highlight it with face defined by | |
287 | `help-highlight-face'; activate such cross references for selection | |
288 | with `help-follow'. Cross-references have the canonical form `...' | |
289 | and the type of reference may be disambiguated by the preceding | |
290 | word(s) used in `help-xref-symbol-regexp'. | |
291 | ||
292 | If the variable `help-xref-mule-regexp' is non-nil, find also | |
293 | cross-reference information related to multilingual environment | |
294 | \(e.g., coding-systems). This variable is also used to disambiguate | |
295 | the type of reference as the same way as `help-xref-symbol-regexp'. | |
296 | ||
297 | A special reference `back' is made to return back through a stack of | |
298 | help buffers. Variable `help-back-label' specifies the text for | |
299 | that." | |
300 | (interactive "b") | |
301 | (save-excursion | |
302 | (set-buffer (or buffer (current-buffer))) | |
303 | (goto-char (point-min)) | |
304 | ;; Skip the header-type info, though it might be useful to parse | |
305 | ;; it at some stage (e.g. "function in `library'"). | |
306 | (forward-paragraph) | |
307 | (let ((old-modified (buffer-modified-p))) | |
308 | (let ((stab (syntax-table)) | |
309 | (case-fold-search t) | |
310 | (inhibit-read-only t)) | |
311 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
312 | ;; The following should probably be abstracted out. | |
313 | (unwind-protect | |
314 | (progn | |
315 | ;; Info references | |
316 | (save-excursion | |
317 | (while (re-search-forward help-xref-info-regexp nil t) | |
318 | (let ((data (match-string 1))) | |
319 | (save-match-data | |
320 | (unless (string-match "^([^)]+)" data) | |
321 | (setq data (concat "(emacs)" data)))) | |
322 | (help-xref-button 1 'help-info data)))) | |
323 | ;; Mule related keywords. Do this before trying | |
324 | ;; `help-xref-symbol-regexp' because some of Mule | |
325 | ;; keywords have variable or function definitions. | |
326 | (if help-xref-mule-regexp | |
327 | (save-excursion | |
328 | (while (re-search-forward help-xref-mule-regexp nil t) | |
329 | (let* ((data (match-string 7)) | |
330 | (sym (intern-soft data))) | |
331 | (cond | |
332 | ((match-string 3) ; coding system | |
333 | (and sym (coding-system-p sym) | |
334 | (help-xref-button 6 'help-coding-system sym))) | |
335 | ((match-string 4) ; input method | |
336 | (and (assoc data input-method-alist) | |
337 | (help-xref-button 7 'help-input-method data))) | |
338 | ((or (match-string 5) (match-string 6)) ; charset | |
339 | (and sym (charsetp sym) | |
340 | (help-xref-button 7 'help-character-set sym))) | |
341 | ((assoc data input-method-alist) | |
342 | (help-xref-button 7 'help-character-set data)) | |
343 | ((and sym (coding-system-p sym)) | |
344 | (help-xref-button 7 'help-coding-system sym)) | |
345 | ((and sym (charsetp sym)) | |
346 | (help-xref-button 7 'help-character-set sym))))))) | |
347 | ;; Quoted symbols | |
348 | (save-excursion | |
349 | (while (re-search-forward help-xref-symbol-regexp nil t) | |
350 | (let* ((data (match-string 8)) | |
351 | (sym (intern-soft data))) | |
352 | (if sym | |
353 | (cond | |
354 | ((match-string 3) ; `variable' &c | |
355 | (and (boundp sym) ; `variable' doesn't ensure | |
356 | ; it's actually bound | |
357 | (help-xref-button 8 'help-variable sym))) | |
358 | ((match-string 4) ; `function' &c | |
359 | (and (fboundp sym) ; similarly | |
360 | (help-xref-button 8 'help-function sym))) | |
361 | ((match-string 5) ; `face' | |
362 | (and (facep sym) | |
363 | (help-xref-button 8 'help-face sym))) | |
364 | ((match-string 6)) ; nothing for `symbol' | |
365 | ((match-string 7) | |
366 | ;; this used: | |
367 | ;; #'(lambda (arg) | |
368 | ;; (let ((location | |
369 | ;; (find-function-noselect arg))) | |
370 | ;; (pop-to-buffer (car location)) | |
371 | ;; (goto-char (cdr location)))) | |
372 | (help-xref-button 8 'help-function-def sym)) | |
373 | ((and (boundp sym) (fboundp sym)) | |
374 | ;; We can't intuit whether to use the | |
375 | ;; variable or function doc -- supply both. | |
376 | (help-xref-button 8 'help-symbol sym)) | |
377 | ((boundp sym) | |
378 | (help-xref-button 8 'help-variable sym)) | |
379 | ((fboundp sym) | |
380 | (help-xref-button 8 'help-function sym)) | |
381 | ((facep sym) | |
382 | (help-xref-button 8 'help-face sym))))))) | |
383 | ;; An obvious case of a key substitution: | |
384 | (save-excursion | |
385 | (while (re-search-forward | |
386 | ;; Assume command name is only word characters | |
387 | ;; and dashes to get things like `use M-x foo.'. | |
388 | "\\<M-x\\s-+\\(\\sw\\(\\sw\\|-\\)+\\)" nil t) | |
389 | (let ((sym (intern-soft (match-string 1)))) | |
390 | (if (fboundp sym) | |
391 | (help-xref-button 1 'help-function sym))))) | |
392 | ;; Look for commands in whole keymap substitutions: | |
393 | (save-excursion | |
394 | ;; Make sure to find the first keymap. | |
395 | (goto-char (point-min)) | |
396 | ;; Find a header and the column at which the command | |
397 | ;; name will be found. | |
398 | (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n" | |
399 | nil t) | |
400 | (let ((col (- (match-end 1) (match-beginning 1)))) | |
401 | (while | |
402 | ;; Ignore single blank lines in table, but not | |
403 | ;; double ones, which should terminate it. | |
404 | (and (not (looking-at "\n\\s-*\n")) | |
405 | (progn | |
406 | (and (eolp) (forward-line)) | |
407 | (end-of-line) | |
408 | (skip-chars-backward "^\t\n") | |
409 | (if (and (>= (current-column) col) | |
410 | (looking-at "\\(\\sw\\|-\\)+$")) | |
411 | (let ((sym (intern-soft (match-string 0)))) | |
412 | (if (fboundp sym) | |
413 | (help-xref-button 0 'help-function sym)))) | |
414 | (zerop (forward-line))))))))) | |
415 | (set-syntax-table stab)) | |
416 | ;; Delete extraneous newlines at the end of the docstring | |
417 | (goto-char (point-max)) | |
418 | (while (and (not (bobp)) (bolp)) | |
419 | (delete-char -1)) | |
420 | ;; Make a back-reference in this buffer if appropriate. | |
89f5b33f | 421 | (when help-xref-stack |
f4ed5b19 MB |
422 | (insert "\n\n") |
423 | (help-insert-xref-button help-back-label 'help-back | |
424 | (current-buffer)))) | |
425 | ;; View mode steals RET from us. | |
426 | (set (make-local-variable 'minor-mode-overriding-map-alist) | |
427 | (list (cons 'view-mode | |
428 | (let ((map (make-sparse-keymap))) | |
429 | (set-keymap-parent map view-mode-map) | |
430 | (define-key map "\r" 'help-follow) | |
431 | map)))) | |
432 | (set-buffer-modified-p old-modified)))) | |
433 | ||
434 | ;;;###autoload | |
435 | (defun help-xref-button (match-number type &rest args) | |
436 | "Make a hyperlink for cross-reference text previously matched. | |
437 | MATCH-NUMBER is the subexpression of interest in the last matched | |
438 | regexp. TYPE is the type of button to use. Any remaining arguments are | |
439 | passed to the button's help-function when it is invoked. | |
440 | See `help-make-xrefs'." | |
441 | ;; Don't mung properties we've added specially in some instances. | |
442 | (unless (button-at (match-beginning match-number)) | |
443 | (make-text-button (match-beginning match-number) | |
444 | (match-end match-number) | |
445 | 'type type 'help-args args))) | |
446 | ||
447 | ;;;###autoload | |
448 | (defun help-insert-xref-button (string type &rest args) | |
449 | "Insert STRING and make a hyperlink from cross-reference text on it. | |
450 | TYPE is the type of button to use. Any remaining arguments are passed | |
451 | to the button's help-function when it is invoked. | |
452 | See `help-make-xrefs'." | |
453 | (unless (button-at (point)) | |
454 | (insert-text-button string 'type type 'help-args args))) | |
455 | ||
456 | ;;;###autoload | |
457 | (defun help-xref-on-pp (from to) | |
458 | "Add xrefs for symbols in `pp's output between FROM and TO." | |
459 | (let ((ost (syntax-table))) | |
460 | (unwind-protect | |
461 | (save-excursion | |
462 | (save-restriction | |
463 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
464 | (narrow-to-region from to) | |
465 | (goto-char (point-min)) | |
466 | (while (not (eobp)) | |
467 | (cond | |
468 | ((looking-at "\"") (forward-sexp 1)) | |
469 | ((looking-at "#<") (search-forward ">" nil 'move)) | |
470 | ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)") | |
471 | (let* ((sym (intern-soft (match-string 1))) | |
472 | (type (cond ((fboundp sym) 'help-function) | |
473 | ((or (memq sym '(t nil)) | |
474 | (keywordp sym)) | |
475 | nil) | |
476 | ((and sym (boundp sym)) | |
477 | 'help-variable)))) | |
478 | (when type (help-xref-button 1 type sym))) | |
479 | (goto-char (match-end 1))) | |
480 | (t (forward-char 1)))))) | |
481 | (set-syntax-table ost)))) | |
482 | ||
483 | \f | |
484 | ;; Additional functions for (re-)creating types of help buffers. | |
485 | (defun help-xref-interned (symbol) | |
486 | "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL. | |
89f5b33f | 487 | Both variable, function and face documentation are extracted into a single |
f4ed5b19 | 488 | help buffer." |
89f5b33f SM |
489 | (with-current-buffer (help-buffer) |
490 | ;; Push the previous item on the stack before clobbering the output buffer. | |
491 | (help-setup-xref nil nil) | |
492 | (let ((facedoc (when (facep symbol) | |
493 | ;; Don't record the current entry in the stack. | |
494 | (setq help-xref-stack-item nil) | |
495 | (describe-face symbol))) | |
496 | (fdoc (when (fboundp symbol) | |
497 | ;; Don't record the current entry in the stack. | |
498 | (setq help-xref-stack-item nil) | |
499 | (describe-function symbol))) | |
500 | (sdoc (when (boundp symbol) | |
501 | ;; Don't record the current entry in the stack. | |
502 | (setq help-xref-stack-item nil) | |
503 | (describe-variable symbol)))) | |
504 | (cond | |
505 | (sdoc | |
506 | ;; We now have a help buffer on the variable. | |
507 | ;; Insert the function and face text before it. | |
f4ed5b19 | 508 | (when (or fdoc facedoc) |
f4ed5b19 MB |
509 | (goto-char (point-min)) |
510 | (let ((inhibit-read-only t)) | |
511 | (when fdoc | |
89f5b33f | 512 | (insert fdoc "\n\n") |
f4ed5b19 MB |
513 | (when facedoc |
514 | (insert (make-string 30 ?-) "\n\n" (symbol-name symbol) | |
89f5b33f SM |
515 | " is also a " "face." "\n\n"))) |
516 | (when facedoc | |
517 | (insert facedoc "\n\n")) | |
f4ed5b19 MB |
518 | (insert (make-string 30 ?-) "\n\n" (symbol-name symbol) |
519 | " is also a " "variable." "\n\n")) | |
89f5b33f SM |
520 | ;; Don't record the `describe-variable' item in the stack. |
521 | (setq help-xref-stack-item nil) | |
522 | (help-setup-xref (list #'help-xref-interned symbol) nil))) | |
523 | (fdoc | |
524 | ;; We now have a help buffer on the function. | |
525 | ;; Insert face text before it. | |
526 | (when facedoc | |
527 | (goto-char (point-max)) | |
528 | (let ((inhibit-read-only t)) | |
529 | (insert "\n\n" (make-string 30 ?-) "\n\n" (symbol-name symbol) | |
530 | " is also a " "face." "\n\n" facedoc)) | |
531 | ;; Don't record the `describe-function' item in the stack. | |
532 | (setq help-xref-stack-item nil) | |
533 | (help-setup-xref (list #'help-xref-interned symbol) nil))))))) | |
f4ed5b19 MB |
534 | |
535 | \f | |
536 | ;;; Navigation/hyperlinking with xrefs | |
537 | ||
89f5b33f SM |
538 | (defun help-follow-mouse (click) |
539 | "Follow the cross-reference that you CLICK on." | |
540 | (interactive "e") | |
541 | (let* ((start (event-start click)) | |
542 | (window (car start)) | |
543 | (pos (car (cdr start)))) | |
544 | (with-current-buffer (window-buffer window) | |
545 | (help-follow pos)))) | |
546 | ||
f4ed5b19 MB |
547 | (defun help-xref-go-back (buffer) |
548 | "From BUFFER, go back to previous help buffer text using `help-xref-stack'." | |
549 | (let (item position method args) | |
550 | (with-current-buffer buffer | |
551 | (when help-xref-stack | |
f4ed5b19 | 552 | (setq item (pop help-xref-stack) |
89f5b33f SM |
553 | ;; Clear the current item so that it won't get pushed |
554 | ;; by the function we're about to call. TODO: We could also | |
555 | ;; push it onto a "forward" stack and add a `forw' button. | |
556 | help-xref-stack-item nil | |
f4ed5b19 MB |
557 | position (car item) |
558 | method (cadr item) | |
559 | args (cddr item)))) | |
560 | (apply method args) | |
89f5b33f SM |
561 | ;; FIXME: are we sure we're in the right buffer ? |
562 | (goto-char position))) | |
f4ed5b19 MB |
563 | |
564 | (defun help-go-back () | |
565 | "Invoke the [back] button (if any) in the Help mode buffer." | |
566 | (interactive) | |
567 | (let ((back-button (button-at (1- (point-max))))) | |
568 | (if back-button | |
569 | (button-activate back-button) | |
570 | (error "No [back] button")))) | |
571 | ||
572 | (defun help-do-xref (pos function args) | |
573 | "Call the help cross-reference function FUNCTION with args ARGS. | |
574 | Things are set up properly so that the resulting help-buffer has | |
575 | a proper [back] button." | |
f4ed5b19 MB |
576 | ;; There is a reference at point. Follow it. |
577 | (let ((help-xref-following t)) | |
578 | (apply function args))) | |
579 | ||
580 | (defun help-follow (&optional pos) | |
581 | "Follow cross-reference at POS, defaulting to point. | |
582 | ||
583 | For the cross-reference format, see `help-make-xrefs'." | |
584 | (interactive "d") | |
585 | (unless pos | |
586 | (setq pos (point))) | |
587 | (unless (push-button pos) | |
588 | ;; check if the symbol under point is a function or variable | |
589 | (let ((sym | |
590 | (intern | |
591 | (save-excursion | |
592 | (goto-char pos) (skip-syntax-backward "w_") | |
593 | (buffer-substring (point) | |
594 | (progn (skip-syntax-forward "w_") | |
595 | (point))))))) | |
89f5b33f | 596 | (when (or (boundp sym) (fboundp sym) (facep sym)) |
f4ed5b19 MB |
597 | (help-do-xref pos #'help-xref-interned (list sym)))))) |
598 | ||
599 | ||
600 | (provide 'help-mode) | |
601 | ||
602 | ;;; help-mode.el ends here |