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

    org-refile: Fix heading being removed when refiling into itself
    
    * lisp/org-refile.el (org-refile): Throw an error when attempting to
    refile heading into itself and the point is not at bol.
    * testing/lisp/test-org.el (test-org/refile): Add tests.
    
    Reported-by: Oliver Epper <oliver.ep...@gmail.com>
    Link: 
https://list.orgmode.org/orgmode/CAP8tsuSEScO+Q7=g7kL=9dk-95rejvumloyqmjjd2-zm6iy...@mail.gmail.com/
---
 lisp/org-refile.el       |  4 +++-
 testing/lisp/test-org.el | 14 +++++++++++++-
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/lisp/org-refile.el b/lisp/org-refile.el
index 03c351cf6b..600953fd6b 100644
--- a/lisp/org-refile.el
+++ b/lisp/org-refile.el
@@ -523,7 +523,9 @@ prefix argument (`C-u C-u C-u C-c C-w')."
                     (if regionp
                         (and (>= pos region-start)
                              (<= pos region-end))
-                      (and (>= pos (point))
+                      (and (>= pos (save-excursion
+                                     (org-back-to-heading t)
+                                     (point)))
                            (< pos (save-excursion
                                     (org-end-of-subtree t t))))))
            (error "Cannot refile to position inside the tree or region"))
diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index c12e597626..0c5128807e 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -7050,8 +7050,20 @@ Paragraph<point>"
 ** H2\n"
     (org-test-with-temp-text-in-file "* H1
 * H2<point>"
+      (org-refile nil nil `("H1" ,(buffer-file-name) nil 1))
+      (buffer-string))))
+  ;; Throw an error when trying to refile into itself.
+  (should-error
+   (org-test-with-temp-text-in-file "* H<point>1
+* H2"
      (org-refile nil nil `("H1" ,(buffer-file-name) nil 1))
-     (buffer-string)))))
+     (buffer-string)))
+  (should-error
+   (org-test-with-temp-text-in-file "* one
+* t<point>wo
+* three"
+     (org-refile nil nil `("two" ,(buffer-file-name) nil 7))
+     (buffer-string))))
 
 
 ;;; Sparse trees

Reply via email to