]> git.eshelyaron.com Git - emacs.git/commitdiff
Make sort-numeric-fields resilient towards blank lines
authorLars Ingebrigtsen <larsi@gnus.org>
Thu, 3 Feb 2022 19:33:58 +0000 (20:33 +0100)
committerLars Ingebrigtsen <larsi@gnus.org>
Thu, 3 Feb 2022 19:33:58 +0000 (20:33 +0100)
* lisp/sort.el (sort-numeric-fields): Don't signal an error on
blank lines (bug#31800).

lisp/sort.el

index eb8e2787d1e38298b95149caf7302581e9bbc65a..90eee01caf4673e9287bdd800233b5d05a2c5986 100644 (file)
@@ -286,25 +286,30 @@ FIELD, BEG and END.  BEG and END specify region to sort."
   (interactive "p\nr")
   (let ;; To make `end-of-line' and etc. to ignore fields.
       ((inhibit-field-text-motion t))
-    (sort-fields-1 field beg end
-                  (lambda ()
-                    (sort-skip-fields field)
-                    (let* ((case-fold-search t)
-                           (base
-                            (if (looking-at "\\(0x\\)[0-9a-f]\\|\\(0\\)[0-7]")
-                                (cond ((match-beginning 1)
-                                       (goto-char (match-end 1))
-                                       16)
-                                      ((match-beginning 2)
-                                       (goto-char (match-end 2))
-                                       8)
-                                      (t nil)))))
-                      (string-to-number (buffer-substring (point)
-                                                          (save-excursion
-                                                            (forward-sexp 1)
-                                                            (point)))
-                                        (or base sort-numeric-base))))
-                  nil)))
+    (sort-fields-1
+     field beg end
+     (lambda ()
+       ;; Don't try to parse blank lines (they'll be
+       ;; sorted at the start).
+       (if (looking-at "[\t ]*$")
+           0
+        (sort-skip-fields field)
+        (let* ((case-fold-search t)
+               (base
+                (if (looking-at "\\(0x\\)[0-9a-f]\\|\\(0\\)[0-7]")
+                    (cond ((match-beginning 1)
+                           (goto-char (match-end 1))
+                           16)
+                          ((match-beginning 2)
+                           (goto-char (match-end 2))
+                           8)
+                          (t nil)))))
+          (string-to-number (buffer-substring (point)
+                                              (save-excursion
+                                                (forward-sexp 1)
+                                                (point)))
+                            (or base sort-numeric-base)))))
+     nil)))
 
 ;;;;;###autoload
 ;;(defun sort-float-fields (field beg end)