forked from jkitchin/org-ref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
org-ref-ivy.el
150 lines (127 loc) · 5.57 KB
/
org-ref-ivy.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
;;; org-ref-ivy.el --- org-ref with ivy completion -*- lexical-binding: t; -*-
;; Copyright (C) 2016-2021 John Kitchin
;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
;; URL: https://github.com/jkitchin/org-ref
;; Version: 1.0
;; Keywords: org-mode, cite, ref, label
;; Package-Requires: ((org-ref "0") (ivy-bibtex "0"))
;; 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 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:
;;
;; This customizes org-ref specifically around using ivy and ivy-bibtex. The
;; citation selection looks like `ivy-bibtex' but it is a customized ivy
;; function with customized actions.
(require 'org-ref-core)
(require 'ivy-bibtex)
;; all these functions are defined in ivy-bibtex, but not in a variable. Here
;; you can set them as you see fit.
(defcustom org-ref-citation-alternate-insert-actions
'(("p" ivy-bibtex-open-pdf "Open PDF file (if present)")
("u" ivy-bibtex-open-url-or-doi "Open URL or DOI in browser")
;; this insert-citation only inserts an org-ref cite.
;; ("c" ivy-bibtex-insert-citation "Insert citation")
("R" ivy-bibtex-insert-reference "Insert formatted reference")
("k" ivy-bibtex-insert-key "Insert BibTeX key")
("b" ivy-bibtex-insert-bibtex "Insert BibTeX entry")
("a" ivy-bibtex-add-PDF-attachment "Attach PDF to email")
("e" ivy-bibtex-edit-notes "Edit notes")
("s" ivy-bibtex-show-entry "Show entry")
("l" ivy-bibtex-add-pdf-to-library "Add PDF to library")
("h" (lambda (candidate)
(org-insert-heading)
(insert (bibtex-completion-apa-format-reference
(cdr (assoc "=key=" candidate)))
" "
(format "cite:&%s" (cdr (assoc "=key=" candidate)))))
"Insert org-heading")
("m" (lambda (candidate)
(insert (bibtex-completion-apa-format-reference
(cdr (assoc "=key=" candidate)))))
"Insert formatted citation")
("f" (lambda (_candidate) (ivy-bibtex-fallback ivy-text)) "Fallback options"))
"Alternate actions to do instead of inserting."
:type '(list (repeat (string function string)))
:group 'org-ref)
;; This is modified from ivy-bibtex which had " " instead of "" which breaks marking.
(defun org-ref-ivy-bibtex-display-transformer (candidate)
"Prepare bib entry CANDIDATE for display."
(let* ((width (- (frame-width) 2))
(idx (get-text-property 1 'idx candidate))
(entry (cdr (nth idx (ivy-state-collection ivy-last)))))
(s-concat (if (s-starts-with-p ivy-mark-prefix candidate) ivy-mark-prefix "")
(bibtex-completion-format-entry entry width))))
(ivy-configure 'org-ref-cite-insert-ivy :display-transformer-fn 'org-ref-ivy-bibtex-display-transformer)
(defun org-ref-cite-multi-insert-ivy (candidates)
"A multi-action function to insert CANDIDATES."
(with-ivy-window
(org-ref-insert-cite-keys
(mapcar (lambda (entry)
(cdr (assoc "=key=" (cdr entry))))
candidates)
ivy-current-prefix-arg)))
(defun org-ref-cite-insert-ivy ()
"Function for inserting a citation."
(interactive)
;; Set this in the function so it is updated if you change the functions while
;; writing
(ivy-set-actions
'org-ref-cite-insert-ivy
org-ref-citation-alternate-insert-actions)
;; This initializes bibtex if the variable is not defined.
(unless bibtex-completion-display-formats-internal
(bibtex-completion-init))
(let* ((bibtex-completion-bibliography (org-ref-find-bibliography))
(candidates (bibtex-completion-candidates)))
(ivy-read "org-ref-ivy BibTeX entries: " candidates
:preselect (ivy-thing-at-point)
:multi-action #'org-ref-cite-multi-insert-ivy
:action '(1
("o" (lambda (candidate)
(org-ref-insert-cite-key
(cdr (assoc "=key=" (cdr candidate)))
ivy-current-prefix-arg))
"insert")
("r" (lambda (candidate)
(let* ((object (org-element-context))
(type (org-element-property :type object))
(begin (org-element-property :begin object))
(end (org-element-property :end object))
(link-string (org-element-property :path object))
(data (org-ref-parse-cite-path link-string))
(references (plist-get data :references))
(cp (point))
(key)
keys i)
;; We only want this to work on citation links
(when (assoc type org-ref-cite-types)
(setq key (org-ref-get-bibtex-key-under-cursor))
(if (null key)
;; delete the whole cite
(cl--set-buffer-substring begin end "")
(setq i (seq-position
references key
(lambda (el key)
(string= key
(plist-get el :key)))))
(setf (plist-get (nth i references) :key)
(cdr (assoc "=key=" (cdr candidate))))
(setq data (plist-put data :references references))
(save-excursion
(goto-char begin)
(re-search-forward link-string)
(replace-match (org-ref-interpret-cite-data data)))
(goto-char cp)))))
"Replace key at point"))
:caller 'org-ref-cite-insert-ivy)))
(provide 'org-ref-ivy)
;;; org-ref-ivy.el ends here