\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{xepersian}[2008/07/17 v0.16111111
	Typesetting Persian with XeTeX]
\RequirePackage{graphicx}
\RequirePackage{amssymb}
\RequirePackage{amsmath}
\RequirePackage{amsthm}
\RequirePackage{fontspec}
\RequirePackage{bidi}
\RequirePackage{xltxtra}
\RequirePackage{xunicode}
\RequirePackage{etoolbox}
\RequirePackage{persianpoem,fmultico}
\AtBeginDocument{\ifdefined\farsifont\relax\else%
\PackageWarning{XePersian}{\string\farsifont\ is not defined! XePersian will try to load Nazli}%
\newfontfamily\farsifont[Script=Arabic,Scale=1.2,Mapping=farsidigits]{Nazli}\fi
}%

\renewcommand{\thepage}{\rl{\the\c@page}}

%to make the digits in math formula persian
\font\tenrgm="Nazli" at 12pt
\font\sevrgm="Nazli" at 10pt
\font\fivrgm="Nazli" at 7pt
\newfam\fardig
\textfont\fardig=\tenrgm
\scriptfont\fardig=\sevrgm
\scriptscriptfont\fardig=\fivrgm
\def\maths{
  \XeTeXmathcode`0="7 \fardig "06F0%
  \XeTeXmathcode`1="7 \fardig "06F1%
  \XeTeXmathcode`2="7 \fardig "06F2%
  \XeTeXmathcode`3="7 \fardig "06F3%
  \XeTeXmathcode`4="7 \fardig "06F4%
  \XeTeXmathcode`5="7 \fardig "06F5%
  \XeTeXmathcode`6="7 \fardig "06F6%
  \XeTeXmathcode`7="7 \fardig "06F7%
  \XeTeXmathcode`8="7 \fardig "06F8%
  \XeTeXmathcode`9="7 \fardig "06F9%
  \XeTeXmathcode`.="7 \fardig "066B%decimal point
}
\XeTeXmathcode`٫="7 \fardig "066B%decimal point
\XeTeXmathcode`۰="7 \fardig "06F0%
\XeTeXmathcode`۱="7 \fardig "06F1%
\XeTeXmathcode`۲="7 \fardig "06F2%
\XeTeXmathcode`۳="7 \fardig "06F3%
\XeTeXmathcode`۴="7 \fardig "06F4%
\XeTeXmathcode`۵="7 \fardig "06F5%
\XeTeXmathcode`۶="7 \fardig "06F6%
\XeTeXmathcode`۷="7 \fardig "06F7%
\XeTeXmathcode`۸="7 \fardig "06F8%
\XeTeXmathcode`۹="7 \fardig "06F9%

\AtBeginDocument{\maths\setRL\farsifont}


%To convert the MILADI date to SHAMSI date, taken from farsitex
\newif\ifFT@leap \newif\ifFT@kabiseh
\newcount\FT@i  \newcount\FT@y  \newcount\FT@m  \newcount\FT@d
\newcount\FT@latini    \newcount\FT@farsii
\newcount\FT@latinii   \newcount\FT@farsiii
\newcount\FT@latiniii  \newcount\FT@farsiiii
\newcount\FT@latiniv   \newcount\FT@farsiiv
\newcount\FT@latinv    \newcount\FT@farsiv
\newcount\FT@latinvi   \newcount\FT@farsivi
\newcount\FT@latinvii  \newcount\FT@farsivii
\newcount\FT@latinviii \newcount\FT@farsiviii
\newcount\FT@latinix   \newcount\FT@farsiix
\newcount\FT@latinx    \newcount\FT@farsix
\newcount\FT@latinxi   \newcount\FT@farsixi
\newcount\FT@latinxii  \newcount\FT@farsixii
                       \newcount\FT@farsixiii

\newcount\FT@temp
\newcount\FT@temptwo
\newcount\FT@tempthree
\newcount\FT@yModHundred
\newcount\FT@thirtytwo
\newcount\FT@dn
\newcount\FT@sn
\newcount\FT@mminusone

\def\ftoday{%
\FT@y=\year \FT@m=\month \FT@d=\day
%
\FT@temp=\FT@y
\divide\FT@temp by 100\relax
\multiply\FT@temp by 100\relax
\FT@yModHundred=\FT@y
\advance\FT@yModHundred by -\FT@temp\relax
%
\ifodd\FT@yModHundred
   \FT@leapfalse
\else
   \FT@temp=\FT@yModHundred
   \divide\FT@temp by 2\relax
   \ifodd\FT@temp\FT@leapfalse
   \else
      \ifnum\FT@yModHundred=0%
         \FT@temp=\FT@y
         \divide\FT@temp by 400\relax
         \multiply\FT@temp by 400\relax
         \ifnum\FT@y=\FT@temp\FT@leaptrue\else\FT@leapfalse\fi
      \else\FT@leaptrue
      \fi
   \fi
\fi
%
\FT@latini=31\relax
\ifFT@leap
  \FT@latinii = 29\relax
\else
  \FT@latinii = 28\relax
\fi
\FT@latiniii = 31\relax
\FT@latiniv  = 30\relax
\FT@latinv = 31\relax
\FT@latinvi = 30\relax
\FT@latinvii = 31\relax
\FT@latinviii = 31\relax
\FT@latinix = 30\relax
\FT@latinx = 31\relax
\FT@latinxi = 30\relax
\FT@latinxii = 31\relax
%
\FT@thirtytwo=32\relax
%
\FT@temp=\FT@y
\advance\FT@temp by -17\relax
\FT@temptwo=\FT@temp
\divide\FT@temptwo by 33\relax
\multiply\FT@temptwo by 33\relax
\advance\FT@temp by -\FT@temptwo
\ifnum\FT@temp=\FT@thirtytwo\FT@kabisehfalse
\else
   \FT@temptwo=\FT@temp
   \divide\FT@temptwo by 4\relax
   \multiply\FT@temptwo by 4\relax
   \advance\FT@temp by -\FT@temptwo
   \ifnum\FT@temp=\z@\FT@kabisehtrue\else\FT@kabisehfalse\fi
\fi
%
% --BE
% In fact farsii is equal to the Leap years from a fixed year to the last
% year minus the Kabise years from a fixed year to the last year plus a const.
%
\FT@tempthree=\FT@y                 % Number of Leap years
\advance\FT@tempthree by -1
\FT@temp=\FT@tempthree              % T := (MY-1) div 4
\divide\FT@temp by 4\relax
\FT@temptwo=\FT@tempthree           % T := T - ((MY-1) div 100)
\divide\FT@temptwo by 100\relax
\advance\FT@temp by -\FT@temptwo
\FT@temptwo=\FT@tempthree           % T := T + ((MY-1) div 400)
\divide\FT@temptwo by 400\relax
\advance\FT@temp by \FT@temptwo
\advance\FT@tempthree by -611       % Number of Kabise years
\FT@temptwo=\FT@tempthree           % T := T - ((SY+10) div 33) * 8
\divide\FT@temptwo by 33\relax
\multiply\FT@temptwo by 8\relax
\advance\FT@temp by -\FT@temptwo
\FT@temptwo=\FT@tempthree           %
\divide\FT@temptwo by 33\relax
\multiply\FT@temptwo by 33\relax
\advance\FT@tempthree by -\FT@temptwo
\ifnum\FT@tempthree=32\advance\FT@temp by 1\fi % if (SY+10) mod 33=32 then Inc(T);
\divide\FT@tempthree by 4\relax     % T := T - ((SY+10) mod 33) div 4
\advance\FT@temp by -\FT@tempthree
\advance\FT@temp by -137            % T := T - 137  Adjust the value
\FT@farsii=31
\advance\FT@farsii by -\FT@temp                 % now 31 - T is the farsii
%
\FT@farsiii = 30\relax
\ifFT@kabiseh
  \FT@farsiiii = 30\relax
\else
  \FT@farsiiii = 29\relax
\fi
\FT@farsiiv  = 31\relax
\FT@farsiv   = 31\relax
\FT@farsivi  = 31\relax
\FT@farsivii = 31\relax
\FT@farsiviii= 31\relax
\FT@farsiix  = 31\relax
\FT@farsix   = 30\relax
\FT@farsixi  = 30\relax
\FT@farsixii = 30\relax
\FT@farsixiii= 30\relax
%
\FT@dn= 0\relax
\FT@sn= 0\relax
\FT@mminusone=\FT@m
\advance\FT@mminusone by -1\relax
%
\FT@i=0\relax
\ifnum\FT@i < \FT@mminusone
\loop
\advance \FT@i by 1\relax
\advance\FT@dn by \csname FT@latin\romannumeral\the\FT@i\endcsname
\ifnum\FT@i<\FT@mminusone \repeat
\fi
\advance \FT@dn by \FT@d
%
\FT@i=1\relax
\FT@sn = \FT@farsii
\ifnum \FT@sn<\FT@dn
\loop
\advance \FT@i by 1\relax
\advance\FT@sn by \csname FT@farsi\romannumeral\the\FT@i\endcsname
\ifnum \FT@sn<\FT@dn \repeat
\fi
\ifnum \FT@i < 4
   \FT@m = 9 \advance\FT@m by \FT@i
   \advance \FT@y by -622\relax
\else
   \FT@m = \FT@i \advance \FT@m by -3\relax
   \advance \FT@y by -621\relax
\fi
\advance\FT@sn by -\csname FT@farsi\romannumeral\the\FT@i%
\endcsname
\ifnum\FT@i = 1
  \FT@d = \FT@dn \advance \FT@d by 30 \advance\FT@d by -\FT@farsii
\else
  \FT@d = \FT@dn \advance \FT@d by -\FT@sn
\fi
\beginL\number\FT@d\endL\space%
%Changedc from here YJ
\farsimonth{\FT@m}\space\beginL\number\FT@y\endL%
}
% added \farsimonth YJ
\def\farsimonth#1{\ifcase#1\or فروردین\or 
اردیبهشت\or 
خرداد\or تیر\or 
مرداد\or 
شهریور\or مهر\or 
آبان\or آذر\or 
دی\or بهمن\or 
اسفند\fi}

\def\normalfont{\farsifont}
\def\today{\ftoday}
\let\@footnote=\footnote
\def\footnote#1{\@footnote{\farsifont#1}}
\let\@uthanks=\thanks
\def\thanks#1{\@uthanks{\farsifont#1}}
\let\@uframetitle=\frametitle
\def\frametitle#1{\@uframetitle{\centerline{\farsifont#1}}}
\let\@uframesubtitle=\framesubtitle
\def\framesubtitle#1{\@uframesubtitle{\centerline{\farsifont#1}}}
\let\@usubtitle=\subtitle
\def\subtitle#1{\@usubtitle{\farsifont#1}}
\let\@uinstitute=\institute
\def\institute#1{\@uinstitute{\farsifont#1}}
\let\@ucaption=\caption
\def\caption#1{\@ucaption{\farsifont#1}}
\let\@uleftmark=\leftmark
\def\leftmark{\beginR\farsifont\@uleftmark\endR}
\let\@urightmark=\rightmark
\def\rightmark{\beginR\farsifont\@urightmark\endR}




\renewcommand{\thefootnote}{\arabic{footnote}}
\renewcommand{\thefigure}{\arabic{figure}}
\renewcommand{\thetable}{\arabic{table}}
\renewcommand{\theequation}{\arabic{equation}}


\renewcommand{\figurename}{\farsifont شکل}
\renewcommand{\tablename}{\farsifont جدول}
\renewcommand{\contentsname}{فهرست مطالب}
\renewcommand{\listfigurename}{لیست تصاویر}
\renewcommand{\listtablename}{لیست جداول}
\renewcommand{\appendixname}{پیوست}
\renewcommand{\indexname}{نمایه}


\@ifclassloaded{article}{%
\renewcommand{\refname}{مراجع}
\renewcommand{\abstractname}{چکیده}
\renewcommand\appendix{\par
  \setcounter{section}{0}%
  \setcounter{subsection}{0}%
  \gdef\thesection{\@farsi\c@section}}
}{}
\@ifclassloaded{amsart}{%
\renewcommand{\refname}{مراجع}
\renewcommand{\abstractname}{چکیده}
\renewcommand\appendix{\par
  \setcounter{section}{0}%
  \setcounter{subsection}{0}%
  \gdef\thesection{\@farsi\c@section}}
}{}
\@ifclassloaded{report}{%
\renewcommand{\bibname}{کتاب‌نامه}
\renewcommand{\abstractname}{چکیده}
\renewcommand{\chaptername}{فصل}
\renewcommand{\partname}{بخش}
%to make appendix numbering farsi
\renewcommand\appendix{\par
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@farsi\c@chapter}
}%end appendix
}{}
\@ifclassloaded{amsbook}{%
\renewcommand{\bibname}{کتاب‌نامه}
\renewcommand{\chaptername}{فصل}
\renewcommand{\partname}{بخش}
%to make appendix numbering farsi
\renewcommand\appendix{\par
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@farsi\c@chapter}
}%end appendix
}{}
\@ifclassloaded{bookest}{%
\NoHyper
\renewcommand{\bibname}{کتاب‌نامه}
\renewcommand{\chaptername}{فصل}
\renewcommand{\partname}{بخش}
%to make appendix numbering farsi
\renewcommand\appendix{\par
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@farsi\c@chapter}
}%end appendix
}{}
\@ifclassloaded{book}{%
\renewcommand{\bibname}{کتاب‌نامه}
\renewcommand{\chaptername}{فصل}
\renewcommand{\partname}{بخش}
%to make appendix numbering farsi
\renewcommand\appendix{\par
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@farsi\c@chapter}
}%end appendix
}{}
\@ifclassloaded{refrep}{%
\renewcommand{\bibname}{کتاب‌نامه}
\renewcommand{\chaptername}{فصل}
\renewcommand{\partname}{بخش}
%to make appendix numbering farsi
\renewcommand\appendix{\par
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@farsi\c@chapter}
}%end appendix
}{}



%We redefine arabic counter style
\def\@@number#1{\ifmmode\else\beginL\fi{#1}\ifmmode\else\endL\fi}
\def\@@latin#1{\@@number{{\@fromrl#1}}}
\def\@number{\protect\@@number}
\def\@latin{\protect\@@latin}
\let\@farsi=\@arabic
\let\@@arabic=\@arabic
\let\@@roman=\@roman
\let\@@Roman=\@Roman
\let\@@alph=\@alph
\let\@@Alph=\@Alph
\def\@arabic#1{\ifmmode\else\@number{\@@arabic#1}\fi}
\def\@Roman#1{\@latin{\@@Roman#1}}
\def\arabicnorl#1{\expandafter\@@arabic\csname c@#1\endcsname}
\def\make@lr#1{\begingroup
    \toks@=\expandafter{#1}%
    \edef\x{\endgroup
  \def\noexpand#1{\noexpand\@number{\the\toks@}}}%
  \x}
\def\@character#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 ی\fi}

\def\@farsi#1{\farsifont\@character{\@@arabic#1}}


%The figure or table caption
\newsavebox\@Arabitempboxa
\long\def\@makecaption#1#2{%
  \vskip\abovecaptionskip
  \sbox\@Arabitempboxa{\beginR #1: #2\endR}%
  \ifdim \wd\@Arabitempboxa >\hsize
  \beginR #1: #2\endR\par
  \else
    \global \@minipagefalse
    \hb@xt@\hsize{\hfil\box\@Arabitempboxa\hfil}%
  \fi
  \vskip\belowcaptionskip}


%The correct the right footnorerule so it does not overlap the texts
\def\r@fn{%
  \hbox to \columnwidth
  {\beginR \vbox{\kern -3\p@
   \hrule width .4\columnwidth \kern2.6\p@}\hfil\endR}}
\def\footnoterule{\r@fn}


%a command to put a footnote from left to right
%while in a right to left context
\newcommand{\Footnote}[1]{%
\bgroup% To make the scope of the change local
\footnotemark%
\renewcommand{\thefootnote}{\arabic{footnote}}%
\unsetRL%
\footnotetext{#1}%
\egroup
}


%For Right-to-Left two column command, I guess taken from rlbabel
\makeatletter 
\let\@old@outputdblcol\@outputdblcol
\newcommand{\rl@outputdblcolumn}{%
  \if@firstcolumn
    \global \@firstcolumnfalse
    \global \setbox\@leftcolumn \box\@outputbox
  \else
    \global \@firstcolumntrue
    \setbox\@outputbox \vbox {\hb@xt@\textwidth {%
                              \hskip\columnwidth%
                              \hfil\vrule\@width\columnseprule\hfil
                              \hb@xt@\columnwidth {%
                                \box\@leftcolumn \hss}%
                              \hb@xt@\columnwidth {%
                                \hskip-\textwidth%
                                \box\@outputbox \hss}%
                              \hskip\columnsep%
                              \hskip\columnwidth}}%
    \@combinedblfloats
    \@outputpage
    \begingroup
      \@dblfloatplacement
      \@startdblcolumn
      \@whilesw\if@fcolmade \fi
        {\@outputpage
         \@startdblcolumn}%
    \endgroup
 \fi}
\newcommand{\rldblcolumn}{\renewcommand{\@outputdblcol}{\rl@outputdblcolumn}}
\newcommand{\lrdblcolumn}{\renewcommand{\@outputdblcol}{\@old@outputdblcol}}
%set the default direction of the twocolumn texts to Right-to-Left
\rldblcolumn


% define the XePersian logo
\def\reflect#1{{\setbox0=\hbox{#1}\rlap{\kern0.5\wd0
  \special{x:gsave}\special{x:scale -1 1}}\box0 \special{x:grestore}}}
\def\XePersian{\leavevmode$\smash{\hbox{X\lower.5ex
  \hbox{\kern-.125em\reflect{E}}Persian}}$}


\newenvironment{english}{\par\vspace{.5cm}\setLR\begin{rmfamily}}{\end{rmfamily}\par\vspace{.5cm}}
\def\lr#1{\LR{\rmfamily{#1}}}
\def\rl#1{\RL{\farsifont#1}}


%To fix tabular problem in bidi. Taken from farsitex
\newif\if@tabsw
\global\@tabswfalse
\def\@tabular{\if@rl\global\@tabswtrue\fi
     \leavevmode \hbox \bgroup \if@tabsw\beginR\normalfont \fi
     $\let\@acol\@tabacol
     \let\@classz\@tabclassz
     \let\@classiv\@tabclassiv \let\\\@tabularcr\@tabarray}
\def\endtabular{\crcr\egroup\if@tabsw\egroup\endR\egroup\fi
		 \egroup $\if@tabsw\endR\fi \egroup
		 \global\@tabswfalse}
\expandafter \let \csname endtabular*\endcsname = \endtabular

\def\@array[#1]#2{\setbox\@arstrutbox=\hbox{\vrule
     height\arraystretch \ht\strutbox
     depth\arraystretch \dp\strutbox
     width\z@}\@mkpream{#2}\edef\@preamble{%
\halign \noexpand\@halignto
\bgroup \tabskip\z@ \@arstrut \@preamble \tabskip\z@ \cr}%
\let\@startpbox\@@startpbox \let\@endpbox\@@endpbox
\if #1t\vtop \else \if#1b\vbox \else \vcenter \fi\fi
\bgroup \let\par\relax
\let\@sharp##\let\protect\relax \lineskip\z@\baselineskip\z@
\if@tabsw\hbox\bgroup\beginR\vbox\bgroup\fi
\@preamble}

%taken from rlbabel.ldf
\newif\if@rl
\AtBeginDocument{% Here we set the main document direction
  \newif\if@rlmain%
  \if@rl% e.g: if the options to babel were [english,hebrew]
    \@rlmaintrue%
  \else%  e.g: if the options to babel were [hebrew,english]
    \@rlmainfalse%
  \fi%
}

%To fix eqref problem taken from amsmath
\let\@@text=\text
\def\text#1{\@@text{\RL{#1}}}

\def\tagform@#1{\maketag@@@{)\ignorespaces\text{#1}\unskip\@@italiccorr(}}
\renewcommand{\eqref}[1]{(\ref{#1})}

%To fix equation numbers. taken from amsmath
{\renewenvironment{equation}{%
  \unsetRL
  \incr@eqnum
  \mathdisplay@push
  \st@rredfalse \global\@eqnswtrue
  \mathdisplay{equation}%
\setRL
}{%
\unsetRL
  \endmathdisplay{equation}%
  \mathdisplay@pop
  \ignorespacesafterend
\setRL
}}


%almost taken verbatim from polyglossia for making abjad numbering in enumerate environment
\providecommand*{\xpg@warning}[1]{%
   \PackageWarning{XePersian}%
   {#1}}      
\ifcsdef{abjad}{}{%
\def\abjad#1{%
\ifnum#1>1999 \xpg@warning{Illegal value (#1) for abjad numeral} {#1}
\else
  \ifnum#1<\z@\space\xpg@warning{Illegal value (#1) for abjad numeral}%
  \else
    \ifnum#1<10\expandafter\abj@num@i\number#1%
    \else
      \ifnum#1<100\expandafter\abj@num@ii\number#1%
      \else
        \ifnum#1<\@m\expandafter\abj@num@iii\number#1%
        \else
          \ifnum#1<\@M\expandafter\abj@num@iv\number#1%since #1<2000, we must have 1000
          \fi
        \fi
      \fi
    \fi
  \fi
\fi
}
\def\abjad@zero{}
\def\abj@num@i#1{%
  \ifcase#1\or ا\or ب\or ج\char"200D\or د% 
           \or ه\or و\or ز\or ح\or ط\fi
  \ifnum#1=\z@\abjad@zero\fi}
\def\abj@num@ii#1{%
  \ifcase#1\or ی\or ک\or ل\or م\or ن%
           \or س\or ع\or ف\or ص\fi 
  \ifnum#1=\z@\fi\abj@num@i}
\def\abj@num@iii#1{%
  \ifcase#1\or ق\or ر\or ش\or ت\or ث%
            \or خ\or ذ\or ض\or ظ\fi
  \ifnum#1=\z@\fi\abj@num@ii}
\def\abj@num@iv#1{%
  \ifcase#1\or غ\fi
  \ifnum#1=\z@\fi\abj@num@iii}
}

\def\farsi@numbers{%
   \let\@latinalph\@alph%
   \let\@latinAlph\@Alph%
   \let\@alph\abjad%
   \let\@Alph\abjad%
}


\AtBeginDocument{\farsi@numbers}


\endinput
