all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: John Mastro <john.b.mastro@gmail.com>
To: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Cc: "B. T. Raven" <btraven@nihilo.net>
Subject: Re: interleaving text lines of two regions
Date: Fri, 27 Jan 2017 11:51:13 -0800	[thread overview]
Message-ID: <CAOj2CQQfbFVb4rg2sy=9k09ubgFzTEvw6Gd92wHEKu-dTvEd5g@mail.gmail.com> (raw)
In-Reply-To: <o6g5bq028sc@news4.newsguy.com>

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	other threads:[~2017-01-27 19:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-27 19:00 interleaving text lines of two regions B. T. Raven
2017-01-27 19:51 ` John Mastro [this message]
2017-01-27 20:01   ` John Mastro

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAOj2CQQfbFVb4rg2sy=9k09ubgFzTEvw6Gd92wHEKu-dTvEd5g@mail.gmail.com' \
    --to=john.b.mastro@gmail.com \
    --cc=btraven@nihilo.net \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.