From 2ded5938753a2fac94e5aebce2e97fc5b4d91076 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Thu, 3 Feb 2022 20:33:58 +0100 Subject: [PATCH] Make sort-numeric-fields resilient towards blank lines * lisp/sort.el (sort-numeric-fields): Don't signal an error on blank lines (bug#31800). --- lisp/sort.el | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/lisp/sort.el b/lisp/sort.el index eb8e2787d1e..90eee01caf4 100644 --- a/lisp/sort.el +++ b/lisp/sort.el @@ -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) -- 2.39.5