[செவ்வாய் டிசம்பர் 23, 2025] Ihor Radchenko wrote:

> Thomas Ramsauer <[email protected]> writes:
>
>> here is a step-by-step recipe:
>>
>> start emacs: emacs -Q -l the-following-el
>> ...
>
> Visuwesh, did you have a chance to take a look at this recipe?

Sorry, it fell through the cracks once again.  :(

Thomas, does the attached patch address your issue?  If the file is
already found, Org asks if you want to overwrite it. If you answer no,
then you get asked if you want a link inserted to the file again.

It would be nice to avoid asking these many questions since it defeats
the purpose of using yank-media in the first place, but I will let
others decide if something like a user option might be good to have.

>From 27c2e64599fa279132ddf5d0694b34bebe07e752 Mon Sep 17 00:00:00 2001
From: Visuwesh <[email protected]>
Date: Wed, 24 Dec 2025 15:18:34 +0530
Subject: [PATCH] Ask to overwrite image if already found when yanking using
 yank-media

* lisp/org.el (org--image-yank-media-handler): If the pasted image
already, ask if it should be overwritten.  If answered no, then ask if
a link should be inserted once more.
* etc/ORG-NEWS: Announce the change.

Reported-By: Thomas Ramsauer <[email protected]>
Link: https://debbugs.gnu.org/78037
---
 etc/ORG-NEWS |  7 +++++++
 lisp/org.el  | 25 +++++++++++++++----------
 2 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index f4ff44d29..5e5749424 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -628,6 +628,13 @@ has been made obsolete. Its value is still used in place of default
 parameter -jar if the variable is defined.
 
 ** Miscellaneous
+*** Images may be overwritten when yanked using ~yank-media~
+
+When yanking images to the Org buffer using ~yank-media~, and if the
+image file already exists, then Org now asks if the file should be
+overwritten.  If the user chooses to not overwrite the existing image,
+then a link to it can be inserted once again by answering the prompt.
+
 *** ~org-open-at-point-global~ now accepts prefix argument
 
 The argument is passed through to ~org-link-open~, allowing alternative
diff --git a/lisp/org.el b/lisp/org.el
index 67d9679fe..f347167d2 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -20695,21 +20695,26 @@ (defun org--image-yank-media-handler (mimetype data)
                         (t (user-error
                             "Unknown value of `org-yank-image-save-method': %S"
                             org-yank-image-save-method))))
-         (absname (expand-file-name
-                   filename
-                   dirname)))
+         (absname (expand-file-name filename dirname))
+         (insert-link-p t))
     (when (and (not (eq org-yank-image-save-method 'attach))
                (not (file-directory-p dirname)))
       (make-directory dirname t))
+    (when (file-exists-p absname)
+      (let ((abbrname (abbreviate-file-name absname)))
+        (if (y-or-n-p (format "File `%s' already exists; overwrite? " abbrname))
+            (delete-file absname)
+          (setq insert-link-p (y-or-n-p (format "Insert link for `%s' again? " abbrname))))))
     ;; 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))
-      (with-temp-file absname
-        (insert data)))
-    (insert
-     (if (not (eq org-yank-image-save-method 'attach))
-         (org-link-make-string-for-buffer (concat "file:" absname))
-       (progn
+    (unless (file-exists-p image)
+      (let ((coding-system-for-write 'emacs-internal))
+       (with-temp-file absname
+         (insert data))))
+    (when insert-link-p
+      (insert
+       (if (not (eq org-yank-image-save-method 'attach))
+           (org-link-make-string-for-buffer (concat "file:" absname))
          (require 'org-attach)
          (apply #'org-link-make-string-for-buffer
                 (org-attach-attach absname nil 'mv)))))))
-- 
2.51.0

Reply via email to