(with-electric-help): Add missing argument MINHEIGHT.
[bpt/emacs.git] / lisp / apropos.el
CommitLineData
3925e76d 1;;; apropos.el --- apropos commands for users and programmers.
c0274f38 2
3925e76d 3;; Copyright (C) 1989, 1994, 1995 Free Software Foundation, Inc.
9750e079 4
e5167999 5;; Author: Joe Wells <jbw@bigbird.bu.edu>
3925e76d 6;; Rewritten: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
e9571d2a 7;; Keywords: help
e5167999 8
6f8e447f
RS
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.
3925e76d
KH
39;;; Made fast-apropos and super-apropos share code.
40;;; Sped up fast-apropos again.
6f8e447f 41;; Added apropos-do-all option.
3925e76d 42;;; Added fast-command-apropos.
6f8e447f 43;; Changed doc strings to comments for helping functions.
3925e76d 44;;; Made doc file buffer read-only, buried it.
6f8e447f
RS
45;; Only call substitute-command-keys if do-all set.
46
645c4f6a
KH
47;; Optionally use configurable faces to make the output more legible.
48;; Differentiate between command, function and macro.
3925e76d
KH
49;; Apropos-command (ex command-apropos) does cmd and optionally user var.
50;; Apropos shows all 3 aspects of symbols (fn, var and plist)
51;; Apropos-documentation (ex super-apropos) now finds all it should.
52;; New apropos-value snoops through all values and optionally plists.
53;; Reading DOC file doesn't load nroff.
54;; Added hypertext following of documentation, mouse-2 on variable gives value
55;; from buffer in active window.
56
e5167999
ER
57;;; Code:
58
3925e76d 59;; I see a degradation of maybe 10-20% only.
6f8e447f 60(defvar apropos-do-all nil
3925e76d
KH
61 "*Whether the apropos commands should do more.
62Slows them down more or less. Set this non-nil if you have a fast machine.")
63
64
645c4f6a
KH
65(defvar apropos-symbol-face (if window-system 'bold)
66 "*Face for symbol name in apropos output or `nil'.
67This looks good, but slows down the commands several times.")
68
69(defvar apropos-keybinding-face (if window-system 'underline)
70 "*Face for keybinding display in apropos output or `nil'.
71This looks good, but slows down the commands several times.")
72
73(defvar apropos-label-face (if window-system 'italic)
74 "*Face for label (Command, Variable ...) in apropos output or `nil'.
75If this is `nil' no mouse highlighting occurs.
76This looks good, but slows down the commands several times.
77When this is a face name, as it is initially, it gets transformed to a
78text-property list for efficiency.")
79
80(defvar apropos-property-face (if window-system 'bold-italic)
81 "*Face for property name in apropos output or `nil'.
82This looks good, but slows down the commands several times.")
83
84(defvar apropos-match-face (if window-system 'highlight)
85 "*Face for matching part in apropos-documentation/value output or `nil'.
3925e76d
KH
86This looks good, but slows down the commands several times.")
87
6f8e447f 88
3925e76d
KH
89(defvar apropos-local-map
90 (let ((map (make-sparse-keymap)))
91 (define-key map "\C-m" 'apropos-follow)
92 (define-key map [mouse-2] 'apropos-mouse-follow)
93 (define-key map [down-mouse-2] nil)
94 map)
95 "Local map active when displaying apropos output.")
4de5599d 96
3925e76d 97
645c4f6a
KH
98(defvar apropos-regexp nil
99 "Regexp used in current apropos run.")
100
101(defvar apropos-files-scanned ()
102 "List of elc files already scanned in current run of `apropos-documentaion'.")
103
104(defvar apropos-accumulator ()
105 "Alist of symbols already found in current apropos run.")
3925e76d 106
645c4f6a
KH
107(defvar apropos-item ()
108 "Current item in or for apropos-accumulator.")
109\f
110;; For auld lang syne:
111;;;###autoload
112(fset 'command-apropos 'apropos-command)
6f8e447f 113;;;###autoload
645c4f6a 114(defun apropos-command (apropos-regexp &optional do-all)
3925e76d
KH
115 "Shows commands (interactively callable functions) that match REGEXP.
116With optional prefix ARG or if `apropos-do-all' is non-nil, also show
117variables."
118 (interactive (list (read-string (concat "Apropos command "
119 (if (or current-prefix-arg
645c4f6a 120 apropos-do-all)
3925e76d
KH
121 "or variable ")
122 "(regexp): "))
645c4f6a 123 current-prefix-arg))
3925e76d
KH
124 (let ((message
125 (let ((standard-output (get-buffer-create "*Help*")))
126 (print-help-return-message 'identity))))
645c4f6a
KH
127 (or do-all (setq do-all apropos-do-all))
128 (setq apropos-accumulator
129 (apropos-internal apropos-regexp
130 (if do-all
131 (lambda (symbol) (or (commandp symbol)
132 (user-variable-p symbol)))
133 'commandp)))
3925e76d 134 (if (apropos-print
3925e76d
KH
135 t
136 (lambda (p)
137 (let (doc symbol)
138 (while p
139 (setcar p (list
140 (setq symbol (car p))
141 (if (commandp symbol)
142 (if (setq doc (documentation symbol t))
143 (substring doc 0 (string-match "\n" doc))
144 "(not documented)"))
145 (and do-all
146 (user-variable-p symbol)
147 (if (setq doc (documentation-property
148 symbol 'variable-documentation t))
149 (substring doc 0
150 (string-match "\n" doc))))))
151 (setq p (cdr p)))))
152 nil)
153 (and message (message message)))))
154
155
3925e76d 156;;;###autoload
645c4f6a
KH
157(defun apropos (apropos-regexp &optional do-all)
158 "Show all bound symbols whose names match REGEXP.
159With optional prefix ARG or if `apropos-do-all' is non-nil, also show unbound
160symbols and key bindings, which is a little more time-consuming.
6f8e447f 161Returns list of symbols and documentation found."
3925e76d 162 (interactive "sApropos symbol (regexp): \nP")
645c4f6a
KH
163 (setq apropos-accumulator
164 (apropos-internal apropos-regexp
165 (and (not do-all)
166 (not apropos-do-all)
167 (lambda (symbol)
168 (or (fboundp symbol)
169 (boundp symbol)
170 (symbol-plist symbol))))))
3925e76d 171 (apropos-print
645c4f6a 172 (or do-all apropos-do-all)
3925e76d
KH
173 (lambda (p)
174 (let (symbol doc)
175 (while p
176 (setcar p (list
177 (setq symbol (car p))
178 (if (fboundp symbol)
179 (if (setq doc (documentation symbol t))
180 (substring doc 0 (string-match "\n" doc))
181 "(not documented)"))
182 (if (boundp symbol)
183 (if (setq doc (documentation-property
184 symbol 'variable-documentation t))
185 (substring doc 0
186 (string-match "\n" doc))
187 "(not documented)"))
188 (if (setq doc (symbol-plist symbol))
189 (if (eq (setq doc (/ (length doc) 2)) 1)
190 "1 property"
191 (concat doc " properties")))))
192 (setq p (cdr p)))))
193 nil))
194
195
6f8e447f 196;;;###autoload
645c4f6a 197(defun apropos-value (apropos-regexp &optional do-all)
3925e76d
KH
198 "Show all symbols whose value's printed image matches REGEXP.
199With optional prefix ARG or if `apropos-do-all' is non-nil, also looks
200at the function and at the names and values of properties.
645c4f6a 201Returns list of symbols and values found."
3925e76d 202 (interactive "sApropos value (regexp): \nP")
645c4f6a
KH
203 (or do-all (setq do-all apropos-do-all))
204 (setq apropos-accumulator ())
205 (let (f v p)
3925e76d
KH
206 (mapatoms
207 (lambda (symbol)
208 (setq f nil v nil p nil)
645c4f6a
KH
209 (or (memq symbol '(apropos-regexp do-all apropos-accumulator
210 symbol f v p))
211 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
3925e76d 212 (if do-all
645c4f6a
KH
213 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
214 p (apropos-format-plist symbol "\n " t)))
3925e76d 215 (if (or f v p)
645c4f6a
KH
216 (setq apropos-accumulator (cons (list symbol f v p)
217 apropos-accumulator))))))
218 (apropos-print nil nil t))
3925e76d
KH
219
220
645c4f6a
KH
221;;;###autoload
222(defun apropos-documentation (apropos-regexp &optional do-all)
223 "Show symbols whose names or documentation contain matches for REGEXP.
224With optional prefix ARG or if `apropos-do-all' is non-nil, also use
225documentation that is not stored in the documentation file and show key
226bindings.
227Returns list of symbols and documentation found."
228 (interactive "sApropos documentation (regexp): \nP")
229 (or do-all (setq do-all apropos-do-all))
230 (setq apropos-accumulator () apropos-files-scanned ())
231 (let ((standard-input (get-buffer-create " apropos-temp"))
232 f v)
233 (unwind-protect
234 (save-excursion
235 (set-buffer standard-input)
236 (apropos-documentation-check-doc-file)
237 (if do-all
238 (mapatoms
239 (lambda (symbol)
240 (setq f (apropos-safe-documentation symbol)
241 v (get symbol 'variable-documentation)
242 v (if (integerp v) nil v))
243 (or (string-match apropos-regexp (symbol-name symbol))
244 (setq f (apropos-documentation-internal f)
245 v (apropos-documentation-internal v)))
246 (if (or f v)
247 (if (setq apropos-item
248 (cdr (assq symbol apropos-accumulator)))
249 (progn
250 (if f
251 (setcar apropos-item f))
252 (if v
253 (setcar (cdr apropos-item) v)))
254 (setq apropos-accumulator
255 (cons (list symbol f v)
256 apropos-accumulator)))))))
257 (apropos-print do-all nil t))
258 (kill-buffer standard-input))))
259
260\f
261(defun apropos-value-internal (predicate symbol function)
262 (if (funcall predicate symbol)
263 (progn
264 (setq symbol (prin1-to-string (funcall function symbol)))
265 (if (string-match apropos-regexp symbol)
266 (progn
267 (if apropos-match-face
268 (put-text-property (match-beginning 0) (match-end 0)
269 'face apropos-match-face
270 symbol))
271 symbol)))))
272
273(defun apropos-documentation-internal (doc)
274 (if (consp doc)
275 (apropos-documentation-check-elc-file (car doc))
276 (and doc
277 (string-match apropos-regexp doc)
278 (progn
279 (if apropos-match-face
280 (put-text-property (match-beginning 0)
281 (match-end 0)
282 'face apropos-match-face
283 (setq doc (copy-sequence doc))))
284 doc))))
285
286(defun apropos-format-plist (pl sep &optional compare)
3925e76d
KH
287 (setq pl (symbol-plist pl))
288 (let (p p-out)
289 (while pl
290 (setq p (format "%s %S" (car pl) (nth 1 pl)))
645c4f6a
KH
291 (if (or (not compare) (string-match apropos-regexp p))
292 (if apropos-property-face
3925e76d 293 (put-text-property 0 (length (symbol-name (car pl)))
645c4f6a 294 'face apropos-property-face p))
3925e76d 295 (setq p nil))
645c4f6a
KH
296 (if p
297 (progn
298 (and compare apropos-match-face
299 (put-text-property (match-beginning 0) (match-end 0)
300 'face apropos-match-face
301 p))
302 (setq p-out (concat p-out (if p-out sep) p))))
3925e76d
KH
303 (setq pl (nthcdr 2 pl)))
304 p-out))
305
6f8e447f 306
645c4f6a 307;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
3925e76d 308
645c4f6a
KH
309(defun apropos-documentation-check-doc-file ()
310 (let (type symbol beg end)
311 (insert-file-contents (concat doc-directory internal-doc-file-name))
312 (while (re-search-forward apropos-regexp nil t)
313 (setq beg (match-beginning 0)
314 end (point))
3925e76d 315 (search-backward "\C-_")
645c4f6a
KH
316 (if (> (point) beg)
317 ()
318 (or (setq type (if (eq ?F (char-after (1+ (point))))
319 1 ;function documentation
320 2) ;variable documentation
321 symbol (prog2
322 (forward-char 2)
323 (read))
324 beg (- beg (point) 1)
325 end (- end (point) 1)
326 doc (buffer-substring
327 (1+ (point))
328 (if (search-forward "\C-_" nil 'move)
329 (1- (point))
330 (point)))
331 apropos-item (assq symbol apropos-accumulator))
332 (setq apropos-item (list symbol nil nil)
333 apropos-accumulator (cons apropos-item apropos-accumulator)))
334 (and apropos-match-face
335 (>= beg 0)
336 (put-text-property beg end 'face apropos-match-face doc))
337 (setcar (nthcdr type apropos-item) doc)))))
338
339(defun apropos-documentation-check-elc-file (file)
340 (if (member file apropos-files-scanned)
341 nil
342 (let (symbol doc beg end end1 this-is-a-variable)
343 (setq apropos-files-scanned (cons file apropos-files-scanned))
344 (erase-buffer)
345 (insert-file-contents file)
346 (while (search-forward "\n#@" nil t)
347 ;; Read the comment length, and advance over it.
348 (setq end (read)
349 beg (point)
350 end (+ (point) end 1))
351 (if (re-search-forward apropos-regexp end t)
352 (progn
353 (goto-char end)
354 (setq doc (buffer-substring (1+ beg) (- end 2))
355 end1 (- (match-end 0) beg 1)
356 beg (- (match-beginning 0) beg 1)
357 this-is-a-variable (looking-at "(defvar\\|(defconst")
358 symbol (progn
359 (skip-chars-forward "(a-z")
360 (forward-char 1)
361 (read))
362 symbol (if (consp symbol)
363 (nth 1 symbol)
364 symbol))
365 (if (if this-is-a-variable
366 (get symbol 'variable-documentation)
367 (and (fboundp symbol) (apropos-safe-documentation symbol)))
368 (progn
369 (or (setq apropos-item (assq symbol apropos-accumulator))
370 (setq apropos-item (list symbol nil nil)
371 apropos-accumulator (cons apropos-item
372 apropos-accumulator)))
373 (if apropos-match-face
374 (put-text-property beg end1 'face apropos-match-face
375 doc))
376 (setcar (nthcdr (if this-is-a-variable 2 1)
377 apropos-item)
378 doc)))))
379 (goto-char end)))))
380
381
382
383(defun apropos-safe-documentation (function)
6f8e447f
RS
384 "Like documentation, except it avoids calling `get_doc_string'.
385Will return nil instead."
3925e76d 386 (while (and function (symbolp function))
6f8e447f 387 (setq function (if (fboundp function)
3925e76d 388 (symbol-function function))))
d2e1218f
RS
389 (if (eq (car-safe function) 'macro)
390 (setq function (cdr function)))
3925e76d 391 (setq function (if (byte-code-function-p function)
645c4f6a
KH
392 (if (> (length function) 4)
393 (aref function 4))
394 (if (eq (car-safe function) 'autoload)
395 (nth 2 function)
396 (if (eq (car-safe function) 'lambda)
397 (if (stringp (nth 2 function))
398 (nth 2 function)
399 (if (stringp (nth 3 function))
400 (nth 3 function)))))))
401 (if (integerp function)
402 nil
403 function))
404
405
406
407(defun apropos-print (do-keys doc-fn spacing)
408 "Output result of various apropos commands with `apropos-regexp'.
409APROPOS-ACCUMULATOR is a list. Optional DOC-FN is called for each element
410of apropos-accumulator and may modify it resulting in (symbol fn-doc
3925e76d
KH
411var-doc [plist-doc]). Returns sorted list of symbols and documentation
412found."
645c4f6a
KH
413 (if (null apropos-accumulator)
414 (message "No apropos matches for `%s'" apropos-regexp)
3925e76d 415 (if doc-fn
645c4f6a
KH
416 (funcall doc-fn apropos-accumulator))
417 (setq apropos-accumulator
418 (sort apropos-accumulator (lambda (a b)
3925e76d 419 (string-lessp (car a) (car b)))))
645c4f6a
KH
420 (and apropos-label-face
421 (symbolp apropos-label-face)
422 (setq apropos-label-face `(face ,apropos-label-face
423 mouse-face highlight)))
3925e76d 424 (with-output-to-temp-buffer "*Help*"
645c4f6a 425 (let ((p apropos-accumulator)
3925e76d 426 (old-buffer (current-buffer))
645c4f6a 427 symbol item point1 point2)
3925e76d
KH
428 (save-excursion
429 (set-buffer standard-output)
430 (if window-system
431 (insert (substitute-command-keys
432 "Click \\<apropos-local-map>\\[apropos-mouse-follow] to get full documentation.\n")))
433 (insert (substitute-command-keys
434 "In this buffer, type \\<apropos-local-map>\\[apropos-follow] to get full documentation.\n\n"))
435 (while (consp p)
436 (or (not spacing) (bobp) (terpri))
645c4f6a
KH
437 (setq apropos-item (car p)
438 symbol (car apropos-item)
3925e76d
KH
439 p (cdr p)
440 point1 (point))
441 (princ symbol) ;print symbol name
442 (setq point2 (point))
443 ;; don't calculate key-bindings unless needed
444 (and do-keys
445 (commandp symbol)
446 (indent-to 30 1)
645c4f6a
KH
447 (insert
448 (if (setq item (save-excursion
449 (set-buffer old-buffer)
450 (where-is-internal symbol)))
451 (mapconcat
452 (if apropos-keybinding-face
453 (lambda (key)
454 (setq key (key-description key))
455 (put-text-property 0 (length key)
456 'face apropos-keybinding-face
457 key)
458 key)
459 'key-description)
460 item ", ")
461 "(not bound to any keys)")))
3925e76d
KH
462 (terpri)
463 ;; only now so we don't propagate text attributes all over
464 (put-text-property point1 (1+ point1) 'item
645c4f6a
KH
465 (if (eval `(or ,@(cdr apropos-item)))
466 (car apropos-item)
467 apropos-item))
468 (if apropos-symbol-face
469 (put-text-property point1 point2 'face apropos-symbol-face))
470 (apropos-print-doc 'describe-function 1
471 (if (commandp symbol)
472 "Command"
473 (if (apropos-macrop symbol)
474 "Macro"
475 "Function"))
476 do-keys)
477 (apropos-print-doc 'describe-variable 2
478 "Variable" do-keys)
479 (apropos-print-doc 'apropos-describe-plist 3
480 "Plist" nil))
3925e76d 481 (put-text-property 1 (point) 'local-map apropos-local-map)))))
645c4f6a
KH
482 (prog1 apropos-accumulator
483 (setq apropos-accumulator ()))) ; permit gc
484
3925e76d 485
645c4f6a
KH
486(defun apropos-macrop (symbol)
487 "T if SYMBOL is a Lisp macro."
488 (and (fboundp symbol)
489 (consp (setq symbol
490 (symbol-function symbol)))
491 (or (eq (car symbol) 'macro)
492 (if (eq (car symbol) 'autoload)
493 (memq (nth 4 symbol)
494 '(macro t))))))
3925e76d 495
645c4f6a
KH
496
497(defun apropos-print-doc (action i str do-keys)
498 (if (stringp (setq i (nth i apropos-item)))
3925e76d
KH
499 (progn
500 (insert " ")
501 (put-text-property (- (point) 2) (1- (point))
502 'action action)
645c4f6a
KH
503 (insert str ": ")
504 (if apropos-label-face
505 (add-text-properties (- (point) (length str) 2)
3925e76d 506 (1- (point))
645c4f6a
KH
507 apropos-label-face))
508 (insert (if do-keys (substitute-command-keys i) i))
509 (or (bolp) (terpri)))))
3925e76d
KH
510
511
512(defun apropos-mouse-follow (event)
513 (interactive "e")
514 (let ((other (if (eq (current-buffer) (get-buffer "*Help*"))
515 ()
516 (current-buffer))))
517 (set-buffer (window-buffer (posn-window (event-start event))))
518 (goto-char (posn-point (event-start event)))
645c4f6a 519 ;; somehow when clicking with the point in another window, undoes badly
3925e76d
KH
520 (undo-boundary)
521 (apropos-follow other)))
522
523
524(defun apropos-follow (&optional other)
525 (interactive)
526 (let ((point (point))
527 (item (get-text-property (point) 'item))
528 action action-point)
529 (or item
530 (setq item (if (bobp)
531 ()
532 (previous-single-property-change (point) 'item))
533 item (get-text-property
534 (1- (goto-char
535 (if item
536 item
537 (1+ (next-single-property-change (point) 'item)))))
538 'item)))
539 (if (consp item)
540 (error "%s is just a lonely smbol." (car item)))
541 (while (if (setq action-point
542 (next-single-property-change (point) 'action))
543 (<= action-point point))
544 (goto-char (1+ action-point))
545 (setq action action-point))
546 (funcall
547 (prog1 (get-text-property (or action action-point (point)) 'action)
548 (if other (set-buffer other)))
549 item))
550 (message "%sype %s (undo) to get back to apropos-listing."
551 (if other "In *Help* buffer t" "T")
552 (key-description (where-is-internal 'undo nil 1))))
553
554
555
556(defun apropos-describe-plist (symbol)
557 "Display a pretty listing of SYMBOL's plist."
558 (with-output-to-temp-buffer "*Help*"
559 (set-buffer standard-output)
560 (princ "Symbol ")
561 (prin1 symbol)
562 (princ "'s plist is\n (")
645c4f6a
KH
563 (if apropos-symbol-face
564 (put-text-property 8 (- (point) 14) 'face apropos-symbol-face))
3925e76d
KH
565 (insert (apropos-format-plist symbol "\n "))
566 (princ ")")))
6f8e447f 567
c0274f38 568;;; apropos.el ends here