Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

interleaving text lines of two regions

36 views
Skip to first unread message

B. T. Raven

unread,
Jan 27, 2017, 2:00:48 PM1/27/17
to
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

0 new messages