如何在Emacs或Vim中分发字符串

如何在Emacs或Vim中分发字符串,vim,emacs,Vim,Emacs,在Emacs或Vim中,连接字符串的平滑方式是什么,如本例所示: Transform from: (alpha, beta, gamma) blah (123, 456, 789) To: (alpha=123, beta=456, gamma=789) 转换自: (阿尔法,贝塔,伽马)诸如此类(123456789) 致: (α=123,β=456,γ=789) 它需要扩展到: 这里面有很多行 括号中有许多元素 我最近发现自己经常需要这种转变 我在Emacs中使用邪恶,这就是

在Emacs或Vim中,连接字符串的平滑方式是什么,如本例所示:

Transform from: (alpha, beta, gamma) blah (123, 456, 789) To: (alpha=123, beta=456, gamma=789) 转换自: (阿尔法,贝塔,伽马)诸如此类(123456789) 致: (α=123,β=456,γ=789) 它需要扩展到:

  • 这里面有很多行
  • 括号中有许多元素
我最近发现自己经常需要这种转变

我在Emacs中使用邪恶,这就是为什么Vim答案可能也会有帮助的原因

更新:

解决办法并不像我希望的那样笼统。例如,当我有一个字符串列表并希望将它们分发到一个大型XML文档中时,我希望该解决方案也能起作用。例如:

<item foo="" bar="barval1"/>
<item foo="" bar="barval2"/>
<item foo="" bar="barval3"/>
<item foo="" bar="barval4"/>

fooval1
fooval2
fooval3
fooval4

食品1
食品2
食品3
食品4
我制定了一个解决方案,并将其添加为答案

%s/(\(\S\{-}\), \(\S\{-}\), \(\S\{-}\)).\{-}(\(\S\{-}\), \(\S\{-}\), \(\S\{-}\))/(\1=\4, \2=\5, \3=\6)
%s
:全局搜索和替换

\(\S{-}\,
:非贪婪搜索非空白字符,直到下一个逗号,用“(”括起来用于反向引用)


\1=\4
:打印出第一个匹配,一个“=”符号,然后是第四个匹配

这是一个Vimscript解决方案。它远没有ash的答案那么优雅,但它可以处理任意长度的列表

function! ListMerge()
    " Get line, remove text between lists, split lists at parentheses:
    let curline = getline('.')
    let curline = substitute(curline,')\zs.*\ze(','','g')
    let curline = substitute(curline,'(','','g')
    let lists = map(split(curline,')'),'split(v:val,",")')
    " Return if we don't have two lists of equal length:
    if len(lists) != 2 || len(lists[0]) != len(lists[1])
        return
    endif
    " Loop over the lists, remove whitespace, build the replacement string:
    let i=0
    let string = '('
    while i<len(lists[0])
        let string .= substitute(lists[0][i],'^ *','','')
        let string .= '='
        let string .= substitute(lists[1][i],'^ *','','')
        let string .= ', '
        let i+=1
    endwhile
    " Add the concluding bracket:
    let string = substitute(string,', $',')','')
    " Replace the current line with the string:
    execute "normal! S" . string
endfunction

对于这种文本转换,我将使用awk:

这一行可能有助于:

awk -F'\\(|\\)' '{split($2,t,",");split($4,v,",");printf "( "; for(x in t)s=s""sprintf("%s=%s, ", t[x],v[x]);sub(", $","",s);printf s")\n";s=""}' file
小测试:

kent$  cat test
(alpha, beta, gamma) blah (123, 456, 789)
(a, b, c) foo (1, 2, 3)
(x, y, z, m, n) bar (100, 200, 300, 400, 500)

kent$  awk -F'\\(|\\)' '{split($2,t,",");split($4,v,",");printf "( "; for(x in t)s=s""sprintf("%s=%s, ", t[x],v[x]);sub(", $","",s);printf s")\n";s=""}' test

( alpha=123,  beta= 456,  gamma= 789)
( a=1,  b= 2,  c= 3)
(  m= 400,  n= 500, x=100,  y= 200,  z= 300)

Emacs Lisp版本的Prince Goulash答案

(require 'cl)

(defun split-and-trim (str separator)
  (let ((strs (split-string str separator)))
    (mapcar (lambda (s)
              (replace-regexp-in-string "^\\s-+" "" s))
            (mapcar (lambda (s)
                      (replace-regexp-in-string "\\s-$" "" s)) strs))))

(defun my/merge-list (beg end)
  (interactive "r")
  (goto-char beg)
  (let ((endmark (set-mark end))
        (regexp "(\\([^)]+\\))[^(]+(\\([^)]+\\))"))
    (while (re-search-forward regexp end t)
      (let ((replace-start (match-beginning 0))
            (replace-end   (match-end 0))
            (keys-str (match-string-no-properties 1))
            (values-str (match-string-no-properties 2)))
        (let* ((keys (split-and-trim keys-str ","))
               (values (split-and-trim values-str ",")))
          (while (> (length keys) (length values))
            (setq values (append values '(""))))
          (let* ((pairs (mapcar* (lambda (k v)
                                   (format "%s=%s" k v)) keys values))
                 (transformed (format "(%s)" (mapconcat #'identity pairs ", "))))
            (goto-char replace-start)
            (delete-region replace-start replace-end)
            (insert transformed)))))
    (goto-char (marker-position endmark))))
例如,选择区域如下所示

(alpha, beta, gamma)  blah (123, 456, 789)
(alpha, beta, gamma, delta)  blah (123, 456, 789, aaa)
在M-x我的/合并列表之后

(alpha=123, beta=456, gamma=789)
(alpha=123, beta=456, gamma=789, delta=aaa)

我将要描述的这种方法有点古怪,但它涉及到我所能管理的最少数量的Elisp代码。只有当要连接的列表中的逗号被删除后可以解释为Lisp列表时,这种方法才适用。如您的示例中所示,字母字符的数字和序列就可以了

首先,确保加载了公共Lisp库:M-:
(require'cl)
RET

现在,从第一个列表开头的光标开始:

杀掉前卫

C-e;移动线的末端

M-C-b;向后sexp

杀掉前卫

C-a;移动行的开头

压井管线

现在,
blah
(或其他)是杀伤环中的第一个条目,第二个列表是第二个条目,第一个列表是第三个条目

键入
),然后键入M-:(
评估表达式
),深呼吸,然后键入以下内容:

(loop with (a b) = (mapcar (lambda (x) (car (read-from-string (remove ?, x))))
                     (subseq kill-ring 1 3))
   for x in a for y in b do (insert (format "%s=%s, " y x)))
(为了演示的目的,我将其拆分,但您可以在一行中全部键入。)


最后DELDEL
,完成了!如果需要,可以将其转换为宏。

我的方法是创建一个命令来设置匹配列表,然后使用replace regexp作为第二个命令来分发匹配列表,利用replace regexp的现有\,工具

评估Elisp,例如在.emacs文件中:

(defvar match-list nil
  "A list of matches, as set through the set-match-list and consumed by the cycle-match-list function. ")
(defvar match-list-iter nil
  "Iterator through the global match-list variable. ")
(defun reset-match-list-iter ()
  "Set match-list-iter to the beginning of match-list and return it. "
  (interactive)
  (setq match-list-iter match-list))
(defun make-match-list (match-regexp use-regexp beg end)
  "Set the match-list variable as described in the documentation for set-match-list. "
  ;; Starts at the beginning of region, searches forward and builds match-list.
  ;; For efficiency, matches are appended to the front of match-list and then reversed
  ;; at the end.
  ;;
  ;; Note that the behavior of re-search-backward is such that the same match-list
  ;; is not created by starting at the end of the region and searching backward.
  (let ((match-list nil))
    (save-excursion
      (goto-char beg)
      (while
          (let ((old-pos (point)) (new-pos (re-search-forward match-regexp end t)))
            (when (equal old-pos new-pos)
              (error "re-search-forward makes no progress.  old-pos=%s new-pos=%s end=%s match-regexp=%s"
                     old-pos new-pos end match-regexp))
            new-pos)
        (setq match-list
              (cons (replace-regexp-in-string match-regexp
                                              use-regexp
                                              (match-string 0)
                                              t)
                    match-list)))
      (setq match-list (nreverse match-list)))))
(defun set-match-list (match-regexp use-regexp beg end)
  "Set the match-list global variable to a list of regexp matches.  MATCH-REGEXP
is used to find matches in the region from BEG to END, and USE-REGEXP is the
regexp to place in the match-list variable.

For example, if the region contains the text: {alpha,beta,gamma}
and MATCH-REGEXP is: \\([a-z]+\\),
and USE-REGEXP is: \\1
then match-list will become the list of strings: (\"alpha\" \"beta\")"
  (interactive "sMatch regexp: \nsPlace in match-list: \nr")
  (setq match-list (make-match-list match-regexp use-regexp beg end))
  (reset-match-list-iter))
(defun cycle-match-list (&optional after-end-string)
  "Return the next element of match-list.

If AFTER-END-STRING is nil, cycle back to the beginning of match-list.
Else return AFTER-END-STRING once the end of match-list is reached."
  (let ((ret-elm (car match-list-iter)))
    (unless ret-elm
      (if after-end-string
          (setq ret-elm after-end-string)
        (reset-match-list-iter)
        (setq ret-elm (car match-list-iter))))
    (setq match-list-iter (cdr match-list-iter))
    ret-elm))
(defadvice replace-regexp (before my-advice-replace-regexp activate)
  "Advise replace-regexp to support match-list functionality. "
  (reset-match-list-iter))
然后解决原来的问题:

M-x set-match-list
Match regexp: \([0-9]+\)[,)]
Place in match-list: \1

M-x replace-regexp
Replace regexp: \([a-z]+\)\([,)]\)
Replace regexp with: \1=\,(cycle-match-list)\2
要解决XML示例,请执行以下操作:

[Select fooval strings.]
M-x set-match-list
Match regexp: .+
Place in match-list: \&

[Select XML tags.]
M-x replace-regexp
Replace regexp: foo=""
Replace regexp with: foo="\,(cycle-match-list)"

哇,那太好了!但它能处理三个以上元素的列表吗?是的,不能,但它可以扩展。我不确定是否有一种方法可以编写一个动态正则表达式来处理变量等等……我想这就是sed和awk的作用所在。“blah”是固定文本还是随机文本?
[Select fooval strings.]
M-x set-match-list
Match regexp: .+
Place in match-list: \&

[Select XML tags.]
M-x replace-regexp
Replace regexp: foo=""
Replace regexp with: foo="\,(cycle-match-list)"