如何修改 emacs'搜索和替换来执行更复杂的任务?

发布于 2024-10-23 18:36:36 字数 773 浏览 4 评论 0原文

这里完全是 Emacs 菜鸟。现在我正在 Emacs 中开发一个相当大的 LaTeX 项目,其中有几个地方需要使用 makeidx 包来索引一些单词。因为我还希望索引单词变为粗体,所以我创建了自己的命令 \ind{} ,这将使参数变为粗体并建立索引。但现在我对此命令不满意,因此我想默认将文本中 \ind{whatever} 的每个实例更改为 \textbf{whatever}\index{whatever }

问题是我确切地知道我想要什么:

  1. 浏览文本,查找 \ind{ 的任何实例,并使用 search-and 替换为 \textbf{ -replace
  2. \ind 的参数(在本例中为“任意”)保存在内存中
  3. 问我用户 \index 的参数应该是什么。默认情况下(按回车键),它应该是第一个参数,但我也可以改变主意并输入不同的内容(在本例中为“默认情况下的任何内容”)。如果没有输入(例如只有一个空格 " "),则停止程序。
  4. 记下 \index{、新参数和 }
  5. 转到文本中的下一个出现位置。

但是,唉!我不知道如何实现这一点,所以我需要有人的帮助。如果需要花费太多时间来解释如何做这样的事情,您能给我发送一些关于编写自己的函数的教程吗?

我希望我说得清楚,感谢您的耐心等待!

total Emacs noob here. So right now I'm working on a fairly big LaTeX project in Emacs in which there are couple of places where I need to index some words, using the makeidx package. Because I also wanted indexed words to be bold, I created my own command \ind{} which would make the argument go bold and indexed. But right now I'm dissatisifed with this command so I'd like to change every instance of \ind{whatever} in my text by \textbf{whatever}\index{whatever by default}.

The thing is I know exactly what I want :

  1. Go through the text, look for any instance of \ind{ and replace by \textbf{ using search-and-replace
  2. Save the argument of \ind ("whatever" in this case) in memory
  3. Ask me the user what should the argument of \index be. By default (by striking enter), it should be the first argument, but I can also change my mind and enter something different ("whatever by default" in this case). If there's no input (only a space " " for example) stop the program.
  4. Write down \index{, the new argument and }.
  5. Go to next occurance in the text.

But, alas!, I know not how to achieve this, so I need someone's help. If it should take too much time to explain how to do such a thing, would you please send me some tutorial about writing my own functions?

I hope I'm being clear, and thanks for your patience!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

悲凉≈ 2024-10-30 18:36:36

这种方法对我来说似乎有点不正统,但它有效并且似乎足以完成一次性工作......

replace-regexpquery-replace-regexp 的替换文本中> (CM-%),一种较新的转义序列是 \,(...),其中 ... 可以是任何 Lisp 表达式。有一个 Lisp 函数 read-from-minibuffer 可以读取用户输入的任意文本,并带有可选的默认值。因此:

CM-%:启动query-replace-regexp

\\ind{\([^}]+?\)}:要搜索的模式。

\\textbf{\1}\\index{\,(read-from-minibuffer "index content? " \1)}:替换文本。系统将提示用户将文本放入 \index{} 元素后面的大括号中,使用 \ind{} 元素后面的大括号之间的原始文本作为默认值。

请注意,使用 query-replace-regexp 时,您必须在每个选项后键入 y 来确认每个选项。如果您想避免此步骤,请使用 Mx Replace-regexp

This approach seems vaguely unorthodox to me, but it works and seems sufficient for a one-off job...

In the replacement text for replace-regexp and query-replace-regexp (C-M-%), one newer escape sequence is \,(...), where ... can be any Lisp expression. There's a Lisp function read-from-minibuffer which reads arbitrary text typed by the user, with an optional default. Therefore:

C-M-%: Start query-replace-regexp.

\\ind{\([^}]+?\)}: The pattern to search for.

\\textbf{\1}\\index{\,(read-from-minibuffer "index content? " \1)}: The replacement text. The user will be prompted for the text to put in the braces following the \index{} element, using the original text between the braces following the \ind{} element as a default.

Note that when using query-replace-regexp, you'll have to confirm each choice by typing y after each. Use M-x replace-regexp if you want to avoid this step.

放肆 2024-10-30 18:36:36

Vlad 为您提供了 LaTeX 问题的答案。 Emacs 解决方案是关键宏:首先

C-x (

定义一个新宏,然后执行一步更改,例如:

C-s \ind{
<left>ex

然后将参数复制并粘贴到 \textbf 宏中...小心地以可重复的方式移动。标准修改完成后,将光标放在whatever by default之后并结束定义,此时

C-x )

Cx e将调用您刚刚定义的宏,让光标位于更改您想要更改的部分的正确位置您也可以重复e一次多次调用宏。

Vlad give you the LaTeX answer to your problem. An Emacs solution is the key-macro: start with

C-x (

to define a new macro, then do one step of your change, say:

C-s \ind{
<left>ex

Then copy and paste the argument in the \textbf macro... You have to be careful to move in a way that will be repeatable. Once the standard modification is done, you let the cursor after the whatever by default and end the definition by

C-x )

now C-x e will call the macro you just define, letting your cursor at the correct place to change the part you want to change You can also repeat the e to call the macro several time at once.

天暗了我发光 2024-10-30 18:36:36

为什么不重新定义 \ind 以便它可以获得可选参数?

例如:

\newcommand{\ind}[2][]{%
  \def\first{#1}%
  \ifx\first\empty
     \textbf{#2}\index{#2}%
  \else
     \textbf{#2}\index{#1}%
  \fi
}

这样您可以使用 \ind{whatever}\ind[whatever-else]{whatever}

Why not just redefine the \ind so that it can get an optional argument?

For example:

\newcommand{\ind}[2][]{%
  \def\first{#1}%
  \ifx\first\empty
     \textbf{#2}\index{#2}%
  \else
     \textbf{#2}\index{#1}%
  \fi
}

This way you can use \ind{whatever} or \ind[whatever-else]{whatever}.

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