branch: elpa/evil-numbers
commit 48b528baec243d0dc860cdd5d20d0bdb83ffb456
Author: Campbell Barton <[email protected]>
Commit: Campbell Barton <[email protected]>
Refactor: instead of passing an amount, pass in a function
This opens up possibilities for changing the number in other ways
besides addition.
---
evil-numbers.el | 28 +++++++++++++++-------------
1 file changed, 15 insertions(+), 13 deletions(-)
diff --git a/evil-numbers.el b/evil-numbers.el
index 91d050eea6..94ac8225dd 100644
--- a/evil-numbers.el
+++ b/evil-numbers.el
@@ -372,10 +372,11 @@ Each item in MATCH-CHARS is a cons pair.
(defun evil-numbers--inc-at-pt-impl-with-match-chars
(match-chars
sign-group num-group
- amount base
+ base
beg end
padded
do-case
+ number-xform-fn
decode-fn encode-fn)
"Perform the increment/decrement on the current line.
@@ -386,8 +387,8 @@ SIGN-GROUP is the match group used for the sign ('-' or
'+').
When PADDED is non-nil,
the number keeps it's current width (with leading zeroes).
-When all characters are found in sequence,
-replace number incremented by AMOUNT in BASE and return non-nil."
+When all characters are found in sequence, evaluate the number in BASE,
+replacing it by the result of NUMBER-XFORM-FN and return non-nil."
(save-match-data
(when (save-excursion
;; Skip backwards (as needed), there may be no
@@ -410,7 +411,7 @@ replace number incremented by AMOUNT in BASE and return
non-nil."
str-prev))
(num-prev (string-to-number str-prev-strip base))
- (num-next (+ amount num-prev))
+ (num-next (funcall number-xform-fn num-prev))
(str-next
(evil-numbers--format
(abs num-next)
@@ -451,7 +452,7 @@ replace number incremented by AMOUNT in BASE and return
non-nil."
t)))
-(defun evil-numbers--inc-at-pt-impl (amount beg end padded)
+(defun evil-numbers--inc-at-pt-impl (beg end padded number-xform-fn)
"Increment the number at the current POINT by AMOUNT limited by BEG and END.
Keep padding when PADDED is non-nil.
@@ -463,15 +464,16 @@ Return non-nil on success, leaving the point at the end
of the number."
(evil-numbers--inc-at-pt-impl-with-match-chars
`(("+-" \?) ("0" 1) ("bB" 1) ("01" + ,evil-numbers-separator-chars))
1 4 ;; Sign & number groups.
- amount 2 beg end padded nil
+ 2 beg end padded nil number-xform-fn
#'identity #'identity)
;; Find octal literals:
;; 0[oO][0-7]+, e.g. 0o42 or 0O5
(evil-numbers--inc-at-pt-impl-with-match-chars
+
`(("+-" \?) ("0" 1) ("oO" 1) ("0-7" + ,evil-numbers-separator-chars))
1 4 ;; Sign & number groups.
- amount 8 beg end padded nil
+ 8 beg end padded nil number-xform-fn
#'identity #'identity)
;; Find hex literals:
@@ -479,7 +481,7 @@ Return non-nil on success, leaving the point at the end of
the number."
(evil-numbers--inc-at-pt-impl-with-match-chars
`(("+-" \?) ("0" 1) ("xX" 1) ("[:xdigit:]" +
,evil-numbers-separator-chars))
1 4 ;; Sign & number groups.
- amount 16 beg end padded t
+ 16 beg end padded t number-xform-fn
#'identity #'identity)
;; Find decimal literals:
@@ -487,21 +489,21 @@ Return non-nil on success, leaving the point at the end
of the number."
(evil-numbers--inc-at-pt-impl-with-match-chars
`(("+-" \?) ("0123456789" + ,evil-numbers-separator-chars))
1 2 ;; Sign & number groups.
- amount 10 beg end padded nil
+ 10 beg end padded nil number-xform-fn
#'identity #'identity)
;; Find decimal literals (super-script).
(evil-numbers--inc-at-pt-impl-with-match-chars
`(("⁺⁻" \?) (,evil-numbers--chars-superscript + nil))
1 2 ;; Sign & number groups.
- amount 10 beg end padded nil
+ 10 beg end padded nil number-xform-fn
#'evil-numbers--decode-super #'evil-numbers--encode-super)
;; Find decimal literals (sub-script).
(evil-numbers--inc-at-pt-impl-with-match-chars
`(("₊₋" \?) (,evil-numbers--chars-subscript + nil))
1 2 ;; Sign & number groups.
- amount 10 beg end padded nil
+ 10 beg end padded nil number-xform-fn
#'evil-numbers--decode-sub #'evil-numbers--encode-sub)))
(defun evil-numbers--inc-at-pt-impl-with-search (amount beg end padded)
@@ -522,14 +524,14 @@ Return non-nil on success, leaving the point at the end
of the number."
;; Found item, exit the loop.
(null
(when (evil-numbers--inc-at-pt-impl
- amount
;; Clamp limits to line bounds.
;; The caller may use a range that spans lines to
;; allow searching and finding items across
;; multiple lines (currently used for selection).
(max beg (point-at-bol))
(min end (point-at-eol))
- padded)
+ padded
+ #'(lambda (n) (+ n amount)))
(setq found t)))
;; Search failed, exit the loop.