1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-12-13 09:32:47 +00:00

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
This commit is contained in:
Remington Furman 2021-07-16 11:47:36 +02:00 committed by Lars Ingebrigtsen
parent dc85ffffc8
commit 865535a24c
2 changed files with 41 additions and 8 deletions

View File

@ -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

View File

@ -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