From cc5427b7ef67bea835f61568479e69d44003ba2f Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Wed, 7 Dec 2022 14:13:49 +0300 Subject: [PATCH] org-babel-result-to-file: Fix results file inside attachment dir MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lisp/ob-core.el (org-babel-result-to-file): When file path in the result contains attachment dir, reduce the path to relative file name when generating attachment: link. * testing/lisp/test-ob.el (test-ob-core/dir-attach): Add test. Reported-by: Johan Tolö Link: https://orgmode.org/list/875yesnia6.fsf@toloe.se --- lisp/ob-core.el | 8 +++++--- testing/lisp/test-ob.el | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 5b78ee946..069fc442f 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -2692,8 +2692,8 @@ specified as an an \"attachment:\" style link." (same-directory? (and base-file-name (not (string= (expand-file-name default-directory) - (expand-file-name - base-directory))))) + (expand-file-name + base-directory))))) (request-attachment (eq type 'attachment)) (attach-dir (let* ((default-directory base-directory) (dir (org-attach-dir nil t))) @@ -2709,7 +2709,9 @@ specified as an an \"attachment:\" style link." ((and 'attachment (guard in-attach-dir)) "attachment") (_ "file")) (if (and request-attachment in-attach-dir) - (file-relative-name result-file-name) + (file-relative-name + result-file-name + (file-name-as-directory attach-dir)) (if (and default-directory base-file-name same-directory?) (if (eq org-link-file-path-type 'adaptive) diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el index e4090d6d8..c8dbd44f4 100644 --- a/testing/lisp/test-ob.el +++ b/testing/lisp/test-ob.el @@ -1954,6 +1954,23 @@ nil (file-exists-p (format "%s/test.txt" (org-attach-dir nil t))) (string= (buffer-substring-no-properties (point) (line-end-position)) "[[attachment:test.txt]]")))) + ;; Strip attach dir from the file path. + (should + (org-test-with-temp-text-in-file + "* heading +:PROPERTIES: +:DIR: custom-attach-dir +:END: + +#+begin_src elisp :results value file +\"custom-attach-dir/test.txt\" +#+end_src" + (message "DIR: %s" (org-attach-dir t)) + (org-babel-execute-src-block) + (goto-char (org-babel-where-is-src-block-result)) + (forward-line) + (string= (buffer-substring-no-properties (point) (line-end-position)) + "[[attachment:test.txt]]"))) (should-error (org-test-with-temp-text-in-file "* 'attach with no ID or DIR