"Return the number at point, or nil if none is found.
Decimal numbers like \"14\" or \"-14.5\", as well as hex numbers
like \"0xBEEF09\" or \"#xBEEF09\", are recognized."
- (when (thing-at-point-looking-at
- "\\(-?[0-9]+\\.?[0-9]*\\)\\|\\(0x\\|#x\\)\\([a-zA-Z0-9]+\\)" 500)
- (if (match-beginning 1)
- (string-to-number
- (buffer-substring (match-beginning 1) (match-end 1)))
- (string-to-number
- (buffer-substring (match-beginning 3) (match-end 3))
- 16))))
+ (cond
+ ((thing-at-point-looking-at "\\(0x\\|#x\\)\\([a-fA-F0-9]+\\)" 500)
+ (string-to-number
+ (buffer-substring (match-beginning 2) (match-end 2))
+ 16))
+ ((thing-at-point-looking-at "-?[0-9]+\\.?[0-9]*" 500)
+ (string-to-number
+ (buffer-substring (match-beginning 0) (match-end 0))))))
(put 'number 'thing-at-point 'number-at-point)
;;;###autoload
(goto-char 2)
(should (eq (symbol-at-point) nil))))
+(defun test--number (number pos)
+ (with-temp-buffer
+ (insert (format "%s\n" number))
+ (goto-char (point-min))
+ (forward-char pos)
+ (number-at-point)))
+
+(ert-deftest test-numbers-none ()
+ (should (equal (test--number "foo" 0) nil)))
+
+(ert-deftest test-numbers-decimal ()
+ (should (equal (test--number "42" 0) 42))
+ (should (equal (test--number "42" 1) 42))
+ (should (equal (test--number "42" 2) 42)))
+
+(ert-deftest test-numbers-hex-lisp ()
+ (should (equal (test--number "#x42" 0) 66))
+ (should (equal (test--number "#x42" 1) 66))
+ (should (equal (test--number "#x42" 2) 66))
+ (should (equal (test--number "#xf00" 0) 3840))
+ (should (equal (test--number "#xf00" 1) 3840))
+ (should (equal (test--number "#xf00" 2) 3840))
+ (should (equal (test--number "#xf00" 3) 3840)))
+
+(ert-deftest test-numbers-hex-c ()
+ (should (equal (test--number "0x42" 0) 66))
+ (should (equal (test--number "0x42" 1) 66))
+ (should (equal (test--number "0x42" 2) 66))
+ (should (equal (test--number "0xf00" 0) 3840))
+ (should (equal (test--number "0xf00" 1) 3840))
+ (should (equal (test--number "0xf00" 2) 3840))
+ (should (equal (test--number "0xf00" 3) 3840)))
+
;;; thingatpt.el ends here