forked from jkitchin/org-ref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
org-ref-latex.el
147 lines (119 loc) · 4.25 KB
/
org-ref-latex.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
;;; org-ref-latex.el --- org-ref functionality for LaTeX files -*- lexical-binding: t; -*-
;; Copyright (C) 2015 John Kitchin
;; Author: John Kitchin <[email protected]>
;; Keywords: languages
;; 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: Make cites in LaTeX documents clickable, and with tooltips.
;; We use font-lock to add some functionality to the
;;
;;; Code:
(require 'org-ref-core)
(defvar latex-mode-map)
(defvar org-ref-cite-types)
(defvar org-ref-latex-cite-re
(concat "\\\\\\(" (mapconcat
(lambda (x)
(replace-regexp-in-string "\\*" "\\\\*" x))
org-ref-cite-types
"\\|")
"\\)"
"\\(\\[[^}]*\\)?" ; optional []
"\\(\\[[^}]*\\)?" ; optional []
"{\\([^}]*\\)}")
"Regexp for LaTeX citations. \citetype[optional]{some,keys}.
The clickable part are the keys.")
(defun org-ref-latex-get-key ()
"Figure out what key the cursor is on."
(let (start end)
;; look back for , or {
(save-excursion
(re-search-backward ",\\|{")
(setq start (+ 1 (point))))
;; look forward to , or }
(save-excursion
(re-search-forward ",\\|}")
(setq end (- (point) 1)))
(buffer-substring-no-properties start end)))
;;;###autoload
(defun org-ref-latex-debug ()
(interactive)
(message-box "%S\n%S\n%S\n%S"
(org-ref-latex-get-key)
(org-ref-find-bibliography)
(org-ref-get-bibtex-key-and-file (org-ref-latex-get-key))
(ignore-errors
(org-ref-latex-help-echo nil nil (point)))))
(defun org-ref-latex-jump-to-bibtex (&optional key)
"Jump to the KEY at point."
(let ((results (org-ref-get-bibtex-key-and-file
(or key (org-ref-latex-get-key)))))
(find-file (cdr results))
(bibtex-search-entry (car results))))
;;;###autoload
(defun org-ref-latex-click ()
"Jump to entry clicked on."
(interactive)
(helm :sources '(((name . "Actions")
(candidates . (("Open Bibtex entry" . org-ref-latex-jump-to-bibtex)
("Bibtex entry menu" . (lambda ()
(org-ref-latex-jump-to-bibtex)
(org-ref-bibtex-hydra/body)))))
(action . (lambda (f)
(funcall f)))))))
(defun org-ref-latex-help-echo (_window _object position)
"Get tool tip for a key in WINDOW for OBJECT at POSITION."
(save-excursion
(goto-char position)
(let* ((key (org-ref-latex-get-key))
(results (org-ref-get-bibtex-key-and-file key))
(bibfile (cdr results))
citation
tooltip)
(setq citation
(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 !!!"))
(setq tooltip
(with-temp-buffer
(insert citation)
(fill-paragraph)
(buffer-string)))
tooltip)))
(defun org-ref-next-latex-cite (&optional limit)
"Font-lock function to make cites in LaTeX documents clickable."
(when (re-search-forward org-ref-latex-cite-re limit t)
(setq font-lock-extra-managed-props (delq 'help-echo font-lock-extra-managed-props))
(add-text-properties
(match-beginning 3)
(match-end 3)
`(mouse-face
highlight
local-map ,(let ((map (copy-keymap latex-mode-map)))
(define-key map [mouse-1]
'org-ref-latex-click)
map)
help-echo org-ref-latex-help-echo))))
(defun org-ref-latex-cite-on ()
"Add the font-lock on for citations."
(font-lock-add-keywords
nil
'((org-ref-next-latex-cite 3 font-lock-constant-face))))
(add-hook 'latex-mode-hook 'org-ref-latex-cite-on)
(add-hook 'LaTeX-mode-hook 'org-ref-latex-cite-on)
(provide 'org-ref-latex)
;;; org-ref-latex.el ends here