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