*** empty log message ***
[emacs.git] / lisp / filecache.el
blob71b67af355f45552d04414d5ec7b872890eae444
1 ;;; filecache.el --- find files using a pre-loaded cache
2 ;;
3 ;; Author: Peter Breton <pbreton@cs.umb.edu>
4 ;; Created: Sun Nov 10 1996
5 ;; Keywords: convenience
6 ;;
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)
14 ;; any later version.
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.
26 ;;; Commentary:
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 ;; * `file-cache-add-directory-recursively': Uses the find-lisp package to
67 ;; add all files matching a pattern to the cache.
69 ;; Use the function `file-cache-clear-cache' to remove all items from the
70 ;; cache. There are a number of `file-cache-delete' functions provided
71 ;; as well, but in general it is probably better to not worry too much
72 ;; about extra files in the cache.
74 ;; The most convenient way to initialize the cache is with an
75 ;; `eval-after-load' function, as noted in the ADDING FILES
76 ;; AUTOMATICALLY section.
78 ;; FINDING FILES USING THE CACHE:
80 ;; You can use the file-cache with any function that expects a filename as
81 ;; an argument. For example:
83 ;; 1) Invoke a function which expects a filename as an argument:
84 ;; M-x find-file
86 ;; 2) Begin typing a file name.
88 ;; 3) Invoke `file-cache-minibuffer-complete' (bound by default to
89 ;; C-TAB) to complete on the filename using the cache.
91 ;; 4) When you have found a unique completion, the minibuffer contents
92 ;; will change to the full name of that file.
94 ;; If there are a number of directories which contain the completion,
95 ;; invoking `file-cache-minibuffer-complete' repeatedly will cycle through
96 ;; them.
98 ;; 5) You can then edit the minibuffer contents, or press RETURN.
100 ;; It is much easier to simply try it than trying to explain it :)
102 ;;; ADDING FILES AUTOMATICALLY
104 ;; For maximum utility, you should probably define an `eval-after-load'
105 ;; form which loads your favorite files:
107 ;; (eval-after-load
108 ;; "filecache"
109 ;; '(progn
110 ;; (message "Loading file cache...")
111 ;; (file-cache-add-directory-using-find "~/projects")
112 ;; (file-cache-add-directory-list load-path)
113 ;; (file-cache-add-directory "~/")
114 ;; (file-cache-add-file-list (list "~/foo/bar" "~/baz/bar"))
115 ;; ))
117 ;; If you clear and reload the cache frequently, it is probably easiest
118 ;; to put your initializations in a function:
120 ;; (eval-after-load
121 ;; "filecache"
122 ;; '(my-file-cache-initialize))
124 ;; (defun my-file-cache-initialize ()
125 ;; (interactive)
126 ;; (message "Loading file cache...")
127 ;; (file-cache-add-directory-using-find "~/projects")
128 ;; (file-cache-add-directory-list load-path)
129 ;; (file-cache-add-directory "~/")
130 ;; (file-cache-add-file-list (list "~/foo/bar" "~/baz/bar"))
131 ;; ))
133 ;; Of course, you can still add files to the cache afterwards, via
134 ;; Lisp functions.
136 ;; RELATED WORK:
138 ;; This package is a distant relative of Noah Friedman's fff utilities.
139 ;; Our goal is pretty similar, but the implementation strategies are
140 ;; different.
142 ;;; Code:
144 (eval-when-compile
145 (require 'find-lisp))
147 (defgroup file-cache nil
148 "Find files using a pre-loaded cache."
149 :group 'files
150 :group 'convenience
151 :prefix "file-cache-")
153 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
154 ;; Customization Variables
155 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157 ;; User-modifiable variables
158 (defcustom file-cache-filter-regexps
159 (list "~$" "\\.o$" "\\.exe$" "\\.a$" "\\.elc$" ",v$" "\\.output$"
160 "\\.$" "#$" "\\.class$")
161 "*List of regular expressions used as filters by the file cache.
162 File names which match these expressions will not be added to the cache.
163 Note that the functions `file-cache-add-file' and `file-cache-add-file-list'
164 do not use this variable."
165 :type '(repeat regexp)
166 :group 'file-cache)
168 (defcustom file-cache-find-command "find"
169 "*External program used by `file-cache-add-directory-using-find'."
170 :type 'string
171 :group 'file-cache)
173 (defcustom file-cache-locate-command "locate"
174 "*External program used by `file-cache-add-directory-using-locate'."
175 :type 'string
176 :group 'file-cache)
178 ;; Minibuffer messages
179 (defcustom file-cache-no-match-message " [File Cache: No match]"
180 "Message to display when there is no completion."
181 :type 'string
182 :group 'file-cache)
184 (defcustom file-cache-sole-match-message " [File Cache: sole completion]"
185 "Message to display when there is only one completion."
186 :type 'string
187 :group 'file-cache)
189 (defcustom file-cache-non-unique-message
190 " [File Cache: complete but not unique]"
191 "Message to display when there is a non-unique completion."
192 :type 'string
193 :group 'file-cache)
195 (defcustom file-cache-completion-ignore-case
196 (if (memq system-type (list 'ms-dos 'windows-nt 'cygwin))
198 completion-ignore-case)
199 "If non-nil, file-cache completion should ignore case.
200 Defaults to the value of `completion-ignore-case'."
201 :type 'sexp
202 :group 'file-cache
205 (defcustom file-cache-case-fold-search
206 (if (memq system-type (list 'ms-dos 'windows-nt 'cygwin))
208 case-fold-search)
209 "If non-nil, file-cache completion should ignore case.
210 Defaults to the value of `case-fold-search'."
211 :type 'sexp
212 :group 'file-cache
215 (defcustom file-cache-ignore-case
216 (memq system-type (list 'ms-dos 'windows-nt 'cygwin))
217 "Non-nil means ignore case when checking completions in the file cache.
218 Defaults to nil on DOS and Windows, and t on other systems."
219 :type 'sexp
220 :group 'file-cache
223 (defvar file-cache-multiple-directory-message nil)
225 ;; Internal variables
226 ;; This should be named *Completions* because that's what the function
227 ;; switch-to-completions in simple.el expects
228 (defcustom file-cache-completions-buffer "*Completions*"
229 "Buffer to display completions when using the file cache."
230 :type 'string
231 :group 'file-cache)
233 (defcustom file-cache-buffer "*File Cache*"
234 "Buffer to hold the cache of file names."
235 :type 'string
236 :group 'file-cache)
238 (defcustom file-cache-buffer-default-regexp "^.+$"
239 "Regexp to match files in `file-cache-buffer'."
240 :type 'regexp
241 :group 'file-cache)
243 (defvar file-cache-last-completion nil)
245 (defvar file-cache-alist nil
246 "Internal data structure to hold cache of file names.")
248 (defvar file-cache-completions-keymap nil
249 "Keymap for file cache completions buffer.")
251 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
252 ;; Functions to add files to the cache
253 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
255 (defun file-cache-add-directory (directory &optional regexp)
256 "Add DIRECTORY to the file cache.
257 If the optional REGEXP argument is non-nil, only files which match it will
258 be added to the cache."
259 (interactive "DAdd files from directory: ")
260 ;; Not an error, because otherwise we can't use load-paths that
261 ;; contain non-existent directories.
262 (if (not (file-accessible-directory-p directory))
263 (message "Directory %s does not exist" directory)
264 (let* ((dir (expand-file-name directory))
265 (dir-files (directory-files dir t regexp))
267 ;; Filter out files we don't want to see
268 (mapcar
269 '(lambda (file)
270 (mapcar
271 '(lambda (regexp)
272 (if (string-match regexp file)
273 (setq dir-files (delq file dir-files))))
274 file-cache-filter-regexps))
275 dir-files)
276 (file-cache-add-file-list dir-files))))
278 (defun file-cache-add-directory-list (directory-list &optional regexp)
279 "Add DIRECTORY-LIST (a list of directory names) to the file cache.
280 If the optional REGEXP argument is non-nil, only files which match it
281 will be added to the cache. Note that the REGEXP is applied to the files
282 in each directory, not to the directory list itself."
283 (interactive "XAdd files from directory list: ")
284 (mapcar
285 '(lambda (dir) (file-cache-add-directory dir regexp))
286 directory-list))
288 (defun file-cache-add-file-list (file-list)
289 "Add FILE-LIST (a list of files names) to the file cache."
290 (interactive "XFile List: ")
291 (mapcar 'file-cache-add-file file-list))
293 ;; Workhorse function
294 (defun file-cache-add-file (file)
295 "Add FILE to the file cache."
296 (interactive "fAdd File: ")
297 (if (not (file-exists-p file))
298 (message "File %s does not exist" file)
299 (let* ((file-name (file-name-nondirectory file))
300 (dir-name (file-name-directory file))
301 (the-entry (assoc-string
302 file-name file-cache-alist
303 file-cache-ignore-case))
305 ;; Does the entry exist already?
306 (if the-entry
307 (if (or (and (stringp (cdr the-entry))
308 (string= dir-name (cdr the-entry)))
309 (and (listp (cdr the-entry))
310 (member dir-name (cdr the-entry))))
312 (setcdr the-entry (append (list dir-name) (cdr the-entry)))
314 ;; If not, add it to the cache
315 (setq file-cache-alist
316 (cons (cons file-name (list dir-name))
317 file-cache-alist)))
320 (defun file-cache-add-directory-using-find (directory)
321 "Use the `find' command to add files to the file cache.
322 Find is run in DIRECTORY."
323 (interactive "DAdd files under directory: ")
324 (let ((dir (expand-file-name directory)))
325 (set-buffer (get-buffer-create file-cache-buffer))
326 (erase-buffer)
327 (call-process file-cache-find-command nil
328 (get-buffer file-cache-buffer) nil
329 dir "-name"
330 (if (eq system-type 'windows-nt) "'*'" "*")
331 "-print")
332 (file-cache-add-from-file-cache-buffer)))
334 (defun file-cache-add-directory-using-locate (string)
335 "Use the `locate' command to add files to the file cache.
336 STRING is passed as an argument to the locate command."
337 (interactive "sAdd files using locate string: ")
338 (set-buffer (get-buffer-create file-cache-buffer))
339 (erase-buffer)
340 (call-process file-cache-locate-command nil
341 (get-buffer file-cache-buffer) nil
342 string)
343 (file-cache-add-from-file-cache-buffer))
345 (defun file-cache-add-directory-recursively (dir &optional regexp)
346 "Adds DIR and any subdirectories to the file-cache.
347 This function does not use any external programs
348 If the optional REGEXP argument is non-nil, only files which match it
349 will be added to the cache. Note that the REGEXP is applied to the files
350 in each directory, not to the directory list itself."
351 (interactive "DAdd directory: ")
352 (require 'find-lisp)
353 (mapcar
354 (function
355 (lambda(file)
356 (or (file-directory-p file)
357 (let (filtered)
358 (mapcar
359 (function
360 (lambda(regexp)
361 (and (string-match regexp file)
362 (setq filtered t))
364 file-cache-filter-regexps)
365 filtered)
366 (file-cache-add-file file))))
367 (find-lisp-find-files dir (if regexp regexp "^"))))
369 (defun file-cache-add-from-file-cache-buffer (&optional regexp)
370 "Add any entries found in the file cache buffer.
371 Each entry matches the regular expression `file-cache-buffer-default-regexp'
372 or the optional REGEXP argument."
373 (set-buffer file-cache-buffer)
374 (mapcar
375 (function (lambda (elt)
376 (goto-char (point-min))
377 (delete-matching-lines elt)))
378 file-cache-filter-regexps)
379 (goto-char (point-min))
380 (let ((full-filename))
381 (while (re-search-forward
382 (or regexp file-cache-buffer-default-regexp)
383 (point-max) t)
384 (setq full-filename (buffer-substring-no-properties
385 (match-beginning 0) (match-end 0)))
386 (file-cache-add-file full-filename))))
388 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
389 ;; Functions to delete from the cache
390 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
392 (defun file-cache-clear-cache ()
393 "Clear the file cache."
394 (interactive)
395 (setq file-cache-alist nil))
397 ;; This clears *all* files with the given name
398 (defun file-cache-delete-file (file)
399 "Delete FILE from the file cache."
400 (interactive
401 (list (completing-read "Delete file from cache: " file-cache-alist)))
402 (setq file-cache-alist
403 (delq (assoc-string file file-cache-alist file-cache-ignore-case)
404 file-cache-alist)))
406 (defun file-cache-delete-file-list (file-list)
407 "Delete FILE-LIST (a list of files) from the file cache."
408 (interactive "XFile List: ")
409 (mapcar 'file-cache-delete-file file-list))
411 (defun file-cache-delete-file-regexp (regexp)
412 "Delete files matching REGEXP from the file cache."
413 (interactive "sRegexp: ")
414 (let ((delete-list))
415 (mapcar '(lambda (elt)
416 (and (string-match regexp (car elt))
417 (setq delete-list (cons (car elt) delete-list))))
418 file-cache-alist)
419 (file-cache-delete-file-list delete-list)
420 (message "Deleted %d files from file cache" (length delete-list))))
422 (defun file-cache-delete-directory (directory)
423 "Delete DIRECTORY from the file cache."
424 (interactive "DDelete directory from file cache: ")
425 (let ((dir (expand-file-name directory))
426 (result 0))
427 (mapcar
428 '(lambda (entry)
429 (if (file-cache-do-delete-directory dir entry)
430 (setq result (1+ result))))
431 file-cache-alist)
432 (if (zerop result)
433 (error "No entries containing %s found in cache" directory)
434 (message "Deleted %d entries" result))))
436 (defun file-cache-do-delete-directory (dir entry)
437 (let ((directory-list (cdr entry))
438 (directory (file-cache-canonical-directory dir))
440 (and (member directory directory-list)
441 (if (equal 1 (length directory-list))
442 (setq file-cache-alist
443 (delq entry file-cache-alist))
444 (setcdr entry (delete directory directory-list)))
448 (defun file-cache-delete-directory-list (directory-list)
449 "Delete DIRECTORY-LIST (a list of directories) from the file cache."
450 (interactive "XDirectory List: ")
451 (mapcar 'file-cache-delete-directory directory-list))
453 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
454 ;; Utility functions
455 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
457 ;; Returns the name of a directory for a file in the cache
458 (defun file-cache-directory-name (file)
459 (let* ((directory-list (cdr (assoc-string
460 file file-cache-alist
461 file-cache-ignore-case)))
462 (len (length directory-list))
463 (directory)
464 (num)
466 (if (not (listp directory-list))
467 (error "Unknown type in file-cache-alist for key %s" file))
468 (cond
469 ;; Single element
470 ((eq 1 len)
471 (setq directory (elt directory-list 0)))
472 ;; No elements
473 ((eq 0 len)
474 (error "No directory found for key %s" file))
475 ;; Multiple elements
477 (let* ((minibuffer-dir (file-name-directory (minibuffer-contents)))
478 (dir-list (member minibuffer-dir directory-list))
480 (setq directory
481 ;; If the directory is in the list, return the next element
482 ;; Otherwise, return the first element
483 (if dir-list
484 (or (elt directory-list
485 (setq num (1+ (- len (length dir-list)))))
486 (elt directory-list (setq num 0)))
487 (elt directory-list (setq num 0))))
491 ;; If there were multiple directories, set up a minibuffer message
492 (setq file-cache-multiple-directory-message
493 (and num (format " [%d of %d]" (1+ num) len)))
494 directory))
496 ;; Returns the name of a file in the cache
497 (defun file-cache-file-name (file)
498 (let ((directory (file-cache-directory-name file)))
499 (concat directory file)))
501 ;; Return a canonical directory for comparison purposes.
502 ;; Such a directory ends with a forward slash.
503 (defun file-cache-canonical-directory (dir)
504 (let ((directory dir))
505 (if (not (char-equal ?/ (string-to-char (substring directory -1))))
506 (concat directory "/")
507 directory)))
509 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
510 ;; Minibuffer functions
511 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
513 ;; The prefix argument works around a bug in the minibuffer completion.
514 ;; The completion function doesn't distinguish between the states:
516 ;; "Multiple completions of name" (eg, Makefile, Makefile.in)
517 ;; "Name available in multiple directories" (/tmp/Makefile, ~me/Makefile)
519 ;; The default is to do the former; a prefix arg forces the latter.
521 ;;;###autoload
522 (defun file-cache-minibuffer-complete (arg)
523 "Complete a filename in the minibuffer using a preloaded cache.
524 Filecache does two kinds of substitution: it completes on names in
525 the cache, and, once it has found a unique name, it cycles through
526 the directories that the name is available in. With a prefix argument,
527 the name is considered already unique; only the second substitution
528 \(directories) is done."
529 (interactive "P")
530 (let*
532 (completion-ignore-case file-cache-completion-ignore-case)
533 (case-fold-search file-cache-case-fold-search)
534 (string (file-name-nondirectory (minibuffer-contents)))
535 (completion-string (try-completion string file-cache-alist))
536 (completion-list)
537 (len)
538 (file-cache-string)
540 (cond
541 ;; If it's the only match, replace the original contents
542 ((or arg (eq completion-string t))
543 (setq file-cache-string (file-cache-file-name string))
544 (if (string= file-cache-string (minibuffer-contents))
545 (file-cache-temp-minibuffer-message file-cache-sole-match-message)
546 (delete-minibuffer-contents)
547 (insert file-cache-string)
548 (if file-cache-multiple-directory-message
549 (file-cache-temp-minibuffer-message
550 file-cache-multiple-directory-message))
553 ;; If it's the longest match, insert it
554 ((stringp completion-string)
555 ;; If we've already inserted a unique string, see if the user
556 ;; wants to use that one
557 (if (and (string= string completion-string)
558 (assoc-string string file-cache-alist
559 file-cache-ignore-case))
560 (if (and (eq last-command this-command)
561 (string= file-cache-last-completion completion-string))
562 (progn
563 (delete-minibuffer-contents)
564 (insert (file-cache-file-name completion-string))
565 (setq file-cache-last-completion nil)
567 (file-cache-temp-minibuffer-message file-cache-non-unique-message)
568 (setq file-cache-last-completion string)
570 (setq file-cache-last-completion string)
571 (setq completion-list (all-completions string file-cache-alist)
572 len (length completion-list))
573 (if (> len 1)
574 (progn
575 (goto-char (point-max))
576 (insert
577 (substring completion-string (length string)))
578 ;; Add our own setup function to the Completions Buffer
579 (let ((completion-setup-hook
580 (reverse
581 (append (list 'file-cache-completion-setup-function)
582 completion-setup-hook)))
584 (with-output-to-temp-buffer file-cache-completions-buffer
585 (display-completion-list completion-list))
588 (setq file-cache-string (file-cache-file-name completion-string))
589 (if (string= file-cache-string (minibuffer-contents))
590 (file-cache-temp-minibuffer-message
591 file-cache-sole-match-message)
592 (delete-minibuffer-contents)
593 (insert file-cache-string)
594 (if file-cache-multiple-directory-message
595 (file-cache-temp-minibuffer-message
596 file-cache-multiple-directory-message)))
599 ;; No match
600 ((eq completion-string nil)
601 (file-cache-temp-minibuffer-message file-cache-no-match-message))
605 ;; Lifted from "complete.el"
606 (defun file-cache-temp-minibuffer-message (msg)
607 "A Lisp version of `temp_minibuffer_message' from minibuf.c."
608 (let ((savemax (point-max)))
609 (save-excursion
610 (goto-char (point-max))
611 (insert msg))
612 (let ((inhibit-quit t))
613 (sit-for 2)
614 (delete-region savemax (point-max))
615 (if quit-flag
616 (setq quit-flag nil
617 unread-command-events (list 7))))))
619 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
620 ;; Completion functions
621 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
623 (defun file-cache-completion-setup-function ()
624 (set-buffer file-cache-completions-buffer)
626 (if file-cache-completions-keymap
628 (setq file-cache-completions-keymap
629 (copy-keymap completion-list-mode-map))
630 (define-key file-cache-completions-keymap [mouse-2]
631 'file-cache-mouse-choose-completion)
632 (define-key file-cache-completions-keymap "\C-m"
633 'file-cache-choose-completion))
635 (use-local-map file-cache-completions-keymap)
638 (defun file-cache-choose-completion ()
639 "Choose a completion in the `*Completions*' buffer."
640 (interactive)
641 (let ((completion-no-auto-exit t))
642 (choose-completion)
643 (select-window (active-minibuffer-window))
644 (file-cache-minibuffer-complete nil)
648 (defun file-cache-mouse-choose-completion (event)
649 "Choose a completion with the mouse."
650 (interactive "e")
651 (let ((completion-no-auto-exit t))
652 (mouse-choose-completion event)
653 (select-window (active-minibuffer-window))
654 (file-cache-minibuffer-complete nil)
658 (defun file-cache-complete ()
659 "Complete the word at point, using the filecache."
660 (interactive)
661 (let (start pattern completion all)
662 (save-excursion
663 (skip-syntax-backward "^\"")
664 (setq start (point)))
665 (setq pattern (buffer-substring-no-properties start (point)))
666 (setq completion (try-completion pattern file-cache-alist))
667 (setq all (all-completions pattern file-cache-alist nil))
668 (cond ((eq completion t))
669 ((null completion)
670 (message "Can't find completion for \"%s\"" pattern)
671 (ding))
672 ((not (string= pattern completion))
673 (delete-region start (point))
674 (insert completion)
677 (with-output-to-temp-buffer "*Completions*"
678 (display-completion-list all))
682 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
683 ;; Show parts of the cache
684 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
686 (defun file-cache-files-matching-internal (regexp)
687 "Output a list of files whose names (not including directories)
688 match REGEXP."
689 (let ((results))
690 (mapcar
691 (function
692 (lambda(cache-element)
693 (and (string-match regexp
694 (elt cache-element 0))
695 (if results
696 (nconc results (list (elt cache-element 0)))
697 (setq results (list (elt cache-element 0)))))))
698 file-cache-alist)
699 results))
701 (defun file-cache-files-matching (regexp)
702 "Output a list of files whose names (not including directories)
703 match REGEXP."
704 (interactive "sFind files matching regexp: ")
705 (let ((results
706 (file-cache-files-matching-internal regexp))
707 buf)
708 (set-buffer
709 (setq buf (get-buffer-create
710 "*File Cache Files Matching*")))
711 (erase-buffer)
712 (insert
713 (mapconcat
714 'identity
715 results
716 "\n"))
717 (goto-char (point-min))
718 (display-buffer buf)))
720 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
721 ;; Debugging functions
722 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
724 (defun file-cache-debug-read-from-minibuffer (file)
725 "Debugging function."
726 (interactive
727 (list (completing-read "File Cache: " file-cache-alist)))
728 (message "%s" (assoc-string file file-cache-alist
729 file-cache-ignore-case))
732 (defun file-cache-display ()
733 "Display the file cache."
734 (interactive)
735 (let ((buf "*File Cache Contents*"))
736 (with-current-buffer
737 (get-buffer-create buf)
738 (erase-buffer)
739 (mapcar
740 (function
741 (lambda(item)
742 (insert (nth 1 item) (nth 0 item) "\n")))
743 file-cache-alist)
744 (pop-to-buffer buf)
747 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
748 ;; Keybindings
749 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
751 ;;;###autoload (define-key minibuffer-local-completion-map [C-tab] 'file-cache-minibuffer-complete)
752 ;;;###autoload (define-key minibuffer-local-map [C-tab] 'file-cache-minibuffer-complete)
753 ;;;###autoload (define-key minibuffer-local-must-match-map [C-tab] 'file-cache-minibuffer-complete)
755 (provide 'filecache)
757 ;;; arch-tag: 433d3ca4-4af2-47ce-b2cf-1f727460f538
758 ;;; filecache.el ends here