summaryrefslogtreecommitdiff
path: root/lisp/km-bib.el
diff options
context:
space:
mode:
authorKyle Meyer <kyle@kyleam.com>2016-03-12 22:11:38 -0500
committerKyle Meyer <kyle@kyleam.com>2016-03-12 22:11:38 -0500
commit621647663373780b54939bf58bc8c06beaa67ed6 (patch)
tree699ed86f1ec9f4aeb5dffcf8410ad6e8d3f3d065 /lisp/km-bib.el
parente29293b313d6dae4066ffafd9b80695acb290526 (diff)
downloademacs.d-621647663373780b54939bf58bc8c06beaa67ed6.tar.gz
Rewrite bibtex-use-title-case
Unlike the previous version, this doesn't take a stance on a word after a hyphen. I'm ok with this because this function always requires a check afterwards and is just meant to take care of the common cases.
Diffstat (limited to 'lisp/km-bib.el')
-rw-r--r--lisp/km-bib.el55
1 files changed, 27 insertions, 28 deletions
diff --git a/lisp/km-bib.el b/lisp/km-bib.el
index c94cc35..360e08a 100644
--- a/lisp/km-bib.el
+++ b/lisp/km-bib.el
@@ -36,34 +36,33 @@ case, unless the word is the first word in the title. Capitalize
all other words unless they are protected by brackets."
(save-excursion
(bibtex-beginning-of-entry)
- (let* ((bounds (bibtex-search-forward-field "title" t))
- (beg (bibtex-start-of-text-in-field bounds)))
- (goto-char (1- beg))
- (while (re-search-forward "\\(\\W\\)\\(\\w+\\)\\(\\W\\)"
- (bibtex-end-of-text-in-field bounds) t)
- (cond
- ((and (string= (match-string 1) "{")
- (string= (match-string 3) "}"))
- ;; Go to previous character in case '}' is within the word.
- (backward-char))
- ;; Leave commands alone.
- ((string= (match-string 1) "\\"))
- ;; 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))
- ;; 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))))))))
+ (let ((bounds (bibtex-search-forward-field "title" t)))
+ (when bounds
+ (let* ((beg (1+ (bibtex-start-of-text-in-field bounds)))
+ (end (1- (bibtex-end-of-text-in-field bounds)))
+ (title-words (split-string
+ (buffer-substring-no-properties beg end)))
+ (cap-if-letter
+ (lambda (word)
+ (let ((case-fold-search nil))
+ (if (string-match-p "\\`[a-z]" word)
+ (capitalize word)
+ word))))
+ (choose-case
+ (lambda (word)
+ (funcall (if (member (downcase word)
+ km/bibtex-unimportant-title-words)
+ #'downcase
+ cap-if-letter)
+ word))))
+ (goto-char beg)
+ (delete-region beg end)
+ (insert (mapconcat
+ #'identity
+ (cons (funcall cap-if-letter (car title-words))
+ (mapcar choose-case (cdr title-words)))
+ " "))
+ (fill-paragraph))))))
(defun km/bibtex-single-space-author-list ()
"Convert multiple spaces in author list to single space."