Skip to content

Instantly share code, notes, and snippets.

@d12frosted
Last active July 3, 2022 02:19
Show Gist options
  • Save d12frosted/4a55f3d072a813159c1d7b31c21bac9a to your computer and use it in GitHub Desktop.
Save d12frosted/4a55f3d072a813159c1d7b31c21bac9a to your computer and use it in GitHub Desktop.
[MERGED] 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
Copy link

Thanks for sharing this. Tiny functions but very useful.

@d12frosted
Copy link
Author

@randomwangran shared this also because of this org-roam/org-roam#1149 (comment)

@d12frosted
Copy link
Author

d12frosted commented Oct 11, 2020

@randomwangran FYI org-roam/org-roam#1183

The version there is even better than I have in my configurations 😸

@randomwangran
Copy link

Perfect!

Time to upgrade org-roam and play with it. 😃

Thanks for your effort, @d12frosted .

@d12frosted
Copy link
Author

@anoopd
Copy link

anoopd commented Jun 28, 2022

Does this code works with v2 ?

@d12frosted
Copy link
Author

@anoopd See previous comments. This code was already merged into org-roam and available as part of V2.

@d12frosted
Copy link
Author

@anoopd just realised that the linked post contains instructions for V2. See screenshot attached.

8307AD99-E735-441E-8D1B-BB98B7287B20

@anoopd
Copy link

anoopd commented Jul 3, 2022

Thank you for the reply .......... will have a look :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment