Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: vi-add-numeric



Hi, here are two zle widgets

They implement Ctrl-A and Ctrl-X (add and substract of vim): add (or 
substract) one (or more if an argument is given) to the current or next 
number on the command line.

It is quite similar to widget "incarg", but when I discovered that 
widget, I had nearly finished programming my function. I still borrowed 
from it the incarg parameter, and a few phrases in comments. But those 
functions don't behave exactly the same, so maybe vi-add-numeric can be 
useful to some.

arno

Index: Functions/Zle/vi-add-numeric
--- /dev/null
+++ Functions/Zle/vi-add-numeric
@@ -0,0 +1,95 @@
+# This function acts like add command in vim (^A in command mode)
+# Only works now for decimal numbers
+#
+# If cursor is on any digit of a number, increment that number.
+# If cursor is not on a number, find the next number on the right and increment
+# it.
+# Cursor is then placed on the last digit of that number.
+#
+# If a numeric argument is given, increment the number by that argument.
+# Otherwise, increment by shell parameter incarg (it can be useful to set that
+# parameter if you plan to do a lot with a particular number). Otherwise,
+# increment by 1.
+#
+# examples:
+# echo 41 43
+# ^^^^^^^ cursor anywhere here
+# ->
+# echo 42 43
+#       ^ cursor here now
+#
+#
+# echo 41 43
+#        ^^^ cursor anywhere here
+# ->
+# echo 42 43
+#          ^ cursor here now
+#
+
+emulate -L zsh
+setopt extendedglob
+
+local pos num newnu sign buf
+
+if [[ $BUFFER[$((CURSOR + 1))] = [0-9] ]]; then
+
+    # cursor is on a number; search its beginning to the left
+    pos=$((${#LBUFFER%%[0-9]##} + 1))
+
+else
+
+    # cursor is not on a number; search a number to the right
+    pos=$(($CURSOR + ${#RBUFFER%%[0-9]*} + 1))
+
+fi
+
+# no number found in BUFFER
+(($pos <= ${#BUFFER}))  || return
+
+
+# ok, get the number now
+num=${${BUFFER[$pos,-1]}%%[^0-9]*}
+
+# checks if number is negative
+if ((pos > 0)) && [ $BUFFER[$((pos - 1))] = '-' ]; then
+    sign="-"
+else
+    sign="+"
+fi
+
+# computes result
+newnum=$((num $sign ${NUMERIC:-${incarg:-1}}))
+
+
+# replaces old number by result
+if ((pos > 1)); then
+    buf=${BUFFER[0,$((pos - 1))]}${BUFFER[$pos,-1]/$num/$newnum}
+else
+    buf=${BUFFER/$num/$newnum}
+fi
+
+
+# going from negative to zero or positive
+if [ $sign = '-' ] && ((newnum <= 0)); then
+    if ((newnum == 0)); then
+
+        # removes obsolete minus sign
+        buf[$((pos - 1))]=""
+        ((pos-=1)) # adjust pos for cursor position computation
+
+    elif ((newnum < 0)); then
+
+        # removes obsolete minus sign
+        buf[$((pos - 1))]=""
+        # minus sign computed in newnum
+        buf[$((pos - 1))]=""
+        ((pos-=2)) # adjust pos for cursor position computation
+
+    fi
+fi
+
+# assign computed string to buffer
+BUFFER=$buf
+
+# places cursor on last digit of number
+CURSOR=$((pos + $#newnum - 2))
Index: Functions/Zle/vi-substract-numeric
--- /dev/null
+++ Functions/Zle/vi-substract-numeric
@@ -0,0 +1,4 @@
+emulate -L zsh
+autoload -U vi-add-numeric
+NUMERIC=$((0 - ${NUMERIC:-${incarg:-1}}))
+vi-add-numeric

Attachment: signature.asc
Description: Digital signature



Messages sorted by: Reverse Date, Date, Thread, Author