aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2017-11-07 12:10:51 -0500
committerStefan Monnier2017-11-07 12:10:51 -0500
commit12c39211b38f641a72eb15c8f046df24d8acc4c3 (patch)
tree2850bd098d99389e76722c72ec7d325efa36fde3
parent1f3f4b1296613b8cdc0632a68fde86e86ddad866 (diff)
downloademacs-12c39211b38f641a72eb15c8f046df24d8acc4c3.tar.gz
emacs-12c39211b38f641a72eb15c8f046df24d8acc4c3.zip
* lisp/filecache.el: Use lexical-binding. Simplify.
Remove redundant :group args, use with-tmep-buffer and let completion-all-completions do its work. (file-cache-minibuffer-complete): Skip `completion-hilit-commonality` since `completion-all-completions' already hilighted its output. Call our setup function directly rather than via `completion-setup-hook`. (file-cache-buffer): Remove variable. (file-cache-buffer-default-regexp): Make it a defvar. (file-cache--add-from-buffer): Rename from file-cache-add-from-file-cache-buffer and use current-buffer and match-string-no-properties. (file-cache-add-directory-using-find) (file-cache-add-directory-using-locate): Adjust accordingly and use with-temp-buffer.
-rw-r--r--lisp/filecache.el123
1 files changed, 46 insertions, 77 deletions
diff --git a/lisp/filecache.el b/lisp/filecache.el
index aac4f488cd9..ea7cbcb6f10 100644
--- a/lisp/filecache.el
+++ b/lisp/filecache.el
@@ -1,4 +1,4 @@
1;;; filecache.el --- find files using a pre-loaded cache 1;;; filecache.el --- find files using a pre-loaded cache -*- lexical-binding:t -*-
2 2
3;; Copyright (C) 1996, 2000-2017 Free Software Foundation, Inc. 3;; Copyright (C) 1996, 2000-2017 Free Software Foundation, Inc.
4 4
@@ -25,16 +25,16 @@
25;; 25;;
26;; The file-cache package is an attempt to make it easy to locate files 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. 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 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. 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 30;; The cache can be used with any interactive command which takes a
31;; filename as an argument. 31;; filename as an argument.
32;; 32;;
33;; It is worth noting that this package works best when most of the files 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 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 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 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 37;; with a Makefile in each directory. In such a case, you should probably
38;; use an alternate strategy to find the files. 38;; use an alternate strategy to find the files.
39;; 39;;
40;; ADDING FILES TO THE CACHE: 40;; ADDING FILES TO THE CACHE:
@@ -49,11 +49,11 @@
49;; `file-cache-delete-regexps' to eliminate unwanted files: 49;; `file-cache-delete-regexps' to eliminate unwanted files:
50;; 50;;
51;; * `file-cache-add-directory': Adds the files in a directory to the 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 52;; cache. You can also specify a regular expression to match the files
53;; which should be added. 53;; which should be added.
54;; 54;;
55;; * `file-cache-add-directory-list': Same as above, but acts on a list 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. 56;; of directories. You can use `load-path', `exec-path' and the like.
57;; 57;;
58;; * `file-cache-add-directory-using-find': Uses the `find' command to 58;; * `file-cache-add-directory-using-find': Uses the `find' command to
59;; add a directory tree to the cache. 59;; add a directory tree to the cache.
@@ -65,7 +65,7 @@
65;; add all files matching a pattern to the cache. 65;; add all files matching a pattern to the cache.
66;; 66;;
67;; Use the function `file-cache-clear-cache' to remove all items from the 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 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 69;; as well, but in general it is probably better to not worry too much
70;; about extra files in the cache. 70;; about extra files in the cache.
71;; 71;;
@@ -76,7 +76,7 @@
76;; FINDING FILES USING THE CACHE: 76;; FINDING FILES USING THE CACHE:
77;; 77;;
78;; You can use the file-cache with any function that expects a filename as 78;; You can use the file-cache with any function that expects a filename as
79;; an argument. For example: 79;; an argument. For example:
80;; 80;;
81;; 1) Invoke a function which expects a filename as an argument: 81;; 1) Invoke a function which expects a filename as an argument:
82;; M-x find-file 82;; M-x find-file
@@ -160,13 +160,11 @@ File names which match these expressions will not be added to the cache.
160Note that the functions `file-cache-add-file' and `file-cache-add-file-list' 160Note that the functions `file-cache-add-file' and `file-cache-add-file-list'
161do not use this variable." 161do not use this variable."
162 :version "25.1" ; added "/\\.#" 162 :version "25.1" ; added "/\\.#"
163 :type '(repeat regexp) 163 :type '(repeat regexp))
164 :group 'file-cache)
165 164
166(defcustom file-cache-find-command "find" 165(defcustom file-cache-find-command "find"
167 "External program used by `file-cache-add-directory-using-find'." 166 "External program used by `file-cache-add-directory-using-find'."
168 :type 'string 167 :type 'string)
169 :group 'file-cache)
170 168
171(defcustom file-cache-find-command-posix-flag 'not-defined 169(defcustom file-cache-find-command-posix-flag 'not-defined
172 "Set to t, if `file-cache-find-command' handles wildcards POSIX style. 170 "Set to t, if `file-cache-find-command' handles wildcards POSIX style.
@@ -178,30 +176,25 @@ Under Windows operating system where Cygwin is available, this value
178should be t." 176should be t."
179 :type '(choice (const :tag "Yes" t) 177 :type '(choice (const :tag "Yes" t)
180 (const :tag "No" nil) 178 (const :tag "No" nil)
181 (const :tag "Unknown" not-defined)) 179 (const :tag "Unknown" not-defined)))
182 :group 'file-cache)
183 180
184(defcustom file-cache-locate-command "locate" 181(defcustom file-cache-locate-command "locate"
185 "External program used by `file-cache-add-directory-using-locate'." 182 "External program used by `file-cache-add-directory-using-locate'."
186 :type 'string 183 :type 'string)
187 :group 'file-cache)
188 184
189;; Minibuffer messages 185;; Minibuffer messages
190(defcustom file-cache-no-match-message " [File Cache: No match]" 186(defcustom file-cache-no-match-message " [File Cache: No match]"
191 "Message to display when there is no completion." 187 "Message to display when there is no completion."
192 :type 'string 188 :type 'string)
193 :group 'file-cache)
194 189
195(defcustom file-cache-sole-match-message " [File Cache: sole completion]" 190(defcustom file-cache-sole-match-message " [File Cache: sole completion]"
196 "Message to display when there is only one completion." 191 "Message to display when there is only one completion."
197 :type 'string 192 :type 'string)
198 :group 'file-cache)
199 193
200(defcustom file-cache-non-unique-message 194(defcustom file-cache-non-unique-message
201 " [File Cache: complete but not unique]" 195 " [File Cache: complete but not unique]"
202 "Message to display when there is a non-unique completion." 196 "Message to display when there is a non-unique completion."
203 :type 'string 197 :type 'string)
204 :group 'file-cache)
205 198
206(defcustom file-cache-completion-ignore-case 199(defcustom file-cache-completion-ignore-case
207 (if (memq system-type '(ms-dos windows-nt cygwin)) 200 (if (memq system-type '(ms-dos windows-nt cygwin))
@@ -209,8 +202,7 @@ should be t."
209 completion-ignore-case) 202 completion-ignore-case)
210 "If non-nil, file-cache completion should ignore case. 203 "If non-nil, file-cache completion should ignore case.
211Defaults to the value of `completion-ignore-case'." 204Defaults to the value of `completion-ignore-case'."
212 :type 'boolean 205 :type 'boolean)
213 :group 'file-cache)
214 206
215(defcustom file-cache-case-fold-search 207(defcustom file-cache-case-fold-search
216 (if (memq system-type '(ms-dos windows-nt cygwin)) 208 (if (memq system-type '(ms-dos windows-nt cygwin))
@@ -218,15 +210,13 @@ Defaults to the value of `completion-ignore-case'."
218 case-fold-search) 210 case-fold-search)
219 "If non-nil, file-cache completion should ignore case. 211 "If non-nil, file-cache completion should ignore case.
220Defaults to the value of `case-fold-search'." 212Defaults to the value of `case-fold-search'."
221 :type 'boolean 213 :type 'boolean)
222 :group 'file-cache)
223 214
224(defcustom file-cache-ignore-case 215(defcustom file-cache-ignore-case
225 (memq system-type '(ms-dos windows-nt cygwin)) 216 (memq system-type '(ms-dos windows-nt cygwin))
226 "Non-nil means ignore case when checking completions in the file cache. 217 "Non-nil means ignore case when checking completions in the file cache.
227Defaults to nil on DOS and Windows, and t on other systems." 218Defaults to nil on DOS and Windows, and t on other systems."
228 :type 'boolean 219 :type 'boolean)
229 :group 'file-cache)
230 220
231(defvar file-cache-multiple-directory-message nil) 221(defvar file-cache-multiple-directory-message nil)
232 222
@@ -235,18 +225,10 @@ Defaults to nil on DOS and Windows, and t on other systems."
235;; switch-to-completions in simple.el expects 225;; switch-to-completions in simple.el expects
236(defcustom file-cache-completions-buffer "*Completions*" 226(defcustom file-cache-completions-buffer "*Completions*"
237 "Buffer to display completions when using the file cache." 227 "Buffer to display completions when using the file cache."
238 :type 'string 228 :type 'string)
239 :group 'file-cache)
240 229
241(defcustom file-cache-buffer "*File Cache*" 230(defvar file-cache-buffer-default-regexp "^.+$"
242 "Buffer to hold the cache of file names." 231 "Regexp to match files in find and locate's output.")
243 :type 'string
244 :group 'file-cache)
245
246(defcustom file-cache-buffer-default-regexp "^.+$"
247 "Regexp to match files in `file-cache-buffer'."
248 :type 'regexp
249 :group 'file-cache)
250 232
251(defvar file-cache-last-completion nil) 233(defvar file-cache-last-completion nil)
252 234
@@ -362,36 +344,31 @@ Find is run in DIRECTORY."
362 (if (eq file-cache-find-command-posix-flag 'not-defined) 344 (if (eq file-cache-find-command-posix-flag 'not-defined)
363 (setq file-cache-find-command-posix-flag 345 (setq file-cache-find-command-posix-flag
364 (executable-command-find-posix-p file-cache-find-command)))) 346 (executable-command-find-posix-p file-cache-find-command))))
365 (set-buffer (get-buffer-create file-cache-buffer)) 347 (with-temp-buffer
366 (erase-buffer) 348 (call-process file-cache-find-command nil t nil
367 (call-process file-cache-find-command nil 349 dir "-name"
368 (get-buffer file-cache-buffer) nil 350 (if (memq system-type '(windows-nt cygwin))
369 dir "-name" 351 (if file-cache-find-command-posix-flag
370 (if (memq system-type '(windows-nt cygwin)) 352 "\\*"
371 (if file-cache-find-command-posix-flag 353 "'*'")
372 "\\*" 354 "*")
373 "'*'") 355 "-print")
374 "*") 356 (file-cache--add-from-buffer))))
375 "-print")
376 (file-cache-add-from-file-cache-buffer)))
377 357
378;;;###autoload 358;;;###autoload
379(defun file-cache-add-directory-using-locate (string) 359(defun file-cache-add-directory-using-locate (string)
380 "Use the `locate' command to add files to the file cache. 360 "Use the `locate' command to add files to the file cache.
381STRING is passed as an argument to the locate command." 361STRING is passed as an argument to the locate command."
382 (interactive "sAdd files using locate string: ") 362 (interactive "sAdd files using locate string: ")
383 (set-buffer (get-buffer-create file-cache-buffer)) 363 (with-temp-buffer
384 (erase-buffer) 364 (call-process file-cache-locate-command nil t nil string)
385 (call-process file-cache-locate-command nil 365 (file-cache--add-from-buffer)))
386 (get-buffer file-cache-buffer) nil
387 string)
388 (file-cache-add-from-file-cache-buffer))
389 366
390(autoload 'find-lisp-find-files "find-lisp") 367(autoload 'find-lisp-find-files "find-lisp")
391 368
392;;;###autoload 369;;;###autoload
393(defun file-cache-add-directory-recursively (dir &optional regexp) 370(defun file-cache-add-directory-recursively (dir &optional regexp)
394 "Adds DIR and any subdirectories to the file-cache. 371 "Add DIR and any subdirectories to the file-cache.
395This function does not use any external programs. 372This function does not use any external programs.
396If the optional REGEXP argument is non-nil, only files which match it 373If the optional REGEXP argument is non-nil, only files which match it
397will be added to the cache. Note that the REGEXP is applied to the 374will be added to the cache. Note that the REGEXP is applied to the
@@ -408,22 +385,16 @@ files in each directory, not to the directory list itself."
408 (file-cache-add-file file))) 385 (file-cache-add-file file)))
409 (find-lisp-find-files dir (or regexp "^")))) 386 (find-lisp-find-files dir (or regexp "^"))))
410 387
411(defun file-cache-add-from-file-cache-buffer (&optional regexp) 388(defun file-cache--add-from-buffer ()
412 "Add any entries found in the file cache buffer. 389 "Add any entries found in the current buffer.
413Each entry matches the regular expression `file-cache-buffer-default-regexp' 390Each entry matches the regular expression `file-cache-buffer-default-regexp'
414or the optional REGEXP argument." 391or the optional REGEXP argument."
415 (set-buffer file-cache-buffer)
416 (dolist (elt file-cache-filter-regexps) 392 (dolist (elt file-cache-filter-regexps)
417 (goto-char (point-min)) 393 (goto-char (point-min))
418 (delete-matching-lines elt)) 394 (delete-matching-lines elt))
419 (goto-char (point-min)) 395 (goto-char (point-min))
420 (let ((full-filename)) 396 (while (re-search-forward file-cache-buffer-default-regexp nil t)
421 (while (re-search-forward 397 (file-cache-add-file (match-string-no-properties 0))))
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))))
427 398
428;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 399;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
429;; Functions to delete from the cache 400;; Functions to delete from the cache
@@ -610,20 +581,18 @@ the name is considered already unique; only the second substitution
610 (delete-region (- (point-max) (length string)) (point-max)) 581 (delete-region (- (point-max) (length string)) (point-max))
611 (save-excursion (insert newstring)) 582 (save-excursion (insert newstring))
612 (forward-char newpoint) 583 (forward-char newpoint)
613 ;; Add our own setup function to the Completions Buffer 584 (with-output-to-temp-buffer file-cache-completions-buffer
614 (let ((completion-setup-hook 585 (display-completion-list completion-list)
615 (append completion-setup-hook 586 ;; Add our own setup function to the Completions Buffer
616 (list 'file-cache-completion-setup-function)))) 587 (file-cache-completion-setup-function)))
617 (with-output-to-temp-buffer file-cache-completions-buffer
618 (display-completion-list
619 (completion-hilit-commonality completion-list newpoint)))))
620 (let ((file-name (file-cache-file-name newstring))) 588 (let ((file-name (file-cache-file-name newstring)))
621 (if (string= file-name (minibuffer-contents)) 589 (if (string= file-name (minibuffer-contents))
622 (minibuffer-message file-cache-sole-match-message) 590 (minibuffer-message file-cache-sole-match-message)
623 (delete-minibuffer-contents) 591 (delete-minibuffer-contents)
624 (insert file-name) 592 (insert file-name)
625 (if file-cache-multiple-directory-message 593 (if file-cache-multiple-directory-message
626 (minibuffer-message file-cache-multiple-directory-message))))))))) 594 (minibuffer-message
595 file-cache-multiple-directory-message)))))))))
627 596
628 ;; No match 597 ;; No match
629 ((eq completion nil) 598 ((eq completion nil)
@@ -646,7 +615,7 @@ the name is considered already unique; only the second substitution
646 (file-cache-minibuffer-complete nil))) 615 (file-cache-minibuffer-complete nil)))
647 616
648(define-obsolete-function-alias 'file-cache-mouse-choose-completion 617(define-obsolete-function-alias 'file-cache-mouse-choose-completion
649 'file-cache-choose-completion "23.2") 618 #'file-cache-choose-completion "23.2")
650 619
651(defun file-cache-complete () 620(defun file-cache-complete ()
652 "Complete the word at point, using the filecache." 621 "Complete the word at point, using the filecache."