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