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

    Query before attempting to overwrite existing attachment/DnD/yank file
    
    * lisp/org-attach.el (org-attach-attach):
    * lisp/org.el (org--image-yank-media-handler):
    (org--dnd-attach-file): Query before trying to overwrite existing
    file.
    * etc/ORG-NEWS (Attaching files, drag-and-drop, and ~yank-media~ will
    query about existing files): Announce the change.
    
    Reported-by: Thomas Ramsauer <[email protected]>
    Link: https://list.orgmode.org/orgmode/[email protected]/
---
 etc/ORG-NEWS       |  8 ++++++++
 lisp/org-attach.el |  6 ++++++
 lisp/org.el        | 12 ++++++++++++
 3 files changed, 26 insertions(+)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 1a0dbab16e..a638a9ed6d 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -120,6 +120,14 @@ cookie =[N%]=.
 
 ** Miscellaneous
 
+*** Attaching files, drag-and-drop, and ~yank-media~ will query about existing 
files
+
+Previously, ~org-attach~, drag-and-drop for files, and ~yank-media~
+either erred (when using ~cp~ attach/save method) or silently
+overwrote existing attachments/stored files (~mv~ method).  Now,
+an interactive yes/no query is displayed before attempting to store
+the files.
+
 *** =ox-latex=: ~#+BEGIN_EXAMPLE~ now checks for ~:options~ in ~#+ATTR_LATEX:~
 
 You can now pass options to the environment you choose for the
diff --git a/lisp/org-attach.el b/lisp/org-attach.el
index 67b430e71a..ccf7ac200c 100644
--- a/lisp/org-attach.el
+++ b/lisp/org-attach.el
@@ -609,6 +609,12 @@ and DESCRIPTION be the file name."
         link description)
     (let* ((attach-dir (org-attach-dir 'get-create))
            (attach-file (expand-file-name basename attach-dir)))
+      (when (file-exists-p attach-file)
+        (if (y-or-n-p
+             (format "Attachment %s already exists.  Overwrite?"
+                     attach-file))
+            (delete-file attach-file)
+          (error "Attachment already exists: %s" attach-file)))
       (cond
        ((eq method 'mv) (rename-file file attach-file))
        ((eq method 'cp)
diff --git a/lisp/org.el b/lisp/org.el
index 222af988aa..265fc0d527 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -20824,6 +20824,12 @@ end."
     (when (and (not (eq org-yank-image-save-method 'attach))
                (not (file-directory-p dirname)))
       (make-directory dirname t))
+    (when (file-exists-p absname)
+      (if (y-or-n-p
+           (format "Yank target %s already exists.  Overwrite?"
+                   absname))
+          (delete-file absname)
+        (error "Yank target already exists: %s" absname)))
     ;; DATA is a raw image.  Tell Emacs to write it raw, without
     ;; trying to auto-detect the coding system.
     (let ((coding-system-for-write 'emacs-internal))
@@ -21006,6 +21012,12 @@ SEPARATOR is the string to insert after each link."
                  (expand-file-name
                   (file-name-nondirectory filename)
                   org-yank-image-save-method)))
+            (when (file-exists-p stored-filename)
+              (if (y-or-n-p
+                   (format "DnD target %s already exists.  Overwrite?"
+                           stored-filename))
+                  (delete-file stored-filename)
+                (error "DnD target already exists: %s" stored-filename)))
             (funcall
              (pcase method
                ('cp #'copy-file)

Reply via email to