Text elisp-在定义良好的文本块中移动

Text elisp-在定义良好的文本块中移动,text,emacs,elisp,edit,Text,Emacs,Elisp,Edit,我在elisp中是一个不折不扣的人,我试图通过使用它来解决我面临的问题来进行练习,但这一次似乎很难 我有一个巨大的缓冲区,它有这样的结构 texta str1 textb str1 textc str1 <more times> textA str2 textB str2 textC str2 <same number of occurances of str2 as str1> texta str1 textb str1 textc str1 textA str2 t

我在elisp中是一个不折不扣的人,我试图通过使用它来解决我面临的问题来进行练习,但这一次似乎很难

我有一个巨大的缓冲区,它有这样的结构

texta
str1
textb
str1
textc
str1
<more times>
textA
str2
textB
str2
textC
str2
<same number of occurances of str2 as str1>
texta
str1
textb
str1
textc
str1
textA
str2
textB
str2
textC
str2
现在我想让str1上面的文本一直移动到前一个str1(或第一个str1的开头)的对应位置,以便第一个str1及其上面的文本正好位于str2的第一个位置之上

textA
texta
str1
str2
textB
textb
str1
str2
textC
textc
str1
str2
<etc>
textA
texta
str1
str2
textB
textb
str1
str2
textC
textc
str1
str2
鉴于elisp是一种将emacs结合在一起的语言,我认为对于一个比我稍有经验的人来说,想出一些解决方案应该不会太难…

好吧,我做到了

(defun move-under (str1 str2)
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (set-mark (point-min))
    (search-forward str1)
    (setq text (buffer-substring (mark) (point)))
    (delete-region (mark) (point))
    (search-forward str2)
    (backward-delete-char (length str2))
    (insert text)))

(defun rearrange-for-nonbuffer-usage ()
  (interactive)
(while (search-forward "str2" nil t)
  (progn (goto-char (point-min))
     (move-under "str1" "str2")))))
好的,我做到了

(defun move-under (str1 str2)
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (set-mark (point-min))
    (search-forward str1)
    (setq text (buffer-substring (mark) (point)))
    (delete-region (mark) (point))
    (search-forward str2)
    (backward-delete-char (length str2))
    (insert text)))

(defun rearrange-for-nonbuffer-usage ()
  (interactive)
(while (search-forward "str2" nil t)
  (progn (goto-char (point-min))
     (move-under "str1" "str2")))))

正如OP所展示的,为此编写Elisp并不太难。但是,如果将键盘宏作为Emacs工作流程的一部分,这是即时的。查看它。

正如OP所展示的,为此编写Elisp并不太难。但是,如果将键盘宏作为Emacs工作流程的一部分,这是即时的。查看它。

while
可以有多个表达式,所以你不需要
(progn…
包装。while的主体可以有多个表达式,所以你不需要
(progn…
包装。那就是你-原始海报!那就是你-原始海报!