branch: elpa/evil-numbers
commit caa467981427431a61f403090a2308fb51525db8
Merge: e40b606d92 6e13c0450a
Author: Michael Markert <[email protected]>
Commit: Michael Markert <[email protected]>
Merge pull request #3 from mlf176f2/evil-numbers-in-region
Allow region processing.
---
README.org | 4 +++
evil-numbers.el | 85 +++++++++++++++++++++++++++++++++++++--------------------
2 files changed, 59 insertions(+), 30 deletions(-)
diff --git a/README.org b/README.org
index 2e073a4e50..e0b8516c2d 100644
--- a/README.org
+++ b/README.org
@@ -4,6 +4,10 @@
- works like C-a/C-x in vim, i.e. searches for number up to eol and then
increments or decrements and keep zero padding up (unlike in vim)
+ - When a region is active, as in evil's visual mode, all the
+ numbers within that region will be incremented/decremented (unlike
+ in vim)
+
** Detected Literals
- binary, e.g. =0b0101=, =0B0101=
- octal, e.g. =0o755=, =0O700=
diff --git a/evil-numbers.el b/evil-numbers.el
index d92843c288..1c3bede879 100644
--- a/evil-numbers.el
+++ b/evil-numbers.el
@@ -53,40 +53,65 @@
;;; Code:
;;;###autoload
-(defun evil-numbers/inc-at-pt (amount)
- "Increment the number at point or after point before end-of-line by `amount'"
+(defun evil-numbers/inc-at-pt (amount &optional no-region)
+ "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'
+
+NO-REGION is internal flag that allows
+`evil-numbers/inc-at-point' to be called recursively when
+applying the regional features of `evil-numbers/inc-at-point'.
+
+"
(interactive "p*")
- (save-match-data
- (if (not (evil-numbers/search-number))
- (error "No number at point or until end of line")
- (or
- ;; find binary literals
- (evil-numbers/search-and-replace "0[bB][01]*" "01" "\\([01]+\\)" amount
2)
-
- ;; find octal literals
- (evil-numbers/search-and-replace "0[oO][0-7]*" "01234567"
"\\([0-7]+\\)" amount 8)
-
- ;; find hex literals
- (evil-numbers/search-and-replace "0[xX][0-9a-fA-F]*"
- "0123456789abcdefABCDEF"
- "\\([0-9a-fA-F]+\\)" amount 16)
-
- ;; find decimal literals
- (progn
- (skip-chars-backward "0123456789")
- (skip-chars-backward "-")
- (when (looking-at "-?\\([0-9]+\\)")
- (replace-match
- (format (format "%%0%dd" (- (match-end 1) (match-beginning 1)))
- (+ amount (string-to-number (match-string 0) 10))))
- ;; Moves point one position back to conform with Vim
- (forward-char -1)
- t))
- (error "No number at point or until end of line")))))
+ (cond
+ ((and (not no-region) (region-active-p))
+ (let (deactivate-mark
+ (rb (region-beginning))
+ (re (region-end)))
+ (save-excursion
+ (save-match-data
+ (goto-char rb)
+ (while (re-search-forward
"\\(?:0\\(?:[Bb][01]+\\|[Oo][0-7]+\\|[Xx][0-9A-Fa-f]+\\)\\|-?[0-9]+\\)" re t)
+ (evil-numbers/inc-at-pt amount 'no-region)
+ ;; Undo vim compatability.
+ (forward-char 1)))))
+ (setq deactivate-mark t))
+ (t
+ (save-match-data
+ (if (not (evil-numbers/search-number))
+ (error "No number at point or until end of line")
+ (or
+ ;; find binary literals
+ (evil-numbers/search-and-replace "0[bB][01]+" "01" "\\([01]+\\)"
amount 2)
+
+ ;; find octal literals
+ (evil-numbers/search-and-replace "0[oO][0-7]+" "01234567"
"\\([0-7]+\\)" amount 8)
+
+ ;; find hex literals
+ (evil-numbers/search-and-replace "0[xX][0-9a-fA-F]*"
+ "0123456789abcdefABCDEF"
+ "\\([0-9a-fA-F]+\\)" amount 16)
+
+ ;; find decimal literals
+ (progn
+ (skip-chars-backward "0123456789")
+ (skip-chars-backward "-")
+ (when (looking-at "-?\\([0-9]+\\)")
+ (replace-match
+ (format (format "%%0%dd" (- (match-end 1) (match-beginning 1)))
+ (+ amount (string-to-number (match-string 0) 10))))
+ ;; Moves point one position back to conform with Vim
+ (forward-char -1)
+ t))
+ (error "No number at point or until end of line")))))))
;;;###autoload
(defun evil-numbers/dec-at-pt (amount)
- "Decrement the number at point or after point before end-of-line by `amount'"
+ "Decrement the number at point or after point before end-of-line by `amount'.
+
+If a region is active, decrement all the numbers at a point by `amount'.
+
+This function uses `evil-numbers/inc-at-pt'"
(interactive "p*")
(evil-numbers/inc-at-pt (- amount)))