unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* interleaving text lines of two regions
@ 2017-01-27 19:00 B. T. Raven
  2017-01-27 19:51 ` John Mastro
  0 siblings, 1 reply; 3+ messages in thread
From: B. T. Raven @ 2017-01-27 19:00 UTC (permalink / raw)
  To: help-gnu-emacs

Hello emacsworld:

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:

;; This buffer is for notes you don't want to save, and for Lisp 
evaluation.asneuthoaseutheoaunteohuouesntuho
aoeoasenthesnheosnthjoantheujoj
santoehoaesuntoahonteheesnthu
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer.

asneuthoaseutheoaunteohuouesntuho
aoeoasenthesnheosnthjoantheujoj
santoehoaesuntoahonteheesnthu

I modified the function by removing the "separator" argument like this:

  (defun yank-interleaved ()  ;; original function called with argument 
'separator'
       "Yank the previous kill, interleaving each line of the yanked
       text with a line in current buffer.

     Interleaving begins on the line containing point, and moves
     downward. If point is at the beginning of the line, the yanked
     lines are inserted before the buffer lines. If it is at the end
     of the line, the yanked lines are inserted after.

     If the argument SEPARATOR is given, insert this between each
     buffer line and yanked line."
       ;; TODO: Make it work with active region
      ;; (interactive "MSeparator: ")
       (interactive)
       (let ((yank (current-kill 1 t))
              dir line)
         (if (null (string-match-p "\n" yank))
     	    (yank)
           (setq dir
     	        (cond
     	         ((bolp) 'front)
     	         ((eolp) 'back)
     	         (t (user-error "Call with point at beginning or end of 
line"))))
           (setq yank (split-string yank "\n" t "\\s-"))
           (while (setq line (pop yank))
     	    (if (eq dir 'front)
     	        (progn
     	          (beginning-of-line)
     	          (insert line)
     	        ;;  (insert separator)
                    )
     	      (end-of-line)
     	   ;;   (insert separator)
     	      (insert line))
        	   (forward-line)))))

Of course what I wanted to see was:

;; This buffer is for notes you don't want to save, and for Lisp evaluation.
asneuthoaseutheoaunteohuouesntuho
;; If you want to create a file, visit that file with C-x C-f,
aoeoasenthesnheosnthjoantheujoj
;; then enter the text in that file's own buffer.
santoehoaesuntoahonteheesnthu


Can any of you help me to understand what's going on and whether my 
proposed interleave-regions function would be worth pursuing for other 
applications?

Thanks,
Ed



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: interleaving text lines of two regions
  2017-01-27 19:00 interleaving text lines of two regions B. T. Raven
@ 2017-01-27 19:51 ` John Mastro
  2017-01-27 20:01   ` John Mastro
  0 siblings, 1 reply; 3+ messages in thread
From: John Mastro @ 2017-01-27 19:51 UTC (permalink / raw)
  To: help-gnu-emacs@gnu.org; +Cc: B. T. Raven

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



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: interleaving text lines of two regions
  2017-01-27 19:51 ` John Mastro
@ 2017-01-27 20:01   ` John Mastro
  0 siblings, 0 replies; 3+ messages in thread
From: John Mastro @ 2017-01-27 20:01 UTC (permalink / raw)
  To: help-gnu-emacs@gnu.org

John Mastro <john.b.mastro@gmail.com> wrote:
>
> 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).

Just for one, here's a version that interleaves the last N kills
(default 2):

(defun yank-interleaved (n)
  (interactive
   (list (if current-prefix-arg (prefix-numeric-value current-prefix-arg) 2)))
  (let ((yanks (mapcar (lambda (i)
                         (split-string (current-kill i t) "\n" t "\\s-"))
                       (number-sequence 0 (1- n)))))
    (while (seq-some #'consp yanks)
      (dotimes (i n)
        (when (nth i yanks)
          (insert (pop (nth i yanks)) "\n"))))))



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-01-27 20:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-01-27 19:00 interleaving text lines of two regions B. T. Raven
2017-01-27 19:51 ` John Mastro
2017-01-27 20:01   ` John Mastro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).