Merge from trunk.
[bpt/emacs.git] / lisp / filecache.el
CommitLineData
ec3476d0 1;;; filecache.el --- find files using a pre-loaded cache
ae732337 2
acaf905b 3;; Copyright (C) 1996, 2000-2012 Free Software Foundation, Inc.
ae732337 4
b662c4bc 5;; Author: Peter Breton <pbreton@cs.umb.edu>
6b279740 6;; Created: Sun Nov 10 1996
f5f727f8 7;; Keywords: convenience
13161e8b
RS
8
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
6b279740 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
13161e8b
RS
15
16;; GNU Emacs is distributed in the hope that it will be useful,
6b279740 17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13161e8b
RS
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
6b279740 21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
13161e8b 23
6b279740
RS
24;;; Commentary:
25;;
26;; The file-cache package is an attempt to make it easy to locate files
27;; by name, without having to remember exactly where they are located.
28;; This is very handy when working with source trees. You can also add
29;; frequently used files to the cache to create a hotlist effect.
30;; The cache can be used with any interactive command which takes a
31;; filename as an argument.
32;;
33;; It is worth noting that this package works best when most of the files
34;; in the cache have unique names, or (if they have the same name) exist in
35;; only a few directories. The worst case is many files all with
36;; the same name and in different directories, for example a big source tree
37;; with a Makefile in each directory. In such a case, you should probably
38;; use an alternate strategy to find the files.
39;;
40;; ADDING FILES TO THE CACHE:
41;;
42;; Use the following functions to add items to the file cache:
24ccf465 43;;
6b279740
RS
44;; * `file-cache-add-file': Adds a single file to the cache
45;;
46;; * `file-cache-add-file-list': Adds a list of files to the cache
47;;
48;; The following functions use the regular expressions in
49;; `file-cache-delete-regexps' to eliminate unwanted files:
24ccf465 50;;
6b279740
RS
51;; * `file-cache-add-directory': Adds the files in a directory to the
52;; cache. You can also specify a regular expression to match the files
53;; which should be added.
54;;
55;; * `file-cache-add-directory-list': Same as above, but acts on a list
56;; of directories. You can use `load-path', `exec-path' and the like.
57;;
58;; * `file-cache-add-directory-using-find': Uses the `find' command to
59;; add a directory tree to the cache.
60;;
61;; * `file-cache-add-directory-using-locate': Uses the `locate' command to
62;; add files matching a pattern to the cache.
63;;
57089611
PB
64;; * `file-cache-add-directory-recursively': Uses the find-lisp package to
65;; add all files matching a pattern to the cache.
66;;
6b279740
RS
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.
71;;
72;; The most convenient way to initialize the cache is with an
b662c4bc
RS
73;; `eval-after-load' function, as noted in the ADDING FILES
74;; AUTOMATICALLY section.
6b279740
RS
75;;
76;; FINDING FILES USING THE CACHE:
77;;
78;; You can use the file-cache with any function that expects a filename as
79;; an argument. For example:
80;;
81;; 1) Invoke a function which expects a filename as an argument:
82;; M-x find-file
83;;
84;; 2) Begin typing a file name.
85;;
86;; 3) Invoke `file-cache-minibuffer-complete' (bound by default to
87;; C-TAB) to complete on the filename using the cache.
88;;
89;; 4) When you have found a unique completion, the minibuffer contents
90;; will change to the full name of that file.
24ccf465 91;;
6b279740
RS
92;; If there are a number of directories which contain the completion,
93;; invoking `file-cache-minibuffer-complete' repeatedly will cycle through
94;; them.
95;;
96;; 5) You can then edit the minibuffer contents, or press RETURN.
97;;
98;; It is much easier to simply try it than trying to explain it :)
99;;
b662c4bc 100;;; ADDING FILES AUTOMATICALLY
6b279740
RS
101;;
102;; For maximum utility, you should probably define an `eval-after-load'
103;; form which loads your favorite files:
104;;
24ccf465 105;; (eval-after-load
6b279740
RS
106;; "filecache"
107;; '(progn
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"))
113;; ))
114;;
115;; If you clear and reload the cache frequently, it is probably easiest
116;; to put your initializations in a function:
117;;
24ccf465 118;; (eval-after-load
6b279740
RS
119;; "filecache"
120;; '(my-file-cache-initialize))
24ccf465 121;;
6b279740
RS
122;; (defun my-file-cache-initialize ()
123;; (interactive)
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"))
129;; ))
130;;
131;; Of course, you can still add files to the cache afterwards, via
132;; Lisp functions.
133;;
134;; RELATED WORK:
24ccf465 135;;
6b279740
RS
136;; This package is a distant relative of Noah Friedman's fff utilities.
137;; Our goal is pretty similar, but the implementation strategies are
138;; different.
13161e8b 139
6b279740
RS
140;;; Code:
141
33933d45
AS
142(defgroup file-cache nil
143 "Find files using a pre-loaded cache."
144 :group 'files
f5f727f8 145 :group 'convenience
33933d45
AS
146 :prefix "file-cache-")
147
6b279740 148;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
b662c4bc 149;; Customization Variables
6b279740
RS
150;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151
152;; User-modifiable variables
24ccf465 153(defcustom file-cache-filter-regexps
20565545
SM
154 ;; These are also used in buffers containing lines of file names,
155 ;; so the end-of-name is matched with $ rather than \\'.
24ccf465 156 (list "~$" "\\.o$" "\\.exe$" "\\.a$" "\\.elc$" ",v$" "\\.output$"
0a162908 157 "\\.$" "#$" "\\.class$")
9201cc28 158 "List of regular expressions used as filters by the file cache.
6b279740 159File names which match these expressions will not be added to the cache.
24ccf465 160Note that the functions `file-cache-add-file' and `file-cache-add-file-list'
33933d45
AS
161do not use this variable."
162 :type '(repeat regexp)
163 :group 'file-cache)
6b279740 164
33933d45 165(defcustom file-cache-find-command "find"
9201cc28 166 "External program used by `file-cache-add-directory-using-find'."
33933d45
AS
167 :type 'string
168 :group 'file-cache)
6b279740 169
6e74cce2 170(defcustom file-cache-find-command-posix-flag 'not-defined
9201cc28 171 "Set to t, if `file-cache-find-command' handles wildcards POSIX style.
6e74cce2
RS
172This variable is automatically set to nil or non-nil
173if it has the initial value `not-defined' whenever you first
174call the `file-cache-add-directory-using-find'.
175
176Under Windows operating system where Cygwin is available, this value
177should be t."
178 :type '(choice (const :tag "Yes" t)
179 (const :tag "No" nil)
180 (const :tag "Unknown" not-defined))
181 :group 'file-cache)
182
33933d45 183(defcustom file-cache-locate-command "locate"
9201cc28 184 "External program used by `file-cache-add-directory-using-locate'."
33933d45
AS
185 :type 'string
186 :group 'file-cache)
6b279740
RS
187
188;; Minibuffer messages
33933d45
AS
189(defcustom file-cache-no-match-message " [File Cache: No match]"
190 "Message to display when there is no completion."
191 :type 'string
192 :group 'file-cache)
193
194(defcustom file-cache-sole-match-message " [File Cache: sole completion]"
195 "Message to display when there is only one completion."
196 :type 'string
197 :group 'file-cache)
198
199(defcustom file-cache-non-unique-message
200 " [File Cache: complete but not unique]"
201 "Message to display when there is a non-unique completion."
202 :type 'string
203 :group 'file-cache)
6b279740 204
24ccf465 205(defcustom file-cache-completion-ignore-case
72bc50c0 206 (if (memq system-type '(ms-dos windows-nt cygwin))
24ccf465
PB
207 t
208 completion-ignore-case)
b047c9b7
GM
209 "If non-nil, file-cache completion should ignore case.
210Defaults to the value of `completion-ignore-case'."
20565545
SM
211 :type 'boolean
212 :group 'file-cache)
b047c9b7 213
24ccf465 214(defcustom file-cache-case-fold-search
72bc50c0 215 (if (memq system-type '(ms-dos windows-nt cygwin))
24ccf465
PB
216 t
217 case-fold-search)
218 "If non-nil, file-cache completion should ignore case.
219Defaults to the value of `case-fold-search'."
20565545
SM
220 :type 'boolean
221 :group 'file-cache)
24ccf465 222
3750be31 223(defcustom file-cache-ignore-case
72bc50c0 224 (memq system-type '(ms-dos windows-nt cygwin))
3750be31
RS
225 "Non-nil means ignore case when checking completions in the file cache.
226Defaults to nil on DOS and Windows, and t on other systems."
20565545
SM
227 :type 'boolean
228 :group 'file-cache)
24ccf465 229
6b279740
RS
230(defvar file-cache-multiple-directory-message nil)
231
232;; Internal variables
233;; This should be named *Completions* because that's what the function
234;; switch-to-completions in simple.el expects
33933d45
AS
235(defcustom file-cache-completions-buffer "*Completions*"
236 "Buffer to display completions when using the file cache."
237 :type 'string
238 :group 'file-cache)
239
24ccf465 240(defcustom file-cache-buffer "*File Cache*"
33933d45
AS
241 "Buffer to hold the cache of file names."
242 :type 'string
243 :group 'file-cache)
244
245(defcustom file-cache-buffer-default-regexp "^.+$"
246 "Regexp to match files in `file-cache-buffer'."
247 :type 'regexp
248 :group 'file-cache)
6b279740
RS
249
250(defvar file-cache-last-completion nil)
251
252(defvar file-cache-alist nil
bed4c972
SM
253 "Internal data structure to hold cache of file names.
254It is a list of entries of the form (FILENAME DIRNAME1 DIRNAME2 ...)
255where FILENAME is a file name component and the entry represents N
256files of names DIRNAME1/FILENAME, DIRNAME2/FILENAME, ...")
6b279740 257
d8e1753c
SM
258(defvar file-cache-completions-keymap
259 (let ((map (make-sparse-keymap)))
260 (set-keymap-parent map completion-list-mode-map)
ae732337 261 (define-key map [mouse-2] 'file-cache-choose-completion)
d8e1753c
SM
262 (define-key map "\C-m" 'file-cache-choose-completion)
263 map)
6b279740
RS
264 "Keymap for file cache completions buffer.")
265
266;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
267;; Functions to add files to the cache
268;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
269
3b5e5e30 270;;;###autoload
6b279740
RS
271(defun file-cache-add-directory (directory &optional regexp)
272 "Add DIRECTORY to the file cache.
24ccf465 273If the optional REGEXP argument is non-nil, only files which match it will
6b279740 274be added to the cache."
b662c4bc
RS
275 (interactive "DAdd files from directory: ")
276 ;; Not an error, because otherwise we can't use load-paths that
277 ;; contain non-existent directories.
278 (if (not (file-accessible-directory-p directory))
279 (message "Directory %s does not exist" directory)
280 (let* ((dir (expand-file-name directory))
20565545 281 (dir-files (directory-files dir t regexp)))
b662c4bc 282 ;; Filter out files we don't want to see
20565545
SM
283 (dolist (file dir-files)
284 (if (file-directory-p file)
285 (setq dir-files (delq file dir-files))
286 (dolist (regexp file-cache-filter-regexps)
287 (if (string-match regexp file)
288 (setq dir-files (delq file dir-files))))))
b662c4bc 289 (file-cache-add-file-list dir-files))))
6b279740 290
3b5e5e30 291;;;###autoload
6b279740
RS
292(defun file-cache-add-directory-list (directory-list &optional regexp)
293 "Add DIRECTORY-LIST (a list of directory names) to the file cache.
24ccf465 294If the optional REGEXP argument is non-nil, only files which match it
770255e9
JB
295will be added to the cache. Note that the REGEXP is applied to the
296files in each directory, not to the directory list itself."
6b279740 297 (interactive "XAdd files from directory list: ")
24ccf465 298 (mapcar
20565545 299 (lambda (dir) (file-cache-add-directory dir regexp))
6b279740
RS
300 directory-list))
301
302(defun file-cache-add-file-list (file-list)
303 "Add FILE-LIST (a list of files names) to the file cache."
304 (interactive "XFile List: ")
305 (mapcar 'file-cache-add-file file-list))
306
307;; Workhorse function
3b5e5e30
RS
308
309;;;###autoload
6b279740
RS
310(defun file-cache-add-file (file)
311 "Add FILE to the file cache."
312 (interactive "fAdd File: ")
ec15e0ff
CY
313 (setq file (file-truename file))
314 (unless (file-exists-p file)
315 (error "Filecache: file %s does not exist" file))
316 (let* ((file-name (file-name-nondirectory file))
317 (dir-name (file-name-directory file))
318 (the-entry (assoc-string file-name file-cache-alist
319 file-cache-ignore-case)))
320 ;; Does the entry exist already?
321 (if the-entry
322 (unless (or (and (stringp (cdr the-entry))
323 (string= dir-name (cdr the-entry)))
324 (and (listp (cdr the-entry))
325 (member dir-name (cdr the-entry))))
326 (setcdr the-entry (cons dir-name (cdr the-entry))))
327 ;; If not, add it to the cache
328 (push (list file-name dir-name) file-cache-alist))))
24ccf465 329
3b5e5e30 330;;;###autoload
6b279740
RS
331(defun file-cache-add-directory-using-find (directory)
332 "Use the `find' command to add files to the file cache.
333Find is run in DIRECTORY."
334 (interactive "DAdd files under directory: ")
335 (let ((dir (expand-file-name directory)))
d9c1ce9d
RS
336 (when (memq system-type '(windows-nt cygwin))
337 (if (eq file-cache-find-command-posix-flag 'not-defined)
338 (setq file-cache-find-command-posix-flag
339 (executable-command-find-posix-p file-cache-find-command))))
6b279740
RS
340 (set-buffer (get-buffer-create file-cache-buffer))
341 (erase-buffer)
24ccf465 342 (call-process file-cache-find-command nil
6b279740 343 (get-buffer file-cache-buffer) nil
24ccf465 344 dir "-name"
da14d1ac
RS
345 (if (memq system-type '(windows-nt cygwin))
346 (if file-cache-find-command-posix-flag
347 "\\*"
348 "'*'")
349 "*")
6b279740
RS
350 "-print")
351 (file-cache-add-from-file-cache-buffer)))
352
3b5e5e30 353;;;###autoload
6b279740
RS
354(defun file-cache-add-directory-using-locate (string)
355 "Use the `locate' command to add files to the file cache.
356STRING is passed as an argument to the locate command."
357 (interactive "sAdd files using locate string: ")
358 (set-buffer (get-buffer-create file-cache-buffer))
359 (erase-buffer)
24ccf465 360 (call-process file-cache-locate-command nil
6b279740
RS
361 (get-buffer file-cache-buffer) nil
362 string)
363 (file-cache-add-from-file-cache-buffer))
364
00a3b041
GM
365(autoload 'find-lisp-find-files "find-lisp")
366
3b5e5e30 367;;;###autoload
57089611
PB
368(defun file-cache-add-directory-recursively (dir &optional regexp)
369 "Adds DIR and any subdirectories to the file-cache.
770255e9 370This function does not use any external programs.
57089611 371If the optional REGEXP argument is non-nil, only files which match it
770255e9
JB
372will be added to the cache. Note that the REGEXP is applied to the
373files in each directory, not to the directory list itself."
57089611 374 (interactive "DAdd directory: ")
57089611 375 (mapcar
00a3b041
GM
376 (lambda (file)
377 (or (file-directory-p file)
378 (let (filtered)
379 (dolist (regexp file-cache-filter-regexps)
380 (and (string-match regexp file)
381 (setq filtered t)))
382 filtered)
383 (file-cache-add-file file)))
384 (find-lisp-find-files dir (or regexp "^"))))
57089611 385
6b279740
RS
386(defun file-cache-add-from-file-cache-buffer (&optional regexp)
387 "Add any entries found in the file cache buffer.
388Each entry matches the regular expression `file-cache-buffer-default-regexp'
389or the optional REGEXP argument."
390 (set-buffer file-cache-buffer)
20565545
SM
391 (dolist (elt file-cache-filter-regexps)
392 (goto-char (point-min))
393 (delete-matching-lines elt))
6b279740
RS
394 (goto-char (point-min))
395 (let ((full-filename))
396 (while (re-search-forward
24ccf465 397 (or regexp file-cache-buffer-default-regexp)
6b279740
RS
398 (point-max) t)
399 (setq full-filename (buffer-substring-no-properties
24ccf465 400 (match-beginning 0) (match-end 0)))
6b279740
RS
401 (file-cache-add-file full-filename))))
402
403;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
404;; Functions to delete from the cache
405;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
406
407(defun file-cache-clear-cache ()
408 "Clear the file cache."
409 (interactive)
410 (setq file-cache-alist nil))
411
412;; This clears *all* files with the given name
413(defun file-cache-delete-file (file)
414 "Delete FILE from the file cache."
415 (interactive
416 (list (completing-read "Delete file from cache: " file-cache-alist)))
24ccf465 417 (setq file-cache-alist
3750be31 418 (delq (assoc-string file file-cache-alist file-cache-ignore-case)
24ccf465 419 file-cache-alist)))
6b279740
RS
420
421(defun file-cache-delete-file-list (file-list)
422 "Delete FILE-LIST (a list of files) from the file cache."
423 (interactive "XFile List: ")
424 (mapcar 'file-cache-delete-file file-list))
425
426(defun file-cache-delete-file-regexp (regexp)
427 "Delete files matching REGEXP from the file cache."
428 (interactive "sRegexp: ")
429 (let ((delete-list))
20565545
SM
430 (dolist (elt file-cache-alist)
431 (and (string-match regexp (car elt))
432 (push (car elt) delete-list)))
6b279740 433 (file-cache-delete-file-list delete-list)
bbc66b08
EZ
434 (message "Filecache: deleted %d files from file cache"
435 (length delete-list))))
6b279740
RS
436
437(defun file-cache-delete-directory (directory)
438 "Delete DIRECTORY from the file cache."
439 (interactive "DDelete directory from file cache: ")
440 (let ((dir (expand-file-name directory))
441 (result 0))
20565545
SM
442 (dolist (entry file-cache-alist)
443 (if (file-cache-do-delete-directory dir entry)
444 (setq result (1+ result))))
6b279740 445 (if (zerop result)
bbc66b08
EZ
446 (error "Filecache: no entries containing %s found in cache" directory)
447 (message "Filecache: deleted %d entries" result))))
6b279740
RS
448
449(defun file-cache-do-delete-directory (dir entry)
450 (let ((directory-list (cdr entry))
20565545 451 (directory (file-cache-canonical-directory dir)))
6b279740
RS
452 (and (member directory directory-list)
453 (if (equal 1 (length directory-list))
24ccf465 454 (setq file-cache-alist
6b279740 455 (delq entry file-cache-alist))
20565545 456 (setcdr entry (delete directory directory-list))))))
6b279740
RS
457
458(defun file-cache-delete-directory-list (directory-list)
459 "Delete DIRECTORY-LIST (a list of directories) from the file cache."
460 (interactive "XDirectory List: ")
461 (mapcar 'file-cache-delete-directory directory-list))
462
463;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
464;; Utility functions
465;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
466
467;; Returns the name of a directory for a file in the cache
468(defun file-cache-directory-name (file)
3750be31
RS
469 (let* ((directory-list (cdr (assoc-string
470 file file-cache-alist
471 file-cache-ignore-case)))
6b279740
RS
472 (len (length directory-list))
473 (directory)
20565545 474 (num))
6b279740 475 (if (not (listp directory-list))
bbc66b08 476 (error "Filecache: unknown type in file-cache-alist for key %s" file))
24ccf465 477 (cond
6b279740
RS
478 ;; Single element
479 ((eq 1 len)
480 (setq directory (elt directory-list 0)))
481 ;; No elements
482 ((eq 0 len)
bbc66b08 483 (error "Filecache: no directory found for key %s" file))
6b279740
RS
484 ;; Multiple elements
485 (t
dea0a87d 486 (let* ((minibuffer-dir (file-name-directory (minibuffer-contents)))
20565545 487 (dir-list (member minibuffer-dir directory-list)))
6b279740
RS
488 (setq directory
489 ;; If the directory is in the list, return the next element
490 ;; Otherwise, return the first element
24ccf465
PB
491 (if dir-list
492 (or (elt directory-list
6b279740
RS
493 (setq num (1+ (- len (length dir-list)))))
494 (elt directory-list (setq num 0)))
20565545 495 (elt directory-list (setq num 0)))))))
6b279740
RS
496 ;; If there were multiple directories, set up a minibuffer message
497 (setq file-cache-multiple-directory-message
498 (and num (format " [%d of %d]" (1+ num) len)))
499 directory))
500
501;; Returns the name of a file in the cache
502(defun file-cache-file-name (file)
503 (let ((directory (file-cache-directory-name file)))
504 (concat directory file)))
24ccf465 505
6b279740
RS
506;; Return a canonical directory for comparison purposes.
507;; Such a directory ends with a forward slash.
508(defun file-cache-canonical-directory (dir)
509 (let ((directory dir))
510 (if (not (char-equal ?/ (string-to-char (substring directory -1))))
511 (concat directory "/")
512 directory)))
513
514;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
515;; Minibuffer functions
516;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
517
13161e8b
RS
518;; The prefix argument works around a bug in the minibuffer completion.
519;; The completion function doesn't distinguish between the states:
24ccf465 520;;
13161e8b
RS
521;; "Multiple completions of name" (eg, Makefile, Makefile.in)
522;; "Name available in multiple directories" (/tmp/Makefile, ~me/Makefile)
24ccf465 523;;
13161e8b
RS
524;; The default is to do the former; a prefix arg forces the latter.
525
6b279740 526;;;###autoload
13161e8b
RS
527(defun file-cache-minibuffer-complete (arg)
528 "Complete a filename in the minibuffer using a preloaded cache.
529Filecache does two kinds of substitution: it completes on names in
530the cache, and, once it has found a unique name, it cycles through
24ccf465
PB
531the directories that the name is available in. With a prefix argument,
532the name is considered already unique; only the second substitution
13161e8b 533\(directories) is done."
24ccf465
PB
534 (interactive "P")
535 (let*
6b279740 536 (
b047c9b7 537 (completion-ignore-case file-cache-completion-ignore-case)
24ccf465 538 (case-fold-search file-cache-case-fold-search)
dea0a87d 539 (string (file-name-nondirectory (minibuffer-contents)))
13161e8b 540 (completion-string (try-completion string file-cache-alist))
6b279740
RS
541 (completion-list)
542 (len)
20565545 543 (file-cache-string))
24ccf465 544 (cond
13161e8b
RS
545 ;; If it's the only match, replace the original contents
546 ((or arg (eq completion-string t))
547 (setq file-cache-string (file-cache-file-name string))
dea0a87d 548 (if (string= file-cache-string (minibuffer-contents))
20565545 549 (minibuffer-message file-cache-sole-match-message)
dea0a87d 550 (delete-minibuffer-contents)
add91c7b 551 (insert file-cache-string)
13161e8b 552 (if file-cache-multiple-directory-message
20565545 553 (minibuffer-message file-cache-multiple-directory-message))))
13161e8b 554
6b279740
RS
555 ;; If it's the longest match, insert it
556 ((stringp completion-string)
557 ;; If we've already inserted a unique string, see if the user
558 ;; wants to use that one
559 (if (and (string= string completion-string)
3750be31
RS
560 (assoc-string string file-cache-alist
561 file-cache-ignore-case))
6b279740
RS
562 (if (and (eq last-command this-command)
563 (string= file-cache-last-completion completion-string))
24ccf465 564 (progn
dea0a87d 565 (delete-minibuffer-contents)
add91c7b 566 (insert (file-cache-file-name completion-string))
20565545
SM
567 (setq file-cache-last-completion nil))
568 (minibuffer-message file-cache-non-unique-message)
569 (setq file-cache-last-completion string))
6b279740
RS
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))
add91c7b 576 (insert
6b279740
RS
577 (substring completion-string (length string)))
578 ;; Add our own setup function to the Completions Buffer
579 (let ((completion-setup-hook
d8e1753c
SM
580 (append completion-setup-hook
581 (list 'file-cache-completion-setup-function))))
6b279740 582 (with-output-to-temp-buffer file-cache-completions-buffer
d8e1753c 583 (display-completion-list completion-list string))))
6b279740 584 (setq file-cache-string (file-cache-file-name completion-string))
dea0a87d 585 (if (string= file-cache-string (minibuffer-contents))
20565545 586 (minibuffer-message file-cache-sole-match-message)
dea0a87d 587 (delete-minibuffer-contents)
add91c7b 588 (insert file-cache-string)
6b279740 589 (if file-cache-multiple-directory-message
20565545 590 (minibuffer-message file-cache-multiple-directory-message)))
6b279740 591 )))
24ccf465 592
6b279740
RS
593 ;; No match
594 ((eq completion-string nil)
20565545 595 (minibuffer-message file-cache-no-match-message)))))
6b279740
RS
596
597;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
598;; Completion functions
599;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
600
601(defun file-cache-completion-setup-function ()
d8e1753c
SM
602 (with-current-buffer standard-output ;; i.e. file-cache-completions-buffer
603 (use-local-map file-cache-completions-keymap)))
6b279740 604
ae732337 605(defun file-cache-choose-completion (&optional event)
6b279740 606 "Choose a completion in the `*Completions*' buffer."
ae732337 607 (interactive (list last-nonmenu-event))
6b279740 608 (let ((completion-no-auto-exit t))
ae732337 609 (choose-completion event)
6b279740 610 (select-window (active-minibuffer-window))
ae732337 611 (file-cache-minibuffer-complete nil)))
6b279740 612
ae732337
GM
613(define-obsolete-function-alias 'file-cache-mouse-choose-completion
614 'file-cache-choose-completion "23.2")
6b279740 615
57089611
PB
616(defun file-cache-complete ()
617 "Complete the word at point, using the filecache."
618 (interactive)
bed4c972 619 (let ((start
57089611
PB
620 (save-excursion
621 (skip-syntax-backward "^\"")
bed4c972
SM
622 (point))))
623 (completion-in-region start (point) file-cache-alist)))
57089611 624
b047c9b7
GM
625;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
626;; Show parts of the cache
627;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
628
629(defun file-cache-files-matching-internal (regexp)
630 "Output a list of files whose names (not including directories)
631match REGEXP."
632 (let ((results))
20565545
SM
633 (dolist (cache-element file-cache-alist)
634 (and (string-match regexp (elt cache-element 0))
635 (push (elt cache-element 0) results)))
636 (nreverse results)))
b047c9b7
GM
637
638(defun file-cache-files-matching (regexp)
639 "Output a list of files whose names (not including directories)
640match REGEXP."
641 (interactive "sFind files matching regexp: ")
24ccf465 642 (let ((results
b047c9b7
GM
643 (file-cache-files-matching-internal regexp))
644 buf)
24ccf465
PB
645 (set-buffer
646 (setq buf (get-buffer-create
b047c9b7
GM
647 "*File Cache Files Matching*")))
648 (erase-buffer)
649 (insert
650 (mapconcat
651 'identity
652 results
653 "\n"))
654 (goto-char (point-min))
655 (display-buffer buf)))
656
6b279740
RS
657;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
658;; Debugging functions
659;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
660
661(defun file-cache-debug-read-from-minibuffer (file)
662 "Debugging function."
24ccf465 663 (interactive
6b279740 664 (list (completing-read "File Cache: " file-cache-alist)))
3750be31 665 (message "%s" (assoc-string file file-cache-alist
20565545 666 file-cache-ignore-case)))
6b279740 667
57089611
PB
668(defun file-cache-display ()
669 "Display the file cache."
670 (interactive)
671 (let ((buf "*File Cache Contents*"))
672 (with-current-buffer
673 (get-buffer-create buf)
674 (erase-buffer)
20565545
SM
675 (dolist (item file-cache-alist)
676 (insert (nth 1 item) (nth 0 item) "\n"))
677 (pop-to-buffer buf))))
57089611 678
6b279740
RS
679;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
680;; Keybindings
681;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
682
6b279740
RS
683(provide 'filecache)
684
685;;; filecache.el ends here