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.")
116 (defvar apropos-regexp nil
117 "Regexp used in current apropos run.")
119 (defvar apropos-files-scanned
()
120 "List of elc files already scanned in current run of `apropos-documentation'.")
122 (defvar apropos-accumulator
()
123 "Alist of symbols already found in current apropos run.")
125 (defvar apropos-item
()
126 "Current item in or for apropos-accumulator.")
128 (defun apropos-mode ()
129 "Major mode for following hyperlinks in output of apropos commands.
131 \\{apropos-mode-map}"
133 (kill-all-local-variables)
134 (use-local-map apropos-mode-map
)
135 (setq major-mode
'apropos-mode
136 mode-name
"Apropos"))
139 (defun apropos-variable (regexp &optional do-all
)
140 "Show user variables that match REGEXP.
141 With optional prefix ARG or if `apropos-do-all' is non-nil, also show
143 (interactive (list (read-string
145 (if (or current-prefix-arg apropos-do-all
)
150 (apropos-command regexp nil
151 (if (or do-all apropos-do-all
)
154 (get symbol
'variable-documentation
)))
157 ;; For auld lang syne:
159 (fset 'command-apropos
'apropos-command
)
161 (defun apropos-command (apropos-regexp &optional do-all var-predicate
)
162 "Show commands (interactively callable functions) that match REGEXP.
163 With optional prefix ARG, or if `apropos-do-all' is non-nil, also show
164 noninteractive functions.
166 If VAR-PREDICATE is non-nil, show only variables, and only those that
167 satisfy the predicate VAR-PREDICATE."
168 (interactive (list (read-string (concat
170 (if (or current-prefix-arg
176 (let ((standard-output (get-buffer-create "*Apropos*")))
177 (print-help-return-message 'identity
))))
178 (or do-all
(setq do-all apropos-do-all
))
179 (setq apropos-accumulator
180 (apropos-internal apropos-regexp
182 (if do-all
'functionp
'commandp
))))
183 (let ((tem apropos-accumulator
))
185 (if (get (car tem
) 'apropos-inhibit
)
186 (setq apropos-accumulator
(delq (car tem
) apropos-accumulator
)))
187 (setq tem
(cdr tem
))))
188 (let ((p apropos-accumulator
)
192 (setq symbol
(car p
))
193 (unless var-predicate
194 (if (functionp symbol
)
195 (if (setq doc
(documentation symbol t
))
196 (substring doc
0 (string-match "\n" doc
))
197 "(not documented)")))
199 (funcall var-predicate symbol
)
200 (if (setq doc
(documentation-property
201 symbol
'variable-documentation t
))
203 (string-match "\n" doc
))))))
205 (and (apropos-print t nil
)
211 (defun apropos (apropos-regexp &optional do-all
)
212 "Show all bound symbols whose names match REGEXP.
213 With optional prefix ARG or if `apropos-do-all' is non-nil, also show unbound
214 symbols and key bindings, which is a little more time-consuming.
215 Returns list of symbols and documentation found."
216 (interactive "sApropos symbol (regexp): \nP")
217 (setq apropos-accumulator
218 (apropos-internal apropos-regexp
225 (symbol-plist symbol
))))))
226 (let ((tem apropos-accumulator
))
228 (if (get (car tem
) 'apropos-inhibit
)
229 (setq apropos-accumulator
(delq (car tem
) apropos-accumulator
)))
230 (setq tem
(cdr tem
))))
231 (let ((p apropos-accumulator
)
232 symbol doc properties
)
235 (setq symbol
(car p
))
236 (when (fboundp symbol
)
237 (if (setq doc
(condition-case nil
238 (documentation symbol t
)
240 "(alias for undefined function)")))
241 (substring doc
0 (string-match "\n" doc
))
243 (when (boundp symbol
)
244 (if (setq doc
(documentation-property
245 symbol
'variable-documentation t
))
246 (substring doc
0 (string-match "\n" doc
))
248 (when (setq properties
(symbol-plist symbol
))
249 (setq doc
(list (car properties
)))
250 (while (setq properties
(cdr (cdr properties
)))
251 (setq doc
(cons (car properties
) doc
)))
252 (mapconcat #'symbol-name
(nreverse doc
) " "))
253 (when (get symbol
'widget-type
)
254 (if (setq doc
(documentation-property
255 symbol
'widget-documentation t
))
257 (string-match "\n" doc
))
260 (if (setq doc
(documentation-property
261 symbol
'face-documentation t
))
263 (string-match "\n" doc
))
265 (when (get symbol
'custom-group
)
266 (if (setq doc
(documentation-property
267 symbol
'group-documentation t
))
269 (string-match "\n" doc
))
270 "(not documented)"))))
273 (or do-all apropos-do-all
)
278 (defun apropos-value (apropos-regexp &optional do-all
)
279 "Show all symbols whose value's printed image matches REGEXP.
280 With optional prefix ARG or if `apropos-do-all' is non-nil, also looks
281 at the function and at the names and values of properties.
282 Returns list of symbols and values found."
283 (interactive "sApropos value (regexp): \nP")
284 (or do-all
(setq do-all apropos-do-all
))
285 (setq apropos-accumulator
())
289 (setq f nil v nil p nil
)
290 (or (memq symbol
'(apropos-regexp do-all apropos-accumulator
292 (setq v
(apropos-value-internal 'boundp symbol
'symbol-value
)))
294 (setq f
(apropos-value-internal 'fboundp symbol
'symbol-function
)
295 p
(apropos-format-plist symbol
"\n " t
)))
297 (setq apropos-accumulator
(cons (list symbol f v p
)
298 apropos-accumulator
))))))
299 (apropos-print nil t
))
303 (defun apropos-documentation (apropos-regexp &optional do-all
)
304 "Show symbols whose documentation contain matches for REGEXP.
305 With optional prefix ARG or if `apropos-do-all' is non-nil, also use
306 documentation that is not stored in the documentation file and show key
308 Returns list of symbols and documentation found."
309 (interactive "sApropos documentation (regexp): \nP")
310 (or do-all
(setq do-all apropos-do-all
))
311 (setq apropos-accumulator
() apropos-files-scanned
())
312 (let ((standard-input (get-buffer-create " apropos-temp"))
316 (set-buffer standard-input
)
317 (apropos-documentation-check-doc-file)
321 (setq f
(apropos-safe-documentation symbol
)
322 v
(get symbol
'variable-documentation
))
323 (if (integerp v
) (setq v
))
324 (setq f
(apropos-documentation-internal f
)
325 v
(apropos-documentation-internal v
))
327 (if (setq apropos-item
328 (cdr (assq symbol apropos-accumulator
)))
331 (setcar apropos-item f
))
333 (setcar (cdr apropos-item
) v
)))
334 (setq apropos-accumulator
335 (cons (list symbol f v
)
336 apropos-accumulator
)))))))
337 (apropos-print nil t
))
338 (kill-buffer standard-input
))))
341 (defun apropos-value-internal (predicate symbol function
)
342 (if (funcall predicate symbol
)
344 (setq symbol
(prin1-to-string (funcall function symbol
)))
345 (if (string-match apropos-regexp symbol
)
347 (if apropos-match-face
348 (put-text-property (match-beginning 0) (match-end 0)
349 'face apropos-match-face
353 (defun apropos-documentation-internal (doc)
355 (apropos-documentation-check-elc-file (car doc
))
357 (string-match apropos-regexp doc
)
359 (if apropos-match-face
360 (put-text-property (match-beginning 0)
362 'face apropos-match-face
363 (setq doc
(copy-sequence doc
))))
366 (defun apropos-format-plist (pl sep
&optional compare
)
367 (setq pl
(symbol-plist pl
))
370 (setq p
(format "%s %S" (car pl
) (nth 1 pl
)))
371 (if (or (not compare
) (string-match apropos-regexp p
))
372 (if apropos-property-face
373 (put-text-property 0 (length (symbol-name (car pl
)))
374 'face apropos-property-face p
))
378 (and compare apropos-match-face
379 (put-text-property (match-beginning 0) (match-end 0)
380 'face apropos-match-face
382 (setq p-out
(concat p-out
(if p-out sep
) p
))))
383 (setq pl
(nthcdr 2 pl
)))
387 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
389 (defun apropos-documentation-check-doc-file ()
390 (let (type symbol
(sepa 2) sepb beg end
)
393 (insert-file-contents (concat doc-directory internal-doc-file-name
))
395 (while (save-excursion
396 (setq sepb
(search-forward "\^_"))
398 (beginning-of-line 2)
399 (if (save-restriction
400 (narrow-to-region (point) (1- sepb
))
401 (re-search-forward apropos-regexp nil t
))
403 (setq beg
(match-beginning 0)
405 (goto-char (1+ sepa
))
406 (or (setq type
(if (eq ?F
(preceding-char))
407 1 ; function documentation
408 2) ; variable documentation
410 beg
(- beg
(point) 1)
411 end
(- end
(point) 1)
412 doc
(buffer-substring (1+ (point)) (1- sepb
))
413 apropos-item
(assq symbol apropos-accumulator
))
414 (setq apropos-item
(list symbol nil nil
)
415 apropos-accumulator
(cons apropos-item
416 apropos-accumulator
)))
417 (if apropos-match-face
418 (put-text-property beg end
'face apropos-match-face doc
))
419 (setcar (nthcdr type apropos-item
) doc
)))
420 (setq sepa
(goto-char sepb
)))))
422 (defun apropos-documentation-check-elc-file (file)
423 (if (member file apropos-files-scanned
)
425 (let (symbol doc beg end this-is-a-variable
)
426 (setq apropos-files-scanned
(cons file apropos-files-scanned
))
428 (insert-file-contents file
)
429 (while (search-forward "\n#@" nil t
)
430 ;; Read the comment length, and advance over it.
433 end
(+ (point) end -
1))
435 (if (save-restriction
436 ;; match ^ and $ relative to doc string
437 (narrow-to-region beg end
)
438 (re-search-forward apropos-regexp nil t
))
440 (goto-char (+ end
2))
441 (setq doc
(buffer-substring beg end
)
442 end
(- (match-end 0) beg
)
443 beg
(- (match-beginning 0) beg
)
444 this-is-a-variable
(looking-at "(def\\(var\\|const\\) ")
446 (skip-chars-forward "(a-z")
449 symbol
(if (consp symbol
)
452 (if (if this-is-a-variable
453 (get symbol
'variable-documentation
)
454 (and (fboundp symbol
) (apropos-safe-documentation symbol
)))
456 (or (setq apropos-item
(assq symbol apropos-accumulator
))
457 (setq apropos-item
(list symbol nil nil
)
458 apropos-accumulator
(cons apropos-item
459 apropos-accumulator
)))
460 (if apropos-match-face
461 (put-text-property beg end
'face apropos-match-face
463 (setcar (nthcdr (if this-is-a-variable
2 1)
469 (defun apropos-safe-documentation (function)
470 "Like documentation, except it avoids calling `get_doc_string'.
471 Will return nil instead."
472 (while (and function
(symbolp function
))
473 (setq function
(if (fboundp function
)
474 (symbol-function function
))))
475 (if (eq (car-safe function
) 'macro
)
476 (setq function
(cdr function
)))
477 (setq function
(if (byte-code-function-p function
)
478 (if (> (length function
) 4)
480 (if (eq (car-safe function
) 'autoload
)
482 (if (eq (car-safe function
) 'lambda
)
483 (if (stringp (nth 2 function
))
485 (if (stringp (nth 3 function
))
486 (nth 3 function
)))))))
487 (if (integerp function
)
493 (defvar apropos-label-properties nil
494 "List of face properties to use for a label.
495 Bound by `apropos-print' for use by `apropos-print-doc'.")
497 (defun apropos-print (do-keys spacing
)
498 "Output result of apropos searching into buffer `*Apropos*'.
499 The value of `apropos-accumulator' is the list of items to output.
500 Each element should have the format (SYMBOL FN-DOC VAR-DOC [PLIST-DOC]).
501 The return value is the list that was in `apropos-accumulator', sorted
502 alphabetically by symbol name; but this function also sets
503 `apropos-accumulator' to nil before returning."
504 (if (null apropos-accumulator
)
505 (message "No apropos matches for `%s'" apropos-regexp
)
506 (setq apropos-accumulator
507 (sort apropos-accumulator
(lambda (a b
)
508 (string-lessp (car a
) (car b
)))))
509 (setq apropos-label-properties
510 (if (and apropos-label-face
511 (symbolp apropos-label-face
))
512 `(face ,apropos-label-face
513 mouse-face highlight
)))
514 (with-output-to-temp-buffer "*Apropos*"
515 (let ((p apropos-accumulator
)
516 (old-buffer (current-buffer))
517 symbol item point1 point2
)
518 (set-buffer standard-output
)
521 (insert "If you move the mouse over text that changes color,\n"
522 (substitute-command-keys
523 "you can click \\[apropos-mouse-follow] to get more information.\n")))
524 (insert (substitute-command-keys
525 "In this buffer, type \\[apropos-follow] to get full documentation.\n\n"))
527 (or (not spacing
) (bobp) (terpri))
528 (setq apropos-item
(car p
)
529 symbol
(car apropos-item
)
532 (princ symbol
) ; print symbol name
533 (setq point2
(point))
534 ;; Calculate key-bindings if we want them.
540 (set-buffer old-buffer
)
541 (where-is-internal symbol
)))
543 ;; Copy over the list of key sequences,
544 ;; omitting any that contain a buffer or a frame.
546 (let ((key (car keys
))
549 (while (< i
(length key
))
550 (if (or (framep (aref key i
))
551 (bufferp (aref key i
)))
555 (setq filtered
(cons key filtered
))))
556 (setq keys
(cdr keys
)))
557 (setq item filtered
))
558 ;; Convert the remaining keys to a string and insert.
562 (setq key
(condition-case ()
563 (key-description key
)
565 (if apropos-keybinding-face
566 (put-text-property 0 (length key
)
567 'face apropos-keybinding-face
572 (put-text-property (- (point) 3) (point)
573 'face apropos-keybinding-face
)
574 (insert " " (symbol-name symbol
) " ")
576 (put-text-property (- (point) 3) (point)
577 'face apropos-keybinding-face
)))
579 ;; only now so we don't propagate text attributes all over
580 (put-text-property point1 point2
'item
581 (if (eval `(or ,@(cdr apropos-item
)))
584 (if apropos-symbol-face
585 (put-text-property point1 point2
'face apropos-symbol-face
))
586 (apropos-print-doc 'describe-function
1
587 (if (commandp symbol
)
589 (if (apropos-macrop symbol
)
593 ;; We used to use customize-variable-other-window instead
594 ;; for a customizable variable, but that is slow.
595 ;; It is better to show an ordinary help buffer
596 ;; and let the user click on the customization button
597 ;; in that buffer, if he wants to.
598 (apropos-print-doc 'describe-variable
2 "Variable" t
)
599 (apropos-print-doc 'customize-group-other-window
6 "Group" t
)
600 (apropos-print-doc 'customize-face-other-window
5 "Face" t
)
601 (apropos-print-doc 'widget-browse-other-window
4 "Widget" t
)
602 (apropos-print-doc 'apropos-describe-plist
3
604 (setq buffer-read-only t
))))
605 (prog1 apropos-accumulator
606 (setq apropos-accumulator
()))) ; permit gc
609 (defun apropos-macrop (symbol)
610 "T if SYMBOL is a Lisp macro."
611 (and (fboundp symbol
)
613 (symbol-function symbol
)))
614 (or (eq (car symbol
) 'macro
)
615 (if (eq (car symbol
) 'autoload
)
620 (defun apropos-print-doc (action i str do-keys
)
621 (if (stringp (setq i
(nth i apropos-item
)))
624 (put-text-property (- (point) 2) (1- (point))
627 (if apropos-label-properties
628 (add-text-properties (- (point) (length str
) 2)
630 apropos-label-properties
))
631 (insert (if do-keys
(substitute-command-keys i
) i
))
632 (or (bolp) (terpri)))))
635 (defun apropos-mouse-follow (event)
637 (let ((other (if (eq (current-buffer) (get-buffer "*Apropos*"))
641 (set-buffer (window-buffer (posn-window (event-start event
))))
642 (goto-char (posn-point (event-start event
)))
643 (or (and (not (eobp)) (get-text-property (point) 'mouse-face
))
644 (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face
))
645 (error "There is nothing to follow here"))
646 (apropos-follow other
))))
649 (defun apropos-follow (&optional other
)
651 (let* (;; Properties are always found at the beginning of the line.
652 (bol (save-excursion (beginning-of-line) (point)))
653 ;; If there is no `item' property here, look behind us.
654 (item (get-text-property bol
'item
))
655 (item-at (if item nil
(previous-single-property-change bol
'item
)))
656 ;; Likewise, if there is no `action' property here, look in front.
657 (action (get-text-property bol
'action
))
658 (action-at (if action nil
(next-single-property-change bol
'action
))))
659 (and (null item
) item-at
660 (setq item
(get-text-property (1- item-at
) 'item
)))
661 (and (null action
) action-at
662 (setq action
(get-text-property action-at
'action
)))
663 (if (not (and item action
))
664 (error "There is nothing to follow here"))
665 (if (consp item
) (error "There is nothing to follow in `%s'" (car item
)))
666 (if other
(set-buffer other
))
667 (funcall action item
)))
671 (defun apropos-describe-plist (symbol)
672 "Display a pretty listing of SYMBOL's plist."
673 (with-output-to-temp-buffer "*Help*"
674 (set-buffer standard-output
)
677 (princ "'s plist is\n (")
678 (if apropos-symbol-face
679 (put-text-property 8 (- (point) 14) 'face apropos-symbol-face
))
680 (insert (apropos-format-plist symbol
"\n "))
682 (print-help-return-message)))
686 ;;; apropos.el ends here