% ----------------------------------------------------------------------- %
% File:    koma.sty           KOMA customizations                         %
% Author:  Abdó Roig-Maranges <abdo.roig@gmail.com>             2012-2013 %
% ----------------------------------------------------------------------- %

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{abdokoma}



% ----------------------------------------------------------------------- %
% Layout                                                                  %
% ----------------------------------------------------------------------- %

% Linespread
\newcommand{\widelinespread}{\linespread{1.3}}
\newcommand{\regularlinespread}{\linespread{1}}



% ----------------------------------------------------------------------- %
% Options                                                                 %
% ----------------------------------------------------------------------- %

% Hooks
\providecommand{\hyperrefhook}{}
\providecommand{\sectioninghook}{}


% Draft option
\DeclareOption{draft}{
  \ExecuteOptions{regularspread}
  \PassOptionsToPackage{showkeys}{draftools}
}


% Line spread options
\DeclareOption{widespread}{
  \widelinespread
}
\DeclareOption{regularspread}{
  \regularlinespread
}


% Hyperref option
\DeclareOption{hyperref}{
  \PassOptionsToPackage{final}{hyperref}
  \renewcommand{\hyperrefhook}{%
    \hypersetup{%
      pdfencoding=auto,%
      unicode,%
      psdextra,%
      pdftitle={\@title},%
      pdfauthor={\@author},%
      colorlinks,%
      citecolor=citecolor,%
      filecolor=filecolor,%
      linkcolor=linkcolor,%
      urlcolor=urlcolor%
    }%
  }
}

\DeclareOption{nohyperref}{
  \PassOptionsToPackage{draft}{hyperref}
  \renewcommand{\hyperrefhook}{}
}

% Sectioning depth (tocdepth3 means paragraph is depth 3, after section and
% subsection)
\DeclareOption{tocdepth3}{
  \renewcommand{\sectioninghook}{
    \setupsectioning{3}
    \@ifclassloaded{scrreprt}{\setupchapterpart}{}
    \@ifclassloaded{scrbook}{\setupchapterpart}{}
  }
}

\DeclareOption{tocdepth2}{
  \renewcommand{\sectioninghook}{
    \setupsectioning{2}
    \@ifclassloaded{scrreprt}{\setupchapterpart}{}
    \@ifclassloaded{scrbook}{\setupchapterpart}{}
  }
}


% Defaults
\ExecuteOptions{tocdepth3}
\ExecuteOptions{hyperref}
\ExecuteOptions{regularspread}


% Process options of the package.
\ProcessOptions

% Delay options to be processed.
\AtBeginDocument{\setupheader}



% ----------------------------------------------------------------------- %
% Package loading                                                         %
% ----------------------------------------------------------------------- %

% NOTE: without etex, tikz + beamer complain about not enough room for dimendions.
\RequirePackage{etex}                   % etex stuff
\RequirePackage{iftex}                  % conditionals on tex engine
\RequirePackage{ifpdf}                  % conditional on pdf output

% NOTE: needed on texlive 2016 due to some packages not following recent lualatex changes.
\ifLuaTeX
    \RequirePackage{luatex85}
\fi

\RequirePackage{babel}                  % language support
\RequirePackage[nodayofweek]{datetime}  % date formating

%\RequirePackage{etoolbox}   % e-tex tools
%\RequirePackage{ifthen}     % Make decisions inside latex
%\RequirePackage{calc}       % Arithmetic with lengths
%\RequirePackage{xparse}     % Extended command definitions
%\RequirePackage{xkeyval}    % Use of Key value pairs

% page layout
\RequirePackage{geometry}               % Geometry of the page
\RequirePackage[automark]{scrpage2}     % Customizing headings
\RequirePackage{titletoc}               % Tweaking TOC

% bibliography
\RequirePackage[autostyle]{csquotes}    % quotation (required by biblatex).
\RequirePackage[                        % Bibliography
    backend=biber,
    style=numeric,
    url=false,
    doi=true,
    eprint=true
]{biblatex}

% biblatex tweaks
\renewbibmacro{in:}{}

% ams
\RequirePackage{amsmath}          % AMS stuff

% graphics and color
\RequirePackage{xcolor}           % provides color
\RequirePackage{graphicx}         % Include external graphics

\ifpdf
  \RequirePackage{tikz}           % Graphics
  \usetikzlibrary{matrix}
  \usetikzlibrary{calc}
  \usetikzlibrary{decorations.markings}
  \usetikzlibrary{cd}
  \tikzcdset{arrow style=tikz}
\fi

% personal
\RequirePackage{abdocolor}        % Custom colors
\RequirePackage{abdoalias}        % My aliases
\RequirePackage{abdofonts}        % My font settings
\RequirePackage{abdothms}         % My theorems
\RequirePackage{draftools}        % Various draft tools

% typesetting tools
\RequirePackage{url}              % provides URL typesetting

% NOTE: hyperref must load last, or it breaks showkeys...
\RequirePackage{hyperref}

% NOTE: but cleveref must load after hyperref
\RequirePackage[                        % Smart references
    noabbrev,
    capitalise
]{cleveref}



% ----------------------------------------------------------------------- %
% Visual appearence                                                       %
% ----------------------------------------------------------------------- %

% change emph
\let\emphold\emph
\renewcommand{\emph}[1]{\textbf{\color{emphcolor}#1}}


\newcommand{\setupheader}{%
  \setkomafont{pageheadfoot}{\sffamily}
  \pagestyle{scrheadings}%
  \setheadsepline{0.0pt}%
  \ihead{\headmark} \chead{} \ohead{}%
  %\ifoot{} \cfoot{} \ofoot{}%
}


\newcommand{\setupsectioning}[1]{
  % section colors
  \addtokomafont{section}{\color{section1color}}
  \addtokomafont{subsection}{\color{section2color}}
  \addtokomafont{subsubsection}{\color{section3color}}
  \addtokomafont{paragraph}{\color{paragraphcolor}}
  \pagecolor{backgroundcolor}
  \color{textcolor}

  \setcounter{tocdepth}{4}

  \renewcommand*{\toclevel@section}{1}
  \renewcommand*{\toclevel@subsection}{2}

  \def\sectioning@argument{#1}
  \if\sectioning@argument2
    \def\sectioning@paragraphmargin{2.5em}
    \renewcommand*{\toclevel@paragraph}{2}
  \else
    \def\sectioning@paragraphmargin{4.5em}
    \renewcommand*{\toclevel@paragraph}{3}
  \fi

  % table of contents style (titletoc)
  \titlecontents{section}[1em]{\bfseries\sffamily\vspace*{1ex}}{\thecontentslabel. }
    {}{\titlerule*[1pc]{}\thecontentspage}[]

  \titlecontents{subsection}[2.5em]{}{\sffamily\thecontentslabel. }
    {}{\ \titlerule*[1em]{$\cdot$}\ \thecontentspage}[]

  \titlecontents*{paragraph}[\sectioning@paragraphmargin]{\sffamily\itshape\small}{\thecontentslabel. }
    {}{\, \thecontentspage}[\hspace*{0.5em} -- \hspace*{0.5em}]
}


\newcommand{\setupchapterpart}{
  % chapter and part colors
  \addtokomafont{part}{\color{chaptercolor}}
  \addtokomafont{chapter}{\color{chaptercolor}}

  % table of contents style
  \renewcommand*{\toclevel@part}{-1}
  \titlecontents{part}[0pt]{\bfseries\sffamily\Large\vspace*{1ex}}{\thecontentslabel. }
      {}{}[]

  \renewcommand*{\toclevel@chapter}{0}
  \titlecontents{chapter}[0pt]{\bfseries\sffamily\vspace*{1ex}}{\thecontentslabel. }
      {}{\titlerule*[1pc]{}\thecontentspage}[]
}



% ----------------------------------------------------------------------- %
% Miscelania                                                              %
% ----------------------------------------------------------------------- %

% Unnumbereds footnotes
\def\unfootnote{\xdef\@thefnmark{}\@footnotetext}


% Shortcut to select a tex-title and an alternate string to embed in pdf's
\newcommand{\texorpdf}[2]{\texorpdfstring{#1}{#2}}



% ----------------------------------------------------------------------- %
% Run hooks                                                               %
% ----------------------------------------------------------------------- %
\hyperrefhook
\sectioninghook