* mixi.el (mixi-directory): Rename from `mixi-cache-directory'.
authorbg66 <bg66>
Fri, 24 Nov 2006 05:09:01 +0000 (05:09 +0000)
committerbg66 <bg66>
Fri, 24 Nov 2006 05:09:01 +0000 (05:09 +0000)
(mixi-cache-regexp): New regexp.
(mixi-save-cache): New function.
(mixi-read-cache): Ditto.
(mixi-load-cache): Ditto.

ChangeLog
mixi.el

index ede17ef..5138b05 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2006-11-24  OHASHI Akira  <bg66@koka-in.org>
 
+       * mixi.el (mixi-directory): Rename from `mixi-cache-directory'.
+       (mixi-cache-regexp): New regexp.
+       (mixi-save-cache): New function.
+       (mixi-read-cache): Ditto.
+       (mixi-load-cache): Ditto.
+
+2006-11-24  OHASHI Akira  <bg66@koka-in.org>
+
        * mixi.el (mixi-retrieve): New macro.
        (mixi-retrieve-1): Use it.
        (mixi-login): Ditto.
diff --git a/mixi.el b/mixi.el
index 23e774a..f90c783 100644 (file)
--- a/mixi.el
+++ b/mixi.el
   :type 'string
   :group 'mixi)
 
+(defcustom mixi-directory (expand-file-name "~/.mixi")
+  "*Where to look for mixi files."
+  :type 'directory
+  :group 'mixi)
+
 (defcustom mixi-coding-system 'euc-jp
   "*Coding system for mixi."
   :type 'coding-system
@@ -190,11 +195,6 @@ Increase this value when unexpected error frequently occurs."
   :type 'boolean
   :group 'mixi)
 
-(defcustom mixi-cache-directory (expand-file-name "~/.mixi")
-  "*Where to look for cache files."
-  :type 'directory
-  :group 'mixi)
-
 (defvar mixi-me nil)
 
 ;; Utilities.
@@ -441,6 +441,77 @@ Increase this value when unexpected error frequently occurs."
        cache
       (puthash key value table))))
 
+(defconst mixi-cache-regexp "mixi-.*-cache")
+
+(defun mixi-save-cache ()
+  (unless (file-directory-p mixi-directory)
+    (make-directory mixi-directory t))
+  (let ((caches (apropos-internal mixi-cache-regexp 'boundp)))
+    (mapc (lambda (symbol)
+           (with-temp-file (expand-file-name (symbol-name symbol)
+                                             mixi-directory)
+             (let ((coding-system-for-write mixi-coding-system)
+                   (cache (default-value symbol)))
+               (insert "#s(hash-table size "
+                       (number-to-string (hash-table-count cache))
+                       " test equal data (\n")
+               (maphash
+                (lambda (key value)
+                  (let (print-level print-length)
+                    (insert (prin1-to-string key) " "
+                            (prin1-to-string value) "\n")))
+                cache))
+             (insert "))")))
+         caches)))
+
+;; stolen (and modified) from lsdb.el
+(defun mixi-read-cache (&optional marker)
+  "Read one Lisp expression as text from MARKER, return as Lisp object."
+  (save-excursion
+    (goto-char marker)
+    (if (looking-at "^#s(")
+       (let ((end-marker
+              (progn
+                (forward-char 2);skip "#s"
+                (forward-sexp);move to the left paren
+                (point-marker))))
+         (with-temp-buffer
+           (buffer-disable-undo)
+           (insert-buffer-substring (marker-buffer marker)
+                                    marker end-marker)
+           (goto-char (point-min))
+           (delete-char 2)
+           (let ((object (read (current-buffer)))
+                 hash-table data)
+             (if (eq 'hash-table (car object))
+                 (progn
+                   (setq hash-table
+                         (make-hash-table
+                          :size (plist-get (cdr object) 'size)
+                          :test 'equal)
+                         data (plist-get (cdr object) 'data))
+                   (while data
+                     (puthash (pop data) (pop data) hash-table))
+                   hash-table)
+               object))))
+      (read marker))))
+
+(defun mixi-load-cache ()
+  (when (file-directory-p mixi-directory)
+    (let ((files (directory-files mixi-directory nil mixi-cache-regexp)))
+      (mapc (lambda (file)
+             (let ((buffer (find-file-noselect (expand-file-name
+                                                file mixi-directory))))
+               (unwind-protect
+                   (save-excursion
+                     (set-buffer buffer)
+                     (goto-char (point-min))
+                     (re-search-forward "^#s(")
+                     (goto-char (match-beginning 0))
+                     (set (intern file) (mixi-read-cache (point-marker))))
+                 (kill-buffer buffer))))
+           files))))
+
 ;; Object.
 (defconst mixi-object-prefix "mixi-")