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