]> code.delx.au - gnu-emacs/blobdiff - lisp/filecache.el
Edward M. Reingold <reingold@emr.cs.iit.edu>
[gnu-emacs] / lisp / filecache.el
index 5af516179c530951bf3d68a3af1217ead9c86460..b8cb51ae1e5c51f6c85c0622b401151717293893 100644 (file)
@@ -1,11 +1,10 @@
-;;; filecache.el --- Find files using a pre-loaded cache
+;;; filecache.el --- find files using a pre-loaded cache
 ;;
 ;; Author:  Peter Breton <pbreton@cs.umb.edu>
 ;; Created: Sun Nov 10 1996
 ;; Keywords: convenience
-;; Time-stamp: <2000-08-28 16:18:03 pbreton>
 ;;
-;; Copyright (C) 1996 Free Software Foundation, Inc.
+;; Copyright (C) 1996, 2000 Free Software Foundation, Inc.
 
 ;; This file is part of GNU Emacs.
 
@@ -64,6 +63,9 @@
 ;;   * `file-cache-add-directory-using-locate': Uses the `locate' command to
 ;;     add files matching a pattern to the cache.
 ;;
+;;   * `file-cache-add-directory-recursively': Uses the find-lisp package to
+;;     add all files matching a pattern to the cache.
+;;
 ;; Use the function `file-cache-clear-cache' to remove all items from the
 ;; cache. There are a number of `file-cache-delete' functions provided
 ;; as well, but in general it is probably better to not worry too much
 
 ;;; Code:
 
+(eval-when-compile
+  (require 'find-lisp))
+
 (defgroup file-cache nil
   "Find files using a pre-loaded cache."
   :group 'files
@@ -188,7 +193,7 @@ do not use this variable."
   :group 'file-cache)
 
 (defcustom file-cache-completion-ignore-case
-   (if (memq system-type (list 'ms-dos 'windows-nt))
+   (if (memq system-type (list 'ms-dos 'windows-nt 'cygwin))
       t
      completion-ignore-case)
   "If non-nil, file-cache completion should ignore case.
@@ -198,7 +203,7 @@ Defaults to the value of `completion-ignore-case'."
   )
 
 (defcustom file-cache-case-fold-search
-  (if (memq system-type (list 'ms-dos 'windows-nt))
+  (if (memq system-type (list 'ms-dos 'windows-nt 'cygwin))
       t
     case-fold-search)
   "If non-nil, file-cache completion should ignore case.
@@ -208,7 +213,7 @@ Defaults to the value of `case-fold-search'."
   )
 
 (defcustom file-cache-assoc-function
-  (if (memq system-type (list 'ms-dos 'windows-nt))
+  (if (memq system-type (list 'ms-dos 'windows-nt 'cygwin))
       'assoc-ignore-case
     'assoc)
   "Function to use to check completions in the file cache.
@@ -324,8 +329,7 @@ Find is run in DIRECTORY."
     (call-process file-cache-find-command nil
                  (get-buffer file-cache-buffer) nil
                  dir "-name"
-                 (if (memq system-type
-                           (list 'windows-nt 'ms-dos)) "'*'" "*")
+                 (if (eq system-type 'windows-nt) "'*'" "*")
                  "-print")
     (file-cache-add-from-file-cache-buffer)))
 
@@ -340,6 +344,30 @@ STRING is passed as an argument to the locate command."
                string)
   (file-cache-add-from-file-cache-buffer))
 
+(defun file-cache-add-directory-recursively  (dir &optional regexp)
+  "Adds DIR and any subdirectories to the file-cache.
+This function does not use any external programs
+If the optional REGEXP argument is non-nil, only files which match it
+will be added to the cache. Note that the REGEXP is applied to the files
+in each directory, not to the directory list itself."
+  (interactive "DAdd directory: ")
+  (require 'find-lisp)
+  (mapcar
+   (function
+    (lambda(file)
+      (or (file-directory-p file)
+         (let (filtered)
+           (mapcar
+            (function
+             (lambda(regexp)
+               (and (string-match regexp file)
+                    (setq filtered t))
+               ))
+            file-cache-filter-regexps)
+           filtered)
+         (file-cache-add-file file))))
+   (find-lisp-find-files dir (if regexp regexp "^"))))
+
 (defun file-cache-add-from-file-cache-buffer (&optional regexp)
   "Add any entries found in the file cache buffer.
 Each entry matches the regular expression `file-cache-buffer-default-regexp'
@@ -447,7 +475,7 @@ or the optional REGEXP argument."
       (error "No directory found for key %s" file))
      ;; Multiple elements
      (t
-      (let* ((minibuffer-dir (file-name-directory (buffer-string)))
+      (let* ((minibuffer-dir (file-name-directory (minibuffer-contents)))
             (dir-list       (member minibuffer-dir directory-list))
             )
        (setq directory
@@ -504,7 +532,7 @@ the name is considered already unique; only the second substitution
       (
        (completion-ignore-case file-cache-completion-ignore-case)
        (case-fold-search       file-cache-case-fold-search)
-       (string                 (file-name-nondirectory (buffer-string)))
+       (string                 (file-name-nondirectory (minibuffer-contents)))
        (completion-string      (try-completion string file-cache-alist))
        (completion-list)
        (len)
@@ -514,10 +542,10 @@ the name is considered already unique; only the second substitution
      ;; If it's the only match, replace the original contents
      ((or arg (eq completion-string t))
       (setq file-cache-string (file-cache-file-name string))
-      (if (string= file-cache-string (buffer-string))
+      (if (string= file-cache-string (minibuffer-contents))
          (file-cache-temp-minibuffer-message file-cache-sole-match-message)
-       (erase-buffer)
-       (insert-string file-cache-string)
+       (delete-minibuffer-contents)
+       (insert file-cache-string)
        (if file-cache-multiple-directory-message
            (file-cache-temp-minibuffer-message
             file-cache-multiple-directory-message))
@@ -532,8 +560,8 @@ the name is considered already unique; only the second substitution
          (if (and (eq last-command this-command)
                   (string= file-cache-last-completion completion-string))
              (progn
-               (erase-buffer)
-               (insert-string (file-cache-file-name completion-string))
+               (delete-minibuffer-contents)
+               (insert (file-cache-file-name completion-string))
                (setq file-cache-last-completion nil)
                )
            (file-cache-temp-minibuffer-message file-cache-non-unique-message)
@@ -545,7 +573,7 @@ the name is considered already unique; only the second substitution
        (if (> len 1)
            (progn
              (goto-char (point-max))
-             (insert-string
+             (insert
               (substring completion-string (length string)))
              ;; Add our own setup function to the Completions Buffer
              (let ((completion-setup-hook
@@ -558,11 +586,11 @@ the name is considered already unique; only the second substitution
                )
              )
          (setq file-cache-string (file-cache-file-name completion-string))
-         (if (string= file-cache-string (buffer-string))
+         (if (string= file-cache-string (minibuffer-contents))
              (file-cache-temp-minibuffer-message
               file-cache-sole-match-message)
-           (erase-buffer)
-           (insert-string file-cache-string)
+           (delete-minibuffer-contents)
+           (insert file-cache-string)
            (if file-cache-multiple-directory-message
                (file-cache-temp-minibuffer-message
                 file-cache-multiple-directory-message)))
@@ -627,6 +655,30 @@ the name is considered already unique; only the second substitution
     )
   )
 
+(defun file-cache-complete  ()
+  "Complete the word at point, using the filecache."
+  (interactive)
+  (let (start pattern completion all)
+    (save-excursion
+      (skip-syntax-backward "^\"")
+      (setq start (point)))
+    (setq pattern    (buffer-substring-no-properties start (point)))
+    (setq completion (try-completion  pattern file-cache-alist))
+    (setq all        (all-completions pattern file-cache-alist nil))
+    (cond ((eq completion t))
+         ((null completion)
+          (message "Can't find completion for \"%s\"" pattern)
+          (ding))
+         ((not (string= pattern completion))
+          (delete-region start (point))
+          (insert completion)
+          )
+         (t
+          (with-output-to-temp-buffer "*Completions*"
+            (display-completion-list all))
+          ))
+    ))
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Show parts of the cache
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -676,6 +728,21 @@ match REGEXP."
   (message "%s" (funcall file-cache-assoc-function file file-cache-alist))
   )
 
+(defun file-cache-display  ()
+  "Display the file cache."
+  (interactive)
+  (let ((buf "*File Cache Contents*"))
+    (with-current-buffer
+       (get-buffer-create buf)
+      (erase-buffer)
+      (mapcar
+       (function
+      (lambda(item)
+       (insert (nth 1 item) (nth 0 item) "\n")))
+    file-cache-alist)
+      (pop-to-buffer buf)
+    )))
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Keybindings
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;