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

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

interleaving text lines of two regions


From: B. T. Raven
Subject: interleaving text lines of two regions
Date: Fri, 27 Jan 2017 13:00:13 -0600
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.6.0

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 via email to

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