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