[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: title-case function

From: Paul W. Rankin
Subject: Re: title-case function
Date: Sun, 21 Apr 2019 22:45:36 +1000
User-agent: mu4e 1.0; emacs 26.2

On Sun, Apr 21 2019, Jean-Christophe Helary wrote:
I'm not aware that we have title-case needs in French, or in any other language I know, but just as a precaution I renamed "title-case-minor-words" to "title-case-minor-english-words".

This seems a little redundant to me if title case is an English-only thing. But also it's a defcustom, so the user is free to make the words any other language (which would then make the name quite silly).

That said, anyone pasting this into their init file is more than welcome to rename the option whatever they like!

A slight improvement below. Function title-case works on a string object, title-case-region works on the region. Using a temp buffer is a bit annoying though...

(defcustom title-case-minor-words
'("the ""a" "an" "and" "but" "for" "of" "or" "nor" "is" "as" "at" "in" "to"
   "v" "vs" "de")
 "List of minor word strings that should be downcased in titles.
These words should be less than four characters."
 :type '(repeat string)
 :group 'editing-basics)

(defun title-case (string)
 "Convert STRING to Title Case.
First and last words are capitalized unconditionally, as are
words following colons, en dashes and em dashes. Words in list
`title-case-minor-words' are downcased."
   (insert string)
   (let (last-word)
     (goto-char (point-max))
     (forward-word -1)
     (setq last-word (point))
     (capitalize-word 1)
     (goto-char (point-min))
     (capitalize-word 1)
     (while (< (point) last-word (point-max))
       (if (looking-at "[:\x2013\x2014]")
           (capitalize-word 1)
         (skip-syntax-forward "-." last-word)
(if (looking-at (concat "\\b" (regexp-opt title-case-minor-words)
             (downcase-word 1)
           (capitalize-word 1)))))

(defun title-case-region (beg end)
 "Convert region from BEG to END to Title Case."
 (interactive "r")
   (goto-char end)
   (unless (looking-at "\\b")
     (forward-word 1)
     (setq end (point)))
   (goto-char beg)
   (unless (looking-at "\\b")
     (forward-word -1))
   (title-case (buffer-substring (point) end))))


reply via email to

[Prev in Thread] Current Thread [Next in Thread]