From 8c9c47f6e3f94d263a5f5e88e28ed583179f2770 Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Sat, 29 Apr 2023 17:25:36 +0200 Subject: [PATCH] Prominently display of active filters in agenda view Displaying the current filters in the mode line alone often goes unnoticed for me, so I need a more direct display. Let's add it to the first structural header. --- init.el | 20 ++++++++++++++------ site-lisp/db-org.el | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/init.el b/init.el index 4d9af23..6ed5303 100644 --- a/init.el +++ b/init.el @@ -742,6 +742,7 @@ endless/org-ispell db/org-agenda-list-deadlines db/org-agenda-skip-tag + db/org-agenda-insert-active-filters hydra-org-agenda-view/body db/org-agenda-insert-efforts db/org-eval-subtree-no-confirm @@ -1134,9 +1135,6 @@ org-agenda-search-headline-for-time nil org-agenda-search-view-always-boolean t - ;; Show daily efforts directly in the agenda - org-agenda-finalize-hook '(db/org-agenda-insert-efforts) - org-agenda-clock-consistency-checks '(:max-duration 9999999 :min-duration 0 @@ -1257,15 +1255,25 @@ (org-agenda-skip-deadline-prewarning-if-scheduled t))))))) :config (progn - ;; avoid important buffers to end up in `org-agenda-new-buffers’ by + ;; Avoid important buffers to end up in `org-agenda-new-buffers’ by ;; opening them manually (mapc #'find-file-noselect org-agenda-files) - (add-hook 'org-agenda-mode-hook #'hl-line-mode 'append) - + ;; Check that all expected agenda files are being displayed. (advice-add 'org-agenda :before #'db/check-special-org-files-in-agenda) + (add-hook 'org-agenda-mode-hook #'hl-line-mode 'append) + + ;; Show daily efforts directly in the agenda + (add-hook 'org-agenda-finalize-hook #'db/org-agenda-insert-efforts) + + ;; Prominently display active filters on top of agenda view; also + ;; update current agenda view when updating filters to make sure our + ;; display is always correct. + (add-hook 'org-agenda-finalize-hook #'db/org-agenda-insert-active-filters) + (add-hook 'org-agenda-filter-hook #'org-agenda-redo-all) + (define-advice org-agenda-redo-all (:around (old-func &rest r) goto-top-and-execute) "Avoid recentering the Org agenda buffer after redo by moving point to the beginning of buffer first." diff --git a/site-lisp/db-org.el b/site-lisp/db-org.el index a72cec8..8eaaa70 100644 --- a/site-lisp/db-org.el +++ b/site-lisp/db-org.el @@ -112,6 +112,40 @@ deadlines." (setq buffer-read-only t) (message "")))) +(defun db/org-agenda-insert-active-filters (&optional match) + "Insert string showing the current agenda filters. + +The filter display is added after the structural header. + +If no agenda filters are active, nothing will be inserted. + +Add this function to `org-agenda-finalize-hook' to enable this." + ;; First delete any present active filter display, as it might be obsolete. + (save-excursion + (when-let ((pos (text-property-any + (point) (point-max) 'db/active-filter-display t))) + (goto-char pos) + (kill-line))) + + ;; Insert current active filters if there are any. + (when (org-agenda-filter-any) + (save-excursion + (when-let ((pos (text-property-any + (point) (point-max) 'org-agenda-structural-header t))) + (goto-char pos) + (end-of-line) + + ;; Current filter display shamelessly stolen from `org-agenda-filter'. + ;; Is there a function to do this? + (let* ((cf (mapconcat #'identity org-agenda-category-filter "")) + (tf (mapconcat #'identity org-agenda-tag-filter "")) + (ef (replace-regexp-in-string "^\\+" "" (or (car org-agenda-effort-filter) ""))) + (rf (replace-regexp-in-string "^\\+" "" (or (car org-agenda-regexp-filter) ""))) + (ff (concat cf tf ef (when (not (equal rf "")) (concat "/" rf "/"))))) + (insert-and-inherit + (propertize (format " [%s]" ff) + 'db/active-filter-display t))))))) + (defun db/org-agenda-skip-tag (tag &optional others) ;; https://stackoverflow.com/questions/10074016/org-mode-filter-on-tag-in-agenda-view "Skip all entries that correspond to TAG.