Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a \string with a non-backslashed string in emacs abbrev mode?

My question is motivated by writing TeX in emacs, but is unrelated to the TeX system itself.

My goal is to set up abbreviations in emacs to replace some (TeX) macros with their unicode characters, for example \alpha with α automatically while I'm typing TeX in emacs.

I planned to achieve this using the abbrev system. While the following abbreviation works fine (replaces test with \test):

("test" "\\test" nil 0)

The abbreviations that could do what I want, i.e.:

("\\alpha" "A" nil 0)

or

("\\\\beta" "B" nil 0)

are not applying when I type \alpha or \beta.

(I could sidestep this by replacing "alpha" itself and erasing one preceding character, but I would like to preserve the option of typing alpha without the backslash and not applying this abbrev rule.)

What is the correct form of the abbreviation that replaces \alpha with α?

like image 747
M. B. Avatar asked Mar 23 '23 18:03

M. B.


1 Answers

You need to change the property :regexp of your abbrev table.

(abbrev-table-put <your-abbrev-table> :regexp "\\(\\\\[a-z0-9@]+\\)")

The only problem is, in a regexp that is matched looking backwards, you can't have optional characters at the beginning. So, either you change the syntax category of \ (but then you don't need any special handling of abbrev tables), or you have to choose to have your all abbrevs in this table start with or without a backslash.

But why don't you use an input method to insert unicode characters?

like image 84
angus Avatar answered Apr 27 '23 18:04

angus