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

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

Re: How do we copy rectangular selected region


From: Michael Heerdegen
Subject: Re: How do we copy rectangular selected region
Date: Mon, 06 May 2019 12:50:50 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Budi <budikusasi@gmail.com> writes:

> How can we copy  rectangular selected region on latest emacs
> we paste it as line region instead
> how do such by script elisp.. thanks in advance

Dunno if I understand correctly.  What I sometimes want is that a yanked
rectangle moves the text after the insertion point completely after the
inserted rectangle text, instead of merging the rectangle into the text.
I do it like following.  Maybe I'm missing something and it can be done
with Emacs ways, dunno.

Anyway, the code modifies the command 'yank-rectangle' to get the
alternative insertion behavior by calling it with a prefix arg.

#+begin_src emacs-lisp
(defun my-insert-rectangle-with-newlines (rectangle)
  "Like `insert-rectangle', but (insert ?\\n) is used to change lines."
  (let ((lines rectangle)
        (insertcolumn (current-column))
        (first t))
    (push-mark)
    (while lines
      (or first
          (progn
            (insert ?\n)
            (or (bolp) (insert ?\n))
            (move-to-column insertcolumn t)))
      (setq first nil)
      (insert (car lines))
      (setq lines (cdr lines)))
    (save-excursion (insert ?\n))))

(eval-when-compile (require 'rect))

(defun my-yank-rectangle (&optional insert-newlines)
  "The optional argument INSERT-NEWLINES specifies if
`forward-line' or (insert ?\\n) is used to change lines.
A value of nil means that `forward-line' will be used and text after
point will occur beside the inserted rectangle. With any other value,
newlines will be inserted instead, with the effect that any following
text will remain after the inserted rectangle."
  (interactive "*P")
  (funcall (if insert-newlines
               #'my-insert-rectangle-with-newlines
             #'insert-rectangle)
           killed-rectangle))

(advice-add 'yank-rectangle :override #'my-yank-rectangle)
#+end_src


Michael.



reply via email to

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