add function to give message when on a cite link.
[org-ref.git] / org-ref.org
1 #+TITLE: Org-ref - The best reference handling for org-mode
2 #+AUTHOR: John Kitchin
3 #+DATE: April 29, 2014
4
5 * Introduction
6
7 This document is an experiment at creating a literate program to provide similar features as reftex for org-mode referencing. These features include:
8
9 1. using completion to create links
10 2. storing links to places,
11 3. Clickable links that do useful things
12 4. Exportable links to LaTeX
13 5. Utility functions for dealing with bibtex files and org-files
14
15 ** Header
16 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
17 ;;; org-ref.el --- setup bibliography, cite, ref and label org-mode links.
18
19 ;; Copyright(C) 2014 John Kitchin
20
21 ;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
22 ;; This file is not currently part of GNU Emacs.
23
24 ;; This program is free software; you can redistribute it and/or
25 ;; modify it under the terms of the GNU General Public License as
26 ;; published by the Free Software Foundation; either version 2, or (at
27 ;; your option) any later version.
28
29 ;; This program is distributed in the hope that it will be useful, but
30 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
31 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
32 ;; General Public License for more details.
33
34 ;; You should have received a copy of the GNU General Public License
35 ;; along with this program ; see the file COPYING.  If not, write to
36 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
37 ;; Boston, MA 02111-1307, USA.
38
39 ;;; Commentary:
40 ;;
41 ;; Lisp code to setup bibliography cite, ref and label org-mode links.  also
42 ;; sets up reftex and helm for org-mode citations. The links are clickable and
43 ;; do things that are useful. You should really read org-ref.org for details.
44 ;;
45 ;; Package-Requires: ((dash) (helm) (helm-bibtex))
46 #+END_SRC
47
48 ** requires
49 The only external require is reftex-cite
50
51 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
52 (require 'reftex-cite)
53 (require 'dash)
54 (require 'helm)
55 (require 'helm-bibtex
56 #+END_SRC
57
58 ** Custom variables
59 There are some variables needed later to tell this library where you store your pdf files, where your notes file is, and your default bibliography. This variable is similar to the reftex-default-bibliography. I do not remember why I made it separate.
60
61 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
62 (defgroup org-ref nil
63   "customization group for org-ref")
64
65 (defcustom org-ref-bibliography-notes
66   nil
67   "filename to where you will put all your notes about an entry in
68   the default bibliography."
69   :type 'file
70   :group 'org-ref)
71
72 (defcustom org-ref-default-bibliography
73   nil
74   "list of bibtex files to search for. You should use full-paths for each file."
75   :type '(repeat :tag "List of bibtex files" file)
76   :group 'org-ref)
77
78 (defcustom org-ref-pdf-directory
79   nil
80   "directory where pdfs are stored by key. put a trailing / in"
81   :type 'directory
82   :group 'org-ref)
83
84 (defcustom org-ref-default-citation-link
85   "cite"
86   "The default type of citation link to use"
87   :type 'string
88   :group 'org-ref)
89
90 (defcustom org-ref-insert-cite-key
91   "C-c ]"
92   "Keyboard shortcut to insert a citation."
93   :type 'string
94   :group 'org-ref)
95
96 (defcustom org-ref-bibliography-entry-format
97   '(("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>.")
98
99     ("book" . "%a, %t, %u (%y).")
100
101     ("proceedings" . "%e, %t in %S, %u (%y).")
102
103     ("inproceedings" . "%a, %t, %p, in %b, edited by %e, %u (%y)"))
104
105   "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."
106   :type 'string
107   :group 'org-ref)
108
109 (defcustom org-ref-open-notes-function
110   (lambda ()
111     (org-show-entry)
112     (show-branches)
113     (show-children)
114     (org-cycle '(64))
115     ;;(org-tree-to-indirect-buffer)
116     (outline-previous-visible-heading 1)
117     (recenter-top-bottom 0))
118   "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"
119 :type 'function
120 :group 'org-ref)
121
122
123 (defcustom org-ref-open-pdf-function
124    'org-ref-open-pdf-at-point
125 "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'."
126   :type 'function
127   :group 'org-ref)
128
129
130 (defcustom org-ref-insert-cite-function
131   'helm-bibtex
132   "Function to call to insert citation links. The default is `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."
133  :type 'function
134  :group 'org-ref)
135
136
137 (defcustom org-ref-cite-onclick-function
138   'org-ref-cite-click-helm
139   "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."
140  :type 'function
141  :group 'org-ref)
142
143 (defcustom org-ref-show-citation-on-enter t
144   "If non-nil add a hook function to show the citation summary in
145   the minibuffer just by putting the cursor in a link"
146  :group 'org-ref)
147
148 #+END_SRC
149
150 This next variable determines the citation types that are available in org-ref. Links for each one are automatically generated, and completion functions are automatically generated. Users may add to this list in their own init files.
151
152 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
153 (defcustom org-ref-cite-types
154   '("cite" "nocite" ;; the default latex cite commands
155     ;; natbib cite commands, http://ctan.unixbrain.com/macros/latex/contrib/natbib/natnotes.pdf
156     "citet" "citet*" "citep" "citep*"
157     "citealt" "citealt*" "citealp" "citealp*"
158     "citenum" "citetext"
159     "citeauthor" "citeauthor*"
160     "citeyear" "citeyear*"
161     "Citet" "Citep" "Citealt" "Citealp" "Citeauthor"
162     ;; biblatex commands
163     ;; http://ctan.mirrorcatalogs.com/macros/latex/contrib/biblatex/doc/biblatex.pdf
164     "Cite"
165     "parencite" "Parencite"
166     "footcite" "footcitetext"
167     "textcite" "Textcite"
168     "smartcite" "Smartcite"
169     "cite*" "parencite*" "supercite"
170     "autocite" "Autocite" "autocite*" "Autocite*"
171     "Citeauthor*"
172     "citetitle" "citetitle*"
173     "citedate" "citedate*"
174     "citeurl"
175     "fullcite" "footfullcite"
176     ;; "volcite" "Volcite" cannot support the syntax
177     "notecite" "Notecite"
178     "pnotecite" "Pnotecite"
179     "fnotecite"
180     ;; multicites. Very limited support for these.
181     "cites" "Cites" "parencites" "Parencites"
182     "footcites" "footcitetexts"
183     "smartcites" "Smartcites" "textcites" "Textcites"
184     "supercites" "autocites" "Autocites"
185     ;; for the bibentry package
186     "bibentry"
187     )
188   "List of citation types known in org-ref"
189   :type '(repeat :tag "List of citation types" string)
190   :group 'org-ref)
191 #+END_SRC
192
193 We need a hook variable to store user-defined bibtex entry cleaning functions
194 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
195 (defcustom org-ref-clean-bibtex-entry-hook nil
196   "Hook that is run in org-ref-clean-bibtex-entry. The functions should take no arguments, and operate on the bibtex entry at point."
197   :group 'org-ref
198   :type 'hook)
199 #+END_SRC
200
201 ** Program variables
202 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
203 (defvar org-ref-bibliography-files
204   nil
205   "variable to hold bibliography files to be searched")
206 #+END_SRC
207
208 ** org-mode / reftex setup
209
210 We setup reftex here. We use a custom insert cite link function defined here: [[*org-ref-insert-cite-link][org-ref-insert-cite-link]]. We setup reftex to use our org citation format.
211
212 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
213 (require 'reftex)
214 (defun org-mode-reftex-setup ()
215     (and (buffer-file-name)
216          (file-exists-p (buffer-file-name))
217          (global-auto-revert-mode t)
218          ;; I do not remember why I put this next line in. It doesn't
219          ;; work for org-files. Nothing very bad happens, but it gives
220          ;; an annoying error. Commenting it out for now.
221          ;(reftex-parse-all)
222          )
223     (make-local-variable 'reftex-cite-format)
224     (setq reftex-cite-format 'org)
225     (define-key org-mode-map (kbd org-ref-insert-cite-key) org-ref-insert-cite-function))
226
227 (add-hook 'org-mode-hook 'org-mode-reftex-setup)
228
229 (eval-after-load 'reftex-vars
230   '(progn
231       (add-to-list 'reftex-cite-format-builtin
232                    '(org "Org-mode citation"
233                          ((?\C-m . "cite:%l")     ; default
234                           (?d . ",%l")            ; for appending
235                           (?a . "autocite:%l")
236                           (?t . "citet:%l")
237                           (?T . "citet*:%l")
238                           (?p . "citep:%l")
239                           (?P . "citep*:%l")
240                           (?h . "citeauthor:%l")
241                           (?H . "citeauthor*:%l")
242                           (?y . "citeyear:%l")
243                           (?x . "citetext:%l")
244                           (?n . "nocite:%l")
245                           )))))
246 #+END_SRC
247
248 You may want to add new formats to the reftex-cite-format-builtin variable. Here is an example of adding two new formats. Note that this does not create the links.
249
250 #+BEGIN_SRC emacs-lisp :tangle no
251 ;; add new format
252 (setf (nth 2 (assoc 'org reftex-cite-format-builtin))
253       (append (nth 2 (assoc 'org reftex-cite-format-builtin)) '((?W  . "textcite:%l")
254             (?z  . "newcite:%l"))))
255 #+END_SRC
256
257 You can define a new citation link like this:
258 #+BEGIN_SRC emacs-lisp :tangle no
259 (org-ref-define-citation-link "citez" ?z)
260 #+END_SRC
261
262 * Links
263 Most of this library is the creation of functional links to help with references and citations.
264 ** General utilities
265 We need several general utilities for this module. They are organized here. We frequently need to remove white space from the front and back of a string. Here we do that for a string.
266
267 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
268 (defun org-ref-strip-string (string)
269   "strip leading and trailing whitespace from the string"
270   (replace-regexp-in-string
271    (concat search-whitespace-regexp "$" ) ""
272    (replace-regexp-in-string
273     (concat "^" search-whitespace-regexp ) "" string)))
274 #+END_SRC
275
276 It is helpful to make the previous function operate on a list of strings here.
277
278 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
279 (defun org-ref-split-and-strip-string (string)
280   "split key-string and strip keys. Assumes the key-string is comma delimited"
281   (mapcar 'org-ref-strip-string (split-string string ",")))
282 #+END_SRC
283
284 ** bibliography and bibliographystyle
285 *** An html bibliography
286
287 Reftex is no longer being developed. I want a url and doi option for formatting, so I am modifying this [[file:emacs-24.3/lisp/textmodes/reftex-cite.el::(defun%20reftex-format-citation%20(entry%20format)][function]] from reftex-cite to provide that. We need to modify the reftex-get-bib-field code a bit to remove enclosing braces and quotes so we can make nice looking links.
288
289 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
290 (defun org-ref-reftex-get-bib-field (field entry &optional format)
291   "similar to reftex-get-bib-field, but removes enclosing braces and quotes"
292   (let ((result))
293     (setq result (reftex-get-bib-field field entry format))
294     (when (and (not (string= result "")) (string= "{" (substring result 0 1)))
295       (setq result (substring result 1 -1)))
296     (when (and (not (string= result "")) (string= "\"" (substring result 0 1)))
297       (setq result (substring result 1 -1)))
298       result))
299
300 (defun org-ref-reftex-format-citation (entry format)
301   "return a formatted string for the bibtex entry (from bibtex-parse-entry) according
302 to the format argument. The format is a string with these percent escapes.
303
304 In the format, the following percent escapes will be expanded.
305
306 %l   The BibTeX label of the citation.
307 %a   List of author names, see also `reftex-cite-punctuation'.
308 %2a  Like %a, but abbreviate more than 2 authors like Jones et al.
309 %A   First author name only.
310 %e   Works like %a, but on list of editor names. (%2e and %E work a well)
311
312 It is also possible to access all other BibTeX database fields:
313 %b booktitle     %c chapter        %d edition    %h howpublished
314 %i institution   %j journal        %k key        %m month
315 %n number        %o organization   %p pages      %P first page
316 %r address       %s school         %u publisher  %t title
317 %v volume        %y year
318 %B booktitle, abbreviated          %T title, abbreviated
319 %U url
320 %D doi
321 %S series
322
323 Usually, only %l is needed.  The other stuff is mainly for the echo area
324 display, and for (setq reftex-comment-citations t).
325
326 %< as a special operator kills punctuation and space around it after the
327 string has been formatted.
328
329 A pair of square brackets indicates an optional argument, and RefTeX
330 will prompt for the values of these arguments.
331
332 Beware that all this only works with BibTeX database files.  When
333 citations are made from the \bibitems in an explicit thebibliography
334 environment, only %l is available."
335   ;; Format a citation from the info in the BibTeX ENTRY
336
337   (unless (stringp format) (setq format "\\cite{%l}"))
338
339   (if (and reftex-comment-citations
340            (string-match "%l" reftex-cite-comment-format))
341       (error "reftex-cite-comment-format contains invalid %%l"))
342
343   (while (string-match
344           "\\(\\`\\|[^%]\\)\\(\\(%\\([0-9]*\\)\\([a-zA-Z]\\)\\)[.,;: ]*\\)"
345           format)
346     (let ((n (string-to-number (match-string 4 format)))
347           (l (string-to-char (match-string 5 format)))
348           rpl b e)
349       (save-match-data
350         (setq rpl
351               (cond
352                ((= l ?l) (concat
353                           (org-ref-reftex-get-bib-field "&key" entry)
354                           (if reftex-comment-citations
355                               reftex-cite-comment-format
356                             "")))
357                ((= l ?a) (reftex-format-names
358                           (reftex-get-bib-names "author" entry)
359                           (or n 2)))
360                ((= l ?A) (car (reftex-get-bib-names "author" entry)))
361                ((= l ?b) (org-ref-reftex-get-bib-field "booktitle" entry "in: %s"))
362                ((= l ?B) (reftex-abbreviate-title
363                           (org-ref-reftex-get-bib-field "booktitle" entry "in: %s")))
364                ((= l ?c) (org-ref-reftex-get-bib-field "chapter" entry))
365                ((= l ?d) (org-ref-reftex-get-bib-field "edition" entry))
366                ((= l ?D) (org-ref-reftex-get-bib-field "doi" entry))
367                ((= l ?e) (reftex-format-names
368                           (reftex-get-bib-names "editor" entry)
369                           (or n 2)))
370                ((= l ?E) (car (reftex-get-bib-names "editor" entry)))
371                ((= l ?h) (org-ref-reftex-get-bib-field "howpublished" entry))
372                ((= l ?i) (org-ref-reftex-get-bib-field "institution" entry))
373                ((= l ?j) (org-ref-reftex-get-bib-field "journal" entry))
374                ((= l ?k) (org-ref-reftex-get-bib-field "key" entry))
375                ((= l ?m) (org-ref-reftex-get-bib-field "month" entry))
376                ((= l ?n) (org-ref-reftex-get-bib-field "number" entry))
377                ((= l ?o) (org-ref-reftex-get-bib-field "organization" entry))
378                ((= l ?p) (org-ref-reftex-get-bib-field "pages" entry))
379                ((= l ?P) (car (split-string
380                                (org-ref-reftex-get-bib-field "pages" entry)
381                                "[- .]+")))
382                ((= l ?s) (org-ref-reftex-get-bib-field "school" entry))
383                ((= l ?S) (org-ref-reftex-get-bib-field "series" entry))
384                ((= l ?u) (org-ref-reftex-get-bib-field "publisher" entry))
385                ((= l ?U) (org-ref-reftex-get-bib-field "url" entry))
386                ((= l ?r) (org-ref-reftex-get-bib-field "address" entry))
387                ;; strip enclosing brackets from title if they are there
388                ((= l ?t) (org-ref-reftex-get-bib-field "title" entry))
389                ((= l ?T) (reftex-abbreviate-title
390                           (org-ref-reftex-get-bib-field "title" entry)))
391                ((= l ?v) (org-ref-reftex-get-bib-field "volume" entry))
392                ((= l ?y) (org-ref-reftex-get-bib-field "year" entry)))))
393
394       (if (string= rpl "")
395           (setq b (match-beginning 2) e (match-end 2))
396         (setq b (match-beginning 3) e (match-end 3)))
397       (setq format (concat (substring format 0 b) rpl (substring format e)))))
398   (while (string-match "%%" format)
399     (setq format (replace-match "%" t t format)))
400   (while (string-match "[ ,.;:]*%<" format)
401     (setq format (replace-match "" t t format)))
402   ;; also replace carriage returns, tabs, and multiple whitespaces
403   (setq format (replace-regexp-in-string "\n\\|\t\\|\s+" " " format))
404   format)
405
406 (defun org-ref-get-bibtex-entry-citation (key)
407   "returns a string for the bibliography entry corresponding to key, and formatted according to the type in `org-ref-bibliography-entry-format'"
408
409   (let ((org-ref-bibliography-files (org-ref-find-bibliography))
410         (file) (entry) (bibtex-entry) (entry-type) (format))
411
412     (setq file (catch 'result
413                  (loop for file in org-ref-bibliography-files do
414                        (if (org-ref-key-in-file-p key (file-truename file))
415                            (throw 'result file)
416                          (message "%s not found in %s" key (file-truename file))))))
417
418     (with-temp-buffer
419       (insert-file-contents file)
420       (bibtex-search-entry key nil 0)
421       (setq bibtex-entry (bibtex-parse-entry))
422       (setq entry-type (downcase (cdr (assoc "=type=" bibtex-entry))))
423       (setq format (cdr (assoc entry-type org-ref-bibliography-entry-format)))
424       (if format
425           (setq entry  (org-ref-reftex-format-citation bibtex-entry format))
426         (save-restriction
427           (bibtex-narrow-to-entry)
428           (setq entry (buffer-string)))))
429     entry))
430 #+END_SRC
431
432 #+RESULTS:
433 : org-ref-reftex-format-citation
434
435 Here is how to use the function. You call it with point in an entry in a bibtex file.
436
437 #+BEGIN_SRC emacs-lisp :tangle no
438 (let((org-ref-bibliography-entry-format   "%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>."))
439   (org-ref-get-bibtex-entry-citation  "armiento-2014-high"))
440 #+END_SRC
441 #+RESULTS:
442 : Armiento, Kozinsky, Hautier, , Fornari \& Ceder, High-throughput screening of perovskite alloys for  piezoelectric performance and thermodynamic  stability, <i>Phys. Rev. B</i>, <b>89()</b>, 134103 (2014). <a href="http://link.aps.org/doi/10.1103/PhysRevB.89.134103">link</a>. <a href="http://dx.doi.org/10.1103/PhysRevB.89.134103">doi</a>.
443
444 I am not sure why full author names are not used.
445
446 This code provides some functions to generate a simple sorted bibliography in html. First we get all the keys in the buffer.
447
448 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
449 (defun org-ref-get-bibtex-keys ()
450   "return a list of unique keys in the buffer."
451   (let ((keys '()))
452     (org-element-map (org-element-parse-buffer) 'link
453       (lambda (link)
454         (let ((plist (nth 1 link)))
455           (when (-contains? org-ref-cite-types (plist-get plist ':type))
456             (dolist
457                 (key
458                  (org-ref-split-and-strip-string (plist-get plist ':path)))
459               (when (not (-contains? keys key))
460                 (setq keys (append keys (list key)))))))))
461     ;; Sort keys alphabetically
462     (setq keys (cl-sort keys 'string-lessp :key 'downcase))
463     keys))
464 #+END_SRC
465
466 This function gets the html for one entry.
467
468 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
469 (defun org-ref-get-bibtex-entry-html (key)
470   "returns an html string for the bibliography entry corresponding to key"
471
472   (format "<li><a id=\"%s\">[%s] %s</a></li>" key key (org-ref-get-bibtex-entry-citation key)))
473 #+END_SRC
474
475 Now, we map over the whole list of keys, and the whole bibliography, formatted as an unordered list.
476
477 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
478 (defun org-ref-get-html-bibliography ()
479   "Create an html bibliography when there are keys"
480   (let ((keys (org-ref-get-bibtex-keys)))
481     (when keys
482       (concat "<h1>Bibliography</h1>
483 <ul>"
484               (mapconcat (lambda (x) (org-ref-get-bibtex-entry-html x)) keys "\n")
485               "\n</ul>"))))
486 #+END_SRC
487
488 I do not have plans to make a numbered bibliography with numbered citations anytime soon. This will require changing the way the citation links are exported, and keeping track of the numbers.
489
490 *** An org bibliography
491 You can export an org-file to an org-file or org-buffer (org-org-epxort-as-org). In this case, it would be useful convert the cite links to links to custom_ids, and the bibliography link to a first-level heading Bibliography with org-bibtex like headings for each entry. This code should enable this. Right now, it does not appear to work for org export though.
492
493 First, we get the string for a single entry.
494 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
495 (defun org-ref-get-bibtex-entry-org (key)
496   "returns an org string for the bibliography entry corresponding to key"
497   (let ((org-ref-bibliography-files (org-ref-find-bibliography))
498         (file) (entry) (bibtex-entry) (entry-type) (format))
499
500     (setq file (catch 'result
501                  (loop for file in org-ref-bibliography-files do
502                        (if (org-ref-key-in-file-p key (file-truename file))
503                            (throw 'result file)
504                          (message "%s not found in %s" key (file-truename file))))))
505
506     (with-temp-buffer
507       (insert-file-contents file)
508       (bibtex-search-entry key nil 0)
509       (setq entry (bibtex-parse-entry))
510       (format "** %s - %s
511   :PROPERTIES:
512   %s
513   :END:
514 " (org-ref-reftex-get-bib-field "author" entry)
515 (org-ref-reftex-get-bib-field "title" entry)
516 (concat "   :CUSTOM_ID: " (org-ref-reftex-get-bib-field "=key=" entry) "\n"
517         (mapconcat (lambda (element) (format "   :%s: %s"
518                                              (upcase (car element))
519                                              (cdr element)))
520                    entry
521                    "\n"))))))
522 #+END_SRC
523
524 Now, we loop over the keys, and combine all the entries into a bibliography.
525 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
526 (defun org-ref-get-org-bibliography ()
527   "Create an org bibliography when there are keys"
528   (let ((keys (org-ref-get-bibtex-keys)))
529     (when keys
530       (concat "* Bibliography
531 "
532               (mapconcat (lambda (x) (org-ref-get-bibtex-entry-org x)) keys "\n")
533               "\n"))))
534 #+END_SRC
535
536 *** An ascii bibliography
537
538 This function gets the html for one entry.
539
540 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
541 (defun org-ref-get-bibtex-entry-ascii (key)
542   "returns an ascii string for the bibliography entry corresponding to key"
543
544   (format "[%s] %s" key (org-ref-get-bibtex-entry-citation key)))
545 #+END_SRC
546
547 Now, we map over the whole list of keys, and the whole bibliography, formatted as an unordered list.
548
549 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
550 (defun org-ref-get-ascii-bibliography ()
551   "Create an html bibliography when there are keys"
552   (let ((keys (org-ref-get-bibtex-keys)))
553     (when keys
554       (concat
555 "Bibliography
556 =============
557 "
558               (mapconcat (lambda (x) (org-ref-get-bibtex-entry-ascii x)) keys "\n")
559               "\n"))))
560 #+END_SRC
561
562
563 *** the links
564 We use a link for the bibliography so that we can click on it to open the bibliography file. The link may have more than one bibliography file in it, separated by commas. Clicking opens the file under the cursor. The bibliographies should be full filenames with the bib extension. Clicking on this link makes reftex-default-bibliography local and sets it to the list of files in the link. We need this to use reftex's searching capability.
565
566 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
567 (org-add-link-type "bibliography"
568                    ;; this code is run on clicking. The bibliography
569                    ;; may contain multiple files. this code finds the
570                    ;; one you clicked on and opens it.
571                    (lambda (link-string)
572                        ;; get link-string boundaries
573                        ;; we have to go to the beginning of the line, and then search forward
574
575                      (let* ((bibfile)
576                             ;; object is the link you clicked on
577                             (object (org-element-context))
578
579                             (link-string-beginning)
580                             (link-string-end))
581
582                      (save-excursion
583                        (goto-char (org-element-property :begin object))
584                        (search-forward link-string nil nil 1)
585                        (setq link-string-beginning (match-beginning 0))
586                        (setq link-string-end (match-end 0)))
587
588                        ;; We set the reftex-default-bibliography
589                        ;; here. it should be a local variable only in
590                        ;; the current buffer. We need this for using
591                        ;; reftex to do citations.
592                        (set (make-local-variable 'reftex-default-bibliography)
593                             (split-string (org-element-property :path object) ","))
594
595                        ;; now if we have comma separated bibliographies
596                        ;; we find the one clicked on. we want to
597                        ;; search forward to next comma from point
598                        (save-excursion
599                          (if (search-forward "," link-string-end 1 1)
600                              (setq key-end (- (match-end 0) 1)) ; we found a match
601                            (setq key-end (point)))) ; no comma found so take the point
602                        ;; and backward to previous comma from point
603                        (save-excursion
604                          (if (search-backward "," link-string-beginning 1 1)
605                              (setq key-beginning (+ (match-beginning 0) 1)) ; we found a match
606                            (setq key-beginning (point)))) ; no match found
607                        ;; save the key we clicked on.
608                        (setq bibfile (org-ref-strip-string (buffer-substring key-beginning key-end)))
609                        (find-file bibfile))) ; open file on click
610
611                      ;; formatting code
612                    (lambda (keyword desc format)
613                      (cond
614                       ((eq format 'org) (org-ref-get-org-bibliography))
615                       ((eq format 'ascii) (org-ref-get-ascii-bibliography))
616                       ((eq format 'html) (org-ref-get-html-bibliography))
617                       ((eq format 'latex)
618                        ;; write out the latex bibliography command
619                        (format "\\bibliography{%s}" (replace-regexp-in-string  "\\.bib" "" (mapconcat 'identity
620                                                                                                       (mapcar 'expand-file-name
621                                                                                                               (split-string keyword ","))
622                                                                                                       ",")))))))
623
624 #+END_SRC
625
626 Believe it or not, sometimes it makes sense /not/ to include the bibliography in a document (e.g. when you are required to submit references as a separate file). To generate the references,  in another file, you must make a little tex file with these contents, and then compile it.
627
628 #+BEGIN_LaTeX
629   \input{project-description.bbl}
630 #+END_LaTeX
631
632 Here, we make a =nobibliography= link that acts like the bibliography, enables creation of the bbl file, but does not put an actual bibliography in the file.
633
634 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
635 (org-add-link-type "nobibliography"
636                    ;; this code is run on clicking. The bibliography
637                    ;; may contain multiple files. this code finds the
638                    ;; one you clicked on and opens it.
639                    (lambda (link-string)
640                        ;; get link-string boundaries
641                        ;; we have to go to the beginning of the line, and then search forward
642
643                      (let* ((bibfile)
644                             ;; object is the link you clicked on
645                             (object (org-element-context))
646
647                             (link-string-beginning)
648                             (link-string-end))
649
650                      (save-excursion
651                        (goto-char (org-element-property :begin object))
652                        (search-forward link-string nil nil 1)
653                        (setq link-string-beginning (match-beginning 0))
654                        (setq link-string-end (match-end 0)))
655
656                        ;; We set the reftex-default-bibliography
657                        ;; here. it should be a local variable only in
658                        ;; the current buffer. We need this for using
659                        ;; reftex to do citations.
660                        (set (make-local-variable 'reftex-default-bibliography)
661                             (split-string (org-element-property :path object) ","))
662
663                        ;; now if we have comma separated bibliographies
664                        ;; we find the one clicked on. we want to
665                        ;; search forward to next comma from point
666                        (save-excursion
667                          (if (search-forward "," link-string-end 1 1)
668                              (setq key-end (- (match-end 0) 1)) ; we found a match
669                            (setq key-end (point)))) ; no comma found so take the point
670                        ;; and backward to previous comma from point
671                        (save-excursion
672                          (if (search-backward "," link-string-beginning 1 1)
673                              (setq key-beginning (+ (match-beginning 0) 1)) ; we found a match
674                            (setq key-beginning (point)))) ; no match found
675                        ;; save the key we clicked on.
676                        (setq bibfile (org-ref-strip-string (buffer-substring key-beginning key-end)))
677                        (find-file bibfile))) ; open file on click
678
679                      ;; formatting code
680                    (lambda (keyword desc format)
681                      (cond
682                       ((eq format 'org) (org-ref-get-org-bibliography))
683                       ((eq format 'ascii) (org-ref-get-ascii-bibliography))
684                       ((eq format 'html) (org-ref-get-html-bibliography))
685                       ((eq format 'latex)
686                        ;; write out the latex bibliography command
687
688 ;                      (format "{\\setbox0\\vbox{\\bibliography{%s}}}"
689 ;                              (replace-regexp-in-string  "\\.bib" "" (mapconcat 'identity
690 ;                                                                                (mapcar 'expand-file-name
691 ;                                                                                        (split-string keyword ","))
692 ;                                                                                ",")))
693
694                        (format "\\nobibliography{%s}"
695                                (replace-regexp-in-string  "\\.bib" "" (mapconcat 'identity
696                                                                                  (mapcar 'expand-file-name
697                                                                                          (split-string keyword ","))
698                                                                                  ",")))
699
700                        ))))
701 #+END_SRC
702
703 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
704 (org-add-link-type "printbibliography"
705                    (lambda (arg) (message "Nothing implemented for clicking here."))
706                    (lambda (keyword desc format)
707                      (cond
708                       ((eq format 'org) (org-ref-get-org-bibliography))
709                       ((eq format 'html) (org-ref-get-html-bibliography))
710                       ((eq format 'latex)
711                        ;; write out the biblatex bibliography command
712                        "\\printbibliography"))
713 ))
714 #+END_SRC
715
716 We also create a bibliographystyle link. There is nothing to do on clicking here, and we create it for consistency. This sets the style for latex export, so use something appropriate there, e.g. unsrt, plain, plainnat, ...
717
718 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
719 (org-add-link-type "bibliographystyle"
720                    (lambda (arg) (message "Nothing implemented for clicking here."))
721                    (lambda (keyword desc format)
722                      (cond
723                       ((eq format 'latex)
724                        ;; write out the latex bibliography command
725                        (format "\\bibliographystyle{%s}" keyword)))))
726 #+END_SRC
727
728 *** Completion for bibliography link
729 It would be nice
730
731 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
732 (defun org-bibliography-complete-link (&optional arg)
733  (format "bibliography:%s" (read-file-name "enter file: " nil nil t)))
734
735 (defun org-ref-insert-bibliography-link ()
736   "insert a bibliography with completion"
737   (interactive)
738   (insert (org-bibliography-complete-link)))
739 #+END_SRC
740
741 ** addbibresource
742 This is apparently used for biblatex.
743 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
744 (org-add-link-type "addbibresource"
745                    ;; this code is run on clicking. The addbibresource
746                    ;; may contain multiple files. this code finds the
747                    ;; one you clicked on and opens it.
748                    (lambda (link-string)
749                        ;; get link-string boundaries
750                        ;; we have to go to the beginning of the line, and then search forward
751
752                      (let* ((bibfile)
753                             ;; object is the link you clicked on
754                             (object (org-element-context))
755
756                             (link-string-beginning)
757                             (link-string-end))
758
759                      (save-excursion
760                        (goto-char (org-element-property :begin object))
761                        (search-forward link-string nil nil 1)
762                        (setq link-string-beginning (match-beginning 0))
763                        (setq link-string-end (match-end 0)))
764
765                        ;; We set the reftex-default-addbibresource
766                        ;; here. it should be a local variable only in
767                        ;; the current buffer. We need this for using
768                        ;; reftex to do citations.
769                        (set (make-local-variable 'reftex-default-addbibresource)
770                             (split-string (org-element-property :path object) ","))
771
772                        ;; now if we have comma separated bibliographies
773                        ;; we find the one clicked on. we want to
774                        ;; search forward to next comma from point
775                        (save-excursion
776                          (if (search-forward "," link-string-end 1 1)
777                              (setq key-end (- (match-end 0) 1)) ; we found a match
778                            (setq key-end (point)))) ; no comma found so take the point
779                        ;; and backward to previous comma from point
780                        (save-excursion
781                          (if (search-backward "," link-string-beginning 1 1)
782                              (setq key-beginning (+ (match-beginning 0) 1)) ; we found a match
783                            (setq key-beginning (point)))) ; no match found
784                        ;; save the key we clicked on.
785                        (setq bibfile (org-ref-strip-string (buffer-substring key-beginning key-end)))
786                        (find-file bibfile))) ; open file on click
787
788                      ;; formatting code
789                    (lambda (keyword desc format)
790                      (cond
791                       ((eq format 'html) (format "")); no output for html
792                       ((eq format 'latex)
793                          ;; write out the latex addbibresource command
794                        (format "\\addbibresource{%s}" keyword)))))
795 #+END_SRC
796
797 ** List of Figures
798
799 In long documents, a list of figures is not uncommon. Here we create a clickable link that generates a temporary buffer containing a list of figures in the document, and their captions. We make a function that can be called interactively, and define a link type that is rendered in LaTeX to create the list of figures.
800
801 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
802 (defun org-ref-list-of-figures (&optional arg)
803   "Generate buffer with list of figures in them"
804   (interactive)
805   (save-excursion (widen)
806   (let* ((c-b (buffer-name))
807          (counter 0)
808          (list-of-figures
809           (org-element-map (org-element-parse-buffer) 'link
810             (lambda (link)
811               "create a link for to the figure"
812               (when
813                   (and (string= (org-element-property :type link) "file")
814                        (string-match-p
815                         "[^.]*\\.\\(png\\|jpg\\|eps\\|pdf\\)$"
816                         (org-element-property :path link)))
817                 (incf counter)
818
819                 (let* ((start (org-element-property :begin link))
820                        (parent (car (cdr (org-element-property :parent link))))
821                        (caption (caaar (plist-get parent :caption)))
822                        (name (plist-get parent :name)))
823                   (if caption
824                       (format
825                        "[[elisp:(progn (switch-to-buffer \"%s\")(widen)(goto-char %s))][figure %s: %s]] %s\n"
826                        c-b start counter (or name "") caption)
827                     (format
828                      "[[elisp:(progn (switch-to-buffer \"%s\")(widen)(goto-char %s))][figure %s: %s]]\n"
829                      c-b start counter (or name "")))))))))
830     (switch-to-buffer "*List of Figures*")
831     (setq buffer-read-only nil)
832     (org-mode)
833     (erase-buffer)
834     (insert (mapconcat 'identity list-of-figures ""))
835     (setq buffer-read-only t)
836     (use-local-map (copy-keymap org-mode-map))
837     (local-set-key "q" #'(lambda () (interactive) (kill-buffer))))))
838
839 (org-add-link-type
840  "list-of-figures"
841  'org-ref-list-of-figures ; on click
842  (lambda (keyword desc format)
843    (cond
844     ((eq format 'latex)
845      (format "\\listoffigures")))))
846 #+END_SRC
847
848 ** List of Tables
849
850 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
851 (defun org-ref-list-of-tables (&optional arg)
852   "Generate a buffer with a list of tables"
853   (interactive)
854   (save-excursion
855   (widen)
856   (let* ((c-b (buffer-name))
857          (counter 0)
858          (list-of-tables
859           (org-element-map (org-element-parse-buffer 'element) 'table
860             (lambda (table)
861               "create a link for to the table"
862               (incf counter)
863               (let ((start (org-element-property :begin table))
864                     (name  (org-element-property :name table))
865                     (caption (caaar (org-element-property :caption table))))
866                 (if caption
867                     (format
868                      "[[elisp:(progn (switch-to-buffer \"%s\")(widen)(goto-char %s))][table %s: %s]] %s\n"
869                      c-b start counter (or name "") caption)
870                   (format
871                    "[[elisp:(progn (switch-to-buffer \"%s\")(widen)(goto-char %s))][table %s: %s]]\n"
872                    c-b start counter (or name ""))))))))
873     (switch-to-buffer "*List of Tables*")
874     (setq buffer-read-only nil)
875     (org-mode)
876     (erase-buffer)
877     (insert (mapconcat 'identity list-of-tables ""))
878     (setq buffer-read-only t)
879     (use-local-map (copy-keymap org-mode-map))
880     (local-set-key "q" #'(lambda () (interactive) (kill-buffer))))))
881
882 (org-add-link-type
883  "list-of-tables"
884  'org-ref-list-of-tables
885  (lambda (keyword desc format)
886    (cond
887     ((eq format 'latex)
888      (format "\\listoftables")))))
889 #+END_SRC
890 ** label
891
892 The label link provides a way to create labels in org-mode. We make it clickable because we want to make sure labels are unique. This code will tell you how many instances of a label are found.  We search for label links, LaTeX labels, and the org-mode format for labels. We probably should search for tblnames too.
893 *************** TODO search tblnames, custom_ids and check for case sensitivity
894 *************** END
895
896 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
897 (org-add-link-type
898  "label"
899  (lambda (label)
900    "on clicking count the number of label tags used in the buffer. A number greater than one means multiple labels!"
901    (message (format "%s occurences"
902                     (+ (count-matches (format "label:%s\\b[^-:]" label) (point-min) (point-max) t)
903                        ;; for tblname, it is not enough to get word boundary
904                        ;; tab-little and tab-little-2 match then.
905                        (count-matches (format "^#\\+tblname:\\s-*%s\\b[^-:]" label) (point-min) (point-max) t)
906                        (count-matches (format "\\label{%s}\\b" label) (point-min) (point-max) t)
907                        ;; this is the org-format #+label:
908                        (count-matches (format "^#\\+label:\\s-*%s\\b[^-:]" label) (point-min) (point-max) t)))))
909  (lambda (keyword desc format)
910    (cond
911     ((eq format 'html) (format "(<label>%s</label>)" path))
912     ((eq format 'latex)
913      (format "\\label{%s}" keyword)))))
914 #+END_SRC
915
916 We want to store links on labels, so you can put the cursor on the label, press C-c l, and later use C-c C-l to insert a link to the label. We also want to store links to tables with a table name, and for sections with CUSTOM_ID.
917
918 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
919 (defun org-label-store-link ()
920   "store a link to a label. The output will be a ref to that label"
921   ;; First we have to make sure we are on a label link.
922   (let* ((object (org-element-context)))
923     (when (and (equal (org-element-type object) 'link)
924                (equal (org-element-property :type object) "label"))
925       (org-store-link-props
926        :type "ref"
927        :link (concat "ref:" (org-element-property :path object))))
928
929     ;; Store link on table
930     (when (equal (org-element-type object) 'table)
931       (org-store-link-props
932        :type "ref"
933        :link (concat "ref:" (org-element-property :name object))))
934
935 ;; it turns out this does not work. you can already store a link to a heading with a CUSTOM_ID
936     ;; store link on heading with custom_id
937 ;    (when (and (equal (org-element-type object) 'headline)
938 ;              (org-entry-get (point) "CUSTOM_ID"))
939 ;      (org-store-link-props
940 ;       :type "ref"
941 ;       :link (concat "ref:" (org-entry-get (point) "CUSTOM_ID"))))
942
943     ;; and to #+label: lines
944     (when (and (equal (org-element-type object) 'paragraph)
945                (org-element-property :name object))
946       (org-store-link-props
947        :type "ref"
948        :link (concat "ref:" (org-element-property :name object))))
949 ))
950
951 (add-hook 'org-store-link-functions 'org-label-store-link)
952 #+END_SRC
953 ** ref
954
955 The ref link allows you make links to labels. Clicking on the link takes you to the label, and provides a mark to go back to.
956
957 At the moment, ref links are not usable for section links. You need [[#CUSTOM_ID]] type links.
958
959 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
960 (org-add-link-type
961  "ref"
962  (lambda (label)
963    "on clicking goto the label. Navigate back with C-c &"
964    (org-mark-ring-push)
965    ;; next search from beginning of the buffer
966
967    ;; it is possible you would not find the label if narrowing is in effect
968    (widen)
969
970    (unless
971        (or
972         ;; our label links
973         (progn
974           (goto-char (point-min))
975           (re-search-forward (format "label:%s\\b" label) nil t))
976
977         ;; a latex label
978         (progn
979           (goto-char (point-min))
980           (re-search-forward (format "\\label{%s}" label) nil t))
981
982         ;; #+label: name  org-definition
983         (progn
984           (goto-char (point-min))
985           (re-search-forward (format "^#\\+label:\\s-*\\(%s\\)\\b" label) nil t))
986
987         ;; org tblname
988         (progn
989           (goto-char (point-min))
990           (re-search-forward (format "^#\\+tblname:\\s-*\\(%s\\)\\b" label) nil t))
991
992 ;; Commented out because these ref links do not actually translate correctly in LaTeX.
993 ;; you need [[#label]] links.
994         ;; CUSTOM_ID
995 ;       (progn
996 ;         (goto-char (point-min))
997 ;         (re-search-forward (format ":CUSTOM_ID:\s-*\\(%s\\)" label) nil t))
998         )
999      ;; we did not find anything, so go back to where we came
1000      (org-mark-ring-goto)
1001      (error "%s not found" label))
1002    (org-show-entry)
1003    (message "go back with (org-mark-ring-goto) `C-c &`"))
1004  ;formatting
1005  (lambda (keyword desc format)
1006    (cond
1007     ((eq format 'html) (format "(<ref>%s</ref>)" path))
1008     ((eq format 'latex)
1009      (format "\\ref{%s}" keyword)))))
1010 #+END_SRC
1011
1012 It would be nice to use completion to enter a ref link, where a list of labels is provided. The following code searches the buffer for org and latex labels, custom_ids, and table names as potential items to make a ref link to.
1013
1014 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1015 (defun org-ref-get-org-labels ()
1016  "find #+LABEL: labels"
1017   (save-excursion
1018     (goto-char (point-min))
1019     (let ((matches '()))
1020       (while (re-search-forward "^#\\+label:\\s-+\\(.*\\)\\b" (point-max) t)
1021         (add-to-list 'matches (match-string-no-properties 1) t))
1022 matches)))
1023 #+END_SRC
1024
1025 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1026 (defun org-ref-get-custom-ids ()
1027  "return a list of custom_id properties in the buffer"
1028  (let ((results '()) custom_id)
1029    (org-map-entries
1030     (lambda ()
1031       (let ((custom_id (org-entry-get (point) "CUSTOM_ID")))
1032         (when (not (null custom_id))
1033           (setq results (append results (list custom_id)))))))
1034 results))
1035 #+END_SRC
1036
1037 Here we get a list of the labels defined as raw latex labels, e.g. \label{eqtre}.
1038 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1039 (defun org-ref-get-latex-labels ()
1040   (save-excursion
1041     (goto-char (point-min))
1042     (let ((matches '()))
1043       (while (re-search-forward "\\\\label{\\([a-zA-z0-9:-]*\\)}" (point-max) t)
1044         (add-to-list 'matches (match-string-no-properties 1) t))
1045 matches)))
1046 #+END_SRC
1047
1048 Finally, we get the table names.
1049
1050 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1051 (defun org-ref-get-tblnames ()
1052   (org-element-map (org-element-parse-buffer 'element) 'table
1053     (lambda (table)
1054       (org-element-property :name table))))
1055 #+END_SRC
1056
1057 Now, we can put all the labels together which will give us a list of candidates.
1058
1059 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
1060 (defun org-ref-get-labels ()
1061   "returns a list of labels in the buffer that you can make a ref link to. this is used to auto-complete ref links."
1062   (save-excursion
1063     (save-restriction
1064       (widen)
1065       (goto-char (point-min))
1066       (let ((matches '()))
1067         (while (re-search-forward "label:\\([a-zA-z0-9:-]*\\)" (point-max) t)
1068           (add-to-list 'matches (match-string-no-properties 1) t))
1069         (append matches (org-ref-get-org-labels) (org-ref-get-latex-labels) (org-ref-get-tblnames) (org-ref-get-custom-ids))))))
1070 #+END_SRC
1071
1072 Now we create the completion function. This works from the org-machinery, e.g. if you type C-c C-l to insert a link, and use completion by pressing tab.
1073
1074 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
1075 (defun org-ref-complete-link (&optional arg)
1076   "Completion function for ref links"
1077   (let ((label))
1078     (setq label (completing-read "label: " (org-ref-get-labels)))
1079     (format "ref:%s" label)))
1080 #+END_SRC
1081
1082 Alternatively, you may want to just call a function that inserts a link with completion:
1083
1084 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
1085 (defun org-ref-insert-ref-link ()
1086  (interactive)
1087  (insert (org-ref-complete-link)))
1088 #+END_SRC
1089
1090 ** pageref
1091
1092 This refers to the page of a label in LaTeX.
1093
1094 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
1095 (org-add-link-type
1096  "pageref"
1097  (lambda (label)
1098    "on clicking goto the label. Navigate back with C-c &"
1099    (org-mark-ring-push)
1100    ;; next search from beginning of the buffer
1101    (widen)
1102    (unless
1103        (or
1104         ;; our label links
1105         (progn
1106           (goto-char (point-min))
1107           (re-search-forward (format "label:%s\\b" label) nil t))
1108
1109         ;; a latex label
1110         (progn
1111           (goto-char (point-min))
1112           (re-search-forward (format "\\label{%s}" label) nil t))
1113
1114         ;; #+label: name  org-definition
1115         (progn
1116           (goto-char (point-min))
1117           (re-search-forward (format "^#\\+label:\\s-*\\(%s\\)\\b" label) nil t))
1118
1119         ;; org tblname
1120         (progn
1121           (goto-char (point-min))
1122           (re-search-forward (format "^#\\+tblname:\\s-*\\(%s\\)\\b" label) nil t))
1123
1124 ;; Commented out because these ref links do not actually translate correctly in LaTeX.
1125 ;; you need [[#label]] links.
1126         ;; CUSTOM_ID
1127 ;       (progn
1128 ;         (goto-char (point-min))
1129 ;         (re-search-forward (format ":CUSTOM_ID:\s-*\\(%s\\)" label) nil t))
1130         )
1131      ;; we did not find anything, so go back to where we came
1132      (org-mark-ring-goto)
1133      (error "%s not found" label))
1134    (message "go back with (org-mark-ring-goto) `C-c &`"))
1135  ;formatting
1136  (lambda (keyword desc format)
1137    (cond
1138     ((eq format 'html) (format "(<pageref>%s</pageref>)" path))
1139     ((eq format 'latex)
1140      (format "\\pageref{%s}" keyword)))))
1141 #+END_SRC
1142
1143 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
1144 (defun org-pageref-complete-link (&optional arg)
1145   "Completion function for ref links"
1146   (let ((label))
1147     (setq label (completing-read "label: " (org-ref-get-labels)))
1148     (format "ref:%s" label)))
1149 #+END_SRC
1150
1151 Alternatively, you may want to just call a function that inserts a link with completion:
1152
1153 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
1154 (defun org-pageref-insert-ref-link ()
1155  (interactive)
1156  (insert (org-pageref-complete-link)))
1157 #+END_SRC
1158
1159 ** nameref
1160
1161 The nameref link allows you make links to the text of a section with a label. Clicking on the link takes you to the label, and provides a mark to go back to. This only works if you put a raw latex label in the headline.
1162
1163 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
1164 (org-add-link-type
1165  "nameref"
1166  (lambda (label)
1167    "on clicking goto the label. Navigate back with C-c &"
1168    (org-mark-ring-push)
1169    ;; next search from beginning of the buffer
1170    (widen)
1171    (unless
1172        (or
1173         ;; a latex label
1174         (progn
1175           (goto-char (point-min))
1176           (re-search-forward (format "\\label{%s}" label) nil t))
1177         )
1178      ;; we did not find anything, so go back to where we came
1179      (org-mark-ring-goto)
1180      (error "%s not found" label))
1181    (message "go back with (org-mark-ring-goto) `C-c &`"))
1182  ;formatting
1183  (lambda (keyword desc format)
1184    (cond
1185     ((eq format 'html) (format "(<nameref>%s</nameref>)" path))
1186     ((eq format 'latex)
1187      (format "\\nameref{%s}" keyword)))))
1188 #+END_SRC
1189
1190 ** eqref
1191 This is just the LaTeX ref for equations. On export, the reference is enclosed in parentheses.
1192
1193 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
1194 (org-add-link-type
1195  "eqref"
1196  (lambda (label)
1197    "on clicking goto the label. Navigate back with C-c &"
1198    (org-mark-ring-push)
1199    ;; next search from beginning of the buffer
1200    (widen)
1201    (goto-char (point-min))
1202    (unless
1203        (or
1204         ;; search forward for the first match
1205         ;; our label links
1206         (re-search-forward (format "label:%s" label) nil t)
1207         ;; a latex label
1208         (re-search-forward (format "\\label{%s}" label) nil t)
1209         ;; #+label: name  org-definition
1210         (re-search-forward (format "^#\\+label:\\s-*\\(%s\\)\\b" label) nil t))
1211      (org-mark-ring-goto)
1212      (error "%s not found" label))
1213    (message "go back with (org-mark-ring-goto) `C-c &`"))
1214  ;formatting
1215  (lambda (keyword desc format)
1216    (cond
1217     ((eq format 'html) (format "(<eqref>%s</eqref>)" path))
1218     ((eq format 'latex)
1219      (format "\\eqref{%s}" keyword)))))
1220 #+END_SRC
1221
1222 ** cite
1223 This is the main reason this library exists. We want the following behavior. A cite link should be able to contain multiple bibtex keys. You should be able to click on the link, and get a brief citation of the entry for that key, and a menu of options to open the bibtex file, open a pdf if you have it, open your notes on the entry, or open a url if it exists. You should be able to insert new references onto an existing cite link, or create new ones easily. The following code implements these features.
1224
1225 *** Implementing the click actions of cite
1226
1227 **** Getting the key we clicked on
1228 The first thing we need is to get the bibtex key we clicked on.
1229
1230 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
1231 (defun org-ref-get-bibtex-key-under-cursor ()
1232   "returns key under the bibtex cursor. We search forward from
1233 point to get a comma, or the end of the link, and then backwards
1234 to get a comma, or the beginning of the link. that delimits the
1235 keyword we clicked on. We also strip the text properties."
1236   (interactive)
1237   (let* ((object (org-element-context))
1238          (link-string (org-element-property :path object)))
1239     ;; you may click on the part before the citations. here we make
1240     ;; sure to move to the beginning so you get the first citation.
1241     (let ((cp (point)))
1242       (goto-char (org-element-property :begin object))
1243       (search-forward link-string (org-element-property :end object))
1244       (goto-char (match-beginning 0))
1245       ;; check if we clicked before the path and move as needed.
1246       (unless (< cp (point))
1247         (goto-char cp)))
1248
1249     (if (not (org-element-property :contents-begin object))
1250         ;; this means no description in the link
1251         (progn
1252           ;; we need the link path start and end
1253           (save-excursion
1254             (goto-char (org-element-property :begin object))
1255             (search-forward link-string nil nil 1)
1256             (setq link-string-beginning (match-beginning 0))
1257             (setq link-string-end (match-end 0)))
1258
1259           ;; The key is the text between commas, or the link boundaries
1260           (save-excursion
1261             (if (search-forward "," link-string-end t 1)
1262                 (setq key-end (- (match-end 0) 1)) ; we found a match
1263               (setq key-end link-string-end))) ; no comma found so take the end
1264           ;; and backward to previous comma from point which defines the start character
1265           (save-excursion
1266             (if (search-backward "," link-string-beginning 1 1)
1267                 (setq key-beginning (+ (match-beginning 0) 1)) ; we found a match
1268               (setq key-beginning link-string-beginning))) ; no match found
1269           ;; save the key we clicked on.
1270           (setq bibtex-key (org-ref-strip-string (buffer-substring key-beginning key-end)))
1271           (set-text-properties 0 (length bibtex-key) nil bibtex-key)
1272           bibtex-key)
1273       ;; link with description. assume only one key
1274       link-string)))
1275 #+END_SRC
1276
1277 We also need to find which bibliography file that key is in. For that, we need to know which bibliography files are referred to in the file. If none are specified with a bibliography link, we use the default bibliography. This function searches for a bibliography link, and then the LaTeX bibliography link. We also consider the addbibresource link which is used with biblatex.
1278
1279 **** Getting the bibliographies
1280 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1281 (defun org-ref-find-bibliography ()
1282   "find the bibliography in the buffer.
1283 This function sets and returns cite-bibliography-files, which is a list of files
1284 either from bibliography:f1.bib,f2.bib
1285 \bibliography{f1,f2}
1286 internal bibliographies
1287
1288 falling back to what the user has set in org-ref-default-bibliography
1289 "
1290   (interactive)
1291   (catch 'result
1292     (save-excursion
1293       (goto-char (point-min))
1294       ;;  look for a bibliography link
1295       (when (re-search-forward "\\<bibliography:\\([^\]\|\n]+\\)" nil t)
1296         (setq org-ref-bibliography-files
1297               (mapcar 'org-ref-strip-string (split-string (match-string 1) ",")))
1298         (throw 'result org-ref-bibliography-files))
1299
1300
1301       ;; we did not find a bibliography link. now look for \bibliography
1302       (goto-char (point-min))
1303       (when (re-search-forward "\\\\bibliography{\\([^}]+\\)}" nil t)
1304         ;; split, and add .bib to each file
1305         (setq org-ref-bibliography-files
1306               (mapcar (lambda (x) (concat x ".bib"))
1307                       (mapcar 'org-ref-strip-string
1308                               (split-string (match-string 1) ","))))
1309         (throw 'result org-ref-bibliography-files))
1310
1311       ;; no bibliography found. maybe we need a biblatex addbibresource
1312       (goto-char (point-min))
1313       ;;  look for a bibliography link
1314       (when (re-search-forward "addbibresource:\\([^\]\|\n]+\\)" nil t)
1315         (setq org-ref-bibliography-files
1316               (mapcar 'org-ref-strip-string (split-string (match-string 1) ",")))
1317         (throw 'result org-ref-bibliography-files))
1318
1319       ;; we did not find anything. use defaults
1320       (setq org-ref-bibliography-files org-ref-default-bibliography)))
1321
1322     ;; set reftex-default-bibliography so we can search
1323     (set (make-local-variable 'reftex-default-bibliography) org-ref-bibliography-files)
1324     org-ref-bibliography-files)
1325 #+END_SRC
1326
1327 **** Finding the bibliography file a key is in
1328 Now, we can see if an entry is in a file.
1329
1330 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1331 (defun org-ref-key-in-file-p (key filename)
1332   "determine if the key is in the file"
1333   (interactive "skey: \nsFile: ")
1334   (save-current-buffer
1335     (let ((bibtex-files (list filename)))
1336       (bibtex-search-entry key t))))
1337 #+END_SRC
1338
1339 Finally, we want to know which file the key is in.
1340
1341 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1342 (defun org-ref-get-bibtex-key-and-file (&optional key)
1343   "returns the bibtex key and file that it is in. If no key is provided, get one under point"
1344  (interactive)
1345  (let ((org-ref-bibliography-files (org-ref-find-bibliography))
1346        (file))
1347    (unless key
1348      (setq key (org-ref-get-bibtex-key-under-cursor)))
1349    (setq file     (catch 'result
1350                     (loop for file in org-ref-bibliography-files do
1351                           (if (org-ref-key-in-file-p key (file-truename file))
1352                               (throw 'result file)))))
1353    (cons key file)))
1354 #+END_SRC
1355
1356 **** convenience functions to act on citation at point
1357      :PROPERTIES:
1358      :ID:       af0b2a82-a7c9-4c08-9dac-09f93abc4a92
1359      :END:
1360 We need some convenience functions to open act on the citation at point. These will get the pdf, open the url, or open the notes.
1361
1362 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1363 (defun org-ref-open-pdf-at-point ()
1364   "open the pdf for bibtex key under point if it exists"
1365   (interactive)
1366   (let* ((results (org-ref-get-bibtex-key-and-file))
1367          (key (car results))
1368          (pdf-file (format (concat org-ref-pdf-directory "%s.pdf") key)))
1369     (if (file-exists-p pdf-file)
1370         (org-open-file pdf-file)
1371 (message "no pdf found for %s" key))))
1372
1373
1374 (defun org-ref-open-url-at-point ()
1375   "open the url for bibtex key under point."
1376   (interactive)
1377   (let* ((results (org-ref-get-bibtex-key-and-file))
1378          (key (car results))
1379          (bibfile (cdr results)))
1380     (save-excursion
1381       (with-temp-buffer
1382         (insert-file-contents bibfile)
1383         (bibtex-search-entry key)
1384         ;; I like this better than bibtex-url which does not always find
1385         ;; the urls
1386         (catch 'done
1387           (let ((url (bibtex-autokey-get-field "url")))
1388             (when  url
1389               (browse-url url)
1390               (throw 'done nil)))
1391
1392           (let ((doi (bibtex-autokey-get-field "doi")))
1393             (when doi
1394               (if (string-match "^http" doi)
1395                   (browse-url doi)
1396                 (browse-url (format "http://dx.doi.org/%s" doi)))
1397               (throw 'done nil))))))))
1398
1399
1400 (defun org-ref-open-notes-at-point ()
1401   "open the notes for bibtex key under point."
1402   (interactive)
1403   (let* ((results (org-ref-get-bibtex-key-and-file))
1404          (key (car results))
1405          (bibfile (cdr results)))
1406     (save-excursion
1407       (with-temp-buffer
1408         (insert-file-contents bibfile)
1409         (bibtex-search-entry key)
1410         (org-ref-open-bibtex-notes)))))
1411
1412
1413 (defun org-ref-citation-at-point ()
1414   "give message of current citation at point"
1415   (interactive)
1416   (let* ((cb (current-buffer))
1417         (results (org-ref-get-bibtex-key-and-file))
1418         (key (car results))
1419         (bibfile (cdr results)))
1420     (message "%s" (progn
1421                     (with-temp-buffer
1422                       (insert-file-contents bibfile)
1423                       (bibtex-search-entry key)
1424                       (org-ref-bib-citation))))))
1425
1426
1427 (defun org-ref-open-citation-at-point ()
1428   "open bibtex file to key at point"
1429   (interactive)
1430   (let* ((cb (current-buffer))
1431         (results (org-ref-get-bibtex-key-and-file))
1432         (key (car results))
1433         (bibfile (cdr results)))
1434     (find-file bibfile)
1435     (bibtex-search-entry key)))
1436 #+END_SRC
1437
1438 **** the actual minibuffer menu
1439 Now, we create the menu. This is a rewrite of the cite action. This makes the function extendable by users.
1440
1441 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
1442 (defvar org-ref-cite-menu-funcs '()
1443  "Functions to run on cite click menu. Each entry is a list of (key menu-name function).
1444 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.")
1445
1446
1447 (defvar org-ref-user-cite-menu-funcs
1448   '(("C" "rossref" org-ref-crossref-at-point)
1449     ("y" "Copy entry to file" org-ref-copy-entry-at-point-to-file)
1450     ("s" "Copy summary" org-ref-copy-entry-as-summary))
1451   "user-defined functions to run on bibtex key at point.")
1452
1453
1454 (defun org-ref-copy-entry-as-summary ()
1455   "Copy the bibtex entry for the citation at point as a summary."
1456   (interactive)
1457     (save-window-excursion
1458       (org-ref-open-citation-at-point)
1459       (kill-new (org-ref-bib-citation))))
1460
1461
1462 (defun org-ref-copy-entry-at-point-to-file ()
1463   "Copy the bibtex entry for the citation at point to NEW-FILE.
1464 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."
1465   (interactive)
1466   (let ((new-file (ido-completing-read
1467                    "Copy to bibfile: "
1468                    (append org-ref-default-bibliography
1469                            (f-entries "." (lambda (f) (f-ext? f "bib"))))))
1470         (key (org-ref-get-bibtex-key-under-cursor)))
1471     (save-window-excursion
1472       (org-ref-open-citation-at-point)
1473       (bibtex-copy-entry-as-kill))
1474
1475     (let ((bibtex-files (list (file-truename new-file))))
1476       (if (assoc key (bibtex-global-key-alist))
1477           (message "That key already exists in %s" new-file)
1478         ;; add to file
1479         (save-window-excursion
1480           (find-file new-file)
1481           (goto-char (point-max))
1482           ;; make sure we are at the beginning of a line.
1483           (unless (looking-at "^") (insert "\n\n"))
1484           (bibtex-yank)
1485           (save-buffer))))))
1486
1487
1488 (defun org-ref-get-doi-at-point ()
1489   "Get doi for key at point."
1490   (interactive)
1491   (let* ((results (org-ref-get-bibtex-key-and-file))
1492          (key (car results))
1493          (bibfile (cdr results))
1494          doi)
1495     (save-excursion
1496       (with-temp-buffer
1497         (insert-file-contents bibfile)
1498         (bibtex-search-entry key)
1499         (setq doi (bibtex-autokey-get-field "doi"))
1500         ;; in case doi is a url, remove the url part.
1501         (replace-regexp-in-string "^http://dx.doi.org/" "" doi)))))
1502
1503
1504 ;; functions that operate on key at point for click menu
1505 (defun org-ref-wos-at-point ()
1506   "open the doi in wos for bibtex key under point."
1507   (interactive)
1508   (doi-utils-wos (org-ref-get-doi-at-point)))
1509
1510
1511 (defun org-ref-wos-citing-at-point ()
1512   "open the doi in wos citing articles for bibtex key under point."
1513   (interactive)
1514   (doi-utils-wos-citing (org-ref-get-doi-at-point)))
1515
1516
1517 (defun org-ref-wos-related-at-point ()
1518   "open the doi in wos related articles for bibtex key under point."
1519   (interactive)
1520   (doi-utils-wos-related (org-ref-get-doi-at-point)))
1521
1522
1523 (defun org-ref-google-scholar-at-point ()
1524   "open the doi in google scholar for bibtex key under point."
1525   (interactive)
1526   (doi-utils-google-scholar (org-ref-get-doi-at-point)))
1527
1528
1529 (defun org-ref-pubmed-at-point ()
1530   "open the doi in pubmed for bibtex key under point."
1531   (interactive)
1532   (doi-utils-pubmed (org-ref-get-doi-at-point)))
1533
1534
1535 (defun org-ref-crossref-at-point ()
1536   "open the doi in crossref for bibtex key under point."
1537   (interactive)
1538   (doi-utils-crossref (org-ref-get-doi-at-point)))
1539
1540
1541 (defun org-ref-cite-onclick-minibuffer-menu (&optional link-string)
1542   "action when a cite link is clicked on.
1543 Provides a menu of context sensitive actions. If the bibtex entry has a pdf, you get an option to open it. If there is a doi, you get a lot of options."
1544   (interactive)
1545   (let* ((results (org-ref-get-bibtex-key-and-file))
1546          (key (car results))
1547          (pdf-file (format (concat org-ref-pdf-directory "%s.pdf") key))
1548          (bibfile (cdr results))
1549          (url (save-excursion
1550                 (with-temp-buffer
1551                   (insert-file-contents bibfile)
1552                   (bibtex-search-entry key)
1553                   (bibtex-autokey-get-field "url"))))
1554          (doi (save-excursion
1555                 (with-temp-buffer
1556                   (insert-file-contents bibfile)
1557                   (bibtex-search-entry key)
1558                   ;; I like this better than bibtex-url which does not always find
1559                   ;; the urls
1560                   (bibtex-autokey-get-field "doi")))))
1561
1562     (when (string= "" doi) (setq doi nil))
1563     (when (string= "" url) (setq url nil))
1564     (setq org-ref-cite-menu-funcs '())
1565
1566     ;; open action
1567     (when
1568         bibfile
1569       (add-to-list
1570        'org-ref-cite-menu-funcs
1571        '("o" "pen" org-ref-open-citation-at-point)))
1572
1573     ;; pdf
1574     (when (file-exists-p pdf-file)
1575       (add-to-list
1576        'org-ref-cite-menu-funcs
1577        `("p" "df" ,org-ref-open-pdf-function) t))
1578
1579     ;; notes
1580     (add-to-list
1581      'org-ref-cite-menu-funcs
1582      '("n" "otes" org-ref-open-notes-at-point) t)
1583
1584     ;; url
1585     (when (or url doi)
1586       (add-to-list
1587        'org-ref-cite-menu-funcs
1588        '("u" "rl" org-ref-open-url-at-point) t))
1589
1590     ;; doi funcs
1591     (when doi
1592       (add-to-list
1593        'org-ref-cite-menu-funcs
1594        '("w" "os" org-ref-wos-at-point) t)
1595
1596       (add-to-list
1597        'org-ref-cite-menu-funcs
1598        '("c" "iting" org-ref-wos-citing-at-point) t)
1599
1600       (add-to-list
1601        'org-ref-cite-menu-funcs
1602        '("r" "elated" org-ref-wos-related-at-point) t)
1603
1604       (add-to-list
1605        'org-ref-cite-menu-funcs
1606        '("g" "oogle scholar" org-ref-google-scholar-at-point) t)
1607
1608       (add-to-list
1609        'org-ref-cite-menu-funcs
1610        '("P" "ubmed" org-ref-pubmed-at-point) t))
1611
1612     ;; add user functions
1613     (dolist (tup org-ref-user-cite-menu-funcs)
1614       (add-to-list
1615        'org-ref-cite-menu-funcs
1616        tup t))
1617
1618     ;; finally quit
1619     (add-to-list
1620      'org-ref-cite-menu-funcs
1621      '("q" "uit" (lambda ())) t)
1622
1623     ;; now we make a menu
1624     ;; construct menu string as a message
1625     (message
1626      (concat
1627       (let* ((results (org-ref-get-bibtex-key-and-file))
1628              (key (car results))
1629              (bibfile (cdr results)))
1630         (save-excursion
1631           (with-temp-buffer
1632             (insert-file-contents bibfile)
1633             (bibtex-search-entry key)
1634             (org-ref-bib-citation))))
1635       "\n"
1636       (mapconcat
1637        (lambda (tup)
1638          (concat "[" (elt tup 0) "]"
1639                  (elt tup 1) " "))
1640        org-ref-cite-menu-funcs "")))
1641     ;; get the input
1642     (let* ((input (read-char-exclusive))
1643            (choice (assoc
1644                     (char-to-string input) org-ref-cite-menu-funcs)))
1645       ;; now run the function (2nd element in choice)
1646       (when choice
1647         (funcall
1648          (elt
1649           choice
1650           2))))))
1651 #+END_SRC
1652
1653 #+RESULTS:
1654 : org-ref-cite-onclick-minibuffer-menu
1655
1656 *** A function to format a cite link
1657
1658 Next, we define a formatting function for the cite link. This is done so that the cite link definition is very short, and easy to change. You just need to specify the functions in the definition. This function is deprecated. The formatting is defined later automatically.
1659
1660 #+BEGIN_SRC emacs-lisp  :tangle no
1661 ;(defun org-ref-cite-link-format (keyword desc format)
1662 ;   (cond
1663 ;    ((eq format 'html) (mapconcat (lambda (key) (format "<a name=\"#%s\">%s</a>" key key) (org-ref-split-and-strip-string keyword) ",")))
1664 ;    ((eq format 'latex)
1665 ;     (concat "\\cite" (when desc (format "[%s]" desc)) "{"
1666 ;            (mapconcat (lambda (key) key) (org-ref-split-and-strip-string keyword) ",")
1667 ;            "}"))))
1668 #+END_SRC
1669
1670 *** The actual cite link
1671 Finally, we define the cite link. This is deprecated; the links are autogenerated later. This is here for memory.
1672
1673 #+BEGIN_SRC emacs-lisp :tangle no
1674 ;(org-add-link-type
1675 ; "cite"
1676 ; 'org-ref-cite-onclick-minibuffer-menu
1677 ; 'org-ref-cite-link-format)
1678 #+END_SRC
1679
1680 *** Automatic definition of the cite links
1681 There are many different kinds of citations in LaTeX, but they are all variants of a basic syntax of \citetype[optional text]{label1,label2}. Here we use lisp to generate the link definitions. We define a function that creates the code to create the link, and then we evaluate it. We also create the completion function for the new link, and add it to the list of known links.
1682
1683 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1684 (defmacro org-ref-make-completion-function (type)
1685   `(defun ,(intern (format "org-%s-complete-link" type)) (&optional arg)
1686      (interactive)
1687      (format "%s:%s"
1688              ,type
1689              (completing-read
1690               "bibtex key: "
1691               (let ((bibtex-files (org-ref-find-bibliography)))
1692                 (bibtex-global-key-alist))))))
1693 #+END_SRC
1694
1695 We will want to generate formatting functions for each citation type. The reason for doing this is so we can on the fly change the formatting later.
1696
1697 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1698 (defmacro org-ref-make-format-function (type)
1699   `(defun ,(intern (format "org-ref-format-%s" type)) (keyword desc format)
1700      (cond
1701       ((eq format 'org)
1702        (mapconcat
1703         (lambda (key)
1704           (format "[[#%s][%s]]" key key))
1705         (org-ref-split-and-strip-string keyword) ","))
1706
1707       ((eq format 'ascii)
1708        (concat "["
1709                (mapconcat
1710                 (lambda (key)
1711                   (format "%s" key))
1712                 (org-ref-split-and-strip-string keyword) ",") "]"))
1713
1714       ((eq format 'html)
1715        (mapconcat
1716         (lambda (key)
1717           (format "<a href=\"#%s\">%s</a>" key key))
1718         (org-ref-split-and-strip-string keyword) ","))
1719
1720       ((eq format 'latex)
1721        (if (string= (substring type -1) "s")
1722            ;; biblatex format for multicite commands, which all end in s. These are formated as \cites{key1}{key2}...
1723            (concat "\\" ,type (mapconcat (lambda (key) (format "{%s}"  key))
1724                                          (org-ref-split-and-strip-string keyword) ""))
1725          ;; bibtex format
1726        (concat "\\" ,type (when desc (org-ref-format-citation-description desc)) "{"
1727                (mapconcat (lambda (key) key) (org-ref-split-and-strip-string keyword) ",")
1728                "}"))))))
1729 #+END_SRC
1730
1731
1732
1733 We create the links by mapping the function onto the list of defined link types.
1734
1735 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1736 (defun org-ref-format-citation-description (desc)
1737   "return formatted citation description. if the cite link has a description, it is optional text for the citation command. You can specify pre and post text by separating these with ::."
1738   (interactive)
1739   (cond
1740    ((string-match "::" desc)
1741     (format "[%s][%s]" (car (setq results (split-string desc "::"))) (cadr results)))
1742    (t (format "[%s]" desc))))
1743
1744 (defun org-ref-define-citation-link (type &optional key)
1745   "add a citation link for org-ref. With optional key, set the reftex binding. For example:
1746 (org-ref-define-citation-link \"citez\" ?z) will create a new citez link, with reftex key of z,
1747 and the completion function."
1748   (interactive "sCitation Type: \ncKey: ")
1749
1750   ;; create the formatting function
1751   (eval `(org-ref-make-format-function ,type))
1752
1753   (eval-expression
1754    `(org-add-link-type
1755      ,type
1756      org-ref-cite-onclick-function
1757      (quote ,(intern (format "org-ref-format-%s" type)))))
1758
1759   ;; create the completion function
1760   (eval `(org-ref-make-completion-function ,type))
1761
1762   ;; store new type so it works with adding citations, which checks
1763   ;; for existence in this list
1764   (add-to-list 'org-ref-cite-types type)
1765
1766   ;; and finally if a key is specified, we modify the reftex menu
1767   (when key
1768     (setf (nth 2 (assoc 'org reftex-cite-format-builtin))
1769           (append (nth 2 (assoc 'org reftex-cite-format-builtin))
1770                   `((,key  . ,(concat type ":%l")))))))
1771
1772 ;; create all the link types and their completion functions
1773 (mapcar 'org-ref-define-citation-link org-ref-cite-types)
1774 #+END_SRC
1775
1776 *** org-ref-insert-cite-link
1777 We need a convenient method to insert links. In reftex you use the keystroke C-c ], which gives you a minibuffer to search the bibtex files from. This function is bound to that same keystroke here [[*org-mode%20/%20reftex%20setup][org-mode / reftex setup]]. This function will append to a cite link if you call it while on a link.
1778
1779 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
1780 (defun org-ref-insert-cite-link (alternative-cite)
1781   "Insert a default citation link using reftex. If you are on a link, it
1782 appends to the end of the link, otherwise, a new link is
1783 inserted. Use a prefix arg to get a menu of citation types."
1784   (interactive "P")
1785   (org-ref-find-bibliography)
1786   (let* ((object (org-element-context))
1787          (link-string-beginning (org-element-property :begin object))
1788          (link-string-end (org-element-property :end object))
1789          (path (org-element-property :path object)))
1790
1791     (if (not alternative-cite)
1792
1793         (cond
1794          ;; case where we are in a link
1795          ((and (equal (org-element-type object) 'link)
1796                (-contains? org-ref-cite-types (org-element-property :type object)))
1797           (goto-char link-string-end)
1798           ;; sometimes there are spaces at the end of the link
1799           ;; this code moves point pack until no spaces are there
1800           (while (looking-back " ") (backward-char))
1801           (insert (concat "," (mapconcat 'identity (reftex-citation t ?a) ","))))
1802
1803          ;; We are next to a link, and we want to append
1804          ((save-excursion
1805             (backward-char)
1806             (and (equal (org-element-type (org-element-context)) 'link)
1807                  (-contains? org-ref-cite-types (org-element-property :type (org-element-context)))))
1808           (while (looking-back " ") (backward-char))
1809           (insert (concat "," (mapconcat 'identity (reftex-citation t ?a) ","))))
1810
1811          ;; insert fresh link
1812          (t
1813           (insert
1814            (concat org-ref-default-citation-link
1815                    ":"
1816                    (mapconcat 'identity (reftex-citation t) ",")))))
1817
1818       ;; you pressed a C-u so we run this code
1819       (reftex-citation)))
1820   )
1821 #+END_SRC
1822 cite:zhou-2004-first-lda-u,paier-2006-errat,boes-2015-estim-bulk
1823
1824
1825 #+RESULTS:
1826 : org-ref-insert-cite-link
1827
1828 *** Completion in cite links
1829 If you know the specific bibtex key, you may like to use completion directly. You use this with the org-mode machinery and tab completion. Here is the prototypical completion function. These are now all created when the links are created.
1830
1831 #+BEGIN_SRC emacs-lisp  :tangle no
1832 (defun org-cite-complete-link (&optional arg)
1833   "Completion function for cite links"
1834   (format "%s:%s"
1835           org-ref-default-citation-link
1836           (completing-read
1837            "bibtex key: "
1838            (let ((bibtex-files (org-ref-find-bibliography)))
1839              (bibtex-global-key-alist)))))
1840 #+END_SRC
1841
1842 Alternatively, you may shortcut the org-machinery with this command. You will be prompted for a citation type, and then offered key completion.
1843
1844 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1845 (defun org-ref-insert-cite-with-completion (type)
1846   "Insert a cite link with completion"
1847   (interactive (list (ido-completing-read "Type: " org-ref-cite-types)))
1848   (insert (funcall (intern (format "org-%s-complete-link" type)))))
1849 #+END_SRC
1850
1851 ** Storing links to a bibtex entry
1852 org-mode already defines a store link function for bibtex entries. It does not store the link I want though, it only stores a brief citation of the entry. I want a citation link. Here is a function to do that.
1853
1854 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1855 (defun org-ref-store-bibtex-entry-link ()
1856   "Save a citation link to the current bibtex entry. Saves in the default link type."
1857   (interactive)
1858   (let ((link (concat org-ref-default-citation-link
1859                  ":"
1860                  (save-excursion
1861                    (bibtex-beginning-of-entry)
1862                    (reftex-get-bib-field "=key=" (bibtex-parse-entry))))))
1863     (message "saved %s" link)
1864     (push (list link) org-stored-links)
1865     (car org-stored-links)))
1866 #+END_SRC
1867
1868 ** Index entries
1869 org-ref minimally supports index entries. To make an index in a file, you should put in the LaTeX header these lines
1870
1871
1872 #+LATEX_HEADER: \usepackage{makeidx}
1873 #+LATEX_HEADER: \makeindex
1874
1875
1876 Finally, put \makeindex at the end of the document where you want the index to appear. You will need to run the makeindex program at an appropriate point in your LaTeX to pdf, or use ox-manuscript, which will do it for you.
1877
1878
1879 Use index links to create entries (see http://en.wikibooks.org/wiki/LaTeX/Indexing). Clicking on an index link runs occur on the buffer for the entry. The link exports to LaTeX. Some links may need to be enclosed in double brackets if they have spaces in them.
1880
1881
1882 index:hello
1883 index:hello!Peter
1884 [[index:hello!Sam@\textsl{Sam}]]
1885 [[index:Lin@\textbf{Lin}]]
1886 [[index:Joe|textit]]
1887 [[index:Lin@\textbf{Lin}]]
1888 [[index:Peter|see {hello}]]
1889 [[index:Jen|seealso{Jenny}]]
1890
1891 index:encodings!input!cp850
1892
1893 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1894 (org-add-link-type
1895  "index"
1896  (lambda (path)
1897    (occur path))
1898
1899  (lambda (path desc format)
1900    (cond
1901     ((eq format 'latex)
1902       (format "\\index{%s}" path)))))
1903
1904 ;; this will generate a temporary index of entries in the file.
1905 (org-add-link-type
1906  "printindex"
1907  (lambda (path)
1908    (let ((*index-links* '())
1909          (*initial-letters* '()))
1910
1911      ;; get links
1912      (org-element-map (org-element-parse-buffer) 'link
1913        (lambda (link)
1914          (let ((type (nth 0 link))
1915                (plist (nth 1 link)))
1916
1917            (when (equal (plist-get plist ':type) "index")
1918              (add-to-list
1919               '*index-links*
1920               (cons (plist-get plist :path)
1921                     (format
1922                      "[[elisp:(progn (switch-to-buffer \"%s\") (goto-char %s))][%s]]"
1923 (current-buffer)
1924                      (plist-get plist :begin)  ;; position of link
1925                      ;; grab a description
1926                      (save-excursion
1927                        (goto-char (plist-get plist :begin))
1928                        (if (thing-at-point 'sentence)
1929                            ;; get a sentence
1930                            (replace-regexp-in-string
1931                             "\n" "" (thing-at-point 'sentence))
1932                          ;; or call it a link
1933                          "link")))))))))
1934
1935      ;; sort the links
1936      (setq *index-links*  (cl-sort *index-links* 'string-lessp :key 'car))
1937
1938      ;; now first letters
1939      (dolist (link *index-links*)
1940        (add-to-list '*initial-letters* (substring (car link) 0 1) t))
1941
1942      ;; now create the index
1943      (switch-to-buffer (get-buffer-create "*index*"))
1944      (org-mode)
1945      (erase-buffer)
1946      (insert "#+TITLE: Index\n\n")
1947      (dolist (letter *initial-letters*)
1948        (insert (format "* %s\n" (upcase letter)))
1949        ;; now process the links
1950        (while (and
1951                ,*index-links*
1952                (string= letter (substring (car (car *index-links*)) 0 1)))
1953          (let ((link (pop *index-links*)))
1954            (insert (format "%s %s\n\n" (car link) (cdr link))))))
1955      (switch-to-buffer "*index*")))
1956  ;; formatting
1957  (lambda (path desc format)
1958    (cond
1959     ((eq format 'latex)
1960       (format "\\printindex")))))
1961 #+END_SRC
1962
1963 #+RESULTS:
1964 | lambda | (path)             | (let ((*index-links* (quote nil)) (*initial-letters* (quote nil))) (org-element-map (org-element-parse-buffer) (quote link) (lambda (link) (let ((type (nth 0 link)) (plist (nth 1 link))) (when (equal (plist-get plist (quote :type)) index) (add-to-list (quote *index-links*) (cons (plist-get plist :path) (format [[elisp:(progn (switch-to-buffer "%s") (goto-char %s))][%s]] (current-buffer) (plist-get plist :begin) (save-excursion (goto-char (plist-get plist :begin)) (if (thing-at-point (quote sentence)) (replace-regexp-in-string \n  (thing-at-point (quote sentence))) link))))))))) (setq *index-links* (cl-sort *index-links* (quote string-lessp) :key (quote car))) (dolist (link *index-links*) (add-to-list (quote *initial-letters*) (substring (car link) 0 1) t)) (switch-to-buffer (get-buffer-create *index*)) (org-mode) (erase-buffer) (insert #+TITLE: Index\n\n) (dolist (letter *initial-letters*) (insert (format * %s\n (upcase letter))) (while (and *index-links* (string= letter (substring (car (car *index-links*)) 0 1))) (let ((link (pop *index-links*))) (insert (format %s %s\n\n (car link) (cdr link)))))) (switch-to-buffer *index*)) |
1965 | lambda | (path desc format) | (cond ((eq format (quote latex)) (format \printindex)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
1966
1967 ** Glossary
1968 org-ref provides some minimal support for a glossary. See http://en.wikibooks.org/wiki/LaTeX/Glossary for details. You need to put these lines in the header.
1969
1970 #+LATEX_HEADER: \usepackage{glossaries}
1971 #+LATEX_HEADER: \makeglossaries
1972
1973 And at the end of the document put \makeglossaries.
1974
1975 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1976 (org-add-link-type
1977  "newglossaryentry"
1978  nil ;; no follow action
1979  (lambda (path desc format)
1980    (cond
1981     ((eq format 'latex)
1982      (format "\\newglossaryentry{%s}{%s}" path desc)))))
1983
1984
1985 ;; link to entry
1986 (org-add-link-type
1987  "gls"
1988   nil ;; no follow action
1989  (lambda (path desc format)
1990    (cond
1991     ((eq format 'latex)
1992      (format "\\gls{%s}" path)))))
1993
1994 ;; plural
1995 (org-add-link-type
1996  "glspl"
1997   nil ;; no follow action
1998  (lambda (path desc format)
1999    (cond
2000     ((eq format 'latex)
2001      (format "\\glspl{%s}" path)))))
2002
2003 ;; capitalized link
2004 (org-add-link-type
2005  "Gls"
2006   nil ;; no follow action
2007  (lambda (path desc format)
2008    (cond
2009     ((eq format 'latex)
2010      (format "\\Gls{%s}" path)))))
2011
2012 ;; capitalized link
2013 (org-add-link-type
2014  "Glspl"
2015   nil ;; no follow action
2016  (lambda (path desc format)
2017    (cond
2018     ((eq format 'latex)
2019      (format "\\Glspl{%s}" path)))))
2020 #+END_SRC
2021
2022 #+RESULTS:
2023 | Glspl             | nil                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | (lambda (path desc format) (cond ((eq format (quote latex)) (format \Glspl{%s} path))))                                                                                                                                                                                                                                                                                                                     |
2024 | Gls               | nil                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | (lambda (path desc format) (cond ((eq format (quote latex)) (format \Gls{%s} path))))                                                                                                                                                                                                                                                                                                                       |
2025 | glspl             | nil                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | (lambda (path desc format) (cond ((eq format (quote latex)) (format \glspl{%s} path))))                                                                                                                                                                                                                                                                                                                     |
2026 | gls               | nil                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | (lambda (path desc format) (cond ((eq format (quote latex)) (format \gls{%s} path))))                                                                                                                                                                                                                                                                                                                       |
2027 | newglossaryentry  | nil                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | (lambda (path desc format) (cond ((eq format (quote latex)) (format \newglossaryentry{%s}{%s} path desc))))                                                                                                                                                                                                                                                                                                 |
2028 | google            | (lambda (link-string) (browse-url (format http://www.google.com/search?q=%s (url-hexify-string link-string))))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2029 | ResearcherID      | (lambda (link-string) (browse-url (format http://www.researcherid.com/rid/%s link-string)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2030 | orcid             | (lambda (link-string) (browse-url (format http://orcid.org/%s link-string)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2031 | message           | org-mac-message-open                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2032 | mac-outlook       | org-mac-outlook-message-open                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2033 | skim              | org-mac-skim-open                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2034 | addressbook       | org-mac-addressbook-item-open                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2035 | x-together-item   | org-mac-together-item-open                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2036 | ans               | (lambda (path) (let* ((fields (split-string path ::)) (label (nth 0 fields)) (data (nth 1 fields)) (data-file (format %s-%s.dat tq-userid label))) (let ((temp-file data-file) (temp-buffer (get-buffer-create (generate-new-buffer-name  *temp file*)))) (unwind-protect (prog1 (save-current-buffer (set-buffer temp-buffer) (insert data)) (save-current-buffer (set-buffer temp-buffer) (write-region nil nil temp-file nil 0))) (and (buffer-name temp-buffer) (kill-buffer temp-buffer)))) (mygit (format git add %s data-file)) (mygit (format git commit -m "%s" data-file)) (mygit git push origin master)))                                                                                                                                                                                              | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2037 | mc                | (lambda (link) (org-entry-put (point) ANSWER link) (save-restriction (save-excursion (org-narrow-to-subtree) (goto-char (point-max)) (if (bolp) nil (insert \n)) (insert (format # you chose %s link)))))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2038 | exercise          | (lambda (arg) (tq-check-internet) (tq-get-assignment arg))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2039 | solution          | (lambda (label) (tq-check-internet) (let ((default-directory (file-name-as-directory (expand-file-name tq-root-directory)))) (if (file-exists-p solutions) nil (make-directory solutions)) (let ((default-directory (file-name-as-directory (expand-file-name solutions)))) (if (file-exists-p label) (progn (find-file (concat label / label .org)) (tq-update)) (mygit (format git clone %s@%s:solutions/%s tq-current-course tq-git-server label)) (find-file (concat label / label .org))))))                                                                                                                                                                                                                                                                                                                  | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2040 | assignment        | (lambda (arg) (tq-check-internet) (tq-get-assignment arg))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2041 | doi               | doi-link-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2042 | bibentry          | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-bibentry                                                                                                                                                                                                                                                                                                                                                                                     |
2043 | Autocites         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Autocites                                                                                                                                                                                                                                                                                                                                                                                    |
2044 | autocites         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-autocites                                                                                                                                                                                                                                                                                                                                                                                    |
2045 | supercites        | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-supercites                                                                                                                                                                                                                                                                                                                                                                                   |
2046 | Textcites         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Textcites                                                                                                                                                                                                                                                                                                                                                                                    |
2047 | textcites         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-textcites                                                                                                                                                                                                                                                                                                                                                                                    |
2048 | Smartcites        | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Smartcites                                                                                                                                                                                                                                                                                                                                                                                   |
2049 | smartcites        | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-smartcites                                                                                                                                                                                                                                                                                                                                                                                   |
2050 | footcitetexts     | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-footcitetexts                                                                                                                                                                                                                                                                                                                                                                                |
2051 | footcites         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-footcites                                                                                                                                                                                                                                                                                                                                                                                    |
2052 | Parencites        | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Parencites                                                                                                                                                                                                                                                                                                                                                                                   |
2053 | parencites        | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-parencites                                                                                                                                                                                                                                                                                                                                                                                   |
2054 | Cites             | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Cites                                                                                                                                                                                                                                                                                                                                                                                        |
2055 | cites             | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-cites                                                                                                                                                                                                                                                                                                                                                                                        |
2056 | fnotecite         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-fnotecite                                                                                                                                                                                                                                                                                                                                                                                    |
2057 | Pnotecite         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Pnotecite                                                                                                                                                                                                                                                                                                                                                                                    |
2058 | pnotecite         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-pnotecite                                                                                                                                                                                                                                                                                                                                                                                    |
2059 | Notecite          | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Notecite                                                                                                                                                                                                                                                                                                                                                                                     |
2060 | notecite          | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-notecite                                                                                                                                                                                                                                                                                                                                                                                     |
2061 | footfullcite      | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-footfullcite                                                                                                                                                                                                                                                                                                                                                                                 |
2062 | fullcite          | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-fullcite                                                                                                                                                                                                                                                                                                                                                                                     |
2063 | citeurl           | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citeurl                                                                                                                                                                                                                                                                                                                                                                                      |
2064 | citedate*         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citedate*                                                                                                                                                                                                                                                                                                                                                                                    |
2065 | citedate          | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citedate                                                                                                                                                                                                                                                                                                                                                                                     |
2066 | citetitle*        | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citetitle*                                                                                                                                                                                                                                                                                                                                                                                   |
2067 | citetitle         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citetitle                                                                                                                                                                                                                                                                                                                                                                                    |
2068 | Citeauthor*       | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Citeauthor*                                                                                                                                                                                                                                                                                                                                                                                  |
2069 | Autocite*         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Autocite*                                                                                                                                                                                                                                                                                                                                                                                    |
2070 | autocite*         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-autocite*                                                                                                                                                                                                                                                                                                                                                                                    |
2071 | Autocite          | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Autocite                                                                                                                                                                                                                                                                                                                                                                                     |
2072 | autocite          | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-autocite                                                                                                                                                                                                                                                                                                                                                                                     |
2073 | supercite         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-supercite                                                                                                                                                                                                                                                                                                                                                                                    |
2074 | parencite*        | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-parencite*                                                                                                                                                                                                                                                                                                                                                                                   |
2075 | cite*             | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-cite*                                                                                                                                                                                                                                                                                                                                                                                        |
2076 | Smartcite         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Smartcite                                                                                                                                                                                                                                                                                                                                                                                    |
2077 | smartcite         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-smartcite                                                                                                                                                                                                                                                                                                                                                                                    |
2078 | Textcite          | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Textcite                                                                                                                                                                                                                                                                                                                                                                                     |
2079 | textcite          | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-textcite                                                                                                                                                                                                                                                                                                                                                                                     |
2080 | footcitetext      | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-footcitetext                                                                                                                                                                                                                                                                                                                                                                                 |
2081 | footcite          | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-footcite                                                                                                                                                                                                                                                                                                                                                                                     |
2082 | Parencite         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Parencite                                                                                                                                                                                                                                                                                                                                                                                    |
2083 | parencite         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-parencite                                                                                                                                                                                                                                                                                                                                                                                    |
2084 | Cite              | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Cite                                                                                                                                                                                                                                                                                                                                                                                         |
2085 | Citeauthor        | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Citeauthor                                                                                                                                                                                                                                                                                                                                                                                   |
2086 | Citealp           | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Citealp                                                                                                                                                                                                                                                                                                                                                                                      |
2087 | Citealt           | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Citealt                                                                                                                                                                                                                                                                                                                                                                                      |
2088 | Citep             | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Citep                                                                                                                                                                                                                                                                                                                                                                                        |
2089 | Citet             | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-Citet                                                                                                                                                                                                                                                                                                                                                                                        |
2090 | citeyear*         | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citeyear*                                                                                                                                                                                                                                                                                                                                                                                    |
2091 | citeyear          | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citeyear                                                                                                                                                                                                                                                                                                                                                                                     |
2092 | citeauthor*       | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citeauthor*                                                                                                                                                                                                                                                                                                                                                                                  |
2093 | citeauthor        | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citeauthor                                                                                                                                                                                                                                                                                                                                                                                   |
2094 | citetext          | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citetext                                                                                                                                                                                                                                                                                                                                                                                     |
2095 | citenum           | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citenum                                                                                                                                                                                                                                                                                                                                                                                      |
2096 | citealp*          | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citealp*                                                                                                                                                                                                                                                                                                                                                                                     |
2097 | citealp           | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citealp                                                                                                                                                                                                                                                                                                                                                                                      |
2098 | citealt*          | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citealt*                                                                                                                                                                                                                                                                                                                                                                                     |
2099 | citealt           | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citealt                                                                                                                                                                                                                                                                                                                                                                                      |
2100 | citep*            | org-ref-cite-onclick-minibuffer-menu                                                                                                                                           &nb