Emacs: Fix org-clock opening up all your agenda files

This commit is contained in:
Joseph Ferano 2024-02-14 13:00:54 +08:00
parent 61326287ce
commit e854e540dd

View File

@ -2451,7 +2451,64 @@ and there's no need for a middle-man when it's already been implemented.
(setq org-directory "~/Notes/") (setq org-directory "~/Notes/")
(evil-define-key 'normal calendar-mode-map (kbd "RET") #'org-calendar-select) (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 #+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 *** Visuals
#+begin_src emacs-lisp #+begin_src emacs-lisp