unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] New command to invert lines in region
@ 2016-10-03 10:43 Tino Calancha
  2016-10-03 11:02 ` Kaushal Modi
  0 siblings, 1 reply; 10+ messages in thread
From: Tino Calancha @ 2016-10-03 10:43 UTC (permalink / raw)
  To: Emacs developers; +Cc: tino.calancha


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.
---
  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



^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2016-10-16 18:25 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-10-03 10:43 [PATCH] New command to invert lines in region Tino Calancha
2016-10-03 11:02 ` Kaushal Modi
2016-10-03 11:21   ` Tino Calancha
2016-10-03 11:33     ` Tino Calancha
2016-10-15 22:25       ` Mathias Dahl
2016-10-15 23:57         ` Kaushal Modi
2016-10-16 11:03           ` Mathias Dahl
2016-10-16 13:24         ` Noam Postavsky
2016-10-16 14:00           ` Eli Zaretskii
2016-10-16 18:25         ` Kalle Olavi Niemitalo

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).