-#+END_SRC
-
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defcustom org-ref-cite-types
- '("cite" "nocite" ;; the default latex cite commands
- ;; natbib cite commands, http://ctan.unixbrain.com/macros/latex/contrib/natbib/natnotes.pdf
- "citet" "citet*" "citep" "citep*"
- "citealt" "citealt*" "citealp" "citealp*"
- "citenum" "citetext"
- "citeauthor" "citeauthor*"
- "citeyear" "citeyear*"
- "Citet" "Citep" "Citealt" "Citealp" "Citeauthor"
- ;; biblatex commands
- ;; http://ctan.mirrorcatalogs.com/macros/latex/contrib/biblatex/doc/biblatex.pdf
- "Cite"
- "parencite" "Parencite"
- "footcite" "footcitetext"
- "textcite" "Textcite"
- "smartcite" "Smartcite"
- "cite*" "parencite*" "supercite"
- "autocite" "Autocite" "autocite*" "Autocite*"
- "Citeauthor*"
- "citetitle" "citetitle*"
- "citedate" "citedate*"
- "citeurl"
- "fullcite" "footfullcite"
- ;; "volcite" "Volcite" cannot support the syntax
- "notecite" "Notecite"
- "pnotecite" "Pnotecite"
- "fnotecite"
- ;; multicites. Very limited support for these.
- "cites" "Cites" "parencites" "Parencites"
- "footcites" "footcitetexts"
- "smartcites" "Smartcites" "textcites" "Textcites"
- "supercites" "autocites" "Autocites"
- ;; for the bibentry package
- "bibentry"
- )
- "List of citation types known in org-ref"
- :type '(repeat :tag "List of citation types" string)
- :group 'org-ref)
-#+END_SRC
-
-We need a hook variable to store user-defined bibtex entry cleaning functions
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defcustom org-ref-clean-bibtex-entry-hook nil
- "Hook that is run in org-ref-clean-bibtex-entry. The functions should take no arguments, and operate on the bibtex entry at point."
- :group 'org-ref
- :type 'hook)
-#+END_SRC
-
-** Program variables
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defvar org-ref-bibliography-files
- nil
- "variable to hold bibliography files to be searched")
-#+END_SRC
-
-** org-mode / reftex setup
-
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(require 'reftex)
-(defun org-mode-reftex-setup ()
- (and (buffer-file-name)
- (file-exists-p (buffer-file-name))
- (global-auto-revert-mode t)
- ;; I do not remember why I put this next line in. It doesn't
- ;; work for org-files. Nothing very bad happens, but it gives
- ;; an annoying error. Commenting it out for now.
- ;(reftex-parse-all)
- )
- (make-local-variable 'reftex-cite-format)
- (setq reftex-cite-format 'org)
- (define-key org-mode-map (kbd org-ref-insert-cite-key) org-ref-insert-cite-function))
-
-(add-hook 'org-mode-hook 'org-mode-reftex-setup)
-
-(eval-after-load 'reftex-vars
- '(progn
- (add-to-list 'reftex-cite-format-builtin
- '(org "Org-mode citation"
- ((?\C-m . "cite:%l") ; default
- (?d . ",%l") ; for appending
- (?a . "autocite:%l")
- (?t . "citet:%l")
- (?T . "citet*:%l")
- (?p . "citep:%l")
- (?P . "citep*:%l")
- (?h . "citeauthor:%l")
- (?H . "citeauthor*:%l")
- (?y . "citeyear:%l")
- (?x . "citetext:%l")
- (?n . "nocite:%l")
- )))))
-#+END_SRC
-
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle no
-;; add new format
-(setf (nth 2 (assoc 'org reftex-cite-format-builtin))
- (append (nth 2 (assoc 'org reftex-cite-format-builtin)) '((?W . "textcite:%l")
- (?z . "newcite:%l"))))
-#+END_SRC
-
-You can define a new citation link like this:
-#+BEGIN_SRC emacs-lisp :tangle no
-(org-ref-define-citation-link "citez" ?z)
-#+END_SRC
-
-* Links
-Most of this library is the creation of functional links to help with references and citations.
-** General utilities
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-strip-string (string)
- "strip leading and trailing whitespace from the string"
- (replace-regexp-in-string
- (concat search-whitespace-regexp "$" ) ""
- (replace-regexp-in-string
- (concat "^" search-whitespace-regexp ) "" string)))
-#+END_SRC
-
-It is helpful to make the previous function operate on a list of strings here.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-split-and-strip-string (string)
- "split key-string and strip keys. Assumes the key-string is comma delimited"
- (mapcar 'org-ref-strip-string (split-string string ",")))
-#+END_SRC
-
-** bibliography and bibliographystyle
-*** An html bibliography
-
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-reftex-get-bib-field (field entry &optional format)
- "similar to reftex-get-bib-field, but removes enclosing braces and quotes"
- (let ((result))
- (setq result (reftex-get-bib-field field entry format))
- (when (and (not (string= result "")) (string= "{" (substring result 0 1)))
- (setq result (substring result 1 -1)))
- (when (and (not (string= result "")) (string= "\"" (substring result 0 1)))
- (setq result (substring result 1 -1)))
- result))
-
-(defun org-ref-reftex-format-citation (entry format)
- "return a formatted string for the bibtex entry (from bibtex-parse-entry) according
-to the format argument. The format is a string with these percent escapes.
-
-In the format, the following percent escapes will be expanded.
-
-%l The BibTeX label of the citation.
-%a List of author names, see also `reftex-cite-punctuation'.
-%2a Like %a, but abbreviate more than 2 authors like Jones et al.
-%A First author name only.
-%e Works like %a, but on list of editor names. (%2e and %E work a well)
-
-It is also possible to access all other BibTeX database fields:
-%b booktitle %c chapter %d edition %h howpublished
-%i institution %j journal %k key %m month
-%n number %o organization %p pages %P first page
-%r address %s school %u publisher %t title
-%v volume %y year
-%B booktitle, abbreviated %T title, abbreviated
-%U url
-%D doi
-%S series
-
-Usually, only %l is needed. The other stuff is mainly for the echo area
-display, and for (setq reftex-comment-citations t).
-
-%< as a special operator kills punctuation and space around it after the
-string has been formatted.
-
-A pair of square brackets indicates an optional argument, and RefTeX
-will prompt for the values of these arguments.
-
-Beware that all this only works with BibTeX database files. When
-citations are made from the \bibitems in an explicit thebibliography
-environment, only %l is available."
- ;; Format a citation from the info in the BibTeX ENTRY
-
- (unless (stringp format) (setq format "\\cite{%l}"))
-
- (if (and reftex-comment-citations
- (string-match "%l" reftex-cite-comment-format))
- (error "reftex-cite-comment-format contains invalid %%l"))
-
- (while (string-match
- "\\(\\`\\|[^%]\\)\\(\\(%\\([0-9]*\\)\\([a-zA-Z]\\)\\)[.,;: ]*\\)"
- format)
- (let ((n (string-to-number (match-string 4 format)))
- (l (string-to-char (match-string 5 format)))
- rpl b e)
- (save-match-data
- (setq rpl
- (cond
- ((= l ?l) (concat
- (org-ref-reftex-get-bib-field "&key" entry)
- (if reftex-comment-citations
- reftex-cite-comment-format
- "")))
- ((= l ?a) (reftex-format-names
- (reftex-get-bib-names "author" entry)
- (or n 2)))
- ((= l ?A) (car (reftex-get-bib-names "author" entry)))
- ((= l ?b) (org-ref-reftex-get-bib-field "booktitle" entry "in: %s"))
- ((= l ?B) (reftex-abbreviate-title
- (org-ref-reftex-get-bib-field "booktitle" entry "in: %s")))
- ((= l ?c) (org-ref-reftex-get-bib-field "chapter" entry))
- ((= l ?d) (org-ref-reftex-get-bib-field "edition" entry))
- ((= l ?D) (org-ref-reftex-get-bib-field "doi" entry))
- ((= l ?e) (reftex-format-names
- (reftex-get-bib-names "editor" entry)
- (or n 2)))
- ((= l ?E) (car (reftex-get-bib-names "editor" entry)))
- ((= l ?h) (org-ref-reftex-get-bib-field "howpublished" entry))
- ((= l ?i) (org-ref-reftex-get-bib-field "institution" entry))
- ((= l ?j) (org-ref-reftex-get-bib-field "journal" entry))
- ((= l ?k) (org-ref-reftex-get-bib-field "key" entry))
- ((= l ?m) (org-ref-reftex-get-bib-field "month" entry))
- ((= l ?n) (org-ref-reftex-get-bib-field "number" entry))
- ((= l ?o) (org-ref-reftex-get-bib-field "organization" entry))
- ((= l ?p) (org-ref-reftex-get-bib-field "pages" entry))
- ((= l ?P) (car (split-string
- (org-ref-reftex-get-bib-field "pages" entry)
- "[- .]+")))
- ((= l ?s) (org-ref-reftex-get-bib-field "school" entry))
- ((= l ?S) (org-ref-reftex-get-bib-field "series" entry))
- ((= l ?u) (org-ref-reftex-get-bib-field "publisher" entry))
- ((= l ?U) (org-ref-reftex-get-bib-field "url" entry))
- ((= l ?r) (org-ref-reftex-get-bib-field "address" entry))
- ;; strip enclosing brackets from title if they are there
- ((= l ?t) (org-ref-reftex-get-bib-field "title" entry))
- ((= l ?T) (reftex-abbreviate-title
- (org-ref-reftex-get-bib-field "title" entry)))
- ((= l ?v) (org-ref-reftex-get-bib-field "volume" entry))
- ((= l ?y) (org-ref-reftex-get-bib-field "year" entry)))))
-
- (if (string= rpl "")
- (setq b (match-beginning 2) e (match-end 2))
- (setq b (match-beginning 3) e (match-end 3)))
- (setq format (concat (substring format 0 b) rpl (substring format e)))))
- (while (string-match "%%" format)
- (setq format (replace-match "%" t t format)))
- (while (string-match "[ ,.;:]*%<" format)
- (setq format (replace-match "" t t format)))
- ;; also replace carriage returns, tabs, and multiple whitespaces
- (setq format (replace-regexp-in-string "\n\\|\t\\|\s+" " " format))
- format)
-
-(defun org-ref-get-bibtex-entry-citation (key)
- "returns a string for the bibliography entry corresponding to key, and formatted according to the type in `org-ref-bibliography-entry-format'"
-
- (let ((org-ref-bibliography-files (org-ref-find-bibliography))
- (file) (entry) (bibtex-entry) (entry-type) (format))
-
- (setq file (catch 'result
- (loop for file in org-ref-bibliography-files do
- (if (org-ref-key-in-file-p key (file-truename file))
- (throw 'result file)
- (message "%s not found in %s" key (file-truename file))))))
-
- (with-temp-buffer
- (insert-file-contents file)
- (bibtex-search-entry key nil 0)
- (setq bibtex-entry (bibtex-parse-entry))
- (setq entry-type (downcase (cdr (assoc "=type=" bibtex-entry))))
- (setq format (cdr (assoc entry-type org-ref-bibliography-entry-format)))
- (if format
- (setq entry (org-ref-reftex-format-citation bibtex-entry format))
- (save-restriction
- (bibtex-narrow-to-entry)
- (setq entry (buffer-string)))))
- entry))