I wrote an Elisp script to deal with some file names with English phrases. capitalize
converts "I'm a cat" to "I'M A Cat".
What's the right way to get the desired result? ("I'm A Cat") Should I use regex and write my own capitalization function?
I wrote an Elisp script to deal with some file names with English phrases. capitalize
converts "I'm a cat" to "I'M A Cat".
What's the right way to get the desired result? ("I'm A Cat") Should I use regex and write my own capitalization function?
What mode is your file in? If I open file.txt
(which according to my auto-mode-alist
makes the major mode of the file text
), then the m
after the apostrophe is not capitalized either by capitalize-region
or by capitalize-word
. That follows from the fact that the syntax class of the apostrophe is word
in text mode, so I'm
is considered a single "word", and only the I
gets capitalized.
In other modes, the syntax class of the apostrophe may be different, leading to different capitalization behavior. For example, if you do the same thing with a file called foo.py
, the file is opened in python-mode
(at least with my settings in auto-mode-alist
) and the syntax class of the apostrophe is "
(i.e. it delimits quoted strings). That makes the I
and the m
separate "words", so each gets capitalized.
To find out the syntax class of a character in your buffer, put the cursor on it and say C-u C-x =
, then look for Syntax
in the description.
To find out more about syntax tables, do C-h i g (elisp) Syntax tables
.