Fix quoting syntax.
[bpt/emacs.git] / lisp / apropos.el
CommitLineData
e8af40ee 1;;; apropos.el --- apropos commands for users and programmers
c0274f38 2
0d30b337
TTN
3;; Copyright (C) 1989, 1994, 1995, 2001, 2002, 2003, 2004,
4;; 2005 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
e5167999 14;; the Free Software Foundation; either version 2, 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
145 "Apropos pattern passed through `regexp-quoute'.")
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")
171 ("yank" "paste"))
172 "List of synonyms known by apropos.
173Each element is a list of words where the first word is the standard emacs
174term, and the rest of the words are alternative terms.")
175
e517f56d
MB
176\f
177;;; Button types used by apropos
178
179(define-button-type 'apropos-symbol
180 'face apropos-symbol-face
894e460c 181 'help-echo "mouse-2, RET: Display more help on this symbol"
d8fac801 182 'follow-link t
894e460c
MB
183 'action #'apropos-symbol-button-display-help
184 'skip t)
e517f56d
MB
185
186(defun apropos-symbol-button-display-help (button)
187 "Display further help for the `apropos-symbol' button BUTTON."
188 (button-activate
189 (or (apropos-next-label-button (button-start button))
190 (error "There is nothing to follow for `%s'" (button-label button)))))
191
894e460c
MB
192(define-button-type 'apropos-function
193 'apropos-label "Function"
d8fac801
KS
194 'help-echo "mouse-2, RET: Display more help on this function"
195 'follow-link t
894e460c 196 'action (lambda (button)
d8fac801
KS
197 (describe-function (button-get button 'apropos-symbol))))
198
894e460c
MB
199(define-button-type 'apropos-macro
200 'apropos-label "Macro"
d8fac801
KS
201 'help-echo "mouse-2, RET: Display more help on this macro"
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-command
207 'apropos-label "Command"
d8fac801
KS
208 'help-echo "mouse-2, RET: Display more help on this command"
209 'follow-link t
894e460c 210 'action (lambda (button)
d8fac801 211 (describe-function (button-get button 'apropos-symbol))))
7cfedc97 212
894e460c
MB
213;; We used to use `customize-variable-other-window' instead for a
214;; customizable variable, but that is slow. It is better to show an
215;; ordinary help buffer and let the user click on the customization
216;; button in that buffer, if he wants to.
217;; Likewise for `customize-face-other-window'.
218(define-button-type 'apropos-variable
219 'apropos-label "Variable"
220 'help-echo "mouse-2, RET: Display more help on this variable"
d8fac801 221 'follow-link t
894e460c
MB
222 'action (lambda (button)
223 (describe-variable (button-get button 'apropos-symbol))))
224
225(define-button-type 'apropos-face
226 'apropos-label "Face"
227 'help-echo "mouse-2, RET: Display more help on this face"
d8fac801 228 'follow-link t
894e460c
MB
229 'action (lambda (button)
230 (describe-face (button-get button 'apropos-symbol))))
231
232(define-button-type 'apropos-group
233 'apropos-label "Group"
234 'help-echo "mouse-2, RET: Display more help on this group"
d8fac801 235 'follow-link t
894e460c 236 'action (lambda (button)
2d37b91e 237 (customize-group-other-window
894e460c
MB
238 (button-get button 'apropos-symbol))))
239
240(define-button-type 'apropos-widget
241 'apropos-label "Widget"
242 'help-echo "mouse-2, RET: Display more help on this widget"
d8fac801 243 'follow-link t
894e460c
MB
244 'action (lambda (button)
245 (widget-browse-other-window (button-get button 'apropos-symbol))))
246
247(define-button-type 'apropos-plist
248 'apropos-label "Plist"
249 'help-echo "mouse-2, RET: Display more help on this plist"
d8fac801 250 'follow-link t
894e460c
MB
251 'action (lambda (button)
252 (apropos-describe-plist (button-get button 'apropos-symbol))))
e517f56d
MB
253
254(defun apropos-next-label-button (pos)
a11a4e9f 255 "Return the next apropos label button after POS, or nil if there's none.
e517f56d
MB
256Will also return nil if more than one `apropos-symbol' button is encountered
257before finding a label."
a101302b 258 (let* ((button (next-button pos t))
e517f56d 259 (already-hit-symbol nil)
3b8c60f1
MB
260 (label (and button (button-get button 'apropos-label)))
261 (type (and button (button-get button 'type))))
e517f56d 262 (while (and button
3b8c60f1
MB
263 (not label)
264 (or (not (eq type 'apropos-symbol))
e517f56d 265 (not already-hit-symbol)))
3b8c60f1 266 (when (eq type 'apropos-symbol)
e517f56d
MB
267 (setq already-hit-symbol t))
268 (setq button (next-button (button-start button)))
269 (when button
3b8c60f1
MB
270 (setq label (button-get button 'apropos-label))
271 (setq type (button-get button 'type))))
272 (and label button)))
e517f56d 273
645c4f6a 274\f
7dbffb1c
KS
275(defun apropos-words-to-regexp (words wild)
276 "Make regexp matching any two of the words in WORDS."
277 (concat "\\("
278 (mapconcat 'identity words "\\|")
80f5d2ef 279 "\\)"
71296446 280 (if (cdr words)
80f5d2ef
AS
281 (concat wild
282 "\\("
7dbffb1c
KS
283 (mapconcat 'identity words "\\|")
284 "\\)")
285 "")))
286
0820b753
KS
287;;;###autoload
288(defun apropos-read-pattern (subject)
289 "Read an apropos pattern, either a word list or a regexp.
290Returns the user pattern, either a list of words which are matched
291literally, or a string which is used as a regexp to search for.
292
293SUBJECT is a string that is included in the prompt to identify what
294kind of objects to search."
295 (let ((pattern
296 (read-string (concat "Apropos " subject " (word list or regexp): "))))
297 (if (string-equal (regexp-quote pattern) pattern)
298 ;; Split into words
299 (split-string pattern "[ \t]+")
300 pattern)))
301
302(defun apropos-parse-pattern (pattern)
303 "Rewrite a list of words to a regexp matching all permutations.
3fefda51
KS
304If PATTERN is a string, that means it is already a regexp.
305This updates variables `apropos-pattern', `apropos-pattern-quoted',
306`apropos-regexp', `apropos-words', and `apropos-all-words-regexp'."
0820b753
KS
307 (setq apropos-words nil
308 apropos-all-words nil)
309 (if (consp pattern)
7dbffb1c
KS
310 ;; We don't actually make a regexp matching all permutations.
311 ;; Instead, for e.g. "a b c", we make a regexp matching
312 ;; any combination of two or more words like this:
313 ;; (a|b|c).*(a|b|c) which may give some false matches,
314 ;; but as long as it also gives the right ones, that's ok.
0820b753
KS
315 (let ((words pattern))
316 (setq apropos-pattern (mapconcat 'identity pattern " ")
317 apropos-pattern-quoted (regexp-quote apropos-pattern))
7dbffb1c
KS
318 (dolist (word words)
319 (let ((syn apropos-synonyms) (s word) (a word))
320 (while syn
321 (if (member word (car syn))
322 (progn
323 (setq a (mapconcat 'identity (car syn) "\\|"))
324 (if (member word (cdr (car syn)))
325 (setq s a))
326 (setq syn nil))
327 (setq syn (cdr syn))))
328 (setq apropos-words (cons s apropos-words)
329 apropos-all-words (cons a apropos-all-words))))
3fefda51
KS
330 (setq apropos-all-words-regexp
331 (apropos-words-to-regexp apropos-all-words ".+"))
332 (setq apropos-regexp
333 (apropos-words-to-regexp apropos-words ".*?")))
0820b753
KS
334 (setq apropos-pattern-quoted (regexp-quote pattern)
335 apropos-all-words-regexp pattern
3fefda51
KS
336 apropos-pattern pattern
337 apropos-regexp pattern)))
0820b753 338
7dbffb1c
KS
339
340(defun apropos-calc-scores (str words)
341 "Return apropos scores for string STR matching WORDS.
342Value is a list of offsets of the words into the string."
0820b753 343 (let (scores i)
7dbffb1c
KS
344 (if words
345 (dolist (word words scores)
346 (if (setq i (string-match word str))
347 (setq scores (cons i scores))))
348 ;; Return list of start and end position of regexp
347a20b8 349 (and (string-match apropos-pattern str)
0820b753 350 (list (match-beginning 0) (match-end 0))))))
7dbffb1c
KS
351
352(defun apropos-score-str (str)
353 "Return apropos score for string STR."
354 (if str
0820b753
KS
355 (let* ((l (length str))
356 (score (- (/ l 10))))
7dbffb1c 357 (dolist (s (apropos-calc-scores str apropos-all-words) score)
d5857a96 358 (setq score (+ score 1000 (/ (* (- l s) 1000) l)))))
7dbffb1c
KS
359 0))
360
361(defun apropos-score-doc (doc)
362 "Return apropos score for documentation string DOC."
b7a2a696
LK
363 (let ((l (length doc)))
364 (if (> l 0)
0820b753
KS
365 (let ((score 0) i)
366 (when (setq i (string-match apropos-pattern-quoted doc))
367 (setq score 10000))
b7a2a696
LK
368 (dolist (s (apropos-calc-scores doc apropos-all-words) score)
369 (setq score (+ score 50 (/ (* (- l s) 50) l)))))
370 0)))
71296446 371
7dbffb1c
KS
372(defun apropos-score-symbol (symbol &optional weight)
373 "Return apropos score for SYMBOL."
374 (setq symbol (symbol-name symbol))
375 (let ((score 0)
0820b753 376 (l (length symbol)))
7dbffb1c
KS
377 (dolist (s (apropos-calc-scores symbol apropos-words) (* score (or weight 3)))
378 (setq score (+ score (- 60 l) (/ (* (- l s) 60) l))))))
379
d2b30292
KS
380(defun apropos-true-hit (str words)
381 "Return t if STR is a genuine hit.
382This may fail if only one of the keywords is matched more than once.
383This requires that at least 2 keywords (unless only one was given)."
384 (or (not str)
385 (not words)
386 (not (cdr words))
387 (> (length (apropos-calc-scores str words)) 1)))
388
389(defun apropos-false-hit-symbol (symbol)
390 "Return t if SYMBOL is not really matched by the current keywords."
391 (not (apropos-true-hit (symbol-name symbol) apropos-words)))
392
393(defun apropos-false-hit-str (str)
394 "Return t if STR is not really matched by the current keywords."
395 (not (apropos-true-hit str apropos-words)))
396
397(defun apropos-true-hit-doc (doc)
398 "Return t if DOC is really matched by the current keywords."
399 (apropos-true-hit doc apropos-all-words))
400
38ab866c 401(define-derived-mode apropos-mode fundamental-mode "Apropos"
26a4a227
KH
402 "Major mode for following hyperlinks in output of apropos commands.
403
38ab866c 404\\{apropos-mode-map}")
26a4a227 405
f38fd610 406;;;###autoload
0820b753
KS
407(defun apropos-variable (pattern &optional do-all)
408 "Show user variables that match PATTERN.
409PATTERN can be a word, a list of words (separated by spaces),
410or a regexp (using some regexp special characters). If it is a word,
411search for matches for that word as a substring. If it is a list of words,
412search for matches for any two (or more) of those words.
413
414With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
05942d06 415normal variables."
0820b753
KS
416 (interactive (list (apropos-read-pattern
417 (if (or current-prefix-arg apropos-do-all)
418 "variable" "user option"))
05942d06 419 current-prefix-arg))
0820b753 420 (apropos-command pattern nil
cd00fd36 421 (if (or do-all apropos-do-all)
05942d06
RS
422 #'(lambda (symbol)
423 (and (boundp symbol)
424 (get symbol 'variable-documentation)))
425 'user-variable-p)))
26a4a227 426
645c4f6a
KH
427;; For auld lang syne:
428;;;###autoload
82e736c1 429(defalias 'command-apropos 'apropos-command)
6f8e447f 430;;;###autoload
0820b753
KS
431(defun apropos-command (pattern &optional do-all var-predicate)
432 "Show commands (interactively callable functions) that match PATTERN.
433PATTERN can be a word, a list of words (separated by spaces),
fe8bc3fa
RS
434or a regexp (using some regexp special characters). If it is a word,
435search for matches for that word as a substring. If it is a list of words,
436search for matches for any two (or more) of those words.
437
0820b753 438With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
9a909b3c 439noninteractive functions.
05942d06 440
9a909b3c 441If VAR-PREDICATE is non-nil, show only variables, and only those that
0820b753
KS
442satisfy the predicate VAR-PREDICATE.
443
444When called from a Lisp program, a string PATTERN is used as a regexp,
445while a list of strings is used as a word list."
446 (interactive (list (apropos-read-pattern
447 (if (or current-prefix-arg apropos-do-all)
448 "command or function" "command"))
645c4f6a 449 current-prefix-arg))
3fefda51 450 (apropos-parse-pattern pattern)
3925e76d 451 (let ((message
26a4a227 452 (let ((standard-output (get-buffer-create "*Apropos*")))
3925e76d 453 (print-help-return-message 'identity))))
645c4f6a
KH
454 (or do-all (setq do-all apropos-do-all))
455 (setq apropos-accumulator
0820b753 456 (apropos-internal apropos-regexp
cd00fd36
KH
457 (or var-predicate
458 (if do-all 'functionp 'commandp))))
dea22c45
RS
459 (let ((tem apropos-accumulator))
460 (while tem
d2b30292
KS
461 (if (or (get (car tem) 'apropos-inhibit)
462 (apropos-false-hit-symbol (car tem)))
dea22c45
RS
463 (setq apropos-accumulator (delq (car tem) apropos-accumulator)))
464 (setq tem (cdr tem))))
a9155e87 465 (let ((p apropos-accumulator)
7dbffb1c 466 doc symbol score)
a9155e87
KH
467 (while p
468 (setcar p (list
469 (setq symbol (car p))
7dbffb1c 470 (setq score (apropos-score-symbol symbol))
a9155e87
KH
471 (unless var-predicate
472 (if (functionp symbol)
473 (if (setq doc (documentation symbol t))
7dbffb1c 474 (progn
71296446 475 (setq score (+ score (apropos-score-doc doc)))
7dbffb1c 476 (substring doc 0 (string-match "\n" doc)))
a9155e87
KH
477 "(not documented)")))
478 (and var-predicate
479 (funcall var-predicate symbol)
480 (if (setq doc (documentation-property
481 symbol 'variable-documentation t))
7dbffb1c
KS
482 (progn
483 (setq score (+ score (apropos-score-doc doc)))
484 (substring doc 0
485 (string-match "\n" doc)))))))
486 (setcar (cdr (car p)) score)
a9155e87 487 (setq p (cdr p))))
0820b753 488 (and (apropos-print t nil nil t)
a9155e87 489 message
8a26c165 490 (message "%s" message))))
3925e76d
KH
491
492
3925e76d 493;;;###autoload
914b40da
RS
494(defun apropos-documentation-property (symbol property raw)
495 "Like (documentation-property SYMBOL PROPERTY RAW) but handle errors."
496 (condition-case ()
497 (let ((doc (documentation-property symbol property raw)))
498 (if doc (substring doc 0 (string-match "\n" doc))
499 "(not documented)"))
500 (error "(error retrieving documentation)")))
501
5760219d
JPW
502
503;;;###autoload
0820b753
KS
504(defun apropos (pattern &optional do-all)
505 "Show all bound symbols whose names match PATTERN.
506PATTERN can be a word, a list of words (separated by spaces),
fe8bc3fa
RS
507or a regexp (using some regexp special characters). If it is a word,
508search for matches for that word as a substring. If it is a list of words,
509search for matches for any two (or more) of those words.
510
0820b753 511With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also
7a5348db
DL
512show unbound symbols and key bindings, which is a little more
513time-consuming. Returns list of symbols and documentation found."
0820b753
KS
514 (interactive (list (apropos-read-pattern "symbol")
515 current-prefix-arg))
3fefda51 516 (apropos-parse-pattern pattern)
caa8e7aa 517 (apropos-symbols-internal
0820b753 518 (apropos-internal apropos-regexp
645c4f6a
KH
519 (and (not do-all)
520 (not apropos-do-all)
521 (lambda (symbol)
522 (or (fboundp symbol)
523 (boundp symbol)
5edc67d3 524 (facep symbol)
caa8e7aa
SM
525 (symbol-plist symbol)))))
526 (or do-all apropos-do-all)))
527
528(defun apropos-symbols-internal (symbols keys &optional text)
529 ;; Filter out entries that are marked as apropos-inhibit.
530 (let ((all nil))
531 (dolist (symbol symbols)
532 (unless (get symbol 'apropos-inhibit)
533 (push symbol all)))
534 (setq symbols all))
535 (let ((apropos-accumulator
536 (mapcar
537 (lambda (symbol)
538 (let (doc properties)
539 (list
540 symbol
541 (apropos-score-symbol symbol)
542 (when (fboundp symbol)
543 (if (setq doc (condition-case nil
544 (documentation symbol t)
545 (void-function
546 "(alias for undefined function)")
547 (error
548 "(can't retrieve function documentation)")))
549 (substring doc 0 (string-match "\n" doc))
550 "(not documented)"))
551 (when (boundp symbol)
552 (apropos-documentation-property
914b40da 553 symbol 'variable-documentation t))
a9155e87
KH
554 (when (setq properties (symbol-plist symbol))
555 (setq doc (list (car properties)))
556 (while (setq properties (cdr (cdr properties)))
557 (setq doc (cons (car properties) doc)))
558 (mapconcat #'symbol-name (nreverse doc) " "))
559 (when (get symbol 'widget-type)
914b40da
RS
560 (apropos-documentation-property
561 symbol 'widget-documentation t))
caa8e7aa
SM
562 (when (facep symbol)
563 (apropos-documentation-property
564 symbol 'face-documentation t))
565 (when (get symbol 'custom-group)
914b40da 566 (apropos-documentation-property
caa8e7aa
SM
567 symbol 'group-documentation t)))))
568 symbols)))
569 (apropos-print keys nil text)))
3925e76d
KH
570
571
6f8e447f 572;;;###autoload
0820b753
KS
573(defun apropos-value (pattern &optional do-all)
574 "Show all symbols whose value's printed image matches PATTERN.
575PATTERN can be a word, a list of words (separated by spaces),
fe8bc3fa
RS
576or a regexp (using some regexp special characters). If it is a word,
577search for matches for that word as a substring. If it is a list of words,
578search for matches for any two (or more) of those words.
579
0820b753 580With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also looks
3925e76d 581at the function and at the names and values of properties.
645c4f6a 582Returns list of symbols and values found."
0820b753
KS
583 (interactive (list (apropos-read-pattern "value")
584 current-prefix-arg))
3fefda51 585 (apropos-parse-pattern pattern)
645c4f6a
KH
586 (or do-all (setq do-all apropos-do-all))
587 (setq apropos-accumulator ())
588 (let (f v p)
3925e76d
KH
589 (mapatoms
590 (lambda (symbol)
591 (setq f nil v nil p nil)
0820b753
KS
592 (or (memq symbol '(apropos-regexp
593 apropos-pattern apropos-all-words-regexp
7dbffb1c
KS
594 apropos-words apropos-all-words
595 do-all apropos-accumulator
596 symbol f v p))
645c4f6a 597 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
3925e76d 598 (if do-all
645c4f6a
KH
599 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
600 p (apropos-format-plist symbol "\n " t)))
d2b30292
KS
601 (if (apropos-false-hit-str v)
602 (setq v nil))
603 (if (apropos-false-hit-str f)
604 (setq f nil))
605 (if (apropos-false-hit-str p)
606 (setq p nil))
3925e76d 607 (if (or f v p)
71296446 608 (setq apropos-accumulator (cons (list symbol
7dbffb1c
KS
609 (+ (apropos-score-str f)
610 (apropos-score-str v)
611 (apropos-score-str p))
612 f v p)
645c4f6a 613 apropos-accumulator))))))
9cc84e31 614 (apropos-print nil "\n----------------\n"))
3925e76d
KH
615
616
645c4f6a 617;;;###autoload
0820b753
KS
618(defun apropos-documentation (pattern &optional do-all)
619 "Show symbols whose documentation contain matches for PATTERN.
620PATTERN can be a word, a list of words (separated by spaces),
fe8bc3fa
RS
621or a regexp (using some regexp special characters). If it is a word,
622search for matches for that word as a substring. If it is a list of words,
623search for matches for any two (or more) of those words.
624
0820b753 625With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also use
645c4f6a
KH
626documentation that is not stored in the documentation file and show key
627bindings.
628Returns list of symbols and documentation found."
0820b753
KS
629 (interactive (list (apropos-read-pattern "documentation")
630 current-prefix-arg))
3fefda51 631 (apropos-parse-pattern pattern)
645c4f6a
KH
632 (or do-all (setq do-all apropos-do-all))
633 (setq apropos-accumulator () apropos-files-scanned ())
634 (let ((standard-input (get-buffer-create " apropos-temp"))
0820b753 635 (apropos-sort-by-scores apropos-documentation-sort-by-scores)
7dbffb1c 636 f v sf sv)
645c4f6a
KH
637 (unwind-protect
638 (save-excursion
639 (set-buffer standard-input)
640 (apropos-documentation-check-doc-file)
641 (if do-all
642 (mapatoms
643 (lambda (symbol)
644 (setq f (apropos-safe-documentation symbol)
26a4a227
KH
645 v (get symbol 'variable-documentation))
646 (if (integerp v) (setq v))
647 (setq f (apropos-documentation-internal f)
648 v (apropos-documentation-internal v))
7dbffb1c
KS
649 (setq sf (apropos-score-doc f)
650 sv (apropos-score-doc v))
645c4f6a
KH
651 (if (or f v)
652 (if (setq apropos-item
653 (cdr (assq symbol apropos-accumulator)))
654 (progn
655 (if f
7dbffb1c
KS
656 (progn
657 (setcar (nthcdr 1 apropos-item) f)
658 (setcar apropos-item (+ (car apropos-item) sf))))
645c4f6a 659 (if v
7dbffb1c
KS
660 (progn
661 (setcar (nthcdr 2 apropos-item) v)
662 (setcar apropos-item (+ (car apropos-item) sv)))))
645c4f6a 663 (setq apropos-accumulator
71296446 664 (cons (list symbol
7dbffb1c
KS
665 (+ (apropos-score-symbol symbol 2) sf sv)
666 f v)
645c4f6a 667 apropos-accumulator)))))))
0820b753 668 (apropos-print nil "\n----------------\n" nil t))
645c4f6a
KH
669 (kill-buffer standard-input))))
670
671\f
672(defun apropos-value-internal (predicate symbol function)
673 (if (funcall predicate symbol)
674 (progn
675 (setq symbol (prin1-to-string (funcall function symbol)))
0820b753 676 (if (string-match apropos-regexp symbol)
645c4f6a
KH
677 (progn
678 (if apropos-match-face
679 (put-text-property (match-beginning 0) (match-end 0)
680 'face apropos-match-face
681 symbol))
682 symbol)))))
683
684(defun apropos-documentation-internal (doc)
685 (if (consp doc)
686 (apropos-documentation-check-elc-file (car doc))
0820b753
KS
687 (if (and doc
688 (string-match apropos-all-words-regexp doc)
689 (apropos-true-hit-doc doc))
690 (when apropos-match-face
691 (setq doc (substitute-command-keys (copy-sequence doc)))
692 (if (or (string-match apropos-pattern-quoted doc)
693 (string-match apropos-all-words-regexp doc))
694 (put-text-property (match-beginning 0)
695 (match-end 0)
696 'face apropos-match-face doc))
697 doc))))
645c4f6a
KH
698
699(defun apropos-format-plist (pl sep &optional compare)
3925e76d
KH
700 (setq pl (symbol-plist pl))
701 (let (p p-out)
702 (while pl
703 (setq p (format "%s %S" (car pl) (nth 1 pl)))
0820b753 704 (if (or (not compare) (string-match apropos-regexp p))
645c4f6a 705 (if apropos-property-face
3925e76d 706 (put-text-property 0 (length (symbol-name (car pl)))
645c4f6a 707 'face apropos-property-face p))
3925e76d 708 (setq p nil))
645c4f6a
KH
709 (if p
710 (progn
711 (and compare apropos-match-face
712 (put-text-property (match-beginning 0) (match-end 0)
713 'face apropos-match-face
714 p))
715 (setq p-out (concat p-out (if p-out sep) p))))
3925e76d
KH
716 (setq pl (nthcdr 2 pl)))
717 p-out))
718
6f8e447f 719
0820b753 720;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
3925e76d 721
645c4f6a 722(defun apropos-documentation-check-doc-file ()
0820b753 723 (let (type symbol (sepa 2) sepb)
26a4a227
KH
724 (insert ?\^_)
725 (backward-char)
645c4f6a 726 (insert-file-contents (concat doc-directory internal-doc-file-name))
26a4a227
KH
727 (forward-char)
728 (while (save-excursion
729 (setq sepb (search-forward "\^_"))
730 (not (eobp)))
731 (beginning-of-line 2)
732 (if (save-restriction
733 (narrow-to-region (point) (1- sepb))
0820b753 734 (re-search-forward apropos-all-words-regexp nil t))
26a4a227 735 (progn
26a4a227 736 (goto-char (1+ sepa))
d2b30292
KS
737 (setq type (if (eq ?F (preceding-char))
738 2 ; function documentation
739 3) ; variable documentation
740 symbol (read)
d2b30292
KS
741 doc (buffer-substring (1+ (point)) (1- sepb)))
742 (when (apropos-true-hit-doc doc)
743 (or (and (setq apropos-item (assq symbol apropos-accumulator))
744 (setcar (cdr apropos-item)
0820b753 745 (apropos-score-doc doc)))
71296446 746 (setq apropos-item (list symbol
d2b30292
KS
747 (+ (apropos-score-symbol symbol 2)
748 (apropos-score-doc doc))
749 nil nil)
750 apropos-accumulator (cons apropos-item
751 apropos-accumulator)))
0820b753
KS
752 (when apropos-match-face
753 (setq doc (substitute-command-keys doc))
754 (if (or (string-match apropos-pattern-quoted doc)
755 (string-match apropos-all-words-regexp doc))
756 (put-text-property (match-beginning 0)
757 (match-end 0)
758 'face apropos-match-face doc)))
d2b30292 759 (setcar (nthcdr type apropos-item) doc))))
26a4a227 760 (setq sepa (goto-char sepb)))))
645c4f6a
KH
761
762(defun apropos-documentation-check-elc-file (file)
763 (if (member file apropos-files-scanned)
764 nil
26a4a227 765 (let (symbol doc beg end this-is-a-variable)
645c4f6a
KH
766 (setq apropos-files-scanned (cons file apropos-files-scanned))
767 (erase-buffer)
768 (insert-file-contents file)
769 (while (search-forward "\n#@" nil t)
770 ;; Read the comment length, and advance over it.
771 (setq end (read)
26a4a227
KH
772 beg (1+ (point))
773 end (+ (point) end -1))
774 (forward-char)
775 (if (save-restriction
776 ;; match ^ and $ relative to doc string
777 (narrow-to-region beg end)
0820b753 778 (re-search-forward apropos-all-words-regexp nil t))
645c4f6a 779 (progn
26a4a227
KH
780 (goto-char (+ end 2))
781 (setq doc (buffer-substring beg end)
782 end (- (match-end 0) beg)
d2b30292
KS
783 beg (- (match-beginning 0) beg))
784 (when (apropos-true-hit-doc doc)
785 (setq this-is-a-variable (looking-at "(def\\(var\\|const\\) ")
786 symbol (progn
787 (skip-chars-forward "(a-z")
788 (forward-char)
789 (read))
790 symbol (if (consp symbol)
791 (nth 1 symbol)
792 symbol))
793 (if (if this-is-a-variable
794 (get symbol 'variable-documentation)
795 (and (fboundp symbol) (apropos-safe-documentation symbol)))
796 (progn
797 (or (and (setq apropos-item (assq symbol apropos-accumulator))
798 (setcar (cdr apropos-item)
799 (+ (cadr apropos-item) (apropos-score-doc doc))))
800 (setq apropos-item (list symbol
801 (+ (apropos-score-symbol symbol 2)
802 (apropos-score-doc doc))
803 nil nil)
804 apropos-accumulator (cons apropos-item
805 apropos-accumulator)))
0820b753
KS
806 (when apropos-match-face
807 (setq doc (substitute-command-keys doc))
808 (if (or (string-match apropos-pattern-quoted doc)
809 (string-match apropos-all-words-regexp doc))
810 (put-text-property (match-beginning 0)
811 (match-end 0)
812 'face apropos-match-face doc)))
d2b30292
KS
813 (setcar (nthcdr (if this-is-a-variable 3 2)
814 apropos-item)
815 doc))))))))))
645c4f6a
KH
816
817
818
819(defun apropos-safe-documentation (function)
7a5348db 820 "Like `documentation', except it avoids calling `get_doc_string'.
6f8e447f 821Will return nil instead."
3925e76d 822 (while (and function (symbolp function))
6f8e447f 823 (setq function (if (fboundp function)
3925e76d 824 (symbol-function function))))
d2e1218f
RS
825 (if (eq (car-safe function) 'macro)
826 (setq function (cdr function)))
3925e76d 827 (setq function (if (byte-code-function-p function)
645c4f6a
KH
828 (if (> (length function) 4)
829 (aref function 4))
830 (if (eq (car-safe function) 'autoload)
831 (nth 2 function)
832 (if (eq (car-safe function) 'lambda)
833 (if (stringp (nth 2 function))
834 (nth 2 function)
835 (if (stringp (nth 3 function))
836 (nth 3 function)))))))
837 (if (integerp function)
838 nil
839 function))
840
841
0820b753 842(defun apropos-print (do-keys spacing &optional text nosubst)
a9155e87
KH
843 "Output result of apropos searching into buffer `*Apropos*'.
844The value of `apropos-accumulator' is the list of items to output.
71296446 845Each element should have the format
7dbffb1c 846 (SYMBOL SCORE FN-DOC VAR-DOC [PLIST-DOC WIDGET-DOC FACE-DOC GROUP-DOC]).
a9155e87
KH
847The return value is the list that was in `apropos-accumulator', sorted
848alphabetically by symbol name; but this function also sets
9cc84e31
RS
849`apropos-accumulator' to nil before returning.
850
caa8e7aa
SM
851If SPACING is non-nil, it should be a string; separate items with that string.
852If non-nil TEXT is a string that will be printed as a heading."
645c4f6a 853 (if (null apropos-accumulator)
0820b753 854 (message "No apropos matches for `%s'" apropos-pattern)
645c4f6a 855 (setq apropos-accumulator
30aab741
RS
856 (sort apropos-accumulator
857 (lambda (a b)
858 ;; Don't sort by score if user can't see the score.
859 ;; It would be confusing. -- rms.
ab765ff7 860 (if apropos-sort-by-scores
30aab741
RS
861 (or (> (cadr a) (cadr b))
862 (and (= (cadr a) (cadr b))
863 (string-lessp (car a) (car b))))
864 (string-lessp (car a) (car b))))))
09e32aaf 865 (with-output-to-temp-buffer "*Apropos*"
645c4f6a 866 (let ((p apropos-accumulator)
3925e76d 867 (old-buffer (current-buffer))
e517f56d 868 symbol item)
26a4a227
KH
869 (set-buffer standard-output)
870 (apropos-mode)
a2ee7919 871 (if (display-mouse-p)
8d33699b
EZ
872 (insert
873 "If moving the mouse over text changes the text's color, "
874 "you can click\n"
875 "mouse-2 (second button from right) on that text to "
876 "get more information.\n"))
53a7d078 877 (insert "In this buffer, go to the name of the command, or function,"
d94d5b5a
EZ
878 " or variable,\n"
879 (substitute-command-keys
880 "and type \\[apropos-follow] to get full documentation.\n\n"))
caa8e7aa 881 (if text (insert text "\n\n"))
26a4a227 882 (while (consp p)
9cc84e31
RS
883 (when (and spacing (not (bobp)))
884 (princ spacing))
26a4a227
KH
885 (setq apropos-item (car p)
886 symbol (car apropos-item)
e517f56d 887 p (cdr p))
0820b753
KS
888 ;; Insert dummy score element for backwards compatibility with 21.x
889 ;; apropos-item format.
890 (if (not (numberp (cadr apropos-item)))
891 (setq apropos-item
892 (cons (car apropos-item)
893 (cons nil (cdr apropos-item)))))
e517f56d
MB
894 (insert-text-button (symbol-name symbol)
895 'type 'apropos-symbol
896 ;; Can't use default, since user may have
897 ;; changed the variable!
898 ;; Just say `no' to variables containing faces!
899 'face apropos-symbol-face)
0820b753
KS
900 (if (and (eq apropos-sort-by-scores 'verbose)
901 (cadr apropos-item))
7dbffb1c 902 (insert " (" (number-to-string (cadr apropos-item)) ") "))
98ce2330 903 ;; Calculate key-bindings if we want them.
26a4a227
KH
904 (and do-keys
905 (commandp symbol)
906 (indent-to 30 1)
98ce2330
RS
907 (if (let ((keys
908 (save-excursion
909 (set-buffer old-buffer)
910 (where-is-internal symbol)))
911 filtered)
912 ;; Copy over the list of key sequences,
913 ;; omitting any that contain a buffer or a frame.
914 (while keys
915 (let ((key (car keys))
916 (i 0)
917 loser)
918 (while (< i (length key))
919 (if (or (framep (aref key i))
920 (bufferp (aref key i)))
921 (setq loser t))
922 (setq i (1+ i)))
923 (or loser
924 (setq filtered (cons key filtered))))
925 (setq keys (cdr keys)))
926 (setq item filtered))
927 ;; Convert the remaining keys to a string and insert.
928 (insert
26a4a227 929 (mapconcat
98ce2330 930 (lambda (key)
7a5348db 931 (setq key (condition-case ()
c3d0fe18
KH
932 (key-description key)
933 (error)))
98ce2330 934 (if apropos-keybinding-face
26a4a227
KH
935 (put-text-property 0 (length key)
936 'face apropos-keybinding-face
98ce2330
RS
937 key))
938 key)
939 item ", "))
eccfba6e
KS
940 (insert "M-x ... RET")
941 (when apropos-keybinding-face
942 (put-text-property (- (point) 11) (- (point) 8)
943 'face apropos-keybinding-face)
944 (put-text-property (- (point) 3) (point)
945 'face apropos-keybinding-face))))
26a4a227 946 (terpri)
7dbffb1c 947 (apropos-print-doc 2
26a4a227 948 (if (commandp symbol)
894e460c 949 'apropos-command
26a4a227 950 (if (apropos-macrop symbol)
894e460c
MB
951 'apropos-macro
952 'apropos-function))
0820b753
KS
953 (not nosubst))
954 (apropos-print-doc 3 'apropos-variable (not nosubst))
7dbffb1c
KS
955 (apropos-print-doc 7 'apropos-group t)
956 (apropos-print-doc 6 'apropos-face t)
957 (apropos-print-doc 5 'apropos-widget t)
958 (apropos-print-doc 4 'apropos-plist nil))
a9155e87 959 (setq buffer-read-only t))))
645c4f6a
KH
960 (prog1 apropos-accumulator
961 (setq apropos-accumulator ()))) ; permit gc
962
3925e76d 963
645c4f6a 964(defun apropos-macrop (symbol)
b965722b 965 "Return t if SYMBOL is a Lisp macro."
645c4f6a
KH
966 (and (fboundp symbol)
967 (consp (setq symbol
968 (symbol-function symbol)))
969 (or (eq (car symbol) 'macro)
970 (if (eq (car symbol) 'autoload)
971 (memq (nth 4 symbol)
972 '(macro t))))))
3925e76d 973
645c4f6a 974
894e460c 975(defun apropos-print-doc (i type do-keys)
645c4f6a 976 (if (stringp (setq i (nth i apropos-item)))
3925e76d
KH
977 (progn
978 (insert " ")
894e460c
MB
979 (insert-text-button (button-type-get type 'apropos-label)
980 'type type
e517f56d
MB
981 ;; Can't use the default button face, since
982 ;; user may have changed the variable!
983 ;; Just say `no' to variables containing faces!
984 'face apropos-label-face
894e460c 985 'apropos-symbol (car apropos-item))
e517f56d 986 (insert ": ")
645c4f6a
KH
987 (insert (if do-keys (substitute-command-keys i) i))
988 (or (bolp) (terpri)))))
3925e76d
KH
989
990
e517f56d
MB
991(defun apropos-follow ()
992 "Invokes any button at point, otherwise invokes the nearest label button."
3925e76d 993 (interactive)
e517f56d
MB
994 (button-activate
995 (or (apropos-next-label-button (line-beginning-position))
996 (error "There is nothing to follow here"))))
3925e76d
KH
997
998
999(defun apropos-describe-plist (symbol)
1000 "Display a pretty listing of SYMBOL's plist."
caa8e7aa
SM
1001 (help-setup-xref (list 'apropos-describe-plist symbol) (interactive-p))
1002 (with-output-to-temp-buffer (help-buffer)
3925e76d
KH
1003 (set-buffer standard-output)
1004 (princ "Symbol ")
1005 (prin1 symbol)
1006 (princ "'s plist is\n (")
645c4f6a 1007 (if apropos-symbol-face
caa8e7aa
SM
1008 (put-text-property (+ (point-min) 7) (- (point) 14)
1009 'face apropos-symbol-face))
3925e76d 1010 (insert (apropos-format-plist symbol "\n "))
26a4a227
KH
1011 (princ ")")
1012 (print-help-return-message)))
6f8e447f 1013
e517f56d 1014
896546cd
RS
1015(provide 'apropos)
1016
ab5796a9 1017;;; arch-tag: d56fa2ac-e56b-4ce3-84ff-852f9c0dc66e
c0274f38 1018;;; apropos.el ends here