[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."
(with-temp-buffer
(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)
"\\b"))
(downcase-word 1)
(capitalize-word 1)))))
(buffer-string)))
(defun title-case-region (beg end)
"Convert region from BEG to END to Title Case."
(interactive "r")
(save-excursion
(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))))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
https://www.paulwrankin.com
- Re: title-case function, (continued)
Re: title-case function, Eli Zaretskii, 2019/04/21
Re: title-case function, Jean-Christophe Helary, 2019/04/21
- Re: title-case function,
Paul W. Rankin <=
- 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
- Re: the English language part 2, Paul W. Rankin, 2019/04/22