Mercurial > emacs
annotate lisp/filecache.el @ 20843:b60871b4710f
Use aref instead of sref.
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Wed, 04 Feb 1998 11:25:47 +0000 |
parents | 886039ba7b3e |
children | cc3f3c1ea725 |
rev | line source |
---|---|
18388 | 1 ;;; filecache.el --- Find files using a pre-loaded cache |
2 ;; | |
3 ;; Author: Peter Breton | |
4 ;; Created: Sun Nov 10 1996 | |
5 ;; Keywords: | |
6 ;; Time-stamp: <97/02/07 17:26:54 peter> | |
7 ;; | |
20578
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
8 ;; Copyright (C) 1996 Free Software Foundation, Inc. |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
9 |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
10 ;; This file is part of GNU Emacs. |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
11 |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
12 ;; GNU Emacs is free software; you can redistribute it and/or modify |
18388 | 13 ;; it under the terms of the GNU General Public License as published by |
14 ;; the Free Software Foundation; either version 2, or (at your option) | |
15 ;; any later version. | |
20578
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
16 |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
17 ;; GNU Emacs is distributed in the hope that it will be useful, |
18388 | 18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
20578
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
20 ;; GNU General Public License for more details. |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
21 |
18388 | 22 ;; You should have received a copy of the GNU General Public License |
20578
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
25 ;; Boston, MA 02111-1307, USA. |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
26 |
18388 | 27 ;;; Commentary: |
28 ;; | |
29 ;; The file-cache package is an attempt to make it easy to locate files | |
30 ;; by name, without having to remember exactly where they are located. | |
31 ;; This is very handy when working with source trees. You can also add | |
32 ;; frequently used files to the cache to create a hotlist effect. | |
33 ;; The cache can be used with any interactive command which takes a | |
34 ;; filename as an argument. | |
35 ;; | |
36 ;; It is worth noting that this package works best when most of the files | |
37 ;; in the cache have unique names, or (if they have the same name) exist in | |
38 ;; only a few directories. The worst case is many files all with | |
39 ;; the same name and in different directories, for example a big source tree | |
40 ;; with a Makefile in each directory. In such a case, you should probably | |
41 ;; use an alternate strategy to find the files. | |
42 ;; | |
43 ;; ADDING FILES TO THE CACHE: | |
44 ;; | |
45 ;; Use the following functions to add items to the file cache: | |
46 ;; | |
47 ;; * `file-cache-add-file': Adds a single file to the cache | |
48 ;; | |
49 ;; * `file-cache-add-file-list': Adds a list of files to the cache | |
50 ;; | |
51 ;; The following functions use the regular expressions in | |
52 ;; `file-cache-delete-regexps' to eliminate unwanted files: | |
53 ;; | |
54 ;; * `file-cache-add-directory': Adds the files in a directory to the | |
55 ;; cache. You can also specify a regular expression to match the files | |
56 ;; which should be added. | |
57 ;; | |
58 ;; * `file-cache-add-directory-list': Same as above, but acts on a list | |
59 ;; of directories. You can use `load-path', `exec-path' and the like. | |
60 ;; | |
61 ;; * `file-cache-add-directory-using-find': Uses the `find' command to | |
62 ;; add a directory tree to the cache. | |
63 ;; | |
64 ;; * `file-cache-add-directory-using-locate': Uses the `locate' command to | |
65 ;; add files matching a pattern to the cache. | |
66 ;; | |
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 | |
73 ;; `eval-after-load' function, as noted in the INSTALLATION section. | |
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. | |
90 ;; | |
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 ;; | |
99 ;;; INSTALLATION | |
100 ;; | |
101 ;; Insert the following into your .emacs: | |
102 ;; | |
103 ;; (autoload 'file-cache-minibuffer-complete "filecache" nil t) | |
104 ;; | |
105 ;; For maximum utility, you should probably define an `eval-after-load' | |
106 ;; form which loads your favorite files: | |
107 ;; | |
108 ;; (eval-after-load | |
109 ;; "filecache" | |
110 ;; '(progn | |
111 ;; (message "Loading file cache...") | |
112 ;; (file-cache-add-directory-using-find "~/projects") | |
113 ;; (file-cache-add-directory-list load-path) | |
114 ;; (file-cache-add-directory "~/") | |
115 ;; (file-cache-add-file-list (list "~/foo/bar" "~/baz/bar")) | |
116 ;; )) | |
117 ;; | |
118 ;; If you clear and reload the cache frequently, it is probably easiest | |
119 ;; to put your initializations in a function: | |
120 ;; | |
121 ;; (eval-after-load | |
122 ;; "filecache" | |
123 ;; '(my-file-cache-initialize)) | |
124 ;; | |
125 ;; (defun my-file-cache-initialize () | |
126 ;; (interactive) | |
127 ;; (message "Loading file cache...") | |
128 ;; (file-cache-add-directory-using-find "~/projects") | |
129 ;; (file-cache-add-directory-list load-path) | |
130 ;; (file-cache-add-directory "~/") | |
131 ;; (file-cache-add-file-list (list "~/foo/bar" "~/baz/bar")) | |
132 ;; )) | |
133 ;; | |
134 ;; Of course, you can still add files to the cache afterwards, via | |
135 ;; Lisp functions. | |
136 ;; | |
137 ;; RELATED WORK: | |
138 ;; | |
139 ;; This package is a distant relative of Noah Friedman's fff utilities. | |
140 ;; Our goal is pretty similar, but the implementation strategies are | |
141 ;; different. | |
20578
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
142 |
18388 | 143 ;;; Code: |
144 | |
20597 | 145 (defgroup file-cache nil |
146 "Find files using a pre-loaded cache." | |
147 :group 'files | |
148 :prefix "file-cache-") | |
149 | |
18388 | 150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
151 ;; Variables | |
152 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
153 | |
154 ;; User-modifiable variables | |
20597 | 155 (defcustom file-cache-filter-regexps |
18388 | 156 (list "~$" "\\.o$" "\\.exe$" "\\.a$" "\\.elc$" ",v$" "\\.output$" |
157 "\\.$" "#$") | |
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' | |
20597 | 161 do not use this variable." |
162 :type '(repeat regexp) | |
163 :group 'file-cache) | |
18388 | 164 |
20597 | 165 (defcustom file-cache-find-command "find" |
166 "*External program used by `file-cache-add-directory-using-find'." | |
167 :type 'string | |
168 :group 'file-cache) | |
18388 | 169 |
20597 | 170 (defcustom file-cache-locate-command "locate" |
171 "*External program used by `file-cache-add-directory-using-locate'." | |
172 :type 'string | |
173 :group 'file-cache) | |
18388 | 174 |
175 ;; Minibuffer messages | |
20597 | 176 (defcustom file-cache-no-match-message " [File Cache: No match]" |
177 "Message to display when there is no completion." | |
178 :type 'string | |
179 :group 'file-cache) | |
18388 | 180 |
20597 | 181 (defcustom file-cache-sole-match-message " [File Cache: sole completion]" |
182 "Message to display when there is only one completion." | |
183 :type 'string | |
184 :group 'file-cache) | |
18388 | 185 |
20597 | 186 (defcustom file-cache-non-unique-message |
187 " [File Cache: complete but not unique]" | |
188 "Message to display when there is a non-unique completion." | |
189 :type 'string | |
190 :group 'file-cache) | |
18388 | 191 |
192 (defvar file-cache-multiple-directory-message nil) | |
193 | |
194 ;; Internal variables | |
195 ;; This should be named *Completions* because that's what the function | |
196 ;; switch-to-completions in simple.el expects | |
20597 | 197 (defcustom file-cache-completions-buffer "*Completions*" |
198 "Buffer to display completions when using the file cache." | |
199 :type 'string | |
200 :group 'file-cache) | |
18388 | 201 |
20597 | 202 (defcustom file-cache-buffer "*File Cache*" |
203 "Buffer to hold the cache of file names." | |
204 :type 'string | |
205 :group 'file-cache) | |
18388 | 206 |
20597 | 207 (defcustom file-cache-buffer-default-regexp "^.+$" |
208 "Regexp to match files in `file-cache-buffer'." | |
209 :type 'regexp | |
210 :group 'file-cache) | |
18388 | 211 |
212 (defvar file-cache-last-completion nil) | |
213 | |
214 (defvar file-cache-alist nil | |
215 "Internal data structure to hold cache of file names.") | |
216 | |
217 (defvar file-cache-completions-keymap nil | |
218 "Keymap for file cache completions buffer.") | |
219 | |
220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
221 ;; Functions to add files to the cache | |
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
223 | |
224 (defun file-cache-add-directory (directory &optional regexp) | |
225 "Add DIRECTORY to the file cache. | |
226 If the optional REGEXP argument is non-nil, only files which match it will | |
227 be added to the cache." | |
228 (interactive "DAdd files from directory: ") | |
229 (let* ((dir (expand-file-name directory)) | |
230 (dir-files (directory-files dir t regexp)) | |
231 ) | |
232 ;; Filter out files we don't want to see | |
233 (mapcar | |
234 '(lambda (file) | |
235 (mapcar | |
236 '(lambda (regexp) | |
237 (if (string-match regexp file) | |
238 (setq dir-files (delq file dir-files)))) | |
239 file-cache-filter-regexps)) | |
240 dir-files) | |
241 (file-cache-add-file-list dir-files))) | |
242 | |
243 (defun file-cache-add-directory-list (directory-list &optional regexp) | |
244 "Add DIRECTORY-LIST (a list of directory names) to the file cache. | |
245 If the optional REGEXP argument is non-nil, only files which match it | |
246 will be added to the cache. Note that the REGEXP is applied to the files | |
247 in each directory, not to the directory list itself." | |
248 (interactive "XAdd files from directory list: ") | |
249 (mapcar | |
250 '(lambda (dir) (file-cache-add-directory dir regexp)) | |
251 directory-list)) | |
252 | |
253 (defun file-cache-add-file-list (file-list) | |
254 "Add FILE-LIST (a list of files names) to the file cache." | |
255 (interactive "XFile List: ") | |
256 (mapcar 'file-cache-add-file file-list)) | |
257 | |
258 ;; Workhorse function | |
259 (defun file-cache-add-file (file) | |
260 "Add FILE to the file cache." | |
261 (interactive "fAdd File: ") | |
262 (let* ((file-name (file-name-nondirectory file)) | |
263 (dir-name (file-name-directory file)) | |
264 (the-entry (assoc file-name file-cache-alist)) | |
265 ) | |
266 ;; Does the entry exist already? | |
267 (if the-entry | |
268 (if (or (and (stringp (cdr the-entry)) | |
269 (string= dir-name (cdr the-entry))) | |
270 (and (listp (cdr the-entry)) | |
271 (member dir-name (cdr the-entry)))) | |
272 nil | |
273 (setcdr the-entry (append (list dir-name) (cdr the-entry))) | |
274 ) | |
275 ;; If not, add it to the cache | |
276 (setq file-cache-alist | |
277 (cons (cons file-name (list dir-name)) | |
278 file-cache-alist))) | |
279 )) | |
280 | |
281 (defun file-cache-add-directory-using-find (directory) | |
282 "Use the `find' command to add files to the file cache. | |
283 Find is run in DIRECTORY." | |
284 (interactive "DAdd files under directory: ") | |
285 (let ((dir (expand-file-name directory))) | |
286 (set-buffer (get-buffer-create file-cache-buffer)) | |
287 (erase-buffer) | |
288 (call-process file-cache-find-command nil | |
289 (get-buffer file-cache-buffer) nil | |
290 dir "-name" | |
291 (if (memq system-type | |
292 (list 'windows-nt 'ms-dos)) "'*'" "*") | |
293 "-print") | |
294 (file-cache-add-from-file-cache-buffer))) | |
295 | |
296 (defun file-cache-add-directory-using-locate (string) | |
297 "Use the `locate' command to add files to the file cache. | |
298 STRING is passed as an argument to the locate command." | |
299 (interactive "sAdd files using locate string: ") | |
300 (set-buffer (get-buffer-create file-cache-buffer)) | |
301 (erase-buffer) | |
302 (call-process file-cache-locate-command nil | |
303 (get-buffer file-cache-buffer) nil | |
304 string) | |
305 (file-cache-add-from-file-cache-buffer)) | |
306 | |
307 (defun file-cache-add-from-file-cache-buffer (&optional regexp) | |
308 "Add any entries found in the file cache buffer. | |
309 Each entry matches the regular expression `file-cache-buffer-default-regexp' | |
310 or the optional REGEXP argument." | |
311 (set-buffer file-cache-buffer) | |
312 (mapcar | |
313 (function (lambda (elt) | |
314 (goto-char (point-min)) | |
315 (delete-matching-lines elt))) | |
316 file-cache-filter-regexps) | |
317 (goto-char (point-min)) | |
318 (let ((full-filename)) | |
319 (while (re-search-forward | |
320 (or regexp file-cache-buffer-default-regexp) | |
321 (point-max) t) | |
322 (setq full-filename (buffer-substring-no-properties | |
323 (match-beginning 0) (match-end 0))) | |
324 (file-cache-add-file full-filename)))) | |
325 | |
326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
327 ;; Functions to delete from the cache | |
328 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
329 | |
330 (defun file-cache-clear-cache () | |
331 "Clear the file cache." | |
332 (interactive) | |
333 (setq file-cache-alist nil)) | |
334 | |
335 ;; This clears *all* files with the given name | |
336 (defun file-cache-delete-file (file) | |
337 "Delete FILE from the file cache." | |
338 (interactive | |
339 (list (completing-read "Delete file from cache: " file-cache-alist))) | |
340 (setq file-cache-alist | |
341 (delq (assoc file file-cache-alist) file-cache-alist))) | |
342 | |
343 (defun file-cache-delete-file-list (file-list) | |
344 "Delete FILE-LIST (a list of files) from the file cache." | |
345 (interactive "XFile List: ") | |
346 (mapcar 'file-cache-delete-file file-list)) | |
347 | |
348 (defun file-cache-delete-file-regexp (regexp) | |
349 "Delete files matching REGEXP from the file cache." | |
350 (interactive "sRegexp: ") | |
351 (let ((delete-list)) | |
352 (mapcar '(lambda (elt) | |
353 (and (string-match regexp (car elt)) | |
354 (setq delete-list (cons (car elt) delete-list)))) | |
355 file-cache-alist) | |
356 (file-cache-delete-file-list delete-list) | |
357 (message "Deleted %d files from file cache" (length delete-list)))) | |
358 | |
359 (defun file-cache-delete-directory (directory) | |
360 "Delete DIRECTORY from the file cache." | |
361 (interactive "DDelete directory from file cache: ") | |
362 (let ((dir (expand-file-name directory)) | |
363 (result 0)) | |
364 (mapcar | |
365 '(lambda (entry) | |
366 (if (file-cache-do-delete-directory dir entry) | |
367 (setq result (1+ result)))) | |
368 file-cache-alist) | |
369 (if (zerop result) | |
370 (error "No entries containing %s found in cache" directory) | |
371 (message "Deleted %d entries" result)))) | |
372 | |
373 (defun file-cache-do-delete-directory (dir entry) | |
374 (let ((directory-list (cdr entry)) | |
375 (directory (file-cache-canonical-directory dir)) | |
376 ) | |
377 (and (member directory directory-list) | |
378 (if (equal 1 (length directory-list)) | |
379 (setq file-cache-alist | |
380 (delq entry file-cache-alist)) | |
381 (setcdr entry (delete directory directory-list))) | |
382 ) | |
383 )) | |
384 | |
385 (defun file-cache-delete-directory-list (directory-list) | |
386 "Delete DIRECTORY-LIST (a list of directories) from the file cache." | |
387 (interactive "XDirectory List: ") | |
388 (mapcar 'file-cache-delete-directory directory-list)) | |
389 | |
390 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
391 ;; Utility functions | |
392 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
393 | |
394 ;; Returns the name of a directory for a file in the cache | |
395 (defun file-cache-directory-name (file) | |
396 (let* ((directory-list (cdr (assoc file file-cache-alist))) | |
397 (len (length directory-list)) | |
398 (directory) | |
399 (num) | |
400 ) | |
401 (if (not (listp directory-list)) | |
402 (error "Unknown type in file-cache-alist for key %s" file)) | |
403 (cond | |
404 ;; Single element | |
405 ((eq 1 len) | |
406 (setq directory (elt directory-list 0))) | |
407 ;; No elements | |
408 ((eq 0 len) | |
409 (error "No directory found for key %s" file)) | |
410 ;; Multiple elements | |
411 (t | |
412 (let* ((minibuffer-dir (file-name-directory (buffer-string))) | |
413 (dir-list (member minibuffer-dir directory-list)) | |
414 ) | |
415 (setq directory | |
416 ;; If the directory is in the list, return the next element | |
417 ;; Otherwise, return the first element | |
418 (if dir-list | |
419 (or (elt directory-list | |
420 (setq num (1+ (- len (length dir-list))))) | |
421 (elt directory-list (setq num 0))) | |
422 (elt directory-list (setq num 0)))) | |
423 ) | |
424 ) | |
425 ) | |
426 ;; If there were multiple directories, set up a minibuffer message | |
427 (setq file-cache-multiple-directory-message | |
428 (and num (format " [%d of %d]" (1+ num) len))) | |
429 directory)) | |
430 | |
431 ;; Returns the name of a file in the cache | |
432 (defun file-cache-file-name (file) | |
433 (let ((directory (file-cache-directory-name file))) | |
434 (concat directory file))) | |
435 | |
436 ;; Return a canonical directory for comparison purposes. | |
437 ;; Such a directory ends with a forward slash. | |
438 (defun file-cache-canonical-directory (dir) | |
439 (let ((directory dir)) | |
440 (if (not (char-equal ?/ (string-to-char (substring directory -1)))) | |
441 (concat directory "/") | |
442 directory))) | |
443 | |
444 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
445 ;; Minibuffer functions | |
446 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
447 | |
20578
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
448 ;; The prefix argument works around a bug in the minibuffer completion. |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
449 ;; The completion function doesn't distinguish between the states: |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
450 ;; |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
451 ;; "Multiple completions of name" (eg, Makefile, Makefile.in) |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
452 ;; "Name available in multiple directories" (/tmp/Makefile, ~me/Makefile) |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
453 ;; |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
454 ;; The default is to do the former; a prefix arg forces the latter. |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
455 |
18388 | 456 ;;;###autoload |
20578
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
457 (defun file-cache-minibuffer-complete (arg) |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
458 "Complete a filename in the minibuffer using a preloaded cache. |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
459 Filecache does two kinds of substitution: it completes on names in |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
460 the cache, and, once it has found a unique name, it cycles through |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
461 the directories that the name is available in. With a prefix argument, |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
462 the name is considered already unique; only the second substitution |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
463 \(directories) is done." |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
464 (interactive "P") |
18388 | 465 (let* |
466 ( | |
467 (completion-ignore-case nil) | |
468 (case-fold-search nil) | |
20578
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
469 (string (file-name-nondirectory (buffer-string))) |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
470 (completion-string (try-completion string file-cache-alist)) |
18388 | 471 (completion-list) |
472 (len) | |
473 (file-cache-string) | |
474 ) | |
475 (cond | |
20578
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
476 ;; If it's the only match, replace the original contents |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
477 ((or arg (eq completion-string t)) |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
478 (setq file-cache-string (file-cache-file-name string)) |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
479 (if (string= file-cache-string (buffer-string)) |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
480 (file-cache-temp-minibuffer-message file-cache-sole-match-message) |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
481 (erase-buffer) |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
482 (insert-string file-cache-string) |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
483 (if file-cache-multiple-directory-message |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
484 (file-cache-temp-minibuffer-message |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
485 file-cache-multiple-directory-message)) |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
486 )) |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
487 |
18388 | 488 ;; If it's the longest match, insert it |
489 ((stringp completion-string) | |
490 ;; If we've already inserted a unique string, see if the user | |
491 ;; wants to use that one | |
492 (if (and (string= string completion-string) | |
493 (assoc string file-cache-alist)) | |
494 (if (and (eq last-command this-command) | |
495 (string= file-cache-last-completion completion-string)) | |
496 (progn | |
497 (erase-buffer) | |
498 (insert-string (file-cache-file-name completion-string)) | |
499 (setq file-cache-last-completion nil) | |
500 ) | |
501 (file-cache-temp-minibuffer-message file-cache-non-unique-message) | |
502 (setq file-cache-last-completion string) | |
503 ) | |
504 (setq file-cache-last-completion string) | |
505 (setq completion-list (all-completions string file-cache-alist) | |
506 len (length completion-list)) | |
507 (if (> len 1) | |
508 (progn | |
509 (goto-char (point-max)) | |
510 (insert-string | |
511 (substring completion-string (length string))) | |
512 ;; Add our own setup function to the Completions Buffer | |
513 (let ((completion-setup-hook | |
514 (reverse | |
515 (append (list 'file-cache-completion-setup-function) | |
516 completion-setup-hook))) | |
517 ) | |
518 (with-output-to-temp-buffer file-cache-completions-buffer | |
519 (display-completion-list completion-list)) | |
520 ) | |
521 ) | |
522 (setq file-cache-string (file-cache-file-name completion-string)) | |
523 (if (string= file-cache-string (buffer-string)) | |
20578
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
524 (file-cache-temp-minibuffer-message |
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
525 file-cache-sole-match-message) |
18388 | 526 (erase-buffer) |
527 (insert-string file-cache-string) | |
528 (if file-cache-multiple-directory-message | |
529 (file-cache-temp-minibuffer-message | |
530 file-cache-multiple-directory-message))) | |
531 ))) | |
20578
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
532 |
18388 | 533 ;; No match |
534 ((eq completion-string nil) | |
535 (file-cache-temp-minibuffer-message file-cache-no-match-message)) | |
536 ) | |
537 )) | |
538 | |
539 ;; Lifted from "complete.el" | |
540 (defun file-cache-temp-minibuffer-message (msg) | |
541 "A Lisp version of `temp_minibuffer_message' from minibuf.c." | |
542 (let ((savemax (point-max))) | |
543 (save-excursion | |
544 (goto-char (point-max)) | |
545 (insert msg)) | |
546 (let ((inhibit-quit t)) | |
547 (sit-for 2) | |
548 (delete-region savemax (point-max)) | |
549 (if quit-flag | |
550 (setq quit-flag nil | |
551 unread-command-events (list 7)))))) | |
552 | |
553 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
554 ;; Completion functions | |
555 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
556 | |
557 (defun file-cache-completion-setup-function () | |
558 (set-buffer file-cache-completions-buffer) | |
559 | |
560 (if file-cache-completions-keymap | |
561 nil | |
562 (setq file-cache-completions-keymap | |
563 (copy-keymap completion-list-mode-map)) | |
564 (define-key file-cache-completions-keymap [mouse-2] | |
565 'file-cache-mouse-choose-completion) | |
566 (define-key file-cache-completions-keymap "\C-m" | |
567 'file-cache-choose-completion)) | |
568 | |
569 (use-local-map file-cache-completions-keymap) | |
570 ) | |
571 | |
572 (defun file-cache-choose-completion () | |
573 "Choose a completion in the `*Completions*' buffer." | |
574 (interactive) | |
575 (let ((completion-no-auto-exit t)) | |
576 (choose-completion) | |
577 (select-window (active-minibuffer-window)) | |
20578
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
578 (file-cache-minibuffer-complete nil) |
18388 | 579 ) |
580 ) | |
581 | |
582 (defun file-cache-mouse-choose-completion (event) | |
583 "Choose a completion with the mouse." | |
584 (interactive "e") | |
585 (let ((completion-no-auto-exit t)) | |
586 (mouse-choose-completion event) | |
587 (select-window (active-minibuffer-window)) | |
20578
7eb29ad824eb
(file-cache-minibuffer-complete): Accept a prefix arg.
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
588 (file-cache-minibuffer-complete nil) |
18388 | 589 ) |
590 ) | |
591 | |
592 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
593 ;; Debugging functions | |
594 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
595 | |
596 (defun file-cache-debug-read-from-minibuffer (file) | |
597 "Debugging function." | |
598 (interactive | |
599 (list (completing-read "File Cache: " file-cache-alist))) | |
600 (message "%s" (assoc file file-cache-alist)) | |
601 ) | |
602 | |
603 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
604 ;; Keybindings | |
605 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
606 | |
607 ;;;###autoload (define-key minibuffer-local-completion-map [C-tab] 'file-cache-minibuffer-complete) | |
608 ;;;###autoload (define-key minibuffer-local-map [C-tab] 'file-cache-minibuffer-complete) | |
609 ;;;###autoload (define-key minibuffer-local-must-match-map [C-tab] 'file-cache-minibuffer-complete) | |
610 | |
611 (provide 'filecache) | |
612 | |
613 ;;; filecache.el ends here |