%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% abidir.sty
% experimental macros for bidirectional linebreaking
% 14.07.94
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (c) Klaus Lagally
%     Institut fuer Informatik
%     Universitaet Stuttgart
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ifx \bidiratcode \undefined \else \xpa \endinput
\fi

\chardef \bidiratcode = \catcode`\@            \catcode`\@ = 11

\version = {3.02}		\datum = {14.07.94}
\message {\the\version }	\message {(\the\datum )}

\edef \arabtexconfig {\arabtexconfig 
abidir.sty \the\version \space (\the\datum) }

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

% <LR insertion inside Rtext>
\def \test@insert #1>{\a@Linsert {#1}}

% \LR{#1} = LR insertion inside Rtext
\def \a@c@LR {\unarab@codes \a@Linsert }
\let \a@c@L \a@c@LR	% usage: \L {LR text}

% \LR{#1} = LR insertion inside Ltext: trivial
\let \LR \relax 	\let \L \relax 

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

% \RL{#1} = RL insertion inside Ltext
\def \RL {\protect \a@RL }
\let \R \RL		% usage: \R {RL text}

% \RL{#1} = RL insertion inside Rtext: trivial
\let \a@c@RL \a@c@relax 	\let \a@c@R \a@c@relax 

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

\def \goodpar {% 	usage: \goodpar {Roman paragraphs}
% put around one or more paragraphs if the linebreaking is bad
% caution: may lead to storage overflow
\dimen0 \prevdepth \setbox0 \vbox \bgroup \prevdepth \dimen0
\def \par {\egroup \endgraf \goodpar x}% dummy character
\aftergroup \g@@dpar \let \next=}

\def \g@@dpar {\unvbox0 }

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

\newdimen \a@Llength	\newdimen \a@Lwidth	\newdimen \a@Lindent	
\newdimen \a@Rlength	\newdimen \a@Rheight	\newdimen \a@splitht
\newbox \insertb@x

\def \arab@space {\space }% no infinite stretch !!

\def \ins@skip {\hskip \z@ plus 0.1em }% before and after insertion

\def \a@vglue {\z@ plus 2ex }% glue at top of the insertion \vbox

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

\def \a@RL {% RL insertion inside Ltext
\bgroup \arab@codes \set@arabfont \@waslafalse \@wasfalse \a@@Rinsert }

\def \a@@Rinsert #1{% RL insertion inside Ltext
\ifshow \a@verb #1>\fi % 	verbating listing
\ifarab \a@Rinsert {#1} \fi % 	arabic writing 
\iftrans \set@transfont \trans@text #1 \end \/\fi
\ifnum \tracingarab > \@ne \message {>}\fi
\unskip \egroup }

\def \a@Rinsert #1{% put a sequence of RL words into Ltext
\leavevmode % go to hmode if not there already
\ifinner \arab@sequence #1>% inside a \hbox: append
\else \a@Rsplit {#1}% splice into the current paragraph
\fi }

\def \a@Rsplit #1{% splice RL words into the current paragraph
\bgroup \def \next@command {\test@special}\transfalse \showfalse 
\a@splitdimen % 	get the dimensions of \arab@strut
\a@Rdimen #1 \@@ % 	get the length of the first RL word
\ifdim \a@Rlength = \z@ \arab@sequence #1>\else
\a@Ldimen % 		get the dimensions of the last line
\a@Rtobox {#1}% 	pack the insertion lines into a \vbox
\a@Runpack % 		unpack and splice into the current paragraph
\fi \egroup }

\def \a@Rdimen #1 #2\@@ {% get the dimensions of the first word
\ifx \relax #2\relax \a@Rlength \z@ \else
\ifx \relax #1\relax \a@Rdimen #2\@@ \else
\setbox0 \hbox {\ins@skip \arab@strut \arab@sequence #1>}\a@Rlength \wd0 
\a@Rheight \ht0 \advance \a@Rheight \dp0 
\edef \a@Rstrut {\vrule height\ht0 depth\dp0 width\z@ }%
\fi\fi }

\def \a@splitdimen {%
\a@splitht 1.4\baselineskip }

\def \a@Rtobox #1{% pack the insertion into \insertb@x
\setbox \insertb@x \vbox 
{\hsize \a@Lwidth \parshape \@ne \a@Lindent \hsize
\let \end \relax \spreadtrue \vskip \a@vglue \hbadness 10000
\putlineb@x {\arab@strut \hskip \a@Llength }\a@spacefalse 
\test@token #1 \end \unskip \unskip \vskip \a@vglue \box\lineb@x }}

\def \a@Runpack {\ins@skip % unpack all lines from the insertion
\loop \a@getline \unhbox \tempb@x \unskip \break
\ifvbox \insertb@x \repeat \unpenalty \penalty 5000 \ins@skip }

\def \a@getline {% unpack the next line globally to \box \tempb@x
\splittopskip \a@vglue \setbox \tempb@x \vsplit \insertb@x to \a@splitht 
\setbox0 \vbox {\unvbox \tempb@x \global \setbox \tempb@x \lastbox }}

\def \a@Ldimen {% get the dimensions of the preceding line
\leavevmode \hbox to \a@Rlength {\a@Rstrut \hfil }% dummy box
\lineskiplimit \lineskip
$$\global \a@Llength \predisplaysize % 		dummy display
\global \a@Lwidth \displaywidth \global \a@Lindent \displayindent 
\hbox to \a@Lwidth {}\postdisplaypenalty 10000
$$ \endgraf
\ifdim \a@Llength = \maxdimen \a@Llength \hsize %	flexible line
\else \advance \a@Llength -2em	% 			fixed line
\fi
\advance \a@Llength -\a@Lindent \advance \a@Llength -\a@Rlength 
\a@skipback }

\def \a@skipback {% get back to the old position
\ifinner 
\unskip \unskip \unpenalty \setbox0 \lastbox % get rid of garbage
\unskip \unskip \unpenalty \setbox0 \lastbox % last line
\dimen0 \baselineskip \advance \dimen0 -\ht0
\advance \dimen0 -\lastskip \prevdepth \dimen0
\unskip \vskip -\parskip 
\noindent \unhbox0 % open and trim last line 
\unskip \unskip \unpenalty \setbox0 \lastbox 
\else % compensate for garbage
\vskip -\belowdisplayskip \vskip -\baselineskip 
\vskip -\abovedisplayskip \vskip -\baselineskip \vskip -\parskip 
\noindent \arab@strut \hskip \a@Llength 
\fi }

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

\def \a@Linsert #1{% splice Ltext into RL paragraph
%\a@spacetrue \putwordb@x {\ins@skip }\a@Rlength \wd \lineb@x 
\a@spacetrue \putwordb@x {\ins@skip }\dimen0 \wd \lineb@x 
\setbox \insertb@x \vbox
%{\rm \parskip \z@ \hangindent -\a@Rlength \hangafter -\@ne 
{\rm \parskip \z@ \hangindent -\dimen0 \hangafter -\@ne 
\rightskip \z@ plus .001fil \vskip \a@vglue \noindent #1\par }%
%\a@splitht 2ex \a@Lunpack 
\a@splitht 3.5ex \a@Lunpack 
\putwordb@x {\ins@skip }%
\a@spacetrue 
\arab@codes \test@token }

\def \a@Lunpack {% unpack all lines from the insertion
\loop \a@getline \a@spacefalse 
\putwordb@x {\unhbox \tempb@x \unskip \unskip }%
\ifvbox \insertb@x \repeat }

%\tracingoutput=1
\showboxdepth=3
\showboxbreadth=\maxdimen

\catcode`\@ = \bidiratcode
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\endinput


