summaryrefslogtreecommitdiff
path: root/lisp/sort.el
diff options
context:
space:
mode:
authorGerd Moellmann <gerd@gnu.org>2000-01-28 12:47:38 +0000
committerGerd Moellmann <gerd@gnu.org>2000-01-28 12:47:38 +0000
commit5d6dd9631751565b4f6037d35bd93d4c2739338b (patch)
tree3021004f3c9c04ee6f33957fc13b7ecbfb298324 /lisp/sort.el
parent3b43c01c60b4baf73c5d1813715e62e89dcdc544 (diff)
downloademacs-5d6dd9631751565b4f6037d35bd93d4c2739338b.tar.gz
(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.
Diffstat (limited to 'lisp/sort.el')
-rw-r--r--lisp/sort.el36
1 files changed, 25 insertions, 11 deletions
diff --git a/lisp/sort.el b/lisp/sort.el
index 886d77eb40c..dc1cc453e22 100644
--- a/lisp/sort.el
+++ b/lisp/sort.el
@@ -258,26 +258,40 @@ the sort order."
(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