Commit | Line | Data |
---|---|---|
e8af40ee | 1 | ;;; apropos.el --- apropos commands for users and programmers |
c0274f38 | 2 | |
e517f56d | 3 | ;; Copyright (C) 1989, 1994, 1995, 2001 Free Software Foundation, Inc. |
9750e079 | 4 | |
e5167999 | 5 | ;; Author: Joe Wells <jbw@bigbird.bu.edu> |
ae212837 | 6 | ;; Rewritten: Daniel Pfeiffer <occitan@esperanto.org> |
e9571d2a | 7 | ;; Keywords: help |
e5167999 | 8 | |
6f8e447f RS |
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 | |
e5167999 | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
6f8e447f RS |
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 | |
b578f267 EN |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
6f8e447f | 25 | |
e5167999 | 26 | ;;; Commentary: |
6f8e447f RS |
27 | |
28 | ;; The ideas for this package were derived from the C code in | |
29 | ;; src/keymap.c and elsewhere. The functions in this file should | |
30 | ;; always be byte-compiled for speed. Someone should rewrite this in | |
31 | ;; C (as part of src/keymap.c) for speed. | |
32 | ||
33 | ;; The idea for super-apropos is based on the original implementation | |
34 | ;; by Lynn Slater <lrs@esl.com>. | |
35 | ||
36 | ;; History: | |
37 | ;; Fixed bug, current-local-map can return nil. | |
38 | ;; Change, doesn't calculate key-bindings unless needed. | |
39 | ;; Added super-apropos capability, changed print functions. | |
3925e76d KH |
40 | ;;; Made fast-apropos and super-apropos share code. |
41 | ;;; Sped up fast-apropos again. | |
6f8e447f | 42 | ;; Added apropos-do-all option. |
3925e76d | 43 | ;;; Added fast-command-apropos. |
6f8e447f | 44 | ;; Changed doc strings to comments for helping functions. |
3925e76d | 45 | ;;; Made doc file buffer read-only, buried it. |
6f8e447f RS |
46 | ;; Only call substitute-command-keys if do-all set. |
47 | ||
645c4f6a KH |
48 | ;; Optionally use configurable faces to make the output more legible. |
49 | ;; Differentiate between command, function and macro. | |
3925e76d KH |
50 | ;; Apropos-command (ex command-apropos) does cmd and optionally user var. |
51 | ;; Apropos shows all 3 aspects of symbols (fn, var and plist) | |
52 | ;; Apropos-documentation (ex super-apropos) now finds all it should. | |
53 | ;; New apropos-value snoops through all values and optionally plists. | |
54 | ;; Reading DOC file doesn't load nroff. | |
55 | ;; Added hypertext following of documentation, mouse-2 on variable gives value | |
56 | ;; from buffer in active window. | |
57 | ||
e5167999 ER |
58 | ;;; Code: |
59 | ||
851befd4 MB |
60 | (require 'button) |
61 | ||
ddd2f740 RS |
62 | (defgroup apropos nil |
63 | "Apropos commands for users and programmers" | |
c906e3ab | 64 | :group 'help |
ddd2f740 RS |
65 | :prefix "apropos") |
66 | ||
3925e76d | 67 | ;; I see a degradation of maybe 10-20% only. |
ddd2f740 | 68 | (defcustom apropos-do-all nil |
3925e76d | 69 | "*Whether the apropos commands should do more. |
ddd2f740 RS |
70 | |
71 | Slows them down more or less. Set this non-nil if you have a fast machine." | |
72 | :group 'apropos | |
73 | :type 'boolean) | |
3925e76d KH |
74 | |
75 | ||
07eeca5d RS |
76 | (defcustom apropos-symbol-face 'bold |
77 | "*Face for symbol name in Apropos output, or nil for none." | |
ddd2f740 RS |
78 | :group 'apropos |
79 | :type 'face) | |
645c4f6a | 80 | |
07eeca5d RS |
81 | (defcustom apropos-keybinding-face 'underline |
82 | "*Face for lists of keybinding in Apropos output, or nil for none." | |
ddd2f740 RS |
83 | :group 'apropos |
84 | :type 'face) | |
645c4f6a | 85 | |
07eeca5d RS |
86 | (defcustom apropos-label-face 'italic |
87 | "*Face for label (`Command', `Variable' ...) in Apropos output. | |
88 | A value of nil means don't use any special font for them, and also | |
89 | turns off mouse highlighting." | |
ddd2f740 RS |
90 | :group 'apropos |
91 | :type 'face) | |
645c4f6a | 92 | |
07eeca5d RS |
93 | (defcustom apropos-property-face 'bold-italic |
94 | "*Face for property name in apropos output, or nil for none." | |
ddd2f740 RS |
95 | :group 'apropos |
96 | :type 'face) | |
645c4f6a | 97 | |
07eeca5d RS |
98 | (defcustom apropos-match-face 'secondary-selection |
99 | "*Face for matching text in Apropos documentation/value, or nil for none. | |
100 | This applies when you look for matches in the documentation or variable value | |
101 | for the regexp; the part that matches gets displayed in this font." | |
ddd2f740 RS |
102 | :group 'apropos |
103 | :type 'face) | |
3925e76d | 104 | |
6f8e447f | 105 | |
26a4a227 | 106 | (defvar apropos-mode-map |
3925e76d | 107 | (let ((map (make-sparse-keymap))) |
e517f56d MB |
108 | (set-keymap-parent map button-buffer-map) |
109 | ;; Use `apropos-follow' instead of just using the button | |
110 | ;; definition of RET, so that users can use it anywhere in an | |
111 | ;; apropos item, not just on top of a button. | |
3925e76d | 112 | (define-key map "\C-m" 'apropos-follow) |
5b23b5e4 RS |
113 | (define-key map " " 'scroll-up) |
114 | (define-key map "\177" 'scroll-down) | |
115 | (define-key map "q" 'quit-window) | |
3925e76d | 116 | map) |
26a4a227 | 117 | "Keymap used in Apropos mode.") |
4de5599d | 118 | |
45f485a6 GM |
119 | (defvar apropos-mode-hook nil |
120 | "*Hook run when mode is turned on.") | |
3925e76d | 121 | |
645c4f6a KH |
122 | (defvar apropos-regexp nil |
123 | "Regexp used in current apropos run.") | |
124 | ||
125 | (defvar apropos-files-scanned () | |
1259a080 | 126 | "List of elc files already scanned in current run of `apropos-documentation'.") |
645c4f6a KH |
127 | |
128 | (defvar apropos-accumulator () | |
129 | "Alist of symbols already found in current apropos run.") | |
3925e76d | 130 | |
645c4f6a | 131 | (defvar apropos-item () |
7a5348db | 132 | "Current item in or for `apropos-accumulator'.") |
e517f56d MB |
133 | |
134 | \f | |
135 | ;;; Button types used by apropos | |
136 | ||
137 | (define-button-type 'apropos-symbol | |
138 | 'face apropos-symbol-face | |
894e460c MB |
139 | 'help-echo "mouse-2, RET: Display more help on this symbol" |
140 | 'action #'apropos-symbol-button-display-help | |
141 | 'skip t) | |
e517f56d MB |
142 | |
143 | (defun apropos-symbol-button-display-help (button) | |
144 | "Display further help for the `apropos-symbol' button BUTTON." | |
145 | (button-activate | |
146 | (or (apropos-next-label-button (button-start button)) | |
147 | (error "There is nothing to follow for `%s'" (button-label button))))) | |
148 | ||
894e460c MB |
149 | (define-button-type 'apropos-function |
150 | 'apropos-label "Function" | |
151 | 'action (lambda (button) | |
152 | (describe-function (button-get button 'apropos-symbol))) | |
153 | 'help-echo "mouse-2, RET: Display more help on this function") | |
154 | (define-button-type 'apropos-macro | |
155 | 'apropos-label "Macro" | |
156 | 'action (lambda (button) | |
157 | (describe-function (button-get button 'apropos-symbol))) | |
158 | 'help-echo "mouse-2, RET: Display more help on this macro") | |
159 | (define-button-type 'apropos-command | |
160 | 'apropos-label "Command" | |
161 | 'action (lambda (button) | |
162 | (describe-function (button-get button 'apropos-symbol))) | |
163 | 'help-echo "mouse-2, RET: Display more help on this command") | |
7cfedc97 | 164 | |
894e460c MB |
165 | ;; We used to use `customize-variable-other-window' instead for a |
166 | ;; customizable variable, but that is slow. It is better to show an | |
167 | ;; ordinary help buffer and let the user click on the customization | |
168 | ;; button in that buffer, if he wants to. | |
169 | ;; Likewise for `customize-face-other-window'. | |
170 | (define-button-type 'apropos-variable | |
171 | 'apropos-label "Variable" | |
172 | 'help-echo "mouse-2, RET: Display more help on this variable" | |
173 | 'action (lambda (button) | |
174 | (describe-variable (button-get button 'apropos-symbol)))) | |
175 | ||
176 | (define-button-type 'apropos-face | |
177 | 'apropos-label "Face" | |
178 | 'help-echo "mouse-2, RET: Display more help on this face" | |
179 | 'action (lambda (button) | |
180 | (describe-face (button-get button 'apropos-symbol)))) | |
181 | ||
182 | (define-button-type 'apropos-group | |
183 | 'apropos-label "Group" | |
184 | 'help-echo "mouse-2, RET: Display more help on this group" | |
185 | 'action (lambda (button) | |
186 | (customize-variable-other-window | |
187 | (button-get button 'apropos-symbol)))) | |
188 | ||
189 | (define-button-type 'apropos-widget | |
190 | 'apropos-label "Widget" | |
191 | 'help-echo "mouse-2, RET: Display more help on this widget" | |
192 | 'action (lambda (button) | |
193 | (widget-browse-other-window (button-get button 'apropos-symbol)))) | |
194 | ||
195 | (define-button-type 'apropos-plist | |
196 | 'apropos-label "Plist" | |
197 | 'help-echo "mouse-2, RET: Display more help on this plist" | |
198 | 'action (lambda (button) | |
199 | (apropos-describe-plist (button-get button 'apropos-symbol)))) | |
e517f56d MB |
200 | |
201 | (defun apropos-next-label-button (pos) | |
a11a4e9f | 202 | "Return the next apropos label button after POS, or nil if there's none. |
e517f56d MB |
203 | Will also return nil if more than one `apropos-symbol' button is encountered |
204 | before finding a label." | |
a101302b | 205 | (let* ((button (next-button pos t)) |
e517f56d | 206 | (already-hit-symbol nil) |
3b8c60f1 MB |
207 | (label (and button (button-get button 'apropos-label))) |
208 | (type (and button (button-get button 'type)))) | |
e517f56d | 209 | (while (and button |
3b8c60f1 MB |
210 | (not label) |
211 | (or (not (eq type 'apropos-symbol)) | |
e517f56d | 212 | (not already-hit-symbol))) |
3b8c60f1 | 213 | (when (eq type 'apropos-symbol) |
e517f56d MB |
214 | (setq already-hit-symbol t)) |
215 | (setq button (next-button (button-start button))) | |
216 | (when button | |
3b8c60f1 MB |
217 | (setq label (button-get button 'apropos-label)) |
218 | (setq type (button-get button 'type)))) | |
219 | (and label button))) | |
e517f56d | 220 | |
645c4f6a | 221 | \f |
bd041ace | 222 | ;;;###autoload |
38ab866c | 223 | (define-derived-mode apropos-mode fundamental-mode "Apropos" |
26a4a227 KH |
224 | "Major mode for following hyperlinks in output of apropos commands. |
225 | ||
38ab866c | 226 | \\{apropos-mode-map}") |
26a4a227 | 227 | |
f38fd610 | 228 | ;;;###autoload |
05942d06 RS |
229 | (defun apropos-variable (regexp &optional do-all) |
230 | "Show user variables that match REGEXP. | |
7a5348db | 231 | With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also show |
05942d06 RS |
232 | normal variables." |
233 | (interactive (list (read-string | |
234 | (concat "Apropos " | |
235 | (if (or current-prefix-arg apropos-do-all) | |
236 | "variable" | |
237 | "user option") | |
238 | " (regexp): ")) | |
239 | current-prefix-arg)) | |
240 | (apropos-command regexp nil | |
cd00fd36 | 241 | (if (or do-all apropos-do-all) |
05942d06 RS |
242 | #'(lambda (symbol) |
243 | (and (boundp symbol) | |
244 | (get symbol 'variable-documentation))) | |
245 | 'user-variable-p))) | |
26a4a227 | 246 | |
645c4f6a KH |
247 | ;; For auld lang syne: |
248 | ;;;###autoload | |
249 | (fset 'command-apropos 'apropos-command) | |
6f8e447f | 250 | ;;;###autoload |
05942d06 | 251 | (defun apropos-command (apropos-regexp &optional do-all var-predicate) |
7a5348db DL |
252 | "Show commands (interactively callable functions) that match APROPOS-REGEXP. |
253 | With optional prefix DO-ALL, or if `apropos-do-all' is non-nil, also show | |
9a909b3c | 254 | noninteractive functions. |
05942d06 | 255 | |
9a909b3c | 256 | If VAR-PREDICATE is non-nil, show only variables, and only those that |
05942d06 | 257 | satisfy the predicate VAR-PREDICATE." |
f38fd610 RS |
258 | (interactive (list (read-string (concat |
259 | "Apropos command " | |
260 | (if (or current-prefix-arg | |
261 | apropos-do-all) | |
9a909b3c | 262 | "or function ") |
f38fd610 | 263 | "(regexp): ")) |
645c4f6a | 264 | current-prefix-arg)) |
3925e76d | 265 | (let ((message |
26a4a227 | 266 | (let ((standard-output (get-buffer-create "*Apropos*"))) |
3925e76d | 267 | (print-help-return-message 'identity)))) |
645c4f6a KH |
268 | (or do-all (setq do-all apropos-do-all)) |
269 | (setq apropos-accumulator | |
270 | (apropos-internal apropos-regexp | |
cd00fd36 KH |
271 | (or var-predicate |
272 | (if do-all 'functionp 'commandp)))) | |
dea22c45 RS |
273 | (let ((tem apropos-accumulator)) |
274 | (while tem | |
275 | (if (get (car tem) 'apropos-inhibit) | |
276 | (setq apropos-accumulator (delq (car tem) apropos-accumulator))) | |
277 | (setq tem (cdr tem)))) | |
a9155e87 KH |
278 | (let ((p apropos-accumulator) |
279 | doc symbol) | |
280 | (while p | |
281 | (setcar p (list | |
282 | (setq symbol (car p)) | |
283 | (unless var-predicate | |
284 | (if (functionp symbol) | |
285 | (if (setq doc (documentation symbol t)) | |
286 | (substring doc 0 (string-match "\n" doc)) | |
287 | "(not documented)"))) | |
288 | (and var-predicate | |
289 | (funcall var-predicate symbol) | |
290 | (if (setq doc (documentation-property | |
291 | symbol 'variable-documentation t)) | |
292 | (substring doc 0 | |
293 | (string-match "\n" doc)))))) | |
294 | (setq p (cdr p)))) | |
295 | (and (apropos-print t nil) | |
296 | message | |
297 | (message message)))) | |
3925e76d KH |
298 | |
299 | ||
3925e76d | 300 | ;;;###autoload |
645c4f6a | 301 | (defun apropos (apropos-regexp &optional do-all) |
7a5348db DL |
302 | "Show all bound symbols whose names match APROPOS-REGEXP. |
303 | With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also | |
304 | show unbound symbols and key bindings, which is a little more | |
305 | time-consuming. Returns list of symbols and documentation found." | |
3925e76d | 306 | (interactive "sApropos symbol (regexp): \nP") |
645c4f6a KH |
307 | (setq apropos-accumulator |
308 | (apropos-internal apropos-regexp | |
309 | (and (not do-all) | |
310 | (not apropos-do-all) | |
311 | (lambda (symbol) | |
312 | (or (fboundp symbol) | |
313 | (boundp symbol) | |
5edc67d3 | 314 | (facep symbol) |
645c4f6a | 315 | (symbol-plist symbol)))))) |
dea22c45 RS |
316 | (let ((tem apropos-accumulator)) |
317 | (while tem | |
318 | (if (get (car tem) 'apropos-inhibit) | |
319 | (setq apropos-accumulator (delq (car tem) apropos-accumulator))) | |
320 | (setq tem (cdr tem)))) | |
a9155e87 KH |
321 | (let ((p apropos-accumulator) |
322 | symbol doc properties) | |
323 | (while p | |
324 | (setcar p (list | |
325 | (setq symbol (car p)) | |
326 | (when (fboundp symbol) | |
327 | (if (setq doc (condition-case nil | |
328 | (documentation symbol t) | |
329 | (void-function | |
330 | "(alias for undefined function)"))) | |
331 | (substring doc 0 (string-match "\n" doc)) | |
332 | "(not documented)")) | |
333 | (when (boundp symbol) | |
334 | (if (setq doc (documentation-property | |
335 | symbol 'variable-documentation t)) | |
336 | (substring doc 0 (string-match "\n" doc)) | |
337 | "(not documented)")) | |
338 | (when (setq properties (symbol-plist symbol)) | |
339 | (setq doc (list (car properties))) | |
340 | (while (setq properties (cdr (cdr properties))) | |
341 | (setq doc (cons (car properties) doc))) | |
342 | (mapconcat #'symbol-name (nreverse doc) " ")) | |
343 | (when (get symbol 'widget-type) | |
344 | (if (setq doc (documentation-property | |
345 | symbol 'widget-documentation t)) | |
346 | (substring doc 0 | |
347 | (string-match "\n" doc)) | |
348 | "(not documented)")) | |
349 | (when (facep symbol) | |
350 | (if (setq doc (documentation-property | |
351 | symbol 'face-documentation t)) | |
352 | (substring doc 0 | |
353 | (string-match "\n" doc)) | |
354 | "(not documented)")) | |
355 | (when (get symbol 'custom-group) | |
356 | (if (setq doc (documentation-property | |
357 | symbol 'group-documentation t)) | |
358 | (substring doc 0 | |
359 | (string-match "\n" doc)) | |
360 | "(not documented)")))) | |
361 | (setq p (cdr p)))) | |
3925e76d | 362 | (apropos-print |
645c4f6a | 363 | (or do-all apropos-do-all) |
3925e76d KH |
364 | nil)) |
365 | ||
366 | ||
6f8e447f | 367 | ;;;###autoload |
645c4f6a | 368 | (defun apropos-value (apropos-regexp &optional do-all) |
7a5348db DL |
369 | "Show all symbols whose value's printed image matches APROPOS-REGEXP. |
370 | With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also looks | |
3925e76d | 371 | at the function and at the names and values of properties. |
645c4f6a | 372 | Returns list of symbols and values found." |
3925e76d | 373 | (interactive "sApropos value (regexp): \nP") |
645c4f6a KH |
374 | (or do-all (setq do-all apropos-do-all)) |
375 | (setq apropos-accumulator ()) | |
376 | (let (f v p) | |
3925e76d KH |
377 | (mapatoms |
378 | (lambda (symbol) | |
379 | (setq f nil v nil p nil) | |
645c4f6a KH |
380 | (or (memq symbol '(apropos-regexp do-all apropos-accumulator |
381 | symbol f v p)) | |
382 | (setq v (apropos-value-internal 'boundp symbol 'symbol-value))) | |
3925e76d | 383 | (if do-all |
645c4f6a KH |
384 | (setq f (apropos-value-internal 'fboundp symbol 'symbol-function) |
385 | p (apropos-format-plist symbol "\n " t))) | |
3925e76d | 386 | (if (or f v p) |
645c4f6a KH |
387 | (setq apropos-accumulator (cons (list symbol f v p) |
388 | apropos-accumulator)))))) | |
9cc84e31 | 389 | (apropos-print nil "\n----------------\n")) |
3925e76d KH |
390 | |
391 | ||
645c4f6a KH |
392 | ;;;###autoload |
393 | (defun apropos-documentation (apropos-regexp &optional do-all) | |
7a5348db DL |
394 | "Show symbols whose documentation contain matches for APROPOS-REGEXP. |
395 | With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also use | |
645c4f6a KH |
396 | documentation that is not stored in the documentation file and show key |
397 | bindings. | |
398 | Returns list of symbols and documentation found." | |
399 | (interactive "sApropos documentation (regexp): \nP") | |
400 | (or do-all (setq do-all apropos-do-all)) | |
401 | (setq apropos-accumulator () apropos-files-scanned ()) | |
402 | (let ((standard-input (get-buffer-create " apropos-temp")) | |
403 | f v) | |
404 | (unwind-protect | |
405 | (save-excursion | |
406 | (set-buffer standard-input) | |
407 | (apropos-documentation-check-doc-file) | |
408 | (if do-all | |
409 | (mapatoms | |
410 | (lambda (symbol) | |
411 | (setq f (apropos-safe-documentation symbol) | |
26a4a227 KH |
412 | v (get symbol 'variable-documentation)) |
413 | (if (integerp v) (setq v)) | |
414 | (setq f (apropos-documentation-internal f) | |
415 | v (apropos-documentation-internal v)) | |
645c4f6a KH |
416 | (if (or f v) |
417 | (if (setq apropos-item | |
418 | (cdr (assq symbol apropos-accumulator))) | |
419 | (progn | |
420 | (if f | |
421 | (setcar apropos-item f)) | |
422 | (if v | |
423 | (setcar (cdr apropos-item) v))) | |
424 | (setq apropos-accumulator | |
425 | (cons (list symbol f v) | |
426 | apropos-accumulator))))))) | |
9cc84e31 | 427 | (apropos-print nil "\n----------------\n")) |
645c4f6a KH |
428 | (kill-buffer standard-input)))) |
429 | ||
430 | \f | |
431 | (defun apropos-value-internal (predicate symbol function) | |
432 | (if (funcall predicate symbol) | |
433 | (progn | |
434 | (setq symbol (prin1-to-string (funcall function symbol))) | |
435 | (if (string-match apropos-regexp symbol) | |
436 | (progn | |
437 | (if apropos-match-face | |
438 | (put-text-property (match-beginning 0) (match-end 0) | |
439 | 'face apropos-match-face | |
440 | symbol)) | |
441 | symbol))))) | |
442 | ||
443 | (defun apropos-documentation-internal (doc) | |
444 | (if (consp doc) | |
445 | (apropos-documentation-check-elc-file (car doc)) | |
446 | (and doc | |
447 | (string-match apropos-regexp doc) | |
448 | (progn | |
449 | (if apropos-match-face | |
450 | (put-text-property (match-beginning 0) | |
451 | (match-end 0) | |
452 | 'face apropos-match-face | |
453 | (setq doc (copy-sequence doc)))) | |
454 | doc)))) | |
455 | ||
456 | (defun apropos-format-plist (pl sep &optional compare) | |
3925e76d KH |
457 | (setq pl (symbol-plist pl)) |
458 | (let (p p-out) | |
459 | (while pl | |
460 | (setq p (format "%s %S" (car pl) (nth 1 pl))) | |
645c4f6a KH |
461 | (if (or (not compare) (string-match apropos-regexp p)) |
462 | (if apropos-property-face | |
3925e76d | 463 | (put-text-property 0 (length (symbol-name (car pl))) |
645c4f6a | 464 | 'face apropos-property-face p)) |
3925e76d | 465 | (setq p nil)) |
645c4f6a KH |
466 | (if p |
467 | (progn | |
468 | (and compare apropos-match-face | |
469 | (put-text-property (match-beginning 0) (match-end 0) | |
470 | 'face apropos-match-face | |
471 | p)) | |
472 | (setq p-out (concat p-out (if p-out sep) p)))) | |
3925e76d KH |
473 | (setq pl (nthcdr 2 pl))) |
474 | p-out)) | |
475 | ||
6f8e447f | 476 | |
645c4f6a | 477 | ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name. |
3925e76d | 478 | |
645c4f6a | 479 | (defun apropos-documentation-check-doc-file () |
26a4a227 KH |
480 | (let (type symbol (sepa 2) sepb beg end) |
481 | (insert ?\^_) | |
482 | (backward-char) | |
645c4f6a | 483 | (insert-file-contents (concat doc-directory internal-doc-file-name)) |
26a4a227 KH |
484 | (forward-char) |
485 | (while (save-excursion | |
486 | (setq sepb (search-forward "\^_")) | |
487 | (not (eobp))) | |
488 | (beginning-of-line 2) | |
489 | (if (save-restriction | |
490 | (narrow-to-region (point) (1- sepb)) | |
491 | (re-search-forward apropos-regexp nil t)) | |
492 | (progn | |
493 | (setq beg (match-beginning 0) | |
494 | end (point)) | |
495 | (goto-char (1+ sepa)) | |
496 | (or (setq type (if (eq ?F (preceding-char)) | |
497 | 1 ; function documentation | |
498 | 2) ; variable documentation | |
499 | symbol (read) | |
500 | beg (- beg (point) 1) | |
501 | end (- end (point) 1) | |
502 | doc (buffer-substring (1+ (point)) (1- sepb)) | |
503 | apropos-item (assq symbol apropos-accumulator)) | |
504 | (setq apropos-item (list symbol nil nil) | |
505 | apropos-accumulator (cons apropos-item | |
506 | apropos-accumulator))) | |
507 | (if apropos-match-face | |
508 | (put-text-property beg end 'face apropos-match-face doc)) | |
509 | (setcar (nthcdr type apropos-item) doc))) | |
510 | (setq sepa (goto-char sepb))))) | |
645c4f6a KH |
511 | |
512 | (defun apropos-documentation-check-elc-file (file) | |
513 | (if (member file apropos-files-scanned) | |
514 | nil | |
26a4a227 | 515 | (let (symbol doc beg end this-is-a-variable) |
645c4f6a KH |
516 | (setq apropos-files-scanned (cons file apropos-files-scanned)) |
517 | (erase-buffer) | |
518 | (insert-file-contents file) | |
519 | (while (search-forward "\n#@" nil t) | |
520 | ;; Read the comment length, and advance over it. | |
521 | (setq end (read) | |
26a4a227 KH |
522 | beg (1+ (point)) |
523 | end (+ (point) end -1)) | |
524 | (forward-char) | |
525 | (if (save-restriction | |
526 | ;; match ^ and $ relative to doc string | |
527 | (narrow-to-region beg end) | |
528 | (re-search-forward apropos-regexp nil t)) | |
645c4f6a | 529 | (progn |
26a4a227 KH |
530 | (goto-char (+ end 2)) |
531 | (setq doc (buffer-substring beg end) | |
532 | end (- (match-end 0) beg) | |
533 | beg (- (match-beginning 0) beg) | |
534 | this-is-a-variable (looking-at "(def\\(var\\|const\\) ") | |
645c4f6a KH |
535 | symbol (progn |
536 | (skip-chars-forward "(a-z") | |
26a4a227 | 537 | (forward-char) |
645c4f6a KH |
538 | (read)) |
539 | symbol (if (consp symbol) | |
540 | (nth 1 symbol) | |
541 | symbol)) | |
542 | (if (if this-is-a-variable | |
543 | (get symbol 'variable-documentation) | |
544 | (and (fboundp symbol) (apropos-safe-documentation symbol))) | |
545 | (progn | |
546 | (or (setq apropos-item (assq symbol apropos-accumulator)) | |
547 | (setq apropos-item (list symbol nil nil) | |
548 | apropos-accumulator (cons apropos-item | |
549 | apropos-accumulator))) | |
550 | (if apropos-match-face | |
26a4a227 | 551 | (put-text-property beg end 'face apropos-match-face |
645c4f6a KH |
552 | doc)) |
553 | (setcar (nthcdr (if this-is-a-variable 2 1) | |
554 | apropos-item) | |
26a4a227 | 555 | doc))))))))) |
645c4f6a KH |
556 | |
557 | ||
558 | ||
559 | (defun apropos-safe-documentation (function) | |
7a5348db | 560 | "Like `documentation', except it avoids calling `get_doc_string'. |
6f8e447f | 561 | Will return nil instead." |
3925e76d | 562 | (while (and function (symbolp function)) |
6f8e447f | 563 | (setq function (if (fboundp function) |
3925e76d | 564 | (symbol-function function)))) |
d2e1218f RS |
565 | (if (eq (car-safe function) 'macro) |
566 | (setq function (cdr function))) | |
3925e76d | 567 | (setq function (if (byte-code-function-p function) |
645c4f6a KH |
568 | (if (> (length function) 4) |
569 | (aref function 4)) | |
570 | (if (eq (car-safe function) 'autoload) | |
571 | (nth 2 function) | |
572 | (if (eq (car-safe function) 'lambda) | |
573 | (if (stringp (nth 2 function)) | |
574 | (nth 2 function) | |
575 | (if (stringp (nth 3 function)) | |
576 | (nth 3 function))))))) | |
577 | (if (integerp function) | |
578 | nil | |
579 | function)) | |
580 | ||
581 | ||
a9155e87 KH |
582 | (defun apropos-print (do-keys spacing) |
583 | "Output result of apropos searching into buffer `*Apropos*'. | |
584 | The value of `apropos-accumulator' is the list of items to output. | |
585 | Each element should have the format (SYMBOL FN-DOC VAR-DOC [PLIST-DOC]). | |
586 | The return value is the list that was in `apropos-accumulator', sorted | |
587 | alphabetically by symbol name; but this function also sets | |
9cc84e31 RS |
588 | `apropos-accumulator' to nil before returning. |
589 | ||
590 | If SPACING is non-nil, it should be a string; | |
591 | separate items with that string." | |
645c4f6a KH |
592 | (if (null apropos-accumulator) |
593 | (message "No apropos matches for `%s'" apropos-regexp) | |
645c4f6a KH |
594 | (setq apropos-accumulator |
595 | (sort apropos-accumulator (lambda (a b) | |
26a4a227 | 596 | (string-lessp (car a) (car b))))) |
09e32aaf | 597 | (with-output-to-temp-buffer "*Apropos*" |
645c4f6a | 598 | (let ((p apropos-accumulator) |
3925e76d | 599 | (old-buffer (current-buffer)) |
e517f56d | 600 | symbol item) |
26a4a227 KH |
601 | (set-buffer standard-output) |
602 | (apropos-mode) | |
a2ee7919 | 603 | (if (display-mouse-p) |
d94d5b5a | 604 | (insert "If moving the mouse over text changes the text's color,\n" |
1f64403f | 605 | (substitute-command-keys |
e517f56d | 606 | "you can click \\[push-button] on that text to get more information.\n"))) |
53a7d078 | 607 | (insert "In this buffer, go to the name of the command, or function," |
d94d5b5a EZ |
608 | " or variable,\n" |
609 | (substitute-command-keys | |
610 | "and type \\[apropos-follow] to get full documentation.\n\n")) | |
26a4a227 | 611 | (while (consp p) |
9cc84e31 RS |
612 | (when (and spacing (not (bobp))) |
613 | (princ spacing)) | |
26a4a227 KH |
614 | (setq apropos-item (car p) |
615 | symbol (car apropos-item) | |
e517f56d MB |
616 | p (cdr p)) |
617 | (insert-text-button (symbol-name symbol) | |
618 | 'type 'apropos-symbol | |
619 | ;; Can't use default, since user may have | |
620 | ;; changed the variable! | |
621 | ;; Just say `no' to variables containing faces! | |
622 | 'face apropos-symbol-face) | |
98ce2330 | 623 | ;; Calculate key-bindings if we want them. |
26a4a227 KH |
624 | (and do-keys |
625 | (commandp symbol) | |
626 | (indent-to 30 1) | |
98ce2330 RS |
627 | (if (let ((keys |
628 | (save-excursion | |
629 | (set-buffer old-buffer) | |
630 | (where-is-internal symbol))) | |
631 | filtered) | |
632 | ;; Copy over the list of key sequences, | |
633 | ;; omitting any that contain a buffer or a frame. | |
634 | (while keys | |
635 | (let ((key (car keys)) | |
636 | (i 0) | |
637 | loser) | |
638 | (while (< i (length key)) | |
639 | (if (or (framep (aref key i)) | |
640 | (bufferp (aref key i))) | |
641 | (setq loser t)) | |
642 | (setq i (1+ i))) | |
643 | (or loser | |
644 | (setq filtered (cons key filtered)))) | |
645 | (setq keys (cdr keys))) | |
646 | (setq item filtered)) | |
647 | ;; Convert the remaining keys to a string and insert. | |
648 | (insert | |
26a4a227 | 649 | (mapconcat |
98ce2330 | 650 | (lambda (key) |
7a5348db | 651 | (setq key (condition-case () |
c3d0fe18 KH |
652 | (key-description key) |
653 | (error))) | |
98ce2330 | 654 | (if apropos-keybinding-face |
26a4a227 KH |
655 | (put-text-property 0 (length key) |
656 | 'face apropos-keybinding-face | |
98ce2330 RS |
657 | key)) |
658 | key) | |
659 | item ", ")) | |
82fbaa5e RS |
660 | (insert "M-x") |
661 | (put-text-property (- (point) 3) (point) | |
662 | 'face apropos-keybinding-face) | |
663 | (insert " " (symbol-name symbol) " ") | |
664 | (insert "RET") | |
665 | (put-text-property (- (point) 3) (point) | |
666 | 'face apropos-keybinding-face))) | |
26a4a227 | 667 | (terpri) |
894e460c | 668 | (apropos-print-doc 1 |
26a4a227 | 669 | (if (commandp symbol) |
894e460c | 670 | 'apropos-command |
26a4a227 | 671 | (if (apropos-macrop symbol) |
894e460c MB |
672 | 'apropos-macro |
673 | 'apropos-function)) | |
8d7f1de3 | 674 | t) |
894e460c MB |
675 | (apropos-print-doc 2 'apropos-variable t) |
676 | (apropos-print-doc 6 'apropos-group t) | |
677 | (apropos-print-doc 5 'apropos-face t) | |
678 | (apropos-print-doc 4 'apropos-widget t) | |
679 | (apropos-print-doc 3 'apropos-plist nil)) | |
a9155e87 | 680 | (setq buffer-read-only t)))) |
645c4f6a KH |
681 | (prog1 apropos-accumulator |
682 | (setq apropos-accumulator ()))) ; permit gc | |
683 | ||
3925e76d | 684 | |
645c4f6a KH |
685 | (defun apropos-macrop (symbol) |
686 | "T if SYMBOL is a Lisp macro." | |
687 | (and (fboundp symbol) | |
688 | (consp (setq symbol | |
689 | (symbol-function symbol))) | |
690 | (or (eq (car symbol) 'macro) | |
691 | (if (eq (car symbol) 'autoload) | |
692 | (memq (nth 4 symbol) | |
693 | '(macro t)))))) | |
3925e76d | 694 | |
645c4f6a | 695 | |
894e460c | 696 | (defun apropos-print-doc (i type do-keys) |
645c4f6a | 697 | (if (stringp (setq i (nth i apropos-item))) |
3925e76d KH |
698 | (progn |
699 | (insert " ") | |
894e460c MB |
700 | (insert-text-button (button-type-get type 'apropos-label) |
701 | 'type type | |
e517f56d MB |
702 | ;; Can't use the default button face, since |
703 | ;; user may have changed the variable! | |
704 | ;; Just say `no' to variables containing faces! | |
705 | 'face apropos-label-face | |
894e460c | 706 | 'apropos-symbol (car apropos-item)) |
e517f56d | 707 | (insert ": ") |
645c4f6a KH |
708 | (insert (if do-keys (substitute-command-keys i) i)) |
709 | (or (bolp) (terpri))))) | |
3925e76d KH |
710 | |
711 | ||
e517f56d MB |
712 | (defun apropos-follow () |
713 | "Invokes any button at point, otherwise invokes the nearest label button." | |
3925e76d | 714 | (interactive) |
e517f56d MB |
715 | (button-activate |
716 | (or (apropos-next-label-button (line-beginning-position)) | |
717 | (error "There is nothing to follow here")))) | |
3925e76d KH |
718 | |
719 | ||
720 | (defun apropos-describe-plist (symbol) | |
721 | "Display a pretty listing of SYMBOL's plist." | |
722 | (with-output-to-temp-buffer "*Help*" | |
723 | (set-buffer standard-output) | |
724 | (princ "Symbol ") | |
725 | (prin1 symbol) | |
726 | (princ "'s plist is\n (") | |
645c4f6a KH |
727 | (if apropos-symbol-face |
728 | (put-text-property 8 (- (point) 14) 'face apropos-symbol-face)) | |
3925e76d | 729 | (insert (apropos-format-plist symbol "\n ")) |
26a4a227 KH |
730 | (princ ")") |
731 | (print-help-return-message))) | |
6f8e447f | 732 | |
e517f56d | 733 | |
896546cd RS |
734 | (provide 'apropos) |
735 | ||
c0274f38 | 736 | ;;; apropos.el ends here |