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