This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathorg-ref-cite-utils.el
229 lines (198 loc) · 8.26 KB
/
org-ref-cite-utils.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
;;; org-ref-cite-utils.el --- Utilities for org-ref-cite
;; Copyright(C) 2021 John Kitchin
;;
;; This file is not currently 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 2, 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 ; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;
;;; Commentary:
;;
;; This library provides some utilities, like opening the readme, extracting the
;; bibliography, and a debugging function.
;;;###autoload
(defun org-ref-cite-help ()
"Open the `org-ref-cite' manual."
(interactive)
(find-file (expand-file-name
"readme.org"
(file-name-directory
(find-library-name "org-ref-cite")))))
(defun org-ref-cite-get-unique-keys ()
"Get the unique keys in the buffer."
(delete-dups
(org-element-map (org-element-parse-buffer) 'citation-reference
(lambda (cr)
(org-element-property :key cr)))))
;;;###autoload
(defun org-ref-cite-extract-bibliography (format output)
"Extract the bibliography from the current org-file.
FORMAT: formatted bibtex
OUTPUT: buffer copy file"
(interactive (list (completing-read "Format: " '(formatted bibtex) nil t)
(completing-read "Output: " '(buffer copy file) nil t)))
(let* ((keys (org-ref-cite-get-unique-keys))
(strings (cl-loop for key in keys collect
(pcase format
("formatted"
(bibtex-completion-apa-format-reference key))
("bibtex"
(save-window-excursion
(bibtex-completion-show-entry (list key))
(bibtex-beginning-of-entry)
(bibtex-copy-entry-as-kill)
(pop bibtex-entry-kill-ring)))))))
(pcase output
("buffer"
(let ((buf (get-buffer-create "*org-ref-cite-export*")))
(with-current-buffer buf
(erase-buffer)
(insert (string-join strings "\n\n"))
(when (string= format "bibtex") (bibtex-mode))
(pop-to-buffer buf))))
("copy"
(kill-new (string-join strings "\n\n")))
("file"
(let ((fname (read-file-name "File: " nil nil nil (concat (file-name-base (buffer-file-name))
(pcase format
("bibtex" ".bib")
("formatted" ".txt"))))))
(with-temp-file fname
(insert (string-join strings "\n\n"))))))))
(defun org-ref-cite-s-insert (s &rest args)
"Insert S formatted with ARGS."
(insert (apply #'format s args)))
;;;###autoload
(defun org-ref-cite ()
"Generate a summary buffer of the current buffer.
This buffer shows the current setup, shows bad citations, etc."
(interactive)
(let* ((buf (get-buffer-create "*org-ref-cite*"))
(fname (buffer-file-name))
(data (org-element-parse-buffer))
(citations (org-element-map data 'citation 'identity))
(citation-references (org-element-map data 'citation-reference 'identity))
(bibtex-completion-bibliography (org-cite-list-bibliography-files))
(valid-keys (cl-loop for candidate in (bibtex-completion-candidates) collect
(cdr (assoc "=key=" (cdr candidate)))))
(bibliographystyle (cadr (assoc "BIBLIOGRAPHYSTYLE"
(org-collect-keywords '("BIBLIOGRAPHYSTYLE")))))
(bibliography (mapcar (lambda (f)
(format "[[%s]]" f))
(org-cite-list-bibliography-files)))
(natbib-options (or
;; keyword settings
(cadr (assoc
"NATBIB_OPTIONS"
(org-collect-keywords
'("NATBIB_OPTIONS"))))
;; settings in the org-file
(save-excursion
(goto-char (point-min))
(when (re-search-forward "\\\\usepackage\\[?\\([^]]*\\)\\]?{natbib}" nil t)
(match-string-no-properties 1)))
;; default global settings
(cl-loop for (option package preview) in
(append org-latex-default-packages-alist org-latex-packages-alist)
when (string= package "natbib")
return option)))
(unique-keys (org-ref-cite-get-unique-keys))
(bad-references (cl-loop for ref in citation-references
if (not (member (org-element-property :key ref) valid-keys))
collect ref))
;; I think we can only really support prefix on the first cite, and
;; suffix on the last cite. This is a convention though, it is not part
;; of the syntax, which allows a prefix and suffix on each
;; citation-reference.
(questionable-cites (cl-loop for citation in citations
;; look for prefix on entry after the first one
if (or (cl-loop for reference in (cdr (org-cite-get-references citation))
if (org-element-property :prefix reference)
return t)
;; or suffix before the last one.
(cl-loop for reference in (butlast (org-cite-get-references citation))
if (org-element-property :suffix reference)
return t))
collect (list citation (buffer-substring-no-properties
(org-element-property :begin citation)
(org-element-property :end citation))))))
(with-current-buffer buf
(erase-buffer)
(org-mode)
(org-ref-cite-s-insert "#+title: org-ref-cite report for %s\n\n" fname)
(org-ref-cite-s-insert "- bibliographystyle :: %s\n" bibliographystyle)
(org-ref-cite-s-insert "- bibliography :: %s\n" bibliography)
(org-ref-cite-s-insert "- natbib options :: %s\n" natbib-options)
;; natbib options
(org-ref-cite-s-insert "- # unique references :: %s\n" (length unique-keys))
(when bad-references
(insert "\n* Bad references\n\n")
(cl-loop for ref in bad-references do
(insert "- ")
(insert-button (org-element-property :key ref)
'face '(:foreground "red")
'keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "<mouse-1>")
`(lambda ()
(interactive)
(save-window-excursion
(find-file ,fname)
(goto-char ,(org-element-property :begin ref))
(org-ref-cite-replace-key-with-suggestions))))
map)
'mouse-face 'highlight
'help-echo "Bad key, click to replace.")
(org-ref-cite-s-insert " (Possible keys: %s)\n" (org-cite-basic--close-keys
(org-element-property :key ref)
valid-keys))))
(when questionable-cites
(insert "* Questionable cites
These citations have prefix text on one or more references that are not the first one, or suffix text that is not on the last reference. This has undefined export behavior.\n\n")
(cl-loop for (citation . cite) in questionable-cites do
(org-ref-cite-s-insert " - ")
(insert-button "[goto]"
'face '(:foreground "red")
'keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "<mouse-1>")
`(lambda ()
(interactive)
(find-file ,fname)
(goto-char ,(org-element-property :begin citation))))
map)
'mouse-face 'highlight
'help-echo "Bad key, click to replace.")
(org-ref-cite-s-insert " %s\n" cite)))
(insert "\n* LaTeX setup\n\n")
(cl-loop for s in `(,(format "org-latex-prefer-user-labels = %s"
org-latex-prefer-user-labels)
,(format "bibtex-dialect = %s" bibtex-dialect)
,(format "emacs-version = %s" (emacs-version))
,(format "org-version = %s" (org-version))
,(format "org-latex-pdf-process is defined as %s" org-latex-pdf-process)
,(format "bibtex-completion installed = %s" (featurep 'bibtex-completion))
,(format "bibtex-completion loaded = %s" (fboundp 'bibtex-completion-candidates)))
do
(insert "- " s "\n"))
(insert (format "- org-latex-default-packages-alist\n"))
(cl-loop for el in org-latex-default-packages-alist
do
(insert (format " %S\n" el)))
(if (null org-latex-packages-alist)
(insert "- org-latex-packages-alist is nil\n")
(insert "- org-latex-packages-alist\n")
(cl-loop for el in org-latex-packages-alist
do
(insert (format " %S\n" el)))))
(display-buffer-in-side-window buf '((side . right)))))
(provide 'org-ref-cite-utils)
;;; org-ref-cite-utils.el ends here