% This is minimal level Hebrew support Version 1.0
% Jacques J. Goldberg, Technion, 1.14.88; modified by R. I. Greenberg

\immediate\write16{Using Hebrew---Make sure font info available for .dvi to .imp conversion}

%\tracingmacros=1
%\tracingonline=1
%\tolerance=10000

\catcode`\@=11 % as in \makeatletter of LaTeX

\font\tenheb = Jerusalem \hyphenchar\tenheb=-1
\def\ivrit{\tenheb} % LaTeX with lhebfonts.tex (input by livritex.tex)
		    %   will do more interesting things.

\def\hwidth{\hsize} % livritex will make \hwidth be \textwidth to allow better
		    %   interface with LaTeX.

% 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\savedpar=\par
\let\savedspace=\ %
\let\savedtilde=~

\newbox\ivrbox  % The Hebrew box to test full line condition
\newbox\ivrstrbox  % The box to hold the current Hebrew string
\newif\ifivrscrboxfull  % Verify if scratch box is full
\newif\ifpendingspace
\newcount\ivrdepth
\ivrdepth=0

\def\ivrbegin{\global\advance\ivrdepth by1 %
\ifnum\ivrdepth>1% Don't do anything if already in ivr mode
\else%
\begingroup%
\def\par{\ifhbox\ivrstrbox\noindent\hbox to\hwidth{\hfill\unhbox\ivrstrbox}\fi%
% OUTPUT Hebrew text
\pendingspacefalse%
\savedpar}%
\def\ {\ivrsp}%
\def~{ }%
\ivrit%
\pendingspacefalse%
\fi}

\def\ivrend{\global\advance\ivrdepth by-1 %
\ifnum\ivrdepth>0% Don't do anything if still in ivr mode
\else%
\ifhbox\ivrstrbox\unhbox\ivrstrbox\fi% OUTPUT Hebrew text
\endgroup%
\fi}

\def\ivrcheckscrbox#1{%
\setbox\ivrbox=\hbox{#1\unhcopy\ivrstrbox}\ivrscrboxfullfalse%
\ifdim \hwidth < \wd\ivrbox \ivrscrboxfulltrue \fi}

\def\ipwcore#1#2{\ivrcheckscrbox{#1#2}%
\ifivrscrboxfull%
\ifhbox\ivrstrbox%
\noindent\hbox to\hwidth{\unhcopy\ivrstrbox}\break% OUTPUT Hebrew text
% Beware can't use \line with LaTeX.
\global\setbox\ivrstrbox=\hbox{#1}%
\else%
\immediate\write16{Ivritex Warning: Overfull Hebrew box.}
\noindent\hbox to\hwidth{\unhcopy\ivrbox}\break% OUTPUT Hebrew text
% Beware can't use \line with LaTeX.
\fi
\pendingspacefalse%
\else%
\global\setbox\ivrstrbox=\hbox{#1#2\unhcopy\ivrstrbox}\fi}

\def\ipw#1{\ifpendingspace\ipwcore{#1}{ }\pendingspacefalse%
\else\ipwcore{#1}{}\fi}

\def\ivrsp{\pendingspacetrue}

\def\ivrmath#1{%
{\let\ =\savedspace\let~=\savedtilde\ipw{#1}}%
\ivrsp}

\catcode`\@=12 % As in \makeatother of LaTeX
