org-persist.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. ;;; org-persist.el --- Persist data across Emacs sessions -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2021-2021 Free Software Foundation, Inc.
  3. ;; Author: Ihor Radchenko <yantar92 at gmail dot com>
  4. ;; Keywords: cache, storage
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; This file implements persistant data storage across Emacs sessions.
  19. ;; Both global and buffer-local data can be stored.
  20. ;;; Code:
  21. (require 'org-compat)
  22. (require 'org-id)
  23. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  24. (declare-function org-next-visible-heading "org" (arg))
  25. (declare-function org-at-heading-p "org" (&optional invisible-not-ok))
  26. (defvar org-persist-path (org-file-name-concat user-emacs-directory "org-persist/")
  27. "Directory where the data is stored.")
  28. (defvar org-persist-index-file "index"
  29. "File name used to store the data index.")
  30. (defvar org-persist-before-write-hook nil
  31. "Abnormal hook ran before saving data for a single variable in a buffer.
  32. The hook must accept the same arguments as `org-persist-write'.
  33. The hooks will be evaluated until a hook returns non-nil.
  34. If any of the hooks return non-nil, do not save the data.")
  35. (defvar org-persist-before-read-hook nil
  36. "Abnormal hook ran before reading data for a single variable in a buffer.
  37. The hook must accept the same arguments as `org-persist-read'.
  38. The hooks will be evaluated until a hook returns non-nil.
  39. If any of the hooks return non-nil, do not read the data.")
  40. (defvar org-persist-after-read-hook nil
  41. "Abnormal hook ran after reading data for a single variable in a buffer.
  42. The hook must accept the same arguments as `org-persist-read'.")
  43. (defvar org-persist--index nil
  44. "Global index.
  45. The index is a list of plists. Each plist contains information about
  46. a data variable. Each plist contains the following properties:
  47. - `:variable' list of variables to be stored in single file
  48. - `:persist-file': data file name
  49. - `:path': buffer file path, if any
  50. - `:inode': buffer file inode, if any
  51. - `:hash': buffer hash, if any")
  52. (defun org-persist--get-index (var &optional buffer)
  53. "Return plist used to store VAR in BUFFER.
  54. When BUFFER is nil, return plist for global VAR."
  55. (let* ((buffer-file (when buffer (buffer-file-name (or (buffer-base-buffer buffer)
  56. buffer))))
  57. (inode (when buffer-file (file-attribute-inode-number (file-attributes buffer-file)))))
  58. (let ((result (seq-find (lambda (plist)
  59. (and (or (memq var (plist-get plist :variable))
  60. (eq var (plist-get plist :variable)))
  61. (or (equal inode (plist-get plist :inode))
  62. (equal buffer-file (plist-get plist :path)))))
  63. org-persist--index)))
  64. (when result
  65. (unless (equal buffer-file (plist-get result :path))
  66. (setf result (plist-put result :path buffer-file))))
  67. (unless result
  68. (push (list :variable (if (listp var) var (list var))
  69. :persist-file (replace-regexp-in-string "^.." "\\&/" (org-id-uuid))
  70. :path buffer-file
  71. :inode inode
  72. :hash (when buffer (secure-hash 'md5 buffer)))
  73. org-persist--index)
  74. (setf result (car org-persist--index)))
  75. result)))
  76. (defun org-persist--read-index ()
  77. "Read `org-persist--index'"
  78. (unless org-persist--index
  79. (when (file-exists-p (org-file-name-concat org-persist-path org-persist-index-file))
  80. (with-temp-buffer
  81. (insert-file-contents (org-file-name-concat org-persist-path org-persist-index-file))
  82. (setq org-persist--index (read (current-buffer)))))))
  83. (cl-defun org-persist-register (var &optional buffer &key inherit)
  84. "Register VAR in BUFFER to be persistent.
  85. Optional key INHERIT make VAR dependent on another variable. Such
  86. dependency means that data shared between variables will be preserved
  87. (see elisp#Circular Objects)."
  88. (unless org-persist--index (org-persist--read-index))
  89. (when inherit
  90. (let ((inherited-index (org-persist--get-index inherit buffer)))
  91. (unless (memq var (plist-get inherited-index :variable))
  92. (push var (plist-get inherited-index :variable)))))
  93. (org-persist--get-index var buffer)
  94. (when buffer
  95. (add-hook 'kill-buffer-hook #'org-persist-write-all-buffer 1000 'local)))
  96. (defun org-persist-unregister (var &optional buffer)
  97. "Unregister VAR in BUFFER to be persistent.
  98. When BUFFER is `all', unregister VAR in all buffers."
  99. (unless org-persist--index (org-persist--read-index))
  100. (setq org-persist--index
  101. (seq-remove
  102. (lambda (plist)
  103. (when (and (memq var (plist-get plist :variable))
  104. (or (eq buffer 'all)
  105. (eq (buffer-file-name
  106. (or (buffer-base-buffer buffer)
  107. buffer))
  108. (plist-get plist :path))))
  109. (if (length> (plist-get plist :variable) 1)
  110. (progn
  111. (setq plist
  112. (plist-put plist :variable
  113. (delq var (plist-get plist :variable))))
  114. ;; Do not remove the index though.
  115. nil)
  116. (let ((persist-file (org-file-name-concat org-persist-path (plist-get plist :persist-file))))
  117. (delete-file persist-file)
  118. (when (directory-empty-p (file-name-directory persist-file))
  119. (delete-directory (file-name-directory persist-file))))
  120. 'delete-from-index)))
  121. org-persist--index))
  122. (org-persist-gc))
  123. (defun org-persist-write (var &optional buffer)
  124. "Save buffer-local data in BUFFER for VAR."
  125. (unless (and buffer (not (get-buffer buffer)))
  126. (unless (listp var) (setq var (list var)))
  127. (with-current-buffer (or buffer (current-buffer))
  128. (let ((index (org-persist--get-index var buffer)))
  129. (setf index (plist-put index :hash (when buffer (secure-hash 'md5 buffer))))
  130. (let ((print-circle t)
  131. print-level
  132. print-length
  133. print-quoted
  134. (print-escape-control-characters t)
  135. (print-escape-nonascii t)
  136. (print-continuous-numbering t)
  137. print-number-table)
  138. (unless (seq-find (lambda (v)
  139. (run-hook-with-args-until-success 'org-persist-before-write-hook v buffer))
  140. (plist-get index :variable))
  141. (unless (file-exists-p org-persist-path)
  142. (make-directory org-persist-path))
  143. (with-temp-file (org-file-name-concat org-persist-path org-persist-index-file)
  144. (prin1 org-persist--index (current-buffer)))
  145. (let ((file (org-file-name-concat org-persist-path (plist-get index :persist-file)))
  146. (data (mapcar (lambda (s) (cons s (symbol-value s)))
  147. (plist-get index :variable))))
  148. (unless (file-exists-p (file-name-directory file))
  149. (make-directory (file-name-directory file) t))
  150. (with-temp-file file
  151. (prin1 data (current-buffer))))))))))
  152. (defun org-persist-write-all (&optional buffer)
  153. "Save all the persistent data."
  154. (dolist (index org-persist--index)
  155. (when (or (not (plist-get index :path))
  156. (and (get-file-buffer (plist-get index :path))
  157. (or (not buffer)
  158. (equal (buffer-file-name (or (buffer-base-buffer buffer)
  159. buffer))
  160. (plist-get index :path)))))
  161. (org-persist-write (plist-get index :variable)
  162. (when (plist-get index :path)
  163. (get-file-buffer (plist-get index :path)))))))
  164. (defun org-persist-write-all-buffer ()
  165. "Call `org-persist-write-all' in current buffer."
  166. (org-persist-write-all (current-buffer)))
  167. (defun org-persist-read (var &optional buffer)
  168. "Restore VAR data in BUFFER."
  169. (let* ((index (org-persist--get-index var buffer))
  170. (persist-file (org-file-name-concat org-persist-path (plist-get index :persist-file)))
  171. (data nil))
  172. (when (and (file-exists-p persist-file)
  173. (or (not buffer)
  174. (equal (secure-hash 'md5 buffer) (plist-get index :hash))))
  175. (unless (seq-find (lambda (v)
  176. (run-hook-with-args-until-success 'org-persist-before-read-hook v buffer))
  177. (plist-get index :variable))
  178. (with-temp-buffer
  179. (let ((coding-system-for-read 'utf-8)
  180. (read-circle t))
  181. (insert-file-contents persist-file))
  182. ;; FIXME: Reading sometimes fails to read circular objects.
  183. ;; I suspect that it happens when we have object reference
  184. ;; #N# read before object definition #N=. If it is really
  185. ;; #so, it should be Emacs bug - either in `read' or in
  186. ;; #`prin1'. Meanwhile, just fail silently when `read'
  187. ;; #fails to parse the saved cache object.
  188. (condition-case err
  189. (setq data (read (current-buffer)))
  190. (error
  191. (warn "Emacs reader failed to read data for %S:%S. The error was: %S"
  192. (or buffer "global") var (error-message-string err))
  193. (setq data nil))))
  194. (with-current-buffer (or buffer (current-buffer))
  195. (cl-loop for var1 in (plist-get index :variable)
  196. do
  197. (when (alist-get var1 data)
  198. (setf (symbol-value var1) (alist-get var1 data)))
  199. (run-hook-with-args 'org-persist-after-read-hook var1 buffer)))))))
  200. (defun org-persist-read-all (&optional buffer)
  201. "Restore all the persistent data in BUFFER."
  202. (unless org-persist--index (org-persist--read-index))
  203. (dolist (index org-persist--index)
  204. (when (equal (buffer-file-name (or (buffer-base-buffer buffer)
  205. buffer))
  206. (plist-get index :path))
  207. (org-persist-read (plist-get index :variable) buffer))))
  208. (defun org-persist-read-all-buffer ()
  209. "Call `org-persist-read-all' in current buffer."
  210. (org-persist-read-all (current-buffer)))
  211. (defun org-persist-gc ()
  212. "Remove stored data for not existing files or unregistered variables."
  213. (let (new-index)
  214. (dolist (index org-persist--index)
  215. (when-let ((file (plist-get index :path))
  216. (persist-file (org-file-name-concat
  217. org-persist-path
  218. (plist-get index :persist-file))))
  219. (if (file-exists-p file)
  220. (push index new-index)
  221. (when (file-exists-p persist-file)
  222. (delete-file persist-file)
  223. (when (directory-empty-p (file-name-directory persist-file))
  224. (delete-directory (file-name-directory persist-file)))))))
  225. (setq org-persist--index (nreverse new-index))))
  226. (add-hook 'kill-emacs-hook #'org-persist-gc)
  227. (add-hook 'kill-emacs-hook #'org-persist-write-all 1000)
  228. (add-hook 'after-init-hook #'org-persist-read-all)
  229. (provide 'org-persist)
  230. ;;; org-persist.el ends here