%%      Commands for drawing pictures:
%%  \pic{width}{filename} ...  \endpic
%%  or \pic{width}{filename,x-shift,y-shift} ... \endpic
%%  or \leftpic... or \rightpic...

\def\nopagebreaks
 %%  A command preventing any further page breaks
 %%  Effect on the entire text up to the end of the current group
 %%  However, if the group in not ended with \par,
 %%  its last paragraph still may be split between pages
{%
  \let\normalpenalty\penalty
  \let\normalpar\par
  \interlinepenalty10000
  \predisplaypenalty10000
  \postdisplaypenalty10000
  \def\par{\normalpar\normalpenalty10000\relax}%
  \def\penalty{\ifvmode\count255 \else\normalpenalty\fi}%
}

\def\breakpar
  {\ifvmode\let\savpar\par
  \else\let\savpar\noindent{\parfillskip=0pt \parskip=0pt \par}\fi
}

\newdimen\savprevdepth

\def\mgraph#1,#2,#3,#4*{\kern-#2\vbox to 0pt{\kern-#3\special{em:graph #1}\vss}}

\def\graph#1{\mgraph#1,0pt,0pt,*}

\def\leftpic#1#2{%
  \breakpar \savprevdepth\prevdepth
  \goodbreak\nointerlineskip
  \vbox to 0pt{\hbox{\graph{#2}}\vss}
  \nobreak
  \prevdepth\savprevdepth
  \begingroup\nopagebreaks\leftskip#1\relax
  \everydisplay{\advance\displayindent#1\relax
                \advance\displaywidth-#1\relax}%
  \everyvbox{\leftskip=0pt}%
  \savpar\ignorespaces
}

\def\rightpic#1#2{%
  \breakpar \savprevdepth\prevdepth
  \goodbreak\nointerlineskip
  \dimen2=#1\relax
  \vbox to 0pt{\hbox{\kern\hsize\kern-\dimen2\relax\graph{#2}}\vss}
  \nobreak
  \prevdepth\savprevdepth
  \begingroup\nopagebreaks\rightskip\dimen2\relax
  \everydisplay{\advance\displaywidth-#1\relax}%
  \everyvbox{\rightskip=0pt}%
  \savpar\ignorespaces
}

\def\pic#1#2{\ifodd\pageno\rightpic{#1}{#2}\else\leftpic{#1}{#2}\fi}

\long\def\endpic#1{\ifx#1\par\par\else\breakpar\savpar\fi\relax\endgroup%
     \everydisplay{\displayindent=0pt\displaywidth=\hsize\relax}#1}


%  Old \ris... commands
%
\long\def\leftrismoved#1//#2//#3//#4//#5//{\leftpic{#2}{#3,#4,#5}#1\endpic\par}
\long\def\rightrismoved#1//#2//#3//#4//#5//{\rightpic{#2}{#3,#4,#5}#1\endpic\par}
\long\def\leftris#1//#2//#3//{\leftpic{#2}{#3}#1\endpic\par}
\long\def\rightris#1//#2//#3//{\rightpic{#2}{#3}#1\endpic\par}
\def\rismoved{\ifodd\pageno\rightrismoved\else\leftrismoved\fi}
\def\ris{\ifodd\pageno\rightris\else\leftris\fi}

\def\nopictures{%
  \def\pic##1##2{\ignorespaces}%
  \let\leftpic\pic
  \let\rightpic\pic
  \let\endpic\relax
}

%%
%% Макрокоманда \lines #1  для рисования клетчатых фигурок
%%     Параметр #1 - ширина фигуры в клеточках
%%
%% Заклинание вроде \cellsize=1.9 ex задает размер клеточек
%%
\newdimen\cellsize
\newdimen\lwidth
\cellsize=1.5ex
\bgroup
\catcode`\@=11

{\catcode`\|=\active \catcode`\_=\active \catcode`\ =\active
\catcode`\^^M=\active%
\gdef\linedefs{\let|\vertline\let_\horline\let \nolines\let^^M\nextrow}}

\gdef\lines#1{\count@\z@\dimen0=\cellsize\advance\dimen0 .4pt
  \def\vertline{\count@\@ne\leavevmode\rlap{\vrule height \cellsize}}
  \def\horline{\count@\z@\leavevmode
    \hbox to\cellsize{\vrule height.4pt width\dimen0\hss}}
  \def\nolines{\relax\ifodd\count@\leavevmode\kern\cellsize\count@\z@
    \else\count@\@ne\fi\relax}
  \def\nextrow{\par\count@\z@}
  \lwidth\cellsize
  \multiply\lwidth by #1
  \catcode`\|=\active \catcode`\_=\active
  \catcode`\^^M=\active \catcode`\ =\active
  \hsize\lwidth\advance\hsize.4pt\parindent\z@\offinterlineskip\linedefs}

\egroup

% Пример применения \lines
%
%\leavevmode \vbox{\lines6
% _   _   _
%| | | | | |_
%| |_| |_|_ _|
%|_ _|_|_ _  |
% _ _ _ _ _| |
%|_ _ _|_ _ _|
%
%}

