%%
%% This is file `locality.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% locality.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Copyright (C) 2010 by Jason Gross
%% 
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.2 of this license or (at your option) any later
%% version. The latest version of this license is in:
%% 
%%   http://www.latex-project.org/lppl.txt
%% 
%% and version 1.2 or later is part of all distributions of
%% LaTeX version 1999/12/01 or later.
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{locality}
 [2010/11/11 v0.2 Various macros for keeping things local]
\RequirePackage{etoolbox}[2009/08/06]
\def\@nil{\@nil\relax} % this way, I'll know if I've messed up; I'll get
\def\if@nil#1{\@if@nil#1\@@nil}
\def\@if@nil#1#2\@@nil{\ifx\@nil#1}
\let\locality@tex@global=\global
\newcount\locality@global@non@collision@unique@count
\locality@global@non@collision@unique@count=0
\long\def\@manyaftergroup#1{\if@nil#1 \else \aftergroup#1
\expandafter\@manyaftergroup\fi}
\newcommand{\manyaftergroup}[1]{\@manyaftergroup#1\@nil}
\newcommand{\locallydefine}[2]{{#2\expandafter}%
  \expandafter\def\expandafter#1\expandafter{#1}}
\let\locality@tex@begingroup=\begingroup
\let\locality@tex@endgroup=\endgroup
\newcount\locality@global@aftergroup@count
\locality@global@aftergroup@count=-1
\newcommand\locality@patch@aftergroup{%
  \def\aftergroup{%
    \locality@tex@global\advance\locality@global@aftergroup@count by 1
    \afterassignment\locality@aftergroup\locality@tex@global
    \expandafter\let\csname locality@nextchar\space\the
      \locality@global@aftergroup@count\endcsname=%
  }%
}
\newcommand\locality@aftergroup{%
  \expandafter\expandafter\expandafter\def
  \expandafter\expandafter\expandafter\endgroup
  \expandafter\expandafter\expandafter{\expandafter\endgroup
    \csname locality@nextchar\space\the\locality@global@aftergroup@count
    \endcsname}%
}
\newcommand{\DeclareNonlocalMacro}[1]{%
  \locality@patch@aftergroup % first, patch |\aftergroup|
  \expandafter\let
  \csname endgroup \the\locality@local@group@non@local@macro@count
    \endcsname=\endgroup
  \expandafter\def\expandafter\endgroup\expandafter{%
    \expandafter\expandafter\expandafter\let\expandafter
      \expandafter\expandafter\endgroup\expandafter\expandafter
      \csname endgroup \the\locality@local@group@non@local@macro@count
        \endcsname\expandafter\endgroup
    \expandafter\def\expandafter#1\expandafter{#1}}%
  \advance\locality@local@group@non@local@macro@count by 1
}%
\newcommand\locality@declarenonlocals[2]{%
  \@for\locality@declarenonlocals@name:=#2\do{%
    \expandafter #1\expandafter{\locality@declarenonlocals@name}%
  }%
}
\newcommand{\DeclareNonlocalMacros}[1]{\locality@declarenonlocals{\DeclareNonlocalMacro}{#1}}
\newcommand{\DeclareNonlocalTheRegister}[1]{%
  \locality@patch@aftergroup % first, patch |\aftergroup|
  \expandafter\let
  \csname endgroup \the\locality@local@group@non@local@macro@count
    \endcsname=\endgroup
  \expandafter\def\expandafter\endgroup\expandafter{%
    \expandafter\expandafter\expandafter\let
      \expandafter\expandafter\expandafter\endgroup
      \expandafter\expandafter
      \csname endgroup \the\locality@local@group@non@local@macro@count
        \endcsname\expandafter\endgroup
    \expandafter#1\expandafter=\the#1 }% Note the space.  This is to
  \advance\locality@local@group@non@local@macro@count by 1
}%
\let\DeclareNonlocalCount=\DeclareNonlocalTheRegister
\let\DeclareNonlocalDimen=\DeclareNonlocalTheRegister
\let\DeclareNonlocalSkip=\DeclareNonlocalTheRegister
\let\DeclareNonlocalMuskip=\DeclareNonlocalTheRegister
\newcommand{\DeclareNonlocalTheRegisters}[1]{\locality@declarenonlocals{\DeclareNonlocalTheRegister}{#1}}
\let\DeclareNonlocalCounts=\DeclareNonlocalTheRegisters
\let\DeclareNonlocalDimens=\DeclareNonlocalTheRegisters
\let\DeclareNonlocalSkips=\DeclareNonlocalTheRegisters
\let\DeclareNonlocalMuskips=\DeclareNonlocalTheRegisters
\newcommand{\DeclareNonlocalToks}[1]{%
  \locality@patch@aftergroup % first, patch |\aftergroup|
  \expandafter\let
  \csname endgroup \the\locality@local@group@non@local@macro@count
    \endcsname=\endgroup
  \expandafter\def\expandafter\endgroup\expandafter{%
    \expandafter\expandafter\expandafter\let\expandafter\expandafter
      \expandafter\endgroup\expandafter\expandafter
      \csname endgroup \the\locality@local@group@non@local@macro@count'
      \endcsname\expandafter\endgroup
    \expandafter#1\expandafter=\expandafter{\the#1}}%
  \advance\locality@local@group@non@local@macro@count by 1
}%
\newcommand{\DeclareNonlocalTokses}[1]{\locality@declarenonlocals{\DeclareNonlocalToks}{#1}}
\newcount\locality@local@group@non@local@macro@count % Hopefully, this
\def\begingroup{\locality@tex@begingroup
  \def\endgroup{\locality@tex@endgroup}% not \let, because
                     % that would break my |\aftergroup| patch
  \locality@local@group@non@local@macro@count=0
}
\def\@savevalues#1{\ifx#1\@nil \else \expandafter\let
  \csname @\string#1\space backup\endcsname=#1
  \expandafter\@savevalues\fi}
\newcommand{\savevalues}[1]{\@savevalues#1\@nil}

\def\@restorevalues#1{%
\ifx
  #1\@nil
\else
  \expandafter\let\expandafter#1\expandafter
    =\csname @\string#1\space backup\endcsname
    \expandafter
  \let\csname @\string#1\space backup\endcsname
    =\relax
  \expandafter\@restorevalues
\fi
}
\newcommand{\restorevalues}[1]{\@restorevalues#1\@nil}
\newcommand{\pushvalue}[1]{%
  \@ifundefined{locality\space backup\space \string#1}{}{%
    \expandafter\pushvalue\csname locality\space backup\space \string#1\endcsname%
  }%
  \expandafter\let\csname locality\space backup\space \string#1\endcsname=#1%
}
\newcommand{\pushvalues}[1]{%
  \@for\locality@pushvalues@macroname:=#1\do{%
    \expandafter\pushvalue\locality@pushvalues@macroname
  }%
}
\newcommand{\popvalue}[1]{%
  \@ifundefined{locality\space backup\space \string#1}{%
    \let#1=\relax
  }{%
    \expandafter\let\expandafter#1\expandafter=\csname locality\space backup\space \string#1\endcsname
    \expandafter\popvalue\csname locality\space backup\space \string#1\endcsname%
  }%
}
\newcommand{\popvalues}[1]{%
  \@for\locality@popvalues@macroname:=#1\do{%
    \expandafter\popvalue\locality@popvalues@macroname
  }%
}
\newcommand{\makecommandslocal}[2]{\savevalues{#1}#2\restorevalues{#1}}
\savevalues{\global\gdef\xdef\@cons}
{\def\begingroup{\begingroup\DeclareNonlocalMacro{##1}}%
  \expandafter\expandafter\expandafter
}%
\expandafter\expandafter\expandafter\def
\expandafter\expandafter\expandafter\locality@cons
\expandafter\expandafter\expandafter#%
\expandafter\expandafter\expandafter1%
\expandafter\expandafter\expandafter#%
\expandafter\expandafter\expandafter2%
\expandafter\expandafter\expandafter{\@cons{#1}{#2}}%
\newcommand{\obeyglobal}{\restorevalues{\global\gdef\xdef\@cons}}
\newcommand{\unignoreglobal}{\popvalues{\global,\gdef,\xdef,\@cons}}
\newcommand{\ignoreglobal}{%
  \pushvalues{\global,\gdef,\xdef,\@cons}%
  \let\global=\relax \let\gdef=\def \let\xdef=\edef
  \let\@cons=\locality@cons
  \expandafter\def\expandafter\unignoreglobal\expandafter{\expandafter
  \def\expandafter\unignoreglobal\expandafter{\unignoreglobal}%
  \unignoreglobal}%
}
\newcommand{\makecounterslocal}{% FIX, to make more robust
  \expandafter\def\expandafter\stepcounter
    \expandafter##\expandafter1\expandafter{%
    \expandafter\ignoreglobal\stepcounter{##1}%
    \unignoreglobal
  }%
 %
  \expandafter\def\expandafter\refstepcounter
    \expandafter##\expandafter1\expandafter{%
    \expandafter\ignoreglobal\refstepcounter{##1}%
    \unignoreglobal
  }%
 %
  \expandafter\def\expandafter\setcounter
    \expandafter##\expandafter1%
    \expandafter##\expandafter2\expandafter{%
    \expandafter\ignoreglobal\setcounter{##1}{##2}%
    \unignoreglobal
  }%
 %
  \expandafter\def\expandafter\addtocounter
    \expandafter##\expandafter1%
    \expandafter##\expandafter2\expandafter{%
    \expandafter\ignoreglobal\addtocounter{##1}{##2}%
    \unignoreglobal
  }%
 %
  \expandafter\def\expandafter\@addtoreset
    \expandafter##\expandafter1%
    \expandafter##\expandafter2\expandafter{%
    \expandafter\ignoreglobal\@addtoreset{##1}{##2}%
    \unignoreglobal
  }%
 %
  \expandafter\def\expandafter\@definecounter
    \expandafter##\expandafter1\expandafter{%
    \expandafter\ignoreglobal\@definecounter{##1}%
    \unignoreglobal
  }%
  \locality@fix@for@amstext
  \locality@fix@for@calc
}
\AtEndPreamble{
  \@ifpackageloaded{amstext}{
    \newcommand{\locality@fix@for@amstext}{
      \expandafter\def\expandafter\stepcounter
        \expandafter##\expandafter1\expandafter{%
        \expandafter\iffirstchoice@\stepcounter{##1}\fi
      }
      \expandafter\def\expandafter\addtocounter
        \expandafter##\expandafter1%
        \expandafter##\expandafter2\expandafter{%
        \expandafter\iffirstchoice@\addtocounter{##1}{##2}\fi
      }
    }
  }{
    \let\locality@fix@for@amstext=\relax
  }
  \@ifpackageloaded{calc}{%
    \def\locality@fix@for@calc{\expandafter\def\expandafter\begingroup\expandafter{\begingroup\DeclareNonlocalTheRegister{\calc@A}}}
  }{
    \let\locality@fix@for@calc=\relax
  }
}
\endinput
%%
%% End of file `locality.sty'.
