From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Mathias Dahl Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] New command to invert lines in region Date: Sun, 16 Oct 2016 00:25:12 +0200 Message-ID: References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=001a1143e3e2294edc053eeed28e X-Trace: blaine.gmane.org 1476570376 4867 195.159.176.226 (15 Oct 2016 22:26:16 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 15 Oct 2016 22:26:16 +0000 (UTC) Cc: Kaushal Modi , Emacs developers To: Tino Calancha Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Oct 16 00:26:12 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 1bvXP5-0008Ug-10 for ged-emacs-devel@m.gmane.org; Sun, 16 Oct 2016 00:26:07 +0200 Original-Received: from localhost ([::1]:54282 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bvXP6-0001dj-5t for ged-emacs-devel@m.gmane.org; Sat, 15 Oct 2016 18:26:08 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47985) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bvXOY-0001db-Uc for emacs-devel@gnu.org; Sat, 15 Oct 2016 18:25:36 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bvXOX-0007vg-Mz for emacs-devel@gnu.org; Sat, 15 Oct 2016 18:25:34 -0400 Original-Received: from mail-vk0-x242.google.com ([2607:f8b0:400c:c05::242]:34107) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1bvXOX-0007vM-EG for emacs-devel@gnu.org; Sat, 15 Oct 2016 18:25:33 -0400 Original-Received: by mail-vk0-x242.google.com with SMTP id 2so6309235vkb.1 for ; Sat, 15 Oct 2016 15:25:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=Y4m4MywFizdv9ulouToUWUxdtnYwP3jpddxwjlgCDBY=; b=kjrIk4RYXOTRT912JBKqxlyIBrPGM9+5poX+D9t/wF3alSR4++rFPoVHlsLizbAyDd m0MPcugsl6gUIund1fgTWXIOg9cchE9ZOEAxZ2p0uS6ruItdEh9ilPZlnEGTj57+a1jB lpTQ2xV2orRNWTgmmWJFiKfBadTtb7vK3W/etleUfXjZMqKt76USH7NQ0UofKztpkXQW 9+ifvPoGDLW5uCudX+ZN4wBuJjE7Y36gp5F3XsgXZk7d/Cunc9loj3e+XxFXySPa3k0k xKk3oNPn9MXH6Q8hmOevNqdfZ2RfU2MIqsQBYyXIrbJY66hw/M1iB3GIdZD7JXhAZORn Ce3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=Y4m4MywFizdv9ulouToUWUxdtnYwP3jpddxwjlgCDBY=; b=gc8sf42rDwuKkxIk7wih5kst2RYmNJV8lP5e5XapZATtg3JAo0fhuWhnfGgjT5P2/s X5uEo+a1SYCJNs4azY4Uhm2ddOKSVuKlyOVrNN5m/nDCtO3/J5X7gnamUTPnf8KsK/2A EVdHFv6hoPn+29ckdPGa7pJDz5P17pc2mLbP5+8J0Q4gr1NPryavy3yO8SM3CVb2Mp5/ pF4kGs4W/OA24A8qHQaRdNUD3mjTAelmt+jBOVjidaB/kb4CNZ8K4wjQuN5zTlnygaSr iPQ03fVwXO2fBFEt/zu9lEnytibHgAylor2GgXNCAcn9/qyz/Q4UZZT+T6xzHKS0BiaC HlPQ== X-Gm-Message-State: AA6/9RlenBHnD+BQdYMg6P63pCWJ0JaK+uzn+tiPnNg5cEnyyFJVFrdys/1TbBW1whKJn1zLhFUQO5IOFRglrQ== X-Received: by 10.31.182.71 with SMTP id g68mr13108092vkf.135.1476570332612; Sat, 15 Oct 2016 15:25:32 -0700 (PDT) Original-Received: by 10.159.55.134 with HTTP; Sat, 15 Oct 2016 15:25:12 -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:c05::242 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:208311 Archived-At: --001a1143e3e2294edc053eeed28e Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable > OK, i found it: reverse-region. Pretty similar. > It doesn't allow writting to a different buffer than the current one. > The proposed command here allow that and save some consing. > Writing the result into another buffer sounds quite arbitrary. Is it really such a common scenario that this function should do it? Those who needs to have the result in another buffer can easily add a few lines in the calling code to make it so. Out of curiosity I had a look at the current implementation of `reverse-region' and found it quite complicated and hard to understand what it does. I wrote my own na=C3=AFve version that I think is much simpler to understand and which seems to be as quick as the current version (I tested on a buffer with 100 000 lines.) I have no idea on how much memory it consumes compared to the current version though. It's so small that I am including it here: (defun reverse-region (beg end) "Reverse the order of lines in a region. >From a program takes two point or marker arguments, BEG and END." (interactive "r") (let ((lines (nreverse (split-string (buffer-substring beg end) "\n"))) (cnt 0)) (delete-region beg end) (dolist (line lines) ;; Try to do the right thing when the end of the region is on an ;; empty line, which should be a common use case. (unless (and (=3D cnt 0) (string=3D "" line)) (setq cnt (1+ cnt)) (insert line "\n"))))) If case people prefer this version and want to replace the current implementation with it, I should have papers on file already. /Mathias --001a1143e3e2294edc053eeed28e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
=C2=A0
OK, i found it: reverse-region.=C2=A0 Pretty similar.
It doesn't allow writting to a different buffer than the current one. The proposed command here allow that and save some consing.

Writing the result into another buffer sounds quite ar= bitrary. Is it really such a common scenario that this function should do i= t? Those who needs to have the result in another buffer can easily add a fe= w lines in the calling code to make it so.

Out of = curiosity I had a look at the current implementation of `reverse-region'= ; and found it quite complicated and hard to understand what it does. I wro= te my own na=C3=AFve version that I think is much simpler to understand and= which seems to be as quick as the current version (I tested on a buffer wi= th 100 000 lines.) I have no idea on how much memory it consumes compared t= o the current version though. It's so small that I am including it here= :

(defun reverse-region (beg end)
<= div>=C2=A0 "Reverse the order of lines in a region.
From a p= rogram takes two point or marker arguments, BEG and END."
= =C2=A0 (interactive "r")
=C2=A0 (let ((lines (nreverse = (split-string (buffer-substring beg end) "\n")))
=C2=A0= =C2=A0 =C2=A0 =C2=A0 (cnt 0))
=C2=A0 =C2=A0 (delete-region beg e= nd)
=C2=A0 =C2=A0 (dolist (line lines)
=C2=A0 =C2=A0 = =C2=A0 ;; Try to do the right thing when the end of the region is on an
=C2=A0 =C2=A0 =C2=A0 ;; empty line, which should be a common use cas= e.
=C2=A0 =C2=A0 =C2=A0 (unless (and (=3D cnt 0) (string=3D "= ;" line))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq cnt (1+ cnt))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (insert line "\n")))))
<= /div>

If case people prefer this version and want = to replace the current implementation with it, I should have papers on file= already.

/Mathias

--001a1143e3e2294edc053eeed28e--