filecache.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. ;;; filecache.el --- find files using a pre-loaded cache
  2. ;; Copyright (C) 1996, 2000-2012 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. (eval-when-compile
  134. (require 'find-lisp))
  135. (defgroup file-cache nil
  136. "Find files using a pre-loaded cache."
  137. :group 'files
  138. :group 'convenience
  139. :prefix "file-cache-")
  140. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  141. ;; Customization Variables
  142. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  143. ;; User-modifiable variables
  144. (defcustom file-cache-filter-regexps
  145. ;; These are also used in buffers containing lines of file names,
  146. ;; so the end-of-name is matched with $ rather than \\'.
  147. (list "~$" "\\.o$" "\\.exe$" "\\.a$" "\\.elc$" ",v$" "\\.output$"
  148. "\\.$" "#$" "\\.class$")
  149. "List of regular expressions used as filters by the file cache.
  150. File names which match these expressions will not be added to the cache.
  151. Note that the functions `file-cache-add-file' and `file-cache-add-file-list'
  152. do not use this variable."
  153. :type '(repeat regexp)
  154. :group 'file-cache)
  155. (defcustom file-cache-find-command "find"
  156. "External program used by `file-cache-add-directory-using-find'."
  157. :type 'string
  158. :group 'file-cache)
  159. (defcustom file-cache-find-command-posix-flag 'not-defined
  160. "Set to t, if `file-cache-find-command' handles wildcards POSIX style.
  161. This variable is automatically set to nil or non-nil
  162. if it has the initial value `not-defined' whenever you first
  163. call the `file-cache-add-directory-using-find'.
  164. Under Windows operating system where Cygwin is available, this value
  165. should be t."
  166. :type '(choice (const :tag "Yes" t)
  167. (const :tag "No" nil)
  168. (const :tag "Unknown" not-defined))
  169. :group 'file-cache)
  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)
  174. ;; Minibuffer messages
  175. (defcustom file-cache-no-match-message " [File Cache: No match]"
  176. "Message to display when there is no completion."
  177. :type 'string
  178. :group 'file-cache)
  179. (defcustom file-cache-sole-match-message " [File Cache: sole completion]"
  180. "Message to display when there is only one completion."
  181. :type 'string
  182. :group 'file-cache)
  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)
  188. (defcustom file-cache-completion-ignore-case
  189. (if (memq system-type '(ms-dos windows-nt cygwin))
  190. t
  191. completion-ignore-case)
  192. "If non-nil, file-cache completion should ignore case.
  193. Defaults to the value of `completion-ignore-case'."
  194. :type 'boolean
  195. :group 'file-cache)
  196. (defcustom file-cache-case-fold-search
  197. (if (memq system-type '(ms-dos windows-nt cygwin))
  198. t
  199. case-fold-search)
  200. "If non-nil, file-cache completion should ignore case.
  201. Defaults to the value of `case-fold-search'."
  202. :type 'boolean
  203. :group 'file-cache)
  204. (defcustom file-cache-ignore-case
  205. (memq system-type '(ms-dos windows-nt cygwin))
  206. "Non-nil means ignore case when checking completions in the file cache.
  207. Defaults to nil on DOS and Windows, and t on other systems."
  208. :type 'boolean
  209. :group 'file-cache)
  210. (defvar file-cache-multiple-directory-message nil)
  211. ;; Internal variables
  212. ;; This should be named *Completions* because that's what the function
  213. ;; switch-to-completions in simple.el expects
  214. (defcustom file-cache-completions-buffer "*Completions*"
  215. "Buffer to display completions when using the file cache."
  216. :type 'string
  217. :group 'file-cache)
  218. (defcustom file-cache-buffer "*File Cache*"
  219. "Buffer to hold the cache of file names."
  220. :type 'string
  221. :group 'file-cache)
  222. (defcustom file-cache-buffer-default-regexp "^.+$"
  223. "Regexp to match files in `file-cache-buffer'."
  224. :type 'regexp
  225. :group 'file-cache)
  226. (defvar file-cache-last-completion nil)
  227. (defvar file-cache-alist nil
  228. "Internal data structure to hold cache of file names.
  229. It is a list of entries of the form (FILENAME DIRNAME1 DIRNAME2 ...)
  230. where FILENAME is a file name component and the entry represents N
  231. files of names DIRNAME1/FILENAME, DIRNAME2/FILENAME, ...")
  232. (defvar file-cache-completions-keymap
  233. (let ((map (make-sparse-keymap)))
  234. (set-keymap-parent map completion-list-mode-map)
  235. (define-key map [mouse-2] 'file-cache-choose-completion)
  236. (define-key map "\C-m" 'file-cache-choose-completion)
  237. map)
  238. "Keymap for file cache completions buffer.")
  239. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  240. ;; Functions to add files to the cache
  241. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  242. ;;;###autoload
  243. (defun file-cache-add-directory (directory &optional regexp)
  244. "Add DIRECTORY to the file cache.
  245. If the optional REGEXP argument is non-nil, only files which match it will
  246. be added to the cache."
  247. (interactive "DAdd files from directory: ")
  248. ;; Not an error, because otherwise we can't use load-paths that
  249. ;; contain non-existent directories.
  250. (if (not (file-accessible-directory-p directory))
  251. (message "Directory %s does not exist" directory)
  252. (let* ((dir (expand-file-name directory))
  253. (dir-files (directory-files dir t regexp)))
  254. ;; Filter out files we don't want to see
  255. (dolist (file dir-files)
  256. (if (file-directory-p file)
  257. (setq dir-files (delq file dir-files))
  258. (dolist (regexp file-cache-filter-regexps)
  259. (if (string-match regexp file)
  260. (setq dir-files (delq file dir-files))))))
  261. (file-cache-add-file-list dir-files))))
  262. ;;;###autoload
  263. (defun file-cache-add-directory-list (directory-list &optional regexp)
  264. "Add DIRECTORY-LIST (a list of directory names) to the file cache.
  265. If the optional REGEXP argument is non-nil, only files which match it
  266. will be added to the cache. Note that the REGEXP is applied to the
  267. files in each directory, not to the directory list itself."
  268. (interactive "XAdd files from directory list: ")
  269. (mapcar
  270. (lambda (dir) (file-cache-add-directory dir regexp))
  271. directory-list))
  272. (defun file-cache-add-file-list (file-list)
  273. "Add FILE-LIST (a list of files names) to the file cache."
  274. (interactive "XFile List: ")
  275. (mapcar 'file-cache-add-file file-list))
  276. ;; Workhorse function
  277. ;;;###autoload
  278. (defun file-cache-add-file (file)
  279. "Add FILE to the file cache."
  280. (interactive "fAdd File: ")
  281. (if (not (file-exists-p file))
  282. (message "Filecache: file %s does not exist" file)
  283. (let* ((file-name (file-name-nondirectory file))
  284. (dir-name (file-name-directory file))
  285. (the-entry (assoc-string
  286. file-name file-cache-alist
  287. file-cache-ignore-case)))
  288. ;; Does the entry exist already?
  289. (if the-entry
  290. (if (or (and (stringp (cdr the-entry))
  291. (string= dir-name (cdr the-entry)))
  292. (and (listp (cdr the-entry))
  293. (member dir-name (cdr the-entry))))
  294. nil
  295. (setcdr the-entry (cons dir-name (cdr the-entry))))
  296. ;; If not, add it to the cache
  297. (push (list file-name dir-name) file-cache-alist)))))
  298. ;;;###autoload
  299. (defun file-cache-add-directory-using-find (directory)
  300. "Use the `find' command to add files to the file cache.
  301. Find is run in DIRECTORY."
  302. (interactive "DAdd files under directory: ")
  303. (let ((dir (expand-file-name directory)))
  304. (when (memq system-type '(windows-nt cygwin))
  305. (if (eq file-cache-find-command-posix-flag 'not-defined)
  306. (setq file-cache-find-command-posix-flag
  307. (executable-command-find-posix-p file-cache-find-command))))
  308. (set-buffer (get-buffer-create file-cache-buffer))
  309. (erase-buffer)
  310. (call-process file-cache-find-command nil
  311. (get-buffer file-cache-buffer) nil
  312. dir "-name"
  313. (if (memq system-type '(windows-nt cygwin))
  314. (if file-cache-find-command-posix-flag
  315. "\\*"
  316. "'*'")
  317. "*")
  318. "-print")
  319. (file-cache-add-from-file-cache-buffer)))
  320. ;;;###autoload
  321. (defun file-cache-add-directory-using-locate (string)
  322. "Use the `locate' command to add files to the file cache.
  323. STRING is passed as an argument to the locate command."
  324. (interactive "sAdd files using locate string: ")
  325. (set-buffer (get-buffer-create file-cache-buffer))
  326. (erase-buffer)
  327. (call-process file-cache-locate-command nil
  328. (get-buffer file-cache-buffer) nil
  329. string)
  330. (file-cache-add-from-file-cache-buffer))
  331. ;;;###autoload
  332. (defun file-cache-add-directory-recursively (dir &optional regexp)
  333. "Adds DIR and any subdirectories to the file-cache.
  334. This function does not use any external programs.
  335. If the optional REGEXP argument is non-nil, only files which match it
  336. will be added to the cache. Note that the REGEXP is applied to the
  337. files in each directory, not to the directory list itself."
  338. (interactive "DAdd directory: ")
  339. (require 'find-lisp)
  340. (mapcar
  341. (function
  342. (lambda (file)
  343. (or (file-directory-p file)
  344. (let (filtered)
  345. (dolist (regexp file-cache-filter-regexps)
  346. (and (string-match regexp file)
  347. (setq filtered t)))
  348. filtered)
  349. (file-cache-add-file file))))
  350. (find-lisp-find-files dir (if regexp regexp "^"))))
  351. (defun file-cache-add-from-file-cache-buffer (&optional regexp)
  352. "Add any entries found in the file cache buffer.
  353. Each entry matches the regular expression `file-cache-buffer-default-regexp'
  354. or the optional REGEXP argument."
  355. (set-buffer file-cache-buffer)
  356. (dolist (elt file-cache-filter-regexps)
  357. (goto-char (point-min))
  358. (delete-matching-lines elt))
  359. (goto-char (point-min))
  360. (let ((full-filename))
  361. (while (re-search-forward
  362. (or regexp file-cache-buffer-default-regexp)
  363. (point-max) t)
  364. (setq full-filename (buffer-substring-no-properties
  365. (match-beginning 0) (match-end 0)))
  366. (file-cache-add-file full-filename))))
  367. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  368. ;; Functions to delete from the cache
  369. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  370. (defun file-cache-clear-cache ()
  371. "Clear the file cache."
  372. (interactive)
  373. (setq file-cache-alist nil))
  374. ;; This clears *all* files with the given name
  375. (defun file-cache-delete-file (file)
  376. "Delete FILE from the file cache."
  377. (interactive
  378. (list (completing-read "Delete file from cache: " file-cache-alist)))
  379. (setq file-cache-alist
  380. (delq (assoc-string file file-cache-alist file-cache-ignore-case)
  381. file-cache-alist)))
  382. (defun file-cache-delete-file-list (file-list)
  383. "Delete FILE-LIST (a list of files) from the file cache."
  384. (interactive "XFile List: ")
  385. (mapcar 'file-cache-delete-file file-list))
  386. (defun file-cache-delete-file-regexp (regexp)
  387. "Delete files matching REGEXP from the file cache."
  388. (interactive "sRegexp: ")
  389. (let ((delete-list))
  390. (dolist (elt file-cache-alist)
  391. (and (string-match regexp (car elt))
  392. (push (car elt) delete-list)))
  393. (file-cache-delete-file-list delete-list)
  394. (message "Filecache: deleted %d files from file cache"
  395. (length delete-list))))
  396. (defun file-cache-delete-directory (directory)
  397. "Delete DIRECTORY from the file cache."
  398. (interactive "DDelete directory from file cache: ")
  399. (let ((dir (expand-file-name directory))
  400. (result 0))
  401. (dolist (entry file-cache-alist)
  402. (if (file-cache-do-delete-directory dir entry)
  403. (setq result (1+ result))))
  404. (if (zerop result)
  405. (error "Filecache: no entries containing %s found in cache" directory)
  406. (message "Filecache: deleted %d entries" result))))
  407. (defun file-cache-do-delete-directory (dir entry)
  408. (let ((directory-list (cdr entry))
  409. (directory (file-cache-canonical-directory dir)))
  410. (and (member directory directory-list)
  411. (if (equal 1 (length directory-list))
  412. (setq file-cache-alist
  413. (delq entry file-cache-alist))
  414. (setcdr entry (delete directory directory-list))))))
  415. (defun file-cache-delete-directory-list (directory-list)
  416. "Delete DIRECTORY-LIST (a list of directories) from the file cache."
  417. (interactive "XDirectory List: ")
  418. (mapcar 'file-cache-delete-directory directory-list))
  419. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  420. ;; Utility functions
  421. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  422. ;; Returns the name of a directory for a file in the cache
  423. (defun file-cache-directory-name (file)
  424. (let* ((directory-list (cdr (assoc-string
  425. file file-cache-alist
  426. file-cache-ignore-case)))
  427. (len (length directory-list))
  428. (directory)
  429. (num))
  430. (if (not (listp directory-list))
  431. (error "Filecache: unknown type in file-cache-alist for key %s" file))
  432. (cond
  433. ;; Single element
  434. ((eq 1 len)
  435. (setq directory (elt directory-list 0)))
  436. ;; No elements
  437. ((eq 0 len)
  438. (error "Filecache: no directory found for key %s" file))
  439. ;; Multiple elements
  440. (t
  441. (let* ((minibuffer-dir (file-name-directory (minibuffer-contents)))
  442. (dir-list (member minibuffer-dir directory-list)))
  443. (setq directory
  444. ;; If the directory is in the list, return the next element
  445. ;; Otherwise, return the first element
  446. (if dir-list
  447. (or (elt directory-list
  448. (setq num (1+ (- len (length dir-list)))))
  449. (elt directory-list (setq num 0)))
  450. (elt directory-list (setq num 0)))))))
  451. ;; If there were multiple directories, set up a minibuffer message
  452. (setq file-cache-multiple-directory-message
  453. (and num (format " [%d of %d]" (1+ num) len)))
  454. directory))
  455. ;; Returns the name of a file in the cache
  456. (defun file-cache-file-name (file)
  457. (let ((directory (file-cache-directory-name file)))
  458. (concat directory file)))
  459. ;; Return a canonical directory for comparison purposes.
  460. ;; Such a directory ends with a forward slash.
  461. (defun file-cache-canonical-directory (dir)
  462. (let ((directory dir))
  463. (if (not (char-equal ?/ (string-to-char (substring directory -1))))
  464. (concat directory "/")
  465. directory)))
  466. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  467. ;; Minibuffer functions
  468. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  469. ;; The prefix argument works around a bug in the minibuffer completion.
  470. ;; The completion function doesn't distinguish between the states:
  471. ;;
  472. ;; "Multiple completions of name" (eg, Makefile, Makefile.in)
  473. ;; "Name available in multiple directories" (/tmp/Makefile, ~me/Makefile)
  474. ;;
  475. ;; The default is to do the former; a prefix arg forces the latter.
  476. ;;;###autoload
  477. (defun file-cache-minibuffer-complete (arg)
  478. "Complete a filename in the minibuffer using a preloaded cache.
  479. Filecache does two kinds of substitution: it completes on names in
  480. the cache, and, once it has found a unique name, it cycles through
  481. the directories that the name is available in. With a prefix argument,
  482. the name is considered already unique; only the second substitution
  483. \(directories) is done."
  484. (interactive "P")
  485. (let*
  486. (
  487. (completion-ignore-case file-cache-completion-ignore-case)
  488. (case-fold-search file-cache-case-fold-search)
  489. (string (file-name-nondirectory (minibuffer-contents)))
  490. (completion-string (try-completion string file-cache-alist))
  491. (completion-list)
  492. (len)
  493. (file-cache-string))
  494. (cond
  495. ;; If it's the only match, replace the original contents
  496. ((or arg (eq completion-string t))
  497. (setq file-cache-string (file-cache-file-name string))
  498. (if (string= file-cache-string (minibuffer-contents))
  499. (minibuffer-message file-cache-sole-match-message)
  500. (delete-minibuffer-contents)
  501. (insert file-cache-string)
  502. (if file-cache-multiple-directory-message
  503. (minibuffer-message file-cache-multiple-directory-message))))
  504. ;; If it's the longest match, insert it
  505. ((stringp completion-string)
  506. ;; If we've already inserted a unique string, see if the user
  507. ;; wants to use that one
  508. (if (and (string= string completion-string)
  509. (assoc-string string file-cache-alist
  510. file-cache-ignore-case))
  511. (if (and (eq last-command this-command)
  512. (string= file-cache-last-completion completion-string))
  513. (progn
  514. (delete-minibuffer-contents)
  515. (insert (file-cache-file-name completion-string))
  516. (setq file-cache-last-completion nil))
  517. (minibuffer-message file-cache-non-unique-message)
  518. (setq file-cache-last-completion string))
  519. (setq file-cache-last-completion string)
  520. (setq completion-list (all-completions string file-cache-alist)
  521. len (length completion-list))
  522. (if (> len 1)
  523. (progn
  524. (goto-char (point-max))
  525. (insert
  526. (substring completion-string (length string)))
  527. ;; Add our own setup function to the Completions Buffer
  528. (let ((completion-setup-hook
  529. (append completion-setup-hook
  530. (list 'file-cache-completion-setup-function))))
  531. (with-output-to-temp-buffer file-cache-completions-buffer
  532. (display-completion-list completion-list string))))
  533. (setq file-cache-string (file-cache-file-name completion-string))
  534. (if (string= file-cache-string (minibuffer-contents))
  535. (minibuffer-message file-cache-sole-match-message)
  536. (delete-minibuffer-contents)
  537. (insert file-cache-string)
  538. (if file-cache-multiple-directory-message
  539. (minibuffer-message file-cache-multiple-directory-message)))
  540. )))
  541. ;; No match
  542. ((eq completion-string nil)
  543. (minibuffer-message file-cache-no-match-message)))))
  544. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  545. ;; Completion functions
  546. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  547. (defun file-cache-completion-setup-function ()
  548. (with-current-buffer standard-output ;; i.e. file-cache-completions-buffer
  549. (use-local-map file-cache-completions-keymap)))
  550. (defun file-cache-choose-completion (&optional event)
  551. "Choose a completion in the `*Completions*' buffer."
  552. (interactive (list last-nonmenu-event))
  553. (let ((completion-no-auto-exit t))
  554. (choose-completion event)
  555. (select-window (active-minibuffer-window))
  556. (file-cache-minibuffer-complete nil)))
  557. (define-obsolete-function-alias 'file-cache-mouse-choose-completion
  558. 'file-cache-choose-completion "23.2")
  559. (defun file-cache-complete ()
  560. "Complete the word at point, using the filecache."
  561. (interactive)
  562. (let ((start
  563. (save-excursion
  564. (skip-syntax-backward "^\"")
  565. (point))))
  566. (completion-in-region start (point) file-cache-alist)))
  567. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  568. ;; Show parts of the cache
  569. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  570. (defun file-cache-files-matching-internal (regexp)
  571. "Output a list of files whose names (not including directories)
  572. match REGEXP."
  573. (let ((results))
  574. (dolist (cache-element file-cache-alist)
  575. (and (string-match regexp (elt cache-element 0))
  576. (push (elt cache-element 0) results)))
  577. (nreverse results)))
  578. (defun file-cache-files-matching (regexp)
  579. "Output a list of files whose names (not including directories)
  580. match REGEXP."
  581. (interactive "sFind files matching regexp: ")
  582. (let ((results
  583. (file-cache-files-matching-internal regexp))
  584. buf)
  585. (set-buffer
  586. (setq buf (get-buffer-create
  587. "*File Cache Files Matching*")))
  588. (erase-buffer)
  589. (insert
  590. (mapconcat
  591. 'identity
  592. results
  593. "\n"))
  594. (goto-char (point-min))
  595. (display-buffer buf)))
  596. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  597. ;; Debugging functions
  598. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  599. (defun file-cache-debug-read-from-minibuffer (file)
  600. "Debugging function."
  601. (interactive
  602. (list (completing-read "File Cache: " file-cache-alist)))
  603. (message "%s" (assoc-string file file-cache-alist
  604. file-cache-ignore-case)))
  605. (defun file-cache-display ()
  606. "Display the file cache."
  607. (interactive)
  608. (let ((buf "*File Cache Contents*"))
  609. (with-current-buffer
  610. (get-buffer-create buf)
  611. (erase-buffer)
  612. (dolist (item file-cache-alist)
  613. (insert (nth 1 item) (nth 0 item) "\n"))
  614. (pop-to-buffer buf))))
  615. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  616. ;; Keybindings
  617. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  618. (provide 'filecache)
  619. ;;; filecache.el ends here