Commit | Line | Data |
---|---|---|
f4ed5b19 MB |
1 | ;;; help-mode.el --- `help-mode' used by *Help* buffers |
2 | ||
ab422c4d PE |
3 | ;; Copyright (C) 1985-1986, 1993-1994, 1998-2013 Free Software |
4 | ;; Foundation, Inc. | |
f4ed5b19 MB |
5 | |
6 | ;; Maintainer: FSF | |
7 | ;; Keywords: help, internal | |
bd78fa1d | 8 | ;; Package: emacs |
f4ed5b19 MB |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
f4ed5b19 | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
f4ed5b19 MB |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
f4ed5b19 MB |
24 | |
25 | ;;; Commentary: | |
26 | ||
27 | ;; Defines `help-mode', which is the mode used by *Help* buffers, and | |
28 | ;; associated support machinery, such as adding hyperlinks, etc., | |
29 | ||
30 | ;;; Code: | |
31 | ||
32 | (require 'button) | |
6f300323 | 33 | (eval-when-compile (require 'easymenu)) |
f4ed5b19 | 34 | |
b016851c SM |
35 | (defvar help-mode-map |
36 | (let ((map (make-sparse-keymap))) | |
09ac1c2a CS |
37 | (set-keymap-parent map (make-composed-keymap button-buffer-map |
38 | special-mode-map)) | |
b016851c SM |
39 | (define-key map [mouse-2] 'help-follow-mouse) |
40 | (define-key map "\C-c\C-b" 'help-go-back) | |
41 | (define-key map "\C-c\C-f" 'help-go-forward) | |
3d10db47 SS |
42 | (define-key map [XF86Back] 'help-go-back) |
43 | (define-key map [XF86Forward] 'help-go-forward) | |
b016851c | 44 | (define-key map "\C-c\C-c" 'help-follow-symbol) |
b016851c SM |
45 | (define-key map "\r" 'help-follow) |
46 | map) | |
f4ed5b19 MB |
47 | "Keymap for help mode.") |
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 | 71 | (defvar help-xref-forward-stack nil |
da2cfeef | 72 | "A stack used to navigate help forwards after using the back button. |
95f731db NR |
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 | 144 | |
53964682 | 145 | ;; Make some more idiosyncratic button types. |
f4ed5b19 MB |
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 | |
06b60517 | 165 | 'help-function (lambda (_a v) (info v)) |
7f9629ce RC |
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 | 206 | |
cf4e5178 | 207 | (define-button-type 'help-function-cmacro ; FIXME: Obsolete since 24.4. |
57c8e7b4 GM |
208 | :supertype 'help-xref |
209 | 'help-function (lambda (fun file) | |
210 | (setq file (locate-library file t)) | |
211 | (if (and file (file-readable-p file)) | |
212 | (progn | |
213 | (pop-to-buffer (find-file-noselect file)) | |
214 | (goto-char (point-min)) | |
215 | (if (re-search-forward | |
cf4e5178 | 216 | (format "^[ \t]*(\\(cl-\\)?define-compiler-macro[ \t]+%s" |
57c8e7b4 GM |
217 | (regexp-quote (symbol-name fun))) nil t) |
218 | (forward-line 0) | |
219 | (message "Unable to find location in file"))) | |
220 | (message "Unable to find file"))) | |
221 | 'help-echo (purecopy "mouse-2, RET: find function's compiler macro")) | |
222 | ||
f4ed5b19 | 223 | (define-button-type 'help-variable-def |
6e881567 | 224 | :supertype 'help-xref |
f4ed5b19 | 225 | 'help-function (lambda (var &optional file) |
2ea0f8fd SM |
226 | (when (eq file 'C-source) |
227 | (setq file (help-C-file-name var 'var))) | |
228 | (let ((location (find-variable-noselect var file))) | |
f4ed5b19 | 229 | (pop-to-buffer (car location)) |
46ab7691 NR |
230 | (if (cdr location) |
231 | (goto-char (cdr location)) | |
232 | (message "Unable to find location in file")))) | |
27313250 | 233 | 'help-echo (purecopy "mouse-2, RET: find variable's definition")) |
f4ed5b19 | 234 | |
12b42b71 RS |
235 | (define-button-type 'help-face-def |
236 | :supertype 'help-xref | |
237 | 'help-function (lambda (fun file) | |
238 | (require 'find-func) | |
239 | ;; Don't use find-function-noselect because it follows | |
240 | ;; aliases (which fails for built-in functions). | |
241 | (let ((location | |
242 | (find-function-search-for-symbol fun 'defface file))) | |
243 | (pop-to-buffer (car location)) | |
46ab7691 NR |
244 | (if (cdr location) |
245 | (goto-char (cdr location)) | |
246 | (message "Unable to find location in file")))) | |
12b42b71 RS |
247 | 'help-echo (purecopy "mouse-2, RET: find face's definition")) |
248 | ||
8adb4c33 CY |
249 | (define-button-type 'help-package |
250 | :supertype 'help-xref | |
251 | 'help-function 'describe-package | |
252 | 'help-echo (purecopy "mouse-2, RET: Describe package")) | |
253 | ||
cced7584 CY |
254 | (define-button-type 'help-package-def |
255 | :supertype 'help-xref | |
256 | 'help-function (lambda (file) (dired file)) | |
257 | 'help-echo (purecopy "mouse-2, RET: visit package directory")) | |
258 | ||
6b09b5d1 CY |
259 | (define-button-type 'help-theme-def |
260 | :supertype 'help-xref | |
261 | 'help-function 'find-file | |
262 | 'help-echo (purecopy "mouse-2, RET: visit theme file")) | |
263 | ||
264 | (define-button-type 'help-theme-edit | |
265 | :supertype 'help-xref | |
266 | 'help-function 'customize-create-theme | |
267 | 'help-echo (purecopy "mouse-2, RET: edit this theme file")) | |
f0422feb CY |
268 | |
269 | (define-button-type 'help-dir-local-var-def | |
270 | :supertype 'help-xref | |
271 | 'help-function (lambda (var &optional file) | |
272 | ;; FIXME: this should go to the point where the | |
273 | ;; local variable was defined. | |
274 | (find-file file)) | |
275 | 'help-echo (purecopy "mouse-2, RET: open directory-local variables file")) | |
276 | ||
f4ed5b19 | 277 | \f |
0c9e42b5 DA |
278 | (defvar bookmark-make-record-function) |
279 | ||
4feb6e73 | 280 | ;;;###autoload |
09ac1c2a | 281 | (define-derived-mode help-mode special-mode "Help" |
f4ed5b19 MB |
282 | "Major mode for viewing help text and navigating references in it. |
283 | Entry to this mode runs the normal hook `help-mode-hook'. | |
284 | Commands: | |
285 | \\{help-mode-map}" | |
dbd6da78 | 286 | (set (make-local-variable 'revert-buffer-function) |
0c9e42b5 DA |
287 | 'help-mode-revert-buffer) |
288 | (set (make-local-variable 'bookmark-make-record-function) | |
289 | 'help-bookmark-make-record)) | |
f4ed5b19 MB |
290 | |
291 | ;;;###autoload | |
292 | (defun help-mode-setup () | |
293 | (help-mode) | |
294 | (setq buffer-read-only nil)) | |
295 | ||
296 | ;;;###autoload | |
297 | (defun help-mode-finish () | |
298 | (when (eq major-mode 'help-mode) | |
b68b3337 | 299 | (setq buffer-read-only t) |
b1ea593c LL |
300 | (save-excursion |
301 | (goto-char (point-min)) | |
302 | (let ((inhibit-read-only t)) | |
def71b5e | 303 | (when (re-search-forward "^This [^[:space:]]+ is advised.$" nil t) |
b1ea593c LL |
304 | (put-text-property (match-beginning 0) |
305 | (match-end 0) | |
306 | 'face 'font-lock-warning-face)))) | |
307 | ||
69e73dd3 | 308 | (help-make-xrefs (current-buffer)))) |
f4ed5b19 | 309 | \f |
410e58b5 SM |
310 | ;; Grokking cross-reference information in doc strings and |
311 | ;; hyperlinking it. | |
f4ed5b19 MB |
312 | |
313 | ;; This may have some scope for extension and the same or something | |
314 | ;; similar should be done for widget doc strings, which currently use | |
315 | ;; another mechanism. | |
316 | ||
f4ed5b19 MB |
317 | (defvar help-back-label (purecopy "[back]") |
318 | "Label to use by `help-make-xrefs' for the go-back reference.") | |
319 | ||
95f731db NR |
320 | (defvar help-forward-label (purecopy "[forward]") |
321 | "Label to use by `help-make-xrefs' for the go-forward reference.") | |
322 | ||
f4ed5b19 | 323 | (defconst help-xref-symbol-regexp |
8a31f813 | 324 | (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|" ; Link to var |
9fc9a531 | 325 | "\\(function\\|command\\|call\\)\\|" ; Link to function |
a79d3474 NR |
326 | "\\(face\\)\\|" ; Link to face |
327 | "\\(symbol\\|program\\|property\\)\\|" ; Don't link | |
040b2fa3 LT |
328 | "\\(source \\(?:code \\)?\\(?:of\\|for\\)\\)\\)" |
329 | "[ \t\n]+\\)?" | |
f4ed5b19 MB |
330 | ;; Note starting with word-syntax character: |
331 | "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")) | |
332 | "Regexp matching doc string references to symbols. | |
333 | ||
334 | The words preceding the quoted symbol can be used in doc strings to | |
335 | distinguish references to variables, functions and symbols.") | |
336 | ||
410e58b5 | 337 | (defvar help-xref-mule-regexp nil |
f4ed5b19 MB |
338 | "Regexp matching doc string references to MULE-related keywords. |
339 | ||
340 | It is usually nil, and is temporarily bound to an appropriate regexp | |
341 | when help commands related to multilingual environment (e.g., | |
342 | `describe-coding-system') are invoked.") | |
343 | ||
344 | ||
345 | (defconst help-xref-info-regexp | |
2e10efeb | 346 | (purecopy "\\<[Ii]nfo[ \t\n]+\\(node\\|anchor\\)[ \t\n]+`\\([^']+\\)'") |
f4ed5b19 MB |
347 | "Regexp matching doc string references to an Info node.") |
348 | ||
5c825567 BW |
349 | (defconst help-xref-url-regexp |
350 | (purecopy "\\<[Uu][Rr][Ll][ \t\n]+`\\([^']+\\)'") | |
351 | "Regexp matching doc string references to a URL.") | |
352 | ||
f4ed5b19 MB |
353 | ;;;###autoload |
354 | (defun help-setup-xref (item interactive-p) | |
355 | "Invoked from commands using the \"*Help*\" buffer to install some xref info. | |
356 | ||
357 | ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help | |
358 | buffer after following a reference. INTERACTIVE-P is non-nil if the | |
359 | calling command was invoked interactively. In this case the stack of | |
89f5b33f SM |
360 | items for help buffer \"back\" buttons is cleared. |
361 | ||
362 | This should be called very early, before the output buffer is cleared, | |
363 | because we want to record the \"previous\" position of point so we can | |
364 | restore it properly when going back." | |
365 | (with-current-buffer (help-buffer) | |
410e58b5 | 366 | (when help-xref-stack-item |
95f731db NR |
367 | (push (cons (point) help-xref-stack-item) help-xref-stack) |
368 | (setq help-xref-forward-stack nil)) | |
410e58b5 SM |
369 | (when interactive-p |
370 | (let ((tail (nthcdr 10 help-xref-stack))) | |
371 | ;; Truncate the stack. | |
372 | (if tail (setcdr tail nil)))) | |
89f5b33f | 373 | (setq help-xref-stack-item item))) |
f4ed5b19 MB |
374 | |
375 | (defvar help-xref-following nil | |
376 | "Non-nil when following a help cross-reference.") | |
377 | ||
91dc07f3 | 378 | ;;;###autoload |
89f5b33f | 379 | (defun help-buffer () |
f4227998 CY |
380 | "Return the name of a buffer for inserting help. |
381 | If `help-xref-following' is non-nil, this is the name of the | |
d23ae2b0 CY |
382 | current buffer. Signal an error if this buffer is not derived |
383 | from `help-mode'. | |
384 | Otherwise, return \"*Help*\", creating a buffer with that name if | |
385 | it does not already exist." | |
89f5b33f | 386 | (buffer-name ;for with-output-to-temp-buffer |
d23ae2b0 CY |
387 | (if (not help-xref-following) |
388 | (get-buffer-create "*Help*") | |
389 | (unless (derived-mode-p 'help-mode) | |
390 | (error "Current buffer is not in Help mode")) | |
391 | (current-buffer)))) | |
89f5b33f | 392 | |
f4ed5b19 MB |
393 | ;;;###autoload |
394 | (defun help-make-xrefs (&optional buffer) | |
395 | "Parse and hyperlink documentation cross-references in the given BUFFER. | |
396 | ||
d1282401 CW |
397 | Find cross-reference information in a buffer and activate such cross |
398 | references for selection with `help-follow'. Cross-references have | |
399 | the canonical form `...' and the type of reference may be | |
400 | disambiguated by the preceding word(s) used in | |
040b2fa3 LT |
401 | `help-xref-symbol-regexp'. Faces only get cross-referenced if |
402 | preceded or followed by the word `face'. Variables without | |
403 | variable documentation do not get cross-referenced, unless | |
9315fc34 | 404 | preceded by the word `variable' or `option'. |
f4ed5b19 MB |
405 | |
406 | If the variable `help-xref-mule-regexp' is non-nil, find also | |
407 | cross-reference information related to multilingual environment | |
408 | \(e.g., coding-systems). This variable is also used to disambiguate | |
409 | the type of reference as the same way as `help-xref-symbol-regexp'. | |
410 | ||
411 | A special reference `back' is made to return back through a stack of | |
412 | help buffers. Variable `help-back-label' specifies the text for | |
413 | that." | |
414 | (interactive "b") | |
589888fe SM |
415 | (with-current-buffer (or buffer (current-buffer)) |
416 | (save-excursion | |
417 | (goto-char (point-min)) | |
418 | ;; Skip the header-type info, though it might be useful to parse | |
419 | ;; it at some stage (e.g. "function in `library'"). | |
420 | (forward-paragraph) | |
421 | (let ((old-modified (buffer-modified-p))) | |
422 | (let ((stab (syntax-table)) | |
423 | (case-fold-search t) | |
424 | (inhibit-read-only t)) | |
425 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
426 | ;; The following should probably be abstracted out. | |
427 | (unwind-protect | |
428 | (progn | |
429 | ;; Info references | |
430 | (save-excursion | |
431 | (while (re-search-forward help-xref-info-regexp nil t) | |
432 | (let ((data (match-string 2))) | |
433 | (save-match-data | |
434 | (unless (string-match "^([^)]+)" data) | |
a50878fa KR |
435 | (setq data (concat "(emacs)" data))) |
436 | (setq data ;; possible newlines if para filled | |
437 | (replace-regexp-in-string "[ \t\n]+" " " data t t))) | |
589888fe SM |
438 | (help-xref-button 2 'help-info data)))) |
439 | ;; URLs | |
440 | (save-excursion | |
441 | (while (re-search-forward help-xref-url-regexp nil t) | |
442 | (let ((data (match-string 1))) | |
443 | (help-xref-button 1 'help-url data)))) | |
444 | ;; Mule related keywords. Do this before trying | |
445 | ;; `help-xref-symbol-regexp' because some of Mule | |
446 | ;; keywords have variable or function definitions. | |
447 | (if help-xref-mule-regexp | |
448 | (save-excursion | |
449 | (while (re-search-forward help-xref-mule-regexp nil t) | |
450 | (let* ((data (match-string 7)) | |
451 | (sym (intern-soft data))) | |
452 | (cond | |
453 | ((match-string 3) ; coding system | |
454 | (and sym (coding-system-p sym) | |
455 | (help-xref-button 6 'help-coding-system sym))) | |
456 | ((match-string 4) ; input method | |
457 | (and (assoc data input-method-alist) | |
458 | (help-xref-button 7 'help-input-method data))) | |
459 | ((or (match-string 5) (match-string 6)) ; charset | |
460 | (and sym (charsetp sym) | |
461 | (help-xref-button 7 'help-character-set sym))) | |
462 | ((assoc data input-method-alist) | |
463 | (help-xref-button 7 'help-character-set data)) | |
464 | ((and sym (coding-system-p sym)) | |
465 | (help-xref-button 7 'help-coding-system sym)) | |
466 | ((and sym (charsetp sym)) | |
467 | (help-xref-button 7 'help-character-set sym))))))) | |
468 | ;; Quoted symbols | |
469 | (save-excursion | |
470 | (while (re-search-forward help-xref-symbol-regexp nil t) | |
471 | (let* ((data (match-string 8)) | |
472 | (sym (intern-soft data))) | |
473 | (if sym | |
474 | (cond | |
475 | ((match-string 3) ; `variable' &c | |
476 | (and (or (boundp sym) ; `variable' doesn't ensure | |
f4ed5b19 | 477 | ; it's actually bound |
589888fe SM |
478 | (get sym 'variable-documentation)) |
479 | (help-xref-button 8 'help-variable sym))) | |
480 | ((match-string 4) ; `function' &c | |
481 | (and (fboundp sym) ; similarly | |
482 | (help-xref-button 8 'help-function sym))) | |
483 | ((match-string 5) ; `face' | |
484 | (and (facep sym) | |
485 | (help-xref-button 8 'help-face sym))) | |
486 | ((match-string 6)) ; nothing for `symbol' | |
487 | ((match-string 7) | |
488 | ;; this used: | |
489 | ;; #'(lambda (arg) | |
490 | ;; (let ((location | |
491 | ;; (find-function-noselect arg))) | |
492 | ;; (pop-to-buffer (car location)) | |
493 | ;; (goto-char (cdr location)))) | |
494 | (help-xref-button 8 'help-function-def sym)) | |
495 | ((and | |
496 | (facep sym) | |
497 | (save-match-data (looking-at "[ \t\n]+face\\W"))) | |
498 | (help-xref-button 8 'help-face sym)) | |
499 | ((and (or (boundp sym) | |
500 | (get sym 'variable-documentation)) | |
501 | (fboundp sym)) | |
502 | ;; We can't intuit whether to use the | |
503 | ;; variable or function doc -- supply both. | |
504 | (help-xref-button 8 'help-symbol sym)) | |
505 | ((and | |
506 | (or (boundp sym) | |
507 | (get sym 'variable-documentation)) | |
06485aa8 SM |
508 | (or |
509 | (documentation-property | |
510 | sym 'variable-documentation) | |
511 | (documentation-property | |
512 | (indirect-variable sym) | |
513 | 'variable-documentation))) | |
589888fe SM |
514 | (help-xref-button 8 'help-variable sym)) |
515 | ((fboundp sym) | |
516 | (help-xref-button 8 'help-function sym))))))) | |
517 | ;; An obvious case of a key substitution: | |
518 | (save-excursion | |
519 | (while (re-search-forward | |
520 | ;; Assume command name is only word and symbol | |
521 | ;; characters to get things like `use M-x foo->bar'. | |
522 | ;; Command required to end with word constituent | |
523 | ;; to avoid `.' at end of a sentence. | |
524 | "\\<M-x\\s-+\\(\\sw\\(\\sw\\|\\s_\\)*\\sw\\)" nil t) | |
525 | (let ((sym (intern-soft (match-string 1)))) | |
526 | (if (fboundp sym) | |
527 | (help-xref-button 1 'help-function sym))))) | |
528 | ;; Look for commands in whole keymap substitutions: | |
529 | (save-excursion | |
530 | ;; Make sure to find the first keymap. | |
531 | (goto-char (point-min)) | |
532 | ;; Find a header and the column at which the command | |
533 | ;; name will be found. | |
534 | ||
535 | ;; If the keymap substitution isn't the last thing in | |
536 | ;; the doc string, and if there is anything on the same | |
537 | ;; line after it, this code won't recognize the end of it. | |
538 | (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n" | |
539 | nil t) | |
540 | (let ((col (- (match-end 1) (match-beginning 1)))) | |
541 | (while | |
542 | (and (not (eobp)) | |
543 | ;; Stop at a pair of blank lines. | |
544 | (not (looking-at "\n\\s-*\n"))) | |
545 | ;; Skip a single blank line. | |
546 | (and (eolp) (forward-line)) | |
547 | (end-of-line) | |
548 | (skip-chars-backward "^ \t\n") | |
549 | (if (and (>= (current-column) col) | |
550 | (looking-at "\\(\\sw\\|\\s_\\)+$")) | |
551 | (let ((sym (intern-soft (match-string 0)))) | |
552 | (if (fboundp sym) | |
553 | (help-xref-button 0 'help-function sym)))) | |
554 | (forward-line)))))) | |
555 | (set-syntax-table stab)) | |
556 | ;; Delete extraneous newlines at the end of the docstring | |
557 | (goto-char (point-max)) | |
558 | (while (and (not (bobp)) (bolp)) | |
559 | (delete-char -1)) | |
560 | (insert "\n") | |
561 | (when (or help-xref-stack help-xref-forward-stack) | |
562 | (insert "\n")) | |
563 | ;; Make a back-reference in this buffer if appropriate. | |
564 | (when help-xref-stack | |
565 | (help-insert-xref-button help-back-label 'help-back | |
566 | (current-buffer))) | |
567 | ;; Make a forward-reference in this buffer if appropriate. | |
568 | (when help-xref-forward-stack | |
569 | (when help-xref-stack | |
570 | (insert "\t")) | |
571 | (help-insert-xref-button help-forward-label 'help-forward | |
572 | (current-buffer))) | |
573 | (when (or help-xref-stack help-xref-forward-stack) | |
574 | (insert "\n"))) | |
589888fe | 575 | (set-buffer-modified-p old-modified))))) |
f4ed5b19 MB |
576 | |
577 | ;;;###autoload | |
578 | (defun help-xref-button (match-number type &rest args) | |
579 | "Make a hyperlink for cross-reference text previously matched. | |
580 | MATCH-NUMBER is the subexpression of interest in the last matched | |
581 | regexp. TYPE is the type of button to use. Any remaining arguments are | |
582 | passed to the button's help-function when it is invoked. | |
583 | See `help-make-xrefs'." | |
584 | ;; Don't mung properties we've added specially in some instances. | |
585 | (unless (button-at (match-beginning match-number)) | |
586 | (make-text-button (match-beginning match-number) | |
587 | (match-end match-number) | |
588 | 'type type 'help-args args))) | |
589 | ||
590 | ;;;###autoload | |
591 | (defun help-insert-xref-button (string type &rest args) | |
592 | "Insert STRING and make a hyperlink from cross-reference text on it. | |
593 | TYPE is the type of button to use. Any remaining arguments are passed | |
594 | to the button's help-function when it is invoked. | |
595 | See `help-make-xrefs'." | |
596 | (unless (button-at (point)) | |
597 | (insert-text-button string 'type type 'help-args args))) | |
598 | ||
599 | ;;;###autoload | |
600 | (defun help-xref-on-pp (from to) | |
601 | "Add xrefs for symbols in `pp's output between FROM and TO." | |
22f5d492 SM |
602 | (if (> (- to from) 5000) nil |
603 | (with-syntax-table emacs-lisp-mode-syntax-table | |
604 | (save-excursion | |
605 | (save-restriction | |
606 | (narrow-to-region from to) | |
607 | (goto-char (point-min)) | |
608 | (condition-case nil | |
609 | (while (not (eobp)) | |
610 | (cond | |
611 | ((looking-at "\"") (forward-sexp 1)) | |
612 | ((looking-at "#<") (search-forward ">" nil 'move)) | |
613 | ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)") | |
614 | (let* ((sym (intern-soft (match-string 1))) | |
615 | (type (cond ((fboundp sym) 'help-function) | |
616 | ((or (memq sym '(t nil)) | |
617 | (keywordp sym)) | |
618 | nil) | |
e715d9b4 LT |
619 | ((and sym |
620 | (or (boundp sym) | |
621 | (get sym | |
622 | 'variable-documentation))) | |
22f5d492 SM |
623 | 'help-variable)))) |
624 | (when type (help-xref-button 1 type sym))) | |
625 | (goto-char (match-end 1))) | |
626 | (t (forward-char 1)))) | |
627 | (error nil))))))) | |
f4ed5b19 MB |
628 | |
629 | \f | |
630 | ;; Additional functions for (re-)creating types of help buffers. | |
631 | (defun help-xref-interned (symbol) | |
632 | "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL. | |
89f5b33f | 633 | Both variable, function and face documentation are extracted into a single |
f4ed5b19 | 634 | help buffer." |
89f5b33f SM |
635 | (with-current-buffer (help-buffer) |
636 | ;; Push the previous item on the stack before clobbering the output buffer. | |
774784f6 | 637 | (help-setup-xref nil nil) |
89f5b33f SM |
638 | (let ((facedoc (when (facep symbol) |
639 | ;; Don't record the current entry in the stack. | |
640 | (setq help-xref-stack-item nil) | |
641 | (describe-face symbol))) | |
642 | (fdoc (when (fboundp symbol) | |
643 | ;; Don't record the current entry in the stack. | |
644 | (setq help-xref-stack-item nil) | |
645 | (describe-function symbol))) | |
e715d9b4 LT |
646 | (sdoc (when (or (boundp symbol) |
647 | (get symbol 'variable-documentation)) | |
89f5b33f SM |
648 | ;; Don't record the current entry in the stack. |
649 | (setq help-xref-stack-item nil) | |
650 | (describe-variable symbol)))) | |
651 | (cond | |
652 | (sdoc | |
653 | ;; We now have a help buffer on the variable. | |
654 | ;; Insert the function and face text before it. | |
ea127bf4 | 655 | (when (or fdoc facedoc) |
f4ed5b19 MB |
656 | (goto-char (point-min)) |
657 | (let ((inhibit-read-only t)) | |
658 | (when fdoc | |
89f5b33f | 659 | (insert fdoc "\n\n") |
ea127bf4 RS |
660 | (when facedoc |
661 | (insert (make-string 30 ?-) "\n\n" (symbol-name symbol) | |
89f5b33f SM |
662 | " is also a " "face." "\n\n"))) |
663 | (when facedoc | |
664 | (insert facedoc "\n\n")) | |
f4ed5b19 MB |
665 | (insert (make-string 30 ?-) "\n\n" (symbol-name symbol) |
666 | " is also a " "variable." "\n\n")) | |
89f5b33f SM |
667 | ;; Don't record the `describe-variable' item in the stack. |
668 | (setq help-xref-stack-item nil) | |
669 | (help-setup-xref (list #'help-xref-interned symbol) nil))) | |
670 | (fdoc | |
671 | ;; We now have a help buffer on the function. | |
672 | ;; Insert face text before it. | |
673 | (when facedoc | |
674 | (goto-char (point-max)) | |
675 | (let ((inhibit-read-only t)) | |
676 | (insert "\n\n" (make-string 30 ?-) "\n\n" (symbol-name symbol) | |
677 | " is also a " "face." "\n\n" facedoc)) | |
678 | ;; Don't record the `describe-function' item in the stack. | |
679 | (setq help-xref-stack-item nil) | |
8e7696a1 CY |
680 | (help-setup-xref (list #'help-xref-interned symbol) nil)))) |
681 | (goto-char (point-min))))) | |
f4ed5b19 MB |
682 | |
683 | \f | |
410e58b5 | 684 | ;; Navigation/hyperlinking with xrefs |
f4ed5b19 MB |
685 | |
686 | (defun help-xref-go-back (buffer) | |
687 | "From BUFFER, go back to previous help buffer text using `help-xref-stack'." | |
688 | (let (item position method args) | |
689 | (with-current-buffer buffer | |
95f731db | 690 | (push (cons (point) help-xref-stack-item) help-xref-forward-stack) |
f4ed5b19 | 691 | (when help-xref-stack |
f4ed5b19 | 692 | (setq item (pop help-xref-stack) |
89f5b33f SM |
693 | ;; Clear the current item so that it won't get pushed |
694 | ;; by the function we're about to call. TODO: We could also | |
695 | ;; push it onto a "forward" stack and add a `forw' button. | |
696 | help-xref-stack-item nil | |
f4ed5b19 MB |
697 | position (car item) |
698 | method (cadr item) | |
699 | args (cddr item)))) | |
700 | (apply method args) | |
77144ebc RS |
701 | (with-current-buffer buffer |
702 | (if (get-buffer-window buffer) | |
703 | (set-window-point (get-buffer-window buffer) position) | |
704 | (goto-char position))))) | |
f4ed5b19 | 705 | |
95f731db NR |
706 | (defun help-xref-go-forward (buffer) |
707 | "From BUFFER, go forward to next help buffer." | |
708 | (let (item position method args) | |
709 | (with-current-buffer buffer | |
710 | (push (cons (point) help-xref-stack-item) help-xref-stack) | |
711 | (when help-xref-forward-stack | |
712 | (setq item (pop help-xref-forward-stack) | |
713 | ;; Clear the current item so that it won't get pushed | |
714 | ;; by the function we're about to call. TODO: We could also | |
715 | ;; push it onto a "forward" stack and add a `forw' button. | |
716 | help-xref-stack-item nil | |
717 | position (car item) | |
718 | method (cadr item) | |
719 | args (cddr item)))) | |
720 | (apply method args) | |
721 | (with-current-buffer buffer | |
722 | (if (get-buffer-window buffer) | |
723 | (set-window-point (get-buffer-window buffer) position) | |
724 | (goto-char position))))) | |
91dc07f3 | 725 | |
f4ed5b19 | 726 | (defun help-go-back () |
933cd61e | 727 | "Go back to previous topic in this help buffer." |
f4ed5b19 | 728 | (interactive) |
933cd61e SM |
729 | (if help-xref-stack |
730 | (help-xref-go-back (current-buffer)) | |
fdeadcd1 | 731 | (error "No previous help buffer"))) |
91dc07f3 | 732 | |
95f731db NR |
733 | (defun help-go-forward () |
734 | "Go back to next topic in this help buffer." | |
735 | (interactive) | |
736 | (if help-xref-forward-stack | |
737 | (help-xref-go-forward (current-buffer)) | |
738 | (error "No next help buffer"))) | |
f4ed5b19 | 739 | |
06b60517 | 740 | (defun help-do-xref (_pos function args) |
f4ed5b19 MB |
741 | "Call the help cross-reference function FUNCTION with args ARGS. |
742 | Things are set up properly so that the resulting help-buffer has | |
743 | a proper [back] button." | |
f4ed5b19 MB |
744 | ;; There is a reference at point. Follow it. |
745 | (let ((help-xref-following t)) | |
746 | (apply function args))) | |
747 | ||
8a31f813 LT |
748 | ;; The doc string is meant to explain what buttons do. |
749 | (defun help-follow-mouse () | |
750 | "Follow the cross-reference that you click on." | |
751 | (interactive) | |
752 | (error "No cross-reference here")) | |
753 | ||
754 | ;; The doc string is meant to explain what buttons do. | |
755 | (defun help-follow () | |
756 | "Follow cross-reference at point. | |
f4ed5b19 MB |
757 | |
758 | For the cross-reference format, see `help-make-xrefs'." | |
8a31f813 LT |
759 | (interactive) |
760 | (error "No cross-reference here")) | |
761 | ||
762 | (defun help-follow-symbol (&optional pos) | |
763 | "In help buffer, show docs for symbol at POS, defaulting to point. | |
764 | Show all docs for that symbol as either a variable, function or face." | |
f4ed5b19 MB |
765 | (interactive "d") |
766 | (unless pos | |
767 | (setq pos (point))) | |
8a31f813 LT |
768 | ;; check if the symbol under point is a function, variable or face |
769 | (let ((sym | |
770 | (intern | |
771 | (save-excursion | |
772 | (goto-char pos) (skip-syntax-backward "w_") | |
773 | (buffer-substring (point) | |
774 | (progn (skip-syntax-forward "w_") | |
775 | (point))))))) | |
776 | (when (or (boundp sym) | |
777 | (get sym 'variable-documentation) | |
778 | (fboundp sym) (facep sym)) | |
779 | (help-do-xref pos #'help-xref-interned (list sym))))) | |
f4ed5b19 | 780 | |
06b60517 | 781 | (defun help-mode-revert-buffer (_ignore-auto noconfirm) |
dbd6da78 JL |
782 | (when (or noconfirm (yes-or-no-p "Revert help buffer? ")) |
783 | (let ((pos (point)) | |
784 | (item help-xref-stack-item) | |
785 | ;; Pretend there is no current item to add to the history. | |
786 | (help-xref-stack-item nil) | |
787 | ;; Use the current buffer. | |
788 | (help-xref-following t)) | |
789 | (apply (car item) (cdr item)) | |
790 | (goto-char pos)))) | |
791 | ||
ee90fe92 NR |
792 | (defun help-insert-string (string) |
793 | "Insert STRING to the help buffer and install xref info for it. | |
794 | This function can be used to restore the old contents of the help buffer | |
795 | when going back to the previous topic in the xref stack. It is needed | |
796 | in case when it is impossible to recompute the old contents of the | |
797 | help buffer by other means." | |
798 | (setq help-xref-stack-item (list #'help-insert-string string)) | |
799 | (with-output-to-temp-buffer (help-buffer) | |
800 | (insert string))) | |
f4ed5b19 | 801 | |
0c9e42b5 DA |
802 | \f |
803 | ;; Bookmark support | |
804 | ||
805 | (declare-function bookmark-prop-get "bookmark" (bookmark prop)) | |
773e1f08 JB |
806 | (declare-function bookmark-make-record-default "bookmark" |
807 | (&optional no-file no-context posn)) | |
0c9e42b5 DA |
808 | |
809 | (defun help-bookmark-make-record () | |
810 | "Create and return a help-mode bookmark record. | |
811 | Implements `bookmark-make-record-function' for help-mode buffers." | |
812 | (unless (car help-xref-stack-item) | |
813 | (error "Cannot create bookmark - help command not known")) | |
814 | `(,@(bookmark-make-record-default 'NO-FILE 'NO-CONTEXT) | |
0c9e42b5 | 815 | (help-fn . ,(car help-xref-stack-item)) |
87e6e64f | 816 | (help-args . ,(cdr help-xref-stack-item)) |
0c9e42b5 DA |
817 | (position . ,(point)) |
818 | (handler . help-bookmark-jump))) | |
819 | ||
820 | ;;;###autoload | |
821 | (defun help-bookmark-jump (bookmark) | |
822 | "Jump to help-mode bookmark BOOKMARK. | |
823 | Handler function for record returned by `help-bookmark-make-record'. | |
824 | BOOKMARK is a bookmark name or a bookmark record." | |
87e6e64f DA |
825 | (let ((help-fn (bookmark-prop-get bookmark 'help-fn)) |
826 | (help-args (bookmark-prop-get bookmark 'help-args)) | |
827 | (position (bookmark-prop-get bookmark 'position))) | |
828 | (apply help-fn help-args) | |
0c9e42b5 DA |
829 | (pop-to-buffer "*Help*") |
830 | (goto-char position))) | |
831 | ||
832 | ||
f4ed5b19 MB |
833 | (provide 'help-mode) |
834 | ||
835 | ;;; help-mode.el ends here |