branch: externals/org
commit f974e85c07e80615bd35d58d3ba564b9a8ff0df9
Author: Ihor Radchenko <[email protected]>
Commit: Ihor Radchenko <[email protected]>

    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 <[email protected]>
    Link: https://orgmode.org/list/[email protected]
---
 lisp/org-refile.el | 13 +++++++------
 lisp/org.el        |  8 +++++---
 2 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/lisp/org-refile.el b/lisp/org-refile.el
index 47827ed182..2fc8e27c0d 100644
--- a/lisp/org-refile.el
+++ b/lisp/org-refile.el
@@ -488,7 +488,7 @@ prefix argument (\\`C-u C-u C-u C-c C-w')."
           (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)
@@ -542,8 +542,8 @@ prefix argument (\\`C-u C-u C-u C-c C-w')."
                         (and (>= pos region-start)
                              (<= pos region-end))
                       (and (>= pos (save-excursion
-                                     (org-back-to-heading t)
-                                     (point)))
+                                      (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"))
@@ -558,9 +558,10 @@ prefix argument (\\`C-u C-u C-u C-c C-w')."
                (org-fold-show-context 'org-goto))
            (if regionp
                (progn
-                 (org-kill-new (buffer-substring region-start region-end))
+                  (setq tree (buffer-substring region-start region-end))
+                 (org-kill-new tree)
                  (org-save-markers-in-region region-start region-end))
-             (org-copy-subtree 1 nil t))
+             (setq tree (org-copy-subtree 1 nil t)))
             (let ((origin (point-marker)))
               ;; Handle special case when we refile to exactly same
               ;; location with tree promotion/demotion.  Point marker
@@ -587,7 +588,7 @@ prefix argument (\\`C-u C-u C-u C-c C-w')."
                     (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.
diff --git a/lisp/org.el b/lisp/org.el
index 68ecb95f28..9ea1ca0e96 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -7368,7 +7368,7 @@ of some markers in the region, even if CUT is non-nil.  
This is
 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
   (interactive "p")
   (org-preserve-local-variables
-   (let (beg end folded (beg0 (point)))
+   (let (beg end folded subtree-text (beg0 (point)))
      (if (called-interactively-p 'any)
         (org-back-to-heading nil)    ; take what looks like a subtree
        (org-back-to-heading t))              ; take what is really there
@@ -7395,11 +7395,13 @@ useful if the caller implements cut-and-paste as 
copy-then-paste-then-cut."
        (setq org-subtree-clip-folded folded)
        (when (or cut force-store-markers)
         (org-save-markers-in-region beg end))
+       (setq subtree-text (buffer-substring-no-properties beg end))
        (if cut (kill-region beg end) (copy-region-as-kill beg end))
-       (setq org-subtree-clip (current-kill 0))
+       (setq org-subtree-clip subtree-text)
        (message "%s: Subtree(s) with %d characters"
                (if cut "Cut" "Copied")
-               (length org-subtree-clip))))))
+               (length org-subtree-clip))
+       subtree-text))))
 
 (defun org-paste-subtree (&optional level tree for-yank remove)
   "Paste the clipboard as a subtree, with modification of headline level.

Reply via email to