Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / filecache.el
blob9dd631001da6933c33b07574822382c9a04d7d2a
1 ;;; filecache.el --- find files using a pre-loaded cache -*- lexical-binding:t -*-
3 ;; Copyright (C) 1996, 2000-2018 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 <https://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))
165 (defcustom file-cache-find-command "find"
166 "External program used by `file-cache-add-directory-using-find'."
167 :type 'string)
169 (defcustom file-cache-find-command-posix-flag 'not-defined
170 "Set to t, if `file-cache-find-command' handles wildcards POSIX style.
171 This variable is automatically set to nil or non-nil
172 if it has the initial value `not-defined' whenever you first
173 call the `file-cache-add-directory-using-find'.
175 Under Windows operating system where Cygwin is available, this value
176 should be t."
177 :type '(choice (const :tag "Yes" t)
178 (const :tag "No" nil)
179 (const :tag "Unknown" not-defined)))
181 (defcustom file-cache-locate-command "locate"
182 "External program used by `file-cache-add-directory-using-locate'."
183 :type 'string)
185 ;; Minibuffer messages
186 (defcustom file-cache-no-match-message " [File Cache: No match]"
187 "Message to display when there is no completion."
188 :type 'string)
190 (defcustom file-cache-sole-match-message " [File Cache: sole completion]"
191 "Message to display when there is only one completion."
192 :type 'string)
194 (defcustom file-cache-non-unique-message
195 " [File Cache: complete but not unique]"
196 "Message to display when there is a non-unique completion."
197 :type 'string)
199 (defcustom file-cache-completion-ignore-case
200 (if (memq system-type '(ms-dos windows-nt cygwin))
202 completion-ignore-case)
203 "If non-nil, file-cache completion should ignore case.
204 Defaults to the value of `completion-ignore-case'."
205 :type 'boolean)
207 (defcustom file-cache-case-fold-search
208 (if (memq system-type '(ms-dos windows-nt cygwin))
210 case-fold-search)
211 "If non-nil, file-cache completion should ignore case.
212 Defaults to the value of `case-fold-search'."
213 :type 'boolean)
215 (defcustom file-cache-ignore-case
216 (memq system-type '(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 'boolean)
221 (defvar file-cache-multiple-directory-message nil)
223 ;; Internal variables
224 ;; This should be named *Completions* because that's what the function
225 ;; switch-to-completions in simple.el expects
226 (defcustom file-cache-completions-buffer "*Completions*"
227 "Buffer to display completions when using the file cache."
228 :type 'string)
230 (defvar file-cache-buffer-default-regexp "^.+$"
231 "Regexp to match files in find and locate's output.")
233 (defvar file-cache-last-completion nil)
235 (defvar file-cache-alist nil
236 "Internal data structure to hold cache of file names.
237 It is a list of entries of the form (FILENAME DIRNAME1 DIRNAME2 ...)
238 where FILENAME is a file name component and the entry represents N
239 files of names DIRNAME1/FILENAME, DIRNAME2/FILENAME, ...")
241 (defvar file-cache-completions-keymap
242 (let ((map (make-sparse-keymap)))
243 (set-keymap-parent map completion-list-mode-map)
244 (define-key map [mouse-2] 'file-cache-choose-completion)
245 (define-key map "\C-m" 'file-cache-choose-completion)
246 map)
247 "Keymap for file cache completions buffer.")
249 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
250 ;; Functions to add files to the cache
251 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
253 (defun file-cache--read-list (file op-prompt)
254 (let* ((fun (if file 'read-file-name 'read-directory-name))
255 (type (if file "file" "directory"))
256 (prompt-1 (concat op-prompt " " type ": "))
257 (prompt-2 (concat op-prompt " another " type "?"))
258 (continue t)
259 result)
260 (while continue
261 (push (funcall fun prompt-1 nil nil t) result)
262 (setq continue (y-or-n-p prompt-2)))
263 (nreverse result)))
265 ;;;###autoload
266 (defun file-cache-add-directory (directory &optional regexp)
267 "Add all files in DIRECTORY to the file cache.
268 If called from Lisp with a non-nil REGEXP argument is non-nil,
269 only add files whose names match REGEXP."
270 (interactive (list (read-directory-name "Add files from directory: "
271 nil nil t)
272 nil))
273 ;; Not an error, because otherwise we can't use load-paths that
274 ;; contain non-existent directories.
275 (when (file-accessible-directory-p directory)
276 (let* ((dir (expand-file-name directory))
277 (dir-files (directory-files dir t regexp)))
278 ;; Filter out files we don't want to see
279 (dolist (file dir-files)
280 (if (file-directory-p file)
281 (setq dir-files (delq file dir-files))
282 (dolist (regexp file-cache-filter-regexps)
283 (if (string-match regexp file)
284 (setq dir-files (delq file dir-files))))))
285 (file-cache-add-file-list dir-files))))
287 ;;;###autoload
288 (defun file-cache-add-directory-list (directories &optional regexp)
289 "Add DIRECTORIES (a list of directory names) to the file cache.
290 If called interactively, read the directory names one by one.
291 If the optional REGEXP argument is non-nil, only files which match it
292 will be added to the cache. Note that the REGEXP is applied to the
293 files in each directory, not to the directory list itself."
294 (interactive (list (file-cache--read-list nil "Add")))
295 (dolist (dir directories)
296 (file-cache-add-directory dir regexp))
297 (let ((n (length directories)))
298 (message "Filecache: cached file names from %d director%s."
299 n (if (= n 1) "y" "ies"))))
301 (defun file-cache-add-file-list (files)
302 "Add FILES (a list of file names) to the file cache.
303 If called interactively, read the file names one by one."
304 (interactive (list (file-cache--read-list t "Add")))
305 (dolist (f files)
306 (file-cache-add-file f))
307 (let ((n (length files)))
308 (message "Filecache: cached %d file name%s."
309 n (if (= n 1) "" "s"))))
311 ;; Workhorse function
313 ;;;###autoload
314 (defun file-cache-add-file (file)
315 "Add FILE to the file cache."
316 (interactive "fAdd File: ")
317 (setq file (file-truename file))
318 (unless (file-exists-p file)
319 (error "Filecache: file %s does not exist" file))
320 (let* ((file-name (file-name-nondirectory file))
321 (dir-name (file-name-directory file))
322 (the-entry (assoc-string file-name file-cache-alist
323 file-cache-ignore-case)))
324 (cond ((null the-entry)
325 ;; If the entry wasn't in the cache, add it.
326 (push (list file-name dir-name) file-cache-alist)
327 (if (called-interactively-p 'interactive)
328 (message "Filecache: cached file name %s." file)))
329 ((not (member dir-name (cdr the-entry)))
330 (setcdr the-entry (cons dir-name (cdr the-entry)))
331 (if (called-interactively-p 'interactive)
332 (message "Filecache: cached file name %s." file)))
334 (if (called-interactively-p 'interactive)
335 (message "Filecache: %s is already cached." file))))))
337 ;;;###autoload
338 (defun file-cache-add-directory-using-find (directory)
339 "Use the `find' command to add files to the file cache.
340 Find is run in DIRECTORY."
341 (interactive "DAdd files under directory: ")
342 (let ((dir (expand-file-name directory)))
343 (when (memq system-type '(windows-nt cygwin))
344 (if (eq file-cache-find-command-posix-flag 'not-defined)
345 (setq file-cache-find-command-posix-flag
346 (executable-command-find-posix-p file-cache-find-command))))
347 (with-temp-buffer
348 (call-process file-cache-find-command nil t nil
349 dir "-name"
350 (if (memq system-type '(windows-nt cygwin))
351 (if file-cache-find-command-posix-flag
352 "\\*"
353 "'*'")
354 "*")
355 "-print")
356 (file-cache--add-from-buffer))))
358 ;;;###autoload
359 (defun file-cache-add-directory-using-locate (string)
360 "Use the `locate' command to add files to the file cache.
361 STRING is passed as an argument to the locate command."
362 (interactive "sAdd files using locate string: ")
363 (with-temp-buffer
364 (call-process file-cache-locate-command nil t nil string)
365 (file-cache--add-from-buffer)))
367 (autoload 'find-lisp-find-files "find-lisp")
369 ;;;###autoload
370 (defun file-cache-add-directory-recursively (dir &optional regexp)
371 "Add DIR and any subdirectories to the file-cache.
372 This function does not use any external programs.
373 If the optional REGEXP argument is non-nil, only files which match it
374 will be added to the cache. Note that the REGEXP is applied to the
375 files in each directory, not to the directory list itself."
376 (interactive "DAdd directory: ")
377 (mapcar
378 (lambda (file)
379 (or (file-directory-p file)
380 (let (filtered)
381 (dolist (regexp file-cache-filter-regexps)
382 (and (string-match regexp file)
383 (setq filtered t)))
384 filtered)
385 (file-cache-add-file file)))
386 (find-lisp-find-files dir (or regexp "^"))))
388 (defun file-cache--add-from-buffer ()
389 "Add any entries found in the current buffer.
390 Each entry matches the regular expression `file-cache-buffer-default-regexp'
391 or the optional REGEXP argument."
392 (dolist (elt file-cache-filter-regexps)
393 (goto-char (point-min))
394 (delete-matching-lines elt))
395 (goto-char (point-min))
396 (while (re-search-forward file-cache-buffer-default-regexp nil t)
397 (file-cache-add-file (match-string-no-properties 0))))
399 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
400 ;; Functions to delete from the cache
401 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
403 (defun file-cache-clear-cache ()
404 "Clear the file cache."
405 (interactive)
406 (setq file-cache-alist nil))
408 ;; This clears *all* files with the given name
409 (defun file-cache-delete-file (file)
410 "Delete FILE (a relative file name) from the file cache.
411 Return nil if FILE was not in the file cache, non-nil otherwise."
412 (interactive
413 (list (completing-read "Delete file from cache: " file-cache-alist)))
414 (let ((elt (assoc-string file file-cache-alist file-cache-ignore-case)))
415 (setq file-cache-alist (delq elt file-cache-alist))
416 elt))
418 (defun file-cache-delete-file-list (files &optional message)
419 "Delete FILES (a list of files) from the file cache.
420 If called interactively, read the file names one by one.
421 If MESSAGE is non-nil, or if called interactively, print a
422 message reporting the number of file names deleted."
423 (interactive (list (file-cache--read-list t "Uncache") t))
424 (let ((n 0))
425 (dolist (f files)
426 (if (file-cache-delete-file f)
427 (setq n (1+ n))))
428 (when message
429 (message "Filecache: uncached %d file name%s."
430 n (if (= n 1) "" "s")))))
432 (defun file-cache-delete-file-regexp (regexp)
433 "Delete files matching REGEXP from the file cache."
434 (interactive "sRegexp: ")
435 (let ((delete-list))
436 (dolist (elt file-cache-alist)
437 (and (string-match regexp (car elt))
438 (push (car elt) delete-list)))
439 (file-cache-delete-file-list delete-list)))
441 (defun file-cache-delete-directory (directory)
442 "Delete DIRECTORY from the file cache."
443 (interactive "DDelete directory from file cache: ")
444 (let ((dir (expand-file-name directory))
445 (n 0))
446 (dolist (entry file-cache-alist)
447 (if (file-cache-do-delete-directory dir entry)
448 (setq n (1+ n))))
449 (message "Filecache: uncached %d file name%s."
450 n (if (= n 1) "" "s"))))
452 (defun file-cache-do-delete-directory (dir entry)
453 (let ((directory-list (cdr entry))
454 (directory (file-cache-canonical-directory dir)))
455 (and (member directory directory-list)
456 (if (equal 1 (length directory-list))
457 (setq file-cache-alist
458 (delq entry file-cache-alist))
459 (setcdr entry (delete directory directory-list))))))
461 (defun file-cache-delete-directory-list (directories)
462 "Delete DIRECTORIES (a list of directory names) from the file cache.
463 If called interactively, read the directory names one by one."
464 (interactive (list (file-cache--read-list nil "Uncache")))
465 (dolist (d directories)
466 (file-cache-delete-directory d)))
468 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
469 ;; Utility functions
470 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
472 ;; Returns the name of a directory for a file in the cache
473 (defun file-cache-directory-name (file)
474 (let* ((directory-list (cdr (assoc-string
475 file file-cache-alist
476 file-cache-ignore-case)))
477 (len (length directory-list))
478 (directory)
479 (num))
480 (if (not (listp directory-list))
481 (error "Filecache: unknown type in file-cache-alist for key %s" file))
482 (cond
483 ;; Single element
484 ((eq 1 len)
485 (setq directory (elt directory-list 0)))
486 ;; No elements
487 ((eq 0 len)
488 (error "Filecache: no directory found for key %s" file))
489 ;; Multiple elements
491 (let* ((minibuffer-dir (file-name-directory (minibuffer-contents)))
492 (dir-list (member minibuffer-dir directory-list)))
493 (setq directory
494 ;; If the directory is in the list, return the next element
495 ;; Otherwise, return the first element
496 (if dir-list
497 (or (elt directory-list
498 (setq num (1+ (- len (length dir-list)))))
499 (elt directory-list (setq num 0)))
500 (elt directory-list (setq num 0)))))))
501 ;; If there were multiple directories, set up a minibuffer message
502 (setq file-cache-multiple-directory-message
503 (and num (format " [%d of %d]" (1+ num) len)))
504 directory))
506 ;; Returns the name of a file in the cache
507 (defun file-cache-file-name (file)
508 (let ((directory (file-cache-directory-name file)))
509 (concat directory file)))
511 ;; Return a canonical directory for comparison purposes.
512 ;; Such a directory ends with a forward slash.
513 (defun file-cache-canonical-directory (dir)
514 (let ((directory dir))
515 (if (not (char-equal ?/ (string-to-char (substring directory -1))))
516 (concat directory "/")
517 directory)))
519 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
520 ;; Minibuffer functions
521 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
523 ;; The prefix argument works around a bug in the minibuffer completion.
524 ;; The completion function doesn't distinguish between the states:
526 ;; "Multiple completions of name" (eg, Makefile, Makefile.in)
527 ;; "Name available in multiple directories" (/tmp/Makefile, ~me/Makefile)
529 ;; The default is to do the former; a prefix arg forces the latter.
531 ;;;###autoload
532 (defun file-cache-minibuffer-complete (arg)
533 "Complete a filename in the minibuffer using a preloaded cache.
534 Filecache does two kinds of substitution: it completes on names in
535 the cache, and, once it has found a unique name, it cycles through
536 the directories that the name is available in. With a prefix argument,
537 the name is considered already unique; only the second substitution
538 \(directories) is done."
539 (interactive "P")
540 (let* ((completion-ignore-case file-cache-completion-ignore-case)
541 (case-fold-search file-cache-case-fold-search)
542 (string (file-name-nondirectory (minibuffer-contents)))
543 (completion (completion-try-completion
544 string file-cache-alist nil 0)))
545 (cond
546 ;; If it's the only match, replace the original contents
547 ((or arg (eq completion t))
548 (let ((file-name (file-cache-file-name string)))
549 (if (string= file-name (minibuffer-contents))
550 (minibuffer-message file-cache-sole-match-message)
551 (delete-minibuffer-contents)
552 (insert file-name)
553 (if file-cache-multiple-directory-message
554 (minibuffer-message file-cache-multiple-directory-message)))))
556 ;; If it's the longest match, insert it
557 ((consp completion)
558 (let ((newstring (car completion))
559 (newpoint (cdr completion)))
560 ;; If we've already inserted a unique string, see if the user
561 ;; wants to use that one
562 (if (and (string= string newstring)
563 (assoc-string string file-cache-alist
564 file-cache-ignore-case))
565 (if (and (eq last-command this-command)
566 (string= file-cache-last-completion newstring))
567 (progn
568 (delete-minibuffer-contents)
569 (insert (file-cache-file-name newstring))
570 (setq file-cache-last-completion nil))
571 (minibuffer-message file-cache-non-unique-message)
572 (setq file-cache-last-completion string))
573 (setq file-cache-last-completion string)
574 (let* ((completion-list (completion-all-completions
575 newstring file-cache-alist nil newpoint))
576 (base-size (cdr (last completion-list))))
577 (when base-size
578 (setcdr (last completion-list) nil))
579 (if (> (length completion-list) 1)
580 (progn
581 (delete-region (- (point-max) (length string)) (point-max))
582 (save-excursion (insert newstring))
583 (forward-char newpoint)
584 (with-output-to-temp-buffer file-cache-completions-buffer
585 (display-completion-list completion-list)
586 ;; Add our own setup function to the Completions Buffer
587 (file-cache-completion-setup-function)))
588 (let ((file-name (file-cache-file-name newstring)))
589 (if (string= file-name (minibuffer-contents))
590 (minibuffer-message file-cache-sole-match-message)
591 (delete-minibuffer-contents)
592 (insert file-name)
593 (if file-cache-multiple-directory-message
594 (minibuffer-message
595 file-cache-multiple-directory-message)))))))))
597 ;; No match
598 ((eq completion nil)
599 (minibuffer-message file-cache-no-match-message)))))
601 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
602 ;; Completion functions
603 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
605 (defun file-cache-completion-setup-function ()
606 (with-current-buffer standard-output ;; i.e. file-cache-completions-buffer
607 (use-local-map file-cache-completions-keymap)))
609 (defun file-cache-choose-completion (&optional event)
610 "Choose a completion in the `*Completions*' buffer."
611 (interactive (list last-nonmenu-event))
612 (let ((completion-no-auto-exit t))
613 (choose-completion event)
614 (select-window (active-minibuffer-window))
615 (file-cache-minibuffer-complete nil)))
617 (define-obsolete-function-alias 'file-cache-mouse-choose-completion
618 #'file-cache-choose-completion "23.2")
620 (defun file-cache-complete ()
621 "Complete the word at point, using the filecache."
622 (interactive)
623 (let ((start
624 (save-excursion
625 (skip-syntax-backward "^\"")
626 (point))))
627 (completion-in-region start (point) file-cache-alist)))
629 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
630 ;; Show parts of the cache
631 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
633 (defun file-cache-files-matching-internal (regexp)
634 "Output a list of files whose names (not including directories)
635 match REGEXP."
636 (let ((results))
637 (dolist (cache-element file-cache-alist)
638 (and (string-match regexp (elt cache-element 0))
639 (push (elt cache-element 0) results)))
640 (nreverse results)))
642 (defun file-cache-files-matching (regexp)
643 "Output a list of files whose names (not including directories)
644 match REGEXP."
645 (interactive "sFind files matching regexp: ")
646 (let ((results
647 (file-cache-files-matching-internal regexp))
648 buf)
649 (set-buffer
650 (setq buf (get-buffer-create
651 "*File Cache Files Matching*")))
652 (erase-buffer)
653 (insert
654 (mapconcat #'identity results "\n"))
655 (goto-char (point-min))
656 (display-buffer buf)))
658 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
659 ;; Debugging functions
660 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
662 (defun file-cache-debug-read-from-minibuffer (file)
663 "Debugging function."
664 (interactive
665 (list (completing-read "File Cache: " file-cache-alist)))
666 (message "%s" (assoc-string file file-cache-alist
667 file-cache-ignore-case)))
669 (defun file-cache-display ()
670 "Display the file cache."
671 (interactive)
672 (let ((buf "*File Cache Contents*"))
673 (with-current-buffer
674 (get-buffer-create buf)
675 (erase-buffer)
676 (dolist (item file-cache-alist)
677 (insert (nth 1 item) (nth 0 item) "\n"))
678 (pop-to-buffer buf))))
680 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
681 ;; Keybindings
682 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
684 (provide 'filecache)
686 ;;; filecache.el ends here