forked from tmalsburg/helm-bibtex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbibtex-completion.el
1439 lines (1322 loc) · 65.7 KB
/
bibtex-completion.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; bibtex-completion.el --- A BibTeX backend for completion frameworks
;; Author: Titus von der Malsburg <[email protected]>
;; Justin Burkett <[email protected]>
;; Maintainer: Titus von der Malsburg <[email protected]>
;; Version: 1.0.0
;; Package-Requires: ((parsebib "1.0") (s "1.9.0") (dash "2.6.0") (f "0.16.2") (cl-lib "0.5"))
;; 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:
;; A BibTeX backend for completion frameworks
;; There are currently two fronends: helm-bibtex and ivy-bibtex.
;;
;; See the github page for details:
;;
;; https://github.com/tmalsburg/helm-bibtex
;;; Code:
(require 'browse-url)
(require 'parsebib)
(require 'cl-lib)
(require 'dash)
(require 's)
(require 'f)
(require 'biblio)
(require 'filenotify)
(require 'subr-x)
;; Silence byte-compiler
(declare-function reftex-what-macro "reftex-parse")
(declare-function reftex-get-bibfile-list "reftex-cite")
(declare-function outline-show-all "outline")
(declare-function org-narrow-to-subtree "org")
(declare-function org-cycle-hide-drawers "org")
(declare-function org-find-property "org")
(declare-function org-show-entry "org")
(declare-function org-entry-get "org")
(declare-function org-element-parse-buffer "org-element")
(declare-function org-element-map "org-element")
(declare-function org-element-property "org-element")
(defgroup bibtex-completion nil
"Helm plugin for searching entries in a BibTeX bibliography."
:group 'completion)
(defcustom bibtex-completion-bibliography nil
"The BibTeX file or list of BibTeX files. Org-bibtex users can
also specify org-mode bibliography files, in which case it will
be assumed that a BibTeX file exists with the same name and
extension bib instead of org. If the bib file has a different
name, use a cons cell (\"orgfile.org\" . \"bibfile.bib\") instead."
:group 'bibtex-completion
:type '(choice file (repeat file)))
(defcustom bibtex-completion-library-path nil
"A directory or list of directories in which PDFs are stored.
Bibtex-completion assumes that the names of these PDFs are
composed of the BibTeX-key plus a \".pdf\" suffix."
:group 'bibtex-completion
:type '(choice directory (repeat directory)))
(defcustom bibtex-completion-pdf-open-function 'find-file
"The function used for opening PDF files. This can be an
arbitrary function that takes one argument: the path to the PDF
file. The default is `find-file' which opens the PDF in
Emacs (either with docview or, if installed, the much superior
pdf-tools. When set to `helm-open-file-with-default-tool', the
systems default viewer for PDFs is used."
:group 'bibtex-completion
:type 'function)
(defcustom bibtex-completion-pdf-extension ".pdf"
"The extension of a BibTeX entry's \"PDF\" file. This makes it
possible to use another file type. It can also be a list of
file types, which are then tried sequentially until a file is
found. Beware that adding file types can reduce performance for
large bibliographies. This variable has no effect if PDFs are
referenced via the file field."
:group 'bibtex-completion
:type 'string)
(defcustom bibtex-completion-find-additional-pdfs nil
"If non-nil, all files whose base name starts with the BibTeX
key and ends with `bibtex-completion-pdf-extension' are
considered as PDFs, not only \"<key>.<extension>\". Note that
for performance reasons, an entry is only marked as having a
PDF if \"<key>.<extension\" exists."
:group 'bibtex-completion
:type 'boolean)
(defcustom bibtex-completion-pdf-symbol "⌘"
"Symbol used to indicate that a PDF file is available for a
publication. This should be a single character."
:group 'bibtex-completion
:type 'string)
(defcustom bibtex-completion-format-citation-functions
'((org-mode . bibtex-completion-format-citation-ebib)
(latex-mode . bibtex-completion-format-citation-cite)
(markdown-mode . bibtex-completion-format-citation-pandoc-citeproc)
(default . bibtex-completion-format-citation-default))
"The functions used for formatting citations. The publication
can be cited, for example, as \cite{key} or ebib:key depending on
the major mode of the current buffer. Note that the functions
should accept a list of keys as input. With multiple marked
entries one can insert multiple keys at once,
e.g. \cite{key1,key2}. See the functions
`bibtex-completion-format-citation-ebib' and
`bibtex-completion-format-citation-cite' as examples."
:group 'bibtex-completion
:type '(alist :key-type symbol :value-type function))
(defcustom bibtex-completion-notes-path nil
"The place where notes are stored. This is either a file, in
which case all notes are stored in that file, or a directory, in
which case each publication gets its own notes file in that
directory. In the latter case, bibtex-completion assumes that the
names of the note files are composed of the BibTeX-key plus a
suffix that is specified in `bibtex-completion-notes-extension'."
:group 'bibtex-completion
:type '(choice file directory (const nil)))
(defcustom bibtex-completion-notes-template-multiple-files
"#+TITLE: Notes on: ${author-or-editor} (${year}): ${title}\n\n"
"Template used to create a new note when each note is stored in
a separate file. '${field-name}' can be used to insert the value
of a BibTeX field into the template. Apart from the fields defined in
the entry, one can also use the virtual field `author-or-editor` which
contains the author names if defined and otherwise the names of the
editors."
:group 'bibtex-completion
:type 'string)
(defcustom bibtex-completion-notes-template-one-file
"\n* ${author-or-editor} (${year}): ${title}\n :PROPERTIES:\n :Custom_ID: ${=key=}\n :END:\n\n"
"Template used to create a new note when all notes are stored
in one file. '${field-name}' can be used to insert the value of
a BibTeX field into the template. Apart from the fields defined in
the entry, one can also use the virtual field `author-or-editor` which
contains the author names if defined and otherwise the names of the
editors."
:group 'bibtex-completion
:type 'string)
(defcustom bibtex-completion-notes-key-pattern
":Custom_ID: +%s\\( \\|$\\)"
"The pattern used to find entries in the notes file. Only
relevant when all notes are stored in one file. The key can be
inserted into the pattern using the `format` function."
:group 'bibtex-completion
:type 'string)
(defcustom bibtex-completion-notes-extension ".org"
"The extension of the files containing notes. This is only
used when `bibtex-completion-notes-path' is a directory (not a file)."
:group 'bibtex-completion
:type 'string)
(defcustom bibtex-completion-notes-symbol "✎"
"Symbol used to indicate that a publication has notes. This
should be a single character."
:group 'bibtex-completion
:type 'string)
(defcustom bibtex-completion-fallback-options
'(("CrossRef (biblio.el)"
. (lambda (search-expression) (biblio-lookup #'biblio-crossref-backend search-expression)))
("arXiv (biblio.el)"
. (lambda (search-expression) (biblio-lookup #'biblio-arxiv-backend search-expression)))
("DBLP (computer science bibliography) (biblio.el)"
. (lambda (search-expression) (biblio--lookup-1 #'biblio-dblp-backend search-expression)))
("HAL (French open archive) (biblio.el)"
. (lambda (search-expression) (biblio--lookup-1 #'biblio-hal-backend search-expression)))
("Google Scholar (web)"
. "https://scholar.google.co.uk/scholar?q=%s")
("Pubmed (web)"
. "https://www.ncbi.nlm.nih.gov/pubmed/?term=%s")
("Bodleian Library (web)"
. "http://solo.bodleian.ox.ac.uk/primo_library/libweb/action/search.do?vl(freeText0)=%s&fn=search&tab=all")
("Library of Congress (web)"
. "https://www.loc.gov/search/?q=%s&all=true&st=list")
("Deutsche Nationalbibliothek (web)"
. "https://portal.dnb.de/opac.htm?query=%s")
("British National Library (web)"
. "http://explore.bl.uk/primo_library/libweb/action/search.do?&vl(freeText0)=%s&fn=search")
("Bibliothèque nationale de France (web)"
. "http://catalogue.bnf.fr/servlet/RechercheEquation?host=catalogue?historique1=Recherche+par+mots+de+la+notice&niveau1=1&url1=/jsp/recherchemots_simple.jsp?host=catalogue&maxNiveau=1&categorieRecherche=RechercheMotsSimple&NomPageJSP=/jsp/recherchemots_simple.jsp?host=catalogue&RechercheMotsSimpleAsauvegarder=0&ecranRechercheMot=/jsp/recherchemots_simple.jsp&resultatsParPage=20&x=40&y=22&nbElementsHDJ=6&nbElementsRDJ=7&nbElementsRCL=12&FondsNumerise=M&CollectionHautdejardin=TVXZROM&HDJ_DAV=R&HDJ_D2=V&HDJ_D1=T&HDJ_D3=X&HDJ_D4=Z&HDJ_SRB=O&CollectionRezdejardin=UWY1SPQM&RDJ_DAV=S&RDJ_D2=W&RDJ_D1=U&RDJ_D3=Y&RDJ_D4=1&RDJ_SRB=P&RDJ_RLR=Q&RICHELIEU_AUTRE=ABCDEEGIKLJ&RCL_D1=A&RCL_D2=K&RCL_D3=D&RCL_D4=E&RCL_D5=E&RCL_D6=C&RCL_D7=B&RCL_D8=J&RCL_D9=G&RCL_D10=I&RCL_D11=L&ARSENAL=H&LivrePeriodique=IP&partitions=C&images_fixes=F&son=S&images_animees=N&Disquette_cederoms=E&multimedia=M&cartes_plans=D&manuscrits=BT&monnaies_medailles_objets=JO&salle_spectacle=V&Monographie_TN=M&Periodique_TN=S&Recueil_TN=R&CollectionEditorial_TN=C&Ensemble_TN=E&Spectacle_TN=A&NoticeB=%s")
("Gallica Bibliothèque Numérique (web)"
. "http://gallica.bnf.fr/Search?q=%s"))
"Alist of online sources that can be used to search for
publications. The key of each entry is the name of the online
source. The value is the URL used for retrieving results. This
URL must contain a %s in the position where the search term
should be inserted. Alternatively, the value can be a function
that will be called when the entry is selected."
:group 'bibtex-completion
:type '(alist :key-type string
:value-type (choice (string :tag "URL")
(function :tag "Function"))))
(defcustom bibtex-completion-browser-function nil
"The browser that is used to access online resources. If
nil (default), the value of `browse-url-browser-function' is
used. If that value is nil, Helm uses the first available
browser in `helm-browse-url-default-browser-alist'"
:group 'bibtex-completion
:type '(choice
(const :tag "Default" :value nil)
(function-item :tag "Emacs interface to w3m" :value w3m-browse-url)
(function-item :tag "Emacs W3" :value browse-url-w3)
(function-item :tag "W3 in another Emacs via `gnudoit'"
:value browse-url-w3-gnudoit)
(function-item :tag "Mozilla" :value browse-url-mozilla)
(function-item :tag "Firefox" :value browse-url-firefox)
(function-item :tag "Chromium" :value browse-url-chromium)
(function-item :tag "Galeon" :value browse-url-galeon)
(function-item :tag "Epiphany" :value browse-url-epiphany)
(function-item :tag "Netscape" :value browse-url-netscape)
(function-item :tag "eww" :value eww-browse-url)
(function-item :tag "Mosaic" :value browse-url-mosaic)
(function-item :tag "Mosaic using CCI" :value browse-url-cci)
(function-item :tag "Text browser in an xterm window"
:value browse-url-text-xterm)
(function-item :tag "Text browser in an Emacs window"
:value browse-url-text-emacs)
(function-item :tag "KDE" :value browse-url-kde)
(function-item :tag "Elinks" :value browse-url-elinks)
(function-item :tag "Specified by `Browse Url Generic Program'"
:value browse-url-generic)
(function-item :tag "Default Windows browser"
:value browse-url-default-windows-browser)
(function-item :tag "Default Mac OS X browser"
:value browse-url-default-macosx-browser)
(function-item :tag "GNOME invoking Mozilla"
:value browse-url-gnome-moz)
(function-item :tag "Default browser"
:value browse-url-default-browser)
(function :tag "Your own function")
(alist :tag "Regexp/function association list"
:key-type regexp :value-type function)))
(defcustom bibtex-completion-additional-search-fields nil
"The fields that are used for searching in addition to author,
editor, title, year, BibTeX key, and entry type."
:group 'bibtex-completion
:type '(repeat string))
(defcustom bibtex-completion-no-export-fields nil
"A list of fields that should be ignored when exporting BibTeX
entries."
:group 'bibtex-completion
:type '(repeat string))
(defcustom bibtex-completion-cite-commands '("cite" "Cite" "parencite"
"Parencite" "footcite" "footcitetext" "textcite" "Textcite"
"smartcite" "Smartcite" "cite*" "parencite*" "supercite" "autocite"
"Autocite" "autocite*" "Autocite*" "citeauthor" "Citeauthor"
"citeauthor*" "Citeauthor*" "citetitle" "citetitle*" "citeyear"
"citeyear*" "citedate" "citedate*" "citeurl" "nocite" "fullcite"
"footfullcite" "notecite" "Notecite" "pnotecite" "Pnotecite"
"fnotecite")
"The list of LaTeX cite commands. When creating LaTeX
citations, these can be accessed as future entries in the
minibuffer history, i.e. by pressing the arrow down key. The
default entries are taken from biblatex. There is currently no
special support for multicite commands and volcite et al. These
commands can be used but bibtex-completion does not prompt for their
extra arguments."
:group 'bibtex-completion
:type '(choice string (repeat string)))
(defcustom bibtex-completion-cite-default-command "cite"
"The LaTeX cite command that is used if the user doesn't enter
anything when prompted for such a command."
:group 'bibtex-completion
:type 'string)
(defcustom bibtex-completion-cite-prompt-for-optional-arguments t
"If t, bibtex-completion prompts for pre- and postnotes for
LaTeX cite commands. Choose nil for no prompts."
:group 'bibtex-completion
:type 'boolean)
(defcustom bibtex-completion-cite-default-as-initial-input nil
"This variable controls how the default command defined in
`bibtex-completion-cite-default-command' is used. If t, it is inserted
into the minibuffer before reading input from the user. If nil,
it is used as the default if the user doesn't enter anything."
:group 'bibtex-completion
:type 'boolean)
(defcustom bibtex-completion-pdf-field nil
"The name of the BibTeX field in which the path to PDF files is
stored or nil if no such field should be used. If an entry has
no value for this field, or if the specified file does not exist,
or if this variable is nil, bibtex-completion will look up the PDF in
the directories listed in `bibtex-completion-library-path'."
:group 'bibtex-completion
:type 'string)
(defcustom bibtex-completion-display-formats
'((t . "${author:36} ${title:*} ${year:4} ${=has-pdf=:1}${=has-note=:1} ${=type=:7}"))
"Alist of format strings for displaying entries in the results list.
The key of each element of this list is either a BibTeX entry
type (in which case the format string applies to entries of this
type only) or t (in which case the format string applies to all
other entry types). The value is the format string.
In the format string, expressions like \"${author:36}\",
\"${title:*}\", etc, are expanded to the value of the
corresponding field. An expression like \"${author:N}\" is
truncated to a width of N characters, whereas an expression like
\"${title:*}\" is truncated to the remaining width in the results
window. Three special fields are available: \"=type=\" holds the
BibTeX entry type, \"=has-pdf=\" holds
`bibtex-completion-pdf-symbol' if the entry has a PDF file, and
\"=has-notes=\" holds `bibtex-completion-notes-symbol' if the
entry has a notes file. The \"author\" field is expanded to
either the author names or, if the entry has no author field, the
editor names."
:group 'bibtex-completion
:type '(alist :key-type symbol :value-type string))
(defvar bibtex-completion-cross-referenced-entry-types
'("proceedings" "mvproceedings" "book" "mvbook" "collection" "mvcollection")
"The list of potentially cross-referenced entry types (in
lowercase). Only entries of these types are checked in
order to resolve cross-references. The default list is usually
sufficient; adding more types can slow down resolution for
large biblioraphies.")
(defvar bibtex-completion-display-formats-internal nil
"Stores `bibtex-completion-display-formats' together with the
\"used width\" of each format string. This is set internally.")
(defvar bibtex-completion-cache nil
"A cache storing the hash of the bibliography content and the
corresponding list of entries, for each bibliography file,
obtained when the bibliography was last parsed. When the
current bibliography hash is identical to the cached hash, the
cached list of candidates is reused, otherwise the bibliography
file is reparsed.")
(defvar bibtex-completion-string-cache nil
"A cache storing bibtex strings, for each bibliography file, obtained when the bibliography was last parsed.")
(defvar bibtex-completion-string-hash-table nil
"A hash table used for string replacements.")
(defun bibtex-completion-normalize-bibliography (&optional type)
"Returns a list of bibliography file(s) in
`bibtex-completion-bibliography'. If there are org-mode
bibliography-files, their corresponding bibtex files are listed
as well, unless TYPE is 'main. If TYPE is 'bibtex, org-mode
bibliography-files are instead replaced with their associated
bibtex files."
(delete-dups
(cl-loop
for bib-file in (-flatten (list bibtex-completion-bibliography))
for main-file = (if (consp bib-file)
(car bib-file)
bib-file)
for bibtex-file = (if (consp bib-file)
(cdr bib-file)
(concat (file-name-sans-extension main-file) ".bib"))
unless (equal type 'bibtex)
collect main-file
unless (equal type 'main)
collect bibtex-file)))
(defvar bibtex-completion-file-watch-descriptors nil
"List of file watches monitoring bibliography files for changes.")
(defun bibtex-completion-init ()
"Checks that the files and directories specified by the user
actually exist. Also sets `bibtex-completion-display-formats-internal'."
;; Remove current watch-descriptors for bibliography files:
(mapc (lambda (watch-descriptor)
(file-notify-rm-watch watch-descriptor))
bibtex-completion-file-watch-descriptors)
(setq bibtex-completion-file-watch-descriptors nil)
;; Check that all specified bibliography files exist and add file
;; watches for automatic reloading of the bibliography when a file
;; is changed:
(mapc (lambda (file)
(if (f-file? file)
(let ((watch-descriptor
(file-notify-add-watch file
'(change)
(lambda (event) (bibtex-completion-candidates)))))
(setq bibtex-completion-file-watch-descriptors
(cons watch-descriptor bibtex-completion-file-watch-descriptors)))
(user-error "Bibliography file %s could not be found." file)))
(bibtex-completion-normalize-bibliography))
;; Pre-calculate minimal widths needed by the format strings for
;; various entry types:
(setq bibtex-completion-display-formats-internal
(mapcar (lambda (format)
(let* ((format-string (cdr format))
(fields-width 0)
(string-width
(length
(s-format format-string
(lambda (field)
(setq fields-width
(+ fields-width
(string-to-number
(or (cadr (split-string field ":"))
""))))
"")))))
(-cons* (car format) format-string (+ fields-width string-width))))
bibtex-completion-display-formats)))
(defun bibtex-completion-clear-cache (&optional files)
"Clears FILES from cache. If FILES is omitted, all files in `bibtex-completion-biblography' are cleared."
(setq bibtex-completion-cache
(cl-remove-if
(lambda (x)
(member (car x)
(or files
(bibtex-completion-normalize-bibliography 'bibtex))))
bibtex-completion-cache)))
(defun bibtex-completion-clear-string-cache (&optional files)
"Clears FILES from cache. If FILES is omitted, all files in `bibtex-completion-bibliography' are cleared."
(setq bibtex-completion-string-cache
(cl-remove-if
(lambda (x)
(member (car x)
(or files
(-flatten (list bibtex-completion-bibliography)))))
bibtex-completion-string-cache)))
(defun bibtex-completion-parse-strings (&optional ht-strings)
"Parse the BibTeX strings listed in the current buffer and
return a list of entries in the order in which they appeared in
the BibTeX file.
If HT-STRINGS is provided it is assumed to be a hash table used
for string replacement."
(goto-char (point-min))
(let ((strings (cl-loop
with ht = (if ht-strings ht-strings (make-hash-table :test #'equal))
for entry-type = (parsebib-find-next-item)
while entry-type
if (string= (downcase entry-type) "string")
collect (let ((entry (parsebib-read-string (point) ht)))
(puthash (car entry) (cdr entry) ht)
entry)
)))
(-filter (lambda (x) x) strings)))
(defun bibtex-completion-update-strings-ht (ht strings)
(cl-loop
for entry in strings
do (puthash (car entry) (cdr entry) ht)))
(defvar bibtex-completion-cached-notes-keys nil
"A cache storing notes keys obtained when the bibliography was last parsed.")
(defun bibtex-completion-candidates ()
"Reads the BibTeX files and returns a list of conses, one for
each entry. The first element of these conses is a string
containing authors, editors, title, year, type, and key of the
entry. This is string is used for matching. The second element
is the entry (only the fields listed above) as an alist."
(let ((files (nreverse (bibtex-completion-normalize-bibliography 'bibtex)))
(ht-strings (make-hash-table :test #'equal))
reparsed-files)
;; Open each bibliography file in a temporary buffer,
;; check hash of bibliography and mark for reparsing if necessary:
(cl-loop
for file in files
do
(with-temp-buffer
(insert-file-contents file)
(let ((bibliography-hash (secure-hash 'sha256 (current-buffer))))
(unless (string= (cadr (assoc file bibtex-completion-cache))
bibliography-hash)
;; Mark file as reparsed.
;; This will be useful to resolve cross-references:
(push file reparsed-files)))))
(when (and bibtex-completion-notes-path
(f-file? bibtex-completion-notes-path))
(require 'org-element)
(with-temp-buffer
(org-mode) ;; need this to avoid error in emacs 25.3.1
(insert-file-contents bibtex-completion-notes-path)
(setq bibtex-completion-cached-notes-keys
(let ((tree (org-element-parse-buffer 'headline)))
(org-element-map tree 'headline
(lambda (key) (org-element-property :CUSTOM_ID key)))))))
;; reparse if necessary
(when reparsed-files
(cl-loop
for file in files
do
(with-temp-buffer
(insert-file-contents file)
(let ((bibliography-hash (secure-hash 'sha256 (current-buffer))))
(if (not (member file reparsed-files))
(bibtex-completion-update-strings-ht ht-strings
(cddr (assoc file bibtex-completion-string-cache)))
(progn
(message "Parsing bibliography file %s ..." file)
(bibtex-completion-clear-string-cache (list file))
(push (-cons* file
bibliography-hash
(bibtex-completion-parse-strings ht-strings))
bibtex-completion-string-cache)
(bibtex-completion-clear-cache (list file))
(push (-cons* file
bibliography-hash
(bibtex-completion-parse-bibliography ht-strings))
bibtex-completion-cache))))))
(setf bibtex-completion-string-hash-table ht-strings))
;; If some files were reparsed, resolve cross-references:
(when reparsed-files
(message "Resolving cross-references ...")
(bibtex-completion-resolve-crossrefs files reparsed-files))
;; Finally return the list of candidates:
(message "Done (re)loading bibliography.")
(nreverse
(cl-loop
for file in files
append (cddr (assoc file bibtex-completion-cache))))))
(defun bibtex-completion-resolve-crossrefs (files reparsed-files)
"Expand all entries with fields from cross-referenced entries
in FILES, assuming that only those files in REPARSED-FILES were
reparsed whereas the other files in FILES were up-to-date."
(cl-loop
with entry-hash = (bibtex-completion-make-entry-hash files reparsed-files)
for file in files
for entries = (cddr (assoc file bibtex-completion-cache))
if (member file reparsed-files)
;; The file was reparsed.
;; Resolve crossrefs then make candidates for all entries:
do (setf
(cddr (assoc file bibtex-completion-cache))
(cl-loop
for entry in entries
;; Entries are alists of \(FIELD . VALUE\) pairs.
for crossref = (bibtex-completion-get-value "crossref" entry)
collect (bibtex-completion-make-candidate
(if crossref
(bibtex-completion-remove-duplicated-fields
;; Insert an empty field so we can discard the crossref info if needed:
(append entry
(cl-acons "" ""
(gethash (downcase crossref) entry-hash))))
entry))))
else
;; The file was not reparsed.
;; Resolve crossrefs then make candidates for the entries with a crossref field:
do (setf
(cddr (assoc file bibtex-completion-cache))
(cl-loop
for entry in entries
;; Entries are \(STRING . ALIST\) conses.
for entry-alist = (cdr entry)
for crossref = (bibtex-completion-get-value "crossref" entry-alist)
collect (if crossref
(bibtex-completion-make-candidate
(bibtex-completion-remove-duplicated-fields
;; Discard crossref info and resolve crossref again:
(append (--take-while (> (length (car it)) 0) entry-alist)
(cl-acons "" ""
(gethash (downcase crossref) entry-hash)))))
entry)))))
(defun bibtex-completion-make-entry-hash (files reparsed-files)
"Return a hash table of all potentially cross-referenced bibliography entries in FILES,
assuming that only those files in REPARSED-FILES were reparsed
whereas the other files in FILES were up-to-date. Only entries
whose type belongs to
`bibtex-completion-cross-referenced-entry-types' are included in
the hash table."
(cl-loop
with entries =
(cl-loop
for file in files
for entries = (cddr (assoc file bibtex-completion-cache))
if (member file reparsed-files)
;; Entries are alists of \(FIELD . VALUE\) pairs.
append entries
;; Entries are \(STRING . ALIST\) conses.
else
append (mapcar 'cdr entries))
with ht = (make-hash-table :test #'equal :size (length entries))
for entry in entries
for key = (bibtex-completion-get-value "=key=" entry)
if (member (downcase (bibtex-completion-get-value "=type=" entry))
bibtex-completion-cross-referenced-entry-types)
do (puthash (downcase key) entry ht)
finally return ht))
(defun bibtex-completion-make-candidate (entry)
"Return a candidate for ENTRY."
(cons (bibtex-completion-clean-string
(s-join " " (-map #'cdr entry)))
entry))
(defun bibtex-completion-parse-bibliography (&optional ht-strings)
"Parse the BibTeX entries listed in the current buffer and
return a list of entries in the order in which they appeared in
the BibTeX file. Also do some preprocessing of the entries.
If HT-STRINGS is provided it is assumed to be a hash table."
(goto-char (point-min))
(cl-loop
with fields = (append '("title" "crossref")
(-map (lambda (it) (if (symbolp it) (symbol-name it) it))
bibtex-completion-additional-search-fields))
for entry-type = (parsebib-find-next-item)
while entry-type
unless (member-ignore-case entry-type '("preamble" "string" "comment"))
collect (let* ((entry (parsebib-read-entry entry-type (point) ht-strings))
(fields (append
(list (if (assoc-string "author" entry 'case-fold)
"author"
"editor")
(if (assoc-string "date" entry 'case-fold)
"date"
"year"))
fields)))
(-map (lambda (it)
(cons (downcase (car it)) (cdr it)))
(bibtex-completion-prepare-entry entry fields)))))
(defun bibtex-completion-get-entry (entry-key)
"Given a BibTeX key this function scans all bibliographies
listed in `bibtex-completion-bibliography' and returns an alist of the
record with that key. Fields from crossreferenced entries are
appended to the requested entry."
(let* ((entry (bibtex-completion-get-entry1 entry-key))
(crossref (bibtex-completion-get-value "crossref" entry))
(crossref (when crossref (bibtex-completion-get-entry1 crossref))))
(bibtex-completion-remove-duplicated-fields (append entry crossref))))
(defun bibtex-completion-get-entry1 (entry-key &optional do-not-find-pdf)
(with-temp-buffer
(mapc #'insert-file-contents
(bibtex-completion-normalize-bibliography 'bibtex))
(goto-char (point-min))
(if (re-search-forward (concat "^[ \t]*@\\(" parsebib--bibtex-identifier
"\\)[[:space:]]*[\(\{][[:space:]]*"
(regexp-quote entry-key) "[[:space:]]*,")
nil t)
(let ((entry-type (match-string 1)))
(reverse (bibtex-completion-prepare-entry
(parsebib-read-entry entry-type (point) bibtex-completion-string-hash-table) nil do-not-find-pdf)))
(progn
(display-warning :warning (concat "Bibtex-completion couldn't find entry with key \"" entry-key "\"."))
nil))))
(defun bibtex-completion-find-pdf-in-field (key-or-entry)
"Returns the path of the PDF specified in the field
`bibtex-completion-pdf-field' if that file exists. Returns nil if no
file is specified, or if the specified file does not exist, or if
`bibtex-completion-pdf-field' is nil."
(when bibtex-completion-pdf-field
(let* ((entry (if (stringp key-or-entry)
(bibtex-completion-get-entry1 key-or-entry t)
key-or-entry))
(value (bibtex-completion-get-value bibtex-completion-pdf-field entry)))
(cond
((not value) nil) ; Field not defined.
((f-file? value) (list value)) ; A bare full path was found.
((-any 'f-file? (--map (f-join it (f-filename value)) (-flatten bibtex-completion-library-path))) (-filter 'f-file? (--map (f-join it (f-filename value)) (-flatten bibtex-completion-library-path))))
(t ; Zotero/Mendeley/JabRef format:
(let ((value (replace-regexp-in-string "\\([^\\]\\);" "\\1\^^" value)))
(cl-loop ; Looping over the files:
for record in (s-split "\^^" value)
; Replace unescaped colons by field separator:
for record = (replace-regexp-in-string "\\([^\\]\\|^\\):" "\\1\^_" record)
; Unescape stuff:
for record = (replace-regexp-in-string "\\\\\\(.\\)" "\\1" record)
; Now we can safely split:
for record = (s-split "\^_" record)
for file-name = (nth 0 record)
for path = (or (nth 1 record) "")
for paths = (if (s-match "^[A-Z]:" path)
(list path) ; Absolute Windows path
; Something else:
(append
(list
path
file-name
(f-join (f-root) path) ; Mendeley #105
(f-join (f-root) path file-name)) ; Mendeley #105
(--map (f-join it path)
(-flatten bibtex-completion-library-path)) ; Jabref #100
(--map (f-join it path file-name)
(-flatten bibtex-completion-library-path)))) ; Jabref #100
for result = (-first (lambda (path)
(if (and (not (s-blank-str? path))
(f-exists? path))
path nil)) paths)
if result collect result)))))))
(defun bibtex-completion-find-pdf-in-library (key-or-entry &optional find-additional)
"Searches the directories in `bibtex-completion-library-path'
for a PDF whose name is composed of the BibTeX key plus
`bibtex-completion-pdf-extension'. The path of the first matching
PDF is returned.
If FIND-ADDITIONAL is non-nil, the paths of all PDFs whose name
starts with the BibTeX key and ends with
`bibtex-completion-pdf-extension' are returned instead."
(let* ((key (if (stringp key-or-entry)
key-or-entry
(bibtex-completion-get-value "=key=" key-or-entry)))
(main-pdf (cl-loop
for dir in (-flatten bibtex-completion-library-path)
append (cl-loop
for ext in (-flatten bibtex-completion-pdf-extension)
collect (f-join dir (s-concat key ext))))))
(if find-additional
(sort ; move main pdf on top of the list if needed
(cl-loop
for dir in (-flatten bibtex-completion-library-path)
append (directory-files dir t
(s-concat "^" (regexp-quote key)
".*\\("
(mapconcat 'regexp-quote
(-flatten bibtex-completion-pdf-extension)
"\\|")
"\\)$")))
(lambda (x y)
(and (member x main-pdf)
(not (member y main-pdf)))))
(-flatten (-first 'f-file? main-pdf)))))
(defun bibtex-completion-find-pdf (key-or-entry &optional find-additional)
"Returns the path of the PDF associated with the specified
entry. This is either the path(s) specified in the field
`bibtex-completion-pdf-field' or, if that does not exist, the
first PDF in any of the directories in
`bibtex-completion-library-path' whose name is composed of the
the BibTeX key plus `bibtex-completion-pdf-extension' (or if
FIND-ADDITIONAL is non-nil, all PDFs in
`bibtex-completion-library-path' whose name starts with the
BibTeX key and ends with `bibtex-completion-pdf-extension').
Returns nil if no PDF is found."
(or (bibtex-completion-find-pdf-in-field key-or-entry)
(bibtex-completion-find-pdf-in-library key-or-entry find-additional)))
(defun bibtex-completion-prepare-entry (entry &optional fields do-not-find-pdf)
"Prepare ENTRY for display.
ENTRY is an alist representing an entry as returned by
parsebib-read-entry. All the fields not in FIELDS are removed
from ENTRY, with the exception of the \"=type=\" and \"=key=\"
fields. If FIELDS is empty, all fields are kept. Also add a
=has-pdf= and/or =has-note= field, if they exist for ENTRY. If
DO-NOT-FIND-PDF is non-nil, this function does not attempt to
find a PDF file."
(when entry ; entry may be nil, in which case just return nil
(let* ((fields (when fields (append fields (list "=type=" "=key=" "=has-pdf=" "=has-note="))))
; Check for PDF:
(entry (if (and (not do-not-find-pdf) (bibtex-completion-find-pdf entry))
(cons (cons "=has-pdf=" bibtex-completion-pdf-symbol) entry)
entry))
(entry-key (cdr (assoc "=key=" entry)))
; Check for notes:
(entry (if (or
;; One note file per entry:
(and bibtex-completion-notes-path
(f-directory? bibtex-completion-notes-path)
(f-file? (f-join bibtex-completion-notes-path
(s-concat entry-key
bibtex-completion-notes-extension))))
;; All notes in one file:
(and bibtex-completion-notes-path
(f-file? bibtex-completion-notes-path)
(member entry-key bibtex-completion-cached-notes-keys)))
(cons (cons "=has-note=" bibtex-completion-notes-symbol) entry)
entry))
; Remove unwanted fields:
(entry (if fields
(--filter (member-ignore-case (car it) fields) entry)
entry)))
;; Normalize case of entry type:
(setcdr (assoc "=type=" entry) (downcase (cdr (assoc "=type=" entry))))
;; Remove duplicated fields:
(bibtex-completion-remove-duplicated-fields entry))))
(defun bibtex-completion-remove-duplicated-fields (entry)
"Remove duplicated fields from ENTRY."
(cl-remove-duplicates entry
:test (lambda (x y) (string= (s-downcase x) (s-downcase y)))
:key 'car :from-end t))
(defun bibtex-completion-format-entry (entry width)
"Formats a BibTeX ENTRY for display in results list. WIDTH is
the width of the results list. The display format is governed by
the variable `bibtex-completion-display-formats'."
(let* ((format
(or (assoc-string (bibtex-completion-get-value "=type=" entry)
bibtex-completion-display-formats-internal
'case-fold)
(assoc t bibtex-completion-display-formats-internal)))
(format-string (cadr format)))
(s-format
format-string
(lambda (field)
(let* ((field (split-string field ":"))
(field-name (car field))
(field-width (cadr field))
(field-value (bibtex-completion-get-value field-name entry)))
(when (and (string= field-name "author")
(not field-value))
(setq field-value (bibtex-completion-get-value "editor" entry)))
(when (and (string= field-name "year")
(not field-value))
(setq field-value (car (split-string (bibtex-completion-get-value "date" entry "") "-"))))
(setq field-value (bibtex-completion-clean-string (or field-value " ")))
(when (member field-name '("author" "editor"))
(setq field-value (bibtex-completion-shorten-authors field-value)))
(if (not field-width)
field-value
(setq field-width (string-to-number field-width))
(truncate-string-to-width
field-value
(if (> field-width 0)
field-width
(- width (cddr format)))
0 ?\s)))))))
(defun bibtex-completion-clean-string (s)
"Removes quoting and superfluous white space from BibTeX field
values."
(if s (replace-regexp-in-string "[\n\t ]+" " "
(replace-regexp-in-string "[\"{}]+" "" s))
nil))
(defun bibtex-completion-shorten-authors (authors)
"Returns a comma-separated list of the surnames in authors."
(if authors
(cl-loop for a in (s-split " and " authors)
for p = (s-split "," a t)
for sep = "" then ", "
concat sep
if (eq 1 (length p))
concat (-last-item (s-split " +" (car p) t))
else
concat (car p))
nil))
(defun bibtex-completion-open-pdf (keys &optional fallback-action)
"Open the PDFs associated with the marked entries using the
function specified in `bibtex-completion-pdf-open-function'.
If multiple PDFs are found for an entry, ask for the one to
open using `completion-read'. If FALLBACK-ACTION is non-nil, it is called in
case no PDF is found."
(dolist (key keys)
(let ((pdf (bibtex-completion-find-pdf key bibtex-completion-find-additional-pdfs)))
(cond
((> (length pdf) 1)
(let* ((pdf (f-uniquify-alist pdf))
(choice (completing-read "File to open: " (mapcar 'cdr pdf) nil t))
(file (car (rassoc choice pdf))))
(funcall bibtex-completion-pdf-open-function file)))
(pdf
(funcall bibtex-completion-pdf-open-function (car pdf)))
(fallback-action
(funcall fallback-action (list key)))
(t
(message "No PDF(s) found for this entry: %s"
key))))))
(defun bibtex-completion-open-url-or-doi (keys)
"Open the associated URL or DOI in a browser."
(dolist (key keys)
(let* ((entry (bibtex-completion-get-entry key))
(url (bibtex-completion-get-value "url" entry))
(doi (bibtex-completion-get-value "doi" entry))
(browse-url-browser-function
(or bibtex-completion-browser-function
browse-url-browser-function)))
(if url
(browse-url url)
(if doi (browse-url
(s-concat "http://dx.doi.org/" doi))
(message "No URL or DOI found for this entry: %s"
key))))))
(defun bibtex-completion-open-any (keys)
"Open the PDFs associated with the marked entries using the
function specified in `bibtex-completion-pdf-open-function'.
If multiple PDFs are found for an entry, ask for the one to
open using `completion-read'. If no PDF is found, try to open a URL
or DOI in the browser instead."
(bibtex-completion-open-pdf keys 'bibtex-completion-open-url-or-doi))
(defun bibtex-completion-format-citation-default (keys)
"Default formatter for keys, separates multiple keys with commas."
(s-join ", " keys))
(defvar bibtex-completion-cite-command-history nil
"History list for LaTeX citation commands.")
(defun bibtex-completion-format-citation-cite (keys)
"Formatter for LaTeX citation commands. Prompts for the command
and for arguments if the commands can take any. If point is
inside or just after a citation command, only adds KEYS to it."
(let (macro)
(cond
((and (require 'reftex-parse nil t)
(setq macro (reftex-what-macro 1))
(stringp (car macro))
(string-match "\\`\\\\cite\\|cite\\'" (car macro)))
;; We are inside a cite macro. Insert key at point, with appropriate delimiters.
(delete-horizontal-space)
(concat (pcase (preceding-char)
(?\{ "")
(?, " ")
(_ ", "))
(s-join ", " keys)
(if (member (following-char) '(?\} ?,))
""
", ")))
((and (equal (preceding-char) ?\})
(require 'reftex-parse nil t)
(save-excursion
(forward-char -1)
(setq macro (reftex-what-macro 1)))
(stringp (car macro))
(string-match "\\`\\\\cite\\|cite\\'" (car macro)))
;; We are right after a cite macro. Append key and leave point at the end.
(delete-char -1)
(delete-horizontal-space t)
(concat (pcase (preceding-char)
(?\{ "")
(?, " ")
(_ ", "))
(s-join ", " keys)
"}"))
(t
;; We are not inside or right after a cite macro. Insert a full citation.
(let* ((initial (when bibtex-completion-cite-default-as-initial-input
bibtex-completion-cite-default-command))
(default (unless bibtex-completion-cite-default-as-initial-input
bibtex-completion-cite-default-command))
(default-info (if default (format " (default \"%s\")" default) ""))
(cite-command (completing-read
(format "Cite command%s: " default-info)
bibtex-completion-cite-commands nil nil initial
'bibtex-completion-cite-command-history default nil)))
(if (member cite-command '("nocite" "supercite")) ; These don't want arguments.
(format "\\%s{%s}" cite-command (s-join ", " keys))
(let ((prenote (if bibtex-completion-cite-prompt-for-optional-arguments
(read-from-minibuffer "Prenote: ")
""))
(postnote (if bibtex-completion-cite-prompt-for-optional-arguments
(read-from-minibuffer "Postnote: ")
"")))
(if (and (string= "" prenote) (string= "" postnote))
(format "\\%s{%s}" cite-command (s-join ", " keys))
(format "\\%s[%s][%s]{%s}" cite-command prenote postnote (s-join ", " keys))))))))))
(defun bibtex-completion-format-citation-pandoc-citeproc (keys)
"Formatter for pandoc-citeproc citations."
(let* ((prenote (if bibtex-completion-cite-prompt-for-optional-arguments (read-from-minibuffer "Prenote: ") ""))
(postnote (if bibtex-completion-cite-prompt-for-optional-arguments (read-from-minibuffer "Postnote: ") ""))
(prenote (if (string= "" prenote) "" (concat prenote " ")))
(postnote (if (string= "" postnote) "" (concat ", " postnote))))
(format "[%s%s%s]" prenote (s-join "; " (--map (concat "@" it) keys)) postnote)))