help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: interleaving text lines of two regions


From: John Mastro
Subject: Re: interleaving text lines of two regions
Date: Fri, 27 Jan 2017 11:51:13 -0800

B. T. Raven <btraven@nihilo.net> wrote:
> I use the 'paste' filter to interleave lines of two text files (as for
> example an original with a translation) but for shorter stretches of text it
> seems like Emacs could do the same thing with an interactive
> interleave-regions function. This would look something like ediff where the
> user would select region-1 and region-2 and then have elisp take the first
> line from one region and then insert the first line from the second region,
> and so on, to produce a new buffer with interleaved text. On the emacs wiki
> I found a 'yank-interleave function which probably has most of the code
> needed for the proposed 'interleave-regions but I don't quite understand it.
> In the *scratch* buffer I tested a mod of that function with the final three
> gibberish lines below and got back a faulty interleave as shown in the first
> lines:

If I understand correctly, I think something simpler like this could
work:

(defun yank-interleaved ()
  (interactive)
  (let ((lines-0 (split-string (current-kill 0 t) "\n" t "\\s-"))
        (lines-1 (split-string (current-kill 1 t) "\n" t "\\s-")))
    (while (or lines-0 lines-1)
      (when lines-0 (insert (pop lines-0) "\n"))
      (when lines-1 (insert (pop lines-1) "\n")))))

It interleaves the last two kills (and could easily be generalized to
interleave the last N kills).

If I use `M-h M-w' on each of these two paragraphs:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccccccccccccccccc

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

And then invoke `M-x yank-interleaved', I get this:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
cccccccccccccccccccccccccccccccccccccccc

Which I think is correct, or on the right track anyway?

        John



reply via email to

[Prev in Thread] Current Thread [Next in Thread]