diff --git a/org-roam.el b/org-roam.el index 96f01b8..7252a91 100644 --- a/org-roam.el +++ b/org-roam.el @@ -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."