1 ;;; apropos.el --- faster apropos commands.
3 ;; Copyright (C) 1989, 1994 Free Software Foundation, Inc.
5 ;; Author: Joe Wells <jbw@bigbird.bu.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; The ideas for this package were derived from the C code in
27 ;; src/keymap.c and elsewhere. The functions in this file should
28 ;; always be byte-compiled for speed. Someone should rewrite this in
29 ;; C (as part of src/keymap.c) for speed.
31 ;; The idea for super-apropos is based on the original implementation
32 ;; by Lynn Slater <lrs@esl.com>.
35 ;; Fixed bug, current-local-map can return nil.
36 ;; Change, doesn't calculate key-bindings unless needed.
37 ;; Added super-apropos capability, changed print functions.
38 ;; Made fast-apropos and super-apropos share code.
39 ;; Sped up fast-apropos again.
40 ;; Added apropos-do-all option.
41 ;; Added fast-command-apropos.
42 ;; Changed doc strings to comments for helping functions.
43 ;; Made doc file buffer read-only, buried it.
44 ;; Only call substitute-command-keys if do-all set.
48 (defvar apropos-do-all nil
49 "*Whether `apropos' and `super-apropos' should do everything that they can.
50 Makes them run 2 or 3 times slower. Set this non-nil if you have a fast
53 (defun apropos-worthy-symbol-p (symbol)
54 "Return non-nil if SYMBOL is not worthless."
57 (symbol-plist symbol
)))
60 (defun apropos (regexp &optional do-all pred no-header
)
61 "Show all symbols whose names contain matches for REGEXP.
62 If optional argument DO-ALL is non-nil (prefix argument if interactive),
63 or if `apropos-do-all' is non-nil, does more (time-consuming) work such as
64 showing key bindings. Optional argument PRED is called with each symbol, and
65 if it returns nil, the symbol is not shown. If PRED is nil, the
66 default predicate is that the symbol has a value, function definition
69 Optional argument NO-HEADER means don't print `Function:' or `Variable:'
72 Returns list of symbols and documentation found."
73 (interactive "sApropos (regexp): \nP")
74 (setq do-all
(or apropos-do-all do-all
))
75 (setq pred
(or pred
'apropos-worthy-symbol-p
))
76 (let ((apropos-accumulate (apropos-internal regexp pred
)))
77 (if (null apropos-accumulate
)
78 (message "No apropos matches for `%s'" regexp
)
79 (apropos-get-doc apropos-accumulate
)
80 (with-output-to-temp-buffer "*Help*"
81 (apropos-print-matches apropos-accumulate regexp nil
85 ;; Takes LIST of symbols and adds documentation. Modifies LIST in place.
86 ;; Resulting alist is of form ((symbol fn-doc var-doc) ...). Should only be
87 ;; called by apropos. Returns LIST.
89 (defun apropos-get-doc (list)
91 fn-doc var-doc symbol
)
94 fn-doc
(and (fboundp symbol
)
95 (documentation symbol
))
96 var-doc
(documentation-property symbol
'variable-documentation
)
98 (substring fn-doc
0 (string-match "\n" fn-doc
)))
100 (substring var-doc
0 (string-match "\n" var-doc
))))
101 (setcar p
(list symbol fn-doc var-doc
))
105 ;; Variables bound by super-apropos and used by its subroutines.
106 ;; It would be good to say what each one is for, but I don't know -- rms.
107 (defvar apropos-item
)
108 (defvar apropos-var-doc
)
109 (defvar apropos-fn-doc
)
110 (defvar apropos-accumulate
)
111 (defvar apropos-regexp
112 "Within `super-apropos', this holds the REGEXP argument.")
113 (defvar apropos-files-scanned
)
116 (defun super-apropos (regexp &optional do-all
)
117 "Show symbols whose names/documentation contain matches for REGEXP.
118 If optional argument DO-ALL is non-nil (prefix argument if interactive),
119 or if `apropos-do-all' is non-nil, does more (time-consuming) work such as
120 showing key bindings and documentation that is not stored in the documentation
123 Returns list of symbols and documentation found."
124 (interactive "sSuper Apropos: \nP")
125 (setq do-all
(or apropos-do-all do-all
))
126 (let ((apropos-regexp regexp
)
127 apropos-accumulate apropos-fn-doc apropos-var-doc apropos-item
128 apropos-files-scanned
)
129 (setq apropos-accumulate
130 (super-apropos-check-doc-file apropos-regexp
))
131 (if do-all
(mapatoms 'super-apropos-accumulate
))
132 (if (null apropos-accumulate
)
133 (message "No apropos matches for `%s'" apropos-regexp
)
134 (with-output-to-temp-buffer "*Help*"
135 (setq apropos-accumulate
136 (apropos-print-matches apropos-accumulate nil t do-all
))))
139 ;; Finds all documentation related to REGEXP in internal-doc-file-name.
140 ;; Returns an alist of form ((symbol fn-doc var-doc) ...).
142 (defun super-apropos-check-doc-file (regexp)
143 (let* ((doc-file (concat doc-directory internal-doc-file-name
))
144 (doc-buffer (get-buffer-create " apropos-temp"))
145 type symbol doc sym-list
)
148 (set-buffer doc-buffer
)
149 (buffer-disable-undo)
151 (insert-file-contents doc-file
)
152 (while (re-search-forward regexp nil t
)
153 (search-backward "\C-_")
154 (setq type
(if (eq ?F
(char-after (1+ (point))))
155 1 ;function documentation
156 2) ;variable documentation
160 doc
(buffer-substring
163 (if (search-forward "\C-_" nil
'move
)
166 apropos-item
(assq symbol sym-list
))
168 (and (fboundp symbol
) (documentation symbol
))
169 (documentation-property symbol
'variable-documentation
))
171 (setq apropos-item
(list symbol nil nil
)
172 sym-list
(cons apropos-item sym-list
)))
173 (setcar (nthcdr type apropos-item
) doc
))))
174 (kill-buffer doc-buffer
))
177 (defun super-apropos-check-elc-file (regexp file
)
178 (let* ((doc-buffer (get-buffer-create " apropos-temp"))
179 symbol doc length beg end this-is-a-variable
)
182 (set-buffer doc-buffer
)
183 (buffer-disable-undo)
185 (insert-file-contents file
)
186 (while (search-forward "\n#@" nil t
)
187 ;; Read the comment length, and advance over it.
188 (setq length
(read (current-buffer)))
190 (setq end
(+ (point) length
1))
191 (if (re-search-forward regexp end t
)
193 (setq this-is-a-variable
(save-excursion
195 (looking-at "(defvar\\|(defconst"))
196 symbol
(save-excursion
198 (skip-chars-forward "(a-z")
201 symbol
(if (consp symbol
)
204 doc
(buffer-substring (1+ beg
) (- end
2))
205 apropos-item
(assq symbol apropos-accumulate
))
206 (and (if this-is-a-variable
207 (documentation-property symbol
'variable-documentation
)
208 (and (fboundp symbol
) (documentation symbol
)))
210 (setq apropos-item
(list symbol nil nil
)
211 apropos-accumulate
(cons apropos-item
212 apropos-accumulate
)))
213 (setcar (nthcdr (if this-is-a-variable
2 1)
217 (kill-buffer doc-buffer
))
220 ;; This is passed as the argument to map-atoms, so it is called once for every
221 ;; symbol in obarray. Takes one argument SYMBOL, and finds any memory-resident
222 ;; documentation on that symbol if it matches a variable regexp.
224 (defun super-apropos-accumulate (symbol)
226 (cond ((string-match apropos-regexp
(symbol-name symbol
))
227 (setq apropos-item
(apropos-get-accum-item symbol
))
228 (setcar (cdr apropos-item
)
229 (or (safe-documentation symbol
)
230 (nth 1 apropos-item
)))
231 (setcar (nthcdr 2 apropos-item
)
232 (or (safe-documentation-property symbol
)
233 (nth 2 apropos-item
))))
234 ((or (consp (setq doc
(safe-documentation symbol
)))
235 (consp (setq doc
(safe-documentation-property symbol
))))
236 ;; This symbol's doc is stored in a file.
237 ;; Scan the file if we have not scanned it before.
238 (let ((file (car doc
)))
239 (or (member file apropos-files-scanned
)
241 (setq apropos-files-scanned
242 (cons file apropos-files-scanned
))
243 (super-apropos-check-elc-file apropos-regexp file
)))))
245 (and (stringp (setq doc
(safe-documentation symbol
)))
246 (setq apropos-fn-doc doc
)
247 (string-match apropos-regexp apropos-fn-doc
)
248 (setcar (cdr (apropos-get-accum-item symbol
)) apropos-fn-doc
))
249 (and (stringp (setq doc
(safe-documentation-property symbol
)))
250 (setq apropos-var-doc doc
)
251 (string-match apropos-regexp apropos-var-doc
)
252 (setcar (nthcdr 2 (apropos-get-accum-item symbol
))
256 ;; Prints the symbols and documentation in alist MATCHES of form ((symbol
257 ;; fn-doc var-doc) ...). Uses optional argument REGEXP to speed up searching
258 ;; for keybindings. The names of all symbols in MATCHES must match REGEXP.
259 ;; Displays in the buffer pointed to by standard-output. Optional argument
260 ;; SPACING means put blank lines in between each symbol's documentation.
261 ;; Optional argument DO-ALL means do more time-consuming work, specifically,
262 ;; consulting key bindings. Should only be called within a
263 ;; with-output-to-temp-buffer.
265 (defun apropos-print-matches (matches &optional regexp
266 spacing do-all no-header
)
267 (setq matches
(sort matches
(function
269 (string-lessp (car a
) (car b
))))))
271 (old-buffer (current-buffer))
272 item keys-done symbol tem
)
274 (set-buffer standard-output
)
275 (or matches
(princ "No matches found."))
280 (or (not spacing
) (bobp) (terpri))
281 (princ symbol
) ;print symbol name
282 ;; don't calculate key-bindings unless needed
283 (cond ((and do-all
(commandp symbol
) (not keys-done
))
285 (set-buffer old-buffer
)
286 (apropos-match-keys matches regexp
))
289 (or (setq tem
(nthcdr 3 item
))
293 (princ (mapconcat 'key-description tem
", "))
294 (princ "(not bound to any keys)"))))
296 (cond ((setq tem
(nth 1 item
))
297 (let ((substed (if do-all
(substitute-command-keys tem
) tem
)))
300 (princ " Function: ")
301 (if (> (length substed
) 67)
305 (cond ((setq tem
(nth 2 item
))
306 (let ((substed (if do-all
(substitute-command-keys tem
) tem
)))
309 (princ " Variable: ")
310 (if (> (length substed
) 67)
313 (or (bolp) (terpri)))
317 ;; Find key bindings for symbols that are cars in ALIST. Optionally, first
318 ;; match the symbol name against REGEXP. Modifies ALIST in place. Each key
319 ;; binding is added as a string to the end of the list in ALIST whose car is
320 ;; the corresponding symbol. The pointer to ALIST is returned.
322 (defun apropos-match-keys (alist &optional regexp
)
323 (let* ((current-local-map (current-local-map))
324 ;; Get a list of the top-level maps now active.
326 (if overriding-local-map
327 (list overriding-local-map
(current-global-map))
328 (append (current-minor-mode-maps)
329 (if current-local-map
330 (list current-local-map
(current-global-map))
331 (list (current-global-map))))))
332 ;; Turn that into a list of all the maps including submaps.
333 (maps (apply 'append
(mapcar 'accessible-keymaps top-maps
)))
334 map
;map we are now inspecting
335 sequence
;key sequence to reach map
336 i
;index into vector map
337 command
;what is bound to current keys
338 key
;last key to reach command
339 local
;local binding for sequence + key
340 item
) ;symbol data item in alist
341 ;; examine all reachable keymaps
343 (setq map
(cdr (car maps
))
344 sequence
(car (car maps
)) ;keys to reach this map
346 ;; Skip the leading `keymap', doc string, etc.
347 (if (eq (car map
) 'keymap
)
348 (setq map
(cdr map
)))
349 (while (stringp (car-safe map
))
350 (setq map
(cdr map
)))
353 (cond ((consp (car map
))
354 (setq command
(cdr (car map
))
356 ;; Skip any menu prompt and help string in this key binding.
357 (while (and (consp command
) (stringp (car command
)))
358 (setq command
(cdr command
)))
359 ;; Skip any cached equivalent key.
361 (consp (car command
))
362 (setq command
(cdr command
)))
363 ;; if is a symbol, and matches optional regexp, and is a car
364 ;; in alist, and is not shadowed by a different local binding,
366 (and (symbolp command
)
368 (string-match regexp
(symbol-name command
))
370 (setq item
(assq command alist
))
371 (if (or (vectorp sequence
) (not (integerp key
)))
372 (setq key
(vconcat sequence
(vector key
)))
373 (setq key
(concat sequence
(char-to-string key
))))
374 ;; checking if shadowed by local binding.
375 ;; either no local map, no local binding, or runs off the
376 ;; binding tree (number), or is the same binding
377 (or (not current-local-map
)
378 (not (setq local
(lookup-key current-local-map key
)))
381 ;; check if this binding is already recorded
382 ;; (this can happen due to inherited keymaps)
383 (not (member key
(nthcdr 3 item
)))
384 ;; add this key binding to the item in alist
385 (nconc item
(cons key nil
))))
389 (len (length (car map
))))
391 (setq command
(aref vec i
))
393 ;; Skip any menu prompt in this key binding.
394 (and (consp command
) (symbolp (cdr command
))
395 (setq command
(cdr command
)))
396 ;; This is the same as the code in the previous case.
397 (and (symbolp command
)
399 (string-match regexp
(symbol-name command
))
401 (setq item
(assq command alist
))
402 (if (or (vectorp sequence
) (not (integerp key
)))
403 (setq key
(vconcat sequence
(vector key
)))
404 (setq key
(concat sequence
(char-to-string key
))))
405 ;; checking if shadowed by local binding.
406 ;; either no local map, no local binding, or runs off the
407 ;; binding tree (number), or is the same binding
408 (or (not current-local-map
)
409 (not (setq local
(lookup-key current-local-map key
)))
412 ;; check if this binding is already recorded
413 ;; (this can happen due to inherited keymaps)
414 (not (member key
(nthcdr 3 item
)))
415 ;; add this key binding to the item in alist
416 (nconc item
(cons key nil
)))
418 (setq map
(cdr map
)))))
421 ;; Get an alist item in alist apropos-accumulate whose car is SYMBOL. Creates
422 ;; the item if not already present. Modifies apropos-accumulate in place.
424 (defun apropos-get-accum-item (symbol)
425 (or (assq symbol apropos-accumulate
)
427 (setq apropos-accumulate
428 (cons (list symbol nil nil
) apropos-accumulate
))
429 (assq symbol apropos-accumulate
))))
431 (defun safe-documentation (function)
432 "Like documentation, except it avoids calling `get_doc_string'.
433 Will return nil instead."
434 (while (symbolp function
)
435 (setq function
(if (fboundp function
)
436 (symbol-function function
)
438 (if (eq (car-safe function
) 'macro
)
439 (setq function
(cdr function
)))
440 (if (byte-code-function-p function
)
441 (if (> (length function
) 4)
443 (if (not (consp function
))
445 (if (not (memq (car function
) '(lambda autoload
)))
447 (setq function
(nth 2 function
))
448 (if (stringp function
)
452 (defun safe-documentation-property (symbol)
453 "Like documentation-property, except it avoids calling `get_doc_string'.
454 Will return nil instead."
455 (setq symbol
(get symbol
'variable-documentation
))
460 ;;; apropos.el ends here