Compare and round more carefully
[emacs.git] / lisp / filecache.el
blob02b5f79c07a2d7e47bb8f552494a6bf01b4f1a79
1 ;;; filecache.el --- find files using a pre-loaded cache
3 ;; Copyright (C) 1996, 2000-2017 Free Software Foundation, Inc.
5 ;; Author: Peter Breton <pbreton@cs.umb.edu>
6 ;; Created: Sun Nov 10 1996
7 ;; Keywords: convenience
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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
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.
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.
40 ;; ADDING FILES TO THE CACHE:
42 ;; Use the following functions to add items to the file cache:
44 ;; * `file-cache-add-file': Adds a single file to the cache
46 ;; * `file-cache-add-file-list': Adds a list of files to the cache
48 ;; The following functions use the regular expressions in
49 ;; `file-cache-delete-regexps' to eliminate unwanted files:
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.
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.
58 ;; * `file-cache-add-directory-using-find': Uses the `find' command to
59 ;; add a directory tree to the cache.
61 ;; * `file-cache-add-directory-using-locate': Uses the `locate' command to
62 ;; add files matching a pattern to the cache.
64 ;; * `file-cache-add-directory-recursively': Uses the find-lisp package to
65 ;; add all files matching a pattern to the cache.
67 ;; Use the function `file-cache-clear-cache' to remove all items from the
68 ;; cache. There are a number of `file-cache-delete' functions provided
69 ;; as well, but in general it is probably better to not worry too much
70 ;; about extra files in the cache.
72 ;; The most convenient way to initialize the cache is with an
73 ;; `eval-after-load' function, as noted in the ADDING FILES
74 ;; AUTOMATICALLY section.
76 ;; FINDING FILES USING THE CACHE:
78 ;; You can use the file-cache with any function that expects a filename as
79 ;; an argument. For example:
81 ;; 1) Invoke a function which expects a filename as an argument:
82 ;; M-x find-file
84 ;; 2) Begin typing a file name.
86 ;; 3) Invoke `file-cache-minibuffer-complete' (bound by default to
87 ;; C-TAB) to complete on the filename using the cache.
89 ;; 4) When you have found a unique completion, the minibuffer contents
90 ;; will change to the full name of that file.
92 ;; If there are a number of directories which contain the completion,
93 ;; invoking `file-cache-minibuffer-complete' repeatedly will cycle through
94 ;; them.
96 ;; 5) You can then edit the minibuffer contents, or press RETURN.
98 ;; It is much easier to simply try it than trying to explain it :)
100 ;;; ADDING FILES AUTOMATICALLY
102 ;; For maximum utility, you should probably define an `eval-after-load'
103 ;; form which loads your favorite files:
105 ;; (eval-after-load
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 ;; ))
115 ;; If you clear and reload the cache frequently, it is probably easiest
116 ;; to put your initializations in a function:
118 ;; (eval-after-load
119 ;; "filecache"
120 ;; '(my-file-cache-initialize))
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 ;; ))
131 ;; Of course, you can still add files to the cache afterwards, via
132 ;; Lisp functions.
134 ;; RELATED WORK:
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.
140 ;;; Code:
142 (defgroup file-cache nil
143 "Find files using a pre-loaded cache."
144 :group 'files
145 :group 'convenience
146 :prefix "file-cache-")
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149 ;; Customization Variables
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 ;; User-modifiable variables
153 (defcustom file-cache-filter-regexps
154 ;; These are also used in buffers containing lines of file names,
155 ;; so the end-of-name is matched with $ rather than \\'.
156 (list "~$" "\\.o$" "\\.exe$" "\\.a$" "\\.elc$" ",v$" "\\.output$"
157 "\\.$" "#$" "\\.class$" "/\\.#")
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 :version "25.1" ; added "/\\.#"
163 :type '(repeat regexp)
164 :group 'file-cache)
166 (defcustom file-cache-find-command "find"
167 "External program used by `file-cache-add-directory-using-find'."
168 :type 'string
169 :group 'file-cache)
171 (defcustom file-cache-find-command-posix-flag 'not-defined
172 "Set to t, if `file-cache-find-command' handles wildcards POSIX style.
173 This variable is automatically set to nil or non-nil
174 if it has the initial value `not-defined' whenever you first
175 call the `file-cache-add-directory-using-find'.
177 Under Windows operating system where Cygwin is available, this value
178 should be t."
179 :type '(choice (const :tag "Yes" t)
180 (const :tag "No" nil)
181 (const :tag "Unknown" not-defined))
182 :group 'file-cache)
184 (defcustom file-cache-locate-command "locate"
185 "External program used by `file-cache-add-directory-using-locate'."
186 :type 'string
187 :group 'file-cache)
189 ;; Minibuffer messages
190 (defcustom file-cache-no-match-message " [File Cache: No match]"
191 "Message to display when there is no completion."
192 :type 'string
193 :group 'file-cache)
195 (defcustom file-cache-sole-match-message " [File Cache: sole completion]"
196 "Message to display when there is only one completion."
197 :type 'string
198 :group 'file-cache)
200 (defcustom file-cache-non-unique-message
201 " [File Cache: complete but not unique]"
202 "Message to display when there is a non-unique completion."
203 :type 'string
204 :group 'file-cache)
206 (defcustom file-cache-completion-ignore-case
207 (if (memq system-type '(ms-dos windows-nt cygwin))
209 completion-ignore-case)
210 "If non-nil, file-cache completion should ignore case.
211 Defaults to the value of `completion-ignore-case'."
212 :type 'boolean
213 :group 'file-cache)
215 (defcustom file-cache-case-fold-search
216 (if (memq system-type '(ms-dos windows-nt cygwin))
218 case-fold-search)
219 "If non-nil, file-cache completion should ignore case.
220 Defaults to the value of `case-fold-search'."
221 :type 'boolean
222 :group 'file-cache)
224 (defcustom file-cache-ignore-case
225 (memq system-type '(ms-dos windows-nt cygwin))
226 "Non-nil means ignore case when checking completions in the file cache.
227 Defaults to nil on DOS and Windows, and t on other systems."
228 :type 'boolean
229 :group 'file-cache)
231 (defvar file-cache-multiple-directory-message nil)
233 ;; Internal variables
234 ;; This should be named *Completions* because that's what the function
235 ;; switch-to-completions in simple.el expects
236 (defcustom file-cache-completions-buffer "*Completions*"
237 "Buffer to display completions when using the file cache."
238 :type 'string
239 :group 'file-cache)
241 (defcustom file-cache-buffer "*File Cache*"
242 "Buffer to hold the cache of file names."
243 :type 'string
244 :group 'file-cache)
246 (defcustom file-cache-buffer-default-regexp "^.+$"
247 "Regexp to match files in `file-cache-buffer'."
248 :type 'regexp
249 :group 'file-cache)
251 (defvar file-cache-last-completion nil)
253 (defvar file-cache-alist nil
254 "Internal data structure to hold cache of file names.
255 It is a list of entries of the form (FILENAME DIRNAME1 DIRNAME2 ...)
256 where FILENAME is a file name component and the entry represents N
257 files of names DIRNAME1/FILENAME, DIRNAME2/FILENAME, ...")
259 (defvar file-cache-completions-keymap
260 (let ((map (make-sparse-keymap)))
261 (set-keymap-parent map completion-list-mode-map)
262 (define-key map [mouse-2] 'file-cache-choose-completion)
263 (define-key map "\C-m" 'file-cache-choose-completion)
264 map)
265 "Keymap for file cache completions buffer.")
267 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
268 ;; Functions to add files to the cache
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
271 (defun file-cache--read-list (file op-prompt)
272 (let* ((fun (if file 'read-file-name 'read-directory-name))
273 (type (if file "file" "directory"))
274 (prompt-1 (concat op-prompt " " type ": "))
275 (prompt-2 (concat op-prompt " another " type "?"))
276 (continue t)
277 result)
278 (while continue
279 (push (funcall fun prompt-1 nil nil t) result)
280 (setq continue (y-or-n-p prompt-2)))
281 (nreverse result)))
283 ;;;###autoload
284 (defun file-cache-add-directory (directory &optional regexp)
285 "Add all files in DIRECTORY to the file cache.
286 If called from Lisp with a non-nil REGEXP argument is non-nil,
287 only add files whose names match REGEXP."
288 (interactive (list (read-directory-name "Add files from directory: "
289 nil nil t)
290 nil))
291 ;; Not an error, because otherwise we can't use load-paths that
292 ;; contain non-existent directories.
293 (when (file-accessible-directory-p directory)
294 (let* ((dir (expand-file-name directory))
295 (dir-files (directory-files dir t regexp)))
296 ;; Filter out files we don't want to see
297 (dolist (file dir-files)
298 (if (file-directory-p file)
299 (setq dir-files (delq file dir-files))
300 (dolist (regexp file-cache-filter-regexps)
301 (if (string-match regexp file)
302 (setq dir-files (delq file dir-files))))))
303 (file-cache-add-file-list dir-files))))
305 ;;;###autoload
306 (defun file-cache-add-directory-list (directories &optional regexp)
307 "Add DIRECTORIES (a list of directory names) to the file cache.
308 If called interactively, read the directory names one by one.
309 If the optional REGEXP argument is non-nil, only files which match it
310 will be added to the cache. Note that the REGEXP is applied to the
311 files in each directory, not to the directory list itself."
312 (interactive (list (file-cache--read-list nil "Add")))
313 (dolist (dir directories)
314 (file-cache-add-directory dir regexp))
315 (let ((n (length directories)))
316 (message "Filecache: cached file names from %d director%s."
317 n (if (= n 1) "y" "ies"))))
319 (defun file-cache-add-file-list (files)
320 "Add FILES (a list of file names) to the file cache.
321 If called interactively, read the file names one by one."
322 (interactive (list (file-cache--read-list t "Add")))
323 (dolist (f files)
324 (file-cache-add-file f))
325 (let ((n (length files)))
326 (message "Filecache: cached %d file name%s."
327 n (if (= n 1) "" "s"))))
329 ;; Workhorse function
331 ;;;###autoload
332 (defun file-cache-add-file (file)
333 "Add FILE to the file cache."
334 (interactive "fAdd File: ")
335 (setq file (file-truename file))
336 (unless (file-exists-p file)
337 (error "Filecache: file %s does not exist" file))
338 (let* ((file-name (file-name-nondirectory file))
339 (dir-name (file-name-directory file))
340 (the-entry (assoc-string file-name file-cache-alist
341 file-cache-ignore-case)))
342 (cond ((null the-entry)
343 ;; If the entry wasn't in the cache, add it.
344 (push (list file-name dir-name) file-cache-alist)
345 (if (called-interactively-p 'interactive)
346 (message "Filecache: cached file name %s." file)))
347 ((not (member dir-name (cdr the-entry)))
348 (setcdr the-entry (cons dir-name (cdr the-entry)))
349 (if (called-interactively-p 'interactive)
350 (message "Filecache: cached file name %s." file)))
352 (if (called-interactively-p 'interactive)
353 (message "Filecache: %s is already cached." file))))))
355 ;;;###autoload
356 (defun file-cache-add-directory-using-find (directory)
357 "Use the `find' command to add files to the file cache.
358 Find is run in DIRECTORY."
359 (interactive "DAdd files under directory: ")
360 (let ((dir (expand-file-name directory)))
361 (when (memq system-type '(windows-nt cygwin))
362 (if (eq file-cache-find-command-posix-flag 'not-defined)
363 (setq file-cache-find-command-posix-flag
364 (executable-command-find-posix-p file-cache-find-command))))
365 (set-buffer (get-buffer-create file-cache-buffer))
366 (erase-buffer)
367 (call-process file-cache-find-command nil
368 (get-buffer file-cache-buffer) nil
369 dir "-name"
370 (if (memq system-type '(windows-nt cygwin))
371 (if file-cache-find-command-posix-flag
372 "\\*"
373 "'*'")
374 "*")
375 "-print")
376 (file-cache-add-from-file-cache-buffer)))
378 ;;;###autoload
379 (defun file-cache-add-directory-using-locate (string)
380 "Use the `locate' command to add files to the file cache.
381 STRING is passed as an argument to the locate command."
382 (interactive "sAdd files using locate string: ")
383 (set-buffer (get-buffer-create file-cache-buffer))
384 (erase-buffer)
385 (call-process file-cache-locate-command nil
386 (get-buffer file-cache-buffer) nil
387 string)
388 (file-cache-add-from-file-cache-buffer))
390 (autoload 'find-lisp-find-files "find-lisp")
392 ;;;###autoload
393 (defun file-cache-add-directory-recursively (dir &optional regexp)
394 "Adds DIR and any subdirectories to the file-cache.
395 This function does not use any external programs.
396 If the optional REGEXP argument is non-nil, only files which match it
397 will be added to the cache. Note that the REGEXP is applied to the
398 files in each directory, not to the directory list itself."
399 (interactive "DAdd directory: ")
400 (mapcar
401 (lambda (file)
402 (or (file-directory-p file)
403 (let (filtered)
404 (dolist (regexp file-cache-filter-regexps)
405 (and (string-match regexp file)
406 (setq filtered t)))
407 filtered)
408 (file-cache-add-file file)))
409 (find-lisp-find-files dir (or regexp "^"))))
411 (defun file-cache-add-from-file-cache-buffer (&optional regexp)
412 "Add any entries found in the file cache buffer.
413 Each entry matches the regular expression `file-cache-buffer-default-regexp'
414 or the optional REGEXP argument."
415 (set-buffer file-cache-buffer)
416 (dolist (elt file-cache-filter-regexps)
417 (goto-char (point-min))
418 (delete-matching-lines elt))
419 (goto-char (point-min))
420 (let ((full-filename))
421 (while (re-search-forward
422 (or regexp file-cache-buffer-default-regexp)
423 (point-max) t)
424 (setq full-filename (buffer-substring-no-properties
425 (match-beginning 0) (match-end 0)))
426 (file-cache-add-file full-filename))))
428 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
429 ;; Functions to delete from the cache
430 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
432 (defun file-cache-clear-cache ()
433 "Clear the file cache."
434 (interactive)
435 (setq file-cache-alist nil))
437 ;; This clears *all* files with the given name
438 (defun file-cache-delete-file (file)
439 "Delete FILE (a relative file name) from the file cache.
440 Return nil if FILE was not in the file cache, non-nil otherwise."
441 (interactive
442 (list (completing-read "Delete file from cache: " file-cache-alist)))
443 (let ((elt (assoc-string file file-cache-alist file-cache-ignore-case)))
444 (setq file-cache-alist (delq elt file-cache-alist))
445 elt))
447 (defun file-cache-delete-file-list (files &optional message)
448 "Delete FILES (a list of files) from the file cache.
449 If called interactively, read the file names one by one.
450 If MESSAGE is non-nil, or if called interactively, print a
451 message reporting the number of file names deleted."
452 (interactive (list (file-cache--read-list t "Uncache") t))
453 (let ((n 0))
454 (dolist (f files)
455 (if (file-cache-delete-file f)
456 (setq n (1+ n))))
457 (when message
458 (message "Filecache: uncached %d file name%s."
459 n (if (= n 1) "" "s")))))
461 (defun file-cache-delete-file-regexp (regexp)
462 "Delete files matching REGEXP from the file cache."
463 (interactive "sRegexp: ")
464 (let ((delete-list))
465 (dolist (elt file-cache-alist)
466 (and (string-match regexp (car elt))
467 (push (car elt) delete-list)))
468 (file-cache-delete-file-list delete-list)))
470 (defun file-cache-delete-directory (directory)
471 "Delete DIRECTORY from the file cache."
472 (interactive "DDelete directory from file cache: ")
473 (let ((dir (expand-file-name directory))
474 (n 0))
475 (dolist (entry file-cache-alist)
476 (if (file-cache-do-delete-directory dir entry)
477 (setq n (1+ n))))
478 (message "Filecache: uncached %d file name%s."
479 n (if (= n 1) "" "s"))))
481 (defun file-cache-do-delete-directory (dir entry)
482 (let ((directory-list (cdr entry))
483 (directory (file-cache-canonical-directory dir)))
484 (and (member directory directory-list)
485 (if (equal 1 (length directory-list))
486 (setq file-cache-alist
487 (delq entry file-cache-alist))
488 (setcdr entry (delete directory directory-list))))))
490 (defun file-cache-delete-directory-list (directories)
491 "Delete DIRECTORIES (a list of directory names) from the file cache.
492 If called interactively, read the directory names one by one."
493 (interactive (list (file-cache--read-list nil "Uncache")))
494 (dolist (d directories)
495 (file-cache-delete-directory d)))
497 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
498 ;; Utility functions
499 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
501 ;; Returns the name of a directory for a file in the cache
502 (defun file-cache-directory-name (file)
503 (let* ((directory-list (cdr (assoc-string
504 file file-cache-alist
505 file-cache-ignore-case)))
506 (len (length directory-list))
507 (directory)
508 (num))
509 (if (not (listp directory-list))
510 (error "Filecache: unknown type in file-cache-alist for key %s" file))
511 (cond
512 ;; Single element
513 ((eq 1 len)
514 (setq directory (elt directory-list 0)))
515 ;; No elements
516 ((eq 0 len)
517 (error "Filecache: no directory found for key %s" file))
518 ;; Multiple elements
520 (let* ((minibuffer-dir (file-name-directory (minibuffer-contents)))
521 (dir-list (member minibuffer-dir directory-list)))
522 (setq directory
523 ;; If the directory is in the list, return the next element
524 ;; Otherwise, return the first element
525 (if dir-list
526 (or (elt directory-list
527 (setq num (1+ (- len (length dir-list)))))
528 (elt directory-list (setq num 0)))
529 (elt directory-list (setq num 0)))))))
530 ;; If there were multiple directories, set up a minibuffer message
531 (setq file-cache-multiple-directory-message
532 (and num (format " [%d of %d]" (1+ num) len)))
533 directory))
535 ;; Returns the name of a file in the cache
536 (defun file-cache-file-name (file)
537 (let ((directory (file-cache-directory-name file)))
538 (concat directory file)))
540 ;; Return a canonical directory for comparison purposes.
541 ;; Such a directory ends with a forward slash.
542 (defun file-cache-canonical-directory (dir)
543 (let ((directory dir))
544 (if (not (char-equal ?/ (string-to-char (substring directory -1))))
545 (concat directory "/")
546 directory)))
548 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
549 ;; Minibuffer functions
550 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
552 ;; The prefix argument works around a bug in the minibuffer completion.
553 ;; The completion function doesn't distinguish between the states:
555 ;; "Multiple completions of name" (eg, Makefile, Makefile.in)
556 ;; "Name available in multiple directories" (/tmp/Makefile, ~me/Makefile)
558 ;; The default is to do the former; a prefix arg forces the latter.
560 ;;;###autoload
561 (defun file-cache-minibuffer-complete (arg)
562 "Complete a filename in the minibuffer using a preloaded cache.
563 Filecache does two kinds of substitution: it completes on names in
564 the cache, and, once it has found a unique name, it cycles through
565 the directories that the name is available in. With a prefix argument,
566 the name is considered already unique; only the second substitution
567 \(directories) is done."
568 (interactive "P")
569 (let*
571 (completion-ignore-case file-cache-completion-ignore-case)
572 (case-fold-search file-cache-case-fold-search)
573 (string (file-name-nondirectory (minibuffer-contents)))
574 (completion-string (try-completion string file-cache-alist))
575 (completion-list)
576 (len)
577 (file-cache-string))
578 (cond
579 ;; If it's the only match, replace the original contents
580 ((or arg (eq completion-string t))
581 (setq file-cache-string (file-cache-file-name string))
582 (if (string= file-cache-string (minibuffer-contents))
583 (minibuffer-message file-cache-sole-match-message)
584 (delete-minibuffer-contents)
585 (insert file-cache-string)
586 (if file-cache-multiple-directory-message
587 (minibuffer-message file-cache-multiple-directory-message))))
589 ;; If it's the longest match, insert it
590 ((stringp completion-string)
591 ;; If we've already inserted a unique string, see if the user
592 ;; wants to use that one
593 (if (and (string= string completion-string)
594 (assoc-string string file-cache-alist
595 file-cache-ignore-case))
596 (if (and (eq last-command this-command)
597 (string= file-cache-last-completion completion-string))
598 (progn
599 (delete-minibuffer-contents)
600 (insert (file-cache-file-name completion-string))
601 (setq file-cache-last-completion nil))
602 (minibuffer-message file-cache-non-unique-message)
603 (setq file-cache-last-completion string))
604 (setq file-cache-last-completion string)
605 (setq completion-list (all-completions string file-cache-alist)
606 len (length completion-list))
607 (if (> len 1)
608 (progn
609 (goto-char (point-max))
610 (insert
611 (substring completion-string (length string)))
612 ;; Add our own setup function to the Completions Buffer
613 (let ((completion-setup-hook
614 (append completion-setup-hook
615 (list 'file-cache-completion-setup-function))))
616 (with-output-to-temp-buffer file-cache-completions-buffer
617 (display-completion-list
618 (completion-hilit-commonality completion-list
619 (length string))))))
620 (setq file-cache-string (file-cache-file-name completion-string))
621 (if (string= file-cache-string (minibuffer-contents))
622 (minibuffer-message file-cache-sole-match-message)
623 (delete-minibuffer-contents)
624 (insert file-cache-string)
625 (if file-cache-multiple-directory-message
626 (minibuffer-message file-cache-multiple-directory-message)))
629 ;; No match
630 ((eq completion-string nil)
631 (minibuffer-message file-cache-no-match-message)))))
633 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
634 ;; Completion functions
635 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
637 (defun file-cache-completion-setup-function ()
638 (with-current-buffer standard-output ;; i.e. file-cache-completions-buffer
639 (use-local-map file-cache-completions-keymap)))
641 (defun file-cache-choose-completion (&optional event)
642 "Choose a completion in the `*Completions*' buffer."
643 (interactive (list last-nonmenu-event))
644 (let ((completion-no-auto-exit t))
645 (choose-completion event)
646 (select-window (active-minibuffer-window))
647 (file-cache-minibuffer-complete nil)))
649 (define-obsolete-function-alias 'file-cache-mouse-choose-completion
650 'file-cache-choose-completion "23.2")
652 (defun file-cache-complete ()
653 "Complete the word at point, using the filecache."
654 (interactive)
655 (let ((start
656 (save-excursion
657 (skip-syntax-backward "^\"")
658 (point))))
659 (completion-in-region start (point) file-cache-alist)))
661 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
662 ;; Show parts of the cache
663 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
665 (defun file-cache-files-matching-internal (regexp)
666 "Output a list of files whose names (not including directories)
667 match REGEXP."
668 (let ((results))
669 (dolist (cache-element file-cache-alist)
670 (and (string-match regexp (elt cache-element 0))
671 (push (elt cache-element 0) results)))
672 (nreverse results)))
674 (defun file-cache-files-matching (regexp)
675 "Output a list of files whose names (not including directories)
676 match REGEXP."
677 (interactive "sFind files matching regexp: ")
678 (let ((results
679 (file-cache-files-matching-internal regexp))
680 buf)
681 (set-buffer
682 (setq buf (get-buffer-create
683 "*File Cache Files Matching*")))
684 (erase-buffer)
685 (insert
686 (mapconcat #'identity results "\n"))
687 (goto-char (point-min))
688 (display-buffer buf)))
690 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
691 ;; Debugging functions
692 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
694 (defun file-cache-debug-read-from-minibuffer (file)
695 "Debugging function."
696 (interactive
697 (list (completing-read "File Cache: " file-cache-alist)))
698 (message "%s" (assoc-string file file-cache-alist
699 file-cache-ignore-case)))
701 (defun file-cache-display ()
702 "Display the file cache."
703 (interactive)
704 (let ((buf "*File Cache Contents*"))
705 (with-current-buffer
706 (get-buffer-create buf)
707 (erase-buffer)
708 (dolist (item file-cache-alist)
709 (insert (nth 1 item) (nth 0 item) "\n"))
710 (pop-to-buffer buf))))
712 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
713 ;; Keybindings
714 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
716 (provide 'filecache)
718 ;;; filecache.el ends here