branch: externals/org-gnosis
commit dd3f1ace88365b558daae153ae5a16549c9a7648
Author: Thanos Apollo <[email protected]>
Commit: Thanos Apollo <[email protected]>

    [fix] cleanup orphaned tags when updating a file.
---
 org-gnosis.el | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/org-gnosis.el b/org-gnosis.el
index 1ca332d980..ad2b710bfc 100644
--- a/org-gnosis.el
+++ b/org-gnosis.el
@@ -403,6 +403,16 @@ If JOURNAL is non-nil, update file as a journal entry."
                      (org-gnosis--delete 'journal `(= id ,node))
                    (org-gnosis--delete 'nodes `(= id ,node)))))))
 
+(defun org-gnosis-tags--cleanup-orphaned ()
+  "Remove orphaned tags that have no associated nodes.
+Efficient: only checks tags not in node-tag table."
+  (let* ((all-tags (org-gnosis-select 'tag 'tags nil t))
+         (used-tags (org-gnosis-select 'tag 'node-tag nil t))
+         (orphaned (cl-set-difference all-tags used-tags :test #'equal)))
+    (when orphaned
+      (dolist (tag orphaned)
+        (emacsql (org-gnosis-db-get) [:delete :from tags :where (= tag $s1)] 
tag)))))
+
 (defun org-gnosis-update-file (&optional file)
   "Update contents of FILE in database.
 
@@ -432,7 +442,9 @@ Removes all contents of FILE in database, adding them anew."
                            ;; For separate journal files, get all items
                            (org-gnosis-get-checked-items parsed-buffer))))
         (cl-loop for done-todo in done-todos
-                do (org-gnosis-mark-todo-as-done done-todo))))))
+                do (org-gnosis-mark-todo-as-done done-todo))))
+    ;; Cleanup orphaned tags
+    (org-gnosis-tags--cleanup-orphaned)))
 
 ;;;###autoload
 (defun org-gnosis-delete-file (&optional file)

Reply via email to