Put isearch before menu-bar.
[bpt/emacs.git] / lisp / apropos.el
CommitLineData
c0274f38
ER
1;;; apropos.el --- faster apropos commands.
2
8f1204db 3;; Copyright (C) 1989, 1994 Free Software Foundation, Inc.
9750e079 4
e5167999 5;; Author: Joe Wells <jbw@bigbird.bu.edu>
e9571d2a 6;; Keywords: help
e5167999 7
6f8e447f
RS
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
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
6f8e447f
RS
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
e5167999 24;;; Commentary:
6f8e447f
RS
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
e5167999
ER
46;;; Code:
47
6f8e447f
RS
48(defvar apropos-do-all nil
49 "*Whether `apropos' and `super-apropos' should do everything that they can.
50Makes them run 2 or 3 times slower. Set this non-nil if you have a fast
51machine.")
52
4de5599d
RS
53(defun apropos-worthy-symbol-p (symbol)
54 "Return non-nil if SYMBOL is not worthless."
55 (or (fboundp symbol)
56 (boundp symbol)
57 (symbol-plist symbol)))
58
6f8e447f 59;;;###autoload
75a76881 60(defun apropos (regexp &optional do-all pred no-header)
6f8e447f 61 "Show all symbols whose names contain matches for REGEXP.
e6d25e14
KH
62If optional argument DO-ALL is non-nil (prefix argument if interactive),
63or if `apropos-do-all' is non-nil, does more (time-consuming) work such as
6f8e447f 64showing key bindings. Optional argument PRED is called with each symbol, and
4de5599d
RS
65if it returns nil, the symbol is not shown. If PRED is nil, the
66default predicate is that the symbol has a value, function definition
67or property list.
6f8e447f 68
75a76881
RS
69Optional argument NO-HEADER means don't print `Function:' or `Variable:'
70in the output.
71
6f8e447f
RS
72Returns list of symbols and documentation found."
73 (interactive "sApropos (regexp): \nP")
74 (setq do-all (or apropos-do-all do-all))
4de5599d 75 (setq pred (or pred 'apropos-worthy-symbol-p))
6f8e447f
RS
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*"
75a76881
RS
81 (apropos-print-matches apropos-accumulate regexp nil
82 do-all no-header)))
6f8e447f
RS
83 apropos-accumulate))
84
6f8e447f
RS
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
a33b76c3
RS
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.")
6cb26914 113(defvar apropos-files-scanned)
a33b76c3 114
6f8e447f
RS
115;;;###autoload
116(defun super-apropos (regexp &optional do-all)
117 "Show symbols whose names/documentation contain matches for REGEXP.
e6d25e14
KH
118If optional argument DO-ALL is non-nil (prefix argument if interactive),
119or if `apropos-do-all' is non-nil, does more (time-consuming) work such as
6f8e447f
RS
120showing key bindings and documentation that is not stored in the documentation
121file.
122
123Returns list of symbols and documentation found."
124 (interactive "sSuper Apropos: \nP")
125 (setq do-all (or apropos-do-all do-all))
a33b76c3 126 (let ((apropos-regexp regexp)
6cb26914
RS
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))
6f8e447f 132 (if (null apropos-accumulate)
a33b76c3 133 (message "No apropos matches for `%s'" apropos-regexp)
6f8e447f 134 (with-output-to-temp-buffer "*Help*"
6cb26914
RS
135 (setq apropos-accumulate
136 (apropos-print-matches apropos-accumulate nil t do-all))))
6f8e447f
RS
137 apropos-accumulate))
138
139;; Finds all documentation related to REGEXP in internal-doc-file-name.
140;; Returns an alist of form ((symbol fn-doc var-doc) ...).
141
142(defun super-apropos-check-doc-file (regexp)
201cb91b 143 (let* ((doc-file (concat doc-directory internal-doc-file-name))
6cb26914
RS
144 (doc-buffer (get-buffer-create " apropos-temp"))
145 type symbol doc sym-list)
146 (unwind-protect
147 (save-excursion
148 (set-buffer doc-buffer)
149 (buffer-disable-undo)
150 (erase-buffer)
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
157 symbol (progn
158 (forward-char 2)
159 (read doc-buffer))
160 doc (buffer-substring
161 (point)
162 (progn
163 (if (search-forward "\C-_" nil 'move)
164 (1- (point))
165 (point))))
166 apropos-item (assq symbol sym-list))
167 (and (if (= type 1)
168 (and (fboundp symbol) (documentation symbol))
169 (documentation-property symbol 'variable-documentation))
170 (or apropos-item
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))
6f8e447f
RS
175 sym-list))
176
6cb26914
RS
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)
180 (unwind-protect
181 (save-excursion
182 (set-buffer doc-buffer)
183 (buffer-disable-undo)
184 (erase-buffer)
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)))
189 (setq beg (point))
190 (setq end (+ (point) length 1))
191 (if (re-search-forward regexp end t)
192 (progn
193 (setq this-is-a-variable (save-excursion
194 (goto-char end)
195 (looking-at "(defvar\\|(defconst"))
196 symbol (save-excursion
197 (goto-char end)
198 (skip-chars-forward "(a-z")
199 (forward-char 1)
200 (read doc-buffer))
201 symbol (if (consp symbol)
202 (nth 1 symbol)
203 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)))
209 (or apropos-item
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)
214 apropos-item)
215 doc))))
216 (goto-char end)))
217 (kill-buffer doc-buffer))
218 apropos-accumulate))
219
6f8e447f
RS
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
a33b76c3 222;; documentation on that symbol if it matches a variable regexp.
6f8e447f
RS
223
224(defun super-apropos-accumulate (symbol)
6cb26914
RS
225 (let (doc)
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)
240 (progn
241 (setq apropos-files-scanned
242 (cons file apropos-files-scanned))
243 (super-apropos-check-elc-file apropos-regexp file)))))
244 (t
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))
253 apropos-var-doc)))))
6f8e447f
RS
254 nil)
255
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.
264
75a76881
RS
265(defun apropos-print-matches (matches &optional regexp
266 spacing do-all no-header)
6f8e447f
RS
267 (setq matches (sort matches (function
268 (lambda (a b)
269 (string-lessp (car a) (car b))))))
270 (let ((p matches)
271 (old-buffer (current-buffer))
2b019431 272 item keys-done symbol tem)
6f8e447f
RS
273 (save-excursion
274 (set-buffer standard-output)
275 (or matches (princ "No matches found."))
276 (while (consp p)
277 (setq item (car p)
278 symbol (car item)
279 p (cdr p))
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))
284 (save-excursion
285 (set-buffer old-buffer)
286 (apropos-match-keys matches regexp))
287 (setq keys-done t)))
288 (cond ((and do-all
289 (or (setq tem (nthcdr 3 item))
290 (commandp symbol)))
291 (indent-to 30 1)
292 (if tem
293 (princ (mapconcat 'key-description tem ", "))
294 (princ "(not bound to any keys)"))))
295 (terpri)
296 (cond ((setq tem (nth 1 item))
75a76881
RS
297 (let ((substed (if do-all (substitute-command-keys tem) tem)))
298 (if no-header
299 (princ " ")
300 (princ " Function: ")
301 (if (> (length substed) 67)
302 (princ "\n ")))
303 (princ substed))))
6f8e447f
RS
304 (or (bolp) (terpri))
305 (cond ((setq tem (nth 2 item))
75a76881
RS
306 (let ((substed (if do-all (substitute-command-keys tem) tem)))
307 (if no-header
308 (princ " ")
309 (princ " Variable: ")
310 (if (> (length substed) 67)
311 (princ "\n ")))
312 (princ substed))))
ca75b358
KH
313 (or (bolp) (terpri)))
314 (help-mode)))
6cb26914 315 matches)
6f8e447f
RS
316
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.
321
322(defun apropos-match-keys (alist &optional regexp)
323 (let* ((current-local-map (current-local-map))
487d5fb2
RS
324 ;; Get a list of the top-level maps now active.
325 (top-maps
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)))
6f8e447f
RS
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
342 (while (consp maps)
343 (setq map (cdr (car maps))
344 sequence (car (car maps)) ;keys to reach this map
345 maps (cdr maps))
4fa88406
RS
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))
6f8e447f 350 (setq map (cdr map)))
487d5fb2 351
4fa88406
RS
352 (while (consp map)
353 (cond ((consp (car map))
6f8e447f 354 (setq command (cdr (car map))
4fa88406 355 key (car (car map)))
487d5fb2
RS
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.
360 (and (consp command)
361 (consp (car command))
4fa88406
RS
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,
365 ;; record it
366 (and (symbolp command)
12fbf178
RS
367 (if regexp
368 (string-match regexp (symbol-name command))
369 t)
4fa88406
RS
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)))
379 (numberp local)
380 (eq command local))
ee829a87
KH
381 ;; check if this binding is already recorded
382 ;; (this can happen due to inherited keymaps)
383 (not (member key (nthcdr 3 item)))
4fa88406
RS
384 ;; add this key binding to the item in alist
385 (nconc item (cons key nil))))
386 ((vectorp (car map))
387 (let ((i 0)
388 (vec (car map))
389 (len (length (car map))))
390 (while (< i len)
391 (setq command (aref vec i))
392 (setq key 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)
12fbf178
RS
398 (if regexp
399 (string-match regexp (symbol-name command))
400 t)
4fa88406
RS
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)))
410 (numberp local)
411 (eq command local))
ee829a87
KH
412 ;; check if this binding is already recorded
413 ;; (this can happen due to inherited keymaps)
414 (not (member key (nthcdr 3 item)))
4fa88406
RS
415 ;; add this key binding to the item in alist
416 (nconc item (cons key nil)))
417 (setq i (1+ i))))))
418 (setq map (cdr map)))))
6f8e447f
RS
419 alist)
420
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.
423
424(defun apropos-get-accum-item (symbol)
425 (or (assq symbol apropos-accumulate)
426 (progn
427 (setq apropos-accumulate
428 (cons (list symbol nil nil) apropos-accumulate))
429 (assq symbol apropos-accumulate))))
430
431(defun safe-documentation (function)
432 "Like documentation, except it avoids calling `get_doc_string'.
433Will return nil instead."
434 (while (symbolp function)
435 (setq function (if (fboundp function)
436 (symbol-function function)
437 0)))
d2e1218f
RS
438 (if (eq (car-safe function) 'macro)
439 (setq function (cdr function)))
6cb26914
RS
440 (if (byte-code-function-p function)
441 (if (> (length function) 4)
442 (aref function 4))
443 (if (not (consp function))
6f8e447f 444 nil
6cb26914
RS
445 (if (not (memq (car function) '(lambda autoload)))
446 nil
447 (setq function (nth 2 function))
448 (if (stringp function)
449 function
450 nil)))))
6f8e447f
RS
451
452(defun safe-documentation-property (symbol)
453 "Like documentation-property, except it avoids calling `get_doc_string'.
454Will return nil instead."
455 (setq symbol (get symbol 'variable-documentation))
456 (if (numberp symbol)
457 nil
458 symbol))
459
c0274f38 460;;; apropos.el ends here