From e854e540dd5a52e663c13cc5d0bfbc92a4195ea0 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Wed, 14 Feb 2024 13:00:54 +0800 Subject: [PATCH] Emacs: Fix org-clock opening up all your agenda files --- .config/emacs/init.org | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/.config/emacs/init.org b/.config/emacs/init.org index 693e670..f5ed5b1 100644 --- a/.config/emacs/init.org +++ b/.config/emacs/init.org @@ -2451,7 +2451,64 @@ and there's no need for a middle-man when it's already been implemented. (setq org-directory "~/Notes/") (evil-define-key 'normal calendar-mode-map (kbd "RET") #'org-calendar-select) +;; This is for org-clock-report +(setq org-duration-format 'h:mm) #+end_src + +There's an issue when I invoke ~org-clock-in~ where it seems to search all my +~org-agenda-files~ for any open clocks. This is a bit annoying as it opens all +these files whenever I clock in, making clocking in slow. This fixes it but then +existing clocks don't get resolved. + +Here's a github [[https://github.com/doomemacs/doomemacs/issues/5317][issue]] describing a little bit about the problem; + +#+begin_src emacs-lisp +(with-eval-after-load 'org-clock + (defun joe/org-files-list () + "A version of `org-files-list' that doesn't open all `org-agenda-files'" + (let ((files '())) + (dolist (buf (buffer-list)) + (with-current-buffer buf + (when (and (derived-mode-p 'org-mode) (buffer-file-name)) + (cl-pushnew (expand-file-name (buffer-file-name)) files + :test #'equal)))) + files)) + + (defun org-resolve-clocks (&optional only-dangling-p prompt-fn last-valid) + "Resolve all currently open Org clocks. +If `only-dangling-p' is non-nil, only ask to resolve dangling +\(i.e., not currently open and valid) clocks." + (interactive "P") + (unless org-clock-resolving-clocks + (let ((org-clock-resolving-clocks t)) + (dolist (file (joe/org-files-list)) + (let ((clocks (org-find-open-clocks file))) + (dolist (clock clocks) + (let ((dangling (or (not (org-clock-is-active)) + (/= (car clock) org-clock-marker)))) + (if (or (not only-dangling-p) dangling) + (org-clock-resolve + clock + (or prompt-fn + (lambda (clock) + (format + "Dangling clock started %d mins ago" + (floor (org-time-convert-to-integer + (time-since (cdr clock))) + 60)))) + (or last-valid + (cdr clock)))))))))))) +#+end_src + +In this modified function, =dolist= processes the list returned by =buffer-list= +instead of =org-files-list=. For each buffer in the list, we switch to it with +=with-current-buffer= and then test if its major mode is =org-mode=. If it is, we +process the clocks in that buffer as before. We find open clocks in the current +buffer's associated file using =(buffer-file-name buffer)=. It's assumed that all +Org mode buffers have associated files. +#+end_src + + *** Visuals #+begin_src emacs-lisp