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

Re: one time in 20 error



On Tue, Nov 29, 2022 at 3:49 AM Risto Laitinen <risto.laitinen@xxxxxxxxx> wrote:
>
> It is a mouse event (SGR report mode)

Thanks!

So here's what's probably happening. The script is enabling mouse
tracking, the user clicks with the mouse, the script disables mouse
tracking and exits before reading the mouse event, zle reads the mouse
event.

The script needs to read from the TTY after disabling mouse tracking.
Reading with timeout is suboptimal because there is no upper bound on
input latency (imagine working over SSH). A better solution is to send
a query and read the response. Something like this perhaps:

    function cleanup() {
      emulate -L zsh -o extended_glob
      # Disable mouse tracking.
      print -n '\e[?1000l'
      # Disable SGR mouse mode.
      print -n '\e[?1006l'
      # Query the TTY (Primary Device Identification).
      print -n '\e[0c'
      # Read stdin until we find the response to our query.
      local resp
      while [[ $resp != *$'\e[?'(<->\;)#<->c ]] &&
            read -skr 'resp[$#resp+1]'; do
        # Do nothing.
      done
    }

This function needs to be invoked before the script exits.

There is probably a better solution to this problem.

Roman.

P.S.

I have the following in my .zshrc:

    function skip-csi-sequence() {
     local key
     while read -sk key && (( $((#key)) < 0x40 || $((#key)) > 0x7E )); do
       # empty body
     done
    }

    zle -N skip-csi-sequence
    bindkey '\e[' skip-csi-sequence

With this binding a buggy script that leaks mouse events into zle
won't have any effect.




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