diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2020-08-14 15:04:25 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2020-08-14 15:04:25 -0700 |
commit | d38e731e7abcd2f82bba3381cfe9232c71cbb8ac (patch) | |
tree | 4c5f5cc354714b831aebf4bad004516581409515 | |
parent | 8d6c4c90f9b4e53dd954b0e5e19e22749f235499 (diff) | |
download | dotfiles-d38e731e7abcd2f82bba3381cfe9232c71cbb8ac.tar.gz |
cycle through existing Org-roam daily files; avoid creating new
-rw-r--r-- | .emacs.d/init-spw.el | 69 |
1 files changed, 44 insertions, 25 deletions
diff --git a/.emacs.d/init-spw.el b/.emacs.d/init-spw.el index fe698ced..a8a2aba7 100644 --- a/.emacs.d/init-spw.el +++ b/.emacs.d/init-spw.el @@ -75,9 +75,9 @@ add places the library might be available to `load-path'." (list bindings) bindings)))) -(defmacro spw/buffer-ring-cycle-lambda (&optional ring action) +(cl-defmacro spw/buffer-ring-cycle-lambda (&optional ring action &key start) `(when-let ((buffers ,(or ring '(spw/buffer-siblings-ring ret-val))) - (buffers-pos 0)) + (buffers-pos ,(or start 0))) (lambda (count) (interactive "p") (setq buffers-pos (+ count buffers-pos)) @@ -2114,31 +2114,50 @@ mutt's review view, after exiting EDITOR." (global-set-key "\C-cof" #'org-roam-find-file) - (defun spw/org-roam-dailies-yesterday (n) + (defun spw/org-roam-dailies-ring () + (let* ((today-file (concat (format-time-string "%F") ".org")) + (files (directory-files + org-roam-directory + nil + "\\`[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\.org\\'")) + (ring (make-ring (length files)))) + ;; rotate the list so we begin with today or the closest day after today + (let ((today (cl-do* ((files files (rest files)) + (next (rest files) (rest files))) + ((or (not next) + (string= today-file (car next)) + (string-lessp today-file (car next))) + (setcdr files nil) + next)))) + (setq files (append today files))) + (dolist (file (nreverse files) ring) + (ring-insert ring file)))) + (defun spw/org-roam-dailies-prev (n) (interactive "p") - (spw/org-roam-dailies-tomorrow (- n))) - (defun spw/org-roam-dailies-tomorrow (n) + (spw/org-roam-dailies-next (* -1 n))) + (defun spw/org-roam-dailies-today () + (interactive) + (call-interactively #'org-roam-dailies-today) + (cons (spw/org-roam-dailies-ring) 0)) + (defun spw/org-roam-dailies-next (n) (interactive "p") - (org-roam-dailies-tomorrow - (+ n (or - (and (buffer-file-name) - (string-match - (concat "\\`" - (regexp-quote org-roam-directory) - "/\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\)\\.org" - "\\'") - (buffer-file-name)) - (days-between - (concat (match-string 1 (buffer-file-name)) - (format-time-string "T00:00:00%z")) - (format-time-string "%FT00:00:00%z"))) - 0)))) - ;; TODO avoid creating the files if they don't exist, except for today's - (spw/bind-command-with-cycling - (([?\C-c ?o left] . spw/org-roam-dailies-yesterday) - ("\C-cod" . org-roam-dailies-today) - ([?\C-c ?o right] . spw/org-roam-dailies-tomorrow)) - #'spw/org-roam-dailies-tomorrow) + (let ((today-file (concat (format-time-string "%F") ".org")) + (ring (spw/org-roam-dailies-ring))) + ;; special case: if positive arg and the first item in the ring is not + ;; today, we need to start one earlier + (when (and (> n 0) + (not (string= today-file (ring-ref ring 0)))) + (decf n)) + (find-file (concat org-roam-directory "/" (ring-ref ring n))) + (cons ring n))) + (spw/bind-command-with-ret-val-cycling + (([?\C-c ?o left] . spw/org-roam-dailies-prev) + ("\C-cod" . spw/org-roam-dailies-today) + ([?\C-c ?o right] . spw/org-roam-dailies-next)) + (spw/buffer-ring-cycle-lambda + (car ret-val) + (find-file (concat org-roam-directory "/" buffer)) + :start (cdr ret-val))) ;; don't bother starting it up until we open something in Org-mode (with-eval-after-load 'org |