From 39f519cdb8bbad260942d73c09a377fab2fb31b9 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Fri, 9 Feb 2018 18:10:01 -0500 Subject: [PATCH] lang/org: use link abbrev for relative image attachment paths --- modules/lang/org/+attach.el | 5 ++-- modules/lang/org/autoload/org-attach.el | 33 +++++++++---------------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/modules/lang/org/+attach.el b/modules/lang/org/+attach.el index 0d31e3b91..2a12ca509 100644 --- a/modules/lang/org/+attach.el +++ b/modules/lang/org/+attach.el @@ -42,9 +42,6 @@ (cond ((executable-find "maim") "maim -s %s") ((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. @@ -64,6 +61,8 @@ ;; (defun +org|init-attach () (setq org-attach-directory (expand-file-name +org-attach-dir +org-dir)) + ;; A shorter link to attachments + (push (cons "attach" (abbreviate-file-name org-attach-directory)) org-link-abbrev-alist) (push (car (last (split-string +org-attach-dir "/" t))) projectile-globally-ignored-directories) diff --git a/modules/lang/org/autoload/org-attach.el b/modules/lang/org/autoload/org-attach.el index 2a493bcfe..f9a8e3426 100644 --- a/modules/lang/org/autoload/org-attach.el +++ b/modules/lang/org/autoload/org-attach.el @@ -93,22 +93,25 @@ the cursor." ;;;###autoload (defun +org-attach*insert-link (_link filename) - "TODO" + "Produces and inserts a link to FILENAME into the document. + +If FILENAME is an image, produce an attach:%s path, otherwise use file:%s (with +an file icon produced by `+org-attach--icon')." (if (looking-back "^[ \t]+" (line-beginning-position)) (delete-region (match-beginning 0) (match-end 0)) (newline)) (cond ((image-type-from-file-name filename) - (when (file-in-directory-p filename org-attach-directory) - (setq filename (file-relative-name filename +org-dir))) (insert - (concat (if (= org-download-image-html-width 0) - "" + (concat (if (= org-download-image-html-width 0) "" (format "#+attr_html: :width %dpx\n" org-download-image-html-width)) - (if (= org-download-image-latex-width 0) - "" + (if (= org-download-image-latex-width 0) "" (format "#+attr_latex: :width %dcm\n" org-download-image-latex-width)) - (format org-download-link-format - (file-relative-name filename (file-name-directory buffer-file-name))))) + (cond ((file-in-directory-p filename org-attach-directory) + (format "[[attach:%s]]" (file-relative-name filename org-attach-directory))) + ((file-in-directory-p filename +org-dir) + (format org-download-link-format (file-relative-name filename +org-dir))) + (t + (format org-download-link-format filename))))) (org-display-inline-images)) (t (insert @@ -117,15 +120,3 @@ the cursor." (file-relative-name filename (file-name-directory buffer-file-name)) (file-name-nondirectory (directory-file-name filename))))))) -;;;###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 (file-in-directory-p file org-attach-directory) - +org-dir - default-directory))) - (apply orig-fn args)) - (apply orig-fn args)))