From 30b790f42bd0de95ca15d927e27b9125b6549043 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Thu, 11 May 2023 12:07:27 +0200 Subject: [PATCH] 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 Link: https://list.orgmode.org/orgmode/CAP8tsuSEScO+Q7=g7kL=9Dk-95reJvUmLoyqmJjd2-ZM6iycfA@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 03c351cf6..600953fd6 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 c12e59762..0c5128807 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -7050,8 +7050,20 @@ Paragraph" ** H2\n" (org-test-with-temp-text-in-file "* H1 * H2" + (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 "* H1 +* 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 +* two +* three" + (org-refile nil nil `("two" ,(buffer-file-name) nil 7)) + (buffer-string)))) ;;; Sparse trees