2017-12-08 22:59:42 -05:00
|
|
|
;;; lang/org/+attach.el -*- lexical-binding: t; -*-
|
2017-07-05 02:33:41 +02:00
|
|
|
|
2018-01-08 20:38:46 -05:00
|
|
|
(add-hook 'org-load-hook #'+org|init-attach)
|
|
|
|
|
2017-07-05 02:33:41 +02:00
|
|
|
;; I believe Org's native attachment system is over-complicated and litters
|
2017-09-07 17:25:38 +02:00
|
|
|
;; files with metadata I don't want. So I wrote my own, which:
|
2017-07-05 02:33:41 +02:00
|
|
|
;;
|
2018-02-18 04:30:49 -05:00
|
|
|
;; + Places attachments in a centralized location (`+org-attach-dir' in
|
2018-06-15 00:45:26 +02:00
|
|
|
;; `org-directory'), using an attach:* link abbreviation.
|
|
|
|
;; + Use `+org-attach/sync' to index all attachments in `org-directory' that use
|
|
|
|
;; the attach:* abbreviation and delete orphaned ones that are no longer
|
2018-02-18 04:30:49 -05:00
|
|
|
;; referenced.
|
2017-09-07 17:25:38 +02:00
|
|
|
;; + Adds drag-and-drop support for images (with inline image preview)
|
|
|
|
;; + Adds drag-and-drop support for media files (pdfs, zips, etc) with a
|
|
|
|
;; filetype icon and short link.
|
2017-07-05 02:33:41 +02:00
|
|
|
|
2017-09-07 17:25:38 +02:00
|
|
|
;; Some commands of interest:
|
|
|
|
;; + `org-download-screenshot'
|
|
|
|
;; + `+org-attach/file'
|
|
|
|
;; + `+org-attach/url'
|
2018-02-18 04:30:49 -05:00
|
|
|
;; + `+org-attach/sync'
|
2017-09-07 17:25:38 +02:00
|
|
|
|
2017-12-24 15:13:36 -05:00
|
|
|
(defvar +org-attach-dir ".attach/"
|
2018-06-15 00:45:26 +02:00
|
|
|
"Where to store attachments relative to `org-directory'.")
|
2017-12-08 22:59:42 -05:00
|
|
|
|
|
|
|
|
2018-02-18 04:30:49 -05:00
|
|
|
;;
|
|
|
|
;; Plugins
|
|
|
|
;;
|
|
|
|
|
2017-07-05 02:33:41 +02:00
|
|
|
(def-package! org-download
|
2017-09-07 17:25:38 +02:00
|
|
|
:commands (org-download-dnd org-download-dnd-base64)
|
|
|
|
:init
|
|
|
|
;; Add these myself, so that org-download is lazy-loaded...
|
|
|
|
(setq dnd-protocol-alist
|
|
|
|
`(("^\\(https?\\|ftp\\|file\\|nfs\\):" . +org-attach-download-dnd)
|
|
|
|
("^data:" . org-download-dnd-base64)
|
|
|
|
,@dnd-protocol-alist))
|
|
|
|
|
|
|
|
(advice-add #'org-download-enable :override #'ignore)
|
2017-07-05 02:33:41 +02:00
|
|
|
:config
|
2017-12-24 15:13:36 -05:00
|
|
|
(setq-default org-download-image-dir org-attach-directory
|
2017-07-05 02:33:41 +02:00
|
|
|
org-download-heading-lvl nil
|
|
|
|
org-download-timestamp "_%Y%m%d_%H%M%S")
|
2017-09-07 17:25:38 +02:00
|
|
|
|
2017-07-05 02:33:41 +02:00
|
|
|
(setq org-download-screenshot-method
|
2017-09-07 17:25:38 +02:00
|
|
|
(cond (IS-MAC "screencapture -i %s")
|
|
|
|
(IS-LINUX
|
2017-12-08 22:59:42 -05:00
|
|
|
(cond ((executable-find "maim") "maim -s %s")
|
|
|
|
((executable-find "scrot") "scrot -s %s")))))
|
2017-09-07 17:25:38 +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.
|
|
|
|
(advice-add #'org-download-insert-link :override #'+org-attach*insert-link)
|
|
|
|
|
|
|
|
(defun +org-attach*download-subdir ()
|
2018-06-15 00:45:26 +02:00
|
|
|
(when (file-in-directory-p buffer-file-name org-directory)
|
|
|
|
(file-relative-name buffer-file-name org-directory)))
|
2017-07-05 02:33:41 +02:00
|
|
|
|
|
|
|
(defun +org-attach*download-fullname (path)
|
2018-03-22 08:13:23 -04:00
|
|
|
"Write PATH relative to current file."
|
2018-03-22 08:26:09 -04:00
|
|
|
(let ((dir (or (if buffer-file-name (file-name-directory buffer-file-name))
|
|
|
|
default-directory)))
|
2018-06-15 00:45:26 +02:00
|
|
|
(if (file-in-directory-p dir org-directory)
|
2018-03-22 08:26:09 -04:00
|
|
|
(file-relative-name path dir)
|
|
|
|
path)))
|
2017-07-05 02:33:41 +02:00
|
|
|
(advice-add #'org-download--dir-2 :override #'ignore)
|
|
|
|
(advice-add #'org-download--fullname
|
|
|
|
:filter-return #'+org-attach*download-fullname))
|
|
|
|
|
2018-02-18 04:30:49 -05:00
|
|
|
|
|
|
|
;;
|
|
|
|
;; Bootstrap
|
2017-07-05 02:33:41 +02:00
|
|
|
;;
|
2018-02-18 04:30:49 -05:00
|
|
|
|
2018-01-08 20:38:46 -05:00
|
|
|
(defun +org|init-attach ()
|
2018-06-15 00:45:26 +02:00
|
|
|
(setq org-attach-directory (expand-file-name +org-attach-dir org-directory))
|
2018-03-22 08:13:23 -04:00
|
|
|
|
2018-02-09 18:10:01 -05:00
|
|
|
;; A shorter link to attachments
|
|
|
|
(push (cons "attach" (abbreviate-file-name org-attach-directory)) org-link-abbrev-alist)
|
2018-03-22 07:31:36 -04:00
|
|
|
(org-link-set-parameters
|
|
|
|
"attach"
|
|
|
|
:follow (lambda (link) (find-file (expand-file-name link org-attach-directory)))
|
|
|
|
:complete (lambda (&optional _arg)
|
|
|
|
(+org--relpath (+org-link-read-file "attach" org-attach-directory)
|
|
|
|
org-attach-directory))
|
|
|
|
:face (lambda (link)
|
|
|
|
(if (file-exists-p (expand-file-name link org-attach-directory))
|
|
|
|
'org-link
|
|
|
|
'error)))
|
|
|
|
|
2017-12-08 22:59:42 -05:00
|
|
|
(push (car (last (split-string +org-attach-dir "/" t)))
|
|
|
|
projectile-globally-ignored-directories)
|
2017-07-05 02:33:41 +02:00
|
|
|
|
2018-02-18 04:30:49 -05:00
|
|
|
;;
|
2017-09-07 17:25:38 +02:00
|
|
|
(after! recentf
|
2017-12-24 15:13:36 -05:00
|
|
|
(push (format "%s.+$" (regexp-quote org-attach-directory))
|
2017-09-07 17:25:38 +02:00
|
|
|
recentf-exclude)))
|
2017-07-05 02:33:41 +02:00
|
|
|
|