Skip to content

Instantly share code, notes, and snippets.

@jsntn

jsntn/agenda.el

Forked from d12frosted/agenda.el
Created Feb 27, 2021
Embed
What would you like to do?
(defun +org-notes-project-p ()
"Return non-nil if current buffer has any todo entry.
TODO entries marked as done are ignored, meaning the this
function returns nil if current buffer contains only completed
tasks."
(seq-find ; (3)
(lambda (type)
(eq type 'todo))
(org-element-map ; (2)
(org-element-parse-buffer 'headline) ; (1)
'headline
(lambda (h)
(org-element-property :todo-type h)))))
(defun +org-notes-project-update-tag ()
"Update PROJECT tag in the current buffer."
(when (and (not (active-minibuffer-window))
(+org-notes-buffer-p))
(let* ((file (buffer-file-name (buffer-base-buffer)))
(all-tags (org-roam--extract-tags file))
(prop-tags (org-roam--extract-tags-prop file))
(tags prop-tags))
(if (+org-notes-project-p)
(setq tags (cons "Project" tags))
(setq tags (remove "Project" tags)))
(unless (eq prop-tags tags)
(org-roam--set-global-prop
"ROAM_TAGS"
(combine-and-quote-strings (seq-uniq tags)))))))
(defun +org-notes-buffer-p ()
"Return non-nil if the currently visited buffer is a note."
(and buffer-file-name
(string-prefix-p
(expand-file-name (file-name-as-directory org-roam-directory))
(file-name-directory buffer-file-name))))
(defun +org-notes-project-files ()
"Return a list of note files containing Project tag."
(seq-map
#'car
(org-roam-db-query
[:select file
:from tags
:where (like tags (quote "%\"Project\"%"))])))
(defun +agenda-files-update (&rest _)
"Update the value of `org-agenda-files'."
(setq org-agenda-files (+org-notes-project-files)))
(add-hook 'find-file-hook #'+org-notes-project-update-tag)
(add-hook 'before-save-hook #'+org-notes-project-update-tag)
(advice-add 'org-agenda :before #'+agenda-files-update)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment