On 2026-05-12 15:03, Ihor Radchenko wrote:
> "Jacob S. Gordon" <[email protected]> writes:
>> IIUC ‘(not (invisible-p …))’ should be used instead to respect
>> ‘buffer-invisibility-spec’.
> 
> This is right. I initially made that small amendment myself, but then
> saw that the invisibility check is only done once.

Understood, thanks!

>> ... In my testing I couldn’t come up with a scenario where only
>> some of the clocking overlays were hidden,
> 
> There is currently no such scenario. But there is nothing stopping
> some future agenda filters from applying filters to certain days
> only. So, I'd prefer something more future-proof that does not rely
> on undocumented state of affairs.

Fair enough, I’ve done this in v3 attached.

Thanks,

-- 
Jacob S. Gordon
[email protected]
Please don’t send me HTML emails or MS Office/Apple iWork documents.
https://useplaintext.email/#etiquette
https://www.fsf.org/campaigns/opendocument
From 8c17d331c1b62ef11356eb8bf0864fa4382b69a7 Mon Sep 17 00:00:00 2001
From: "Jacob S. Gordon" <[email protected]>
Date: Tue, 12 May 2026 16:20:00 -0400
Subject: [PATCH v3] org-agenda-clock-goto: Jump to closest entry and respect
 filtering

When a clocked-in item appears multiple times in an agenda view, jump
to the closest entry.  If the item is filtered out, display it in
another window.

* doc/org-manual.org (Change display, Remote editing): Update
description of 'org-agenda-clock-goto'.
* etc/ORG-NEWS (Miscellaneous): Announce change.
* lisp/org-agenda.el (org-agenda-clock-goto): Jump to the clocking
overlay closest to the one at point if it's not filtered out.

Link: https://list.orgmode.org/[email protected]/
---
 doc/org-manual.org |  6 ++++--
 etc/ORG-NEWS       |  8 ++++++++
 lisp/org-agenda.el | 41 +++++++++++++++++++++++++++--------------
 3 files changed, 39 insertions(+), 16 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index efdcab4e2..333965319 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -10226,7 +10226,8 @@ *** Change display
 
   #+kindex: J
   #+findex: org-agenda-clock-goto
-  Go to the currently clocked-in task /in the agenda buffer/.
+  Jump to the currently clocked-in task /in the agenda buffer/ if
+  visible, or in another window if not.
 
 - {{{kbd(D)}}} (~org-agenda-toggle-diary~) ::
 
@@ -10605,7 +10606,8 @@ *** Remote editing
 
   #+kindex: J
   #+findex: org-agenda-clock-goto
-  Jump to the running clock in another window.
+  Jump to the currently clocked-in task /in the agenda buffer/ if
+  visible, or in another window if not.
 
 - {{{kbd(k)}}} (~org-agenda-capture~) ::
 
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index aef019552..e62a8ec46 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -249,6 +249,14 @@ tangled files.  This behavior no longer applies when =:tangle=
 specifies multiple targets; in that case, absolute links are always
 used and the variable is ignored.
 
+*** Jump to the closest entry and respect filtering in ~org-agenda-clock-goto~
+
+Previously, when a clocked-in item appeared multiple times in an
+agenda view, ~org-agenda-clock-goto~ would choose one arbitrarily.
+Similarly, if filtered out, ~org-agenda-clock-goto~ would jump close
+to one of the invisible entries.  Now, it jumps to the closest visible
+entry, or displays it in another window if filtered out.
+
 * Version 9.8
 ** Important announcements and breaking changes
 
diff --git a/lisp/org-agenda.el b/lisp/org-agenda.el
index 91e0f1bce..1bf444691 100644
--- a/lisp/org-agenda.el
+++ b/lisp/org-agenda.el
@@ -10301,21 +10301,34 @@ (defun org-agenda-clock-cancel (&optional _arg)
   (org-agenda-unmark-clocking-task))
 
 (defun org-agenda-clock-goto ()
-  "Jump to the currently clocked in task within the agenda.
-If the currently clocked in task is not listed in the agenda
-buffer, display it in another window."
+  "Jump to the currently clocked-in task from the agenda.
+If there are multiple entries in the agenda view, jump to the one
+closest to the point.  Otherwise, if the task is not listed in the
+agenda buffer or filtered out, display it in another window."
   (interactive nil org-agenda-mode)
-  (let (pos)
-    (mapc (lambda (o)
-	    (when (eq (overlay-get o 'type) 'org-agenda-clocking)
-	      (setq pos (overlay-start o))))
-	  (overlays-in (point-min) (point-max)))
-    (cond (pos (goto-char pos))
-	  ;; If the currently clocked entry is not in the agenda
-	  ;; buffer, we visit it in another window:
-	  ((bound-and-true-p org-clock-current-task)
-	   (switch-to-buffer-other-window (org-clock-goto)))
-	  (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
+  (let* ((pt (point))
+         (column (current-column))
+         (visible-clock-positions
+          (sort
+           (delete-dups
+            (remq nil
+                  (mapcar (lambda (o)
+                            (when-let* ((_ (eq (overlay-get o 'type)
+                                               'org-agenda-clocking))
+                                        (start (overlay-start o))
+                                        (_ (not (invisible-p start))))
+                              start))
+                          (overlays-in (point-min) (point-max)))))
+           :key (lambda (p) (count-lines p pt t))))
+         (closest (car visible-clock-positions)))
+    (cond (closest
+           (goto-char closest)
+           (move-to-column column))
+          ;; If the currently clocked entry is not in the agenda
+          ;; buffer, we visit it in another window:
+          ((bound-and-true-p org-clock-current-task)
+           (switch-to-buffer-other-window (org-clock-goto)))
+          (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
 
 (defun org-agenda-diary-entry-in-org-file ()
   "Make a diary entry in the file `org-agenda-diary-file'."

base-commit: fb9ebffa9fd900bdd68752a16c6dc493d665d336
-- 
Jacob S. Gordon
[email protected]
Please don’t send me HTML emails or MS Office/Apple iWork documents.
https://useplaintext.email/#etiquette
https://www.fsf.org/campaigns/opendocument

Reply via email to