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