%\iffalse
% luabidi.dtx generated using makedtx version 0.94b (c) Nicola Talbot
% Command line args:
%   -src "arabmaths\.tex=>arabmaths.tex"
%   -src "autofootnoterule\.tex=>autofootnoterule.tex"
%   -src "luabidi\.sty=>luabidi.sty"
%   -src "textwidthfootnoterule\.tex=>textwidthfootnoterule.tex"
%   -doc "doc/test.tex"
%   -dir "tex"
%   luabidi
% Created on 2013/5/27 3:45
%\fi
%\iffalse
%<*package>
%% \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         \~}
%</package>
%\fi
% \iffalse
% Doc-Source file to use with LaTeX2e
% Copyright (C) 2013 Arthur Reutenauer, all rights reserved.
% \fi
% \iffalse
%<*driver>
\documentclass[12pt]{article}
\usepackage[arabmaths]{luabidi}
\begin{document}
\DocInput{luabidi.dtx}
\end{document}
%</driver>
%\fi
%\setRTLmain
%\section{ghesmate 1}
%
%in yek ghesmat ast ke an ra dar inja minevisam
%
%\begin{equation}
%1+2=3
%\end{equation}
%in ham dobare masalei ast ke man minevisam va shoma an ra mibinid.
%
%
%\StopEventually{}
%\section{The Code}
%\iffalse
%    \begin{macrocode}
%<*arabmaths.tex>
%    \end{macrocode}
%\fi
\luatexmathdir TRT
\def\@eqnnum{{\normalfont \normalcolor \luatextextdir TLT ({\luatextextdir TRT\theequation})}}
%\iffalse
%    \begin{macrocode}
%</arabmaths.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*autofootnoterule.tex>
%    \end{macrocode}
%\fi
\ifx \CatEscape\undefined
    \chardef\CatEscape=0
    \chardef\CatOpen=1
    \chardef\CatClose=2
    \chardef\CatIgnore=9
    \chardef\CatLetter=11
    \chardef\CatOther=12
    \chardef\CatActive=13		% is defined in Plain already

    \chardef\CatUsCode=\catcode`\_
\fi

\catcode`\_=\CatLetter			% top level macro file


\def\l@fn{%
  \hbox to \columnwidth
  {\luatexpagedir TLT\vbox{\kern -3\p@
   \hrule width .4\columnwidth \kern2.6\p@}\hfil}}
\def\r@fn{%
   \hrule width .4\columnwidth\kern 2.6\p@}


\def\@makefnmark{\hbox{$^{\hbox{\scriptsize\@thefnmark}}\m@th$}}


\def\leftfootnoterule{\global\def\footnoterule{\l@fn}}
\def\rightfootnoterule{\global\def\footnoterule{\r@fn}}
\leftfootnoterule

%%% \beginprog
\newread\old_foot_file
\newwrite\foot_file
\def\foot_file_name{\jobname.fot\relax}
\def\init_footnote{%
   \openin\old_foot_file\foot_file_name
   \ifeof\old_foot_file  \closein\old_foot_file
   \else  \closein\old_foot_file
      \read_foot_file
   \fi
   \immediate\openout\foot_file\foot_file_name
   \immediate\write\foot_file{\relax}%
   \global\let\init_footnote\relax
   }

\newcount\foot_name_no  % for generating footnote mark names





\newcount\autofootnote
\def\fnpp_next_footnote{%
    \init_footnote
    \global\advance\foot_name_no\@ne
    \global\advance\c@footnote\@ne
    \edef\do_write{%
	\immediate\write\foot_file{%
	    \string\advance\autofootnote\@ne
	    \string\expandafter\xdef
			\string\csname\space f@\number\foot_name_no \endcsname{%
		\string\number\autofootnote
		}%
	    }%
	}%
    \do_write
    \global\autofootnote 0\csname f@\number\foot_name_no \endcsname \relax
    }

\def\read_foot_file{%
   \begingroup
      \catcode`\@\CatLetter \catcode`\^^M\CatIgnore
      \input \foot_file_name
   \endgroup
   }

\let\fnpp_orig_outputpage=\@outputpage
\def\@outputpage{%
    \ifx \init_footnote\relax
	\immediate\write\foot_file{\autofootnote\z@}%
    \fi
    \fnpp_orig_outputpage
    }

\let\FnppOrigFootnote=\footnote		% save original bindings
\let\FnppOrigFootnotemark=\footnotemark

\def\footnote{%
    \@ifnextchar[%			% ] (Emacs)
	\@xfootnote
	{\fnpp_next_footnote \ifnum\autofootnote=1\rightfootnoterule\fi \@xfootnote[\the\c@footnote] %
}%
    }
\def\footnotemark{%
    \@ifnextchar[%			% ] (Emacs)
	\@xfootnotemark
	{\fnpp_next_footnote \ifnum\autofootnote=1\rightfootnoterule\fi \@xfootnotemark[\the\c@footnote]}%
    }




\def\footnotemarkLR{%
    \@ifnextchar[%			% ] (Emacs)
	\@xfootnotemark
	{\fnpp_next_footnote \ifnum\autofootnote=1\leftfootnoterule\fi\@xfootnotemark[\the\c@footnote]}%
    }


\def\Footnote#1{%
\begingroup
\footnotemarkLR%
\renewcommand{\thefootnote}{\@arabic\c@footnote}%
\luatexpardir TLT \luatextextdir TLT\footnotetext{#1}%
\endgroup
}
%\iffalse
%    \begin{macrocode}
%</autofootnoterule.tex>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*luabidi.sty>
%    \end{macrocode}
%\fi
%% This file is luabidi.sty
%%
%% Copyright © 2009 Vafa Khalighi, 2013 Arthur Reutenauer
%%
%%%% It may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{luabidi}[2013/05/27 v0.2
	Bidirectional typesetting in LuaTeX]
\newif\if@RTL
\newif\if@RTLmain
\def\setRTLmain{\luatexpagedir TRT \luatexbodydir TRT \luatexpardir TRT \luatextextdir TRT}
\def\setRTL{\@RTLtrue\luatexpardir TRT \luatextextdir TRT}
\let\setRL=\setRTL
\def\setLTR{\@RTLfalse\luatexpardir TLT \luatextextdir TLT}
\let\setLR=\setLTR
\let\unsetRTL=\setLTR
\let\unsetLTR=\setRTL
\def\RTL{\@RTLtrue\trivlist \luatexpardir TRT \luatextextdir TRT\item\relax}
\def\endRTL{\@RTLfalse\endtrivlist}
\def\LTR{\trivlist \luatexpardir TLT \luatextextdir TLT\item\relax}
\def\endLTR{\endtrivlist}
\def\@ensure@RTL#1{\if@RTL#1\else\RLE{#1}\fi}
\def\@ensure@LTR#1{\if@RTL#1\else\LRE{#1}\fi}
\def\@ensure@dir#1{\if@RTL\RLE{#1}\else{#1}\fi}
\let\@ensure@maindir=\@ensuredir
\@RTLmainfalse
\DeclareOption{arabmaths}{\input{arabmaths}}
\DeclareOption{textwidthfootnoterule}{\input{textwidthfootnoterule}}
\let\n@xt=\
\DeclareRobustCommand*\LRE{\protect\pLRE}
\def\pLRE{\protect\afterassignment\moreLRE \let\n@xt= }
\DeclareRobustCommand*\RLE{\protect\pRLE}
\def\pRLE{\protect\afterassignment\moreRLE \let\n@xt= }
\def\bracetext{\ifcat\n@xt{\else\ifcat\n@xt}\fi
  \errmessage{Missing left brace has been substituted}\fi \bgroup}
\def\moreLRE{\bracetext \aftergroup\unsetLTR \setLTR\@RTLfalse}
\def\moreRLE{\bracetext \aftergroup\unsetRTL \setRTL\@RTLtrue}
\let\LR=\LRE
\let\RL=\RLE

\def\Footnote#1{%
\begingroup
\footnotemark
\renewcommand{\thefootnote}{\@arabic\c@footnote}%
\luatexpardir TLT \luatextextdir TLT\footnotetext{#1}%
\endgroup
}

\DeclareOption{autofootnoterule}{\input{autofootnoterule}}



\ProcessOptions
\endinput
%\iffalse
%    \begin{macrocode}
%</luabidi.sty>
%    \end{macrocode}
%\fi
%\iffalse
%    \begin{macrocode}
%<*textwidthfootnoterule.tex>
%    \end{macrocode}
%\fi
\def\footnoterule{\kern-3\p@
  \hrule \@width \textwidth \kern 2.6\p@}
%\iffalse
%    \begin{macrocode}
%</textwidthfootnoterule.tex>
%    \end{macrocode}
%\fi
%\Finale
\endinput
