%%
%% This is file `beamersubframe.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% beamersubframe.dtx  (with options: `package')
%% 
%% Copyright (c) 2011 Mike Kaufmann, all rights reserved
%% 
%% This program is provided under the terms of the
%% LaTeX Project Public License distributed from CTAN
%% archives in directory macros/latex/base/lppl.txt.
%% 
%% Author: Mike Kaufmann
%%         m.km@gmx.de
%% 
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
%%
%%
\def\fileversion{0.2}
\def\filedate{2011/08/07}

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{beamersubframe}[\filedate\space
    v\fileversion\space reordering beamer frames]
\@ifclassloaded{beamer}{}{%
  \PackageError{beamersubframe}{%
    The package works only with the beamer class,\MessageBreak
    therefore it is not loaded.
  }{%
    The package is not loaded, because it needs the\MessageBreak
    beamer class. Continuing may lead to additional\MessageBreak
    errors because of undefined commands.
  }
  \endinput
}
\newif\if@bsf@append
\newif\if@bsf@miniframes
\@bsf@miniframestrue
\newif\if@bsf@subframe
\@bsf@subframefalse
\newif\if@bsf@nosfnum
\newif\if@bsf@firstline
\newif\if@bsf@firstpart
\newif\if@bsf@firstsection
\newif\if@bsf@firstsubsection
\newif\if@bsf@nosubsection
\newif\if@bsf@prevnosubsection
\newtoks\bsf@frame@param
\newwrite\bsf@sfrout
\DeclareOption{embed}{\@bsf@appendfalse}
\DeclareOption{append}{\@bsf@appendtrue}
\DeclareOption{nominiframes}{\@bsf@miniframesfalse}
\ExecuteOptions{embed}
\ProcessOptions*\relax
\RequirePackage{verbatim}

\newcommand{\bsfrestore}[4]{%
  \setcounter{part}{#1}%
  \setcounter{section}{#2}%
  \setcounter{subsection}{#3}%
  \setcounter{subsectionslide}{#4}%
  \expandafter\let\expandafter\insertsectionhead
    \csname bsf@sectionhead\the\c@part.\the\c@section\endcsname
  \expandafter\let\expandafter\insertsubsectionhead
    \csname bsf@subsectionhead\the\c@part.\the\c@section.\the\c@subsection\endcsname
  \def\insertpartheadnumber{#1}%
  \def\insertsectionheadnumber{#2}%
  \def\insertsubsectionheadnumber{#3}%
}
\def\bsf@subnum{%
  \the\c@part.\the\c@section.\the\c@subsection.\the\c@subsectionslide
}
\def\beamer@writeslidentry@miniframes{%
  \addtocontents{nav}%
    {\protect\headcommand{%
      \protect\subslideentry{\the\c@section}{\the\c@subsection}%
        {\the\c@subsectionslide}%
        {\protect\bsf@usenum{bsf@substartpage\bsf@subnum}/%
         \protect\bsf@usenum{bsf@subendpage\bsf@subnum}}%
        {\lastsubsection}{\the\c@part}}}%
}
\def\beamer@writeslidentry@navbar{%
  \expandafter\beamer@ifempty\expandafter{\beamer@framestartpage}{}% does not happen normally
  {%else
    \addtocontents{nav}%
      {\protect\headcommand{%
        \protect\beamer@framepages{\beamer@framestartpage}{\beamer@frameendpage}}}%
    \addtocontents{sfp}{%
      \protect\bsfsubframepages{\the\c@part}{\the\c@section}{\the\c@subsection}%
        {\the\c@subsectionslide}{\beamer@framestartpage}{\beamer@frameendpage}}%
    \clearpage\beamer@notesactions%
  }%
}
\def\subslideentry{\slideentry}
\def\bsfsubframepages#1#2#3#4#5#6{%
  \expandafter\def\csname bsf@substartpage#1.#2.#3.#4\endcsname{#5}%
  \expandafter\def\csname bsf@subendpage#1.#2.#3.#4\endcsname{#6}%
}
\def\bsf@usenum#1{%
  \@ifundefined{#1}{1}{\csname #1\endcsname}%
}
\def\bsf@checksfp{%
  \begingroup
    \@bsf@nosfnumfalse
    \def\bsf@usenum##1{%
      \@ifundefined{##1}{\@bsf@nosfnumtrue}{}}
    \def\slideentry##1##2##3##4##5##6{}%
    \def\partentry##1##2{}%
    \def\sectionentry##1##2##3##4##5{}%
    \def\beamer@subsectionentry##1##2##3##4##5{}%
    \def\subslideentry##1##2##3##4##5##6{%
      \bsf@checksfnum(##4)}%
    \dohead
    \if@bsf@nosfnum
      \PackageWarningNoLine{Beamer SubFrame}{%
        Missing, incomplete or corrupted file\MessageBreak
        \jobname.sfp! Links for miniframes of\MessageBreak
        subframes may be wrong. Please run TeX\MessageBreak
        again}
    \fi
  \endgroup
}
\def\bsf@checksfnum(#1/#2){#1#2}

\def\hyperlinkslidenext{%
  \bsf@nextpage\c@page%
  \hyperlink{Navigation\the\beamer@tempcount}}
\def\hyperlinkframestartnext{%
  \bsf@nextpage\beamer@endpageofframe%
  \hyperlink{Navigation\the\beamer@tempcount}}
\def\bsf@nextpage#1{%
  \beamer@tempcount=#1%
  \advance\beamer@tempcount by1\relax%
  \ifnum\beamer@tempcount>\bsf@endpageofdocument%
  \beamer@tempcount=\bsf@endpageofdocument%
  \fi}
\def\bsf@documentpages#1{\def\bsf@endpageofdocument{#1}}
\def\bsf@endpageofdocument{\beamer@endpageofdocument}
\let\beamer@partpages@orig\beamer@partpages
\let\beamer@subsectionpages@orig\beamer@subsectionpages
\let\beamer@sectionpages@orig\beamer@sectionpages
\let\beamer@documentpages@orig\beamer@documentpages
\def\bsf@enablenaventries{%
  \let\beamer@partpages\beamer@partpages@orig
  \let\beamer@subsectionpages\beamer@subsectionpages@orig
  \let\beamer@sectionpages\beamer@sectionpages@orig
  \let\beamer@documentpages\beamer@documentpages@orig
}
\def\bsf@disablenaventries{%
  \let\beamer@partpages\@gobbletwo
  \let\beamer@subsectionpages\@gobbletwo
  \let\beamer@sectionpages\@gobbletwo
  \let\beamer@documentpages\@gobble
}
\let\part@orig\part
\let\section@orig\section
\let\subsection@orig\subsection
\if@bsf@append
  \def\part{%
    \immediate\write\bsf@sfrout{\string\bsfrestorepart{\the\c@part}%
        {\the\c@page}}%
    \part@orig
  }
  \def\section{%
    \immediate\write\bsf@sfrout{\string\bsfrestoresection{\the\c@section}%
        {\the\c@page}}%
    \section@orig
  }
  \def\subsection{%
    \immediate\write\bsf@sfrout{\string\bsfrestoresubsection{\the\c@subsection}%
        {\the\c@page}}%
    \subsection@orig
  }
\fi
\def\bsfrestorepart#1#2{%
  \if@bsf@firstpart
    \@bsf@firstpartfalse
    \def\bsf@partstartpage{#2}%
    \def\bsf@prevpart{#1}%
    \edef\bsf@partfirstsubframepage{\the\c@page}%
  \else
    \@tempcnta=\bsf@prevpart\relax
    \ifnum#1>\@tempcnta
      \@tempcnta\c@page\advance\@tempcnta -1\relax
      \@tempcntb=#2\relax\advance\@tempcntb -1\relax
      \if@filesw
        \immediate\write\@auxout{\string\@writefile{nav}%
          {\noexpand\headcommand{%
            \noexpand\bsf@partpages{\bsf@partfirstsubframepage}%
              {\the\@tempcnta}{\bsf@partstartpage}{\the\@tempcntb}}}}%
      \fi
      \def\bsf@partstartpage{#2}%
      \def\bsf@prevpart{#1}%
      \edef\bsf@partfirstsubframepage{\the\c@page}%
    \fi
  \fi
}
\def\bsf@partstartpage{1}%
\def\bsf@prevpart{0}%
\def\bsfrestoresection#1#2{%
  \if@bsf@firstsection
    \@bsf@firstsectionfalse
    \def\bsf@prevsectionstartpage{#2}%
    \@bsf@nosubsectiontrue
    \@bsf@prevnosubsectionfalse
    \def\bsf@sectionstartpage{#2}%
    \def\bsf@prevsection{#1}%
    \edef\bsf@sectionfirstsubframepage{\the\c@page}%
  \else
    \@tempcnta=\bsf@prevsection\relax
    \ifnum#1>\@tempcnta
      \edef\bsf@prevsectionstartpage{\bsf@sectionstartpage}%
      \if@bsf@nosubsection
        \@bsf@prevnosubsectiontrue
      \else
        \@bsf@prevnosubsectionfalse
      \fi
      \@bsf@nosubsectiontrue
      \def\bsf@prevsubsection{-1}%
      \@tempcnta\c@page\advance\@tempcnta -1\relax
      \@tempcntb=#2\relax\advance\@tempcntb -1\relax
      \if@filesw
        \immediate\write\@auxout{\string\@writefile{nav}%
          {\noexpand\headcommand{%
            \noexpand\bsf@sectionpages{\bsf@sectionfirstsubframepage}%
              {\the\@tempcnta}{\bsf@sectionstartpage}{\the\@tempcntb}}}}%
      \fi
      \def\bsf@sectionstartpage{#2}%
      \def\bsf@prevsection{#1}%
      \edef\bsf@sectionfirstsubframepage{\the\c@page}%
    \fi
  \fi
}
\def\bsf@sectionstartpage{1}%
\def\bsf@prevsection{0}%
\def\bsfrestoresubsection#1#2{%
  \if@bsf@firstsubsection
    \@bsf@firstsubsectionfalse
    \@bsf@prevnosubsectionfalse
    \def\bsf@subsectionstartpage{#2}%
    \def\bsf@prevsubsection{#1}%
    \edef\bsf@subsectionfirstsubframepage{\the\c@page}%
  \else
    \@tempcnta=\bsf@prevsubsection\relax
    \ifnum#1>\@tempcnta
      \@tempcnta\c@page\advance\@tempcnta -1\relax
      \if@bsf@prevnosubsection
        \@tempcntb=\bsf@prevsectionstartpage\relax
      \else
        \@tempcntb=#2\relax
      \fi
      \advance\@tempcntb -1\relax
      \@bsf@prevnosubsectionfalse
      \@bsf@nosubsectionfalse
      \if@filesw
        \immediate\write\@auxout{\string\@writefile{nav}%
          {\noexpand\headcommand{%
            \noexpand\bsf@subsectionpages{\bsf@subsectionfirstsubframepage}%
              {\the\@tempcnta}{\bsf@subsectionstartpage}{\the\@tempcntb}}}}%
      \fi
      \def\bsf@subsectionstartpage{#2}%
      \def\bsf@prevsubsection{#1}%
      \edef\bsf@subsectionfirstsubframepage{\the\c@page}%
    \fi
  \fi
}
\def\bsf@subsectionstartpage{1}%
\def\bsf@prevsubsection{0}%
\def\bsf@partpages#1#2#3#4{%
  \ifnum\c@page<#1%
  \else%
    \ifnum\c@page>#2%
    \else%
      \gdef\beamer@startpageofpart{#3}%
      \gdef\beamer@endpageofpart{#4}%
    \fi%
  \fi%
}
\def\bsf@sectionpages#1#2#3#4{%
  \ifnum\c@page<#1%
  \else%
    \ifnum\c@page>#2%
    \else%
      \gdef\beamer@startpageofsection{#3}%
      \gdef\beamer@endpageofsection{#4}%
    \fi%
  \fi%
}
\def\bsf@subsectionpages#1#2#3#4{%
  \ifnum\c@page<#1%
  \else%
    \ifnum\c@page>#2%
    \else%
      \gdef\beamer@startpageofsubsection{#3}%
      \gdef\beamer@endpageofsubsection{#4}%
    \fi%
  \fi%
}
\def\inserttotalframenumberwithsub{\inserttotalframenumber}
\if@bsf@append
  \newenvironment{subframe}{%
    \@tempcnta\c@subsectionslide
    \if@bsf@miniframes\else \advance\@tempcnta by -1\fi
    \expandafter\global\expandafter\let
      \csname bsf@sectionhead\the\c@part.\the\c@section
        \endcsname\insertsectionhead
    \expandafter\global\expandafter\let
      \csname bsf@subsectionhead\the\c@part.\the\c@section.\the\c@subsection
        \endcsname\insertsubsectionhead
    \immediate\write\bsf@sfrout{\string\bsfrestore{\the\c@part}{\the\c@section}%
        {\the\c@subsection}{\the\@tempcnta}}%
    \if@bsf@miniframes \addtocounter{subsectionslide}{1}\fi
    \@bsf@firstlinetrue
    \let\do\@makeother\dospecials\catcode`\^^M\active
    \def\verbatim@processline{%
      \if@bsf@firstline
        \immediate\write\bsf@sfrout{\string\begin{frame}\the\verbatim@line}%
        \@bsf@firstlinefalse
      \else
        \immediate\write\bsf@sfrout{\the\verbatim@line}%
      \fi
    }%
    \verbatim@}{\immediate\write\bsf@sfrout{\string\end{frame}^^J}%
    \if@bsf@miniframes \beamer@writeslidentry@miniframes \fi
  }
\else
  \newenvironment<>{subframe}[1][]{%
    \@bsf@subframetrue
    \bsf@frame{subframe}{#2}{#1}}{\end{frame}\@bsf@subframefalse}
\fi
\def\bsf@frame#1#2#3{\bsf@frame@param={}%
  \@ifnextchar[{\bsf@@frame{#1}{#2}{#3}}{\bsf@@frame{#1}{#2}{#3}[]}%
}
\def\bsf@@frame#1#2#3[#4]{%
  \def\@tempa{#3}
  \ifx\@tempa\@empty
    \bsf@frame@param={#2[environment=#1]}%
  \else
    \def\@tempb{#4}
    \ifx\@tempb\@empty
      \bsf@@@frame{#1}{#2}#3\@@end
    \else
      \bsf@frame@param={#2[#3][environment=#1,#4]}%
    \fi
  \fi
  \expandafter\bsf@frame@\the\bsf@frame@param\@@end
}
\def\bsf@@@frame#1#2#3#4\@@end{%
  \def\@tempa{#3}\def\@tempb{<}%
  \ifx\@tempa\@tempb\relax
    \bsf@frame@param={#2[#3#4][environment=#1]}%
  \else
    \bsf@frame@param={#2[environment=#1,#3#4]}%
  \fi
}
\def\bsf@frame@#1\@@end{\begin{frame}#1}
\newenvironment<>{lastframe}[1][]{%
  \bsfrestore{0}{0}{0}{0}%
  \bsf@frame{lastframe}{#2}{#1}}{\end{frame}}
\newcommand{\appendsubframes}{%
  \if@bsf@append
    \immediate\write\bsf@sfrout{\string\endinput}%
    \edef\bsf@partfirstsubframepage{\the\c@page}%
    \edef\bsf@sectionfirstsubframepage{\the\c@page}%
    \edef\bsf@subsectionfirstsubframepage{\the\c@page}%
    \@tempcnta\c@page\advance\@tempcnta -1\relax
    \edef\bsf@pagebeforesub{\the\@tempcnta}%
    \addtocontents{nav}{\protect\headcommand{%
        \protect\beamer@partpages{\the\beamer@partstartpage}{\bsf@pagebeforesub}}}%
    \addtocontents{nav}{\protect\headcommand{%
        \protect\beamer@subsectionpages{\the\beamer@subsectionstartpage}{\bsf@pagebeforesub}}}%
    \addtocontents{nav}{\protect\headcommand{%
        \protect\beamer@sectionpages{\the\beamer@sectionstartpage}{\bsf@pagebeforesub}}}%
    \addtocontents{nav}{\protect\headcommand{%
        \protect\beamer@documentpages{\bsf@pagebeforesub}}}%
    \edef\bsf@totalframenumber{\the\c@framenumber}%
    \let\beamer@writeslidentry\beamer@writeslidentry@navbar
    \immediate\closeout\bsf@sfrout
    \@bsf@firstparttrue
    \@bsf@firstsectiontrue
    \@bsf@firstsubsectiontrue
    \@bsf@subframetrue
    \input{\jobname.sfr}
    \@tempcnta\c@page\advance\@tempcnta -1\relax
    \if@filesw
      \immediate\write\@auxout{\string\@writefile{nav}%
        {\noexpand\headcommand{\noexpand\bsf@partpages{\bsf@partfirstsubframepage}%
          {\the\@tempcnta}{\bsf@partstartpage}{\bsf@pagebeforesub}}}}%
      \immediate\write\@auxout{\string\@writefile{nav}%
        {\noexpand\headcommand{\noexpand\bsf@sectionpages{\bsf@sectionfirstsubframepage}%
          {\the\@tempcnta}{\bsf@sectionstartpage}{\bsf@pagebeforesub}}}}%
      \immediate\write\@auxout{\string\@writefile{nav}%
        {\noexpand\headcommand{\noexpand\bsf@subsectionpages{\bsf@subsectionfirstsubframepage}%
          {\the\@tempcnta}{\bsf@subsectionstartpage}{\bsf@pagebeforesub}}}}%
      \immediate\write\@auxout{\string\@writefile{nav}%
        {\noexpand\headcommand{\noexpand\bsf@documentpages{\the\@tempcnta}}}}%
      \immediate\write\@auxout{\string\@writefile{nav}%
        {\noexpand\headcommand{\noexpand\bsf@disablenaventries}}}%
    \fi
  \fi
}
\AtBeginDocument{%
  \InputIfFileExists{\jobname.sfp}{}{}%
  \if@bsf@append
    \immediate\openout\bsf@sfrout\jobname.sfr\relax
    \if@filesw
      \immediate\write\@auxout{\string\@writefile{nav}%
        {\noexpand\headcommand{\noexpand\bsf@enablenaventries}}}%
    \fi
  \fi
}
\AtEndDocument{%
  \if@bsf@append
    \bsf@checksfp
    \@ifundefined{bsf@totalframenumber}{%
      \edef\bsf@totalframenumber{\the\c@framenumber}}{}%
    \if@filesw
      \immediate\write\@auxout{\string\@writefile{nav}%
        {\noexpand\headcommand{%
          \noexpand\def\noexpand\inserttotalframenumber{\bsf@totalframenumber}}}}
      \newwrite\tf@sfp
      \immediate\openout\tf@sfp\jobname.sfp\relax
    \fi
  \fi
  \if@filesw
    \immediate\write\@auxout{\string\@writefile{nav}%
      {\noexpand\headcommand{%
        \noexpand\def\noexpand\inserttotalframenumberwithsub{\the\c@framenumber}}}}
  \fi
}
\newcommand{\ifappend}[2]{\if@bsf@append #1\else #2\fi}
\newcommand{\ifsubframe}[2]{\if@bsf@subframe #1\else #2\fi}
\endinput
%%
%% End of file `beamersubframe.sty'.
