zsh-users
 help / color / mirror / code / Atom feed
* Strange behaviour of setopt and binkey in a widget
@ 2010-04-20  9:02 Mike Perdide
  2010-04-20  9:47 ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Mike Perdide @ 2010-04-20  9:02 UTC (permalink / raw)
  To: zsh-users

Hello zsh users,
I'm trying to customize the behaviour of CTRL-D in certain conditions
and I 'm observing strange behaviours from zsh.
Here is what I do :

-------------- From my .zshrc ---------------
widg0() { echo "not really exiting"; }
zle -N widg0
widg1() { setopt ignoreeof; bindkey "^D" widg0; }
widg2() { unsetopt ignoreeof; bindkey -r "^D"; }
zle -N widg1
zle -N widg2
bindkey "^O" widg1
bindkey "^P" widg2

-------------- Starting from a bash shell :

[22:49] :~$ zsh
north.~% <CTRL-O> <CTRL-D>
zsh: use 'exit' to exit.
north.~% <CTRL-D>not really exiting
          !!! First strange behaviour : the prompt is not redrawn. At
this point I press <ENTER> to get my prompt back :
north.~% <CTRL-P> <CTRL-D>
!!! The previous ligne is rewritten with these two :
[22:53] :~$
zsh: use 'exit' to exit

-----------------------------------------------------------------------------------------------

The long story behind the ^D rebinding is that I'm using
virtualenvwrapper, that is a wrapper around virtualenv, a tool
designed to build virtual environments using python. This wrapper
activates virtual environments by making directories and setting env
variables to these directories (such as PATH). When a environment is
activated, the only way to  deactivate it is to enter "deactivate".
Typing ctrl-d will exit the shell you're in, and I think that's not a
nice behaviour. What I'm trying to do is to rebind ctrl-d so when it
is used in a virtual environment, this environment would be
deactivated but the shell wouldn't exit.

Am I doing something wrong ? Is the "strange" behaviour I'm observing
a feature I'm not using correctly ?
Thanks in advance.

(I already posted this on zsh-workers, but I guess it wasn't the
proper mailing list)

Regards,

Mike Perdide.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Strange behaviour of setopt and binkey in a widget
  2010-04-20  9:02 Strange behaviour of setopt and binkey in a widget Mike Perdide
@ 2010-04-20  9:47 ` Peter Stephenson
  2010-04-20 20:57   ` Mike Perdide
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2010-04-20  9:47 UTC (permalink / raw)
  To: zsh-users

On Tue, 20 Apr 2010 11:02:29 +0200
Mike Perdide <mike.perdide@gmail.com> wrote:
> Hello zsh users,
> I'm trying to customize the behaviour of CTRL-D in certain conditions
> and I 'm observing strange behaviours from zsh.
> Here is what I do :
> 
> -------------- From my .zshrc ---------------
> widg0() { echo "not really exiting"; }
> zle -N widg0
> widg1() { setopt ignoreeof; bindkey "^D" widg0; }
> widg2() { unsetopt ignoreeof; bindkey -r "^D"; }
> zle -N widg1
> zle -N widg2
> bindkey "^O" widg1
> bindkey "^P" widg2
> 
> -------------- Starting from a bash shell :
> 
> [22:49] :~$ zsh
> north.~% <CTRL-O> <CTRL-D>
> zsh: use 'exit' to exit.
> north.~% <CTRL-D>not really exiting
>           !!! First strange behaviour : the prompt is not redrawn. At
> this point I press <ENTER> to get my prompt back :

This isn't actually strange at all if you consider that all widg0 done is
echo "not really exiting" to the screen.  You haven't told the shell to
redraw the prompt.  The way to do this is tell ZLE you're taking over the
display for a bit, so that when the widget exits it will redraw the prompt
below:

widg0() {
  zle -I
  echo "not really exiting"
}


> north.~% <CTRL-P> <CTRL-D>
> !!! The previous ligne is rewritten with these two :
> [22:53] :~$
> zsh: use 'exit' to exit

This actually did exit the shell for me (which is what you want, although
I'm not sure now much I expected it given the ^D wasn't bound to anything
any more and it's being processed by zsh, not handled directly as EOF which
zsh has to synthesise).  Does "stty -a" show that ^D is the EOF character?


I once wrote a widget delete-char-or-list-new, which is a reimplementation
of what ^D usually does with Emacs bindings with slightly different
behaviour.  The interesting thing is I had to put in the EOF handling
directly.  I don't know if it's any use to you.  Note that unlike the
default shell behaviour you need to tell it the maximum number of "EOF"s to
ignore, e.g.

  zstyle ':zle:*' ignore-eof-count 10"

The message is also customisable, e.g.

  zstyle ':zle:*' ignore-eof-msg "ignoring eof (%c / %m times)"

I tried this and it seemed to work...


#start delete-char-or-list-new
# This is supposed to work like delete-char-or-list, except that the
# list generated is always new.

if [[ -z $BUFFER ]]; then
  integer eofmax ignoreeof
  if zstyle -s ":zle:$WIDGET" ignore-eof-count eofmax; then
    ignoreeof=1
  else
    [[ -o ignore_eof ]] && ignoreeof=1
  fi
  if [[ -n $ignoreeof ]]; then
    integer -g __eof_count

    if [[ eofmax -le 0 ]]; then
      __eof_count=0
    elif [[ $LASTWIDGET = $WIDGET ]]; then
      if (( ++__eof_count == eofmax )); then
	exit
      fi
    else
      __eof_count=1
    fi

    zmodload -i zsh/zutil

    local fmt msg what=exit
    zstyle -s ":zle:$WIDGET" ignore-eof-msg fmt ||
    fmt="zsh: use '%s' to %s."
    [[ -o login ]] && what=logout
    zformat -f msg $fmt s:$what m:$eofmax c:$__eof_count
    zle -M $msg

    return 1
  else
    exit
  fi
fi

# The <= 0 (formally just != 0) is so that negative prefixes used in
# Perforce completion force a new listing.

if (( ${NUMERIC:-0} <= 0 )); then
  (( CURSOR = CURSOR ))
fi

zle delete-char-or-list
#end delete-char-or-list-new


-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Strange behaviour of setopt and binkey in a widget
  2010-04-20  9:47 ` Peter Stephenson
@ 2010-04-20 20:57   ` Mike Perdide
  0 siblings, 0 replies; 3+ messages in thread
From: Mike Perdide @ 2010-04-20 20:57 UTC (permalink / raw)
  To: zsh-users

2010/4/20 Peter Stephenson <pws@csr.com>:
> On Tue, 20 Apr 2010 11:02:29 +0200
> Mike Perdide <mike.perdide@gmail.com> wrote:
>> [22:49] :~$ zsh
>> north.~% <CTRL-O> <CTRL-D>
>> zsh: use 'exit' to exit.
>> north.~% <CTRL-D>not really exiting
>>           !!! First strange behaviour : the prompt is not redrawn. At
>> this point I press <ENTER> to get my prompt back :
>
> This isn't actually strange at all if you consider that all widg0 done is
> echo "not really exiting" to the screen.  You haven't told the shell to
> redraw the prompt.  The way to do this is tell ZLE you're taking over the
> display for a bit, so that when the widget exits it will redraw the prompt
> below:
>
> widg0() {
>  zle -I
>  echo "not really exiting"
> }
Thanks for that -I option, I wouldn't have guess it's utility : ). It
works like I wanted, althought it seems to take some time to be active
:
[22:15] :~$ zsh
north.~% <CTRL-O> # Bind widg0 to ^D
north.~% <CTRL-D>
zsh: use 'exit' to exit.
north.~% <CTRL-D>
Deactivating ...

>> north.~% <CTRL-P> <CTRL-D>
>> !!! The previous ligne is rewritten with these two :
>> [22:53] :~$
>> zsh: use 'exit' to exit
>
> This actually did exit the shell for me (which is what you want, although
> I'm not sure now much I expected it given the ^D wasn't bound to anything
> any more and it's being processed by zsh, not handled directly as EOF which
> zsh has to synthesise).  Does "stty -a" show that ^D is the EOF character?
It does : "eof = ^D;"
It exits the shell for me too, but prints this message "zsh: use
'exit" to exit", so I'm guessing I'm still doing something wrong.

Moreover, I manage to get a segfault error by using virtualenwrapper
and wigets, but that's another story.

Thanks fo your help !


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-04-20 21:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-20  9:02 Strange behaviour of setopt and binkey in a widget Mike Perdide
2010-04-20  9:47 ` Peter Stephenson
2010-04-20 20:57   ` Mike Perdide

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).