1 ;;; filecache.el --- Find files using a pre-loaded cache
3 ;; Author: Peter Breton
4 ;; Created: Sun Nov 10 1996
6 ;; Time-stamp: <97/02/07 17:26:54 peter>
8 ;; Copyright (C) 1996 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 INSTALLATION 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 :)
101 ;; Insert the following into your .emacs:
103 ;; (autoload 'file-cache-minibuffer-complete "filecache" nil t)
105 ;; For maximum utility, you should probably define an `eval-after-load'
106 ;; form which loads your favorite files:
111 ;; (message "Loading file cache...")
112 ;; (file-cache-add-directory-using-find "~/projects")
113 ;; (file-cache-add-directory-list load-path)
114 ;; (file-cache-add-directory "~/")
115 ;; (file-cache-add-file-list (list "~/foo/bar" "~/baz/bar"))
118 ;; If you clear and reload the cache frequently, it is probably easiest
119 ;; to put your initializations in a function:
123 ;; '(my-file-cache-initialize))
125 ;; (defun my-file-cache-initialize ()
127 ;; (message "Loading file cache...")
128 ;; (file-cache-add-directory-using-find "~/projects")
129 ;; (file-cache-add-directory-list load-path)
130 ;; (file-cache-add-directory "~/")
131 ;; (file-cache-add-file-list (list "~/foo/bar" "~/baz/bar"))
134 ;; Of course, you can still add files to the cache afterwards, via
139 ;; This package is a distant relative of Noah Friedman's fff utilities.
140 ;; Our goal is pretty similar, but the implementation strategies are
145 (defgroup file-cache nil
146 "Find files using a pre-loaded cache."
148 :prefix
"file-cache-")
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
154 ;; User-modifiable variables
155 (defcustom file-cache-filter-regexps
156 (list "~$" "\\.o$" "\\.exe$" "\\.a$" "\\.elc$" ",v$" "\\.output$"
158 "*List of regular expressions used as filters by the file cache.
159 File names which match these expressions will not be added to the cache.
160 Note that the functions `file-cache-add-file' and `file-cache-add-file-list'
161 do not use this variable."
162 :type
'(repeat regexp
)
165 (defcustom file-cache-find-command
"find"
166 "*External program used by `file-cache-add-directory-using-find'."
170 (defcustom file-cache-locate-command
"locate"
171 "*External program used by `file-cache-add-directory-using-locate'."
175 ;; Minibuffer messages
176 (defcustom file-cache-no-match-message
" [File Cache: No match]"
177 "Message to display when there is no completion."
181 (defcustom file-cache-sole-match-message
" [File Cache: sole completion]"
182 "Message to display when there is only one completion."
186 (defcustom file-cache-non-unique-message
187 " [File Cache: complete but not unique]"
188 "Message to display when there is a non-unique completion."
192 (defvar file-cache-multiple-directory-message nil
)
194 ;; Internal variables
195 ;; This should be named *Completions* because that's what the function
196 ;; switch-to-completions in simple.el expects
197 (defcustom file-cache-completions-buffer
"*Completions*"
198 "Buffer to display completions when using the file cache."
202 (defcustom file-cache-buffer
"*File Cache*"
203 "Buffer to hold the cache of file names."
207 (defcustom file-cache-buffer-default-regexp
"^.+$"
208 "Regexp to match files in `file-cache-buffer'."
212 (defvar file-cache-last-completion nil
)
214 (defvar file-cache-alist nil
215 "Internal data structure to hold cache of file names.")
217 (defvar file-cache-completions-keymap nil
218 "Keymap for file cache completions buffer.")
220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
221 ;; Functions to add files to the cache
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
224 (defun file-cache-add-directory (directory &optional regexp
)
225 "Add DIRECTORY to the file cache.
226 If the optional REGEXP argument is non-nil, only files which match it will
227 be added to the cache."
228 (interactive "DAdd files from directory: ")
229 (let* ((dir (expand-file-name directory
))
230 (dir-files (directory-files dir t regexp
))
232 ;; Filter out files we don't want to see
237 (if (string-match regexp file
)
238 (setq dir-files
(delq file dir-files
))))
239 file-cache-filter-regexps
))
241 (file-cache-add-file-list dir-files
)))
243 (defun file-cache-add-directory-list (directory-list &optional regexp
)
244 "Add DIRECTORY-LIST (a list of directory names) to the file cache.
245 If the optional REGEXP argument is non-nil, only files which match it
246 will be added to the cache. Note that the REGEXP is applied to the files
247 in each directory, not to the directory list itself."
248 (interactive "XAdd files from directory list: ")
250 '(lambda (dir) (file-cache-add-directory dir regexp
))
253 (defun file-cache-add-file-list (file-list)
254 "Add FILE-LIST (a list of files names) to the file cache."
255 (interactive "XFile List: ")
256 (mapcar 'file-cache-add-file file-list
))
258 ;; Workhorse function
259 (defun file-cache-add-file (file)
260 "Add FILE to the file cache."
261 (interactive "fAdd File: ")
262 (let* ((file-name (file-name-nondirectory file
))
263 (dir-name (file-name-directory file
))
264 (the-entry (assoc file-name file-cache-alist
))
266 ;; Does the entry exist already?
268 (if (or (and (stringp (cdr the-entry
))
269 (string= dir-name
(cdr the-entry
)))
270 (and (listp (cdr the-entry
))
271 (member dir-name
(cdr the-entry
))))
273 (setcdr the-entry
(append (list dir-name
) (cdr the-entry
)))
275 ;; If not, add it to the cache
276 (setq file-cache-alist
277 (cons (cons file-name
(list dir-name
))
281 (defun file-cache-add-directory-using-find (directory)
282 "Use the `find' command to add files to the file cache.
283 Find is run in DIRECTORY."
284 (interactive "DAdd files under directory: ")
285 (let ((dir (expand-file-name directory
)))
286 (set-buffer (get-buffer-create file-cache-buffer
))
288 (call-process file-cache-find-command nil
289 (get-buffer file-cache-buffer
) nil
291 (if (memq system-type
292 (list 'windows-nt
'ms-dos
)) "'*'" "*")
294 (file-cache-add-from-file-cache-buffer)))
296 (defun file-cache-add-directory-using-locate (string)
297 "Use the `locate' command to add files to the file cache.
298 STRING is passed as an argument to the locate command."
299 (interactive "sAdd files using locate string: ")
300 (set-buffer (get-buffer-create file-cache-buffer
))
302 (call-process file-cache-locate-command nil
303 (get-buffer file-cache-buffer
) nil
305 (file-cache-add-from-file-cache-buffer))
307 (defun file-cache-add-from-file-cache-buffer (&optional regexp
)
308 "Add any entries found in the file cache buffer.
309 Each entry matches the regular expression `file-cache-buffer-default-regexp'
310 or the optional REGEXP argument."
311 (set-buffer file-cache-buffer
)
313 (function (lambda (elt)
314 (goto-char (point-min))
315 (delete-matching-lines elt
)))
316 file-cache-filter-regexps
)
317 (goto-char (point-min))
318 (let ((full-filename))
319 (while (re-search-forward
320 (or regexp file-cache-buffer-default-regexp
)
322 (setq full-filename
(buffer-substring-no-properties
323 (match-beginning 0) (match-end 0)))
324 (file-cache-add-file full-filename
))))
326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
327 ;; Functions to delete from the cache
328 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
330 (defun file-cache-clear-cache ()
331 "Clear the file cache."
333 (setq file-cache-alist nil
))
335 ;; This clears *all* files with the given name
336 (defun file-cache-delete-file (file)
337 "Delete FILE from the file cache."
339 (list (completing-read "Delete file from cache: " file-cache-alist
)))
340 (setq file-cache-alist
341 (delq (assoc file file-cache-alist
) file-cache-alist
)))
343 (defun file-cache-delete-file-list (file-list)
344 "Delete FILE-LIST (a list of files) from the file cache."
345 (interactive "XFile List: ")
346 (mapcar 'file-cache-delete-file file-list
))
348 (defun file-cache-delete-file-regexp (regexp)
349 "Delete files matching REGEXP from the file cache."
350 (interactive "sRegexp: ")
352 (mapcar '(lambda (elt)
353 (and (string-match regexp
(car elt
))
354 (setq delete-list
(cons (car elt
) delete-list
))))
356 (file-cache-delete-file-list delete-list
)
357 (message "Deleted %d files from file cache" (length delete-list
))))
359 (defun file-cache-delete-directory (directory)
360 "Delete DIRECTORY from the file cache."
361 (interactive "DDelete directory from file cache: ")
362 (let ((dir (expand-file-name directory
))
366 (if (file-cache-do-delete-directory dir entry
)
367 (setq result
(1+ result
))))
370 (error "No entries containing %s found in cache" directory
)
371 (message "Deleted %d entries" result
))))
373 (defun file-cache-do-delete-directory (dir entry
)
374 (let ((directory-list (cdr entry
))
375 (directory (file-cache-canonical-directory dir
))
377 (and (member directory directory-list
)
378 (if (equal 1 (length directory-list
))
379 (setq file-cache-alist
380 (delq entry file-cache-alist
))
381 (setcdr entry
(delete directory directory-list
)))
385 (defun file-cache-delete-directory-list (directory-list)
386 "Delete DIRECTORY-LIST (a list of directories) from the file cache."
387 (interactive "XDirectory List: ")
388 (mapcar 'file-cache-delete-directory directory-list
))
390 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
392 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
394 ;; Returns the name of a directory for a file in the cache
395 (defun file-cache-directory-name (file)
396 (let* ((directory-list (cdr (assoc file file-cache-alist
)))
397 (len (length directory-list
))
401 (if (not (listp directory-list
))
402 (error "Unknown type in file-cache-alist for key %s" file
))
406 (setq directory
(elt directory-list
0)))
409 (error "No directory found for key %s" file
))
412 (let* ((minibuffer-dir (file-name-directory (buffer-string)))
413 (dir-list (member minibuffer-dir directory-list
))
416 ;; If the directory is in the list, return the next element
417 ;; Otherwise, return the first element
419 (or (elt directory-list
420 (setq num
(1+ (- len
(length dir-list
)))))
421 (elt directory-list
(setq num
0)))
422 (elt directory-list
(setq num
0))))
426 ;; If there were multiple directories, set up a minibuffer message
427 (setq file-cache-multiple-directory-message
428 (and num
(format " [%d of %d]" (1+ num
) len
)))
431 ;; Returns the name of a file in the cache
432 (defun file-cache-file-name (file)
433 (let ((directory (file-cache-directory-name file
)))
434 (concat directory file
)))
436 ;; Return a canonical directory for comparison purposes.
437 ;; Such a directory ends with a forward slash.
438 (defun file-cache-canonical-directory (dir)
439 (let ((directory dir
))
440 (if (not (char-equal ?
/ (string-to-char (substring directory -
1))))
441 (concat directory
"/")
444 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
445 ;; Minibuffer functions
446 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
448 ;; The prefix argument works around a bug in the minibuffer completion.
449 ;; The completion function doesn't distinguish between the states:
451 ;; "Multiple completions of name" (eg, Makefile, Makefile.in)
452 ;; "Name available in multiple directories" (/tmp/Makefile, ~me/Makefile)
454 ;; The default is to do the former; a prefix arg forces the latter.
457 (defun file-cache-minibuffer-complete (arg)
458 "Complete a filename in the minibuffer using a preloaded cache.
459 Filecache does two kinds of substitution: it completes on names in
460 the cache, and, once it has found a unique name, it cycles through
461 the directories that the name is available in. With a prefix argument,
462 the name is considered already unique; only the second substitution
463 \(directories) is done."
467 (completion-ignore-case nil
)
468 (case-fold-search nil
)
469 (string (file-name-nondirectory (buffer-string)))
470 (completion-string (try-completion string file-cache-alist
))
476 ;; If it's the only match, replace the original contents
477 ((or arg
(eq completion-string t
))
478 (setq file-cache-string
(file-cache-file-name string
))
479 (if (string= file-cache-string
(buffer-string))
480 (file-cache-temp-minibuffer-message file-cache-sole-match-message
)
482 (insert-string file-cache-string
)
483 (if file-cache-multiple-directory-message
484 (file-cache-temp-minibuffer-message
485 file-cache-multiple-directory-message
))
488 ;; If it's the longest match, insert it
489 ((stringp completion-string
)
490 ;; If we've already inserted a unique string, see if the user
491 ;; wants to use that one
492 (if (and (string= string completion-string
)
493 (assoc string file-cache-alist
))
494 (if (and (eq last-command this-command
)
495 (string= file-cache-last-completion completion-string
))
498 (insert-string (file-cache-file-name completion-string
))
499 (setq file-cache-last-completion nil
)
501 (file-cache-temp-minibuffer-message file-cache-non-unique-message
)
502 (setq file-cache-last-completion string
)
504 (setq file-cache-last-completion string
)
505 (setq completion-list
(all-completions string file-cache-alist
)
506 len
(length completion-list
))
509 (goto-char (point-max))
511 (substring completion-string
(length string
)))
512 ;; Add our own setup function to the Completions Buffer
513 (let ((completion-setup-hook
515 (append (list 'file-cache-completion-setup-function
)
516 completion-setup-hook
)))
518 (with-output-to-temp-buffer file-cache-completions-buffer
519 (display-completion-list completion-list
))
522 (setq file-cache-string
(file-cache-file-name completion-string
))
523 (if (string= file-cache-string
(buffer-string))
524 (file-cache-temp-minibuffer-message
525 file-cache-sole-match-message
)
527 (insert-string file-cache-string
)
528 (if file-cache-multiple-directory-message
529 (file-cache-temp-minibuffer-message
530 file-cache-multiple-directory-message
)))
534 ((eq completion-string nil
)
535 (file-cache-temp-minibuffer-message file-cache-no-match-message
))
539 ;; Lifted from "complete.el"
540 (defun file-cache-temp-minibuffer-message (msg)
541 "A Lisp version of `temp_minibuffer_message' from minibuf.c."
542 (let ((savemax (point-max)))
544 (goto-char (point-max))
546 (let ((inhibit-quit t
))
548 (delete-region savemax
(point-max))
551 unread-command-events
(list 7))))))
553 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
554 ;; Completion functions
555 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
557 (defun file-cache-completion-setup-function ()
558 (set-buffer file-cache-completions-buffer
)
560 (if file-cache-completions-keymap
562 (setq file-cache-completions-keymap
563 (copy-keymap completion-list-mode-map
))
564 (define-key file-cache-completions-keymap
[mouse-2
]
565 'file-cache-mouse-choose-completion
)
566 (define-key file-cache-completions-keymap
"\C-m"
567 'file-cache-choose-completion
))
569 (use-local-map file-cache-completions-keymap
)
572 (defun file-cache-choose-completion ()
573 "Choose a completion in the `*Completions*' buffer."
575 (let ((completion-no-auto-exit t
))
577 (select-window (active-minibuffer-window))
578 (file-cache-minibuffer-complete nil
)
582 (defun file-cache-mouse-choose-completion (event)
583 "Choose a completion with the mouse."
585 (let ((completion-no-auto-exit t
))
586 (mouse-choose-completion event
)
587 (select-window (active-minibuffer-window))
588 (file-cache-minibuffer-complete nil
)
592 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
593 ;; Debugging functions
594 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
596 (defun file-cache-debug-read-from-minibuffer (file)
597 "Debugging function."
599 (list (completing-read "File Cache: " file-cache-alist
)))
600 (message "%s" (assoc file file-cache-alist
))
603 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
605 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
607 ;;;###autoload (define-key minibuffer-local-completion-map [C-tab] 'file-cache-minibuffer-complete)
608 ;;;###autoload (define-key minibuffer-local-map [C-tab] 'file-cache-minibuffer-complete)
609 ;;;###autoload (define-key minibuffer-local-must-match-map [C-tab] 'file-cache-minibuffer-complete)
613 ;;; filecache.el ends here