org/org-attach: support relative paths in inline images

This commit is contained in:
Henrik Lissner 2017-11-14 01:50:58 +01:00
parent c2bb5a0951
commit 902edfce24
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 19 additions and 0 deletions

View file

@ -97,6 +97,8 @@ the cursor."
(delete-region (match-beginning 0) (match-end 0)) (delete-region (match-beginning 0) (match-end 0))
(newline)) (newline))
(cond ((image-type-from-file-name filename) (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 (insert
(concat (if (= org-download-image-html-width 0) (concat (if (= org-download-image-html-width 0)
"" ""
@ -114,3 +116,17 @@ the cursor."
(file-relative-name filename buffer-file-name) (file-relative-name filename buffer-file-name)
(file-name-nondirectory (directory-file-name rel-path))))))) (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)))

View file

@ -44,6 +44,9 @@
((executable-find "scrot") ((executable-find "scrot")
"scrot -s %s"))))) "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 ;; 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 ;; as-is, as image previews. Other files, like pdfs or zips, should be linked
;; to, with an icon indicating the type of file. ;; to, with an icon indicating the type of file.