Commit | Line | Data |
---|---|---|
c0274f38 ER |
1 | ;;; apropos.el --- faster apropos commands. |
2 | ||
6f8e447f RS |
3 | ;; Copyright (C) 1989 Free Software Foundation, Inc. |
4 | ||
5 | ;; This file is part of GNU Emacs. | |
6 | ||
7 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
8 | ;; it under the terms of the GNU General Public License as published by | |
9 | ;; the Free Software Foundation; either version 1, or (at your option) | |
10 | ;; any later version. | |
11 | ||
12 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ;; GNU General Public License for more details. | |
16 | ||
17 | ;; You should have received a copy of the GNU General Public License | |
18 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
19 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | ||
21 | ;; Author: Joe Wells | |
22 | ;; Last changed: Fri May 5 18:08:16 1989 by jbw (Joseph Wells) on bucsf | |
23 | ;; jbw%bucsf.bu.edu@bu-it.bu.edu (school year) | |
24 | ;; joew@uswest.com (summer) | |
25 | ||
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. | |
30 | ||
31 | ;; The idea for super-apropos is based on the original implementation | |
32 | ;; by Lynn Slater <lrs@esl.com>. | |
33 | ||
34 | ;; History: | |
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. | |
45 | ||
46 | (defvar apropos-do-all nil | |
47 | "*Whether `apropos' and `super-apropos' should do everything that they can. | |
48 | Makes them run 2 or 3 times slower. Set this non-nil if you have a fast | |
49 | machine.") | |
50 | ||
51 | ;; If there isn't already a lisp variable named internal-doc-file-name, create | |
52 | ;; it and document it. This is so the code will work right after RMS adds | |
53 | ;; internal-doc-file-name. | |
54 | ;(or (boundp 'internal-doc-file-name) | |
1e6dacf6 | 55 | ; (setq internal-doc-file-name (concat data-directory "DOC"))) |
6f8e447f RS |
56 | ;(or (documentation-property 'internal-doc-file-name 'variable-documentation) |
57 | ; (put 'internal-doc-file-name 'variable-documentation | |
58 | ; "The complete pathname of the documentation file that contains all | |
59 | ;documentation for functions and variables defined before Emacs is dumped.")) | |
60 | ||
61 | ;;;###autoload | |
62 | (defun apropos (regexp &optional do-all pred) | |
63 | "Show all symbols whose names contain matches for REGEXP. | |
64 | If optional argument DO-ALL is non-nil, does more (time-consuming) work such as | |
65 | showing key bindings. Optional argument PRED is called with each symbol, and | |
66 | if it returns nil, the symbol is not shown. | |
67 | ||
68 | Returns list of symbols and documentation found." | |
69 | (interactive "sApropos (regexp): \nP") | |
70 | (setq do-all (or apropos-do-all do-all)) | |
71 | (let ((apropos-accumulate (apropos-internal regexp pred))) | |
72 | (if (null apropos-accumulate) | |
73 | (message "No apropos matches for `%s'" regexp) | |
74 | (apropos-get-doc apropos-accumulate) | |
75 | (with-output-to-temp-buffer "*Help*" | |
76 | (apropos-print-matches apropos-accumulate regexp nil do-all))) | |
77 | apropos-accumulate)) | |
78 | ||
79 | ;; If "C-h a" still has its original binding of command-apropos, change it to | |
80 | ;; use fast-command-apropos. I don't use substitute-key-definition because | |
81 | ;; it's slow. | |
82 | ;(if (eq 'command-apropos (lookup-key help-map "a")) | |
83 | ; (define-key help-map "a" 'fast-command-apropos)) | |
84 | ||
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. | |
88 | ||
89 | (defun apropos-get-doc (list) | |
90 | (let ((p list) | |
91 | fn-doc var-doc symbol) | |
92 | (while (consp p) | |
93 | (setq symbol (car p) | |
94 | fn-doc (and (fboundp symbol) | |
95 | (documentation symbol)) | |
96 | var-doc (documentation-property symbol 'variable-documentation) | |
97 | fn-doc (and fn-doc | |
98 | (substring fn-doc 0 (string-match "\n" fn-doc))) | |
99 | var-doc (and var-doc | |
100 | (substring var-doc 0 (string-match "\n" var-doc)))) | |
101 | (setcar p (list symbol fn-doc var-doc)) | |
102 | (setq p (cdr p))) | |
103 | list)) | |
104 | ||
105 | ;;;###autoload | |
106 | (defun super-apropos (regexp &optional do-all) | |
107 | "Show symbols whose names/documentation contain matches for REGEXP. | |
108 | If optional argument DO-ALL is non-nil, does more (time-consuming) work such as | |
109 | showing key bindings and documentation that is not stored in the documentation | |
110 | file. | |
111 | ||
112 | Returns list of symbols and documentation found." | |
113 | (interactive "sSuper Apropos: \nP") | |
114 | (setq do-all (or apropos-do-all do-all)) | |
115 | (let (apropos-accumulate fn-doc var-doc item) | |
116 | (setq apropos-accumulate (super-apropos-check-doc-file regexp)) | |
117 | (if (null apropos-accumulate) | |
118 | (message "No apropos matches for `%s'" regexp) | |
119 | (if do-all (mapatoms 'super-apropos-accumulate)) | |
120 | (with-output-to-temp-buffer "*Help*" | |
121 | (apropos-print-matches apropos-accumulate nil t do-all))) | |
122 | apropos-accumulate)) | |
123 | ||
124 | ;; Finds all documentation related to REGEXP in internal-doc-file-name. | |
125 | ;; Returns an alist of form ((symbol fn-doc var-doc) ...). | |
126 | ||
127 | (defun super-apropos-check-doc-file (regexp) | |
128 | (let ((doc-buffer (find-file-noselect internal-doc-file-name t)) | |
129 | ;; (doc-buffer (or (get-file-buffer internal-doc-file-name) | |
130 | ;; (find-file-noselect internal-doc-file-name))) | |
131 | type symbol doc sym-list) | |
132 | (save-excursion | |
133 | (set-buffer doc-buffer) | |
134 | ;; a user said he might accidentally edit the doc file | |
135 | (setq buffer-read-only t) | |
136 | (bury-buffer doc-buffer) | |
137 | (goto-char (point-min)) | |
138 | (while (re-search-forward regexp nil t) | |
139 | (search-backward "\C-_") | |
140 | (setq type (if (eq ?F (char-after (1+ (point)))) | |
141 | 1 ;function documentation | |
142 | 2) ;variable documentation | |
143 | symbol (progn | |
144 | (forward-char 2) | |
145 | (read doc-buffer)) | |
146 | doc (buffer-substring | |
147 | (point) | |
148 | (progn | |
149 | (if (search-forward "\C-_" nil 'move) | |
150 | (1- (point)) | |
151 | (point)))) | |
152 | item (assq symbol sym-list)) | |
153 | (or item | |
154 | (setq item (list symbol nil nil) | |
155 | sym-list (cons item sym-list))) | |
156 | (setcar (nthcdr type item) doc))) | |
157 | sym-list)) | |
158 | ||
159 | ;; This is passed as the argument to map-atoms, so it is called once for every | |
160 | ;; symbol in obarray. Takes one argument SYMBOL, and finds any memory-resident | |
161 | ;; documentation on that symbol if it matches a variable regexp. WARNING: this | |
162 | ;; function depends on the symbols fn-doc var-doc regexp and item being bound | |
163 | ;; correctly when it is called!" | |
164 | ||
165 | (defun super-apropos-accumulate (symbol) | |
166 | (cond ((string-match regexp (symbol-name symbol)) | |
167 | (setq item (apropos-get-accum-item symbol)) | |
168 | (setcar (cdr item) (or (safe-documentation symbol) | |
169 | (nth 1 item))) | |
170 | (setcar (nthcdr 2 item) (or (safe-documentation-property symbol) | |
171 | (nth 2 item)))) | |
172 | (t | |
173 | (and (setq fn-doc (safe-documentation symbol)) | |
174 | (string-match regexp fn-doc) | |
175 | (setcar (cdr (apropos-get-accum-item symbol)) fn-doc)) | |
176 | (and (setq var-doc (safe-documentation-property symbol)) | |
177 | (string-match regexp var-doc) | |
178 | (setcar (nthcdr 2 (apropos-get-accum-item symbol)) var-doc)))) | |
179 | nil) | |
180 | ||
181 | ;; Prints the symbols and documentation in alist MATCHES of form ((symbol | |
182 | ;; fn-doc var-doc) ...). Uses optional argument REGEXP to speed up searching | |
183 | ;; for keybindings. The names of all symbols in MATCHES must match REGEXP. | |
184 | ;; Displays in the buffer pointed to by standard-output. Optional argument | |
185 | ;; SPACING means put blank lines in between each symbol's documentation. | |
186 | ;; Optional argument DO-ALL means do more time-consuming work, specifically, | |
187 | ;; consulting key bindings. Should only be called within a | |
188 | ;; with-output-to-temp-buffer. | |
189 | ||
190 | (defun apropos-print-matches (matches &optional regexp spacing do-all) | |
191 | (setq matches (sort matches (function | |
192 | (lambda (a b) | |
193 | (string-lessp (car a) (car b)))))) | |
194 | (let ((p matches) | |
195 | (old-buffer (current-buffer)) | |
196 | item keys-done symbol) | |
197 | (save-excursion | |
198 | (set-buffer standard-output) | |
199 | (or matches (princ "No matches found.")) | |
200 | (while (consp p) | |
201 | (setq item (car p) | |
202 | symbol (car item) | |
203 | p (cdr p)) | |
204 | (or (not spacing) (bobp) (terpri)) | |
205 | (princ symbol) ;print symbol name | |
206 | ;; don't calculate key-bindings unless needed | |
207 | (cond ((and do-all (commandp symbol) (not keys-done)) | |
208 | (save-excursion | |
209 | (set-buffer old-buffer) | |
210 | (apropos-match-keys matches regexp)) | |
211 | (setq keys-done t))) | |
212 | (cond ((and do-all | |
213 | (or (setq tem (nthcdr 3 item)) | |
214 | (commandp symbol))) | |
215 | (indent-to 30 1) | |
216 | (if tem | |
217 | (princ (mapconcat 'key-description tem ", ")) | |
218 | (princ "(not bound to any keys)")))) | |
219 | (terpri) | |
220 | (cond ((setq tem (nth 1 item)) | |
221 | (princ " Function: ") | |
222 | (princ (if do-all (substitute-command-keys tem) tem)))) | |
223 | (or (bolp) (terpri)) | |
224 | (cond ((setq tem (nth 2 item)) | |
225 | (princ " Variable: ") | |
226 | (princ (if do-all (substitute-command-keys tem) tem)))) | |
227 | (or (bolp) (terpri))))) | |
228 | t) | |
229 | ||
230 | ;; Find key bindings for symbols that are cars in ALIST. Optionally, first | |
231 | ;; match the symbol name against REGEXP. Modifies ALIST in place. Each key | |
232 | ;; binding is added as a string to the end of the list in ALIST whose car is | |
233 | ;; the corresponding symbol. The pointer to ALIST is returned. | |
234 | ||
235 | (defun apropos-match-keys (alist &optional regexp) | |
236 | (let* ((current-local-map (current-local-map)) | |
237 | (maps (append (and current-local-map | |
238 | (accessible-keymaps current-local-map)) | |
239 | (accessible-keymaps (current-global-map)))) | |
240 | map ;map we are now inspecting | |
241 | sequence ;key sequence to reach map | |
242 | i ;index into vector map | |
243 | command ;what is bound to current keys | |
244 | key ;last key to reach command | |
245 | local ;local binding for sequence + key | |
246 | item) ;symbol data item in alist | |
247 | ;; examine all reachable keymaps | |
248 | (while (consp maps) | |
249 | (setq map (cdr (car maps)) | |
250 | sequence (car (car maps)) ;keys to reach this map | |
251 | maps (cdr maps)) | |
252 | (setq i 0) | |
253 | ;; In an alist keymap, skip the leading `keymap', doc string, etc. | |
254 | (while (and (consp map) (not (consp (car map)))) | |
255 | (setq map (cdr map))) | |
256 | (while (and map (< i 128)) ;vector keymaps have 128 entries | |
257 | (cond ((consp map) | |
258 | (setq command (cdr (car map)) | |
259 | key (car (car map)) | |
260 | map (cdr map)) | |
261 | ;; Skip any atoms in the keymap. | |
262 | (while (and (consp map) (not (consp (car map)))) | |
263 | (setq map (cdr map)))) | |
264 | ((vectorp map) | |
265 | (setq command (aref map i) | |
266 | key i | |
267 | i (1+ i)))) | |
268 | ;; Skip any menu prompt in this key binding. | |
269 | (and (consp command) (symbolp (cdr command)) | |
270 | (setq command (cdr command))) | |
271 | ;; if is a symbol, and matches optional regexp, and is a car | |
272 | ;; in alist, and is not shadowed by a different local binding, | |
273 | ;; record it | |
274 | (and (symbolp command) | |
275 | (if regexp (string-match regexp (symbol-name command))) | |
276 | (setq item (assq command alist)) | |
277 | (setq key (concat sequence (char-to-string key))) | |
278 | ;; checking if shadowed by local binding. | |
279 | ;; either no local map, no local binding, or runs off the | |
280 | ;; binding tree (number), or is the same binding | |
281 | (or (not current-local-map) | |
282 | (not (setq local (lookup-key current-local-map key))) | |
283 | (numberp local) | |
284 | (eq command local)) | |
285 | ;; add this key binding to the item in alist | |
286 | (nconc item (cons key nil)))))) | |
287 | alist) | |
288 | ||
289 | ;; Get an alist item in alist apropos-accumulate whose car is SYMBOL. Creates | |
290 | ;; the item if not already present. Modifies apropos-accumulate in place. | |
291 | ||
292 | (defun apropos-get-accum-item (symbol) | |
293 | (or (assq symbol apropos-accumulate) | |
294 | (progn | |
295 | (setq apropos-accumulate | |
296 | (cons (list symbol nil nil) apropos-accumulate)) | |
297 | (assq symbol apropos-accumulate)))) | |
298 | ||
299 | (defun safe-documentation (function) | |
300 | "Like documentation, except it avoids calling `get_doc_string'. | |
301 | Will return nil instead." | |
302 | (while (symbolp function) | |
303 | (setq function (if (fboundp function) | |
304 | (symbol-function function) | |
305 | 0))) | |
306 | (if (not (consp function)) | |
307 | nil | |
308 | (if (eq (car function) 'macro) | |
309 | (setq function (cdr function))) | |
310 | (if (not (memq (car function) '(lambda autoload))) | |
311 | nil | |
312 | (setq function (nth 2 function)) | |
313 | (if (stringp function) | |
314 | function | |
315 | nil)))) | |
316 | ||
317 | (defun safe-documentation-property (symbol) | |
318 | "Like documentation-property, except it avoids calling `get_doc_string'. | |
319 | Will return nil instead." | |
320 | (setq symbol (get symbol 'variable-documentation)) | |
321 | (if (numberp symbol) | |
322 | nil | |
323 | symbol)) | |
324 | ||
c0274f38 | 325 | ;;; apropos.el ends here |