%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% arabtex.sty 
% version 3.04
% this is the driver module for the ArabTeX macro package
% 19.03.96
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (c) Klaus Lagally
%     Institut fuer Informatik
%     Universitaet Stuttgart
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ifx \undefined \@r@bt@x \else
\errhelp {ArabTex should be loaded *after* EDMAC (if applicable),^^J
but *before* any non-standard LaTeX packages or options.^^J
Correct your configuration and try again.}
\errmessage {Inconsistent configuration.^^J
The command `\string \undefined' 
has been made defined by some package.^^J
`\string \undefined' is defined as `\meaning \undefined'.^^J
This might utterly confuse ArabTeX which is therefore not loaded.^^J
}\fi

\ifx \ar@batcode \undefined \else \expandafter \endinput
\fi % load only once

\chardef \ar@batcode = \catcode`\@ \catcode`\@ = 11 \chardef \@other = 12

\def \ArabTeX {Arab\TeX }	\let \xpa \expandafter	
\let \nxp \noexpand		\def \n@xp {\nxp \nxp \nxp }

\input aconfig.sty % check environment

%%%%%%%%%%%%%%%%%%%% identify yourself %%%%%%%%%%%%%%%%%%%%%%%

\a@ident {arabtex.sty} {3.04 ArabTeX main module} {19.03.96}

\newtoks \version   		\newtoks \datum     		

%%%%%%%%%%%%%%%%%%% various declarations %%%%%%%%%%%%%%%%%%%%%

\newbox \wordb@x        \newbox \lineb@x        \newbox \tempb@x
\newtoks \a@tokens      \newtoks \sc@next       \newtoks \sc@rest
\newtoks \a@@cmd        \newtoks \a@@name	\newtoks \a@@tail
\newtoks \a@digits   	\newtoks \a@first   	\newtoks \a@paren
\newtoks \a@@everypar	\newtoks \a@last
\newskip \a@parindent   \newskip \a@parskip    	
\newwrite \a@@err       \newwrite \a@@out
\newcount \tracingarab	\def \quiet {\tracingarab = 0 }

\newif \if@vocals       \newif \ifv@c           \newif \if@sukun
\newif \if@suk          \newif \if@wasla        \newif \if@was
\newif \if@high         \newif \ifacc@hi        \newif \ifa@vmode
\newif \ifa@space	\newif \iftr@ns		\newif \ifa@oldhmode

\newif \iftest         	% \testtrue % for internal use only
\newif \iftest@scan     \newif \iftest@trans
\newif \iftest@parse    \newif \iftest@write

%%%%%%%%%%%%%%%%%%% language specific switches %%%%%%%%%%%%%%%%%%%%%

\newif \ifarab     % generate the arabic script
\newif \ifshow     % display the input text verbatim
\newif \ifligs     % set all ligatures active
\newif \ifspread   % spread characters by keshide
\newif \iftrans    % generate the transliteration

\newif \ifaN@alif  	% tanwin on alif
\newif \ifqur@n    	% old hamza rules
\newif \ifh@mza    	% hamza rules, assimilation
\newif \ifiz@fet   	% izafet rules
\newif \ifyahd@ts  	% yah with dots
\newif \ifk@fs     	% final kaf without mark

\newif \ifpasht@   	% loop chars, e as two dots, diphtongs
\newif \ifurd@     	% e as long yah, urdu accent, two hah chars
\newif \ifmagh@    	% variants of fah, vah and qaf
\newif \ift@rk		% special transliteration and coding
\newif \ifk@rd		% special transliteration and coding
\newif \ifn@arab	% load Arabic default

%%%%%%%%%%%%%%%%%%% various options %%%%%%%%%%%%%%%%%%%%%

\def \yahdots {\yahd@tstrue }		\def \yahnodots {\yahd@tsfalse }
\def \accentshigh {\acc@hitrue }	\def \accentslow {\acc@hifalse }
\def \oldtanwin {\aN@aliffalse }	\def \newtanwin {\aN@aliftrue }
\def \oldhamza {\qur@ntrue }		\def \newhamza {\qur@nfalse }

%%%%%%%%%%%%%%%%%%% various defaults %%%%%%%%%%%%%%%%%%%%%

\def \transtrue {% activate the transliteration
\iftr@ns \else \tr@zdmg \fi \let \iftrans \iftrue }

\let \tr@zdmg \relax		\n@arabtrue 	

\def \arab@output {\ifn@arab % switch to Arabic output conventions
\def@arabskel 	% define character skeletons for Arabic
\def@arabchars 	% define character macros for Arabic
\def@arabaccent % define accent macros for Arabic
\def@arabvoc 	% define vowel parsing routines for Arabic
\def@arabstrut 	% define strut for Arabic
\tr@zdmg	% define standard transliteration
\let \a@write \a@@write		\let \a@write@hook \relax
\n@arabfalse \fi }

%%%%%%%%%%%%%%%%%%% ML-TeX compatibility (br) %%%%%%%%%%%%%%%%%%%%%

\newcount \arab@charsubdefmax	\arab@charsubdefmax \m@ne

\ifx \charsubdefmax \undefined 
	\csname newcount\endcsname \charsubdefmax
\else
	\arab@charsubdefmax=\charsubdefmax
	% save actual \charsubdefmax value

	% restore it, when outputting a new box in \output routine
	\csname newtoks\endcsname \a@@output	\a@@output=\output
	\output={\charsubdefmax \arab@charsubdefmax \the\a@@output }
\fi

%%%%%%%%%%%%%%%%%%% language selection %%%%%%%%%%%%%%%%%%%%%

\def \all@false {% reset language code to default 
\k@rdfalse \t@rkfalse \magh@false \urd@false \pasht@false 
\arab@output \catcode`\<=\active }

\def \setarab   {% kaf with mark, hamza rules, yah with dots
\all@false \iz@fetfalse \h@mzatrue  \yahd@tstrue  \k@fsfalse 
\oldtanwin \setnash }

\def \setmaghribi {% variants of fah and qaf
\setarab \magh@true  }

\def \setfarsi  {% no kaf mark, yah without dots, izafet
\all@false \iz@fettrue  \h@mzatrue  \yahd@tsfalse \k@fstrue  
\newtanwin \setnast }

\def \setkurdish {% no kaf mark, yah without dots, izafet
\setfarsi \k@rdtrue }

\def \setturk   {% kaf with mark, yah without dots, izafet, hamza
\all@false \iz@fettrue  \h@mzatrue  \yahd@tsfalse \k@fsfalse 
\newtanwin \setnash \t@rktrue }

\def \seturdu   {% two hah, long yah, urdu accent, long e, izafet
\all@false \iz@fettrue  \h@mzatrue  \yahd@tsfalse \k@fstrue  
\newtanwin \setnast \urd@true  }

\def \setpashto {% pashto loop, e: two dots, izafet, hamza
\all@false \iz@fettrue  \h@mzatrue  \yahd@tstrue  \k@fstrue  
\newtanwin \setnast \pasht@true  }

\def \setverb   {% no special processing
\all@false \iz@fetfalse \h@mzafalse \yahd@tstrue  \k@fsfalse 
\oldtanwin \setnash }

\def \setnone {\catcode`\<=\@other }% deselect arabic quotations

\def \setquot {\catcode`\<=\active }% select arabic quotations

%%%%%%%%%%%%%%%%%%%%%%% vowelization options %%%%%%%%%%%%%%%%%%%%%%%%

\def \novocalize  {% no diacritics except explicitly indicated
\@vocalsfalse \v@cfalse \@sukunfalse \@sukfalse }

\def \v@calize    {% diacritics for short vowels only
\@vocalstrue \v@ctrue \@sukunfalse \@sukfalse }

\def \fullv@calize {% all diacritics
\@vocalstrue \v@ctrue \@sukuntrue \@suktrue }

\let \vocalize \v@calize    \let \fullvocalize \fullv@calize

%%%%%%%%%%%%%%%%%%%%%%%% list utilities %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newif \ifin@list    \newif \ifend@char

\long\def \test@in@list #1#2>{% #1 = token to check, #2 = list
\begingroup \def \l@@kup ##1#1##2>{% look for the token
\ifx \relax ##2\relax \global\in@listfalse
\else \global\in@listtrue \fi }%
\l@@kup #2#1>\endgroup }

\long\def \test@in@set #1#2{% #1 = symbol, #2 = token list of symbols
\xpa \test@in@list \xpa #1\the #2>}

\a@last = {aAdFrzjwUO\a@r\a@d\a@D\a@z\a@R\a@H\a@W}

\def \test@end@char #1{% does not connect to the left
\ifx #1>\end@chartrue \else \test@in@set #1\a@last
\ifin@list \end@chartrue \else \end@charfalse
\fi\fi }

%%%%%%%%%%%%%%%%%%%%%%%% font selection %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ifx \nashbf \undefined % nashbf.sty not yet loaded
\gdef \nashbf {\global \let \nashbf \nash \setnash % default
\message {nashbf.sty not loaded; bold-face ignored }\nash }
\fi

\ifx \nastaliq \undefined % nastaliq.sty not yet loaded
\gdef \nastaliq {\global \let \nastaliq \nash \setnash % default
\message {nastaliq.sty not loaded; nash is used instead }\nash }
\fi

\def \setarabfont #1{\def \set@arabfont {#1}}

\def \setnash 	  {\setarabfont \nash }
\def \setnashbf	  {\setarabfont \nashbf }
\def \setnastaliq {\setarabfont \nastaliq }
\let \setnast \setnastaliq

%%%%%%%%%%%%%%%%%%%%%%%% handling of special characters %%%%%%%%%%%%%%%

\def \arab@codes {% save and set special characters
\chardef \spc@de = \catcode`\ 	\catcode`\  = 10
\chardef \dqc@de = \catcode`\"	\catcode`\" = \@other 		
\chardef \lsc@de = \catcode`\<	\catcode`\< = \@other 
\arab@charsubdefmax \charsubdefmax \charsubdefmax \m@ne }

\def \unarab@codes {% reset catcodes
\catcode`\  = \spc@de \catcode`\< = \lsc@de \catcode`\" = \dqc@de 
\charsubdefmax \arab@charsubdefmax }

\let \@@ldtie ~            \def ~{\protect \@@ldtie}

%%%%%%%%%%%%%%%%%%%%%%% Arabic environment %%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def \arabtext {% environment for inverse formatting
\a@parindent \parindent \a@parskip \parskip 
\ifhmode \a@oldhmodetrue \else \a@oldhmodefalse \fi
\def \\{\hfil \break }%    
\let \| \break
\let \emphasize \underbar % define internal commands
\a@@everypar {}%
\a@vmodetrue 
\let \a@last@line \short@line \let \a@put@line \put@line
\def \next@command {\test@special}%
\arab@codes \set@arabfont \@waslafalse \@wasfalse
\a@parparams \the\arabtext@hook \test@token }

\def \a@par {% end an Arabic paragraph
\ifa@vmode \else \a@last@line \spreadfalse \a@vmodetrue
\parindent \a@parindent \parskip \a@parskip 
\a@resetpar \a@par@hook
\ifnum \tracingarab > 0 \message {##}\fi
\fi }

\let \a@@par \a@par % save for \a@@everypar in \a@c@autopar

\def \endarabtext {% end arabic environment
\a@last@line \ifa@oldhmode \noindent \fi
\ifnum \tracingarab > 0 \message {||}\fi
\global \@ignoretrue }

\newtoks \arabtext@hook 	\arabtext@hook {}

\def \at@arabtext #1{\arabtext@hook \xpa {\the\arabtext@hook #1}}
\let \at@arabtex \at@arabtext % catch typo

%%%%%%%%%%%%%%%%%%%%%%%%%% next token %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\xpa \def \xpa \kill@space \space {% gobble space
\futurelet \next@token \test@space }

\def \test@space {% check for space
\if \space \nxp \next@token \let \next \kill@space
\else \let \next \next@command
\fi \next }

\def \test@@token {\futurelet \next@token \test@space }

\let \test@token \test@@token	% get next token

\def \test@rest {% continue after \control sequence in text
\let \test@token \test@@token
\xpa \xpa \xpa \test@token \xpa \a@rest \space }

%%%%%%%%%%%%%%%%%%%%%%%%%% process Arabic item %%%%%%%%%%%%%%%%%%%%%%%%%

\a@digits = {0123456789}     	\a@paren  = {()[]}
\a@first  = {.^_-'`~,|"=:}	% default, scanner dependent

\long \def \test@special #1{% check for special cases
\if \bgroup \nxp \next@token 			% arabic group
	\def \next {\arab@group {#1}}\else
	\test@in@set #1\a@first			% normal case
\ifin@list \def \next {\test@word #1}\else
\if \relax \nxp #1%				% command
	\def \next {\test@command #1}\else
\ifcat \nxp ~\nxp #1%				% active character
	\def \next {\test@command #1}\else
\if \nxp <\nxp #1\unarab@codes 			% Roman insert
	\let \next \test@insert \else
\ifx $#1\unarab@codes 				% Math insert
	\let \next \test@math \else
        \def \next {\test@word #1}%		% normal case
\fi\fi\fi\fi\fi\fi \next }

\def \test@word #1 {% process next Arabic word
\ifarab \putwordb@x {\arab@item #1>}\fi
\a@spacetrue \show@input #1>\test@token }

\def \arab@item #1#2>{% split the word if necessary
\ifx \relax #2\relax \arab@write #1>\else	% single character
   \test@in@set #1\a@first
\ifin@list \arab@write #1#2>\else		% start symbol: word
   \test@in@set #1\a@digits
\ifin@list \a@split #1#2>\else			% digit: number
   \test@in@set #1\a@paren
\ifin@list \arab@item #2>\arab@write #1>\else	% parenthesis: test content
        \arab@write #1#2>%			% normal case
\fi\fi\fi\fi }

%%%%%%%%%%%%%%%%%%%%%% Arabic word %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def \arab@write #1>{% process arabic word
{\tracingmacros 0 		% use \test@XXXtrue for testing !
  \a@scan #1>%			% generate the token sequence
  \xpa \a@parse \the\a@tokens >%  split it into syllables and reverse
  \xpa \a@write \the\a@revwd >%   output the word
\ifnum \tracingarab > 2 \message {.}\fi }}

%%%%%%%%%%%%%%%%%%%%%%%%%% Arabic commands %%%%%%%%%%%%%%%%%%%%%%%%%%%%

\gdef \a@c@last@ {\\}	\gdef \a@c@put@ {\|}	\gdef \a@c@par@ {\par }

{\catcode `\^^M=\active \gdef \a@c@M {^^M}}
{\catcode `\ =\active\gdef\a@c@SP{ }}

\long \def \test@command #1{% check for special commands
\gdef \a@arg {#1}\let \next \test@token
\ifx \a@arg \a@c@par@ \a@@par \else			% \par
\ifx \a@arg \a@c@M \a@@par \else			% \obeylines
\ifx \a@arg \a@c@SP \else				% \obeyspaces
\ifx \a@arg \a@c@last@ \a@last@line \else		% \\
\ifx \a@arg \a@c@put@ \a@put@line \else			% \|
     \def \next {\a@command #1}% process the command inside "acmd.sty"
\fi\fi\fi\fi\fi \next }

%%%%%%%%%%%%%%%%%%%%%%%%%% Arabic groups %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def \arab@group #1{% process the next group
\bgroup \initlineb@x
\def \end {\global \setbox1 \hbox {\unhbox \lineb@x}%
\egroup \putwordb@x {\unhbox1 }\a@spacetrue \test@token }% last line
\test@token #1 \end }

\def \arab@sequence #1>{% put a sequence of words into a \hbox
\bgroup \transfalse \showfalse \let \end \relax 
\let \a@last@line \finish@line \let \a@put@line \break@line
\def \next@command {\test@special}\let \put@word \addt@line
\initlineb@x \test@token #1 \end \box\lineb@x \egroup }

%%%%%%%%%%%%%%%%%%%%%%%%%% arabic number %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def \a@split #1>{% split off any punctuation from the number
\def \a@body {}\split@body #1>%
\xpa \test@in@set \a@rest \a@digits
\ifin@list \arab@digits #1>\else
\xpa \arab@write \a@rest >\arab@space
\xpa \arab@digits \a@body >\fi }

\def \split@body #1#2>{% separate the body and the rest
\ifx \relax #2\relax \def \a@rest {#1}\else
\edef \a@body {\a@body #1}\split@body #2>\fi }

\def \arab@digits #1#2>{% split a digit sequence
\arab@write #1>%
\ifx \relax #2\relax \else \arab@digits #2>\fi }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def \a@split #1#2>{% next word starts with digit
\ifx \relax #2\relax \arab@write #1>\else
	\def \a@body {}\def \a@number {#1}%
	\a@splitnumber #2>%
	\xpa \arab@write \a@body >%
\fi }

{\catcode `\" =12 \gdef \f@sila {"}}

\def \a@splitnumber #1#2>{% process word starting with digit
\test@in@set #1\a@digits
\ifin@list \edef \a@number {#1\a@number }\else 
\ifx -#1\edef \a@number {--\a@number }\else 
\ifx ,#1\ifx \relax #2\relax
	\edef \a@body {\a@body \a@number #1}\def \a@number {}%
	\else \edef \a@number {\f@sila \a@number }\fi 
\else \edef \a@body {\a@body \a@number #1}\def \a@number {}%
\fi\fi\fi 
\ifx \relax #2\relax \edef \a@body {\a@body \a@number}\let \next \relax
\else \def \next {\a@splitnumber #2>}%
\fi \next }

%%%%%%%%%%%%%%%%%%%%%%%%%% insertions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def \test@math #1${% Math insertion in Arabic text
\putwordb@x {$#1$}\arab@codes \test@token }

\def \test@insert #1>{% Roman insertion in Arabic text
\putwordb@x {\rm #1}\arab@codes \test@token }

%%%%%%%%%%%%%%%%%%%%%%%%%% verbatim mode %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def \a@verb #1>{\def \verb@text {#1}% list input words verbatim
{\penalty \z@ \tt \xpa \get@meaning \meaning \verb@text }\ }

\def \get@meaning #1>{} % strip off macro header

\def \show@input #1>{% show verbatim text and transliteration
\ifshow \a@verb #1>\fi
\iftrans {\tracingmacros 0 \set@transfont \trans@text #1 \end }\fi }

%%%%%%%%%%%%%%%%%%%%%%%%%%%% arabic insertion %%%%%%%%%%%%%%%%%%%%%%%%

{\catcode `\< = \@other		% better make sure

\gdef \a@ins {% arabic insertion or 'less'
\ifmmode \global\let \a@less <
\else \global\let \a@less \arab@insert \leavevmode
  \bgroup \arab@codes \set@arabfont \@waslafalse \@wasfalse
\fi \a@less }

\gdef \<{\protect \a@ins }	% explicit command \<

\catcode`\< = \active		% prepare active <
\gdef <{\protect \a@ins }
} % catcode < 

\def \break@line {% \| inside insertion
\box\lineb@x \break \initlineb@x }

\def \finish@line {% \\ inside insertion
\box\lineb@x \hfil \break \initlineb@x }

\def \arab@insert #1>{% Arabic insertion in Roman text
\ifshow \a@verb #1>\fi % verbating listing
\ifarab \arab@sequence #1> \fi % arabic writing
\iftrans \set@transfont \trans@text #1 \end \/\fi
\ifnum \tracingarab > \@ne \message {>}\fi
\unskip \egroup }

%%%%%%%%%%%%%%%%%%%%%%%%% transliteration %%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def \set@transfont {\rm \it }% font for transliteration

\long \def \trans@text #1{% check for special cases
\leavevmode
   \def \next {\trans@text }%
\ifx \end #1\let \next \relax \else
\ifx \par #1\endgraf \else
\ifx \\#1\hfil \break \else
\ifx \|#1\hfil \break \else
\if \relax \nxp #1\relax % ignore control sequences
\else \def \next {\trans@elem #1}% normal case
\fi\fi\fi\fi\fi \next }

\def \trans@elem #1 {% transliterate next word of text
\trans@word #1>\trans@text }

\def \trans@word #1>{% transliterate a word
{\tracingmacros 0 \a@scan #1>\xpa \a@trans \the\a@tokens >\ }}

\ifx \a@trans \undefined % atrans.sty not yet loaded
\def \a@trans #1>{\gdef \a@trans ##1>{\relax }% default 
\message {`atrans.sty' not loaded; transliteration ignored.}}
\fi

%%%%%%%%%%%%%%%%%%%%%%%%% initialization %%%%%%%%%%%%%%%%%%%%%%%%

\def \startarab {% set default initializations
\ifx \dospecials \undefined \else
  \toks0 = \expandafter {\dospecials }%
%  \edef \dospecials {\the\toks0 \noexpand \do <}
\fi
\transfalse   \arabtrue   \vocalize   \ligstrue   \setnash
\ligsfalse
\tracingarab = 3 \setnone } % \catcode < := 12 for user macros

\def \oldarabtex {% patch for catching the old input notation
\errhelp {If you continue now, places to change will be marked.}%
\errmessage {Obsolete coding; please update your input file.}%
\gdef \sc@c {@} \setarab }

%%%%%%%%%%%%%%%%%%%%%%% load the later passes %%%%%%%%%%%%%%%%%%%%%%

\UsePackage {acmd}		% load the command processor module
\UsePackage {afonts}		% load the default fonts
\UsePackage {ascan}		% generate a token sequence
\UsePackage {aparse}		% parse it into syllables and reverse them
\UsePackage {awrite}		% generate the basic writing
\UsePackage {aligs}		% modify for any ligatures
\UsePackage {aoutput}		% generate output lines
\UsePackage {abidir}		% bidirectional linebreaking
%\UsePackage {atrans}		% generate the transliteration
\UsePackage {alatex} 		% extensions for LaTeX
\UsePackage {abjad}		% abjad numerals
\ifx \edmacloaded \relax
  \UsePackage {aedpatch} \fi	% patches for EDMAC ?
\UsePackage {apatch}		% any last minute corrections

%%%%%%%%%%%%%%%%%%%%%%% statistics %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ifx \AtEndDocument \undefined \else \xpa \AtEndDocument \fi
{{\newlinechar `$ 
\wlog {$Parameterless commands valid in arabic mode:}
\wlog {\space (\the\a@@listzero )}
\wlog {$Commands with 1 parameter valid in arabic mode:}
\wlog {\space (\the\a@@listone )}
\wlog {$Commands with 2 parameters valid in arabic mode:}
\wlog {\space (\the\a@@listtwo )}
\wlog {$User defined commands valid in arabic mode:}
\wlog {\space (\the\a@@listreg )$}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%% here we go %%%%%%%%%%%%%%%%%%%%%%%

\startarab	\a@report	\typeout {... now be patient ...}

\catcode`\@ = \ar@batcode		   

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\endinput
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% EOF %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
