Subject: Re: bug#13521: `sort-lines' on the empty region

From: Stefan Kangas <[email-masked]>
Date: Thu, 20 Aug 2020 18:18:34 -0700

tags 13521 + patch
thanks

Stefan Monnier <[email-masked]> 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
From 6c1d6eb1dc8306f99fd01ba848213632ac6cd973 Mon Sep 17 00:00:00 2001
From: Stefan Kangas <[email-masked]>
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.
+
 
 * 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