+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]};
+}
+