Reputation: 6866
I am quite a noob in elisp and try to practice by solving problems I face using it but this one seems to hard.
I have an enormous buffer which has this structure
texta
str1
textb
str1
textc
str1
<more times>
textA
str2
textB
str2
textC
str2
<same number of occurances of str2 as str1>
Now i want the text above str1 until the previous str1(or the beginning for the first one to be moved to the corresponding occurance of str2 so that the first str1 and the text above it is right above the first occurance of str2
textA
texta
str1
str2
textB
textb
str1
str2
textC
textc
str1
str2
<etc>
Given that elisp is a language that binds emacs together i think it shouldnt be too hard for someone with little more experience than me to come up with some solution...
Upvotes: 1
Views: 147
Reputation: 6866
ok I did it!
(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")))))
Upvotes: 3
Reputation: 17717
As OP showed, it's not too hard to write Elisp for this. However, it's instantaneous if you have keyboard macros as a part of your Emacs workflow. Look into it.
Upvotes: 1