On 2026-05-09 01:06, Jacob S. Gordon wrote:
> It turns out that ‘count-lines’ can already do this, so please find v1
> attached.

I missed a check when the clocked task is filtered, attached is a v2.

Range-diff against v1:
1:  7127f0cac ! 1:  0d9a731ad org-agenda-clock-goto: Jump to closest entry and 
respect filtering
    @@ lisp/org-agenda.el: (defun org-agenda-clock-cancel (&optional _arg)
    -+    (cond (closest
    ++    (cond ((and closest
    ++                (not (get-text-property closest 'invisible)))

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 0d9a731ad274a6588654b2bfd602cf4150cf1296 Mon Sep 17 00:00:00 2001
From: "Jacob S. Gordon" <[email protected]>
Date: Sat, 9 May 2026 04:20:00 -0400
Subject: [PATCH v2] 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 | 38 ++++++++++++++++++++++++--------------
 3 files changed, 36 insertions(+), 16 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index b244a68ed..79d58ad64 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 5d91569b6..2d47ea6e8 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -236,6 +236,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..495cafe36 100644
--- a/lisp/org-agenda.el
+++ b/lisp/org-agenda.el
@@ -10301,21 +10301,31 @@ (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))
+         (clock-positions
+          (sort (remq nil (delete-dups
+                           (mapcar (lambda (o)
+                                     (when (eq (overlay-get o 'type)
+                                               'org-agenda-clocking)
+                                       (overlay-start o)))
+                                   (overlays-in (point-min) (point-max)))))
+                :key (lambda (p) (count-lines p pt t))))
+         (closest (car clock-positions)))
+    (cond ((and closest
+                (not (get-text-property closest 'invisible)))
+           (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'."

Range-diff against v1:
1:  7127f0cac ! 1:  0d9a731ad org-agenda-clock-goto: Jump to closest entry and respect filtering
    @@ lisp/org-agenda.el: (defun org-agenda-clock-cancel (&optional _arg)
     +                                   (overlays-in (point-min) (point-max)))))
     +                :key (lambda (p) (count-lines p pt t))))
     +         (closest (car clock-positions)))
    -+    (cond (closest
    ++    (cond ((and closest
    ++                (not (get-text-property closest 'invisible)))
     +           (goto-char closest)
     +           (move-to-column column))
     +          ;; If the currently clocked entry is not in the agenda

base-commit: 8497d48976d5e8e44e496315c5bef3c9c24b44f1
-- 
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