(ensure-mark): Minor rewrite.
[emacs.git] / lisp / apropos.el
blob6dd736e880e580f1765298ba7adf6140dff61f8a
1 ;;; apropos.el --- apropos commands for users and programmers.
3 ;; Copyright (C) 1989, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Joe Wells <jbw@bigbird.bu.edu>
6 ;; Rewritten: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
7 ;; Keywords: help
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
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.
33 ;; The idea for super-apropos is based on the original implementation
34 ;; by Lynn Slater <lrs@esl.com>.
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.
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.
58 ;;; Code:
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.")
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.")
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.")
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.")
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.")
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.")
90 (defvar apropos-mode-map
91 (let ((map (make-sparse-keymap)))
92 (define-key map "\C-m" 'apropos-follow)
93 (define-key map [mouse-2] 'apropos-mouse-follow)
94 (define-key map [down-mouse-2] nil)
95 map)
96 "Keymap used in Apropos mode.")
99 (defvar apropos-regexp nil
100 "Regexp used in current apropos run.")
102 (defvar apropos-files-scanned ()
103 "List of elc files already scanned in current run of `apropos-documentation'.")
105 (defvar apropos-accumulator ()
106 "Alist of symbols already found in current apropos run.")
108 (defvar apropos-item ()
109 "Current item in or for apropos-accumulator.")
111 (defun apropos-mode ()
112 "Major mode for following hyperlinks in output of apropos commands.
114 \\{apropos-mode-map}"
115 (interactive)
116 (kill-all-local-variables)
117 (use-local-map apropos-mode-map)
118 (view-mode)
119 (setq major-mode 'apropos-mode
120 mode-name "Apropos"))
123 ;; For auld lang syne:
124 ;;;###autoload
125 (fset 'command-apropos 'apropos-command)
126 ;;;###autoload
127 (defun apropos-command (apropos-regexp &optional do-all)
128 "Shows commands (interactively callable functions) that match REGEXP.
129 With optional prefix ARG or if `apropos-do-all' is non-nil, also show
130 variables."
131 (interactive (list (read-string (concat "Apropos command "
132 (if (or current-prefix-arg
133 apropos-do-all)
134 "or variable ")
135 "(regexp): "))
136 current-prefix-arg))
137 (let ((message
138 (let ((standard-output (get-buffer-create "*Apropos*")))
139 (print-help-return-message 'identity))))
140 (or do-all (setq do-all apropos-do-all))
141 (setq apropos-accumulator
142 (apropos-internal apropos-regexp
143 (if do-all
144 (lambda (symbol) (or (commandp symbol)
145 (user-variable-p symbol)))
146 'commandp)))
147 (if (apropos-print
149 (lambda (p)
150 (let (doc symbol)
151 (while p
152 (setcar p (list
153 (setq symbol (car p))
154 (if (commandp symbol)
155 (if (setq doc (documentation symbol t))
156 (substring doc 0 (string-match "\n" doc))
157 "(not documented)"))
158 (and do-all
159 (user-variable-p symbol)
160 (if (setq doc (documentation-property
161 symbol 'variable-documentation t))
162 (substring doc 0
163 (string-match "\n" doc))))))
164 (setq p (cdr p)))))
165 nil)
166 (and message (message message)))))
169 ;;;###autoload
170 (defun apropos (apropos-regexp &optional do-all)
171 "Show all bound symbols whose names match REGEXP.
172 With optional prefix ARG or if `apropos-do-all' is non-nil, also show unbound
173 symbols and key bindings, which is a little more time-consuming.
174 Returns list of symbols and documentation found."
175 (interactive "sApropos symbol (regexp): \nP")
176 (setq apropos-accumulator
177 (apropos-internal apropos-regexp
178 (and (not do-all)
179 (not apropos-do-all)
180 (lambda (symbol)
181 (or (fboundp symbol)
182 (boundp symbol)
183 (symbol-plist symbol))))))
184 (apropos-print
185 (or do-all apropos-do-all)
186 (lambda (p)
187 (let (symbol doc)
188 (while p
189 (setcar p (list
190 (setq symbol (car p))
191 (if (fboundp symbol)
192 (if (setq doc (documentation symbol t))
193 (substring doc 0 (string-match "\n" doc))
194 "(not documented)"))
195 (if (boundp symbol)
196 (if (setq doc (documentation-property
197 symbol 'variable-documentation t))
198 (substring doc 0
199 (string-match "\n" doc))
200 "(not documented)"))
201 (if (setq doc (symbol-plist symbol))
202 (if (eq (/ (length doc) 2) 1)
203 (format "1 property (%s)" (car doc))
204 (concat (/ (length doc) 2) " properties")))))
205 (setq p (cdr p)))))
206 nil))
209 ;;;###autoload
210 (defun apropos-value (apropos-regexp &optional do-all)
211 "Show all symbols whose value's printed image matches REGEXP.
212 With optional prefix ARG or if `apropos-do-all' is non-nil, also looks
213 at the function and at the names and values of properties.
214 Returns list of symbols and values found."
215 (interactive "sApropos value (regexp): \nP")
216 (or do-all (setq do-all apropos-do-all))
217 (setq apropos-accumulator ())
218 (let (f v p)
219 (mapatoms
220 (lambda (symbol)
221 (setq f nil v nil p nil)
222 (or (memq symbol '(apropos-regexp do-all apropos-accumulator
223 symbol f v p))
224 (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
225 (if do-all
226 (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
227 p (apropos-format-plist symbol "\n " t)))
228 (if (or f v p)
229 (setq apropos-accumulator (cons (list symbol f v p)
230 apropos-accumulator))))))
231 (apropos-print nil nil t))
234 ;;;###autoload
235 (defun apropos-documentation (apropos-regexp &optional do-all)
236 "Show symbols whose documentation contain matches for REGEXP.
237 With optional prefix ARG or if `apropos-do-all' is non-nil, also use
238 documentation that is not stored in the documentation file and show key
239 bindings.
240 Returns list of symbols and documentation found."
241 (interactive "sApropos documentation (regexp): \nP")
242 (or do-all (setq do-all apropos-do-all))
243 (setq apropos-accumulator () apropos-files-scanned ())
244 (let ((standard-input (get-buffer-create " apropos-temp"))
245 f v)
246 (unwind-protect
247 (save-excursion
248 (set-buffer standard-input)
249 (apropos-documentation-check-doc-file)
250 (if do-all
251 (mapatoms
252 (lambda (symbol)
253 (setq f (apropos-safe-documentation symbol)
254 v (get symbol 'variable-documentation))
255 (if (integerp v) (setq v))
256 (setq f (apropos-documentation-internal f)
257 v (apropos-documentation-internal v))
258 (if (or f v)
259 (if (setq apropos-item
260 (cdr (assq symbol apropos-accumulator)))
261 (progn
262 (if f
263 (setcar apropos-item f))
264 (if v
265 (setcar (cdr apropos-item) v)))
266 (setq apropos-accumulator
267 (cons (list symbol f v)
268 apropos-accumulator)))))))
269 (apropos-print nil nil t))
270 (kill-buffer standard-input))))
273 (defun apropos-value-internal (predicate symbol function)
274 (if (funcall predicate symbol)
275 (progn
276 (setq symbol (prin1-to-string (funcall function symbol)))
277 (if (string-match apropos-regexp symbol)
278 (progn
279 (if apropos-match-face
280 (put-text-property (match-beginning 0) (match-end 0)
281 'face apropos-match-face
282 symbol))
283 symbol)))))
285 (defun apropos-documentation-internal (doc)
286 (if (consp doc)
287 (apropos-documentation-check-elc-file (car doc))
288 (and doc
289 (string-match apropos-regexp doc)
290 (progn
291 (if apropos-match-face
292 (put-text-property (match-beginning 0)
293 (match-end 0)
294 'face apropos-match-face
295 (setq doc (copy-sequence doc))))
296 doc))))
298 (defun apropos-format-plist (pl sep &optional compare)
299 (setq pl (symbol-plist pl))
300 (let (p p-out)
301 (while pl
302 (setq p (format "%s %S" (car pl) (nth 1 pl)))
303 (if (or (not compare) (string-match apropos-regexp p))
304 (if apropos-property-face
305 (put-text-property 0 (length (symbol-name (car pl)))
306 'face apropos-property-face p))
307 (setq p nil))
308 (if p
309 (progn
310 (and compare apropos-match-face
311 (put-text-property (match-beginning 0) (match-end 0)
312 'face apropos-match-face
314 (setq p-out (concat p-out (if p-out sep) p))))
315 (setq pl (nthcdr 2 pl)))
316 p-out))
319 ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
321 (defun apropos-documentation-check-doc-file ()
322 (let (type symbol (sepa 2) sepb beg end)
323 (insert ?\^_)
324 (backward-char)
325 (insert-file-contents (concat doc-directory internal-doc-file-name))
326 (forward-char)
327 (while (save-excursion
328 (setq sepb (search-forward "\^_"))
329 (not (eobp)))
330 (beginning-of-line 2)
331 (if (save-restriction
332 (narrow-to-region (point) (1- sepb))
333 (re-search-forward apropos-regexp nil t))
334 (progn
335 (setq beg (match-beginning 0)
336 end (point))
337 (goto-char (1+ sepa))
338 (or (setq type (if (eq ?F (preceding-char))
339 1 ; function documentation
340 2) ; variable documentation
341 symbol (read)
342 beg (- beg (point) 1)
343 end (- end (point) 1)
344 doc (buffer-substring (1+ (point)) (1- sepb))
345 apropos-item (assq symbol apropos-accumulator))
346 (setq apropos-item (list symbol nil nil)
347 apropos-accumulator (cons apropos-item
348 apropos-accumulator)))
349 (if apropos-match-face
350 (put-text-property beg end 'face apropos-match-face doc))
351 (setcar (nthcdr type apropos-item) doc)))
352 (setq sepa (goto-char sepb)))))
354 (defun apropos-documentation-check-elc-file (file)
355 (if (member file apropos-files-scanned)
357 (let (symbol doc beg end this-is-a-variable)
358 (setq apropos-files-scanned (cons file apropos-files-scanned))
359 (erase-buffer)
360 (insert-file-contents file)
361 (while (search-forward "\n#@" nil t)
362 ;; Read the comment length, and advance over it.
363 (setq end (read)
364 beg (1+ (point))
365 end (+ (point) end -1))
366 (forward-char)
367 (if (save-restriction
368 ;; match ^ and $ relative to doc string
369 (narrow-to-region beg end)
370 (re-search-forward apropos-regexp nil t))
371 (progn
372 (goto-char (+ end 2))
373 (setq doc (buffer-substring beg end)
374 end (- (match-end 0) beg)
375 beg (- (match-beginning 0) beg)
376 this-is-a-variable (looking-at "(def\\(var\\|const\\) ")
377 symbol (progn
378 (skip-chars-forward "(a-z")
379 (forward-char)
380 (read))
381 symbol (if (consp symbol)
382 (nth 1 symbol)
383 symbol))
384 (if (if this-is-a-variable
385 (get symbol 'variable-documentation)
386 (and (fboundp symbol) (apropos-safe-documentation symbol)))
387 (progn
388 (or (setq apropos-item (assq symbol apropos-accumulator))
389 (setq apropos-item (list symbol nil nil)
390 apropos-accumulator (cons apropos-item
391 apropos-accumulator)))
392 (if apropos-match-face
393 (put-text-property beg end 'face apropos-match-face
394 doc))
395 (setcar (nthcdr (if this-is-a-variable 2 1)
396 apropos-item)
397 doc)))))))))
401 (defun apropos-safe-documentation (function)
402 "Like documentation, except it avoids calling `get_doc_string'.
403 Will return nil instead."
404 (while (and function (symbolp function))
405 (setq function (if (fboundp function)
406 (symbol-function function))))
407 (if (eq (car-safe function) 'macro)
408 (setq function (cdr function)))
409 (setq function (if (byte-code-function-p function)
410 (if (> (length function) 4)
411 (aref function 4))
412 (if (eq (car-safe function) 'autoload)
413 (nth 2 function)
414 (if (eq (car-safe function) 'lambda)
415 (if (stringp (nth 2 function))
416 (nth 2 function)
417 (if (stringp (nth 3 function))
418 (nth 3 function)))))))
419 (if (integerp function)
421 function))
425 (defun apropos-print (do-keys doc-fn spacing)
426 "Output result of various apropos commands with `apropos-regexp'.
427 APROPOS-ACCUMULATOR is a list. Optional DOC-FN is called for each element
428 of apropos-accumulator and may modify it resulting in (symbol fn-doc
429 var-doc [plist-doc]). Returns sorted list of symbols and documentation
430 found."
431 (if (null apropos-accumulator)
432 (message "No apropos matches for `%s'" apropos-regexp)
433 (if doc-fn
434 (funcall doc-fn apropos-accumulator))
435 (setq apropos-accumulator
436 (sort apropos-accumulator (lambda (a b)
437 (string-lessp (car a) (car b)))))
438 (and apropos-label-face
439 (symbolp apropos-label-face)
440 (setq apropos-label-face `(face ,apropos-label-face
441 mouse-face highlight)))
442 (with-output-to-temp-buffer "*Apropos*"
443 (let ((p apropos-accumulator)
444 (old-buffer (current-buffer))
445 symbol item point1 point2)
446 (set-buffer standard-output)
447 (apropos-mode)
448 (if window-system
449 (insert "If you move the mouse over text that changes color,\n"
450 (substitute-command-keys
451 "you can click \\[apropos-mouse-follow] to get more information.\n")))
452 (insert (substitute-command-keys
453 "In this buffer, type \\[apropos-follow] to get full documentation.\n\n"))
454 (while (consp p)
455 (or (not spacing) (bobp) (terpri))
456 (setq apropos-item (car p)
457 symbol (car apropos-item)
458 p (cdr p)
459 point1 (point))
460 (princ symbol) ; print symbol name
461 (setq point2 (point))
462 ;; Calculate key-bindings if we want them.
463 (and do-keys
464 (commandp symbol)
465 (indent-to 30 1)
466 (if (let ((keys
467 (save-excursion
468 (set-buffer old-buffer)
469 (where-is-internal symbol)))
470 filtered)
471 ;; Copy over the list of key sequences,
472 ;; omitting any that contain a buffer or a frame.
473 (while keys
474 (let ((key (car keys))
475 (i 0)
476 loser)
477 (while (< i (length key))
478 (if (or (framep (aref key i))
479 (bufferp (aref key i)))
480 (setq loser t))
481 (setq i (1+ i)))
482 (or loser
483 (setq filtered (cons key filtered))))
484 (setq keys (cdr keys)))
485 (setq item filtered))
486 ;; Convert the remaining keys to a string and insert.
487 (insert
488 (mapconcat
489 (lambda (key)
490 (setq key (key-description key))
491 (if apropos-keybinding-face
492 (put-text-property 0 (length key)
493 'face apropos-keybinding-face
494 key))
495 key)
496 item ", "))
497 (insert "Type ")
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
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))))))
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)))))
554 (defun apropos-mouse-follow (event)
555 (interactive "e")
556 (let ((other (if (eq (current-buffer) (get-buffer "*Apropos*"))
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))))
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)))
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)))
603 ;;; apropos.el ends here