Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[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

This comment has been minimized.

Copy link

@randomwangran 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 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 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 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

This comment has been minimized.

Copy link
Owner Author

@d12frosted 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

This comment has been minimized.

Copy link

@randomwangran randomwangran commented Oct 9, 2020

Thanks for sharing this. Tiny functions but very useful.

@d12frosted

This comment has been minimized.

Copy link
Owner Author

@d12frosted d12frosted commented Oct 9, 2020

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

@d12frosted

This comment has been minimized.

Copy link
Owner Author

@d12frosted 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

This comment has been minimized.

Copy link

@randomwangran randomwangran commented Oct 11, 2020

Perfect!

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

Thanks for your effort, @d12frosted .

@d12frosted

This comment has been minimized.

Copy link
Owner Author

@d12frosted d12frosted commented Oct 12, 2020

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