From 53e166cb0c7c2624a5a54edafe8828d7b7edc612 Mon Sep 17 00:00:00 2001 From: Joseph Turner Date: Tue, 23 May 2023 13:09:29 -0700 Subject: [PATCH 3/5] Make persist-defvar work with hash tables Previously, when persist-defvar received a hash table for an initial value, updated values were not persisted. --- persist.el | 16 +++++++++++----- test/persist-tests.el | 8 ++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/persist.el b/persist.el index 0069273ca2..725f2f71cf 100644 --- a/persist.el +++ b/persist.el @@ -118,7 +118,9 @@ (defun persist-symbol (symbol &optional initvalue) (let ((initvalue (or initvalue (symbol-value symbol)))) (add-to-list 'persist--symbols symbol) (put symbol 'persist t) - (put symbol 'persist-default initvalue))) + (if (hash-table-p initvalue) + (put symbol 'persist-default (copy-hash-table initvalue)) + (put symbol 'persist-default initvalue)))) (defun persist--persistant-p (symbol) "Return non-nil if SYMBOL is a persistant variable." @@ -132,9 +134,13 @@ (defun persist-save (symbol) (unless (persist--persistant-p symbol) (error (format "Symbol %s is not persistant" symbol))) - (let ((symbol-file-loc (persist--file-location symbol))) - (if (equal (symbol-value symbol) - (persist-default symbol)) + (let ((symbol-file-loc (persist--file-location symbol)) + (value (symbol-value symbol)) + (default (persist-default symbol))) + (if (if (and (hash-table-p value) + (hash-table-p default)) + (persist-hash-equal value default) + (equal value default)) (when (file-exists-p symbol-file-loc) (delete-file symbol-file-loc)) (let ((dir-loc @@ -148,7 +154,7 @@ (defun persist-save (symbol) (print-escape-control-characters t) (print-escape-nonascii t) (print-circle t)) - (print (symbol-value symbol) (current-buffer))) + (print value (current-buffer))) (write-region (point-min) (point-max) symbol-file-loc nil 'quiet)))))) diff --git a/test/persist-tests.el b/test/persist-tests.el index 0a85b78767..8a30a24e23 100644 --- a/test/persist-tests.el +++ b/test/persist-tests.el @@ -61,6 +61,14 @@ (ert-deftest test-persist-save-string () "Test saving string." (test-persist-save "foo" "foo" (set sym "bar") "bar")) +(ert-deftest test-persist-save-hash () + "Test saving hash table." + (let* ((hash (make-hash-table)) + (default (copy-hash-table hash))) + (test-persist-save hash default + (puthash 'foo "bar" (symbol-value sym)) + "#s(hash-table size 65 test eql rehash-size 1.5 rehash-threshold 0.8125 data (foo \"bar\"))"))) + (ert-deftest test-persist-load () (with-local-temp-persist (let ((sym (cl-gensym))) -- 2.40.1