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