home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / misc / fast-apropos.el < prev    next >
Encoding:
Text File  |  1991-06-03  |  14.2 KB  |  362 lines

  1. ; Path: utkcs2!emory!att!bu.edu!nntp-read!jbw
  2. ; From: jbw@bigbird.bu.edu (Joe Wells)
  3. ; Newsgroups: comp.emacs,gnu.emacs.sources
  4. ; Subject: Re: fast-apropos.el.Z:  corrupted?  Alternate source?
  5. ; Date: 17 Apr 91 16:41:23 GMT
  6. ; Organization: Boston University Computer Science Department
  7. ; Here's the latest version, which has some small bug fixes over the last
  8. ; posted version.
  9. ; -- 
  10. ; Enjoy,
  11. ; Joe Wells <jbw@cs.bu.edu>
  12. ; ----------------------------------------------------------------------
  13. ;; Faster apropos commands.
  14. ;; Copyright (C) 1989 Free Software Foundation, Inc.
  15.  
  16. ;; This file is not officially part of GNU Emacs, but is being donated
  17. ;; to the Free Software Foundation.
  18.  
  19. ;; GNU Emacs is distributed in the hope that it will be useful,
  20. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  21. ;; accepts responsibility to anyone for the consequences of using it
  22. ;; or for whether it serves any particular purpose or works at all,
  23. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  24. ;; License for full details.
  25.  
  26. ;; Everyone is granted permission to copy, modify and redistribute
  27. ;; GNU Emacs, but only under the conditions described in the
  28. ;; GNU Emacs General Public License.   A copy of this license is
  29. ;; supposed to have been given to you along with GNU Emacs so you
  30. ;; can know your rights and responsibilities.  It should be in a
  31. ;; file named COPYING.  Among other things, the copyright notice
  32. ;; and this notice must be preserved on all copies.
  33.  
  34. ;; Created by: Joseph Wells, jbw@bucsf.bu.edu
  35. ;; Created on: Mon Sep 24 17:16:38 1990
  36. ;; Last modified by: David Brennan
  37. ;; Last modified on: Tue Jun  4 15:33:54 1991
  38. ;; Filename: fast-apropos.el
  39. ;; Purpose: Replacement for old slow apropos
  40. ;; Change log: 
  41. ;; 
  42. ;; Mon Sep 24 17:17:00 1990  Joseph Wells  (jbw at bucsf.bu.edu)
  43. ;; 
  44. ;;     * Robert Potter <rpotter@grip.cis.upenn.edu> noticed that key
  45. ;;     bindings were not printed out correctly with super-apropos.
  46. ;;     apropos-match-keys refused to find any bindings unless regexp was
  47. ;;     non-nil.  Simple fix.
  48. ;; 
  49.  
  50. ;; The ideas for this package were derived from the C code in
  51. ;; src/keymap.c and elsewhere.  The functions in this file should
  52. ;; always be byte-compiled for speed.  Someone should rewrite this in
  53. ;; C (as part of src/keymap.c) for speed.
  54.  
  55. ;; The idea for super-apropos is based on the original implementation
  56. ;; by Lynn Slater <lrs@esl.com>.
  57.  
  58. ;; Old History:
  59. ;; Fixed bug, current-local-map can return nil.
  60. ;; Change, doesn't calculate key-bindings unless needed.
  61. ;; Added super-apropos capability, changed print functions.
  62. ;; Made fast-apropos and super-apropos share code.
  63. ;; Sped up fast-apropos again.
  64. ;; Added apropos-do-all option.
  65. ;; Added fast-command-apropos.
  66. ;; Changed doc strings to comments for helping functions.
  67. ;; Made doc file buffer read-only, buried it.
  68. ;; Only call substitute-command-keys if do-all set.
  69. ;; Added installation instructions
  70.  
  71. ;; If you are using this package before GNU Emacs version 19, you must
  72. ;; install it properly.  Put the file in a directory that is included in
  73. ;; the Lisp variable load-path.  Then put the following statements either
  74. ;; in your .emacs file or in the system-wide lisp/defaults.el file:
  75. ;;
  76. ;;   (autoload 'fast-apropos "fast-apropos" nil t)
  77. ;;   (autoload 'fast-command-apropos "fast-apropos" nil t)
  78. ;;   (autoload 'super-apropos "fast-apropos" nil t)
  79. ;;   (define-key help-map "a" 'fast-command-apropos)
  80.  
  81. (defvar apropos-do-all nil
  82.   "*Whether fast-apropos and super-apropos should do everything that they can.
  83. Makes them run 2 or 3 times slower.  Set this non-nil if you have a fast
  84. machine.")
  85.  
  86. ;; If there isn't already a lisp variable named internal-doc-file-name, create
  87. ;; it and document it.  This is so the code will work right after RMS adds
  88. ;; internal-doc-file-name.
  89. (or (boundp 'internal-doc-file-name)
  90.     (setq internal-doc-file-name (concat exec-directory "DOC")))
  91. (or (documentation-property 'internal-doc-file-name 'variable-documentation)
  92.     (put 'internal-doc-file-name 'variable-documentation
  93.      "The complete pathname of the documentation file that contains all
  94. documentation for functions and variables defined before Emacs is dumped."))
  95.  
  96. (defun fast-apropos (regexp &optional do-all pred)
  97.   "Show all symbols whose names contain matches for REGEXP.
  98. If optional argument DO-ALL is non-nil, does more (time-consuming) work such as
  99. showing key bindings.  Optional argument PRED is called with each symbol, and
  100. if it returns nil, the symbol is not shown.  Returns list of symbols and
  101. documentation found."
  102.   (interactive "sFast Apropos (regexp): \nP")
  103.   (setq do-all (or apropos-do-all do-all))
  104.   (let ((apropos-accumulate (apropos regexp pred t)))
  105.     (fast-apropos-get-doc apropos-accumulate)
  106.     (with-output-to-temp-buffer "*Help*"
  107.       (apropos-print-matches apropos-accumulate regexp nil do-all))
  108.     apropos-accumulate))
  109.  
  110. (defun fast-command-apropos (regexp)
  111.   "Like fast-apropos but lists only symbols that are names of commands
  112. \(interactively callable functions\).  Argument REGEXP is a regular expression
  113. that is matched against command symbol names.  Returns list of symbols and
  114. documentation found."
  115.   (interactive "sFast Command Apropos (regexp): ")
  116.   (let* ((message
  117.       (let ((standard-output (get-buffer-create "*Help*")))
  118.         (print-help-return-message 'identity)))
  119.      (result (fast-apropos regexp t 'commandp)))
  120.     (and message (message message))
  121.     result))
  122.  
  123. ;; If "C-h a" still has its original binding of command-apropos, change it to
  124. ;; use fast-command-apropos.  I don't use substitute-key-definition because
  125. ;; it's slow.
  126. (if (eq 'command-apropos (lookup-key help-map "a"))
  127.     (define-key help-map "a" 'fast-command-apropos))
  128.  
  129. (defun fast-apropos-get-doc (list)
  130.   "Helping function for fast-apropos."
  131. ;; Takes LIST of symbols and adds documentation.  Modifies LIST in place.
  132. ;; Resulting alist is of form ((symbol fn-doc var-doc) ...).  Should only be
  133. ;; called by fast-apropos.  Returns LIST.
  134.   (let ((p list)
  135.     fn-doc var-doc symbol)
  136.     (while (consp p)
  137.       (setq symbol (car p)
  138.         fn-doc (and (fboundp symbol)
  139.             (documentation symbol))
  140.         var-doc (documentation-property symbol 'variable-documentation)
  141.         fn-doc (and fn-doc
  142.             (substring fn-doc 0 (string-match "\n" fn-doc)))
  143.         var-doc (and var-doc
  144.              (substring var-doc 0 (string-match "\n" var-doc))))
  145.       (setcar p (list symbol fn-doc var-doc))
  146.       (setq p (cdr p)))
  147.     list))
  148.  
  149. (defun super-apropos (regexp &optional do-all)
  150.   "Show symbols whose names/documentation contain matches for REGEXP.
  151. If optional argument DO-ALL is non-nil, does more (time-consuming) work such as
  152. showing key bindings and documentation that is not stored in the documentation
  153. file.  Returns list of symbols and documentation found."
  154.   (interactive "sSuper Apropos: \nP")
  155.   (setq do-all (or apropos-do-all do-all))
  156.   (let (apropos-accumulate fn-doc var-doc item)
  157.     (setq apropos-accumulate (super-apropos-check-doc-file regexp))
  158.     (if do-all (mapatoms 'super-apropos-accumulate))
  159.     (with-output-to-temp-buffer "*Help*"
  160.       (apropos-print-matches apropos-accumulate nil t do-all))
  161.     apropos-accumulate))
  162.  
  163. (defun super-apropos-check-doc-file (regexp)
  164.   "Helping function for super-apropos."
  165. ;; Finds all documentation related to REGEXP in internal-doc-file-name.
  166. ;; Returns an alist of form ((symbol fn-doc var-doc) ...).
  167.   (let ((doc-buffer (find-file-noselect internal-doc-file-name t))
  168.     ;;    (doc-buffer (or (get-file-buffer internal-doc-file-name)
  169.     ;;            (find-file-noselect internal-doc-file-name)))
  170.     type symbol doc sym-list)
  171.     (save-excursion
  172.       (set-buffer doc-buffer)
  173.       ;; a user said he might accidentally edit the doc file
  174.       (setq buffer-read-only t)
  175.       (bury-buffer doc-buffer)
  176.       (goto-char (point-min))
  177.       (while (re-search-forward regexp nil t)
  178.     (search-backward "\C-_")
  179.     (setq type (if (eq ?F (char-after (1+ (point))))
  180.                1        ;function documentation
  181.              2)            ;variable documentation
  182.           symbol (progn
  183.                (forward-char 2)
  184.                (read doc-buffer))
  185.           doc (buffer-substring
  186.            (point)
  187.            (progn
  188.              (if (search-forward "\C-_" nil 'move)
  189.              (1- (point))
  190.                (point))))
  191.           item (assq symbol sym-list))
  192.     (or item
  193.         (setq item (list symbol nil nil)
  194.           sym-list (cons item sym-list)))
  195.     (setcar (nthcdr type item) doc)))
  196.     sym-list))
  197.  
  198. (defun super-apropos-accumulate (symbol)
  199.   "Helping function for super-apropos."
  200. ;; This is passed as the argument to map-atoms, so it is called once for every
  201. ;; symbol in obarray.  Takes one argument SYMBOL, and finds any memory-resident
  202. ;; documentation on that symbol if it matches a variable regexp.  WARNING: this
  203. ;; function depends on the symbols fn-doc var-doc regexp and item being bound
  204. ;; correctly when it is called!"
  205.   (cond ((string-match regexp (symbol-name symbol))
  206.      (setq item (apropos-get-accum-item symbol))
  207.      (setcar (cdr item) (or (safe-documentation symbol)
  208.                 (nth 1 item)))
  209.      (setcar (nthcdr 2 item) (or (safe-documentation-property symbol)
  210.                      (nth 2 item))))
  211.     (t
  212.      (and (setq fn-doc (safe-documentation symbol))
  213.           (string-match regexp fn-doc)
  214.           (setcar (cdr (apropos-get-accum-item symbol)) fn-doc))
  215.      (and (setq var-doc (safe-documentation-property symbol))
  216.           (string-match regexp var-doc)
  217.           (setcar (nthcdr 2 (apropos-get-accum-item symbol)) var-doc))))
  218.   nil)
  219.  
  220. (defun apropos-print-matches (matches &optional regexp spacing do-all)
  221.   "Helping function for fast-apropos and super-apropos."
  222. ;; Prints the symbols and documentation in alist MATCHES of form ((symbol
  223. ;; fn-doc var-doc) ...).  Uses optional argument REGEXP to speed up searching
  224. ;; for keybindings.  The names of all symbols in MATCHES must match REGEXP.
  225. ;; Displays in the buffer pointed to by standard-output.  Optional argument
  226. ;; SPACING means put blank lines in between each symbol's documentation.
  227. ;; Optional argument DO-ALL means do more time-consuming work, specifically,
  228. ;; consulting key bindings.  Should only be called within a
  229. ;; with-output-to-temp-buffer.
  230.   (setq matches (sort matches (function
  231.                    (lambda (a b)
  232.                  (string-lessp (car a) (car b))))))
  233.   (let ((p matches)
  234.     (old-buffer (current-buffer))
  235.     item keys-done symbol)
  236.     (save-excursion
  237.       (set-buffer standard-output)
  238.       (or matches (princ "No matches found."))
  239.       (while (consp p)
  240.     (setq item (car p)
  241.           symbol (car item)
  242.           p (cdr p))
  243.     (or (not spacing) (bobp) (terpri))
  244.     (princ symbol)                ;print symbol name
  245.     ;; don't calculate key-bindings unless needed
  246.     (cond ((and do-all (commandp symbol) (not keys-done))
  247.            (save-excursion
  248.          (set-buffer old-buffer)
  249.          (apropos-match-keys matches regexp))
  250.            (setq keys-done t)))
  251.     (cond ((and do-all
  252.             (or (setq tem (nthcdr 3 item))
  253.             (commandp symbol)))
  254.            (indent-to 30 1)
  255.            (if tem
  256.            (princ (mapconcat 'key-description tem ", "))
  257.          (princ "(not bound to any keys)"))))
  258.     (terpri)
  259.     (cond ((setq tem (nth 1 item))
  260.            (princ "  Function: ")
  261.            (princ (if do-all (substitute-command-keys tem) tem))))
  262.     (or (bolp) (terpri))
  263.     (cond ((setq tem (nth 2 item))
  264.            (princ "  Variable: ")
  265.            (princ (if do-all (substitute-command-keys tem) tem))))
  266.     (or (bolp) (terpri)))))
  267.   t)
  268.  
  269. (defun apropos-match-keys (alist &optional regexp)
  270.   "Helping function for apropos-print-matches."
  271. ;; Find key bindings for symbols that are cars in ALIST.  Optionally, first
  272. ;; match the symbol name against REGEXP.  Modifies ALIST in place.  Each key
  273. ;; binding is added as a string to the end of the list in ALIST whose car is
  274. ;; the corresponding symbol.  The pointer to ALIST is returned.
  275.   (let* ((current-local-map (current-local-map))
  276.      (maps (append (and current-local-map
  277.                 (accessible-keymaps current-local-map))
  278.                (accessible-keymaps (current-global-map))))
  279.      map                ;map we are now inspecting
  280.      sequence            ;key sequence to reach map
  281.      i                ;index into vector map
  282.      command            ;what is bound to current keys
  283.      key                ;last key to reach command
  284.      local                ;local binding for sequence + key
  285.      item)                ;symbol data item in alist
  286.     ;; examine all reachable keymaps
  287.     (while (consp maps)
  288.       (setq map (cdr (car maps))
  289.         sequence (car (car maps))    ;keys to reach this map
  290.         maps (cdr maps))
  291.       (cond ((consp map)
  292.          (setq map (cdr map))))    ;skip keymap symbol
  293.       (setq i 0)
  294.       (while (and map (< i 128))    ;vector keymaps have 128 entries
  295.     (cond ((consp map)
  296.            (setq command (cdr (car map))
  297.              key (car (car map))
  298.              map (cdr map)))
  299.           ((vectorp map)
  300.            (setq command (aref map i)
  301.              key i
  302.              i (1+ i))))
  303.     ;; if is a symbol, and matches optional regexp, and is a car
  304.     ;; in alist, and is not shadowed by a different local binding,
  305.     ;; record it
  306.     (and (symbolp command)
  307.          (or (null regexp)
  308.          (string-match regexp (symbol-name command)))
  309.          (setq item (assq command alist))
  310.          (setq key (concat sequence (char-to-string key)))
  311.          ;; checking if shadowed by local binding.
  312.          ;; either no local map, no local binding, or runs off the
  313.          ;; binding tree (number), or is the same binding
  314.          (or (not current-local-map)
  315.          (not (setq local (lookup-key current-local-map key)))
  316.          (numberp local)
  317.          (eq command local))
  318.          ;; add this key binding to the item in alist
  319.          (nconc item (cons key nil))))))
  320.   alist)
  321.  
  322. (defun apropos-get-accum-item (symbol)
  323.   "Helping function for super-apropos-accumulate."
  324. ;; Get an alist item in alist apropos-accumulate whose car is SYMBOL.  Creates
  325. ;; the item if not already present.  Modifies apropos-accumulate in place.
  326.   (or (assq symbol apropos-accumulate)
  327.       (progn
  328.     (setq apropos-accumulate
  329.           (cons (list symbol nil nil) apropos-accumulate))
  330.     (assq symbol apropos-accumulate))))
  331.  
  332. (defun safe-documentation (function)
  333.   "Like documentation, except it avoids calling get_doc_string().
  334. Will return nil instead."
  335.   (while (symbolp function)
  336.     (setq function (if (fboundp function)
  337.                (symbol-function function)
  338.              0)))
  339.   (if (not (consp function))
  340.       nil
  341.     (if (eq (car function) 'macro)
  342.     (setq function (cdr function)))
  343.     (if (not (memq (car function) '(lambda autoload)))
  344.     nil
  345.       (setq function (nth 2 function))
  346.       (if (stringp function)
  347.       function
  348.     nil))))
  349.  
  350. (defun safe-documentation-property (symbol)
  351.   "Like documentation-property, except it avoids calling get_doc_string().
  352. Will return nil instead."
  353.   (setq symbol (get symbol 'variable-documentation))
  354.   (if (numberp symbol)
  355.       nil
  356.     symbol))
  357.  
  358.  
  359.