* PATCH: vi-add-numeric
@ 2007-03-10 23:06 arno.
2007-03-11 10:04 ` arno.
0 siblings, 1 reply; 2+ messages in thread
From: arno. @ 2007-03-10 23:06 UTC (permalink / raw)
To: zsh-workers
[-- Attachment #1: Type: text/plain, Size: 3222 bytes --]
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 #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: PATCH: vi-add-numeric
2007-03-10 23:06 PATCH: vi-add-numeric arno.
@ 2007-03-11 10:04 ` arno.
0 siblings, 0 replies; 2+ messages in thread
From: arno. @ 2007-03-11 10:04 UTC (permalink / raw)
To: zsh-workers
[-- Attachment #1: Type: text/plain, Size: 2350 bytes --]
Le dimanche 11 mars 2007, à 00:06:18 +0100, arno. a écrit :
> Hi, here are two zle widgets
>
Oups, negative number management was quite messy.
Here is a better version
arno
Index: Functions/Zle/vi-add-numeric
--- /dev/null
+++ Functions/Zle/vi-add-numeric
@@ -0,0 +1,74 @@
+# 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
+ num=$((0 - num))
+ ((pos--))
+fi
+
+# computes result
+newnum=$((num + ${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
+
+# 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 #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2007-03-11 10:05 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-10 23:06 PATCH: vi-add-numeric arno.
2007-03-11 10:04 ` arno.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).