[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 <hello@paulwrankin.com> 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
Re: title-case function, Eli Zaretskii, 2019/04/21
Re: title-case function,
Jean-Christophe Helary <=
- Re: title-case function, Paul W. Rankin, 2019/04/21
- Re: title-case function, Jean-Christophe Helary, 2019/04/21
- Re: title-case function, Paul W. Rankin, 2019/04/21
- the English language part 2 (was: Re: title-case function), Emanuel Berg, 2019/04/21
- Re: the English language part 2, Ralph Seichter, 2019/04/21
- Re: the English language part 2, Emanuel Berg, 2019/04/21
- Re: the English language part 2, Ralph Seichter, 2019/04/21
- Re: the English language part 2, Emanuel Berg, 2019/04/21
- Re: the English language part 2, Ralph Seichter, 2019/04/21
- Re: the English language part 2, Marcin Borkowski, 2019/04/22