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@Informatik.START.dbp.de, fax (+49 69) 7588-2389
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
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;; The ideas for this package were derived from the C code in
28 ;; src/keymap.c and elsewhere. The functions in this file should
29 ;; always be byte-compiled for speed. Someone should rewrite this in
30 ;; C (as part of src/keymap.c) for speed.
32 ;; The idea for super-apropos is based on the original implementation
33 ;; by Lynn Slater <lrs@esl.com>.
36 ;; Fixed bug, current-local-map can return nil.
37 ;; Change, doesn't calculate key-bindings unless needed.
38 ;; Added super-apropos capability, changed print functions.
39 ;;; Made fast-apropos and super-apropos share code.
40 ;;; Sped up fast-apropos again.
41 ;; Added apropos-do-all option.
42 ;;; Added fast-command-apropos.
43 ;; Changed doc strings to comments for helping functions.
44 ;;; Made doc file buffer read-only, buried it.
45 ;; Only call substitute-command-keys if do-all set.
47 ;; Optionally use configurable faces to make the output more legible.
48 ;; Differentiate between command, function and macro.
49 ;; Apropos-command (ex command-apropos) does cmd and optionally user var.
50 ;; Apropos shows all 3 aspects of symbols (fn, var and plist)
51 ;; Apropos-documentation (ex super-apropos) now finds all it should.
52 ;; New apropos-value snoops through all values and optionally plists.
53 ;; Reading DOC file doesn't load nroff.
54 ;; Added hypertext following of documentation, mouse-2 on variable gives value
55 ;; from buffer in active window.
59 ;; I see a degradation of maybe 10-20% only.
60 (defvar apropos-do-all nil
61 "*Whether the apropos commands should do more.
62 Slows them down more or less. Set this non-nil if you have a fast machine.")
65 (defvar apropos-symbol-face (if window-system 'bold)
66 "*Face for symbol name in apropos output or `nil'.
67 This looks good, but slows down the commands several times.")
69 (defvar apropos-keybinding-face (if window-system 'underline)
70 "*Face for keybinding display in apropos output or `nil'.
71 This looks good, but slows down the commands several times.")
73 (defvar apropos-label-face (if window-system 'italic)
74 "*Face for label (Command, Variable ...) in apropos output or `nil'.
75 If this is `nil' no mouse highlighting occurs.
76 This looks good, but slows down the commands several times.
77 When this is a face name, as it is initially, it gets transformed to a
78 text-property list for efficiency.")
80 (defvar apropos-property-face (if window-system 'bold-italic)
81 "*Face for property name in apropos output or `nil'.
82 This looks good, but slows down the commands several times.")
84 (defvar apropos-match-face (if window-system 'highlight)
85 "*Face for matching part in apropos-documentation/value output or `nil'.
86 This looks good, but slows down the commands several times.")
89 (defvar apropos-local-map
90 (let ((map (make-sparse-keymap)))
91 (define-key map "\C-m" 'apropos-follow)
92 (define-key map [mouse-2] 'apropos-mouse-follow)
93 (define-key map [down-mouse-2] nil)
95 "Local map active when displaying apropos output.")
98 (defvar apropos-regexp nil
99 "Regexp used in current apropos run.")
101 (defvar apropos-files-scanned ()
102 "List of elc files already scanned in current run of `apropos-documentaion'.")
104 (defvar apropos-accumulator ()
105 "Alist of symbols already found in current apropos run.")
107 (defvar apropos-item ()
108 "Current item in or for apropos-accumulator.")
110 ;; For auld lang syne:
112 (fset 'command-apropos 'apropos-command)
114 (defun apropos-command (apropos-regexp &optional do-all)
115 "Shows commands (interactively callable functions) that match REGEXP.
116 With optional prefix ARG or if `apropos-do-all' is non-nil, also show
118 (interactive (list (read-string (concat "Apropos command "
119 (if (or current-prefix-arg
125 (let ((standard-output (get-buffer-create "*Help*")))
126 (print-help-return-message 'identity))))
127 (or do-all (setq do-all apropos-do-all))
128 (setq apropos-accumulator
129 (apropos-internal apropos-regexp
131 (lambda (symbol) (or (commandp symbol)
132 (user-variable-p symbol)))
140 (setq symbol (car p))
141 (if (commandp symbol)
142 (if (setq doc (documentation symbol t))
143 (substring doc 0 (string-match "\n" doc))
146 (user-variable-p symbol)
147 (if (setq doc (documentation-property
148 symbol 'variable-documentation t))
150 (string-match "\n" doc))))))
153 (and message (message message)))))
157 (defun apropos (apropos-regexp &optional do-all)
158 "Show all bound symbols whose names match REGEXP.
159 With optional prefix ARG or if `apropos-do-all' is non-nil, also show unbound
160 symbols and key bindings, which is a little more time-consuming.
161 Returns list of symbols and documentation found."
162 (interactive "sApropos symbol (regexp): \nP")
163 (setq apropos-accumulator
164 (apropos-internal apropos-regexp
170 (symbol-plist symbol))))))
172 (or do-all apropos-do-all)
177 (setq symbol (car p))
179 (if (setq doc (documentation symbol t))
180 (substring doc 0 (string-match "\n" doc))
183 (if (setq doc (documentation-property
184 symbol 'variable-documentation t))
186 (string-match "\n" doc))
188 (if (setq doc (symbol-plist symbol))
189 (if (eq (setq doc (/ (length doc) 2)) 1)
191 (concat doc " properties")))))
197 (defun apropos-value (apropos-regexp &optional do-all)
198 "Show all symbols whose value's printed image matches REGEXP.
199 With optional prefix ARG or if `apropos-do-all' is non-nil, also looks
200 at the function and at the names and values of properties.
201 Returns list of symbols and values found."
202 (interactive "sApropos value (regexp): \nP")
203 (or do-all (setq do-all apropos-do-all))
204 (setq apropos-accumulator ())
208 (setq f nil v nil p nil)
209 (or (memq symbol '(apropos-regexp do-all apropos-accumulator
211 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
213 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
214 p (apropos-format-plist symbol "\n " t)))
216 (setq apropos-accumulator (cons (list symbol f v p)
217 apropos-accumulator))))))
218 (apropos-print nil nil t))
222 (defun apropos-documentation (apropos-regexp &optional do-all)
223 "Show symbols whose names or documentation contain matches for REGEXP.
224 With optional prefix ARG or if `apropos-do-all' is non-nil, also use
225 documentation that is not stored in the documentation file and show key
227 Returns list of symbols and documentation found."
228 (interactive "sApropos documentation (regexp): \nP")
229 (or do-all (setq do-all apropos-do-all))
230 (setq apropos-accumulator () apropos-files-scanned ())
231 (let ((standard-input (get-buffer-create " apropos-temp"))
235 (set-buffer standard-input)
236 (apropos-documentation-check-doc-file)
240 (setq f (apropos-safe-documentation symbol)
241 v (get symbol 'variable-documentation)
242 v (if (integerp v) nil v))
243 (or (string-match apropos-regexp (symbol-name symbol))
244 (setq f (apropos-documentation-internal f)
245 v (apropos-documentation-internal v)))
247 (if (setq apropos-item
248 (cdr (assq symbol apropos-accumulator)))
251 (setcar apropos-item f))
253 (setcar (cdr apropos-item) v)))
254 (setq apropos-accumulator
255 (cons (list symbol f v)
256 apropos-accumulator)))))))
257 (apropos-print do-all nil t))
258 (kill-buffer standard-input))))
261 (defun apropos-value-internal (predicate symbol function)
262 (if (funcall predicate symbol)
264 (setq symbol (prin1-to-string (funcall function symbol)))
265 (if (string-match apropos-regexp symbol)
267 (if apropos-match-face
268 (put-text-property (match-beginning 0) (match-end 0)
269 'face apropos-match-face
273 (defun apropos-documentation-internal (doc)
275 (apropos-documentation-check-elc-file (car doc))
277 (string-match apropos-regexp doc)
279 (if apropos-match-face
280 (put-text-property (match-beginning 0)
282 'face apropos-match-face
283 (setq doc (copy-sequence doc))))
286 (defun apropos-format-plist (pl sep &optional compare)
287 (setq pl (symbol-plist pl))
290 (setq p (format "%s %S" (car pl) (nth 1 pl)))
291 (if (or (not compare) (string-match apropos-regexp p))
292 (if apropos-property-face
293 (put-text-property 0 (length (symbol-name (car pl)))
294 'face apropos-property-face p))
298 (and compare apropos-match-face
299 (put-text-property (match-beginning 0) (match-end 0)
300 'face apropos-match-face
302 (setq p-out (concat p-out (if p-out sep) p))))
303 (setq pl (nthcdr 2 pl)))
307 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
309 (defun apropos-documentation-check-doc-file ()
310 (let (type symbol beg end)
311 (insert-file-contents (concat doc-directory internal-doc-file-name))
312 (while (re-search-forward apropos-regexp nil t)
313 (setq beg (match-beginning 0)
315 (search-backward "\C-_")
318 (or (setq type (if (eq ?F (char-after (1+ (point))))
319 1 ;function documentation
320 2) ;variable documentation
324 beg (- beg (point) 1)
325 end (- end (point) 1)
326 doc (buffer-substring
328 (if (search-forward "\C-_" nil 'move)
331 apropos-item (assq symbol apropos-accumulator))
332 (setq apropos-item (list symbol nil nil)
333 apropos-accumulator (cons apropos-item apropos-accumulator)))
334 (and apropos-match-face
336 (put-text-property beg end 'face apropos-match-face doc))
337 (setcar (nthcdr type apropos-item) doc)))))
339 (defun apropos-documentation-check-elc-file (file)
340 (if (member file apropos-files-scanned)
342 (let (symbol doc beg end end1 this-is-a-variable)
343 (setq apropos-files-scanned (cons file apropos-files-scanned))
345 (insert-file-contents file)
346 (while (search-forward "\n#@" nil t)
347 ;; Read the comment length, and advance over it.
350 end (+ (point) end 1))
351 (if (re-search-forward apropos-regexp end t)
354 (setq doc (buffer-substring (1+ beg) (- end 2))
355 end1 (- (match-end 0) beg 1)
356 beg (- (match-beginning 0) beg 1)
357 this-is-a-variable (looking-at "(defvar\\|(defconst")
359 (skip-chars-forward "(a-z")
362 symbol (if (consp symbol)
365 (if (if this-is-a-variable
366 (get symbol 'variable-documentation)
367 (and (fboundp symbol) (apropos-safe-documentation symbol)))
369 (or (setq apropos-item (assq symbol apropos-accumulator))
370 (setq apropos-item (list symbol nil nil)
371 apropos-accumulator (cons apropos-item
372 apropos-accumulator)))
373 (if apropos-match-face
374 (put-text-property beg end1 'face apropos-match-face
376 (setcar (nthcdr (if this-is-a-variable 2 1)
383 (defun apropos-safe-documentation (function)
384 "Like documentation, except it avoids calling `get_doc_string'.
385 Will return nil instead."
386 (while (and function (symbolp function))
387 (setq function (if (fboundp function)
388 (symbol-function function))))
389 (if (eq (car-safe function) 'macro)
390 (setq function (cdr function)))
391 (setq function (if (byte-code-function-p function)
392 (if (> (length function) 4)
394 (if (eq (car-safe function) 'autoload)
396 (if (eq (car-safe function) 'lambda)
397 (if (stringp (nth 2 function))
399 (if (stringp (nth 3 function))
400 (nth 3 function)))))))
401 (if (integerp function)
407 (defun apropos-print (do-keys doc-fn spacing)
408 "Output result of various apropos commands with `apropos-regexp'.
409 APROPOS-ACCUMULATOR is a list. Optional DOC-FN is called for each element
410 of apropos-accumulator and may modify it resulting in (symbol fn-doc
411 var-doc [plist-doc]). Returns sorted list of symbols and documentation
413 (if (null apropos-accumulator)
414 (message "No apropos matches for `%s'" apropos-regexp)
416 (funcall doc-fn apropos-accumulator))
417 (setq apropos-accumulator
418 (sort apropos-accumulator (lambda (a b)
419 (string-lessp (car a) (car b)))))
420 (and apropos-label-face
421 (symbolp apropos-label-face)
422 (setq apropos-label-face `(face ,apropos-label-face
423 mouse-face highlight)))
424 (with-output-to-temp-buffer "*Apropos*"
425 (let ((p apropos-accumulator)
426 (old-buffer (current-buffer))
427 symbol item point1 point2)
429 (set-buffer standard-output)
431 (insert (substitute-command-keys
432 "Click \\<apropos-local-map>\\[apropos-mouse-follow] to get full documentation.\n")))
433 (insert (substitute-command-keys
434 "In this buffer, type \\<apropos-local-map>\\[apropos-follow] to get full documentation.\n\n"))
435 (use-local-map apropos-local-map)
437 (or (not spacing) (bobp) (terpri))
438 (setq apropos-item (car p)
439 symbol (car apropos-item)
442 (princ symbol) ;print symbol name
443 (setq point2 (point))
444 ;; don't calculate key-bindings unless needed
449 (if (setq item (save-excursion
450 (set-buffer old-buffer)
451 (where-is-internal symbol)))
453 (if apropos-keybinding-face
455 (setq key (key-description key))
456 (put-text-property 0 (length key)
457 'face apropos-keybinding-face
462 "(not bound to any keys)")))
464 ;; only now so we don't propagate text attributes all over
465 (put-text-property point1 (1+ point1) 'item
466 (if (eval `(or ,@(cdr apropos-item)))
469 (if apropos-symbol-face
470 (put-text-property point1 point2 'face apropos-symbol-face))
471 (apropos-print-doc 'describe-function 1
472 (if (commandp symbol)
474 (if (apropos-macrop symbol)
478 (apropos-print-doc 'describe-variable 2
480 (apropos-print-doc 'apropos-describe-plist 3
482 (prog1 apropos-accumulator
483 (setq apropos-accumulator ()))) ; permit gc
486 (defun apropos-macrop (symbol)
487 "T if SYMBOL is a Lisp macro."
488 (and (fboundp symbol)
490 (symbol-function symbol)))
491 (or (eq (car symbol) 'macro)
492 (if (eq (car symbol) 'autoload)
497 (defun apropos-print-doc (action i str do-keys)
498 (if (stringp (setq i (nth i apropos-item)))
501 (put-text-property (- (point) 2) (1- (point))
504 (if apropos-label-face
505 (add-text-properties (- (point) (length str) 2)
508 (insert (if do-keys (substitute-command-keys i) i))
509 (or (bolp) (terpri)))))
512 (defun apropos-mouse-follow (event)
514 (let ((other (if (eq (current-buffer) (get-buffer "*Help*"))
517 (set-buffer (window-buffer (posn-window (event-start event))))
518 (goto-char (posn-point (event-start event)))
519 ;; somehow when clicking with the point in another window, undoes badly
521 (apropos-follow other)))
524 (defun apropos-follow (&optional other)
526 (let ((point (point))
527 (item (get-text-property (point) 'item))
530 (setq item (if (bobp)
532 (previous-single-property-change (point) 'item))
533 item (get-text-property
537 (1+ (next-single-property-change (point) 'item)))))
540 (error "%s is just a lonely symbol" (car item)))
541 (while (if (setq action-point
542 (next-single-property-change (point) 'action))
543 (<= action-point point))
544 (goto-char (1+ action-point))
545 (setq action action-point))
547 (prog1 (get-text-property (or action action-point (point)) 'action)
548 (if other (set-buffer other)))
553 (defun apropos-describe-plist (symbol)
554 "Display a pretty listing of SYMBOL's plist."
555 (with-output-to-temp-buffer "*Help*"
556 (set-buffer standard-output)
559 (princ "'s plist is\n (")
560 (if apropos-symbol-face
561 (put-text-property 8 (- (point) 14) 'face apropos-symbol-face))
562 (insert (apropos-format-plist symbol "\n "))
565 ;;; apropos.el ends here