forked from jkitchin/org-ref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
org-ref-core.el
3974 lines (3434 loc) · 130 KB
/
org-ref-core.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
;;; org-ref-core.el --- citations, cross-references and bibliographies in org-mode
;; Copyright(C) 2014-2017 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:
;;
;; Lisp code to setup bibliography, cite, ref and label org-mode links.
;; Also sets up reftex and helm for org-mode citations. The links are
;; clickable and do things that are useful. You should really read
;; org-ref.org in this package for details.
;;
;;; Code:
(eval-when-compile
(require 'cl-lib))
(require 'dash)
(require 'f)
(require 'htmlize)
(require 's)
(require 'doi-utils)
(add-to-list 'load-path
(expand-file-name
"citeproc"
(file-name-directory (or load-file-name (buffer-file-name)))))
(add-to-list 'load-path
(expand-file-name
"citeproc/csl"
(file-name-directory (or load-file-name (buffer-file-name)))))
(require 'org-ref-bibtex)
(require 'org-ref-utils)
(require 'org-ref-glossary)
(require 'org)
(require 'org-element)
(require 'ox)
(require 'parsebib)
(require 'reftex-cite)
(defvar org-export-exclude-tags)
(defvar warning-suppress-types)
(declare-function bibtex-completion-get-entry "bibtex-completion")
(declare-function bibtex-completion-edit-notes "bibtex-completion")
;;* Custom variables
(defgroup org-ref nil
"Customization group for org-ref."
:tag "Org Ref"
:group 'org)
(defcustom org-ref-bibliography-notes
nil
"Filename where you will put all your notes about an entry in the default bibliography.
Used by backends that append all notes as entries in a single file.
See also `org-ref-notes-function'"
:type '(choice (const nil)
(file))
:group 'org-ref)
(defcustom org-ref-notes-directory
nil
"Directory where you will put all your notes about an entry in the default bibliography.
Used for backends that create a single file of notes per entry.
See also `org-ref-notes-function'."
:type 'directory
:group 'org-ref)
(defcustom org-ref-default-bibliography
nil
"List of bibtex files to search for.
You should use full-paths for each file. Note that you must
include a bibliography link in your document if you will be
exporting it to pdf; org-ref-default-bibliography is not
used by the LaTeX exporter."
:type '(repeat :tag "List of bibtex files" file)
:group 'org-ref)
(defcustom org-ref-pdf-directory
nil
"Directory where pdfs are stored by key.
Put a trailing / in the name."
:type '(choice directory (repeat directory))
:group 'org-ref)
(defcustom org-ref-default-citation-link
"cite"
"The default type of citation link to use."
:type 'string
:group 'org-ref)
(defcustom org-ref-insert-cite-key
"C-c ]"
"Keyboard shortcut to insert a citation."
:type 'string
:group 'org-ref)
(defcustom org-ref-completion-library
'org-ref-helm-bibtex
"Symbol for library to define completion functions.
The completion library should provide functions for
`org-ref-insert-link-function', `org-ref-insert-cite-function',
`org-ref-insert-label-function', `org-ref-insert-ref-function',
and `org-ref-cite-onclick-function', and set those variables to
the values of those functions."
:type 'symbol
:options '(org-ref-helm-bibtex ; completion with helm + helm-bibtex
org-ref-helm-cite ; completion with helm in org-ref
org-ref-ivy-cite ; completion with ivy
org-ref-reftex ; org-completion
)
:group 'org-ref)
(defcustom org-ref-insert-link-function
nil
"Generic function for inserting org-ref links.
The function should take a prefix arg.
No arg means insert a cite link
1 arg means insert a ref link
2 args means insert a label."
:type 'function
:group 'org-ref)
(defcustom org-ref-insert-cite-function
nil
"Function to call to insert citation links.
This function should prompt for keys with completion, and insert
the citation link into the buffer."
:type 'function
:group 'org-ref)
(defcustom org-ref-prefer-bracket-links nil
"If non-nil use bracketed links when inserting them."
:type 'boolean
:group 'org-ref)
(defcustom org-ref-cite-completion-function
nil
"Function to prompt for keys with completion."
:type '(choice (const nil)
(function))
:group 'org-ref)
(defcustom org-ref-insert-label-function
nil
"Function to call to insert label links.
This function should prompt for a label, and insert the label
link."
:type 'function
:group 'org-ref)
(defcustom org-ref-insert-ref-function
nil
"Function to call to insert ref links.
This function should prompt for a label with completion, and
insert the ref link."
:type 'function
:group 'org-ref)
(defcustom org-ref-cite-onclick-function
nil
"Function that runs when you click on a cite link.
The function must take one argument which is the path of the link
that was clicked on. This function is normally set by the
function in `org-ref-completion-library'."
:type 'function
:group 'org-ref)
;; define key for inserting citations
(define-key org-mode-map
(kbd org-ref-insert-cite-key)
org-ref-insert-link-function)
(defcustom org-ref-cite-keymap
(let ((map (copy-keymap org-mouse-map)))
(define-key map (kbd "H-o") 'org-ref-cite-hydra/body)
(define-key map (kbd "H-b") 'org-ref-open-citation-at-point)
(define-key map (kbd "H-u") 'org-ref-open-url-at-point)
(define-key map (kbd "H-p") 'org-ref-open-pdf-at-point)
(define-key map (kbd "H-n") 'org-ref-open-notes-at-point)
(define-key map (kbd "H-r") 'org-ref-wos-related-at-point)
(define-key map (kbd "H-c") 'org-ref-wos-citing-at-point)
(define-key map (kbd "H-e") (lambda ()
"Email entry at point"
(interactive)
(org-ref-open-citation-at-point)
(org-ref-email-bibtex-entry)))
(define-key map (kbd "H-g") 'org-ref-google-scholar-at-point)
(define-key map (kbd "H-f") (lambda ()
(interactive)
(save-excursion
(org-ref-open-citation-at-point)
(kill-new
(org-ref-format-bibtex-entry-at-point)))))
(define-key map (kbd "H-w") (lambda ()
(interactive)
(kill-new (car (org-ref-get-bibtex-key-and-file)))))
(define-key map (kbd "H-W") (lambda ()
"Copy all the keys at point."
(interactive)
(kill-new (org-element-property :path (org-element-context)))))
(define-key map (kbd "H-y") (lambda ()
"Paste key at point. Assumes the first thing in the killring is a key."
(interactive)
(org-ref-insert-key-at-point (car kill-ring))))
;; Navigation keys
(define-key map (kbd "C-<left>") 'org-ref-previous-key)
(define-key map (kbd "C-<right>") 'org-ref-next-key)
;; rearrangement keys
(define-key map (kbd "S-<left>") (lambda () (interactive) (org-ref-swap-citation-link -1)))
(define-key map (kbd "S-<right>") (lambda () (interactive) (org-ref-swap-citation-link 1)))
(define-key map (kbd "S-<up>") 'org-ref-sort-citation-link)
map)
"Keymap for cite links."
:type 'symbol
:group 'org-ref)
(defcustom org-ref-bibliography-entry-format
'(("article" . "%a, %t, <i>%j</i>, <b>%v(%n)</b>, %p (%y). <a href=\"%U\">link</a>. <a href=\"http://dx.doi.org/%D\">doi</a>.")
("book" . "%a, %t, %u (%y).")
("techreport" . "%a, %t, %i, %u (%y).")
("proceedings" . "%e, %t in %S, %u (%y).")
("inproceedings" . "%a, %t, %p, in %b, edited by %e, %u (%y)"))
"String to format an entry.
Just the reference, no numbering at the beginning, etc... see the
`org-ref-reftex-format-citation' docstring for the escape codes."
:type '(alist :key-type (string) :value-type (string))
:group 'org-ref)
(defcustom org-ref-note-title-format
"** TODO %y - %t
:PROPERTIES:
:Custom_ID: %k
:AUTHOR: %9a
:JOURNAL: %j
:YEAR: %y
:VOLUME: %v
:PAGES: %p
:DOI: %D
:URL: %U
:END:
"
"String to format the title and properties drawer of a note.
See the `org-ref-reftex-format-citation' docstring for the escape
codes."
:type 'string
:group 'org-ref)
(defcustom org-ref-ref-html "<a class='org-ref-reference' href=\"#%s\">%s</a>"
"HTML code to represent a reference."
:type 'string
:group 'org-ref)
(defcustom org-ref-notes-function #'org-ref-notes-function-one-file
"Function to open the notes for the bibtex key in a cite link at point.
The default behavior adds entries to a long file with headlines
for each entry. It also tries to be compatible with `org-bibtex'.
An alternative is `org-ref-notes-function-many-files'. Use that
if you prefer the `bibtex-completion' approach, which also
supports an additional method for storing notes. See
`bibtex-completion-notes-path' for more information. You may also
want to set `org-ref-notes-directory'."
:type 'function
:group 'org-ref)
(defcustom org-ref-open-notes-function
(lambda ()
(org-show-entry)
(outline-show-branches)
(outline-show-children)
(org-cycle '(64))
(recenter-top-bottom 0))
"User-defined way to open a notes entry.
This is executed after the entry is found in
`org-ref-open-bibtex-notes', with the cursor at the beginning of
the headline. The default setting fully expands the notes, and
moves the headline to the top of the buffer."
:type 'function
:group 'org-ref)
(defcustom org-ref-create-notes-hook
'((lambda ()
(org-narrow-to-subtree)
(insert (format "cite:%s\n" (org-entry-get (point) "Custom_ID")))))
"List of hook functions to run in the note entry after it is created.
The function takes no arguments. It could be used to insert links
to the citation, or pdf, etc..."
:type 'hook
:group 'org-ref)
(defcustom org-ref-open-pdf-function
'org-ref-open-pdf-at-point
"User-defined function to open a pdf from a link.
The function must get the key at point, and derive a path to the pdf
file, then open it. The default function is
`org-ref-open-pdf-at-point'."
:type 'function
:group 'org-ref)
(defcustom org-ref-get-pdf-filename-function
'org-ref-get-pdf-filename
"User-defined function to get a filename from a bibtex key.
The function must take a key as an argument, and return the path
to the corresponding filename. The default is
`org-ref-get-pdf-filename'. Alternative values are
`org-ref-get-mendeley-filename' or
`org-ref-get-pdf-filename-helm-bibtex'."
:type 'function
:group 'org-ref)
(defcustom org-ref-clean-bibtex-key-function
(lambda (key)
(replace-regexp-in-string ":" "" key))
"Function to modify a bibtex key.
The default behavior is to remove : from the key."
:type 'function
:group 'org-ref)
(defcustom org-ref-show-citation-on-enter t
"If non-nil show the citation summary.
Uses a hook function to display the message in the minibuffer."
:type 'boolean
:group 'org-ref)
(defcustom org-ref-natbib-types
'("citet" "citet*" "citep" "citep*"
"citealt" "citealt*" "citealp" "citealp*"
"citenum" "citetext"
"citeauthor" "citeauthor*"
"citeyear" "citeyear*" "citeyearpar"
"Citet" "Citep" "Citealt" "Citealp" "Citeauthor")
"natbib cite commands, http://tug.ctan.org/macros/latex/contrib/natbib/natnotes.pdf"
:type '(repeat :tag "List of citation types" string)
:group 'org-ref)
(defcustom org-ref-biblatex-types
'("Cite"
"parencite" "Parencite"
"footcite" "footcitetext"
"textcite" "Textcite"
"smartcite" "Smartcite"
"cite*" "parencite*" "supercite"
"autocite" "Autocite" "autocite*" "Autocite*"
"Citeauthor*"
"citetitle" "citetitle*"
"citedate" "citedate*"
"citeurl"
"fullcite" "footfullcite"
;; "volcite" "Volcite" cannot support the syntax
"notecite" "Notecite"
"pnotecite" "Pnotecite"
"fnotecite"
;; multicites. Very limited support for these.
"cites" "Cites" "parencites" "Parencites"
"footcites" "footcitetexts"
"smartcites" "Smartcites" "textcites" "Textcites"
"supercites" "autocites" "Autocites")
"biblatex commands
http://ctan.mirrorcatalogs.com/macros/latex/contrib/biblatex/doc/biblatex.pdf"
:type '(repeat :tag "List of citation types" string)
:group 'org-ref)
(defcustom org-ref-cite-types
(append
'("cite" "nocite") ;; the default latex cite commands
org-ref-natbib-types
org-ref-biblatex-types
;; for the bibentry package
'("bibentry"))
"List of citation types known in `org-ref'."
:type '(repeat :tag "List of citation types" string)
:group 'org-ref)
(defcustom org-ref-ref-types
'("ref" "eqref" "pageref" "nameref" "autoref" "cref" "Cref")
"List of ref link types."
:type '(repeat :tag "List of ref types" string)
:group 'org-ref)
(defcustom org-ref-default-ref-type "ref"
"Default ref link type to use when inserting ref links"
:type 'string
:group 'org-ref)
(defcustom org-ref-clean-bibtex-entry-hook
'(org-ref-bibtex-format-url-if-doi
orcb-key-comma
org-ref-replace-nonascii
orcb-&
orcb-%
org-ref-title-case-article
orcb-clean-year
orcb-key
orcb-clean-doi
orcb-clean-pages
orcb-check-journal
org-ref-sort-bibtex-entry
orcb-fix-spacing)
"Hook that is run in `org-ref-clean-bibtex-entry'.
The functions should have no arguments, and
operate on the bibtex entry at point. You can assume point starts
at the beginning of the entry. These functions are wrapped in
`save-restriction' and `save-excursion' so you do not need to
save the point position.
Org ref contains some functions that are not included by default
such as `orcb-clean-nil' or `orcb-clean-nil-opinionated' that
users may be interested in adding themselves."
:group 'org-ref
:type 'hook)
(defcustom org-ref-bibtex-sort-order
'(("article" . ("author" "title" "journal" "volume" "number" "pages" "year" "doi" "url"))
("inproceedings" . ("author" "title" "booktitle" "year" "volume" "number" "pages" "doi" "url"))
("book" . ("author" "title" "year" "publisher" "url")))
"A-list of bibtex entry fields and the order to sort an entry with.
\(entry-type . (list of fields). This is used in
`org-ref-sort-bibtex-entry'. Entry types not listed here will
have fields sorted alphabetically."
:type '(alist :key-type (string) :value-type (repeat string))
:group 'org-ref)
(defcustom org-ref-printbibliography-cmd "\\printbibliography"
"LaTeX command to print bibliography. Customize this to add options."
:type 'string
:group 'org-ref)
(defvar org-ref-bibliography-files
nil
"Variable to hold bibliography files to be searched.")
(defcustom org-ref-show-broken-links t
"If non-nil show bad org-ref links in a warning face."
:type 'boolean
:group 'org-ref)
;;* Messages for link at cursor
(defvar org-ref-message-timer nil
"Variable to store the link message timer in.")
;;;###autoload
(defun org-ref-show-link-messages ()
"Turn on link messages.
You will see a message in the minibuffer when on a cite, ref or
label link."
(interactive)
(or org-ref-message-timer
(setq org-ref-message-timer
(run-with-idle-timer 0.5 t 'org-ref-link-message)
org-ref-show-citation-on-enter t)))
;;;###autoload
(defun org-ref-cancel-link-messages ()
"Stop showing messages in minibuffer when on a link."
(interactive)
(cancel-timer org-ref-message-timer)
(setq org-ref-message-timer nil
org-ref-show-citation-on-enter nil))
(when org-ref-show-citation-on-enter
(org-ref-show-link-messages))
;;;###autoload
(defun org-ref-change-completion ()
"Change the completion backend.
Options are \"org-ref-helm-bibtex\", \"org-ref-helm-cite\",
\"org-ref-ivy-cite\" and \"org-ref-reftex\"."
(interactive)
(require
(intern
(completing-read "Backend: " '("org-ref-helm-bibtex"
"org-ref-helm-cite"
"org-ref-ivy-cite"
"org-ref-reftex")
nil
t
"org-ref-helm-cite"))))
;;** Messages for context under mouse pointer
(defvar org-ref-last-mouse-pos nil
"Stores last mouse position for use in `org-ref-mouse-message'.")
(defun org-ref-can-move-p ()
"See if a character is under the mouse.
If so return the position for `goto-char'."
(let* ((line (cddr org-ref-last-mouse-pos))
(col (cadr org-ref-last-mouse-pos)))
(save-excursion
(goto-char (window-start))
(forward-line line)
(if
(> (- (line-end-position) (line-beginning-position)) col)
(progn (forward-char col) (point))
nil))))
;;;###autoload
(defun org-ref-mouse-message ()
"Display message for link under mouse cursor."
(interactive)
(when (not (equal (mouse-position) org-ref-last-mouse-pos))
(setq org-ref-last-mouse-pos (mouse-position))
(let ((p (org-ref-can-move-p)))
(when p
(save-excursion
(goto-char p)
(org-ref-link-message))))))
(defvar org-ref-message-timer-mouse nil
"Store mouse timer.")
(defvar org-ref-mouse-message-interval 0.5
"How often to run the mouse message timer in seconds.")
;;;###autoload
(defun org-ref-mouse-messages-on ()
"Turn on mouse messages."
(interactive)
(or org-ref-message-timer-mouse
(setq org-ref-message-timer-mouse
(run-at-time "0.5 sec"
org-ref-mouse-message-interval
'org-ref-mouse-message))))
;;;###autoload
(defun org-ref-mouse-messages-off ()
"Turn off mouse messages."
(interactive)
(cancel-timer org-ref-message-timer-mouse)
(setq org-ref-message-timer-mouse nil)
(message "Mouse messages are off"))
;;* font lock for org-ref
(defcustom org-ref-colorize-links
t
"When non-nil, change colors of links."
:type 'boolean
:group 'org-ref)
(defcustom org-ref-cite-color
"forest green"
"Color of cite like links."
:type 'string
:group 'org-ref)
(defcustom org-ref-ref-color
"dark red"
"Color of ref like links."
:type 'string
:group 'org-ref)
(defcustom org-ref-label-color
"dark magenta"
"Color of label links."
:type 'string
:group 'org-ref)
(defvar org-ref-cite-re
(concat "\\(" (mapconcat
(lambda (x)
(replace-regexp-in-string "\*" "\\\\*" x))
org-ref-cite-types "\\|") "\\):"
"\\([a-zA-Z0-9-_:\\./]+,?\\)+")
"Regexp for cite links.
Group 1 contains the cite type.
Group 2 contains the keys.")
(defvar org-ref-label-re
"label:\\([a-zA-Z0-9-_:]+,?\\)+"
"Regexp for label links.")
(defvar org-ref-ref-re
"\\(eq\\)?ref:\\([a-zA-Z0-9-_:]+,?\\)+"
"Regexp for ref links.")
(defface org-ref-cite-face
`((t (:inherit org-link
:foreground ,org-ref-cite-color)))
"Color for cite-like links in org-ref.")
(defface org-ref-label-face
`((t (:inherit org-link :foreground ,org-ref-label-color)))
"Color for ref links in org-ref.")
(defface org-ref-ref-face
`((t (:inherit org-link :foreground ,org-ref-ref-color)))
"Face for ref links in org-ref.")
;;** Font-lock org-ref links
;; We use functions to search for the next link, and then use org-mode to find
;; the boundaries. I wasn't able to figure out robust regexps for these links
;; that includes all possible link syntaxes eg. bare, [[cite:key]] and
;; [[cite:key] [text]]. Using regexps might be a bit more efficient, so if they
;; ever get figured out, we could eliminate the org-element code in these
;; functions.
;; These functions are not used with org-9. I define them here to make
;; byte-compiling quiet.
(defun org-ref-match-next-cite-link (_) nil)
(defun org-ref-match-next-label-link (_) nil)
(defun org-ref-match-next-ref-link (_) nil)
(defun org-ref-make-org-link-cite-key-visible (_) nil)
(when (not (fboundp 'org-link-set-parameters))
(defun org-ref-match-next-cite-link (&optional limit)
"Search forward to next cite link up to LIMIT
Add a tooltip to the match."
(when (and (re-search-forward org-ref-cite-re limit t)
(not (org-in-src-block-p))
(not (org-at-comment-p)))
;; we think we are on a cite link lets get on it and make sure
(forward-char -2)
(let ((this-link (org-element-context)))
(if (-contains? org-ref-cite-types (org-element-property :type this-link))
;; we are on a cite link
(progn
(when org-ref-show-citation-on-enter
(add-text-properties
(org-element-property :begin this-link)
(- (org-element-property :end this-link)
(org-element-property :post-blank this-link))
(list
'help-echo (lambda (window object position)
(save-excursion
(goto-char position)
;; Here we wrap the citation string to a reasonable size.
(let ((s (org-ref-format-entry
(org-ref-get-bibtex-key-under-cursor))))
(with-temp-buffer
(insert s)
(fill-paragraph)
(buffer-string))))))))
(set-match-data
(list (org-element-property :begin this-link)
(- (org-element-property :end this-link)
(org-element-property :post-blank this-link))))
(goto-char (org-element-property :end this-link)))
;; Must be a false match.
;; somehow were not on a
;; cite link, so we try
;; again.
(org-ref-match-next-cite-link limit)))))
(defun org-ref-match-next-label-link (limit)
"Find next label link up to LIMIT.
Add tooltip."
(if (and (re-search-forward "label:\\([[:alnum:]]\\)\\{2,\\}" limit t)
(not (org-in-src-block-p))
(not (org-at-comment-p)))
(progn
(forward-char -2)
(let ((this-link (org-element-context)))
(if (string= "label" (org-element-property :type this-link))
;; on a label
(progn
(add-text-properties
(org-element-property :begin this-link)
(- (org-element-property :end this-link)
(org-element-property :post-blank this-link))
(list
'help-echo (lambda (window object position)
(save-excursion
(goto-char position)
(let ((s (org-ref-link-message)))
(with-temp-buffer
(insert s)
(fill-paragraph)
(buffer-string)))))))
(set-match-data
(list (org-element-property :begin this-link)
(- (org-element-property :end this-link)
(org-element-property :post-blank this-link))))
(goto-char (org-element-property :end this-link)))
;; false match
(org-ref-match-next-label-link limit))))))
(defun org-ref-match-next-ref-link (limit)
"Find next ref link up to LIMIT.
Add tooltip to the link. We avoid tags by not finding :ref: in
tags."
(when (and (re-search-forward "[^:]\\(eq\\)?ref:\\([[:alnum:]]\\)\\{2,\\}" limit t)
(not (org-in-src-block-p))
(not (org-at-comment-p)))
;; we think we are on a ref link, lets make sure.
(forward-char -2)
(let ((this-link (org-element-context)))
(if (-contains? org-ref-ref-types
(org-element-property :type this-link))
;; we are, so we do our business
(progn
(add-text-properties
(org-element-property :begin this-link)
(- (org-element-property :end this-link)
(org-element-property :post-blank this-link))
(list
'help-echo (lambda (window object position)
(save-excursion
(goto-char position)
(let ((s (org-ref-link-message)))
(with-temp-buffer
(insert s)
(fill-paragraph)
(buffer-string)))))))
(set-match-data
(list (org-element-property :begin this-link)
(- (org-element-property :end this-link)
(org-element-property :post-blank this-link))))
(goto-char (org-element-property :end this-link)))
;; False match, let's try again
(org-ref-match-next-ref-link limit)))))
(defun org-ref-match-next-bibliography-link (limit)
"Find next bibliography link up to LIMIT.
Add tooltip to the link."
(when (and (re-search-forward "bibliography:\\([[:alnum:]]\\)\\{2,\\}" limit t)
(not (org-in-src-block-p))
(not (org-at-comment-p)))
(forward-char -2)
(let ((this-link (org-element-context)))
(add-text-properties
(org-element-property :begin this-link)
(- (org-element-property :end this-link)
(org-element-property :post-blank this-link))
(list
'help-echo (lambda (window object position)
(save-excursion
(goto-char position)
(let ((s (org-ref-link-message)))
(with-temp-buffer
(insert s)
(fill-paragraph)
(buffer-string)))))))
(set-match-data
(list (org-element-property :begin this-link)
(- (org-element-property :end this-link)
(org-element-property :post-blank this-link))))
(goto-char (org-element-property :end this-link)))))
(defun org-ref-match-next-bibliographystyle-link (limit)
"Find next bibliographystyle link up to LIMIT.
Add tooltip to the link."
(when (and (re-search-forward "bibliographystyle:\\([[:alnum:]]\\)\\{2,\\}" limit t)
(not (org-in-src-block-p))
(not (org-at-comment-p)))
(forward-char -2)
(let* ((this-link (org-element-context))
(path (org-element-property :path this-link))
(msg (shell-command-to-string (format "kpsewhich %s.bst" path))))
(add-text-properties
(org-element-property :begin this-link)
(- (org-element-property :end this-link)
(org-element-property :post-blank this-link))
(list
'help-echo msg))
(set-match-data
(list (org-element-property :begin this-link)
(- (org-element-property :end this-link)
(org-element-property :post-blank this-link))))
(goto-char (org-element-property :end this-link)))))
(defun org-ref-make-org-link-cite-key-visible (&rest _)
"Make the org-ref cite link visible in descriptive links."
(when (string-match-p "\\.org$\\|\\.txt$" (buffer-name))
(save-match-data
(let ((s (match-string 1))
(s-begin (match-beginning 1))
(s-end (match-end 1))
(beg (match-beginning 0))
(end (match-end 0))
(cite-re (format "^\\(%s:\\)"
(regexp-opt (-sort
(lambda (a b)
(> (length a) (length b)))
org-ref-cite-types))))
cite-type)
(when (and s (string-match cite-re s))
(setq cite-type (match-string 1 s))
(remove-text-properties beg end
'(invisible))
(add-text-properties
beg end
`(face (:foreground ,org-ref-cite-color))))))))
(when org-ref-colorize-links
(add-hook
'org-mode-hook
(lambda ()
(advice-add 'org-activate-bracket-links :after #'org-ref-make-org-link-cite-key-visible)
(font-lock-add-keywords
nil
'((org-ref-match-next-cite-link (0 'org-ref-cite-face t))
(org-ref-match-next-label-link (0 'org-ref-label-face t))
(org-ref-match-next-ref-link (0 'org-ref-ref-face t))
(org-ref-match-next-bibliography-link (0 'org-link t))
(org-ref-match-next-bibliographystyle-link (0 'org-link t)))
t)))))
;;* Links
;;** bibliography and bibliographystyle
(defun org-ref-open-bibliography-no-org (link-string)
"Open a bibliography link when you are not in org-mode.
This means you cannot use the usual org-machinery to figure it
out. We don't try to be clever here. If there is only one file,
we open it, otherwise prompt for which one to open."
(let ((bibfiles (split-string link-string ",")))
(find-file (if (= 1 (length bibfiles))
(car bibfiles)
(completing-read
"Bib file: " bibfiles nil t)))))
(defun org-ref-bibinputs ()
"Feed BIBINPUTS environment variable to `parse-colon-path'."
(parse-colon-path (getenv "BIBINPUTS")))
(defun org-ref-bibfile-kpsewhich (bibfile)
"Try to find BIBFILE using kpsewhich."
(let ((f (replace-regexp-in-string
"\n$" ""
(shell-command-to-string (format "kpsewhich %s" bibfile)))))
(unless (string= "" f)
f)))
(defun org-ref-find-bibfile (bibfile)
"Find BIBFILE as local file, or using kpsewhich or bibinputs."
(or (if (file-exists-p bibfile) bibfile)
(org-ref-bibfile-kpsewhich bibfile)
;; this should never be reached if bibfile exists, because kpsewhich is
;; stronger
(org-ref-locate-file bibfile (org-ref-bibinputs))))
(defun org-ref-locate-file (filename path)
"Search for FILENAME through PATH.
Like `locate-file-internal', but with `file-exists-p' as
PREDICATE."
(locate-file-internal filename path () #'file-exists-p))
(defun org-ref-open-bibliography (link-string)
"The click function for a bibliography link."
;; get link-string boundaries we have to go to the
;; beginning of the line, and then search forward
(if (not (eq major-mode 'org-mode))
(org-ref-open-bibliography-no-org link-string)
(let* ((bibfile)
;; object is the link you clicked on
(object (org-element-context))
(link-string-beginning)
(link-string-end)
(cp (point)))
(save-excursion
(goto-char (org-element-property :begin object))
(search-forward link-string nil nil 1)
(setq link-string-beginning (match-beginning 0))
(setq link-string-end (match-end 0)))
;; Make sure point is in the link-path.
(if (< cp link-string-beginning)
(goto-char link-string-beginning))
;; We set the reftex-default-bibliography
;; here. it should be a local variable only in
;; the current buffer. We need this for using
;; reftex to do citations.
(set (make-local-variable 'reftex-default-bibliography)
(split-string
(org-element-property :path object) ","))
(let (key-beginning key-end)
;; now if we have comma separated bibliographies
;; we find the one clicked on. we want to
;; search forward to next comma from point
(save-excursion
(if (search-forward "," link-string-end 1 1)
;; we found a match
(setq key-end (- (match-end 0) 1))
;; no comma found so take the point
(setq key-end (point))))
;; and backward to previous comma from point
(save-excursion
(if (search-backward "," link-string-beginning 1 1)
;; we found a match
(setq key-beginning (+ (match-beginning 0) 1))
(setq key-beginning (point)))) ; no match found
;; save the key we clicked on.
(setq bibfile (org-ref-strip-string
(buffer-substring key-beginning key-end)))
;; open file on click. I use or because org-ref-find-bibfile returns nil
;; if the file doesn't exist, and clicking should open the file in that
;; case.
(find-file (or (org-ref-find-bibfile bibfile) bibfile))))))
(defun org-ref-bibliography-format (keyword desc format)
"Formatting function for bibliography links."
(cond
((eq format 'org) (org-ref-get-org-bibliography))
((eq format 'ascii) (org-ref-get-ascii-bibliography))
((eq format 'md) (org-ref-get-md-bibliography))
((eq format 'odt) (org-ref-get-odt-bibliography))
((eq format 'html) (org-ref-get-html-bibliography))
((eq format 'latex)
;; write out the latex bibliography command