forked from jkitchin/org-ref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
org-ref-helm-bibtex.el
736 lines (637 loc) · 26.7 KB
/
org-ref-helm-bibtex.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
;;; org-ref-helm-bibtex.el --- Customization of helm-bibtex for org-ref -*- lexical-binding: t; -*-
;; Copyright (C) 2016 John Kitchin
;; Author: John Kitchin <[email protected]>
;; Keywords:
;; 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 file defines the completion engine for org-ref using `helm-bibtex'.
(declare-function 'org-ref-find-bibliography "org-ref-core.el")
(declare-function 'org-ref-get-bibtex-key-and-file "org-ref-core.el")
(defvar org-ref-get-pdf-filename-function)
(defvar org-ref-default-citation-link)
(defvar org-ref-cite-types)
(defvar org-ref-insert-link-function)
(defvar org-ref-insert-cite-function)
(defvar org-ref-insert-label-function)
(defvar org-ref-insert-ref-function)
(defvar org-ref-cite-onclick-function)
(defvar org-ref-insert-cite-key)
;;; Code:
(require 'helm-config)
(require 'helm)
(require 'helm-bibtex)
(require 'helm-utils)
(require 'org-ref-helm)
(require 'async)
(require 'package)
;;;###autoload
(defun org-ref-bibtex-completion-completion ()
"Use helm and ‘helm-bibtex’ for completion."
(interactive)
;; Define core functions for org-ref
(setq org-ref-insert-link-function 'org-ref-helm-insert-cite-link
org-ref-insert-cite-function 'org-ref-helm-insert-cite-link
org-ref-insert-label-function 'org-ref-helm-insert-label-link
org-ref-insert-ref-function 'org-ref-helm-insert-ref-link
org-ref-cite-onclick-function 'org-ref-cite-click-helm))
(org-ref-bibtex-completion-completion)
(define-key org-mode-map
(kbd org-ref-insert-cite-key)
org-ref-insert-link-function)
(defcustom org-ref-bibtex-completion-actions
'(("Insert citation" . helm-bibtex-insert-citation)
("Open PDF, URL or DOI" . helm-bibtex-open-any)
("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)
("Insert formatted citation(s)" . (lambda (_)
(insert
(mapconcat 'identity
(cl-loop for key in (helm-marked-candidates)
collect (org-ref-format-entry key))
"\n\n"))))
("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" . bibtex-completion-copy-candidate)
("Add PDF to library" . helm-bibtex-add-pdf-to-library))
"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 '(alist :key-type (string) :value-type (function))
:group 'org-ref)
(cl-loop for i from 0 to (length org-ref-bibtex-completion-actions)
for ccell in org-ref-bibtex-completion-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-bibtex-completion-format-org
'org-ref-bibtex-completion-format-org
"Function for how `helm-bibtex' inserts citations."
:type 'function
:group 'org-ref)
(setf (cdr (assoc 'org-mode bibtex-completion-format-citation-functions))
org-ref-bibtex-completion-format-org)
(setq org-ref-insert-cite-function 'org-ref-helm-insert-cite-link
org-ref-cite-onclick-function 'org-ref-cite-click-helm)
;;* Helm bibtex setup.
(setq bibtex-completion-additional-search-fields '(keywords))
(setq bibtex-completion-display-formats
'((t . "${author:36} ${title:*} ${year:4} ${=has-pdf=:1}${=has-note=:1} ${=type=:7} ${keywords:31}")))
(defun bibtex-completion-copy-candidate (_candidate)
"Copy the selected bibtex entries to the clipboard.
Used as a new action in `helm-bibtex'.
CANDIDATE is ignored."
(with-temp-buffer
(bibtex-mode)
(mapc #'insert-file-contents
(-flatten (list bibtex-completion-bibliography)))
(let ((entries '()))
(cl-loop for bibtex-key in (helm-marked-candidates)
do
(goto-char (point-min))
(re-search-forward (concat "^@\\(" parsebib--bibtex-identifier
"\\)[[:space:]]*[\(\{][[:space:]]*"
(regexp-quote bibtex-key)
"[[:space:]]*,"))
(cl-pushnew (buffer-substring
(bibtex-beginning-of-entry)
(bibtex-end-of-entry))
entries))
(with-temp-buffer
(dolist (entry entries)
(insert (format "%s\n\n" entry)))
(kill-new (buffer-string))))))
(defun org-ref-helm-tag-entries (_candidates)
"Set tags on selected bibtex entries from `helm-bibtex'.
User is prompted for tags. This function is called from `helm-bibtex'.
Argument CANDIDATES helm candidates."
(message "")
(let* ((keys (helm-marked-candidates))
(entry (bibtex-completion-get-entry (car keys)))
(field (cdr (assoc-string "keywords" entry)))
(value (when field (replace-regexp-in-string "^{\\|}$" "" field)))
(keywords (read-string "Keywords (comma separated): "
(when (and value (not (equal "" value)))
(concat value ", ")))))
(cl-loop for key in keys
do
(save-window-excursion
(bibtex-completion-show-entry (list key))
;; delete keyword field if empty
(if (equal "" keywords)
(save-restriction
(bibtex-narrow-to-entry)
(goto-char (car (cdr (bibtex-search-forward-field "keywords" t))))
(bibtex-kill-field))
(bibtex-set-field
"keywords"
(concat
(if (listp keywords)
(if (string-match value keywords)
(and (replace-match "")
(mapconcat 'identity keywords ", "))
(mapconcat 'identity keywords ", "))
;; remove trailing comma
(replace-regexp-in-string ", $" "" keywords)))))
(save-buffer)))))
(defun org-ref-bibtex-completion-format-org (keys)
"Insert selected KEYS as cite link.
Append KEYS if you are on a link.
Technically, this function should return a string that is
inserted by helm. This function does the insertion and gives helm
an empty string to insert. This lets us handle appending to a
link properly.
In the `helm-bibtex' buffer, \\[universal-argument] will give you a helm menu to
select a new link type for the selected entries.
A double \\[universal-argument] \\[universal-argument] will
change the key at point to the selected keys."
(let* ((object (org-element-context))
(last-char (save-excursion
(when (org-element-property :end object)
(goto-char (org-element-property :end object))
(unless (bobp)
(backward-char))
(if (looking-at " ")
" "
"")))))
(cond
;; case where we are in a link
((and (equal (org-element-type object) 'link)
(-contains?
org-ref-cite-types
(org-element-property :type object)))
(cond
;; no prefix. insert or append keys
((equal helm-current-prefix-arg nil)
(cond
;; point after :
((looking-back ":" (- (point) 2))
(insert (concat (mapconcat 'identity keys ",") ",")))
;; point on :
((looking-at ":")
(forward-char)
(insert (concat (mapconcat 'identity keys ",") ",")))
;; point on the cite type
((-contains? org-ref-cite-types (thing-at-point 'word))
(re-search-forward ":")
(insert (concat (mapconcat 'identity keys ",") ",")))
;; after ,
((looking-back "," (- (point) 2))
(insert (concat (mapconcat 'identity keys ",") ",")))
;; on comma
((looking-at ",")
(forward-char)
(insert (concat (mapconcat 'identity keys ",") ",")))
;; somewhere in the middle or end
(t
;; goto next comma or end
(re-search-forward
","
(org-element-property :end object) 'mv)
(skip-chars-backward " ")
(skip-chars-backward "]")
(unless (looking-at ",") (insert ","))
(insert (mapconcat 'identity keys ",")))))
;; double prefix, replace key at point
((equal helm-current-prefix-arg '(16))
(setf (buffer-substring
(org-element-property :begin object)
(org-element-property :end object))
(concat
(replace-regexp-in-string
(car (org-ref-get-bibtex-key-and-file)) ; key
(mapconcat 'identity keys ",") ; new keys
(org-element-property :raw-link object))
;; replace space at end to avoid collapsing into next word.
last-char))
;; and we want to go to the end of the new link
(goto-char
(org-element-property :end (org-element-context))))
(t
(message "Not found"))))
;; We are next to a link, and we want to append
;; next to a link means one character back is on a link.
((save-excursion
(unless (bobp) (backward-char))
(and (equal (org-element-type (org-element-context)) 'link)
(-contains?
org-ref-cite-types
(org-element-property :type (org-element-context)))))
(skip-chars-backward " ")
(insert (concat "," (mapconcat 'identity keys ","))))
;; insert fresh link
(t
(insert
(concat
(when org-ref-prefer-bracket-links "[[")
(if (equal helm-current-prefix-arg '(4))
(helm :sources `((name . "link types")
(candidates . ,org-ref-cite-types)
(action . (lambda (x) x))))
org-ref-default-citation-link)
":"
(s-join "," keys)
(when org-ref-prefer-bracket-links "]]"))))))
;; return empty string for helm
"")
(defun org-ref-format-citation (keys)
"Formatter for org-ref citation commands.
Prompt for the command and additional arguments if the commands can
take any. If point is inside a citation link, append KEYS. Otherwise
prompt for pre/post text. Prompts can also be switched off by setting
the variable `bibtex-completion-cite-prompt-for-optional-arguments' to
nil. To enable this formatter, add it to
`bibtex-completion-format-citation-functions'. For example:
\(setf (cdr (assoc 'org-mode bibtex-completion-format-citation-functions)) 'org-ref-format-citation)
Note also that pre text is preceded by a double colon, for example:
\[[cite:key][See::Chapter 1]], which exports to:
\\cite[See][Chapter 1]{key}."
;; Check if point is inside a cite link
(let ((link (org-element-context))
end path)
(if (-contains? org-ref-cite-types (org-element-property :type link))
(progn
(setq end (org-element-property :end link)
path (org-element-property :path link))
(goto-char end)
(skip-chars-backward " ")
;; Check if link has pre/post text
(if (looking-back "\]" (line-beginning-position))
(progn
(re-search-backward path nil t)
(re-search-forward "\]" nil t)
(backward-char 1)
(format ",%s" (s-join "," keys))))
(format ",%s" (s-join "," keys)))
(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 ((text (if bibtex-completion-cite-prompt-for-optional-arguments
(read-from-minibuffer "Pre/post text: ")
"")))
(if (string= "" text)
(format "%s:%s" cite-command (s-join "," keys))
(format "[[%s:%s][%s]]" cite-command (s-join "," keys) text))))))))
(defvar bibtex-completion-cached-candidates)
(defvar bibtex-completion-bibliography-hash)
;;;###autoload
(defun org-ref-helm-load-completions-async ()
"Load the bibtex files into helm sources asynchronously.
For large bibtext files, the intial call to ‘org-ref-helm-insert-cite-link’
can take a long time to load the completion sources. This function loads
the completion sources in the background so the initial call to ‘org-ref-helm-insert-cite-link’ is much faster."
(interactive)
(async-start
`(lambda (&optional formatter)
(require 'package)
(package-initialize)
(require 'helm-bibtex)
,(async-inject-variables "bibtex-compl.*")
(with-temp-buffer
(mapc #'insert-file-contents
(-flatten (list bibtex-completion-bibliography)))
;; Check hash of bibliography and reparse if necessary:
(let ((bibliography-hash (secure-hash 'sha256 (current-buffer))))
(unless (and bibtex-completion-cached-candidates
(string= bibtex-completion-bibliography-hash bibliography-hash))
(message "Loading bibliography ...")
(let* ((entries (bibtex-completion-parse-bibliography))
(entries (bibtex-completion-resolve-crossrefs entries))
(entries (bibtex-completion-prepare-entries entries))
(entries (nreverse entries))
(entries
(--map (cons (bibtex-completion-clean-string
(s-join " " (-map #'cdr it))) it)
entries)))
(setq bibtex-completion-cached-candidates
(if (functionp formatter)
(funcall formatter entries)
entries)))
(setq bibtex-completion-bibliography-hash bibliography-hash))
(cons bibliography-hash bibtex-completion-cached-candidates))))
(lambda (result)
(setq bibtex-completion-cached-candidates (cdr result))
(setq bibtex-completion-bibliography-hash (car result))
(message "Finished loading org-ref completions"))))
;;;###autoload
(defun org-ref-helm-insert-cite-link (&optional arg)
"Insert a citation link with `helm-bibtex'.
With one prefix ARG, insert a ref link.
With two prefix ARGs, insert a label link."
(interactive "P")
;; save all bibtex buffers so we get the most up-to-date selection. I find
;; that I often edit a bibliography and forget to save it, so the newest entry
;; does not show in helm-bibtex.
(org-ref-save-all-bibtex-buffers)
(cond
((equal arg nil)
(let ((bibtex-completion-bibliography (org-ref-find-bibliography)))
(helm-bibtex)))
((equal arg '(4))
(org-ref-helm-insert-ref-link))
((equal arg '(16))
(org-ref-helm-insert-label-link))))
;; add our own fallback entries where we want them. These appear in reverse
;; order of adding in the menu
(setq bibtex-completion-fallback-options
(-insert-at 1 '("Crossref" . "http://search.crossref.org/?q=%s") bibtex-completion-fallback-options))
(setq bibtex-completion-fallback-options
(-insert-at
1
'("Scopus" . "http://www.scopus.com/scopus/search/submit/xadvanced.url?searchfield=TITLE-ABS-KEY(%s)")
bibtex-completion-fallback-options))
(setq bibtex-completion-fallback-options
(-insert-at 1 '("WOS" . "http://gateway.webofknowledge.com/gateway/Gateway.cgi?topic=%s&GWVersion=2&SrcApp=WEB&SrcAuth=HSB&DestApp=UA&DestLinkType=GeneralSearchSummary") bibtex-completion-fallback-options))
(defun org-ref-cite-candidates ()
"Generate the list of possible candidates for click actions on a cite link.
Checks for pdf and doi, and add appropriate functions."
(let* ((results (org-ref-get-bibtex-key-and-file))
(key (car results))
(bibfile (cdr results))
(bibtex-completion-bibliography (list bibfile))
(entry (bibtex-completion-get-entry key))
(pdf-file (funcall org-ref-get-pdf-filename-function key))
(pdf-bibtex-completion (car (bibtex-completion-find-pdf key)))
(notes-p (cdr (assoc "=has-note=" entry)))
(url (save-excursion
(with-temp-buffer
(insert-file-contents bibfile)
(bibtex-set-dialect (parsebib-find-bibtex-dialect) t)
(bibtex-search-entry key)
(bibtex-autokey-get-field "url"))))
(doi (save-excursion
(with-temp-buffer
(insert-file-contents bibfile)
(bibtex-set-dialect (parsebib-find-bibtex-dialect) t)
(bibtex-search-entry key)
;; I like this better than bibtex-url which does not always find
;; the urls
(bibtex-autokey-get-field "doi"))))
(candidates `(("Quit" . org-ref-citation-at-point)
("Open bibtex entry" . org-ref-open-citation-at-point))))
;; for some reason, when there is no doi or url, they are returned as "". I
;; prefer nil so we correct this here.
(when (string= doi "") (setq doi nil))
(when (string= url "") (setq url nil))
;; Conditional pdf functions
;; try with org-ref first
(cond ((file-exists-p pdf-file)
(cl-pushnew
'("Open pdf" . (lambda ()
(funcall org-ref-open-pdf-function)))
candidates))
;; try with bibtex-completion
(pdf-bibtex-completion
(cl-pushnew
'("Open pdf" . (lambda ()
(funcall org-ref-open-pdf-function)))
candidates))
;; try with doi
(t
(cl-pushnew
'("Try to get pdf" . (lambda ()
(save-window-excursion
(org-ref-open-citation-at-point)
(bibtex-beginning-of-entry)
(doi-utils-get-bibtex-entry-pdf))))
candidates)))
(if notes-p
(cl-pushnew
'("Open notes" . org-ref-open-notes-at-point)
candidates)
(cl-pushnew
'("Add notes" . org-ref-open-notes-at-point)
candidates))
;; conditional url and doi functions
(when (or url doi)
(cl-pushnew
'("Open in browser" . org-ref-open-url-at-point)
candidates))
(when doi
(mapc (lambda (x)
(cl-pushnew x candidates))
`(("WOS" . org-ref-wos-at-point)
("Related articles in WOS" . org-ref-wos-related-at-point)
("Citing articles in WOS" . org-ref-wos-citing-at-point)
("Google Scholar" . org-ref-google-scholar-at-point)
("Pubmed" . org-ref-pubmed-at-point)
("Crossref" . org-ref-crossref-at-point))))
(cl-pushnew
'("Insert new citation" . (lambda ()
(org-ref-helm-insert-cite-link nil)))
candidates)
(cl-pushnew
'("Delete key at point" . org-ref-delete-key-at-point)
candidates)
;; This is kind of clunky. We store the key at point. Add the new ref. Get
;; it off the end, and put it in the original position.
(cl-pushnew
'("Replace key at point" . org-ref-replace-key-at-point)
candidates)
(cl-pushnew
'("Delete citation at point" . org-ref-delete-cite-at-point)
candidates)
(when bibtex-completion-cite-prompt-for-optional-arguments
(cl-pushnew
'("Update pre/post text" . org-ref-update-pre-post-text)
candidates))
(cl-pushnew
'("Sort keys by year" . org-ref-sort-citation-link)
candidates)
(cl-pushnew
'("Copy formatted citation to clipboard" . org-ref-copy-entry-as-summary)
candidates)
(cl-pushnew
'("Copy key to clipboard" . (lambda ()
(kill-new
(car (org-ref-get-bibtex-key-and-file)))))
candidates)
(cl-pushnew
'("Copy bibtex entry to file" . org-ref-copy-entry-at-point-to-file)
candidates)
(cl-pushnew
'("Email bibtex entry and pdf" . (lambda ()
(save-excursion
(org-ref-open-citation-at-point)
(org-ref-email-bibtex-entry))))
candidates)
;; add Scopus functions. These work by looking up a DOI to get a Scopus
;; EID. This may only work for Scopus articles. Not all DOIs are recognized
;; in the Scopus API. We only load these if you have defined a
;; `*scopus-api-key*', which is required to do the API queries. See
;; `scopus'. These functions are appended to the candidate list.
(when (and (boundp '*scopus-api-key*) *scopus-api-key*)
(cl-pushnew
'("Open in Scopus" . (lambda ()
(let ((eid (scopus-doi-to-eid (org-ref-get-doi-at-point))))
(if eid
(scopus-open-eid eid)
(message "No EID found.")))))
candidates)
(cl-pushnew
'("Scopus citing articles" . (lambda ()
(let ((url (scopus-citing-url
(org-ref-get-doi-at-point))))
(if url
(browse-url url)
(message "No url found.")))))
candidates)
(cl-pushnew
'("Scopus related by authors" . (lambda ()
(let ((url (scopus-related-by-author-url
(org-ref-get-doi-at-point))))
(if url
(browse-url url)
(message "No url found.")))))
candidates)
(cl-pushnew
'("Scopus related by references" . (lambda ()
(let ((url (scopus-related-by-references-url
(org-ref-get-doi-at-point))))
(if url
(browse-url url)
(message "No url found.")))))
candidates)
(cl-pushnew
'("Scopus related by keywords" . (lambda ()
(let ((url (scopus-related-by-keyword-url
(org-ref-get-doi-at-point))))
(if url
(browse-url url)
(message "No url found.")))))
candidates))
;; finally return a numbered list of the candidates
(cl-loop for i from 0
for cell in (reverse candidates)
collect (cons (format "%2s. %s" i (car cell))
(cdr cell)))))
(defvar org-ref-helm-user-candidates '()
"List of user-defined candidates to act when clicking on a cite link.
This is a list of cons cells '((\"description\" . action)). The
action function should not take an argument, and should assume
point is on the cite key of interest.")
;; example of adding your own function
(add-to-list
'org-ref-helm-user-candidates
'("Open pdf in emacs" . (lambda ()
(find-file
(concat
(file-name-as-directory org-ref-pdf-directory)
(car (org-ref-get-bibtex-key-and-file))
".pdf"))))
t)
;;;###autoload
(defun org-ref-cite-click-helm (_key)
"Open helm for actions on a cite link.
subtle points.
1. get name and candidates before entering helm because we need
the org-buffer.
2. switch back to the org buffer before evaluating the
action. most of them need the point and buffer.
KEY is returned for the selected item(s) in helm."
(interactive)
(let ((name (org-ref-format-entry (org-ref-get-bibtex-key-under-cursor)))
(candidates (org-ref-cite-candidates))
(cb (current-buffer)))
(helm :sources `(((name . ,name)
(candidates . ,candidates)
(action . (lambda (f)
(switch-to-buffer ,cb)
(funcall f))))
((name . "User functions")
(candidates . ,org-ref-helm-user-candidates)
(action . (lambda (f)
(switch-to-buffer ,cb)
(funcall f))))))))
;; browse labels
(defun org-ref-browser-label-source ()
(let ((labels (org-ref-get-labels)))
(helm-build-sync-source "Browse labels"
:follow 1
:candidates labels
:action '(("Browse labels" . (lambda (label)
(with-selected-window (selected-window)
(org-open-link-from-string
(format "ref:%s" label)))))))))
;; browse citation links
(defun org-ref-browser-transformer (candidates)
"Add counter to candidates."
(let ((counter 0))
(cl-loop for i in candidates
collect (format "%s %s" (cl-incf counter) i))))
(defun org-ref-browser-display (candidate)
"Strip counter from candidates."
(replace-regexp-in-string "^[0-9]+? " "" candidate))
;;;###autoload
(defun org-ref-browser (&optional arg)
"Quickly browse label links in helm.
With a prefix ARG, browse citation links."
(interactive "P")
(if arg
(let ((keys nil)
(alist nil))
(widen)
(outline-show-all)
(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))
(let ((start (org-element-property :begin link)))
(dolist (key
(org-ref-split-and-strip-string (plist-get plist ':path)))
(setq keys (append keys (list key)))
(setq alist (append alist (list (cons key start))))))))))
(let ((counter 0)
count-key-pos)
;; the idea here is to create an alist with ("counter key" .
;; position) to produce unique candidates
(setq count-key-pos (mapcar (lambda (x)
(cons
(format "%s %s" (cl-incf counter) (car x)) (cdr x)))
alist))
;; push mark to restore position with C-u C-SPC
(push-mark (point))
;; move point to the first citation link in the buffer
(goto-char (cdr (assoc (caar alist) alist)))
(helm :sources
(helm-build-sync-source "Browse citation links"
:follow 1
:candidates keys
:candidate-transformer 'org-ref-browser-transformer
:real-to-display 'org-ref-browser-display
:persistent-action (lambda (candidate)
(helm-goto-char
(cdr (assoc candidate count-key-pos))))
:action `(("Open menu" . ,(lambda (candidate)
(helm-goto-char
(cdr (assoc candidate count-key-pos)))
(org-open-at-point)))))
:candidate-number-limit 10000
:buffer "*helm browser*")))
(helm :sources (org-ref-browser-label-source)
:buffer "*helm labels*")))
(provide 'org-ref-helm-bibtex)
;;; org-ref-helm-bibtex.el ends here