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