From 621647663373780b54939bf58bc8c06beaa67ed6 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Sat, 12 Mar 2016 22:11:38 -0500 Subject: 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. --- lisp/km-bib.el | 55 +++++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) (limited to 'lisp/km-bib.el') 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." -- cgit v1.2.3