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