summaryrefslogtreecommitdiffhomepage
path: root/org/jao-org-links.el
blob: 614bfaad8239f1d4502a371c4c7819ad284466d6 (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
(require 'jao-org-utils)
(require 'pdf-info)

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

(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))))
           (org-open-file path 1)
           (pdf-view-goto-page page)
           (image-set-window-vscroll
            (round (/ (* height (cdr (pdf-view-image-size))) (frame-char-height))))))
        ((string-match "\\(.*\\)::\\([0-9]+\\)$"  link)
         (let* ((path (match-string 1 link))
                (page (string-to-number (match-string 2 link))))
           (org-open-file path 1)
           (pdf-view-goto-page page)))
        (t
         (org-open-file link 1))))

(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 p)
          (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 ()
  (let ((base (file-name-base (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)))
    cur-title))

(defun jao-org-links--store-pdf-link ()
  (when (derived-mode-p 'pdf-view-mode)
    (let ((path buffer-file-name)
          (page (pdf-view-current-page)))
      (org-store-link-props
       :type "doc"
       :link (format "doc:%s::%d" (file-name-nondirectory path) page)
       :description (format "%s (p. %d)" (jao-org--pdf-section-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)
  (setq jao-org--sink-dir (file-name-as-directory sink-dir)))

(defun jao-org--open-other (otp)
  (let ((rxs ))))

;;;###autoload
(defun jao-org-pdf-goto-org (arg)
  (interactive "P")
  (when (jao-org--pdf-p buffer-file-name)
    (let* ((file (replace-regexp-in-string "/doc/\\(.+\\)\\.pdf$"
                                           "/notes/\\1.org"
                                           buffer-file-name))
           (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
        (insert "#+title: " title "\n#+author:\n#+startup: latexpreview\n\n")
        (org-insert-link)))))

(provide 'jao-org-links)