Rewrite org-download/attach systems

+ Cut down on unnecessary code & advice
+ Fix 'attach and 'download methods for org-download so that a) their
  links actually work and b) inline previews of their links show up.
+ Allow users to only set org-attach-id-dir (which will be mirrored to
  org-download-image-dir, unless the user has changed it).
+ Prevent a few edge cases where org-attach-id-dir or
  org-download-image-dir were blank.
This commit is contained in:
Henrik Lissner 2020-04-25 01:27:25 -04:00
parent adbe9041ce
commit 8c1525e3d8
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
3 changed files with 44 additions and 56 deletions

View file

@ -21,57 +21,47 @@
:requires 'org-download))
:config
(unless org-download-image-dir
(setq org-download-image-dir (expand-file-name (or org-attach-id-dir "")
org-directory)))
(setq org-download-link-format "[[download:%s]]\n"
org-download-method 'attach
org-download-heading-lvl nil
(setq org-download-image-dir org-attach-id-dir))
(setq org-download-method 'attach
org-download-timestamp "_%Y%m%d_%H%M%S"
org-download-screenshot-method
(cond (IS-MAC "screencapture -i %s")
(IS-LINUX
(cond ((executable-find "maim") "maim -s %s")
((executable-find "scrot") "scrot -s %s")
((executable-find "gnome-screenshot") "gnome-screenshot -a -f %s")))))
((executable-find "gnome-screenshot") "gnome-screenshot -a -f %s"))))
;; 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.
(defadvice! +org--dragndrop-insert-link-a (_link filename)
"Produces and inserts a link to FILENAME into the document.
org-download-heading-lvl nil
org-download-link-format "[[download:%s]]\n"
org-download-annotate-function (lambda (_link) "")
org-download-link-format-function
(lambda (filename)
(if (eq org-download-method 'attach)
(format "[[attachment:%s]]\n"
(org-link-escape
(file-relative-name filename (org-attach-dir))))
;; Handle non-image files a little differently. Images should be
;; inserted as normal with previews. Other files, like pdfs or zips,
;; should be linked to, with an icon indicating the type of file.
(format (concat (unless (image-type-from-file-name filename)
(concat (+org-attach-icon-for filename)
" "))
org-download-link-format)
(org-link-escape
(funcall org-download-abbreviate-filename-function filename)))))
org-download-abbreviate-filename-function
(lambda (path)
(if (file-in-directory-p path org-download-image-dir)
(file-relative-name path org-download-image-dir)
path)))
If FILENAME is an image, produce an download:%s path, otherwise use file:%s (with
an file icon produced by `+org-attach-icon-for')."
:override #'org-download-insert-link
(if (looking-back "^[ \t]+" (line-beginning-position))
(delete-region (match-beginning 0) (match-end 0))
(newline))
(cond ((image-type-from-file-name filename)
(insert
(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) ""
(format "#+attr_latex: :width %dcm\n" org-download-image-latex-width))
(if (= org-download-image-org-width 0) ""
(format "#+attr_org: :width %dpx\n" org-download-image-org-width))
(format org-download-link-format
(if (file-in-directory-p filename org-download-image-dir)
(file-relative-name filename org-download-image-dir)
filename))))
(org-display-inline-images))
((insert
(format "%s [[./%s][%s]] "
(+org-attach-icon-for filename)
(file-relative-name filename (file-name-directory buffer-file-name))
(file-name-nondirectory (directory-file-name filename)))))))
(advice-add #'org-download--dir-2 :override #'ignore)
(defadvice! +org--dragndrop-download-fullname-a (path)
"Write PATH relative to current file."
:filter-return #'org-download--fullname
(let ((dir (or (if buffer-file-name (file-name-directory buffer-file-name))
default-directory)))
(if (file-in-directory-p dir org-attach-id-dir)
(file-relative-name path dir)
path))))
(defadvice! +org--dragndrop-then-display-inline-images-a (_link filename)
:after #'org-download-insert-link
(when (image-type-from-file-name filename)
(save-excursion
(org-display-inline-images
t t
(progn (org-back-to-heading t) (point))
(progn (org-end-of-subtree t t)
(when (and (org-at-heading-p) (not (eobp))) (backward-char 1))
(point)))))))