% This is the hebrew version of cw.tex.
% It is not clear why the hskips on line 52 and later are not performed\
% when ifheb is true. See the enactment of cwheb.tex.
% To be corrected.

% file: cwh.tex -- crossword puzzle macros
% Tad White    [tadpole@ucrmath.ucr.edu]
% Revised: May 22, 1992
%
% Notes:
% 0. For examples, see accompanying file, cw-sample-hebrew.tex.
% 1. TeX will distinguish upper/lower case letters.
% 2. Use . for a black box, and use , for an empty space.
% 3. TeX ignores all spacing while reading the puzzle array.
% 4. The command \automaticnumbering causes the boxes to be
%    numbered automatically, and keeps track of which words
%    are "across" and "down."  The clue numbers can then
%    be generated automatically using \across, \down, and \clue.
%    The box-numbering algorithm assumes you have a "real"
%    crossword puzzle; that is, every letter is part of both
%    a horizontal and a vertical word.  The algorithm should
%    succeed in this case.  To put a number where there would
%    not ordinarily be one, put # before the letter.  To
%    suppress a number in a spot where there would ordinarily
%    be one, put / before the letter.  If, for some reason, an
%    unwanted number appears in the clue list, it can be popped
%    off the list (see the sample puzzle).
%    \noautomaticnumbering (default) turns off \automaticnumbering.
% 5. To leave a box open with an unspecified letter, put "-" in that
%    position; for example, while a puzzle is being created.
% 6. Also: use \lettertrue or \letterfalse to control the
%    printing of the letters in the puzzle.  Use \numbertrue
%    or \numberfalse to control the printing of numbers in the puzzle.

\input heb_macros

\font\small=cmr6 \font\lf=cmss10
\newbox\letterbox\newbox\solidbox
\newdimen\boxsize\newdimen\auxsize
\newcount\wordcount\newcount\abovecount
\newcount\leftcount\newcount\currcount
\newcount\thecount\newcount\increment
\newif\ifletter\newif\ifnumber\newif\ifinhibit

\boxsize=16pt\auxsize=\boxsize\advance\auxsize by-0.4truept%
\letterfalse\numbertrue\inhibitfalse%
\def\init-cw{\wordcount=0\abovecount=0\leftcount=0\currcount=0\increment=
1%
\def\acrosslist{}\def\downlist{}\def\emptylist{}}
\init-cw

\setbox\letterbox=\vbox{\hbox{%
\vrule height\boxsize width0.4truept \kern-0.4truept%
\ifheb\hskip\boxsize\fi\vrule height\boxsize width\boxsize depth-\auxsize \kern-\boxsize%
\vrule width\boxsize height0.4truept \kern-0.4truept%
\ifheb\hskip-\boxsize\fi\vrule height\boxsize width0.4truept%
}}

\setbox\solidbox\vbox{\hbox{%
\ifheb\hskip\boxsize\fi%
\vrule height\boxsize width\boxsize%
\ifheb\hskip-\boxsize\fi%
}}
\advance\auxsize by-5pt

\def\dolist{\afterassignment\dodolist\let\next= }
\def\dodolist{\ifx\next\endlist\let\next\relax%
\else\parse\let\next\dolist\fi\next}
\def\endlist{\endlist}

\def\automaticnumbering{%
\def\drawbox##1{\leavevmode%
\ifcase##1\copy\solidbox\leftcount=0%
\or{\thecount=\leftcount\multiply\thecount by\abovecount%
\ifodd\thecount\else\drawnum%
{\ifodd\leftcount\else%
\xdef\acrosslist{\acrosslist\the\wordcount:}\fi%
\ifodd\abovecount\else%
\xdef\downlist{\downlist\the\wordcount:}\fi}\fi}%
\advance\currcount by\increment \leftcount=1 \copy\letterbox%
\or\kern\boxsize\leftcount=0\fi%
\divide\abovecount by2 \multiply\increment by2}%
\def\xpop##1:##2\endlist{\gdef\nextii{##1}\gdef\testlist{##2}}%
\def\pop{\ifx\testlist\emptylist\gdef\nextii{?}%
\else\expandafter\xpop\testlist\endlist\fi}%
\def\across{\noindent{\bf ACROSS}\global\parindent20pt%
\smallskip\global\let\testlist\acrosslist}%
\def\down{\noindent{\bf DOWN}\global\parindent20pt%
\smallskip\global\let\testlist\downlist}%
\def\clue{\par\item{\pop{\bf\nextii}\ }}}

\def\noautomaticnumbering{%
\def\drawbox##1{\leavevmode\ifcase##1\copy\solidbox\or\copy\letterbox%
\or\kern\boxsize\fi}}
\noautomaticnumbering

\def\drawnum{\ifinhibit\global\inhibitfalse\else{%
\global\advance\wordcount by1\ifnumber\leavevmode%
\raise\auxsize\rlap{\smash{\hskip1pt\L{\small\the\wordcount}}}\fi}\fi}
%\raise\auxsize\rlap{\smash{\hskip1pt\small\the\wordcount}}\fi}\fi}

\long\def\parse{%
\if\next.\drawbox0%
\else\if\next,\drawbox2%
\else\if\next-\drawbox1%
\else\ifcat\next a\drawbox1%
\ifletter\kern-\boxsize\raise2pt\hbox to\boxsize{\hfil\lf\next\hfil}\fi%
\else\if\next##\drawnum%
\else\if\next/\inhibittrue%
\else\ifx\next\par\par%
\abovecount=\currcount \currcount=0 \leftcount=0 \increment=1%
\fi\fi\fi\fi\fi\fi\fi}

%this really ought to be local in \beginCW...
\parindent=0pt\parskip=0pt\obeylines\raggedright

\long\def\beginCW#1\endCW{\begingroup%
\init-cw\inhibitfalse\offinterlineskip\dolist#1\endlist\endgroup}
