Ignacio Casso <[email protected]> writes:

> I tried to use `org-sort-entries` programmatically to keep some
> heading sorted when capturing in it or refiling in it. But I saw that
> sometimes a different heading was the one being sorted. After
> debugging it a little, I realize that `org-sort-entries` works only if
> the target heading is visible in the buffer. If it's not, it sorts its
> top-most parent instead.

This patch fixes it:

>From fd4472c971e8f4931b7207e380ecc3ba630d5965 Mon Sep 17 00:00:00 2001
From: Ignacio Casso <[email protected]>
Date: Wed, 29 Apr 2026 10:28:01 +0200
Subject: [PATCH] org.el: fix `org-sort-entries' bug when heading at point not
 visible

* lisp/org.el (org-sort-entries): Consider invisible text when moving
back to heading. Otherwise if the heading at point is not visible, the
function will not sort that heading but its closest visible parent.

Link: https://list.orgmode.org/orgmode/[email protected]
---
 lisp/org.el | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index cb36497f4..8312ab62b 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -8085,9 +8085,9 @@ function is being called interactively."
             what "region")
       (goto-char start))
      ((or (org-at-heading-p)
-          (ignore-errors (progn (org-back-to-heading) t)))
+          (ignore-errors (progn (org-back-to-heading t) t)))
       ;; we will sort the children of the current headline
-      (org-back-to-heading)
+      (org-back-to-heading t)
       (setq start (point)
 	    end (progn (org-end-of-subtree t t)
 		       (or (bolp) (insert "\n"))
-- 
2.43.0

Reply via email to