unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#21785: 25.0.50; sort-fields fails if region ends in a newline and sort-paragraph fails if it doesn't
@ 2015-10-29 12:38 Artur Malabarba
  2019-08-01 18:38 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 3+ messages in thread
From: Artur Malabarba @ 2015-10-29 12:38 UTC (permalink / raw)
  To: 21785

I noticed this while writing tests for sort.el

With the following buffer contents (the text between the dashes,
including a final newline), issue `C-x h M-x sort-numeric-fields'.
You'll get the error that the last line doesn't have enough fields. I
think that error should not be thrown because of a final newline.

---------------------
1 hi
2 bye

----------------------

Meanwhile take the following buffer contents,
--------------------
paragraph 2

paragraph 1
--------------------

If you issue `sort-paragraphs' on the whole buffer, you'll get this:
--------------------
paragraph 1
paragraph 2

-------------------

In my opinion both behavior are bugs. But surely one of them is, as
the functions should be somewhat consistent on demanding final
newlines.





^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#21785: 25.0.50; sort-fields fails if region ends in a newline and sort-paragraph fails if it doesn't
  2015-10-29 12:38 bug#21785: 25.0.50; sort-fields fails if region ends in a newline and sort-paragraph fails if it doesn't Artur Malabarba
@ 2019-08-01 18:38 ` Lars Ingebrigtsen
  2019-08-23  1:44   ` Lars Ingebrigtsen
  0 siblings, 1 reply; 3+ messages in thread
From: Lars Ingebrigtsen @ 2019-08-01 18:38 UTC (permalink / raw)
  To: Artur Malabarba; +Cc: 21785

(I'm going through old bug reports that have unfortunately not gotten
any responses.)

Artur Malabarba <bruce.connor.am@gmail.com> writes:

> I noticed this while writing tests for sort.el
>
> With the following buffer contents (the text between the dashes,
> including a final newline), issue `C-x h M-x sort-numeric-fields'.
> You'll get the error that the last line doesn't have enough fields. I
> think that error should not be thrown because of a final newline.
>
> ---------------------
> 1 hi
> 2 bye
>
> ----------------------
>
> Meanwhile take the following buffer contents,
> --------------------
> paragraph 2
>
> paragraph 1
> --------------------
>
> If you issue `sort-paragraphs' on the whole buffer, you'll get this:
> --------------------
> paragraph 1
> paragraph 2
>
> -------------------
>
> In my opinion both behavior are bugs. But surely one of them is, as
> the functions should be somewhat consistent on demanding final
> newlines.

I don't really think this first is a bug. You've asked Emacs to sort
numeric fields, and one of the lines in question doesn't have a numeric
field, so bugging out seems like what's required.

The second does seem like a bug.  If the final paragraph doesn't end in
a newline, it's squashed into the following paragraph after sorting.

The following patch fixes the problem (by adding a newline in these
cases).  Does this seems like the correct approach for everybody?

diff --git a/lisp/sort.el b/lisp/sort.el
index 6ea1c44060..6ceda8e448 100644
--- a/lisp/sort.el
+++ b/lisp/sort.el
@@ -225,11 +225,17 @@ sort-paragraphs
       (narrow-to-region beg end)
       (goto-char (point-min))
       (sort-subr reverse
-		 (function
-		  (lambda ()
-		    (while (and (not (eobp)) (looking-at paragraph-separate))
-		      (forward-line 1))))
-		 'forward-paragraph))))
+		 (lambda ()
+		   (while (and (not (eobp)) (looking-at paragraph-separate))
+		     (forward-line 1)))
+		 (lambda ()
+                   (forward-paragraph)
+                   ;; If the buffer doesn't end with a newline, add a
+                   ;; newline to avoid having paragraphs being
+                   ;; concatenated after sorting.
+                   (when (and (eobp)
+                              (not (bolp)))
+                     (insert "\n")))))))
 
 ;;;###autoload
 (defun sort-pages (reverse beg end)


-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply related	[flat|nested] 3+ messages in thread

* bug#21785: 25.0.50; sort-fields fails if region ends in a newline and sort-paragraph fails if it doesn't
  2019-08-01 18:38 ` Lars Ingebrigtsen
@ 2019-08-23  1:44   ` Lars Ingebrigtsen
  0 siblings, 0 replies; 3+ messages in thread
From: Lars Ingebrigtsen @ 2019-08-23  1:44 UTC (permalink / raw)
  To: Artur Malabarba; +Cc: 21785

Lars Ingebrigtsen <larsi@gnus.org> writes:

> The second does seem like a bug.  If the final paragraph doesn't end in
> a newline, it's squashed into the following paragraph after sorting.
>
> The following patch fixes the problem (by adding a newline in these
> cases).  Does this seems like the correct approach for everybody?

There were no comments, so I've applied the patch.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-08-23  1:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-29 12:38 bug#21785: 25.0.50; sort-fields fails if region ends in a newline and sort-paragraph fails if it doesn't Artur Malabarba
2019-08-01 18:38 ` Lars Ingebrigtsen
2019-08-23  1:44   ` Lars Ingebrigtsen

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).