From 97bbe5e3225def2e39a9d109620d62b1841ad5be Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Thu, 12 May 2022 22:38:01 -0500 Subject: [PATCH] Inhibit org-mode-hook in org-agenda --- init.el | 1 + lisp/+org.el | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/init.el b/init.el index 73f5685..c511871 100644 --- a/init.el +++ b/init.el @@ -571,6 +571,7 @@ (setq load-path (cl-remove-if (lambda (path) (string-match-p "lisp/org\\'" path)) load-path)) (:also-load +org) + (with-eval-after-load '+org (+org-agenda-inhibit-hooks-mode +1)) (:option org-adapt-indentation nil org-auto-align-tags t org-archive-mark-done t diff --git a/lisp/+org.el b/lisp/+org.el index 6075b60..89ed483 100644 --- a/lisp/+org.el +++ b/lisp/+org.el @@ -650,5 +650,39 @@ and POST-PROCESS are passed to `org-export-to-file'." (set-category-table +org-category-table) (setq-local word-wrap-by-category t)) + +;;; Inhibit hooks on `org-agenda' +;; It's really annoying when I call `org-agenda' and five hundred Ispell +;; processes are created because I have `flyspell-mode' in the hook. This mode +;; inhibits those hooks when entering the agenda, but runs them when opening the +;; actual buffer. + +(defun +org-agenda-inhibit-hooks (fn &rest r) + "Advice to inhibit hooks when entering `org-agenda'." + (dlet ((org-mode-hook nil)) ; I'm not sure if `dlet' is strictly needed + (apply fn r))) + +(defvar-local +org-hook-has-run-p nil + "Whether `org-mode-hook' has run in the current buffer.") + +(defun +org-agenda-switch-run-hooks (&rest _) + "Advice to run `org-mode-hook' when entering org-mode. +This should only fire when switching to a buffer from `org-agenda'." + (unless +org-hook-has-run-p + (run-hooks 'org-mode-hook) + (setq +org-hook-has-run-p t))) + +(define-minor-mode +org-agenda-inhibit-hooks-mode + "Inhibit `org-mode-hook' when opening `org-agenda'." + :lighter "" + :global t + (if +org-agenda-inhibit-hooks-mode + (progn ; Enable + (advice-add 'org-agenda :around #'+org-agenda-inhibit-hooks) + (advice-add 'org-agenda-switch-to :after #'+org-agenda-switch-run-hooks)) + (progn ; Disable + (advice-remove 'org-agenda #'+org-agenda-inhibit-hooks) + (advice-remove 'org-agenda-switch-to #'+org-agenda-switch-run-hooks)))) + (provide '+org) ;;; +org.el ends here