summaryrefslogtreecommitdiff
path: root/bog.el
blob: 6eb3b2782318ab3be1eeabf76d85887e29cf29d4 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
;;; bog.el --- Extensions for research notes in Org mode

;; Copyright (C) 2013 Kyle Meyer <kyle@kyleam.com>

;; Author: Kyle Meyer <kyle@kyleam.com>
;; URL: https://github.com/kyleam/bog
;; Keywords: BibTeX, org-mode
;; Version: 0.5.0
;; Package-Requires: ((org "8.0.0") (dash "2.5.0"))

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Bog provides a few convenience functions for taking research notes in
;; Org mode.

;;; Code:

(require 'dash)
(require 'org)


;;; Customization

(defgroup bog nil
  "Extensions for research notes in Org mode"
  :group 'tools
  :group 'convenience)

(defcustom bog-notes-directory "~/bib"
  "The name of the directory that Org note are stored in."
  :group 'bog
  :type 'string)

(defcustom bog-pdf-directory
  (expand-file-name "pdfs" bog-notes-directory)
  "The name of the directory that PDF files are stored in."
  :group 'bog
  :type 'string)

(defcustom bog-pdf-directory-stage
  (expand-file-name "pdf-stage" bog-notes-directory)
  "The name of the directory to search for new PDFs in.
`bog-rename-staged-pdf-to-citekey' will search here for files to
rename."
  :group 'bog
  :type 'string)

(defcustom bog-read-file-name 'read-file-name
  "A function that will be used to promtp for file name.
The function should accept one arguments, a string to use for the
prompt. A good alternative is `ido-read-file-name'."
  :group 'bog
  :type 'function)

(defcustom bog-completing-read 'completing-read
  "A function that will be used for completion prompts.
The function should accept two arguments, a string to use for the
prompt and a list of strings to offer as choices. A good
alternative is `ido-completing-read'."
  :group 'bog
  :type 'function)

(defcustom bog-pdf-opener "xdg-open"
  "Program to open PDF files with."
  :group 'bog
  :type 'string)

(defcustom bog-citekey-format
  "\\([0-9]*[a-z]+[-a-z]*\\)\\([0-9]\\{4\\}\\)\\([a-z]+\\)"
  "Regex used to match study citekey.
By default, this matches any sequence of lower case
letters (allowing hyphenation) that is followed by 4 digits and
then lower case letters."
  :type 'string
  :group 'bog)


;;; General utilities

(defun bog-citekey-action (action ask-func ask-citekey)
  "Perform ACTION on file associtated with a citekey.

ASK-FUNC should be a function that queries the user for a citekey
when ASK-CITEKEY is non-nil. Otherwise, the citekey will be taken
from the text under point if it matches `bog-citekey-format' or
from the first parent heading that matches `bog-citekey-format'.

ACTION will be called with the resulting citekey as an argument."
  (let* ((citekey (and ask-citekey (funcall ask-func)))
         (citekey (or (bog-citekey-at-point)
                      (bog-citekey-heading))))
    (funcall action citekey)))

(defun bog-select-citekey (citekeys)
  "Prompt for citekey from CITEKEYS"
  (funcall bog-completing-read "Select citekey: " citekeys))

(defun bog-citekey-at-point ()
  (let ((maybe-citekey (thing-at-point 'word)))
    (when (and maybe-citekey
               (bog-citekey-only-p maybe-citekey))
      maybe-citekey)))

(defun bog-citekey-heading ()
  "Return first org heading that matches `bog-citekey-format'."
  (save-excursion
    (save-restriction
      (widen)
      (org-back-to-heading)
      (let ((heading (org-element-property :raw-value (org-element-at-point))))
        (while (and (not (bog-citekey-only-p heading))
                    (org-up-heading-safe))
          (setq heading
                (org-element-property :raw-value (org-element-at-point))))
        (when (not (bog-citekey-only-p heading))
          (error "Citekey not found"))
        heading))))

(defun bog-citekey-p (text)
  "Indicate if TEXT matches `bog-citekey-format'."
  (when (string-match bog-citekey-format text)
    t))

(defun bog-citekey-only-p (text)
  "Indicate if all of TEXT matches `bog-citekey-format'."
  (string-match bog-citekey-format text)
  (when (equal (length text) (match-end 0))
    t))


;;; PDF-related

;;;###autoload
(defun bog-find-citekey-pdf (arg)
  "Open PDF file for a citekey.
If a prefix argument is given, a prompt will open to select from
available citekeys. Otherwise, the citekey will be taken from the
text under point if it matches `bog-citekey-format' or from the
first parent heading that matches `bog-citekey-format'."
  (interactive "P")
  (bog-citekey-action 'bog-open-citekey-pdf
                      '(lambda () (bog-select-citekey (bog-pdf-citekeys)))
                      arg))

(defun bog-open-citekey-pdf (citekey)
  (let ((pdf-file (bog-citekey-as-pdf citekey)))
    (unless (file-exists-p pdf-file)
      (error "%s does not exist" pdf-file))
    (start-process "bog-pdf" nil bog-pdf-opener pdf-file)))

;;;###autoload
(defun bog-rename-staged-pdf-to-citekey ()
  "Rename PDF in  `bog-pdf-directory-stage' to `bog-pdf-directory'/<citekey>.pdf.
The citekey will be taken from the text under point if it matches
`bog-citekey-format' or from the first parent heading that
matches `bog-citekey-format'."
  (interactive)
  (bog-citekey-action 'bog-rename-staged-pdf
                      nil
                      nil))

(defun bog-rename-staged-pdf (citekey)
  (let* ((pdf-file (bog-citekey-as-pdf citekey))
         (choices
          (file-expand-wildcards
           (concat (file-name-as-directory bog-pdf-directory-stage) "*.pdf")))
         (num-choices (length choices))
         staged-pdf)
    (cond
     ((= 0 num-choices)
      (setq staged-pdf (funcall bog-read-file-name
                                "Select PDF file to rename: ")))
     ((= 1 num-choices)
      (setq staged-pdf (car choices)))
     (t
      (setq staged-pdf (funcall bog-completing-read
                                "Select PDF file to rename: " choices))))
    (rename-file staged-pdf pdf-file)
    (message "Renamed %s to %s." staged-pdf pdf-file)))

(defun bog-citekey-as-pdf (citekey)
  (expand-file-name (concat citekey ".pdf") bog-pdf-directory))

(defun bog-pdf-citekeys ()
  "Return a list citekeys for all pdf files in
`bog-pdf-directory'."
  (-map 'file-name-base
        (file-expand-wildcards (concat
                                (file-name-as-directory bog-pdf-directory)
                                "*.pdf"))))

(provide 'bog)

;; bog.el ends here