From 9730e07763a81b622276488c97d7787e0998bf9e Mon Sep 17 00:00:00 2001 From: midchildan Date: Sun, 28 Jan 2024 04:03:53 +0900 Subject: [PATCH] fix(zsh/vim-incarg): incorrect number of leading zeros when signs change --- .../share/zsh/site-functions/vim-incarg | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/files/.local/share/zsh/site-functions/vim-incarg b/files/.local/share/zsh/site-functions/vim-incarg index 8187d7a7..414ce588 100644 --- a/files/.local/share/zsh/site-functions/vim-incarg +++ b/files/.local/share/zsh/site-functions/vim-incarg @@ -152,21 +152,36 @@ case "$base" in 16) fmt1="$BUFFER[first-1]"; fmt2='#16' ;; esac -local raw_result new +local raw_result padded raw_result="$( \ printf "%0$ndigits$fmt1" $(( [$fmt2] "$base#$old" + delta )) 2> /dev/null)" -new="${raw_result// /0}" +padded="${raw_result// /0}" -if zstyle -t ":zle:$WIDGET" debug; then - zle -M "[$WIDGET] base: $base delta: $delta old: '$old' new: '$new'" -fi - -integer oldnum="$base#$old" newnum="$base#$new" 2> /dev/null -if (( delta > 0 && newnum < oldnum || delta < 0 && newnum > oldnum )); then +integer oldnum="$base#$old" newnum="$base#$padded" 2> /dev/null +if (( base != 10 && newnum < 0 + || delta > 0 && newnum < oldnum + || delta < 0 && newnum > oldnum )); then zle -M "[$WIDGET] The resulting number is either too big or too small." return 1 fi +# adjust the number of leading zeros if the sign of the integer changed +local new +if (( base == 10 && ndigits == $#padded )); then + if (( oldnum < 0 && newnum >= 0 )); then + new="${padded#0}" + elif (( oldnum >= 0 && newnum < 0 )); then + new="-0${padded#-}" + fi +fi +if [[ -z "$new" ]]; then + new="$padded" +fi + +if zstyle -t ":zle:$WIDGET" debug; then + zle -M "[$WIDGET] base: $base delta: $delta old: '$old' new: '$new'" +fi + BUFFER[first,last]="$new" integer offset=0