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