emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/persist 87ac101: Support secondary storage location


From: Phillip Lord
Subject: [elpa] externals/persist 87ac101: Support secondary storage location
Date: Sun, 30 Jun 2019 05:42:34 -0400 (EDT)

branch: externals/persist
commit 87ac101420cb9c378cd68f197647e2721a78d2ae
Author: Phillip Lord <address@hidden>
Commit: Phillip Lord <address@hidden>

    Support secondary storage location
---
 Makefile              |  3 +++
 persist.el            | 34 +++++++++++++++++++++++++++-------
 persist.texi          |  8 +++++++-
 test/persist-tests.el | 25 +++++++++++++++++++++++++
 4 files changed, 62 insertions(+), 8 deletions(-)

diff --git a/Makefile b/Makefile
index 3a2278c..4f7baa8 100644
--- a/Makefile
+++ b/Makefile
@@ -24,3 +24,6 @@ html:
 
 push-elpa:
        git push elpa HEAD:externals/persist
+
+pull-elpa:
+       git pull elpa externals/persist
diff --git a/persist.el b/persist.el
index b02bc31..7292f4a 100644
--- a/persist.el
+++ b/persist.el
@@ -41,7 +41,7 @@
 
 ;;; Code:
 (defvar persist--directory-location
-  (concat user-emacs-directory "persist/")
+  (concat user-emacs-directory "persist")
   "The location of persist directory.")
 
 (defvar persist--symbols nil
@@ -56,9 +56,13 @@ variable is not set to the value.")
 
 (defun persist--file-location (symbol)
   "Return the file name at which SYMBOL does or will persist."
-  (concat persist--directory-location (symbol-name symbol)))
+  (concat
+   (or (get symbol 'persist-location)
+       persist--directory-location)
+   "/"
+   (symbol-name symbol)))
 
-(defmacro persist-defvar (symbol initvalue docstring)
+(defmacro persist-defvar (symbol initvalue docstring &optional location)
   "Define SYMBOL as a persistant variable and return SYMBOL.
 
 This form is nearly equivalent to `defvar', except that the
@@ -74,16 +78,25 @@ DOCSTRING need to be given."
 
   ;; Don't support 2-arity calls either because we are lazy and
   ;; because if you want to persist it, you want to doc it.
-  (declare (debug defvar) (doc-string 3))
+  (declare (debug (symbolp form stringp &optional form)) (doc-string 3))
   ;; Define inside progn so the byte compiler sees defvar
   `(progn
      (defvar ,symbol ,initvalue ,docstring)
      ;; Access initvalue through its symbol because the defvar form
      ;; has to stay at first level within a progn
+     (persist-location ',symbol ,location)
      (persist-symbol ',symbol (symbol-value ',symbol))
      (persist-load ',symbol)
      ',symbol))
 
+(defun persist-location (symbol directory)
+  "Set the directory for persisting the value of symbol.
+
+This does force the loading of value from this directory, so to
+persist a variable, you will normally need to call `persist-load'
+to load a previously saved location."
+  (put symbol 'persist-location directory))
+
 (defun persist-symbol (symbol &optional initvalue)
   "Make SYMBOL a persistant variable.
 
@@ -92,7 +105,11 @@ If non-nil, INITVALUE is the value to which SYMBOL will be 
set if
 current `symbol-value' of SYMBOL.
 
 INITVALUE is set for the session and will itself not persist
-across sessions."
+across sessions.
+
+This does force the loading of value from this directory, so to
+persist a variable, you will normally need to call `persist-load'
+to load a previously saved location."
   (let ((initvalue (or initvalue (symbol-value symbol))))
     (add-to-list 'persist--symbols symbol)
     (put symbol 'persist t)
@@ -112,8 +129,11 @@ variables persist automatically when Emacs exits."
             "Symbol %s is not persistant" symbol)))
   (when (not (= (symbol-value symbol)
                 (persist-default symbol)))
-    (unless (file-exists-p persist--directory-location)
-      (mkdir persist--directory-location))
+    (let ((dir-loc
+           (file-name-directory
+            (persist--file-location symbol))))
+      (unless (file-exists-p dir-loc)
+        (mkdir dir-loc)))
     (with-temp-buffer
       (print (symbol-value symbol) (current-buffer))
       (write-region (point-min) (point-max)
diff --git a/persist.texi b/persist.texi
index 2309f88..8cb17a7 100644
--- a/persist.texi
+++ b/persist.texi
@@ -104,7 +104,6 @@ This function saves @code{symbol} immediately rather than 
waiting till
 the normal time
 @end defun
 
-
 @defun persist-default (symbol)
 Return the default value for @code{symbol}. The default value is
 actually set for each session and does not persist from session to
@@ -117,6 +116,13 @@ value across sessions.
 Change the value of @code{symbol} to the last saved value if it exists.
 @end defun
 
+@defun persist-location (symbol directory)
+Values are usually persisted to a standard location; it is possible to
+change this for individual symbol using this function. Be aware that
+this does not call @code{persist-load}, so this will not restore a
+previously saved value.
+@end defun
+
 @node Comparison
 @section Comparison
 
diff --git a/test/persist-tests.el b/test/persist-tests.el
index bedf0ae..6429ed2 100644
--- a/test/persist-tests.el
+++ b/test/persist-tests.el
@@ -73,3 +73,28 @@
    (should (persist--persistant-p 'test-persist-variable))
    (should (= 20
               (persist-default 'test-persist-variable)))))
+
+(ert-deftest test-persist-location ()
+   (let ((sym (cl-gensym)))
+     (set sym 10)
+     (persist-symbol sym 10)
+     (persist-location sym "./persist-defined-location")
+     (should
+      (equal (concat "./persist-defined-location/"
+                     (symbol-name sym))
+             (persist--file-location sym)))
+     (persist-save sym)
+     (should t)
+     (should-not (file-exists-p (persist--file-location sym)))
+     (set sym 20)
+     (persist-save sym)
+     (should (file-exists-p (persist--file-location sym)))
+     (should
+      (string-match-p
+       "20"
+       (with-temp-buffer
+         (insert-file-contents (persist--file-location sym))
+         (buffer-string))))
+     (should-error
+      (persist-save 'fred))
+     (delete-directory "./persist-defined-location" t)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]