forked from jkitchin/org-ref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg-ref.el
4614 lines (4029 loc) · 169 KB
/
org-ref.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.el --- citations, cross-references and bibliographies in org-mode
;; Copyright(C) 2014,2015 John Kitchin
;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
;; URL: https://github.com/jkitchin/org-ref
;; Version: 0.6.0
;; Keywords: org-mode, cite, ref, label
;; Package-Requires: ((dash "2.11.0") (helm "1.5.5") (helm-bibtex "1.0.0") (hydra "0.13.2") (key-chord "0") (s "1.10.0") (f "0.18.0") (emacs "24.4"))
;; 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))
(require 'dash)
(require 'doi-utils)
(require 'f)
(require 'helm-config)
(require 'helm)
(require 'helm-bibtex)
(require 'hydra)
(require 'org-ref-bibtex)
(require 'org)
(require 'org-element)
(require 'ox)
(require 'reftex)
(require 'reftex-cite)
(require 'org-ref-glossary)
;;for byte-compile error avoidance
(defvar-local org-export-exclude-tags nil)
(declare-function 'org-ref-email-bibtex-entry "org-ref-bibtex.el")
;;* Custom variables
(defgroup org-ref nil
"Customization group for org-ref."
:tag "Org Ref"
:group 'org)
(defcustom org-ref-clean-bibtex-key-function
(lambda (key)
(replace-regexp-in-string ":" "" key))
"Clean a bibtex key from unwanted characters"
:type 'function
:group 'org-ref)
(defcustom org-ref-bibliography-notes
nil
"File or directory where notes about an entry are saved. This
can be either an org file where notes are saved as headings, or a
directory where notes are saved as separate org files."
:type 'file
:group 'org-ref)
(defcustom org-ref-default-bibliography
nil
"List of bibtex files to search for.
You should use full-paths for each file."
: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."
:type '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-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 '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-notes-function
(lambda (thekey)
(let* ((results (org-ref-get-bibtex-key-and-file thekey))
(key (car results))
(bibfile (cdr results)))
(save-excursion
(with-temp-buffer
(insert-file-contents bibfile)
(bibtex-set-dialect (parsebib-find-bibtex-dialect) t)
(bibtex-search-entry key)
(org-ref-open-bibtex-notes)))))
"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
(lambda ()
(helm-bibtex-edit-notes (car (org-ref-get-bibtex-key-and-file thekey))))
Use that if you like the one file one note approach of `helm-bibtex'."
:type 'function
:group 'org-ref)
(defcustom org-ref-open-notes-function
(lambda ()
(org-show-entry)
(show-branches)
(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-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'. An alternative value is
`org-ref-get-mendeley-filename'."
:type 'function
:group 'org-ref)
(defcustom org-ref-helm-bibtex-format-org
'org-ref-helm-bibtex-format-org
"Function for how `helm-bibtex' inserts citations."
:type 'function
:group 'org-ref)
(setf (cdr (assoc 'org-mode helm-bibtex-format-citation-functions))
org-ref-helm-bibtex-format-org)
(defcustom org-ref-helm-bibtex-actions
'(("Insert citation" . helm-bibtex-insert-citation)
("Open PDF file (if present)" . helm-bibtex-open-pdf)
("Open URL or DOI in browser" . helm-bibtex-open-url-or-doi)
("Insert reference" . helm-bibtex-insert-reference)
("Insert BibTeX key" . helm-bibtex-insert-key)
("Insert BibTeX entry" . helm-bibtex-insert-bibtex)
("Attach PDF to email" . helm-bibtex-add-PDF-attachment)
("Edit notes" . helm-bibtex-edit-notes)
("Show entry" . helm-bibtex-show-entry)
("Add keywords to entries" . org-ref-helm-tag-entries)
("Copy entry to clipboard" . helm-bibtex-copy-candidate)
("Add keywords to entries" . org-ref-helm-tag-entries)
("Add keywords to entries" . org-ref-helm-tag-entries))
"Cons cells of string and function to set the actions of `helm-bibtex' to.
The car of cons cell is the string describing the function.
The cdr of the the cons cell is the function to use."
:type 'list
:group 'org-ref)
(cl-loop for i from 0 to (length org-ref-helm-bibtex-actions)
for ccell in org-ref-helm-bibtex-actions
do
(helm-delete-action-from-source (car ccell) helm-source-bibtex)
(helm-add-action-to-source
(car ccell)
(cdr ccell)
helm-source-bibtex))
(defcustom org-ref-insert-cite-function
'org-ref-helm-insert-cite-link
"Function to call to insert citation links.
The default is `org-ref-helm-insert-cite-link' which uses
`helm-bibtex'. `org-ref' modifies `helm-bibtex' a little bit to
give `org-mode' citations, and to reorder default actions. You
may use `org-ref-insert-cite-link' if you like the reftex
interface. Finally, you may like `org-ref-helm-cite' which is an
alternative to `helm-bibtex' that shows full citations and has
sortable candidates."
:type 'function
:group 'org-ref)
(defcustom org-ref-cite-onclick-function
'org-ref-cite-click-helm
"Function that runs when you click on a cite link.
The function must take no arguments. You may also use
`org-ref-cite-onclick-minibuffer-menu' if you do not like helm.
If you like `hydra', consider using `org-ref-cite-hydra'."
: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-cite-types
'("cite" "nocite" ;; the default latex cite commands
;; natbib cite commands, http://ctan.unixbrain.com/macros/latex/contrib/natbib/natnotes.pdf
"citet" "citet*" "citep" "citep*"
"citealt" "citealt*" "citealp" "citealp*"
"citenum" "citetext"
"citeauthor" "citeauthor*"
"citeyear" "citeyear*"
"Citet" "Citep" "Citealt" "Citealp" "Citeauthor"
;; biblatex commands
;; http://ctan.mirrorcatalogs.com/macros/latex/contrib/biblatex/doc/biblatex.pdf
"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"
;; 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-clean-bibtex-entry-hook
'(orcb-key-comma
org-ref-replace-nonascii
orcb-&
org-ref-title-case-article
orcb-key
orcb-clean-doi
orcb-clean-year
orcb-clean-pages
org-ref-sort-bibtex-entry)
"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."
: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 'list)
:group 'org-ref)
(defvar org-ref-bibliography-files
nil
"Variable to hold bibliography files to be searched.")
;;* org-mode / reftex setup
(defun org-mode-reftex-setup ()
"Setup `org-mode' and reftex for `org-ref'."
(and (buffer-file-name)
(file-exists-p (buffer-file-name))
(global-auto-revert-mode t))
(make-local-variable 'reftex-cite-format)
(setq reftex-cite-format 'org))
;; define key for inserting citations
(define-key org-mode-map
(kbd org-ref-insert-cite-key)
org-ref-insert-cite-function)
(add-hook 'org-mode-hook 'org-mode-reftex-setup)
(eval-after-load 'reftex-vars
'(progn
(add-to-list 'reftex-cite-format-builtin
'(org "Org-mode citation"
((?\C-m . "cite:%l") ; default
(?d . ",%l") ; for appending
(?a . "autocite:%l")
(?t . "citet:%l")
(?T . "citet*:%l")
(?p . "citep:%l")
(?P . "citep*:%l")
(?h . "citeauthor:%l")
(?H . "citeauthor*:%l")
(?y . "citeyear:%l")
(?x . "citetext:%l")
(?n . "nocite:%l"))))))
;;;###autoload
(defun org-ref-version ()
"Provide a version string for org-ref.
Copies the string to the clipboard."
(interactive)
;; version in the el file.
(let* ((org-ref-el (concat
(file-name-sans-extension
(locate-library "org-ref"))
".el"))
(org-ref-dir (file-name-directory org-ref-el))
org-version
git-commit
version-string)
(setq org-version (with-temp-buffer
(insert-file-contents org-ref-el)
(goto-char (point-min))
(re-search-forward ";; Version:")
(s-trim (buffer-substring (point)
(line-end-position)))))
(setq git-commit
;; If in git, get current commit
(let ((default-directory org-ref-dir))
(when (= 0 (shell-command "git rev-parse --git-dir"))
(s-trim (shell-command-to-string "git rev-parse HEAD")))))
(setq version-string (format "org-ref: Version %s%s" org-version
(if git-commit
(format " (git-commit %s)" git-commit)
"")))
(kill-new version-string)
(message version-string)))
(defun org-ref-report-issue ()
"Report an issue in org-ref.
Opens https://github.com/jkitchin/org-ref/issues/new."
(save-window-excursion
(org-ref-debug)
(kill-new (buffer-string)))
(message "org-ref-debug has been run. You can paste the results in the issue website if you like.")
(browse-url "https://github.com/jkitchin/org-ref/issues/new"))
;;* 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))))
;;;###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))
(when org-ref-show-citation-on-enter
(org-ref-show-link-messages))
;;** 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.")
(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.
(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)
;; make sure we are not in a comment
(save-excursion
(beginning-of-line)
(not (looking-at "# "))))
;; 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
(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-get-citation-string-at-point)))
(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)
;; make sure we are not in a comment
(save-excursion
(beginning-of-line)
(not (looking-at "# "))))
(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 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 "[^:]ref:\\([[:alnum:]]\\)\\{2,\\}" limit t)
;; make sure we are not on a comment
(save-excursion
(beginning-of-line)
(not (looking-at "# "))))
;; we think we are on a ref link, lets make sure.
(forward-char -2)
(let ((this-link (org-element-context)))
(if (string= "ref" (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 (re-search-forward "bibliography:\\([[:alnum:]]\\)\\{2,\\}" limit t)
(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 (re-search-forward "bibliographystyle:\\([[:alnum:]]\\)\\{2,\\}" limit t)
(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)))))
(when org-ref-colorize-links
(add-hook
'org-mode-hook
(lambda ()
(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))))
;;* General org-ref utilities
(defun org-ref-strip-string (string)
"Strip leading and trailing whitespace from the STRING."
(replace-regexp-in-string
(concat search-whitespace-regexp "$" ) ""
(replace-regexp-in-string
(concat "^" search-whitespace-regexp ) "" string)))
(defun org-ref-split-and-strip-string (string)
"Split key-string and strip keys in STRING.
Assumes the key-string is comma delimited."
(mapcar 'org-ref-strip-string (split-string string ",")))
(defun org-ref-reftex-get-bib-field (field entry &optional format)
"Get FIELD from a bibtex ENTRY in optional FORMAT.
Similar to `reftex-get-bib-field', but removes enclosing braces
and quotes in FIELD in the bibtex ENTRY."
(let ((result))
(setq result (reftex-get-bib-field field entry format))
(when (and (not (string= result "")) (string= "{" (substring result 0 1)))
(setq result (substring result 1 -1)))
(when (and (not (string= result "")) (string= "\"" (substring result 0 1)))
(setq result (substring result 1 -1)))
result))
(defun org-ref-reftex-format-citation (entry format)
"Format the bibtex ENTRY according to the FORMAT argument.
ENTRY is from `bibtex-parse-entry'
The FORMAT is a string with these percent escapes.
In the format, the following percent escapes will be expanded.
%l The BibTeX label of the citation.
%a List of author names, see also `reftex-cite-punctuation'.
%2a Like %a, but abbreviate more than 2 authors like Jones et al.
%A First author name only.
%e Works like %a, but on list of editor names. (%2e and %E work a well)
It is also possible to access all other BibTeX database fields:
%b booktitle %c chapter %d edition %h howpublished
%i institution %j journal %k key %m month
%n number %o organization %p pages %P first page
%r address %s school %u publisher %t title
%v volume %y year
%B booktitle, abbreviated %T title, abbreviated
%U url
%D doi
%S series
Usually, only %l is needed. The other stuff is mainly for the echo area
display, and for (setq reftex-comment-citations t).
%< as a special operator kills punctuation and space around it after the
string has been formatted.
A pair of square brackets indicates an optional argument, and RefTeX
will prompt for the values of these arguments.
Beware that all this only works with BibTeX database files. When
citations are made from the \bibitems in an explicit thebibliography
environment, only %l is available."
;; Format a citation from the info in the BibTeX ENTRY
(unless (stringp format) (setq format "\\cite{%l}"))
(if (and reftex-comment-citations
(string-match "%l" reftex-cite-comment-format))
(error "Reftex-cite-comment-format contains invalid %%l"))
(while (string-match
"\\(\\`\\|[^%]\\)\\(\\(%\\([0-9]*\\)\\([a-zA-Z]\\)\\)[.,;: ]*\\)"
format)
(let ((n (string-to-number (match-string 4 format)))
(l (string-to-char (match-string 5 format)))
rpl b e)
(save-match-data
(setq rpl
(cond
((= l ?l) (concat
(org-ref-reftex-get-bib-field "&key" entry)
(if reftex-comment-citations
reftex-cite-comment-format
"")))
((= l ?a) (replace-regexp-in-string
"\n\\|\t\\|\s+" " "
(reftex-format-names
(reftex-get-bib-names "author" entry)
(or n 2))))
((= l ?A) (replace-regexp-in-string
"\n\\|\t\\|\s+" " "
(car (reftex-get-bib-names "author" entry))))
((= l ?b) (org-ref-reftex-get-bib-field "booktitle" entry "in: %s"))
((= l ?B) (reftex-abbreviate-title
(org-ref-reftex-get-bib-field "booktitle" entry "in: %s")))
((= l ?c) (org-ref-reftex-get-bib-field "chapter" entry))
((= l ?d) (org-ref-reftex-get-bib-field "edition" entry))
((= l ?D) (org-ref-reftex-get-bib-field "doi" entry))
((= l ?e) (reftex-format-names
(reftex-get-bib-names "editor" entry)
(or n 2)))
((= l ?E) (car (reftex-get-bib-names "editor" entry)))
((= l ?h) (org-ref-reftex-get-bib-field "howpublished" entry))
((= l ?i) (org-ref-reftex-get-bib-field "institution" entry))
((= l ?j) (let ((jt (reftex-get-bib-field "journal" entry)))
(if (string= "" jt)
(reftex-get-bib-field "journaltitle" entry)
jt)))
((= l ?k) (org-ref-reftex-get-bib-field "=key=" entry))
((= l ?m) (org-ref-reftex-get-bib-field "month" entry))
((= l ?n) (org-ref-reftex-get-bib-field "number" entry))
((= l ?o) (org-ref-reftex-get-bib-field "organization" entry))
((= l ?p) (org-ref-reftex-get-bib-field "pages" entry))
((= l ?P) (car (split-string
(org-ref-reftex-get-bib-field "pages" entry)
"[- .]+")))
((= l ?s) (org-ref-reftex-get-bib-field "school" entry))
((= l ?S) (org-ref-reftex-get-bib-field "series" entry))
((= l ?u) (org-ref-reftex-get-bib-field "publisher" entry))
((= l ?U) (org-ref-reftex-get-bib-field "url" entry))
((= l ?r) (org-ref-reftex-get-bib-field "address" entry))
;; strip enclosing brackets from title if they are there
((= l ?t) (replace-regexp-in-string
"\n\\|\t\\|\s+" " "
(org-ref-reftex-get-bib-field "title" entry)))
((= l ?T) (reftex-abbreviate-title
(replace-regexp-in-string
"\n\\|\t\\|\s+" " "
(org-ref-reftex-get-bib-field "title" entry))))
((= l ?v) (org-ref-reftex-get-bib-field "volume" entry))
((= l ?y) (org-ref-reftex-get-bib-field "year" entry)))))
(if (string= rpl "")
(setq b (match-beginning 2) e (match-end 2))
(setq b (match-beginning 3) e (match-end 3)))
(setq format (concat (substring format 0 b) rpl (substring format e)))))
(while (string-match "%%" format)
(setq format (replace-match "%" t t format)))
(while (string-match "[ ,.;:]*%<" format)
(setq format (replace-match "" t t format)))
format)
(defun org-ref-get-bibtex-entry-citation (key)
"Return a string for the bibliography entry corresponding to KEY.
Format according to the type in `org-ref-bibliography-entry-format'."
(let ((org-ref-bibliography-files (org-ref-find-bibliography))
(file) (entry) (bibtex-entry) (entry-type) (format))
(setq file (catch 'result
(cl-loop for file in org-ref-bibliography-files do
(if (org-ref-key-in-file-p key (file-truename file))
(throw 'result file)
(message "%s not found in %s"
key (file-truename file))))))
(with-temp-buffer
(insert-file-contents file)
(bibtex-set-dialect (parsebib-find-bibtex-dialect) t)
(bibtex-search-entry key nil 0)
(setq bibtex-entry (bibtex-parse-entry))
;; downcase field names so they work in the format-citation code
(dolist (cons-cell bibtex-entry)
(setf (car cons-cell) (downcase (car cons-cell))))
(setq entry-type (downcase (cdr (assoc "=type=" bibtex-entry))))
(setq format (cdr (assoc entry-type org-ref-bibliography-entry-format)))
(if format
(setq entry (org-ref-reftex-format-citation bibtex-entry format))
;; if no format, we use the bibtex entry itself as a fallback
(save-restriction
(bibtex-narrow-to-entry)
(setq entry (buffer-string)))))
entry))
(defun org-ref-get-bibtex-keys (&optional sort)
"Return a list of unique keys in the buffer.
Use SORT to specify alphabetical order by key."
(let ((keys '()))
(org-element-map (org-element-parse-buffer) 'link
(lambda (link)
(let ((plist (nth 1 link)))
(when (-contains? org-ref-cite-types (plist-get plist ':type))
(dolist
(key
(org-ref-split-and-strip-string (plist-get plist ':path)))
(when (not (-contains? keys key))
(setq keys (append keys (list key))))))))
;; set with-affiliated to get keys in captions
nil nil nil t)
(when sort
;; Sort keys alphabetically
(setq keys (cl-sort keys 'string-lessp :key 'downcase)))
keys))
;;;###autoload
(defun org-ref-bibliography (&optional sort)
"Create a new buffer with a bibliography.
If SORT is non-nil it is alphabetically sorted by key
This is mostly for convenience to see what has been cited.
Entries are formatted according to the bibtex entry type in
`org-ref-bibliography-entry-format', and the actual entries are
generated by `org-ref-reftex-format-citation'."
(interactive)
(let ((bib (mapconcat
'identity
(cl-loop for i from 1
for citation in
(mapcar
(lambda (key)
(let* ((results (org-ref-get-bibtex-key-and-file key))
(key (car results))
(bibfile (cdr results)))
(format "cite:%s %s" key
(if bibfile
(save-excursion
(with-temp-buffer
(insert-file-contents bibfile)
(bibtex-set-dialect (parsebib-find-bibtex-dialect) t)
(bibtex-search-entry key)
(org-ref-bib-citation)))
"!!! No entry found !!!"))))
(org-ref-get-bibtex-keys sort))
collect (format "%3s. %s" i citation))
"\n\n")))