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

@ -42,7 +42,7 @@ PATH (a string) can be an url, a local file path, or a base64 encoded datauri."
(unless (eq major-mode 'org-mode)
(user-error "Not in an org buffer"))
(require 'org-download)
(condition-case ex
(condition-case-unless-debug e
(let ((raw-uri (url-unhex-string path)))
(cond ((string-match-p "^data:image/png;base64," path)
(org-download-dnd-base64 path nil))
@ -56,4 +56,4 @@ PATH (a string) can be an url, a local file path, or a base64 encoded datauri."
;; insert the link
(org-download-insert-link raw-uri new-path)))))
(error
(user-error "Failed to attach file: %s" (error-message-string ex)))))
(user-error "Failed to attach file: %s" (error-message-string e)))))

View file

@ -367,11 +367,10 @@ underlying, modified buffer. This fixes that."
(defun +org-init-attachments-h ()
"Sets up org's attachment system."
;; Centralized attachments directory
(setq org-attach-id-dir (doom-path org-directory org-attach-id-dir)
;; Store a link to attachments when they are attached
org-attach-store-link-p t
;; Inherit attachment properties from parent nodes
org-attach-use-inheritance t)
(unless org-attach-id-dir
(setq org-attach-id-dir (expand-file-name ".attach/" org-directory)))
(setq org-attach-store-link-p t ; store link when attaching files
org-attach-use-inheritance t) ; inherit attachment properties from parent nodes
(after! projectile
(add-to-list 'projectile-globally-ignored-directories org-attach-id-dir)))
@ -932,9 +931,8 @@ compelling reason, so..."
org-capture
:preface
;; Change org defaults (should be set before org loads)
(setq org-directory "~/org/"
org-attach-id-dir ".attach/"
org-publish-timestamp-directory (concat doom-cache-dir "org-timestamps/")
(defvar org-attach-id-dir nil) ; set later
(setq org-publish-timestamp-directory (concat doom-cache-dir "org-timestamps/")
org-preview-latex-image-directory (concat doom-cache-dir "org-latex/"))
;; Make most of the default modules opt-in, because I sincerely doubt most

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)))))))