help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: title-case function


From: Jean-Christophe Helary
Subject: Re: title-case function
Date: Sun, 21 Apr 2019 14:57:36 +0900


> On Apr 21, 2019, at 12:56, Paul W. Rankin <address@hidden> wrote:
> 
> Happy Easter to those who celebrate it!
> 
> I couldn't find a title-case function I considered good enough, so I wrote 
> the one below. Please take a look and let me know if there are any edge cases 
> I've missed or improvements you might have.

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".

The day we have proper localization in Emacs we'll probably have a use for that 
:)

> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(defcustom title-case-minor-english-words
'("the ""a" "an" "and" "but" "for" "of" "or" "nor" "is" "as" "in" "to"  "into"
  "v" "vs" "de")
"List of minor English 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-region (beg end)
"Convert region from BEG to END to Title Case.
First and last words are capitalized unconditionally, as are
words following colons and dashes. Words in list
`title-case-minor-english-words' are downcased."
(interactive "r")
(save-excursion
  (let (last-word)
    (goto-char end)
    (forward-word -1)
    (setq last-word (point))
    (capitalize-word 1)
    (goto-char beg)
    (unless (looking-at "\\b")
      (forward-word -1))
    (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-english-words)
                                "\\b"))
            (downcase-word 1)
          (capitalize-word 1)))))))


Jean-Christophe Helary
-----------------------------------------------
http://mac4translators.blogspot.com @brandelune





reply via email to

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