]> git.donarmstrong.com Git - org-ref.git/blob - org-ref.org
0908cd4aeed87971cda7770d3a37ca6eafaebbe5
[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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citep*                                                                                                                                                                                                                                                                                                                                                                                       |
2101 | citep             | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citep                                                                                                                                                                                                                                                                                                                                                                                        |
2102 | citet*            | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citet*                                                                                                                                                                                                                                                                                                                                                                                       |
2103 | citet             | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-citet                                                                                                                                                                                                                                                                                                                                                                                        |
2104 | nocite            | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-nocite                                                                                                                                                                                                                                                                                                                                                                                       |
2105 | cite              | org-ref-cite-onclick-minibuffer-menu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | org-ref-format-cite                                                                                                                                                                                                                                                                                                                                                                                         |
2106 | eqref             | (lambda (label) on clicking goto the label. Navigate back with C-c & (org-mark-ring-push) (widen) (goto-char (point-min)) (if (or (re-search-forward (format label:%s label) nil t) (re-search-forward (format \label{%s} label) nil t) (re-search-forward (format ^#\+label:\s-*\(%s\)\b label) nil t)) nil (org-mark-ring-goto) (error %s not found label)) (message go back with (org-mark-ring-goto) `C-c &`))                                                                                                                                                                                                                                                                                                                                                                                                 | (lambda (keyword desc format) (cond ((eq format (quote html)) (format (<eqref>%s</eqref>) path)) ((eq format (quote latex)) (format \eqref{%s} keyword))))                                                                                                                                                                                                                                                  |
2107 | nameref           | (lambda (label) on clicking goto the label. Navigate back with C-c & (org-mark-ring-push) (widen) (if (or (progn (goto-char (point-min)) (re-search-forward (format \label{%s} label) nil t))) nil (org-mark-ring-goto) (error %s not found label)) (message go back with (org-mark-ring-goto) `C-c &`))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | (lambda (keyword desc format) (cond ((eq format (quote html)) (format (<nameref>%s</nameref>) path)) ((eq format (quote latex)) (format \nameref{%s} keyword))))                                                                                                                                                                                                                                            |
2108 | pageref           | (lambda (label) on clicking goto the label. Navigate back with C-c & (org-mark-ring-push) (widen) (if (or (progn (goto-char (point-min)) (re-search-forward (format label:%s\b label) nil t)) (progn (goto-char (point-min)) (re-search-forward (format \label{%s} label) nil t)) (progn (goto-char (point-min)) (re-search-forward (format ^#\+label:\s-*\(%s\)\b label) nil t)) (progn (goto-char (point-min)) (re-search-forward (format ^#\+tblname:\s-*\(%s\)\b label) nil t))) nil (org-mark-ring-goto) (error %s not found label)) (message go back with (org-mark-ring-goto) `C-c &`))                                                                                                                                                                                                                     | (lambda (keyword desc format) (cond ((eq format (quote html)) (format (<pageref>%s</pageref>) path)) ((eq format (quote latex)) (format \pageref{%s} keyword))))                                                                                                                                                                                                                                            |
2109 | ref               | (lambda (label) on clicking goto the label. Navigate back with C-c & (org-mark-ring-push) (widen) (if (or (progn (goto-char (point-min)) (re-search-forward (format label:%s\b label) nil t)) (progn (goto-char (point-min)) (re-search-forward (format \label{%s} label) nil t)) (progn (goto-char (point-min)) (re-search-forward (format ^#\+label:\s-*\(%s\)\b label) nil t)) (progn (goto-char (point-min)) (re-search-forward (format ^#\+tblname:\s-*\(%s\)\b label) nil t))) nil (org-mark-ring-goto) (error %s not found label)) (org-show-entry) (message go back with (org-mark-ring-goto) `C-c &`))                                                                                                                                                                                                    | (lambda (keyword desc format) (cond ((eq format (quote html)) (format (<ref>%s</ref>) path)) ((eq format (quote latex)) (format \ref{%s} keyword))))                                                                                                                                                                                                                                                        |
2110 | label             | (lambda (label) on clicking count the number of label tags used in the buffer. A number greater than one means multiple labels! (message (format %s occurences (+ (count-matches (format label:%s\b[^-:] label) (point-min) (point-max) t) (count-matches (format ^#\+tblname:\s-*%s\b[^-:] label) (point-min) (point-max) t) (count-matches (format \label{%s}\b label) (point-min) (point-max) t) (count-matches (format ^#\+label:\s-*%s\b[^-:] label) (point-min) (point-max) t)))))                                                                                                                                                                                                                                                                                                                           | (lambda (keyword desc format) (cond ((eq format (quote html)) (format (<label>%s</label>) path)) ((eq format (quote latex)) (format \label{%s} keyword))))                                                                                                                                                                                                                                                  |
2111 | list-of-tables    | org-ref-list-of-tables                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             | (lambda (keyword desc format) (cond ((eq format (quote latex)) (format \listoftables))))                                                                                                                                                                                                                                                                                                                    |
2112 | list-of-figures   | org-ref-list-of-figures                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | (lambda (keyword desc format) (cond ((eq format (quote latex)) (format \listoffigures))))                                                                                                                                                                                                                                                                                                                   |
2113 | addbibresource    | (lambda (link-string) (let* ((bibfile) (object (org-element-context)) (link-string-beginning) (link-string-end)) (save-excursion (goto-char (org-element-property :begin object)) (search-forward link-string nil nil 1) (setq link-string-beginning (match-beginning 0)) (setq link-string-end (match-end 0))) (set (make-local-variable (quote reftex-default-addbibresource)) (split-string (org-element-property :path object) ,)) (save-excursion (if (search-forward , link-string-end 1 1) (setq key-end (- (match-end 0) 1)) (setq key-end (point)))) (save-excursion (if (search-backward , link-string-beginning 1 1) (setq key-beginning (+ (match-beginning 0) 1)) (setq key-beginning (point)))) (setq bibfile (org-ref-strip-string (buffer-substring key-beginning key-end))) (find-file bibfile))) | (lambda (keyword desc format) (cond ((eq format (quote html)) (format )) ((eq format (quote latex)) (format \addbibresource{%s} keyword))))                                                                                                                                                                                                                                                                 |
2114 | bibliographystyle | (lambda (arg) (message Nothing implemented for clicking here.))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | (lambda (keyword desc format) (cond ((eq format (quote latex)) (format \bibliographystyle{%s} keyword))))                                                                                                                                                                                                                                                                                                   |
2115 | printbibliography | (lambda (arg) (message Nothing implemented for clicking here.))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | (lambda (keyword desc format) (cond ((eq format (quote org)) (org-ref-get-org-bibliography)) ((eq format (quote html)) (org-ref-get-html-bibliography)) ((eq format (quote latex)) \printbibliography)))                                                                                                                                                                                                    |
2116 | nobibliography    | (lambda (link-string) (let* ((bibfile) (object (org-element-context)) (link-string-beginning) (link-string-end)) (save-excursion (goto-char (org-element-property :begin object)) (search-forward link-string nil nil 1) (setq link-string-beginning (match-beginning 0)) (setq link-string-end (match-end 0))) (set (make-local-variable (quote reftex-default-bibliography)) (split-string (org-element-property :path object) ,)) (save-excursion (if (search-forward , link-string-end 1 1) (setq key-end (- (match-end 0) 1)) (setq key-end (point)))) (save-excursion (if (search-backward , link-string-beginning 1 1) (setq key-beginning (+ (match-beginning 0) 1)) (setq key-beginning (point)))) (setq bibfile (org-ref-strip-string (buffer-substring key-beginning key-end))) (find-file bibfile)))   | (lambda (keyword desc format) (cond ((eq format (quote org)) (org-ref-get-org-bibliography)) ((eq format (quote ascii)) (org-ref-get-ascii-bibliography)) ((eq format (quote html)) (org-ref-get-html-bibliography)) ((eq format (quote latex)) (format \nobibliography{%s} (replace-regexp-in-string \.bib  (mapconcat (quote identity) (mapcar (quote expand-file-name) (split-string keyword ,)) ,)))))) |
2117 | bibliography      | (lambda (link-string) (let* ((bibfile) (object (org-element-context)) (link-string-beginning) (link-string-end)) (save-excursion (goto-char (org-element-property :begin object)) (search-forward link-string nil nil 1) (setq link-string-beginning (match-beginning 0)) (setq link-string-end (match-end 0))) (set (make-local-variable (quote reftex-default-bibliography)) (split-string (org-element-property :path object) ,)) (save-excursion (if (search-forward , link-string-end 1 1) (setq key-end (- (match-end 0) 1)) (setq key-end (point)))) (save-excursion (if (search-backward , link-string-beginning 1 1) (setq key-beginning (+ (match-beginning 0) 1)) (setq key-beginning (point)))) (setq bibfile (org-ref-strip-string (buffer-substring key-beginning key-end))) (find-file bibfile)))   | (lambda (keyword desc format) (cond ((eq format (quote org)) (org-ref-get-org-bibliography)) ((eq format (quote ascii)) (org-ref-get-ascii-bibliography)) ((eq format (quote html)) (org-ref-get-html-bibliography)) ((eq format (quote latex)) (format \bibliography{%s} (replace-regexp-in-string \.bib  (mapconcat (quote identity) (mapcar (quote expand-file-name) (split-string keyword ,)) ,))))))   |
2118 | rmail             | org-rmail-open                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2119 | mhe               | org-mhe-open                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2120 | irc               | org-irc-visit                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2121 | info              | org-info-open                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2122 | gnus              | org-gnus-open                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2123 | docview           | org-docview-open                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | org-docview-export                                                                                                                                                                                                                                                                                                                                                                                          |
2124 | bibtex            | org-bibtex-open                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2125 | bbdb              | org-bbdb-open                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | org-bbdb-export                                                                                                                                                                                                                                                                                                                                                                                             |
2126 | pydoc             | (lambda (link-string) (shell-command (format python -m pydoc %s link-string)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2127 | index             | (lambda (path) (tq-index) (occur path))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2128 | attachfile        | (lambda (link-string) (org-open-file link-string))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | (lambda (keyword desc format) (cond ((eq format (quote html)) (format )) ((eq format (quote latex)) (format \attachfile{%s} keyword))))                                                                                                                                                                                                                                                                     |
2129 | msx               | org-msx-open                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2130 | id                | org-id-open                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2131 | file+emacs        | org-open-file-with-emacs                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2132 | file+sys          | org-open-file-with-system                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | nil                                                                                                                                                                                                                                                                                                                                                                                                         |
2133
2134
2135
2136 * Utilities
2137 ** create simple text citation from bibtex entry
2138
2139 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2140 (defun org-ref-bib-citation ()
2141   "From a bibtex entry, create and return a simple citation string.
2142 This assumes you are in an article."
2143
2144   (bibtex-beginning-of-entry)
2145   (let* ((cb (current-buffer))
2146          (bibtex-expand-strings t)
2147          (entry (loop for (key . value) in (bibtex-parse-entry t)
2148                       collect (cons (downcase key) value)))
2149          (title (replace-regexp-in-string "\n\\|\t\\|\s+" " " (reftex-get-bib-field "title" entry)))
2150          (year  (reftex-get-bib-field "year" entry))
2151          (author (replace-regexp-in-string "\n\\|\t\\|\s+" " " (reftex-get-bib-field "author" entry)))
2152          (key (reftex-get-bib-field "=key=" entry))
2153          (journal (reftex-get-bib-field "journal" entry))
2154          (volume (reftex-get-bib-field "volume" entry))
2155          (pages (reftex-get-bib-field "pages" entry))
2156          (doi (reftex-get-bib-field "doi" entry))
2157          (url (reftex-get-bib-field "url" entry))
2158          )
2159     ;;authors, "title", Journal, vol(iss):pages (year).
2160     (format "%s, \"%s\", %s, %s:%s (%s)"
2161             author title journal  volume pages year)))
2162 #+END_SRC
2163
2164 #+RESULTS:
2165 : org-ref-bib-citation
2166
2167
2168 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2169 (defun org-ref-bib-html-citation ()
2170   "from a bibtex entry, create and return a simple citation with html links."
2171
2172   (bibtex-beginning-of-entry)
2173   (let* ((cb (current-buffer))
2174          (bibtex-expand-strings t)
2175          (entry (loop for (key . value) in (bibtex-parse-entry t)
2176                       collect (cons (downcase key) value)))
2177          (title (replace-regexp-in-string "\n\\|\t\\|\s+" " " (reftex-get-bib-field "title" entry)))
2178          (year  (reftex-get-bib-field "year" entry))
2179          (author (replace-regexp-in-string "\n\\|\t\\|\s+" " " (reftex-get-bib-field "author" entry)))
2180          (key (reftex-get-bib-field "=key=" entry))
2181          (journal (reftex-get-bib-field "journal" entry))
2182          (volume (reftex-get-bib-field "volume" entry))
2183          (pages (reftex-get-bib-field "pages" entry))
2184          (doi (reftex-get-bib-field "doi" entry))
2185          (url (reftex-get-bib-field "url" entry))
2186          )
2187     ;;authors, "title", Journal, vol(iss):pages (year).
2188     (concat (format "%s, \"%s\", %s, %s:%s (%s)."
2189                     author title journal  volume pages year)
2190             (when url (format " <a href=\"%s\">link</a>" url))
2191             (when doi (format " <a href=\"http://dx.doi.org/%s\">doi</a>" doi)))
2192     ))
2193 #+END_SRC
2194
2195 ** open pdf from bibtex
2196 We bind this to a key here: [[*key%20bindings%20for%20utilities][key bindings for utilities]].
2197 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2198 (defun org-ref-open-bibtex-pdf ()
2199   "open pdf for a bibtex entry, if it exists. assumes point is in
2200 the entry of interest in the bibfile. but does not check that."
2201   (interactive)
2202   (save-excursion
2203     (bibtex-beginning-of-entry)
2204     (let* ((bibtex-expand-strings t)
2205            (entry (bibtex-parse-entry t))
2206            (key (reftex-get-bib-field "=key=" entry))
2207            (pdf (format (concat org-ref-pdf-directory "%s.pdf") key)))
2208       (message "%s" pdf)
2209       (if (file-exists-p pdf)
2210           (org-open-link-from-string (format "[[file:%s]]" pdf))
2211         (ding)))))
2212 #+END_SRC
2213
2214 ** open notes from bibtex
2215 We bind this to a key here [[*key%20bindings%20for%20utilities][key bindings for utilities]].
2216
2217 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2218 (defun org-ref-open-bibtex-notes ()
2219   "from a bibtex entry, open the notes if they exist, and create a heading if they do not.
2220
2221 I never did figure out how to use reftex to make this happen
2222 non-interactively. the reftex-format-citation function did not
2223 work perfectly; there were carriage returns in the strings, and
2224 it did not put the key where it needed to be. so, below I replace
2225 the carriage returns and extra spaces with a single space and
2226 construct the heading by hand."
2227   (interactive)
2228
2229   (bibtex-beginning-of-entry)
2230   (let* ((cb (current-buffer))
2231          (bibtex-expand-strings t)
2232          (entry (loop for (key . value) in (bibtex-parse-entry t)
2233                       collect (cons (downcase key) value)))
2234          (title (replace-regexp-in-string "\n\\|\t\\|\s+" " " (reftex-get-bib-field "title" entry)))
2235          (year  (reftex-get-bib-field "year" entry))
2236          (author (replace-regexp-in-string "\n\\|\t\\|\s+" " " (reftex-get-bib-field "author" entry)))
2237          (key (reftex-get-bib-field "=key=" entry))
2238          (journal (reftex-get-bib-field "journal" entry))
2239          (volume (reftex-get-bib-field "volume" entry))
2240          (pages (reftex-get-bib-field "pages" entry))
2241          (doi (reftex-get-bib-field "doi" entry))
2242          (url (reftex-get-bib-field "url" entry))
2243          )
2244
2245     ;; save key to clipboard to make saving pdf later easier by pasting.
2246     (with-temp-buffer
2247       (insert key)
2248       (kill-ring-save (point-min) (point-max)))
2249
2250     ;; now look for entry in the notes file
2251     (if  org-ref-bibliography-notes
2252         (find-file-other-window org-ref-bibliography-notes)
2253       (error "org-ref-bib-bibliography-notes is not set to anything"))
2254
2255     (goto-char (point-min))
2256     ;; put new entry in notes if we don't find it.
2257     (if (re-search-forward (format ":Custom_ID: %s$" key) nil 'end)
2258         (funcall org-ref-open-notes-function)
2259       ;; no entry found, so add one
2260       (insert (format "\n** TODO %s - %s" year title))
2261       (insert (format"
2262  :PROPERTIES:
2263   :Custom_ID: %s
2264   :AUTHOR: %s
2265   :JOURNAL: %s
2266   :YEAR: %s
2267   :VOLUME: %s
2268   :PAGES: %s
2269   :DOI: %s
2270   :URL: %s
2271  :END:
2272 [[cite:%s]] [[file:%s/%s.pdf][pdf]]\n\n"
2273 key author journal year volume pages doi url key org-ref-pdf-directory key))
2274 (save-buffer))))
2275 #+END_SRC
2276
2277 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2278 (defun org-ref-open-notes-from-reftex ()
2279   "Call reftex, and open notes for selected entry."
2280   (interactive)
2281   (let ((bibtex-key )))
2282
2283     ;; now look for entry in the notes file
2284     (if  org-ref-bibliography-notes
2285         (find-file-other-window org-ref-bibliography-notes)
2286       (error "org-ref-bib-bibliography-notes is not set to anything"))
2287
2288     (goto-char (point-min))
2289
2290     (re-search-forward (format
2291                         ":Custom_ID: %s$"
2292                         (first (reftex-citation t)) nil 'end))
2293     (funcall org-ref-open-notes-function))
2294 #+END_SRC
2295
2296 ** open url in browser from bibtex
2297
2298 We bind this to a key here [[*key%20bindings%20for%20utilities][key bindings for utilities]].
2299
2300 + This function may be duplicative of bibtex-url. But I think my function is better unless you do some complicated customization of bibtex-generate-url-list.
2301
2302 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2303 (defun org-ref-open-in-browser ()
2304   "Open the bibtex entry at point in a browser using the url field or doi field"
2305 (interactive)
2306 (save-excursion
2307   (bibtex-beginning-of-entry)
2308   (catch 'done
2309     (let ((url (bibtex-autokey-get-field "url")))
2310       (when  url
2311         (browse-url url)
2312         (throw 'done nil)))
2313
2314     (let ((doi (bibtex-autokey-get-field "doi")))
2315       (when doi
2316         (if (string-match "^http" doi)
2317             (browse-url doi)
2318           (browse-url (format "http://dx.doi.org/%s" doi)))
2319         (throw 'done nil)))
2320     (message "No url or doi found"))))
2321 #+END_SRC
2322
2323 ** citeulike
2324    I discovered you could upload a bibtex entry to citeulike using http requests. The upload is actually done by a [[*The%20upload%20script][python script]], because it was easy to write. Here is the emacs command to do this. It is not a fast operation, and  do not use it frequently.
2325
2326 *** function to upload bibtex to citeulike
2327
2328 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2329 (defun org-ref-upload-bibtex-entry-to-citeulike ()
2330   "with point in  a bibtex entry get bibtex string and submit to citeulike.
2331
2332 Relies on the python script /upload_bibtex_citeulike.py being in the user directory."
2333   (interactive)
2334   (message "uploading to citeulike")
2335   (save-restriction
2336     (bibtex-narrow-to-entry)
2337     (let ((startpos (point-min))
2338           (endpos (point-max))
2339           (bibtex-string (buffer-string))
2340           (script (concat "python " starter-kit-dir "/upload_bibtex_citeulike.py&")))
2341       (with-temp-buffer (insert bibtex-string)
2342                         (shell-command-on-region (point-min) (point-max) script t nil nil t)))))
2343 #+END_SRC
2344
2345 *** The upload script
2346 Here is the python script for uploading.
2347
2348 *************** TODO document how to get the cookies
2349 *************** END
2350
2351
2352 #+BEGIN_SRC python :tangle upload_bibtex_citeulike.py
2353 #!python
2354 import pickle, requests, sys
2355
2356 # reload cookies
2357 with open('c:/Users/jkitchin/Dropbox/blogofile-jkitchin.github.com/_blog/cookies.pckl', 'rb') as f:
2358     cookies = pickle.load(f)
2359
2360 url = 'http://www.citeulike.org/profile/jkitchin/import_do'
2361
2362 bibtex = sys.stdin.read()
2363
2364 data = {'pasted':bibtex,
2365         'to_read':2,
2366         'tag_parsing':'simple',
2367         'strip_brackets':'no',
2368         'update_id':'bib-key',
2369         'btn_bibtex':'Import BibTeX file ...'}
2370
2371 headers = {'content-type': 'multipart/form-data',
2372            'User-Agent':'jkitchin/johnrkitchin@gmail.com bibtexupload'}
2373
2374 r = requests.post(url, headers=headers, data=data, cookies=cookies, files={})
2375 print r
2376 #+END_SRC
2377
2378 ** Build a pdf from a bibtex file
2379    It is useful to have a pdf version of an entire bibliography to check it for formatting, spelling, or to share it. This function creates a pdf from a bibtex file. I only include the packages  I commonly use in my bitex files.
2380
2381 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2382 (defun org-ref-build-full-bibliography ()
2383   "build pdf of all bibtex entries, and open it."
2384   (interactive)
2385   (let* ((bibfile (file-name-nondirectory (buffer-file-name)))
2386         (bib-base (file-name-sans-extension bibfile))
2387         (texfile (concat bib-base ".tex"))
2388         (pdffile (concat bib-base ".pdf")))
2389     (find-file texfile)
2390     (erase-buffer)
2391     (insert (format "\\documentclass[12pt]{article}
2392 \\usepackage[version=3]{mhchem}
2393 \\usepackage{url}
2394 \\usepackage[numbers]{natbib}
2395 \\usepackage[colorlinks=true, linkcolor=blue, urlcolor=blue, pdfstartview=FitH]{hyperref}
2396 \\usepackage{doi}
2397 \\begin{document}
2398 \\nocite{*}
2399 \\bibliographystyle{unsrtnat}
2400 \\bibliography{%s}
2401 \\end{document}" bib-base))
2402     (save-buffer)
2403     (shell-command (concat "pdflatex " bib-base))
2404     (shell-command (concat "bibtex " bib-base))
2405     (shell-command (concat "pdflatex " bib-base))
2406     (shell-command (concat "pdflatex " bib-base))
2407     (kill-buffer texfile)
2408     (org-open-file pdffile)
2409     ))
2410 #+END_SRC
2411
2412 ** Extract bibtex entries cited in an org-file
2413 When you use your default bibliography file, and you want to send an org-file to a collaborator, you may need to include bibtex entries so the other person can see them. This function does that and puts the entries in a section at the end of the document that can be tangled to a bib-file.
2414
2415 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
2416 (defun org-ref-extract-bibtex-entries ()
2417   "extract the bibtex entries referred to by cite links in the current buffer into a src block at the bottom of the current buffer.
2418
2419 If no bibliography is in the buffer the `reftex-default-bibliography' is used."
2420   (interactive)
2421   (let* ((temporary-file-directory (file-name-directory (buffer-file-name)))
2422          (tempname (make-temp-file "extract-bib"))
2423          (contents (buffer-string))
2424          (cb (current-buffer))
2425          basename texfile bibfile results)
2426
2427     ;; open tempfile and insert org-buffer contents
2428     (find-file tempname)
2429     (insert contents)
2430     (setq basename (file-name-sans-extension
2431                     (file-name-nondirectory buffer-file-name))
2432           texfile (concat tempname ".tex")
2433           bibfile (concat tempname ".bib"))
2434
2435     ;; see if we have a bibliography, and insert the default one if not.
2436     (save-excursion
2437       (goto-char (point-min))
2438       (unless (re-search-forward "^bibliography:" (point-max) 'end)
2439         (insert (format "\nbibliography:%s"
2440                         (mapconcat 'identity reftex-default-bibliography ",")))))
2441     (save-buffer)
2442
2443     ;; get a latex file and extract the references
2444     (org-latex-export-to-latex)
2445     (find-file texfile)
2446     (reftex-parse-all)
2447     (reftex-create-bibtex-file bibfile)
2448     (save-buffer)
2449     ;; save results of the references
2450     (setq results (buffer-string))
2451
2452     ;; kill buffers. these are named by basename, not full path
2453     (kill-buffer (concat basename ".bib"))
2454     (kill-buffer (concat basename ".tex"))
2455     (kill-buffer basename)
2456
2457     (delete-file bibfile)
2458     (delete-file texfile)
2459     (delete-file tempname)
2460
2461     ;; Now back to the original org buffer and insert the results
2462     (switch-to-buffer cb)
2463     (when (not (string= "" results))
2464       (save-excursion
2465         (goto-char (point-max))
2466         (insert "\n\n")
2467         (org-insert-heading)
2468         (insert (format " Bibtex entries
2469
2470 ,#+BEGIN_SRC text :tangle %s
2471 %s
2472 ,#+END_SRC" (concat (file-name-sans-extension (file-name-nondirectory (buffer-file-name))) ".bib") results))))))
2473 #+END_SRC
2474
2475 ** Find bad cite links
2476 Depending on how you enter citations, you may have citations with no corresponding bibtex entry. This function finds them and gives you a clickable table to navigate to them.
2477
2478 #+BEGIN_SRC emacs-lisp  :tangle org-ref.el
2479 (require 'cl)
2480
2481 (defun index (substring list)
2482   "return the index of string in a list of strings"
2483   (let ((i 0)
2484         (found nil))
2485     (dolist (arg list i)
2486       (if (string-match (concat "^" substring "$") arg)
2487           (progn
2488             (setq found t)
2489             (return i)))
2490       (setq i (+ i 1)))
2491     ;; return counter if found, otherwise return nil
2492     (if found i nil)))
2493
2494
2495 (defun org-ref-find-bad-citations ()
2496   "Create a list of citation keys in an org-file that do not have a bibtex entry in the known bibtex files.
2497
2498 Makes a new buffer with clickable links."
2499   (interactive)
2500   ;; generate the list of bibtex-keys and cited keys
2501   (let* ((bibtex-files (org-ref-find-bibliography))
2502          (bibtex-file-path (mapconcat (lambda (x) (file-name-directory (file-truename x))) bibtex-files ":"))
2503          (bibtex-keys (mapcar (lambda (x) (car x)) (bibtex-global-key-alist)))
2504          (bad-citations '()))
2505
2506     (org-element-map (org-element-parse-buffer) 'link
2507       (lambda (link)
2508         (let ((plist (nth 1 link)))
2509           (when (equal (plist-get plist ':type) "cite")
2510             (dolist (key (org-ref-split-and-strip-string (plist-get plist ':path)) )
2511               (when (not (index key bibtex-keys))
2512                 (setq bad-citations (append bad-citations
2513                                             `(,(format "%s [[elisp:(progn (switch-to-buffer-other-frame \"%s\")(goto-char %s))][not found here]]\n"
2514                                                        key (buffer-name)(plist-get plist ':begin)))))
2515                 ))))))
2516
2517     (if bad-citations
2518       (progn
2519         (switch-to-buffer-other-window "*Missing citations*")
2520         (org-mode)
2521         (erase-buffer)
2522         (insert "* List of bad cite links\n")
2523         (insert (mapconcat 'identity bad-citations ""))
2524                                         ;(setq buffer-read-only t)
2525         (use-local-map (copy-keymap org-mode-map))
2526         (local-set-key "q" #'(lambda () (interactive) (kill-buffer))))
2527
2528       (when (get-buffer "*Missing citations*")
2529           (kill-buffer "*Missing citations*"))
2530       (message "No bad cite links found"))))
2531 #+END_SRC
2532
2533 ** Finding non-ascii characters
2534 I like my bibtex files to be 100% ascii. This function finds the non-ascii characters so you can replace them.
2535
2536 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2537 (defun org-ref-find-non-ascii-characters ()
2538   "finds non-ascii characters in the buffer. Useful for cleaning up bibtex files"
2539   (interactive)
2540   (occur "[^[:ascii:]]"))
2541 #+END_SRC
2542
2543 ** Resort a bibtex entry
2544 I like neat and orderly bibtex entries.That means the fields are in a standard order that I like. This function reorders the fields in an entry for articles, and makes sure the fields are in lowercase.
2545
2546 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2547 (defun org-ref-sort-bibtex-entry ()
2548   "sort fields of entry in standard order and downcase them"
2549   (interactive)
2550   (bibtex-beginning-of-entry)
2551   (let* ((master '("author" "title" "journal" "volume" "number" "pages" "year" "doi" "url"))
2552          (entry (bibtex-parse-entry))
2553          (entry-fields)
2554          (other-fields)
2555          (type (cdr (assoc "=type=" entry)))
2556          (key (cdr (assoc "=key=" entry))))
2557
2558     ;; these are the fields we want to order that are in this entry
2559     (setq entry-fields (mapcar (lambda (x) (car x)) entry))
2560     ;; we do not want to reenter these fields
2561     (setq entry-fields (remove "=key=" entry-fields))
2562     (setq entry-fields (remove "=type=" entry-fields))
2563
2564     ;;these are the other fields in the entry
2565     (setq other-fields (remove-if-not (lambda(x) (not (member x master))) entry-fields))
2566
2567     (cond
2568      ;; right now we only resort articles
2569      ((string= (downcase type) "article")
2570       (bibtex-kill-entry)
2571       (insert
2572        (concat "@article{" key ",\n"
2573                (mapconcat
2574                 (lambda (field)
2575                   (when (member field entry-fields)
2576                     (format "%s = %s," (downcase field) (cdr (assoc field entry))))) master "\n")
2577                (mapconcat
2578                 (lambda (field)
2579                   (format "%s = %s," (downcase field) (cdr (assoc field entry)))) other-fields "\n")
2580                "\n}\n\n"))
2581       (bibtex-find-entry key)
2582       (bibtex-fill-entry)
2583       (bibtex-clean-entry)
2584        ))))
2585 #+END_SRC
2586
2587 ** Clean a bibtex entry
2588    I like neat and orderly bibtex entries. This code will eventually replace the key with my style key, clean the entry, and sort the fields in the order I like them.
2589 see [[file:emacs-24.3/lisp/textmodes/bibtex.el::bibtex-autokey-before-presentation-function]] for how to set a function that checks for uniqueness of the key.
2590 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2591 (defun org-ref-clean-bibtex-entry(&optional keep-key)
2592   "clean and replace the key in a bibtex function. When keep-key is t, do not replace it. You can use a prefix to specify the key should be kept"
2593   (interactive "P")
2594   (bibtex-beginning-of-entry)
2595 (end-of-line)
2596   ;; some entries do not have a key or comma in first line. We check and add it, if needed.
2597   (unless (string-match ",$" (thing-at-point 'line))
2598     (end-of-line)
2599     (insert ","))
2600
2601   ;; check for empty pages, and put eid or article id in its place
2602   (let ((entry (bibtex-parse-entry))
2603         (pages (bibtex-autokey-get-field "pages"))
2604         (year (bibtex-autokey-get-field "year"))
2605         (doi  (bibtex-autokey-get-field "doi"))
2606         ;; The Journal of Chemical Physics uses eid
2607         (eid (bibtex-autokey-get-field "eid")))
2608
2609     ;; replace http://dx.doi.org/ in doi. some journals put that in,
2610     ;; but we only want the doi.
2611     (when (string-match "^http://dx.doi.org/" doi)
2612       (bibtex-beginning-of-entry)
2613       (goto-char (car (cdr (bibtex-search-forward-field "doi" t))))
2614       (bibtex-kill-field)
2615       (bibtex-make-field "doi")
2616       (backward-char)
2617       (insert (replace-regexp-in-string "^http://dx.doi.org/" "" doi)))
2618
2619     ;; asap articles often set year to 0, which messes up key
2620     ;; generation. fix that.
2621     (when (string= "0" year)
2622       (bibtex-beginning-of-entry)
2623       (goto-char (car (cdr (bibtex-search-forward-field "year" t))))
2624       (bibtex-kill-field)
2625       (bibtex-make-field "year")
2626       (backward-char)
2627       (insert (read-string "Enter year: ")))
2628
2629     ;; fix pages if they are empty if there is an eid to put there.
2630     (when (string= "-" pages)
2631       (when eid
2632         (bibtex-beginning-of-entry)
2633         ;; this seems like a clunky way to set the pages field.But I
2634         ;; cannot find a better way.
2635         (goto-char (car (cdr (bibtex-search-forward-field "pages" t))))
2636         (bibtex-kill-field)
2637         (bibtex-make-field "pages")
2638         (backward-char)
2639         (insert eid)))
2640
2641     ;; replace naked & with \&
2642     (save-restriction
2643       (bibtex-narrow-to-entry)
2644       (bibtex-beginning-of-entry)
2645       (message "checking &")
2646       (replace-regexp " & " " \\\\& ")
2647       (widen))
2648
2649     ;; generate a key, and if it duplicates an existing key, edit it.
2650     (unless keep-key
2651       (let ((key (bibtex-generate-autokey)))
2652
2653         ;; first we delete the existing key
2654         (bibtex-beginning-of-entry)
2655         (re-search-forward bibtex-entry-maybe-empty-head)
2656         (if (match-beginning bibtex-key-in-head)
2657             (delete-region (match-beginning bibtex-key-in-head)
2658                            (match-end bibtex-key-in-head)))
2659         ;; check if the key is in the buffer
2660         (when (save-excursion
2661                 (bibtex-search-entry key))
2662           (save-excursion
2663             (bibtex-search-entry key)
2664             (bibtex-copy-entry-as-kill)
2665             (switch-to-buffer-other-window "*duplicate entry*")
2666             (bibtex-yank))
2667           (setq key (bibtex-read-key "Duplicate Key found, edit: " key)))
2668
2669         (insert key)
2670         (kill-new key))) ;; save key for pasting
2671
2672     ;; run hooks. each of these operates on the entry with no arguments.
2673     ;; this did not work like  i thought, it gives a symbolp error.
2674     ;; (run-hooks org-ref-clean-bibtex-entry-hook)
2675     (mapcar (lambda (x)
2676               (save-restriction
2677                 (save-excursion
2678                   (funcall x))))
2679             org-ref-clean-bibtex-entry-hook)
2680
2681     ;; sort fields within entry
2682     (org-ref-sort-bibtex-entry)
2683     ;; check for non-ascii characters
2684     (occur "[^[:ascii:]]")
2685     ))
2686 #+END_SRC
2687
2688 #+RESULTS:
2689 : org-ref-clean-bibtex-entry
2690
2691 ** Sort the entries in a citation link by year
2692 I prefer citations in chronological order within a grouping. These functions sort the link under the cursor by year.
2693
2694 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2695 (defun org-ref-get-citation-year (key)
2696   "get the year of an entry with key. Returns year as a string."
2697   (interactive)
2698   (let* ((results (org-ref-get-bibtex-key-and-file key))
2699          (bibfile (cdr results)))
2700     (with-temp-buffer
2701       (insert-file-contents bibfile)
2702       (bibtex-search-entry key nil 0)
2703       (prog1 (reftex-get-bib-field "year" (bibtex-parse-entry t))
2704         ))))
2705
2706 (defun org-ref-sort-citation-link ()
2707  "replace link at point with sorted link by year"
2708  (interactive)
2709  (let* ((object (org-element-context))
2710         (type (org-element-property :type object))
2711         (begin (org-element-property :begin object))
2712         (end (org-element-property :end object))
2713         (link-string (org-element-property :path object))
2714         keys years data)
2715   (setq keys (org-ref-split-and-strip-string link-string))
2716   (setq years (mapcar 'org-ref-get-citation-year keys))
2717   (setq data (mapcar* (lambda (a b) `(,a . ,b)) years keys))
2718   (setq data (cl-sort data (lambda (x y) (< (string-to-int (car x)) (string-to-int (car y))))))
2719   ;; now get the keys separated by commas
2720   (setq keys (mapconcat (lambda (x) (cdr x)) data ","))
2721   ;; and replace the link with the sorted keys
2722   (cl--set-buffer-substring begin end (concat type ":" keys))))
2723 #+END_SRC
2724
2725 ** Sort entries in citation links with shift-arrow keys
2726 Sometimes it may be helpful to manually change the order of citations. These functions define shift-arrow functions.
2727 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2728 (defun org-ref-swap-keys (i j keys)
2729  "swap the keys in a list with index i and j"
2730  (let ((tempi (nth i keys)))
2731    (setf (nth i keys) (nth j keys))
2732    (setf (nth j keys) tempi))
2733   keys)
2734
2735 (defun org-ref-swap-citation-link (direction)
2736  "move citation at point in direction +1 is to the right, -1 to the left"
2737  (interactive)
2738  (let* ((object (org-element-context))
2739         (type (org-element-property :type object))
2740         (begin (org-element-property :begin object))
2741         (end (org-element-property :end object))
2742         (link-string (org-element-property :path object))
2743         key keys i)
2744    ;;   We only want this to work on citation links
2745    (when (-contains? org-ref-cite-types type)
2746         (setq key (org-ref-get-bibtex-key-under-cursor))
2747         (setq keys (org-ref-split-and-strip-string link-string))
2748         (setq i (index key keys))  ;; defined in org-ref
2749         (if (> direction 0) ;; shift right
2750             (org-ref-swap-keys i (+ i 1) keys)
2751           (org-ref-swap-keys i (- i 1) keys))
2752         (setq keys (mapconcat 'identity keys ","))
2753         ;; and replace the link with the sorted keys
2754         (cl--set-buffer-substring begin end (concat type ":" keys " "))
2755         ;; now go forward to key so we can move with the key
2756         (re-search-forward key)
2757         (goto-char (match-beginning 0)))))
2758
2759 ;; add hooks to make it work
2760 (add-hook 'org-shiftright-hook (lambda () (org-ref-swap-citation-link 1)))
2761 (add-hook 'org-shiftleft-hook (lambda () (org-ref-swap-citation-link -1)))
2762 #+END_SRC
2763 * Aliases
2764 I like convenience. Here are some aliases for faster typing.
2765
2766 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2767 (defalias 'oro 'org-ref-open-citation-at-point)
2768 (defalias 'orc 'org-ref-citation-at-point)
2769 (defalias 'orp 'org-ref-open-pdf-at-point)
2770 (defalias 'oru 'org-ref-open-url-at-point)
2771 (defalias 'orn 'org-ref-open-notes-at-point)
2772 (defalias 'ornr 'org-ref-open-notes-from-reftex)
2773
2774 (defalias 'orib 'org-ref-insert-bibliography-link)
2775 (defalias 'oric 'org-ref-insert-cite-link)
2776 (defalias 'orir 'org-ref-insert-ref-link)
2777 (defalias 'orsl 'org-ref-store-bibtex-entry-link)
2778
2779 (defalias 'orcb 'org-ref-clean-bibtex-entry)
2780 #+END_SRC
2781 * Helm interface
2782 [[https://github.com/tmalsburg/helm-bibtex][helm-bibtex]] is a very cool interface to bibtex files. Out of the box though, it is not super convenient for org-ref. Here, we modify it to make it fit our workflow and extend it where needed.
2783
2784 1. Make the default action to insert selected keys.
2785 2. Make open entry second action
2786 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2787 (setq helm-source-bibtex
2788       '((name                                      . "BibTeX entries")
2789         (init                                      . helm-bibtex-init)
2790         (candidates                                . helm-bibtex-candidates)
2791         (filtered-candidate-transformer            . helm-bibtex-candidates-formatter)
2792         (action . (("Insert citation"              . helm-bibtex-insert-citation)
2793                    ("Show entry"                   . helm-bibtex-show-entry)
2794                    ("Open PDF file (if present)"   . helm-bibtex-open-pdf)
2795                    ("Open URL or DOI in browser"   . helm-bibtex-open-url-or-doi)
2796                    ("Insert reference"             . helm-bibtex-insert-reference)
2797                    ("Insert BibTeX key"            . helm-bibtex-insert-key)
2798                    ("Insert BibTeX entry"          . helm-bibtex-insert-bibtex)
2799                    ("Attach PDF to email"          . helm-bibtex-add-PDF-attachment)
2800                    ("Edit notes"                   . helm-bibtex-edit-notes)
2801                    ))))
2802 #+END_SRC
2803
2804 Now, let us define a function that inserts the cite links:
2805 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2806 (defun helm-bibtex-format-org-ref (keys)
2807   "insert selected KEYS as cite link. Append KEYS if you are on a link."
2808   (let* ((object (org-element-context)))
2809     (cond
2810      ;; case where we are in a link
2811      ((and (equal (org-element-type object) 'link)
2812            (-contains? org-ref-cite-types (org-element-property :type object)))
2813       (goto-char link-string-end)
2814       ;; sometimes there are spaces at the end of the link
2815       ;; this code moves point pack until no spaces are there
2816       (while (looking-back " ") (backward-char))
2817       (insert (concat "," (mapconcat 'identity keys ","))))
2818
2819      ;; We are next to a link, and we want to append
2820      ((save-excursion
2821         (backward-char)
2822         (and (equal (org-element-type (org-element-context)) 'link)
2823              (-contains? org-ref-cite-types (org-element-property :type (org-element-context)))))
2824       (while (looking-back " ") (backward-char))
2825       (insert (concat "," (mapconcat 'identity keys ","))))
2826
2827      ;; insert fresh link
2828      (t
2829       (insert
2830        (concat org-ref-default-citation-link
2831                ":"
2832                (s-join keys ",")))))))
2833
2834 (setq helm-bibtex-format-citation-functions
2835       '((org-mode . helm-bibtex-format-org-ref)))
2836
2837 (require 'helm-bibtex)
2838 #+END_SRC
2839
2840 ** A helm click menu
2841
2842 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2843 (defun org-ref-get-citation-string-at-point ()
2844   (interactive)
2845   (let* ((results (org-ref-get-bibtex-key-and-file))
2846          (key (car results))
2847          (bibfile (cdr results)))
2848     (save-excursion
2849       (with-temp-buffer
2850         (insert-file-contents bibfile)
2851         (bibtex-search-entry key)
2852         (org-ref-bib-citation)))))
2853
2854 (defun org-ref-cite-candidates ()
2855   "Generate the list of possible candidates.
2856 Check for pdf and doi, and add appropriate functions."
2857   (interactive)
2858   (let* ((results (org-ref-get-bibtex-key-and-file))
2859          (key (car results))
2860          (pdf-file (format (concat org-ref-pdf-directory "%s.pdf") key))
2861          (bibfile (cdr results))
2862          (url (save-excursion
2863                 (with-temp-buffer
2864                   (insert-file-contents bibfile)
2865                   (bibtex-search-entry key)
2866                   (bibtex-autokey-get-field "url"))))
2867          (doi (save-excursion
2868                 (with-temp-buffer
2869                   (insert-file-contents bibfile)
2870                   (bibtex-search-entry key)
2871                   ;; I like this better than bibtex-url which does not always find
2872                   ;; the urls
2873                   (bibtex-autokey-get-field "doi"))))
2874          (candidates `(                 ;;the first candidate is a brief summary
2875                        ("Quit" . org-ref-citation-at-point)
2876                        ("Open bibtex entry" . org-ref-open-citation-at-point))))
2877
2878
2879     (when (file-exists-p pdf-file)
2880       (add-to-list
2881        'candidates
2882        '("Open pdf" . org-ref-open-pdf-at-point)
2883        t
2884        ))
2885
2886     (add-to-list
2887      'candidates
2888      '("Open notes" . org-ref-open-notes-at-point)
2889      t)
2890
2891     (when (or url doi)
2892       (add-to-list
2893        'candidates
2894        '("Open in browser" . org-ref-open-url-at-point)
2895        t))
2896
2897     (when doi
2898       (mapc (lambda (x)
2899               (add-to-list 'candidates x t))
2900             `(("WOS" . org-ref-wos-at-point)
2901               ("Related articles in WOS" . org-ref-wos-related-at-point)
2902               ("Citing articles in WOS" . org-ref-wos-citing-at-point)
2903               ("Google Scholar" . org-ref-google-scholar-at-point)
2904               ("Pubmed" . org-ref-pubmed-at-point)
2905               ("Crossref" . org-ref-crossref-at-point)
2906               )))
2907
2908     (add-to-list
2909      'candidates
2910      '("Copy formatted citation to clipboard" . org-ref-copy-entry-as-summary)
2911      t)
2912
2913     (add-to-list
2914      'candidates
2915      '("Copy key to clipboard" . (lambda ()
2916                                   (kill-new
2917                                    (car (org-ref-get-bibtex-key-and-file)))))
2918      t)
2919
2920     (add-to-list
2921      'candidates
2922      '("Copy bibtex entry to file" . org-ref-copy-entry-at-point-to-file)
2923      t)
2924
2925     (add-to-list
2926      'candidates
2927      '("Email bibtex entry and pdf" . (lambda ()
2928                   (save-excursion
2929                     (org-ref-open-citation-at-point)
2930                     (email-bibtex-entry))))
2931      t)
2932   ;; finally return a numbered list of the candidates
2933   (loop for i from 0
2934         for cell in candidates
2935         collect (cons (format "%2s. %s" i (car cell))
2936                       (cdr cell)))))
2937
2938
2939 (defvar org-ref-helm-user-candidates '()
2940   "List of user-defined candidates to act when clicking on a cite link.
2941 This is a list of cons cells '((\"description\" . action)). The action function should not take an argument, and should assume point is on the cite key of interest.
2942 ")
2943
2944 ;; example of adding your own function
2945 (add-to-list
2946  'org-ref-helm-user-candidates
2947  '("Example" . (lambda () (message-box "You did it!")))
2948  t)
2949
2950 (defun org-ref-cite-click-helm (key)
2951   "subtle points.
2952 1. get name and candidates before entering helm because we need the org-buffer.
2953 2. switch back to the org buffer before evaluating the action. most of them need the point and buffer."
2954   (interactive)
2955   (let ((name (org-ref-get-citation-string-at-point))
2956         (candidates (org-ref-cite-candidates))
2957         (cb (current-buffer)))
2958
2959     (helm :sources `(((name . ,name)
2960                       (candidates . ,candidates)
2961                       (action . (lambda (f)
2962                                   (switch-to-buffer cb)
2963                                   (funcall f))))
2964                      ((name . "User functions")
2965                       (candidates . ,org-ref-helm-user-candidates)
2966                       (action . (lambda (f)
2967                                   (switch-to-buffer cb)
2968                                   (funcall f))))
2969                      ))))
2970 #+END_SRC
2971
2972 #+RESULTS:
2973 : org-ref-cite-click-helm
2974
2975 To get a lighter weight message about the cite link, we define a function that gives us the minibuffer message, without the menu. We add it to a hook that updates after every command, including cursor movements.
2976
2977 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2978 (defun org-ref-cite-link-p () (interactive)
2979        (let* ((object (org-element-context))
2980               (type (org-element-property :type object)))
2981          ;;   We only want this to work on citation links
2982          (when (-contains? org-ref-cite-types type)
2983            (message (org-ref-get-citation-string-at-point)))))
2984
2985 (when org-ref-show-citation-on-enter
2986  (add-hook 'post-command-hook 'org-ref-cite-link-p))
2987 #+END_SRC
2988
2989 * End of code
2990 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2991 (provide 'org-ref)
2992 #+END_SRC
2993
2994 * Build                                                            :noexport:
2995 This code will tangle the elisp code out to org-ref.el and load it.
2996
2997 [[elisp:(progn (org-babel-tangle) (load-file "org-ref.el"))]]
2998
2999 Alternatively you may use:
3000
3001 [[elisp:(org-babel-load-file "org-ref.org")]]