Emacs:正则表达式替换为更改大小写(在脚本中) [英] Emacs: regular expression replacing to change case (in scripts)

查看:221
本文介绍了Emacs:正则表达式替换为更改大小写(在脚本中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与
有关 Emacs:正则表达式替换为更改case



我的另外一个问题是我需要脚本搜索替换,但是\,()解决方案仅在交互式使用时(对我来说)(emacs 24.2.1)。在脚本中,它会显示错误:替换文本中使用 \'



我通常在需要加载的文件中写入执行替换。例如:

(执行替换< \\([^>] + \\)>< \\ \\\,(downcase \1)> tt nil 1 nil(point-min)(point-max))



应该可以调用一个函数来生成替换(emacs lisp手册的第741页),但是我尝试了许多以下的变体,没有运气: ($)

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ (执行 - 替换...(myfun。())tt nil)

解决方案

构造如 \,()只允许交互式调用 query-replace ,这就是为什么Emacs抱怨你的情况。



perform-replace 提到你不应该在elisp代码中使用它,并提出一个更好的选择,我们可以在其上构建以下代码:

 (while(re-search-forward< \\([^>] + \\)>nil t)
(replace-match(downcase(match-string 0) )t nil))






如果你还想使用 perform-replace 交互地查询用户关于替换,就像您所做的一样,可能是正确的事情。您的代码中有一些不同的问题:


  1. elisp手册替换函数必须使用两个参数(您在cons单元中提供的数据和


  2. query-replace-regexp (或 elisp手册),您需要确保 case-fold-search case-replace 设置为不要将案例模式转移到替换。


  3. 您需要引用cons单元格(myfun。nil),否则将被解释为一个函数调用,并且评估太早。


一个工作版本:

 (let((case-fold-search nil))
(perform-replace< \\([^>] + \\)>
`(,(lambda(data count)
(downcase(match-string 0))))
tt nil))


This is related to Emacs: regular expression replacing to change case

My additional problem is that I need to script the search-replace but the "\,()" solution works (for me) only when used interactively (emacs 24.2.1). Inside a script it gives the error: "Invalid use of \' in replacement text".

I usually write a "perform-replace" to some file to be loaded when needed. Something like:

(perform-replace "<\\([^>]+\\)>" "<\\,(downcase \1)>" t t nil 1 nil (point-min) (point-max))

It should be possible to call a function to generate the replacement (pg 741 of the emacs lisp manual), but I've tried many variations of the following with no luck:

(defun myfun ()
    (downcase (match-string 0)))

(perform-replace "..." (myfun . ()) t t nil)

Can anyone help?

解决方案

Constructs like \,() are only allowed in interactive calls to query-replace, which is why Emacs complains in your case.

The documentation of perform-replace mentions that you should not use it in elisp code and proposes a better alternative, upon which we can build the following code:

(while (re-search-forward "<\\([^>]+\\)>" nil t)
  (replace-match (downcase (match-string 0)) t nil))


If you still want to interactively query the user about the replacements, using perform-replace like you did is probably the right thing to do. There were a few different problems in your code:

  1. As stated in the elisp manual the replacement function must take two arguments (the data you provide in the cons cell and the number of replacements already made).

  2. As stated in the documentation of query-replace-regexp (or the elisp manual), you need to ensure that case-fold-search or case-replace is set to nil so that the case pattern is not transferred to the replacement.

  3. You need to quote the cons cell (myfun . nil), otherwise it will be interpreted as a function call and evaluated too early.

Here is a working version:

(let ((case-fold-search nil))
  (perform-replace "<\\([^>]+\\)>"
                   `(,(lambda (data count)
                       (downcase (match-string 0))))
                   t t nil))

这篇关于Emacs:正则表达式替换为更改大小写(在脚本中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆