#+TITLE: 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. 

#+begin_src emacs-lisp
(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)
  (jm/dt-filter-tasks
   (lambda () (when (equal priority (org-entry-get nil "PRIORITY"))
                (jm/dt-format-link "-")))
   "TODO=\"TODO\"|TODO=\"IN PROGRESS\""))

(defun jm/dt-get-due-today ()
  (jm/dt-filter-tasks
   (lambda () (when (member (org-get-todo-state) '("TODO" "IN PROGRESS"))
                (jm/dt-format-link "- [ ]")))
   (format-time-string "DEADLINE<=\"<%Y-%m-%d>\""
                       (or (org-capture-get :default-time) (current-time)))))

(defun jm/dt-get-status (status)
  (jm/dt-filter-tasks (lambda () (jm/dt-format-link "- [ ]"))
                      (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)))
#+end_src

