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.epper@gmail.com>
Link: https://list.orgmode.org/orgmode/CAP8tsuSEScO+Q7=g7kL=9Dk-95reJvUmLoyqmJjd2-ZM6iycfA@mail.gmail.com/
This commit is contained in:
Ihor Radchenko 2023-05-11 12:07:27 +02:00
parent 6e6354c074
commit 30b790f42b
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 16 additions and 2 deletions

View File

@ -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"))

View File

@ -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