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> |
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 | ||
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 | |
cd00fd36 | 155 | (if (or do-all apropos-do-all) |
05942d06 RS |
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 | |
cd00fd36 KH |
185 | (or var-predicate |
186 | (if do-all 'functionp '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)))) | |
a9155e87 KH |
192 | (let ((p apropos-accumulator) |
193 | doc symbol) | |
194 | (while p | |
195 | (setcar p (list | |
196 | (setq symbol (car p)) | |
197 | (unless var-predicate | |
198 | (if (functionp symbol) | |
199 | (if (setq doc (documentation symbol t)) | |
200 | (substring doc 0 (string-match "\n" doc)) | |
201 | "(not documented)"))) | |
202 | (and var-predicate | |
203 | (funcall var-predicate symbol) | |
204 | (if (setq doc (documentation-property | |
205 | symbol 'variable-documentation t)) | |
206 | (substring doc 0 | |
207 | (string-match "\n" doc)))))) | |
208 | (setq p (cdr p)))) | |
209 | (and (apropos-print t nil) | |
210 | message | |
211 | (message message)))) | |
3925e76d KH |
212 | |
213 | ||
3925e76d | 214 | ;;;###autoload |
645c4f6a KH |
215 | (defun apropos (apropos-regexp &optional do-all) |
216 | "Show all bound symbols whose names match REGEXP. | |
217 | With optional prefix ARG or if `apropos-do-all' is non-nil, also show unbound | |
218 | symbols and key bindings, which is a little more time-consuming. | |
6f8e447f | 219 | Returns list of symbols and documentation found." |
3925e76d | 220 | (interactive "sApropos symbol (regexp): \nP") |
645c4f6a KH |
221 | (setq apropos-accumulator |
222 | (apropos-internal apropos-regexp | |
223 | (and (not do-all) | |
224 | (not apropos-do-all) | |
225 | (lambda (symbol) | |
226 | (or (fboundp symbol) | |
227 | (boundp symbol) | |
5edc67d3 | 228 | (facep symbol) |
645c4f6a | 229 | (symbol-plist symbol)))))) |
dea22c45 RS |
230 | (let ((tem apropos-accumulator)) |
231 | (while tem | |
232 | (if (get (car tem) 'apropos-inhibit) | |
233 | (setq apropos-accumulator (delq (car tem) apropos-accumulator))) | |
234 | (setq tem (cdr tem)))) | |
a9155e87 KH |
235 | (let ((p apropos-accumulator) |
236 | symbol doc properties) | |
237 | (while p | |
238 | (setcar p (list | |
239 | (setq symbol (car p)) | |
240 | (when (fboundp symbol) | |
241 | (if (setq doc (condition-case nil | |
242 | (documentation symbol t) | |
243 | (void-function | |
244 | "(alias for undefined function)"))) | |
245 | (substring doc 0 (string-match "\n" doc)) | |
246 | "(not documented)")) | |
247 | (when (boundp symbol) | |
248 | (if (setq doc (documentation-property | |
249 | symbol 'variable-documentation t)) | |
250 | (substring doc 0 (string-match "\n" doc)) | |
251 | "(not documented)")) | |
252 | (when (setq properties (symbol-plist symbol)) | |
253 | (setq doc (list (car properties))) | |
254 | (while (setq properties (cdr (cdr properties))) | |
255 | (setq doc (cons (car properties) doc))) | |
256 | (mapconcat #'symbol-name (nreverse doc) " ")) | |
257 | (when (get symbol 'widget-type) | |
258 | (if (setq doc (documentation-property | |
259 | symbol 'widget-documentation t)) | |
260 | (substring doc 0 | |
261 | (string-match "\n" doc)) | |
262 | "(not documented)")) | |
263 | (when (facep symbol) | |
264 | (if (setq doc (documentation-property | |
265 | symbol 'face-documentation t)) | |
266 | (substring doc 0 | |
267 | (string-match "\n" doc)) | |
268 | "(not documented)")) | |
269 | (when (get symbol 'custom-group) | |
270 | (if (setq doc (documentation-property | |
271 | symbol 'group-documentation t)) | |
272 | (substring doc 0 | |
273 | (string-match "\n" doc)) | |
274 | "(not documented)")))) | |
275 | (setq p (cdr p)))) | |
3925e76d | 276 | (apropos-print |
645c4f6a | 277 | (or do-all apropos-do-all) |
3925e76d KH |
278 | nil)) |
279 | ||
280 | ||
6f8e447f | 281 | ;;;###autoload |
645c4f6a | 282 | (defun apropos-value (apropos-regexp &optional do-all) |
3925e76d KH |
283 | "Show all symbols whose value's printed image matches REGEXP. |
284 | With optional prefix ARG or if `apropos-do-all' is non-nil, also looks | |
285 | at the function and at the names and values of properties. | |
645c4f6a | 286 | Returns list of symbols and values found." |
3925e76d | 287 | (interactive "sApropos value (regexp): \nP") |
645c4f6a KH |
288 | (or do-all (setq do-all apropos-do-all)) |
289 | (setq apropos-accumulator ()) | |
290 | (let (f v p) | |
3925e76d KH |
291 | (mapatoms |
292 | (lambda (symbol) | |
293 | (setq f nil v nil p nil) | |
645c4f6a KH |
294 | (or (memq symbol '(apropos-regexp do-all apropos-accumulator |
295 | symbol f v p)) | |
296 | (setq v (apropos-value-internal 'boundp symbol 'symbol-value))) | |
3925e76d | 297 | (if do-all |
645c4f6a KH |
298 | (setq f (apropos-value-internal 'fboundp symbol 'symbol-function) |
299 | p (apropos-format-plist symbol "\n " t))) | |
3925e76d | 300 | (if (or f v p) |
645c4f6a KH |
301 | (setq apropos-accumulator (cons (list symbol f v p) |
302 | apropos-accumulator)))))) | |
a9155e87 | 303 | (apropos-print nil t)) |
3925e76d KH |
304 | |
305 | ||
645c4f6a KH |
306 | ;;;###autoload |
307 | (defun apropos-documentation (apropos-regexp &optional do-all) | |
26a4a227 | 308 | "Show symbols whose documentation contain matches for REGEXP. |
645c4f6a KH |
309 | With optional prefix ARG or if `apropos-do-all' is non-nil, also use |
310 | documentation that is not stored in the documentation file and show key | |
311 | bindings. | |
312 | Returns list of symbols and documentation found." | |
313 | (interactive "sApropos documentation (regexp): \nP") | |
314 | (or do-all (setq do-all apropos-do-all)) | |
315 | (setq apropos-accumulator () apropos-files-scanned ()) | |
316 | (let ((standard-input (get-buffer-create " apropos-temp")) | |
317 | f v) | |
318 | (unwind-protect | |
319 | (save-excursion | |
320 | (set-buffer standard-input) | |
321 | (apropos-documentation-check-doc-file) | |
322 | (if do-all | |
323 | (mapatoms | |
324 | (lambda (symbol) | |
325 | (setq f (apropos-safe-documentation symbol) | |
26a4a227 KH |
326 | v (get symbol 'variable-documentation)) |
327 | (if (integerp v) (setq v)) | |
328 | (setq f (apropos-documentation-internal f) | |
329 | v (apropos-documentation-internal v)) | |
645c4f6a KH |
330 | (if (or f v) |
331 | (if (setq apropos-item | |
332 | (cdr (assq symbol apropos-accumulator))) | |
333 | (progn | |
334 | (if f | |
335 | (setcar apropos-item f)) | |
336 | (if v | |
337 | (setcar (cdr apropos-item) v))) | |
338 | (setq apropos-accumulator | |
339 | (cons (list symbol f v) | |
340 | apropos-accumulator))))))) | |
a9155e87 | 341 | (apropos-print nil t)) |
645c4f6a KH |
342 | (kill-buffer standard-input)))) |
343 | ||
344 | \f | |
345 | (defun apropos-value-internal (predicate symbol function) | |
346 | (if (funcall predicate symbol) | |
347 | (progn | |
348 | (setq symbol (prin1-to-string (funcall function symbol))) | |
349 | (if (string-match apropos-regexp symbol) | |
350 | (progn | |
351 | (if apropos-match-face | |
352 | (put-text-property (match-beginning 0) (match-end 0) | |
353 | 'face apropos-match-face | |
354 | symbol)) | |
355 | symbol))))) | |
356 | ||
357 | (defun apropos-documentation-internal (doc) | |
358 | (if (consp doc) | |
359 | (apropos-documentation-check-elc-file (car doc)) | |
360 | (and doc | |
361 | (string-match apropos-regexp doc) | |
362 | (progn | |
363 | (if apropos-match-face | |
364 | (put-text-property (match-beginning 0) | |
365 | (match-end 0) | |
366 | 'face apropos-match-face | |
367 | (setq doc (copy-sequence doc)))) | |
368 | doc)))) | |
369 | ||
370 | (defun apropos-format-plist (pl sep &optional compare) | |
3925e76d KH |
371 | (setq pl (symbol-plist pl)) |
372 | (let (p p-out) | |
373 | (while pl | |
374 | (setq p (format "%s %S" (car pl) (nth 1 pl))) | |
645c4f6a KH |
375 | (if (or (not compare) (string-match apropos-regexp p)) |
376 | (if apropos-property-face | |
3925e76d | 377 | (put-text-property 0 (length (symbol-name (car pl))) |
645c4f6a | 378 | 'face apropos-property-face p)) |
3925e76d | 379 | (setq p nil)) |
645c4f6a KH |
380 | (if p |
381 | (progn | |
382 | (and compare apropos-match-face | |
383 | (put-text-property (match-beginning 0) (match-end 0) | |
384 | 'face apropos-match-face | |
385 | p)) | |
386 | (setq p-out (concat p-out (if p-out sep) p)))) | |
3925e76d KH |
387 | (setq pl (nthcdr 2 pl))) |
388 | p-out)) | |
389 | ||
6f8e447f | 390 | |
645c4f6a | 391 | ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name. |
3925e76d | 392 | |
645c4f6a | 393 | (defun apropos-documentation-check-doc-file () |
26a4a227 KH |
394 | (let (type symbol (sepa 2) sepb beg end) |
395 | (insert ?\^_) | |
396 | (backward-char) | |
645c4f6a | 397 | (insert-file-contents (concat doc-directory internal-doc-file-name)) |
26a4a227 KH |
398 | (forward-char) |
399 | (while (save-excursion | |
400 | (setq sepb (search-forward "\^_")) | |
401 | (not (eobp))) | |
402 | (beginning-of-line 2) | |
403 | (if (save-restriction | |
404 | (narrow-to-region (point) (1- sepb)) | |
405 | (re-search-forward apropos-regexp nil t)) | |
406 | (progn | |
407 | (setq beg (match-beginning 0) | |
408 | end (point)) | |
409 | (goto-char (1+ sepa)) | |
410 | (or (setq type (if (eq ?F (preceding-char)) | |
411 | 1 ; function documentation | |
412 | 2) ; variable documentation | |
413 | symbol (read) | |
414 | beg (- beg (point) 1) | |
415 | end (- end (point) 1) | |
416 | doc (buffer-substring (1+ (point)) (1- sepb)) | |
417 | apropos-item (assq symbol apropos-accumulator)) | |
418 | (setq apropos-item (list symbol nil nil) | |
419 | apropos-accumulator (cons apropos-item | |
420 | apropos-accumulator))) | |
421 | (if apropos-match-face | |
422 | (put-text-property beg end 'face apropos-match-face doc)) | |
423 | (setcar (nthcdr type apropos-item) doc))) | |
424 | (setq sepa (goto-char sepb))))) | |
645c4f6a KH |
425 | |
426 | (defun apropos-documentation-check-elc-file (file) | |
427 | (if (member file apropos-files-scanned) | |
428 | nil | |
26a4a227 | 429 | (let (symbol doc beg end this-is-a-variable) |
645c4f6a KH |
430 | (setq apropos-files-scanned (cons file apropos-files-scanned)) |
431 | (erase-buffer) | |
432 | (insert-file-contents file) | |
433 | (while (search-forward "\n#@" nil t) | |
434 | ;; Read the comment length, and advance over it. | |
435 | (setq end (read) | |
26a4a227 KH |
436 | beg (1+ (point)) |
437 | end (+ (point) end -1)) | |
438 | (forward-char) | |
439 | (if (save-restriction | |
440 | ;; match ^ and $ relative to doc string | |
441 | (narrow-to-region beg end) | |
442 | (re-search-forward apropos-regexp nil t)) | |
645c4f6a | 443 | (progn |
26a4a227 KH |
444 | (goto-char (+ end 2)) |
445 | (setq doc (buffer-substring beg end) | |
446 | end (- (match-end 0) beg) | |
447 | beg (- (match-beginning 0) beg) | |
448 | this-is-a-variable (looking-at "(def\\(var\\|const\\) ") | |
645c4f6a KH |
449 | symbol (progn |
450 | (skip-chars-forward "(a-z") | |
26a4a227 | 451 | (forward-char) |
645c4f6a KH |
452 | (read)) |
453 | symbol (if (consp symbol) | |
454 | (nth 1 symbol) | |
455 | symbol)) | |
456 | (if (if this-is-a-variable | |
457 | (get symbol 'variable-documentation) | |
458 | (and (fboundp symbol) (apropos-safe-documentation symbol))) | |
459 | (progn | |
460 | (or (setq apropos-item (assq symbol apropos-accumulator)) | |
461 | (setq apropos-item (list symbol nil nil) | |
462 | apropos-accumulator (cons apropos-item | |
463 | apropos-accumulator))) | |
464 | (if apropos-match-face | |
26a4a227 | 465 | (put-text-property beg end 'face apropos-match-face |
645c4f6a KH |
466 | doc)) |
467 | (setcar (nthcdr (if this-is-a-variable 2 1) | |
468 | apropos-item) | |
26a4a227 | 469 | doc))))))))) |
645c4f6a KH |
470 | |
471 | ||
472 | ||
473 | (defun apropos-safe-documentation (function) | |
6f8e447f RS |
474 | "Like documentation, except it avoids calling `get_doc_string'. |
475 | Will return nil instead." | |
3925e76d | 476 | (while (and function (symbolp function)) |
6f8e447f | 477 | (setq function (if (fboundp function) |
3925e76d | 478 | (symbol-function function)))) |
d2e1218f RS |
479 | (if (eq (car-safe function) 'macro) |
480 | (setq function (cdr function))) | |
3925e76d | 481 | (setq function (if (byte-code-function-p function) |
645c4f6a KH |
482 | (if (> (length function) 4) |
483 | (aref function 4)) | |
484 | (if (eq (car-safe function) 'autoload) | |
485 | (nth 2 function) | |
486 | (if (eq (car-safe function) 'lambda) | |
487 | (if (stringp (nth 2 function)) | |
488 | (nth 2 function) | |
489 | (if (stringp (nth 3 function)) | |
490 | (nth 3 function))))))) | |
491 | (if (integerp function) | |
492 | nil | |
493 | function)) | |
494 | ||
495 | ||
496 | ||
a9155e87 KH |
497 | (defun apropos-print (do-keys spacing) |
498 | "Output result of apropos searching into buffer `*Apropos*'. | |
499 | The value of `apropos-accumulator' is the list of items to output. | |
500 | Each element should have the format (SYMBOL FN-DOC VAR-DOC [PLIST-DOC]). | |
501 | The return value is the list that was in `apropos-accumulator', sorted | |
502 | alphabetically by symbol name; but this function also sets | |
503 | `apropos-accumulator' to nil before returning." | |
645c4f6a KH |
504 | (if (null apropos-accumulator) |
505 | (message "No apropos matches for `%s'" apropos-regexp) | |
645c4f6a KH |
506 | (setq apropos-accumulator |
507 | (sort apropos-accumulator (lambda (a b) | |
26a4a227 | 508 | (string-lessp (car a) (car b))))) |
645c4f6a KH |
509 | (and apropos-label-face |
510 | (symbolp apropos-label-face) | |
511 | (setq apropos-label-face `(face ,apropos-label-face | |
512 | mouse-face highlight))) | |
09e32aaf | 513 | (with-output-to-temp-buffer "*Apropos*" |
645c4f6a | 514 | (let ((p apropos-accumulator) |
3925e76d | 515 | (old-buffer (current-buffer)) |
645c4f6a | 516 | symbol item point1 point2) |
26a4a227 KH |
517 | (set-buffer standard-output) |
518 | (apropos-mode) | |
519 | (if window-system | |
1f64403f EN |
520 | (insert "If you move the mouse over text that changes color,\n" |
521 | (substitute-command-keys | |
3787cf2c | 522 | "you can click \\[apropos-mouse-follow] to get more information.\n"))) |
26a4a227 KH |
523 | (insert (substitute-command-keys |
524 | "In this buffer, type \\[apropos-follow] to get full documentation.\n\n")) | |
525 | (while (consp p) | |
526 | (or (not spacing) (bobp) (terpri)) | |
527 | (setq apropos-item (car p) | |
528 | symbol (car apropos-item) | |
529 | p (cdr p) | |
530 | point1 (point)) | |
531 | (princ symbol) ; print symbol name | |
532 | (setq point2 (point)) | |
98ce2330 | 533 | ;; Calculate key-bindings if we want them. |
26a4a227 KH |
534 | (and do-keys |
535 | (commandp symbol) | |
536 | (indent-to 30 1) | |
98ce2330 RS |
537 | (if (let ((keys |
538 | (save-excursion | |
539 | (set-buffer old-buffer) | |
540 | (where-is-internal symbol))) | |
541 | filtered) | |
542 | ;; Copy over the list of key sequences, | |
543 | ;; omitting any that contain a buffer or a frame. | |
544 | (while keys | |
545 | (let ((key (car keys)) | |
546 | (i 0) | |
547 | loser) | |
548 | (while (< i (length key)) | |
549 | (if (or (framep (aref key i)) | |
550 | (bufferp (aref key i))) | |
551 | (setq loser t)) | |
552 | (setq i (1+ i))) | |
553 | (or loser | |
554 | (setq filtered (cons key filtered)))) | |
555 | (setq keys (cdr keys))) | |
556 | (setq item filtered)) | |
557 | ;; Convert the remaining keys to a string and insert. | |
558 | (insert | |
26a4a227 | 559 | (mapconcat |
98ce2330 | 560 | (lambda (key) |
c3d0fe18 KH |
561 | (setq key (condition-case () |
562 | (key-description key) | |
563 | (error))) | |
98ce2330 | 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) |
045c4971 RS |
592 | ;; We used to use customize-variable-other-window instead |
593 | ;; for a customizable variable, but that is slow. | |
594 | ;; It is better to show an ordinary help buffer | |
595 | ;; and let the user click on the customization button | |
596 | ;; in that buffer, if he wants to. | |
597 | (apropos-print-doc 'describe-variable 2 "Variable" t) | |
8d7f1de3 RS |
598 | (apropos-print-doc 'customize-group-other-window 6 "Group" t) |
599 | (apropos-print-doc 'customize-face-other-window 5 "Face" t) | |
600 | (apropos-print-doc 'widget-browse-other-window 4 "Widget" t) | |
26a4a227 | 601 | (apropos-print-doc 'apropos-describe-plist 3 |
a9155e87 KH |
602 | "Plist" nil)) |
603 | (setq buffer-read-only t)))) | |
645c4f6a KH |
604 | (prog1 apropos-accumulator |
605 | (setq apropos-accumulator ()))) ; permit gc | |
606 | ||
3925e76d | 607 | |
645c4f6a KH |
608 | (defun apropos-macrop (symbol) |
609 | "T if SYMBOL is a Lisp macro." | |
610 | (and (fboundp symbol) | |
611 | (consp (setq symbol | |
612 | (symbol-function symbol))) | |
613 | (or (eq (car symbol) 'macro) | |
614 | (if (eq (car symbol) 'autoload) | |
615 | (memq (nth 4 symbol) | |
616 | '(macro t)))))) | |
3925e76d | 617 | |
645c4f6a KH |
618 | |
619 | (defun apropos-print-doc (action i str do-keys) | |
620 | (if (stringp (setq i (nth i apropos-item))) | |
3925e76d KH |
621 | (progn |
622 | (insert " ") | |
623 | (put-text-property (- (point) 2) (1- (point)) | |
624 | 'action action) | |
645c4f6a KH |
625 | (insert str ": ") |
626 | (if apropos-label-face | |
627 | (add-text-properties (- (point) (length str) 2) | |
3925e76d | 628 | (1- (point)) |
645c4f6a KH |
629 | apropos-label-face)) |
630 | (insert (if do-keys (substitute-command-keys i) i)) | |
631 | (or (bolp) (terpri))))) | |
3925e76d KH |
632 | |
633 | ||
634 | (defun apropos-mouse-follow (event) | |
635 | (interactive "e") | |
26a4a227 | 636 | (let ((other (if (eq (current-buffer) (get-buffer "*Apropos*")) |
3925e76d KH |
637 | () |
638 | (current-buffer)))) | |
17ef0353 RS |
639 | (save-excursion |
640 | (set-buffer (window-buffer (posn-window (event-start event)))) | |
641 | (goto-char (posn-point (event-start event))) | |
642 | (or (and (not (eobp)) (get-text-property (point) 'mouse-face)) | |
643 | (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face)) | |
644 | (error "There is nothing to follow here")) | |
17ef0353 | 645 | (apropos-follow other)))) |
3925e76d KH |
646 | |
647 | ||
648 | (defun apropos-follow (&optional other) | |
649 | (interactive) | |
17ef0353 RS |
650 | (let* (;; Properties are always found at the beginning of the line. |
651 | (bol (save-excursion (beginning-of-line) (point))) | |
652 | ;; If there is no `item' property here, look behind us. | |
653 | (item (get-text-property bol 'item)) | |
654 | (item-at (if item nil (previous-single-property-change bol 'item))) | |
655 | ;; Likewise, if there is no `action' property here, look in front. | |
656 | (action (get-text-property bol 'action)) | |
657 | (action-at (if action nil (next-single-property-change bol 'action)))) | |
658 | (and (null item) item-at | |
659 | (setq item (get-text-property (1- item-at) 'item))) | |
660 | (and (null action) action-at | |
661 | (setq action (get-text-property action-at 'action))) | |
662 | (if (not (and item action)) | |
0a812366 | 663 | (error "There is nothing to follow here")) |
17ef0353 RS |
664 | (if (consp item) (error "There is nothing to follow in `%s'" (car item))) |
665 | (if other (set-buffer other)) | |
666 | (funcall action item))) | |
3925e76d KH |
667 | |
668 | ||
669 | ||
670 | (defun apropos-describe-plist (symbol) | |
671 | "Display a pretty listing of SYMBOL's plist." | |
672 | (with-output-to-temp-buffer "*Help*" | |
673 | (set-buffer standard-output) | |
674 | (princ "Symbol ") | |
675 | (prin1 symbol) | |
676 | (princ "'s plist is\n (") | |
645c4f6a KH |
677 | (if apropos-symbol-face |
678 | (put-text-property 8 (- (point) 14) 'face apropos-symbol-face)) | |
3925e76d | 679 | (insert (apropos-format-plist symbol "\n ")) |
26a4a227 KH |
680 | (princ ")") |
681 | (print-help-return-message))) | |
6f8e447f | 682 | |
896546cd RS |
683 | (provide 'apropos) |
684 | ||
c0274f38 | 685 | ;;; apropos.el ends here |