From 5f0806f1b49006e5977c8100fa3e336a4e0e1347 Mon Sep 17 00:00:00 2001
From: "Ryan C. Scott" <ryan@5pmcasual.com>
Date: Mon, 4 Oct 2021 19:06:26 -0700
Subject: [PATCH] ob-core.el/babel: Special handling for attachment links in
 src block

* ob-core.el (org-babel-merge-params): Specifying the symbol 'attach`
or string "'attach" as the value of the `:dir' header now functions as
":dir (org-attach-dir nil t) :mkdirp t"
(org-babel-result-to-file): Optional `TYPE' argument accepts symbol
'attachment` to fixup up paths under `(org-attach-dir)' and use the
link type "attachment:" when that is detected.
(org-babel-insert-result): Pass symbol `attachment' as `TYPE' to
`org-babel-result-to-file'
* org-attach.el (org-attach-dir): Added autoload header to simplify
dependencies necessary to support this feature (called in
`org-babel-merge-params').
---
 doc/org-manual.org |  6 ++++
 etc/ORG-NEWS       |  7 +++++
 lisp/ob-core.el    | 76 +++++++++++++++++++++++++++++++++-------------
 lisp/org-attach.el |  1 +
 4 files changed, 69 insertions(+), 21 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index d34d33561..6ab340a5a 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -17232,6 +17232,12 @@ directory with {{{kbd(M-x cd RET DIRECTORY)}}}, and then not setting
 =dir=.  Under the surface, =dir= simply sets the value of the Emacs
 variable ~default-directory~.  Setting =mkdirp= header argument to
 a non-~nil~ value creates the directory, if necessary.
+Setting =dir= to the symbol ~attach~ or the string ~"'attach"~ will
+set =dir= to the directory returned by ~(org-attach-dir)~, set =:mkdir
+yes=, and insert any file paths, as when using =:results file=, which
+are under the node's attachment directory using =attachment:= links
+instead of the usual =file:= links. Any returned path outside of the
+attachment directory will use =file:= links as per usual.
 
 For example, to save the plot file in the =Work/= folder of the home
 directory---notice tilde is expanded:
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 83a67da96..d0d54cbb5 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -354,6 +354,13 @@ argument is present.  You can also set =:async no= to force it off
 
 Async evaluation is disabled during export.
 
+*** New special value ~'attach~ for src block =:dir= option
+
+Passing the symbol ~attach~ or string ='attach= to the =:dir= option
+of a src block is now equivalent to =:dir (org-attach-dir) :mkdir yes= and any
+file results with a path descended from the attachment directory will use
+=attachment:= style links instead of the standard =file:= link type.
+
 ** Miscellaneous
 *** =org-bibtex= includes =doi= and =url= entries when exporting to BiBTeX
 =doi= and =url= entries have been made optional for some publication
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 384c06c9a..05522ca24 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -2241,11 +2241,14 @@ 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
+                 (org-babel-result-to-file
+		  result
+		  (org-babel--file-desc (nth 2 info) result)
+                  'attachment))))
 	((listp result))
 	(t (setq result (format "%S" result))))
+
   (if (and result-params (member "silent" result-params))
       (progn (message (replace-regexp-in-string "%" "%%" (format "%S" result)))
 	     result)
@@ -2548,27 +2551,47 @@ 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."
+file's directory then expand relative links.
+
+If the optional TYPE is passed as 'attachment` and the path is a
+descendant of the DEFAULT-DIRECTORY, the generated link will be
+specified as an an \"attachment:\" style link"
   (when (stringp result)
-    (let ((same-directory?
-	   (and (buffer-file-name (buffer-base-buffer))
-		(not (string= (expand-file-name default-directory)
-			    (expand-file-name
-			     (file-name-directory
-			      (buffer-file-name (buffer-base-buffer)))))))))
-      (format "[[file:%s]%s]"
-	      (if (and default-directory
-		       (buffer-file-name (buffer-base-buffer)) same-directory?)
-		  (if (eq org-link-file-path-type 'adaptive)
-		      (file-relative-name
-		       (expand-file-name result default-directory)
-		       (file-name-directory
-			(buffer-file-name (buffer-base-buffer))))
-		    (expand-file-name result default-directory))
-		result)
+    (let* ((result-file-name (expand-file-name result))
+           (base-file-name (buffer-file-name (buffer-base-buffer)))
+           (base-directory (file-name-directory base-file-name))
+           (same-directory?
+	    (and base-file-name
+	         (not (string= (expand-file-name default-directory)
+			       (expand-file-name
+			        base-directory)))))
+           (request-attachment (eq type 'attachment))
+           (attach-dir (when request-attachment
+                         (let ((default-directory base-directory))
+                           (org-attach-dir nil t))))
+           (attach-dir-len (when request-attachment (length attach-dir)))
+           (in-attach-dir (when (and request-attachment (> (length result-file-name) attach-dir-len))
+                            (string=
+                             (substring result-file-name 0 attach-dir-len)
+                             attach-dir))))
+      (format "[[%s:%s]%s]"
+              (pcase type
+                ((and 'attachment (guard in-attach-dir)) "attachment")
+                (_ "file"))
+              (if (and request-attachment in-attach-dir)
+                  (file-relative-name result-file-name)
+	        (if (and default-directory
+		         base-file-name same-directory?)
+		    (if (eq org-link-file-path-type 'adaptive)
+		        (file-relative-name
+		         result-file-name
+                         (file-name-directory
+			  base-file-name))
+		      result-file-name)
+		  result))
 	      (if description (concat "[" description "]") "")))))
 
 (defun org-babel-examplify-region (beg end &optional results-switches inline)
@@ -2698,6 +2721,17 @@ parameters when merging lists."
 				  exports-exclusive-groups
 				  exports
 				  (split-string (or value "")))))
+          ((or '(:dir . attach) '(:dir . "'attach"))
+           (unless (org-id-get)
+             (if (y-or-n-p (format "Create ID for entry \"%s\"?"
+                                   (org-get-heading t t t t)))
+                 (org-id-get-create)
+               (error "Can't attach to entry \"%s\". Entry has no ID"
+                      (org-get-heading t t t t))))
+           (setq params (append
+                         `((:dir . ,(org-attach-dir nil t))
+                           (:mkdirp . "yes"))
+                         (assq-delete-all :dir (assq-delete-all :mkdir params)))))
 	  ;; Regular keywords: any value overwrites the previous one.
 	  (_ (setq params (cons pair (assq-delete-all (car pair) params)))))))
     ;; Handle `:var' and clear out colnames and rownames for replaced
diff --git a/lisp/org-attach.el b/lisp/org-attach.el
index f18453103..b06b85360 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.33.0.windows.2

