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

[tip] mouse support



Hi zsh users,

I just posted that to comp.unix.shell, I thought it might be of
some interest for some of you. Basically, it's cursor
positionning with the mouse under xterm like terminals (works
with xterm, gnome-terminal and rxvt AFAICS, probably also with
putty, not if there are tabs or NLs (or multi-byte characters)
in the zle buffer).

I'm not used to writing zle code. Please tell me what you think.

### code begins
if [[ $TERM = *xterm* ]]; then
zle-xterm-mouse() {
 emulate -L zsh
 setopt extendedglob # for (#b)
 local bt mx my cx cy i match mbegin mend

 read -k bt # mouse button, x, y reported after \e[M
 read -k mx
 read -k my
 [[ $bt = "#" ]] || return 0 # only for btn1 release

 print -n '\e[6n' # query cursor position
 while read -k i && [[ $i != R ]]; do cx+=$i; done
 # can't use read -d R in zle

 [[ $cx = (#b)??(*)\;(*) ]] || return
 cy=$match[1]
 cx=$match[2]

 (( CURSOR += #mx - 32 - cx + (#my - 32 - cy) * COLUMNS ))
 return 0
}

precmd() {
  # enable mouse tracking
  print -n '\e[?1000h'
}
preexec() {
  # disable mouse tracking
  print -n '\e[?1000l'
}

zle -N zle-xterm-mouse zle-xterm-mouse
bindkey '\e[M' zle-xterm-mouse
fi
### code ends

-- 
Stéphane



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