1 ;;; filecache.el --- Find files using a pre-loaded cache
3 ;; Author: Peter Breton <pbreton@cs.umb.edu>
4 ;; Created: Sun Nov 10 1996
5 ;; Keywords: convenience
6 ;; Time-stamp: <2001-11-26 12:03:18 pavel>
8 ;; Copyright (C) 1996, 2000 Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; The file-cache package is an attempt to make it easy to locate files
30 ;; by name, without having to remember exactly where they are located.
31 ;; This is very handy when working with source trees. You can also add
32 ;; frequently used files to the cache to create a hotlist effect.
33 ;; The cache can be used with any interactive command which takes a
34 ;; filename as an argument.
36 ;; It is worth noting that this package works best when most of the files
37 ;; in the cache have unique names, or (if they have the same name) exist in
38 ;; only a few directories. The worst case is many files all with
39 ;; the same name and in different directories, for example a big source tree
40 ;; with a Makefile in each directory. In such a case, you should probably
41 ;; use an alternate strategy to find the files.
43 ;; ADDING FILES TO THE CACHE:
45 ;; Use the following functions to add items to the file cache:
47 ;; * `file-cache-add-file': Adds a single file to the cache
49 ;; * `file-cache-add-file-list': Adds a list of files to the cache
51 ;; The following functions use the regular expressions in
52 ;; `file-cache-delete-regexps' to eliminate unwanted files:
54 ;; * `file-cache-add-directory': Adds the files in a directory to the
55 ;; cache. You can also specify a regular expression to match the files
56 ;; which should be added.
58 ;; * `file-cache-add-directory-list': Same as above, but acts on a list
59 ;; of directories. You can use `load-path', `exec-path' and the like.
61 ;; * `file-cache-add-directory-using-find': Uses the `find' command to
62 ;; add a directory tree to the cache.
64 ;; * `file-cache-add-directory-using-locate': Uses the `locate' command to
65 ;; add files matching a pattern to the cache.
67 ;; Use the function `file-cache-clear-cache' to remove all items from the
68 ;; cache. There are a number of `file-cache-delete' functions provided
69 ;; as well, but in general it is probably better to not worry too much
70 ;; about extra files in the cache.
72 ;; The most convenient way to initialize the cache is with an
73 ;; `eval-after-load' function, as noted in the ADDING FILES
74 ;; AUTOMATICALLY section.
76 ;; FINDING FILES USING THE CACHE:
78 ;; You can use the file-cache with any function that expects a filename as
79 ;; an argument. For example:
81 ;; 1) Invoke a function which expects a filename as an argument:
84 ;; 2) Begin typing a file name.
86 ;; 3) Invoke `file-cache-minibuffer-complete' (bound by default to
87 ;; C-TAB) to complete on the filename using the cache.
89 ;; 4) When you have found a unique completion, the minibuffer contents
90 ;; will change to the full name of that file.
92 ;; If there are a number of directories which contain the completion,
93 ;; invoking `file-cache-minibuffer-complete' repeatedly will cycle through
96 ;; 5) You can then edit the minibuffer contents, or press RETURN.
98 ;; It is much easier to simply try it than trying to explain it :)
100 ;;; ADDING FILES AUTOMATICALLY
102 ;; For maximum utility, you should probably define an `eval-after-load'
103 ;; form which loads your favorite files:
108 ;; (message "Loading file cache...")
109 ;; (file-cache-add-directory-using-find "~/projects")
110 ;; (file-cache-add-directory-list load-path)
111 ;; (file-cache-add-directory "~/")
112 ;; (file-cache-add-file-list (list "~/foo/bar" "~/baz/bar"))
115 ;; If you clear and reload the cache frequently, it is probably easiest
116 ;; to put your initializations in a function:
120 ;; '(my-file-cache-initialize))
122 ;; (defun my-file-cache-initialize ()
124 ;; (message "Loading file cache...")
125 ;; (file-cache-add-directory-using-find "~/projects")
126 ;; (file-cache-add-directory-list load-path)
127 ;; (file-cache-add-directory "~/")
128 ;; (file-cache-add-file-list (list "~/foo/bar" "~/baz/bar"))
131 ;; Of course, you can still add files to the cache afterwards, via
136 ;; This package is a distant relative of Noah Friedman's fff utilities.
137 ;; Our goal is pretty similar, but the implementation strategies are
142 (defgroup file-cache nil
143 "Find files using a pre-loaded cache."
146 :prefix
"file-cache-")
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149 ;; Customization Variables
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 ;; User-modifiable variables
153 (defcustom file-cache-filter-regexps
154 (list "~$" "\\.o$" "\\.exe$" "\\.a$" "\\.elc$" ",v$" "\\.output$"
155 "\\.$" "#$" "\\.class$")
156 "*List of regular expressions used as filters by the file cache.
157 File names which match these expressions will not be added to the cache.
158 Note that the functions `file-cache-add-file' and `file-cache-add-file-list'
159 do not use this variable."
160 :type
'(repeat regexp
)
163 (defcustom file-cache-find-command
"find"
164 "*External program used by `file-cache-add-directory-using-find'."
168 (defcustom file-cache-locate-command
"locate"
169 "*External program used by `file-cache-add-directory-using-locate'."
173 ;; Minibuffer messages
174 (defcustom file-cache-no-match-message
" [File Cache: No match]"
175 "Message to display when there is no completion."
179 (defcustom file-cache-sole-match-message
" [File Cache: sole completion]"
180 "Message to display when there is only one completion."
184 (defcustom file-cache-non-unique-message
185 " [File Cache: complete but not unique]"
186 "Message to display when there is a non-unique completion."
190 (defcustom file-cache-completion-ignore-case
191 (if (memq system-type
(list 'ms-dos
'windows-nt
))
193 completion-ignore-case
)
194 "If non-nil, file-cache completion should ignore case.
195 Defaults to the value of `completion-ignore-case'."
200 (defcustom file-cache-case-fold-search
201 (if (memq system-type
(list 'ms-dos
'windows-nt
))
204 "If non-nil, file-cache completion should ignore case.
205 Defaults to the value of `case-fold-search'."
210 (defcustom file-cache-assoc-function
211 (if (memq system-type
(list 'ms-dos
'windows-nt
))
214 "Function to use to check completions in the file cache.
215 Defaults to `assoc-ignore-case' on DOS and Windows, and `assoc' on
221 (defvar file-cache-multiple-directory-message nil
)
223 ;; Internal variables
224 ;; This should be named *Completions* because that's what the function
225 ;; switch-to-completions in simple.el expects
226 (defcustom file-cache-completions-buffer
"*Completions*"
227 "Buffer to display completions when using the file cache."
231 (defcustom file-cache-buffer
"*File Cache*"
232 "Buffer to hold the cache of file names."
236 (defcustom file-cache-buffer-default-regexp
"^.+$"
237 "Regexp to match files in `file-cache-buffer'."
241 (defvar file-cache-last-completion nil
)
243 (defvar file-cache-alist nil
244 "Internal data structure to hold cache of file names.")
246 (defvar file-cache-completions-keymap nil
247 "Keymap for file cache completions buffer.")
249 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
250 ;; Functions to add files to the cache
251 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
253 (defun file-cache-add-directory (directory &optional regexp
)
254 "Add DIRECTORY to the file cache.
255 If the optional REGEXP argument is non-nil, only files which match it will
256 be added to the cache."
257 (interactive "DAdd files from directory: ")
258 ;; Not an error, because otherwise we can't use load-paths that
259 ;; contain non-existent directories.
260 (if (not (file-accessible-directory-p directory
))
261 (message "Directory %s does not exist" directory
)
262 (let* ((dir (expand-file-name directory
))
263 (dir-files (directory-files dir t regexp
))
265 ;; Filter out files we don't want to see
270 (if (string-match regexp file
)
271 (setq dir-files
(delq file dir-files
))))
272 file-cache-filter-regexps
))
274 (file-cache-add-file-list dir-files
))))
276 (defun file-cache-add-directory-list (directory-list &optional regexp
)
277 "Add DIRECTORY-LIST (a list of directory names) to the file cache.
278 If the optional REGEXP argument is non-nil, only files which match it
279 will be added to the cache. Note that the REGEXP is applied to the files
280 in each directory, not to the directory list itself."
281 (interactive "XAdd files from directory list: ")
283 '(lambda (dir) (file-cache-add-directory dir regexp
))
286 (defun file-cache-add-file-list (file-list)
287 "Add FILE-LIST (a list of files names) to the file cache."
288 (interactive "XFile List: ")
289 (mapcar 'file-cache-add-file file-list
))
291 ;; Workhorse function
292 (defun file-cache-add-file (file)
293 "Add FILE to the file cache."
294 (interactive "fAdd File: ")
295 (if (not (file-exists-p file
))
296 (message "File %s does not exist" file
)
297 (let* ((file-name (file-name-nondirectory file
))
298 (dir-name (file-name-directory file
))
299 (the-entry (funcall file-cache-assoc-function
300 file-name file-cache-alist
))
302 ;; Does the entry exist already?
304 (if (or (and (stringp (cdr the-entry
))
305 (string= dir-name
(cdr the-entry
)))
306 (and (listp (cdr the-entry
))
307 (member dir-name
(cdr the-entry
))))
309 (setcdr the-entry
(append (list dir-name
) (cdr the-entry
)))
311 ;; If not, add it to the cache
312 (setq file-cache-alist
313 (cons (cons file-name
(list dir-name
))
317 (defun file-cache-add-directory-using-find (directory)
318 "Use the `find' command to add files to the file cache.
319 Find is run in DIRECTORY."
320 (interactive "DAdd files under directory: ")
321 (let ((dir (expand-file-name directory
)))
322 (set-buffer (get-buffer-create file-cache-buffer
))
324 (call-process file-cache-find-command nil
325 (get-buffer file-cache-buffer
) nil
327 (if (eq system-type
'windows-nt
) "'*'" "*")
329 (file-cache-add-from-file-cache-buffer)))
331 (defun file-cache-add-directory-using-locate (string)
332 "Use the `locate' command to add files to the file cache.
333 STRING is passed as an argument to the locate command."
334 (interactive "sAdd files using locate string: ")
335 (set-buffer (get-buffer-create file-cache-buffer
))
337 (call-process file-cache-locate-command nil
338 (get-buffer file-cache-buffer
) nil
340 (file-cache-add-from-file-cache-buffer))
342 (defun file-cache-add-from-file-cache-buffer (&optional regexp
)
343 "Add any entries found in the file cache buffer.
344 Each entry matches the regular expression `file-cache-buffer-default-regexp'
345 or the optional REGEXP argument."
346 (set-buffer file-cache-buffer
)
348 (function (lambda (elt)
349 (goto-char (point-min))
350 (delete-matching-lines elt
)))
351 file-cache-filter-regexps
)
352 (goto-char (point-min))
353 (let ((full-filename))
354 (while (re-search-forward
355 (or regexp file-cache-buffer-default-regexp
)
357 (setq full-filename
(buffer-substring-no-properties
358 (match-beginning 0) (match-end 0)))
359 (file-cache-add-file full-filename
))))
361 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
362 ;; Functions to delete from the cache
363 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
365 (defun file-cache-clear-cache ()
366 "Clear the file cache."
368 (setq file-cache-alist nil
))
370 ;; This clears *all* files with the given name
371 (defun file-cache-delete-file (file)
372 "Delete FILE from the file cache."
374 (list (completing-read "Delete file from cache: " file-cache-alist
)))
375 (setq file-cache-alist
376 (delq (funcall file-cache-assoc-function file file-cache-alist
)
379 (defun file-cache-delete-file-list (file-list)
380 "Delete FILE-LIST (a list of files) from the file cache."
381 (interactive "XFile List: ")
382 (mapcar 'file-cache-delete-file file-list
))
384 (defun file-cache-delete-file-regexp (regexp)
385 "Delete files matching REGEXP from the file cache."
386 (interactive "sRegexp: ")
388 (mapcar '(lambda (elt)
389 (and (string-match regexp
(car elt
))
390 (setq delete-list
(cons (car elt
) delete-list
))))
392 (file-cache-delete-file-list delete-list
)
393 (message "Deleted %d files from file cache" (length delete-list
))))
395 (defun file-cache-delete-directory (directory)
396 "Delete DIRECTORY from the file cache."
397 (interactive "DDelete directory from file cache: ")
398 (let ((dir (expand-file-name directory
))
402 (if (file-cache-do-delete-directory dir entry
)
403 (setq result
(1+ result
))))
406 (error "No entries containing %s found in cache" directory
)
407 (message "Deleted %d entries" result
))))
409 (defun file-cache-do-delete-directory (dir entry
)
410 (let ((directory-list (cdr entry
))
411 (directory (file-cache-canonical-directory dir
))
413 (and (member directory directory-list
)
414 (if (equal 1 (length directory-list
))
415 (setq file-cache-alist
416 (delq entry file-cache-alist
))
417 (setcdr entry
(delete directory directory-list
)))
421 (defun file-cache-delete-directory-list (directory-list)
422 "Delete DIRECTORY-LIST (a list of directories) from the file cache."
423 (interactive "XDirectory List: ")
424 (mapcar 'file-cache-delete-directory directory-list
))
426 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
428 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
430 ;; Returns the name of a directory for a file in the cache
431 (defun file-cache-directory-name (file)
432 (let* ((directory-list (cdr (funcall file-cache-assoc-function
433 file file-cache-alist
)))
434 (len (length directory-list
))
438 (if (not (listp directory-list
))
439 (error "Unknown type in file-cache-alist for key %s" file
))
443 (setq directory
(elt directory-list
0)))
446 (error "No directory found for key %s" file
))
449 (let* ((minibuffer-dir (file-name-directory (minibuffer-contents)))
450 (dir-list (member minibuffer-dir directory-list
))
453 ;; If the directory is in the list, return the next element
454 ;; Otherwise, return the first element
456 (or (elt directory-list
457 (setq num
(1+ (- len
(length dir-list
)))))
458 (elt directory-list
(setq num
0)))
459 (elt directory-list
(setq num
0))))
463 ;; If there were multiple directories, set up a minibuffer message
464 (setq file-cache-multiple-directory-message
465 (and num
(format " [%d of %d]" (1+ num
) len
)))
468 ;; Returns the name of a file in the cache
469 (defun file-cache-file-name (file)
470 (let ((directory (file-cache-directory-name file
)))
471 (concat directory file
)))
473 ;; Return a canonical directory for comparison purposes.
474 ;; Such a directory ends with a forward slash.
475 (defun file-cache-canonical-directory (dir)
476 (let ((directory dir
))
477 (if (not (char-equal ?
/ (string-to-char (substring directory -
1))))
478 (concat directory
"/")
481 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
482 ;; Minibuffer functions
483 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
485 ;; The prefix argument works around a bug in the minibuffer completion.
486 ;; The completion function doesn't distinguish between the states:
488 ;; "Multiple completions of name" (eg, Makefile, Makefile.in)
489 ;; "Name available in multiple directories" (/tmp/Makefile, ~me/Makefile)
491 ;; The default is to do the former; a prefix arg forces the latter.
494 (defun file-cache-minibuffer-complete (arg)
495 "Complete a filename in the minibuffer using a preloaded cache.
496 Filecache does two kinds of substitution: it completes on names in
497 the cache, and, once it has found a unique name, it cycles through
498 the directories that the name is available in. With a prefix argument,
499 the name is considered already unique; only the second substitution
500 \(directories) is done."
504 (completion-ignore-case file-cache-completion-ignore-case
)
505 (case-fold-search file-cache-case-fold-search
)
506 (string (file-name-nondirectory (minibuffer-contents)))
507 (completion-string (try-completion string file-cache-alist
))
513 ;; If it's the only match, replace the original contents
514 ((or arg
(eq completion-string t
))
515 (setq file-cache-string
(file-cache-file-name string
))
516 (if (string= file-cache-string
(minibuffer-contents))
517 (file-cache-temp-minibuffer-message file-cache-sole-match-message
)
518 (delete-minibuffer-contents)
519 (insert file-cache-string
)
520 (if file-cache-multiple-directory-message
521 (file-cache-temp-minibuffer-message
522 file-cache-multiple-directory-message
))
525 ;; If it's the longest match, insert it
526 ((stringp completion-string
)
527 ;; If we've already inserted a unique string, see if the user
528 ;; wants to use that one
529 (if (and (string= string completion-string
)
530 (funcall file-cache-assoc-function string file-cache-alist
))
531 (if (and (eq last-command this-command
)
532 (string= file-cache-last-completion completion-string
))
534 (delete-minibuffer-contents)
535 (insert (file-cache-file-name completion-string
))
536 (setq file-cache-last-completion nil
)
538 (file-cache-temp-minibuffer-message file-cache-non-unique-message
)
539 (setq file-cache-last-completion string
)
541 (setq file-cache-last-completion string
)
542 (setq completion-list
(all-completions string file-cache-alist
)
543 len
(length completion-list
))
546 (goto-char (point-max))
548 (substring completion-string
(length string
)))
549 ;; Add our own setup function to the Completions Buffer
550 (let ((completion-setup-hook
552 (append (list 'file-cache-completion-setup-function
)
553 completion-setup-hook
)))
555 (with-output-to-temp-buffer file-cache-completions-buffer
556 (display-completion-list completion-list
))
559 (setq file-cache-string
(file-cache-file-name completion-string
))
560 (if (string= file-cache-string
(minibuffer-contents))
561 (file-cache-temp-minibuffer-message
562 file-cache-sole-match-message
)
563 (delete-minibuffer-contents)
564 (insert file-cache-string
)
565 (if file-cache-multiple-directory-message
566 (file-cache-temp-minibuffer-message
567 file-cache-multiple-directory-message
)))
571 ((eq completion-string nil
)
572 (file-cache-temp-minibuffer-message file-cache-no-match-message
))
576 ;; Lifted from "complete.el"
577 (defun file-cache-temp-minibuffer-message (msg)
578 "A Lisp version of `temp_minibuffer_message' from minibuf.c."
579 (let ((savemax (point-max)))
581 (goto-char (point-max))
583 (let ((inhibit-quit t
))
585 (delete-region savemax
(point-max))
588 unread-command-events
(list 7))))))
590 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
591 ;; Completion functions
592 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
594 (defun file-cache-completion-setup-function ()
595 (set-buffer file-cache-completions-buffer
)
597 (if file-cache-completions-keymap
599 (setq file-cache-completions-keymap
600 (copy-keymap completion-list-mode-map
))
601 (define-key file-cache-completions-keymap
[mouse-2
]
602 'file-cache-mouse-choose-completion
)
603 (define-key file-cache-completions-keymap
"\C-m"
604 'file-cache-choose-completion
))
606 (use-local-map file-cache-completions-keymap
)
609 (defun file-cache-choose-completion ()
610 "Choose a completion in the `*Completions*' buffer."
612 (let ((completion-no-auto-exit t
))
614 (select-window (active-minibuffer-window))
615 (file-cache-minibuffer-complete nil
)
619 (defun file-cache-mouse-choose-completion (event)
620 "Choose a completion with the mouse."
622 (let ((completion-no-auto-exit t
))
623 (mouse-choose-completion event
)
624 (select-window (active-minibuffer-window))
625 (file-cache-minibuffer-complete nil
)
629 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
630 ;; Show parts of the cache
631 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
633 (defun file-cache-files-matching-internal (regexp)
634 "Output a list of files whose names (not including directories)
639 (lambda(cache-element)
640 (and (string-match regexp
641 (elt cache-element
0))
643 (nconc results
(list (elt cache-element
0)))
644 (setq results
(list (elt cache-element
0)))))))
648 (defun file-cache-files-matching (regexp)
649 "Output a list of files whose names (not including directories)
651 (interactive "sFind files matching regexp: ")
653 (file-cache-files-matching-internal regexp
))
656 (setq buf
(get-buffer-create
657 "*File Cache Files Matching*")))
664 (goto-char (point-min))
665 (display-buffer buf
)))
667 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
668 ;; Debugging functions
669 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
671 (defun file-cache-debug-read-from-minibuffer (file)
672 "Debugging function."
674 (list (completing-read "File Cache: " file-cache-alist
)))
675 (message "%s" (funcall file-cache-assoc-function file file-cache-alist
))
678 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
680 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
682 ;;;###autoload (define-key minibuffer-local-completion-map [C-tab] 'file-cache-minibuffer-complete)
683 ;;;###autoload (define-key minibuffer-local-map [C-tab] 'file-cache-minibuffer-complete)
684 ;;;###autoload (define-key minibuffer-local-must-match-map [C-tab] 'file-cache-minibuffer-complete)
688 ;;; filecache.el ends here