filecache.el 26 KB

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