jman <emacs-orgm...@city17.xyz> writes:

> I don't have the context to figure out things further, all I could do was 
> disabling xclip-mode 
> around executing `org-refile`:
>
> --8<---------------cut here---------------start------------->8---
> ;; disable xclip-mode before executing org-refile
> (advice-add 'org-refile :before (lambda (&rest _)
>                                  (when xclip-mode
>                                    (xclip-mode -1))))
>
> ;; enable xclip-mode again after executing org-refile
> (advice-add 'org-refile :after (lambda (&rest _)
>                                  (when (not xclip-mode)
>                                    (xclip-mode t))))
> --8<---------------cut here---------------end--------------->8---
>
> Ihor, any thoughts on this?

Can you try the attached patch?
>From a75e575a0ae8172d0d50c738d67b36f518ad5f98 Mon Sep 17 00:00:00 2001
Message-ID: <a75e575a0ae8172d0d50c738d67b36f518ad5f98.1753018325.git.yanta...@posteo.net>
From: Ihor Radchenko <yanta...@posteo.net>
Date: Sun, 20 Jul 2025 15:30:33 +0200
Subject: [PATCH] org-refile: Do not assume that kill ring cannot change under
 our feet

* lisp/org-refile.el (org-refile): Store region to be refiled in
variable rather than relying upon it being held as the first entry in
the kill ring.

Reported-by: jman <emacs-orgm...@city17.xyz>
Link: https://orgmode.org/list/87msaxsn6c....@city17.xyz
---
 lisp/org-refile.el | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/lisp/org-refile.el b/lisp/org-refile.el
index 5caed50a6..4bca6ee85 100644
--- a/lisp/org-refile.el
+++ b/lisp/org-refile.el
@@ -484,7 +484,7 @@ (defun org-refile (&optional arg default-buffer rfloc msg)
 	   (region-start (and regionp (region-beginning)))
 	   (region-end (and regionp (region-end)))
 	   (org-refile-keep (if (equal arg 3) t org-refile-keep))
-	   pos it nbuf file level reversed)
+	   pos it nbuf file level reversed tree)
       (setq last-command nil)
       (when regionp
 	(goto-char region-start)
@@ -557,6 +557,9 @@ (defun org-refile (&optional arg default-buffer rfloc msg)
 		  (org-kill-new (buffer-substring region-start region-end))
 		  (org-save-markers-in-region region-start region-end))
 	      (org-copy-subtree 1 nil t))
+            ;; We cannot rely on the kill remaining intact before we
+            ;; paste.  See https://orgmode.org/list/87msaxsn6c....@city17.xyz
+            (setq tree (current-kill 0))
             (let ((origin (point-marker)))
               ;; Handle special case when we refile to exactly same
               ;; location with tree promotion/demotion.  Point marker
@@ -583,7 +586,7 @@ (defun org-refile (&optional arg default-buffer rfloc msg)
 		     (goto-char (point-min))
 		     (or (outline-next-heading) (goto-char (point-max)))))
 	         (unless (bolp) (newline))
-	         (org-paste-subtree level nil nil t)
+	         (org-paste-subtree level tree nil t)
 	         ;; Record information, according to `org-log-refile'.
 	         ;; Do not prompt for a note when refiling multiple
 	         ;; headlines, however.  Simply add a time stamp.
-- 
2.50.1

-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

Reply via email to