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