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,  7:17am, Mikael Magnusson wrote:
} Subject: Re: the function to show a digit argument while it is being typed
}
} 2009/11/11 Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>:
} >
} >    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 $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.

I have no idea whether that should be considered a bug.  One possible
interpretation is that ESC 5 ESC - should run neg-argument five times,
which would result in -1, but in practice neg-argument after either
digit-argument or neg-argument simply erases NUMERIC.  At the very
least there's a documentation omission.

}   elif [[ $LASTWIDGET = neg-argument ]]; then
}     zle -M - "$((NUMERIC * $KEYS[-1] ? NUMERIC * $KEYS[-1] : $KEYS[-1]))"

I think [[ $LASTWIDGET = neg-argument && -n $NUMERIC ]] also works,
and may in fact be better ... then you don't need the ternary.

} 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.

That's quite interesting; it means a tty interrupt stops read-command
but doesn't set $KEYS.  In fact after some quick testing the doc for
read-command is wildly inaccurate:

    ... If the key sequence is not bound, status 1 is
    returned; typically, however, REPLY is set to undefined-key to
    indicate a useless key sequence.

In fact for an unbound sequence status 0 is returned and REPLY is set
to undefined-key.  The only way I can get a 1 for the status is with
the tty interrupt character, and then REPLY is set to self-insert
(which makes no sense at all).  That also means zle read-command is a
way to trap interrupts, which may be yet another bug.

   function digit-argument-show {
     if [[ $LASTWIDGET == neg-argument && -n $NUMERIC ]]
     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=""
     [[ -n $KEYS ]] && zle -U $KEYS
   }

-- 



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