# Task management with org-roam Vol. 4: Automatic tagging

July 7, 2020

In the previous article we covered automatic tagging of notes related to a specific person, and today we are going to cover automatic tagging of an org-mode heading upon insertion of link related to a person. To put it simple, when I mention someone in the task, I would love this task to be automatically tagged with that persons name. As they say, it’s better to see once, than imagine multiple times, so here is a screencast.

The solution is based on the relatively recent pull request to the org-roam. Instead of using org-roam-insert directly, we can write a wrapper for it leveraging the return value, name manipulation from the previous article and tags lookup from Org-roam tags article.

(defun +org-notes-insert ()
"Insert a link to the note."
(interactive)
(when-let*
((res (org-roam-insert))
(path (plist-get res :path))
(title (plist-get res :title))
(roam-tags (+seq-flatten
(+seq-flatten
(org-roam-db-query [:select tags
:from tags
:where (= file \$s1)]
path)))))
(when (seq-contains-p roam-tags "People")
(save-excursion
(ignore-errors
(org-set-tags
(seq-uniq
(cons
(+org-notes--title-to-tag title)
(org-get-tags nil t)))))))))

The implementation is straight-forward. We start with calling org-roam-insert that asks for the note to insert. Then we parse result and query the roam tags to understand if the inserted note is related to a person. And if the answer is yes, we use org-set-tags to automatically tag the heading.

In my case, implementation of +org-notes--title-to-tag is the following.

(defun +org-notes--title-to-tag (title)
"Convert TITLE to tag."
(concat "@" (s-replace " " "" title)))

And that’s it! Keep in mind, that with this approach automatic tagging does not happen when mentioning people using company interface (e.g. using org-roam-company) and when using +org-notes-insert to mention someone unknown to your system (e.g. when you create a new file). Both of the ‘features’ can be implemented using hooks.

P.S. +org-notes-insert can be modified to skip tagging when called with universal argument. Feel free to reach me if you need help with that!