%\tracingmacros=1
%\tracingonline=1
%\tolerance=10000
\font\ivrit=redis10
\hyphenchar\ivrit=-1

% The two lines below are taken from D.Knuth and P.MacKay
%     TuGBoat vol.8,#1,page 14.
\def\hebrew#1{{\ivrit \reflect#1\empty\tcelfer }}
\def\reflect#1#2\tcelfer{\ifx#1\empty\else\reflect#2\tcelfer#1\fi}

\let\ivrswch=|        % This is the Hebrew-Latin toggle
\def\ivrbar{\vrule height7pt width0.5pt}  % This makes the vertical bar printabl

\newbox\ivrbox  % The Hebrew box to test full line condition
\newbox\ivrstrbox  % The box to hold the current Hebrew string
\setbox\ivrbox=\hbox{}
\setbox\ivrstrbox=\hbox{}

\newif\ifivrmd         % Mode flag defaults to false
\def\ivrswitch{
       \ifivrmd \message{Latin }  % Toggle Hebrew on / Hebrew off
           \hbox to\hsize{\hfil \unhbox\ivrstrbox \unskip} \par   \ivrmdfalse
       \else \message{Hebrew } \unskip \par \setbox\ivrstrbox=\hbox{}
             \ivrmdtrue \parindent=0pt
       \fi }

\def\ivrconcat#1#2{#1 #2}

\newif\ifivrscrboxfull  % Verify if scratch box is full
\def\ivrcheckscrbox{
  \setbox\ivrbox=\hbox{\ivrconcat{\blabla}{\unhcopy\ivrstrbox}}
                 \ivrscrboxfullfalse
                 \ifdim \hsize < \wd\ivrbox \ivrscrboxfulltrue \fi}

\newif\ifivrmorinp     % Stuff waiting for processing?

\def\ivrmorinp{\ivrmorinptrue\ifeof\ivrfile\ivrmorinpfalse\fi}

\def\ivrplugword{\read\ivrfile to\blabla  % Process a token from input file
    \if\ivrswch\blabla \ivrswitch    % Watch for a V-bar
      \else
        \ifivrmd \ivrcheckscrbox
% \immediate\write16{\blabla}
         \ifivrscrboxfull \hbox to\hsize{\unhcopy\ivrstrbox \unskip}
          \setbox\ivrstrbox=\hbox{\blabla}
      \else \setbox\ivrstrbox=\hbox{\ivrconcat{\blabla}{\unhcopy\ivrstrbox}} \fi
         \else \blabla \fi % Hebrew mode
        \fi} % Switcher

\newread\ivrfile
\openin\ivrfile=hebtext.tex

\loop \ivrmorinp \ifivrmorinp \ivrplugword \repeat

\ifivrmd \ivrstr \fi

\closein\ivrfile
\bye
