home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / apropos.el < prev    next >
Encoding:
Text File  |  1995-05-08  |  10.1 KB  |  276 lines

  1. ;;; apropos.el --- faster apropos commands.
  2.  
  3. ;; Copyright (C) 1989, 1992 Free Software Foundation, Inc.
  4.  
  5. ;; Keywords: help
  6.  
  7. ;; This file is part of XEmacs.
  8.  
  9. ;; XEmacs is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; XEmacs is distributed in the hope that it will be useful, but
  15. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. ;; General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  21. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;;; Synched up with: Not synched with FSF.
  24.  
  25. ;; Author: Joe Wells
  26. ;; Last changed: Fri May  5 18:08:16 1989 by jbw (Joseph Wells) on bucsf
  27. ;; jbw%bucsf.bu.edu@bu-it.bu.edu (school year)
  28. ;; joew@uswest.com (summer)
  29.  
  30. ;; The ideas for this package were derived from the C code in
  31. ;; src/keymap.c and elsewhere.  The functions in this file should
  32. ;; always be byte-compiled for speed.  Someone should rewrite this in
  33. ;; C (as part of src/keymap.c) for speed.
  34.  
  35. ;; The idea for super-apropos is based on the original implementation
  36. ;; by Lynn Slater <lrs@esl.com>.
  37.  
  38. ;; History:
  39. ;; Fixed bug, current-local-map can return nil.
  40. ;; Change, doesn't calculate key-bindings unless needed.
  41. ;; Added super-apropos capability, changed print functions.
  42. ;; Made fast-apropos and super-apropos share code.
  43. ;; Sped up fast-apropos again.
  44. ;; Added apropos-do-all option.
  45. ;; Added fast-command-apropos.
  46. ;; Changed doc strings to comments for helping functions.
  47. ;; Made doc file buffer read-only, buried it.
  48. ;; Only call substitute-command-keys if do-all set.
  49.  
  50. (defvar apropos-do-all nil
  51.   "*Whether `apropos' and `super-apropos' should do everything that they can.
  52. Makes them run 2 or 3 times slower.  Set this non-nil if you have a fast
  53. machine.")
  54.  
  55. ;;;###autoload
  56. (defun apropos (regexp &optional do-all pred)
  57.   "Show all symbols whose names contain matches for REGEXP.
  58. If optional argument DO-ALL is non-nil, does more (time-consuming) work such as
  59. showing key bindings.  Optional argument PRED is called with each symbol, and
  60. if it returns nil, the symbol is not shown.  Returns list of symbols and
  61. documentation found."
  62.   (interactive "sApropos (regexp): \nP")
  63.   ;; XEmacs change: hitting ENTER by mistake is a common mess-up and
  64.   ;; shouldn't make Emacs hang for a long time trying to list all symbols.
  65.   (or (> (length regexp) 0) (error "Must pass non-empty regexp to `apropos'"))
  66.   (setq do-all (or apropos-do-all do-all))
  67.   (let ((apropos-accumulate (apropos-internal regexp pred)))
  68.     (apropos-get-doc apropos-accumulate)
  69.     (with-output-to-temp-buffer "*Help*"
  70.       (apropos-print-matches apropos-accumulate regexp nil do-all))
  71.     apropos-accumulate))
  72.  
  73. ;; If "C-h a" still has its original binding of command-apropos, change it to
  74. ;; use fast-command-apropos.  I don't use substitute-key-definition because
  75. ;; it's slow.
  76. ;(if (eq 'command-apropos (lookup-key help-map "a"))
  77. ;    (define-key help-map "a" 'fast-command-apropos))
  78.  
  79. ;; Takes LIST of symbols and adds documentation.  Modifies LIST in place.
  80. ;; Resulting alist is of form ((symbol fn-doc var-doc) ...).  Should only be
  81. ;; called by apropos.  Returns LIST.
  82.  
  83. (defun apropos-get-doc (list)
  84.   (let ((p list)
  85.     fn-doc var-doc symbol)
  86.     (while (consp p)
  87.       (setq symbol (car p)
  88.         fn-doc (and (fboundp symbol)
  89.             (or
  90.              (function-obsoleteness-doc symbol)
  91.              (documentation symbol)))
  92.         var-doc (and (boundp symbol)
  93.              (or
  94.               (variable-obsoleteness-doc symbol)
  95.               (documentation-property symbol
  96.                           '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.  
  105. ;;;###autoload
  106. (defun super-apropos (regexp &optional do-all)
  107.   "Show symbols whose names/documentation contain matches for REGEXP.
  108. If optional argument DO-ALL is non-nil, does more (time-consuming) work such as
  109. showing key bindings and documentation that is not stored in the documentation
  110. file.  Returns list of symbols and documentation found."
  111.   (interactive "sSuper Apropos: \nP")
  112.   (setq do-all (or apropos-do-all do-all))
  113.   (let (apropos-accumulate fn-doc var-doc item)
  114.     (setq apropos-accumulate (super-apropos-check-doc-file regexp))
  115.     (if do-all (mapatoms 'super-apropos-accumulate))
  116.     (with-output-to-temp-buffer "*Help*"
  117.       (apropos-print-matches apropos-accumulate nil t do-all))
  118.     apropos-accumulate))
  119.  
  120. ;; Finds all documentation related to REGEXP in internal-doc-file-name.
  121. ;; Returns an alist of form ((symbol fn-doc var-doc) ...).
  122.  
  123. (defun super-apropos-check-doc-file (regexp)
  124.   (let ((doc-buffer (find-file-noselect
  125.              (expand-file-name internal-doc-file-name exec-directory)
  126.              t))
  127.     type symbol doc sym-list)
  128.     (save-excursion
  129.       (set-buffer doc-buffer)
  130.       ;; a user said he might accidentally edit the doc file
  131.       (setq buffer-read-only t)
  132.       (bury-buffer doc-buffer)
  133.       (goto-char (point-min))
  134.       (while (re-search-forward regexp nil t)
  135.     (search-backward "\C-_")
  136.     (setq type (if (eq ?F (char-after (1+ (point))))
  137.                1        ;function documentation
  138.              2)            ;variable documentation
  139.           symbol (progn
  140.                (forward-char 2)
  141.                (read doc-buffer))
  142.           doc (buffer-substring
  143.            (point)
  144.            (progn
  145.              (if (search-forward "\C-_" nil 'move)
  146.              (1- (point))
  147.                (point))))
  148.           item (assq symbol sym-list))
  149.     (or item
  150.         (setq item (list symbol nil nil)
  151.           sym-list (cons item sym-list)))
  152.     (setcar (nthcdr type item) doc)))
  153.     sym-list))
  154.  
  155. ;; This is passed as the argument to map-atoms, so it is called once for every
  156. ;; symbol in obarray.  Takes one argument SYMBOL, and finds any memory-resident
  157. ;; documentation on that symbol if it matches a variable regexp.  WARNING: this
  158. ;; function depends on the symbols fn-doc var-doc regexp and item being bound
  159. ;; correctly when it is called!"
  160.  
  161. (defun super-apropos-accumulate (symbol)
  162.   (cond ((string-match regexp (symbol-name symbol))
  163.      (setq item (apropos-get-accum-item symbol))
  164.      (setcar (cdr item) (or (safe-documentation symbol)
  165.                 (nth 1 item)))
  166.      (setcar (nthcdr 2 item) (or (safe-documentation-property symbol)
  167.                      (nth 2 item))))
  168.     (t
  169.      (and (setq fn-doc (safe-documentation symbol))
  170.           (string-match regexp fn-doc)
  171.           (setcar (cdr (apropos-get-accum-item symbol)) fn-doc))
  172.      (and (setq var-doc (safe-documentation-property symbol))
  173.           (string-match regexp var-doc)
  174.           (setcar (nthcdr 2 (apropos-get-accum-item symbol)) var-doc))))
  175.   nil)
  176.  
  177. ;; Prints the symbols and documentation in alist MATCHES of form ((symbol
  178. ;; fn-doc var-doc) ...).  Uses optional argument REGEXP to speed up searching
  179. ;; for keybindings.  The names of all symbols in MATCHES must match REGEXP.
  180. ;; Displays in the buffer pointed to by standard-output.  Optional argument
  181. ;; SPACING means put blank lines in between each symbol's documentation.
  182. ;; Optional argument DO-ALL means do more time-consuming work, specifically,
  183. ;; consulting key bindings.  Should only be called within a
  184. ;; with-output-to-temp-buffer.
  185.  
  186. (defun apropos-print-matches (matches &optional regexp spacing do-all)
  187.   (setq matches (sort matches (function
  188.                    (lambda (a b)
  189.                  (string-lessp (car a) (car b))))))
  190.   (let ((p matches)
  191.     (old-buffer (current-buffer))
  192.     item keys-done symbol)
  193.     (save-excursion
  194.       (set-buffer standard-output)
  195.       (or matches (princ "No matches found."))
  196.       (while (consp p)
  197.     (setq item (car p)
  198.           symbol (car item)
  199.           p (cdr p))
  200.     (or (not spacing) (bobp) (terpri))
  201.     (princ symbol)                ;print symbol name
  202.     ;; don't calculate key-bindings unless needed
  203.     (cond ((and do-all (commandp symbol) (not keys-done))
  204.            (save-excursion
  205.          (set-buffer old-buffer)
  206.          (apropos-match-keys matches regexp))
  207.            (setq keys-done t)))
  208.     (cond ((and do-all
  209.             (or (setq tem (nthcdr 3 item))
  210.             (commandp symbol)))
  211.            (indent-to 30 1)
  212.            (if tem
  213.            (princ (mapconcat 'key-description tem ", "))
  214.          (princ "(not bound to any keys)"))))
  215.     (terpri)
  216.     (cond ((setq tem (nth 1 item))
  217.            (princ "  Function: ")
  218.            (princ (if do-all (substitute-command-keys tem) tem))))
  219.     (or (bolp) (terpri))
  220.     (cond ((setq tem (nth 2 item))
  221.            (princ "  Variable: ")
  222.            (princ (if do-all (substitute-command-keys tem) tem))))
  223.     (or (bolp) (terpri)))))
  224.   t)
  225.  
  226. ;; Find key bindings for symbols that are cars in ALIST.  Optionally, first
  227. ;; match the symbol name against REGEXP.  Modifies ALIST in place.  Each key
  228. ;; binding is added as a string to the end of the list in ALIST whose car is
  229. ;; the corresponding symbol.  The pointer to ALIST is returned.
  230.  
  231. (defun apropos-match-keys (alist &optional regexp)
  232.   ;; #### REGEXP isn't necessary any more, right?
  233.   (let ((rest alist))
  234.     (while rest
  235.       (nconc (car rest) (where-is-internal (car (car rest))))
  236.       (setq rest (cdr rest)))
  237.     alist))
  238.  
  239.  
  240. ;; Get an alist item in alist apropos-accumulate whose car is SYMBOL.  Creates
  241. ;; the item if not already present.  Modifies apropos-accumulate in place.
  242.  
  243. (defun apropos-get-accum-item (symbol)
  244.   (or (assq symbol apropos-accumulate)
  245.       (progn
  246.     (setq apropos-accumulate
  247.           (cons (list symbol nil nil) apropos-accumulate))
  248.     (assq symbol apropos-accumulate))))
  249.  
  250. (defun safe-documentation (function)
  251.   "Like documentation, except it avoids calling `get_doc_string'.
  252. Will return nil instead."
  253.   (while (symbolp function)
  254.     (setq function (if (fboundp function)
  255.                (symbol-function function)
  256.              0)))
  257.   (if (not (consp function))
  258.       nil
  259.     (if (eq (car function) 'macro)
  260.     (setq function (cdr function)))
  261.     (if (not (and (consp function) (memq (car function) '(lambda autoload))))
  262.     nil
  263.       (setq function (nth 2 function))
  264.       (if (stringp function)
  265.       function
  266.     nil))))
  267.  
  268. (defun safe-documentation-property (symbol)
  269.   "Like documentation-property, except it avoids calling `get_doc_string'.
  270. Will return nil instead."
  271.   (setq symbol (get symbol 'variable-documentation))
  272.   (if (numberp symbol)
  273.       nil
  274.     symbol))
  275.  
  276.