summaryrefslogtreecommitdiff
path: root/lisp/init-bib.el
diff options
context:
space:
mode:
authorKyle Meyer <kyle@kyleam.com>2015-01-19 21:34:57 -0500
committerKyle Meyer <kyle@kyleam.com>2015-01-19 21:34:57 -0500
commitfc41568c60cea6730aca12c7c4a31a841f5cb40f (patch)
treeb961a6c1095075959a1e234ebc25a78dfbaa9c1d /lisp/init-bib.el
parent7f0986d2416f185d7a0bcf52d09e32cfd3cc4877 (diff)
downloademacs.d-fc41568c60cea6730aca12c7c4a31a841f5cb40f.tar.gz
Rewrite bibtex-use-title-case
In addition to capitalizing important or unprotected words: - Convert unimportant words to lower case. - Allow protecting brackets to be within a word. - Always capitalize word at start of title unless protected.
Diffstat (limited to 'lisp/init-bib.el')
-rw-r--r--lisp/init-bib.el40
1 files changed, 28 insertions, 12 deletions
diff --git a/lisp/init-bib.el b/lisp/init-bib.el
index b03af20..fe8a8f1 100644
--- a/lisp/init-bib.el
+++ b/lisp/init-bib.el
@@ -34,22 +34,38 @@ the next article you read will have \"athwart\" in the title.")
(defun km/bibtex-use-title-case ()
"Convert title of current BibTeX entry to title case.
-All words except those in `km/bibtex-unimportant-title-words' are
-capitalized."
+Change words in `km/bibtex-unimportant-title-words' to lower
+case, unless the word is the first word in the title. Capitalize
+all other words unless they are protected by brackets."
(interactive)
(save-excursion
(bibtex-beginning-of-entry)
- (goto-char (car (cdr (bibtex-search-forward-field "title" t))))
- (while (not (looking-at "},"))
- ;; Not using `forward-word' because I want to capture character
- ;; before word. If "-" or "{", the word should not be capitalized.
- (re-search-forward "\\(.\\)[a-z]+")
- (let ((before-word (match-string-no-properties 1))
- (word (thing-at-point 'word)))
- (unless (or (member before-word '("-" "{"))
- (member word km/bibtex-unimportant-title-words))
+ (let* ((text-bounds (cdr (bibtex-search-forward-field "title" t)))
+ (beg (car text-bounds))
+ (end (cadr text-bounds)))
+ (goto-char (1- beg))
+ (while (re-search-forward "\\(\\W\\)\\(\\w+\\)\\(\\W\\)" end t)
+ (cond
+ ((and (string= (match-string 1) "{")
+ (string= (match-string 3) "}"))
+ ;; Go to previous character in case '}' is within the word.
+ (backward-char))
+ ;; Capitalize the first word of the title. This will fail if
+ ;; there is a space after '{'.
+ ((= (match-beginning 1) beg)
(backward-word)
- (capitalize-word 1))))))
+ (capitalize-word 1))
+ ;; Subword is separated by '-' or '{'.
+ ((or (string= (match-string 1) "-")
+ (string= (match-string 1) "}"))
+ (backward-word)
+ (downcase-word 1))
+ (t
+ (backward-word)
+ (if (member (downcase (match-string-no-properties 2))
+ km/bibtex-unimportant-title-words)
+ (downcase-word 1)
+ (capitalize-word 1))))))))
(add-hook 'bibtex-clean-entry-hook 'km/bibtex-use-title-case)