1 ;;; apropos.el --- apropos commands for users and programmers.
3 ;; Copyright (C) 1989, 1994, 1995 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.
61 "Apropos commands for users and programmers"
65 ;; I see a degradation of maybe 10-20% only.
66 (defcustom apropos-do-all nil
67 "*Whether the apropos commands should do more.
69 Slows them down more or less. Set this non-nil if you have a fast machine."
74 (defcustom apropos-symbol-face
'bold
75 "*Face for symbol name in Apropos output, or nil for none."
79 (defcustom apropos-keybinding-face
'underline
80 "*Face for lists of keybinding in Apropos output, or nil for none."
84 (defcustom apropos-label-face
'italic
85 "*Face for label (`Command', `Variable' ...) in Apropos output.
86 A value of nil means don't use any special font for them, and also
87 turns off mouse highlighting."
91 (defcustom apropos-property-face
'bold-italic
92 "*Face for property name in apropos output, or nil for none."
96 (defcustom apropos-match-face
'secondary-selection
97 "*Face for matching text in Apropos documentation/value, or nil for none.
98 This applies when you look for matches in the documentation or variable value
99 for the regexp; the part that matches gets displayed in this font."
104 (defvar apropos-mode-map
105 (let ((map (make-sparse-keymap)))
106 (define-key map
"\C-m" 'apropos-follow
)
107 (define-key map
" " 'scroll-up
)
108 (define-key map
"\177" 'scroll-down
)
109 (define-key map
"q" 'quit-window
)
110 (define-key map
[mouse-2
] 'apropos-mouse-follow
)
111 (define-key map
[down-mouse-2
] nil
)
113 "Keymap used in Apropos mode.")
115 (defvar apropos-mode-hook nil
116 "*Hook run when mode is turned on.")
118 (defvar apropos-regexp nil
119 "Regexp used in current apropos run.")
121 (defvar apropos-files-scanned
()
122 "List of elc files already scanned in current run of `apropos-documentation'.")
124 (defvar apropos-accumulator
()
125 "Alist of symbols already found in current apropos run.")
127 (defvar apropos-item
()
128 "Current item in or for `apropos-accumulator'.")
131 (defun apropos-mode ()
132 "Major mode for following hyperlinks in output of apropos commands.
134 \\{apropos-mode-map}"
136 (kill-all-local-variables)
137 (use-local-map apropos-mode-map
)
138 (setq major-mode
'apropos-mode
140 (run-hooks 'apropos-mode-hook
))
143 (defun apropos-variable (regexp &optional do-all
)
144 "Show user variables that match REGEXP.
145 With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also show
147 (interactive (list (read-string
149 (if (or current-prefix-arg apropos-do-all
)
154 (apropos-command regexp nil
155 (if (or do-all apropos-do-all
)
158 (get symbol
'variable-documentation
)))
161 ;; For auld lang syne:
163 (fset 'command-apropos
'apropos-command
)
165 (defun apropos-command (apropos-regexp &optional do-all var-predicate
)
166 "Show commands (interactively callable functions) that match APROPOS-REGEXP.
167 With optional prefix DO-ALL, or if `apropos-do-all' is non-nil, also show
168 noninteractive functions.
170 If VAR-PREDICATE is non-nil, show only variables, and only those that
171 satisfy the predicate VAR-PREDICATE."
172 (interactive (list (read-string (concat
174 (if (or current-prefix-arg
180 (let ((standard-output (get-buffer-create "*Apropos*")))
181 (print-help-return-message 'identity
))))
182 (or do-all
(setq do-all apropos-do-all
))
183 (setq apropos-accumulator
184 (apropos-internal apropos-regexp
186 (if do-all
'functionp
'commandp
))))
187 (let ((tem apropos-accumulator
))
189 (if (get (car tem
) 'apropos-inhibit
)
190 (setq apropos-accumulator
(delq (car tem
) apropos-accumulator
)))
191 (setq tem
(cdr tem
))))
192 (let ((p apropos-accumulator
)
196 (setq symbol
(car p
))
197 (unless var-predicate
198 (if (functionp symbol
)
199 (if (setq doc
(documentation symbol t
))
200 (substring doc
0 (string-match "\n" doc
))
201 "(not documented)")))
203 (funcall var-predicate symbol
)
204 (if (setq doc
(documentation-property
205 symbol
'variable-documentation t
))
207 (string-match "\n" doc
))))))
209 (and (apropos-print t nil
)
215 (defun apropos (apropos-regexp &optional do-all
)
216 "Show all bound symbols whose names match APROPOS-REGEXP.
217 With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also
218 show unbound symbols and key bindings, which is a little more
219 time-consuming. Returns list of symbols and documentation found."
220 (interactive "sApropos symbol (regexp): \nP")
221 (setq apropos-accumulator
222 (apropos-internal apropos-regexp
229 (symbol-plist symbol
))))))
230 (let ((tem apropos-accumulator
))
232 (if (get (car tem
) 'apropos-inhibit
)
233 (setq apropos-accumulator
(delq (car tem
) apropos-accumulator
)))
234 (setq tem
(cdr tem
))))
235 (let ((p apropos-accumulator
)
236 symbol doc properties
)
239 (setq symbol
(car p
))
240 (when (fboundp symbol
)
241 (if (setq doc
(condition-case nil
242 (documentation symbol t
)
244 "(alias for undefined function)")))
245 (substring doc
0 (string-match "\n" doc
))
247 (when (boundp symbol
)
248 (if (setq doc
(documentation-property
249 symbol
'variable-documentation t
))
250 (substring doc
0 (string-match "\n" doc
))
252 (when (setq properties
(symbol-plist symbol
))
253 (setq doc
(list (car properties
)))
254 (while (setq properties
(cdr (cdr properties
)))
255 (setq doc
(cons (car properties
) doc
)))
256 (mapconcat #'symbol-name
(nreverse doc
) " "))
257 (when (get symbol
'widget-type
)
258 (if (setq doc
(documentation-property
259 symbol
'widget-documentation t
))
261 (string-match "\n" doc
))
264 (if (setq doc
(documentation-property
265 symbol
'face-documentation t
))
267 (string-match "\n" doc
))
269 (when (get symbol
'custom-group
)
270 (if (setq doc
(documentation-property
271 symbol
'group-documentation t
))
273 (string-match "\n" doc
))
274 "(not documented)"))))
277 (or do-all apropos-do-all
)
282 (defun apropos-value (apropos-regexp &optional do-all
)
283 "Show all symbols whose value's printed image matches APROPOS-REGEXP.
284 With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also looks
285 at the function and at the names and values of properties.
286 Returns list of symbols and values found."
287 (interactive "sApropos value (regexp): \nP")
288 (or do-all
(setq do-all apropos-do-all
))
289 (setq apropos-accumulator
())
293 (setq f nil v nil p nil
)
294 (or (memq symbol
'(apropos-regexp do-all apropos-accumulator
296 (setq v
(apropos-value-internal 'boundp symbol
'symbol-value
)))
298 (setq f
(apropos-value-internal 'fboundp symbol
'symbol-function
)
299 p
(apropos-format-plist symbol
"\n " t
)))
301 (setq apropos-accumulator
(cons (list symbol f v p
)
302 apropos-accumulator
))))))
303 (apropos-print nil t
))
307 (defun apropos-documentation (apropos-regexp &optional do-all
)
308 "Show symbols whose documentation contain matches for APROPOS-REGEXP.
309 With optional prefix DO-ALL or if `apropos-do-all' is non-nil, also use
310 documentation that is not stored in the documentation file and show key
312 Returns list of symbols and documentation found."
313 (interactive "sApropos documentation (regexp): \nP")
314 (or do-all
(setq do-all apropos-do-all
))
315 (setq apropos-accumulator
() apropos-files-scanned
())
316 (let ((standard-input (get-buffer-create " apropos-temp"))
320 (set-buffer standard-input
)
321 (apropos-documentation-check-doc-file)
325 (setq f
(apropos-safe-documentation symbol
)
326 v
(get symbol
'variable-documentation
))
327 (if (integerp v
) (setq v
))
328 (setq f
(apropos-documentation-internal f
)
329 v
(apropos-documentation-internal v
))
331 (if (setq apropos-item
332 (cdr (assq symbol apropos-accumulator
)))
335 (setcar apropos-item f
))
337 (setcar (cdr apropos-item
) v
)))
338 (setq apropos-accumulator
339 (cons (list symbol f v
)
340 apropos-accumulator
)))))))
341 (apropos-print nil t
))
342 (kill-buffer standard-input
))))
345 (defun apropos-value-internal (predicate symbol function
)
346 (if (funcall predicate symbol
)
348 (setq symbol
(prin1-to-string (funcall function symbol
)))
349 (if (string-match apropos-regexp symbol
)
351 (if apropos-match-face
352 (put-text-property (match-beginning 0) (match-end 0)
353 'face apropos-match-face
357 (defun apropos-documentation-internal (doc)
359 (apropos-documentation-check-elc-file (car doc
))
361 (string-match apropos-regexp doc
)
363 (if apropos-match-face
364 (put-text-property (match-beginning 0)
366 'face apropos-match-face
367 (setq doc
(copy-sequence doc
))))
370 (defun apropos-format-plist (pl sep
&optional compare
)
371 (setq pl
(symbol-plist pl
))
374 (setq p
(format "%s %S" (car pl
) (nth 1 pl
)))
375 (if (or (not compare
) (string-match apropos-regexp p
))
376 (if apropos-property-face
377 (put-text-property 0 (length (symbol-name (car pl
)))
378 'face apropos-property-face p
))
382 (and compare apropos-match-face
383 (put-text-property (match-beginning 0) (match-end 0)
384 'face apropos-match-face
386 (setq p-out
(concat p-out
(if p-out sep
) p
))))
387 (setq pl
(nthcdr 2 pl
)))
391 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
393 (defun apropos-documentation-check-doc-file ()
394 (let (type symbol
(sepa 2) sepb beg end
)
397 (insert-file-contents (concat doc-directory internal-doc-file-name
))
399 (while (save-excursion
400 (setq sepb
(search-forward "\^_"))
402 (beginning-of-line 2)
403 (if (save-restriction
404 (narrow-to-region (point) (1- sepb
))
405 (re-search-forward apropos-regexp nil t
))
407 (setq beg
(match-beginning 0)
409 (goto-char (1+ sepa
))
410 (or (setq type
(if (eq ?F
(preceding-char))
411 1 ; function documentation
412 2) ; variable documentation
414 beg
(- beg
(point) 1)
415 end
(- end
(point) 1)
416 doc
(buffer-substring (1+ (point)) (1- sepb
))
417 apropos-item
(assq symbol apropos-accumulator
))
418 (setq apropos-item
(list symbol nil nil
)
419 apropos-accumulator
(cons apropos-item
420 apropos-accumulator
)))
421 (if apropos-match-face
422 (put-text-property beg end
'face apropos-match-face doc
))
423 (setcar (nthcdr type apropos-item
) doc
)))
424 (setq sepa
(goto-char sepb
)))))
426 (defun apropos-documentation-check-elc-file (file)
427 (if (member file apropos-files-scanned
)
429 (let (symbol doc beg end this-is-a-variable
)
430 (setq apropos-files-scanned
(cons file apropos-files-scanned
))
432 (insert-file-contents file
)
433 (while (search-forward "\n#@" nil t
)
434 ;; Read the comment length, and advance over it.
437 end
(+ (point) end -
1))
439 (if (save-restriction
440 ;; match ^ and $ relative to doc string
441 (narrow-to-region beg end
)
442 (re-search-forward apropos-regexp nil t
))
444 (goto-char (+ end
2))
445 (setq doc
(buffer-substring beg end
)
446 end
(- (match-end 0) beg
)
447 beg
(- (match-beginning 0) beg
)
448 this-is-a-variable
(looking-at "(def\\(var\\|const\\) ")
450 (skip-chars-forward "(a-z")
453 symbol
(if (consp symbol
)
456 (if (if this-is-a-variable
457 (get symbol
'variable-documentation
)
458 (and (fboundp symbol
) (apropos-safe-documentation symbol
)))
460 (or (setq apropos-item
(assq symbol apropos-accumulator
))
461 (setq apropos-item
(list symbol nil nil
)
462 apropos-accumulator
(cons apropos-item
463 apropos-accumulator
)))
464 (if apropos-match-face
465 (put-text-property beg end
'face apropos-match-face
467 (setcar (nthcdr (if this-is-a-variable
2 1)
473 (defun apropos-safe-documentation (function)
474 "Like `documentation', except it avoids calling `get_doc_string'.
475 Will return nil instead."
476 (while (and function
(symbolp function
))
477 (setq function
(if (fboundp function
)
478 (symbol-function function
))))
479 (if (eq (car-safe function
) 'macro
)
480 (setq function
(cdr function
)))
481 (setq function
(if (byte-code-function-p function
)
482 (if (> (length function
) 4)
484 (if (eq (car-safe function
) 'autoload
)
486 (if (eq (car-safe function
) 'lambda
)
487 (if (stringp (nth 2 function
))
489 (if (stringp (nth 3 function
))
490 (nth 3 function
)))))))
491 (if (integerp function
)
497 (defvar apropos-label-properties nil
498 "List of face properties to use for a label.
499 Bound by `apropos-print' for use by `apropos-print-doc'.")
501 (defun apropos-print (do-keys spacing
)
502 "Output result of apropos searching into buffer `*Apropos*'.
503 The value of `apropos-accumulator' is the list of items to output.
504 Each element should have the format (SYMBOL FN-DOC VAR-DOC [PLIST-DOC]).
505 The return value is the list that was in `apropos-accumulator', sorted
506 alphabetically by symbol name; but this function also sets
507 `apropos-accumulator' to nil before returning."
508 (if (null apropos-accumulator
)
509 (message "No apropos matches for `%s'" apropos-regexp
)
510 (setq apropos-accumulator
511 (sort apropos-accumulator
(lambda (a b
)
512 (string-lessp (car a
) (car b
)))))
513 (setq apropos-label-properties
514 (if (and apropos-label-face
515 (symbolp apropos-label-face
))
516 `(face ,apropos-label-face
517 mouse-face highlight
)))
518 (with-output-to-temp-buffer "*Apropos*"
519 (let ((p apropos-accumulator
)
520 (old-buffer (current-buffer))
521 symbol item point1 point2
)
522 (set-buffer standard-output
)
524 (if (display-mouse-p)
525 (insert "If you move the mouse over text that changes color,\n"
526 (substitute-command-keys
527 "you can click \\[apropos-mouse-follow] to get more information.\n")))
528 (insert (substitute-command-keys
529 "In this buffer, type \\[apropos-follow] to get full documentation.\n\n"))
531 (or (not spacing
) (bobp) (terpri))
532 (setq apropos-item
(car p
)
533 symbol
(car apropos-item
)
536 (princ symbol
) ; print symbol name
537 (setq point2
(point))
538 ;; Calculate key-bindings if we want them.
544 (set-buffer old-buffer
)
545 (where-is-internal symbol
)))
547 ;; Copy over the list of key sequences,
548 ;; omitting any that contain a buffer or a frame.
550 (let ((key (car keys
))
553 (while (< i
(length key
))
554 (if (or (framep (aref key i
))
555 (bufferp (aref key i
)))
559 (setq filtered
(cons key filtered
))))
560 (setq keys
(cdr keys
)))
561 (setq item filtered
))
562 ;; Convert the remaining keys to a string and insert.
566 (setq key
(condition-case ()
567 (key-description key
)
569 (if apropos-keybinding-face
570 (put-text-property 0 (length key
)
571 'face apropos-keybinding-face
576 (put-text-property (- (point) 3) (point)
577 'face apropos-keybinding-face
)
578 (insert " " (symbol-name symbol
) " ")
580 (put-text-property (- (point) 3) (point)
581 'face apropos-keybinding-face
)))
583 ;; only now so we don't propagate text attributes all over
584 (put-text-property point1 point2
'item
585 (if (eval `(or ,@(cdr apropos-item
)))
588 (if apropos-symbol-face
589 (put-text-property point1 point2
'face apropos-symbol-face
))
590 (apropos-print-doc 'describe-function
1
591 (if (commandp symbol
)
593 (if (apropos-macrop symbol
)
597 ;; We used to use `customize-variable-other-window' instead
598 ;; for a customizable variable, but that is slow.
599 ;; It is better to show an ordinary help buffer
600 ;; and let the user click on the customization button
601 ;; in that buffer, if he wants to.
602 ;; Likewise for `customize-face-other-window'.
603 (apropos-print-doc 'describe-variable
2 "Variable" t
)
604 (apropos-print-doc 'customize-group-other-window
6 "Group" t
)
605 (apropos-print-doc 'describe-face
5 "Face" t
)
606 (apropos-print-doc 'widget-browse-other-window
4 "Widget" t
)
607 (apropos-print-doc 'apropos-describe-plist
3
609 (setq buffer-read-only t
))))
610 (prog1 apropos-accumulator
611 (setq apropos-accumulator
()))) ; permit gc
614 (defun apropos-macrop (symbol)
615 "T if SYMBOL is a Lisp macro."
616 (and (fboundp symbol
)
618 (symbol-function symbol
)))
619 (or (eq (car symbol
) 'macro
)
620 (if (eq (car symbol
) 'autoload
)
625 (defun apropos-print-doc (action i str do-keys
)
626 (if (stringp (setq i
(nth i apropos-item
)))
629 (put-text-property (- (point) 2) (1- (point))
632 (if apropos-label-properties
633 (add-text-properties (- (point) (length str
) 2)
635 apropos-label-properties
))
636 (insert (if do-keys
(substitute-command-keys i
) i
))
637 (or (bolp) (terpri)))))
640 (defun apropos-mouse-follow (event)
642 (let ((other (if (eq (current-buffer) (get-buffer "*Apropos*"))
646 (set-buffer (window-buffer (posn-window (event-start event
))))
647 (goto-char (posn-point (event-start event
)))
648 (or (and (not (eobp)) (get-text-property (point) 'mouse-face
))
649 (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face
))
650 (error "There is nothing to follow here"))
651 (apropos-follow other
))))
654 (defun apropos-follow (&optional other
)
656 (let* (;; Properties are always found at the beginning of the line.
657 (bol (save-excursion (beginning-of-line) (point)))
658 ;; If there is no `item' property here, look behind us.
659 (item (get-text-property bol
'item
))
660 (item-at (if item nil
(previous-single-property-change bol
'item
)))
661 ;; Likewise, if there is no `action' property here, look in front.
662 (action (get-text-property bol
'action
))
663 (action-at (if action nil
(next-single-property-change bol
'action
))))
664 (and (null item
) item-at
665 (setq item
(get-text-property (1- item-at
) 'item
)))
666 (and (null action
) action-at
667 (setq action
(get-text-property action-at
'action
)))
668 (if (not (and item action
))
669 (error "There is nothing to follow here"))
670 (if (consp item
) (error "There is nothing to follow in `%s'" (car item
)))
671 (if other
(set-buffer other
))
672 (funcall action item
)))
676 (defun apropos-describe-plist (symbol)
677 "Display a pretty listing of SYMBOL's plist."
678 (with-output-to-temp-buffer "*Help*"
679 (set-buffer standard-output
)
682 (princ "'s plist is\n (")
683 (if apropos-symbol-face
684 (put-text-property 8 (- (point) 14) 'face apropos-symbol-face
))
685 (insert (apropos-format-plist symbol
"\n "))
687 (print-help-return-message)))
691 ;;; apropos.el ends here