%%
%% This is file `biditools.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% bidi.dtx  (with options: `biditools.sty')
%% 
%%   ______________________________________
%%   Copyright © 2009–2012  Vafa Khalighi
%% 
%%   License information appended.
%% 
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{biditools}[2012/01/01 v0.1 Programming tools for bidi package]
\def\@tagsextension{clo}
\def\@definitionfileextension{def}
\def\@iftagsloaded{\@ifl@aded\@tagsextension}
\def\@ifdefinitionfileloaded{\@ifl@aded\@definitionfileextension}
\def\eqnewif#1#2{%
  \count@\escapechar \escapechar\m@ne
    \let#1\iffalse
    \let#2\iffalse
    \eq@if#1#2\iftrue
    \eq@if#1#2\iffalse
  \escapechar\count@}
\def\eq@if#1#2#3{%
  \expandafter\def\csname\expandafter\@gobbletwo\string#1%
                    \expandafter\@gobbletwo\string#3\endcsname
                       {\let#1#3%
                       \let#2#3}%
  \expandafter\def\csname\expandafter\@gobbletwo\string#2%
                    \expandafter\@gobbletwo\string#3\endcsname
                       {\let#2#3%
                       \let#1#3}}
\newcommand*{\if@bidi@csdef}[1]{%
  \ifcsname#1\endcsname
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}
\newcommand*{\if@bidi@csundef}[1]{%
  \ifcsname#1\endcsname
    \expandafter\ifx\csname#1\endcsname\relax
      \expandafter\expandafter
      \expandafter\@firstoftwo
    \else
      \expandafter\expandafter
      \expandafter\@secondoftwo
    \fi
  \else
    \expandafter\@firstoftwo
  \fi}
\newcommand{\if@bidi@def}[1]{%
  \ifdefined#1%
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}
\newcommand{\if@bidi@undef}[1]{%
  \ifdefined#1%
    \ifx#1\relax
      \expandafter\expandafter
      \expandafter\@firstoftwo
    \else
      \expandafter\expandafter
      \expandafter\@secondoftwo
    \fi
  \else
    \expandafter\@firstoftwo
  \fi}
\newcommand{\if@bidi@blank}[1]{% from url.sty
  \bidi@ifblank@i#1&&\@secondoftwo\@firstoftwo:}
\long\def\bidi@ifblank@i#1#2&#3#4#5:{#4}
\newcommand{\bidi@notblank}[1]{%
  \bidi@ifblank@i#1&&\@firstoftwo\@secondoftwo:}
\newcommand{\if@bidi@defmacro}{}
\long\edef\if@bidi@defmacro#1{%
  \noexpand\expandafter\noexpand\bidi@ifdefmacro
  \noexpand\meaning#1\detokenize{macro}:&}
\edef\bidi@ifdefmacro{%
  \def\noexpand\bidi@ifdefmacro##1\detokenize{macro}:##2&}
\bidi@ifdefmacro{\bidi@notblank{#2}}
\newcommand*{\if@bidi@csmacro}[1]{%
  \if@bidi@csdef{#1}
    {\expandafter\if@bidi@defmacro\csname#1\endcsname}
    {\@secondoftwo}}
\newcommand{\if@bidi@defprimitive}[1]{%
  \ifprimitive#1%
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}
\newcommand{\if@bidi@csprimitive}[1]{%
  \expandafter\ifprimitive\csname#1\endcsname%
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}
\newcommand*{\bidi@csdefcs}[2]{%
\expandafter\@ifdefinable  \csname#1\endcsname{%
\expandafter\def\csname#1\expandafter\endcsname{\csname#2\endcsname}}}
\newcommand*{\bidi@csletcs}[2]{%
\expandafter\@ifdefinable  \csname#1\endcsname{%
\expandafter\let\csname#1\expandafter\endcsname\csname#2\endcsname}}
\newcommand*{\bidi@cslet}[2]{%
\expandafter\@ifdefinable  \csname#1\endcsname{%
\expandafter\let\csname#1\endcsname#2}}
\newcommand{\bidi@namelongdef}[1]{%
  \long\expandafter\def\csname #1\endcsname}
\def\bidi@ensure@newcommand{\@star@or@long\bidi@ensure@new@command}
\def\bidi@ensure@new@command#1{%
  \begingroup \escapechar\m@ne\xdef\@gtempa{{\string#1}}\endgroup
  \expandafter\if@bidi@csundef\@gtempa
     {\new@command#1}{\relax%
  \let\@ifdefinable\@rc@ifdefinable%
  \new@command#1}}
\def\bidi@ensure@newlength#1{\if@bidi@undef#1{\newskip#1}{}}

\protected\def\bidi@error{\PackageError{bidi}}
\protected\def\bidi@warning{\PackageWarning{bidi}}
\protected\def\bidi@info{\PackageInfo{bidi}}

\newcommand*{\bidi@newrobustcmd}{}
\protected\def\bidi@newrobustcmd{\@star@or@long\bidi@new@command}

\def\bidi@new@command#1{\@testopt{\bidi@newcommand#1}0}

\def\bidi@newcommand#1[#2]{%
  \@ifnextchar[%]
    {\bidi@xargdef#1[#2]}
    {\ifx\l@ngrel@x\relax
       \let\l@ngrel@x\protected
     \else
       \protected\def\l@ngrel@x{\protected\long}%
     \fi
     \@argdef#1[#2]}}

\long\def\bidi@xargdef#1[#2][#3]#4{%
  \@ifdefinable#1{%
    \expandafter\protected
    \expandafter\def
    \expandafter#1%
    \expandafter{%
      \expandafter\@testopt
      \csname\string#1\endcsname{#3}}%
    \expandafter\@yargdef\csname\string#1\endcsname\tw@{#2}{#4}}}

\bidi@newrobustcmd*{\bidi@renewrobustcmd}{\@star@or@long\bidi@renew@command}

\def\bidi@renew@command#1{%
  \if@bidi@undef{#1}
     {\bidi@error{\string#1 undefined}\@ehc}
     {}%
  \let\@ifdefinable\@rc@ifdefinable
  \bidi@new@command#1}

\bidi@newrobustcmd*{\bidi@providerobustcmd}{\@star@or@long\bidi@provide@command}

\def\bidi@provide@command#1{%
  \if@bidi@undef{#1}
    {\def\reserved@a{\bidi@new@command#1}}
    {\def\reserved@a{\bidi@renew@command\reserved@a}}%
  \reserved@a}

\newcommand*{\bidi@csuse}[1]{%
  \ifcsname#1\endcsname
    \csname#1\expandafter\endcsname
  \fi}

\newcommand{\bidi@expandonce}[1]{%
  \unexpanded\expandafter{#1}}

\def\bidi@protected{%
  \let\@@protect\protect
  \let\protect\@unexpandable@protect
  \afterassignment\restore@protect}

\bidi@newrobustcmd{\bidi@appto}[2]{%
  \if@bidi@undef{#1}
    {\edef#1{\unexpanded{#2}}}
    {\edef#1{\bidi@expandonce#1\unexpanded{#2}}}}
\bidi@newrobustcmd{\bidi@eappto}[2]{%
  \if@bidi@undef{#1}
    {\edef#1{#2}}
    {\edef#1{\bidi@expandonce#1#2}}}
\bidi@newrobustcmd{\bidi@gappto}[2]{%
  \if@bidi@undef{#1}
    {\xdef#1{\unexpanded{#2}}}
    {\xdef#1{\bidi@expandonce#1\unexpanded{#2}}}}
\bidi@newrobustcmd{\bidi@xappto}[2]{%
  \if@bidi@undef{#1}
    {\xdef#1{#2}}
    {\xdef#1{\bidi@expandonce#1#2}}}

\bidi@newrobustcmd*{\bidi@protected@eappto}{\bidi@protected\bidi@eappto}
\bidi@newrobustcmd*{\bidi@protected@xappto}{\bidi@protected\bidi@xappto}

\bidi@newrobustcmd{\bidi@preto}[2]{%
  \if@bidi@undef{#1}
    {\edef#1{\unexpanded{#2}}}
    {\edef#1{\unexpanded{#2}\bidi@expandonce#1}}}
\bidi@newrobustcmd{\bidi@epreto}[2]{%
  \if@bidi@undef{#1}
    {\edef#1{#2}}
    {\edef#1{#2\bidi@expandonce#1}}}
\bidi@newrobustcmd{\bidi@gpreto}[2]{%
  \if@bidi@undef{#1}
    {\xdef#1{\unexpanded{#2}}}
    {\xdef#1{\unexpanded{#2}\bidi@expandonce#1}}}
\bidi@newrobustcmd{\bidi@xpreto}[2]{%
  \if@bidi@undef{#1}
    {\xdef#1{#2}}
    {\xdef#1{#2\bidi@expandonce#1}}}

\bidi@newrobustcmd*{\bidi@protected@epreto}{\bidi@protected\bidi@epreto}
\bidi@newrobustcmd*{\bidi@protected@xpreto}{\bidi@protected\bidi@xpreto}

\bidi@newrobustcmd*{\bidi@csappto}[1]{\expandafter\bidi@appto\csname#1\endcsname}
\bidi@newrobustcmd*{\bidi@cseappto}[1]{\expandafter\bidi@eappto\csname#1\endcsname}
\bidi@newrobustcmd*{\bidi@csgappto}[1]{\expandafter\bidi@gappto\csname#1\endcsname}
\bidi@newrobustcmd*{\bidi@csxappto}[1]{\expandafter\bidi@xappto\csname#1\endcsname}
\bidi@newrobustcmd*{\bidi@protected@cseappto}{\bidi@protected\bidi@cseappto}
\bidi@newrobustcmd*{\bidi@protected@csxappto}{\bidi@protected\bidi@csxappto}

\bidi@newrobustcmd*{\bidi@cspreto}[1]{\expandafter\bidi@preto\csname#1\endcsname}
\bidi@newrobustcmd*{\bidi@csepreto}[1]{\expandafter\bidi@epreto\csname#1\endcsname}
\bidi@newrobustcmd*{\bidi@csgpreto}[1]{\expandafter\bidi@gpreto\csname#1\endcsname}
\bidi@newrobustcmd*{\bidi@csxpreto}[1]{\expandafter\bidi@xpreto\csname#1\endcsname}
\bidi@newrobustcmd*{\bidi@protected@csepreto}{\bidi@protected\bidi@csepreto}
\bidi@newrobustcmd*{\bidi@protected@csxpreto}{\bidi@protected\bidi@csxpreto}

\bidi@newrobustcmd{\if@bidi@patchable}{%
  \bidi@dbg@trce\if@bidi@patchable
  \begingroup
  \@makeother\#%
  \@ifstar\bidi@ifpatchable@i\bidi@ifpatchable}

\long\def\bidi@ifpatchable#1#2{%
  \endgroup
  \bidi@dbg@init#1%
  \if@bidi@undef{#1}
    {\bidi@dbg@fail{def}\@secondoftwo}
    {\bidi@dbg@info{def}%
     \if@bidi@defmacro{#1}
       {\bidi@dbg@info{mac}%
        \bidi@ifscanable{#1}
          {\bidi@ifhashcheck{#2}
             {\bidi@dbg@info{tok}%
              \bidi@ifpattern#1{#2}
                 {\bidi@dbg@info{pat}%
                  \bidi@dbg@info{pos}\@firstoftwo}
                 {\bidi@dbg@fail{pat}\@secondoftwo}}
             {\bidi@dbg@fail{hsh}\@secondoftwo}}
          {\bidi@dbg@fail{tok}\@secondoftwo}}
       {\bidi@dbg@fail{mac}\@secondoftwo}}}

\long\def\bidi@ifpatchable@i#1{%
  \endgroup
  \bidi@dbg@init#1%
  \if@bidi@undef{#1}
    {\bidi@dbg@fail{def}\@secondoftwo}
    {\bidi@dbg@info{def}%
     \if@bidi@defmacro{#1}
       {\bidi@dbg@info{mac}%
        \if@bidi@defparam{#1}
          {\bidi@dbg@info{prm}%
           \bidi@ifscanable{#1}
             {\bidi@dbg@info{tok}%
              \bidi@dbg@info{pos}\@firstoftwo}
             {\bidi@dbg@fail{tok}\@secondoftwo}}
          {\bidi@dbg@info{prl}%
           \if@bidi@defprotected{#1}
             {\bidi@dbg@info{pro}}
             {}%
           \bidi@dbg@info{pos}\@firstoftwo}}
       {\bidi@dbg@fail{mac}\@secondoftwo}}}

\bidi@newrobustcmd*{\bidi@patchcmd}{%
  \bidi@dbg@trce\bidi@patchcmd
  \begingroup
  \@makeother\#%
  \bidi@@patchcmd}

\newcommand{\bidi@@patchcmd}[4][########1]{%
  \bidi@ifpatchable#2{#3}
    {\bidi@dbg@succ{ret}%
     \begingroup
     \edef\bidi@resrvda{%
       \def\noexpand\bidi@resrvda####1\detokenize{macro:}####2->####3&{%
         #1\def\string\bidi@resrvda\space####2{\noexpand\bidi@resrvdb####3&}}%
       \def\noexpand\bidi@resrvdb####1\detokenize{#3}####2&{%
         ####1\detokenize{#4}####2}%
       \edef\noexpand\bidi@resrvda{%
         \noexpand\bidi@resrvda\meaning#2&}}%
     \bidi@resrvda
     \bidi@patchcmd@scantoks\bidi@resrvda
     \let#2\bidi@resrvda
     \bidi@undef\bidi@resrvda
     \@firstoftwo}
    {\@secondoftwo}}

\def\bidi@patchcmd@scantoks#1{%
  \edef\bidi@resrvda{\endgroup
    \unexpanded{\makeatletter\scantokens}{#1}%
    \catcode\number`\@=\the\catcode`\@\relax}%
  \bidi@resrvda}

\newcommand\bidi@isloaded[2][]{
  \expandafter\ifx\csname if@bidi@#2loaded@\endcsname\relax
    \expandafter\newif\csname if@bidi@#2loaded@\endcsname
  \fi
  \@ifpackageloaded{#2}
    {\csname @bidi@#2loaded@true\endcsname #1}
    {\csname @bidi@#2loaded@false\endcsname}}

\protected\def\bidi@ifscanable#1{%
  \begingroup
  \edef\bidi@resrvda{%
    \def\noexpand\bidi@resrvda####1\detokenize{macro}:####2->####3&{%
      ####1\def\string\bidi@resrvda####2{####3}}%
    \edef\noexpand\bidi@resrvda{\noexpand\bidi@resrvda\meaning#1&}}%
  \bidi@resrvda
  \makeatletter
  \scantokens\expandafter{\bidi@resrvda}%
  \expandafter\endgroup\ifx#1\bidi@resrvda
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\protected\long\def\bidi@ifhashcheck#1{%
  \begingroup
  \edef\bidi@resrvda{\detokenize{#1}}%
  \expandafter\endgroup
  \expandafter\bidi@ifhashcheck@i\meaning\bidi@resrvda&}

\edef\bidi@ifhashcheck@i#1&{%
  \noexpand\expandafter
  \noexpand\bidi@ifhashcheck@ii
  \noexpand\strip@prefix#1\string#\string#&}

\edef\bidi@ifhashcheck@ii{%
  \def\noexpand\bidi@ifhashcheck@ii##1\string#\string###2&}
\bidi@ifhashcheck@ii{\if@bidi@blank{#2}}

\protected\long\def\bidi@ifpattern#1#2{%
  \begingroup
  \edef\bidi@resrvda{%
    \def\noexpand\bidi@resrvda####1\detokenize{#2}####2&{%
      \endgroup\noexpand\noexpand\noexpand\if@bidi@blank{####2}}%
    \edef\noexpand\bidi@resrvda{\noexpand\bidi@resrvda
      \expandafter\strip@prefix\meaning#1\detokenize{#2}&}%
    \noexpand\bidi@resrvda}
  \bidi@resrvda\@secondoftwo\@firstoftwo}

\newcommand{\if@bidi@defparam}{}
\long\edef\if@bidi@defparam#1{%
  \noexpand\expandafter\noexpand\bidi@ifdefparam
  \noexpand\meaning#1\detokenize{macro}:->&}
\edef\bidi@ifdefparam{%
  \def\noexpand\bidi@ifdefparam##1\detokenize{macro}:##2->##3&}
\bidi@ifdefparam{\bidi@notblank{#2}}

\newcommand{\if@bidi@defprotected}{}
\long\edef\if@bidi@defprotected#1{%
  \noexpand\expandafter\noexpand\bidi@ifdefprotected
  \noexpand\meaning#1\string\protected&}
\edef\bidi@ifdefprotected{%
  \def\noexpand\bidi@ifdefprotected##1\string\protected##2&}
\bidi@ifdefprotected{\bidi@notblank{#2}}

\bidi@newrobustcmd{\bidi@undef}[1]{\let#1\bidi@undefined}

\let\bidi@dbg@trce\@gobble
\let\bidi@dbg@init\@gobble
\let\bidi@dbg@info\@gobble
\let\bidi@dbg@succ\@gobble
\let\bidi@dbg@fail\@gobble

\bidi@newrobustcmd*{\bidi@apptocmd}{%
  \bidi@dbg@trce\bidi@apptocmd
  \begingroup
  \@makeother\#%
  \bidi@hooktocmd\bidi@append}

\bidi@newrobustcmd*{\bidi@pretocmd}{%
  \bidi@dbg@trce\bidi@pretocmd
  \begingroup
  \@makeother\#%
  \bidi@hooktocmd\bidi@prepend}

\long\def\bidi@hooktocmd#1#2#3{%
  \endgroup
  \bidi@dbg@init#2%
  \if@bidi@undef{#2}
    {\bidi@dbg@fail{def}\@secondoftwo}
    {\bidi@dbg@info{def}%
     \if@bidi@defmacro{#2}
       {\bidi@dbg@info{mac}%
        \if@bidi@defparam{#2}
          {\bidi@dbg@info{prm}%
           \bidi@ifscanable{#2}
             {\bidi@ifhashcheck{#3}
                {\bidi@dbg@info{tok}%
                 \bidi@dbg@succ{ret}%
                 \bidi@hooktocmd@i#1#2{#3}%
                 \@firstoftwo}
                {\bidi@dbg@fail{hsh}\@secondoftwo}}
             {\bidi@dbg@fail{tok}\@secondoftwo}}
          {\bidi@dbg@info{prl}%
           \if@bidi@defprotected{#2}
             {\bidi@dbg@info{pro}%
              \bidi@dbg@succ{red}%
              \protected}
             {\bidi@dbg@succ{red}}%
           \edef#2{#1{\bidi@expandonce#2}{\unexpanded{#3}}}%
           \@firstoftwo}}
       {\bidi@dbg@fail{mac}\@secondoftwo}}}

\long\def\bidi@hooktocmd@i#1#2#3{%
  \begingroup
  \edef\bidi@resrvda{%
    \def\noexpand\bidi@resrvda####1\detokenize{macro}:####2->####3&{%
      ####1\def\string\bidi@resrvda\space####2{#1{####3}{\detokenize{#3}}}}%
    \edef\noexpand\bidi@resrvda{%
      \noexpand\bidi@resrvda\meaning#2&}}%
  \bidi@resrvda
  \bidi@patchcmd@scantoks\bidi@resrvda
  \let#2\bidi@resrvda
  \bidi@undef\bidi@resrvda}

\long\def\bidi@append#1#2{#1#2}
\long\def\bidi@prepend#1#2{#2#1}

\bidi@newrobustcmd*{\bidi@AtEndPreamble}{\bidi@gappto\bidi@endpreamblehook}
\newcommand*{\bidi@endpreamblehook}{}

\bidi@preto\document{%
  \endgroup
  \let\bidi@AtEndPreamble\@firstofone
  \bidi@endpreamblehook
  \protected\def\bidi@AtEndPreamble{\@notprerr\@gobble}%
  \bidi@undef\bidi@endpreamblehook
  \begingroup}

\bidi@newrobustcmd*{\bidi@AfterPreamble}{\AtBeginDocument}
\bidi@AtEndPreamble{\let\bidi@AfterPreamble\@firstofone}

\bidi@newrobustcmd*{\bidi@AfterEndPreamble}{\bidi@gappto\bidi@afterendpreamblehook}
\newcommand*{\bidi@afterendpreamblehook}{}

\bidi@appto\document{%
  \let\bidi@AfterEndPreamble\@firstofone
  \bidi@afterendpreamblehook
  \protected\def\bidi@AfterEndPreamble{\@notprerr\@gobble}%
  \bidi@undef\bidi@afterendpreamblehook
  \ignorespaces}

\AtEndDocument{\let\bidi@AfterEndPreamble\@gobble}

\bidi@newrobustcmd*{\bidi@AfterEndDocument}{\bidi@gappto\bidi@afterenddocumenthook}
\newcommand*{\bidi@afterenddocumenthook}{}

\bidi@patchcmd\enddocument
  {\deadcycles}
  {\let\bidi@AfterEndDocument\@firstofone
   \bidi@afterenddocumenthook
   \deadcycles}
  {}
  {\let\bidi@@end\@@end
   \def\@@end{%
     \let\bidi@AfterEndDocument\@firstofone
     \bidi@afterenddocumenthook
     \bidi@@end}}

\bidi@newrobustcmd{\bidi@AtBeginEnvironment}[1]{%
  \bidi@csgappto{@begin@#1@hook}}

\bidi@patchcmd\begin
  {\csname #1\endcsname}
  {\bidi@csuse{@begin@#1@hook}%
   \csname #1\endcsname}
  {}
  {\bidi@warning{%
     Patching '\string\begin' failed!\MessageBreak
     '\string\bidi@AtBeginEnvironment' will not work\@gobble}}

\bidi@newrobustcmd{\bidi@AtEndEnvironment}[1]{%
  \bidi@csgappto{@end@#1@hook}}

\bidi@patchcmd\end
  {\csname end#1\endcsname}
  {\bidi@csuse{@end@#1@hook}%
   \csname end#1\endcsname}
  {}
  {\bidi@warning{%
     Patching '\string\end' failed!\MessageBreak
     '\string\bidi@AtEndEnvironment' will not work\@gobble}}

\bidi@newrobustcmd{\bidi@BeforeBeginEnvironment}[1]{%
  \bidi@csgappto{@beforebegin@#1@hook}}

\bidi@pretocmd\begin
  {\bidi@csuse{@beforebegin@#1@hook}}
  {}
  {\bidi@warning{%
     Patching '\string\begin' failed!\MessageBreak
     '\string\bidi@BeforeBeginEnvironment' will not work\@gobble}}

\bidi@newrobustcmd{\bidi@AfterEndEnvironment}[1]{%
  \bidi@csgappto{@afterend@#1@hook}}

\bidi@patchcmd\end
  {\if@ignore}
  {\bidi@csuse{@afterend@#1@hook}%
   \if@ignore}
  {}
  {\bidi@warning{%
     Patching '\string\end' failed!\MessageBreak
     '\string\bidi@AfterEndEnvironment' will not work\@gobble}}
%% 
%% Copyright © 2009–2012  Vafa Khalighi <vafaklg@gmail.com>
%% 
%% It may be distributed and/or modified under the LaTeX Project Public License,
%% version 1.3c or higher (your choice). The latest version of
%% this license is at: http://www.latex-project.org/lppl.txt
%% 
%% This work is “author-maintained” (as per LPPL maintenance status)
%% by Vafa Khalighi.
%% 
%% 
%% 
%%
%% End of file `biditools.sty'.
