Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Helpers to manage `org-roam` tags -- https://d12frosted.io/posts/2020-06-10-org-roam-tags.html
(defun +org-notes-tags-read ()
"Return list of tags as set in the buffer."
(org-roam--extract-tags-prop (buffer-file-name (buffer-base-buffer))))
(defun +org-notes-tags-delete ()
"Delete a tag from current note."
(interactive)
(unless (+org-notes-buffer-p)
(user-error "Current buffer is not a note"))
(let* ((tags (+org-notes-tags-read))
(tag (completing-read "Tag: " tags nil 'require-match)))
(+org-buffer-prop-set
"ROAM_TAGS"
(combine-and-quote-strings (delete tag tags)))
(org-roam-db--update-tags)))
(defun +org-notes-tags-add ()
"Add a tag to current note."
(interactive)
(unless (+org-notes-buffer-p)
(user-error "Current buffer is not a note"))
(let* ((tags (seq-uniq
(+seq-flatten
(+seq-flatten
(org-roam-db-query [:select tags :from tags])))))
(tag (completing-read "Tag: " tags)))
(when (string-empty-p tag)
(user-error "Tag can't be empty"))
(+org-buffer-prop-set
"ROAM_TAGS"
(combine-and-quote-strings (seq-uniq (cons tag (+org-notes-tags-read)))))
(org-roam-db--update-tags)))
(defun +org-notes-buffer-p ()
"Return non-nil if the currently visited buffer is a note."
(and buffer-file-name
(string-equal (expand-file-name (file-name-as-directory org-roam-directory))
(file-name-directory buffer-file-name))))
(defun +seq-flatten (list-of-lists)
"Flatten LIST-OF-LISTS."
(apply #'append list-of-lists))
(defun +org-buffer-prop-set (name value)
"Set a buffer property called NAME to VALUE."
(save-excursion
(widen)
(goto-char (point-min))
(if (re-search-forward (concat "^#\\+" name ": \\(.*\\)") (point-max) t)
(replace-match (concat "#+" name ": " value))
;; find the first line that doesn't begin with ':' or '#'
(let ((found))
(while (not (or found (eobp)))
(beginning-of-line)
(if (or (looking-at "^#")
(looking-at "^:"))
(line-move 1 t)
(setq found t)))
(insert "#+" name ": " value "\n")))))
@randomwangran

This comment has been minimized.

Copy link

randomwangran commented Jun 25, 2020

I find I need to add expand-file-name to tell the buffer is a note or not.

(defun +org-notes-buffer-p ():

(string-equal (expand-file-name (file-name-as-directory org-roam-directory))
                     (file-name-directory buffer-file-name))
@d12frosted

This comment has been minimized.

Copy link
Owner Author

d12frosted commented Jun 25, 2020

Yes, good point! Depending on the value of org-roam-directory you might need to expand it.

@randomwangran

This comment has been minimized.

Copy link

randomwangran commented Jun 25, 2020

Thanks for your code. It really helps to tag the ZK notes.

Do you consider to merge into org-roam? I think this is a useful function.

@d12frosted

This comment has been minimized.

Copy link
Owner Author

d12frosted commented Jul 8, 2020

Thanks for the kind words. Actually, I didn't think about contributing this to org-roam 🤔 Maybe I should to think about it haha.

By the way, I have more utilities for working with tags and filetags. You can read about them here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.