From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Kaushal Modi Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] New command to invert lines in region Date: Mon, 03 Oct 2016 11:02:33 +0000 Message-ID: References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=001a114a18c23361a0053df3e25f X-Trace: blaine.gmane.org 1475492634 8992 195.159.176.226 (3 Oct 2016 11:03:54 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 3 Oct 2016 11:03:54 +0000 (UTC) To: Tino Calancha , Emacs developers Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Oct 03 13:03:44 2016 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1br122-0000PT-W4 for ged-emacs-devel@m.gmane.org; Mon, 03 Oct 2016 13:03:39 +0200 Original-Received: from localhost ([::1]:36142 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1br120-0005UW-7I for ged-emacs-devel@m.gmane.org; Mon, 03 Oct 2016 07:03:36 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:56399) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1br11E-0005UP-9H for emacs-devel@gnu.org; Mon, 03 Oct 2016 07:02:53 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1br11B-00085Y-Rx for emacs-devel@gnu.org; Mon, 03 Oct 2016 07:02:47 -0400 Original-Received: from mail-ua0-x234.google.com ([2607:f8b0:400c:c08::234]:33105) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1br11B-00085M-Ke for emacs-devel@gnu.org; Mon, 03 Oct 2016 07:02:45 -0400 Original-Received: by mail-ua0-x234.google.com with SMTP id v7so72362964uaa.0 for ; Mon, 03 Oct 2016 04:02:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=AbzXi2on5QJNe0vHz2/MU3KgSXlxPGJIetxArDwMKwY=; b=IZ8B1qwz8FVYQI9AOvdW2chQjnHbrlWrXdGQxalPsRbkCykpJOiiZ2xYX27YeYru1M Ptfi5quwvS6n7WrETHtiMoLuN5Br/8jhAvg5omXTw02h/1JAkqv3ONkq6QoqtRRrqQBj /hF1HB4mlubIy9OF7MNVMKQFQyK3zqxljEwUfGDN6e7l7Bb1NtSR2HTTXm+VUxr8OOh4 H4axKhKoChItN9xSazJPGsez4rZJd+H6nirRb1cfhfaX3VfkT7YJKDzgW+ynPBuoUB6W O+GZS4WmxQXouO9w491xU1EqKF247KwTVw1szIbBzeDmP64ZHw7r+UrxjmZcgyY4spIU H5MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=AbzXi2on5QJNe0vHz2/MU3KgSXlxPGJIetxArDwMKwY=; b=OYnvF+7KKDfCyzvLK/VXTePzR0mVlrR29/sbo0/cGDlJ3s8S78Yljb6q8g4RMIzRKH 2+fn+BDuFH+w4ysk2mB+NWTHA6z1xedW5uFkLMriQ+JIKN94QiSoE9cdbXG3iO2+z7BL nnsvqupPnoY5lQs4JeGHd1RWO8C70vNq/ClYvVgxcCHnaZbFFwkkDSsECcR3kwucGeRw SNF8CLO+dYGEqXQoYQzok0VPwUnJ3LYfrrJsrgPPBQIulrAqo5jhYIGgwKlCR7gUF3mu hwmlChvw19LFYPaiG5/+oMBbVjy5E7B4wswzLodetx/0CHR3G3SixgcmD7qMzh8fWjBG Bx7g== X-Gm-Message-State: AA6/9Rli1CjNJ0YI8oObQdZiqMv6UXPU0w2wBhhPKSSmD0/hfdqL3K0TR88jC8L4r5LZ4AWJFOk8Zg5vtdzPMQ== X-Received: by 10.176.69.6 with SMTP id r6mr11161085uar.109.1475492564916; Mon, 03 Oct 2016 04:02:44 -0700 (PDT) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400c:c08::234 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:207956 Archived-At: --001a114a18c23361a0053df3e25f Content-Type: text/plain; charset=UTF-8 I believe there is reverse-lines On Mon, Oct 3, 2016, 6:43 AM Tino Calancha wrote: > > Hello Emacs, > > i cannot find a command to invert the lines within a region. > Does such thing already exists? Where? > IMO, it might be good to have a command doing such thing. > For instance, let's suppose the region contains following lines: > > foo 2 3 > bar 8 9 > baz 10 14 > qux 22 28 > > The proposed command would change the region to: > > qux 22 28 > baz 10 14 > bar 8 9 > foo 2 3 > > What do you think about this idea? Does it have sense? > Feel free to make comments. > Regards, > Tino > > > I have prepared following patch: > > > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; > From 763e69bd0f22ae91be23cb384ec33cca0220f937 Mon Sep 17 00:00:00 2001 > From: Tino Calancha > Date: Mon, 3 Oct 2016 19:25:18 +0900 > Subject: [PATCH] invert-lines: New comman to invert lines in region > > * lisp/simple.el (invert-lines): New command. > Bind to 'C-x I'. > * etc/NEWS: Add entry for this new feature. > --- > etc/NEWS | 4 ++++ > lisp/simple.el | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 56 insertions(+) > > diff --git a/etc/NEWS b/etc/NEWS > index bd94c94..d3dffc0 100644 > --- a/etc/NEWS > +++ b/etc/NEWS > @@ -61,6 +61,10 @@ affected by this, as SGI stopped supporting IRIX in > December 2013. > > * Changes in Emacs 26.1 > > + > +** The new command 'invert-lines' invert the lines in region. Bound > +to 'C-x I'. > + > +++ > ** The new function 'call-shell-region' executes a command in an > inferior shell with the buffer region as input. > diff --git a/lisp/simple.el b/lisp/simple.el > index 70bd759..9233bf6 100644 > --- a/lisp/simple.el > +++ b/lisp/simple.el > @@ -6761,6 +6761,58 @@ global-visual-line-mode > visual-line-mode turn-on-visual-line-mode) > > > +(defun invert-lines (&optional buffer start end) > + "Invert order of lines in region. > +Optional arg BUFFER (or buffer name) is the output buffer. > + Default to current one. > +Optional arguments, START and END, define the region. > + If START is nil, then default to minimum permissible value of point > + in the current buffer. > + If END is nil, then default to maximum permissible value of point > + in the current buffer. > +With prefix argument prompt for BUFFER." > + (interactive > + (let ((buf (if current-prefix-arg > + (read-buffer "Invert lines to buffer: " > + (current-buffer) > + 'must-match) > + (current-buffer))) > + (beg (region-beginning)) > + (end (region-end))) > + (list buf beg end))) > + (let ((tmp-buf (get-buffer-create > + (generate-new-buffer " *invert-lines*"))) > + (buf (or buffer (current-buffer))) > + (beg-pos (or start (point-min))) > + (end-pos (or end (point-max))) > + (init-pos (point)) > + line) > + (unwind-protect > + (progn > + (goto-char beg-pos) > + (while (and (not (eobp)) > + (not (>= (point) end-pos)) > + (re-search-forward "^.*$")) > + (setq line (match-string 0)) > + (with-current-buffer tmp-buf > + (save-excursion > + (insert line "\n"))) > + (forward-line 1)) > + (if (eq (get-buffer buf) (current-buffer)) > + (progn > + (delete-region beg-pos end-pos) > + (goto-char beg-pos) > + (insert-buffer-substring tmp-buf)) > + (with-current-buffer tmp-buf > + (copy-to-buffer buf (point-min) (point-max))))) > + (kill-buffer tmp-buf) > + (goto-char init-pos) > + (when (region-active-p) > + (deactivate-mark 'force))))) > + > +(define-key ctl-x-map "I" 'invert-lines) > + > + > (defun transpose-chars (arg) > "Interchange characters around point, moving forward one character. > With prefix arg ARG, effect is to take character before point > -- > 2.9.3 > > > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; > In GNU Emacs 26.0.50.4 (x86_64-pc-linux-gnu, GTK+ Version 3.22.0) > of 2016-10-03 built on calancha-pc > Repository revision: 8cd975cebd588d5435fa2b333dba6c526e602933 > > -- Kaushal Modi --001a114a18c23361a0053df3e25f Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

I believe there is reverse-lines


On Mon, Oct 3, 2016, 6:43 A= M Tino Calancha <tino.calanch= a@gmail.com> wrote:

Hello Emacs,

i cannot find a command to invert the lines within a region.
Does such thing already exists? Where?
IMO, it might be good to have a command doing such thing.
For instance, let's suppose the region contains following lines:

foo 2 3
bar 8 9
baz 10 14
qux 22 28

The proposed command would change the region to:

qux 22 28
baz 10 14
bar 8 9
foo 2 3

What do you think about this idea? Does it have sense?
Feel free to make comments.
Regards,
Tino


I have prepared following patch:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;= ;;;;
>From 763e69bd0f22ae91be23cb384ec33cca0220f937 Mon Sep 17 00:00:00 2001
From: Tino Calancha <tino.calancha@gmail.com>
Date: Mon, 3 Oct 2016 19:25:18 +0900
Subject: [PATCH] invert-lines: New comman to invert lines in region

* lisp/simple.el (invert-lines): New command.
Bind to 'C-x I'.
* etc/NEWS: Add entry for this new feature.
---
=C2=A0 etc/NEWS=C2=A0 =C2=A0 =C2=A0 =C2=A0|=C2=A0 4 ++++
=C2=A0 lisp/simple.el | 52 ++++++++++++++++++++++++++++++++++++++++++++++++= ++++
=C2=A0 2 files changed, 56 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index bd94c94..d3dffc0 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -61,6 +61,10 @@ affected by this, as SGI stopped supporting IRIX in Dece= mber 2013.

=C2=A0 * Changes in Emacs 26.1

+
+** The new command 'invert-lines' invert the lines in region.=C2= =A0 Bound
+to 'C-x I'.
+
=C2=A0 +++
=C2=A0 ** The new function 'call-shell-region' executes a command i= n an
=C2=A0 inferior shell with the buffer region as input.
diff --git a/lisp/simple.el b/lisp/simple.el
index 70bd759..9233bf6 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -6761,6 +6761,58 @@ global-visual-line-mode
=C2=A0 =C2=A0 visual-line-mode turn-on-visual-line-mode)


+(defun invert-lines (&optional buffer start end)
+=C2=A0 "Invert order of lines in region.
+Optional arg BUFFER (or buffer name) is the output buffer.
+ Default to current one.
+Optional arguments, START and END, define the region.
+ If START is nil, then default to minimum permissible value of point
+ in the current buffer.
+ If END is nil, then default to maximum permissible value of point
+ in the current buffer.
+With prefix argument prompt for BUFFER."
+=C2=A0 (interactive
+=C2=A0 =C2=A0(let ((buf (if current-prefix-arg
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (read-buffe= r "Invert lines to buffer: "
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(current-buffer)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0'must-match)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (current-buffer)))=
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(beg (region-beginning))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(end (region-end)))
+=C2=A0 =C2=A0 =C2=A0(list buf beg end)))
+=C2=A0 (let ((tmp-buf (get-buffer-create
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (generate-n= ew-buffer " *invert-lines*")))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 (buf (or buffer (current-buffer)))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 (beg-pos (or start (point-min)))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 (end-pos (or end (point-max)))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 (init-pos (point))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 line)
+=C2=A0 =C2=A0 (unwind-protect
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 (progn
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (goto-char beg-pos)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (while (and (not (eobp))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 (not (>=3D (point) end-pos))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 (re-search-forward "^.*$"))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq line (match-string 0))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (with-current-buffer tmp-buf
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (save-excursion
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (insert line "= ;\n")))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (forward-line 1))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (if (eq (get-buffer buf) (current-buffe= r))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (progn
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (delete-region beg= -pos end-pos)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (goto-char beg-pos= )
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (insert-buffer-sub= string tmp-buf))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (with-current-buffer tmp-buf
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (copy-to-buffer buf (poin= t-min) (point-max)))))
+=C2=A0 =C2=A0 =C2=A0 (kill-buffer tmp-buf)
+=C2=A0 =C2=A0 =C2=A0 (goto-char init-pos)
+=C2=A0 =C2=A0 =C2=A0 (when (region-active-p)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 (deactivate-mark 'force)))))
+
+(define-key ctl-x-map "I" 'invert-lines)
+
+
=C2=A0 (defun transpose-chars (arg)
=C2=A0 =C2=A0 "Interchange characters around point, moving forward one= character.
=C2=A0 With prefix arg ARG, effect is to take character before point
--
2.9.3

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;= ;;;;
In GNU Emacs 26.0.50.4 (x86_64-pc-linux-gnu, GTK+ Version 3.22.0)
=C2=A0 of 2016-10-03 built on calancha-pc
Repository revision: 8cd975cebd588d5435fa2b333dba6c526e602933

--

Kaushal Modi

--001a114a18c23361a0053df3e25f--