如何修改 emacs'搜索和替换来执行更复杂的任务?
这里完全是 Emacs 菜鸟。现在我正在 Emacs 中开发一个相当大的 LaTeX 项目,其中有几个地方需要使用 makeidx 包来索引一些单词。因为我还希望索引单词变为粗体,所以我创建了自己的命令 \ind{}
,这将使参数变为粗体并建立索引。但现在我对此命令不满意,因此我想默认将文本中 \ind{whatever}
的每个实例更改为 \textbf{whatever}\index{whatever }
。
问题是我确切地知道我想要什么:
- 浏览文本,查找
\ind{
的任何实例,并使用search-and 替换为
\textbf{
-replace - 将
\ind
的参数(在本例中为“任意”)保存在内存中 - 问我用户
\index
的参数应该是什么。默认情况下(按回车键),它应该是第一个参数,但我也可以改变主意并输入不同的内容(在本例中为“默认情况下的任何内容”)。如果没有输入(例如只有一个空格" "
),则停止程序。 - 记下
\index{
、新参数和}
。 - 转到文本中的下一个出现位置。
但是,唉!我不知道如何实现这一点,所以我需要有人的帮助。如果需要花费太多时间来解释如何做这样的事情,您能给我发送一些关于编写自己的函数的教程吗?
我希望我说得清楚,感谢您的耐心等待!
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 :
- Go through the text, look for any instance of
\ind{
and replace by\textbf{
usingsearch-and-replace
- Save the argument of
\ind
("whatever" in this case) in memory - 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. - Write down
\index{
, the new argument and}
. - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这种方法对我来说似乎有点不正统,但它有效并且似乎足以完成一次性工作......
在
replace-regexp
和query-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
andquery-replace-regexp
(C-M-%), one newer escape sequence is\,(...)
, where...
can be any Lisp expression. There's a Lisp functionread-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.Vlad 为您提供了 LaTeX 问题的答案。 Emacs 解决方案是关键宏:首先
定义一个新宏,然后执行一步更改,例如:
然后将参数复制并粘贴到
\textbf
宏中...小心地以可重复的方式移动。标准修改完成后,将光标放在whatever by default
之后并结束定义,此时Cx e
将调用您刚刚定义的宏,让光标位于更改您想要更改的部分的正确位置您也可以重复e
一次多次调用宏。Vlad give you the LaTeX answer to your problem. An Emacs solution is the key-macro: start with
to define a new macro, then do one step of your change, say:
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 thewhatever by default
and end the definition bynow
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 thee
to call the macro several time at once.为什么不重新定义
\ind
以便它可以获得可选参数?例如:
这样您可以使用
\ind{whatever}
或\ind[whatever-else]{whatever}
。Why not just redefine the
\ind
so that it can get an optional argument?For example:
This way you can use
\ind{whatever}
or\ind[whatever-else]{whatever}
.