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

Re: the function to show a digit argument while it is being typed



2009/11/11 Dmitry Bolshakov <bdimych@xxxxxxxx>:
> hi
>
> in bash a digit argument is showed when it is being typed
>
> i have writed small function which do the same
>
> #####
> function digit-argument-show-while-entering {
>        arg="${KEYS[2]}"
>        if [[ $arg == - ]]
>        then
>                zle .neg-argument
>        else
>                zle .digit-argument
>        fi
>        while true
>        do
>                PREDISPLAY="(arg: $arg) "
>                zle -R
>
>                read -k k1
>                if [[ $k1 == $'\C-g' ]]
>                then
>                        NUMERIC=1
>                        break
>                elif [[ $k1 != $'\e' ]]
>                then
>                        zle -U $k1
>                        break
>                fi
>
>                read -k k2
>                if [[ $k2 == <0-9> ]]
>                then
>                        arg=$arg$k2
>                        NUMERIC=$arg
>                else
>                        zle -U "$k1$k2"
>                        break
>                fi
>        done
>        PREDISPLAY=""
> }
> zle -N digit-argument digit-argument-show-while-entering
> zle -N neg-argument digit-argument-show-while-entering
> #####
>
> it is not well tested but seems to be working

You probably want to declare local variables so they don't overwrite
things in the main shell, ie "local arg k1 k2" in the beginning.

I have a similar function, it doesn't work well for negative arguments though:

function _digit_argument () {
  zle -M "$NUMERIC$KEYS[-1]"
  zle .digit-argument
}

The problem is $NUMERIC is -1 after pressing just -, so pressing -1
shows -11. After the first digit it shows things correctly though.
(I've tried the obvious thing of swapping the order of the lines and
showing just $NUMERIC, but it doesn't seem to be updated at that
point).

You can do zle .$WIDGET instead of your first if condition.

-- 
Mikael Magnusson



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