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