% Copyright 2003--2007 by Till Tantau
% Copyright 2010 by Vedran Mileti\'c
% Copyright 2012,2013,2015 by Vedran Mileti\'c, Joseph Wright
% Copyright 2017 by Joseph Wright
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/licenses/LICENSE for more details.

\mode
<all>
{
  \def\beamer@secgobble{\beamer@savemode\@ifnextchar*{\beamer@secstargobble}{\beamer@sec@gobbleoptional}}
  \def\beamer@secstargobble*{\beamer@sec@gobbleoptional}
  \newcommand\beamer@sec@gobbleoptional[2][]{\beamer@resumemode}
  \def\secname{}
  \def\subsecname{}
  \def\subsubsecname{}
}

\mode
<presentation>

%
% Lecture Definitions
%
\newif\ifbeamer@inlecture
\beamer@inlecturetrue

\newcommand{\beamer@if@in@clist@TF}[2]{%
  \def\beamer@if@in@clist@aux##1,#2,{}%
  \expandafter\expandafter\expandafter\ifx
    \expandafter\expandafter\expandafter\@nil
    \expandafter\beamer@if@in@clist@aux\expandafter,#1,{}{},#2,\@nil
    \expandafter\@secondoftwo
  \else
    \expandafter\@firstoftwo
  \fi
}

\newcounter{lecture}
\renewcommand\thelecture{\@arabic\c@lecture}
\let\insertlecturenumber=\thelecture
\newrobustcmd*\lecture{\@dblarg\beamer@lecture}
\long\def\beamer@lecture[#1]#2#3{%
  \beamer@savemode
  \mode<all>%
  \refstepcounter{lecture}%
  \def\beamer@currentlecturelabel{#3}%
  \@onelevel@sanitize\beamer@currentlecturelabel
  \def\beamer@lecturename{#2}%
  \def\beamer@shortlecturename{#1}%
  \ifx\beamer@onlylecture\@empty
  \else
    \expandafter\beamer@if@in@clist@TF\expandafter\beamer@onlylecture
      \expandafter{\beamer@currentlecturelabel}%
      {\beamer@inlecturetrue}%
      {\beamer@inlecturefalse}%
  \fi
  \beamer@atbeginlecture
  \beamer@resumemode
}

\mode
<all>
{
  \newrobustcmd\AtBeginLecture[1]{%
    \def\beamer@atbeginlecture{#1}}
  \AtBeginLecture{}

  \newrobustcmd*\includeonlylecture[1]{%
    \def\beamer@onlylecture{#1}%
    \@onelevel@sanitize\beamer@onlylecture
  }

  \includeonlylecture{}
}

\newrobustcmd*\insertlecture{\beamer@lecturename}
\newrobustcmd*\insertshortlecture[1][]{%
  \begingroup
    \beamer@setupshort{#1}%
    \beamer@insertshort{\beamer@shortlecturename}%
  \endgroup}

\mode
<article>
{
  \newrobustcmd*\lecture[3][]{\def\insertlecture{#2}\def\insertshortlecture{#1}}
}

%
% Part Definitions
%
\newcounter{part}
\renewcommand\thepart{\@arabic\c@part}
\beamer@partstartpage=1

\newcommand<>{\part}{\alt#1{\@dblarg\beamer@part}{\beamer@gobbleoptional}}
\long\def\beamer@part[#1]#2{%
  \beamer@savemode
  \mode<all>%
  \ifbeamer@inlecture
    \refstepcounter{part}%
    \def\beamer@partname{#2}%
    \def\beamer@partnameshort{#1}%
    \addtocontents{nav}{\protect\headcommand{\protect\partentry{#2}{\the\c@part}}}%
    \xdef\partlink{{Navigation\the\c@page}{\noexpand\beamer@partname}}%
    \xdef\partlinkshort{{Navigation\the\c@page}{\noexpand\beamer@partnameshort}}%
    \beamer@tempcount=\c@page\advance\beamer@tempcount by -1%
    \addtocontents{nav}{\protect\headcommand{%
        \protect\beamer@partpages{\the\beamer@partstartpage}{\the\beamer@tempcount}}}%
    \addtocontents{nav}{\protect\headcommand{%
        \protect\beamer@sectionpages{\the\beamer@sectionstartpage}{\the\beamer@tempcount}}}%
    \addtocontents{nav}{\protect\headcommand{%
        \protect\beamer@subsectionpages{\the\beamer@subsectionstartpage}{\the\beamer@tempcount}}}%
    \beamer@partstartpage=\c@page
    \beamer@sectionstartpage=\c@page
    \beamer@subsectionstartpage=\c@page
    \setcounter{subsection}{0}%
    \protected\def\insertsection{}%
    \protected\def\insertsubsection{}%
    \protected\def\insertsubsubsection{}%
    \protected\def\insertsectionhead{}%
    \protected\def\insertsubsectionhead{}%
    \protected\def\insertsubsubsectionhead{}%
    \def\lastsubsection{}%
    \protected\def\insertpart{\expandafter\hyperlink\partlink}%
    \Hy@writebookmark{\the\c@part}{#1}{Outline\the\c@part}{1}{toc}%
    \hyper@anchorstart{Outline\the\c@part}\hyper@anchorend
    \beamer@atbeginpart
  \fi%
  \beamer@resumemode}%
\def\insertpart{}
\def\partlinkshort{{Navigation1}{}}

\newrobustcmd*\insertromanpartnumber{\@Roman\c@part}
\newrobustcmd*\insertpartnumber{\@arabic\c@part}

\newrobustcmd*\insertshortpart[1][]{%
  \begingroup
    \beamer@setupshort{#1}%
    \beamer@insertshort{\expandafter\hyperlink\partlinkshort}%
  \endgroup}

\mode
<all>
{
  \newrobustcmd\AtBeginPart[1]{%
    \def\beamer@atbeginpart{#1}}
  \AtBeginPart{}
}

\mode
<article>
{
  \ifx\part\@undefined%
  \else
    \renewcommand<>{\part}{\alt#1{\beameroriginal\part}{\beamer@gobbleoptional}}
  \fi
}

%
% Section Definitions
%
\def\sectionname{\translate{Section}}
\newcounter{section}
\renewcommand\thesection{\@arabic\c@section}
\beamer@sectionstartpage=1\relax
\beamer@tocsectionnumber=0\relax

\newcommand<>{\section}{\alt#1{\@ifnextchar[\beamer@section\beamer@@section}{\beamer@secgobble}}

\def\beamer@@section{\@ifnextchar*\beamer@@ssection\beamer@@@section}
\long\def\beamer@@ssection*#1{\beamer@section[{#1}]{}}
\long\def\beamer@@@section#1{\beamer@section[{#1}]{#1}}
\long\def\beamer@section[#1]#2{%
  \beamer@savemode%
  \mode<all>%
  \ifbeamer@inlecture
    \refstepcounter{section}%
    \ifblank{#2}%
    {\long\def\secname{#1}\long\def\lastsection{#1}}%
    {\global\advance\beamer@tocsectionnumber by 1\relax%
      \long\def\secname{#2}%
      \long\def\lastsection{#1}%
      \addtocontents{toc}{\protect\beamer@sectionintoc{\the\c@section}{#2}{\the\c@page}{\the\c@part}%
        {\the\beamer@tocsectionnumber}}}%
    {\let\\=\relax\xdef\sectionlink{{Navigation\the\c@page}{\noexpand\secname}}}%
    \beamer@tempcount=\c@page\advance\beamer@tempcount by -1%
    \addtocontents{nav}{\protect\headcommand{\protect\beamer@sectionpages{\the\beamer@sectionstartpage}{\the\beamer@tempcount}}}%
    \addtocontents{nav}{\protect\headcommand{\protect\beamer@subsectionpages{\the\beamer@subsectionstartpage}{\the\beamer@tempcount}}}%
    \ifblank{#1}{}{%
      \addtocontents{nav}{\protect\headcommand{\protect\sectionentry{\the\c@section}{#1}{\the\c@page}{\lastsection}{\the\c@part}}}%
    }%
    \beamer@sectionstartpage=\c@page%
    \beamer@subsectionstartpage=\c@page%
    \def\insertsection{\expandafter\hyperlink\sectionlink}%
    \def\insertsubsection{}%
    \def\insertsubsubsection{}%
    % Deal with a defective patch in metropolis theme
    \def\insertsectionhead{\hyperlink{Navigation\the\c@page}{#1}}% 
    \edef\insertsectionhead{\noexpand\hyperlink{Navigation\the\c@page}{\unexpanded{#1}}}%
    \def\insertsubsectionhead{}%
    \def\insertsubsubsectionhead{}%
    \def\lastsubsection{}%
    \Hy@writebookmark{\the\c@section}{\secname}{Outline\the\c@part.\the\c@section}{2}{toc}%
    \hyper@anchorstart{Outline\the\c@part.\the\c@section}\hyper@anchorend%
    \ifblank{#2}{\beamer@atbeginsections}{\beamer@atbeginsection}%
  \fi%
  \beamer@resumemode}%
\def\insertsection{}
\def\insertsectionhead{}
\def\insertsectionnumber{\@arabic\c@section}

\def\breakhere{\protect\beamer@breakhere}
\def\beamer@breakhere{}


\mode
<all>
{
  \newrobustcmd*\AtBeginSection{\@ifnextchar[{\beamer@AtBeginSection}{\beamer@@AtBeginSection}}
  \long\def\beamer@@AtBeginSection#1{\beamer@AtBeginSection[{#1}]{#1}}
  \long\def\beamer@AtBeginSection[#1]#2{%
    \def\beamer@atbeginsection{#2}%
    \def\beamer@atbeginsections{#1}%
  }
  \AtBeginSection{}
}

\mode
<article>
{
  \renewcommand<>{\section}{\alt#1{\beameroriginal\section}{\beamer@secgobble}}
}


%
% Subsection Definitions
%
\def\subsectionname{\translate{Subsection}}
\newcounter{subsection}[section]
\renewcommand\thesubsection{\@arabic\c@subsection}

\beamer@subsectionstartpage=1

\newcommand<>{\subsection}{\alt#1{\@ifnextchar[\beamer@subsection\beamer@@subsection}{\beamer@secgobble}}

\def\beamer@@subsection{\@ifnextchar*\beamer@@ssubsection\beamer@@@subsection}
\long\def\beamer@@ssubsection*#1{\beamer@subsection[{#1}]{}}
\def\beamer@@@subsection#1{\beamer@subsection[{#1}]{#1}}
\def\beamer@subsection[#1]#2{%
  \beamer@savemode%
  \mode<all>%
  \ifbeamer@inlecture%
    \refstepcounter{subsection}%
    \ifblank{#2}{\long\def\subsecname{#1}\long\def\lastsubsection{#1}}
    {%
      \long\def\subsecname{#2}%
      \long\def\lastsubsection{#1}%
      \addtocontents{toc}{\protect\beamer@subsectionintoc{\the\c@section}{\the\c@subsection}{#2}{\the\c@page}{\the\c@part}{\the\beamer@tocsectionnumber}}%
    }%
    \beamer@tempcount=\c@page\advance\beamer@tempcount by -1%
    \addtocontents{nav}{\protect\headcommand{\protect\beamer@subsectionpages{\the\beamer@subsectionstartpage}{\the\beamer@tempcount}}}
    \ifblank{#1}{}{%
      \addtocontents{nav}{\protect\headcommand{\protect\beamer@subsectionentry{\the\c@part}{\the\c@section}{\the\c@subsection}{\the\c@page}{\lastsubsection}}}
    }%
    \beamer@subsectionstartpage=\c@page%
    \edef\subsectionlink{{Navigation\the\c@page}{\noexpand\subsecname}}%
    \def\insertsubsection{\expandafter\hyperlink\subsectionlink}%
    \def\insertsubsubsection{}%
    % Deal with a faulty patch in metropolis theme
    \def\insertsubsectionhead{\hyperlink{Navigation\the\c@page}{#1}}%
    \edef\insertsubsectionhead{\noexpand\hyperlink{Navigation\the\c@page}{\unexpanded{#1}}}%
    \def\insertsubsubsectionhead{}%
    \Hy@writebookmark{\the\c@subsection}{\subsecname}{Outline\the\c@part.\the\c@section.\the\c@subsection.\the\c@page}{3}{toc}%
    \hyper@anchorstart{Outline\the\c@part.\the\c@section.\the\c@subsection.\the\c@page}\hyper@anchorend%
    \ifblank{#2}{\beamer@atbeginsubsections}{\beamer@atbeginsubsection}%
  \fi%
  \beamer@resumemode}
\def\insertsubsection{}
\def\insertsubsectionnumber{\@arabic\c@subsection}
\def\insertsubsectionhead{}
\def\lastsubsection{}

\mode
<all>
{
  \newrobustcmd*\AtBeginSubsection{\@ifnextchar[{\beamer@AtBeginSubsection}{\beamer@@AtBeginSubsection}}
  \long\def\beamer@@AtBeginSubsection#1{\beamer@AtBeginSubsection[{#1}]{#1}}
  \long\def\beamer@AtBeginSubsection[#1]#2{%
    \def\beamer@atbeginsubsection{#2}%
    \def\beamer@atbeginsubsections{#1}}
  \AtBeginSubsection{}
}

\mode
<article>
{
  \renewcommand<>{\subsection}{\alt#1{\beameroriginal\subsection}{\beamer@secgobble}}
}


%
% Subsubsection Definitions
%
\newcounter{subsubsection}[subsection]
\renewcommand\thesubsubsection{\@arabic\c@subsubsection}

\beamer@subsectionstartpage=1

\newcommand<>{\subsubsection}{\alt#1{\@ifnextchar[\beamer@subsubsection\beamer@@subsubsection}{\beamer@secgobble}}

\def\beamer@@subsubsection{\@ifnextchar*\beamer@@ssubsubsection\beamer@@@subsubsection}
\long\def\beamer@@ssubsubsection*#1{\beamer@subsubsection[{#1}]{}}
\def\beamer@@@subsubsection#1{\beamer@subsubsection[{#1}]{#1}}
\def\beamer@subsubsection[#1]#2{%
  \beamer@savemode%
  \mode<all>%
  \ifbeamer@inlecture%
    \refstepcounter{subsubsection}%
    \ifblank{#2}{\long\def\subsubsecname{#1}\long\def\lastsubsubsection{#1}}
    {%
      \long\def\subsubsecname{#2}%
      \long\def\lastsubsubsection{#1}%
      \addtocontents{toc}{\protect\beamer@subsubsectionintoc%
        {\the\c@section}{\the\c@subsection}{\the\c@subsubsection}{#2}{\the\c@page}{\the\c@part}{\the\beamer@tocsectionnumber}}%
    }%
    \ifblank{#1}{}{%
      \addtocontents{nav}{\protect\headcommand{\protect\beamer@subsubsectionentry%
        {\the\c@part}{\the\c@section}{\the\c@subsection}{\the\c@subsubsection}{\the\c@page}{\lastsubsubsection}}}%
    }
    \edef\subsubsectionlink{{Navigation\the\c@page}{\noexpand\subsubsecname}}%
    \def\insertsubsubsection{\expandafter\hyperlink\subsubsectionlink}%
    \edef\insertsubsubsectionhead{\noexpand\hyperlink{Navigation\the\c@page}{\unexpanded{#1}}}
    \Hy@writebookmark{\the\c@subsubsection}{\subsubsecname}{Outline\the\c@part.\the\c@section.\the\c@subsection.\the\c@subsubsection.\the\c@page}{4}{toc}%
    \hyper@anchorstart{Outline\the\c@part.\the\c@section.\the\c@subsection.\the\c@subsubsection.\the\c@page}\hyper@anchorend%
    \ifblank{#2}{\beamer@atbeginsubsubsections}{\beamer@atbeginsubsubsection}%
  \fi%
  \beamer@resumemode}
\def\insertsubsubsection{}
\def\insertsubsubsectionnumber{\@arabic\c@subsubsection}
\def\insertsubsubsectionhead{}

\mode
<all>
{
  \newrobustcmd*\AtBeginSubsubsection{\@ifnextchar[{\beamer@AtBeginSubsubsection}{\beamer@@AtBeginSubsubsection}}
  \long\def\beamer@@AtBeginSubsubsection#1{\beamer@AtBeginSubsubsection[{#1}]{#1}}
  \long\def\beamer@AtBeginSubsubsection[#1]#2{%
    \def\beamer@atbeginsubsubsection{#2}%
    \def\beamer@atbeginsubsubsections{#1}}
  \AtBeginSubsubsection{}
}

\mode
<article>
{
  \renewcommand<>{\subsubsection}{\alt#1{\beameroriginal\subsubsection}{\beamer@secgobble}}
}


%
% Appendix stuff
%

\newif\ifbeamer@inappendix

\newcommand<>\beamer@appendix{}
\newcommand<>\appendix{%
  \beamer@inappendixtrue%
  \only#1{\part{\appendixname}%
  \immediate\write\@auxout{\string\@writefile{nav}%
    {\noexpand\headcommand{\noexpand\gdef\noexpand\insertmainframenumber{\the\c@framenumber}}}}%
  \addtocontents{nav}{\protect\headcommand{\protect\beamer@appendixpages{\the\c@page}}}%
  \global\let\appendix\beamer@appendix}%
}

\def\insertappendixframenumber{\the\numexpr\inserttotalframenumber-\insertmainframenumber\relax}
\def\insertframenumberinappendix{\ifbeamer@inappendix\the\numexpr\insertframenumber-\insertmainframenumber\relax\else0\fi}

\mode
<article>
{
  \ifx\appendix\@undefined
  \else
    \renewcommand<>{\appendix}{\only#1{\beameroriginal\appendix}}
  \fi
}


\mode
<all>
