2019-06-28 16:53:26 +02:00
|
|
|
;;; lang/org/contrib/dragndrop.el -*- lexical-binding: t; -*-
|
2019-07-09 22:44:51 +02:00
|
|
|
;;;###if (featurep! +dragndrop)
|
2019-06-28 16:53:26 +02:00
|
|
|
|
2019-07-23 12:44:03 +02:00
|
|
|
(use-package! org-download
|
2020-01-27 17:04:29 -05:00
|
|
|
:commands
|
|
|
|
org-download-dnd
|
|
|
|
org-download-yank
|
|
|
|
org-download-screenshot
|
|
|
|
org-download-dnd-base64
|
2019-06-28 16:53:26 +02:00
|
|
|
:init
|
2019-07-21 23:31:42 +02:00
|
|
|
;; HACK We add these manually so that org-download is truly lazy-loaded
|
2019-10-20 18:45:15 -04:00
|
|
|
(pushnew! dnd-protocol-alist
|
|
|
|
'("^\\(?:https?\\|ftp\\|file\\|nfs\\):" . +org-dragndrop-download-dnd-fn)
|
|
|
|
'("^data:" . org-download-dnd-base64))
|
2019-06-28 16:53:26 +02:00
|
|
|
(advice-add #'org-download-enable :override #'ignore)
|
2020-02-19 18:21:24 -05:00
|
|
|
|
|
|
|
(after! org
|
|
|
|
;; A shorter link to attachments
|
2020-04-25 01:23:55 -04:00
|
|
|
(+org-define-basic-link "download" (lambda () (or org-download-image-dir org-attach-id-dir "."))
|
2020-04-24 20:41:56 -04:00
|
|
|
:image-data-fun #'+org-image-file-data-fn
|
|
|
|
:requires 'org-download))
|
2019-06-28 16:53:26 +02:00
|
|
|
:config
|
2020-04-24 20:41:56 -04:00
|
|
|
(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"
|
2019-12-29 16:31:25 -05:00
|
|
|
org-download-method 'attach
|
2019-06-28 16:53:26 +02:00
|
|
|
org-download-heading-lvl nil
|
|
|
|
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")
|
2020-03-28 10:13:44 +00:00
|
|
|
((executable-find "scrot") "scrot -s %s")
|
|
|
|
((executable-find "gnome-screenshot") "gnome-screenshot -a -f %s")))))
|
2019-06-28 16:53:26 +02:00
|
|
|
|
|
|
|
;; 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.
|
2019-07-23 17:24:56 +02:00
|
|
|
(defadvice! +org--dragndrop-insert-link-a (_link filename)
|
2019-07-21 02:38:42 +02:00
|
|
|
"Produces and inserts a link to FILENAME into the document.
|
2019-06-28 16:53:26 +02:00
|
|
|
|
2020-04-23 23:49:35 -04:00
|
|
|
If FILENAME is an image, produce an download:%s path, otherwise use file:%s (with
|
2019-10-25 20:00:06 -04:00
|
|
|
an file icon produced by `+org-attach-icon-for')."
|
2019-07-21 02:38:42 +02:00
|
|
|
: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
|
2020-01-26 02:20:39 -05:00
|
|
|
(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))
|
2020-03-23 19:19:55 +01:00
|
|
|
(if (= org-download-image-org-width 0) ""
|
|
|
|
(format "#+attr_org: :width %dpx\n" org-download-image-org-width))
|
2020-01-26 02:20:39 -05:00
|
|
|
(format org-download-link-format
|
2020-04-24 21:22:12 -04:00
|
|
|
(if (file-in-directory-p filename org-download-image-dir)
|
|
|
|
(file-relative-name filename org-download-image-dir)
|
2020-04-23 23:49:35 -04:00
|
|
|
filename))))
|
2019-07-21 02:38:42 +02:00
|
|
|
(org-display-inline-images))
|
|
|
|
((insert
|
|
|
|
(format "%s [[./%s][%s]] "
|
2019-10-25 20:00:06 -04:00
|
|
|
(+org-attach-icon-for filename)
|
2019-07-21 02:38:42 +02:00
|
|
|
(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)
|
2019-07-23 17:24:56 +02:00
|
|
|
(defadvice! +org--dragndrop-download-fullname-a (path)
|
2019-06-28 16:53:26 +02:00
|
|
|
"Write PATH relative to current file."
|
2019-07-21 02:38:42 +02:00
|
|
|
:filter-return #'org-download--fullname
|
2019-06-28 16:53:26 +02:00
|
|
|
(let ((dir (or (if buffer-file-name (file-name-directory buffer-file-name))
|
|
|
|
default-directory)))
|
2020-04-23 23:49:35 -04:00
|
|
|
(if (file-in-directory-p dir org-attach-id-dir)
|
2019-06-28 16:53:26 +02:00
|
|
|
(file-relative-name path dir)
|
2019-07-21 02:38:42 +02:00
|
|
|
path))))
|