all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "B. T. Raven" <btraven@nihilo.net>
To: help-gnu-emacs@gnu.org
Subject: interleaving text lines of two regions
Date: Fri, 27 Jan 2017 13:00:13 -0600	[thread overview]
Message-ID: <o6g5bq028sc@news4.newsguy.com> (raw)

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



             reply	other threads:[~2017-01-27 19:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-27 19:00 B. T. Raven [this message]
2017-01-27 19:51 ` interleaving text lines of two regions John Mastro
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=o6g5bq028sc@news4.newsguy.com \
    --to=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.