From 902edfce2410ba97c664315a528cf73202683bfd Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Tue, 14 Nov 2017 01:50:58 +0100 Subject: [PATCH] org/org-attach: support relative paths in inline images --- modules/org/org-attach/autoload/org-attach.el | 16 ++++++++++++++++ modules/org/org-attach/config.el | 3 +++ 2 files changed, 19 insertions(+) diff --git a/modules/org/org-attach/autoload/org-attach.el b/modules/org/org-attach/autoload/org-attach.el index 783ee2d99..518af450d 100644 --- a/modules/org/org-attach/autoload/org-attach.el +++ b/modules/org/org-attach/autoload/org-attach.el @@ -97,6 +97,8 @@ the cursor." (delete-region (match-beginning 0) (match-end 0)) (newline)) (cond ((image-type-from-file-name filename) + (when (file-in-directory-p filename +org-attach-dir) + (setq filename (file-relative-name filename +org-dir))) (insert (concat (if (= org-download-image-html-width 0) "" @@ -114,3 +116,17 @@ the cursor." (file-relative-name filename buffer-file-name) (file-name-nondirectory (directory-file-name rel-path))))))) +;;;###autoload +(defun +org-attach*relative-to-attach-dir (orig-fn &rest args) + "TODO" + (if (file-in-directory-p buffer-file-name +org-dir) + (let* ((context (save-match-data (org-element-context))) + (file (org-link-unescape (org-element-property :path context))) + (default-directory + (if (string-prefix-p + (concat "./" (car (last (split-string +org-attach-dir "/" t)))) + file) + +org-dir + default-directory))) + (apply orig-fn args)) + (apply orig-fn args))) diff --git a/modules/org/org-attach/config.el b/modules/org/org-attach/config.el index 6f5dda9c5..d879419c6 100644 --- a/modules/org/org-attach/config.el +++ b/modules/org/org-attach/config.el @@ -44,6 +44,9 @@ ((executable-find "scrot") "scrot -s %s"))))) + ;; Ensure that relative inline image paths are relative to the attachment folder. + (advice-add #'org-display-inline-images :around #'+org-attach*relative-to-attach-dir) + ;; Handle non-image files a little differently. Images should be inserted ;; as-is, as image previews. Other files, like pdfs or zips, should be linked ;; to, with an icon indicating the type of file.