emacs-devel
[Top][All Lists]
Advanced

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

Re: [PATCH] New command to invert lines in region


From: Kaushal Modi
Subject: Re: [PATCH] New command to invert lines in region
Date: Mon, 03 Oct 2016 11:02:33 +0000

I believe there is reverse-lines


On Mon, Oct 3, 2016, 6:43 AM Tino Calancha <address@hidden> 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 <address@hidden>
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


reply via email to

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