add org-roam-tag-{add,remove}

also fix org-roam interactive functions to operate on node
This commit is contained in:
Jethro Kuan
2021-04-27 18:04:53 +08:00
parent fd23360100
commit eb9a9e487c

View File

@ -791,29 +791,78 @@ window instead."
(org-set-property prop (combine-and-quote-strings lst))
(org-delete-property prop))))
;;;; Tags
(defun org-roam-tag-completions ()
"Return list of tags for completions within Org-roam."
(let ((roam-tags (mapcar #'car (org-roam-db-query [:select :distinct [tag] :from tags])))
(org-tags (cl-loop for tagg in org-tag-alist
nconc (pcase tagg
('(:newline)
nil)
(`(,tag . ,_)
(list tag))
(_ nil)))))
(append roam-tags org-tags)))
(defun org-roam-tag-add (tag)
"Add a tag to the node at point."
(interactive
(list (completing-read "Tag: " (org-roam-tag-completions))))
(let ((node (org-roam-node-at-point 'assert)))
(save-excursion
(goto-char (org-roam-node-point node))
(if (= (org-outline-level) 0)
(org-roam-add-property tag "ROAM_TAGS")
(let ((current-tags (org-get-tags)))
(cl-pushnew tag current-tags)
(org-set-tags current-tags))))))
(defun org-roam-tag-remove ()
"Remove a tag to the node at point."
(interactive)
(let ((node (org-roam-node-at-point 'assert)))
(save-excursion
(goto-char (org-roam-node-point node))
(if (= (org-outline-level) 0)
(org-roam-remove-property "ROAM_TAGS")
(let* ((current-tags (org-get-tags))
(tag (completing-read "Tag: " current-tags)))
(org-set-tags (delete tag current-tags)))))))
;;;; Aliases
(defun org-roam-alias-add (alias)
"Add ALIAS to the node at point."
(interactive "sAlias: ")
(org-roam-add-property alias "ROAM_ALIASES"))
(let ((node (org-roam-node-at-point 'assert)))
(save-excursion
(goto-char (org-roam-node-point node))
(org-roam-add-property alias "ROAM_ALIASES"))))
(defun org-roam-alias-remove ()
"Remove an alias from the node at point."
(interactive)
(org-roam-remove-property "ROAM_ALIASES"))
(let ((node (org-roam-node-at-point 'assert)))
(save-excursion
(goto-char (org-roam-node-point node))
(org-roam-remove-property "ROAM_ALIASES"))))
;;;; Refs
(defun org-roam-ref-add (ref)
"Add REF to the node at point."
(interactive "sRef: ")
(org-roam-add-property ref "ROAM_REFS"))
(let ((node (org-roam-node-at-point 'assert)))
(save-excursion
(goto-char (org-roam-node-point node))
(org-roam-add-property ref "ROAM_REFS"))))
(defun org-roam-ref-remove ()
"Remove a ref from the node at point."
(interactive)
(org-roam-remove-property "ROAM_REFS"))
(let ((node (org-roam-node-at-point 'assert)))
(save-excursion
(goto-char (org-roam-node-point node))
(org-roam-remove-property "ROAM_REFS"))))
;;;; Refs
(defun org-roam-ref--completions ()
"Return an alist for ref completion.
The car is the ref, and the cdr is the corresponding node for the ref."