我怎么可以交换或在同一时间更换code多串? [英] How can I swap or replace multiple strings in code at the same time?

查看:123
本文介绍了我怎么可以交换或在同一时间更换code多串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下code样品:<​​/ P>

  uint8_t有我在,妮;
I =在= 2; NI = 1;
而(2 == I&的Ni)在++;

我怎么能代替我在和Ni ,分别为中,Ni和我 INNI,ININ和妮妮使用emacs,六,* nix的命令,或者别的什么吗?


解决方案

如果我没有记错,提供的解决方案至今(使用Perl和VIM)当任何替代的是后者的话中无法正常工作来代替。特别是,没有解决方案的工作原理的第一个例子:i的将与中,这随后将与に,然后返回到被错误地替换被替换i的被随后的规则,而它应该保持为在。

的取代不能认为独立且连续地施加;它们应并联应用

在Emacs中,你可以这样做:

M-X 并行替换

和在提示符下,输入

<大骨节病>我在倪妮我

如果被选择的一个的替代将光标和缓冲区的端部之间发生,或在一个区域

(只要你有这个定义你的〜/ .emacs.d / init.el : - )

(需要'CL)
(defun函数并行取代(的plist&安培;可选的始端)
  (互动
   `((环带输入=(读 - 从 - 迷你缓冲区替换)
            与极限=(长度输入)
            对于(项目。指数)=(读 - 从字符串输入0)
                            然后(读 - 从字符串输入指数)
            收集(prin1到字符串的项目t),直到(小于=限制指数))
     ,@(如果(使用区域-P)`((区域开头),(区域末端)))))
  (让*((ALIST(环路上的plist(VAL的关键。尾)由#'CDDR
                      收集(利弊关键VAL)))
         (匹配(正则表达式-OPT(mapcar#'车ALIST)的话)))
    (节省冲程
      (转到-CHAR(或启动(点)))
      (而(重新搜索前进匹配(或结束(点-MAX))T)
        (替换匹配(CDR(assoc命令字符串(匹配字符串0)ALIST)))))))

编辑(二零一三年八月二十零日):

有一些改进:


  1. 对于那些只给出了两个项目的特殊情况,进行交换,而不是(即互相替换);

  2. 要求确认在相同的方式每次更换为查询替换

(需要'CL)
(defun函数并行查询取代(的plist&安培;可选分隔的起始端)
  替换(2N)的每一次出现在第PLIST的令牌
与第(2n + 1)的令牌缓冲;如果仅提供了两个标记,
与对方(即交换它们)取代它们。如果第二个可选参数DELIMITED为零,比赛的话
根据语法表;否则匹配符号。在交互调用,PLIST输入为空格分隔
令牌和划界为preFIX ARG。
  (互动
   `((环带输入=(读 - 从 - 迷你缓冲区替换)
            与极限=(长度输入)
            对于j = 0,那么我
            对于(项目I)=(读 - 从字符串输入j)条
            收集(prin1到字符串的项目t),直到(小于=限制i))
     ,电流 - preFIX精氨酸
     ,@(如果(使用区域-P)`((区域开头),(区域末端)))))
  (让*((ALIST(条件((=(长的plist)2)(名单的plist(反向的plist)))
                      ((环由#上的plist(VAL的关键。尾)CDDR
                             收集(列表(prin1到字符串键T)VAL)))))
         (匹配(正则表达式-OPT(mapcar#'车ALIST)
                              (如果分隔'字'符号)))
         (以规格`(更换-EVAL-更换更换引号
                    (CADR(assoc命令字符串(匹配字符串0),ALIST
                                        案例倍,搜索)))))
    (查询替换,正则表达式匹配器规格零开始结束)))

Given the following code sample:

uint8_t i, in, ni;
i = in = 2; ni = 1;
while (2 == i > ni) in++;

How can I replace i, in, and ni, respectively with either in, ni, and i or inni, inin, and nini using emacs, vi, *nix commands, or anything else?

解决方案

If I'm not mistaken, the solutions provided so far (using Perl and Vim) do not work correctly when any of the replacements is among the latter words to be replaced. In particular, none of the solutions works for the first example: "i" would be replaced with "in", which would then be incorrectly replaced with "ni" and then back to "i" by the subsequent rules, while it should stay as "in".

The substitutions cannot be assumed independent and applied in succession; they should be applied in parallel.

In Emacs, you can do this:

M-xparallel-replace,

and at the prompt, enter

i in in ni ni i.

The replacements will happen between the cursor and the end of buffer, or in a region if one is selected.

(Provided you have this definition in your ~/.emacs.d/init.el:-)

(require 'cl)
(defun parallel-replace (plist &optional start end)
  (interactive
   `(,(loop with input = (read-from-minibuffer "Replace: ")
            with limit = (length input)
            for (item . index) = (read-from-string input 0)
                            then (read-from-string input index)
            collect (prin1-to-string item t) until (<= limit index))
     ,@(if (use-region-p) `(,(region-beginning) ,(region-end)))))
  (let* ((alist (loop for (key val . tail) on plist by #'cddr
                      collect (cons key val)))
         (matcher (regexp-opt (mapcar #'car alist) 'words)))
    (save-excursion
      (goto-char (or start (point)))
      (while (re-search-forward matcher (or end (point-max)) t)
        (replace-match (cdr (assoc-string (match-string 0) alist)))))))

Edit(2013-08-20):

A few enhancements:

  1. For the special case that only two items are given, perform a swap instead (ie, replace with each other);
  2. Ask for confirmation for each replacement in the same manner as query-replace.

(require 'cl)
(defun parallel-query-replace (plist &optional delimited start end)
  "Replace every occurrence of the (2n)th token of PLIST in
buffer with the (2n+1)th token; if only two tokens are provided,
replace them with each other (ie, swap them).

If optional second argument DELIMITED is nil, match words
according to syntax-table; otherwise match symbols.

When called interactively, PLIST is input as space separated
tokens, and DELIMITED as prefix arg."
  (interactive
   `(,(loop with input = (read-from-minibuffer "Replace: ")
            with limit = (length input)
            for  j = 0 then i
            for (item . i) = (read-from-string input j)
            collect (prin1-to-string item t) until (<= limit i))
     ,current-prefix-arg
     ,@(if (use-region-p) `(,(region-beginning) ,(region-end)))))
  (let* ((alist (cond ((= (length plist) 2) (list plist (reverse plist)))
                      ((loop for (key val . tail) on plist by #'cddr
                             collect (list (prin1-to-string key t) val)))))
         (matcher (regexp-opt (mapcar #'car alist)
                              (if delimited 'words 'symbols)))
         (to-spec `(replace-eval-replacement replace-quote
                    (cadr (assoc-string (match-string 0) ',alist
                                        case-fold-search)))))
    (query-replace-regexp matcher to-spec nil start end)))

这篇关于我怎么可以交换或在同一时间更换code多串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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