summaryrefslogtreecommitdiffhomepage
path: root/lib/doc/jao-org-links.el
blob: d6bb8dc727a9ebd977194bab2153a8de0b1fe9b3 (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
;; -*- lexical-binding: t; -*-

(require 'jao-org-notes)
(require 'jao-doc-view)
(require 'jao-pdf)

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

(defun jao-org--pdf-open (path page &optional height)
  (org-open-file path 1)
  (jao-doc-view-goto-page page height))

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

(defun jao-org-links--open-pdf (link)
  "Open LINK in pdf-view-mode."
  (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))))
        (rename-file real-file dest-path)))
    (if (jao-pdf-is-pdf-file 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-pdf-is-pdf-file f)
          (let ((page (read-from-minibuffer "Page: " "")))
            (if (> (string-to-number page) 0)
                (concat f "::" (read-from-minibuffer "Page: " ""))
              f))
        f))))

;;;###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 'doc-view-mode)
        (jao-org-links-store-pdf-link buffer-file-name
                                      (jao-doc-view-current-page)
                                      (jao-pdf-section-title)))))

;;;###autoload
(defun jao-org-links-store-pdf-link (path page title)
  (org-link-store-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-pdf-title-to-file-name title) title)))

;;;###autoload
(defun jao-org-org-to-pdf-file ()
  (expand-file-name (concat "doc/" (file-name-base buffer-file-name) ".pdf")
                    (file-name-directory jao-org-notes-dir)))

;;;###autoload
(defun jao-org-pdf-to-org-file (&optional file-name)
  (let* ((file-name (or file-name buffer-file-name))
         (bn (file-name-base file-name))
         (rx (format "%s\\.org$" (regexp-quote bn))))
    (save-some-buffers nil
                       (lambda ()
                         (string-prefix-p jao-org-notes-dir buffer-file-name)))
    (or (car (directory-files-recursively jao-org-notes-dir rx))
        (let* ((dirs (jao-org-notes-cats))
               (dir (completing-read "Notes subdir: " dirs nil t)))
          (expand-file-name (concat dir "/" bn ".org") jao-org-notes-dir)))))

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

;;;###autoload
(defun jao-org-pdf-goto-org (arg)
  (interactive "P")
  (when (jao-pdf-is-pdf-file buffer-file-name)
    (let* ((file (jao-org-pdf-to-org-file))
           (new (not (file-exists-p file)))
           (title (jao-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))

;;;###autoload
(defun jao-org-goto-pdf ()
  (interactive)
  (if-let (f (jao-org-org-to-pdf-file))
      (funcall jao-org-open-pdf-fun f 1)
    (user-error "No PDF file associated with this buffer")))

(with-eval-after-load "org"
  (define-key org-mode-map (kbd "C-c o") #'jao-org-goto-pdf))

;;;###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 "docview" :store #'ignore)
  (setq jao-org--sink-dir (file-name-as-directory sink-dir)))

(provide 'jao-org-links)