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