1 #+TITLE: Org-ref - The best reference handling for org-mode
7 This document is an experiment at creating a literate program to provide similar features as reftex for org-mode referencing. These features include:
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
16 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
17 ;;; org-ref.el --- setup bibliography, cite, ref and label org-mode links.
19 ;; Copyright(C) 2014 John Kitchin
21 ;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
22 ;; This file is not currently part of GNU Emacs.
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.
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.
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.
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.
45 ;; Package-Requires: ((dash) (helm) (helm-bibtex))
49 The only external require is reftex-cite
51 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
52 (require 'reftex-cite)
55 (require 'helm-bibtex)
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.
61 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
63 "customization group for org-ref")
65 (defcustom org-ref-bibliography-notes
67 "filename to where you will put all your notes about an entry in
68 the default bibliography."
72 (defcustom org-ref-default-bibliography
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)
78 (defcustom org-ref-pdf-directory
80 "directory where pdfs are stored by key. put a trailing / in"
84 (defcustom org-ref-default-citation-link
86 "The default type of citation link to use"
90 (defcustom org-ref-insert-cite-key
92 "Keyboard shortcut to insert a citation."
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>.")
99 ("book" . "%a, %t, %u (%y).")
101 ("proceedings" . "%e, %t in %S, %u (%y).")
103 ("inproceedings" . "%a, %t, %p, in %b, edited by %e, %u (%y)"))
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."
109 (defcustom org-ref-open-notes-function
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"
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'."
130 (defcustom org-ref-insert-cite-function
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."
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."
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"
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.
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*"
159 "citeauthor" "citeauthor*"
160 "citeyear" "citeyear*"
161 "Citet" "Citep" "Citealt" "Citealp" "Citeauthor"
163 ;; http://ctan.mirrorcatalogs.com/macros/latex/contrib/biblatex/doc/biblatex.pdf
165 "parencite" "Parencite"
166 "footcite" "footcitetext"
167 "textcite" "Textcite"
168 "smartcite" "Smartcite"
169 "cite*" "parencite*" "supercite"
170 "autocite" "Autocite" "autocite*" "Autocite*"
172 "citetitle" "citetitle*"
173 "citedate" "citedate*"
175 "fullcite" "footfullcite"
176 ;; "volcite" "Volcite" cannot support the syntax
177 "notecite" "Notecite"
178 "pnotecite" "Pnotecite"
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
188 "List of citation types known in org-ref"
189 :type '(repeat :tag "List of citation types" string)
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."
202 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
203 (defvar org-ref-bibliography-files
205 "variable to hold bibliography files to be searched")
208 ** org-mode / reftex setup
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.
212 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
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.
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))
227 (add-hook 'org-mode-hook 'org-mode-reftex-setup)
229 (eval-after-load 'reftex-vars
231 (add-to-list 'reftex-cite-format-builtin
232 '(org "Org-mode citation"
233 ((?\C-m . "cite:%l") ; default
234 (?d . ",%l") ; for appending
240 (?h . "citeauthor:%l")
241 (?H . "citeauthor*:%l")
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.
250 #+BEGIN_SRC emacs-lisp :tangle no
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"))))
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)
263 Most of this library is the creation of functional links to help with references and citations.
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.
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)))
276 It is helpful to make the previous function operate on a list of strings here.
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 ",")))
284 ** bibliography and bibliographystyle
285 *** An html bibliography
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.
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"
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)))
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.
304 In the format, the following percent escapes will be expanded.
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)
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
318 %B booktitle, abbreviated %T title, abbreviated
323 Usually, only %l is needed. The other stuff is mainly for the echo area
324 display, and for (setq reftex-comment-citations t).
326 %< as a special operator kills punctuation and space around it after the
327 string has been formatted.
329 A pair of square brackets indicates an optional argument, and RefTeX
330 will prompt for the values of these arguments.
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
337 (unless (stringp format) (setq format "\\cite{%l}"))
339 (if (and reftex-comment-citations
340 (string-match "%l" reftex-cite-comment-format))
341 (error "reftex-cite-comment-format contains invalid %%l"))
344 "\\(\\`\\|[^%]\\)\\(\\(%\\([0-9]*\\)\\([a-zA-Z]\\)\\)[.,;: ]*\\)"
346 (let ((n (string-to-number (match-string 4 format)))
347 (l (string-to-char (match-string 5 format)))
353 (org-ref-reftex-get-bib-field "&key" entry)
354 (if reftex-comment-citations
355 reftex-cite-comment-format
357 ((= l ?a) (reftex-format-names
358 (reftex-get-bib-names "author" entry)
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)
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)
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)))))
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))
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'"
409 (let ((org-ref-bibliography-files (org-ref-find-bibliography))
410 (file) (entry) (bibtex-entry) (entry-type) (format))
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))
416 (message "%s not found in %s" key (file-truename file))))))
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)))
425 (setq entry (org-ref-reftex-format-citation bibtex-entry format))
427 (bibtex-narrow-to-entry)
428 (setq entry (buffer-string)))))
433 : org-ref-reftex-format-citation
435 Here is how to use the function. You call it with point in an entry in a bibtex file.
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"))
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>.
444 I am not sure why full author names are not used.
446 This code provides some functions to generate a simple sorted bibliography in html. First we get all the keys in the buffer.
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."
452 (org-element-map (org-element-parse-buffer) 'link
454 (let ((plist (nth 1 link)))
455 (when (-contains? org-ref-cite-types (plist-get plist ':type))
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))
466 This function gets the html for one entry.
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"
472 (format "<li><a id=\"%s\">[%s] %s</a></li>" key key (org-ref-get-bibtex-entry-citation key)))
475 Now, we map over the whole list of keys, and the whole bibliography, formatted as an unordered list.
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)))
482 (concat "<h1>Bibliography</h1>
484 (mapconcat (lambda (x) (org-ref-get-bibtex-entry-html x)) keys "\n")
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.
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.
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))
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))
504 (message "%s not found in %s" key (file-truename file))))))
507 (insert-file-contents file)
508 (bibtex-search-entry key nil 0)
509 (setq entry (bibtex-parse-entry))
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))
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)))
530 (concat "* Bibliography
532 (mapconcat (lambda (x) (org-ref-get-bibtex-entry-org x)) keys "\n")
536 *** An ascii bibliography
538 This function gets the html for one entry.
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"
544 (format "[%s] %s" key (org-ref-get-bibtex-entry-citation key)))
547 Now, we map over the whole list of keys, and the whole bibliography, formatted as an unordered list.
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)))
558 (mapconcat (lambda (x) (org-ref-get-bibtex-entry-ascii x)) keys "\n")
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.
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
576 ;; object is the link you clicked on
577 (object (org-element-context))
579 (link-string-beginning)
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)))
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) ","))
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
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
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
612 (lambda (keyword desc format)
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))
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 ","))
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.
629 \input{project-description.bbl}
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.
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
644 ;; object is the link you clicked on
645 (object (org-element-context))
647 (link-string-beginning)
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)))
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) ","))
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
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
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
680 (lambda (keyword desc format)
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))
686 ;; write out the latex bibliography command
688 ; (format "{\\setbox0\\vbox{\\bibliography{%s}}}"
689 ; (replace-regexp-in-string "\\.bib" "" (mapconcat 'identity
690 ; (mapcar 'expand-file-name
691 ; (split-string keyword ","))
694 (format "\\nobibliography{%s}"
695 (replace-regexp-in-string "\\.bib" "" (mapconcat 'identity
696 (mapcar 'expand-file-name
697 (split-string keyword ","))
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)
708 ((eq format 'org) (org-ref-get-org-bibliography))
709 ((eq format 'html) (org-ref-get-html-bibliography))
711 ;; write out the biblatex bibliography command
712 "\\printbibliography"))
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, ...
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)
724 ;; write out the latex bibliography command
725 (format "\\bibliographystyle{%s}" keyword)))))
728 *** Completion for bibliography link
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)))
735 (defun org-ref-insert-bibliography-link ()
736 "insert a bibliography with completion"
738 (insert (org-bibliography-complete-link)))
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
753 ;; object is the link you clicked on
754 (object (org-element-context))
756 (link-string-beginning)
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)))
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) ","))
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
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
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
789 (lambda (keyword desc format)
791 ((eq format 'html) (format "")); no output for html
793 ;; write out the latex addbibresource command
794 (format "\\addbibresource{%s}" keyword)))))
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.
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"
805 (save-excursion (widen)
806 (let* ((c-b (buffer-name))
809 (org-element-map (org-element-parse-buffer) 'link
811 "create a link for to the figure"
813 (and (string= (org-element-property :type link) "file")
815 "[^.]*\\.\\(png\\|jpg\\|eps\\|pdf\\)$"
816 (org-element-property :path link)))
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)))
825 "[[elisp:(progn (switch-to-buffer \"%s\")(widen)(goto-char %s))][figure %s: %s]] %s\n"
826 c-b start counter (or name "") caption)
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)
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))))))
841 'org-ref-list-of-figures ; on click
842 (lambda (keyword desc format)
845 (format "\\listoffigures")))))
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"
856 (let* ((c-b (buffer-name))
859 (org-element-map (org-element-parse-buffer 'element) 'table
861 "create a link for to the table"
863 (let ((start (org-element-property :begin table))
864 (name (org-element-property :name table))
865 (caption (caaar (org-element-property :caption table))))
868 "[[elisp:(progn (switch-to-buffer \"%s\")(widen)(goto-char %s))][table %s: %s]] %s\n"
869 c-b start counter (or name "") caption)
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)
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))))))
884 'org-ref-list-of-tables
885 (lambda (keyword desc format)
888 (format "\\listoftables")))))
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
896 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
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)
911 ((eq format 'html) (format "(<label>%s</label>)" path))
913 (format "\\label{%s}" keyword)))))
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.
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
927 :link (concat "ref:" (org-element-property :path object))))
929 ;; Store link on table
930 (when (equal (org-element-type object) 'table)
931 (org-store-link-props
933 :link (concat "ref:" (org-element-property :name object))))
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
941 ; :link (concat "ref:" (org-entry-get (point) "CUSTOM_ID"))))
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
948 :link (concat "ref:" (org-element-property :name object))))
951 (add-hook 'org-store-link-functions 'org-label-store-link)
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.
957 At the moment, ref links are not usable for section links. You need [[#CUSTOM_ID]] type links.
959 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
963 "on clicking goto the label. Navigate back with C-c &"
965 ;; next search from beginning of the buffer
967 ;; it is possible you would not find the label if narrowing is in effect
974 (goto-char (point-min))
975 (re-search-forward (format "label:%s\\b" label) nil t))
979 (goto-char (point-min))
980 (re-search-forward (format "\\label{%s}" label) nil t))
982 ;; #+label: name org-definition
984 (goto-char (point-min))
985 (re-search-forward (format "^#\\+label:\\s-*\\(%s\\)\\b" label) nil t))
989 (goto-char (point-min))
990 (re-search-forward (format "^#\\+tblname:\\s-*\\(%s\\)\\b" label) nil t))
992 ;; Commented out because these ref links do not actually translate correctly in LaTeX.
993 ;; you need [[#label]] links.
996 ; (goto-char (point-min))
997 ; (re-search-forward (format ":CUSTOM_ID:\s-*\\(%s\\)" label) nil t))
999 ;; we did not find anything, so go back to where we came
1000 (org-mark-ring-goto)
1001 (error "%s not found" label))
1003 (message "go back with (org-mark-ring-goto) `C-c &`"))
1005 (lambda (keyword desc format)
1007 ((eq format 'html) (format "(<ref>%s</ref>)" path))
1009 (format "\\ref{%s}" keyword)))))
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.
1014 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1015 (defun org-ref-get-org-labels ()
1016 "find #+LABEL: labels"
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))
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)
1031 (let ((custom_id (org-entry-get (point) "CUSTOM_ID")))
1032 (when (not (null custom_id))
1033 (setq results (append results (list custom_id)))))))
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 ()
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))
1048 Finally, we get the table names.
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
1054 (org-element-property :name table))))
1057 Now, we can put all the labels together which will give us a list of candidates.
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."
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))))))
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.
1074 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1075 (defun org-ref-complete-link (&optional arg)
1076 "Completion function for ref links"
1078 (setq label (completing-read "label: " (org-ref-get-labels)))
1079 (format "ref:%s" label)))
1082 Alternatively, you may want to just call a function that inserts a link with completion:
1084 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1085 (defun org-ref-insert-ref-link ()
1087 (insert (org-ref-complete-link)))
1092 This refers to the page of a label in LaTeX.
1094 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1098 "on clicking goto the label. Navigate back with C-c &"
1099 (org-mark-ring-push)
1100 ;; next search from beginning of the buffer
1106 (goto-char (point-min))
1107 (re-search-forward (format "label:%s\\b" label) nil t))
1111 (goto-char (point-min))
1112 (re-search-forward (format "\\label{%s}" label) nil t))
1114 ;; #+label: name org-definition
1116 (goto-char (point-min))
1117 (re-search-forward (format "^#\\+label:\\s-*\\(%s\\)\\b" label) nil t))
1121 (goto-char (point-min))
1122 (re-search-forward (format "^#\\+tblname:\\s-*\\(%s\\)\\b" label) nil t))
1124 ;; Commented out because these ref links do not actually translate correctly in LaTeX.
1125 ;; you need [[#label]] links.
1128 ; (goto-char (point-min))
1129 ; (re-search-forward (format ":CUSTOM_ID:\s-*\\(%s\\)" label) nil t))
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 &`"))
1136 (lambda (keyword desc format)
1138 ((eq format 'html) (format "(<pageref>%s</pageref>)" path))
1140 (format "\\pageref{%s}" keyword)))))
1143 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1144 (defun org-pageref-complete-link (&optional arg)
1145 "Completion function for ref links"
1147 (setq label (completing-read "label: " (org-ref-get-labels)))
1148 (format "ref:%s" label)))
1151 Alternatively, you may want to just call a function that inserts a link with completion:
1153 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1154 (defun org-pageref-insert-ref-link ()
1156 (insert (org-pageref-complete-link)))
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.
1163 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1167 "on clicking goto the label. Navigate back with C-c &"
1168 (org-mark-ring-push)
1169 ;; next search from beginning of the buffer
1175 (goto-char (point-min))
1176 (re-search-forward (format "\\label{%s}" label) nil t))
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 &`"))
1183 (lambda (keyword desc format)
1185 ((eq format 'html) (format "(<nameref>%s</nameref>)" path))
1187 (format "\\nameref{%s}" keyword)))))
1191 This is just the LaTeX ref for equations. On export, the reference is enclosed in parentheses.
1193 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1197 "on clicking goto the label. Navigate back with C-c &"
1198 (org-mark-ring-push)
1199 ;; next search from beginning of the buffer
1201 (goto-char (point-min))
1204 ;; search forward for the first match
1206 (re-search-forward (format "label:%s" label) nil t)
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 &`"))
1215 (lambda (keyword desc format)
1217 ((eq format 'html) (format "(<eqref>%s</eqref>)" path))
1219 (format "\\eqref{%s}" keyword)))))
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.
1225 *** Implementing the click actions of cite
1227 **** Getting the key we clicked on
1228 The first thing we need is to get the bibtex key we clicked on.
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."
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.
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))
1249 (if (not (org-element-property :contents-begin object))
1250 ;; this means no description in the link
1252 ;; we need the link path start and end
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)))
1259 ;; The key is the text between commas, or the link boundaries
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
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)
1273 ;; link with description. assume only one key
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.
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
1288 falling back to what the user has set in org-ref-default-bibliography
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))
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))
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))
1319 ;; we did not find anything. use defaults
1320 (setq org-ref-bibliography-files org-ref-default-bibliography)))
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)
1327 **** Finding the bibliography file a key is in
1328 Now, we can see if an entry is in a file.
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))))
1339 Finally, we want to know which file the key is in.
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"
1345 (let ((org-ref-bibliography-files (org-ref-find-bibliography))
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)))))
1356 **** convenience functions to act on citation at point
1358 :ID: af0b2a82-a7c9-4c08-9dac-09f93abc4a92
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.
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"
1366 (let* ((results (org-ref-get-bibtex-key-and-file))
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))))
1374 (defun org-ref-open-url-at-point ()
1375 "open the url for bibtex key under point."
1377 (let* ((results (org-ref-get-bibtex-key-and-file))
1379 (bibfile (cdr results)))
1382 (insert-file-contents bibfile)
1383 (bibtex-search-entry key)
1384 ;; I like this better than bibtex-url which does not always find
1387 (let ((url (bibtex-autokey-get-field "url")))
1392 (let ((doi (bibtex-autokey-get-field "doi")))
1394 (if (string-match "^http" doi)
1396 (browse-url (format "http://dx.doi.org/%s" doi)))
1397 (throw 'done nil))))))))
1400 (defun org-ref-open-notes-at-point ()
1401 "open the notes for bibtex key under point."
1403 (let* ((results (org-ref-get-bibtex-key-and-file))
1405 (bibfile (cdr results)))
1408 (insert-file-contents bibfile)
1409 (bibtex-search-entry key)
1410 (org-ref-open-bibtex-notes)))))
1413 (defun org-ref-citation-at-point ()
1414 "give message of current citation at point"
1416 (let* ((cb (current-buffer))
1417 (results (org-ref-get-bibtex-key-and-file))
1419 (bibfile (cdr results)))
1420 (message "%s" (progn
1422 (insert-file-contents bibfile)
1423 (bibtex-search-entry key)
1424 (org-ref-bib-citation))))))
1427 (defun org-ref-open-citation-at-point ()
1428 "open bibtex file to key at point"
1430 (let* ((cb (current-buffer))
1431 (results (org-ref-get-bibtex-key-and-file))
1433 (bibfile (cdr results)))
1435 (bibtex-search-entry key)))
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.
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.")
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.")
1454 (defun org-ref-copy-entry-as-summary ()
1455 "Copy the bibtex entry for the citation at point as a summary."
1457 (save-window-excursion
1458 (org-ref-open-citation-at-point)
1459 (kill-new (org-ref-bib-citation))))
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."
1466 (let ((new-file (ido-completing-read
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))
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)
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"))
1488 (defun org-ref-get-doi-at-point ()
1489 "Get doi for key at point."
1491 (let* ((results (org-ref-get-bibtex-key-and-file))
1493 (bibfile (cdr results))
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)))))
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."
1508 (doi-utils-wos (org-ref-get-doi-at-point)))
1511 (defun org-ref-wos-citing-at-point ()
1512 "open the doi in wos citing articles for bibtex key under point."
1514 (doi-utils-wos-citing (org-ref-get-doi-at-point)))
1517 (defun org-ref-wos-related-at-point ()
1518 "open the doi in wos related articles for bibtex key under point."
1520 (doi-utils-wos-related (org-ref-get-doi-at-point)))
1523 (defun org-ref-google-scholar-at-point ()
1524 "open the doi in google scholar for bibtex key under point."
1526 (doi-utils-google-scholar (org-ref-get-doi-at-point)))
1529 (defun org-ref-pubmed-at-point ()
1530 "open the doi in pubmed for bibtex key under point."
1532 (doi-utils-pubmed (org-ref-get-doi-at-point)))
1535 (defun org-ref-crossref-at-point ()
1536 "open the doi in crossref for bibtex key under point."
1538 (doi-utils-crossref (org-ref-get-doi-at-point)))
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."
1545 (let* ((results (org-ref-get-bibtex-key-and-file))
1547 (pdf-file (format (concat org-ref-pdf-directory "%s.pdf") key))
1548 (bibfile (cdr results))
1549 (url (save-excursion
1551 (insert-file-contents bibfile)
1552 (bibtex-search-entry key)
1553 (bibtex-autokey-get-field "url"))))
1554 (doi (save-excursion
1556 (insert-file-contents bibfile)
1557 (bibtex-search-entry key)
1558 ;; I like this better than bibtex-url which does not always find
1560 (bibtex-autokey-get-field "doi")))))
1562 (when (string= "" doi) (setq doi nil))
1563 (when (string= "" url) (setq url nil))
1564 (setq org-ref-cite-menu-funcs '())
1570 'org-ref-cite-menu-funcs
1571 '("o" "pen" org-ref-open-citation-at-point)))
1574 (when (file-exists-p pdf-file)
1576 'org-ref-cite-menu-funcs
1577 `("p" "df" ,org-ref-open-pdf-function) t))
1581 'org-ref-cite-menu-funcs
1582 '("n" "otes" org-ref-open-notes-at-point) t)
1587 'org-ref-cite-menu-funcs
1588 '("u" "rl" org-ref-open-url-at-point) t))
1593 'org-ref-cite-menu-funcs
1594 '("w" "os" org-ref-wos-at-point) t)
1597 'org-ref-cite-menu-funcs
1598 '("c" "iting" org-ref-wos-citing-at-point) t)
1601 'org-ref-cite-menu-funcs
1602 '("r" "elated" org-ref-wos-related-at-point) t)
1605 'org-ref-cite-menu-funcs
1606 '("g" "oogle scholar" org-ref-google-scholar-at-point) t)
1609 'org-ref-cite-menu-funcs
1610 '("P" "ubmed" org-ref-pubmed-at-point) t))
1612 ;; add user functions
1613 (dolist (tup org-ref-user-cite-menu-funcs)
1615 'org-ref-cite-menu-funcs
1620 'org-ref-cite-menu-funcs
1621 '("q" "uit" (lambda ())) t)
1623 ;; now we make a menu
1624 ;; construct menu string as a message
1627 (let* ((results (org-ref-get-bibtex-key-and-file))
1629 (bibfile (cdr results)))
1632 (insert-file-contents bibfile)
1633 (bibtex-search-entry key)
1634 (org-ref-bib-citation))))
1638 (concat "[" (elt tup 0) "]"
1640 org-ref-cite-menu-funcs "")))
1642 (let* ((input (read-char-exclusive))
1644 (char-to-string input) org-ref-cite-menu-funcs)))
1645 ;; now run the function (2nd element in choice)
1654 : org-ref-cite-onclick-minibuffer-menu
1656 *** A function to format a cite link
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.
1660 #+BEGIN_SRC emacs-lisp :tangle no
1661 ;(defun org-ref-cite-link-format (keyword desc format)
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) ",")
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.
1673 #+BEGIN_SRC emacs-lisp :tangle no
1676 ; 'org-ref-cite-onclick-minibuffer-menu
1677 ; 'org-ref-cite-link-format)
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.
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)
1691 (let ((bibtex-files (org-ref-find-bibliography)))
1692 (bibtex-global-key-alist))))))
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.
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)
1704 (format "[[#%s][%s]]" key key))
1705 (org-ref-split-and-strip-string keyword) ","))
1712 (org-ref-split-and-strip-string keyword) ",") "]"))
1717 (format "<a href=\"#%s\">%s</a>" key key))
1718 (org-ref-split-and-strip-string keyword) ","))
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) ""))
1726 (concat "\\" ,type (when desc (org-ref-format-citation-description desc)) "{"
1727 (mapconcat (lambda (key) key) (org-ref-split-and-strip-string keyword) ",")
1733 We create the links by mapping the function onto the list of defined link types.
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 ::."
1740 ((string-match "::" desc)
1741 (format "[%s][%s]" (car (setq results (split-string desc "::"))) (cadr results)))
1742 (t (format "[%s]" desc))))
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: ")
1750 ;; create the formatting function
1751 (eval `(org-ref-make-format-function ,type))
1756 org-ref-cite-onclick-function
1757 (quote ,(intern (format "org-ref-format-%s" type)))))
1759 ;; create the completion function
1760 (eval `(org-ref-make-completion-function ,type))
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)
1766 ;; and finally if a key is specified, we modify the reftex menu
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")))))))
1772 ;; create all the link types and their completion functions
1773 (mapcar 'org-ref-define-citation-link org-ref-cite-types)
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.
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."
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)))
1791 (if (not alternative-cite)
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) ","))))
1803 ;; We are next to a link, and we want to append
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) ","))))
1811 ;; insert fresh link
1814 (concat org-ref-default-citation-link
1816 (mapconcat 'identity (reftex-citation t) ",")))))
1818 ;; you pressed a C-u so we run this code
1822 cite:zhou-2004-first-lda-u,paier-2006-errat,boes-2015-estim-bulk
1826 : org-ref-insert-cite-link
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.
1831 #+BEGIN_SRC emacs-lisp :tangle no
1832 (defun org-cite-complete-link (&optional arg)
1833 "Completion function for cite links"
1835 org-ref-default-citation-link
1838 (let ((bibtex-files (org-ref-find-bibliography)))
1839 (bibtex-global-key-alist)))))
1842 Alternatively, you may shortcut the org-machinery with this command. You will be prompted for a citation type, and then offered key completion.
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)))))
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.
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."
1858 (let ((link (concat org-ref-default-citation-link
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)))
1869 org-ref minimally supports index entries. To make an index in a file, you should put in the LaTeX header these lines
1872 #+LATEX_HEADER: \usepackage{makeidx}
1873 #+LATEX_HEADER: \makeindex
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.
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.
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}]]
1891 index:encodings!input!cp850
1893 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1899 (lambda (path desc format)
1902 (format "\\index{%s}" path)))))
1904 ;; this will generate a temporary index of entries in the file.
1908 (let ((*index-links* '())
1909 (*initial-letters* '()))
1912 (org-element-map (org-element-parse-buffer) 'link
1914 (let ((type (nth 0 link))
1915 (plist (nth 1 link)))
1917 (when (equal (plist-get plist ':type) "index")
1920 (cons (plist-get plist :path)
1922 "[[elisp:(progn (switch-to-buffer \"%s\") (goto-char %s))][%s]]"
1924 (plist-get plist :begin) ;; position of link
1925 ;; grab a description
1927 (goto-char (plist-get plist :begin))
1928 (if (thing-at-point 'sentence)
1930 (replace-regexp-in-string
1931 "\n" "" (thing-at-point 'sentence))
1932 ;; or call it a link
1936 (setq *index-links* (cl-sort *index-links* 'string-lessp :key 'car))
1938 ;; now first letters
1939 (dolist (link *index-links*)
1940 (add-to-list '*initial-letters* (substring (car link) 0 1) t))
1942 ;; now create the index
1943 (switch-to-buffer (get-buffer-create "*index*"))
1946 (insert "#+TITLE: Index\n\n")
1947 (dolist (letter *initial-letters*)
1948 (insert (format "* %s\n" (upcase letter)))
1949 ;; now process the 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*")))
1957 (lambda (path desc format)
1960 (format "\\printindex")))))
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))) |
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.
1970 #+LATEX_HEADER: \usepackage{glossaries}
1971 #+LATEX_HEADER: \makeglossaries
1973 And at the end of the document put \makeglossaries.
1975 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
1978 nil ;; no follow action
1979 (lambda (path desc format)
1982 (format "\\newglossaryentry{%s}{%s}" path desc)))))
1988 nil ;; no follow action
1989 (lambda (path desc format)
1992 (format "\\gls{%s}" path)))))
1997 nil ;; no follow action
1998 (lambda (path desc format)
2001 (format "\\glspl{%s}" path)))))
2006 nil ;; no follow action
2007 (lambda (path desc format)
2010 (format "\\Gls{%s}" path)))))
2015 nil ;; no follow action
2016 (lambda (path desc format)
2019 (format "\\Glspl{%s}" path)))))
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 |
2137 ** create simple text citation from bibtex entry
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."
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))
2159 ;;authors, "title", Journal, vol(iss):pages (year).
2160 (format "%s, \"%s\", %s, %s:%s (%s)"
2161 author title journal volume pages year)))
2165 : org-ref-bib-citation
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."
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))
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)))
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."
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)))
2209 (if (file-exists-p pdf)
2210 (org-open-link-from-string (format "[[file:%s]]" pdf))
2214 ** open notes from bibtex
2215 We bind this to a key here [[*key%20bindings%20for%20utilities][key bindings for utilities]].
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.
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."
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))
2245 ;; save key to clipboard to make saving pdf later easier by pasting.
2248 (kill-ring-save (point-min) (point-max)))
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"))
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))
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))
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."
2281 (let ((bibtex-key )))
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"))
2288 (goto-char (point-min))
2290 (re-search-forward (format
2292 (first (reftex-citation t)) nil 'end))
2293 (funcall org-ref-open-notes-function))
2296 ** open url in browser from bibtex
2298 We bind this to a key here [[*key%20bindings%20for%20utilities][key bindings for utilities]].
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.
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"
2307 (bibtex-beginning-of-entry)
2309 (let ((url (bibtex-autokey-get-field "url")))
2314 (let ((doi (bibtex-autokey-get-field "doi")))
2316 (if (string-match "^http" doi)
2318 (browse-url (format "http://dx.doi.org/%s" doi)))
2320 (message "No url or doi found"))))
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.
2326 *** function to upload bibtex to citeulike
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.
2332 Relies on the python script /upload_bibtex_citeulike.py being in the user directory."
2334 (message "uploading to citeulike")
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)))))
2345 *** The upload script
2346 Here is the python script for uploading.
2348 *************** TODO document how to get the cookies
2352 #+BEGIN_SRC python :tangle upload_bibtex_citeulike.py
2354 import pickle, requests, sys
2357 with open('c:/Users/jkitchin/Dropbox/blogofile-jkitchin.github.com/_blog/cookies.pckl', 'rb') as f:
2358 cookies = pickle.load(f)
2360 url = 'http://www.citeulike.org/profile/jkitchin/import_do'
2362 bibtex = sys.stdin.read()
2364 data = {'pasted':bibtex,
2366 'tag_parsing':'simple',
2367 'strip_brackets':'no',
2368 'update_id':'bib-key',
2369 'btn_bibtex':'Import BibTeX file ...'}
2371 headers = {'content-type': 'multipart/form-data',
2372 'User-Agent':'jkitchin/johnrkitchin@gmail.com bibtexupload'}
2374 r = requests.post(url, headers=headers, data=data, cookies=cookies, files={})
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.
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."
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")))
2391 (insert (format "\\documentclass[12pt]{article}
2392 \\usepackage[version=3]{mhchem}
2394 \\usepackage[numbers]{natbib}
2395 \\usepackage[colorlinks=true, linkcolor=blue, urlcolor=blue, pdfstartview=FitH]{hyperref}
2399 \\bibliographystyle{unsrtnat}
2401 \\end{document}" bib-base))
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)
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.
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.
2419 If no bibliography is in the buffer the `reftex-default-bibliography' is used."
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)
2427 ;; open tempfile and insert org-buffer contents
2428 (find-file tempname)
2430 (setq basename (file-name-sans-extension
2431 (file-name-nondirectory buffer-file-name))
2432 texfile (concat tempname ".tex")
2433 bibfile (concat tempname ".bib"))
2435 ;; see if we have a bibliography, and insert the default one if not.
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 ",")))))
2443 ;; get a latex file and extract the references
2444 (org-latex-export-to-latex)
2447 (reftex-create-bibtex-file bibfile)
2449 ;; save results of the references
2450 (setq results (buffer-string))
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)
2457 (delete-file bibfile)
2458 (delete-file texfile)
2459 (delete-file tempname)
2461 ;; Now back to the original org buffer and insert the results
2462 (switch-to-buffer cb)
2463 (when (not (string= "" results))
2465 (goto-char (point-max))
2467 (org-insert-heading)
2468 (insert (format " Bibtex entries
2470 ,#+BEGIN_SRC text :tangle %s
2472 ,#+END_SRC" (concat (file-name-sans-extension (file-name-nondirectory (buffer-file-name))) ".bib") results))))))
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.
2478 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2481 (defun index (substring list)
2482 "return the index of string in a list of strings"
2485 (dolist (arg list i)
2486 (if (string-match (concat "^" substring "$") arg)
2491 ;; return counter if found, otherwise return nil
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.
2498 Makes a new buffer with clickable links."
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 '()))
2506 (org-element-map (org-element-parse-buffer) '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)))))
2519 (switch-to-buffer-other-window "*Missing citations*")
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))))
2528 (when (get-buffer "*Missing citations*")
2529 (kill-buffer "*Missing citations*"))
2530 (message "No bad cite links found"))))
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.
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"
2540 (occur "[^[:ascii:]]"))
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.
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"
2550 (bibtex-beginning-of-entry)
2551 (let* ((master '("author" "title" "journal" "volume" "number" "pages" "year" "doi" "url"))
2552 (entry (bibtex-parse-entry))
2555 (type (cdr (assoc "=type=" entry)))
2556 (key (cdr (assoc "=key=" entry))))
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))
2564 ;;these are the other fields in the entry
2565 (setq other-fields (remove-if-not (lambda(x) (not (member x master))) entry-fields))
2568 ;; right now we only resort articles
2569 ((string= (downcase type) "article")
2572 (concat "@article{" key ",\n"
2575 (when (member field entry-fields)
2576 (format "%s = %s," (downcase field) (cdr (assoc field entry))))) master "\n")
2579 (format "%s = %s," (downcase field) (cdr (assoc field entry)))) other-fields "\n")
2581 (bibtex-find-entry key)
2583 (bibtex-clean-entry)
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"
2594 (bibtex-beginning-of-entry)
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))
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")))
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))))
2615 (bibtex-make-field "doi")
2617 (insert (replace-regexp-in-string "^http://dx.doi.org/" "" doi)))
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))))
2625 (bibtex-make-field "year")
2627 (insert (read-string "Enter year: ")))
2629 ;; fix pages if they are empty if there is an eid to put there.
2630 (when (string= "-" pages)
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))))
2637 (bibtex-make-field "pages")
2641 ;; replace naked & with \&
2643 (bibtex-narrow-to-entry)
2644 (bibtex-beginning-of-entry)
2645 (message "checking &")
2646 (replace-regexp " & " " \\\\& ")
2649 ;; generate a key, and if it duplicates an existing key, edit it.
2651 (let ((key (bibtex-generate-autokey)))
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))
2663 (bibtex-search-entry key)
2664 (bibtex-copy-entry-as-kill)
2665 (switch-to-buffer-other-window "*duplicate entry*")
2667 (setq key (bibtex-read-key "Duplicate Key found, edit: " key)))
2670 (kill-new key))) ;; save key for pasting
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)
2679 org-ref-clean-bibtex-entry-hook)
2681 ;; sort fields within entry
2682 (org-ref-sort-bibtex-entry)
2683 ;; check for non-ascii characters
2684 (occur "[^[:ascii:]]")
2689 : org-ref-clean-bibtex-entry
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.
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."
2698 (let* ((results (org-ref-get-bibtex-key-and-file key))
2699 (bibfile (cdr results)))
2701 (insert-file-contents bibfile)
2702 (bibtex-search-entry key nil 0)
2703 (prog1 (reftex-get-bib-field "year" (bibtex-parse-entry t))
2706 (defun org-ref-sort-citation-link ()
2707 "replace link at point with sorted link by year"
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))
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))))
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))
2735 (defun org-ref-swap-citation-link (direction)
2736 "move citation at point in direction +1 is to the right, -1 to the left"
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))
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)))))
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)))
2764 I like convenience. Here are some aliases for faster typing.
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)
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)
2779 (defalias 'orcb 'org-ref-clean-bibtex-entry)
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.
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)
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)))
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 ","))))
2819 ;; We are next to a link, and we want to append
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 ","))))
2827 ;; insert fresh link
2830 (concat org-ref-default-citation-link
2832 (s-join keys ",")))))))
2834 (setq helm-bibtex-format-citation-functions
2835 '((org-mode . helm-bibtex-format-org-ref)))
2837 (require 'helm-bibtex)
2840 ** A helm click menu
2842 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2843 (defun org-ref-get-citation-string-at-point ()
2845 (let* ((results (org-ref-get-bibtex-key-and-file))
2847 (bibfile (cdr results)))
2850 (insert-file-contents bibfile)
2851 (bibtex-search-entry key)
2852 (org-ref-bib-citation)))))
2854 (defun org-ref-cite-candidates ()
2855 "Generate the list of possible candidates.
2856 Check for pdf and doi, and add appropriate functions."
2858 (let* ((results (org-ref-get-bibtex-key-and-file))
2860 (pdf-file (format (concat org-ref-pdf-directory "%s.pdf") key))
2861 (bibfile (cdr results))
2862 (url (save-excursion
2864 (insert-file-contents bibfile)
2865 (bibtex-search-entry key)
2866 (bibtex-autokey-get-field "url"))))
2867 (doi (save-excursion
2869 (insert-file-contents bibfile)
2870 (bibtex-search-entry key)
2871 ;; I like this better than bibtex-url which does not always find
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))))
2879 (when (file-exists-p pdf-file)
2882 '("Open pdf" . org-ref-open-pdf-at-point)
2888 '("Open notes" . org-ref-open-notes-at-point)
2894 '("Open in browser" . org-ref-open-url-at-point)
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)
2910 '("Copy formatted citation to clipboard" . org-ref-copy-entry-as-summary)
2915 '("Copy key to clipboard" . (lambda ()
2917 (car (org-ref-get-bibtex-key-and-file)))))
2922 '("Copy bibtex entry to file" . org-ref-copy-entry-at-point-to-file)
2927 '("Email bibtex entry and pdf" . (lambda ()
2929 (org-ref-open-citation-at-point)
2930 (email-bibtex-entry))))
2932 ;; finally return a numbered list of the candidates
2934 for cell in candidates
2935 collect (cons (format "%2s. %s" i (car cell))
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.
2944 ;; example of adding your own function
2946 'org-ref-helm-user-candidates
2947 '("Example" . (lambda () (message-box "You did it!")))
2950 (defun org-ref-cite-click-helm (key)
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."
2955 (let ((name (org-ref-get-citation-string-at-point))
2956 (candidates (org-ref-cite-candidates))
2957 (cb (current-buffer)))
2959 (helm :sources `(((name . ,name)
2960 (candidates . ,candidates)
2961 (action . (lambda (f)
2962 (switch-to-buffer cb)
2964 ((name . "User functions")
2965 (candidates . ,org-ref-helm-user-candidates)
2966 (action . (lambda (f)
2967 (switch-to-buffer cb)
2973 : org-ref-cite-click-helm
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.
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)))))
2985 (when org-ref-show-citation-on-enter
2986 (add-hook 'post-command-hook 'org-ref-cite-link-p))
2990 #+BEGIN_SRC emacs-lisp :tangle org-ref.el
2995 This code will tangle the elisp code out to org-ref.el and load it.
2997 [[elisp:(progn (org-babel-tangle) (load-file "org-ref.el"))]]
2999 Alternatively you may use:
3001 [[elisp:(org-babel-load-file "org-ref.org")]]