1 ;;;_ tinydb/persist.el --- Create persisting data
5 ;; Copyright (C) 2007,2010 Tom Breton (Tehom)
7 ;; Author: Tom Breton (Tehom) <tehom@panix.com>
10 ;; This file is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This file is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; This was originally part of my `password' library. I separated it
28 ;; so that it could be re-used.
30 ;;Drawbacks: It only makes one object persist per buffer. This could
31 ;;be remedied with a major rewrite. Even so, it would probably be
36 (require 'tinydb
/asynq
)
43 ;;;_ . tehom-persist-sync-obj-to-buffer
46 (defun tehom-persist-sync-obj-to-buffer (initial-obj &optional type-pred
)
47 "Return the current buffer's data as an object.
48 If we can't read an object, signal error.
49 If TYPE-PRED is given and object doesn't satisfy it, signal error.
51 Do NOT call this unless you know what you are doing. It must only be
52 called in a buffer visiting a file meant to store sexps persistently."
61 (goto-char (point-min))
62 (read (current-buffer))))
65 "In persist buffer %s, contents could not be read"
70 (or (null type-pred
) (funcall type-pred obj
))
74 "In persist buffer %s, object was wrong type"
80 (defalias 'tehom-persist-sync-list-to-buffer
81 'tehom-persist-sync-obj-to-buffer
)
83 ;;;_ . tehom-persist-sync-file-to-obj
85 ;;$$OBSOLESCENT. Use `tinydb-persist--write-obj'
87 (defun tehom-persist-sync-file-to-obj (obj &optional force-save type-pred
)
88 "Sync to the current buffer and file to OBJ
90 Do NOT call this unless you know what you are doing. It will ERASE
91 the contents of the buffer and write another object into it."
96 (funcall type-pred obj
))
97 (error "Object %s does not satisfy %s" obj type-pred
))
100 ((old-str (buffer-string)))
102 (insert (pp-to-string obj
))
105 (goto-char (point-min))
106 (read (current-buffer)))
110 (apply #'signal
(car err
) (cdr err
)))))
115 ((file-precious-flag t
))
116 ;;Save, forcing backups to exist.
120 (defalias 'tehom-persist-sync-file-to-list
'tehom-persist-sync-file-to-obj
)
121 ;;;_ . tinydb-persist--write-obj
122 (defun tinydb-persist--write-obj (obj &optional force-save
)
126 ((old-str (buffer-string)))
128 (insert (pp-to-string obj
))
129 ;;If it can't be read back, it's an error
132 (goto-char (point-min))
133 (read (current-buffer)))
137 (apply #'signal
(car err
) (cdr err
)))))
140 ;;Save, forcing backups to exist.
142 ((file-precious-flag t
))
145 ;;;_ , Macros to use the persisting object
147 ;;;_ . tehom-persist-buffer-as-const-obj
149 (defmacro tehom-persist-buffer-as-const-obj
150 (persist-file var initial-obj type-pred
&rest body
)
151 "Treat the password buffer as an immutable object named VAR"
153 `(with-current-buffer (find-file-noselect ,persist-file
)
156 (tehom-persist-sync-obj-to-buffer ,initial-obj
,type-pred
)))
159 ;;;_ . tehom-persist-buffer-as-const-list
161 (defmacro tehom-persist-buffer-as-const-list
(persist-file var
&rest body
)
162 "Treat the password buffer as an immutable list named VAR"
163 `(tehom-persist-buffer-as-const-obj
164 ,persist-file
,var
() #'listp
,@body
))
167 ;;;_ . tehom-persist-buffer-as-mutable-obj
169 (defmacro tehom-persist-buffer-as-mutable-obj
170 (persist-file var initial-obj type-pred
&rest body
)
173 (with-current-buffer (find-file-noselect ,persist-file
)
175 ((,var
(tehom-persist-sync-obj-to-buffer ,initial-obj
,type-pred
)))
178 (tehom-persist-sync-file-to-obj ,var t
,type-pred
)))))
181 ;;;_ . tehom-persist-buffer-as-mutable-list
184 (defmacro tehom-persist-buffer-as-mutable-list
(persist-file var
&rest body
)
185 "Treat the file PERSIST-FILE as a mutable object named by the
187 `(tehom-persist-buffer-as-mutable-obj
188 ,persist-file
,var
() #'listp
,@body
))
190 ;;;_ . tehom-update-persist-buffer
193 (defun tehom-update-persist-buffer
194 (persist-file obj
&optional force-save type-pred
)
195 "Assign OBJ to the persisting object in the buffer visiting PERSIST-FILE.
196 If TYPE-PRED is given, only write OBJ if it satisfies TYPE-PRED,
197 otherwise signal error."
199 (with-current-buffer (find-file-noselect persist-file
)
200 (tehom-persist-sync-file-to-obj obj force-save type-pred
)))
203 ;;;_ , File-buffer instantiation of asynq
205 ;;;_ . tinydb-persist-make-q
206 (defun tinydb-persist-make-q
207 (filename initial-obj
&optional eager-save type-pred
)
208 "Make a file-based persisting queue"
210 ;;This is the setup for file-based asynq
211 #'(lambda (filename initial-object eager-save
)
213 (find-file-noselect filename eager-save
)
214 (declare (special persist-eager-save
))
222 '(tinydb-persist--write-obj initial-object eager-save
)
223 (tehom-persist-sync-file-to-obj
224 initial-object eager-save
))
229 (with-current-buffer buffer
230 ;;$$USE ME INSTEAD after testing.
234 (goto-char (point-min))
235 (read (current-buffer)))
238 "In persist buffer %s, contents could not be read"
241 (tehom-persist-sync-obj-to-buffer nil
)))
244 #'(lambda (buffer obj
)
245 (with-current-buffer buffer
246 (declare (special persist-eager-save
))
247 ;;Type check is handled by asynq now
249 '(tinydb-persist--write-obj obj persist-eager-save
)
250 (tehom-persist-sync-file-to-obj
255 filename initial-obj eager-save
))
261 ;;;_ * Local emacs vars.
262 ;;;_ + Local variables:
266 (provide 'tinydb
/persist
)
267 ;;; tinydb/persist.el ends here