branch: externals/org
commit cd4cb4552a83ab42efc58b956fdeb152781b8a81
Author: Ihor Radchenko <yanta...@posteo.net>
Commit: Ihor Radchenko <yanta...@posteo.net>

    org-metaup: Fix region extending unexpectedly
    
    * lisp/org.el (org-metaup): When `org-move-subtree-down' puts the
    heading before region end marker, adjust the region to not include it.
    
    Reported-by: Philipp Kiefer <phil.kie...@gmail.com>
    Link: 
https://orgmode.org/list/d0cca792-39ce-4896-92da-dec3f30dd...@gmail.com
---
 lisp/org.el | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 8b989a47cc..c48ec613f3 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -17263,7 +17263,8 @@ function runs `org-metaup-final-hook' using the same 
logic."
             (org-at-heading-p))))
     (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
     (let ((beg (region-beginning))
-          (end (region-end)))
+          (end (region-end))
+          (region-extended nil))
       (save-excursion
         ;; Go a little earlier because `org-move-subtree-down' will
         ;; insert before markers and we may overshoot in some cases.
@@ -17281,7 +17282,16 @@ function runs `org-metaup-final-hook' using the same 
logic."
           ;; Drag first subtree above below the selected.
           (while (< (point) end)
             (call-interactively 'org-move-subtree-down)
-            (setq deactivate-mark (org--deactivate-mark)))))))
+            (setq deactivate-mark (org--deactivate-mark)))
+          ;; When `org-move-subtree-down' inserts before markers, the
+          ;; region boundaries will extend to the moved
+          ;; heading. Prevent this.
+          (when (<= (point) (region-end))
+            (setq region-extended t))))
+      (when region-extended
+        (if (= (region-beginning) (point))
+            (set-mark (1+ end))
+          (goto-char (1+ end))))))
    ((org-region-active-p)
     (let* ((a (save-excursion
                 (goto-char (region-beginning))

Reply via email to