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