\ProvidesClass{LuaUUThesis}
\NeedsTeXFormat{LaTeX2e}

% The thesis template is based on the standard class book
\LoadClass{book}


% ============================================
%                Basic adjustments
% ============================================

% disabled for now /TA
% \renewcommand{\bibname}{\refname}

% Prevent widows and orphans by setting a very large penalty
\widowpenalty=\@M
\clubpenalty=\@M

% Set spacing around paragraphs and displays
\setlength{\parindent}{\baselineskip}
\providecommand{\mathindent}{\baselineskip plus 3pt minus 3pt}
\renewcommand{\mathindent}{\baselineskip}
\setlength{\parskip}{0pt}
\setlength{\belowdisplayskip}{10pt plus 3pt minus 3pt}
\setlength{\abovedisplayskip}{10pt plus 3pt minus 3pt}
\setlength{\belowdisplayshortskip}{10pt plus 3pt minus 3pt}
\setlength{\abovedisplayshortskip}{10pt plus 3pt minus 3pt}

% No indentation of footnotes
\renewcommand\@makefntext[1]{%
    \parindent 1em%
    \noindent%
    \hb@xt@0.4em{\hss\@makefnmark}#1%
}

\renewcommand\footnoterule{{\leftskip 0pt\noindent\rule{26mm}{.5pt}\vspace{3pt}\par}}

% Remove default spacing between items in lists
\renewcommand{\labelitemi}{\textbullet}%
\renewcommand{\labelitemii}{\textendash}%
\renewcommand{\labelitemiii}{\textasteriskcentered}%
\renewcommand{\labelenumi}{\arabic{enumi}.}%
\renewcommand{\labelenumii}{\alph{enumii})}%
\renewcommand{\labelenumiii}{\roman{enumiii}.}%
\setlength{\labelsep}{0.4em}
\settowidth{\leftmargini}{\@Roman 8.\hspace{\labelsep}}

\renewcommand{\@listI}{%
	\setlength\leftmargin\leftmargini%
	\setlength\parsep{0\p@}%
	\setlength\topsep{0\p@}%
	\setlength\itemsep{0\p@}%
	\setlength{\partopsep}{0pt}%
}
\let\@listi\@listI
\renewcommand{\@listii}{%
	\setlength\leftmargin\leftmarginii%
}
\renewcommand{\@listiii}{%
	\setlength\leftmargin\leftmarginiii%
}
\renewcommand{\@listiv}{%
	\setlength\leftmargin\leftmarginiv%
}
\renewcommand{\@listv}{%
	\setlength\leftmargin\leftmarginv%
}

\newlength{\UU@tmplen}
\newlength{\UU@listindent}
\newcounter{UU@tmpcounter}
\providecommand{\setlistmargin}[1][]{
	\def\tmp{#1}
	\ifx\tmp\@empty
		\settowidth{\UU@tmplen}{\@itemlabel\hspace{\labelsep}}
	\else
		\ifx\@listctr\@undefined
		\settowidth{\UU@tmplen}{\@itemlabel\hspace{\labelsep}}
		\else
		\setcounter{UU@tmpcounter}{\value{\@listctr}}
		\setcounter{\@listctr}{#1}
		\settowidth{\UU@tmplen}{\@itemlabel\hspace{\labelsep}}
		\setlength{\labelwidth}{\UU@tmplen}
		\setcounter{\@listctr}{\value{UU@tmpcounter}}
		\fi
	\fi
	\advance\UU@tmplen-\leftmargin
	\advance\UU@tmplen\UU@listindent
	\advance\leftskip\UU@tmplen
	\advance\leftmargin\UU@tmplen
	\advance\@totalleftmargin\UU@tmplen
	\setlength\linewidth\textwidth
	\advance\linewidth-\@totalleftmargin
}


% ============================================
%          Dispositional Settings
% ============================================

% Default to not using numbering for headings
\setcounter{secnumdepth}{-1}

% Set numbering valid only withing the main matter
\newcounter{secnumdepthmainmatter}
\AtBeginDocument{\setcounter{secnumdepthmainmatter}{\c@secnumdepth}}


% ============================================
%                  Headings
% ============================================

% Provides a readable command to set numbering of headings
\providecommand{\toclevel@part}{-1}
\providecommand{\toclevel@appendix}{0}%
\providecommand{\toclevel@chapter}{0}%
\providecommand{\toclevel@section}{1}%
\providecommand{\toclevel@subsection}{2}%
\providecommand{\toclevel@subsubsection}{3}%
\providecommand{\toclevel@paragraph}{4}%
\providecommand{\toclevel@subparagraph}{5}%

\providecommand{\numberingdepth}[1]{%
   \setcounter{secnumdepth}{\csname toclevel@#1\endcsname}%
}


% ============================================
%              Package fixes
% ============================================

% Prevents raggedright from creating bad line breaks
\@ifpackageloaded{ragged2e}{
	\providecommand{\UU@RaggedRight}{\RaggedRight}
}{%
	\providecommand{\UU@RaggedRight}{\raggedright\parfillskip\fill}%
}


% ============================================
%             Table of contents
% ============================================

% Provides a readable command to set the level of detail in toc
\providecommand{\contentsdepth}[1]{%
   \setcounter{tocdepth}{\csname toclevel@#1\endcsname}%
}


% ============================================
%                List of papers
% ============================================

% disabled for now /TA
%\renewcommand{\bibname}{\refname}
\providecommand{\listofpapersname}{List of papers}
\providecommand{\continuednextpage}{Continued on next page}
\providecommand{\keywordsname}{Keywords}

\providecommand{\listofpapersintro}[1]{\gdef\@listofpapersintro{#1}}
\providecommand{\listofpapersoutro}[1]{\gdef\@listofpapersoutro{#1}}
\listofpapersintro{This thesis is based on the following papers, which are referred to in the text by their roman numerals.}%
\listofpapersoutro{Reprints were made with permission from the publishers.}

\providecommand{\pgs@language@swedish}{%
	\renewcommand{\bibname}{\refname}%
	\renewcommand{\listofpapersname}{Lista \"over delarbeten}%
	\listofpapersintro{Denna avhandling \"ar baserad p\r{a} f\"oljande artiklar, vilka i texten h\"anvisas till med romerska siffror.}%
	\listofpapersoutro{Reproduktion med tillst\aa nd fr\aa n respektive utgivare.}%
	\renewcommand{\continuednextpage}{Fortsatt p\r{a} n\"a{}sta sida}%
	\renewcommand{\keywordsname}{Nyckelord}%
}

\providecommand{\pgs@language@english}{%
	\renewcommand{\bibname}{\refname}%
	\renewcommand{\listofpapersname}{List of papers}%
	\listofpapersintro{This thesis is based on the following papers, which are referred to in the text by their Roman numerals.}%
	\listofpapersoutro{Reprints were made with permission from the publishers.}%
	\renewcommand{\continuednextpage}{Continued on next page}%
	\renewcommand{\keywordsname}{Keywords}%
}

\AtBeginDocument{%
	\@ifpackageloaded{babel}{%
		\addto\captionsswedish{%
			\pgs@language@swedish
		}%
		\addto\captionsenglish{%
			\pgs@language@english
		}%
	}{}%
	\@ifpackageloaded{polyglossia}{%
		\renewcommand{\xpg@hook@setlanguage}{\csname pgs@language@\languagename \endcsname}
		\xpg@hook@setlanguage%
	}{}%
}

\newcounter{listofpapersc}
\renewcommand{\thelistofpapersc}{\Roman{listofpapersc}}
\newenvironment{listofpapers}{%
	\cleardoublepage%
	\@ifundefined{pdfbookmark}{}{\pdfbookmark[0]{\listofpapersname}{lop}}%
	\chapter*{\listofpapersname}%
	\noindent {\@listofpapersintro}\vspace{1\baselineskip}%
    \setcounter{listofpapersc}{0}%
    \begin{list}{\Roman{listofpapersc}}{%
	   \def\@currentlabel{\Roman{listofpapersc}}%
	   \UU@RaggedRight%
	   \hyphenpenalty=10000%
        \usecounter{listofpapersc}%
            \setlength{\leftmargin}{2,5em}%
            \setlength{\labelsep}{1em}%
            \setlength{\itemsep}{1em plus 0.2em minus 0.2em}%
            \setlength{\parsep}{0mm}%
            \setlength{\topsep}{0mm}}%
}{%
    \end{list}\nopagebreak%
    \vspace{1\baselineskip}\noindent {\@listofpapersoutro}%
}


% ============================================
%                Dummy pages
% ============================================

\providecommand{\keywordsname}[1]{Keywords}
\providecommand{\authorshort}[1]{\gdef\@authorshort{#1}}
\providecommand{\address}[1]{\gdef\@address{#1}}
\providecommand{\publisher}[1]{\gdef\@publisher{#1}}
\providecommand{\subtitle}[1]{\gdef\@subtitle{#1}}
\providecommand{\series}[1]{\gdef\@series{#1}}
\providecommand{\serialnumber}[1]{\gdef\@serialnumber{#1}}
\providecommand{\pagecount}[1]{\write\@auxout{\string\gdef\string\@pagecount{#1}}\gdef\@pagecount{#1}\gdef\pagecount##1{}}
\providecommand{\URN}[1]{\gdef\@URN{#1}}
\providecommand{\ISBN}[1]{\gdef\@ISBN{#1}}
\providecommand{\ISSN}[1]{\gdef\@ISSN{#1}}
\providecommand{\keywords}[1]{\gdef\@keywords{#1}}
\providecommand{\titlepagelogo}[1]{\gdef\@titlepagelogo{#1}}%
\let\@publisher\relax
\let\@series\relax
\let\@serialnumber\relax
\let\@authorshort\relax
\let\@address\relax
\let\@titlepagelogo\relax
\providecommand{\@pagecount}{??}
\let\@keywords\relax
\let\@ISSN\relax
\let\@ISBN\relax
\let\@URN\relax

\title{Title Page Dummy}
\providecommand{\@subtitle}{This Page will be Replaced before Printing}
\author{ }
\titlepagelogo{assets/images/UU_logo_sv_42}
\providecommand{\makehalftitle}{%
   \begin{center}%
      \ifx\@publisher\relax\ifx\@series\relax Half Title Dummy Page\par \fi\fi%
      \MakeUppercase{\@publisher}\par%
      \@series\par%
      \@serialnumber\par%
   \end{center}%
}

\renewenvironment{titlepage}{%
   \cleardoublepage%
}{%
   \clearpage%
}

\renewcommand{\maketitle}{%
   \providecommand{\titlepagelogo}{assets/images/UU_logo_sv_42}%
   \begin {titlepage}%
      \def\first##1{##1}%
      \def\last##1{##1}%
      \def\I##1{##1}%
      \vspace*{40mm}%
      \begin{center}%
         {\Large \@author\par}%
         \vspace{6mm}%
         \Huge{\@title}\par%
         \vspace{6mm}%
         {\fontsize{14}{16}\fontshape{it}\selectfont \@subtitle}%
      \end{center}%
      \begin{figure}[b]%
         \begin{center}%
            \immediate\openin0 =\@titlepagelogo\space%
            \ifeof 0\relax\null%
               %\ClassWarning{UUThesisTemplate warning: Image \@titlepagelogo does not exist, placeholder used.}\null
               \framebox(120,120)[c]{Title page logo}%
            \else%
               \ifx\includegraphics\undefined%
                  \framebox(120,120)[c]{Title page logo}%
               \else%
                  \includegraphics{\@titlepagelogo}%
               \fi%
            \fi%
            \immediate\closein0%
         \end{center}%
      \end{figure}%
   \end{titlepage}%
   \global \let \title \relax %
   \global \let \author \relax %
   \global \let \and \relax%
}

\newcommand{\dedicationfont}[1]{\gdef\@dedicationfont{#1}}
\newcommand{\@dedicationfont}{\textit}
\newcommand{\dedication}[1]{%
   \cleardoublepage%
   \vspace*{\stretch{3}}%
   \begin{flushright}%
   {\@dedicationfont\Large\selectfont\emph{#1}}%
   \end{flushright}%
   \vspace*{\stretch{1}}%
}

\providecommand{\abstracthead}{}
\providecommand{\abstractfoot}{}
\providecommand{\abstractsection}{\vskip\baselineskip\noindent\textbf{Abstract}\par\noindent\ignorespaces}
\providecommand{\abstractpreamble}{\clearpage\thispagestyle{empty}}
\providecommand{\abstractfont}{\fontsize{9}{11}\fontshape{rm}\selectfont}

\newenvironment{abstract}{%
   \abstractfont\abstractpreamble %
   \abstracthead%
   \abstractsection %
}{%
\abstractfoot%
}

\providecommand{\abstractdummy}[1]{\gdef\@abstractdummy{#1}}
\abstractdummy{\clearpage\vspace*{\stretch{1}}\hfill Abstract Dummy Page. \hfill\vspace*{\stretch{1}}\clearpage}

\newcommand{\frontmatterCS}{\maketitle\@abstractdummy}

\AtEndDocument{
   \pagecount{\the\c@page}
}


% ============================================
%        Compability and short-hands
% ============================================

\newenvironment{numberedlist}{
   \begin{enumerate}
	\setlistmargin[5]
}{\end{enumerate}}

\newenvironment{numberedlist-indent}{
	\setlength{\UU@listindent}{1cm}
	\addtolength{\leftmargini}{\UU@listindent}
	\begin{enumerate}
		\setlistmargin[5]
}{\end{enumerate}}

\newenvironment{enumerate-indent}{
	\setlength{\UU@listindent}{1cm}
	\addtolength{\leftmargini}{\UU@listindent}
	\begin{enumerate}
		\setlistmargin[5]
}{\end{enumerate}}

\newenvironment{bulletlist}{
	\begin{itemize}
		\setlistmargin
}{\end{itemize}}

\newenvironment{bulletlist-indent}{
	\setlength{\UU@listindent}{1cm}
	\addtolength{\leftmargini}{\UU@listindent}
	\begin{itemize}
		\setlistmargin
}{\end{itemize}}

\newenvironment{itemize-indent}{
	\setlength{\UU@listindent}{1cm}
	\addtolength{\leftmargini}{\UU@listindent}
	\begin{itemize}
		\setlistmargin
}{\end{itemize}}

\newenvironment{romanlist}{
	\renewcommand{\labelenumi}{\Roman{enumi}.}
   \renewcommand{\labelenumii}{\roman{enumii}.}
   \begin{enumerate}
		\setlistmargin[8]
}{\end{enumerate}}

\newenvironment{romanlist-indent}{
	\setlength{\UU@listindent}{1cm}
	\addtolength{\leftmargini}{\UU@listindent}
	\begin{romanlist}
}{\end{romanlist}}

\newenvironment{simplelist}{
	\begin{list}{}{
		\setlength{\leftmargin}{0mm}
	   \setlength{\itemsep}{0mm}
	   \setlength{\parsep}{0mm}
	   \setlength{\topsep}{0mm}
}}{\end{list}}

\newenvironment{simplelist-indent}{
	\begin{list}{}{
		\setlength{\leftmargin}{1cm}
      \setlength{\itemsep}{0mm}
      \setlength{\parsep}{0mm}
      \setlength{\topsep}{0mm}
}}{\end{list}}

\newenvironment{definitionlist}[1][]{
	\@afterindenttrue\par\parindent -1em\advance\leftskip-\parindent\renewcommand{\item}[1]{\par{\it #1 ##1} -- \ignorespaces}
}{\par}

\newenvironment{definitionlist-indent}[1][]{
	\leftskip 1cm\begin{definitionlist}[#1]
}{\end{definitionlist}}

\newcommand{\code}[1]{{\small\texttt{#1}}}
\newcommand\listheading[1]{%
	\if@noskipsec\leavevmode\fi %
	\par\@tempskipa 13pt \@plus 2pt \@minus 3pt\relax\@afterindentfalse%
	\if@nobreak%
		\everypar{}%
	\else%
		\addpenalty\@secpenalty\addvspace\@tempskipa%
	\fi%
	\noindent\textit{#1}%
}

