summaryrefslogtreecommitdiff
path: root/lisp/km-bib.el
diff options
context:
space:
mode:
authorKyle Meyer <kyle@kyleam.com>2016-03-12 22:13:34 -0500
committerKyle Meyer <kyle@kyleam.com>2016-03-12 22:13:34 -0500
commit170f8addf9986404ae3f852c15c83aa1cd3d80ca (patch)
treee12afcf61b18d12a2e87ca43f69e6768159c76fc /lisp/km-bib.el
parent621647663373780b54939bf58bc8c06beaa67ed6 (diff)
downloademacs.d-170f8addf9986404ae3f852c15c83aa1cd3d80ca.tar.gz
Add bibtex-author-last-name-first command
Diffstat (limited to 'lisp/km-bib.el')
-rw-r--r--lisp/km-bib.el38
1 files changed, 38 insertions, 0 deletions
diff --git a/lisp/km-bib.el b/lisp/km-bib.el
index 360e08a..b7b7946 100644
--- a/lisp/km-bib.el
+++ b/lisp/km-bib.el
@@ -162,6 +162,44 @@ to
(delete-region (bibtex-start-of-field bounds)
(point))))))))
+(defun km/bibtex-author-last-name-first ()
+ "Use last name first format for authors.
+
+Convert
+
+ name1 name2 name3 and ...
+
+to
+
+ name3, name1 name2 and ..."
+ (interactive)
+ (save-excursion
+ (bibtex-beginning-of-entry)
+ (let ((bounds (bibtex-search-forward-field "author" t)))
+ (when bounds
+ (let* ((beg (1+ (bibtex-start-of-text-in-field bounds)))
+ (end (1- (bibtex-end-of-text-in-field bounds)))
+ (author-list (buffer-substring-no-properties beg end))
+ (author-re (rx line-start
+ (group (one-or-more not-newline))
+ (syntax whitespace)
+ (group (one-or-more (not (syntax whitespace))))
+ line-end))
+ authors)
+ (with-temp-buffer
+ (insert (replace-regexp-in-string
+ " and " "\n"
+ (replace-regexp-in-string
+ "[\n\t ]+" " " author-list)))
+ (goto-char (point-min))
+ (while (re-search-forward author-re nil t)
+ (push (concat (match-string 2) ", " (match-string 1))
+ authors)))
+ (goto-char beg)
+ (delete-region beg end)
+ (insert (mapconcat #'identity (nreverse authors) " and "))
+ (fill-paragraph))))))
+
;;;###autoload
(defun km/browse-doi (doi)
"Open DOI in browser.