From c52a993f91567d87eb87ded907dcccd83a0f2ed3 Mon Sep 17 00:00:00 2001
From: Daniel Gomez <d.gomez@posteo.org>
Date: Thu, 1 Mar 2018 01:49:54 +0100
Subject: [PATCH 1/1] Add support for :absolute-paths in #+INCLUDE derivatives.

---
 lisp/ox.el | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/lisp/ox.el b/lisp/ox.el
index bd49a8a26..b6f6e576e 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -3302,6 +3302,11 @@ storing and resolving footnotes.  It is created automatically."
 				       value)
 			 (prog1 (org-not-nil (match-string 1 value))
 			   (setq value (replace-match "" nil nil value)))))
+		   (absolute-paths
+                    (and (string-match ":absolute-paths *\\([^: \r\t\n]\\S-*\\)?"
+                                       value)
+                         (prog1 (org-not-nil (match-string 1 value))
+                           (setq value (replace-match "" nil nil value)))))
 		   (lines
 		    (and (string-match
 			  ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
@@ -3373,7 +3378,8 @@ storing and resolving footnotes.  It is created automatically."
 			 (or
 			  (gethash file file-prefix)
 			  (puthash file (cl-incf current-prefix) file-prefix))
-			 footnotes)))
+			 footnotes
+			 absolute-paths)))
 		     (org-export-expand-include-keyword
 		      (cons (list file lines) included)
 		      (file-name-directory file)
@@ -3451,7 +3457,7 @@ Return a string of lines to be included in the format expected by
 		       counter))))))))
 
 (defun org-export--prepare-file-contents
-    (file &optional lines ind minlevel id footnotes)
+    (file &optional lines ind minlevel id footnotes absolute-paths)
   "Prepare contents of FILE for inclusion and return it as a string.
 
 When optional argument LINES is a string specifying a range of
@@ -3473,7 +3479,10 @@ This is useful to avoid conflicts when more than one Org file
 with footnotes is included in a document.
 
 Optional argument FOOTNOTES is a hash-table to store footnotes in
-the included document."
+the included document.
+
+Optional argument ABSOLUTE-PATHS, when non-nil, toggles the convertion
+of all relative paths in the included document into absolute paths."
   (with-temp-buffer
     (insert-file-contents file)
     (when lines
@@ -3533,6 +3542,23 @@ the included document."
 		(lambda ()
 		  (if (< offset 0) (delete-char (abs offset))
 		    (insert (make-string offset ?*)))))))))))
+    ;;; When ABSOLUTE-PATHS is specified, all paths from links of type
+    ;;; "file" that are present in the file being included are
+    ;;; converted to absolute and canonicalized.
+    (when absolute-paths
+      (goto-char (point-min))
+      (while (re-search-forward org-any-link-re nil t)
+        (let ((link (save-excursion
+                      (backward-char)
+                      (org-element-context))))
+          (when (string= (org-element-property :type link) "file")
+            (let* ((old-path (org-element-property :path link))
+                   (new-path (expand-file-name old-path
+                                               (file-name-directory file)))
+                   (remove (list (org-element-property :begin link)
+                                 (org-element-property :end link))))
+              (apply #'delete-region remove)
+              (insert "[[file:" new-path "]]"))))))
     ;; Append ID to all footnote references and definitions, so they
     ;; become file specific and cannot collide with footnotes in other
     ;; included files.  Further, collect relevant footnote definitions
-- 
2.16.2

