summaryrefslogtreecommitdiffhomepage
path: root/org/jao-org-links.el
blob: bd3512e40599e07715a5d89fda36e093716526e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
(require 'jao-org-utils)
(require 'jao-org-pdfview)

(jao-when-darwin
 (require 'jao-devon)

 (autoload 'jao-as-safari-doc "jao-applescript.el")

 (defun jao-org-insert-devon-link ()
  (interactive)
  (insert (jao-devon-selection)))

 (defun jao-org-insert-safari-link ()
  (interactive)
  (let ((ln (jao-as-safari-doc)))
    (when ln (jao-org-insert-link (car ln) (cdr ln)))))

 (org-add-link-type "x-devonthink-item" 'jao-devon-open 'identity)

 (define-key org-mode-map "\C-cd" 'jao-org-insert-devon-link)
 (define-key org-mode-map "\C-cs" 'jao-org-insert-safari-link))

(defvar jao-org--sink-dir "./")

(defun jao-org-follow-doc (link)
  (let ((dest-path (concat org-directory "/doc/" link)))
    (when (not (file-exists-p dest-path))
      (let* ((sink-file (expand-file-name link jao-org--sink-dir))
             (real-file (if (file-exists-p sink-file) sink-file
                          (read-file-name "Import file: "
                                          jao-org--sink-dir link link))))
        (shell-command (format "mv %s %s" real-file dest-path))))
    (browse-url (format "file://%s" (expand-file-name  dest-path)))))

(defun jao-org-complete-doc (&optional arg)
  (let ((default-directory jao-org--sink-dir))
    (replace-regexp-in-string "^file:" "doc:" (org-file-complete-link arg))))

(defsubst jao-org--title->file (title)
  (concat (mapconcat 'downcase (split-string title nil t) "-") ".pdf"))

;;;###autoload
(defun jao-org-insert-doc (title)
  (interactive "sDocument title: ")
  (insert (format "[[doc:%s][%s]]" (jao-org--title->file title) title)))

;;;###autoload
(defun jao-org-links-setup (sink-dir)
  (org-link-set-parameters "doc"
                           :follow #'jao-org-follow-doc
                           :complete #'jao-org-complete-doc)
  (setq jao-org--sink-dir (file-name-as-directory sink-dir)))

(provide 'jao-org-links)