-
Notifications
You must be signed in to change notification settings - Fork 53
/
oc-bibtex-actions.el
271 lines (240 loc) · 10.3 KB
/
oc-bibtex-actions.el
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
;;; bibtex-actions-org-cite.el --- Org-cite support for bibtex-actions -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2021 Bruce D'Arcus
;;
;; Author: Bruce D'Arcus <https://github.com/bdarcus>
;; Maintainer: Bruce D'Arcus <https://github.com/bdarcus>
;; Created: July 11, 2021
;; License: GPL-3.0-or-later
;; Version: 0.1
;; Homepage: https://github.com/bdarcus/bibtex-actions
;; Package-Requires: ((emacs "26.3")(org "9.5"))
;;
;; This file is not part of GNU Emacs.
;;
;; 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 of the License, 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;
;; This is a small package that intergrates bibtex-actions and org-cite. It
;; provides a simple org-cite processor with "follow" and "insert" capabilties.
;;
;; Simply load this file and it will configure them for 'org-cite.'
;;
;;; Code:
(require 'bibtex-actions)
(require 'org)
(require 'oc)
(require 'oc-basic)
(require 'oc-csl)
(require 'citeproc)
(require 'embark)
(declare-function bibtex-actions-at-point "bibtex-actions")
(declare-function org-open-at-point "org")
(declare-function org-cite-make-insert-processor "oc")
(defface bibtex-actions-org-cite-style-preview
;; Not sure if this is the best parent face.
'((t :inherit minibuffer-prompt))
"Face for org-cite previews."
:group 'oc-bibtex-actions)
(defcustom oc-bibtex-actions-preview-target 'natbib
"Export processor target for which to display previews."
;; REVIEW not sure this is the best approach.
:group 'oc-bibtex-actions
:type '(choice
(const biblatex)
(const csl)
(const natbib)))
;;; Internal variables
(defvar oc-bibtex-actions--csl-processor-cache nil
"Cache for the citation preview processor.")
(make-variable-buffer-local 'bibtex-actions--csl-processor-cache)
(defun oc-bibtex-actions--csl-processor ()
"Return a `citeproc-el' processor for style preview."
(or oc-bibtex-actions--csl-processor-cache
(let* ((bibliography (org-cite-list-bibliography-files))
(processor
(citeproc-create
org-cite-csl--fallback-style-file
(org-cite-csl--itemgetter bibliography)
(org-cite-csl--locale-getter))))
(setq oc-bibtex-actions--csl-processor-cache processor)
processor)))
;; TODO maybe connvert to defcustoms. But this is not really the right approach;
;; better to just run the export processors to get the previews. But we need
;; citation context for that, or some other solution to have a citation to
;; process.
(defvar oc-bibtex-actions-style-preview-alist
'((natbib .
(;; Default style.
("/" . "\\citep")
("/b" . "\\citealp")
("/c" . "\\Citep")
("/f" . "\\citep*")
("/bc" . "\\Citealp")
("/bf" . "\\citealp*")
("/cf" . "\\Citep*")
("/bcf" . "\\Citealp*")
;; "text" style.
("text" . "\\citet")
("text/b" . "\\citealt")
("text/c" . "\\Citet")
("text/f" . "\\citet*")
("text/bc" . "\\Citealt")
("text/bf" . "\\citealt*")
("text/cf" . "\\Citet*")
("text/bcf" . "\\Citealt*")
;; "author" style.
("author" . "\\citeauthor")
("author/c" . "\\Citeauthor")
("author/f" . "\\citeauthor*")
;; "noauthor" style.
("noauthor" . "\\citeyearpar")
("noauthor/b" . "\\citeyear")
;; "nocite" style.
("nocite" . "\\nocite")))
(biblatex .
( ;; Default style.
("/" . "\\autocite")
("/b" . "\\cite")
("/c" . "\\Autocite")
("/bc" . "\\Cite")
;; "text" style.
("text" . "\\textcite")
("text/c" . "\\Textcite")
;; "nocite" style.
("nocite" . "\\nocite")
;; "author" style.
("author/c" . "\\Citeauthor*")
("author/f" . "\\citeauthor")
("author/cf" . "\\Citeauthor")
("author" . "\\citeauthor*")
;; "locators" style.
("locators/b" . "\\notecite")
("locators/c" . "\\Pnotecite")
("locators/bc" . "\\Notecite")
("locators" . "\\pnotecite")
;; "noauthor" style.
("noauthor" . "\\autocite*")))
(csl .
(
("/" . "(de Loas, 2019)")
("/c" . "(De Loas, 2019")
("text" . "de Loas (2019)")
("text" ' "De Loas (2019)")))))
;TODO
;(defvar oc-bibtex-actions-open-default
;;; Org-cite processors
;; NOTE I may move some or all of these to a separate project
(defun oc-bibtex-actions-insert (&optional multiple)
"Return a list of keys when MULTIPLE, or else a key string."
(let ((references (bibtex-actions-read)))
(if multiple
references
(car references))))
(defun oc-bibtex-actions-follow (_datum _arg)
"Follow processor for org-cite."
(call-interactively bibtex-actions-at-point-function))
(defun oc-bibtex-actions-select-style ()
"Complete a citation style for org-cite with preview."
(interactive)
(let* ((oc-styles (oc-bibtex-actions--styles-candidates))
(style
(completing-read
"Styles: "
(lambda (str pred action)
(if (eq action 'metadata)
`(metadata
(annotation-function . oc-bibtex-actions--style-preview-annote)
(cycle-sort-function . identity)
(display-sort-function . identity)
(group-function . oc-bibtex-actions--styles-group-fn))
(complete-with-action action oc-styles str pred)))))
(style-final (string-trim style)))
(if (string= style-final "/") "" style-final)))
(defun oc-bibtex-actions--styles-candidates ()
"Generate candidate list."
;; TODO extract the style+variant strings from 'org-cite-support-styles'.
(cl-loop for style in
(cdr (assoc oc-bibtex-actions-preview-target
oc-bibtex-actions-style-preview-alist))
collect (cons
(concat " " (truncate-string-to-width (car style) 20 nil 32)) (cdr style))))
(defun oc-bibtex-actions--styles-group-fn (style transform)
"Return group title of STYLE or TRANSFORM the candidate.
This is a group-function that groups org-cite style/variant
strings by style."
(let* ((style-str (string-trim style))
(short-style
(if (string-match "^/[bcf]*" style-str) "default"
(car (split-string style-str "/")))))
(if transform
;; Use the candidate string as is, but add back whitespace alignment.
(concat " " (truncate-string-to-width style-str 20 nil 32))
;; Transform for grouping and display.
(cond
((string= short-style "default") "Default")
((string= short-style "author") "Author-Only")
((string= short-style "locators") "Locators-Only")
((string= short-style "text") "Textual/Narrative")
((string= short-style "nocite") "No Cite")
((string= short-style "noauthor") "Suppress Author")))))
(defun oc-bibtex-actions-csl-render-citation (citation)
"Render CITATION."
;; TODO hook this up to previews.
(let ((proc (oc-bibtex-actions--csl-processor)))
(citeproc-clear proc)
(let* ((info (list :cite-citeproc-processor proc))
(cit-struct (org-cite-csl--create-structure citation info)))
(citeproc-append-citations (list cit-struct) proc)
(car (citeproc-render-citations proc 'plain t)))))
(defun oc-bibtex-actions--style-preview-annote (style &optional _citation)
"Annotate STYLE with CITATION preview."
;; TODO rather than use the alist, run the export processors on the citation..
(let* ((preview (cdr (assoc style (oc-bibtex-actions--styles-candidates))))
;; TODO look at how define-face does this.
(formatted-preview (truncate-string-to-width preview 50 nil 32)))
(propertize formatted-preview 'face 'oc-bibtex-actions-style-preview)))
;;; Embark target finder
(defun oc-bibtex-actions-citation-finder ()
"Return org-cite citation keys at point as a list for `embark'."
(when-let ((keys (bibtex-actions-get-key-org-cite)))
(cons 'oc-citation (bibtex-actions--stringify-keys keys))))
;;; Keymap
(defvar oc-bibtex-actions-map
(let ((map (make-sparse-keymap)))
(define-key map "c" '("edit citation" . org-cite-insert))
(define-key map (kbd "o") '("open source document" . bibtex-actions-open))
(define-key map (kbd "e") '("open bibtex entry" . bibtex-actions-open-entry))
(define-key map (kbd "l") '("open source URL or DOI" . bibtex-actions-open-link))
(define-key map (kbd "n") '("open notes" . bibtex-actions-open-notes))
(define-key map (kbd "p") '("open PDF" . bibtex-actions-open-pdf))
(define-key map (kbd "r") '("refresh library" . bibtex-actions-refresh))
(define-key map (kbd "RET") '("default action" . bibtex-actions-run-default-action))
map)
"Keymap for 'oc-bibtex-actions' `embark' at-point functionality.")
;; Embark configuration for org-cite
(add-to-list 'embark-target-finders 'oc-bibtex-actions-citation-finder)
(add-to-list 'embark-keymap-alist '(bibtex . bibtex-actions-map))
(add-to-list 'embark-keymap-alist '(oc-citation . oc-bibtex-actions-map))
(when (boundp 'embark-pre-action-hooks)
;; Ensure that Embark ignores the target for 'org-cite-insert'.
(add-to-list 'embark-pre-action-hooks '(org-cite-insert embark--ignore-target)))
;; Load this last.
(org-cite-register-processor 'oc-bibtex-actions
:insert (org-cite-make-insert-processor
#'oc-bibtex-actions-insert
; #'org-cite-basic--complete-style)
#'oc-bibtex-actions-select-style)
:follow #'oc-bibtex-actions-follow)
(provide 'oc-bibtex-actions)
;;; oc-bibtex-actions.el ends here