1 ;;; apropos.el --- apropos commands for users and programmers
3 ;; Copyright (C) 1989, 1994, 1995, 2001 Free Software Foundation, Inc.
5 ;; Author: Joe Wells <jbw@bigbird.bu.edu>
6 ;; Rewritten: Daniel Pfeiffer <occitan@esperanto.org>
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)
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.
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>.
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.
63 "Apropos commands for users and programmers"
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."
76 (defcustom apropos-symbol-face
'bold
77 "*Face for symbol name in Apropos output, or nil for none."
81 (defcustom apropos-keybinding-face
'underline
82 "*Face for lists of keybinding in Apropos output, or nil for none."
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."
93 (defcustom apropos-property-face
'bold-italic
94 "*Face for property name in apropos output, or nil for none."
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."
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
)
117 "Keymap used in Apropos mode.")
119 (defvar apropos-mode-hook nil
120 "*Hook run when mode is turned on.")
122 (defvar apropos-regexp nil
123 "Regexp used in current apropos run.")
125 (defvar apropos-files-scanned
()
126 "List of elc files already scanned in current run of `apropos-documentation'.")
128 (defvar apropos-accumulator
()
129 "Alist of symbols already found in current apropos run.")
131 (defvar apropos-item
()
132 "Current item in or for `apropos-accumulator'.")
135 ;;; Button types used by apropos
137 (define-button-type 'apropos-symbol
138 'face apropos-symbol-face
139 'help-echo
"mouse-2, RET: Display more help on this symbol"
140 'action
#'apropos-symbol-button-display-help
143 (defun apropos-symbol-button-display-help (button)
144 "Display further help for the `apropos-symbol' button BUTTON."
146 (or (apropos-next-label-button (button-start button
))
147 (error "There is nothing to follow for `%s'" (button-label button
)))))
149 (define-button-type 'apropos-function
150 'apropos-label
"Function"
151 'action
(lambda (button)
152 (describe-function (button-get button
'apropos-symbol
)))
153 'help-echo
"mouse-2, RET: Display more help on this function")
154 (define-button-type 'apropos-macro
155 'apropos-label
"Macro"
156 'action
(lambda (button)
157 (describe-function (button-get button
'apropos-symbol
)))
158 'help-echo
"mouse-2, RET: Display more help on this macro")
159 (define-button-type 'apropos-command
160 'apropos-label
"Command"
161 'action
(lambda (button)
162 (describe-function (button-get button
'apropos-symbol
)))
163 'help-echo
"mouse-2, RET: Display more help on this command")
165 ;; We used to use `customize-variable-other-window' instead for a
166 ;; customizable variable, but that is slow. It is better to show an
167 ;; ordinary help buffer and let the user click on the customization
168 ;; button in that buffer, if he wants to.
169 ;; Likewise for `customize-face-other-window'.
170 (define-button-type 'apropos-variable
171 'apropos-label
"Variable"
172 'help-echo
"mouse-2, RET: Display more help on this variable"
173 'action
(lambda (button)
174 (describe-variable (button-get button
'apropos-symbol
))))
176 (define-button-type 'apropos-face
177 'apropos-label
"Face"
178 'help-echo
"mouse-2, RET: Display more help on this face"
179 'action
(lambda (button)
180 (describe-face (button-get button
'apropos-symbol
))))
182 (define-button-type 'apropos-group
183 'apropos-label
"Group"
184 'help-echo
"mouse-2, RET: Display more help on this group"
185 'action
(lambda (button)
186 (customize-variable-other-window
187 (button-get button
'apropos-symbol
))))
189 (define-button-type 'apropos-widget
190 'apropos-label
"Widget"
191 'help-echo
"mouse-2, RET: Display more help on this widget"
192 'action
(lambda (button)
193 (widget-browse-other-window (button-get button
'apropos-symbol
))))
195 (define-button-type 'apropos-plist
196 'apropos-label
"Plist"
197 'help-echo
"mouse-2, RET: Display more help on this plist"
198 'action
(lambda (button)
199 (apropos-describe-plist (button-get button
'apropos-symbol
))))
201 (defun apropos-next-label-button (pos)
202 "Return the next apropos label button after POS, or nil if there's none.
203 Will also return nil if more than one `apropos-symbol' button is encountered
204 before finding a label."
205 (let* ((button (next-button pos t
))
206 (already-hit-symbol nil
)
207 (label (and button
(button-get button
'apropos-label
)))
208 (type (and button
(button-get button
'type
))))
211 (or (not (eq type
'apropos-symbol
))
212 (not already-hit-symbol
)))
213 (when (eq type
'apropos-symbol
)
214 (setq already-hit-symbol t
))
215 (setq button
(next-button (button-start button
)))
217 (setq label
(button-get button
'apropos-label
))
218 (setq type
(button-get button
'type
))))
223 (define-derived-mode apropos-mode fundamental-mode
"Apropos"
224 "Major mode for following hyperlinks in output of apropos commands.
226 \\{apropos-mode-map}")
229 (defun apropos-variable (regexp &optional do-all
)
230 "Show user variables that match REGEXP.
231 With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also show
233 (interactive (list (read-string
235 (if (or current-prefix-arg apropos-do-all
)
240 (apropos-command regexp nil
241 (if (or do-all apropos-do-all
)
244 (get symbol
'variable-documentation
)))
247 ;; For auld lang syne:
249 (fset 'command-apropos
'apropos-command
)
251 (defun apropos-command (apropos-regexp &optional do-all var-predicate
)
252 "Show commands (interactively callable functions) that match APROPOS-REGEXP.
253 With optional prefix DO-ALL, or if `apropos-do-all' is non-nil, also show
254 noninteractive functions.
256 If VAR-PREDICATE is non-nil, show only variables, and only those that
257 satisfy the predicate VAR-PREDICATE."
258 (interactive (list (read-string (concat
260 (if (or current-prefix-arg
266 (let ((standard-output (get-buffer-create "*Apropos*")))
267 (print-help-return-message 'identity
))))
268 (or do-all
(setq do-all apropos-do-all
))
269 (setq apropos-accumulator
270 (apropos-internal apropos-regexp
272 (if do-all
'functionp
'commandp
))))
273 (let ((tem apropos-accumulator
))
275 (if (get (car tem
) 'apropos-inhibit
)
276 (setq apropos-accumulator
(delq (car tem
) apropos-accumulator
)))
277 (setq tem
(cdr tem
))))
278 (let ((p apropos-accumulator
)
282 (setq symbol
(car p
))
283 (unless var-predicate
284 (if (functionp symbol
)
285 (if (setq doc
(documentation symbol t
))
286 (substring doc
0 (string-match "\n" doc
))
287 "(not documented)")))
289 (funcall var-predicate symbol
)
290 (if (setq doc
(documentation-property
291 symbol
'variable-documentation t
))
293 (string-match "\n" doc
))))))
295 (and (apropos-print t nil
)
301 (defun apropos (apropos-regexp &optional do-all
)
302 "Show all bound symbols whose names match APROPOS-REGEXP.
303 With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also
304 show unbound symbols and key bindings, which is a little more
305 time-consuming. Returns list of symbols and documentation found."
306 (interactive "sApropos symbol (regexp): \nP")
307 (setq apropos-accumulator
308 (apropos-internal apropos-regexp
315 (symbol-plist symbol
))))))
316 (let ((tem apropos-accumulator
))
318 (if (get (car tem
) 'apropos-inhibit
)
319 (setq apropos-accumulator
(delq (car tem
) apropos-accumulator
)))
320 (setq tem
(cdr tem
))))
321 (let ((p apropos-accumulator
)
322 symbol doc properties
)
325 (setq symbol
(car p
))
326 (when (fboundp symbol
)
327 (if (setq doc
(condition-case nil
328 (documentation symbol t
)
330 "(alias for undefined function)")))
331 (substring doc
0 (string-match "\n" doc
))
333 (when (boundp symbol
)
334 (if (setq doc
(documentation-property
335 symbol
'variable-documentation t
))
336 (substring doc
0 (string-match "\n" doc
))
338 (when (setq properties
(symbol-plist symbol
))
339 (setq doc
(list (car properties
)))
340 (while (setq properties
(cdr (cdr properties
)))
341 (setq doc
(cons (car properties
) doc
)))
342 (mapconcat #'symbol-name
(nreverse doc
) " "))
343 (when (get symbol
'widget-type
)
344 (if (setq doc
(documentation-property
345 symbol
'widget-documentation t
))
347 (string-match "\n" doc
))
350 (if (setq doc
(documentation-property
351 symbol
'face-documentation t
))
353 (string-match "\n" doc
))
355 (when (get symbol
'custom-group
)
356 (if (setq doc
(documentation-property
357 symbol
'group-documentation t
))
359 (string-match "\n" doc
))
360 "(not documented)"))))
363 (or do-all apropos-do-all
)
368 (defun apropos-value (apropos-regexp &optional do-all
)
369 "Show all symbols whose value's printed image matches APROPOS-REGEXP.
370 With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also looks
371 at the function and at the names and values of properties.
372 Returns list of symbols and values found."
373 (interactive "sApropos value (regexp): \nP")
374 (or do-all
(setq do-all apropos-do-all
))
375 (setq apropos-accumulator
())
379 (setq f nil v nil p nil
)
380 (or (memq symbol
'(apropos-regexp do-all apropos-accumulator
382 (setq v
(apropos-value-internal 'boundp symbol
'symbol-value
)))
384 (setq f
(apropos-value-internal 'fboundp symbol
'symbol-function
)
385 p
(apropos-format-plist symbol
"\n " t
)))
387 (setq apropos-accumulator
(cons (list symbol f v p
)
388 apropos-accumulator
))))))
389 (apropos-print nil
"\n----------------\n"))
393 (defun apropos-documentation (apropos-regexp &optional do-all
)
394 "Show symbols whose documentation contain matches for APROPOS-REGEXP.
395 With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also use
396 documentation that is not stored in the documentation file and show key
398 Returns list of symbols and documentation found."
399 (interactive "sApropos documentation (regexp): \nP")
400 (or do-all
(setq do-all apropos-do-all
))
401 (setq apropos-accumulator
() apropos-files-scanned
())
402 (let ((standard-input (get-buffer-create " apropos-temp"))
406 (set-buffer standard-input
)
407 (apropos-documentation-check-doc-file)
411 (setq f
(apropos-safe-documentation symbol
)
412 v
(get symbol
'variable-documentation
))
413 (if (integerp v
) (setq v
))
414 (setq f
(apropos-documentation-internal f
)
415 v
(apropos-documentation-internal v
))
417 (if (setq apropos-item
418 (cdr (assq symbol apropos-accumulator
)))
421 (setcar apropos-item f
))
423 (setcar (cdr apropos-item
) v
)))
424 (setq apropos-accumulator
425 (cons (list symbol f v
)
426 apropos-accumulator
)))))))
427 (apropos-print nil
"\n----------------\n"))
428 (kill-buffer standard-input
))))
431 (defun apropos-value-internal (predicate symbol function
)
432 (if (funcall predicate symbol
)
434 (setq symbol
(prin1-to-string (funcall function symbol
)))
435 (if (string-match apropos-regexp symbol
)
437 (if apropos-match-face
438 (put-text-property (match-beginning 0) (match-end 0)
439 'face apropos-match-face
443 (defun apropos-documentation-internal (doc)
445 (apropos-documentation-check-elc-file (car doc
))
447 (string-match apropos-regexp doc
)
449 (if apropos-match-face
450 (put-text-property (match-beginning 0)
452 'face apropos-match-face
453 (setq doc
(copy-sequence doc
))))
456 (defun apropos-format-plist (pl sep
&optional compare
)
457 (setq pl
(symbol-plist pl
))
460 (setq p
(format "%s %S" (car pl
) (nth 1 pl
)))
461 (if (or (not compare
) (string-match apropos-regexp p
))
462 (if apropos-property-face
463 (put-text-property 0 (length (symbol-name (car pl
)))
464 'face apropos-property-face p
))
468 (and compare apropos-match-face
469 (put-text-property (match-beginning 0) (match-end 0)
470 'face apropos-match-face
472 (setq p-out
(concat p-out
(if p-out sep
) p
))))
473 (setq pl
(nthcdr 2 pl
)))
477 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
479 (defun apropos-documentation-check-doc-file ()
480 (let (type symbol
(sepa 2) sepb beg end
)
483 (insert-file-contents (concat doc-directory internal-doc-file-name
))
485 (while (save-excursion
486 (setq sepb
(search-forward "\^_"))
488 (beginning-of-line 2)
489 (if (save-restriction
490 (narrow-to-region (point) (1- sepb
))
491 (re-search-forward apropos-regexp nil t
))
493 (setq beg
(match-beginning 0)
495 (goto-char (1+ sepa
))
496 (or (setq type
(if (eq ?F
(preceding-char))
497 1 ; function documentation
498 2) ; variable documentation
500 beg
(- beg
(point) 1)
501 end
(- end
(point) 1)
502 doc
(buffer-substring (1+ (point)) (1- sepb
))
503 apropos-item
(assq symbol apropos-accumulator
))
504 (setq apropos-item
(list symbol nil nil
)
505 apropos-accumulator
(cons apropos-item
506 apropos-accumulator
)))
507 (if apropos-match-face
508 (put-text-property beg end
'face apropos-match-face doc
))
509 (setcar (nthcdr type apropos-item
) doc
)))
510 (setq sepa
(goto-char sepb
)))))
512 (defun apropos-documentation-check-elc-file (file)
513 (if (member file apropos-files-scanned
)
515 (let (symbol doc beg end this-is-a-variable
)
516 (setq apropos-files-scanned
(cons file apropos-files-scanned
))
518 (insert-file-contents file
)
519 (while (search-forward "\n#@" nil t
)
520 ;; Read the comment length, and advance over it.
523 end
(+ (point) end -
1))
525 (if (save-restriction
526 ;; match ^ and $ relative to doc string
527 (narrow-to-region beg end
)
528 (re-search-forward apropos-regexp nil t
))
530 (goto-char (+ end
2))
531 (setq doc
(buffer-substring beg end
)
532 end
(- (match-end 0) beg
)
533 beg
(- (match-beginning 0) beg
)
534 this-is-a-variable
(looking-at "(def\\(var\\|const\\) ")
536 (skip-chars-forward "(a-z")
539 symbol
(if (consp symbol
)
542 (if (if this-is-a-variable
543 (get symbol
'variable-documentation
)
544 (and (fboundp symbol
) (apropos-safe-documentation symbol
)))
546 (or (setq apropos-item
(assq symbol apropos-accumulator
))
547 (setq apropos-item
(list symbol nil nil
)
548 apropos-accumulator
(cons apropos-item
549 apropos-accumulator
)))
550 (if apropos-match-face
551 (put-text-property beg end
'face apropos-match-face
553 (setcar (nthcdr (if this-is-a-variable
2 1)
559 (defun apropos-safe-documentation (function)
560 "Like `documentation', except it avoids calling `get_doc_string'.
561 Will return nil instead."
562 (while (and function
(symbolp function
))
563 (setq function
(if (fboundp function
)
564 (symbol-function function
))))
565 (if (eq (car-safe function
) 'macro
)
566 (setq function
(cdr function
)))
567 (setq function
(if (byte-code-function-p function
)
568 (if (> (length function
) 4)
570 (if (eq (car-safe function
) 'autoload
)
572 (if (eq (car-safe function
) 'lambda
)
573 (if (stringp (nth 2 function
))
575 (if (stringp (nth 3 function
))
576 (nth 3 function
)))))))
577 (if (integerp function
)
582 (defun apropos-print (do-keys spacing
)
583 "Output result of apropos searching into buffer `*Apropos*'.
584 The value of `apropos-accumulator' is the list of items to output.
585 Each element should have the format (SYMBOL FN-DOC VAR-DOC [PLIST-DOC]).
586 The return value is the list that was in `apropos-accumulator', sorted
587 alphabetically by symbol name; but this function also sets
588 `apropos-accumulator' to nil before returning.
590 If SPACING is non-nil, it should be a string;
591 separate items with that string."
592 (if (null apropos-accumulator
)
593 (message "No apropos matches for `%s'" apropos-regexp
)
594 (setq apropos-accumulator
595 (sort apropos-accumulator
(lambda (a b
)
596 (string-lessp (car a
) (car b
)))))
597 (with-output-to-temp-buffer "*Apropos*"
598 (let ((p apropos-accumulator
)
599 (old-buffer (current-buffer))
601 (set-buffer standard-output
)
603 (if (display-mouse-p)
604 (insert "If moving the mouse over text changes the text's color,\n"
605 (substitute-command-keys
606 "you can click \\[push-button] on that text to get more information.\n")))
607 (insert "In this buffer, go to the name of the command, or function,"
609 (substitute-command-keys
610 "and type \\[apropos-follow] to get full documentation.\n\n"))
612 (when (and spacing
(not (bobp)))
614 (setq apropos-item
(car p
)
615 symbol
(car apropos-item
)
617 (insert-text-button (symbol-name symbol
)
618 'type
'apropos-symbol
619 ;; Can't use default, since user may have
620 ;; changed the variable!
621 ;; Just say `no' to variables containing faces!
622 'face apropos-symbol-face
)
623 ;; Calculate key-bindings if we want them.
629 (set-buffer old-buffer
)
630 (where-is-internal symbol
)))
632 ;; Copy over the list of key sequences,
633 ;; omitting any that contain a buffer or a frame.
635 (let ((key (car keys
))
638 (while (< i
(length key
))
639 (if (or (framep (aref key i
))
640 (bufferp (aref key i
)))
644 (setq filtered
(cons key filtered
))))
645 (setq keys
(cdr keys
)))
646 (setq item filtered
))
647 ;; Convert the remaining keys to a string and insert.
651 (setq key
(condition-case ()
652 (key-description key
)
654 (if apropos-keybinding-face
655 (put-text-property 0 (length key
)
656 'face apropos-keybinding-face
661 (put-text-property (- (point) 3) (point)
662 'face apropos-keybinding-face
)
663 (insert " " (symbol-name symbol
) " ")
665 (put-text-property (- (point) 3) (point)
666 'face apropos-keybinding-face
)))
669 (if (commandp symbol
)
671 (if (apropos-macrop symbol
)
675 (apropos-print-doc 2 'apropos-variable t
)
676 (apropos-print-doc 6 'apropos-group t
)
677 (apropos-print-doc 5 'apropos-face t
)
678 (apropos-print-doc 4 'apropos-widget t
)
679 (apropos-print-doc 3 'apropos-plist nil
))
680 (setq buffer-read-only t
))))
681 (prog1 apropos-accumulator
682 (setq apropos-accumulator
()))) ; permit gc
685 (defun apropos-macrop (symbol)
686 "T if SYMBOL is a Lisp macro."
687 (and (fboundp symbol
)
689 (symbol-function symbol
)))
690 (or (eq (car symbol
) 'macro
)
691 (if (eq (car symbol
) 'autoload
)
696 (defun apropos-print-doc (i type do-keys
)
697 (if (stringp (setq i
(nth i apropos-item
)))
700 (insert-text-button (button-type-get type
'apropos-label
)
702 ;; Can't use the default button face, since
703 ;; user may have changed the variable!
704 ;; Just say `no' to variables containing faces!
705 'face apropos-label-face
706 'apropos-symbol
(car apropos-item
))
708 (insert (if do-keys
(substitute-command-keys i
) i
))
709 (or (bolp) (terpri)))))
712 (defun apropos-follow ()
713 "Invokes any button at point, otherwise invokes the nearest label button."
716 (or (apropos-next-label-button (line-beginning-position))
717 (error "There is nothing to follow here"))))
720 (defun apropos-describe-plist (symbol)
721 "Display a pretty listing of SYMBOL's plist."
722 (with-output-to-temp-buffer "*Help*"
723 (set-buffer standard-output
)
726 (princ "'s plist is\n (")
727 (if apropos-symbol-face
728 (put-text-property 8 (- (point) 14) 'face apropos-symbol-face
))
729 (insert (apropos-format-plist symbol
"\n "))
731 (print-help-return-message)))
736 ;;; apropos.el ends here