*** empty log message ***
[bpt/emacs.git] / lisp / filecache.el
index 34d30a6..d6aa353 100644 (file)
@@ -1,9 +1,9 @@
 ;;; filecache.el --- Find files using a pre-loaded cache
 ;;
-;; Author:  Peter Breton <pbreton@i-kinetics.com>
+;; Author:  Peter Breton <pbreton@cs.umb.edu>
 ;; Created: Sun Nov 10 1996
-;; Keywords: 
-;; Time-stamp: <97/02/07 17:26:54 peter>
+;; Keywords: convenience
+;; Time-stamp: <1998-04-29 22:38:56 pbreton>
 ;;
 ;; Copyright (C) 1996 Free Software Foundation, Inc.
 
@@ -70,7 +70,8 @@
 ;; about extra files in the cache.
 ;;
 ;; The most convenient way to initialize the cache is with an
-;; `eval-after-load' function, as noted in the INSTALLATION section.
+;; `eval-after-load' function, as noted in the ADDING FILES
+;; AUTOMATICALLY section.
 ;;
 ;; FINDING FILES USING THE CACHE:
 ;;
 ;;
 ;; It is much easier to simply try it than trying to explain it :)
 ;;
-;;; INSTALLATION
-;;
-;; Insert the following into your .emacs:
-;;
-;; (autoload 'file-cache-minibuffer-complete "filecache" nil t)
+;;; ADDING FILES AUTOMATICALLY
 ;;
 ;; For maximum utility, you should probably define an `eval-after-load'
 ;; form which loads your favorite files:
 (defgroup file-cache nil
   "Find files using a pre-loaded cache."
   :group 'files
+  :group 'convenience
   :prefix "file-cache-")
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;; Variables
+;; Customization Variables
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 ;; User-modifiable variables
 (defcustom file-cache-filter-regexps 
   (list "~$" "\\.o$" "\\.exe$" "\\.a$" "\\.elc$" ",v$" "\\.output$" 
-       "\\.$" "#$")
+       "\\.$" "#$" "\\.class$")
   "*List of regular expressions used as filters by the file cache.
 File names which match these expressions will not be added to the cache.
 Note that the functions `file-cache-add-file' and `file-cache-add-file-list' 
@@ -189,6 +187,13 @@ do not use this variable."
   :type 'string
   :group 'file-cache)
 
+(defcustom file-cache-completion-ignore-case completion-ignore-case
+  "If non-nil, file-cache completion should ignore case.
+Defaults to the value of `completion-ignore-case'."
+  :type 'sexp
+  :group 'file-cache
+  )
+
 (defvar file-cache-multiple-directory-message nil)
 
 ;; Internal variables
@@ -225,20 +230,24 @@ do not use this variable."
   "Add DIRECTORY to the file cache.
 If the optional REGEXP argument is non-nil, only files which match it will 
 be added to the cache."
-  (interactive "DAdd files from directory: ") 
-  (let* ((dir       (expand-file-name directory))
-        (dir-files (directory-files dir t regexp))
-        )
-    ;; Filter out files we don't want to see
-    (mapcar
-     '(lambda (file)
+  (interactive "DAdd files from directory: ")
+  ;; Not an error, because otherwise we can't use load-paths that
+  ;; contain non-existent directories.
+  (if (not (file-accessible-directory-p directory))
+      (message "Directory %s does not exist" directory)
+    (let* ((dir       (expand-file-name directory))
+          (dir-files (directory-files dir t regexp))
+          )
+      ;; Filter out files we don't want to see
+      (mapcar
+       '(lambda (file)
        (mapcar 
         '(lambda (regexp)
            (if (string-match regexp file)
                (setq dir-files (delq file dir-files))))
         file-cache-filter-regexps))
-     dir-files)
-    (file-cache-add-file-list dir-files)))
+       dir-files)
+      (file-cache-add-file-list dir-files))))
 
 (defun file-cache-add-directory-list (directory-list &optional regexp)
   "Add DIRECTORY-LIST (a list of directory names) to the file cache.
@@ -259,25 +268,27 @@ in each directory, not to the directory list itself."
 (defun file-cache-add-file (file)
   "Add FILE to the file cache."
   (interactive "fAdd File: ")
-  (let* ((file-name (file-name-nondirectory file))
-        (dir-name  (file-name-directory    file))
-        (the-entry (assoc file-name file-cache-alist))
-       )
-    ;; Does the entry exist already?
-    (if the-entry
-       (if (or (and (stringp (cdr the-entry))
-                    (string= dir-name (cdr the-entry)))
-               (and (listp (cdr the-entry))
-                    (member dir-name (cdr the-entry))))
-           nil
-         (setcdr the-entry (append (list dir-name) (cdr the-entry)))
-         )
-      ;; If not, add it to the cache
-      (setq file-cache-alist
-           (cons (cons file-name (list dir-name)) 
-                 file-cache-alist)))
-    ))
-
+  (if (not (file-exists-p file))
+      (message "File %s does not exist" file)
+    (let* ((file-name (file-name-nondirectory file))
+          (dir-name  (file-name-directory    file))
+          (the-entry (assoc file-name file-cache-alist))
+          )
+      ;; Does the entry exist already?
+      (if the-entry
+         (if (or (and (stringp (cdr the-entry))
+                      (string= dir-name (cdr the-entry)))
+                 (and (listp (cdr the-entry))
+                      (member dir-name (cdr the-entry))))
+             nil
+           (setcdr the-entry (append (list dir-name) (cdr the-entry)))
+           )
+       ;; If not, add it to the cache
+       (setq file-cache-alist
+             (cons (cons file-name (list dir-name)) 
+                   file-cache-alist)))
+      )))
+  
 (defun file-cache-add-directory-using-find (directory)
   "Use the `find' command to add files to the file cache.
 Find is run in DIRECTORY."
@@ -464,7 +475,7 @@ the name is considered already unique; only the second substitution
   (interactive "P") 
   (let* 
       (
-       (completion-ignore-case nil)
+       (completion-ignore-case file-cache-completion-ignore-case)
        (case-fold-search       nil)
        (string                 (file-name-nondirectory (buffer-string)))
        (completion-string      (try-completion string file-cache-alist))
@@ -589,6 +600,44 @@ the name is considered already unique; only the second substitution
     )
   )
 
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Show parts of the cache
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(defun file-cache-files-matching-internal (regexp)
+  "Output a list of files whose names (not including directories)
+match REGEXP."
+  (let ((results))
+    (mapcar
+     (function
+      (lambda(cache-element)
+       (and (string-match regexp
+                          (elt cache-element 0))
+            (if results
+                (nconc results (list (elt cache-element 0)))
+              (setq results (list (elt cache-element 0)))))))
+     file-cache-alist)
+    results))
+
+(defun file-cache-files-matching (regexp)
+  "Output a list of files whose names (not including directories)
+match REGEXP."
+  (interactive "sFind files matching regexp: ")
+  (let ((results 
+        (file-cache-files-matching-internal regexp))
+       buf)
+    (set-buffer 
+     (setq buf (get-buffer-create 
+               "*File Cache Files Matching*")))
+    (erase-buffer)
+    (insert
+     (mapconcat
+      'identity
+      results
+      "\n"))
+    (goto-char (point-min))
+    (display-buffer buf)))
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Debugging functions
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;