[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/evil-numbers 7602f3b484 101/145: Cleanup: split number inc
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/evil-numbers 7602f3b484 101/145: Cleanup: split number incrementing into it's own function |
Date: |
Thu, 6 Jan 2022 03:00:22 -0500 (EST) |
branch: elpa/evil-numbers
commit 7602f3b4842f4ec149d3b8ed32c9da9f771597c3
Author: Campbell Barton <ideasman42@gmail.com>
Commit: Campbell Barton <ideasman42@gmail.com>
Cleanup: split number incrementing into it's own function
This simplifies evil-numbers/dec-at-pt point placement and error
handling, as this logic is called direction and in a loop.
---
evil-numbers.el | 212 ++++++++++++++++++++++++++------------------------------
1 file changed, 97 insertions(+), 115 deletions(-)
diff --git a/evil-numbers.el b/evil-numbers.el
index dc151fb73b..1cb4879b49 100644
--- a/evil-numbers.el
+++ b/evil-numbers.el
@@ -128,9 +128,78 @@
(evil-numbers--translate-with-alist
(evil-numbers--swap-alist evil-numbers--subscript-alist) x))
+(defun evil-numbers--inc-at-pt-impl (amount beg end padded)
+ "Increment the number at the current POINT by AMOUNT limited by BEG and END.
+
+Keep padding when PADDED is non-nil.
+
+Return non-nil on success, leaving the point at the end of the number."
+ (save-match-data
+ (cond
+ ;; Failure (caller may error).
+ ((not (evil-numbers--search-number beg end))
+ nil)
+
+ ;; Find binary literals.
+ ((evil-numbers--search-and-replace
+ '(("+-" . *)
+ ("0" . 1)
+ ("bB" . 1)
+ ("01" . +))
+ 1 ;; Sign group.
+ 4 ;; Number group.
+ amount 2 padded)
+ t)
+
+ ;; Find octal literals.
+ ((evil-numbers--search-and-replace
+ '(("+-" . *)
+ ("0" . 1)
+ ("oO" . 1)
+ ("0-7" . +))
+ 1 ;; Sign group.
+ 4 ;; Number group.
+ amount 8 padded)
+ t)
+
+ ;; Find hex literals.
+ ((evil-numbers--search-and-replace
+ '(("+-" . *)
+ ("0" . 1)
+ ("xX" . 1)
+ ("[:xdigit:]" . +))
+ 1 ;; Sign group.
+ 4 ;; Number group.
+ amount 16 padded)
+ t)
+
+ ;; Find superscript literals.
+ ((evil-numbers--search-and-replace-decimal
+ amount padded
+ #'evil-numbers--decode-super
+ #'evil-numbers--encode-super)
+ t)
+
+ ;; Find subscript literals.
+ ((evil-numbers--search-and-replace-decimal
+ amount padded
+ #'evil-numbers--decode-sub
+ #'evil-numbers--encode-sub)
+ t)
+
+ ;; Find normal decimal literals.
+ ((evil-numbers--search-and-replace-decimal
+ amount padded
+ #'identity #'identity)
+ t)
+
+ ;; Failure (caller may error).
+ (t
+ nil))))
+
;;;###autoload (autoload 'evil-numbers/inc-at-pt "evil-numbers" nil t)
(evil-define-operator evil-numbers/inc-at-pt
- (amount beg end type &optional incremental padded no-error no-offset)
+ (amount beg end type &optional incremental padded)
"Increment the number at point or after point before `end-of-line' by AMOUNT.
When region is selected, increment all numbers in the region by AMOUNT
@@ -147,15 +216,7 @@ behaviour set by `evil-numbers/pad-default', t is the
opposite of
`evil-numbers/pad-default', '(t) enables padding and '(nil) disables padding.
Numbers with a leading zero are always padded. Signs are preserved when padding
is enabled, i.e. increasing a negative number to a positive will result in a
-number with a + sign.
-
-NO-ERROR when non-nil, don't call error when no number is found.
-
-NO-OFFSET don't apply offsets, expected for VIM like behavior.
-- Don't move the character back one at the end of the word.
-- Don't move the character forward one to prevent the previous word
- from matching.
- This simplifies calling this function in a loop."
+number with a + sign."
:motion nil
(interactive "*<c><R>")
@@ -170,112 +231,33 @@ NO-OFFSET don't apply offsets, expected for VIM like
behavior.
((and beg end type)
(let ((count 1))
(save-excursion
- (save-match-data
- (funcall
- (if (eq type 'block)
- (lambda (f) (evil-apply-on-block f beg end nil))
- (lambda (f) (funcall f beg end)))
- (lambda (beg end)
- (evil-with-restriction beg end
- (goto-char beg)
- (while (evil-numbers/inc-at-pt
- (* amount count)
- ;; Don't search back before this point.
- (point)
- ;; Ignore values after this point.
- ;; Rely on narrowing, don't use `end' since
- ;; edits cause the absolute maximum point
- ;; to change while looping.
- (point-max)
- ;; Type is handled here, ignore.
- nil
- ;; Incremental is handled here, ignore.
- nil
- ;; Causes padded to be used as-is,
- ;; without modification.
- (list padded)
- ;; No error, silently fails once there are
- ;; no other numbers to operate on.
- ;; Or even if there were no numbers
- ;; (this matches VIM's behavior).
- t
- ;; No offset, simplifies stepping in this loop.
- t)
- (when incremental
- (setq count (+ count 1)))))))))))
+ (funcall
+ (if (eq type 'block)
+ (lambda (f) (evil-apply-on-block f beg end nil))
+ (lambda (f) (funcall f beg end)))
+ (lambda (beg end)
+ (evil-with-restriction beg end
+ (goto-char beg)
+ (while (evil-numbers--inc-at-pt-impl
+ amount (point) (point-max) padded)
+ (when incremental
+ (setq count (+ count 1))))))))))
+
+ ;; Handle the simple case, either the cursor is over a number,
+ ;; or a number exists between the cursor and `end-of-line'.
(t
- (let ((point-next nil))
- (save-excursion
- (save-match-data
- (cond
- ((not (evil-numbers--search-number
- (or beg (point-at-bol))
- (or end (point-at-eol))))
- (unless no-error
- (error "No number at point or until end of line")))
-
- ;; Find binary literals.
- ((evil-numbers--search-and-replace
- '(("+-" . *)
- ("0" . 1)
- ("bB" . 1)
- ("01" . +))
- 1 ;; Sign group.
- 4 ;; Number group.
- amount 2 padded)
- (setq point-next (point)))
-
- ;; Find octal literals.
- ((evil-numbers--search-and-replace
- '(("+-" . *)
- ("0" . 1)
- ("oO" . 1)
- ("0-7" . +))
- 1 ;; Sign group.
- 4 ;; Number group.
- amount 8 padded)
- (setq point-next (point)))
-
- ;; Find hex literals.
- ((evil-numbers--search-and-replace
- '(("+-" . *)
- ("0" . 1)
- ("xX" . 1)
- ("[:xdigit:]" . +))
- 1 ;; Sign group.
- 4 ;; Number group.
- amount 16 padded)
- (setq point-next (point)))
-
- ;; Find superscript literals.
- ((evil-numbers--search-and-replace-decimal
- amount padded
- #'evil-numbers--decode-super
- #'evil-numbers--encode-super)
- (setq point-next (point)))
-
- ;; Find subscript literals.
- ((evil-numbers--search-and-replace-decimal
- amount padded
- #'evil-numbers--decode-sub
- #'evil-numbers--encode-sub)
- (setq point-next (point)))
-
- ;; Find normal decimal literals.
- ((evil-numbers--search-and-replace-decimal
- amount padded
- #'identity #'identity)
- (setq point-next (point)))
-
- (t
- (unless no-error
- (error "No number at point or until end of line"))))))
-
- (when point-next
- (unless no-offset
- ;; Moves point one position back to conform with VIM.
- (setq point-next (1- point-next)))
- (goto-char point-next)
+ (let ((point-next
+ (save-excursion
+ (when (evil-numbers--inc-at-pt-impl
+ amount (point-at-bol) (point-at-eol) padded)
+ (point)))))
+
+ (if (null point-next)
+ (error "No number at point or until end of line")
+
+ ;; Moves point one position back to conform with VIM,
+ ;; see `evil-adjust-cursor' for details.
+ (goto-char (1- point-next))
t)))))
;;;###autoload (autoload 'evil-numbers/dec-at-pt "evil-numbers" nil t)
- [nongnu] elpa/evil-numbers 0ddb39d98e 027/145: Stray paren fix, (continued)
- [nongnu] elpa/evil-numbers 0ddb39d98e 027/145: Stray paren fix, ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers 780db06056 018/145: Tag version 0.2., ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers b754f53c82 039/145: support for increasing superscripts, ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers fb16206158 054/145: Fix autoloading, ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers 4308935a03 061/145: Resolve looking-back warnings, ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers aa94cd1b26 063/145: Merge pull request #11 from ideasman42/patch-1, ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers 5356111930 077/145: Cleanup: avoid let binding to set default values for arguments, ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers b54cc5ba35 076/145: Fix spelling, ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers c37a4cf92a 092/145: Fix padding being ignored with block selection, ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers e8dfba58c9 081/145: Cleanup: split skip characters matching out into a function, ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers 7602f3b484 101/145: Cleanup: split number incrementing into it's own function,
ELPA Syncer <=
- [nongnu] elpa/evil-numbers 3fe56219af 091/145: Cleanup: docstring, ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers b2397f278a 094/145: Cleanup: move forward character check in evil-numbers--search-number, ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers 6093a5e039 085/145: Cleanup: use identity functions, ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers b98a3dccd3 104/145: Cleanup: forward limits to evil-numbers--search-and-replace, ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers cb9eb99998 141/145: Tests: add simple-nop-non-number-with-newline-after test, ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers 29859e64c0 138/145: Add automated tests, resolve #22, ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers 95fd1faf90 006/145: Use `while' instead of `do'., ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers b84ee09de1 012/145: Fix license., ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers 83b2ad44ff 010/145: Use lowercase and uppercase example., ELPA Syncer, 2022/01/06
- [nongnu] elpa/evil-numbers bd2359c96f 003/145: Fix provide., ELPA Syncer, 2022/01/06