(help-xref): Add follow-link property.
[bpt/emacs.git] / lisp / apropos.el
CommitLineData
e8af40ee 1;;; apropos.el --- apropos commands for users and programmers
c0274f38 2
caa8e7aa 3;; Copyright (C) 1989,94,1995,2001,02,03,2004 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 60(require 'button)
caa8e7aa 61(eval-when-compile (require 'cl))
851befd4 62
ddd2f740
RS
63(defgroup apropos nil
64 "Apropos commands for users and programmers"
c906e3ab 65 :group 'help
ddd2f740
RS
66 :prefix "apropos")
67
3925e76d 68;; I see a degradation of maybe 10-20% only.
ddd2f740 69(defcustom apropos-do-all nil
3925e76d 70 "*Whether the apropos commands should do more.
ddd2f740
RS
71
72Slows them down more or less. Set this non-nil if you have a fast machine."
73 :group 'apropos
74 :type 'boolean)
3925e76d
KH
75
76
07eeca5d
RS
77(defcustom apropos-symbol-face 'bold
78 "*Face for symbol name in Apropos output, or nil for none."
ddd2f740
RS
79 :group 'apropos
80 :type 'face)
645c4f6a 81
07eeca5d
RS
82(defcustom apropos-keybinding-face 'underline
83 "*Face for lists of keybinding in Apropos output, or nil for none."
ddd2f740
RS
84 :group 'apropos
85 :type 'face)
645c4f6a 86
07eeca5d
RS
87(defcustom apropos-label-face 'italic
88 "*Face for label (`Command', `Variable' ...) in Apropos output.
89A value of nil means don't use any special font for them, and also
90turns off mouse highlighting."
ddd2f740
RS
91 :group 'apropos
92 :type 'face)
645c4f6a 93
07eeca5d
RS
94(defcustom apropos-property-face 'bold-italic
95 "*Face for property name in apropos output, or nil for none."
ddd2f740
RS
96 :group 'apropos
97 :type 'face)
645c4f6a 98
5248b3e3 99(defcustom apropos-match-face 'match
07eeca5d
RS
100 "*Face for matching text in Apropos documentation/value, or nil for none.
101This applies when you look for matches in the documentation or variable value
102for the regexp; the part that matches gets displayed in this font."
ddd2f740
RS
103 :group 'apropos
104 :type 'face)
3925e76d 105
ab765ff7
KS
106(defcustom apropos-sort-by-scores nil
107 "*Non-nil means sort matches by scores; best match is shown first.
108The computed score is shown for each match."
30aab741
RS
109 :group 'apropos
110 :type 'boolean)
6f8e447f 111
26a4a227 112(defvar apropos-mode-map
3925e76d 113 (let ((map (make-sparse-keymap)))
e517f56d
MB
114 (set-keymap-parent map button-buffer-map)
115 ;; Use `apropos-follow' instead of just using the button
116 ;; definition of RET, so that users can use it anywhere in an
117 ;; apropos item, not just on top of a button.
3925e76d 118 (define-key map "\C-m" 'apropos-follow)
5b23b5e4
RS
119 (define-key map " " 'scroll-up)
120 (define-key map "\177" 'scroll-down)
121 (define-key map "q" 'quit-window)
3925e76d 122 map)
26a4a227 123 "Keymap used in Apropos mode.")
4de5599d 124
45f485a6
GM
125(defvar apropos-mode-hook nil
126 "*Hook run when mode is turned on.")
3925e76d 127
645c4f6a
KH
128(defvar apropos-regexp nil
129 "Regexp used in current apropos run.")
130
7dbffb1c
KS
131(defvar apropos-orig-regexp nil
132 "Regexp as entered by user.")
133
134(defvar apropos-all-regexp nil
135 "Regexp matching apropos-all-words.")
136
645c4f6a 137(defvar apropos-files-scanned ()
1259a080 138 "List of elc files already scanned in current run of `apropos-documentation'.")
645c4f6a
KH
139
140(defvar apropos-accumulator ()
141 "Alist of symbols already found in current apropos run.")
3925e76d 142
645c4f6a 143(defvar apropos-item ()
7a5348db 144 "Current item in or for `apropos-accumulator'.")
e517f56d 145
7dbffb1c
KS
146(defvar apropos-synonyms '(
147 ("find" "open" "edit")
148 ("kill" "cut")
149 ("yank" "paste"))
150 "List of synonyms known by apropos.
151Each element is a list of words where the first word is the standard emacs
152term, and the rest of the words are alternative terms.")
153
154(defvar apropos-words ()
155 "Current list of words.")
156
157(defvar apropos-all-words ()
158 "Current list of words and synonyms.")
159
e517f56d
MB
160\f
161;;; Button types used by apropos
162
163(define-button-type 'apropos-symbol
164 'face apropos-symbol-face
894e460c
MB
165 'help-echo "mouse-2, RET: Display more help on this symbol"
166 'action #'apropos-symbol-button-display-help
167 'skip t)
e517f56d
MB
168
169(defun apropos-symbol-button-display-help (button)
170 "Display further help for the `apropos-symbol' button BUTTON."
171 (button-activate
172 (or (apropos-next-label-button (button-start button))
173 (error "There is nothing to follow for `%s'" (button-label button)))))
174
894e460c
MB
175(define-button-type 'apropos-function
176 'apropos-label "Function"
177 'action (lambda (button)
178 (describe-function (button-get button 'apropos-symbol)))
179 'help-echo "mouse-2, RET: Display more help on this function")
180(define-button-type 'apropos-macro
181 'apropos-label "Macro"
182 'action (lambda (button)
183 (describe-function (button-get button 'apropos-symbol)))
184 'help-echo "mouse-2, RET: Display more help on this macro")
185(define-button-type 'apropos-command
186 'apropos-label "Command"
187 'action (lambda (button)
188 (describe-function (button-get button 'apropos-symbol)))
189 'help-echo "mouse-2, RET: Display more help on this command")
7cfedc97 190
894e460c
MB
191;; We used to use `customize-variable-other-window' instead for a
192;; customizable variable, but that is slow. It is better to show an
193;; ordinary help buffer and let the user click on the customization
194;; button in that buffer, if he wants to.
195;; Likewise for `customize-face-other-window'.
196(define-button-type 'apropos-variable
197 'apropos-label "Variable"
198 'help-echo "mouse-2, RET: Display more help on this variable"
199 'action (lambda (button)
200 (describe-variable (button-get button 'apropos-symbol))))
201
202(define-button-type 'apropos-face
203 'apropos-label "Face"
204 'help-echo "mouse-2, RET: Display more help on this face"
205 'action (lambda (button)
206 (describe-face (button-get button 'apropos-symbol))))
207
208(define-button-type 'apropos-group
209 'apropos-label "Group"
210 'help-echo "mouse-2, RET: Display more help on this group"
211 'action (lambda (button)
2d37b91e 212 (customize-group-other-window
894e460c
MB
213 (button-get button 'apropos-symbol))))
214
215(define-button-type 'apropos-widget
216 'apropos-label "Widget"
217 'help-echo "mouse-2, RET: Display more help on this widget"
218 'action (lambda (button)
219 (widget-browse-other-window (button-get button 'apropos-symbol))))
220
221(define-button-type 'apropos-plist
222 'apropos-label "Plist"
223 'help-echo "mouse-2, RET: Display more help on this plist"
224 'action (lambda (button)
225 (apropos-describe-plist (button-get button 'apropos-symbol))))
e517f56d
MB
226
227(defun apropos-next-label-button (pos)
a11a4e9f 228 "Return the next apropos label button after POS, or nil if there's none.
e517f56d
MB
229Will also return nil if more than one `apropos-symbol' button is encountered
230before finding a label."
a101302b 231 (let* ((button (next-button pos t))
e517f56d 232 (already-hit-symbol nil)
3b8c60f1
MB
233 (label (and button (button-get button 'apropos-label)))
234 (type (and button (button-get button 'type))))
e517f56d 235 (while (and button
3b8c60f1
MB
236 (not label)
237 (or (not (eq type 'apropos-symbol))
e517f56d 238 (not already-hit-symbol)))
3b8c60f1 239 (when (eq type 'apropos-symbol)
e517f56d
MB
240 (setq already-hit-symbol t))
241 (setq button (next-button (button-start button)))
242 (when button
3b8c60f1
MB
243 (setq label (button-get button 'apropos-label))
244 (setq type (button-get button 'type))))
245 (and label button)))
e517f56d 246
645c4f6a 247\f
7dbffb1c
KS
248(defun apropos-words-to-regexp (words wild)
249 "Make regexp matching any two of the words in WORDS."
250 (concat "\\("
251 (mapconcat 'identity words "\\|")
80f5d2ef 252 "\\)"
71296446 253 (if (cdr words)
80f5d2ef
AS
254 (concat wild
255 "\\("
7dbffb1c
KS
256 (mapconcat 'identity words "\\|")
257 "\\)")
258 "")))
259
260(defun apropos-rewrite-regexp (regexp)
261 "Rewrite a list of words to a regexp matching all permutations.
262If REGEXP is already a regexp, don't modify it."
263 (setq apropos-orig-regexp regexp)
264 (setq apropos-words () apropos-all-words ())
265 (if (string-equal (regexp-quote regexp) regexp)
266 ;; We don't actually make a regexp matching all permutations.
267 ;; Instead, for e.g. "a b c", we make a regexp matching
268 ;; any combination of two or more words like this:
269 ;; (a|b|c).*(a|b|c) which may give some false matches,
270 ;; but as long as it also gives the right ones, that's ok.
271 (let ((words (split-string regexp "[ \t]+")))
272 (dolist (word words)
273 (let ((syn apropos-synonyms) (s word) (a word))
274 (while syn
275 (if (member word (car syn))
276 (progn
277 (setq a (mapconcat 'identity (car syn) "\\|"))
278 (if (member word (cdr (car syn)))
279 (setq s a))
280 (setq syn nil))
281 (setq syn (cdr syn))))
282 (setq apropos-words (cons s apropos-words)
283 apropos-all-words (cons a apropos-all-words))))
284 (setq apropos-all-regexp (apropos-words-to-regexp apropos-all-words ".+"))
285 (apropos-words-to-regexp apropos-words ".*?"))
286 (setq apropos-all-regexp regexp)))
287
288(defun apropos-calc-scores (str words)
289 "Return apropos scores for string STR matching WORDS.
290Value is a list of offsets of the words into the string."
291 (let ((scores ())
292 i)
293 (if words
294 (dolist (word words scores)
295 (if (setq i (string-match word str))
296 (setq scores (cons i scores))))
297 ;; Return list of start and end position of regexp
298 (string-match apropos-regexp str)
299 (list (match-beginning 0) (match-end 0)))))
300
301(defun apropos-score-str (str)
302 "Return apropos score for string STR."
303 (if str
d5857a96
KS
304 (let* (
305 (l (length str))
306 (score (- (/ l 10)))
7dbffb1c
KS
307 i)
308 (dolist (s (apropos-calc-scores str apropos-all-words) score)
d5857a96 309 (setq score (+ score 1000 (/ (* (- l s) 1000) l)))))
7dbffb1c
KS
310 0))
311
312(defun apropos-score-doc (doc)
313 "Return apropos score for documentation string DOC."
314 (if doc
315 (let ((score 0)
316 (l (length doc))
317 i)
318 (dolist (s (apropos-calc-scores doc apropos-all-words) score)
319 (setq score (+ score 50 (/ (* (- l s) 50) l)))))
320 0))
71296446 321
7dbffb1c
KS
322(defun apropos-score-symbol (symbol &optional weight)
323 "Return apropos score for SYMBOL."
324 (setq symbol (symbol-name symbol))
325 (let ((score 0)
326 (l (length symbol))
327 i)
328 (dolist (s (apropos-calc-scores symbol apropos-words) (* score (or weight 3)))
329 (setq score (+ score (- 60 l) (/ (* (- l s) 60) l))))))
330
d2b30292
KS
331(defun apropos-true-hit (str words)
332 "Return t if STR is a genuine hit.
333This may fail if only one of the keywords is matched more than once.
334This requires that at least 2 keywords (unless only one was given)."
335 (or (not str)
336 (not words)
337 (not (cdr words))
338 (> (length (apropos-calc-scores str words)) 1)))
339
340(defun apropos-false-hit-symbol (symbol)
341 "Return t if SYMBOL is not really matched by the current keywords."
342 (not (apropos-true-hit (symbol-name symbol) apropos-words)))
343
344(defun apropos-false-hit-str (str)
345 "Return t if STR is not really matched by the current keywords."
346 (not (apropos-true-hit str apropos-words)))
347
348(defun apropos-true-hit-doc (doc)
349 "Return t if DOC is really matched by the current keywords."
350 (apropos-true-hit doc apropos-all-words))
351
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))
caa8e7aa
SM
455 (apropos-symbols-internal
456 (apropos-internal apropos-regexp
645c4f6a
KH
457 (and (not do-all)
458 (not apropos-do-all)
459 (lambda (symbol)
460 (or (fboundp symbol)
461 (boundp symbol)
5edc67d3 462 (facep symbol)
caa8e7aa
SM
463 (symbol-plist symbol)))))
464 (or do-all apropos-do-all)))
465
466(defun apropos-symbols-internal (symbols keys &optional text)
467 ;; Filter out entries that are marked as apropos-inhibit.
468 (let ((all nil))
469 (dolist (symbol symbols)
470 (unless (get symbol 'apropos-inhibit)
471 (push symbol all)))
472 (setq symbols all))
473 (let ((apropos-accumulator
474 (mapcar
475 (lambda (symbol)
476 (let (doc properties)
477 (list
478 symbol
479 (apropos-score-symbol symbol)
480 (when (fboundp symbol)
481 (if (setq doc (condition-case nil
482 (documentation symbol t)
483 (void-function
484 "(alias for undefined function)")
485 (error
486 "(can't retrieve function documentation)")))
487 (substring doc 0 (string-match "\n" doc))
488 "(not documented)"))
489 (when (boundp symbol)
490 (apropos-documentation-property
914b40da 491 symbol 'variable-documentation t))
a9155e87
KH
492 (when (setq properties (symbol-plist symbol))
493 (setq doc (list (car properties)))
494 (while (setq properties (cdr (cdr properties)))
495 (setq doc (cons (car properties) doc)))
496 (mapconcat #'symbol-name (nreverse doc) " "))
497 (when (get symbol 'widget-type)
914b40da
RS
498 (apropos-documentation-property
499 symbol 'widget-documentation t))
caa8e7aa
SM
500 (when (facep symbol)
501 (apropos-documentation-property
502 symbol 'face-documentation t))
503 (when (get symbol 'custom-group)
914b40da 504 (apropos-documentation-property
caa8e7aa
SM
505 symbol 'group-documentation t)))))
506 symbols)))
507 (apropos-print keys nil text)))
3925e76d
KH
508
509
6f8e447f 510;;;###autoload
645c4f6a 511(defun apropos-value (apropos-regexp &optional do-all)
7a5348db
DL
512 "Show all symbols whose value's printed image matches APROPOS-REGEXP.
513With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also looks
3925e76d 514at the function and at the names and values of properties.
645c4f6a 515Returns list of symbols and values found."
7dbffb1c
KS
516 (interactive "sApropos value (regexp or words): \nP")
517 (setq apropos-regexp (apropos-rewrite-regexp apropos-regexp))
645c4f6a
KH
518 (or do-all (setq do-all apropos-do-all))
519 (setq apropos-accumulator ())
520 (let (f v p)
3925e76d
KH
521 (mapatoms
522 (lambda (symbol)
523 (setq f nil v nil p nil)
7dbffb1c
KS
524 (or (memq symbol '(apropos-regexp
525 apropos-orig-regexp apropos-all-regexp
526 apropos-words apropos-all-words
527 do-all apropos-accumulator
528 symbol f v p))
645c4f6a 529 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
3925e76d 530 (if do-all
645c4f6a
KH
531 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
532 p (apropos-format-plist symbol "\n " t)))
d2b30292
KS
533 (if (apropos-false-hit-str v)
534 (setq v nil))
535 (if (apropos-false-hit-str f)
536 (setq f nil))
537 (if (apropos-false-hit-str p)
538 (setq p nil))
3925e76d 539 (if (or f v p)
71296446 540 (setq apropos-accumulator (cons (list symbol
7dbffb1c
KS
541 (+ (apropos-score-str f)
542 (apropos-score-str v)
543 (apropos-score-str p))
544 f v p)
645c4f6a 545 apropos-accumulator))))))
9cc84e31 546 (apropos-print nil "\n----------------\n"))
3925e76d
KH
547
548
645c4f6a
KH
549;;;###autoload
550(defun apropos-documentation (apropos-regexp &optional do-all)
7a5348db
DL
551 "Show symbols whose documentation contain matches for APROPOS-REGEXP.
552With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also use
645c4f6a
KH
553documentation that is not stored in the documentation file and show key
554bindings.
555Returns list of symbols and documentation found."
7dbffb1c
KS
556 (interactive "sApropos documentation (regexp or words): \nP")
557 (setq apropos-regexp (apropos-rewrite-regexp apropos-regexp))
645c4f6a
KH
558 (or do-all (setq do-all apropos-do-all))
559 (setq apropos-accumulator () apropos-files-scanned ())
560 (let ((standard-input (get-buffer-create " apropos-temp"))
7dbffb1c 561 f v sf sv)
645c4f6a
KH
562 (unwind-protect
563 (save-excursion
564 (set-buffer standard-input)
565 (apropos-documentation-check-doc-file)
566 (if do-all
567 (mapatoms
568 (lambda (symbol)
569 (setq f (apropos-safe-documentation symbol)
26a4a227
KH
570 v (get symbol 'variable-documentation))
571 (if (integerp v) (setq v))
572 (setq f (apropos-documentation-internal f)
573 v (apropos-documentation-internal v))
7dbffb1c
KS
574 (setq sf (apropos-score-doc f)
575 sv (apropos-score-doc v))
645c4f6a
KH
576 (if (or f v)
577 (if (setq apropos-item
578 (cdr (assq symbol apropos-accumulator)))
579 (progn
580 (if f
7dbffb1c
KS
581 (progn
582 (setcar (nthcdr 1 apropos-item) f)
583 (setcar apropos-item (+ (car apropos-item) sf))))
645c4f6a 584 (if v
7dbffb1c
KS
585 (progn
586 (setcar (nthcdr 2 apropos-item) v)
587 (setcar apropos-item (+ (car apropos-item) sv)))))
645c4f6a 588 (setq apropos-accumulator
71296446 589 (cons (list symbol
7dbffb1c
KS
590 (+ (apropos-score-symbol symbol 2) sf sv)
591 f v)
645c4f6a 592 apropos-accumulator)))))))
9cc84e31 593 (apropos-print nil "\n----------------\n"))
645c4f6a
KH
594 (kill-buffer standard-input))))
595
596\f
597(defun apropos-value-internal (predicate symbol function)
598 (if (funcall predicate symbol)
599 (progn
600 (setq symbol (prin1-to-string (funcall function symbol)))
601 (if (string-match apropos-regexp symbol)
602 (progn
603 (if apropos-match-face
604 (put-text-property (match-beginning 0) (match-end 0)
605 'face apropos-match-face
606 symbol))
607 symbol)))))
608
609(defun apropos-documentation-internal (doc)
610 (if (consp doc)
611 (apropos-documentation-check-elc-file (car doc))
612 (and doc
7dbffb1c 613 (string-match apropos-all-regexp doc)
d2b30292 614 (save-match-data (apropos-true-hit-doc doc))
645c4f6a
KH
615 (progn
616 (if apropos-match-face
617 (put-text-property (match-beginning 0)
618 (match-end 0)
619 'face apropos-match-face
620 (setq doc (copy-sequence doc))))
621 doc))))
622
623(defun apropos-format-plist (pl sep &optional compare)
3925e76d
KH
624 (setq pl (symbol-plist pl))
625 (let (p p-out)
626 (while pl
627 (setq p (format "%s %S" (car pl) (nth 1 pl)))
645c4f6a
KH
628 (if (or (not compare) (string-match apropos-regexp p))
629 (if apropos-property-face
3925e76d 630 (put-text-property 0 (length (symbol-name (car pl)))
645c4f6a 631 'face apropos-property-face p))
3925e76d 632 (setq p nil))
645c4f6a
KH
633 (if p
634 (progn
635 (and compare apropos-match-face
636 (put-text-property (match-beginning 0) (match-end 0)
637 'face apropos-match-face
638 p))
639 (setq p-out (concat p-out (if p-out sep) p))))
3925e76d
KH
640 (setq pl (nthcdr 2 pl)))
641 p-out))
642
6f8e447f 643
645c4f6a 644;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
3925e76d 645
645c4f6a 646(defun apropos-documentation-check-doc-file ()
26a4a227
KH
647 (let (type symbol (sepa 2) sepb beg end)
648 (insert ?\^_)
649 (backward-char)
645c4f6a 650 (insert-file-contents (concat doc-directory internal-doc-file-name))
26a4a227
KH
651 (forward-char)
652 (while (save-excursion
653 (setq sepb (search-forward "\^_"))
654 (not (eobp)))
655 (beginning-of-line 2)
656 (if (save-restriction
657 (narrow-to-region (point) (1- sepb))
7dbffb1c 658 (re-search-forward apropos-all-regexp nil t))
26a4a227
KH
659 (progn
660 (setq beg (match-beginning 0)
661 end (point))
662 (goto-char (1+ sepa))
d2b30292
KS
663 (setq type (if (eq ?F (preceding-char))
664 2 ; function documentation
665 3) ; variable documentation
666 symbol (read)
667 beg (- beg (point) 1)
668 end (- end (point) 1)
669 doc (buffer-substring (1+ (point)) (1- sepb)))
670 (when (apropos-true-hit-doc doc)
671 (or (and (setq apropos-item (assq symbol apropos-accumulator))
672 (setcar (cdr apropos-item)
673 (+ (cadr apropos-item) (apropos-score-doc doc))))
71296446 674 (setq apropos-item (list symbol
d2b30292
KS
675 (+ (apropos-score-symbol symbol 2)
676 (apropos-score-doc doc))
677 nil nil)
678 apropos-accumulator (cons apropos-item
679 apropos-accumulator)))
680 (if apropos-match-face
681 (put-text-property beg end 'face apropos-match-face doc))
682 (setcar (nthcdr type apropos-item) doc))))
26a4a227 683 (setq sepa (goto-char sepb)))))
645c4f6a
KH
684
685(defun apropos-documentation-check-elc-file (file)
686 (if (member file apropos-files-scanned)
687 nil
26a4a227 688 (let (symbol doc beg end this-is-a-variable)
645c4f6a
KH
689 (setq apropos-files-scanned (cons file apropos-files-scanned))
690 (erase-buffer)
691 (insert-file-contents file)
692 (while (search-forward "\n#@" nil t)
693 ;; Read the comment length, and advance over it.
694 (setq end (read)
26a4a227
KH
695 beg (1+ (point))
696 end (+ (point) end -1))
697 (forward-char)
698 (if (save-restriction
699 ;; match ^ and $ relative to doc string
700 (narrow-to-region beg end)
7dbffb1c 701 (re-search-forward apropos-all-regexp nil t))
645c4f6a 702 (progn
26a4a227
KH
703 (goto-char (+ end 2))
704 (setq doc (buffer-substring beg end)
705 end (- (match-end 0) beg)
d2b30292
KS
706 beg (- (match-beginning 0) beg))
707 (when (apropos-true-hit-doc doc)
708 (setq this-is-a-variable (looking-at "(def\\(var\\|const\\) ")
709 symbol (progn
710 (skip-chars-forward "(a-z")
711 (forward-char)
712 (read))
713 symbol (if (consp symbol)
714 (nth 1 symbol)
715 symbol))
716 (if (if this-is-a-variable
717 (get symbol 'variable-documentation)
718 (and (fboundp symbol) (apropos-safe-documentation symbol)))
719 (progn
720 (or (and (setq apropos-item (assq symbol apropos-accumulator))
721 (setcar (cdr apropos-item)
722 (+ (cadr apropos-item) (apropos-score-doc doc))))
723 (setq apropos-item (list symbol
724 (+ (apropos-score-symbol symbol 2)
725 (apropos-score-doc doc))
726 nil nil)
727 apropos-accumulator (cons apropos-item
728 apropos-accumulator)))
729 (if apropos-match-face
730 (put-text-property beg end 'face apropos-match-face
731 doc))
732 (setcar (nthcdr (if this-is-a-variable 3 2)
733 apropos-item)
734 doc))))))))))
645c4f6a
KH
735
736
737
738(defun apropos-safe-documentation (function)
7a5348db 739 "Like `documentation', except it avoids calling `get_doc_string'.
6f8e447f 740Will return nil instead."
3925e76d 741 (while (and function (symbolp function))
6f8e447f 742 (setq function (if (fboundp function)
3925e76d 743 (symbol-function function))))
d2e1218f
RS
744 (if (eq (car-safe function) 'macro)
745 (setq function (cdr function)))
3925e76d 746 (setq function (if (byte-code-function-p function)
645c4f6a
KH
747 (if (> (length function) 4)
748 (aref function 4))
749 (if (eq (car-safe function) 'autoload)
750 (nth 2 function)
751 (if (eq (car-safe function) 'lambda)
752 (if (stringp (nth 2 function))
753 (nth 2 function)
754 (if (stringp (nth 3 function))
755 (nth 3 function)))))))
756 (if (integerp function)
757 nil
758 function))
759
760
caa8e7aa 761(defun apropos-print (do-keys spacing &optional text)
a9155e87
KH
762 "Output result of apropos searching into buffer `*Apropos*'.
763The value of `apropos-accumulator' is the list of items to output.
71296446 764Each element should have the format
7dbffb1c 765 (SYMBOL SCORE FN-DOC VAR-DOC [PLIST-DOC WIDGET-DOC FACE-DOC GROUP-DOC]).
a9155e87
KH
766The return value is the list that was in `apropos-accumulator', sorted
767alphabetically by symbol name; but this function also sets
9cc84e31
RS
768`apropos-accumulator' to nil before returning.
769
caa8e7aa
SM
770If SPACING is non-nil, it should be a string; separate items with that string.
771If non-nil TEXT is a string that will be printed as a heading."
645c4f6a 772 (if (null apropos-accumulator)
7dbffb1c 773 (message "No apropos matches for `%s'" apropos-orig-regexp)
645c4f6a 774 (setq apropos-accumulator
30aab741
RS
775 (sort apropos-accumulator
776 (lambda (a b)
777 ;; Don't sort by score if user can't see the score.
778 ;; It would be confusing. -- rms.
ab765ff7 779 (if apropos-sort-by-scores
30aab741
RS
780 (or (> (cadr a) (cadr b))
781 (and (= (cadr a) (cadr b))
782 (string-lessp (car a) (car b))))
783 (string-lessp (car a) (car b))))))
09e32aaf 784 (with-output-to-temp-buffer "*Apropos*"
645c4f6a 785 (let ((p apropos-accumulator)
3925e76d 786 (old-buffer (current-buffer))
e517f56d 787 symbol item)
26a4a227
KH
788 (set-buffer standard-output)
789 (apropos-mode)
a2ee7919 790 (if (display-mouse-p)
8d33699b
EZ
791 (insert
792 "If moving the mouse over text changes the text's color, "
793 "you can click\n"
794 "mouse-2 (second button from right) on that text to "
795 "get more information.\n"))
53a7d078 796 (insert "In this buffer, go to the name of the command, or function,"
d94d5b5a
EZ
797 " or variable,\n"
798 (substitute-command-keys
799 "and type \\[apropos-follow] to get full documentation.\n\n"))
caa8e7aa 800 (if text (insert text "\n\n"))
26a4a227 801 (while (consp p)
9cc84e31
RS
802 (when (and spacing (not (bobp)))
803 (princ spacing))
26a4a227
KH
804 (setq apropos-item (car p)
805 symbol (car apropos-item)
e517f56d
MB
806 p (cdr p))
807 (insert-text-button (symbol-name symbol)
808 'type 'apropos-symbol
809 ;; Can't use default, since user may have
810 ;; changed the variable!
811 ;; Just say `no' to variables containing faces!
812 'face apropos-symbol-face)
ab765ff7 813 (if apropos-sort-by-scores
7dbffb1c 814 (insert " (" (number-to-string (cadr apropos-item)) ") "))
98ce2330 815 ;; Calculate key-bindings if we want them.
26a4a227
KH
816 (and do-keys
817 (commandp symbol)
818 (indent-to 30 1)
98ce2330
RS
819 (if (let ((keys
820 (save-excursion
821 (set-buffer old-buffer)
822 (where-is-internal symbol)))
823 filtered)
824 ;; Copy over the list of key sequences,
825 ;; omitting any that contain a buffer or a frame.
826 (while keys
827 (let ((key (car keys))
828 (i 0)
829 loser)
830 (while (< i (length key))
831 (if (or (framep (aref key i))
832 (bufferp (aref key i)))
833 (setq loser t))
834 (setq i (1+ i)))
835 (or loser
836 (setq filtered (cons key filtered))))
837 (setq keys (cdr keys)))
838 (setq item filtered))
839 ;; Convert the remaining keys to a string and insert.
840 (insert
26a4a227 841 (mapconcat
98ce2330 842 (lambda (key)
7a5348db 843 (setq key (condition-case ()
c3d0fe18
KH
844 (key-description key)
845 (error)))
98ce2330 846 (if apropos-keybinding-face
26a4a227
KH
847 (put-text-property 0 (length key)
848 'face apropos-keybinding-face
98ce2330
RS
849 key))
850 key)
851 item ", "))
82fbaa5e
RS
852 (insert "M-x")
853 (put-text-property (- (point) 3) (point)
854 'face apropos-keybinding-face)
855 (insert " " (symbol-name symbol) " ")
856 (insert "RET")
857 (put-text-property (- (point) 3) (point)
858 'face apropos-keybinding-face)))
26a4a227 859 (terpri)
7dbffb1c 860 (apropos-print-doc 2
26a4a227 861 (if (commandp symbol)
894e460c 862 'apropos-command
26a4a227 863 (if (apropos-macrop symbol)
894e460c
MB
864 'apropos-macro
865 'apropos-function))
8d7f1de3 866 t)
7dbffb1c
KS
867 (apropos-print-doc 3 'apropos-variable t)
868 (apropos-print-doc 7 'apropos-group t)
869 (apropos-print-doc 6 'apropos-face t)
870 (apropos-print-doc 5 'apropos-widget t)
871 (apropos-print-doc 4 'apropos-plist nil))
a9155e87 872 (setq buffer-read-only t))))
645c4f6a
KH
873 (prog1 apropos-accumulator
874 (setq apropos-accumulator ()))) ; permit gc
875
3925e76d 876
645c4f6a 877(defun apropos-macrop (symbol)
b965722b 878 "Return t if SYMBOL is a Lisp macro."
645c4f6a
KH
879 (and (fboundp symbol)
880 (consp (setq symbol
881 (symbol-function symbol)))
882 (or (eq (car symbol) 'macro)
883 (if (eq (car symbol) 'autoload)
884 (memq (nth 4 symbol)
885 '(macro t))))))
3925e76d 886
645c4f6a 887
894e460c 888(defun apropos-print-doc (i type do-keys)
645c4f6a 889 (if (stringp (setq i (nth i apropos-item)))
3925e76d
KH
890 (progn
891 (insert " ")
894e460c
MB
892 (insert-text-button (button-type-get type 'apropos-label)
893 'type type
e517f56d
MB
894 ;; Can't use the default button face, since
895 ;; user may have changed the variable!
896 ;; Just say `no' to variables containing faces!
897 'face apropos-label-face
894e460c 898 'apropos-symbol (car apropos-item))
e517f56d 899 (insert ": ")
645c4f6a
KH
900 (insert (if do-keys (substitute-command-keys i) i))
901 (or (bolp) (terpri)))))
3925e76d
KH
902
903
e517f56d
MB
904(defun apropos-follow ()
905 "Invokes any button at point, otherwise invokes the nearest label button."
3925e76d 906 (interactive)
e517f56d
MB
907 (button-activate
908 (or (apropos-next-label-button (line-beginning-position))
909 (error "There is nothing to follow here"))))
3925e76d
KH
910
911
912(defun apropos-describe-plist (symbol)
913 "Display a pretty listing of SYMBOL's plist."
caa8e7aa
SM
914 (help-setup-xref (list 'apropos-describe-plist symbol) (interactive-p))
915 (with-output-to-temp-buffer (help-buffer)
3925e76d
KH
916 (set-buffer standard-output)
917 (princ "Symbol ")
918 (prin1 symbol)
919 (princ "'s plist is\n (")
645c4f6a 920 (if apropos-symbol-face
caa8e7aa
SM
921 (put-text-property (+ (point-min) 7) (- (point) 14)
922 'face apropos-symbol-face))
3925e76d 923 (insert (apropos-format-plist symbol "\n "))
26a4a227
KH
924 (princ ")")
925 (print-help-return-message)))
6f8e447f 926
e517f56d 927
896546cd
RS
928(provide 'apropos)
929
ab5796a9 930;;; arch-tag: d56fa2ac-e56b-4ce3-84ff-852f9c0dc66e
c0274f38 931;;; apropos.el ends here