org-persist-unregister: Allow removing related containers

* lisp/org-persist.el (org-persist-unregister): Add new optional key
:remove-related to remove all the containers stored alongside with
CONTAINER in addition to CONTAINER itself.  Update to docstring.
This commit is contained in:
Ihor Radchenko 2023-01-03 10:56:49 +03:00
parent 82cc6f0fe8
commit 9e5c25ba75
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
1 changed files with 6 additions and 4 deletions

View File

@ -906,20 +906,22 @@ with `org-persist-write'."
(add-hook 'kill-buffer-hook #'org-persist-write-all-buffer nil 'local)))
(when write-immediately (org-persist-write container associated)))
(defun org-persist-unregister (container &optional associated)
(cl-defun org-persist-unregister (container &optional associated &key remove-related)
"Unregister CONTAINER in ASSOCIATED to be persistent.
When ASSOCIATED is `all', unregister CONTAINER everywhere."
When ASSOCIATED is `all', unregister CONTAINER everywhere.
When REMOVE-RELATED is non-nil, remove all the containers stored with
the CONTAINER as well."
(unless org-persist--index (org-persist--load-index))
(setq container (org-persist--normalize-container container))
(setq associated (org-persist--normalize-associated associated))
(if (eq associated 'all)
(mapc (lambda (collection)
(when (member container (plist-get collection :container))
(org-persist-unregister container (plist-get collection :associated))))
(org-persist-unregister container (plist-get collection :associated) :remove-related remove-related)))
org-persist--index)
(let ((collection (org-persist--find-index `(:container ,container :associated ,associated))))
(when collection
(if (= (length (plist-get collection :container)) 1)
(if (or remove-related (= (length (plist-get collection :container)) 1))
(org-persist--remove-from-index collection)
(plist-put collection :container
(remove container (plist-get collection :container)))