all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Mathias Dahl <brakjoller@gmail.com>
Subject: Proposing two new functions in filecache.el
Date: Thu, 15 Dec 2005 19:27:24 +0100	[thread overview]
Message-ID: <uek4erzar.fsf@gmail.com> (raw)


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.

             reply	other threads:[~2005-12-15 18:27 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-15 18:27 Mathias Dahl [this message]
2005-12-16  5:08 ` Proposing two new functions in filecache.el Richard M. Stallman
2005-12-16 11:55   ` roadmap? (was Re: Proposing two new functions in filecache.el) Ryan Barrett
2005-12-16 17:40     ` Xavier Maillard
2005-12-16 21:03     ` roadmap? Chong Yidong
2005-12-17  1:04     ` roadmap? (was Re: Proposing two new functions in filecache.el) Richard M. Stallman
2005-12-16 13:18   ` Proposing two new functions in filecache.el Mathias Dahl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=uek4erzar.fsf@gmail.com \
    --to=brakjoller@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.