From cbe97587df4b660f56e420015222e4f3559588a2 Mon Sep 17 00:00:00 2001
From: "Ryan C. Scott" <ryan@5pmcasual.com>
Date: Wed, 25 Aug 2021 02:15:09 -0700
Subject: [PATCH] ob-core: Added option for attaching file from babel
src block result

* ob-core.el (org-babel-format-result): Added `:results-param' option
`attach' which when used with `file' will cause the resultant file path
to be moved to the `org-attach-dir' for the node.
(org-babel-result-to-file): Added optional 3rd parameter `type' which
will be used in place of "file" in link type, if present.
(org-babel-result-to-file-attachment): New function that moves the file
at the provided `result' path to the nodes attachment directory,
creating it as necessary using `org-attach-dir'.  A new function was
created to adhere to the design of the API and not complicated
`org-babel-result-to-file' with the movement to the attachment
directory.

* org-attach.el (org-attach-dir): Added autoload header to simplify
dependencies necessary to support this feature (called in
`org-babel-result-to-file-attachment').
---
 lisp/ob-core.el    | 31 ++++++++++++++++++++++++++-----
 lisp/org-attach.el |  1 +
 2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 857e03e55..c1d1b77e0 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -2188,6 +2188,10 @@ silent -- no results are inserted into the Org buffer but
 file ---- the results are interpreted as a file path, and are
           inserted into the buffer using the Org file syntax.
 
+attach -- used with `file', will move the file at the path returned
+          by the source block to the nodes attachment directory (as
+          reported by `org-attach-dir'.
+
 list ---- the results are interpreted as an Org list.
 
 raw ----- results are added directly to the Org file.  This is
@@ -2241,9 +2245,13 @@ INFO may provide the values of these header arguments (in the
   (cond ((stringp result)
 	 (setq result (org-no-properties result))
 	 (when (member "file" result-params)
-	   (setq result (org-babel-result-to-file
-			 result
-			 (org-babel--file-desc (nth 2 info) result)))))
+	   (setq result (if (member "attach" result-params)
+                            (org-babel-result-to-file-attachment
+                             result
+                             (org-babel--file-desc (nth 2 info) result))
+                          (org-babel-result-to-file
+			   result
+			   (org-babel--file-desc (nth 2 info) result))))))
 	((listp result))
 	(t (setq result (format "%S" result))))
   (if (and result-params (member "silent" result-params))
@@ -2548,7 +2556,7 @@ in the buffer."
 		 (line-beginning-position 2))
 	     (point))))))
 
-(defun org-babel-result-to-file (result &optional description)
+(defun org-babel-result-to-file (result &optional description type)
   "Convert RESULT into an Org link with optional DESCRIPTION.
 If the `default-directory' is different from the containing
 file's directory then expand relative links."
@@ -2559,7 +2567,8 @@ file's directory then expand relative links."
 			    (expand-file-name
 			     (file-name-directory
 			      (buffer-file-name (buffer-base-buffer)))))))))
-      (format "[[file:%s]%s]"
+      (format "[[%s:%s]%s]"
+              (or type "file")
 	      (if (and default-directory
 		       (buffer-file-name (buffer-base-buffer)) same-directory?)
 		  (if (eq org-link-file-path-type 'adaptive)
@@ -2571,6 +2580,18 @@ file's directory then expand relative links."
 		result)
 	      (if description (concat "[" description "]") "")))))
 
+(defun org-babel-result-to-file-attachment (result &optional description)
+  "Convert RESULT into an Org link after moving the provided path to the current node's attachment directory as reported by `org-attach-dir', using the optional DESCRIPTION if provided.
+See `org-babel-result-to-file' for further link generation details."
+
+  (let* ((dir (file-relative-name
+               (org-attach-dir t)))
+         (filename (file-name-nondirectory result))
+         (newname (format "%s/%s" dir filename)))
+
+    (rename-file result newname t)
+    (org-babel-result-to-file filename description "attachment")))
+
 (defun org-babel-examplify-region (beg end &optional results-switches inline)
   "Comment out region using the inline `==' or `: ' org example quote."
   (interactive "*r")
diff --git a/lisp/org-attach.el b/lisp/org-attach.el
index e8e8ade83..9f180255a 100644
--- a/lisp/org-attach.el
+++ b/lisp/org-attach.el
@@ -323,6 +323,7 @@ Shows a list of commands and prompts for another key to execute a command."
 	    (call-interactively command)
 	  (error "No such attachment command: %c" c))))))
 
+;;;###autoload
 (defun org-attach-dir (&optional create-if-not-exists-p no-fs-check)
   "Return the directory associated with the current outline node.
 First check for DIR property, then ID property.
-- 
2.32.0.windows.2

