Skip to content

Instantly share code, notes, and snippets.

@d12frosted
Last active July 3, 2022 02:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

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
Copy link
Author

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

@randomwangran
Copy link

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
Copy link
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.

@d12frosted
Copy link
Author

d12frosted commented Oct 9, 2020

Similar stuff for aliases:

(defun +org-notes-alias-add ()
  "Add an alias to current note."
  (interactive)
  (unless (+org-notes-buffer-p)
    (user-error "Current buffer is not a note"))
  (let ((alias (read-string "Alias: " )))
    (when (string-empty-p alias)
      (user-error "Alias can't be empty"))
    (+org-buffer-prop-set
     "ROAM_ALIAS"
     (combine-and-quote-strings (seq-uniq (cons alias (+org-notes-alias-read)))))
    (org-roam-db--update-file (buffer-file-name (buffer-base-buffer)))))

(defun +org-notes-alias-delete ()
  "Delete an alias from current note."
  (interactive)
  (unless (+org-notes-buffer-p)
    (user-error "Current buffer is not a note"))
  (let* ((aliases (+org-notes-alias-read))
         (alias (completing-read "Alias: " aliases nil 'require-match)))
    (+org-buffer-prop-set
     "ROAM_ALIAS"
     (combine-and-quote-strings (delete alias aliases)))
    (org-roam-db--update-file (buffer-file-name (buffer-base-buffer)))))

(defun +org-notes-alias-read ()
  "Return list of aliases as set in the buffer."
  (unless (+org-notes-buffer-p)
    (user-error "Current buffer is not a note"))
  (org-roam--extract-titles-alias))

@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