Skip to content

Instantly share code, notes, and snippets.

@bdarcus
Last active September 23, 2022 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdarcus/643616e5a9532f7a73576d3dadbd44b9 to your computer and use it in GitHub Desktop.
Save bdarcus/643616e5a9532f7a73576d3dadbd44b9 to your computer and use it in GitHub Desktop.
(defun org-ref-to-org-cite ()
"Attempt to convert org-ref citations to org-cite syntax."
; adapted from https://tecosaur.github.io/emacs-config/config.html#citation,code--4
(interactive)
(let* ((cite-conversions '(("cite" . "//b") ("Cite" . "//bc")
("nocite" . "/n")
("citep" . "") ("citep*" . "//f")
("parencite" . "") ("Parencite" . "//c")
("citeauthor" . "/a/f") ("citeauthor*" . "/a")
("citeyear" . "/na/b")
("Citep" . "//c") ("Citealp" . "//bc")
("Citeauthor" . "/a/cf") ("Citeauthor*" . "/a/c")
("autocite" . "") ("Autocite" . "//c")
("textcite" . "/text") ("Textcite" . "/text")
("notecite" . "/l/b") ("Notecite" . "/l/bc")
("pnotecite" . "/l") ("Pnotecite" . "/l/bc")))
(cite-regexp (rx (? "[[") (regexp (regexp-opt (mapcar #'car cite-conversions) t))
":"
(group (or (+ (not (any "\n ,.)]}")))))
(opt "]")
(opt (seq "[" (group (+ (any alnum ","))(opt (seq "\\" (+ alpha))))"]]")))))
(save-excursion
(goto-char (point-min))
(while (re-search-forward cite-regexp nil t)
(message (format "[cite%s:@%s%s"
(cdr (assoc (match-string 1) cite-conversions))
(match-string 2)
(if (match-string 3)
(concat " " (string-replace "\\" "\\\\" (match-string 3))) (concat ""))))
(replace-match (format "[cite%s:@%s%s"
(cdr (assoc (match-string 1) cite-conversions))
(match-string 2)
(if (match-string 3)
(concat " " (string-replace "\\" "\\\\" (match-string 3))) (concat ""))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment