-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-get-bibtex-entry-ascii (key)
- "returns an ascii string for the bibliography entry corresponding to key"
-
- (format "[%s] %s" key (org-ref-get-bibtex-entry-citation key)))
-#+END_SRC
-
-Now, we map over the whole list of keys, and the whole bibliography, formatted as an unordered list.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-get-ascii-bibliography ()
- "Create an html bibliography when there are keys"
- (let ((keys (org-ref-get-bibtex-keys)))
- (when keys
- (concat
-"Bibliography
-=============
-"
- (mapconcat (lambda (x) (org-ref-get-bibtex-entry-ascii x)) keys "\n")
- "\n"))))
-#+END_SRC
-
-
-*** the links
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(org-add-link-type "bibliography"
- ;; this code is run on clicking. The bibliography
- ;; may contain multiple files. this code finds the
- ;; one you clicked on and opens it.
- (lambda (link-string)
- ;; get link-string boundaries
- ;; we have to go to the beginning of the line, and then search forward
-
- (let* ((bibfile)
- ;; object is the link you clicked on
- (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)))
-
- ;; We set the reftex-default-bibliography
- ;; here. it should be a local variable only in
- ;; the current buffer. We need this for using
- ;; reftex to do citations.
- (set (make-local-variable 'reftex-default-bibliography)
- (split-string (org-element-property :path object) ","))
-
- ;; now if we have comma separated bibliographies
- ;; we find the one clicked on. we want to
- ;; search forward to next comma from point
- (save-excursion
- (if (search-forward "," link-string-end 1 1)
- (setq key-end (- (match-end 0) 1)) ; we found a match
- (setq key-end (point)))) ; no comma found so take the point
- ;; and backward to previous comma from point
- (save-excursion
- (if (search-backward "," link-string-beginning 1 1)
- (setq key-beginning (+ (match-beginning 0) 1)) ; we found a match
- (setq key-beginning (point)))) ; no match found
- ;; save the key we clicked on.
- (setq bibfile (org-ref-strip-string (buffer-substring key-beginning key-end)))
- (find-file bibfile))) ; open file on click
-
- ;; formatting code
- (lambda (keyword desc format)
- (cond
- ((eq format 'org) (org-ref-get-org-bibliography))
- ((eq format 'ascii) (org-ref-get-ascii-bibliography))
- ((eq format 'html) (org-ref-get-html-bibliography))
- ((eq format 'latex)
- ;; write out the latex bibliography command
- (format "\\bibliography{%s}" (replace-regexp-in-string "\\.bib" "" (mapconcat 'identity
- (mapcar 'expand-file-name
- (split-string keyword ","))
- ",")))))))
-
-#+END_SRC
-
-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.
-
-#+BEGIN_LaTeX
- \input{project-description.bbl}
-#+END_LaTeX
-
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(org-add-link-type "nobibliography"
- ;; this code is run on clicking. The bibliography
- ;; may contain multiple files. this code finds the
- ;; one you clicked on and opens it.
- (lambda (link-string)
- ;; get link-string boundaries
- ;; we have to go to the beginning of the line, and then search forward
-
- (let* ((bibfile)
- ;; object is the link you clicked on
- (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)))
-
- ;; We set the reftex-default-bibliography
- ;; here. it should be a local variable only in
- ;; the current buffer. We need this for using
- ;; reftex to do citations.
- (set (make-local-variable 'reftex-default-bibliography)
- (split-string (org-element-property :path object) ","))
-
- ;; now if we have comma separated bibliographies
- ;; we find the one clicked on. we want to
- ;; search forward to next comma from point
- (save-excursion
- (if (search-forward "," link-string-end 1 1)
- (setq key-end (- (match-end 0) 1)) ; we found a match
- (setq key-end (point)))) ; no comma found so take the point
- ;; and backward to previous comma from point
- (save-excursion
- (if (search-backward "," link-string-beginning 1 1)
- (setq key-beginning (+ (match-beginning 0) 1)) ; we found a match
- (setq key-beginning (point)))) ; no match found
- ;; save the key we clicked on.
- (setq bibfile (org-ref-strip-string (buffer-substring key-beginning key-end)))
- (find-file bibfile))) ; open file on click
-
- ;; formatting code
- (lambda (keyword desc format)
- (cond
- ((eq format 'org) (org-ref-get-org-bibliography))
- ((eq format 'ascii) (org-ref-get-ascii-bibliography))
- ((eq format 'html) (org-ref-get-html-bibliography))
- ((eq format 'latex)
- ;; write out the latex bibliography command
-
-; (format "{\\setbox0\\vbox{\\bibliography{%s}}}"
-; (replace-regexp-in-string "\\.bib" "" (mapconcat 'identity
-; (mapcar 'expand-file-name
-; (split-string keyword ","))
-; ",")))
-
- (format "\\nobibliography{%s}"
- (replace-regexp-in-string "\\.bib" "" (mapconcat 'identity
- (mapcar 'expand-file-name
- (split-string keyword ","))
- ",")))
-
- ))))
-#+END_SRC
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(org-add-link-type "printbibliography"
- (lambda (arg) (message "Nothing implemented for clicking here."))
- (lambda (keyword desc format)
- (cond
- ((eq format 'org) (org-ref-get-org-bibliography))
- ((eq format 'html) (org-ref-get-html-bibliography))
- ((eq format 'latex)
- ;; write out the biblatex bibliography command
- "\\printbibliography"))
-))
-#+END_SRC
-
-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, ...
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(org-add-link-type "bibliographystyle"
- (lambda (arg) (message "Nothing implemented for clicking here."))
- (lambda (keyword desc format)
- (cond
- ((eq format 'latex)
- ;; write out the latex bibliography command
- (format "\\bibliographystyle{%s}" keyword)))))
-#+END_SRC
-
-*** Completion for bibliography link
-It would be nice
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-bibliography-complete-link (&optional arg)
- (format "bibliography:%s" (read-file-name "enter file: " nil nil t)))
-
-(defun org-ref-insert-bibliography-link ()
- "insert a bibliography with completion"
- (interactive)
- (insert (org-bibliography-complete-link)))
-#+END_SRC
-
-** addbibresource
-This is apparently used for biblatex.
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(org-add-link-type "addbibresource"
- ;; this code is run on clicking. The addbibresource
- ;; may contain multiple files. this code finds the
- ;; one you clicked on and opens it.
- (lambda (link-string)
- ;; get link-string boundaries
- ;; we have to go to the beginning of the line, and then search forward
-
- (let* ((bibfile)
- ;; object is the link you clicked on
- (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)))
-
- ;; We set the reftex-default-addbibresource
- ;; here. it should be a local variable only in
- ;; the current buffer. We need this for using
- ;; reftex to do citations.
- (set (make-local-variable 'reftex-default-addbibresource)
- (split-string (org-element-property :path object) ","))
-
- ;; now if we have comma separated bibliographies
- ;; we find the one clicked on. we want to
- ;; search forward to next comma from point
- (save-excursion
- (if (search-forward "," link-string-end 1 1)
- (setq key-end (- (match-end 0) 1)) ; we found a match
- (setq key-end (point)))) ; no comma found so take the point
- ;; and backward to previous comma from point
- (save-excursion
- (if (search-backward "," link-string-beginning 1 1)
- (setq key-beginning (+ (match-beginning 0) 1)) ; we found a match
- (setq key-beginning (point)))) ; no match found
- ;; save the key we clicked on.
- (setq bibfile (org-ref-strip-string (buffer-substring key-beginning key-end)))
- (find-file bibfile))) ; open file on click
-
- ;; formatting code
- (lambda (keyword desc format)
- (cond
- ((eq format 'html) (format "")); no output for html
- ((eq format 'latex)
- ;; write out the latex addbibresource command
- (format "\\addbibresource{%s}" keyword)))))
-#+END_SRC
-
-** List of Figures
-
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-list-of-figures (&optional arg)
- "Generate buffer with list of figures in them"
- (interactive)
- (save-excursion (widen)
- (let* ((c-b (buffer-name))
- (counter 0)
- (list-of-figures
- (org-element-map (org-element-parse-buffer) 'link
- (lambda (link)
- "create a link for to the figure"
- (when
- (and (string= (org-element-property :type link) "file")
- (string-match-p
- "[^.]*\\.\\(png\\|jpg\\|eps\\|pdf\\)$"
- (org-element-property :path link)))
- (incf counter)
-
- (let* ((start (org-element-property :begin link))
- (parent (car (cdr (org-element-property :parent link))))
- (caption (caaar (plist-get parent :caption)))
- (name (plist-get parent :name)))
- (if caption
- (format
- "[[elisp:(progn (switch-to-buffer \"%s\")(widen)(goto-char %s))][figure %s: %s]] %s\n"
- c-b start counter (or name "") caption)
- (format
- "[[elisp:(progn (switch-to-buffer \"%s\")(widen)(goto-char %s))][figure %s: %s]]\n"
- c-b start counter (or name "")))))))))
- (switch-to-buffer "*List of Figures*")
- (setq buffer-read-only nil)
- (org-mode)
- (erase-buffer)
- (insert (mapconcat 'identity list-of-figures ""))
- (setq buffer-read-only t)
- (use-local-map (copy-keymap org-mode-map))
- (local-set-key "q" #'(lambda () (interactive) (kill-buffer))))))
-
-(org-add-link-type
- "list-of-figures"
- 'org-ref-list-of-figures ; on click
- (lambda (keyword desc format)
- (cond
- ((eq format 'latex)
- (format "\\listoffigures")))))
-#+END_SRC
-
-** List of Tables
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-list-of-tables (&optional arg)
- "Generate a buffer with a list of tables"
- (interactive)
- (save-excursion
- (widen)
- (let* ((c-b (buffer-name))
- (counter 0)
- (list-of-tables
- (org-element-map (org-element-parse-buffer 'element) 'table
- (lambda (table)
- "create a link for to the table"
- (incf counter)
- (let ((start (org-element-property :begin table))
- (name (org-element-property :name table))
- (caption (caaar (org-element-property :caption table))))
- (if caption
- (format
- "[[elisp:(progn (switch-to-buffer \"%s\")(widen)(goto-char %s))][table %s: %s]] %s\n"
- c-b start counter (or name "") caption)
- (format
- "[[elisp:(progn (switch-to-buffer \"%s\")(widen)(goto-char %s))][table %s: %s]]\n"
- c-b start counter (or name ""))))))))
- (switch-to-buffer "*List of Tables*")
- (setq buffer-read-only nil)
- (org-mode)
- (erase-buffer)
- (insert (mapconcat 'identity list-of-tables ""))
- (setq buffer-read-only t)
- (use-local-map (copy-keymap org-mode-map))
- (local-set-key "q" #'(lambda () (interactive) (kill-buffer))))))
-
-(org-add-link-type
- "list-of-tables"
- 'org-ref-list-of-tables
- (lambda (keyword desc format)
- (cond
- ((eq format 'latex)
- (format "\\listoftables")))))
-#+END_SRC
-** label
-
-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 org-mode format for labels, tblnames too.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-count-labels (label)
- "Counts number of matches for label in the document"
- (+ (count-matches (format "label:%s\\b[^-:]" label) (point-min) (point-max))
- ;; for tblname, it is not enough to get word boundary
- ;; tab-little and tab-little-2 match then.
- (count-matches (format "^#\\+tblname:\\s-*%s\\b[^-:]" label) (point-min) (point-max))
- (count-matches (format "\\label{%s}" label) (point-min) (point-max))
- ;; this is the org-format #+label:
- (count-matches (format "^#\\+label:\\s-*%s\\b[^-:]" label) (point-min) (point-max))
- (let ((custom-id-count 0))
- (org-map-entries
- (lambda ()
- (when (string= label (org-entry-get (point) "CUSTOM_ID"))
- (setq custom-id-count (+ 1 custom-id-count)))))
- custom-id-count)))
-
-(org-add-link-type
- "label"
- (lambda (label)
- "on clicking count the number of label tags used in the buffer. A number greater than one means multiple labels!"
- (let ((count (org-ref-count-labels label)))
- (message (format "%s occurence%s"
- count
- (if (or (= count 0)
- (> count 1))
- "s"
- ""))
- (org-ref-count-labels label))))
- (lambda (keyword desc format)
- (cond
- ((eq format 'html) (format "(<label>%s</label>)" path))
- ((eq format 'latex)
- (format "\\label{%s}" keyword)))))
-#+END_SRC
-
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-label-store-link ()
- "store a link to a label. The output will be a ref to that label"
- ;; First we have to make sure we are on a label link.
- (let* ((object (org-element-context)))
- (when (and (equal (org-element-type object) 'link)
- (equal (org-element-property :type object) "label"))
- (org-store-link-props
- :type "ref"
- :link (concat "ref:" (org-element-property :path object))))
-
- ;; Store link on table
- (when (equal (org-element-type object) 'table)
- (org-store-link-props
- :type "ref"
- :link (concat "ref:" (org-element-property :name object))))
-
-;; it turns out this does not work. you can already store a link to a heading with a CUSTOM_ID
- ;; store link on heading with custom_id
-; (when (and (equal (org-element-type object) 'headline)
-; (org-entry-get (point) "CUSTOM_ID"))
-; (org-store-link-props
-; :type "ref"
-; :link (concat "ref:" (org-entry-get (point) "CUSTOM_ID"))))
-
- ;; and to #+label: lines
- (when (and (equal (org-element-type object) 'paragraph)
- (org-element-property :name object))
- (org-store-link-props
- :type "ref"
- :link (concat "ref:" (org-element-property :name object))))
-))
-
-(add-hook 'org-store-link-functions 'org-label-store-link)
-#+END_SRC
-** ref
-
-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.
-
-At the moment, ref links are not usable for section links. You need [[#CUSTOM_ID]] type links.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(org-add-link-type
- "ref"
- (lambda (label)
- "on clicking goto the label. Navigate back with C-c &"
- (org-mark-ring-push)
- ;; next search from beginning of the buffer
-
- ;; it is possible you would not find the label if narrowing is in effect
- (widen)
-
- (unless
- (or
- ;; our label links
- (progn
- (goto-char (point-min))
- (re-search-forward (format "label:%s\\b" label) nil t))
-
- ;; a latex label
- (progn
- (goto-char (point-min))
- (re-search-forward (format "\\label{%s}" label) nil t))
-
- ;; #+label: name org-definition
- (progn
- (goto-char (point-min))
- (re-search-forward (format "^#\\+label:\\s-*\\(%s\\)\\b" label) nil t))
-
- ;; org tblname
- (progn
- (goto-char (point-min))
- (re-search-forward (format "^#\\+tblname:\\s-*\\(%s\\)\\b" label) nil t))
-
-;; Commented out because these ref links do not actually translate correctly in LaTeX.
-;; you need [[#label]] links.
- ;; CUSTOM_ID
-; (progn
-; (goto-char (point-min))
-; (re-search-forward (format ":CUSTOM_ID:\s-*\\(%s\\)" label) nil t))
- )
- ;; we did not find anything, so go back to where we came
- (org-mark-ring-goto)
- (error "%s not found" label))
- (org-show-entry)
- (message "go back with (org-mark-ring-goto) `C-c &`"))
- ;formatting
- (lambda (keyword desc format)
- (cond
- ((eq format 'html) (format "(<ref>%s</ref>)" path))
- ((eq format 'latex)
- (format "\\ref{%s}" keyword)))))
-#+END_SRC
-
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-get-org-labels ()
- "Return a list of #+LABEL: labels."
- (save-excursion
- (goto-char (point-min))
- (let ((matches '()))
- (while (re-search-forward "^#\\+label:\\s-+\\(.*\\)\\b" (point-max) t)
- (add-to-list 'matches (match-string-no-properties 1) t))
-matches)))
-#+END_SRC
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-get-custom-ids ()
- "Return a list of custom_id properties in the buffer."
- (let ((results '()) custom_id)
- (org-map-entries
- (lambda ()
- (let ((custom_id (org-entry-get (point) "CUSTOM_ID")))
- (when (not (null custom_id))
- (setq results (append results (list custom_id)))))))
-results))
-#+END_SRC
-
-Here we get a list of the labels defined as raw latex labels, e.g. \label{eqtre}.
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-get-latex-labels ()
- (save-excursion
- (goto-char (point-min))
- (let ((matches '()))
- (while (re-search-forward "\\\\label{\\([a-zA-z0-9:-]*\\)}" (point-max) t)
- (add-to-list 'matches (match-string-no-properties 1) t))
-matches)))
-#+END_SRC
-
-Finally, we get the table names.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-get-tblnames ()
- "Return list of table names in the buffer."
- (org-element-map (org-element-parse-buffer 'element) 'table
- (lambda (table)
- (org-element-property :name table))))
-#+END_SRC
-
-Now, we can put all the labels together which will give us a list of candidates.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-get-labels ()
- "Returns a list of labels in the buffer that you can make a ref link to.
-This is used to auto-complete ref links and in helm menus."
- (save-excursion
- (save-restriction
- (widen)
- (goto-char (point-min))
- (let ((matches '()))
- ;; these are the label:stuff kinds
- (while (re-search-forward "[^#+]label:\\([a-zA-z0-9:-]*\\)" (point-max) t)
- (add-to-list 'matches (match-string-no-properties 1) t))
- (append matches
- (org-ref-get-org-labels)
- (org-ref-get-latex-labels)
- (org-ref-get-tblnames)
- (org-ref-get-custom-ids))))))
-#+END_SRC
-
-Let us make a helm function to insert a label link. This will help you enter unique labels by showing matching labels until they are all gone and you are left with a unique one. If you are on a link, it means you want to replace it.
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-helm-insert-label-link ()
- "Insert a label link. helm just shows you what labels already exist.
-If you are on a label link, replace it."
- (interactive)
- (let* ((labels (org-ref-get-labels))
- (cb (current-buffer)))
- (helm :sources `(((name . "Existing labels")
- (candidates . ,labels)
- ;; default action is to open to the label
- (action . (lambda (label)
- ;; unfortunately I do not have markers here
- (org-open-link-from-string (format "ref:%s" label))))
- ;; if you select a label, replace current one
- (action . (lambda (label)
- (switch-to-buffer ,cb)
- (cond
- ;; no prefix or on a link
- ((equal helm-current-prefix-arg nil)
- (let* ((object (org-element-context))
- (last-char (save-excursion
- (goto-char (org-element-property :end object))
- (backward-char)
- (if (looking-at " ")
- " "
- ""))))
- (when (-contains? '("label")
- (org-element-property :type object))
- ;; we are on a link, so replace it.
- (setf
- (buffer-substring
- (org-element-property :begin object)
- (org-element-property :end object))
- (concat
- (replace-regexp-in-string
- (org-element-property :path object)
- label
- (org-element-property :raw-link object))
- last-char)))))
- ;; no prefix options defined
- ))))
- ;; no matching selection creates a new label
- ((name . "Create new label")
- (dummy)
- ;; default action creates a new label, or replaces old one
- (action . (lambda (label)
- (switch-to-buffer ,cb)
- (let* ((object (org-element-context))
- (last-char (save-excursion
- (goto-char (org-element-property :end object))
- (backward-char)
- (if (looking-at " ")
- " "
- ""))))
- (if (-contains? '("label")
- (org-element-property :type object))
- ;; we are on a link, so replace it.
- (setf
- (buffer-substring
- (org-element-property :begin object)
- (org-element-property :end object))
- (concat
- (replace-regexp-in-string
- (org-element-property :path object)
- helm-pattern
- (org-element-property :raw-link object))
- last-char))
- ;; new link
- (insert
- (concat
- "label:"
- (or label
- helm-pattern))))))))))))
-#+END_SRC
-
-Now we create a 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.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-complete-link (&optional arg)
- "Completion function for ref links"
- (let ((label))
- (setq label (completing-read "label: " (org-ref-get-labels)))
- (format "ref:%s" label)))
-#+END_SRC
-
-Alternatively, you may want to just call a function that inserts a link with completion:
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-insert-ref-link ()
- (interactive)
- (insert (org-ref-complete-link)))
-#+END_SRC
-
-Another alternative ref insertion is to use helm.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-helm-insert-ref-link ()
- "Helm menu to insert ref links to labels in the document.
-If you are on link, replace with newly selected label.
-Use C-u to insert a different kind of ref link.
-Use C-u C-u to insert a [[#custom-id]] link
-"
- (interactive)
- (let* ((labels (org-ref-get-labels))
- (bs (buffer-string))
- (contexts (with-temp-buffer
- (insert bs)
- (mapcar 'org-ref-get-label-context labels)))
- (cb (current-buffer)))
-
- (helm :input (thing-at-point 'word)
- :sources `(((name . "Available labels to ref")
- (candidates . ,(loop for label in labels
- for context in contexts
- ;; we do some kludgy adding spaces
- ;; and bars to make it "easier" to
- ;; see in helm.
- collect (cons (concat
- label "\n"
- (mapconcat
- (lambda (x)
- (concat " |" x))
- (split-string context "\n")
- "\n"
- ) "\n\n") label)))
- ;; default action to replace or insert ref link.
- (action . (lambda (label)
- (switch-to-buffer ,cb)
-
- (cond
- ;; no prefix or on a link
- ((equal helm-current-prefix-arg nil)
- (let* ((object (org-element-context))
- (last-char (save-excursion
- (goto-char (org-element-property :end object))
- (backward-char)
- (if (looking-at " ")
- " "
- ""))))
- (if (-contains? '("ref" "eqref" "pageref" "nameref")
- (org-element-property :type object))
- ;; we are on a link, so replace it.
- (setf
- (buffer-substring
- (org-element-property :begin object)
- (org-element-property :end object))
- (concat
- (replace-regexp-in-string
- (org-element-property :path object)
- label
- (org-element-property :raw-link object))
- last-char))
- ;; insert a new link
- (insert
- (concat
- "ref:" label))
- )))
- ;; one prefix, alternate ref link
- ((equal helm-current-prefix-arg '(4))
- (insert
- (concat
- (helm :sources '((name . "Ref link types")
- (candidates . ("ref" "eqref" "pageref" "nameref"))
- (action . (lambda (x) x))))
- ":" label)))
- ;; two prefixes, insert section custom-id link
- ((equal helm-current-prefix-arg '(16))
- (insert
- (format "[[#%s]]" label)))
- ))
- ))))))
-#+END_SRC
-
-#+RESULTS:
-: org-ref-helm-insert-ref-link
-
-** pageref
-
-This refers to the page of a label in LaTeX.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(org-add-link-type
- "pageref"
- (lambda (label)
- "on clicking goto the label. Navigate back with C-c &"
- (org-mark-ring-push)
- ;; next search from beginning of the buffer
- (widen)
- (unless
- (or
- ;; our label links
- (progn
- (goto-char (point-min))
- (re-search-forward (format "label:%s\\b" label) nil t))
-
- ;; a latex label
- (progn
- (goto-char (point-min))
- (re-search-forward (format "\\label{%s}" label) nil t))
-
- ;; #+label: name org-definition
- (progn
- (goto-char (point-min))
- (re-search-forward (format "^#\\+label:\\s-*\\(%s\\)\\b" label) nil t))
-
- ;; org tblname
- (progn
- (goto-char (point-min))
- (re-search-forward (format "^#\\+tblname:\\s-*\\(%s\\)\\b" label) nil t))
-
-;; Commented out because these ref links do not actually translate correctly in LaTeX.
-;; you need [[#label]] links.
- ;; CUSTOM_ID
-; (progn
-; (goto-char (point-min))
-; (re-search-forward (format ":CUSTOM_ID:\s-*\\(%s\\)" label) nil t))
- )
- ;; we did not find anything, so go back to where we came
- (org-mark-ring-goto)
- (error "%s not found" label))
- (message "go back with (org-mark-ring-goto) `C-c &`"))
- ;formatting
- (lambda (keyword desc format)
- (cond
- ((eq format 'html) (format "(<pageref>%s</pageref>)" path))
- ((eq format 'latex)
- (format "\\pageref{%s}" keyword)))))
-#+END_SRC
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-pageref-complete-link (&optional arg)
- "Completion function for ref links"
- (let ((label))
- (setq label (completing-read "label: " (org-ref-get-labels)))
- (format "ref:%s" label)))
-#+END_SRC
-
-Alternatively, you may want to just call a function that inserts a link with completion:
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-pageref-insert-ref-link ()
- (interactive)
- (insert (org-pageref-complete-link)))
-#+END_SRC
-
-** nameref
-
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(org-add-link-type
- "nameref"
- (lambda (label)
- "on clicking goto the label. Navigate back with C-c &"
- (org-mark-ring-push)
- ;; next search from beginning of the buffer
- (widen)
- (unless
- (or
- ;; a latex label
- (progn
- (goto-char (point-min))
- (re-search-forward (format "\\label{%s}" label) nil t))
- )
- ;; we did not find anything, so go back to where we came
- (org-mark-ring-goto)
- (error "%s not found" label))
- (message "go back with (org-mark-ring-goto) `C-c &`"))
- ;formatting
- (lambda (keyword desc format)
- (cond
- ((eq format 'html) (format "(<nameref>%s</nameref>)" path))
- ((eq format 'latex)
- (format "\\nameref{%s}" keyword)))))
-#+END_SRC
-
-** eqref
-This is just the LaTeX ref for equations. On export, the reference is enclosed in parentheses.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(org-add-link-type
- "eqref"
- (lambda (label)
- "on clicking goto the label. Navigate back with C-c &"
- (org-mark-ring-push)
- ;; next search from beginning of the buffer
- (widen)
- (goto-char (point-min))
- (unless
- (or
- ;; search forward for the first match
- ;; our label links
- (re-search-forward (format "label:%s" label) nil t)
- ;; a latex label
- (re-search-forward (format "\\label{%s}" label) nil t)
- ;; #+label: name org-definition
- (re-search-forward (format "^#\\+label:\\s-*\\(%s\\)\\b" label) nil t))
- (org-mark-ring-goto)
- (error "%s not found" label))
- (message "go back with (org-mark-ring-goto) `C-c &`"))
- ;formatting
- (lambda (keyword desc format)
- (cond
- ((eq format 'html) (format "(<eqref>%s</eqref>)" path))
- ((eq format 'latex)
- (format "\\eqref{%s}" keyword)))))
-#+END_SRC
-
-** cite
-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.
-
-*** Implementing the click actions of cite
-
-**** Getting the key we clicked on
-The first thing we need is to get the bibtex key we clicked on.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-get-bibtex-key-under-cursor ()
- "returns key under the bibtex cursor. We search forward from
-point to get a comma, or the end of the link, and then backwards
-to get a comma, or the beginning of the link. that delimits the
-keyword we clicked on. We also strip the text properties."
- (interactive)
- (let* ((object (org-element-context))
- (link-string (org-element-property :path object)))
- ;; you may click on the part before the citations. here we make
- ;; sure to move to the beginning so you get the first citation.
- (let ((cp (point)))
- (goto-char (org-element-property :begin object))
- (search-forward link-string (org-element-property :end object))
- (goto-char (match-beginning 0))
- ;; check if we clicked before the path and move as needed.
- (unless (< cp (point))
- (goto-char cp)))
-
- (if (not (org-element-property :contents-begin object))
- ;; this means no description in the link
- (progn
- ;; we need the link path start and 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)))
-
- ;; The key is the text between commas, or the link boundaries
- (save-excursion
- (if (search-forward "," link-string-end t 1)
- (setq key-end (- (match-end 0) 1)) ; we found a match
- (setq key-end link-string-end))) ; no comma found so take the end
- ;; and backward to previous comma from point which defines the start character
- (save-excursion
- (if (search-backward "," link-string-beginning 1 1)
- (setq key-beginning (+ (match-beginning 0) 1)) ; we found a match
- (setq key-beginning link-string-beginning))) ; no match found
- ;; save the key we clicked on.
- (setq bibtex-key (org-ref-strip-string (buffer-substring key-beginning key-end)))
- (set-text-properties 0 (length bibtex-key) nil bibtex-key)
- bibtex-key)
- ;; link with description. assume only one key
- link-string)))
-#+END_SRC
-
-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.
-
-**** Getting the bibliographies
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-find-bibliography ()
- "find the bibliography in the buffer.
-This function sets and returns cite-bibliography-files, which is a list of files
-either from bibliography:f1.bib,f2.bib
-\bibliography{f1,f2}
-internal bibliographies
-
-falling back to what the user has set in org-ref-default-bibliography
-"
- (interactive)
- (catch 'result
- (save-excursion
- (goto-char (point-min))
- ;; look for a bibliography link
- (when (re-search-forward "\\<bibliography:\\([^\]\|\n]+\\)" nil t)
- (setq org-ref-bibliography-files
- (mapcar 'org-ref-strip-string (split-string (match-string 1) ",")))
- (throw 'result org-ref-bibliography-files))
-
-
- ;; we did not find a bibliography link. now look for \bibliography
- (goto-char (point-min))
- (when (re-search-forward "\\\\bibliography{\\([^}]+\\)}" nil t)
- ;; split, and add .bib to each file
- (setq org-ref-bibliography-files
- (mapcar (lambda (x) (concat x ".bib"))
- (mapcar 'org-ref-strip-string
- (split-string (match-string 1) ","))))
- (throw 'result org-ref-bibliography-files))
-
- ;; no bibliography found. maybe we need a biblatex addbibresource
- (goto-char (point-min))
- ;; look for a bibliography link
- (when (re-search-forward "addbibresource:\\([^\]\|\n]+\\)" nil t)
- (setq org-ref-bibliography-files
- (mapcar 'org-ref-strip-string (split-string (match-string 1) ",")))
- (throw 'result org-ref-bibliography-files))
-
- ;; we did not find anything. use defaults
- (setq org-ref-bibliography-files org-ref-default-bibliography)))
-
- ;; set reftex-default-bibliography so we can search
- (set (make-local-variable 'reftex-default-bibliography) org-ref-bibliography-files)
- org-ref-bibliography-files)
-#+END_SRC
-
-**** Finding the bibliography file a key is in
-Now, we can see if an entry is in a file.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-key-in-file-p (key filename)
- "determine if the key is in the file"
- (interactive "skey: \nsFile: ")
- (save-current-buffer
- (let ((bibtex-files (list filename)))
- (bibtex-search-entry key t))))
-#+END_SRC
-
-Finally, we want to know which file the key is in.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-get-bibtex-key-and-file (&optional key)
- "returns the bibtex key and file that it is in. If no key is provided, get one under point"
- (interactive)
- (let ((org-ref-bibliography-files (org-ref-find-bibliography))
- (file))
- (unless key
- (setq key (org-ref-get-bibtex-key-under-cursor)))
- (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)))))
- (cons key file)))
-#+END_SRC
-
-**** convenience functions to act on citation at point
- :PROPERTIES:
- :ID: af0b2a82-a7c9-4c08-9dac-09f93abc4a92
- :END:
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-open-pdf-at-point ()
- "open the pdf for bibtex key under point if it exists"
- (interactive)
- (let* ((results (org-ref-get-bibtex-key-and-file))
- (key (car results))
- (pdf-file (format (concat org-ref-pdf-directory "%s.pdf") key)))
- (if (file-exists-p pdf-file)
- (org-open-file pdf-file)
-(message "no pdf found for %s" key))))
-
-
-(defun org-ref-open-url-at-point ()
- "open the url for bibtex key under point."
- (interactive)
- (let* ((results (org-ref-get-bibtex-key-and-file))
- (key (car results))
- (bibfile (cdr results)))
- (save-excursion
- (with-temp-buffer
- (insert-file-contents bibfile)
- (bibtex-search-entry key)
- ;; I like this better than bibtex-url which does not always find
- ;; the urls
- (catch 'done
- (let ((url (bibtex-autokey-get-field "url")))
- (when url
- (browse-url url)
- (throw 'done nil)))
-
- (let ((doi (bibtex-autokey-get-field "doi")))
- (when doi
- (if (string-match "^http" doi)
- (browse-url doi)
- (browse-url (format "http://dx.doi.org/%s" doi)))
- (throw 'done nil))))))))
-
-
-(defun org-ref-open-notes-at-point ()
- "open the notes for bibtex key under point."
- (interactive)
- (let* ((results (org-ref-get-bibtex-key-and-file))
- (key (car results))
- (bibfile (cdr results)))
- (save-excursion
- (with-temp-buffer
- (insert-file-contents bibfile)
- (bibtex-search-entry key)
- (org-ref-open-bibtex-notes)))))
-
-
-(defun org-ref-citation-at-point ()
- "give message of current citation at point"
- (interactive)
- (let* ((cb (current-buffer))
- (results (org-ref-get-bibtex-key-and-file))
- (key (car results))
- (bibfile (cdr results)))
- (message "%s" (progn
- (with-temp-buffer
- (insert-file-contents bibfile)
- (bibtex-search-entry key)
- (org-ref-bib-citation))))))
-
-
-(defun org-ref-open-citation-at-point ()
- "open bibtex file to key at point"
- (interactive)
- (let* ((cb (current-buffer))
- (results (org-ref-get-bibtex-key-and-file))
- (key (car results))
- (bibfile (cdr results)))
- (find-file bibfile)
- (bibtex-search-entry key)))
-#+END_SRC
-
-**** the actual minibuffer menu
-Now, we create the menu. This is a rewrite of the cite action. This makes the function extendable by users.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defvar org-ref-cite-menu-funcs '()
- "Functions to run on cite click menu. Each entry is a list of (key menu-name function).
-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.")
-
-
-(defvar org-ref-user-cite-menu-funcs
- '(("C" "rossref" org-ref-crossref-at-point)
- ("y" "Copy entry to file" org-ref-copy-entry-at-point-to-file)
- ("s" "Copy summary" org-ref-copy-entry-as-summary))
- "user-defined functions to run on bibtex key at point.")
-
-
-(defun org-ref-copy-entry-as-summary ()
- "Copy the bibtex entry for the citation at point as a summary."
- (interactive)
- (save-window-excursion
- (org-ref-open-citation-at-point)
- (kill-new (org-ref-bib-citation))))
-
-
-(defun org-ref-copy-entry-at-point-to-file ()
- "Copy the bibtex entry for the citation at point to NEW-FILE.
-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."
- (interactive)
- (let ((new-file (ido-completing-read
- "Copy to bibfile: "
- (append org-ref-default-bibliography
- (f-entries "." (lambda (f) (f-ext? f "bib"))))))
- (key (org-ref-get-bibtex-key-under-cursor)))
- (save-window-excursion
- (org-ref-open-citation-at-point)
- (bibtex-copy-entry-as-kill))
-
- (let ((bibtex-files (list (file-truename new-file))))
- (if (assoc key (bibtex-global-key-alist))
- (message "That key already exists in %s" new-file)
- ;; add to file
- (save-window-excursion
- (find-file new-file)
- (goto-char (point-max))
- ;; make sure we are at the beginning of a line.
- (unless (looking-at "^") (insert "\n\n"))
- (bibtex-yank)
- (save-buffer))))))
-
-
-(defun org-ref-get-doi-at-point ()
- "Get doi for key at point."
- (interactive)
- (let* ((results (org-ref-get-bibtex-key-and-file))
- (key (car results))
- (bibfile (cdr results))
- doi)
- (save-excursion
- (with-temp-buffer
- (insert-file-contents bibfile)
- (bibtex-search-entry key)
- (setq doi (bibtex-autokey-get-field "doi"))
- ;; in case doi is a url, remove the url part.
- (replace-regexp-in-string "^http://dx.doi.org/" "" doi)))))
-
-
-;; functions that operate on key at point for click menu
-(defun org-ref-wos-at-point ()
- "open the doi in wos for bibtex key under point."
- (interactive)
- (doi-utils-wos (org-ref-get-doi-at-point)))
-
-
-(defun org-ref-wos-citing-at-point ()
- "open the doi in wos citing articles for bibtex key under point."
- (interactive)
- (doi-utils-wos-citing (org-ref-get-doi-at-point)))
-
-
-(defun org-ref-wos-related-at-point ()
- "open the doi in wos related articles for bibtex key under point."
- (interactive)
- (doi-utils-wos-related (org-ref-get-doi-at-point)))
-
-
-(defun org-ref-google-scholar-at-point ()
- "open the doi in google scholar for bibtex key under point."
- (interactive)
- (doi-utils-google-scholar (org-ref-get-doi-at-point)))
-
-
-(defun org-ref-pubmed-at-point ()
- "open the doi in pubmed for bibtex key under point."
- (interactive)
- (doi-utils-pubmed (org-ref-get-doi-at-point)))
-
-
-(defun org-ref-crossref-at-point ()
- "open the doi in crossref for bibtex key under point."
- (interactive)
- (doi-utils-crossref (org-ref-get-doi-at-point)))
-
-
-(defun org-ref-cite-onclick-minibuffer-menu (&optional link-string)
- "action when a cite link is clicked on.
-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."
- (interactive)
- (let* ((results (org-ref-get-bibtex-key-and-file))
- (key (car results))
- (pdf-file (format (concat org-ref-pdf-directory "%s.pdf") key))
- (bibfile (cdr results))
- (url (save-excursion
- (with-temp-buffer
- (insert-file-contents bibfile)
- (bibtex-search-entry key)
- (bibtex-autokey-get-field "url"))))
- (doi (save-excursion
- (with-temp-buffer
- (insert-file-contents bibfile)
- (bibtex-search-entry key)
- ;; I like this better than bibtex-url which does not always find
- ;; the urls
- (bibtex-autokey-get-field "doi")))))
-
- (when (string= "" doi) (setq doi nil))
- (when (string= "" url) (setq url nil))
- (setq org-ref-cite-menu-funcs '())
-
- ;; open action
- (when
- bibfile
- (add-to-list
- 'org-ref-cite-menu-funcs
- '("o" "pen" org-ref-open-citation-at-point)))
-
- ;; pdf
- (when (file-exists-p pdf-file)
- (add-to-list
- 'org-ref-cite-menu-funcs
- `("p" "df" ,org-ref-open-pdf-function) t))
-
- ;; notes
- (add-to-list
- 'org-ref-cite-menu-funcs
- '("n" "otes" org-ref-open-notes-at-point) t)
-
- ;; url
- (when (or url doi)
- (add-to-list
- 'org-ref-cite-menu-funcs
- '("u" "rl" org-ref-open-url-at-point) t))
-
- ;; doi funcs
- (when doi
- (add-to-list
- 'org-ref-cite-menu-funcs
- '("w" "os" org-ref-wos-at-point) t)
-
- (add-to-list
- 'org-ref-cite-menu-funcs
- '("c" "iting" org-ref-wos-citing-at-point) t)
-
- (add-to-list
- 'org-ref-cite-menu-funcs
- '("r" "elated" org-ref-wos-related-at-point) t)
-
- (add-to-list
- 'org-ref-cite-menu-funcs
- '("g" "oogle scholar" org-ref-google-scholar-at-point) t)
-
- (add-to-list
- 'org-ref-cite-menu-funcs
- '("P" "ubmed" org-ref-pubmed-at-point) t))
-
- ;; add user functions
- (dolist (tup org-ref-user-cite-menu-funcs)
- (add-to-list
- 'org-ref-cite-menu-funcs
- tup t))
-
- ;; finally quit
- (add-to-list
- 'org-ref-cite-menu-funcs
- '("q" "uit" (lambda ())) t)
-
- ;; now we make a menu
- ;; construct menu string as a message
- (message
- (concat
- (let* ((results (org-ref-get-bibtex-key-and-file))
- (key (car results))
- (bibfile (cdr results)))
- (save-excursion
- (with-temp-buffer
- (insert-file-contents bibfile)
- (bibtex-search-entry key)
- (org-ref-bib-citation))))
- "\n"
- (mapconcat
- (lambda (tup)
- (concat "[" (elt tup 0) "]"
- (elt tup 1) " "))
- org-ref-cite-menu-funcs "")))
- ;; get the input
- (let* ((input (read-char-exclusive))
- (choice (assoc
- (char-to-string input) org-ref-cite-menu-funcs)))
- ;; now run the function (2nd element in choice)
- (when choice
- (funcall
- (elt
- choice
- 2))))))
-#+END_SRC
-
-#+RESULTS:
-: org-ref-cite-onclick-minibuffer-menu
-
-*** A function to format a cite link
-
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle no
-;(defun org-ref-cite-link-format (keyword desc format)
-; (cond
-; ((eq format 'html) (mapconcat (lambda (key) (format "<a name=\"#%s\">%s</a>" key key) (org-ref-split-and-strip-string keyword) ",")))
-; ((eq format 'latex)
-; (concat "\\cite" (when desc (format "[%s]" desc)) "{"
-; (mapconcat (lambda (key) key) (org-ref-split-and-strip-string keyword) ",")
-; "}"))))
-#+END_SRC
-
-*** The actual cite link
-Finally, we define the cite link. This is deprecated; the links are autogenerated later. This is here for memory.
-
-#+BEGIN_SRC emacs-lisp :tangle no
-;(org-add-link-type
-; "cite"
-; 'org-ref-cite-onclick-minibuffer-menu
-; 'org-ref-cite-link-format)
-#+END_SRC
-
-*** Automatic definition of the cite links
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defmacro org-ref-make-completion-function (type)
- `(defun ,(intern (format "org-%s-complete-link" type)) (&optional arg)
- (interactive)
- (format "%s:%s"
- ,type
- (completing-read
- "bibtex key: "
- (let ((bibtex-files (org-ref-find-bibliography)))
- (bibtex-global-key-alist))))))
-#+END_SRC
-
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defmacro org-ref-make-format-function (type)
- `(defun ,(intern (format "org-ref-format-%s" type)) (keyword desc format)
- (cond
- ((eq format 'org)
- (mapconcat
- (lambda (key)
- (format "[[#%s][%s]]" key key))
- (org-ref-split-and-strip-string keyword) ","))
-
- ((eq format 'ascii)
- (concat "["
- (mapconcat
- (lambda (key)
- (format "%s" key))
- (org-ref-split-and-strip-string keyword) ",") "]"))
-
- ((eq format 'html)
- (mapconcat
- (lambda (key)
- (format "<a href=\"#%s\">%s</a>" key key))
- (org-ref-split-and-strip-string keyword) ","))
-
- ((eq format 'latex)
- (if (string= (substring type -1) "s")
- ;; biblatex format for multicite commands, which all end in s. These are formated as \cites{key1}{key2}...
- (concat "\\" ,type (mapconcat (lambda (key) (format "{%s}" key))
- (org-ref-split-and-strip-string keyword) ""))
- ;; bibtex format
- (concat "\\" ,type (when desc (org-ref-format-citation-description desc)) "{"
- (mapconcat (lambda (key) key) (org-ref-split-and-strip-string keyword) ",")
- "}")))
- ;; for markdown we generate pandoc citations
- ((eq format 'md)
- (cond
- (desc ;; pre and or post text
- (let* ((text (split-string desc "::"))
- (pre (car text))
- (post (cadr text)))
- (concat
- (format "[@%s," keyword)
- (when pre (format " %s" pre))
- (when post (format ", %s" post))
- "]")))
- (t
- (format "[%s]"
- (mapconcat
- (lambda (key) (concat "@" key))
- (org-ref-split-and-strip-string keyword)
- "; "))))))))
-#+END_SRC
-
-
-
-We create the links by mapping the function onto the list of defined link types.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-format-citation-description (desc)
- "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 ::."
- (interactive)
- (cond
- ((string-match "::" desc)
- (format "[%s][%s]" (car (setq results (split-string desc "::"))) (cadr results)))
- (t (format "[%s]" desc))))
-
-(defun org-ref-define-citation-link (type &optional key)
- "add a citation link for org-ref. With optional key, set the reftex binding. For example:
-(org-ref-define-citation-link \"citez\" ?z) will create a new citez link, with reftex key of z,
-and the completion function."
- (interactive "sCitation Type: \ncKey: ")
-
- ;; create the formatting function
- (eval `(org-ref-make-format-function ,type))
-
- (eval-expression
- `(org-add-link-type
- ,type
- org-ref-cite-onclick-function
- (quote ,(intern (format "org-ref-format-%s" type)))))
-
- ;; create the completion function
- (eval `(org-ref-make-completion-function ,type))
-
- ;; store new type so it works with adding citations, which checks
- ;; for existence in this list
- (add-to-list 'org-ref-cite-types type)
-
- ;; and finally if a key is specified, we modify the reftex menu
- (when key
- (setf (nth 2 (assoc 'org reftex-cite-format-builtin))
- (append (nth 2 (assoc 'org reftex-cite-format-builtin))
- `((,key . ,(concat type ":%l")))))))
-
-;; create all the link types and their completion functions
-(mapcar 'org-ref-define-citation-link org-ref-cite-types)
-#+END_SRC
-
-*** org-ref-insert-cite-link
-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.
-
-#+BEGIN_SRC emacs-lisp :tangle org-ref.el
-(defun org-ref-insert-cite-link (alternative-cite)
- "Insert a default citation link using reftex. If you are on a link, it
-appends to the end of the link, otherwise, a new link is
-inserted. Use a prefix arg to get a menu of citation types."
- (interactive "P")
- (org-ref-find-bibliography)
- (let* ((object (org-element-context))
- (link-string-beginning (org-element-property :begin object))
- (link-string-end (org-element-property :end object))
- (path (org-element-property :path object)))
-
- (if (not alternative-cite)
-
- (cond
- ;; case where we are in a link
- ((and (equal (org-element-type object) 'link)
- (-contains? org-ref-cite-types (org-element-property :type object)))
- (goto-char link-string-end)
- ;; sometimes there are spaces at the end of the link
- ;; this code moves point pack until no spaces are there
- (while (looking-back " ") (backward-char))
- (insert (concat "," (mapconcat 'identity (reftex-citation t ?a) ","))))
-
- ;; We are next to a link, and we want to append
- ((save-excursion
- (backward-char)
- (and (equal (org-element-type (org-element-context)) 'link)
- (-contains? org-ref-cite-types (org-element-property :type (org-element-context)))))
- (while (looking-back " ") (backward-char))
- (insert (concat "," (mapconcat 'identity (reftex-citation t ?a) ","))))
-
- ;; insert fresh link
- (t
- (insert
- (concat org-ref-default-citation-link
- ":"
- (mapconcat 'identity (reftex-citation t) ",")))))
-
- ;; you pressed a C-u so we run this code
- (reftex-citation)))
- )