dotfiles/.emacs.d/functions.org

1.7 KiB

Custom Elisp Functions

As of now, I haven't added anything here, though I do expect to start adding some custom functions as I learn more about Elisp.

Capture Template Functions

These functions are for my org roam daily capture template.

(defun jm/dt-filter-tasks (helper query)
  (let ((entries (org-map-entries helper query 'agenda)))
    (mapconcat #'identity (delq nil entries) "\n")))

(defun jm/dt-format-link (prefix)
  (let ((item-name (org-entry-get nil "ITEM"))
        (item-id (or (org-entry-get nil "ID")
                     (car (org-property-values "ID")))))
    (when (stringp item-id)
      (format "%s [[id:%s][%s]]" prefix item-id item-name))))

(defun jm/dt-get-priority (priority &optional prompt)
  (jm/dt-filter-tasks
   (lambda () (when (equal priority (org-entry-get nil "PRIORITY"))
                (jm/dt-format-link (or prompt "-"))))
   "TODO=\"TODO\"|TODO=\"IN PROGRESS\""))

(defun jm/dt-get-due-within (days &optional prompt)
  (let* ((time (or (org-capture-get :default-time) (current-time)))
         (date (+ (time-convert time 'integer) (* days 86400))))
    (jm/dt-filter-tasks
     (lambda () (when (member (org-get-todo-state) '("TODO" "IN PROGRESS"))
                  (jm/dt-format-link (or prompt "-"))))
     (format-time-string "DEADLINE<=\"<%Y-%m-%d>\"" date))))

(defun jm/dt-get-status (status &optional prompt)
  (jm/dt-filter-tasks (lambda () (jm/dt-format-link (or prompt "- [ ]")))
                      (concat "TODO=\"" status "\"")))

(defun jm/dt-dynamic-habits (habit week-days)
  (let* ((org-date (or (org-capture-get :default-time) (current-time)))
         (today (downcase (format-time-string "%a" org-date))))
    (when (seq-contains-p week-days today) habit)))