From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Mathias Dahl Newsgroups: gmane.emacs.devel Subject: Proposing two new functions in filecache.el Date: Thu, 15 Dec 2005 19:27:24 +0100 Message-ID: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1134678826 4706 80.91.229.2 (15 Dec 2005 20:33:46 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 15 Dec 2005 20:33:46 +0000 (UTC) Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Dec 15 21:33:43 2005 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1Emzle-0007mx-A6 for ged-emacs-devel@m.gmane.org; Thu, 15 Dec 2005 21:31:58 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Emzkd-0005jB-7T for ged-emacs-devel@m.gmane.org; Thu, 15 Dec 2005 15:30:55 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Emxve-0002JP-58 for emacs-devel@gnu.org; Thu, 15 Dec 2005 13:34:10 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Emxvd-0002IA-7J for emacs-devel@gnu.org; Thu, 15 Dec 2005 13:34:09 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Emxva-0002Hv-RC for emacs-devel@gnu.org; Thu, 15 Dec 2005 13:34:08 -0500 Original-Received: from [80.91.229.2] (helo=ciao.gmane.org) by monty-python.gnu.org with esmtp (TLS-1.0:RSA_AES_128_CBC_SHA:16) (Exim 4.34) id 1Emxxs-0001iw-Hn for emacs-devel@gnu.org; Thu, 15 Dec 2005 13:36:28 -0500 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1EmxrR-0005F9-Il for emacs-devel@gnu.org; Thu, 15 Dec 2005 19:29:50 +0100 Original-Received: from user.ifsab.se ([193.41.170.225]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 15 Dec 2005 19:29:49 +0100 Original-Received: from brakjoller by user.ifsab.se with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 15 Dec 2005 19:29:49 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-To: emacs-devel@gnu.org Original-Lines: 76 Original-X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: user.ifsab.se User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (windows-nt) Cancel-Lock: sha1:M4CxCSEV82efqaH0KvRNN/Iutps= X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:47811 Archived-At: I just learned about the very usefil File Name Cache and propose the following or similar addition to filecache.el. The reason is that adding remote directories is a time-consuming operation, and for directories that seldom change, there is no point in creating the file cache each time Emacs is restarted. So, why not add functionality to save and read the cache from a file? ;;; Code begins here (defun file-cache-save-cache-to-file (file) "Save contents of `file-cache-alist' to FILE. For later retrieval using `file-cache-read-cache-from-file'" (interactive) (let ((buf (get-buffer-create "*file-cache*"))) (save-excursion (set-buffer buf) (erase-buffer) ;; If there is some neater way of doing the saving, I am all for ;; it... (insert (with-output-to-string (print file-cache-alist))) (write-region (point-min) (point-max) (expand-file-name file))))) (defun file-cache-read-cache-from-file (file) "Clear `file-cache-alist' and read cache from FILE. The file cache can be saved to a file using `file-cache-save-cache-to-file'." (interactive) (file-cache-clear-cache) (let ((buf (get-buffer-create "*file-cache*"))) (save-excursion (set-buffer buf) (erase-buffer) ;; Similary to the read function, if there is a better way to ;; restore file-cache-alist... (insert-file-contents file) (setq file-cache-alist (car (read-from-string (buffer-substring (point-min) (point-max)))))))) ;;; Code ends here Here is some code to test and verify that it works: First load the cache with some file names: (file-cache-add-directory "~/") Next, check size of cache: (message "File cache size: %d" (length file-cache-alist)) Save cache to a file: (file-cache-save-cache-to-file "~/.file_cache") Clear it and make sure it is empty: (file-cache-clear-cache) (message "File cache size: %d" (length file-cache-alist)) Read cache from file and verify we got the same number of cache items back: (file-cache-read-cache-from-file "~/.file_cache") (message "File cache size: %d" (length file-cache-alist)) In your .emacs file, you only need to do this: (require 'filecache) (file-cache-read-cache-from-file "~/.file_cache") What do others think? Would this be acceptable/useful? If not, I will happily keep this hack to myself.