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

trap signal with functional scope?




I have the following zsh function:

ssh () {
        set_screen_title ${*##-*}
        command ssh "$@"
        set_screen_title "${HOST/.*}"
}

It sets my screens title to the first ssh argument not prefixed by "-" (usually the remote hostname), executes the ssh command and then changes screens title back to my local hostname.

(set_screen_title is defined as:

set_screen_title () {
        print -nb "\ek${1}\e\\"
}

but this should not be relevant for this discussion)

Most of the time this just works and is cool. It fails when I send ctrl-c to the ssh process. The function has no chance to execute the second set_screen_title command.

At the moment I see two and a half solutions to this problem:

1) Always call set_screen_title in a precmd_ function. Kind of last resort because it raises other problems.

2) Install a signal handler like this:

   trap "set_screen_title ${HOST/.*}" INT

This is bad because the signal handler is still there after the function exits

2b) Install a signal handler like this:

    trap "set_screen_title ${HOST/.*}; trap - INT" INT

which is even worse because it might destroy handlers already set before.

I hope somebody on this list has a suggestion 3) that is better then the others. Best would be a signal handler kind of thing that is limited to the scope of the function it was installed in. The special signal EXIT does not help here.


Sebastian



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