Commit | Line | Data |
---|---|---|
3925e76d | 1 | ;;; apropos.el --- apropos commands for users and programmers. |
c0274f38 | 2 | |
3925e76d | 3 | ;; Copyright (C) 1989, 1994, 1995 Free Software Foundation, Inc. |
9750e079 | 4 | |
e5167999 | 5 | ;; Author: Joe Wells <jbw@bigbird.bu.edu> |
3925e76d | 6 | ;; Rewritten: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389 |
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" | |
62 | :group 'Help | |
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 | ||
ddd2f740 RS |
74 | (defcustom apropos-symbol-face (if window-system 'bold) |
75 | "*Face for symbol name in apropos output or `nil'. | |
76 | This looks good, but slows down the commands several times." | |
77 | :group 'apropos | |
78 | :type 'face) | |
645c4f6a | 79 | |
ddd2f740 | 80 | (defcustom apropos-keybinding-face (if window-system 'underline) |
645c4f6a | 81 | "*Face for keybinding display in apropos output or `nil'. |
ddd2f740 RS |
82 | This looks good, but slows down the commands several times." |
83 | :group 'apropos | |
84 | :type 'face) | |
645c4f6a | 85 | |
ddd2f740 | 86 | (defcustom apropos-label-face (if window-system 'italic) |
645c4f6a KH |
87 | "*Face for label (Command, Variable ...) in apropos output or `nil'. |
88 | If this is `nil' no mouse highlighting occurs. | |
89 | This looks good, but slows down the commands several times. | |
90 | When this is a face name, as it is initially, it gets transformed to a | |
ddd2f740 RS |
91 | text-property list for efficiency." |
92 | :group 'apropos | |
93 | :type 'face) | |
645c4f6a | 94 | |
ddd2f740 | 95 | (defcustom apropos-property-face (if window-system 'bold-italic) |
645c4f6a | 96 | "*Face for property name in apropos output or `nil'. |
ddd2f740 RS |
97 | This looks good, but slows down the commands several times." |
98 | :group 'apropos | |
99 | :type 'face) | |
645c4f6a | 100 | |
ddd2f740 | 101 | (defcustom apropos-match-face (if window-system 'secondary-selection) |
645c4f6a | 102 | "*Face for matching part in apropos-documentation/value output or `nil'. |
ddd2f740 RS |
103 | This looks good, but slows down the commands several times." |
104 | :group 'apropos | |
105 | :type 'face) | |
3925e76d | 106 | |
6f8e447f | 107 | |
26a4a227 | 108 | (defvar apropos-mode-map |
3925e76d KH |
109 | (let ((map (make-sparse-keymap))) |
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 KH |
114 | (define-key map [mouse-2] 'apropos-mouse-follow) |
115 | (define-key map [down-mouse-2] nil) | |
116 | map) | |
26a4a227 | 117 | "Keymap used in Apropos mode.") |
4de5599d | 118 | |
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 KH |
129 | (defvar apropos-item () |
130 | "Current item in or for apropos-accumulator.") | |
131 | \f | |
26a4a227 KH |
132 | (defun apropos-mode () |
133 | "Major mode for following hyperlinks in output of apropos commands. | |
134 | ||
135 | \\{apropos-mode-map}" | |
136 | (interactive) | |
137 | (kill-all-local-variables) | |
138 | (use-local-map apropos-mode-map) | |
139 | (setq major-mode 'apropos-mode | |
140 | mode-name "Apropos")) | |
141 | ||
f38fd610 | 142 | ;;;###autoload |
05942d06 RS |
143 | (defun apropos-variable (regexp &optional do-all) |
144 | "Show user variables that match REGEXP. | |
145 | With optional prefix ARG or if `apropos-do-all' is non-nil, also show | |
146 | normal variables." | |
147 | (interactive (list (read-string | |
148 | (concat "Apropos " | |
149 | (if (or current-prefix-arg apropos-do-all) | |
150 | "variable" | |
151 | "user option") | |
152 | " (regexp): ")) | |
153 | current-prefix-arg)) | |
154 | (apropos-command regexp nil | |
155 | (if arg | |
156 | #'(lambda (symbol) | |
157 | (and (boundp symbol) | |
158 | (get symbol 'variable-documentation))) | |
159 | 'user-variable-p))) | |
26a4a227 | 160 | |
645c4f6a KH |
161 | ;; For auld lang syne: |
162 | ;;;###autoload | |
163 | (fset 'command-apropos 'apropos-command) | |
6f8e447f | 164 | ;;;###autoload |
05942d06 | 165 | (defun apropos-command (apropos-regexp &optional do-all var-predicate) |
3ccee345 KH |
166 | "Show commands (interactively callable functions) that match REGEXP. |
167 | With optional prefix ARG, or if `apropos-do-all' is non-nil, also show | |
9a909b3c | 168 | noninteractive functions. |
05942d06 | 169 | |
9a909b3c | 170 | If VAR-PREDICATE is non-nil, show only variables, and only those that |
05942d06 | 171 | satisfy the predicate VAR-PREDICATE." |
f38fd610 RS |
172 | (interactive (list (read-string (concat |
173 | "Apropos command " | |
174 | (if (or current-prefix-arg | |
175 | apropos-do-all) | |
9a909b3c | 176 | "or function ") |
f38fd610 | 177 | "(regexp): ")) |
645c4f6a | 178 | current-prefix-arg)) |
3925e76d | 179 | (let ((message |
26a4a227 | 180 | (let ((standard-output (get-buffer-create "*Apropos*"))) |
3925e76d | 181 | (print-help-return-message 'identity)))) |
645c4f6a KH |
182 | (or do-all (setq do-all apropos-do-all)) |
183 | (setq apropos-accumulator | |
184 | (apropos-internal apropos-regexp | |
9a909b3c | 185 | (if do-all 'functionp |
05942d06 | 186 | (or var-predicate 'commandp)))) |
dea22c45 RS |
187 | (let ((tem apropos-accumulator)) |
188 | (while tem | |
189 | (if (get (car tem) 'apropos-inhibit) | |
190 | (setq apropos-accumulator (delq (car tem) apropos-accumulator))) | |
191 | (setq tem (cdr tem)))) | |
3925e76d | 192 | (if (apropos-print |
3925e76d KH |
193 | t |
194 | (lambda (p) | |
195 | (let (doc symbol) | |
196 | (while p | |
197 | (setcar p (list | |
198 | (setq symbol (car p)) | |
9a909b3c RS |
199 | (unless var-predicate |
200 | (if (functionp symbol) | |
201 | (if (setq doc (documentation symbol t)) | |
202 | (substring doc 0 (string-match "\n" doc)) | |
203 | "(not documented)"))) | |
05942d06 RS |
204 | (and var-predicate |
205 | (funcall var-predicate symbol) | |
3925e76d KH |
206 | (if (setq doc (documentation-property |
207 | symbol 'variable-documentation t)) | |
208 | (substring doc 0 | |
209 | (string-match "\n" doc)))))) | |
210 | (setq p (cdr p))))) | |
211 | nil) | |
212 | (and message (message message))))) | |
213 | ||
214 | ||
3925e76d | 215 | ;;;###autoload |
645c4f6a KH |
216 | (defun apropos (apropos-regexp &optional do-all) |
217 | "Show all bound symbols whose names match REGEXP. | |
218 | With optional prefix ARG or if `apropos-do-all' is non-nil, also show unbound | |
219 | symbols and key bindings, which is a little more time-consuming. | |
6f8e447f | 220 | Returns list of symbols and documentation found." |
3925e76d | 221 | (interactive "sApropos symbol (regexp): \nP") |
645c4f6a KH |
222 | (setq apropos-accumulator |
223 | (apropos-internal apropos-regexp | |
224 | (and (not do-all) | |
225 | (not apropos-do-all) | |
226 | (lambda (symbol) | |
227 | (or (fboundp symbol) | |
228 | (boundp symbol) | |
5edc67d3 | 229 | (facep symbol) |
645c4f6a | 230 | (symbol-plist symbol)))))) |
dea22c45 RS |
231 | (let ((tem apropos-accumulator)) |
232 | (while tem | |
233 | (if (get (car tem) 'apropos-inhibit) | |
234 | (setq apropos-accumulator (delq (car tem) apropos-accumulator))) | |
235 | (setq tem (cdr tem)))) | |
3925e76d | 236 | (apropos-print |
645c4f6a | 237 | (or do-all apropos-do-all) |
3925e76d | 238 | (lambda (p) |
d69e3b68 | 239 | (let (symbol doc properties) |
3925e76d KH |
240 | (while p |
241 | (setcar p (list | |
242 | (setq symbol (car p)) | |
d69e3b68 | 243 | (when (fboundp symbol) |
af57e0fa RS |
244 | (if (setq doc (condition-case nil |
245 | (documentation symbol t) | |
246 | (void-function | |
247 | "(alias for undefined function)"))) | |
d69e3b68 KH |
248 | (substring doc 0 (string-match "\n" doc)) |
249 | "(not documented)")) | |
250 | (when (boundp symbol) | |
251 | (if (setq doc (documentation-property | |
252 | symbol 'variable-documentation t)) | |
253 | (substring doc 0 (string-match "\n" doc)) | |
254 | "(not documented)")) | |
255 | (when (setq properties (symbol-plist symbol)) | |
256 | (setq doc (list (car properties))) | |
257 | (while (setq properties (cdr (cdr properties))) | |
258 | (setq doc (cons (car properties) doc))) | |
5edc67d3 RS |
259 | (mapconcat #'symbol-name (nreverse doc) " ")) |
260 | (when (get symbol 'widget-type) | |
261 | (if (setq doc (documentation-property | |
262 | symbol 'widget-documentation t)) | |
263 | (substring doc 0 | |
264 | (string-match "\n" doc)) | |
265 | "(not documented)")) | |
266 | (when (facep symbol) | |
267 | (if (setq doc (documentation-property | |
268 | symbol 'face-documentation t)) | |
269 | (substring doc 0 | |
270 | (string-match "\n" doc)) | |
2be7e3fa RS |
271 | "(not documented)")) |
272 | (when (get symbol 'custom-group) | |
273 | (if (setq doc (documentation-property | |
274 | symbol 'group-documentation t)) | |
275 | (substring doc 0 | |
276 | (string-match "\n" doc)) | |
5edc67d3 | 277 | "(not documented)")))) |
3925e76d KH |
278 | (setq p (cdr p))))) |
279 | nil)) | |
280 | ||
281 | ||
6f8e447f | 282 | ;;;###autoload |
645c4f6a | 283 | (defun apropos-value (apropos-regexp &optional do-all) |
3925e76d KH |
284 | "Show all symbols whose value's printed image matches REGEXP. |
285 | With optional prefix ARG or if `apropos-do-all' is non-nil, also looks | |
286 | at the function and at the names and values of properties. | |
645c4f6a | 287 | Returns list of symbols and values found." |
3925e76d | 288 | (interactive "sApropos value (regexp): \nP") |
645c4f6a KH |
289 | (or do-all (setq do-all apropos-do-all)) |
290 | (setq apropos-accumulator ()) | |
291 | (let (f v p) | |
3925e76d KH |
292 | (mapatoms |
293 | (lambda (symbol) | |
294 | (setq f nil v nil p nil) | |
645c4f6a KH |
295 | (or (memq symbol '(apropos-regexp do-all apropos-accumulator |
296 | symbol f v p)) | |
297 | (setq v (apropos-value-internal 'boundp symbol 'symbol-value))) | |
3925e76d | 298 | (if do-all |
645c4f6a KH |
299 | (setq f (apropos-value-internal 'fboundp symbol 'symbol-function) |
300 | p (apropos-format-plist symbol "\n " t))) | |
3925e76d | 301 | (if (or f v p) |
645c4f6a KH |
302 | (setq apropos-accumulator (cons (list symbol f v p) |
303 | apropos-accumulator)))))) | |
304 | (apropos-print nil nil t)) | |
3925e76d KH |
305 | |
306 | ||
645c4f6a KH |
307 | ;;;###autoload |
308 | (defun apropos-documentation (apropos-regexp &optional do-all) | |
26a4a227 | 309 | "Show symbols whose documentation contain matches for REGEXP. |
645c4f6a KH |
310 | With optional prefix ARG or if `apropos-do-all' is non-nil, also use |
311 | documentation that is not stored in the documentation file and show key | |
312 | bindings. | |
313 | Returns list of symbols and documentation found." | |
314 | (interactive "sApropos documentation (regexp): \nP") | |
315 | (or do-all (setq do-all apropos-do-all)) | |
316 | (setq apropos-accumulator () apropos-files-scanned ()) | |
317 | (let ((standard-input (get-buffer-create " apropos-temp")) | |
318 | f v) | |
319 | (unwind-protect | |
320 | (save-excursion | |
321 | (set-buffer standard-input) | |
322 | (apropos-documentation-check-doc-file) | |
323 | (if do-all | |
324 | (mapatoms | |
325 | (lambda (symbol) | |
326 | (setq f (apropos-safe-documentation symbol) | |
26a4a227 KH |
327 | v (get symbol 'variable-documentation)) |
328 | (if (integerp v) (setq v)) | |
329 | (setq f (apropos-documentation-internal f) | |
330 | v (apropos-documentation-internal v)) | |
645c4f6a KH |
331 | (if (or f v) |
332 | (if (setq apropos-item | |
333 | (cdr (assq symbol apropos-accumulator))) | |
334 | (progn | |
335 | (if f | |
336 | (setcar apropos-item f)) | |
337 | (if v | |
338 | (setcar (cdr apropos-item) v))) | |
339 | (setq apropos-accumulator | |
340 | (cons (list symbol f v) | |
341 | apropos-accumulator))))))) | |
26a4a227 | 342 | (apropos-print nil nil t)) |
645c4f6a KH |
343 | (kill-buffer standard-input)))) |
344 | ||
345 | \f | |
346 | (defun apropos-value-internal (predicate symbol function) | |
347 | (if (funcall predicate symbol) | |
348 | (progn | |
349 | (setq symbol (prin1-to-string (funcall function symbol))) | |
350 | (if (string-match apropos-regexp symbol) | |
351 | (progn | |
352 | (if apropos-match-face | |
353 | (put-text-property (match-beginning 0) (match-end 0) | |
354 | 'face apropos-match-face | |
355 | symbol)) | |
356 | symbol))))) | |
357 | ||
358 | (defun apropos-documentation-internal (doc) | |
359 | (if (consp doc) | |
360 | (apropos-documentation-check-elc-file (car doc)) | |
361 | (and doc | |
362 | (string-match apropos-regexp doc) | |
363 | (progn | |
364 | (if apropos-match-face | |
365 | (put-text-property (match-beginning 0) | |
366 | (match-end 0) | |
367 | 'face apropos-match-face | |
368 | (setq doc (copy-sequence doc)))) | |
369 | doc)))) | |
370 | ||
371 | (defun apropos-format-plist (pl sep &optional compare) | |
3925e76d KH |
372 | (setq pl (symbol-plist pl)) |
373 | (let (p p-out) | |
374 | (while pl | |
375 | (setq p (format "%s %S" (car pl) (nth 1 pl))) | |
645c4f6a KH |
376 | (if (or (not compare) (string-match apropos-regexp p)) |
377 | (if apropos-property-face | |
3925e76d | 378 | (put-text-property 0 (length (symbol-name (car pl))) |
645c4f6a | 379 | 'face apropos-property-face p)) |
3925e76d | 380 | (setq p nil)) |
645c4f6a KH |
381 | (if p |
382 | (progn | |
383 | (and compare apropos-match-face | |
384 | (put-text-property (match-beginning 0) (match-end 0) | |
385 | 'face apropos-match-face | |
386 | p)) | |
387 | (setq p-out (concat p-out (if p-out sep) p)))) | |
3925e76d KH |
388 | (setq pl (nthcdr 2 pl))) |
389 | p-out)) | |
390 | ||
6f8e447f | 391 | |
645c4f6a | 392 | ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name. |
3925e76d | 393 | |
645c4f6a | 394 | (defun apropos-documentation-check-doc-file () |
26a4a227 KH |
395 | (let (type symbol (sepa 2) sepb beg end) |
396 | (insert ?\^_) | |
397 | (backward-char) | |
645c4f6a | 398 | (insert-file-contents (concat doc-directory internal-doc-file-name)) |
26a4a227 KH |
399 | (forward-char) |
400 | (while (save-excursion | |
401 | (setq sepb (search-forward "\^_")) | |
402 | (not (eobp))) | |
403 | (beginning-of-line 2) | |
404 | (if (save-restriction | |
405 | (narrow-to-region (point) (1- sepb)) | |
406 | (re-search-forward apropos-regexp nil t)) | |
407 | (progn | |
408 | (setq beg (match-beginning 0) | |
409 | end (point)) | |
410 | (goto-char (1+ sepa)) | |
411 | (or (setq type (if (eq ?F (preceding-char)) | |
412 | 1 ; function documentation | |
413 | 2) ; variable documentation | |
414 | symbol (read) | |
415 | beg (- beg (point) 1) | |
416 | end (- end (point) 1) | |
417 | doc (buffer-substring (1+ (point)) (1- sepb)) | |
418 | apropos-item (assq symbol apropos-accumulator)) | |
419 | (setq apropos-item (list symbol nil nil) | |
420 | apropos-accumulator (cons apropos-item | |
421 | apropos-accumulator))) | |
422 | (if apropos-match-face | |
423 | (put-text-property beg end 'face apropos-match-face doc)) | |
424 | (setcar (nthcdr type apropos-item) doc))) | |
425 | (setq sepa (goto-char sepb))))) | |
645c4f6a KH |
426 | |
427 | (defun apropos-documentation-check-elc-file (file) | |
428 | (if (member file apropos-files-scanned) | |
429 | nil | |
26a4a227 | 430 | (let (symbol doc beg end this-is-a-variable) |
645c4f6a KH |
431 | (setq apropos-files-scanned (cons file apropos-files-scanned)) |
432 | (erase-buffer) | |
433 | (insert-file-contents file) | |
434 | (while (search-forward "\n#@" nil t) | |
435 | ;; Read the comment length, and advance over it. | |
436 | (setq end (read) | |
26a4a227 KH |
437 | beg (1+ (point)) |
438 | end (+ (point) end -1)) | |
439 | (forward-char) | |
440 | (if (save-restriction | |
441 | ;; match ^ and $ relative to doc string | |
442 | (narrow-to-region beg end) | |
443 | (re-search-forward apropos-regexp nil t)) | |
645c4f6a | 444 | (progn |
26a4a227 KH |
445 | (goto-char (+ end 2)) |
446 | (setq doc (buffer-substring beg end) | |
447 | end (- (match-end 0) beg) | |
448 | beg (- (match-beginning 0) beg) | |
449 | this-is-a-variable (looking-at "(def\\(var\\|const\\) ") | |
645c4f6a KH |
450 | symbol (progn |
451 | (skip-chars-forward "(a-z") | |
26a4a227 | 452 | (forward-char) |
645c4f6a KH |
453 | (read)) |
454 | symbol (if (consp symbol) | |
455 | (nth 1 symbol) | |
456 | symbol)) | |
457 | (if (if this-is-a-variable | |
458 | (get symbol 'variable-documentation) | |
459 | (and (fboundp symbol) (apropos-safe-documentation symbol))) | |
460 | (progn | |
461 | (or (setq apropos-item (assq symbol apropos-accumulator)) | |
462 | (setq apropos-item (list symbol nil nil) | |
463 | apropos-accumulator (cons apropos-item | |
464 | apropos-accumulator))) | |
465 | (if apropos-match-face | |
26a4a227 | 466 | (put-text-property beg end 'face apropos-match-face |
645c4f6a KH |
467 | doc)) |
468 | (setcar (nthcdr (if this-is-a-variable 2 1) | |
469 | apropos-item) | |
26a4a227 | 470 | doc))))))))) |
645c4f6a KH |
471 | |
472 | ||
473 | ||
474 | (defun apropos-safe-documentation (function) | |
6f8e447f RS |
475 | "Like documentation, except it avoids calling `get_doc_string'. |
476 | Will return nil instead." | |
3925e76d | 477 | (while (and function (symbolp function)) |
6f8e447f | 478 | (setq function (if (fboundp function) |
3925e76d | 479 | (symbol-function function)))) |
d2e1218f RS |
480 | (if (eq (car-safe function) 'macro) |
481 | (setq function (cdr function))) | |
3925e76d | 482 | (setq function (if (byte-code-function-p function) |
645c4f6a KH |
483 | (if (> (length function) 4) |
484 | (aref function 4)) | |
485 | (if (eq (car-safe function) 'autoload) | |
486 | (nth 2 function) | |
487 | (if (eq (car-safe function) 'lambda) | |
488 | (if (stringp (nth 2 function)) | |
489 | (nth 2 function) | |
490 | (if (stringp (nth 3 function)) | |
491 | (nth 3 function))))))) | |
492 | (if (integerp function) | |
493 | nil | |
494 | function)) | |
495 | ||
496 | ||
497 | ||
498 | (defun apropos-print (do-keys doc-fn spacing) | |
499 | "Output result of various apropos commands with `apropos-regexp'. | |
500 | APROPOS-ACCUMULATOR is a list. Optional DOC-FN is called for each element | |
05942d06 RS |
501 | of apropos-accumulator and may modify it resulting in (SYMBOL FN-DOC |
502 | VAR-DOC [PLIST-DOC]). Returns sorted list of symbols and documentation | |
3925e76d | 503 | found." |
645c4f6a KH |
504 | (if (null apropos-accumulator) |
505 | (message "No apropos matches for `%s'" apropos-regexp) | |
3925e76d | 506 | (if doc-fn |
645c4f6a KH |
507 | (funcall doc-fn apropos-accumulator)) |
508 | (setq apropos-accumulator | |
509 | (sort apropos-accumulator (lambda (a b) | |
26a4a227 | 510 | (string-lessp (car a) (car b))))) |
645c4f6a KH |
511 | (and apropos-label-face |
512 | (symbolp apropos-label-face) | |
513 | (setq apropos-label-face `(face ,apropos-label-face | |
514 | mouse-face highlight))) | |
09e32aaf | 515 | (with-output-to-temp-buffer "*Apropos*" |
645c4f6a | 516 | (let ((p apropos-accumulator) |
3925e76d | 517 | (old-buffer (current-buffer)) |
645c4f6a | 518 | symbol item point1 point2) |
26a4a227 KH |
519 | (set-buffer standard-output) |
520 | (apropos-mode) | |
521 | (if window-system | |
1f64403f EN |
522 | (insert "If you move the mouse over text that changes color,\n" |
523 | (substitute-command-keys | |
3787cf2c | 524 | "you can click \\[apropos-mouse-follow] to get more information.\n"))) |
26a4a227 KH |
525 | (insert (substitute-command-keys |
526 | "In this buffer, type \\[apropos-follow] to get full documentation.\n\n")) | |
527 | (while (consp p) | |
528 | (or (not spacing) (bobp) (terpri)) | |
529 | (setq apropos-item (car p) | |
530 | symbol (car apropos-item) | |
531 | p (cdr p) | |
532 | point1 (point)) | |
533 | (princ symbol) ; print symbol name | |
534 | (setq point2 (point)) | |
98ce2330 | 535 | ;; Calculate key-bindings if we want them. |
26a4a227 KH |
536 | (and do-keys |
537 | (commandp symbol) | |
538 | (indent-to 30 1) | |
98ce2330 RS |
539 | (if (let ((keys |
540 | (save-excursion | |
541 | (set-buffer old-buffer) | |
542 | (where-is-internal symbol))) | |
543 | filtered) | |
544 | ;; Copy over the list of key sequences, | |
545 | ;; omitting any that contain a buffer or a frame. | |
546 | (while keys | |
547 | (let ((key (car keys)) | |
548 | (i 0) | |
549 | loser) | |
550 | (while (< i (length key)) | |
551 | (if (or (framep (aref key i)) | |
552 | (bufferp (aref key i))) | |
553 | (setq loser t)) | |
554 | (setq i (1+ i))) | |
555 | (or loser | |
556 | (setq filtered (cons key filtered)))) | |
557 | (setq keys (cdr keys))) | |
558 | (setq item filtered)) | |
559 | ;; Convert the remaining keys to a string and insert. | |
560 | (insert | |
26a4a227 | 561 | (mapconcat |
98ce2330 RS |
562 | (lambda (key) |
563 | (setq key (key-description key)) | |
564 | (if apropos-keybinding-face | |
26a4a227 KH |
565 | (put-text-property 0 (length key) |
566 | 'face apropos-keybinding-face | |
98ce2330 RS |
567 | key)) |
568 | key) | |
569 | item ", ")) | |
82fbaa5e RS |
570 | (insert "M-x") |
571 | (put-text-property (- (point) 3) (point) | |
572 | 'face apropos-keybinding-face) | |
573 | (insert " " (symbol-name symbol) " ") | |
574 | (insert "RET") | |
575 | (put-text-property (- (point) 3) (point) | |
576 | 'face apropos-keybinding-face))) | |
26a4a227 KH |
577 | (terpri) |
578 | ;; only now so we don't propagate text attributes all over | |
579 | (put-text-property point1 point2 'item | |
580 | (if (eval `(or ,@(cdr apropos-item))) | |
581 | (car apropos-item) | |
582 | apropos-item)) | |
583 | (if apropos-symbol-face | |
584 | (put-text-property point1 point2 'face apropos-symbol-face)) | |
585 | (apropos-print-doc 'describe-function 1 | |
586 | (if (commandp symbol) | |
587 | "Command" | |
588 | (if (apropos-macrop symbol) | |
589 | "Macro" | |
590 | "Function")) | |
8d7f1de3 | 591 | t) |
5edc67d3 RS |
592 | (if (get symbol 'custom-type) |
593 | (apropos-print-doc 'customize-variable-other-window 2 | |
8d7f1de3 | 594 | "User Option" t) |
5edc67d3 | 595 | (apropos-print-doc 'describe-variable 2 |
8d7f1de3 RS |
596 | "Variable" t)) |
597 | (apropos-print-doc 'customize-group-other-window 6 "Group" t) | |
598 | (apropos-print-doc 'customize-face-other-window 5 "Face" t) | |
599 | (apropos-print-doc 'widget-browse-other-window 4 "Widget" t) | |
26a4a227 KH |
600 | (apropos-print-doc 'apropos-describe-plist 3 |
601 | "Plist" nil))))) | |
645c4f6a KH |
602 | (prog1 apropos-accumulator |
603 | (setq apropos-accumulator ()))) ; permit gc | |
604 | ||
3925e76d | 605 | |
645c4f6a KH |
606 | (defun apropos-macrop (symbol) |
607 | "T if SYMBOL is a Lisp macro." | |
608 | (and (fboundp symbol) | |
609 | (consp (setq symbol | |
610 | (symbol-function symbol))) | |
611 | (or (eq (car symbol) 'macro) | |
612 | (if (eq (car symbol) 'autoload) | |
613 | (memq (nth 4 symbol) | |
614 | '(macro t)))))) | |
3925e76d | 615 | |
645c4f6a KH |
616 | |
617 | (defun apropos-print-doc (action i str do-keys) | |
618 | (if (stringp (setq i (nth i apropos-item))) | |
3925e76d KH |
619 | (progn |
620 | (insert " ") | |
621 | (put-text-property (- (point) 2) (1- (point)) | |
622 | 'action action) | |
645c4f6a KH |
623 | (insert str ": ") |
624 | (if apropos-label-face | |
625 | (add-text-properties (- (point) (length str) 2) | |
3925e76d | 626 | (1- (point)) |
645c4f6a KH |
627 | apropos-label-face)) |
628 | (insert (if do-keys (substitute-command-keys i) i)) | |
629 | (or (bolp) (terpri))))) | |
3925e76d KH |
630 | |
631 | ||
632 | (defun apropos-mouse-follow (event) | |
633 | (interactive "e") | |
26a4a227 | 634 | (let ((other (if (eq (current-buffer) (get-buffer "*Apropos*")) |
3925e76d KH |
635 | () |
636 | (current-buffer)))) | |
17ef0353 RS |
637 | (save-excursion |
638 | (set-buffer (window-buffer (posn-window (event-start event)))) | |
639 | (goto-char (posn-point (event-start event))) | |
640 | (or (and (not (eobp)) (get-text-property (point) 'mouse-face)) | |
641 | (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face)) | |
642 | (error "There is nothing to follow here")) | |
17ef0353 | 643 | (apropos-follow other)))) |
3925e76d KH |
644 | |
645 | ||
646 | (defun apropos-follow (&optional other) | |
647 | (interactive) | |
17ef0353 RS |
648 | (let* (;; Properties are always found at the beginning of the line. |
649 | (bol (save-excursion (beginning-of-line) (point))) | |
650 | ;; If there is no `item' property here, look behind us. | |
651 | (item (get-text-property bol 'item)) | |
652 | (item-at (if item nil (previous-single-property-change bol 'item))) | |
653 | ;; Likewise, if there is no `action' property here, look in front. | |
654 | (action (get-text-property bol 'action)) | |
655 | (action-at (if action nil (next-single-property-change bol 'action)))) | |
656 | (and (null item) item-at | |
657 | (setq item (get-text-property (1- item-at) 'item))) | |
658 | (and (null action) action-at | |
659 | (setq action (get-text-property action-at 'action))) | |
660 | (if (not (and item action)) | |
0a812366 | 661 | (error "There is nothing to follow here")) |
17ef0353 RS |
662 | (if (consp item) (error "There is nothing to follow in `%s'" (car item))) |
663 | (if other (set-buffer other)) | |
664 | (funcall action item))) | |
3925e76d KH |
665 | |
666 | ||
667 | ||
668 | (defun apropos-describe-plist (symbol) | |
669 | "Display a pretty listing of SYMBOL's plist." | |
670 | (with-output-to-temp-buffer "*Help*" | |
671 | (set-buffer standard-output) | |
672 | (princ "Symbol ") | |
673 | (prin1 symbol) | |
674 | (princ "'s plist is\n (") | |
645c4f6a KH |
675 | (if apropos-symbol-face |
676 | (put-text-property 8 (- (point) 14) 'face apropos-symbol-face)) | |
3925e76d | 677 | (insert (apropos-format-plist symbol "\n ")) |
26a4a227 KH |
678 | (princ ")") |
679 | (print-help-return-message))) | |
6f8e447f | 680 | |
896546cd RS |
681 | (provide 'apropos) |
682 | ||
c0274f38 | 683 | ;;; apropos.el ends here |