1 ;;; org-ref.el --- cite and cross-reference in org-mode
3 ;; Copyright(C) 2014 John Kitchin
5 ;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
6 ;; URL: https://github.com/jkitchin/org-ref
8 ;; Keywords: org-mode, cite, ref, label
9 ;; Package-Requires: ((org) (dash) (helm) (helm-bibtex) (hydra))
11 ;; This file is not currently part of GNU Emacs.
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program ; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; Lisp code to setup bibliography cite, ref and label org-mode links.
31 ;; also sets up reftex and helm for org-mode citations. The links are
32 ;; clickable and do things that are useful. You should really read
33 ;; org-ref.org for details.
38 (require 'reftex-cite)
41 (require 'helm-config)
42 (require 'helm-bibtex)
47 "Customization group for org-ref.")
49 (defcustom org-ref-bibliography-notes
51 "Filename where you will put all your notes about an entry in the default bibliography."
55 (defcustom org-ref-default-bibliography
57 "List of bibtex files to search for.
58 You should use full-paths for each file."
59 :type '(repeat :tag "List of bibtex files" file)
62 (defcustom org-ref-pdf-directory
64 "Directory where pdfs are stored by key. put a trailing / in."
68 (defcustom org-ref-default-citation-link
70 "The default type of citation link to use."
74 (defcustom org-ref-insert-cite-key
76 "Keyboard shortcut to insert a citation."
80 (defcustom org-ref-bibliography-entry-format
81 '(("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>.")
83 ("book" . "%a, %t, %u (%y).")
85 ("proceedings" . "%e, %t in %S, %u (%y).")
87 ("inproceedings" . "%a, %t, %p, in %b, edited by %e, %u (%y)"))
89 "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."
93 (defcustom org-ref-open-notes-function
99 ;;(org-tree-to-indirect-buffer)
100 (outline-previous-visible-heading 1)
101 (recenter-top-bottom 0))
102 "User-defined way to open a notes entry. This is excecuted after the entry is found, 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."
107 (defcustom org-ref-open-pdf-function
108 'org-ref-open-pdf-at-point
109 "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'."
114 (defcustom org-ref-insert-cite-function
115 'org-ref-helm-insert-cite-link
116 "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."
121 (defcustom org-ref-cite-onclick-function
122 'org-ref-cite-click-helm
123 "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'."
128 (defcustom org-ref-show-citation-on-enter t
129 "If non-nil add a hook function to show the citation summary in the minibuffer."
132 (defcustom org-ref-cite-types
133 '("cite" "nocite" ;; the default latex cite commands
134 ;; natbib cite commands, http://ctan.unixbrain.com/macros/latex/contrib/natbib/natnotes.pdf
135 "citet" "citet*" "citep" "citep*"
136 "citealt" "citealt*" "citealp" "citealp*"
138 "citeauthor" "citeauthor*"
139 "citeyear" "citeyear*"
140 "Citet" "Citep" "Citealt" "Citealp" "Citeauthor"
142 ;; http://ctan.mirrorcatalogs.com/macros/latex/contrib/biblatex/doc/biblatex.pdf
144 "parencite" "Parencite"
145 "footcite" "footcitetext"
146 "textcite" "Textcite"
147 "smartcite" "Smartcite"
148 "cite*" "parencite*" "supercite"
149 "autocite" "Autocite" "autocite*" "Autocite*"
151 "citetitle" "citetitle*"
152 "citedate" "citedate*"
154 "fullcite" "footfullcite"
155 ;; "volcite" "Volcite" cannot support the syntax
156 "notecite" "Notecite"
157 "pnotecite" "Pnotecite"
159 ;; multicites. Very limited support for these.
160 "cites" "Cites" "parencites" "Parencites"
161 "footcites" "footcitetexts"
162 "smartcites" "Smartcites" "textcites" "Textcites"
163 "supercites" "autocites" "Autocites"
164 ;; for the bibentry package
167 "List of citation types known in `org-ref'."
168 :type '(repeat :tag "List of citation types" string)
171 (defcustom org-ref-clean-bibtex-entry-hook nil
172 "Hook that is run in `org-ref-clean-bibtex-entry'. The functions should take no arguments, and operate on the bibtex entry at point."
176 (defvar org-ref-bibliography-files
178 "Variable to hold bibliography files to be searched.")
180 ;; * org-mode / reftex setup
182 (defun org-mode-reftex-setup ()
183 "Setup `org-mode' and reftex for org-ref."
184 (and (buffer-file-name)
185 (file-exists-p (buffer-file-name))
186 (global-auto-revert-mode t))
187 (make-local-variable 'reftex-cite-format)
188 (setq reftex-cite-format 'org))
190 ;; define key for inserting citations
191 (define-key org-mode-map
192 (kbd org-ref-insert-cite-key)
193 org-ref-insert-cite-function)
195 (add-hook 'org-mode-hook 'org-mode-reftex-setup)
197 (eval-after-load 'reftex-vars
199 (add-to-list 'reftex-cite-format-builtin
200 '(org "Org-mode citation"
201 ((?\C-m . "cite:%l") ; default
202 (?d . ",%l") ; for appending
208 (?h . "citeauthor:%l")
209 (?H . "citeauthor*:%l")
215 ;; * Messages for link at cursor
216 (defvar org-ref-message-timer nil
217 "Variable to store the link message timer in.")
220 (defun org-ref-show-link-messages ()
221 "Turn on link messages.
222 You will see a message in the minibuffer when on a cite, ref or label link."
224 (or org-ref-message-timer
225 (setq org-ref-message-timer
226 (run-with-idle-timer 0.5 t 'org-ref-link-message))))
229 (defun org-ref-cancel-link-messages ()
230 "Stop showing messages in minibuffer when on a link."
232 (cancel-timer org-ref-message-timer)
233 (setq org-ref-message-timer nil))
236 (when org-ref-show-citation-on-enter
237 (org-ref-show-link-messages))
239 ;; ** Messages for context under mouse pointer
241 (defvar org-ref-last-mouse-pos nil
242 "Stores last mouse position for use in `org-ref-mouse-message'.")
244 (defun org-ref-can-move-p ()
245 "See if a character is under the mouse. If so return the position for `goto-char'."
246 (let* ((line (cddr org-ref-last-mouse-pos))
247 (col (cadr org-ref-last-mouse-pos)))
249 (goto-char (window-start))
252 (> (- (line-end-position) (line-beginning-position)) col)
253 (progn (forward-char col) (point))
257 (defun org-ref-mouse-message ()
258 "Display message for link under mouse cursor."
260 (when (not (equal (mouse-position) org-ref-last-mouse-pos))
261 (setq org-ref-last-mouse-pos (mouse-position))
262 (let ((p (org-ref-can-move-p)))
266 (org-ref-link-message))))))
269 (defvar org-ref-message-timer-mouse nil
270 "Store mouse timer.")
273 (defvar org-ref-mouse-message-interval 0.5
274 "How often to run the mouse message timer in seconds.")
277 (defun org-ref-mouse-messages-on ()
278 "Turn on mouse messages."
280 (or org-ref-message-timer-mouse
281 (setq org-ref-message-timer-mouse
282 (run-at-time "0.5 sec"
283 org-ref-mouse-message-interval
284 'org-ref-mouse-message))))
287 (defun org-ref-mouse-messages-off ()
288 "Turn off mouse messages."
290 (cancel-timer org-ref-message-timer-mouse)
291 (setq org-ref-message-timer-mouse nil)
292 (message "Mouse messages are off"))
294 ;; Colorizing org-ref links
295 (defcustom org-ref-colorize-links
297 "When non-nil, change colors of links."
301 (defcustom org-ref-cite-color
303 "Color of cite like links."
307 (defcustom org-ref-ref-color
309 "Color of ref like links."
313 (defcustom org-ref-label-color
315 "Color of label links."
319 (defvar org-ref-cite-re nil
320 "Regexp for cite links.")
323 (setq org-ref-cite-re
324 (concat "\\(" (mapconcat
326 (replace-regexp-in-string "\*" "\\\\*" x)
328 org-ref-cite-types "\\|") "\\)"
329 ":\\([a-zA-Z0-9-_:]*,?\\)*"))
332 (setq org-ref-label-re
333 "label:\\([a-zA-Z0-9-_:]*,?\\)*")
337 "\\(eq\\)?ref:\\([a-zA-Z0-9-_:]*,?\\)*")
340 (defface org-ref-cite-face
341 `((t (:inherit org-link :foreground ,org-ref-cite-color)))
342 "Color for cite-like links in org-ref.")
345 (defface org-ref-label-face
346 `((t (:inherit org-link :foreground ,org-ref-label-color)))
347 "Color for ref links in org-ref.")
350 (defface org-ref-ref-face
351 `((t (:inherit org-link :foreground ,org-ref-ref-color)))
352 "Face for ref links in org-ref.")
355 (defun org-ref-colorize-links ()
356 "Colorize org-ref links."
358 (highlight-regexp org-ref-cite-re 'org-ref-cite-face)
359 (highlight-regexp org-ref-label-re 'org-ref-label-face)
360 (highlight-regexp org-ref-ref-re 'org-ref-ref-face))
363 (when org-ref-colorize-links
364 (add-hook 'org-mode-hook 'org-ref-colorize-links))
366 ;; * General org-ref utilities
367 (defun org-ref-strip-string (string)
368 "Strip leading and trailing whitespace from the STRING."
369 (replace-regexp-in-string
370 (concat search-whitespace-regexp "$" ) ""
371 (replace-regexp-in-string
372 (concat "^" search-whitespace-regexp ) "" string)))
374 (defun org-ref-split-and-strip-string (string)
375 "Split key-string and strip keys in STRING.
376 Assumes the key-string is comma delimited."
377 (mapcar 'org-ref-strip-string (split-string string ",")))
379 (defun org-ref-reftex-get-bib-field (field entry &optional format)
380 "Similar to reftex-get-bib-field, but removes enclosing braces and quotes in FIELD in the bibtex ENTRY.
381 Optional argument FORMAT bibtex format."
383 (setq result (reftex-get-bib-field field entry format))
384 (when (and (not (string= result "")) (string= "{" (substring result 0 1)))
385 (setq result (substring result 1 -1)))
386 (when (and (not (string= result "")) (string= "\"" (substring result 0 1)))
387 (setq result (substring result 1 -1)))
390 (defun org-ref-reftex-format-citation (entry format)
391 "Return a formatted string for the bibtex ENTRY (from bibtex-parse-entry) according to the FORMAT argument.
392 The format is a string with these percent escapes.
394 In the format, the following percent escapes will be expanded.
396 %l The BibTeX label of the citation.
397 %a List of author names, see also `reftex-cite-punctuation'.
398 %2a Like %a, but abbreviate more than 2 authors like Jones et al.
399 %A First author name only.
400 %e Works like %a, but on list of editor names. (%2e and %E work a well)
402 It is also possible to access all other BibTeX database fields:
403 %b booktitle %c chapter %d edition %h howpublished
404 %i institution %j journal %k key %m month
405 %n number %o organization %p pages %P first page
406 %r address %s school %u publisher %t title
408 %B booktitle, abbreviated %T title, abbreviated
413 Usually, only %l is needed. The other stuff is mainly for the echo area
414 display, and for (setq reftex-comment-citations t).
416 %< as a special operator kills punctuation and space around it after the
417 string has been formatted.
419 A pair of square brackets indicates an optional argument, and RefTeX
420 will prompt for the values of these arguments.
422 Beware that all this only works with BibTeX database files. When
423 citations are made from the \bibitems in an explicit thebibliography
424 environment, only %l is available."
425 ;; Format a citation from the info in the BibTeX ENTRY
427 (unless (stringp format) (setq format "\\cite{%l}"))
429 (if (and reftex-comment-citations
430 (string-match "%l" reftex-cite-comment-format))
431 (error "Reftex-cite-comment-format contains invalid %%l"))
434 "\\(\\`\\|[^%]\\)\\(\\(%\\([0-9]*\\)\\([a-zA-Z]\\)\\)[.,;: ]*\\)"
436 (let ((n (string-to-number (match-string 4 format)))
437 (l (string-to-char (match-string 5 format)))
443 (org-ref-reftex-get-bib-field "&key" entry)
444 (if reftex-comment-citations
445 reftex-cite-comment-format
447 ((= l ?a) (reftex-format-names
448 (reftex-get-bib-names "author" entry)
450 ((= l ?A) (car (reftex-get-bib-names "author" entry)))
451 ((= l ?b) (org-ref-reftex-get-bib-field "booktitle" entry "in: %s"))
452 ((= l ?B) (reftex-abbreviate-title
453 (org-ref-reftex-get-bib-field "booktitle" entry "in: %s")))
454 ((= l ?c) (org-ref-reftex-get-bib-field "chapter" entry))
455 ((= l ?d) (org-ref-reftex-get-bib-field "edition" entry))
456 ((= l ?D) (org-ref-reftex-get-bib-field "doi" entry))
457 ((= l ?e) (reftex-format-names
458 (reftex-get-bib-names "editor" entry)
460 ((= l ?E) (car (reftex-get-bib-names "editor" entry)))
461 ((= l ?h) (org-ref-reftex-get-bib-field "howpublished" entry))
462 ((= l ?i) (org-ref-reftex-get-bib-field "institution" entry))
463 ((= l ?j) (org-ref-reftex-get-bib-field "journal" entry))
464 ((= l ?k) (org-ref-reftex-get-bib-field "key" entry))
465 ((= l ?m) (org-ref-reftex-get-bib-field "month" entry))
466 ((= l ?n) (org-ref-reftex-get-bib-field "number" entry))
467 ((= l ?o) (org-ref-reftex-get-bib-field "organization" entry))
468 ((= l ?p) (org-ref-reftex-get-bib-field "pages" entry))
469 ((= l ?P) (car (split-string
470 (org-ref-reftex-get-bib-field "pages" entry)
472 ((= l ?s) (org-ref-reftex-get-bib-field "school" entry))
473 ((= l ?S) (org-ref-reftex-get-bib-field "series" entry))
474 ((= l ?u) (org-ref-reftex-get-bib-field "publisher" entry))
475 ((= l ?U) (org-ref-reftex-get-bib-field "url" entry))
476 ((= l ?r) (org-ref-reftex-get-bib-field "address" entry))
477 ;; strip enclosing brackets from title if they are there
478 ((= l ?t) (org-ref-reftex-get-bib-field "title" entry))
479 ((= l ?T) (reftex-abbreviate-title
480 (org-ref-reftex-get-bib-field "title" entry)))
481 ((= l ?v) (org-ref-reftex-get-bib-field "volume" entry))
482 ((= l ?y) (org-ref-reftex-get-bib-field "year" entry)))))
485 (setq b (match-beginning 2) e (match-end 2))
486 (setq b (match-beginning 3) e (match-end 3)))
487 (setq format (concat (substring format 0 b) rpl (substring format e)))))
488 (while (string-match "%%" format)
489 (setq format (replace-match "%" t t format)))
490 (while (string-match "[ ,.;:]*%<" format)
491 (setq format (replace-match "" t t format)))
492 ;; also replace carriage returns, tabs, and multiple whitespaces
493 (setq format (replace-regexp-in-string "\n\\|\t\\|\s+" " " format))
496 (defun org-ref-get-bibtex-entry-citation (key)
497 "Return a string for the bibliography entry corresponding to KEY.
498 Format according to the type in `org-ref-bibliography-entry-format'."
500 (let ((org-ref-bibliography-files (org-ref-find-bibliography))
501 (file) (entry) (bibtex-entry) (entry-type) (format))
503 (setq file (catch 'result
504 (loop for file in org-ref-bibliography-files do
505 (if (org-ref-key-in-file-p key (file-truename file))
507 (message "%s not found in %s" key (file-truename file))))))
510 (insert-file-contents file)
511 (bibtex-search-entry key nil 0)
512 (setq bibtex-entry (bibtex-parse-entry))
513 (setq entry-type (downcase (cdr (assoc "=type=" bibtex-entry))))
514 (setq format (cdr (assoc entry-type org-ref-bibliography-entry-format)))
516 (setq entry (org-ref-reftex-format-citation bibtex-entry format))
518 (bibtex-narrow-to-entry)
519 (setq entry (buffer-string)))))
522 (defun org-ref-get-bibtex-keys ()
523 "Return a list of unique keys in the buffer."
525 (org-element-map (org-element-parse-buffer) 'link
527 (let ((plist (nth 1 link)))
528 (when (-contains? org-ref-cite-types (plist-get plist ':type))
531 (org-ref-split-and-strip-string (plist-get plist ':path)))
532 (when (not (-contains? keys key))
533 (setq keys (append keys (list key))))))))
534 ;; set with-affiliated to get keys in captions
536 ;; Sort keys alphabetically
537 (setq keys (cl-sort keys 'string-lessp :key 'downcase))
540 (defun org-ref-get-bibtex-entry-html (key)
541 "Return an html string for the bibliography entry corresponding to KEY."
543 (format "<li><a id=\"%s\">[%s] %s</a></li>" key key (org-ref-get-bibtex-entry-citation key)))
545 (defun org-ref-get-html-bibliography ()
546 "Create an html bibliography when there are keys."
547 (let ((keys (org-ref-get-bibtex-keys)))
549 (concat "<h1>Bibliography</h1>
551 (mapconcat (lambda (x) (org-ref-get-bibtex-entry-html x)) keys "\n")
554 (defun org-ref-get-bibtex-entry-org (key)
555 "Return an org string for the bibliography entry corresponding to KEY."
556 (let ((org-ref-bibliography-files (org-ref-find-bibliography))
557 (file) (entry) (bibtex-entry) (entry-type) (format))
559 (setq file (catch 'result
560 (loop for file in org-ref-bibliography-files do
561 (if (org-ref-key-in-file-p key (file-truename file))
563 (message "%s not found in %s" key (file-truename file))))))
566 (insert-file-contents file)
567 (bibtex-search-entry key nil 0)
568 (setq entry (bibtex-parse-entry))
573 " (org-ref-reftex-get-bib-field "author" entry)
574 (org-ref-reftex-get-bib-field "title" entry)
575 (concat " :CUSTOM_ID: " (org-ref-reftex-get-bib-field "=key=" entry) "\n"
576 (mapconcat (lambda (element) (format " :%s: %s"
577 (upcase (car element))
582 (defun org-ref-get-org-bibliography ()
583 "Create an org bibliography when there are keys."
584 (let ((keys (org-ref-get-bibtex-keys)))
586 (concat "* Bibliography
588 (mapconcat (lambda (x) (org-ref-get-bibtex-entry-org x)) keys "\n")
591 (defun org-ref-get-bibtex-entry-ascii (key)
592 "Return an ascii string for the bibliography entry corresponding to KEY."
594 (format "[%s] %s" key (org-ref-get-bibtex-entry-citation key)))
596 (defun org-ref-get-ascii-bibliography ()
597 "Create an html bibliography when there are keys."
598 (let ((keys (org-ref-get-bibtex-keys)))
604 (mapconcat (lambda (x) (org-ref-get-bibtex-entry-ascii x)) keys "\n")
608 ;; ** bibliography and bibliographystyle
609 (org-add-link-type "bibliography"
610 ;; this code is run on clicking. The bibliography
611 ;; may contain multiple files. this code finds the
612 ;; one you clicked on and opens it.
613 (lambda (link-string)
614 ;; get link-string boundaries
615 ;; we have to go to the beginning of the line, and then search forward
618 ;; object is the link you clicked on
619 (object (org-element-context))
620 (link-string-beginning)
624 (goto-char (org-element-property :begin object))
625 (search-forward link-string nil nil 1)
626 (setq link-string-beginning (match-beginning 0))
627 (setq link-string-end (match-end 0)))
629 ;; We set the reftex-default-bibliography
630 ;; here. it should be a local variable only in
631 ;; the current buffer. We need this for using
632 ;; reftex to do citations.
633 (set (make-local-variable 'reftex-default-bibliography)
634 (split-string (org-element-property :path object) ","))
636 ;; now if we have comma separated bibliographies
637 ;; we find the one clicked on. we want to
638 ;; search forward to next comma from point
640 (if (search-forward "," link-string-end 1 1)
641 (setq key-end (- (match-end 0) 1)) ; we found a match
642 (setq key-end (point)))) ; no comma found so take the point
643 ;; and backward to previous comma from point
645 (if (search-backward "," link-string-beginning 1 1)
646 (setq key-beginning (+ (match-beginning 0) 1)) ; we found a match
647 (setq key-beginning (point)))) ; no match found
648 ;; save the key we clicked on.
649 (setq bibfile (org-ref-strip-string (buffer-substring key-beginning key-end)))
650 (find-file bibfile))) ; open file on click
653 (lambda (keyword desc format)
655 ((eq format 'org) (org-ref-get-org-bibliography))
656 ((eq format 'ascii) (org-ref-get-ascii-bibliography))
657 ((eq format 'html) (org-ref-get-html-bibliography))
659 ;; write out the latex bibliography command
660 (format "\\bibliography{%s}" (replace-regexp-in-string "\\.bib" "" (mapconcat 'identity
661 (mapcar 'expand-file-name
662 (split-string keyword ","))
665 (org-add-link-type "nobibliography"
666 ;; this code is run on clicking. The bibliography
667 ;; may contain multiple files. this code finds the
668 ;; one you clicked on and opens it.
669 (lambda (link-string)
670 ;; get link-string boundaries
671 ;; we have to go to the beginning of the line, and then search forward
674 ;; object is the link you clicked on
675 (object (org-element-context))
677 (link-string-beginning)
681 (goto-char (org-element-property :begin object))
682 (search-forward link-string nil nil 1)
683 (setq link-string-beginning (match-beginning 0))
684 (setq link-string-end (match-end 0)))
686 ;; We set the reftex-default-bibliography
687 ;; here. it should be a local variable only in
688 ;; the current buffer. We need this for using
689 ;; reftex to do citations.
690 (set (make-local-variable 'reftex-default-bibliography)
691 (split-string (org-element-property :path object) ","))
693 ;; now if we have comma separated bibliographies
694 ;; we find the one clicked on. we want to
695 ;; search forward to next comma from point
697 (if (search-forward "," link-string-end 1 1)
698 (setq key-end (- (match-end 0) 1)) ; we found a match
699 (setq key-end (point)))) ; no comma found so take the point
700 ;; and backward to previous comma from point
702 (if (search-backward "," link-string-beginning 1 1)
703 (setq key-beginning (+ (match-beginning 0) 1)) ; we found a match
704 (setq key-beginning (point)))) ; no match found
705 ;; save the key we clicked on.
706 (setq bibfile (org-ref-strip-string (buffer-substring key-beginning key-end)))
707 (find-file bibfile))) ; open file on click
710 (lambda (keyword desc format)
712 ((eq format 'org) (org-ref-get-org-bibliography))
713 ((eq format 'ascii) (org-ref-get-ascii-bibliography))
714 ((eq format 'html) (org-ref-get-html-bibliography))
716 ;; write out the latex bibliography command
718 ; (format "{\\setbox0\\vbox{\\bibliography{%s}}}"
719 ; (replace-regexp-in-string "\\.bib" "" (mapconcat 'identity
720 ; (mapcar 'expand-file-name
721 ; (split-string keyword ","))
724 (format "\\nobibliography{%s}"
725 (replace-regexp-in-string "\\.bib" "" (mapconcat 'identity
726 (mapcar 'expand-file-name
727 (split-string keyword ","))
732 (org-add-link-type "printbibliography"
733 (lambda (arg) (message "Nothing implemented for clicking here."))
734 (lambda (keyword desc format)
736 ((eq format 'org) (org-ref-get-org-bibliography))
737 ((eq format 'html) (org-ref-get-html-bibliography))
739 ;; write out the biblatex bibliography command
740 "\\printbibliography"))
743 (org-add-link-type "bibliographystyle"
744 (lambda (arg) (message "Nothing implemented for clicking here."))
745 (lambda (keyword desc format)
748 ;; write out the latex bibliography command
749 (format "\\bibliographystyle{%s}" keyword)))))
752 (defun org-bibliographystyle-complete-link (&optional arg)
753 "Completion function for bibliographystyle link.
755 (format "bibliographystyle:%s" (ido-completing-read
757 '("unsrt" "plain" "alpha"
759 ;; https://www.sharelatex.com/learn/Natbib_bibliography_styles
760 "dinat" "humannat" "plainnat"
761 "abbrnat" "unsrtnat" "rusnat"
765 (defun org-bibliography-complete-link (&optional arg)
766 "Completion function for bibliography link.
768 (format "bibliography:%s" (read-file-name "enter file: " nil nil t)))
771 (defun org-ref-insert-bibliography-link ()
772 "Insert a bibliography with completion."
774 (insert (org-bibliography-complete-link)))
778 (org-add-link-type "addbibresource"
779 ;; this code is run on clicking. The addbibresource
780 ;; may contain multiple files. this code finds the
781 ;; one you clicked on and opens it.
782 (lambda (link-string)
783 ;; get link-string boundaries
784 ;; we have to go to the beginning of the line, and then search forward
787 ;; object is the link you clicked on
788 (object (org-element-context))
790 (link-string-beginning)
794 (goto-char (org-element-property :begin object))
795 (search-forward link-string nil nil 1)
796 (setq link-string-beginning (match-beginning 0))
797 (setq link-string-end (match-end 0)))
799 ;; We set the reftex-default-addbibresource
800 ;; here. it should be a local variable only in
801 ;; the current buffer. We need this for using
802 ;; reftex to do citations.
803 (set (make-local-variable 'reftex-default-addbibresource)
804 (split-string (org-element-property :path object) ","))
806 ;; now if we have comma separated bibliographies
807 ;; we find the one clicked on. we want to
808 ;; search forward to next comma from point
810 (if (search-forward "," link-string-end 1 1)
811 (setq key-end (- (match-end 0) 1)) ; we found a match
812 (setq key-end (point)))) ; no comma found so take the point
813 ;; and backward to previous comma from point
815 (if (search-backward "," link-string-beginning 1 1)
816 (setq key-beginning (+ (match-beginning 0) 1)) ; we found a match
817 (setq key-beginning (point)))) ; no match found
818 ;; save the key we clicked on.
819 (setq bibfile (org-ref-strip-string (buffer-substring key-beginning key-end)))
820 (find-file bibfile))) ; open file on click
823 (lambda (keyword desc format)
825 ((eq format 'html) (format "")); no output for html
827 ;; write out the latex addbibresource command
828 (format "\\addbibresource{%s}" keyword)))))
830 ;; ** List of figures
831 (defun org-ref-list-of-figures (&optional arg)
832 "Generate buffer with list of figures in them.
835 (save-excursion (widen)
836 (let* ((c-b (buffer-name))
839 (org-element-map (org-element-parse-buffer) 'link
841 "create a link for to the figure"
843 (and (string= (org-element-property :type link) "file")
845 "[^.]*\\.\\(png\\|jpg\\|eps\\|pdf\\)$"
846 (org-element-property :path link)))
849 (let* ((start (org-element-property :begin link))
850 (parent (car (cdr (org-element-property :parent link))))
851 (caption (caaar (plist-get parent :caption)))
852 (name (plist-get parent :name)))
855 "[[elisp:(progn (switch-to-buffer \"%s\")(widen)(goto-char %s))][figure %s: %s]] %s\n"
856 c-b start counter (or name "") caption)
858 "[[elisp:(progn (switch-to-buffer \"%s\")(widen)(goto-char %s))][figure %s: %s]]\n"
859 c-b start counter (or name "")))))))))
860 (switch-to-buffer "*List of Figures*")
861 (setq buffer-read-only nil)
864 (insert (mapconcat 'identity list-of-figures ""))
865 (setq buffer-read-only t)
866 (use-local-map (copy-keymap org-mode-map))
867 (local-set-key "q" #'(lambda () (interactive) (kill-buffer))))))
871 'org-ref-list-of-figures ; on click
872 (lambda (keyword desc format)
875 (format "\\listoffigures")))))
878 (defun org-ref-list-of-tables (&optional arg)
879 "Generate a buffer with a list of tables.
884 (let* ((c-b (buffer-name))
887 (org-element-map (org-element-parse-buffer 'element) 'table
889 "create a link for to the table"
891 (let ((start (org-element-property :begin table))
892 (name (org-element-property :name table))
893 (caption (caaar (org-element-property :caption table))))
896 "[[elisp:(progn (switch-to-buffer \"%s\")(widen)(goto-char %s))][table %s: %s]] %s\n"
897 c-b start counter (or name "") caption)
899 "[[elisp:(progn (switch-to-buffer \"%s\")(widen)(goto-char %s))][table %s: %s]]\n"
900 c-b start counter (or name ""))))))))
901 (switch-to-buffer "*List of Tables*")
902 (setq buffer-read-only nil)
905 (insert (mapconcat 'identity list-of-tables ""))
906 (setq buffer-read-only t)
907 (use-local-map (copy-keymap org-mode-map))
908 (local-set-key "q" #'(lambda () (interactive) (kill-buffer))))))
912 'org-ref-list-of-tables
913 (lambda (keyword desc format)
916 (format "\\listoftables")))))
918 (defun org-ref-count-labels (label)
919 "Counts number of matches for LABEL in the document."
920 (+ (count-matches (format "label:%s\\b[^-:]" label) (point-min) (point-max))
921 ;; for tblname, it is not enough to get word boundary
922 ;; tab-little and tab-little-2 match then.
923 (count-matches (format "^#\\+tblname:\\s-*%s\\b[^-:]" label) (point-min) (point-max))
924 (count-matches (format "\\label{%s}" label) (point-min) (point-max))
925 ;; this is the org-format #+label:
926 (count-matches (format "^#\\+label:\\s-*%s\\b[^-:]" label) (point-min) (point-max))
927 (let ((custom-id-count 0))
930 (when (string= label (org-entry-get (point) "CUSTOM_ID"))
931 (setq custom-id-count (+ 1 custom-id-count)))))
938 "on clicking count the number of label tags used in the buffer. A number greater than one means multiple labels!"
939 (let ((count (org-ref-count-labels label)))
940 (message (format "%s occurence%s"
946 (org-ref-count-labels label))))
947 (lambda (keyword desc format)
949 ((eq format 'html) (format "(<label>%s</label>)" path))
951 (format "\\label{%s}" keyword)))))
953 (defun org-label-store-link ()
954 "Store a link to a label. The output will be a ref to that label."
955 ;; First we have to make sure we are on a label link.
956 (let* ((object (org-element-context)))
957 (when (and (equal (org-element-type object) 'link)
958 (equal (org-element-property :type object) "label"))
959 (org-store-link-props
961 :link (concat "ref:" (org-element-property :path object))))
963 ;; Store link on table
964 (when (equal (org-element-type object) 'table)
965 (org-store-link-props
967 :link (concat "ref:" (org-element-property :name object))))
969 ;; it turns out this does not work. you can already store a link to a heading with a CUSTOM_ID
970 ;; store link on heading with custom_id
971 ; (when (and (equal (org-element-type object) 'headline)
972 ; (org-entry-get (point) "CUSTOM_ID"))
973 ; (org-store-link-props
975 ; :link (concat "ref:" (org-entry-get (point) "CUSTOM_ID"))))
977 ;; and to #+label: lines
978 (when (and (equal (org-element-type object) 'paragraph)
979 (org-element-property :name object))
980 (org-store-link-props
982 :link (concat "ref:" (org-element-property :name object))))
985 (add-hook 'org-store-link-functions 'org-label-store-link)
991 "on clicking goto the label. Navigate back with C-c &"
993 ;; next search from beginning of the buffer
995 ;; it is possible you would not find the label if narrowing is in effect
1002 (goto-char (point-min))
1003 (re-search-forward (format "label:%s\\b" label) nil t))
1007 (goto-char (point-min))
1008 (re-search-forward (format "\\label{%s}" label) nil t))
1010 ;; #+label: name org-definition
1012 (goto-char (point-min))
1013 (re-search-forward (format "^#\\+label:\\s-*\\(%s\\)\\b" label) nil t))
1017 (goto-char (point-min))
1018 (re-search-forward (format "^#\\+tblname:\\s-*\\(%s\\)\\b" label) nil t))
1020 ;; Commented out because these ref links do not actually translate correctly in LaTeX.
1021 ;; you need [[#label]] links.
1024 ; (goto-char (point-min))
1025 ; (re-search-forward (format ":CUSTOM_ID:\s-*\\(%s\\)" label) nil t))
1027 ;; we did not find anything, so go back to where we came
1028 (org-mark-ring-goto)
1029 (error "%s not found" label))
1031 (message "go back with (org-mark-ring-goto) `C-c &`"))
1033 (lambda (keyword desc format)
1035 ((eq format 'html) (format "(<ref>%s</ref>)" path))
1037 (format "\\ref{%s}" keyword)))))
1039 (defun org-ref-get-org-labels ()
1040 "Return a list of #+LABEL: labels."
1042 (goto-char (point-min))
1043 (let ((matches '()))
1044 (while (re-search-forward "^#\\+label:\\s-+\\(.*\\)\\b" (point-max) t)
1045 (add-to-list 'matches (match-string-no-properties 1) t))
1048 (defun org-ref-get-custom-ids ()
1049 "Return a list of custom_id properties in the buffer."
1050 (let ((results '()) custom_id)
1053 (let ((custom_id (org-entry-get (point) "CUSTOM_ID")))
1054 (when (not (null custom_id))
1055 (setq results (append results (list custom_id)))))))
1058 (defun org-ref-get-latex-labels ()
1059 "Return list of matchin LaTeX defined labels in buffer."
1061 (goto-char (point-min))
1062 (let ((matches '()))
1063 (while (re-search-forward "\\\\label{\\([a-zA-z0-9:-]*\\)}" (point-max) t)
1064 (add-to-list 'matches (match-string-no-properties 1) t))
1067 (defun org-ref-get-tblnames ()
1068 "Return list of table names in the buffer."
1069 (org-element-map (org-element-parse-buffer 'element) 'table
1071 (org-element-property :name table))))
1073 (defun org-ref-get-labels ()
1074 "Return a list of labels in the buffer that you can make a ref link to.
1075 This is used to complete ref links and in helm menus."
1079 (goto-char (point-min))
1080 (let ((matches '()))
1081 ;; these are the label:stuff kinds
1082 (while (re-search-forward "[^#+]label:\\([a-zA-z0-9:-]*\\)" (point-max) t)
1083 (add-to-list 'matches (match-string-no-properties 1) t))
1085 (org-ref-get-org-labels)
1086 (org-ref-get-latex-labels)
1087 (org-ref-get-tblnames)
1088 (org-ref-get-custom-ids))))))
1090 (defun org-ref-helm-insert-label-link ()
1091 "Insert a label link. helm just shows you what labels already exist.
1092 If you are on a label link, replace it."
1094 (let* ((labels (org-ref-get-labels))
1095 (cb (current-buffer)))
1096 (helm :sources `(((name . "Existing labels")
1097 (candidates . ,labels)
1098 ;; default action is to open to the label
1099 (action . (lambda (label)
1100 ;; unfortunately I do not have markers here
1101 (org-open-link-from-string (format "ref:%s" label))))
1102 ;; if you select a label, replace current one
1103 (action . (lambda (label)
1104 (switch-to-buffer ,cb)
1106 ;; no prefix or on a link
1107 ((equal helm-current-prefix-arg nil)
1108 (let* ((object (org-element-context))
1109 (last-char (save-excursion
1110 (goto-char (org-element-property :end object))
1112 (if (looking-at " ")
1115 (when (-contains? '("label")
1116 (org-element-property :type object))
1117 ;; we are on a link, so replace it.
1120 (org-element-property :begin object)
1121 (org-element-property :end object))
1123 (replace-regexp-in-string
1124 (org-element-property :path object)
1126 (org-element-property :raw-link object))
1128 ;; no prefix options defined
1130 ;; no matching selection creates a new label
1131 ((name . "Create new label")
1133 ;; default action creates a new label, or replaces old one
1134 (action . (lambda (label)
1135 (switch-to-buffer ,cb)
1136 (let* ((object (org-element-context))
1137 (last-char (save-excursion
1138 (goto-char (org-element-property :end object))
1140 (if (looking-at " ")
1143 (if (-contains? '("label")
1144 (org-element-property :type object))
1145 ;; we are on a link, so replace it.
1148 (org-element-property :begin object)
1149 (org-element-property :end object))
1151 (replace-regexp-in-string
1152 (org-element-property :path object)
1154 (org-element-property :raw-link object))
1161 helm-pattern))))))))))))
1163 (defun org-ref-complete-link (&optional arg)
1164 "Completion function for ref links.
1165 Optional argument ARG Does nothing."
1167 (setq label (completing-read "label: " (org-ref-get-labels)))
1168 (format "ref:%s" label)))
1170 (defun org-ref-insert-ref-link ()
1171 "Completion function for a ref link."
1173 (insert (org-ref-complete-link)))
1175 (defun org-ref-helm-insert-ref-link ()
1176 "Helm menu to insert ref links to labels in the document.
1177 If you are on link, replace with newly selected label.
1178 Use C-u to insert a different kind of ref link.
1179 Use C-u C-u to insert a [[#custom-id]] link"
1181 (let* ((labels (org-ref-get-labels))
1182 (bs (buffer-string))
1183 (contexts (with-temp-buffer
1185 (mapcar 'org-ref-get-label-context labels)))
1186 (cb (current-buffer)))
1188 (helm :input (thing-at-point 'word)
1189 :sources `(((name . "Available labels to ref")
1190 (candidates . ,(loop for label in labels
1191 for context in contexts
1192 ;; we do some kludgy adding spaces
1193 ;; and bars to make it "easier" to
1195 collect (cons (concat
1200 (split-string context "\n")
1203 ;; default action to replace or insert ref link.
1204 (action . (lambda (label)
1205 (switch-to-buffer ,cb)
1208 ;; no prefix or on a link
1209 ((equal helm-current-prefix-arg nil)
1210 (let* ((object (org-element-context))
1211 (last-char (save-excursion
1212 (goto-char (org-element-property :end object))
1214 (if (looking-at " ")
1217 (if (-contains? '("ref" "eqref" "pageref" "nameref")
1218 (org-element-property :type object))
1219 ;; we are on a link, so replace it.
1222 (org-element-property :begin object)
1223 (org-element-property :end object))
1225 (replace-regexp-in-string
1226 (org-element-property :path object)
1228 (org-element-property :raw-link object))
1230 ;; insert a new link
1235 ;; one prefix, alternate ref link
1236 ((equal helm-current-prefix-arg '(4))
1239 (helm :sources '((name . "Ref link types")
1240 (candidates . ("ref" "eqref" "pageref" "nameref"))
1241 (action . (lambda (x) x))))
1243 ;; two prefixes, insert section custom-id link
1244 ((equal helm-current-prefix-arg '(16))
1246 (format "[[#%s]]" label)))
1254 "on clicking goto the label. Navigate back with C-c &"
1255 (org-mark-ring-push)
1256 ;; next search from beginning of the buffer
1262 (goto-char (point-min))
1263 (re-search-forward (format "label:%s\\b" label) nil t))
1267 (goto-char (point-min))
1268 (re-search-forward (format "\\label{%s}" label) nil t))
1270 ;; #+label: name org-definition
1272 (goto-char (point-min))
1273 (re-search-forward (format "^#\\+label:\\s-*\\(%s\\)\\b" label) nil t))
1277 (goto-char (point-min))
1278 (re-search-forward (format "^#\\+tblname:\\s-*\\(%s\\)\\b" label) nil t))
1280 ;; Commented out because these ref links do not actually translate correctly in LaTeX.
1281 ;; you need [[#label]] links.
1284 ; (goto-char (point-min))
1285 ; (re-search-forward (format ":CUSTOM_ID:\s-*\\(%s\\)" label) nil t))
1287 ;; we did not find anything, so go back to where we came
1288 (org-mark-ring-goto)
1289 (error "%s not found" label))
1290 (message "go back with (org-mark-ring-goto) `C-c &`"))
1292 (lambda (keyword desc format)
1294 ((eq format 'html) (format "(<pageref>%s</pageref>)" path))
1296 (format "\\pageref{%s}" keyword)))))
1298 (defun org-pageref-complete-link (&optional arg)
1299 "Completion function for ref links.
1300 Optional argument ARG Does nothing."
1302 (setq label (completing-read "label: " (org-ref-get-labels)))
1303 (format "ref:%s" label)))
1305 (defun org-pageref-insert-ref-link ()
1306 "Insert a pageref link with completion."
1308 (insert (org-pageref-complete-link)))
1314 "on clicking goto the label. Navigate back with C-c &"
1315 (org-mark-ring-push)
1316 ;; next search from beginning of the buffer
1322 (goto-char (point-min))
1323 (re-search-forward (format "\\label{%s}" label) nil t))
1325 ;; we did not find anything, so go back to where we came
1326 (org-mark-ring-goto)
1327 (error "%s not found" label))
1328 (message "go back with (org-mark-ring-goto) `C-c &`"))
1330 (lambda (keyword desc format)
1332 ((eq format 'html) (format "(<nameref>%s</nameref>)" path))
1334 (format "\\nameref{%s}" keyword)))))
1341 "on clicking goto the label. Navigate back with C-c &"
1342 (org-mark-ring-push)
1343 ;; next search from beginning of the buffer
1345 (goto-char (point-min))
1348 ;; search forward for the first match
1350 (re-search-forward (format "label:%s" label) nil t)
1352 (re-search-forward (format "\\label{%s}" label) nil t)
1353 ;; #+label: name org-definition
1354 (re-search-forward (format "^#\\+label:\\s-*\\(%s\\)\\b" label) nil t))
1355 (org-mark-ring-goto)
1356 (error "%s not found" label))
1357 (message "go back with (org-mark-ring-goto) `C-c &`"))
1359 (lambda (keyword desc format)
1361 ((eq format 'html) (format "(<eqref>%s</eqref>)" path))
1363 (format "\\eqref{%s}" keyword)))))
1367 (defun org-ref-get-bibtex-key-under-cursor ()
1368 "Return key under the bibtex cursor.
1369 We search forward from
1370 point to get a comma, or the end of the link, and then backwards
1371 to get a comma, or the beginning of the link. that delimits the
1372 keyword we clicked on. We also strip the text properties."
1373 (let* ((object (org-element-context))
1374 (link-string (org-element-property :path object)))
1375 ;; you may click on the part before the citations. here we make
1376 ;; sure to move to the beginning so you get the first citation.
1378 (goto-char (org-element-property :begin object))
1379 (search-forward link-string (org-element-property :end object))
1380 (goto-char (match-beginning 0))
1381 ;; check if we clicked before the path and move as needed.
1382 (unless (< cp (point))
1385 (if (not (org-element-property :contents-begin object))
1386 ;; this means no description in the link
1388 ;; we need the link path start and end
1390 (goto-char (org-element-property :begin object))
1391 (search-forward link-string nil nil 1)
1392 (setq link-string-beginning (match-beginning 0))
1393 (setq link-string-end (match-end 0)))
1395 ;; The key is the text between commas, or the link boundaries
1397 (if (search-forward "," link-string-end t 1)
1398 (setq key-end (- (match-end 0) 1)) ; we found a match
1399 (setq key-end link-string-end))) ; no comma found so take the end
1400 ;; and backward to previous comma from point which defines the start character
1402 (if (search-backward "," link-string-beginning 1 1)
1403 (setq key-beginning (+ (match-beginning 0) 1)) ; we found a match
1404 (setq key-beginning link-string-beginning))) ; no match found
1405 ;; save the key we clicked on.
1406 (setq bibtex-key (org-ref-strip-string (buffer-substring key-beginning key-end)))
1407 (set-text-properties 0 (length bibtex-key) nil bibtex-key)
1409 ;; link with description. assume only one key
1412 (defun org-ref-find-bibliography ()
1413 "Find the bibliography in the buffer.
1414 This function sets and returns cite-bibliography-files, which is a list of files
1415 either from bibliography:f1.bib,f2.bib
1416 \bibliography{f1,f2}
1417 internal bibliographies
1419 falling back to what the user has set in `org-ref-default-bibliography'"
1422 (goto-char (point-min))
1423 ;; look for a bibliography link
1424 (when (re-search-forward "\\<bibliography:\\([^\]\|\n]+\\)" nil t)
1425 (setq org-ref-bibliography-files
1426 (mapcar 'org-ref-strip-string (split-string (match-string 1) ",")))
1427 (throw 'result org-ref-bibliography-files))
1430 ;; we did not find a bibliography link. now look for \bibliography
1431 (goto-char (point-min))
1432 (when (re-search-forward "\\\\bibliography{\\([^}]+\\)}" nil t)
1433 ;; split, and add .bib to each file
1434 (setq org-ref-bibliography-files
1435 (mapcar (lambda (x) (concat x ".bib"))
1436 (mapcar 'org-ref-strip-string
1437 (split-string (match-string 1) ","))))
1438 (throw 'result org-ref-bibliography-files))
1440 ;; no bibliography found. maybe we need a biblatex addbibresource
1441 (goto-char (point-min))
1442 ;; look for a bibliography link
1443 (when (re-search-forward "addbibresource:\\([^\]\|\n]+\\)" nil t)
1444 (setq org-ref-bibliography-files
1445 (mapcar 'org-ref-strip-string (split-string (match-string 1) ",")))
1446 (throw 'result org-ref-bibliography-files))
1448 ;; we did not find anything. use defaults
1449 (setq org-ref-bibliography-files org-ref-default-bibliography)))
1451 ;; set reftex-default-bibliography so we can search
1452 (set (make-local-variable 'reftex-default-bibliography) org-ref-bibliography-files)
1453 org-ref-bibliography-files)
1455 (defun org-ref-key-in-file-p (key filename)
1456 "Determine if the KEY is in the FILENAME."
1457 (save-current-buffer
1458 (let ((bibtex-files (list filename)))
1459 ;; This is something I am trying because when the bibtex file is open, and
1460 ;; you have added to it, the only way I find to get the update to update
1461 ;; is to close it and reopen it. or to save it and revert it.
1462 (when (get-file-buffer filename)
1463 (set-buffer (get-file-buffer filename))
1465 (revert-buffer t t))
1466 (bibtex-search-entry key t))))
1468 (defun org-ref-get-bibtex-key-and-file (&optional key)
1469 "Return the bibtex KEY and file that it is in. If no key is provided, get one under point."
1470 (let ((org-ref-bibliography-files (org-ref-find-bibliography))
1473 (setq key (org-ref-get-bibtex-key-under-cursor)))
1474 (setq file (catch 'result
1475 (loop for file in org-ref-bibliography-files do
1476 (if (org-ref-key-in-file-p key (file-truename file))
1477 (throw 'result file)))))
1480 ;; *** key at point functions
1482 (defun org-ref-open-pdf-at-point ()
1483 "Open the pdf for bibtex key under point if it exists."
1485 (let* ((results (org-ref-get-bibtex-key-and-file))
1487 (pdf-file (format (concat org-ref-pdf-directory "%s.pdf") key)))
1488 (if (file-exists-p pdf-file)
1489 (org-open-file pdf-file)
1490 (message "no pdf found for %s" key))))
1493 (defun org-ref-open-url-at-point ()
1494 "Open the url for bibtex key under point."
1496 (let* ((results (org-ref-get-bibtex-key-and-file))
1498 (bibfile (cdr results)))
1501 (insert-file-contents bibfile)
1502 (bibtex-search-entry key)
1503 ;; I like this better than bibtex-url which does not always find
1506 (let ((url (bibtex-autokey-get-field "url")))
1508 (browse-url (s-trim url))
1511 (let ((doi (bibtex-autokey-get-field "doi")))
1513 (if (string-match "^http" doi)
1515 (browse-url (format "http://dx.doi.org/%s" (s-trim doi))))
1516 (throw 'done nil))))))))
1519 (defun org-ref-open-notes-at-point ()
1520 "Open the notes for bibtex key under point."
1522 (let* ((results (org-ref-get-bibtex-key-and-file))
1524 (bibfile (cdr results)))
1527 (insert-file-contents bibfile)
1528 (bibtex-search-entry key)
1529 (org-ref-open-bibtex-notes)))))
1532 (defun org-ref-citation-at-point ()
1533 "Give message of current citation at point."
1535 (let* ((cb (current-buffer))
1536 (results (org-ref-get-bibtex-key-and-file))
1538 (bibfile (cdr results)))
1539 (message "%s" (progn
1541 (insert-file-contents bibfile)
1542 (bibtex-search-entry key)
1543 (org-ref-bib-citation))))))
1546 (defun org-ref-open-citation-at-point ()
1547 "Open bibtex file to key at point."
1549 (let* ((cb (current-buffer))
1550 (results (org-ref-get-bibtex-key-and-file))
1552 (bibfile (cdr results)))
1554 (bibtex-search-entry key)))
1558 (defvar org-ref-cite-menu-funcs '()
1559 "Functions to run on cite click menu.
1560 Each entry is a list of (key menu-name function).
1561 The function must take no arguments and work on the key at point. Do not modify this variable, it is set to empty in the menu click function, and functions are conditionally added to it.")
1564 (defvar org-ref-user-cite-menu-funcs
1565 '(("C" "rossref" org-ref-crossref-at-point)
1566 ("y" "Copy entry to file" org-ref-copy-entry-at-point-to-file)
1567 ("s" "Copy summary" org-ref-copy-entry-as-summary))
1568 "User-defined functions to run on bibtex key at point.")
1571 (defun org-ref-copy-entry-as-summary ()
1572 "Copy the bibtex entry for the citation at point as a summary."
1574 (save-window-excursion
1575 (org-ref-open-citation-at-point)
1576 (kill-new (org-ref-bib-citation))))
1579 (defun org-ref-copy-entry-at-point-to-file ()
1580 "Copy the bibtex entry for the citation at point to NEW-FILE.
1581 Prompt for NEW-FILE includes bib files in `org-ref-default-bibliography', and bib files in current working directory. You can also specify a new file."
1583 (let ((new-file (ido-completing-read
1585 (append org-ref-default-bibliography
1586 (f-entries "." (lambda (f) (f-ext? f "bib"))))))
1587 (key (org-ref-get-bibtex-key-under-cursor)))
1588 (save-window-excursion
1589 (org-ref-open-citation-at-point)
1590 (bibtex-copy-entry-as-kill))
1592 (let ((bibtex-files (list (file-truename new-file))))
1593 (if (assoc key (bibtex-global-key-alist))
1594 (message "That key already exists in %s" new-file)
1596 (save-window-excursion
1597 (find-file new-file)
1598 (goto-char (point-max))
1599 ;; make sure we are at the beginning of a line.
1600 (unless (looking-at "^") (insert "\n\n"))
1605 (defun org-ref-get-doi-at-point ()
1606 "Get doi for key at point."
1607 (let* ((results (org-ref-get-bibtex-key-and-file))
1609 (bibfile (cdr results))
1613 (insert-file-contents bibfile)
1614 (bibtex-search-entry key)
1615 (setq doi (bibtex-autokey-get-field "doi"))
1616 ;; in case doi is a url, remove the url part.
1617 (replace-regexp-in-string "^http://dx.doi.org/" "" doi)))))
1620 ;; **** functions that operate on key at point for click menu
1621 (defun org-ref-wos-at-point ()
1622 "Open the doi in wos for bibtex key under point."
1624 (doi-utils-wos (org-ref-get-doi-at-point)))
1627 (defun org-ref-wos-citing-at-point ()
1628 "Open the doi in wos citing articles for bibtex key under point."
1630 (doi-utils-wos-citing (org-ref-get-doi-at-point)))
1633 (defun org-ref-wos-related-at-point ()
1634 "Open the doi in wos related articles for bibtex key under point."
1636 (doi-utils-wos-related (org-ref-get-doi-at-point)))
1639 (defun org-ref-google-scholar-at-point ()
1640 "Open the doi in google scholar for bibtex key under point."
1642 (doi-utils-google-scholar (org-ref-get-doi-at-point)))
1645 (defun org-ref-pubmed-at-point ()
1646 "Open the doi in pubmed for bibtex key under point."
1648 (doi-utils-pubmed (org-ref-get-doi-at-point)))
1651 (defun org-ref-crossref-at-point ()
1652 "Open the doi in crossref for bibtex key under point."
1654 (doi-utils-crossref (org-ref-get-doi-at-point)))
1656 ;; *** Minibuffer menu
1658 (defun org-ref-cite-onclick-minibuffer-menu (&optional link-string)
1659 "Action when a cite link is clicked on.
1660 Provides a menu of context sensitive actions. If the bibtex entry
1661 has a pdf, you get an option to open it. If there is a doi, you
1662 get a lot of options. LINK-STRING is used by the link function."
1664 (let* ((results (org-ref-get-bibtex-key-and-file))
1666 (pdf-file (format (concat org-ref-pdf-directory "%s.pdf") key))
1667 (bibfile (cdr results))
1668 (url (save-excursion
1670 (insert-file-contents bibfile)
1671 (bibtex-search-entry key)
1672 (bibtex-autokey-get-field "url"))))
1673 (doi (save-excursion
1675 (insert-file-contents bibfile)
1676 (bibtex-search-entry key)
1677 ;; I like this better than bibtex-url which does not always find
1679 (bibtex-autokey-get-field "doi")))))
1681 (when (string= "" doi) (setq doi nil))
1682 (when (string= "" url) (setq url nil))
1683 (setq org-ref-cite-menu-funcs '())
1689 'org-ref-cite-menu-funcs
1690 '("o" "pen" org-ref-open-citation-at-point)))
1693 (when (file-exists-p pdf-file)
1695 'org-ref-cite-menu-funcs
1696 `("p" "df" ,org-ref-open-pdf-function) t))
1700 'org-ref-cite-menu-funcs
1701 '("n" "otes" org-ref-open-notes-at-point) t)
1706 'org-ref-cite-menu-funcs
1707 '("u" "rl" org-ref-open-url-at-point) t))
1712 'org-ref-cite-menu-funcs
1713 '("w" "os" org-ref-wos-at-point) t)
1716 'org-ref-cite-menu-funcs
1717 '("c" "iting" org-ref-wos-citing-at-point) t)
1720 'org-ref-cite-menu-funcs
1721 '("r" "elated" org-ref-wos-related-at-point) t)
1724 'org-ref-cite-menu-funcs
1725 '("g" "oogle scholar" org-ref-google-scholar-at-point) t)
1728 'org-ref-cite-menu-funcs
1729 '("P" "ubmed" org-ref-pubmed-at-point) t))
1731 ;; add user functions
1732 (dolist (tup org-ref-user-cite-menu-funcs)
1734 'org-ref-cite-menu-funcs
1739 'org-ref-cite-menu-funcs
1740 '("q" "uit" (lambda ())) t)
1742 ;; now we make a menu
1743 ;; construct menu string as a message
1746 (let* ((results (org-ref-get-bibtex-key-and-file))
1748 (bibfile (cdr results)))
1751 (insert-file-contents bibfile)
1752 (bibtex-search-entry key)
1753 (org-ref-bib-citation))))
1757 (concat "[" (elt tup 0) "]"
1759 org-ref-cite-menu-funcs "")))
1761 (let* ((input (read-char-exclusive))
1763 (char-to-string input) org-ref-cite-menu-funcs)))
1764 ;; now run the function (2nd element in choice)
1771 ;; *** Generation of the cite links
1772 (defmacro org-ref-make-completion-function (type)
1773 "Macro to make a link completion function for a link of TYPE."
1774 `(defun ,(intern (format "org-%s-complete-link" type)) (&optional arg)
1780 (let ((bibtex-files (org-ref-find-bibliography)))
1781 (bibtex-global-key-alist))))))
1783 (defmacro org-ref-make-format-function (type)
1784 "Macro to make a format function for a link of TYPE."
1785 `(defun ,(intern (format "org-ref-format-%s" type)) (keyword desc format)
1790 (format "[[#%s][%s]]" key key))
1791 (org-ref-split-and-strip-string keyword) ","))
1798 (org-ref-split-and-strip-string keyword) ",") "]"))
1803 (format "<a href=\"#%s\">%s</a>" key key))
1804 (org-ref-split-and-strip-string keyword) ","))
1807 (if (string= (substring type -1) "s")
1808 ;; biblatex format for multicite commands, which all end in s. These are formated as \cites{key1}{key2}...
1809 (concat "\\" ,type (mapconcat (lambda (key) (format "{%s}" key))
1810 (org-ref-split-and-strip-string keyword) ""))
1812 (concat "\\" ,type (when desc (org-ref-format-citation-description desc)) "{"
1813 (mapconcat (lambda (key) key) (org-ref-split-and-strip-string keyword) ",")
1815 ;; for markdown we generate pandoc citations
1818 (desc ;; pre and or post text
1819 (let* ((text (split-string desc "::"))
1823 (format "[@%s," keyword)
1824 (when pre (format " %s" pre))
1825 (when post (format ", %s" post))
1830 (lambda (key) (concat "@" key))
1831 (org-ref-split-and-strip-string keyword)
1834 (defun org-ref-format-citation-description (desc)
1835 "Return formatted citation description.
1836 If the cite link has a DESC (description), it is optional text
1837 for the citation command. You can specify pre and post text by
1838 separating these with ::, for example [[cite:key][pre text::post
1841 ((string-match "::" desc)
1842 (format "[%s][%s]" (car (setq results (split-string desc "::"))) (cadr results)))
1843 (t (format "[%s]" desc))))
1845 (defun org-ref-define-citation-link (type &optional key)
1846 "Add a citation link of TYPE for org-ref.
1847 With optional KEY, set the reftex binding. For example:
1848 \(org-ref-define-citation-link \"citez\" ?z) will create a new
1849 citez link, with reftex key of z, and the completion function."
1850 (interactive "sCitation Type: \ncKey: ")
1852 ;; create the formatting function
1853 (eval `(org-ref-make-format-function ,type))
1858 org-ref-cite-onclick-function
1859 (quote ,(intern (format "org-ref-format-%s" type)))))
1861 ;; create the completion function
1862 (eval `(org-ref-make-completion-function ,type))
1864 ;; store new type so it works with adding citations, which checks
1865 ;; for existence in this list
1866 (add-to-list 'org-ref-cite-types type)
1868 ;; and finally if a key is specified, we modify the reftex menu
1870 (setf (nth 2 (assoc 'org reftex-cite-format-builtin))
1871 (append (nth 2 (assoc 'org reftex-cite-format-builtin))
1872 `((,key . ,(concat type ":%l")))))))
1874 ;; create all the link types and their completion functions
1875 (mapcar 'org-ref-define-citation-link org-ref-cite-types)
1877 (defun org-ref-insert-cite-link (alternative-cite)
1878 "Insert a default citation link using reftex.
1879 If you are on a link, it appends to the end of the link,
1880 otherwise, a new link is inserted. Use a prefix
1881 arg (ALTERNATIVE-CITE) to get a menu of citation types."
1883 (org-ref-find-bibliography)
1884 (let* ((object (org-element-context))
1885 (link-string-beginning (org-element-property :begin object))
1886 (link-string-end (org-element-property :end object))
1887 (path (org-element-property :path object)))
1889 (if (not alternative-cite)
1892 ;; case where we are in a link
1893 ((and (equal (org-element-type object) 'link)
1894 (-contains? org-ref-cite-types (org-element-property :type object)))
1895 (goto-char link-string-end)
1896 ;; sometimes there are spaces at the end of the link
1897 ;; this code moves point pack until no spaces are there
1898 (while (looking-back " ") (backward-char))
1899 (insert (concat "," (mapconcat 'identity (reftex-citation t ?a) ","))))
1901 ;; We are next to a link, and we want to append
1904 (and (equal (org-element-type (org-element-context)) 'link)
1905 (-contains? org-ref-cite-types (org-element-property :type (org-element-context)))))
1906 (while (looking-back " ") (backward-char))
1907 (insert (concat "," (mapconcat 'identity (reftex-citation t ?a) ","))))
1909 ;; insert fresh link
1912 (concat org-ref-default-citation-link
1914 (mapconcat 'identity (reftex-citation t) ",")))))
1916 ;; you pressed a C-u so we run this code
1917 (reftex-citation))))
1919 (defun org-ref-insert-cite-with-completion (type)
1920 "Insert a cite link of TYPE with completion."
1921 (interactive (list (ido-completing-read "Type: " org-ref-cite-types)))
1922 (insert (funcall (intern (format "org-%s-complete-link" type)))))
1924 (defun org-ref-store-bibtex-entry-link ()
1925 "Save a citation link to the current bibtex entry. Save in the default link type."
1927 (let ((link (concat org-ref-default-citation-link
1930 (bibtex-beginning-of-entry)
1931 (reftex-get-bib-field "=key=" (bibtex-parse-entry))))))
1932 (message "saved %s" link)
1933 (push (list link) org-stored-links)
1934 (car org-stored-links)))
1942 (lambda (path desc format)
1945 (format "\\index{%s}" path)))))
1947 ;; this will generate a temporary index of entries in the file.
1951 (let ((*index-links* '())
1952 (*initial-letters* '()))
1955 (org-element-map (org-element-parse-buffer) 'link
1957 (let ((type (nth 0 link))
1958 (plist (nth 1 link)))
1960 (when (equal (plist-get plist ':type) "index")
1963 (cons (plist-get plist :path)
1965 "[[elisp:(progn (switch-to-buffer \"%s\") (goto-char %s))][%s]]"
1967 (plist-get plist :begin) ;; position of link
1968 ;; grab a description
1970 (goto-char (plist-get plist :begin))
1971 (if (thing-at-point 'sentence)
1973 (replace-regexp-in-string
1974 "\n" "" (thing-at-point 'sentence))
1975 ;; or call it a link
1979 (setq *index-links* (cl-sort *index-links* 'string-lessp :key 'car))
1981 ;; now first letters
1982 (dolist (link *index-links*)
1983 (add-to-list '*initial-letters* (substring (car link) 0 1) t))
1985 ;; now create the index
1986 (switch-to-buffer (get-buffer-create "*index*"))
1989 (insert "#+TITLE: Index\n\n")
1990 (dolist (letter *initial-letters*)
1991 (insert (format "* %s\n" (upcase letter)))
1992 ;; now process the links
1995 (string= letter (substring (car (car *index-links*)) 0 1)))
1996 (let ((link (pop *index-links*)))
1997 (insert (format "%s %s\n\n" (car link) (cdr link))))))
1998 (switch-to-buffer "*index*")))
2000 (lambda (path desc format)
2003 (format "\\printindex")))))
2008 nil ;; no follow action
2009 (lambda (path desc format)
2012 (format "\\newglossaryentry{%s}{%s}" path desc)))))
2018 nil ;; no follow action
2019 (lambda (path desc format)
2022 (format "\\gls{%s}" path)))))
2027 nil ;; no follow action
2028 (lambda (path desc format)
2031 (format "\\glspl{%s}" path)))))
2036 nil ;; no follow action
2037 (lambda (path desc format)
2040 (format "\\Gls{%s}" path)))))
2045 nil ;; no follow action
2046 (lambda (path desc format)
2049 (format "\\Glspl{%s}" path)))))
2052 ;; ** create text citations from a bibtex entry
2053 (defun org-ref-bib-citation ()
2054 "From a bibtex entry, create and return a simple citation string.
2055 This assumes you are in an article."
2057 (bibtex-beginning-of-entry)
2058 (let* ((cb (current-buffer))
2059 (bibtex-expand-strings t)
2060 (entry (loop for (key . value) in (bibtex-parse-entry t)
2061 collect (cons (downcase key) value)))
2062 (title (replace-regexp-in-string "\n\\|\t\\|\s+" " " (reftex-get-bib-field "title" entry)))
2063 (year (reftex-get-bib-field "year" entry))
2064 (author (replace-regexp-in-string "\n\\|\t\\|\s+" " " (reftex-get-bib-field "author" entry)))
2065 (key (reftex-get-bib-field "=key=" entry))
2066 (journal (reftex-get-bib-field "journal" entry))
2067 (volume (reftex-get-bib-field "volume" entry))
2068 (pages (reftex-get-bib-field "pages" entry))
2069 (doi (reftex-get-bib-field "doi" entry))
2070 (url (reftex-get-bib-field "url" entry))
2072 ;;authors, "title", Journal, vol(iss):pages (year).
2073 (format "%s, \"%s\", %s, %s:%s (%s)"
2074 author title journal volume pages year)))
2076 (defun org-ref-bib-html-citation ()
2077 "From a bibtex entry, create and return a simple citation with html links."
2079 (bibtex-beginning-of-entry)
2080 (let* ((cb (current-buffer))
2081 (bibtex-expand-strings t)
2082 (entry (loop for (key . value) in (bibtex-parse-entry t)
2083 collect (cons (downcase key) value)))
2084 (title (replace-regexp-in-string "\n\\|\t\\|\s+" " " (reftex-get-bib-field "title" entry)))
2085 (year (reftex-get-bib-field "year" entry))
2086 (author (replace-regexp-in-string "\n\\|\t\\|\s+" " " (reftex-get-bib-field "author" entry)))
2087 (key (reftex-get-bib-field "=key=" entry))
2088 (journal (reftex-get-bib-field "journal" entry))
2089 (volume (reftex-get-bib-field "volume" entry))
2090 (pages (reftex-get-bib-field "pages" entry))
2091 (doi (reftex-get-bib-field "doi" entry))
2092 (url (reftex-get-bib-field "url" entry))
2094 ;;authors, "title", Journal, vol(iss):pages (year).
2095 (concat (format "%s, \"%s\", %s, %s:%s (%s)."
2096 author title journal volume pages year)
2097 (when url (format " <a href=\"%s\">link</a>" url))
2098 (when doi (format " <a href=\"http://dx.doi.org/%s\">doi</a>" doi)))
2101 ;; ** Open pdf in bibtex entry
2102 (defun org-ref-open-bibtex-pdf ()
2103 "Open pdf for a bibtex entry, if it exists.
2105 the entry of interest in the bibfile. but does not check that."
2108 (bibtex-beginning-of-entry)
2109 (let* ((bibtex-expand-strings t)
2110 (entry (bibtex-parse-entry t))
2111 (key (reftex-get-bib-field "=key=" entry))
2112 (pdf (format (concat org-ref-pdf-directory "%s.pdf") key)))
2114 (if (file-exists-p pdf)
2115 (org-open-link-from-string (format "[[file:%s]]" pdf))
2118 ;; ** Open notes from bibtex entry
2120 (defun org-ref-open-bibtex-notes ()
2121 "From a bibtex entry, open the notes if they exist, and create a heading if they do not.
2123 I never did figure out how to use reftex to make this happen
2124 non-interactively. the reftex-format-citation function did not
2125 work perfectly; there were carriage returns in the strings, and
2126 it did not put the key where it needed to be. so, below I replace
2127 the carriage returns and extra spaces with a single space and
2128 construct the heading by hand."
2131 (bibtex-beginning-of-entry)
2132 (let* ((cb (current-buffer))
2133 (bibtex-expand-strings t)
2134 (entry (loop for (key . value) in (bibtex-parse-entry t)
2135 collect (cons (downcase key) value)))
2136 (title (replace-regexp-in-string "\n\\|\t\\|\s+" " " (reftex-get-bib-field "title" entry)))
2137 (year (reftex-get-bib-field "year" entry))
2138 (author (replace-regexp-in-string "\n\\|\t\\|\s+" " " (reftex-get-bib-field "author" entry)))
2139 (key (reftex-get-bib-field "=key=" entry))
2140 (journal (reftex-get-bib-field "journal" entry))
2141 (volume (reftex-get-bib-field "volume" entry))
2142 (pages (reftex-get-bib-field "pages" entry))
2143 (doi (reftex-get-bib-field "doi" entry))
2144 (url (reftex-get-bib-field "url" entry))
2147 ;; save key to clipboard to make saving pdf later easier by pasting.
2150 (kill-ring-save (point-min) (point-max)))
2152 ;; now look for entry in the notes file
2153 (if org-ref-bibliography-notes
2154 (find-file-other-window org-ref-bibliography-notes)
2155 (error "Org-ref-bib-bibliography-notes is not set to anything"))
2157 (goto-char (point-min))
2158 ;; put new entry in notes if we don't find it.
2159 (if (re-search-forward (format ":Custom_ID: %s$" key) nil 'end)
2160 (funcall org-ref-open-notes-function)
2161 ;; no entry found, so add one
2162 (insert (format "\n** TODO %s - %s" year title))
2174 [[cite:%s]] [[file:%s/%s.pdf][pdf]]\n\n"
2175 key author journal year volume pages doi url key org-ref-pdf-directory key))
2178 (defun org-ref-open-notes-from-reftex ()
2179 "Call reftex, and open notes for selected entry."
2181 (let ((bibtex-key )))
2183 ;; now look for entry in the notes file
2184 (if org-ref-bibliography-notes
2185 (find-file-other-window org-ref-bibliography-notes)
2186 (error "Org-ref-bib-bibliography-notes is not set to anything"))
2188 (goto-char (point-min))
2190 (re-search-forward (format
2192 (first (reftex-citation t)) nil 'end))
2193 (funcall org-ref-open-notes-function))
2195 ;; ** Open bibtex entry in browser
2196 (defun org-ref-open-in-browser ()
2197 "Open the bibtex entry at point in a browser using the url field or doi field."
2200 (bibtex-beginning-of-entry)
2202 (let ((url (bibtex-autokey-get-field "url")))
2207 (let ((doi (bibtex-autokey-get-field "doi")))
2209 (if (string-match "^http" doi)
2211 (browse-url (format "http://dx.doi.org/%s" doi)))
2213 (message "No url or doi found"))))
2215 ;; ** upload entry to citeulike
2217 (defun org-ref-upload-bibtex-entry-to-citeulike ()
2218 "With point in a bibtex entry get bibtex string and submit to citeulike.
2220 Relies on the python script /upload_bibtex_citeulike.py being in the user directory."
2222 (message "uploading to citeulike")
2224 (bibtex-narrow-to-entry)
2225 (let ((startpos (point-min))
2226 (endpos (point-max))
2227 (bibtex-string (buffer-string))
2228 (script (concat "python " starter-kit-dir "/upload_bibtex_citeulike.py&")))
2229 (with-temp-buffer (insert bibtex-string)
2230 (shell-command-on-region (point-min) (point-max) script t nil nil t)))))
2232 ;; ** Build a pdf of the bibtex file
2233 (defun org-ref-build-full-bibliography ()
2234 "Build pdf of all bibtex entries, and open it."
2236 (let* ((bibfile (file-name-nondirectory (buffer-file-name)))
2237 (bib-base (file-name-sans-extension bibfile))
2238 (texfile (concat bib-base ".tex"))
2239 (pdffile (concat bib-base ".pdf")))
2242 (insert (format "\\documentclass[12pt]{article}
2243 \\usepackage[version=3]{mhchem}
2245 \\usepackage[numbers]{natbib}
2246 \\usepackage[colorlinks=true, linkcolor=blue, urlcolor=blue, pdfstartview=FitH]{hyperref}
2250 \\bibliographystyle{unsrtnat}
2252 \\end{document}" bib-base))
2254 (shell-command (concat "pdflatex " bib-base))
2255 (shell-command (concat "bibtex " bib-base))
2256 (shell-command (concat "pdflatex " bib-base))
2257 (shell-command (concat "pdflatex " bib-base))
2258 (kill-buffer texfile)
2259 (org-open-file pdffile)
2262 ;; ** Extract bibtex entries in org-file
2264 (defun org-ref-extract-bibtex-entries ()
2265 "Extract the bibtex entries referred to by cite links in the current buffer into a src block at the bottom of the current buffer.
2267 If no bibliography is in the buffer the variable
2268 `reftex-default-bibliography' is used."
2270 (let* ((temporary-file-directory (file-name-directory (buffer-file-name)))
2271 (tempname (make-temp-file "extract-bib"))
2272 (contents (buffer-string))
2273 (cb (current-buffer))
2274 basename texfile bibfile results)
2276 ;; open tempfile and insert org-buffer contents
2277 (find-file tempname)
2279 (setq basename (file-name-sans-extension
2280 (file-name-nondirectory buffer-file-name))
2281 texfile (concat tempname ".tex")
2282 bibfile (concat tempname ".bib"))
2284 ;; see if we have a bibliography, and insert the default one if not.
2286 (goto-char (point-min))
2287 (unless (re-search-forward "^bibliography:" (point-max) 'end)
2288 (insert (format "\nbibliography:%s"
2289 (mapconcat 'identity reftex-default-bibliography ",")))))
2292 ;; get a latex file and extract the references
2293 (org-latex-export-to-latex)
2296 (reftex-create-bibtex-file bibfile)
2298 ;; save results of the references
2299 (setq results (buffer-string))
2301 ;; kill buffers. these are named by basename, not full path
2302 (kill-buffer (concat basename ".bib"))
2303 (kill-buffer (concat basename ".tex"))
2304 (kill-buffer basename)
2306 (delete-file bibfile)
2307 (delete-file texfile)
2308 (delete-file tempname)
2310 ;; Now back to the original org buffer and insert the results
2311 (switch-to-buffer cb)
2312 (when (not (string= "" results))
2314 (goto-char (point-max))
2316 (org-insert-heading)
2317 (insert (format " Bibtex entries
2319 #+BEGIN_SRC text :tangle %s
2321 #+END_SRC" (concat (file-name-sans-extension (file-name-nondirectory (buffer-file-name))) ".bib") results))))))
2323 ;; ** Find bad citations
2326 (defun index (substring list)
2327 "Return the index of SUBSTRING in a LIST of strings."
2330 (dolist (arg list i)
2331 (if (string-match (concat "^" substring "$") arg)
2336 ;; return counter if found, otherwise return nil
2340 (defun org-ref-find-bad-citations ()
2341 "Create a list of citation keys in an org-file that do not have a bibtex entry in the known bibtex files.
2343 Makes a new buffer with clickable links."
2345 ;; generate the list of bibtex-keys and cited keys
2346 (let* ((bibtex-files (org-ref-find-bibliography))
2347 (bibtex-file-path (mapconcat (lambda (x) (file-name-directory (file-truename x))) bibtex-files ":"))
2348 (bibtex-keys (mapcar (lambda (x) (car x)) (bibtex-global-key-alist)))
2349 (bad-citations '()))
2351 (org-element-map (org-element-parse-buffer) 'link
2353 (let ((plist (nth 1 link)))
2354 (when (-contains? org-ref-cite-types (plist-get plist :type))
2355 (dolist (key (org-ref-split-and-strip-string (plist-get plist :path)))
2356 (when (not (index key bibtex-keys))
2361 `(,(format "%s [[elisp:(progn (switch-to-buffer-other-frame \"%s\")(goto-char %s))][not found here]]\n"
2364 (plist-get plist :begin)))))
2366 ;; set with-affilates to t to get citations in a caption
2371 (switch-to-buffer-other-window "*Missing citations*")
2374 (insert "* List of bad cite links\n")
2375 (insert (mapconcat 'identity bad-citations ""))
2376 ;(setq buffer-read-only t)
2377 (use-local-map (copy-keymap org-mode-map))
2378 (local-set-key "q" #'(lambda () (interactive) (kill-buffer))))
2380 (when (get-buffer "*Missing citations*")
2381 (kill-buffer "*Missing citations*"))
2382 (message "No bad cite links found"))))
2384 ;; ** helm interface to bad citations, labels, refs in orgfile
2385 (defun org-ref-bad-cite-candidates ()
2386 "Return a list of conses (key . marker) where key does not exist in the known bibliography files, and marker points to the key."
2387 (let* ((cp (point)) ; save to return to later
2388 (bibtex-files (org-ref-find-bibliography))
2389 (bibtex-file-path (mapconcat
2391 (file-name-directory (file-truename x)))
2393 (bibtex-keys (mapcar (lambda (x) (car x))
2394 (bibtex-global-key-alist)))
2395 (bad-citations '()))
2397 (org-element-map (org-element-parse-buffer) 'link
2399 (let ((plist (nth 1 link)))
2400 (when (-contains? org-ref-cite-types (plist-get plist :type))
2401 (dolist (key (org-ref-split-and-strip-string (plist-get plist :path)) )
2402 (when (not (index key bibtex-keys))
2403 (goto-char (plist-get plist :begin))
2404 (re-search-forward key)
2405 (push (cons key (point-marker)) bad-citations)))
2407 ;; add with-affiliates to get cites in caption
2413 (defun org-ref-bad-ref-candidates ()
2414 "Return a list of conses (ref . marker) where ref is a ref link that does not point to anything (i.e. a label)."
2415 ;; first get a list of legitimate labels
2417 (labels (org-ref-get-labels))
2419 ;; now loop over ref links
2420 (goto-char (point-min))
2421 (org-element-map (org-element-parse-buffer) 'link
2423 (let ((plist (nth 1 link)))
2424 (when (or (equal (plist-get plist ':type) "ref")
2425 (equal (plist-get plist ':type) "eqref")
2426 (equal (plist-get plist ':type) "pageref")
2427 (equal (plist-get plist ':type) "nameref"))
2428 (unless (-contains? labels (plist-get plist :path))
2429 (goto-char (plist-get plist :begin))
2432 (cons (plist-get plist :path)
2433 (point-marker))))))))
2438 (defun org-ref-bad-label-candidates ()
2439 "Return a list of labels where label is multiply defined."
2440 (let ((labels (org-ref-get-labels))
2441 (multiple-labels '()))
2442 (when (not (= (length labels)
2443 (length (-uniq labels))))
2444 (dolist (label labels)
2445 (when (> (-count (lambda (a)
2448 ;; this is a multiply defined label.
2450 (goto-char (point-min))
2451 (while (re-search-forward
2452 (format "[^#+]label:%s\\s-" label) nil t)
2453 (push (cons label (point-marker)) multiple-labels))
2455 (goto-char (point-min))
2456 (while (re-search-forward
2457 (format "\\label{%s}\\s-?" label) nil t)
2458 (push (cons label (point-marker)) multiple-labels))
2460 (goto-char (point-min))
2461 (while (re-search-forward
2462 (format "^#\\+label:\\s-*%s" label) nil t)
2463 (push (cons label (point-marker)) multiple-labels))
2465 (goto-char (point-min))
2466 (while (re-search-forward
2467 (format "^#\\+tblname:\\s-*%s" label) nil t)
2468 (push (cons label (point-marker)) multiple-labels))
2474 "Opens a helm interface to actions for org-ref.
2475 Shows bad citations, ref links and labels"
2477 (let ((cb (current-buffer))
2478 (bad-citations (org-ref-bad-cite-candidates))
2479 (bad-refs (org-ref-bad-ref-candidates))
2480 (bad-labels (org-ref-bad-label-candidates)))
2482 (helm :sources `(((name . "Bad citations")
2483 (candidates . ,bad-citations)
2484 (action . (lambda (marker)
2485 (switch-to-buffer (marker-buffer marker))
2486 (goto-char marker))))
2488 ((name . "Bad Labels")
2489 (candidates . ,bad-labels)
2490 (action . (lambda (marker)
2491 (switch-to-buffer (marker-buffer marker))
2492 (goto-char marker))))
2494 ((name . "Bad ref links")
2495 (candidates . ,bad-refs)
2496 (action . (lambda (marker)
2497 (switch-to-buffer (marker-buffer marker))
2498 (goto-char marker))))
2500 ((name . "Utilities")
2501 (candidates . (("Check buffer again" . org-ref)
2502 ("Insert citation" . helm-bibtex)
2503 ("Insert label link" . org-ref-helm-insert-label-link)
2504 ("Insert ref link" . org-ref-helm-insert-ref-link)
2505 ("List of figures" . org-ref-list-of-figures)
2506 ("List of tables" . org-ref-list-of-tables)
2507 ("Table of contents" . nil)
2509 (action . (lambda (x)
2510 (switch-to-buffer ,cb)
2513 ((name . "Export functions")
2514 (candidates . (("Extract cited entries" . org-ref-extract-bibtex-entries)
2515 ("Export to html and open" . (lambda () (org-open-file (org-html-export-to-html))))
2516 ("Export to pdf and open" . (lambda ()
2517 (org-open-file (org-latex-export-to-pdf))))
2518 ("Export to manuscript pdf and open" . ox-manuscript-export-and-build-and-open)
2519 ("Export submission manuscript pdf and open" . ox-manuscript-build-submission-manuscript-and-open)
2522 (action . (lambda (x)
2523 (switch-to-buffer ,cb)
2527 ;; ** Find non-ascii charaters
2528 (defun org-ref-find-non-ascii-characters ()
2529 "Find non-ascii characters in the buffer. Useful for cleaning up bibtex files."
2531 (occur "[^[:ascii:]]"))
2533 ;; ** Sort fields in a bibtex entry
2535 (defun org-ref-sort-bibtex-entry ()
2536 "Sort fields of entry in standard order and downcase them."
2538 (bibtex-beginning-of-entry)
2539 (let* ((master '("author" "title" "journal" "volume" "number" "pages" "year" "doi" "url"))
2540 (entry (bibtex-parse-entry))
2543 (type (cdr (assoc "=type=" entry)))
2544 (key (cdr (assoc "=key=" entry))))
2546 ;; these are the fields we want to order that are in this entry
2547 (setq entry-fields (mapcar (lambda (x) (car x)) entry))
2548 ;; we do not want to reenter these fields
2549 (setq entry-fields (remove "=key=" entry-fields))
2550 (setq entry-fields (remove "=type=" entry-fields))
2552 ;;these are the other fields in the entry
2553 (setq other-fields (remove-if-not (lambda(x) (not (member x master))) entry-fields))
2556 ;; right now we only resort articles
2557 ((string= (downcase type) "article")
2560 (concat "@article{" key ",\n"
2563 (when (member field entry-fields)
2564 (format "%s = %s," (downcase field) (cdr (assoc field entry))))) master "\n")
2567 (format "%s = %s," (downcase field) (cdr (assoc field entry)))) other-fields "\n")
2569 (bibtex-find-entry key)
2571 (bibtex-clean-entry)
2574 ;; ** Clean a bibtex entry
2575 (defun org-ref-clean-bibtex-entry(&optional keep-key)
2576 "clean and replace the key in a bibtex function. When keep-key is t, do not replace it. You can use a prefix to specify the key should be kept"
2578 (bibtex-beginning-of-entry)
2580 ;; some entries do not have a key or comma in first line. We check and add it, if needed.
2581 (unless (string-match ",$" (thing-at-point 'line))
2585 ;; check for empty pages, and put eid or article id in its place
2586 (let ((entry (bibtex-parse-entry))
2587 (pages (bibtex-autokey-get-field "pages"))
2588 (year (bibtex-autokey-get-field "year"))
2589 (doi (bibtex-autokey-get-field "doi"))
2590 ;; The Journal of Chemical Physics uses eid
2591 (eid (bibtex-autokey-get-field "eid")))
2593 ;; replace http://dx.doi.org/ in doi. some journals put that in,
2594 ;; but we only want the doi.
2595 (when (string-match "^http://dx.doi.org/" doi)
2596 (bibtex-beginning-of-entry)
2597 (goto-char (car (cdr (bibtex-search-forward-field "doi" t))))
2599 (bibtex-make-field "doi")
2601 (insert (replace-regexp-in-string "^http://dx.doi.org/" "" doi)))
2603 ;; asap articles often set year to 0, which messes up key
2604 ;; generation. fix that.
2605 (when (string= "0" year)
2606 (bibtex-beginning-of-entry)
2607 (goto-char (car (cdr (bibtex-search-forward-field "year" t))))
2609 (bibtex-make-field "year")
2611 (insert (read-string "Enter year: ")))
2613 ;; fix pages if they are empty if there is an eid to put there.
2614 (when (string= "-" pages)
2616 (bibtex-beginning-of-entry)
2617 ;; this seems like a clunky way to set the pages field.But I
2618 ;; cannot find a better way.
2619 (goto-char (car (cdr (bibtex-search-forward-field "pages" t))))
2621 (bibtex-make-field "pages")
2625 ;; replace naked & with \&
2627 (bibtex-narrow-to-entry)
2628 (bibtex-beginning-of-entry)
2629 (message "checking &")
2630 (replace-regexp " & " " \\\\& ")
2633 ;; generate a key, and if it duplicates an existing key, edit it.
2635 (let ((key (bibtex-generate-autokey)))
2637 ;; first we delete the existing key
2638 (bibtex-beginning-of-entry)
2639 (re-search-forward bibtex-entry-maybe-empty-head)
2640 (if (match-beginning bibtex-key-in-head)
2641 (delete-region (match-beginning bibtex-key-in-head)
2642 (match-end bibtex-key-in-head)))
2643 ;; check if the key is in the buffer
2644 (when (save-excursion
2645 (bibtex-search-entry key))
2647 (bibtex-search-entry key)
2648 (bibtex-copy-entry-as-kill)
2649 (switch-to-buffer-other-window "*duplicate entry*")
2651 (setq key (bibtex-read-key "Duplicate Key found, edit: " key)))
2654 (kill-new key))) ;; save key for pasting
2656 ;; run hooks. each of these operates on the entry with no arguments.
2657 ;; this did not work like i thought, it gives a symbolp error.
2658 ;; (run-hooks org-ref-clean-bibtex-entry-hook)
2663 org-ref-clean-bibtex-entry-hook)
2665 ;; sort fields within entry
2666 (org-ref-sort-bibtex-entry)
2667 ;; check for non-ascii characters
2668 (occur "[^[:ascii:]]")
2671 (defun org-ref-get-citation-year (key)
2672 "Get the year of an entry with KEY. Return year as a string."
2673 (let* ((results (org-ref-get-bibtex-key-and-file key))
2674 (bibfile (cdr results)))
2676 (insert-file-contents bibfile)
2677 (bibtex-search-entry key nil 0)
2678 (prog1 (reftex-get-bib-field "year" (bibtex-parse-entry t))
2681 ;; ** Sort cite in cite link
2682 (defun org-ref-sort-citation-link ()
2683 "Replace link at point with sorted link by year."
2685 (let* ((object (org-element-context))
2686 (type (org-element-property :type object))
2687 (begin (org-element-property :begin object))
2688 (end (org-element-property :end object))
2689 (link-string (org-element-property :path object))
2691 (setq keys (org-ref-split-and-strip-string link-string))
2692 (setq years (mapcar 'org-ref-get-citation-year keys))
2693 (setq data (mapcar* (lambda (a b) `(,a . ,b)) years keys))
2694 (setq data (cl-sort data (lambda (x y) (< (string-to-int (car x)) (string-to-int (car y))))))
2695 ;; now get the keys separated by commas
2696 (setq keys (mapconcat (lambda (x) (cdr x)) data ","))
2697 ;; and replace the link with the sorted keys
2698 (cl--set-buffer-substring begin end (concat type ":" keys))))
2700 ;; ** Shift-arrow sorting of keys in a cite link
2702 (defun org-ref-swap-keys (i j keys)
2703 "Swap the KEYS in a list with index I and J."
2704 (let ((tempi (nth i keys)))
2705 (setf (nth i keys) (nth j keys))
2706 (setf (nth j keys) tempi))
2710 (defun org-ref-swap-citation-link (direction)
2711 "Move citation at point in DIRECTION +1 is to the right, -1 to the left."
2713 (let* ((object (org-element-context))
2714 (type (org-element-property :type object))
2715 (begin (org-element-property :begin object))
2716 (end (org-element-property :end object))
2717 (link-string (org-element-property :path object))
2719 ;; We only want this to work on citation links
2720 (when (-contains? org-ref-cite-types type)
2721 (setq key (org-ref-get-bibtex-key-under-cursor))
2722 (setq keys (org-ref-split-and-strip-string link-string))
2723 (setq i (index key keys)) ;; defined in org-ref
2724 (if (> direction 0) ;; shift right
2725 (org-ref-swap-keys i (+ i 1) keys)
2726 (org-ref-swap-keys i (- i 1) keys))
2727 (setq keys (mapconcat 'identity keys ","))
2728 ;; and replace the link with the sorted keys
2729 (cl--set-buffer-substring
2733 ;; It seems the space at the end can get consumed, so we see if there
2734 ;; is a space, and add it if so. Sometimes there is a comma or period,
2735 ;; then we do not want a space.
2739 (looking-back " ")) " ")))
2740 ;; now go forward to key so we can move with the key
2741 (re-search-forward key)
2742 (goto-char (match-beginning 0)))))
2744 ;; add hooks to make it work
2745 (add-hook 'org-shiftright-hook (lambda () (org-ref-swap-citation-link 1)))
2746 (add-hook 'org-shiftleft-hook (lambda () (org-ref-swap-citation-link -1)))
2748 ;; ** context around org-ref links
2749 (defun org-ref-get-label-context (label)
2750 "Return a string of context around a LABEL."
2753 (goto-char (point-min))
2754 (when (re-search-forward
2755 (format "label:%s\\b" label) nil t)
2756 (throw 'result (buffer-substring
2765 (goto-char (point-min))
2766 (when (re-search-forward
2767 (format "\\label{%s}" label) nil t)
2768 (throw 'result (buffer-substring
2777 (goto-char (point-min))
2778 (when (re-search-forward
2779 (format "^#\\+label:\\s-*\\(%s\\)\\b" label) nil t)
2780 (throw 'result (buffer-substring
2789 (goto-char (point-min))
2790 (when (re-search-forward
2791 (format "^#\\+tblname:\\s-*\\(%s\\)\\b" label) nil t)
2792 (throw 'result (buffer-substring
2800 (throw 'result "!!! NO CONTEXT FOUND !!!"))))
2803 (defun org-ref-link-message ()
2804 "Print a minibuffer message about the link that point is on."
2806 (when (eq major-mode 'org-mode)
2807 (let* ((object (org-element-context))
2808 (type (org-element-property :type object)))
2812 ((-contains? org-ref-cite-types type)
2813 (message (org-ref-get-citation-string-at-point)))
2815 ;; message some context about the label we are referring to
2816 ((string= type "ref")
2817 (message "%scount: %s"
2818 (org-ref-get-label-context
2819 (org-element-property :path object))
2820 (org-ref-count-labels
2821 (org-element-property :path object))))
2823 ((string= type "eqref")
2824 (message "%scount: %s"
2825 (org-ref-get-label-context
2826 (org-element-property :path object))
2827 (org-ref-count-labels
2828 (org-element-property :path object))))
2830 ;; message the count
2831 ((string= type "label")
2832 (let ((count (org-ref-count-labels
2833 (org-element-property :path object))))
2834 ;; get plurality on occurrence correct
2836 (number-to-string count)
2838 (when (or (= count 0)
2842 ((string= type "custom-id")
2844 (org-open-link-from-string
2845 (format "[[#%s]]" (org-element-property :path object)))
2846 (message "%s" (org-get-heading))))
2848 ;; check if the bibliography files exist.
2849 ((string= type "bibliography")
2851 ;; object is the link you clicked on
2852 (object (org-element-context))
2853 (link-string (org-element-property :path object))
2854 (link-string-beginning)
2857 (goto-char (org-element-property :begin object))
2858 (search-forward link-string nil nil 1)
2859 (setq link-string-beginning (match-beginning 0))
2860 (setq link-string-end (match-end 0)))
2862 ;; make sure we are in link and not before the :
2863 (when (> link-string-beginning (point))
2864 (goto-char link-string-beginning))
2866 ;; now if we have comma separated bibliographies
2867 ;; we find the one clicked on. we want to
2868 ;; search forward to next comma from point
2870 (if (search-forward "," link-string-end 1 1)
2871 (setq key-end (- (match-end 0) 1)) ; we found a match
2872 (setq key-end (point)))) ; no comma found so take the point
2874 ;; and backward to previous comma from point
2876 (if (search-backward "," link-string-beginning 1 1)
2877 (setq key-beginning (+ (match-beginning 0) 1)) ; we found a match
2878 (setq key-beginning (point)))) ; no match found
2879 ;; save the key we clicked on.
2881 (org-ref-strip-string
2882 (buffer-substring key-beginning key-end)))
2883 (if (file-exists-p bibfile)
2884 (message "%s exists." bibfile)
2885 (message "!!! %s NOT FOUND !!!" bibfile))))
2889 (defalias 'oro 'org-ref-open-citation-at-point)
2890 (defalias 'orc 'org-ref-citation-at-point)
2891 (defalias 'orp 'org-ref-open-pdf-at-point)
2892 (defalias 'oru 'org-ref-open-url-at-point)
2893 (defalias 'orn 'org-ref-open-notes-at-point)
2894 (defalias 'ornr 'org-ref-open-notes-from-reftex)
2896 (defalias 'orib 'org-ref-insert-bibliography-link)
2897 (defalias 'oric 'org-ref-insert-cite-link)
2898 (defalias 'orir 'org-ref-insert-ref-link)
2899 (defalias 'orsl 'org-ref-store-bibtex-entry-link)
2901 (defalias 'orcb 'org-ref-clean-bibtex-entry)
2903 ;; * Helm bibtex setup
2905 (setq helm-bibtex-additional-search-fields '(keywords))
2907 (defun helm-bibtex-candidates-formatter (candidates source)
2908 "Formats BibTeX entries for display in results list.
2909 Argument CANDIDATES helm candidates.
2910 Argument SOURCE the helm source."
2912 with width = (with-helm-window (helm-bibtex-window-width))
2913 for entry in candidates
2914 for entry = (cdr entry)
2915 for entry-key = (helm-bibtex-get-value "=key=" entry)
2916 if (assoc-string "author" entry 'case-fold)
2917 for fields = '("author" "title" "year" "=has-pdf=" "=has-note=" "=type=")
2919 for fields = '("editor" "title" "year" "=has-pdf=" "=has-note=" "=type=")
2920 for fields = (--map (helm-bibtex-clean-string
2921 (helm-bibtex-get-value it entry " "))
2923 for fields = (-update-at 0 'helm-bibtex-shorten-authors fields)
2924 for fields = (append fields
2925 (list (or (helm-bibtex-get-value "keywords" entry)
2928 (cons (s-format "$0 $1 $2 $3 $4$5 $6" 'elt
2929 (-zip-with (lambda (f w) (truncate-string-to-width f w 0 ?\s))
2930 fields (list 36 (- width 85) 4 1 1 7 7)))
2933 ;; * org-ref bibtex keywords
2934 ;; adapted from bibtex-utils.el
2935 ;; these are candidates for selecting keywords/tags
2936 (defun org-ref-bibtex-keywords ()
2937 "Get keywords defined in current bibtex file.
2938 These are in the keywords field, and are comma or semicolon separated."
2940 (goto-char (point-min))
2941 (let (keywords kstring)
2942 (while (re-search-forward "^\\s-*keywords.*{\\([^}]+\\)}" nil t)
2943 ;; TWS - remove newlines/multiple spaces:
2944 (setq kstring (replace-regexp-in-string "[ \t\n]+" " " (match-string 1)))
2947 (add-to-list 'keywords v t))
2948 (split-string kstring "\\(,\\|;\\)[ \n]*\\|{\\|}" t)))
2952 (defun org-ref-set-bibtex-keywords (keywords &optional arg)
2953 "Add KEYWORDS to a bibtex entry.
2954 If KEYWORDS is a list, it is converted to a comma-separated
2955 string. The KEYWORDS are added to the beginning of the
2956 field. Otherwise KEYWORDS should be a string of comma-separate
2957 keywords. Optional argument ARG prefix arg to replace keywords."
2958 (interactive "sKeywords: \nP")
2963 (if (listp keywords)
2964 (mapconcat 'identity keywords ", ")
2966 ;; else concatentate
2968 (if (listp keywords)
2969 (mapconcat 'identity keywords ", ")
2971 (when (not (string= "" (bibtex-autokey-get-field "keywords")))
2972 (concat ", " (bibtex-autokey-get-field "keywords"))))))
2976 (defun helm-tag-bibtex-entry ()
2977 "Helm interface to add keywords to a bibtex entry.
2978 Run this with the point in a bibtex entry."
2980 (let ((keyword-source `((name . "Existing keywords")
2981 (candidates . ,(org-ref-bibtex-keywords))
2982 (action . (lambda (candidate)
2983 (org-ref-set-bibtex-keywords
2986 (helm-marked-candidates)
2988 (fallback-source `((name . "Add new keywords")
2990 (action . (lambda (candidate)
2991 (org-ref-set-bibtex-keywords helm-pattern)
2993 (helm :sources '(keyword-source fallback-source))))
2995 (defun helm-bibtex-show-entry (key)
2996 "Show the entry for KEY in the BibTeX file.
2997 The original function in `helm-bibtex' has a bug where it finds the
2998 first key that partially matches. This version avoids that."
3000 (dolist (bibtex-file (if (listp helm-bibtex-bibliography)
3001 helm-bibtex-bibliography
3002 (list helm-bibtex-bibliography)))
3003 (let ((buf (helm-bibtex-buffer-visiting bibtex-file))
3005 (find-file bibtex-file)
3007 (lambda (key start end)
3008 (add-to-list 'entries (cons key start))))
3009 (if (assoc key entries)
3011 (goto-char (cdr (assoc key entries)))
3016 (defun org-ref-helm-tag-entries (candidates)
3017 "Set tags on selected bibtex entries from `helm-bibtex'.
3018 User is prompted for tags. This function is called from `helm-bibtex'.
3019 Argument CANDIDATES helm candidates."
3021 (let ((keywords (read-input "Keywords (comma separated): ")))
3022 (loop for key in (helm-marked-candidates)
3024 (save-window-excursion
3025 (helm-bibtex-show-entry key)
3030 ", " (bibtex-autokey-get-field "keywords")))
3033 (setq helm-source-bibtex
3034 '((name . "BibTeX entries")
3035 (init . helm-bibtex-init)
3036 (candidates . helm-bibtex-candidates)
3037 (filtered-candidate-transformer . helm-bibtex-candidates-formatter)
3038 (action . (("Insert citation" . helm-bibtex-insert-citation)
3039 ("Show entry" . helm-bibtex-show-entry)
3040 ("Open PDF file (if present)" . helm-bibtex-open-pdf)
3041 ("Open URL or DOI in browser" . helm-bibtex-open-url-or-doi)
3042 ("Insert formatted reference" . helm-bibtex-insert-reference)
3043 ("Insert BibTeX key" . helm-bibtex-insert-key)
3044 ("Insert BibTeX entry" . helm-bibtex-insert-bibtex)
3045 ("Attach PDF to email" . helm-bibtex-add-PDF-attachment)
3046 ("Edit notes" . helm-bibtex-edit-notes)
3047 ("Add keywords to entries" . org-ref-helm-tag-entries)
3050 (defun helm-bibtex-format-org-ref (keys)
3051 "Insert selected KEYS as cite link. Append KEYS if you are on a link.
3052 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.
3054 In the helm-bibtex buffer, C-u will give you a helm menu to select a new link type for the selected entries.
3056 C-u C-u will change the key at point to the selected keys."
3057 (let* ((object (org-element-context))
3058 (last-char (save-excursion
3059 (goto-char (org-element-property :end object))
3061 (if (looking-at " ")
3065 ;; case where we are in a link
3066 ((and (equal (org-element-type object) 'link)
3069 (org-element-property :type object)))
3071 ;; no prefix. append keys
3072 ((equal helm-current-prefix-arg nil)
3073 (goto-char (org-element-property :end object))
3074 (while (looking-back " ") (backward-char))
3075 (insert (concat "," (mapconcat 'identity keys ","))))
3076 ;; double prefix, replace key at point
3077 ((equal helm-current-prefix-arg '(16))
3078 (setf (buffer-substring
3079 (org-element-property :begin object)
3080 (org-element-property :end object))
3082 (replace-regexp-in-string
3083 (car (org-ref-get-bibtex-key-and-file)) ; key
3084 (mapconcat 'identity keys ",") ; new keys
3085 (org-element-property :raw-link object))
3086 ;; replace space at end to avoid collapsing into next word.
3088 ;; and we want to go to the end of the new link
3090 (org-element-property :end (org-element-context))))
3092 (message "Not found"))))
3094 ;; We are next to a link, and we want to append
3095 ;; next to a link means one character back is on a link.
3098 (and (equal (org-element-type (org-element-context)) 'link)
3101 (org-element-property :type (org-element-context)))))
3102 (while (looking-back " ") (backward-char))
3103 (insert (concat "," (mapconcat 'identity keys ","))))
3105 ;; insert fresh link
3107 ;;(message-box "fresh link")
3109 (concat (if (equal helm-current-prefix-arg '(4))
3110 (helm :sources `((name . "link types")
3111 (candidates . ,org-ref-cite-types)
3112 (action . (lambda (x) x))))
3113 org-ref-default-citation-link)
3115 (s-join "," keys))))))
3116 ;; return empty string for helm
3119 (setq helm-bibtex-format-citation-functions
3120 '((org-mode . helm-bibtex-format-org-ref)))
3123 (defun org-ref-helm-insert-cite-link (arg)
3124 "org-ref function to use helm-bibtex to insert a citation link.
3125 With one prefix arg, insert a ref link.
3126 With two prefix args, insert a label link."
3130 (let ((helm-bibtex-bibliography (org-ref-find-bibliography)))
3133 (org-ref-helm-insert-ref-link))
3135 (org-ref-helm-insert-label-link))))
3138 ;; add our own fallback entries where we want them. These appear in reverse order of adding in the menu
3139 (setq helm-bibtex-fallback-options
3140 (-insert-at 1 '("Crossref" . "http://search.crossref.org/?q=%s") helm-bibtex-fallback-options))
3142 (setq helm-bibtex-fallback-options
3145 '("Scopus" . "http://www.scopus.com/scopus/search/submit/xadvanced.url?searchfield=TITLE-ABS-KEY(%s)")
3146 helm-bibtex-fallback-options))
3148 (setq helm-bibtex-fallback-options
3149 (-insert-at 1 '("WOS" . "http://gateway.webofknowledge.com/gateway/Gateway.cgi?topic=%s&GWVersion=2&SrcApp=WEB&SrcAuth=HSB&DestApp=UA&DestLinkType=GeneralSearchSummary") helm-bibtex-fallback-options))
3151 (defun org-ref-get-citation-string-at-point ()
3152 "Get a string of a formatted citation."
3153 (let* ((results (org-ref-get-bibtex-key-and-file))
3155 (bibfile (cdr results)))
3159 (insert-file-contents bibfile)
3160 (bibtex-search-entry key)
3161 (org-ref-bib-citation)))
3162 "!!! No entry found !!!" )))
3165 (defun org-ref-cite-candidates ()
3166 "Generate the list of possible candidates for click actions on a cite link.
3167 Checks for pdf and doi, and add appropriate functions."
3168 (let* ((results (org-ref-get-bibtex-key-and-file))
3170 (pdf-file (format (concat org-ref-pdf-directory "%s.pdf") key))
3171 (bibfile (cdr results))
3172 (url (save-excursion
3174 (insert-file-contents bibfile)
3175 (bibtex-search-entry key)
3176 (bibtex-autokey-get-field "url"))))
3177 (doi (save-excursion
3179 (insert-file-contents bibfile)
3180 (bibtex-search-entry key)
3181 ;; I like this better than bibtex-url which does not always find
3183 (bibtex-autokey-get-field "doi"))))
3184 (candidates `(("Quit" . org-ref-citation-at-point)
3185 ("Open bibtex entry" . org-ref-open-citation-at-point))))
3186 ;; for some reason, when there is no doi or url, they are returned as "". I
3187 ;; prefer nil so we correct this here.
3188 (when (string= doi "") (setq doi nil))
3189 (when (string= url "") (setq url nil))
3191 ;; Conditional pdf functions
3192 (if (file-exists-p pdf-file)
3195 '("Open pdf" . org-ref-open-pdf-at-point)
3199 '("Try to get pdf" . (lambda ()
3200 (save-window-excursion
3201 (org-ref-open-citation-at-point)
3202 (bibtex-beginning-of-entry)
3203 (doi-utils-get-bibtex-entry-pdf))))
3209 '("Open notes" . org-ref-open-notes-at-point)
3212 ;; conditional url and doi functions
3216 '("Open in browser" . org-ref-open-url-at-point)
3221 (add-to-list 'candidates x t))
3222 `(("WOS" . org-ref-wos-at-point)
3223 ("Related articles in WOS" . org-ref-wos-related-at-point)
3224 ("Citing articles in WOS" . org-ref-wos-citing-at-point)
3225 ("Google Scholar" . org-ref-google-scholar-at-point)
3226 ("Pubmed" . org-ref-pubmed-at-point)
3227 ("Crossref" . org-ref-crossref-at-point)
3232 '("Copy formatted citation to clipboard" . org-ref-copy-entry-as-summary)
3237 '("Copy key to clipboard" . (lambda ()
3239 (car (org-ref-get-bibtex-key-and-file)))))
3244 '("Copy bibtex entry to file" . org-ref-copy-entry-at-point-to-file)
3249 '("Email bibtex entry and pdf" . (lambda ()
3251 (org-ref-open-citation-at-point)
3252 (email-bibtex-entry))))
3254 ;; finally return a numbered list of the candidates
3256 for cell in candidates
3257 collect (cons (format "%2s. %s" i (car cell))
3261 (defvar org-ref-helm-user-candidates '()
3262 "List of user-defined candidates to act when clicking on a cite link.
3263 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.")
3265 ;; example of adding your own function
3267 'org-ref-helm-user-candidates
3268 '("Example" . (lambda () (message-box "You did it!")))
3272 (defun org-ref-cite-click-helm (key)
3273 "Open helm for actions on a cite link.
3276 1. get name and candidates before entering helm because we need
3279 2. switch back to the org buffer before evaluating the
3280 action. most of them need the point and buffer.
3282 KEY is returned for the selected item(s) in helm."
3284 (let ((name (org-ref-get-citation-string-at-point))
3285 (candidates (org-ref-cite-candidates))
3286 (cb (current-buffer)))
3288 (helm :sources `(((name . ,name)
3289 (candidates . ,candidates)
3290 (action . (lambda (f)
3291 (switch-to-buffer cb)
3293 ((name . "User functions")
3294 (candidates . ,org-ref-helm-user-candidates)
3295 (action . (lambda (f)
3296 (switch-to-buffer cb)
3300 ;; * Hydra menus in org-ref
3302 (when (featurep 'hydra)
3304 (setq hydra-is-helpful t)
3306 (defhydra org-ref-cite-hydra (:color blue)
3308 _p_: Open pdf _w_: WOS _g_: Google Scholar _K_: Copy citation to clipboard
3309 _u_: Open url _r_: WOS related _P_: Pubmed _k_: Copy key to clipboard
3310 _n_: Open notes _c_: WOS citing _C_: Crossref _f_: Copy bibtex entry to file
3311 _o_: Open entry _e_: Email entry and pdf
3313 ("o" org-ref-open-citation-at-point nil)
3314 ("p" org-ref-open-pdf-at-point nil)
3315 ("n" org-ref-open-notes-at-point nil)
3316 ("u" org-ref-open-url-at-point nil)
3317 ("w" org-ref-wos-at-point nil)
3318 ("r" org-ref-wos-related-at-point nil)
3319 ("c" org-ref-wos-citing-at-point nil)
3320 ("g" org-ref-google-scholar-at-point nil)
3321 ("P" org-ref-pubmed-at-point nil)
3322 ("C" org-ref-crossref-at-point nil)
3323 ("K" org-ref-copy-entry-as-summary nil)
3326 (car (org-ref-get-bibtex-key-and-file)))) nil)
3327 ("f" org-ref-copy-entry-at-point-to-file nil)
3329 ("e" (save-excursion
3330 (org-ref-open-citation-at-point)
3331 (email-bibtex-entry)) nil)))
3334 (defun org-ref-help ()
3335 "Open the org-ref manual."
3337 (find-file (expand-file-name
3339 (file-name-directory
3340 (find-library-name "org-ref")))))
3343 (defun org-ref-org-menu ()
3344 "Add org-ref menu to the Org menu."
3348 '( ["Insert citation" org-ref-helm-insert-cite-link]
3349 ["Insert ref" org-ref-helm-insert-ref-link]
3350 ["Insert label" org-ref-helm-insert-label-link]
3352 ["List of figures" org-ref-list-of-figures]
3353 ["List of tables" org-ref-list-of-tables]
3354 ["Extract bibtex entries" org-ref-extract-bibtex-entries]
3355 ["Check org-file" org-ref]
3357 ["Help" org-ref-help]
3358 ["Customize org-ref" (customize-group 'org-ref)])
3361 (easy-menu-change '("Org") "--" nil "Show/Hide"))
3363 (add-hook 'org-mode-hook 'org-ref-org-menu)
3368 ;;; org-ref.el ends here