unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Kangas <stefan@marxist.se>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: Xue Fuqiao <xfq.free@gmail.com>, Glenn Morris <rgm@gnu.org>,
	13521@debbugs.gnu.org
Subject: bug#13521: `sort-lines' on the empty region
Date: Thu, 20 Aug 2020 18:18:34 -0700	[thread overview]
Message-ID: <CADwFkm=SDc3obOA_KXvtWOBRay+aWtnOtkKX5SGPwXGVB=+hYg@mail.gmail.com> (raw)
In-Reply-To: <jwv1udd2gab.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Tue, 22 Jan 2013 14:04:02 -0500")

[-- Attachment #1: Type: text/plain, Size: 899 bytes --]

tags 13521 + patch
thanks

Stefan Monnier <monnier@IRO.UMontreal.CA> writes:

>>> That seems to be considering a different case: not the case of an empty
>>> region, but the case where there's no active region.
>>> Currently, this case signals an error, but I think I agree it makes
>>> sense to let it apply to the whole buffer if there is no active region.
>> flush-lines, keep-lines instead operate on all lines after point.
>
> Yes, we have an inconsistency in this respect: some commands use "the
> whole buffer" and others use "everything after point".  Whichever choice
> we make for sort-lines, it will be inconsistent with some of the
> existing commands ;-)
>
> I personally prefer the "whole buffer", but whoever makes the change
> gets to make the choice.

The attached patch makes 'sort-lines' sort the entire buffer when there
is no region.

Any comments?

Best regards,
Stefan Kangas

[-- Attachment #2: 0001-Make-sort-lines-sort-entire-buffer-without-active-re.patch --]
[-- Type: text/x-diff, Size: 2693 bytes --]

From 6c1d6eb1dc8306f99fd01ba848213632ac6cd973 Mon Sep 17 00:00:00 2001
From: Stefan Kangas <stefankangas@gmail.com>
Date: Fri, 21 Aug 2020 03:06:16 +0200
Subject: [PATCH] Make 'sort-lines' sort entire buffer without active region

* lisp/sort.el (sort-lines): Sort entire buffer if there is no active
region.  Doc fix.  (Bug#13521)
* test/lisp/sort-tests.el (sort-tests--lines/entire-buffer): New test.
* etc/NEWS: Announce the above change.
---
 etc/NEWS                |  3 +++
 lisp/sort.el            | 12 +++++++++++-
 test/lisp/sort-tests.el |  6 ++++++
 3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index 53391f91f7..1a21525328 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -145,6 +145,9 @@ setting the variable 'auto-save-visited-mode' buffer-locally to nil.
 description of the properties.  Likewise 'button-describe' does the
 same for a button.
 
+---
+** 'sort-lines' will now sort the entire buffer if there is no region.
+
 \f
 * Changes in Specialized Modes and Packages in Emacs 28.1
 
diff --git a/lisp/sort.el b/lisp/sort.el
index f878db24a3..b1cade4266 100644
--- a/lisp/sort.el
+++ b/lisp/sort.el
@@ -200,11 +200,21 @@ sort-reorder-buffer
 (defun sort-lines (reverse beg end)
   "Sort lines in region alphabetically; REVERSE non-nil means descending order.
 Interactively, REVERSE is the prefix argument, and BEG and END are the region.
+If there is no region selected, sort the entire buffer.
+
 Called from a program, there are three arguments:
 REVERSE (non-nil means reverse order), BEG and END (region to sort).
 The variable `sort-fold-case' determines whether alphabetic case affects
 the sort order."
-  (interactive "P\nr")
+  (interactive
+   (progn
+     (barf-if-buffer-read-only)
+     (list current-prefix-arg nil nil)))
+  (if (and (called-interactively-p 'any) (use-region-p))
+      (setq beg (region-beginning)
+            end (region-end))
+    (setq beg (point-min)
+          end (point-max)))
   (save-excursion
     (save-restriction
       (narrow-to-region beg end)
diff --git a/test/lisp/sort-tests.el b/test/lisp/sort-tests.el
index 21f483a23a..3c6f0a0024 100644
--- a/test/lisp/sort-tests.el
+++ b/test/lisp/sort-tests.el
@@ -78,6 +78,12 @@ sort-tests-test-sorter-function
 (ert-deftest sort-tests--lines ()
   (sort-tests-test-sorter-function "\n" #'sort-lines))
 
+(ert-deftest sort-tests--lines/entire-buffer ()
+  (with-temp-buffer
+    (insert "C\nA\nB")
+    (call-interactively #'sort-lines)
+    (should (equal (buffer-string) "A\nB\nC"))))
+
 (ert-deftest sort-tests--paragraphs ()
   (let ((paragraph-separate "[\s\t\f]*$"))
     (sort-tests-test-sorter-function "\n\n" #'sort-paragraphs)))
-- 
2.28.0


  reply	other threads:[~2020-08-21  1:18 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-22  0:53 bug#13521: `sort-lines' on the empty region Xue Fuqiao
2013-01-22  3:19 ` Glenn Morris
2013-01-22  3:44   ` Xue Fuqiao
2013-01-22  3:44   ` Xue Fuqiao
2013-01-22 14:59 ` Stefan Monnier
2013-01-22 18:19   ` Glenn Morris
2013-01-22 19:04     ` Stefan Monnier
2020-08-21  1:18       ` Stefan Kangas [this message]
2020-08-21  6:22         ` Eli Zaretskii
2020-08-21  7:15           ` Stefan Kangas
2020-08-21  7:36             ` Eli Zaretskii
2020-09-05 11:13               ` Stefan Kangas
2020-09-05 11:29                 ` Eli Zaretskii
2021-05-10 11:25           ` Lars Ingebrigtsen
     [not found] <<20130122085354.75ced82538551655e724e0ea@gmail.com>
     [not found] ` <<jwvobgh2rkw.fsf-monnier+emacs@gnu.org>
     [not found]   ` <<i2ehhdaxnq.fsf@fencepost.gnu.org>
     [not found]     ` <<jwv1udd2gab.fsf-monnier+emacs@gnu.org>
     [not found]       ` <<CADwFkm=SDc3obOA_KXvtWOBRay+aWtnOtkKX5SGPwXGVB=+hYg@mail.gmail.com>
     [not found]         ` <<83mu2objro.fsf@gnu.org>
     [not found]           ` <<CADwFkmnf6ch6CBN13opEWDVnZYY-TaT236xg21Qg2KjWuUVz+w@mail.gmail.com>
     [not found]             ` <<83imdcbgc5.fsf@gnu.org>
     [not found]               ` <<CADwFkm=V7cUz2_5Xr8=k6EZbFK96pJu8wpo6uAA+H7ABDYdH0w@mail.gmail.com>
     [not found]                 ` <<83imcspim8.fsf@gnu.org>
2020-09-05 14:36                   ` Drew Adams
2020-09-05 14:51                     ` Eli Zaretskii
2020-09-05 15:34                       ` Stefan Monnier
     [not found] <<<20130122085354.75ced82538551655e724e0ea@gmail.com>
     [not found] ` <<<jwvobgh2rkw.fsf-monnier+emacs@gnu.org>
     [not found]   ` <<<i2ehhdaxnq.fsf@fencepost.gnu.org>
     [not found]     ` <<<jwv1udd2gab.fsf-monnier+emacs@gnu.org>
     [not found]       ` <<<CADwFkm=SDc3obOA_KXvtWOBRay+aWtnOtkKX5SGPwXGVB=+hYg@mail.gmail.com>
     [not found]         ` <<<83mu2objro.fsf@gnu.org>
     [not found]           ` <<<CADwFkmnf6ch6CBN13opEWDVnZYY-TaT236xg21Qg2KjWuUVz+w@mail.gmail.com>
     [not found]             ` <<<83imdcbgc5.fsf@gnu.org>
     [not found]               ` <<<CADwFkm=V7cUz2_5Xr8=k6EZbFK96pJu8wpo6uAA+H7ABDYdH0w@mail.gmail.com>
     [not found]                 ` <<<83imcspim8.fsf@gnu.org>
     [not found]                   ` <<3aad96d8-2503-40e8-b684-8ed18a089083@default>
     [not found]                     ` <<83a6y4p98g.fsf@gnu.org>
2020-09-05 15:22                       ` Drew Adams
2020-09-05 15:37                         ` Eli Zaretskii
     [not found] <<<<20130122085354.75ced82538551655e724e0ea@gmail.com>
     [not found] ` <<<<jwvobgh2rkw.fsf-monnier+emacs@gnu.org>
     [not found]   ` <<<<i2ehhdaxnq.fsf@fencepost.gnu.org>
     [not found]     ` <<<<jwv1udd2gab.fsf-monnier+emacs@gnu.org>
     [not found]       ` <<<<CADwFkm=SDc3obOA_KXvtWOBRay+aWtnOtkKX5SGPwXGVB=+hYg@mail.gmail.com>
     [not found]         ` <<<<83mu2objro.fsf@gnu.org>
     [not found]           ` <<<<CADwFkmnf6ch6CBN13opEWDVnZYY-TaT236xg21Qg2KjWuUVz+w@mail.gmail.com>
     [not found]             ` <<<<83imdcbgc5.fsf@gnu.org>
     [not found]               ` <<<<CADwFkm=V7cUz2_5Xr8=k6EZbFK96pJu8wpo6uAA+H7ABDYdH0w@mail.gmail.com>
     [not found]                 ` <<<<83imcspim8.fsf@gnu.org>
     [not found]                   ` <<<3aad96d8-2503-40e8-b684-8ed18a089083@default>
     [not found]                     ` <<<83a6y4p98g.fsf@gnu.org>
     [not found]                       ` <<7cfc062e-05b2-43f1-8714-19ebd955b88c@default>
     [not found]                         ` <<837dt8p74z.fsf@gnu.org>
2020-09-05 15:46                           ` Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CADwFkm=SDc3obOA_KXvtWOBRay+aWtnOtkKX5SGPwXGVB=+hYg@mail.gmail.com' \
    --to=stefan@marxist.se \
    --cc=13521@debbugs.gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=rgm@gnu.org \
    --cc=xfq.free@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).