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