From 3b5849819226e50ef098d7362ffd2bd59f3e1817 Mon Sep 17 00:00:00 2001 From: Danilo Reyes Date: Fri, 11 Apr 2025 20:03:14 -0600 Subject: [PATCH] emacs update cache complete --- dotfiles/doom/config.org | 32 +++++++++++++++------ hosts/workstation/toggles.nix | 1 + modules/scripts/update-org-agenda-cache.nix | 22 ++++++++++++++ 3 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 modules/scripts/update-org-agenda-cache.nix diff --git a/dotfiles/doom/config.org b/dotfiles/doom/config.org index e4022f8..2a62664 100755 --- a/dotfiles/doom/config.org +++ b/dotfiles/doom/config.org @@ -706,13 +706,25 @@ empty list if the cache doesn’t 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 doesn’t 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 diff --git a/hosts/workstation/toggles.nix b/hosts/workstation/toggles.nix index 82f9ca2..2d82202 100644 --- a/hosts/workstation/toggles.nix +++ b/hosts/workstation/toggles.nix @@ -43,6 +43,7 @@ download.enable = true; ffmpreg.enable = true; ffmpeg4discord.enable = true; + update-org-agenda-cache.enable = true; }; servers.drpp.enable = true; } diff --git a/modules/scripts/update-org-agenda-cache.nix b/modules/scripts/update-org-agenda-cache.nix new file mode 100644 index 0000000..540ce09 --- /dev/null +++ b/modules/scripts/update-org-agenda-cache.nix @@ -0,0 +1,22 @@ +{ + config, + lib, + pkgs, + ... +}: +{ + imports = [ ./base.nix ]; + config.my.scripts.update-org-agenda-cache = { + enable = lib.mkDefault false; + install = config.my.emacs.enable; + service = config.my.emacs.enable; + name = "update-org-agenda-cache"; + timer = "*:0/30"; + description = "runs a function which builds a cache file."; + package = pkgs.writeScriptBin "update-org-agenda-cache" '' + #!/usr/bin/env nix-shell + #! nix-shell -i bash -p bash + ${config.services.emacs.package}/bin/emacsclient --eval '(my/update-org-agenda-cache)' + ''; + }; +}