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 Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>:
> On Nov 11, Â3:22am, Mikael Magnusson wrote:
> }
> } 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
> } }
>
> Hmm, that suggests yet another possible improvement to my function. ÂI
> assumed $NUMERIC was updated too late to use it in PREDISPLAY, but it
> is updated in time to be used in place of my __digit_arg global.
>
> Â Âfunction digit-argument-show {
> Â Â Âif [[ $LASTWIDGET == neg-argument ]]
> Â Â Âthen PREDISPLAY="(arg: $((NUMERIC*$KEYS[-1]))) "
> Â Â Âelse PREDISPLAY="(arg: $NUMERIC$KEYS[-1]) "
> Â Â Âfi
> Â Â Âzle -R
> Â Â Âzle .digit-argument
> Â Â Âzle read-command
> Â Â Â[[ $REPLY != digit-argument ]] && PREDISPLAY=""
> Â Â Âzle -U $KEYS
> Â Â}
>
> With this, you don't need the neg-argument-show function at all, and
> assignments to PREDISPLAY can be replaced with zle -M if you like.

With $LASTWIDGET, I can make my function work pretty well too. It only
messes up when you do a neg-argument when there are already some
digits in numeric. Apparently that only clears numeric, rather than
starting over with a new -1. Pressing alt-5 alt-- alt-5 shows arg: 0,
but pressing a key at this point inserts 5 characters. Pressing alt-5
again correctly shows arg: 55. I tried to work around it by running
zle .neg-argument twice, but it has no apparent effect. Aha, this
works:
  elif [[ $LASTWIDGET = neg-argument ]]; then
    zle -M - "$((NUMERIC * $KEYS[-1] ? NUMERIC * $KEYS[-1] : $KEYS[-1]))"


I also noticed ctrl-c gives this message for your function:
_digit_argument:zle:9: not enough arguments for -U
and doesn't reset numeric.

I also noticed zle -M -5 complains about 5 not being an option, zle -M
- -5 does work but I pretty much had to guess that.

Here's my whole working (as far as I can tell) function, bound both to
digit-argument and neg-argument:

function _digit_argument () {
  if [[ $WIDGET = neg-argument ]]; then
    if [[ -n $NUMERIC ]]; then
      zle -M - ""
    else
      zle -M - -
    fi
  elif [[ $LASTWIDGET = neg-argument ]]; then
    zle -M - "$((NUMERIC * $KEYS[-1] ? NUMERIC * $KEYS[-1] : $KEYS[-1]))"
  else
    zle -M - "$NUMERIC$KEYS[-1]"
  fi
  zle .$WIDGET
}

(It does not clear the display when something else is pressed).

-- 
Mikael Magnusson



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