Subject: bug#69239: 30.0.50; number-at-point and bounds-of-thing-at-point disagree

From: Visuwesh <[email-masked]>
Date: Sat, 09 Mar 2024 15:18:03 +0530

[ஞாயிறு பிப்ரவரி 25, 2024] Eli Zaretskii wrote:

>> But if you want to be on the safer side, then I can write a patch that
>> doesn't use number-at-point.
>
> Yes, please.
>
> Thanks.

Sorry for the delay, life had me busy.  Please find attached patch.

>From f402a765365ec76f741d8eaffb75f4177abb1261 Mon Sep 17 00:00:00 2001
From: Visuwesh <[email-masked]>
Date: Sat, 9 Mar 2024 15:17:26 +0530
Subject: [PATCH] Add bounds-of-thing-at-point property for 'number'

* lisp/thingatpt.el (thing-at-point-decimal-regexp)
(thing-at-point-hexadecimal-regexp): Extract regexps from...
(number-at-point): here.  Use above.
(number): Add 'bounds-of-thing-at-point' property as
`forward-word' does not always return the right boundary e.g.,
in latex-mode buffers.  (bug#69239)
---
 lisp/thingatpt.el | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/lisp/thingatpt.el b/lisp/thingatpt.el
index 83ddc640d35..7896ad984df 100644
--- a/lisp/thingatpt.el
+++ b/lisp/thingatpt.el
@@ -735,20 +735,33 @@ symbol-at-point
   (let ((thing (thing-at-point 'symbol)))
     (if thing (intern thing))))
 
+(defvar thing-at-point-decimal-regexp
+  "-?[0-9]+\\.?[0-9]*"
+  "A regexp matching a decimal number.")
+
+(defvar thing-at-point-hexadecimal-regexp
+  "\\(0x\\|#x\\)\\([a-fA-F0-9]+\\)"
+  "A regexp matchin a hexadecimal number.")
+
 ;;;###autoload
 (defun number-at-point ()
   "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."
   (cond
-   ((thing-at-point-looking-at "\\(0x\\|#x\\)\\([a-fA-F0-9]+\\)" 500)
+   ((thing-at-point-looking-at thing-at-point-hexadecimal-regexp 500)
     (string-to-number
      (buffer-substring (match-beginning 2) (match-end 2))
      16))
-   ((thing-at-point-looking-at "-?[0-9]+\\.?[0-9]*" 500)
+   ((thing-at-point-looking-at thing-at-point-decimal-regexp 500)
     (string-to-number
      (buffer-substring (match-beginning 0) (match-end 0))))))
 
+(put 'number 'bounds-of-thing-at-point
+     (lambda ()
+       (and (or (thing-at-point-looking-at thing-at-point-hexadecimal-regexp 500)
+                (thing-at-point-looking-at thing-at-point-decimal-regexp 500))
+            (cons (match-beginning 0) (match-end 0)))))
 (put 'number 'forward-op 'forward-word)
 (put 'number 'thing-at-point 'number-at-point)
 
-- 
2.43.0