如何使用 Emacs 同时修改两个匹配的分隔符?

发布于 2024-12-22 02:00:40 字数 567 浏览 0 评论 0原文

虽然这个问题涉及 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回心转意 2024-12-29 02:00:40

在 Emacs 中评估以下命令。重新加载后,您可以将点(文本光标)立即放在右括号之后。然后执行 Mx Replace-matching-parens 将结束的 ) 替换为 \right) 和匹配的开始 paren (\left(

(defun replace-matching-parens ()
  (interactive)
  (save-excursion
    (let ((end-point (point)))
      (backward-list)
      (let ((start-point (point)))
        (goto-char end-point)
        (re-search-backward ")" nil t)
        (replace-match " \\\\right)" nil nil)

        (goto-char start-point)
        (re-search-forward "(" nil t)
        (replace-match "\\\\left( " nil nil)))))

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(.

(defun replace-matching-parens ()
  (interactive)
  (save-excursion
    (let ((end-point (point)))
      (backward-list)
      (let ((start-point (point)))
        (goto-char end-point)
        (re-search-backward ")" nil t)
        (replace-match " \\\\right)" nil nil)

        (goto-char start-point)
        (re-search-forward "(" nil t)
        (replace-match "\\\\left( " nil nil)))))

The interactive bit indicates that I want a "command", so it can be executed using M-x. To avoid the cursor ending up in a strange place after execution I'm wrapping the logic in save-excursion. The point jumps back to the opening paren using backward-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 invalidating end-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 out set-syntax-table in this Parenthesis Matching article.

Use global-set-key to setup a key binding to replace-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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文