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