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