unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* title-case function
@ 2019-04-21  3:56 Paul W. Rankin
  2019-04-21  5:29 ` Emanuel Berg
                   ` (2 more replies)
  0 siblings, 3 replies; 38+ messages in thread
From: Paul W. Rankin @ 2019-04-21  3:56 UTC (permalink / raw)
  To: help-gnu-emacs

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.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(defcustom title-case-minor-words
  '("the ""a" "an" "and" "but" "for" "of" "or" "nor" "is" "as" "in" "to" 
  "into"
    "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-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-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-words)
                                  "\\b"))
              (downcase-word 1)
            (capitalize-word 1)))))))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-- 
https://www.paulwrankin.com



^ permalink raw reply	[flat|nested] 38+ messages in thread

end of thread, other threads:[~2019-04-29  3:26 UTC | newest]

Thread overview: 38+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-04-21  3:56 title-case function Paul W. Rankin
2019-04-21  5:29 ` Emanuel Berg
2019-04-21  6:40   ` Paul W. Rankin
2019-04-21 10:31     ` `if' in Elisp (was: Re: title-case function) Emanuel Berg
2019-04-21 12:34       ` Drew Adams
2019-04-21 14:02         ` Emanuel Berg
2019-04-21  5:57 ` title-case function Eli Zaretskii
2019-04-21  6:10   ` Emanuel Berg
2019-04-21  6:45   ` Paul W. Rankin
2019-04-21  7:20     ` Eli Zaretskii
2019-04-21  5:57 ` Jean-Christophe Helary
2019-04-21 12:45   ` Paul W. Rankin
2019-04-21 12:59     ` Jean-Christophe Helary
2019-04-21 13:25       ` Paul W. Rankin
2019-04-21 13:58       ` the English language part 2 (was: Re: title-case function) Emanuel Berg
2019-04-21 14:23         ` the English language part 2 Ralph Seichter
2019-04-21 14:32           ` Emanuel Berg
2019-04-21 14:42             ` Ralph Seichter
2019-04-21 15:08               ` Emanuel Berg
2019-04-21 15:21                 ` Ralph Seichter
2019-04-22  4:13                 ` Marcin Borkowski
2019-04-22  7:55                   ` Paul W. Rankin
2019-04-22 21:21                     ` Emanuel Berg
2019-04-22 21:31                   ` Emanuel Berg
2019-04-25 22:58                   ` Europe in 1648 (was: Re: the English language part 2) Emanuel Berg
2019-04-26 13:40                     ` Ralph Seichter
2019-04-27 21:16                       ` Emanuel Berg
2019-04-29  3:26                         ` Europe in 1648 Van L
2019-04-21 15:22             ` the English language part 2 Drew Adams
2019-04-21 15:38               ` Jean-Christophe Helary
2019-04-21 16:16               ` Emanuel Berg
2019-04-21 13:45     ` title-case function Emanuel Berg
2019-04-21 13:37   ` the English language, again (was: Re: title-case function) Emanuel Berg
2019-04-21 13:38   ` Emanuel Berg
2019-04-22 16:41     ` Robert Thorpe
2019-04-22 21:44       ` Emanuel Berg
2019-04-23  2:35         ` Emanuel Berg
2019-04-24 11:56           ` the English language, again Van L

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).