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