Org-roam-dailies: how to browse through previous days

Here is how to I browse through previsous days. I use file-per-day scheme.

If you are in a buffer: 2022-12-28.org, how you’re going to
switch to day 2022-12-27.org or to day 2022-12-29.org? I used
org-roam-dailies-goto-date to do such a switch, but the default selection is
based on the current date, not the buffer the user is currently visiting.

The following snippet can help you to navigate through yesterday’s or
tomorrow based on the current daily note.

(require 'ts)
(defun wr/org-roam-dailies-capture-current-daily-date-yesterday (&optional goto keys)
  ""
  (interactive "P")
  (if (org-roam-dailies--daily-note-p)
      (let* ((ts-default-format "%Y-%m-%d")
             (current-date-for-this-buffer (buffer-file-name))
             (yesterday-date (ts-format (ts-adjust 'day -1 (ts-parse (file-name-sans-extension
                                                                      (file-name-nondirectory current-date-for-this-buffer)))))))
        (progn
          (org-roam-dailies--capture (time-convert  (ts-unix (ts-parse yesterday-date))) t keys)
          (org-content 2)))
    (message "Not in a daily buffer")))

(defun wr/org-roam-dailies-capture-current-daily-date-tomorrow (&optional goto keys)
  ""
  (interactive "P")
  (if (org-roam-dailies--daily-note-p)
      (let* ((ts-default-format "%Y-%m-%d")
             (current-date-for-this-buffer (buffer-file-name))
             (tomorrow-date (ts-format (ts-adjust 'day +1 (ts-parse (file-name-sans-extension
                                                                     (file-name-nondirectory current-date-for-this-buffer)))))))
        (progn
          (org-roam-dailies--capture (time-convert  (ts-unix (ts-parse tomorrow-date))) t keys)
          (org-content 2)))
    (message "Not in a daily buffer")))

(defhydra wr/hydra-daily-jump (global-map "C-M-; d")
  "There is no yesterday and tomorrow. Only present exists."
  ("j" wr/org-roam-dailies-capture-current-daily-date-yesterday "yesterday")
  ("k" wr/org-roam-dailies-capture-current-daily-date-tomorrow "tomorrow"))

Hope it is useful for someone.

1 Like

Thanks!

Thanks! I have the same daily-note-taking methodology as yours. The org-roam now has the org-roam-dailies-goto-previous-note and org-roam-dailies-goto-next-note functions, which provide navigation for day switching. However, the org-journal provides maturer integration to Emacs calendar compared to org-roam-dailies. Therefore, I borrowed two functions from org-journal to jump to daily notes in calendar view, which might be helpful for those who do not want to install the entire org-journal package.

      ;; Modified from newest org-journal resipotory [2023-01-23].
      (defun pmg/journal-read-entry (_arg &optional event)
        "Open journal entry for selected date for viewing."
        (interactive
         (list current-prefix-arg last-nonmenu-event))
        (let* ((formated-date (pmg/calendar-date->journal-date
                               (calendar-cursor-to-date t event)))
               (file-full-name (expand-file-name (format "%s.org" formated-date) org-roam-directory)))
          (if (file-exists-p file-full-name)
              (find-file file-full-name))))

      (defun pmg/journal-show-entry (_arg &optional event)
        "Open journal entry for selected date for viewing."
        (interactive
         (list current-prefix-arg last-nonmenu-event))
        (let* ((formated-date (pmg/calendar-date->journal-date
                               (calendar-cursor-to-date t event)))
               (file-full-name (expand-file-name (format "%s.org" formated-date) org-roam-directory)))
          (if (file-exists-p file-full-name)
              (let ((buf (find-file-noselect file-full-name)))
                (display-buffer buf t)))))

      (defun pmg/calendar-date->journal-date (date)
        "Convert a date as returned from the calendar (MONTH DAY YEAR) to a time."
        (format "%04d-%02d-%02d" (nth 2 date) (nth 0 date) (nth 1 date)))
      
      (define-key calendar-mode-map (kbd "r") 'pmg/journal-read-entry)
      (define-key calendar-mode-map (kbd "v") 'pmg/journal-show-entry)

There is a built-in binding in Dired C-x C-j (dired-jump) from buffer to its dir, very useful and saves some keystrokes.

1 Like