emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] master 385bb14: Make number-at-point recognize some hex nu


From: Lars Ingebrigtsen
Subject: [Emacs-diffs] master 385bb14: Make number-at-point recognize some hex numbers
Date: Fri, 20 Sep 2019 13:38:03 -0400 (EDT)

branch: master
commit 385bb140de767ff59b026f5540e0e8bfae53fb55
Author: Lars Ingebrigtsen <address@hidden>
Commit: Lars Ingebrigtsen <address@hidden>

    Make number-at-point recognize some hex numbers
    
    * lisp/thingatpt.el (number-at-point): Also return common hex
    numbers (bug#37458).
---
 etc/NEWS          |  4 ++++
 lisp/thingatpt.el | 15 +++++++++++----
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index c129e8a..567f3cb 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1662,6 +1662,10 @@ A symbol 'uuid' can be passed to 'thing-at-point' and it 
returns the
 UUID at point.
 
 ---
+*** 'number-at-point' will now recognize hex number like 0xAb09 and #xAb09
+and return them as numbers.
+
+---
 *** 'word-at-point' and 'sentence-at-point' accept NO-PROPERTIES.
 Just like 'thing-at-point' itself.
 
diff --git a/lisp/thingatpt.el b/lisp/thingatpt.el
index 319f4b2..1ce4b98 100644
--- a/lisp/thingatpt.el
+++ b/lisp/thingatpt.el
@@ -632,10 +632,17 @@ Signal an error if the entire string was not used."
     (if thing (intern thing))))
 ;;;###autoload
 (defun number-at-point ()
-  "Return the number at point, or nil if none is found."
-  (when (thing-at-point-looking-at "-?[0-9]+\\.?[0-9]*" 500)
-    (string-to-number
-     (buffer-substring (match-beginning 0) (match-end 0)))))
+  "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 regognized."
+  (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))))
 
 (put 'number 'thing-at-point 'number-at-point)
 ;;;###autoload



reply via email to

[Prev in Thread] Current Thread [Next in Thread]