%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
%%
%\iffalse
%
% (c) Copyright 2019 Apostolos Syropoulos Bat-erdene Altangerel
% This program can be redistributed and/or modified under the
% terms of the LaTeX Project Public License Distributed from
% http://www.latex-project.org/lppl.txt; either
% version 1.3c of the License, or any later version.
%
% This work has the LPPL maintenance status `maintained'.
%
% Please report errors or suggestions for improvement to
%
%    Apostolos Syropoulos  ([email protected])
%
%\fi
% \CheckSum{228}
% \iffalse This is a Metacomment
%
%<xecyrmongolian, >\ProvidesFile{xecyrmongolian.sty}
%
%<xecyrmongolian, > [2019/12/14 v1.0.0 Package `xecyrmongolian.sty']
%
%    \begin{macrocode}
%<*driver>
\documentclass{ltxdoc}
\GetFileInfo{xecyrmongolian.drv}
\usepackage{xltxtra}
\usepackage{fullpage}
\begin{document}
\setmainfont[Mapping=tex-text]{Arno Pro}
\setmonofont{UM Typewriter}
\setsansfont[Mapping=tex-text]{GFS Neohellenic}
  \DocInput{xecyrmongolian.dtx}
\end{document}
%</driver>
%    \end{macrocode}
% \fi
%\StopEventually{}
%\title{Mongolian Cyrillic Support for\\ \XeLaTeX\ and Lua\LaTeX}
%\author{Apostolos Syropoulos\\
%        Xanthi, Greece\\
%        \texttt{[email protected]} \and
%        Bat-erdene Altangerel\\
%        Ulaanbaatar, Mongolia\\
%        \texttt{[email protected]}
%}
% \date{2019/12/14}
%\maketitle
% \begin{abstract}
% The \textsf{xecyrmongolian} package provides basic support for Mongolian Cyrillic so to be able
% to prepare documents with either \XeLaTeX or Lua\LaTeX.
%\end{abstract}
%
%\section{Introduction}
%
% The package \textsf{xecyrmongolian} has been designed for people who want to prepare documents whose
% main language is Mongolian Cyrillic and want to typeset their work with either \XeLaTeX or Lua\LaTeX.
% The package allows users to load other hyphenation patterns so to be able to create trully
% multilingial documents. In addition, all standard enumerations use the Cyrillic alphabet used in
% Mongolia. The following simple \LaTeX\ code is a typical usage example of the package.
%\begin{verbatim}
%       \documentclass[a4paper]{article}
%       \usepackage{xltxtra}
%       \usepackage{enumitem}
%       \usepackage{xecyrmongolian}
%       \begin{document}
%         \setmainfont{MenkGarqagTig.ttf} % or any font you like
%         \begin{enumerate}[label=(\Alph*)]
%            \item an apple
%            \item a banana
%            \item a carrot
%            \item a durian
%         \end{enumerate}
%         \Useg{12}
%       \end{document}
%\end{verbatim}
% \section{The Source Code}
% First we define the various strings that correspond to the standard \LaTeX\ captions.
%    \begin{macrocode}
%<*xecyrmongolian>
\message{Package `xecyrmongolian' version 1.0.0 by ^^J%
        Apostolos Syropoulos and Bat-erdene Altangerel}
\def\prefacename{Оршил}%
\def\refname{Ашигласан ном}%
\def\abstractname{Товчлол}%
\def\bibname{Ашигласан номзүй}%
\def\chaptername{Бүлэг}%
\def\appendixname{Хавсралт}%
\def\contentsname{Гарчиг}%
\def\listfigurename{Зургийн жагсаалт}%
\def\listtablename{Хүснэгтийн жагсаалт}%
\def\indexname{Товъёог}%
\def\figurename{Зураг}%
\def\tablename{Хүснэгт}%
\def\partname{Хэсэг}%
\def\enclname{Оруулах}%
\def\ccname{Мэдэгдэл}%
\def\headtoname{}%
\def\pagename{Хуудас}%
\def\seename{Үзнэ үү}%
\def\alsoname{мөн үзнэ үү}%
\def\proofname{Баталгаа:}%
\def\glossaryname{Тайлбар}%
%    \end{macrocode}
%  \begin{macro}{\Useg}
%  \begin{macro}{\useg}
% Next, we define the macros |\Useg| and |\useg| that are the Mongolian counterpart of |\Alph| and
% |\alph|, respectively. However, these commands should not be used in enumerations, etc. It
% is better to make the |\Alph| commands to produce Cyrillic letters by giving the command
% |\usegalph|. The behavior of this command is the default behavior of the package.
%    \begin{macrocode}
\def\Useg#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
   Ш\or Щ\or Ъ\or Ы\or Ь\or Э\or Ю\or Я\else\@ctrerr\fi}
\def\useg#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
   ш\or щ\or ъ\or ы\or ь\or э\or ю\or я\else\@ctrerr\fi}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
% The previous commands do not work if their argument is a counter. And since we may want to
% use them in enumeration or to number chapters, we introduce the following commands that work
% properly when their arguments are counters.
%    \begin{macrocode}
\def\useg@mong#1{\expandafter\useg\expandafter{\the#1}}
\def\Useg@mong#1{\expandafter\Useg\expandafter{\the#1}}
%    \end{macrocode}
%  \begin{macro}{\mongmonth}
% Now we redefine |\today| so as to produce dates in Mongolian. The
% names of months are defined by the macro |\mongmonth|.
%    \begin{macrocode}
\def\mongmonth{%
 \ifcase\month\or 1-р ~сарын\or 2-р ~сарын\or 3-р ~сарын\or 4-р ~сарын\or
 5-р ~сарын\or 6-р ~сарын\or 7-р ~сарын\or 8-р ~сарын\or 9-р ~сарын\or
 10-р ~сарын\or 11-р ~сарын\or 12-р ~сарын\fi}
\def\today{\number\year~оны~\mongmonth\space \number\day}
%    \end{macrocode}
%  \end{macro}
% Lua\LaTeX\ and \XeLaTeX\ have different ways to load hyphenation patterns. Package
% \textsf{luahyphenrules} by Javier Bezos facilitates this process for people who
% want to use Lua\LaTeX\ and the ``traditional'' way to load hyphenation patterns.
% To ensure proper inclusion of LuaTeX staff, I use the following ``idiom'':
% \begin{center}
% |\ifx\directlua\undefined |\texttt{\textit{non Lua\LaTeX\ code}}|\else |%
% \texttt{\textit{Lua\LaTeX\ code}}|\fi|
% \end{center}
%    \begin{macrocode}
\ifx\directlua\undefined\else\RequirePackage{luahyphenrules}\fi
%    \end{macrocode}
% The code that follows loads the hyphenation patterns. The \XeLaTeX\ code
% is quite standard and depends on the \textsf{babel} pattern loading mechanism, while the
% Lua\LaTeX\ code uses the |\HyphenRules| macro, which has essentially the functionality
% of the |\selectlanguage| macro.
%    \begin{macrocode}
\ifx\directlua\undefined%
  \language\l@mongolian\else\HyphenRules{mongolian}\fi%
%    \end{macrocode}
% By default the Mongolian alphabetic enumeration is used instead of enumerations with Latin letters.
%    \begin{macrocode}
\let\latin@alph\@alph
\let\latin@Alph\@Alph
\let\@alph\useg@mong
\let\@Alph\Useg@mong
%    \end{macrocode}
%  \begin{macro}{\nousegalph}
%  \begin{macro}{\usegalph}
% If for some reason, the user needs to have the original enumeration back, then the user should used
% the command |\nousegalph|. And if she wants to switch back, then she has to use the |\usegalph|
% command:
%    \begin{macrocode}
\def\nousegalph{%
 \let\@alph\latin@alph
 \let\@Alph\latin@Alph}
\def\usegalph{%
 \let\@alph\useg@mong
 \let\@Alph\Useg@mong}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%  \begin{macro}{\setlanguage}
% We provide the |\setlanguage| command which
% activates the hypehnation patterns of some other language. It is similar
% to babel's |\selectlanguage|, but we opted to use a new name to avoid possible name conflicts.
% Valid arguments include |monogreek|, |mongolian|, and |american|. As was noted
% previously, package \textsf{luahyphenrules} provides the command |\HyphenRules| which has exactly
% the same functionality as this command. So when using Lua\LaTeX\ users will actually use the
% |\HyphenRules| command. And since the main language of the documenyt will be Mongolian, we
% have to load the Mongolian hypehnation patterns.
%    \begin{macrocode}
\ifx\directlua\undefined%
 \def\setlanguage#1{%
    \expandafter\ifx\csname l@#1\endcsname\relax%
    \typeout{^^J Error: No hyphenation pattern for language #1 loaded,}%
    \typeout{ default hyphenation patterns are used.^^J}%
    \language=0%
    \else\language=\csname l@#1\endcsname\fi}
\else
 \let\setlanguage\HyphenRules
\fi
\setlanguage{mongolian}
%</xecyrmongolian>
%    \end{macrocode}
%  \end{macro}
% \Finale