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