X-Git-Url: http://git.vpit.fr/?p=perl%2Fscripts%2Fxchat.git;a=blobdiff_plain;f=Xchat%2FXPI%2FUtils.pm;fp=Xchat%2FXPI%2FUtils.pm;h=ad99f0ff0162b140e40634d560a0c4a1d7fc49df;hp=82699db9ca405de9aa5b76e71e7b98ccc655524a;hb=edd1cbfd1a969e16d8df9988eb448a53c97c02e7;hpb=dd329bf05f9a50bd10d79e522334761f9785d7ab diff --git a/Xchat/XPI/Utils.pm b/Xchat/XPI/Utils.pm index 82699db..ad99f0f 100644 --- a/Xchat/XPI/Utils.pm +++ b/Xchat/XPI/Utils.pm @@ -23,10 +23,35 @@ sub dye_nick { return sprintf "\003%d%s", $rcolors[$col % @rcolors], $nick; } +sub save_context { + my $cur_cxt = get_context; + bless \$cur_cxt, 'Xchat::XPI::Utils::ContextGuard'; +} + +sub local_context { + return unless @_ >= 2; + my $code = pop; + my $new_cxt = (@_ == 1) ? $_[0] : find_context(@_); + my $cur_cxt = get_context; + if (set_context $new_cxt) { + my $guard = bless \$cur_cxt, 'Xchat::XPI::Utils::ContextGuard'; + return $code->(); + } else { + return undef; + } +} + +sub Xchat::XPI::Utils::ContextGuard::DESTROY { + set_context ${$_[0]}; +} + use base qw; our @EXPORT = (); -our %EXPORT_TAGS = ('funcs' => [ qw ]); +our %EXPORT_TAGS = ('funcs' => [ qw< + dye_nick + save_context local_context +> ]); our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS; $EXPORT_TAGS{'all'} = [ @EXPORT_OK ];