From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tino Calancha Newsgroups: gmane.emacs.devel Subject: [PATCH] New command to invert lines in region Date: Mon, 3 Oct 2016 19:43:18 +0900 (JST) Message-ID: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; format=flowed; charset=US-ASCII X-Trace: blaine.gmane.org 1475491425 19676 195.159.176.226 (3 Oct 2016 10:43:45 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 3 Oct 2016 10:43:45 +0000 (UTC) User-Agent: Alpine 2.20 (DEB 67 2015-01-07) Cc: tino.calancha@gmail.com To: Emacs developers Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Oct 03 12:43:41 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 1br0id-000402-ED for ged-emacs-devel@m.gmane.org; Mon, 03 Oct 2016 12:43:35 +0200 Original-Received: from localhost ([::1]:35998 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1br0ib-0000jL-Td for ged-emacs-devel@m.gmane.org; Mon, 03 Oct 2016 06:43:33 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:52334) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1br0iV-0000jC-Fu for emacs-devel@gnu.org; Mon, 03 Oct 2016 06:43:28 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1br0iQ-0003t3-Gj for emacs-devel@gnu.org; Mon, 03 Oct 2016 06:43:26 -0400 Original-Received: from mail-pa0-x241.google.com ([2607:f8b0:400e:c03::241]:36671) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1br0iQ-0003sz-6i for emacs-devel@gnu.org; Mon, 03 Oct 2016 06:43:22 -0400 Original-Received: by mail-pa0-x241.google.com with SMTP id cd13so6521747pac.3 for ; Mon, 03 Oct 2016 03:43:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:date:to:cc:subject:message-id:user-agent:mime-version; bh=Cz65syII8u/m+GFaUR0umGJl/EADeldKn5uAZEg+x9E=; b=SLBgdAIAZYWmy4lriC74OPN+nXfz3576dmtJuaaMj/aUU9Snn6s+BRVuaUWnWulYh1 OWUjCCqWjLmsslVZm/woYJRbWKEl9u2UygBWtjQcMw0TnZL/gkKfc0lDdsMLhzZ85QG0 iInnXxlZQIy1yoFxDUU9uK57d1EYSA5b8VFhLVEb2gOZrRvZN/qTr6k8iuiGeaJjuq5b SQjqKP7RhI5z4ahSsQGj+IMwYbym/J1o0sRyJvw2QK1kkNBPguvF3fLgK555nqMitoFP B8eZzhdozjK0XZR72Hh8yNgQ0aDt1+8FoWXGZKS32BMQa2vDkyygCIxdNA3Eh0lLNRG7 aTaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:date:to:cc:subject:message-id:user-agent :mime-version; bh=Cz65syII8u/m+GFaUR0umGJl/EADeldKn5uAZEg+x9E=; b=bO4KATn0Nl3Kjb/6Vjlo8jGAHIGnRgfQWyCzYAM7y+k8GEdEB/1pWXal80ZPRQVpDX hagjA2Y71CrO9v4UJwU2nXVKeRDvdskYE92syaEBFrlk3JLdxmkCfT3FtfXBxusTEzB/ 9c0ChGtOCA0STvp+H80jMgwK3vrzWtCDFMauCEIMO0UNyHuSVAbrFzr5PSpPb22T+vij ya37WftEnPV5vaDUsJQw/cZWaz9hmdXEIPihYbIwsP3urngh9bxtBUQMqAjP9u/XIGNq 48b8Wdy7qmCi2f9NCr7AotVa53ftEmmoMvQHsOFESOL3LZymAawNs3QbLWWPzGDZwxVk eyMw== X-Gm-Message-State: AA6/9Rk6bM4ZQlvnVvMlaCN03iyXwJMv3rsMJjyHulehxidOOpItNU+sWj3awvBCwZ9ILA== X-Received: by 10.66.100.225 with SMTP id fb1mr36625305pab.4.1475491401372; Mon, 03 Oct 2016 03:43:21 -0700 (PDT) Original-Received: from calancha-pc ([103.5.140.159]) by smtp.gmail.com with ESMTPSA id r29sm46288600pfd.37.2016.10.03.03.43.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Oct 2016 03:43:20 -0700 (PDT) X-Google-Original-From: Tino Calancha X-X-Sender: calancha@calancha-pc X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c03::241 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:207955 Archived-At: 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