branch: externals/org
commit 7660d40c97cf7304c685b37fbf96a3378cb60bde
Author: Lockywolf <>
Commit: Ihor Radchenko <yanta...@posteo.net>

    lisp/org.el: Allow `org-yank-image-save-method' to be a function
    
    * lisp/org.el (yank-media): Allow `org-yank-image-save-method' to be
    a function to be called to produce the directory path.  Also make
    sure that there just a single `(insert)'.
    (org-yank-image-save-method): Allow function as a value.
    * doc/org-manual.org (Drag and Drop & ~yank-media~): Synchronise
    documentation with the changes in org.el.
    * etc/ORG-NEWS (New and changed options): Document the fact that
    `org-yank-image-save-method' can now be a function.
    
    `org-yank-image-save-method' had no way to obtain a directory to save
    clipboard contents dynamically.  It could either specify that the
    yank location should be determined by the attachment machinery, or
    it could be a global fixed directory name.
    
    Current change allows setting up `org-yank-image-save-method' to a
    function which would be called to determine where to save an image,
    and could take into account buffer's name, whether it is remote or
    local, et cetera.
---
 doc/org-manual.org |  7 ++++---
 etc/ORG-NEWS       |  7 +++++++
 lisp/org.el        | 42 ++++++++++++++++++++++++++++--------------
 3 files changed, 39 insertions(+), 17 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index 1e37fe2f36..85fe56390d 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -21822,9 +21822,10 @@ from LibreOffice Calc documents.
 
 #+vindex: org-yank-image-save-method
 When yanking images from clipboard, Org saves the image on disk and
-inserts the image link to Org buffer.  Images are either saved as
-attachments to heading (default) or to a globally defined directory.
-The save location is controlled by ~org-yank-image-save-method~.
+inserts the image link to Org buffer.  Images can be saved as
+attachments to heading (default), to a globally defined directory, or
+to a directory returned by a function call.  The save location is
+controlled by ~org-yank-image-save-method~.
 
 #+vindex: org-yank-image-file-name-function
 The yanked images are saved under automatically generated name.  You
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index b415cd2d9a..5b59086292 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -371,6 +371,13 @@ This option makes it possible to disable mapping of linked 
org files
 to markdown during export to Markdown. This is analogous to how
 ~org-html-link-org-files-as-html~ works in export to HTML.
 
+*** ~org-yank-image-save-method~ can be a function producing directory name
+
+In previous versions, ~org-yank-image-save-method~ could be either a
+symbol ~attach~ or a string -- directory name.  Now it can also be a
+function, which will be called with no arguments and its return value
+will be used as a directory to save the image to.
+
 ** New functions and changes in function arguments
 
 # This also includes changes in function behavior from Elisp perspective.
diff --git a/lisp/org.el b/lisp/org.el
index c4d02e343c..21622523b7 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -20490,11 +20490,14 @@ it has a `diary' type."
 (defcustom org-yank-image-save-method 'attach
   "Method to save images yanked from clipboard and dropped to Emacs.
 It can be the symbol `attach' to add it as an attachment, or a
-directory name to copy/cut the image to that directory."
+directory name to copy/cut the image to that directory, or a
+function that will be called without arguments and should return the
+directory name, as a string."
   :group 'org
   :package-version '(Org . "9.7")
   :type '(choice (const :tag "Add it as attachment" attach)
-                 (directory :tag "Save it in directory"))
+                 (directory :tag "Save it in directory")
+                 (function :tag "Save it in a directory returned from the 
function call"))
   :safe (lambda (x) (eq x 'attach)))
 
 (defcustom org-yank-image-file-name-function #'org-yank-image-autogen-filename
@@ -20534,26 +20537,37 @@ end."
          (iname (funcall org-yank-image-file-name-function))
          (filename (with-no-warnings ; Suppress warning in Emacs <28
                      (file-name-with-extension iname ext)))
+         (dirname (cond ((eq org-yank-image-save-method 'attach) 
temporary-file-directory)
+                        ((stringp org-yank-image-save-method) 
org-yank-image-save-method)
+                        ((functionp org-yank-image-save-method)
+                         (let ((retval (funcall org-yank-image-save-method)))
+                           (when (not (stringp retval))
+                             (user-error
+                              "`org-yank-image-save-method' did not return a 
string: %S"
+                              retval))
+                           retval))
+                        (t (user-error
+                            "Unknown value of `org-yank-image-save-method': %S"
+                            org-yank-image-save-method))))
          (absname (expand-file-name
                    filename
-                   (if (eq org-yank-image-save-method 'attach)
-                       temporary-file-directory
-                     org-yank-image-save-method))))
+                   dirname)))
     (when (and (not (eq org-yank-image-save-method 'attach))
-               (not (file-directory-p org-yank-image-save-method)))
-      (make-directory org-yank-image-save-method t))
+               (not (file-directory-p dirname)))
+      (make-directory dirname t))
     ;; 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)))
-    (if (null (eq org-yank-image-save-method 'attach))
-        (insert (org-link-make-string
-                 (concat "file:"
-                         (org-link--normalize-filename absname))))
-      (require 'org-attach)
-      (org-attach-attach absname nil 'mv)
-      (insert (org-link-make-string (concat "attachment:" filename))))))
+    (insert
+     (if (not (eq org-yank-image-save-method 'attach))
+         (org-link-make-string (concat "file:" (org-link--normalize-filename 
absname)))
+       (progn
+         (require 'org-attach)
+         (org-attach-attach absname nil 'mv)
+         (org-link-make-string (concat "attachment:" filename)))))
+    ))
 
 ;; I cannot find a spec for this but
 ;; https://indigo.re/posts/2021-12-21-clipboard-data.html and pcmanfm

Reply via email to