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

yank-pop and user widgets



Hello,

In my mouse.zsh xterm and X clipboard extension for zsh, I
wanted to redefine the yank-like widgets so that they paste the
X clipboard.

I first thought of something like:

yank() {
  get-x-selection
  zle .yank
}
zle -N yank

But the problem is that yank-pop then stops to work because it
checks that the last invoked widget was a yank-type one (and my
user defined one is not).

Then, I thought of something like:

bindkey -s '^Y' '<key1><key2>'
where <key1> is mapped to "get-x-selection" and <key2> to yank.
It worked but then, the numeric arguments stop working (it
applies to <key1> but not to <key2>, so not to the yanking).

I finally came to the solution below, but I wonder if there's
not anything simpler (see http://stchaz.free.fr/mouse.zsh for
details):

[...]
      # hardcode ^[[2;5~ which is sent by <Ctrl-Insert> on xterm
      bindkey -M emacs '\e[2;5~' get-x-selection
      bindkey -M viins '\e[2;5~' get-x-selection
      bindkey -M vicmd '\e[2;5~' get-x-selection

      # the convoluted stuff below is to work around two problems:
      #  1- we can't just redefine the widgets as then yank-pop would
      #  stop working
      #  2- we can't just rebind the keys to <Ctrl-Insert><other-key> as
      #  then we'll loose the numeric argument
      propagate-numeric() {
        # next key (\e[0-dum) is mapped to <Ctrl-Insert>, plus the
	# targeted widget with NUMERIC restored.
	case $KEYMAP in
	  vicmd)
	    bindkey -M vicmd -s '\e[0-dum' $'\e[2;5~'$NUMERIC${KEYS/x/};;
	  *)
	    bindkey -M $KEYMAP -s '\e[0-dum' $'\e[2;5~'${NUMERIC//(#m)?/$'\e'$MATCH}${KEYS/x/};;
	esac
      }
      zle -N propagate-numeric
      bindkey -M emacs '\e[1-dum' yank
      bindkey -M emacs '\e[1-xdum' propagate-numeric
      bindkey -M emacs -s '^Y' $'\e[1-xdum\e[0-dum'
      bindkey -M vicmd '\e[2-dum' vi-put-before
      bindkey -M vicmd '\e[2-xdum' propagate-numeric
      bindkey -M vicmd -s 'P' $'\e[2-xdum\e[0-dum'
      bindkey -M vicmd '\e[3-dum' vi-put-after
      bindkey -M vicmd '\e[3-xdum' propagate-numeric
      bindkey -M vicmd -s 'p' $'\e[3-xdum\e[0-dum'
[...]

Best regards,
Stéphane



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