emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[nongnu] elpa/evil-numbers 445943fbce 082/145: Fix #17 bin/octal/hex num


From: ELPA Syncer
Subject: [nongnu] elpa/evil-numbers 445943fbce 082/145: Fix #17 bin/octal/hex numbers don't support becoming negative
Date: Thu, 6 Jan 2022 03:00:20 -0500 (EST)

branch: elpa/evil-numbers
commit 445943fbce4eb2045b13d6832b83a84c7041d738
Author: Campbell Barton <ideasman42@gmail.com>
Commit: Campbell Barton <ideasman42@gmail.com>

    Fix #17 bin/octal/hex numbers don't support becoming negative
    
    When decrementing 0x0 for eg. it became 0x-1 instead of -0x1.
---
 evil-numbers.el | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)

diff --git a/evil-numbers.el b/evil-numbers.el
index 8e49c7186c..e9681bf4d5 100644
--- a/evil-numbers.el
+++ b/evil-numbers.el
@@ -206,26 +206,32 @@ number with a + sign."
           (or
            ;; Find binary literals.
            (evil-numbers--search-and-replace
-            '(("0"  . 1)
+            '(("+-" . *)
+              ("0"  . 1)
               ("bB" . 1)
               ("01" . +))
-            3 ;; Number group.
+            1 ;; Sign group.
+            4 ;; Number group.
             amount 2)
 
            ;; Find octal literals.
            (evil-numbers--search-and-replace
-            '(("0"   . 1)
+            '(("+-"  . *)
+              ("0"   . 1)
               ("oO"  . 1)
               ("0-7" . +))
-            3 ;; Number group.
+            1 ;; Sign group.
+            4 ;; Number group.
             amount 8)
 
            ;; Find hex literals.
            (evil-numbers--search-and-replace
-            '(("0"          . 1)
+            '(("+-"         . *)
+              ("0"          . 1)
               ("xX"         . 1)
               ("[:xdigit:]" . +))
-            3 ;; Number group.
+            1 ;; Sign group.
+            4 ;; Number group.
             amount 16)
 
            ;; Find superscript literals.
@@ -394,7 +400,7 @@ Each item in SKIP-CHARS is a cons pair.
         (set-match-data match-list)))
     t))
 
-(defun evil-numbers--search-and-replace (skip-chars num-group inc base)
+(defun evil-numbers--search-and-replace (skip-chars sign-group num-group inc 
base)
   "Perform the increment/decrement on the current line.
 
 For SKIP-CHARS docs see `evil-numbers--match-from-skip-chars'.
@@ -413,18 +419,28 @@ replace number incremented by INC in BASE and return 
non-nil."
       (goto-char (match-end num-group))
       (let* ((num-prev
               (string-to-number
-               (match-string num-group)
+               (concat (match-string sign-group)
+                       (match-string num-group))
                base))
              (num-next (+ inc num-prev))
              (str-next
               (evil-numbers--format
-               num-next
+               (abs num-next)
                (if evil-numbers/padDefault
                    (- (match-end num-group)
                       (match-beginning num-group))
                  1)
                base)))
 
+        ;; Replace the sign (as needed).
+        (cond
+         ;; From negative to positive.
+         ((and (< num-prev 0) (not (< num-next 0)))
+          (replace-match "" t t nil sign-group))
+         ;; From positive to negative.
+         ((and (not (< num-prev 0)) (< num-next 0))
+          (replace-match "-" t t nil sign-group)))
+
         ;; Replace the number.
         (replace-match str-next t t nil num-group))
 



reply via email to

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