aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemington Furman2021-07-16 11:47:36 +0200
committerLars Ingebrigtsen2021-07-16 11:47:36 +0200
commit865535a24cd07efee3c2d323de6e9baae8bc817d (patch)
tree8f3a56f94215063840733972b7aa3445c38173c8
parentdc85ffffc88c08742072573539f8bfae9dcbbccb (diff)
downloademacs-865535a24cd07efee3c2d323de6e9baae8bc817d.tar.gz
emacs-865535a24cd07efee3c2d323de6e9baae8bc817d.zip
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
-rw-r--r--lisp/thingatpt.el16
-rw-r--r--test/lisp/thingatpt-tests.el33
2 files changed, 41 insertions, 8 deletions
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."
677 "Return the number at point, or nil if none is found. 677 "Return the number at point, or nil if none is found.
678Decimal numbers like \"14\" or \"-14.5\", as well as hex numbers 678Decimal numbers like \"14\" or \"-14.5\", as well as hex numbers
679like \"0xBEEF09\" or \"#xBEEF09\", are recognized." 679like \"0xBEEF09\" or \"#xBEEF09\", are recognized."
680 (when (thing-at-point-looking-at 680 (cond
681 "\\(-?[0-9]+\\.?[0-9]*\\)\\|\\(0x\\|#x\\)\\([a-zA-Z0-9]+\\)" 500) 681 ((thing-at-point-looking-at "\\(0x\\|#x\\)\\([a-fA-F0-9]+\\)" 500)
682 (if (match-beginning 1) 682 (string-to-number
683 (string-to-number 683 (buffer-substring (match-beginning 2) (match-end 2))
684 (buffer-substring (match-beginning 1) (match-end 1))) 684 16))
685 (string-to-number 685 ((thing-at-point-looking-at "-?[0-9]+\\.?[0-9]*" 500)
686 (buffer-substring (match-beginning 3) (match-end 3)) 686 (string-to-number
687 16)))) 687 (buffer-substring (match-beginning 0) (match-end 0))))))
688 688
689(put 'number 'thing-at-point 'number-at-point) 689(put 'number 'thing-at-point 'number-at-point)
690;;;###autoload 690;;;###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.")
190 (goto-char 2) 190 (goto-char 2)
191 (should (eq (symbol-at-point) nil)))) 191 (should (eq (symbol-at-point) nil))))
192 192
193(defun test--number (number pos)
194 (with-temp-buffer
195 (insert (format "%s\n" number))
196 (goto-char (point-min))
197 (forward-char pos)
198 (number-at-point)))
199
200(ert-deftest test-numbers-none ()
201 (should (equal (test--number "foo" 0) nil)))
202
203(ert-deftest test-numbers-decimal ()
204 (should (equal (test--number "42" 0) 42))
205 (should (equal (test--number "42" 1) 42))
206 (should (equal (test--number "42" 2) 42)))
207
208(ert-deftest test-numbers-hex-lisp ()
209 (should (equal (test--number "#x42" 0) 66))
210 (should (equal (test--number "#x42" 1) 66))
211 (should (equal (test--number "#x42" 2) 66))
212 (should (equal (test--number "#xf00" 0) 3840))
213 (should (equal (test--number "#xf00" 1) 3840))
214 (should (equal (test--number "#xf00" 2) 3840))
215 (should (equal (test--number "#xf00" 3) 3840)))
216
217(ert-deftest test-numbers-hex-c ()
218 (should (equal (test--number "0x42" 0) 66))
219 (should (equal (test--number "0x42" 1) 66))
220 (should (equal (test--number "0x42" 2) 66))
221 (should (equal (test--number "0xf00" 0) 3840))
222 (should (equal (test--number "0xf00" 1) 3840))
223 (should (equal (test--number "0xf00" 2) 3840))
224 (should (equal (test--number "0xf00" 3) 3840)))
225
193;;; thingatpt.el ends here 226;;; thingatpt.el ends here