The Complete Computing Environment



Why shouldn't my Mood Log just write in to my Journal daily files? A macro would be nice.

(defvar defdaily-dailies nil
  "A list of daily templates to generate.")

(defvar cce/journal-refresh-timer nil)

(with-eval-after-load 'org-capture
  (add-to-list 'org-capture-templates
               '("r" "Review Entries"))
  (add-to-list 'org-capture-templates
               '("j" "Journal Templates")))

(defun cce/update-defdailies ()
  ;; set up recurring timer to run this function at 11:55pm
  (unless (and cce/journal-refresh-timer
               (time-less-p (current-time) (timer--time cce/journal-refresh-timer)))
    (setq cce/journal-refresh-timer 
          (run-at-time "1day" nil #'cce/update-defdailies)))
  (dolist (daily defdaily-dailies)
    (cce/defdaily daily)))

(defun cce/defdaily (daily)
  (let ((plist (cddr daily)))
    (let ((extra (list :tree-type 'week :unnarrowed 't))
          (key (car daily))
          (name (cadr daily))
          (template (eval (plist-get plist :template)))
          (file-name (eval (plist-get plist :path-fn))))
      (message "Updating %s path to %s" name file-name)

      ;;  remove
      (setq org-capture-templates
            (remove-if (lambda (elt) (equal (car elt) key))

      ;; template
      (unless (file-exists-p file-name)
        (with-current-buffer (find-file-noselect file-name)
          (insert (eval (plist-get plist :header)))

      ;; set up template
      (add-to-list 'org-capture-templates
                   (append `(,key ,name entry
                                  (file+datetree ,file-name) 
                                  (file ,template))
                           (or (append (plist-get plist :extra) extra)

;;  make sure this runs after all the defdailies have been defined
(add-hook 'after-cce-hook 'cce/update-defdailies)
(provide 'cce/defdaily)