]> git.eshelyaron.com Git - emacs.git/commitdiff
Add bounds-of-thing-at-point property for 'number'
authorVisuwesh <visuweshm@gmail.com>
Sat, 9 Mar 2024 09:47:26 +0000 (15:17 +0530)
committerEshel Yaron <me@eshelyaron.com>
Fri, 15 Mar 2024 09:04:09 +0000 (10:04 +0100)
* lisp/thingatpt.el (thing-at-point-decimal-regexp)
(thing-at-point-hexadecimal-regexp): Extract regexps from...
(number-at-point): ...here.  Use them in 'number-at-point'.
(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)

(cherry picked from commit cb9ee24ea69be4a70f68cb2d564b23a55cb84216)

lisp/thingatpt.el

index 83ddc640d35db3d66f6cb5cba154591af68ab0cd..7896ad984df716700af8d44261f7053a13d6c82b 100644 (file)
@@ -735,20 +735,33 @@ Signal an error if the entire string was not used."
   (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)