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