如何使用 Emacs 同时修改两个匹配的分隔符?
虽然这个问题涉及 Emacs(也可能是 Auctex)中 LaTeX 的格式,但我相信这可以应用于 Emacs 中有关括号、方括号和大括号等分隔符的更一般情况。
我希望能够使用 Emacs(和 elisp)执行以下操作,但不知道从哪里开始。假设我有:
(This is in parentheses)
通过 Emacs 中的一些键绑定,我希望 Emacs 找到与我的光标所在的任何一个匹配的分隔符(我知道 Emacs 可以做到这一点,因为它可以在各种模式下突出显示匹配的分隔符)并能够更改它们 /
\left( This is in parentheses \right)
我希望使用的分隔符是: (...)
, [...]
, \lvert ... \rvert< 代码>, <代码>\langle ... \rangle
,\{ ... \}
。我需要什么 elisp 来完成这项任务?
欢迎使用更通用的方法来处理匹配分隔符。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Emacs 中评估以下命令。重新加载后,您可以将点(文本光标)立即放在右括号之后。然后执行
Mx Replace-matching-parens
将结束的)
替换为\right)
和匹配的开始 paren(
与\left(
。interactive
位表示我想要一个“命令”,因此可以使用Mx
执行它。为了避免执行后光标最终出现在一个奇怪的地方我将逻辑包装在其中save-excursion
。该点使用backward-list
跳转回起始括号,并保留括号匹配区域的开始和结束位置。通过向后而不是向前替换字符串,我可以避免在需要它之前使端点无效。
backward-list
应该适用于 emacs 识别为(
和)
类似的任何字符串对。请查看这篇括号匹配文章中的set-syntax-table
。使用
global-set-key
设置与
replace-matching-parens
的键绑定。公平警告:
replace-matching-parens
是我实现的第一个 elisp 命令,因此它可能与最佳实践不符。对于所有的专家,我愿意接受建设性的批评。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-parens
to replace the closing)
with\right)
and the matching start paren(
with\left(
.The
interactive
bit 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-list
and 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-point
before I need it.Generalizing this to handle different kinds of delimiters shouldn't be too bad.
backward-list
ought to work with any pair of strings emacs recognizes as analogues of(
and)
. To add more parenthesis-like string pairs, check outset-syntax-table
in this Parenthesis Matching article.Use
global-set-key
to setup a key binding toreplace-matching-parens
.Fair warning:
replace-matching-parens
is 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.