Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a character as a word boundary

Tags:

syntax

emacs

I've defined the \ character to behave as a word constituent in latex-mode, and I'm pretty happy with the results. The only thing bothering me is that a sequence like \alpha\beta gets treated as a single word (which is the expected behavior, of course).

Is there a way to make emacs interpret a specific character as a word "starter"? This way it would always be considered part of the word following it, but never part of the word preceding it.

For clarity, here's an example:

\alpha\beta
^          ^
1          2

If the point is at 1 and I press M-d, the string "\alpha" should be killed. If the point is at 2 and I press M-<backspace>, the string "\beta" should be killed.

How can I achieve this?

like image 522
Malabarba Avatar asked May 04 '12 02:05

Malabarba


1 Answers

Another thought:
Your requirement is very like what subword-mode provides for camelCase.

You can't customize subword-mode's behaviour -- the regexps are hard-coded -- but you could certainly copy that library and modify it for your purposes.

M-x find-library RET subword RET

That would presumably be a pretty robust solution.

Edit: updated from the comments, as suggested:

For the record, changing every instance of [[:upper:]] to [\\\\[:upper:]] in the functions subword-forward-internal and subword-backward-internal inside subword.el works great =) (as long as "\" is defined as "w" syntax).

Personally I would be more inclined to make a copy of the library than edit it directly, unless for the purpose of making the existing library a little more general-purpose, for which the simplest solution would seem to be to move those regexps into variables -- after which it would be trivial to have buffer-local modified versions for this kind of purpose.

Edit 2: As of Emacs 24.3 (currently a release candidate), subword-mode facilitates this with the new subword-forward-regexp and subword-backward-regexp variables (for simple modifications), and the subword-forward-function and subword-backward-function variables (for more complex modifications).

By making those regexp variables buffer-local in latex-mode with the desired values, you can just use subword-mode directly.

like image 63
phils Avatar answered Sep 20 '22 00:09

phils