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