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



On Nov 11,  3:00am, Dmitry Bolshakov wrote:
} Subject: the function to show a digit argument while it is being typed
}
} in bash a digit argument is showed when it is being typed
} 
} i have writed small function which do the same

That's very nice, but it shouldn't need to be that complex.  ZLE will
make sure that your function is never called except when $KEYS[2] is a
digit, so you don't need to do your own loop to read more keys.

    function digit-argument-show {
      typeset -g __digit_arg
      if [[ $LASTWIDGET != *-argument ]]
      then
        __digit_arg=""
        PREDISPLAY=""
      fi
      __digit_arg+=$KEYS[2]
      PREDISPLAY="(arg: $__digit_arg) "
      zle -R
      zle .digit-argument
    }

    function neg-argument-show {
      typeset -g __digit_arg=-
      zle .neg-argument
    }

Aha, I see the problem with that ... you need to erase PREDISPLAY after
the last digit-argument has happened.  There's still a simpler way than
writing your own loop, namely by using zle read-command.

    function digit-argument-show {
      typeset -g __digit_arg
      if [[ $LASTWIDGET != (digit|neg)-argument ]]
      then
        __digit_arg=""
        PREDISPLAY=""
      fi
      __digit_arg+=$KEYS[2]
      PREDISPLAY="(arg: $__digit_arg) "
      zle -R
      zle .digit-argument
      local REPLY        # Not strictly necessary
      zle read-command   # Sets $REPLY and $KEYS
      if [[ $REPLY != digit-argument ]]
      then
        __digit_arg=""
        PREDISPLAY=""
      fi
      zle -U $KEYS
    }



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