org-persist.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. (setq inherited-index
  93. (plist-put inherited-index :variable
  94. (cons var (plist-get inherited-index :variable)))))))
  95. (org-persist--get-index var buffer)
  96. (when buffer
  97. (add-hook 'kill-buffer-hook #'org-persist-write-all-buffer 1000 'local)))
  98. (defun org-persist-unregister (var &optional buffer)
  99. "Unregister VAR in BUFFER to be persistent.
  100. When BUFFER is `all', unregister VAR in all buffers."
  101. (unless org-persist--index (org-persist--read-index))
  102. (setq org-persist--index
  103. (seq-remove
  104. (lambda (plist)
  105. (when (and (memq var (plist-get plist :variable))
  106. (or (eq buffer 'all)
  107. (eq (buffer-file-name
  108. (or (buffer-base-buffer buffer)
  109. buffer))
  110. (plist-get plist :path))))
  111. (if (> (length (plist-get plist :variable)) 1)
  112. (progn
  113. (setq plist
  114. (plist-put plist :variable
  115. (delq var (plist-get plist :variable))))
  116. ;; Do not remove the index though.
  117. nil)
  118. (let ((persist-file (org-file-name-concat org-persist-path (plist-get plist :persist-file))))
  119. (delete-file persist-file)
  120. (when (org-directory-empty-p (file-name-directory persist-file))
  121. (delete-directory (file-name-directory persist-file))))
  122. 'delete-from-index)))
  123. org-persist--index))
  124. (org-persist-gc))
  125. (defun org-persist-write (var &optional buffer)
  126. "Save buffer-local data in BUFFER for VAR."
  127. (unless (and buffer (not (get-buffer buffer)))
  128. (unless (listp var) (setq var (list var)))
  129. (with-current-buffer (or buffer (current-buffer))
  130. (let ((index (org-persist--get-index var buffer)))
  131. (setf index (plist-put index :hash (when buffer (secure-hash 'md5 buffer))))
  132. (let ((print-circle t)
  133. print-level
  134. print-length
  135. print-quoted
  136. (print-escape-control-characters t)
  137. (print-escape-nonascii t)
  138. (print-continuous-numbering t)
  139. print-number-table)
  140. (unless (seq-find (lambda (v)
  141. (run-hook-with-args-until-success 'org-persist-before-write-hook v buffer))
  142. (plist-get index :variable))
  143. (unless (file-exists-p org-persist-path)
  144. (make-directory org-persist-path))
  145. (with-temp-file (org-file-name-concat org-persist-path org-persist-index-file)
  146. (prin1 org-persist--index (current-buffer)))
  147. (let ((file (org-file-name-concat org-persist-path (plist-get index :persist-file)))
  148. (data (mapcar (lambda (s) (cons s (symbol-value s)))
  149. (plist-get index :variable))))
  150. (unless (file-exists-p (file-name-directory file))
  151. (make-directory (file-name-directory file) t))
  152. (with-temp-file file
  153. (prin1 data (current-buffer))))))))))
  154. (defun org-persist-write-all (&optional buffer)
  155. "Save all the persistent data."
  156. (dolist (index org-persist--index)
  157. (when (or (not (plist-get index :path))
  158. (and (get-file-buffer (plist-get index :path))
  159. (or (not buffer)
  160. (equal (buffer-file-name (or (buffer-base-buffer buffer)
  161. buffer))
  162. (plist-get index :path)))))
  163. (org-persist-write (plist-get index :variable)
  164. (when (plist-get index :path)
  165. (get-file-buffer (plist-get index :path)))))))
  166. (defun org-persist-write-all-buffer ()
  167. "Call `org-persist-write-all' in current buffer."
  168. (org-persist-write-all (current-buffer)))
  169. (defun org-persist-read (var &optional buffer)
  170. "Restore VAR data in BUFFER."
  171. (let* ((index (org-persist--get-index var buffer))
  172. (persist-file (org-file-name-concat org-persist-path (plist-get index :persist-file)))
  173. (data nil))
  174. (when (and (file-exists-p persist-file)
  175. (or (not buffer)
  176. (equal (secure-hash 'md5 buffer) (plist-get index :hash))))
  177. (unless (seq-find (lambda (v)
  178. (run-hook-with-args-until-success 'org-persist-before-read-hook v buffer))
  179. (plist-get index :variable))
  180. (with-temp-buffer
  181. (let ((coding-system-for-read 'utf-8)
  182. (read-circle t))
  183. (insert-file-contents persist-file))
  184. ;; FIXME: Reading sometimes fails to read circular objects.
  185. ;; I suspect that it happens when we have object reference
  186. ;; #N# read before object definition #N=. If it is really
  187. ;; #so, it should be Emacs bug - either in `read' or in
  188. ;; #`prin1'. Meanwhile, just fail silently when `read'
  189. ;; #fails to parse the saved cache object.
  190. (condition-case err
  191. (setq data (read (current-buffer)))
  192. (error
  193. (warn "Emacs reader failed to read data for %S:%S. The error was: %S"
  194. (or buffer "global") var (error-message-string err))
  195. (setq data nil))))
  196. (with-current-buffer (or buffer (current-buffer))
  197. (cl-loop for var1 in (plist-get index :variable)
  198. do
  199. (when (alist-get var1 data)
  200. (setf (symbol-value var1) (alist-get var1 data)))
  201. (run-hook-with-args 'org-persist-after-read-hook var1 buffer)))))))
  202. (defun org-persist-read-all (&optional buffer)
  203. "Restore all the persistent data in BUFFER."
  204. (unless org-persist--index (org-persist--read-index))
  205. (dolist (index org-persist--index)
  206. (when (equal (buffer-file-name (or (buffer-base-buffer buffer)
  207. buffer))
  208. (plist-get index :path))
  209. (org-persist-read (plist-get index :variable) buffer))))
  210. (defun org-persist-read-all-buffer ()
  211. "Call `org-persist-read-all' in current buffer."
  212. (org-persist-read-all (current-buffer)))
  213. (defun org-persist-gc ()
  214. "Remove stored data for not existing files or unregistered variables."
  215. (let (new-index)
  216. (dolist (index org-persist--index)
  217. (when-let ((file (plist-get index :path))
  218. (persist-file (org-file-name-concat
  219. org-persist-path
  220. (plist-get index :persist-file))))
  221. (if (file-exists-p file)
  222. (push index new-index)
  223. (when (file-exists-p persist-file)
  224. (delete-file persist-file)
  225. (when (org-directory-empty-p (file-name-directory persist-file))
  226. (delete-directory (file-name-directory persist-file)))))))
  227. (setq org-persist--index (nreverse new-index))))
  228. (add-hook 'kill-emacs-hook #'org-persist-gc)
  229. (add-hook 'kill-emacs-hook #'org-persist-write-all 1000)
  230. (add-hook 'after-init-hook #'org-persist-read-all)
  231. (provide 'org-persist)
  232. ;;; org-persist.el ends here