1

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?

2
  • 3
    capitalize forwards to the next word, so the issue is probably with the syntax table for the current mode, where word boundaries are specified.
    – Juancho
    Apr 24, 2021 at 13:06
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Apr 24, 2021 at 20:21

1 Answer 1

2

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.

3
  • Interesting. I understand your point. I was reading a string from the minibuffer prompt using the interactive code letter M and then passing it to the capitalize function. This is inside an elisp utility function that I wrote.
    – sixter
    May 26, 2021 at 6:03
  • This is what I get: syntax: ' which means: prefix for apostrophe in emacs-lisp-mode. How do I get it to work? This is the piece of code where it happens: (string-join (mapcar #'capitalize (split-string title " ")) "_")
    – sixter
    May 26, 2021 at 6:06
  • I figure it out. Thanks. I used this: (modify-syntax-entry ?\' "w")
    – sixter
    May 26, 2021 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.