% Language definition file (part of polyglossia v2.10 -- 2026/04/18)
%
\ProvidesFile{gloss-russian.ldf}[polyglossia: module for Russian]

\RequirePackage{xpg-cyrillicnumbers}

\PolyglossiaSetup{russian}{
  bcp47=ru,
  bcp47-language=ru,
  bcp47-variant=luna1918,
  bcp47-script=Cyrl,
  script=Cyrillic,
  scripttag=cyrl,
  langtag=RUS,
  hyphennames={russian},
  hyphenmins={2,2},
  frenchspacing=true,
  indentfirst=true,
  localnumeral=russiannumerals,
  Localnumeral=Russiannumerals
}

% BCP-47 compliant aliases
\setlanguagealias*{russian}{ru}
\setlanguagealias*[spelling=modern]{russian}{ru-luna1918}
\setlanguagealias*[spelling=old]{russian}{ru-petr1708}

\newif\if@russian@modern

\def\captionsrussian{%
   \if@russian@modern\captionsrussian@modern\else\captionsrussian@old\fi%
}%

\def\daterussian{%
   \if@russian@modern\daterussian@modern\else\daterussian@old\fi%
}%

\newif\ifcyrillic@numerals
\newif\ifcyrillic@asbuk@numerals

\DeclareKeys[polyglossia/gloss/russian]
  {
    babelshorthands.if = russian@babelshorthands,
    babelshorthands.default:n = true,
    forceheadingpunctuation.if = russian@forceheadingpunctuation,
    forceheadingpunctuation.default:n = true,
    mathfunctions.if = russian@mathfunctions,
    mathfunctions.default:n = true,
    spelling.choice:,
    spelling / modern.code =
      {%
        \@russian@moderntrue
        \SetLanguageKeys{russian}{bcp47=ru,bcp47-variant=luna1918}%
      },
    spelling / old.code =
      {%
        \@russian@modernfalse
        \SetLanguageKeys{russian}{bcp47=ru-petr1708,bcp47-variant=petr1708}%
      },
    spelling.default:n = modern,
    numerals.choice:,
    numerals / arabic.code =
      {%
        \cyrillic@numeralsfalse
        \cyrillic@asbuk@numeralsfalse
        \SetLanguageKeys{russian}{bcp47-extension-u=nu-latn}%
      },
    numerals / cyrillic.code =
      {%
        \cyrillic@numeralstrue
        \cyrillic@asbuk@numeralsfalse
        \SetLanguageKeys{russian}{bcp47-extension-u=nu-cyrl}%
      },
    numerals / cyrillic-trad.code =
      {%
        \cyrillic@numeralstrue
        \cyrillic@asbuk@numeralsfalse
        \SetLanguageKeys{russian}{bcp47-extension-u=nu-cyrl}%
      },
    numerals / cyrillic-alph.code =
      {%
        \cyrillic@numeralstrue
        \cyrillic@asbuk@numeralstrue
        \SetLanguageKeys{russian}{bcp47-extension-u=nu-cyrl}%
      },
    numerals.default:n = arabic,
    numerals/unknown.code = \xpg@warning{Unknown Russian numerals value `#1'},
    indentfirst.choice:,
    indentfirst / true.code =
      {%
        \SetLanguageKeys{russian}{indentfirst=true}%
      },
    indentfirst / false.code =
      {%
        \SetLanguageKeys{russian}{indentfirst=false}%
      },
    indentfirst.default:n = true,
  }

\ifsystem@babelshorthands
  \SetGlossOptions{russian}{babelshorthands=true}
  % Register default options
  \InitializeGlossOptions{russian}{babelshorthands=true,
                                       spelling=modern,
                                       numerals=arabic,
                                       indentfirst=true,
                                       forceheadingpunctuation=true,
                                       mathfunctions=true}
\else
  \SetGlossOptions{russian}{babelshorthands=false}
  % Register default options
  \InitializeGlossOptions{russian}{babelshorthands=false,
                                       spelling=modern,
                                       numerals=arabic,
                                       indentfirst=true,
                                       forceheadingpunctuation=true,
                                       mathfunctions=true}
\fi

\InitializeBabelShorthands

\def\russian@shorthands{%
  \xpg@activate@shorthands%
  \def\language@group{russian}%
%  \declare@shorthand{russian}{"`}{„}%
%  \declare@shorthand{russian}{"'}{“}%
%  \declare@shorthand{russian}{"<}{«}%
%  \declare@shorthand{russian}{">}{»}%
  \declare@shorthand{russian}{""}{\xpg@invisible@break}%
  \declare@shorthand{russian}{"~}{\xpg@nobreak@hyphen}%
  \declare@shorthand{russian}{"=}{\xpg@breakable@hyphen}%
  \declare@shorthand{russian}{"|}{\xpg@ligature@break}%
  \declare@shorthand{russian}{"-}{\xpg@cyrillic@dash}%
  \declare@shorthand{russian}{",}{\xpg@thinspace@allow@hyphenation@after}%
}

\ifx\cyrdash\undefined
  \def\cyrdash{\leavevmode\hbox to.8em{\textendash\hss\textendash}}%
\fi

\def\norussian@shorthands{%
  \xpg@deactivate@shorthands%
}


\def\captionsrussian@modern{%
   \def\prefacename{Предисловие}%
   \def\refname{Список литературы}%
   \def\abstractname{Аннотация}%
   \def\bibname{Литература}%
   \def\chaptername{Глава}%
   \def\appendixname{Приложение}%
   \@ifundefined{thechapter}%
     {\def\contentsname{Содержание}}%
     {\def\contentsname{Оглавление}}%
   \def\listfigurename{Список иллюстраций}%
   \def\listtablename{Список таблиц}%
   \def\indexname{Предметный указатель}%
   \def\authorname{Именной указатель}%
   \def\figurename{Рис.}%
   \def\tablename{Таблица}%
   \def\partname{Часть}%
   \def\enclname{вкл.}%
   \def\ccname{исх.}%
   \def\headtoname{вх.}%
   \def\pagename{с.}%
   \def\seename{см.}%
   \def\alsoname{см.~также}%
   \def\proofname{Доказательство}%
}
\def\daterussian@modern{%
      \def\today{\number\day%
      \space\ifcase\month\or%
      января\or
      февраля\or
      марта\or
      апреля\or
      мая\or
      июня\or
      июля\or
      августа\or
      сентября\or
      октября\or
      ноября\or
      декабря\fi%
      \space \number\year\space г.}%
}
     
\def\captionsrussian@old{%
   \def\prefacename{Предисловіе}%
   \def\refname{Примѣчанія}%
   \def\abstractname{Аннотація}%
   \def\bibname{Библіографія}%
   \def\chaptername{Глава}%
   \def\appendixname{Приложеніе}%
   \@ifundefined{thechapter}%
     {\def\contentsname{Содержаніе}}%
     {\def\contentsname{Оглавленіе}}%
   \def\listfigurename{Списокъ иллюстрацій}%
   \def\listtablename{Списокъ таблицъ}%
   \def\indexname{Предмѣтный указатель}%
   \def\authorname{Именной указатель}%
   \def\figurename{Рис.}%
   \def\tablename{Таблица}%
   \def\partname{Часть}%
   \def\enclname{вкл.}%
   \def\ccname{исх.}%
   \def\headtoname{вх.}%
   \def\pagename{с.}%
   \def\seename{см.}%
   \def\alsoname{см.~также}%
   \def\proofname{Доказательство}%
}

\def\daterussian@old{%
      \def\today{\number\day%
      \space\ifcase\month\or%
      января\or
      февраля\or
      марта\or
      апреля\or
      мая\or
      іюня\or
      іюля\or
      августа\or
      сентября\or
      октября\or
      ноября\or
      декабря\fi%
      \space \number\year\space г.}%
}

% Russian needs trailing dots in all headings
\def\xpg@save@autodot{}
\ifdef{\KOMAClassName}{%
    \providecommand*\autodot{}%
    \let\xpg@save@autodot\autodot%
}{}

\def\russian@capsformat{%
  \ifrussian@forceheadingpunctuation%
   \ifdef{\KOMAClassName}{%
      \renewcommand*\autodot{.}%
   }{%
      % The following is based on some ideas from ruscor.sty
      \def\@seccntformat##1{\csname pre##1\endcsname%
         \csname the##1\endcsname%
         \csname post##1\endcsname}%
       \def\@aftersepkern{\hspace{0.5em}}%
       \def\postchapter{.\@aftersepkern}%
       \def\postsection{.\@aftersepkern}%
       \def\postsubsection{.\@aftersepkern}%
       \def\postsubsubsection{.\@aftersepkern}%
       \def\postparagraph{.\@aftersepkern}%
       \def\postsubparagraph{.\@aftersepkern}%
       \def\prechapter{}%
       \def\presection{}%
       \def\presubsection{}%
       \def\presubsubsection{}%
       \def\preparagraph{}%
       \def\presubparagraph{}%
    }%
  \fi%
}

\def\norussian@capsformat{%
  \ifrussian@forceheadingpunctuation%
    \ifdef{\KOMAClassName}{%
       \let\autodot\xpg@save@autodot%
    }{%
       \def\@seccntformat##1{\csname the##1\endcsname\quad}% = LaTeX kernel
    }%
  \fi%
}

\newcommand{\russiannumerals}[2]{\russiannumber{#2}}
\newcommand{\Russiannumerals}[2]{\Russiannumber{#2}}

\def\russiannumber#1{%
  \ifcyrillic@numerals
    \ifcyrillic@asbuk@numerals
      \russian@asbuk@alph{#1}%
    \else
      \cyr@alph{#1}%
    \fi
  \else
    \number#1%
  \fi%
}

\def\Russiannumber#1{%
  \ifcyrillic@numerals
    \ifcyrillic@asbuk@numerals
      \russian@asbuk@Alph{#1}%
    \else
      \cyr@Alph{#1}%
    \fi
  \else
    \number#1%
  \fi%
}

\let\russiannumeral=\russiannumber
\let\Russiannumeral=\Russiannumber

\def\Asbuk#1{\expandafter\russian@asbuk@Alph\csname c@#1\endcsname}
\def\asbuk#1{\expandafter\russian@asbuk@alph\csname c@#1\endcsname}

\def\AsbukTrad#1{\expandafter\cyr@Alph\csname c@#1\endcsname}
\def\asbukTrad#1{\expandafter\cyr@alph\csname c@#1\endcsname}


% This is a poor man's cyrillic alphanumeric. It just uses the alphabet and
% thus ends at 30.
\def\russian@asbuk@Alph#1{\ifcase#1\or
   А\or Б\or В\or Г\or Д\or Е\or Ж\or
   З\or И\or К\or Л\or М\or Н\or О\or
   П\or Р\or С\or Т\or У\or Ф\or Х\or
   Ц\or Ч\or Ш\or Щ\or Э\or Ю\or Я%
   \else\xpg@ill@value{#1}{russian@asbuk@Alph}\fi%
}

\def\russian@asbuk@alph#1{\ifcase#1\or
   а\or б\or в\or г\or д\or е\or ж\or
   з\or и\or к\or л\or м\or н\or о\or
   п\or р\or с\or т\or у\or ф\or х\or
   ц\or ч\or ш\or щ\or э\or ю\or я%
   \else\xpg@ill@value{#1}{russian@asbuk@alph}\fi%
}

\def\russian@numbers{%
   \let\latin@alph\@alph
   \let\latin@Alph\@Alph
   \ifcyrillic@numerals%
     \def\russian@alph##1{\expandafter\russiannumeral\expandafter{\the##1}}%
     \def\russian@Alph##1{\expandafter\Russiannumeral\expandafter{\the##1}}%
     \let\@alph\russian@alph%
     \let\@Alph\russian@Alph%
   \fi
}

\def\norussian@numbers{%
   \let\@alph\latin@alph%
   \let\@Alph\latin@Alph%
}

\def\noextras@russian{%
   \norussian@capsformat%
   \ifcyrillic@numerals\norussian@numbers\fi%
   \ifrussian@babelshorthands\norussian@shorthands\fi%
}

\def\blockextras@russian{%
   \russian@capsformat%
   \ifcyrillic@numerals\russian@numbers\else\norussian@numbers\fi%
   \ifrussian@babelshorthands\russian@shorthands\else\norussian@shorthands\fi%
}

\def\inlineextras@russian{%
   \ifrussian@babelshorthands\russian@shorthands\else\norussian@shorthands\fi%
}

%%% These lines taken from russianb.ldf, part of babel package.
\AtBeginDocument{%
\ifrussian@mathfunctions%
  \def\sh    {\mathop{\operator@font sh}\nolimits}
  \def\ch    {\mathop{\operator@font ch}\nolimits}
  \def\tg    {\mathop{\operator@font tg}\nolimits}
  \def\arctg {\mathop{\operator@font arctg}\nolimits}
  \def\arcctg{\mathop{\operator@font arcctg}\nolimits}
  \def\th    {\mathop{\operator@font th}\nolimits}
  \def\ctg   {\mathop{\operator@font ctg}\nolimits}
  \def\cth   {\mathop{\operator@font cth}\nolimits}
  \def\cosec {\mathop{\operator@font cosec}\nolimits}
  \def\Prob  {\mathop{\kern\z@\mathsf{P}}\nolimits}
  \def\Variance{\mathop{\kern\z@\mathsf{D}}\nolimits}
  \def\nod   {\mathop{\mathrm{н.о.д.}}\nolimits}
  \def\nok   {\mathop{\mathrm{н.о.к.}}\nolimits}
  \def\NOD   {\mathop{\mathrm{НОД}}\nolimits}
  \def\NOK   {\mathop{\mathrm{НОК}}\nolimits}
  \def\Proj  {\mathop{\mathrm{Пр}}\nolimits}
  %\DeclareRobustCommand{\No}{№}
\fi
}

\endinput

