While this question concerns the formatting of LaTeX within Emacs (and maybe Auctex), I believe this can be applied to more general situations in Emacs concerning delimiters like parentheses, brackets, and braces.
I am looking to be able to do the following with Emacs (and elisp), and don’t know where to begin. Say I have:
(This is in parentheses)
With some keybinding in Emacs, I want Emacs to find the matching delimiter to whichever one is by my cursor (something I know Emacs can do since it can highlight matching delimiters in various modes) and be able to change both of them to
\left( This is in parentheses \right)
The delimiters I would like this to work with are: (...), [...], \lvert ... \rvert, \langle ... \rangle, \{ ... \}. What elisp would I need to accomplish this task?
More general ways to handle matching delimiters are welcome.
Evaluate the command below in Emacs. After reloading you can put the point (text cursor) immediately after a closing paren. Then do
M-x replace-matching-parensto replace the closing)with\right)and the matching start paren(with\left(.The
interactivebit indicates that I want a “command”, so it can be executed usingM-x. To avoid the cursor ending up in a strange place after execution I’m wrapping the logic insave-excursion. The point jumps back to the opening paren usingbackward-listand holds on to the start and end positions of the paren-matched region. Lastly, starting at the end and working backwards I replace the strings. By replacing backwards rather than forwards I avoid invalidatingend-pointbefore I need it.Generalizing this to handle different kinds of delimiters shouldn’t be too bad.
backward-listought to work with any pair of strings emacs recognizes as analogues of(and). To add more parenthesis-like string pairs, check outset-syntax-tablein this Parenthesis Matching article.Use
global-set-keyto setup a key binding toreplace-matching-parens.Fair warning:
replace-matching-parensis the first elisp command I’ve implemented, so it may not align with best practices. To all the gurus out there, I’m open to constructive criticism.