% Copyright 2016 Slavonic Computing Initiative
% http://sci.ponomar.net
%
\ProvidesPackage{churchslavonic}[v0.2.4 2023.06.22 Typesetting in Church Slavonic]

\DeclareOption{autocolormarks}{
	\PassOptionsToPackage{\CurrentOption}{cu-kruk}
}
\DeclareOption{noautocolormarks}{
	\PassOptionsToPackage{\CurrentOption}{cu-kruk}
}
\DeclareOption*{
	\PassOptionsToPackage{\CurrentOption}{cu-kinovar}
}
\ProcessOptions\relax

\RequirePackage{ifluatex}
\RequirePackage{cu-num}
\RequirePackage{cu-calendar}
\RequirePackage{cu-util}
\RequirePackage{cu-kinovar}
\RequirePackage{cu-kruk}

% underscore is a valid character in Church Slavonic
\let\cu@oldunderscore=_
{%
\catcode`\_\active
\global\protected\def_{\ifmmode\cu@oldunderscore\else\textunderscore\discretionary{}{}{}\fi}%
}%
\AtBeginDocument{\catcode`\_\active}%

\ifluatex
\AtBeginDocument{\cu@set@hyphenchar@lualatex}
\else
\AtBeginDocument{\cu@set@hyphenchar@xelatex}
\fi

\def\cu@set@hyphenchar@lualatex{
  \@ifpackageloaded{polyglossia}{%
    \textchurchslavonic{\prehyphenchar=`\_}%
  }{%
  }%
}%

\def\cu@set@hyphenchar@xelatex{
}%

% suppress variable distance between lines
\lineskiplimit -1ex

% margin marks
\def\cu@mmark@counter{0}%
\def\cuMarginMarkSkip{0.6em}%
\def\cuMarginMarkText#1{#1}%
\protected\def\cuMarginMark#1{%
\leavevmode
\global\edef\cu@mmark@counter{\the\numexpr 1 + \cu@mmark@counter}%
\label{mnote.\cu@mmark@counter}%
\ifodd\getpagerefnumber{mnote.\cu@mmark@counter}\relax
  \vadjust{\hbox{\vbox to 0pt{\vss\hbox to 0pt{\hskip\textwidth\hskip\cuMarginMarkSkip \cuMarginMarkText{#1}\hss}\vskip 0.25\baselineskip}}}%
\else
  \vadjust{\hbox{\vbox to 0pt{\vss\hbox to 0pt{\hss \cuMarginMarkText{#1}\hskip\cuMarginMarkSkip}\vskip 0.25\baselineskip}}}%
\fi
}%


\endinput
