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
7 ;; Copyright (C) 1996, 2000 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; The file-cache package is an attempt to make it easy to locate files
29 ;; by name, without having to remember exactly where they are located.
30 ;; This is very handy when working with source trees. You can also add
31 ;; frequently used files to the cache to create a hotlist effect.
32 ;; The cache can be used with any interactive command which takes a
33 ;; filename as an argument.
35 ;; It is worth noting that this package works best when most of the files
36 ;; in the cache have unique names, or (if they have the same name) exist in
37 ;; only a few directories. The worst case is many files all with
38 ;; the same name and in different directories, for example a big source tree
39 ;; with a Makefile in each directory. In such a case, you should probably
40 ;; use an alternate strategy to find the files.
42 ;; ADDING FILES TO THE CACHE:
44 ;; Use the following functions to add items to the file cache:
46 ;; * `file-cache-add-file': Adds a single file to the cache
48 ;; * `file-cache-add-file-list': Adds a list of files to the cache
50 ;; The following functions use the regular expressions in
51 ;; `file-cache-delete-regexps' to eliminate unwanted files:
53 ;; * `file-cache-add-directory': Adds the files in a directory to the
54 ;; cache. You can also specify a regular expression to match the files
55 ;; which should be added.
57 ;; * `file-cache-add-directory-list': Same as above, but acts on a list
58 ;; of directories. You can use `load-path', `exec-path' and the like.
60 ;; * `file-cache-add-directory-using-find': Uses the `find' command to
61 ;; add a directory tree to the cache.
63 ;; * `file-cache-add-directory-using-locate': Uses the `locate' command to
64 ;; add files matching a pattern to the cache.
66 ;; Use the function `file-cache-clear-cache' to remove all items from the
67 ;; cache. There are a number of `file-cache-delete' functions provided
68 ;; as well, but in general it is probably better to not worry too much
69 ;; about extra files in the cache.
71 ;; The most convenient way to initialize the cache is with an
72 ;; `eval-after-load' function, as noted in the ADDING FILES
73 ;; AUTOMATICALLY section.
75 ;; FINDING FILES USING THE CACHE:
77 ;; You can use the file-cache with any function that expects a filename as
78 ;; an argument. For example:
80 ;; 1) Invoke a function which expects a filename as an argument:
83 ;; 2) Begin typing a file name.
85 ;; 3) Invoke `file-cache-minibuffer-complete' (bound by default to
86 ;; C-TAB) to complete on the filename using the cache.
88 ;; 4) When you have found a unique completion, the minibuffer contents
89 ;; will change to the full name of that file.
91 ;; If there are a number of directories which contain the completion,
92 ;; invoking `file-cache-minibuffer-complete' repeatedly will cycle through
95 ;; 5) You can then edit the minibuffer contents, or press RETURN.
97 ;; It is much easier to simply try it than trying to explain it :)
99 ;;; ADDING FILES AUTOMATICALLY
101 ;; For maximum utility, you should probably define an `eval-after-load'
102 ;; form which loads your favorite files:
107 ;; (message "Loading file cache...")
108 ;; (file-cache-add-directory-using-find "~/projects")
109 ;; (file-cache-add-directory-list load-path)
110 ;; (file-cache-add-directory "~/")
111 ;; (file-cache-add-file-list (list "~/foo/bar" "~/baz/bar"))
114 ;; If you clear and reload the cache frequently, it is probably easiest
115 ;; to put your initializations in a function:
119 ;; '(my-file-cache-initialize))
121 ;; (defun my-file-cache-initialize ()
123 ;; (message "Loading file cache...")
124 ;; (file-cache-add-directory-using-find "~/projects")
125 ;; (file-cache-add-directory-list load-path)
126 ;; (file-cache-add-directory "~/")
127 ;; (file-cache-add-file-list (list "~/foo/bar" "~/baz/bar"))
130 ;; Of course, you can still add files to the cache afterwards, via
135 ;; This package is a distant relative of Noah Friedman's fff utilities.
136 ;; Our goal is pretty similar, but the implementation strategies are
141 (defgroup file-cache nil
142 "Find files using a pre-loaded cache."
145 :prefix
"file-cache-")
147 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
148 ;; Customization Variables
149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 ;; User-modifiable variables
152 (defcustom file-cache-filter-regexps
153 (list "~$" "\\.o$" "\\.exe$" "\\.a$" "\\.elc$" ",v$" "\\.output$"
154 "\\.$" "#$" "\\.class$")
155 "*List of regular expressions used as filters by the file cache.
156 File names which match these expressions will not be added to the cache.
157 Note that the functions `file-cache-add-file' and `file-cache-add-file-list'
158 do not use this variable."
159 :type
'(repeat regexp
)
162 (defcustom file-cache-find-command
"find"
163 "*External program used by `file-cache-add-directory-using-find'."
167 (defcustom file-cache-locate-command
"locate"
168 "*External program used by `file-cache-add-directory-using-locate'."
172 ;; Minibuffer messages
173 (defcustom file-cache-no-match-message
" [File Cache: No match]"
174 "Message to display when there is no completion."
178 (defcustom file-cache-sole-match-message
" [File Cache: sole completion]"
179 "Message to display when there is only one completion."
183 (defcustom file-cache-non-unique-message
184 " [File Cache: complete but not unique]"
185 "Message to display when there is a non-unique completion."
189 (defcustom file-cache-completion-ignore-case
190 (if (memq system-type
(list 'ms-dos
'windows-nt
'cygwin
))
192 completion-ignore-case
)
193 "If non-nil, file-cache completion should ignore case.
194 Defaults to the value of `completion-ignore-case'."
199 (defcustom file-cache-case-fold-search
200 (if (memq system-type
(list 'ms-dos
'windows-nt
'cygwin
))
203 "If non-nil, file-cache completion should ignore case.
204 Defaults to the value of `case-fold-search'."
209 (defcustom file-cache-assoc-function
210 (if (memq system-type
(list 'ms-dos
'windows-nt
'cygwin
))
213 "Function to use to check completions in the file cache.
214 Defaults to `assoc-ignore-case' on DOS and Windows, and `assoc' on
220 (defvar file-cache-multiple-directory-message nil
)
222 ;; Internal variables
223 ;; This should be named *Completions* because that's what the function
224 ;; switch-to-completions in simple.el expects
225 (defcustom file-cache-completions-buffer
"*Completions*"
226 "Buffer to display completions when using the file cache."
230 (defcustom file-cache-buffer
"*File Cache*"
231 "Buffer to hold the cache of file names."
235 (defcustom file-cache-buffer-default-regexp
"^.+$"
236 "Regexp to match files in `file-cache-buffer'."
240 (defvar file-cache-last-completion nil
)
242 (defvar file-cache-alist nil
243 "Internal data structure to hold cache of file names.")
245 (defvar file-cache-completions-keymap nil
246 "Keymap for file cache completions buffer.")
248 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
249 ;; Functions to add files to the cache
250 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
252 (defun file-cache-add-directory (directory &optional regexp
)
253 "Add DIRECTORY to the file cache.
254 If the optional REGEXP argument is non-nil, only files which match it will
255 be added to the cache."
256 (interactive "DAdd files from directory: ")
257 ;; Not an error, because otherwise we can't use load-paths that
258 ;; contain non-existent directories.
259 (if (not (file-accessible-directory-p directory
))
260 (message "Directory %s does not exist" directory
)
261 (let* ((dir (expand-file-name directory
))
262 (dir-files (directory-files dir t regexp
))
264 ;; Filter out files we don't want to see
269 (if (string-match regexp file
)
270 (setq dir-files
(delq file dir-files
))))
271 file-cache-filter-regexps
))
273 (file-cache-add-file-list dir-files
))))
275 (defun file-cache-add-directory-list (directory-list &optional regexp
)
276 "Add DIRECTORY-LIST (a list of directory names) to the file cache.
277 If the optional REGEXP argument is non-nil, only files which match it
278 will be added to the cache. Note that the REGEXP is applied to the files
279 in each directory, not to the directory list itself."
280 (interactive "XAdd files from directory list: ")
282 '(lambda (dir) (file-cache-add-directory dir regexp
))
285 (defun file-cache-add-file-list (file-list)
286 "Add FILE-LIST (a list of files names) to the file cache."
287 (interactive "XFile List: ")
288 (mapcar 'file-cache-add-file file-list
))
290 ;; Workhorse function
291 (defun file-cache-add-file (file)
292 "Add FILE to the file cache."
293 (interactive "fAdd File: ")
294 (if (not (file-exists-p file
))
295 (message "File %s does not exist" file
)
296 (let* ((file-name (file-name-nondirectory file
))
297 (dir-name (file-name-directory file
))
298 (the-entry (funcall file-cache-assoc-function
299 file-name file-cache-alist
))
301 ;; Does the entry exist already?
303 (if (or (and (stringp (cdr the-entry
))
304 (string= dir-name
(cdr the-entry
)))
305 (and (listp (cdr the-entry
))
306 (member dir-name
(cdr the-entry
))))
308 (setcdr the-entry
(append (list dir-name
) (cdr the-entry
)))
310 ;; If not, add it to the cache
311 (setq file-cache-alist
312 (cons (cons file-name
(list dir-name
))
316 (defun file-cache-add-directory-using-find (directory)
317 "Use the `find' command to add files to the file cache.
318 Find is run in DIRECTORY."
319 (interactive "DAdd files under directory: ")
320 (let ((dir (expand-file-name directory
)))
321 (set-buffer (get-buffer-create file-cache-buffer
))
323 (call-process file-cache-find-command nil
324 (get-buffer file-cache-buffer
) nil
326 (if (eq system-type
'windows-nt
) "'*'" "*")
328 (file-cache-add-from-file-cache-buffer)))
330 (defun file-cache-add-directory-using-locate (string)
331 "Use the `locate' command to add files to the file cache.
332 STRING is passed as an argument to the locate command."
333 (interactive "sAdd files using locate string: ")
334 (set-buffer (get-buffer-create file-cache-buffer
))
336 (call-process file-cache-locate-command nil
337 (get-buffer file-cache-buffer
) nil
339 (file-cache-add-from-file-cache-buffer))
341 (defun file-cache-add-from-file-cache-buffer (&optional regexp
)
342 "Add any entries found in the file cache buffer.
343 Each entry matches the regular expression `file-cache-buffer-default-regexp'
344 or the optional REGEXP argument."
345 (set-buffer file-cache-buffer
)
347 (function (lambda (elt)
348 (goto-char (point-min))
349 (delete-matching-lines elt
)))
350 file-cache-filter-regexps
)
351 (goto-char (point-min))
352 (let ((full-filename))
353 (while (re-search-forward
354 (or regexp file-cache-buffer-default-regexp
)
356 (setq full-filename
(buffer-substring-no-properties
357 (match-beginning 0) (match-end 0)))
358 (file-cache-add-file full-filename
))))
360 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
361 ;; Functions to delete from the cache
362 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
364 (defun file-cache-clear-cache ()
365 "Clear the file cache."
367 (setq file-cache-alist nil
))
369 ;; This clears *all* files with the given name
370 (defun file-cache-delete-file (file)
371 "Delete FILE from the file cache."
373 (list (completing-read "Delete file from cache: " file-cache-alist
)))
374 (setq file-cache-alist
375 (delq (funcall file-cache-assoc-function file file-cache-alist
)
378 (defun file-cache-delete-file-list (file-list)
379 "Delete FILE-LIST (a list of files) from the file cache."
380 (interactive "XFile List: ")
381 (mapcar 'file-cache-delete-file file-list
))
383 (defun file-cache-delete-file-regexp (regexp)
384 "Delete files matching REGEXP from the file cache."
385 (interactive "sRegexp: ")
387 (mapcar '(lambda (elt)
388 (and (string-match regexp
(car elt
))
389 (setq delete-list
(cons (car elt
) delete-list
))))
391 (file-cache-delete-file-list delete-list
)
392 (message "Deleted %d files from file cache" (length delete-list
))))
394 (defun file-cache-delete-directory (directory)
395 "Delete DIRECTORY from the file cache."
396 (interactive "DDelete directory from file cache: ")
397 (let ((dir (expand-file-name directory
))
401 (if (file-cache-do-delete-directory dir entry
)
402 (setq result
(1+ result
))))
405 (error "No entries containing %s found in cache" directory
)
406 (message "Deleted %d entries" result
))))
408 (defun file-cache-do-delete-directory (dir entry
)
409 (let ((directory-list (cdr entry
))
410 (directory (file-cache-canonical-directory dir
))
412 (and (member directory directory-list
)
413 (if (equal 1 (length directory-list
))
414 (setq file-cache-alist
415 (delq entry file-cache-alist
))
416 (setcdr entry
(delete directory directory-list
)))
420 (defun file-cache-delete-directory-list (directory-list)
421 "Delete DIRECTORY-LIST (a list of directories) from the file cache."
422 (interactive "XDirectory List: ")
423 (mapcar 'file-cache-delete-directory directory-list
))
425 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
427 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
429 ;; Returns the name of a directory for a file in the cache
430 (defun file-cache-directory-name (file)
431 (let* ((directory-list (cdr (funcall file-cache-assoc-function
432 file file-cache-alist
)))
433 (len (length directory-list
))
437 (if (not (listp directory-list
))
438 (error "Unknown type in file-cache-alist for key %s" file
))
442 (setq directory
(elt directory-list
0)))
445 (error "No directory found for key %s" file
))
448 (let* ((minibuffer-dir (file-name-directory (minibuffer-contents)))
449 (dir-list (member minibuffer-dir directory-list
))
452 ;; If the directory is in the list, return the next element
453 ;; Otherwise, return the first element
455 (or (elt directory-list
456 (setq num
(1+ (- len
(length dir-list
)))))
457 (elt directory-list
(setq num
0)))
458 (elt directory-list
(setq num
0))))
462 ;; If there were multiple directories, set up a minibuffer message
463 (setq file-cache-multiple-directory-message
464 (and num
(format " [%d of %d]" (1+ num
) len
)))
467 ;; Returns the name of a file in the cache
468 (defun file-cache-file-name (file)
469 (let ((directory (file-cache-directory-name file
)))
470 (concat directory file
)))
472 ;; Return a canonical directory for comparison purposes.
473 ;; Such a directory ends with a forward slash.
474 (defun file-cache-canonical-directory (dir)
475 (let ((directory dir
))
476 (if (not (char-equal ?
/ (string-to-char (substring directory -
1))))
477 (concat directory
"/")
480 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
481 ;; Minibuffer functions
482 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
484 ;; The prefix argument works around a bug in the minibuffer completion.
485 ;; The completion function doesn't distinguish between the states:
487 ;; "Multiple completions of name" (eg, Makefile, Makefile.in)
488 ;; "Name available in multiple directories" (/tmp/Makefile, ~me/Makefile)
490 ;; The default is to do the former; a prefix arg forces the latter.
493 (defun file-cache-minibuffer-complete (arg)
494 "Complete a filename in the minibuffer using a preloaded cache.
495 Filecache does two kinds of substitution: it completes on names in
496 the cache, and, once it has found a unique name, it cycles through
497 the directories that the name is available in. With a prefix argument,
498 the name is considered already unique; only the second substitution
499 \(directories) is done."
503 (completion-ignore-case file-cache-completion-ignore-case
)
504 (case-fold-search file-cache-case-fold-search
)
505 (string (file-name-nondirectory (minibuffer-contents)))
506 (completion-string (try-completion string file-cache-alist
))
512 ;; If it's the only match, replace the original contents
513 ((or arg
(eq completion-string t
))
514 (setq file-cache-string
(file-cache-file-name string
))
515 (if (string= file-cache-string
(minibuffer-contents))
516 (file-cache-temp-minibuffer-message file-cache-sole-match-message
)
517 (delete-minibuffer-contents)
518 (insert file-cache-string
)
519 (if file-cache-multiple-directory-message
520 (file-cache-temp-minibuffer-message
521 file-cache-multiple-directory-message
))
524 ;; If it's the longest match, insert it
525 ((stringp completion-string
)
526 ;; If we've already inserted a unique string, see if the user
527 ;; wants to use that one
528 (if (and (string= string completion-string
)
529 (funcall file-cache-assoc-function string file-cache-alist
))
530 (if (and (eq last-command this-command
)
531 (string= file-cache-last-completion completion-string
))
533 (delete-minibuffer-contents)
534 (insert (file-cache-file-name completion-string
))
535 (setq file-cache-last-completion nil
)
537 (file-cache-temp-minibuffer-message file-cache-non-unique-message
)
538 (setq file-cache-last-completion string
)
540 (setq file-cache-last-completion string
)
541 (setq completion-list
(all-completions string file-cache-alist
)
542 len
(length completion-list
))
545 (goto-char (point-max))
547 (substring completion-string
(length string
)))
548 ;; Add our own setup function to the Completions Buffer
549 (let ((completion-setup-hook
551 (append (list 'file-cache-completion-setup-function
)
552 completion-setup-hook
)))
554 (with-output-to-temp-buffer file-cache-completions-buffer
555 (display-completion-list completion-list
))
558 (setq file-cache-string
(file-cache-file-name completion-string
))
559 (if (string= file-cache-string
(minibuffer-contents))
560 (file-cache-temp-minibuffer-message
561 file-cache-sole-match-message
)
562 (delete-minibuffer-contents)
563 (insert file-cache-string
)
564 (if file-cache-multiple-directory-message
565 (file-cache-temp-minibuffer-message
566 file-cache-multiple-directory-message
)))
570 ((eq completion-string nil
)
571 (file-cache-temp-minibuffer-message file-cache-no-match-message
))
575 ;; Lifted from "complete.el"
576 (defun file-cache-temp-minibuffer-message (msg)
577 "A Lisp version of `temp_minibuffer_message' from minibuf.c."
578 (let ((savemax (point-max)))
580 (goto-char (point-max))
582 (let ((inhibit-quit t
))
584 (delete-region savemax
(point-max))
587 unread-command-events
(list 7))))))
589 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
590 ;; Completion functions
591 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
593 (defun file-cache-completion-setup-function ()
594 (set-buffer file-cache-completions-buffer
)
596 (if file-cache-completions-keymap
598 (setq file-cache-completions-keymap
599 (copy-keymap completion-list-mode-map
))
600 (define-key file-cache-completions-keymap
[mouse-2
]
601 'file-cache-mouse-choose-completion
)
602 (define-key file-cache-completions-keymap
"\C-m"
603 'file-cache-choose-completion
))
605 (use-local-map file-cache-completions-keymap
)
608 (defun file-cache-choose-completion ()
609 "Choose a completion in the `*Completions*' buffer."
611 (let ((completion-no-auto-exit t
))
613 (select-window (active-minibuffer-window))
614 (file-cache-minibuffer-complete nil
)
618 (defun file-cache-mouse-choose-completion (event)
619 "Choose a completion with the mouse."
621 (let ((completion-no-auto-exit t
))
622 (mouse-choose-completion event
)
623 (select-window (active-minibuffer-window))
624 (file-cache-minibuffer-complete nil
)
628 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
629 ;; Show parts of the cache
630 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
632 (defun file-cache-files-matching-internal (regexp)
633 "Output a list of files whose names (not including directories)
638 (lambda(cache-element)
639 (and (string-match regexp
640 (elt cache-element
0))
642 (nconc results
(list (elt cache-element
0)))
643 (setq results
(list (elt cache-element
0)))))))
647 (defun file-cache-files-matching (regexp)
648 "Output a list of files whose names (not including directories)
650 (interactive "sFind files matching regexp: ")
652 (file-cache-files-matching-internal regexp
))
655 (setq buf
(get-buffer-create
656 "*File Cache Files Matching*")))
663 (goto-char (point-min))
664 (display-buffer buf
)))
666 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
667 ;; Debugging functions
668 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
670 (defun file-cache-debug-read-from-minibuffer (file)
671 "Debugging function."
673 (list (completing-read "File Cache: " file-cache-alist
)))
674 (message "%s" (funcall file-cache-assoc-function file file-cache-alist
))
677 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
679 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
681 ;;;###autoload (define-key minibuffer-local-completion-map [C-tab] 'file-cache-minibuffer-complete)
682 ;;;###autoload (define-key minibuffer-local-map [C-tab] 'file-cache-minibuffer-complete)
683 ;;;###autoload (define-key minibuffer-local-must-match-map [C-tab] 'file-cache-minibuffer-complete)
687 ;;; filecache.el ends here