> Also, it would be nice to have a unit test since you've already put together 
> an example.

I wrote a test and check it failed without the fix but passes with
it. Here is the updated patch:

>From d02f2ee647fdf9a1c1c81500e3a99a5a5512a1bc 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 ++--
 testing/lisp/test-org.el | 27 +++++++++++++++++++++++++++
 2 files changed, 29 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"))
diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index c019d4f3d..ba249ef4e 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -4099,6 +4099,33 @@ SCHEDULED: <2017-05-06 Sat>
 	      "\n* B\n* A\n# Local Variables:\n# foo: t\n# End:"
 	    (org-sort-entries nil ?a)
 	    (buffer-string))))
+  ;; Sort invisible entries
+  (should
+   (equal "
+* Top
+** B
+** A
+*** 1
+*** 2
+*** 3
+** C
+"
+	  (org-test-with-temp-text
+	   "
+* Top
+** B
+** A
+*** 1
+*** 3
+*** 2
+** C
+"
+           (org-overview)
+           (goto-char 13)
+           (org-sort-entries nil ?a)
+	   (buffer-string)
+           )))
+
   ;; Sort region
   (should
    (equal "
-- 
2.43.0

Btw, what is the proper way to run a single test? I run `make test` like
it says in the documentation, but it's too slow, and I did not find it
documented anywhere.

Reply via email to