summaryrefslogtreecommitdiffhomepage
path: root/lib/org/jao-org-links.el
blob: f8080f52e7fa82267742afb73f79136e8abe59cc (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
(require 'jao-org-utils)
(require 'jao-maildir)
(require 'pdf-info)

(defvar jao-org--sink-dir "./")
(defvar jao-org-open-pdf-fun 'jao-org--pdf-tools-open)

(defun jao-org--pdf-go (page height)
  (when page (pdf-view-goto-page page))
  (when height
    (image-set-window-vscroll
     (round (/ (* height (cdr (pdf-view-image-size))) (frame-char-height))))))

(defun jao-org--pdf-tools-open (path page &optional height)
  (org-open-file path 1)
  (jao-org--pdf-go path page))

(defun jao-org--pdf-open (path page &optional height)
  (funcall (or jao-org-open-pdf-fun 'jao-org--pdf-tools-open) path page height))

(defun jao-org--pdf-p (file) (string-match-p ".*\\.pdf$" file))

(defun jao-org-links--open-pdf (link)
  "Open LINK in pdf-view-mode."
  (require 'pdf-tools)
  (cond ((string-match "\\(.*\\)::\\([0-9]*\\)\\+\\+\\([[0-9]\\.*[0-9]*\\)"  link)
         (let* ((path (match-string 1 link))
                (page (string-to-number (match-string 2 link)))
                (height (string-to-number (match-string 3 link))))
           (jao-org--pdf-open path page height)))
        ((string-match "\\(.*\\)::\\([0-9]+\\)$"  link)
         (let* ((path (match-string 1 link))
                (page (max 1 (string-to-number (match-string 2 link)))))
           (jao-org--pdf-open path page)))
        (t (jao-org--pdf-open link nil))))

(defun jao-org-links--follow-doc (link)
  (let* ((full-link (concat org-directory "/doc/" link))
         (dest-path (car (split-string full-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))))
    (if (jao-org--pdf-p dest-path)
        (jao-org-links--open-pdf full-link)
      (browse-url (format "file://%s" (expand-file-name  dest-path))))))

(defun jao-org-links--complete-doc (&optional arg)
  (let ((default-directory jao-org--sink-dir))
    (let ((f (replace-regexp-in-string "^file:" "doc:"
                                       (org-file-complete-link arg))))
      (if (jao-org--pdf-p f)
          (concat f "::" (read-from-minibuffer "Page: " "1"))
        f))))

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

(defun jao-org--pdf-title (&optional fname)
  (let ((base (file-name-base (or fname (pdf-view-buffer-file-name)))))
    (capitalize (replace-regexp-in-string "-" " " base))))

(defvar-local jao--pdf-outline nil)

(defun jao-org--pdf-section-title (&optional page)
  (when (not jao--pdf-outline)
    (setq-local jao--pdf-outline (pdf-info-outline)))
  (let ((page (or page (pdf-view-current-page)))
        (outline jao--pdf-outline)
        (cur-page 0)
        (cur-title (jao-org--pdf-title)))
    (while (and (car outline) (< cur-page page))
      (setq cur-page (cdr (assoc 'page (car outline))))
      (when (<= cur-page page)
        (setq cur-title (cdr (assoc 'title (car outline)))))
      (setq outline (cdr outline)))
    (replace-regexp-in-string "[[:blank:]]+" " " cur-title)))

(defun jao-org-links-open-mail (fname)
  (let ((group (jao-maildir-file-to-group fname))
        (id (with-temp-buffer
              (insert-file fname)
              (goto-char (point-min))
              (message-mode)
              (not-modified)
              (message-fetch-field "Message-ID"))))
    (when (and group id) (org-gnus-follow-link group id))))

;;;###autoload
(defvar jao-org-links-pdf-store-fun nil)

(defun jao-org-links--store-pdf-link ()
  (or (when (fboundp jao-org-links-pdf-store-fun)
        (funcall jao-org-links-pdf-store-fun))
      (when (derived-mode-p 'pdf-view-mode)
        (jao-org-links-store-pdf-link buffer-file-name
                                      (pdf-view-current-page)
                                      (jao-org--pdf-section-title)))))

;;;###autoload
(defun jao-org-links-store-pdf-link (path page title)
  (org-store-link-props
   :type "doc"
   :link (format "doc:%s::%d" (file-name-nondirectory path) page)
   :description (format "%s (p. %d)" title page)))

;;;###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)
  (interactive)
  (org-link-set-parameters "doc"
                           :follow #'jao-org-links--follow-doc
                           :complete #'jao-org-links--complete-doc
                           :store #'jao-org-links--store-pdf-link)
  (org-link-set-parameters "message" :follow #'jao-org-links-open-mail)
  (setq jao-org--sink-dir (file-name-as-directory sink-dir)))

;;;###autoload
(defvar jao-org-doc-notes-dir "notes/books")

;;;###autoload
(defun jao-org-org-to-pdf-file ()
  (replace-regexp-in-string (format "/%s/\\(.+\\)\\.org$" jao-org-doc-notes-dir)
                            "/doc/\\1.org"
                            buffer-file-name))

;;;###autoload
(defun jao-org-pdf-to-org-file (&optional file-name)
  (replace-regexp-in-string "/doc/\\(.+\\)\\.pdf$"
                            (format "/%s/\\1.org" jao-org-doc-notes-dir)
                            (or file-name buffer-file-name)))

;;;###autoload
(defun jao-org-insert-doc-skeleton (&optional title)
  (insert "#+title: " (or title (jao-org--pdf-title (buffer-file-name)))
          "\n#+author:\n#+startup: latexpreview\n\n"))

;;;###autoload
(defun jao-org-pdf-goto-org (arg)
  (interactive "P")
  (when (jao-org--pdf-p buffer-file-name)
    (let* ((file (jao-org-pdf-to-org-file))
           (new (not (file-exists-p file)))
           (title (jao-org--pdf-title)))
      (when (or arg new) (org-store-link nil t))
      (find-file-other-window file)
      (when new
        (jao-org-insert-doc-skeleton title)
        (org-insert-link)))))

;;;###autoload
(defun jao-org-pdf-goto-org* ()
  (interactive)
  (jao-org-pdf-goto-org t))

(provide 'jao-org-links)