0

I use org-roam-dailies for my TODO lists for given day. And currently, I set my org-agenda-files to org-roam-dailies's directory as below:

  (setq org-agenda-files  '("~/roamnotes/daily/"))

Problem: as my number of daily entries increasing, org-agenda is loading all my entries in the directory above, resulting quite some time lag.

Possible Fix: For me, I don't need to load entries that's older than 2 or more week, (and also 2 weeks in the future). Less files to open = faster org-agenda. Daily entries have filename format yyyy-mm-dd.org (e.g. 2022-11-09.org). So I'm guessing that with some regular-expression tricks this can be achieved.

Question: How to make org-agenda to load entries that is within ±2 weeks time windows?

1 Answer 1

1

Here's a function that you can use:

;;; -*- lexical-binding: t -*-

(defun my/filter-org-roam-dailies (dir ndays-before ndays-after)

  "Filter the list of files in DIR that have a `.org' suffix. They are all
 supposed to have names of the form \"YYYY-MM-DD.org\". We only
 accept those whose names have a date part that falls within the
 date range `<today> - NDAYS-BEFORE' and `<today> + NDAYS-AFTER',
 inclusive of the ends dates."

  ;; This "inner" function finds the "free" variables `ndays-before' and
  ;; `ndays-after' in the lexical environment, in this case the
  ;; arguments of the "outer" function
  (cl-flet ((my/pred-org-roam-dailies (filename)
    "Check if the FILENAME qualifies for inclusion."
    (let ((fname-date (file-name-sans-extension (file-name-nondirectory filename)))
          (date-min (format-time-string "%Y-%m-%d" (time-add nil (* -86400 (+ 1 ndays-before)))))
          (date-max (format-time-string "%Y-%m-%d" (time-add nil (* 86400 (+ 1 ndays-after))))))
      ;; if the date corresponding to the name of the file falls between date-min
      ;; and date-max, return the (complete) filename
      (when (and (string<  date-min fname-date) (string< fname-date date-max))
        filename))))
    ;; end of inner function

    ;; the "outer" function maps the above predicate on the list of files
    ;; with a ".org" suffix in the given DIRectory and returns the
    ;; filtered list (after deleting nil entries).
    (delq nil
          (mapcar #'my/pred-org-roam-dailies (directory-files dir t "\\.org\\'")))))

Save the above in a file, load the file and then you can use the function like this:

(setq org-agenda-files (my/filter-org-roam-dailies "~/roamnotes/daily/" 14 14)

Check the value of org-agenda-files afterwards (with C-h v org-agenda-files) to make sure you got everything you wanted.

You will have to recalculate org-agenda-files every day however: there is no automation for that in this answer.

EDIT: Fixed the regexp in the directory-files call to match only .org files. This is the same regexp as the one used in auto-mode-alist, so it should be correct, but it's only lightly tested at this point.

4
  • Thanks dude ! This Rocks
    – Garid
    Commented Apr 3, 2023 at 8:00
  • This seems accepting the emacs backup for each org-files. (both 2023-04-04.org & 2023-04-04.org~ loaded.) How to exclude files that are ends with ~?
    – Garid
    Commented Apr 3, 2023 at 8:31
  • 1
    Fixed the regexp to do that. Thanks for the heads-up! I should have seen that coming :-)
    – NickD
    Commented Apr 3, 2023 at 12:03
  • Thanks @NickD, This is working perfectly
    – Garid
    Commented Apr 3, 2023 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.