Fix more typos.
[bpt/emacs.git] / lisp / apropos.el
CommitLineData
e8af40ee 1;;; apropos.el --- apropos commands for users and programmers
c0274f38 2
73b0cd50 3;; Copyright (C) 1989, 1994-1995, 2001-2011 Free Software Foundation, Inc.
9750e079 4
e5167999 5;; Author: Joe Wells <jbw@bigbird.bu.edu>
ba32b5d2 6;; Daniel Pfeiffer <occitan@esperanto.org> (rewrite)
e9571d2a 7;; Keywords: help
aad4679e 8;; Package: emacs
e5167999 9
6f8e447f
RS
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
6f8e447f 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
6f8e447f
RS
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
6f8e447f 24
e5167999 25;;; Commentary:
6f8e447f
RS
26
27;; The ideas for this package were derived from the C code in
28;; src/keymap.c and elsewhere. The functions in this file should
29;; always be byte-compiled for speed. Someone should rewrite this in
30;; C (as part of src/keymap.c) for speed.
31
32;; The idea for super-apropos is based on the original implementation
33;; by Lynn Slater <lrs@esl.com>.
34
35;; History:
36;; Fixed bug, current-local-map can return nil.
37;; Change, doesn't calculate key-bindings unless needed.
38;; Added super-apropos capability, changed print functions.
3925e76d
KH
39;;; Made fast-apropos and super-apropos share code.
40;;; Sped up fast-apropos again.
6f8e447f 41;; Added apropos-do-all option.
3925e76d 42;;; Added fast-command-apropos.
6f8e447f 43;; Changed doc strings to comments for helping functions.
3925e76d 44;;; Made doc file buffer read-only, buried it.
6f8e447f
RS
45;; Only call substitute-command-keys if do-all set.
46
645c4f6a
KH
47;; Optionally use configurable faces to make the output more legible.
48;; Differentiate between command, function and macro.
3925e76d
KH
49;; Apropos-command (ex command-apropos) does cmd and optionally user var.
50;; Apropos shows all 3 aspects of symbols (fn, var and plist)
51;; Apropos-documentation (ex super-apropos) now finds all it should.
52;; New apropos-value snoops through all values and optionally plists.
53;; Reading DOC file doesn't load nroff.
54;; Added hypertext following of documentation, mouse-2 on variable gives value
55;; from buffer in active window.
56
e5167999
ER
57;;; Code:
58
851befd4 59(require 'button)
caa8e7aa 60(eval-when-compile (require 'cl))
851befd4 61
ddd2f740 62(defgroup apropos nil
32463e4d 63 "Apropos commands for users and programmers."
c906e3ab 64 :group 'help
ddd2f740
RS
65 :prefix "apropos")
66
3925e76d 67;; I see a degradation of maybe 10-20% only.
ddd2f740 68(defcustom apropos-do-all nil
671c04d9 69 "Whether the apropos commands should do more.
ddd2f740
RS
70
71Slows them down more or less. Set this non-nil if you have a fast machine."
72 :group 'apropos
73 :type 'boolean)
3925e76d
KH
74
75
07eeca5d 76(defcustom apropos-symbol-face 'bold
671c04d9 77 "Face for symbol name in Apropos output, or nil for none."
ddd2f740
RS
78 :group 'apropos
79 :type 'face)
645c4f6a 80
07eeca5d 81(defcustom apropos-keybinding-face 'underline
671c04d9 82 "Face for lists of keybinding in Apropos output, or nil for none."
ddd2f740
RS
83 :group 'apropos
84 :type 'face)
645c4f6a 85
4ef177aa 86(defcustom apropos-label-face '(italic)
671c04d9 87 "Face for label (`Command', `Variable' ...) in Apropos output.
07eeca5d
RS
88A value of nil means don't use any special font for them, and also
89turns off mouse highlighting."
ddd2f740
RS
90 :group 'apropos
91 :type 'face)
645c4f6a 92
07eeca5d 93(defcustom apropos-property-face 'bold-italic
671c04d9 94 "Face for property name in apropos output, or nil for none."
ddd2f740
RS
95 :group 'apropos
96 :type 'face)
645c4f6a 97
5248b3e3 98(defcustom apropos-match-face 'match
671c04d9 99 "Face for matching text in Apropos documentation/value, or nil for none.
07eeca5d 100This applies when you look for matches in the documentation or variable value
0820b753 101for the pattern; the part that matches gets displayed in this font."
ddd2f740
RS
102 :group 'apropos
103 :type 'face)
3925e76d 104
ab765ff7 105(defcustom apropos-sort-by-scores nil
671c04d9 106 "Non-nil means sort matches by scores; best match is shown first.
0820b753
KS
107This applies to all `apropos' commands except `apropos-documentation'.
108If value is `verbose', the computed score is shown for each match."
30aab741 109 :group 'apropos
0820b753
KS
110 :type '(choice (const :tag "off" nil)
111 (const :tag "on" t)
112 (const :tag "show scores" verbose)))
113
114(defcustom apropos-documentation-sort-by-scores t
9201cc28 115 "Non-nil means sort matches by scores; best match is shown first.
0820b753
KS
116This applies to `apropos-documentation' only.
117If value is `verbose', the computed score is shown for each match."
118 :group 'apropos
119 :type '(choice (const :tag "off" nil)
120 (const :tag "on" t)
121 (const :tag "show scores" verbose)))
6f8e447f 122
26a4a227 123(defvar apropos-mode-map
abef340a
SS
124 (let ((map (copy-keymap button-buffer-map)))
125 (set-keymap-parent map special-mode-map)
e517f56d
MB
126 ;; Use `apropos-follow' instead of just using the button
127 ;; definition of RET, so that users can use it anywhere in an
128 ;; apropos item, not just on top of a button.
3925e76d 129 (define-key map "\C-m" 'apropos-follow)
3925e76d 130 map)
26a4a227 131 "Keymap used in Apropos mode.")
4de5599d 132
45f485a6 133(defvar apropos-mode-hook nil
671c04d9 134 "Hook run when mode is turned on.")
3925e76d 135
fe8bc3fa 136(defvar apropos-pattern nil
0820b753
KS
137 "Apropos pattern as entered by user.")
138
139(defvar apropos-pattern-quoted nil
c6b19225 140 "Apropos pattern passed through `regexp-quote'.")
0820b753
KS
141
142(defvar apropos-words ()
143 "Current list of apropos words extracted from `apropos-pattern'.")
645c4f6a 144
0820b753
KS
145(defvar apropos-all-words ()
146 "Current list of words and synonyms.")
7dbffb1c 147
0820b753
KS
148(defvar apropos-regexp nil
149 "Regexp used in current apropos run.")
150
151(defvar apropos-all-words-regexp nil
7dbffb1c
KS
152 "Regexp matching apropos-all-words.")
153
645c4f6a 154(defvar apropos-files-scanned ()
1259a080 155 "List of elc files already scanned in current run of `apropos-documentation'.")
645c4f6a
KH
156
157(defvar apropos-accumulator ()
4ef177aa
CY
158 "Alist of symbols already found in current apropos run.
159Each element has the form
160
161 (SYMBOL SCORE FUN-DOC VAR-DOC PLIST WIDGET-DOC FACE-DOC CUS-GROUP-DOC)
162
163where SYMBOL is the symbol name, SCORE is its relevance score (a
164number), FUN-DOC is the function docstring, VAR-DOC is the
165variable docstring, PLIST is the list of the symbols names in the
166property list, WIDGET-DOC is the widget docstring, FACE-DOC is
167the face docstring, and CUS-GROUP-DOC is the custom group
168docstring. Each docstring is either nil or a string.")
3925e76d 169
645c4f6a 170(defvar apropos-item ()
7a5348db 171 "Current item in or for `apropos-accumulator'.")
e517f56d 172
7dbffb1c
KS
173(defvar apropos-synonyms '(
174 ("find" "open" "edit")
175 ("kill" "cut")
f4517e51
KS
176 ("yank" "paste")
177 ("region" "selection"))
7dbffb1c 178 "List of synonyms known by apropos.
5e24ee14 179Each element is a list of words where the first word is the standard Emacs
7dbffb1c
KS
180term, and the rest of the words are alternative terms.")
181
e517f56d
MB
182\f
183;;; Button types used by apropos
184
185(define-button-type 'apropos-symbol
186 'face apropos-symbol-face
894e460c 187 'help-echo "mouse-2, RET: Display more help on this symbol"
d8fac801 188 'follow-link t
60f4c0c8 189 'action #'apropos-symbol-button-display-help)
e517f56d
MB
190
191(defun apropos-symbol-button-display-help (button)
192 "Display further help for the `apropos-symbol' button BUTTON."
193 (button-activate
194 (or (apropos-next-label-button (button-start button))
195 (error "There is nothing to follow for `%s'" (button-label button)))))
196
894e460c
MB
197(define-button-type 'apropos-function
198 'apropos-label "Function"
1d69bd9b 199 'apropos-short-label "f"
4ef177aa 200 'face '(font-lock-function-name-face button)
d8fac801
KS
201 'help-echo "mouse-2, RET: Display more help on this function"
202 'follow-link t
894e460c 203 'action (lambda (button)
d8fac801
KS
204 (describe-function (button-get button 'apropos-symbol))))
205
894e460c
MB
206(define-button-type 'apropos-macro
207 'apropos-label "Macro"
1d69bd9b 208 'apropos-short-label "m"
4ef177aa 209 'face '(font-lock-function-name-face button)
d8fac801
KS
210 'help-echo "mouse-2, RET: Display more help on this macro"
211 'follow-link t
894e460c 212 'action (lambda (button)
d8fac801
KS
213 (describe-function (button-get button 'apropos-symbol))))
214
894e460c
MB
215(define-button-type 'apropos-command
216 'apropos-label "Command"
1d69bd9b 217 'apropos-short-label "c"
4ef177aa 218 'face '(font-lock-function-name-face button)
d8fac801
KS
219 'help-echo "mouse-2, RET: Display more help on this command"
220 'follow-link t
894e460c 221 'action (lambda (button)
d8fac801 222 (describe-function (button-get button 'apropos-symbol))))
7cfedc97 223
894e460c
MB
224;; We used to use `customize-variable-other-window' instead for a
225;; customizable variable, but that is slow. It is better to show an
226;; ordinary help buffer and let the user click on the customization
227;; button in that buffer, if he wants to.
228;; Likewise for `customize-face-other-window'.
229(define-button-type 'apropos-variable
230 'apropos-label "Variable"
1d69bd9b 231 'apropos-short-label "v"
4ef177aa 232 'face '(font-lock-variable-name-face button)
894e460c 233 'help-echo "mouse-2, RET: Display more help on this variable"
d8fac801 234 'follow-link t
894e460c
MB
235 'action (lambda (button)
236 (describe-variable (button-get button 'apropos-symbol))))
237
238(define-button-type 'apropos-face
239 'apropos-label "Face"
1d69bd9b 240 'apropos-short-label "F"
4ef177aa 241 'face '(font-lock-variable-name-face button)
894e460c 242 'help-echo "mouse-2, RET: Display more help on this face"
d8fac801 243 'follow-link t
894e460c
MB
244 'action (lambda (button)
245 (describe-face (button-get button 'apropos-symbol))))
246
247(define-button-type 'apropos-group
248 'apropos-label "Group"
1d69bd9b 249 'apropos-short-label "g"
4ef177aa 250 'face '(font-lock-builtin-face button)
894e460c 251 'help-echo "mouse-2, RET: Display more help on this group"
d8fac801 252 'follow-link t
894e460c 253 'action (lambda (button)
2d37b91e 254 (customize-group-other-window
894e460c
MB
255 (button-get button 'apropos-symbol))))
256
257(define-button-type 'apropos-widget
258 'apropos-label "Widget"
1d69bd9b 259 'apropos-short-label "w"
4ef177aa 260 'face '(font-lock-builtin-face button)
894e460c 261 'help-echo "mouse-2, RET: Display more help on this widget"
d8fac801 262 'follow-link t
894e460c
MB
263 'action (lambda (button)
264 (widget-browse-other-window (button-get button 'apropos-symbol))))
265
266(define-button-type 'apropos-plist
4ef177aa 267 'apropos-label "Properties"
1d69bd9b 268 'apropos-short-label "p"
4ef177aa 269 'face '(font-lock-keyword-face button)
894e460c 270 'help-echo "mouse-2, RET: Display more help on this plist"
d8fac801 271 'follow-link t
894e460c
MB
272 'action (lambda (button)
273 (apropos-describe-plist (button-get button 'apropos-symbol))))
e517f56d 274
2e3d43ac
SM
275(define-button-type 'apropos-library
276 'help-echo "mouse-2, RET: Display more help on this library"
277 'follow-link t
278 'action (lambda (button)
279 (apropos-library (button-get button 'apropos-symbol))))
280
e517f56d 281(defun apropos-next-label-button (pos)
a11a4e9f 282 "Return the next apropos label button after POS, or nil if there's none.
e517f56d
MB
283Will also return nil if more than one `apropos-symbol' button is encountered
284before finding a label."
a101302b 285 (let* ((button (next-button pos t))
e517f56d 286 (already-hit-symbol nil)
3b8c60f1
MB
287 (label (and button (button-get button 'apropos-label)))
288 (type (and button (button-get button 'type))))
e517f56d 289 (while (and button
3b8c60f1
MB
290 (not label)
291 (or (not (eq type 'apropos-symbol))
e517f56d 292 (not already-hit-symbol)))
3b8c60f1 293 (when (eq type 'apropos-symbol)
e517f56d
MB
294 (setq already-hit-symbol t))
295 (setq button (next-button (button-start button)))
296 (when button
3b8c60f1
MB
297 (setq label (button-get button 'apropos-label))
298 (setq type (button-get button 'type))))
299 (and label button)))
e517f56d 300
645c4f6a 301\f
7dbffb1c
KS
302(defun apropos-words-to-regexp (words wild)
303 "Make regexp matching any two of the words in WORDS."
304 (concat "\\("
305 (mapconcat 'identity words "\\|")
80f5d2ef 306 "\\)"
71296446 307 (if (cdr words)
80f5d2ef
AS
308 (concat wild
309 "\\("
7dbffb1c
KS
310 (mapconcat 'identity words "\\|")
311 "\\)")
312 "")))
313
0820b753
KS
314;;;###autoload
315(defun apropos-read-pattern (subject)
316 "Read an apropos pattern, either a word list or a regexp.
317Returns the user pattern, either a list of words which are matched
318literally, or a string which is used as a regexp to search for.
319
320SUBJECT is a string that is included in the prompt to identify what
321kind of objects to search."
322 (let ((pattern
323 (read-string (concat "Apropos " subject " (word list or regexp): "))))
324 (if (string-equal (regexp-quote pattern) pattern)
325 ;; Split into words
326 (split-string pattern "[ \t]+")
327 pattern)))
328
329(defun apropos-parse-pattern (pattern)
330 "Rewrite a list of words to a regexp matching all permutations.
3fefda51
KS
331If PATTERN is a string, that means it is already a regexp.
332This updates variables `apropos-pattern', `apropos-pattern-quoted',
333`apropos-regexp', `apropos-words', and `apropos-all-words-regexp'."
0820b753
KS
334 (setq apropos-words nil
335 apropos-all-words nil)
336 (if (consp pattern)
7dbffb1c
KS
337 ;; We don't actually make a regexp matching all permutations.
338 ;; Instead, for e.g. "a b c", we make a regexp matching
339 ;; any combination of two or more words like this:
340 ;; (a|b|c).*(a|b|c) which may give some false matches,
341 ;; but as long as it also gives the right ones, that's ok.
0820b753
KS
342 (let ((words pattern))
343 (setq apropos-pattern (mapconcat 'identity pattern " ")
344 apropos-pattern-quoted (regexp-quote apropos-pattern))
7dbffb1c
KS
345 (dolist (word words)
346 (let ((syn apropos-synonyms) (s word) (a word))
347 (while syn
348 (if (member word (car syn))
349 (progn
350 (setq a (mapconcat 'identity (car syn) "\\|"))
351 (if (member word (cdr (car syn)))
352 (setq s a))
353 (setq syn nil))
354 (setq syn (cdr syn))))
355 (setq apropos-words (cons s apropos-words)
356 apropos-all-words (cons a apropos-all-words))))
3fefda51
KS
357 (setq apropos-all-words-regexp
358 (apropos-words-to-regexp apropos-all-words ".+"))
359 (setq apropos-regexp
360 (apropos-words-to-regexp apropos-words ".*?")))
0820b753
KS
361 (setq apropos-pattern-quoted (regexp-quote pattern)
362 apropos-all-words-regexp pattern
3fefda51
KS
363 apropos-pattern pattern
364 apropos-regexp pattern)))
0820b753 365
7dbffb1c
KS
366
367(defun apropos-calc-scores (str words)
368 "Return apropos scores for string STR matching WORDS.
369Value is a list of offsets of the words into the string."
0820b753 370 (let (scores i)
7dbffb1c
KS
371 (if words
372 (dolist (word words scores)
373 (if (setq i (string-match word str))
374 (setq scores (cons i scores))))
375 ;; Return list of start and end position of regexp
347a20b8 376 (and (string-match apropos-pattern str)
0820b753 377 (list (match-beginning 0) (match-end 0))))))
7dbffb1c
KS
378
379(defun apropos-score-str (str)
380 "Return apropos score for string STR."
381 (if str
0820b753
KS
382 (let* ((l (length str))
383 (score (- (/ l 10))))
7dbffb1c 384 (dolist (s (apropos-calc-scores str apropos-all-words) score)
d5857a96 385 (setq score (+ score 1000 (/ (* (- l s) 1000) l)))))
7dbffb1c
KS
386 0))
387
388(defun apropos-score-doc (doc)
389 "Return apropos score for documentation string DOC."
b7a2a696
LK
390 (let ((l (length doc)))
391 (if (> l 0)
06b60517
JB
392 (let ((score 0))
393 (when (string-match apropos-pattern-quoted doc)
0820b753 394 (setq score 10000))
b7a2a696
LK
395 (dolist (s (apropos-calc-scores doc apropos-all-words) score)
396 (setq score (+ score 50 (/ (* (- l s) 50) l)))))
397 0)))
71296446 398
7dbffb1c
KS
399(defun apropos-score-symbol (symbol &optional weight)
400 "Return apropos score for SYMBOL."
401 (setq symbol (symbol-name symbol))
402 (let ((score 0)
0820b753 403 (l (length symbol)))
7dbffb1c
KS
404 (dolist (s (apropos-calc-scores symbol apropos-words) (* score (or weight 3)))
405 (setq score (+ score (- 60 l) (/ (* (- l s) 60) l))))))
406
d2b30292
KS
407(defun apropos-true-hit (str words)
408 "Return t if STR is a genuine hit.
409This may fail if only one of the keywords is matched more than once.
410This requires that at least 2 keywords (unless only one was given)."
411 (or (not str)
412 (not words)
413 (not (cdr words))
414 (> (length (apropos-calc-scores str words)) 1)))
415
416(defun apropos-false-hit-symbol (symbol)
417 "Return t if SYMBOL is not really matched by the current keywords."
418 (not (apropos-true-hit (symbol-name symbol) apropos-words)))
419
420(defun apropos-false-hit-str (str)
421 "Return t if STR is not really matched by the current keywords."
422 (not (apropos-true-hit str apropos-words)))
423
424(defun apropos-true-hit-doc (doc)
425 "Return t if DOC is really matched by the current keywords."
426 (apropos-true-hit doc apropos-all-words))
427
abef340a 428(define-derived-mode apropos-mode special-mode "Apropos"
26a4a227
KH
429 "Major mode for following hyperlinks in output of apropos commands.
430
38ab866c 431\\{apropos-mode-map}")
26a4a227 432
1d69bd9b
SM
433(defvar apropos-multi-type t
434 "If non-nil, this apropos query concerns multiple types.
435This is used to decide whether to print the result's type or not.")
436
f38fd610 437;;;###autoload
0820b753
KS
438(defun apropos-variable (pattern &optional do-all)
439 "Show user variables that match PATTERN.
440PATTERN can be a word, a list of words (separated by spaces),
441or a regexp (using some regexp special characters). If it is a word,
442search for matches for that word as a substring. If it is a list of words,
443search for matches for any two (or more) of those words.
444
445With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
05942d06 446normal variables."
0820b753
KS
447 (interactive (list (apropos-read-pattern
448 (if (or current-prefix-arg apropos-do-all)
449 "variable" "user option"))
05942d06 450 current-prefix-arg))
0820b753 451 (apropos-command pattern nil
cd00fd36 452 (if (or do-all apropos-do-all)
05942d06
RS
453 #'(lambda (symbol)
454 (and (boundp symbol)
455 (get symbol 'variable-documentation)))
456 'user-variable-p)))
26a4a227 457
645c4f6a
KH
458;; For auld lang syne:
459;;;###autoload
82e736c1 460(defalias 'command-apropos 'apropos-command)
6f8e447f 461;;;###autoload
0820b753
KS
462(defun apropos-command (pattern &optional do-all var-predicate)
463 "Show commands (interactively callable functions) that match PATTERN.
464PATTERN can be a word, a list of words (separated by spaces),
fe8bc3fa
RS
465or a regexp (using some regexp special characters). If it is a word,
466search for matches for that word as a substring. If it is a list of words,
467search for matches for any two (or more) of those words.
468
0820b753 469With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
9a909b3c 470noninteractive functions.
05942d06 471
9a909b3c 472If VAR-PREDICATE is non-nil, show only variables, and only those that
0820b753
KS
473satisfy the predicate VAR-PREDICATE.
474
475When called from a Lisp program, a string PATTERN is used as a regexp,
476while a list of strings is used as a word list."
477 (interactive (list (apropos-read-pattern
478 (if (or current-prefix-arg apropos-do-all)
479 "command or function" "command"))
645c4f6a 480 current-prefix-arg))
3fefda51 481 (apropos-parse-pattern pattern)
c851bcec 482 (let ((message
26a4a227 483 (let ((standard-output (get-buffer-create "*Apropos*")))
d5d105e8 484 (help-print-return-message 'identity))))
645c4f6a
KH
485 (or do-all (setq do-all apropos-do-all))
486 (setq apropos-accumulator
0820b753 487 (apropos-internal apropos-regexp
cd00fd36 488 (or var-predicate
554fde6e
SM
489 ;; We used to use `functionp' here, but this
490 ;; rules out macros. `fboundp' rules in
491 ;; keymaps, but it seems harmless.
492 (if do-all 'fboundp 'commandp))))
dea22c45
RS
493 (let ((tem apropos-accumulator))
494 (while tem
d2b30292
KS
495 (if (or (get (car tem) 'apropos-inhibit)
496 (apropos-false-hit-symbol (car tem)))
dea22c45
RS
497 (setq apropos-accumulator (delq (car tem) apropos-accumulator)))
498 (setq tem (cdr tem))))
a9155e87 499 (let ((p apropos-accumulator)
7dbffb1c 500 doc symbol score)
a9155e87
KH
501 (while p
502 (setcar p (list
503 (setq symbol (car p))
7dbffb1c 504 (setq score (apropos-score-symbol symbol))
a9155e87 505 (unless var-predicate
554fde6e 506 (if (fboundp symbol)
9da97cf0
GM
507 (if (setq doc (condition-case nil
508 (documentation symbol t)
509 (error 'error)))
510 ;; Eg alias to undefined function.
511 (if (eq doc 'error)
512 "(documentation error)"
71296446 513 (setq score (+ score (apropos-score-doc doc)))
7dbffb1c 514 (substring doc 0 (string-match "\n" doc)))
a9155e87
KH
515 "(not documented)")))
516 (and var-predicate
517 (funcall var-predicate symbol)
518 (if (setq doc (documentation-property
519 symbol 'variable-documentation t))
7dbffb1c
KS
520 (progn
521 (setq score (+ score (apropos-score-doc doc)))
522 (substring doc 0
523 (string-match "\n" doc)))))))
524 (setcar (cdr (car p)) score)
a9155e87 525 (setq p (cdr p))))
1d69bd9b
SM
526 (and (let ((apropos-multi-type do-all))
527 (apropos-print t nil nil t))
a9155e87 528 message
8a26c165 529 (message "%s" message))))
3925e76d
KH
530
531
3925e76d 532;;;###autoload
914b40da
RS
533(defun apropos-documentation-property (symbol property raw)
534 "Like (documentation-property SYMBOL PROPERTY RAW) but handle errors."
535 (condition-case ()
536 (let ((doc (documentation-property symbol property raw)))
537 (if doc (substring doc 0 (string-match "\n" doc))
538 "(not documented)"))
539 (error "(error retrieving documentation)")))
540
5760219d
JPW
541
542;;;###autoload
0820b753 543(defun apropos (pattern &optional do-all)
2a4ec7e1
RS
544 "Show all meaningful Lisp symbols whose names match PATTERN.
545Symbols are shown if they are defined as functions, variables, or
546faces, or if they have nonempty property lists.
547
0820b753 548PATTERN can be a word, a list of words (separated by spaces),
fe8bc3fa
RS
549or a regexp (using some regexp special characters). If it is a word,
550search for matches for that word as a substring. If it is a list of words,
551search for matches for any two (or more) of those words.
552
543e570f
RS
553With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil,
554consider all symbols (if they match PATTERN).
555
556Returns list of symbols and documentation found."
0820b753
KS
557 (interactive (list (apropos-read-pattern "symbol")
558 current-prefix-arg))
3fefda51 559 (apropos-parse-pattern pattern)
caa8e7aa 560 (apropos-symbols-internal
0820b753 561 (apropos-internal apropos-regexp
543e570f
RS
562 (and (not do-all)
563 (not apropos-do-all)
564 (lambda (symbol)
565 (or (fboundp symbol)
566 (boundp symbol)
567 (facep symbol)
568 (symbol-plist symbol)))))
caa8e7aa
SM
569 (or do-all apropos-do-all)))
570
2e3d43ac
SM
571(defun apropos-library-button (sym)
572 (if (null sym)
573 "<nothing>"
574 (let ((name (copy-sequence (symbol-name sym))))
575 (make-text-button name nil
576 'type 'apropos-library
577 'face apropos-symbol-face
578 'apropos-symbol name)
579 name)))
580
581;;;###autoload
582(defun apropos-library (file)
583 "List the variables and functions defined by library FILE.
584FILE should be one of the libraries currently loaded and should
585thus be found in `load-history'."
586 (interactive
47529322
GM
587 (let* ((libs (delq nil (mapcar 'car load-history)))
588 (libs
589 (nconc (delq nil
590 (mapcar
591 (lambda (l)
592 (setq l (file-name-nondirectory l))
593 (while
594 (not (equal (setq l (file-name-sans-extension l))
595 l)))
596 l)
597 libs))
598 libs)))
2e3d43ac
SM
599 (list (completing-read "Describe library: " libs nil t))))
600 (let ((symbols nil)
601 ;; (autoloads nil)
602 (provides nil)
603 (requires nil)
604 (lh-entry (assoc file load-history)))
605 (unless lh-entry
606 ;; `file' may be the "shortname".
607 (let ((lh load-history)
608 (re (concat "\\(?:\\`\\|[\\/]\\)" (regexp-quote file)
609 "\\(\\.\\|\\'\\)")))
610 (while (and lh (null lh-entry))
7aad296a 611 (if (and (caar lh) (string-match re (caar lh)))
2e3d43ac
SM
612 (setq lh-entry (car lh))
613 (setq lh (cdr lh)))))
614 (unless lh-entry (error "Unknown library `%s'" file)))
615 (dolist (x (cdr lh-entry))
616 (case (car-safe x)
617 ;; (autoload (push (cdr x) autoloads))
618 (require (push (cdr x) requires))
619 (provide (push (cdr x) provides))
620 (t (push (or (cdr-safe x) x) symbols))))
621 (let ((apropos-pattern "")) ;Dummy binding for apropos-symbols-internal.
622 (apropos-symbols-internal
623 symbols apropos-do-all
624 (concat
625 (format "Library `%s' provides: %s\nand requires: %s"
626 file
627 (mapconcat 'apropos-library-button
628 (or provides '(nil)) " and ")
629 (mapconcat 'apropos-library-button
630 (or requires '(nil)) " and ")))))))
631
caa8e7aa
SM
632(defun apropos-symbols-internal (symbols keys &optional text)
633 ;; Filter out entries that are marked as apropos-inhibit.
634 (let ((all nil))
635 (dolist (symbol symbols)
636 (unless (get symbol 'apropos-inhibit)
637 (push symbol all)))
638 (setq symbols all))
639 (let ((apropos-accumulator
640 (mapcar
641 (lambda (symbol)
642 (let (doc properties)
643 (list
644 symbol
645 (apropos-score-symbol symbol)
646 (when (fboundp symbol)
647 (if (setq doc (condition-case nil
648 (documentation symbol t)
649 (void-function
650 "(alias for undefined function)")
651 (error
652 "(can't retrieve function documentation)")))
653 (substring doc 0 (string-match "\n" doc))
654 "(not documented)"))
655 (when (boundp symbol)
656 (apropos-documentation-property
4ef177aa
CY
657 symbol 'variable-documentation t))
658 (when (setq properties (symbol-plist symbol))
659 (setq doc (list (car properties)))
660 (while (setq properties (cdr (cdr properties)))
661 (setq doc (cons (car properties) doc)))
662 (mapconcat #'symbol-name (nreverse doc) " "))
663 (when (get symbol 'widget-type)
664 (apropos-documentation-property
665 symbol 'widget-documentation t))
caa8e7aa 666 (when (facep symbol)
19b72ab7
GM
667 (let ((alias (get symbol 'face-alias)))
668 (if alias
669 (if (facep alias)
670 (format "%slias for the face `%s'."
671 (if (get symbol 'obsolete-face)
672 "Obsolete a"
673 "A")
674 alias)
675 ;; Never happens in practice because fails
676 ;; (facep symbol) test.
677 "(alias for undefined face)")
678 (apropos-documentation-property
679 symbol 'face-documentation t))))
caa8e7aa 680 (when (get symbol 'custom-group)
4ef177aa
CY
681 (apropos-documentation-property
682 symbol 'group-documentation t)))))
caa8e7aa
SM
683 symbols)))
684 (apropos-print keys nil text)))
3925e76d
KH
685
686
6f8e447f 687;;;###autoload
0820b753 688(defun apropos-value (pattern &optional do-all)
2a4ec7e1 689 "Show all symbols whose value's printed representation matches PATTERN.
0820b753 690PATTERN can be a word, a list of words (separated by spaces),
fe8bc3fa
RS
691or a regexp (using some regexp special characters). If it is a word,
692search for matches for that word as a substring. If it is a list of words,
693search for matches for any two (or more) of those words.
694
0820b753 695With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also looks
3925e76d 696at the function and at the names and values of properties.
645c4f6a 697Returns list of symbols and values found."
0820b753
KS
698 (interactive (list (apropos-read-pattern "value")
699 current-prefix-arg))
3fefda51 700 (apropos-parse-pattern pattern)
645c4f6a
KH
701 (or do-all (setq do-all apropos-do-all))
702 (setq apropos-accumulator ())
703 (let (f v p)
3925e76d
KH
704 (mapatoms
705 (lambda (symbol)
706 (setq f nil v nil p nil)
0820b753
KS
707 (or (memq symbol '(apropos-regexp
708 apropos-pattern apropos-all-words-regexp
7dbffb1c
KS
709 apropos-words apropos-all-words
710 do-all apropos-accumulator
711 symbol f v p))
645c4f6a 712 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
3925e76d 713 (if do-all
645c4f6a
KH
714 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
715 p (apropos-format-plist symbol "\n " t)))
d2b30292
KS
716 (if (apropos-false-hit-str v)
717 (setq v nil))
718 (if (apropos-false-hit-str f)
719 (setq f nil))
720 (if (apropos-false-hit-str p)
721 (setq p nil))
3925e76d 722 (if (or f v p)
71296446 723 (setq apropos-accumulator (cons (list symbol
7dbffb1c
KS
724 (+ (apropos-score-str f)
725 (apropos-score-str v)
726 (apropos-score-str p))
727 f v p)
645c4f6a 728 apropos-accumulator))))))
1d69bd9b
SM
729 (let ((apropos-multi-type do-all))
730 (apropos-print nil "\n----------------\n")))
3925e76d
KH
731
732
645c4f6a 733;;;###autoload
0820b753 734(defun apropos-documentation (pattern &optional do-all)
2a4ec7e1 735 "Show symbols whose documentation contains matches for PATTERN.
0820b753 736PATTERN can be a word, a list of words (separated by spaces),
fe8bc3fa
RS
737or a regexp (using some regexp special characters). If it is a word,
738search for matches for that word as a substring. If it is a list of words,
739search for matches for any two (or more) of those words.
740
0820b753 741With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also use
645c4f6a
KH
742documentation that is not stored in the documentation file and show key
743bindings.
744Returns list of symbols and documentation found."
0820b753
KS
745 (interactive (list (apropos-read-pattern "documentation")
746 current-prefix-arg))
3fefda51 747 (apropos-parse-pattern pattern)
645c4f6a
KH
748 (or do-all (setq do-all apropos-do-all))
749 (setq apropos-accumulator () apropos-files-scanned ())
750 (let ((standard-input (get-buffer-create " apropos-temp"))
0820b753 751 (apropos-sort-by-scores apropos-documentation-sort-by-scores)
7dbffb1c 752 f v sf sv)
645c4f6a 753 (unwind-protect
7fdbcd83 754 (with-current-buffer standard-input
645c4f6a
KH
755 (apropos-documentation-check-doc-file)
756 (if do-all
757 (mapatoms
758 (lambda (symbol)
759 (setq f (apropos-safe-documentation symbol)
26a4a227
KH
760 v (get symbol 'variable-documentation))
761 (if (integerp v) (setq v))
762 (setq f (apropos-documentation-internal f)
763 v (apropos-documentation-internal v))
7dbffb1c
KS
764 (setq sf (apropos-score-doc f)
765 sv (apropos-score-doc v))
645c4f6a
KH
766 (if (or f v)
767 (if (setq apropos-item
768 (cdr (assq symbol apropos-accumulator)))
769 (progn
770 (if f
7dbffb1c
KS
771 (progn
772 (setcar (nthcdr 1 apropos-item) f)
773 (setcar apropos-item (+ (car apropos-item) sf))))
645c4f6a 774 (if v
7dbffb1c
KS
775 (progn
776 (setcar (nthcdr 2 apropos-item) v)
777 (setcar apropos-item (+ (car apropos-item) sv)))))
645c4f6a 778 (setq apropos-accumulator
71296446 779 (cons (list symbol
7dbffb1c
KS
780 (+ (apropos-score-symbol symbol 2) sf sv)
781 f v)
645c4f6a 782 apropos-accumulator)))))))
0820b753 783 (apropos-print nil "\n----------------\n" nil t))
645c4f6a
KH
784 (kill-buffer standard-input))))
785
786\f
787(defun apropos-value-internal (predicate symbol function)
788 (if (funcall predicate symbol)
789 (progn
790 (setq symbol (prin1-to-string (funcall function symbol)))
0820b753 791 (if (string-match apropos-regexp symbol)
645c4f6a
KH
792 (progn
793 (if apropos-match-face
794 (put-text-property (match-beginning 0) (match-end 0)
795 'face apropos-match-face
796 symbol))
797 symbol)))))
798
799(defun apropos-documentation-internal (doc)
800 (if (consp doc)
801 (apropos-documentation-check-elc-file (car doc))
0820b753
KS
802 (if (and doc
803 (string-match apropos-all-words-regexp doc)
804 (apropos-true-hit-doc doc))
805 (when apropos-match-face
806 (setq doc (substitute-command-keys (copy-sequence doc)))
807 (if (or (string-match apropos-pattern-quoted doc)
808 (string-match apropos-all-words-regexp doc))
809 (put-text-property (match-beginning 0)
810 (match-end 0)
811 'face apropos-match-face doc))
812 doc))))
645c4f6a
KH
813
814(defun apropos-format-plist (pl sep &optional compare)
3925e76d
KH
815 (setq pl (symbol-plist pl))
816 (let (p p-out)
817 (while pl
818 (setq p (format "%s %S" (car pl) (nth 1 pl)))
0820b753 819 (if (or (not compare) (string-match apropos-regexp p))
645c4f6a 820 (if apropos-property-face
3925e76d 821 (put-text-property 0 (length (symbol-name (car pl)))
645c4f6a 822 'face apropos-property-face p))
3925e76d 823 (setq p nil))
645c4f6a
KH
824 (if p
825 (progn
826 (and compare apropos-match-face
827 (put-text-property (match-beginning 0) (match-end 0)
828 'face apropos-match-face
829 p))
830 (setq p-out (concat p-out (if p-out sep) p))))
3925e76d
KH
831 (setq pl (nthcdr 2 pl)))
832 p-out))
833
6f8e447f 834
0820b753 835;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
3925e76d 836
645c4f6a 837(defun apropos-documentation-check-doc-file ()
a62f564f 838 (let (type symbol (sepa 2) sepb doc)
26a4a227
KH
839 (insert ?\^_)
840 (backward-char)
645c4f6a 841 (insert-file-contents (concat doc-directory internal-doc-file-name))
26a4a227
KH
842 (forward-char)
843 (while (save-excursion
844 (setq sepb (search-forward "\^_"))
845 (not (eobp)))
846 (beginning-of-line 2)
847 (if (save-restriction
848 (narrow-to-region (point) (1- sepb))
0820b753 849 (re-search-forward apropos-all-words-regexp nil t))
26a4a227 850 (progn
26a4a227 851 (goto-char (1+ sepa))
d2b30292
KS
852 (setq type (if (eq ?F (preceding-char))
853 2 ; function documentation
854 3) ; variable documentation
855 symbol (read)
d2b30292 856 doc (buffer-substring (1+ (point)) (1- sepb)))
4a6c9bec
GM
857 (when (and (apropos-true-hit-doc doc)
858 ;; The DOC file lists all built-in funcs and vars.
859 ;; If any are not currently bound, they can
860 ;; only be platform-specific stuff (eg NS) not
861 ;; in use on the current platform.
862 ;; So we exclude them.
863 (cond ((= 3 type) (boundp symbol))
864 ((= 2 type) (fboundp symbol))))
d2b30292
KS
865 (or (and (setq apropos-item (assq symbol apropos-accumulator))
866 (setcar (cdr apropos-item)
0820b753 867 (apropos-score-doc doc)))
71296446 868 (setq apropos-item (list symbol
d2b30292
KS
869 (+ (apropos-score-symbol symbol 2)
870 (apropos-score-doc doc))
871 nil nil)
872 apropos-accumulator (cons apropos-item
873 apropos-accumulator)))
0820b753
KS
874 (when apropos-match-face
875 (setq doc (substitute-command-keys doc))
876 (if (or (string-match apropos-pattern-quoted doc)
877 (string-match apropos-all-words-regexp doc))
878 (put-text-property (match-beginning 0)
879 (match-end 0)
880 'face apropos-match-face doc)))
d2b30292 881 (setcar (nthcdr type apropos-item) doc))))
26a4a227 882 (setq sepa (goto-char sepb)))))
645c4f6a
KH
883
884(defun apropos-documentation-check-elc-file (file)
885 (if (member file apropos-files-scanned)
886 nil
26a4a227 887 (let (symbol doc beg end this-is-a-variable)
645c4f6a
KH
888 (setq apropos-files-scanned (cons file apropos-files-scanned))
889 (erase-buffer)
890 (insert-file-contents file)
891 (while (search-forward "\n#@" nil t)
892 ;; Read the comment length, and advance over it.
893 (setq end (read)
26a4a227
KH
894 beg (1+ (point))
895 end (+ (point) end -1))
896 (forward-char)
897 (if (save-restriction
898 ;; match ^ and $ relative to doc string
899 (narrow-to-region beg end)
0820b753 900 (re-search-forward apropos-all-words-regexp nil t))
645c4f6a 901 (progn
26a4a227
KH
902 (goto-char (+ end 2))
903 (setq doc (buffer-substring beg end)
904 end (- (match-end 0) beg)
d2b30292
KS
905 beg (- (match-beginning 0) beg))
906 (when (apropos-true-hit-doc doc)
907 (setq this-is-a-variable (looking-at "(def\\(var\\|const\\) ")
908 symbol (progn
909 (skip-chars-forward "(a-z")
910 (forward-char)
911 (read))
912 symbol (if (consp symbol)
913 (nth 1 symbol)
914 symbol))
915 (if (if this-is-a-variable
916 (get symbol 'variable-documentation)
917 (and (fboundp symbol) (apropos-safe-documentation symbol)))
918 (progn
919 (or (and (setq apropos-item (assq symbol apropos-accumulator))
920 (setcar (cdr apropos-item)
921 (+ (cadr apropos-item) (apropos-score-doc doc))))
922 (setq apropos-item (list symbol
923 (+ (apropos-score-symbol symbol 2)
924 (apropos-score-doc doc))
925 nil nil)
926 apropos-accumulator (cons apropos-item
927 apropos-accumulator)))
0820b753
KS
928 (when apropos-match-face
929 (setq doc (substitute-command-keys doc))
930 (if (or (string-match apropos-pattern-quoted doc)
931 (string-match apropos-all-words-regexp doc))
932 (put-text-property (match-beginning 0)
933 (match-end 0)
934 'face apropos-match-face doc)))
d2b30292
KS
935 (setcar (nthcdr (if this-is-a-variable 3 2)
936 apropos-item)
937 doc))))))))))
645c4f6a
KH
938
939
940
941(defun apropos-safe-documentation (function)
7a5348db 942 "Like `documentation', except it avoids calling `get_doc_string'.
6f8e447f 943Will return nil instead."
3925e76d 944 (while (and function (symbolp function))
6f8e447f 945 (setq function (if (fboundp function)
3925e76d 946 (symbol-function function))))
d2e1218f
RS
947 (if (eq (car-safe function) 'macro)
948 (setq function (cdr function)))
3925e76d 949 (setq function (if (byte-code-function-p function)
645c4f6a
KH
950 (if (> (length function) 4)
951 (aref function 4))
952 (if (eq (car-safe function) 'autoload)
953 (nth 2 function)
954 (if (eq (car-safe function) 'lambda)
955 (if (stringp (nth 2 function))
956 (nth 2 function)
957 (if (stringp (nth 3 function))
958 (nth 3 function)))))))
959 (if (integerp function)
960 nil
961 function))
962
1d69bd9b
SM
963(defcustom apropos-compact-layout nil
964 "If non-nil, use a single line per binding."
965 :type 'boolean)
645c4f6a 966
0820b753 967(defun apropos-print (do-keys spacing &optional text nosubst)
a9155e87
KH
968 "Output result of apropos searching into buffer `*Apropos*'.
969The value of `apropos-accumulator' is the list of items to output.
71296446 970Each element should have the format
7dbffb1c 971 (SYMBOL SCORE FN-DOC VAR-DOC [PLIST-DOC WIDGET-DOC FACE-DOC GROUP-DOC]).
a9155e87
KH
972The return value is the list that was in `apropos-accumulator', sorted
973alphabetically by symbol name; but this function also sets
9cc84e31
RS
974`apropos-accumulator' to nil before returning.
975
caa8e7aa
SM
976If SPACING is non-nil, it should be a string; separate items with that string.
977If non-nil TEXT is a string that will be printed as a heading."
645c4f6a 978 (if (null apropos-accumulator)
0820b753 979 (message "No apropos matches for `%s'" apropos-pattern)
645c4f6a 980 (setq apropos-accumulator
30aab741
RS
981 (sort apropos-accumulator
982 (lambda (a b)
983 ;; Don't sort by score if user can't see the score.
984 ;; It would be confusing. -- rms.
ab765ff7 985 (if apropos-sort-by-scores
30aab741
RS
986 (or (> (cadr a) (cadr b))
987 (and (= (cadr a) (cadr b))
988 (string-lessp (car a) (car b))))
989 (string-lessp (car a) (car b))))))
09e32aaf 990 (with-output-to-temp-buffer "*Apropos*"
645c4f6a 991 (let ((p apropos-accumulator)
3925e76d 992 (old-buffer (current-buffer))
abd20d91 993 (inhibit-read-only t)
e517f56d 994 symbol item)
26a4a227 995 (set-buffer standard-output)
abd20d91 996 (apropos-mode)
4ef177aa
CY
997 (insert (substitute-command-keys "Type \\[apropos-follow] on ")
998 (if apropos-multi-type "a type label" "an entry")
999 " to view its full documentation.\n\n")
caa8e7aa 1000 (if text (insert text "\n\n"))
671c04d9 1001 (dolist (apropos-item p)
9cc84e31
RS
1002 (when (and spacing (not (bobp)))
1003 (princ spacing))
671c04d9 1004 (setq symbol (car apropos-item))
0820b753
KS
1005 ;; Insert dummy score element for backwards compatibility with 21.x
1006 ;; apropos-item format.
1007 (if (not (numberp (cadr apropos-item)))
1008 (setq apropos-item
1009 (cons (car apropos-item)
1010 (cons nil (cdr apropos-item)))))
e517f56d
MB
1011 (insert-text-button (symbol-name symbol)
1012 'type 'apropos-symbol
60f4c0c8 1013 'skip apropos-multi-type
e517f56d
MB
1014 ;; Can't use default, since user may have
1015 ;; changed the variable!
1016 ;; Just say `no' to variables containing faces!
1017 'face apropos-symbol-face)
0820b753
KS
1018 (if (and (eq apropos-sort-by-scores 'verbose)
1019 (cadr apropos-item))
7dbffb1c 1020 (insert " (" (number-to-string (cadr apropos-item)) ") "))
98ce2330 1021 ;; Calculate key-bindings if we want them.
1d69bd9b
SM
1022 (unless apropos-compact-layout
1023 (and do-keys
1024 (commandp symbol)
1025 (not (eq symbol 'self-insert-command))
1026 (indent-to 30 1)
1027 (if (let ((keys
1028 (with-current-buffer old-buffer
1029 (where-is-internal symbol)))
1030 filtered)
1031 ;; Copy over the list of key sequences,
1032 ;; omitting any that contain a buffer or a frame.
1033 ;; FIXME: Why omit keys that contain buffers and
1034 ;; frames? This looks like a bad workaround rather
1035 ;; than a proper fix. Does anybod know what problem
1036 ;; this is trying to address? --Stef
1037 (dolist (key keys)
1038 (let ((i 0)
1039 loser)
1040 (while (< i (length key))
1041 (if (or (framep (aref key i))
1042 (bufferp (aref key i)))
1043 (setq loser t))
1044 (setq i (1+ i)))
1045 (or loser
1046 (push key filtered))))
1047 (setq item filtered))
1048 ;; Convert the remaining keys to a string and insert.
1049 (insert
1050 (mapconcat
1051 (lambda (key)
1052 (setq key (condition-case ()
1053 (key-description key)
1054 (error)))
1055 (if apropos-keybinding-face
1056 (put-text-property 0 (length key)
1057 'face apropos-keybinding-face
1058 key))
1059 key)
1060 item ", "))
1061 (insert "M-x ... RET")
1062 (when apropos-keybinding-face
1063 (put-text-property (- (point) 11) (- (point) 8)
1064 'face apropos-keybinding-face)
1065 (put-text-property (- (point) 3) (point)
1066 'face apropos-keybinding-face))))
1067 (terpri))
7dbffb1c 1068 (apropos-print-doc 2
26a4a227 1069 (if (commandp symbol)
894e460c 1070 'apropos-command
26a4a227 1071 (if (apropos-macrop symbol)
894e460c
MB
1072 'apropos-macro
1073 'apropos-function))
0820b753
KS
1074 (not nosubst))
1075 (apropos-print-doc 3 'apropos-variable (not nosubst))
7dbffb1c
KS
1076 (apropos-print-doc 7 'apropos-group t)
1077 (apropos-print-doc 6 'apropos-face t)
1078 (apropos-print-doc 5 'apropos-widget t)
1079 (apropos-print-doc 4 'apropos-plist nil))
1d69bd9b 1080 (set (make-local-variable 'truncate-partial-width-windows) t)
abd20d91 1081 (set (make-local-variable 'truncate-lines) t))))
645c4f6a
KH
1082 (prog1 apropos-accumulator
1083 (setq apropos-accumulator ()))) ; permit gc
1084
645c4f6a 1085(defun apropos-macrop (symbol)
b965722b 1086 "Return t if SYMBOL is a Lisp macro."
645c4f6a
KH
1087 (and (fboundp symbol)
1088 (consp (setq symbol
1089 (symbol-function symbol)))
1090 (or (eq (car symbol) 'macro)
1091 (if (eq (car symbol) 'autoload)
1092 (memq (nth 4 symbol)
1093 '(macro t))))))
3925e76d 1094
645c4f6a 1095
894e460c 1096(defun apropos-print-doc (i type do-keys)
4ef177aa
CY
1097 (let ((doc (nth i apropos-item)))
1098 (when (stringp doc)
1099 (if apropos-compact-layout
1100 (insert (propertize "\t" 'display '(space :align-to 32)) " ")
1101 (insert " "))
1102 (if apropos-multi-type
1103 (let ((button-face (button-type-get type 'face)))
1104 (unless (consp button-face)
1105 (setq button-face (list button-face)))
1106 (insert-text-button
1107 (if apropos-compact-layout
1108 (format "<%s>" (button-type-get type 'apropos-short-label))
1109 (button-type-get type 'apropos-label))
1110 'type type
1111 ;; Can't use the default button face, since user may have changed the
1112 ;; variable! Just say `no' to variables containing faces!
1113 'face (append button-face apropos-label-face)
1114 'apropos-symbol (car apropos-item))
1115 (insert (if apropos-compact-layout " " ": ")))
1116
1117 ;; If the query is only for a single type, there's no point
1118 ;; writing it over and over again. Insert a blank button, and
1119 ;; put the 'apropos-label property there (needed by
1120 ;; apropos-symbol-button-display-help).
1121 (insert-text-button
60f4c0c8 1122 " " 'type type 'skip t
4ef177aa
CY
1123 'face 'default 'apropos-symbol (car apropos-item)))
1124
1125 (let ((opoint (point))
1126 (ocol (current-column)))
1127 (cond ((equal doc "")
1128 (setq doc "(not documented)"))
1129 (do-keys
1130 (setq doc (substitute-command-keys doc))))
1131 (insert doc)
1132 (if (equal doc "(not documented)")
1133 (put-text-property opoint (point) 'font-lock-face 'shadow))
1134 ;; The labeling buttons might make the line too long, so fill it if
1135 ;; necessary.
275b59b0
NF
1136 (let ((fill-column (+ 5 (if (integerp emacs-lisp-docstring-fill-column)
1137 emacs-lisp-docstring-fill-column
1138 fill-column)))
4ef177aa
CY
1139 (fill-prefix (make-string ocol ?\s)))
1140 (fill-region opoint (point) nil t)))
1141 (or (bolp) (terpri)))))
3925e76d 1142
e517f56d
MB
1143(defun apropos-follow ()
1144 "Invokes any button at point, otherwise invokes the nearest label button."
3925e76d 1145 (interactive)
e517f56d
MB
1146 (button-activate
1147 (or (apropos-next-label-button (line-beginning-position))
1148 (error "There is nothing to follow here"))))
3925e76d
KH
1149
1150
1151(defun apropos-describe-plist (symbol)
1152 "Display a pretty listing of SYMBOL's plist."
32226619
JB
1153 (help-setup-xref (list 'apropos-describe-plist symbol)
1154 (called-interactively-p 'interactive))
c6808785 1155 (with-help-window (help-buffer)
3925e76d
KH
1156 (set-buffer standard-output)
1157 (princ "Symbol ")
1158 (prin1 symbol)
1159 (princ "'s plist is\n (")
645c4f6a 1160 (if apropos-symbol-face
caa8e7aa
SM
1161 (put-text-property (+ (point-min) 7) (- (point) 14)
1162 'face apropos-symbol-face))
3925e76d 1163 (insert (apropos-format-plist symbol "\n "))
c6808785 1164 (princ ")")))
6f8e447f 1165
e517f56d 1166
896546cd
RS
1167(provide 'apropos)
1168
c0274f38 1169;;; apropos.el ends here