emacs update cache complete

This commit is contained in:
2025-04-11 20:03:14 -06:00
parent 41ad6a2c6d
commit 3b58498192
3 changed files with 46 additions and 9 deletions

View File

@@ -706,13 +706,25 @@ empty list if the cache doesnt exist.
#+begin_src emacs-lisp :tangle ./config.el
(defvar my/org-agenda-cache-file "~/Documents/Notes/.cache"
"Path to the org-agenda cache file.")
(defvar my/org-always-included-agenda-files
'("~/Documents/Notes/20220823172331-chores.org")
"Org files to always include in the agenda, even if no TODOs are found.")
(defun my/update-org-agenda-cache ()
"Scan Org files and cache only those that contain TODOs or checkboxes."
"Scan Org files and cache those with TODOs, checkboxes, or timestamped entries."
(interactive)
(let* ((org-dir "~/Documents/Notes/")
(files (directory-files-recursively org-dir "\\.org$"))
(todo-files '())
(regex "\\*+[ \t]+\\(TODO\\|PROJ\\|ART\\|IDEA\\|HOLD\\|\\[ \\]\\|\\[-\\]\\|\\[\\?\\]\\)"))
(regex (concat
;; Standard TODO entries and checkboxes
"\\*+[ \t]+\\(TODO\\|PROJ\\|ART\\|IDEA\\|HOLD\\|\\[ \\]\\|\\[-\\]\\|\\[\\?\\]\\)"
;; Org scheduling keywords
"\\|SCHEDULED:"
"\\|DEADLINE:"
;; Standalone timestamps like <2025-04-14 Mon>
"\\|<[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}")))
(dolist (file files)
(message "Scanning: %s" file)
(condition-case err
@@ -720,27 +732,29 @@ empty list if the cache doesnt exist.
(insert-file-contents file)
(goto-char (point-min))
(when (re-search-forward regex nil t)
(message " -> TODO found in: %s" file)
(message " -> Relevant entry found in: %s" file)
(push file todo-files)))
(error (message "Error reading %s: %s" file err))))
(setq todo-files (delete-dups todo-files))
(setq todo-files (delete-dups (append todo-files my/org-always-included-agenda-files)))
(if todo-files
(progn
(message "Writing %d TODO files to cache" (length todo-files))
(message "Writing %d agenda-relevant files to cache" (length todo-files))
(with-temp-file my/org-agenda-cache-file
(prin1 todo-files (current-buffer))))
(message "No TODOs found — not writing cache."))))
(message "No relevant entries found — not writing cache."))))
(defun my/load-org-agenda-from-cache ()
"Load org-agenda-files from .cache, or fallback to an empty list."
"Load org-agenda-files from the cache, or fallback to an empty list."
(interactive)
(let ((file my/org-agenda-cache-file))
(if (and (file-exists-p file)
(> (nth 7 (file-attributes file)) 0)) ; size > 0
(if (and (file-readable-p file)
(> (nth 7 (file-attributes file)) 0)) ; file size > 0
(with-temp-buffer
(insert-file-contents file)
(read (current-buffer)))
(message "Agenda cache not found or empty")
nil)))
(after! org
(setq org-agenda-files (my/load-org-agenda-from-cache)))
#+end_src