From: Remington Furman Date: Fri, 16 Jul 2021 09:47:36 +0000 (+0200) Subject: Make `number-at-point' work for more hex numbers X-Git-Tag: emacs-28.0.90~1844 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=865535a24cd07efee3c2d323de6e9baae8bc817d;p=emacs.git Make `number-at-point' work for more hex numbers * lisp/thingatpt.el (number-at-point): Rewrite to actually catch the hex numbers (bug#49588). Copyright-paperwork-exempt: yes --- diff --git a/lisp/thingatpt.el b/lisp/thingatpt.el index 8ca0f429ca1..4c2470fbcb6 100644 --- a/lisp/thingatpt.el +++ b/lisp/thingatpt.el @@ -677,14 +677,14 @@ Signal an error if the entire string was not used." "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 diff --git a/test/lisp/thingatpt-tests.el b/test/lisp/thingatpt-tests.el index 07eb8bb250e..fba6f21d5dc 100644 --- a/test/lisp/thingatpt-tests.el +++ b/test/lisp/thingatpt-tests.el @@ -190,4 +190,37 @@ position to retrieve THING.") (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