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?

Drew
  • 75,699
  • 9
  • 109
  • 225
sixter
  • 113
  • 7
  • 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 '21 at 13:06
  • https://emacs.stackexchange.com/tags/elisp/info – Drew Apr 24 '21 at 20:21

1 Answers1

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.

NickD
  • 27,023
  • 3
  • 23
  • 42
  • 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 '21 at 06: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 '21 at 06:06
  • I figure it out. Thanks. I used this: `(modify-syntax-entry ?\' "w")` – sixter May 26 '21 at 06:24