changeset 27468:8a6ee5b485d2

(sort-numeric-base): New option. (sort-numeric-fields): If number starts with `0' or `0[xX[', interpret it as octal or hexadecimal. Use sort-numeric-base as default base.
author Gerd Moellmann <gerd@gnu.org>
date Fri, 28 Jan 2000 12:47:38 +0000
parents 0199b90f15c3
children 98f24cb3efa5
files lisp/sort.el
diffstat 1 files changed, 25 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/sort.el	Fri Jan 28 11:58:22 2000 +0000
+++ b/lisp/sort.el	Fri Jan 28 12:47:38 2000 +0000
@@ -258,26 +258,40 @@
     (modify-syntax-entry ?\. "_" table)	; for floating pt. numbers. -wsr
     (setq sort-fields-syntax-table table)))
 
+(defcustom sort-numeric-base 10
+  "*The default base used by `sort-numeric-fields'."
+  :group 'sort
+  :type 'integer)
+
 ;;;###autoload
 (defun sort-numeric-fields (field beg end)
   "Sort lines in region numerically by the ARGth field of each line.
 Fields are separated by whitespace and numbered from 1 up.
-Specified field must contain a number in each line of the region.
+Specified field must contain a number in each line of the region,
+which may begin with \"0x\" or \"0\" for hexadecimal and octal values.
+Otherwise, the number is interpreted according to sort-numeric-base.
 With a negative arg, sorts by the ARGth field counted from the right.
 Called from a program, there are three arguments:
 FIELD, BEG and END.  BEG and END specify region to sort."
   (interactive "p\nr")
   (sort-fields-1 field beg end
-		 (function (lambda ()
-			     (sort-skip-fields field)
-			     (string-to-number
-			      (buffer-substring
-			        (point)
-				(save-excursion
-				  ;; This is just wrong! Even without floats...
-				  ;; (skip-chars-forward "[0-9]")
-				  (forward-sexp 1)
-				  (point))))))
+		 (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))
 
 ;;;;;###autoload