简体   繁体   English

一个`query-replace`,它要求每次出现一个新的替换字符串

[英]A `query-replace` that asks for a new replacement string for each occurrence

I'm looking for a function which works similar as query-replace but instead of using the same replacement string over and over again, asks for a replacement string each time a match is found. 我正在寻找一个与query-replace类似的函数,但不是一遍又一遍地使用相同的替换字符串,每次找到匹配时都要求替换字符串。 That is to say, once a match is found, you can either skip this match or enter replacement mode, where you can specify which string is to be used for replacing the match (with a history of previous replacements). 也就是说,一旦找到匹配,您可以跳过此匹配或进入替换模式,您可以在其中指定要用于替换匹配的字符串(具有先前替换的历史记录)。

Even more useful would be the query-replace-regexp variant, which allows to use the groupings of the search string. 更有用的是query-replace-regexp变体,它允许使用搜索字符串的分组。

I have written some piece of code to be used for the non-regexp version, however, I'm stuck at the part of actually replacing something and repeating the whole search after a replacement was done/the match was skipped. 我已经编写了一些代码用于非正则表达式版本,但是,我已经停留在实际替换某些内容的部分,并在替换完成后重复整个搜索/跳过匹配。 The code so far is: 到目前为止的代码是:

(defun query-replace-ask (search)
  (interactive "sQuery replace: ")
  (if (search-forward search nil t)
      (let ((beg (match-beginning 0))
            (end (match-end 0))
            (if (y-or-n-p "Replace?")
                (replacement (read-string "Replacement: "))
              (
               ;; Do the next search
               )))
        ;; Replace from beg to end with replacement
        ;; do the next search
        )))

Also, it would be nice if the function could output the number of total replacements once the function finishes. 此外,如果函数完成后函数可以输出总替换次数,那将是很好的。

This functionality is already part of query-replace . 此功能已经是query-replace一部分。 You can call it with E . 你可以用E调用它。 To see all options, press ? 要查看所有选项,请按 while replacing. 在更换时。

For query-replace-regexp , you may want to use \\? 对于query-replace-regexp ,您可能想要使用\\? in the replacement string. 在替换字符串中。 It will ask for input at every occurrence that you decide to act on, and you may combine it with other regexp features such as groupings and elisp ( \\, ). 它会在您决定采取行动的每个事件中请求输入,并且您可以将其与其他正则表达式功能(例如分组和elisp( \\, ))结合使用。 This is described in this manual page . 本手册页对此进行了描述。

Yes, the answer for query-replace is e (or E ). 是的,查询替换的答案是e (或E )。


But you might also be interested in an alternative: search with replacement on demand . 但您可能也会对替代方案感兴趣: 按需更换搜索。

In Icicles you can access search hits in any order -- visit (or revisit) just the ones you're interested in. You can see some surrounding context for choosing hits, without having to visit them. Icicles中,您可以按任意顺序访问搜索命中 - 访问(或重新访问)您感兴趣的搜索命中。您可以看到选择命中的一些周围环境,而无需访问它们。 You can visit some of them individually or cycle among some or all of them, in various cycle orders. 您可以单独访问其中一些或在各种循环订单中循环访问其中的一些或全部。

When you visit an occurrence you can hit a key to tell Icicles you want to replace text in the match, and if so whether to prompt you for new replacement text. 当您访问某个事件时,您可以点击一个键告诉Icicles您想要替换匹配中的文本,如果是,则是否提示您输入新的替换文本。

IOW, replacement is on demand. IOW,需要更换。 You need not visit all hits in a particular order (occurrence in buffer) and you are not obliged to indicate for each hit what to do (skip/replace/edit etc.). 您无需按特定顺序访问所有匹配(在缓冲区中出现),并且您没有义务为每个命中指示要做什么(跳过/替换/编辑等)。

Go directly to the matches you are interested in, and perform replacement when it is appropriate. 直接进入您感兴趣的比赛,并在适当时进行替换。 You tell Emacs what you want; 你告诉Emacs你想要什么; it does not ask you "Do you want this one? this one? this one? this one?...)" 它没有问你“你想要这个吗?这个?这个?这一个?...”

Just mentioning this as an alternative that can be powerful for some uses. 只是提到这个作为替代品,可以用于某些用途。 It is not a replacement for query-replace for all uses. 它不是所有用途的查询替换的替代品。

http://www.emacswiki.org/emacs/Icicles_-_Search_Commands%2c_Overview http://www.emacswiki.org/emacs/Icicles_-_Search_Commands%2c_Overview

Isearch+ now offers a similar on-demand replacing feature to that provided by Icicles . ISEARCH +现在提供的按需更换功能,通过提供类似的冰柱

When you visit any search hit you can use Cu CM-RET to be prompted for replacement text. 当您访问任何搜索命中时,您可以使用Cu CM-RET提示替换文本。 (Without the prefix arg, the last replacement text is reused.) (如果没有前缀arg,则重复使用最后一个替换文本。)

Replacement can also use the special regexp constructs allowed for query-replace-regexp : \\& , \\=\\N', # , \\, and \\?`. 替换也可以使用query-replace-regexp允许的特殊regexp构造: \\&\\=\\N',, \\ and \\?`。

For example, suppose you use a regexp-search pattern of \\(e\\)\\|a and a replacement pattern of \\,(if \\1 "a" "e") . 例如,假设您使用\\(e\\)\\|a的正则表达式搜索模式和\\(e\\)\\|a的替换模式\\,(if \\1 "a" "e") Each CM-RET will then swap e for a and vice versa. 然后每个CM-RETe换成a ,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM