%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                                %%
%%  Wrap 1.01 (April 22, 2025)                    %%
%%                                                %%
%%  Usage example:                                %%
%%                                                %%
%%    % \wrapmargin = 6mm                         %%
%%    \Wrap\right 55mm (3-13) % or \left          %%
%%      \epsfxsize 55mm \epsfbox{picture.eps}     %%
%%    \endWrap                                    %%
%%                                                %%
%%  (c) Alexander E. Gutman <gutman@math.nsc.ru>  %%
%%                                                %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\expandafter\edef\csname restore at\endcsname{\catcode`\noexpand\@\the\catcode`\@\relax}
\catcode`\@=11\relax

\newdimen\wrapmargin \wrapmargin = 6mm

\newtoks\wrap@shapetoks
\newcount\wrap@till
\newdimen\wrap@indent
\newbox\wrap@box

\def\wrap@leftshape#1#2{%
  \dimen0=\hsize\advance\dimen0 by-\wrap@indent%
  \count255=#2\relax
  \advance\count255 by1\relax
  \expandafter\wrap@shapetoks
    \expandafter{\expandafter\parshape\expandafter=\the\count255 \space}%
  \count255=1\relax
  \loop
    \ifnum\count255<#1\relax
      \expandafter\wrap@shapetoks\expandafter{\the\wrap@shapetoks 0pt\hsize}%
      \advance\count255 by1\relax
  \repeat
  \loop
    \expandafter\wrap@shapetoks\expandafter{\the\wrap@shapetoks\wrap@indent\dimen0 }%
    \ifnum\count255<#2
      \advance\count255 by1\relax
  \repeat
  \expandafter\wrap@shapetoks\expandafter{\the\wrap@shapetoks 0pt\hsize}%
  \the\wrap@shapetoks\relax}
\def\wrap@rightshape#1#2{%
  \dimen0=\hsize\advance\dimen0 by-\wrap@indent%
  \count255=#2\relax
  \advance\count255 by1\relax
  \expandafter\wrap@shapetoks
    \expandafter{\expandafter\parshape\expandafter=\the\count255 \space}%
  \count255=1\relax
  \loop
    \ifnum\count255<#1\relax
      \expandafter\wrap@shapetoks\expandafter{\the\wrap@shapetoks 0pt\hsize}%
      \advance\count255 by1\relax
  \repeat
  \loop
    \expandafter\wrap@shapetoks\expandafter{\the\wrap@shapetoks 0pt\dimen0 }%
    \ifnum\count255<#2\relax
      \advance\count255 by1\relax
  \repeat
  \expandafter\wrap@shapetoks\expandafter{\the\wrap@shapetoks 0pt\hsize}%
  \the\wrap@shapetoks\relax}
\long\def\wrap@left#1#2{%
  \ifvmode\leavevmode\fi
  \vadjust{\hbox to\hsize{\vbox to0pt{\copy\wrap@box\vss}\hss}}%
  \global\wrap@indent=\wd\wrap@box\global\advance\wrap@indent by \wrapmargin
  \global\wrap@till=#2\relax
  \global\let\wrap@par\par
  \global\def\par{\wrap@par
    \parshape=0\relax
    \global\hangindent=\wrap@indent
    \ifnum \prevgraf < \wrap@till
      \global\advance\wrap@till by-\prevgraf
      \global\prevgraf=0\relax
      \global\hangafter = -\wrap@till
    \else
      \global\hangindent=0pt\relax
      \global\hangafter =0\relax
      \global\let\par\wrap@par
    \fi}%
  \wrap@leftshape{#1}{#2}}
\long\def\wrap@right#1#2{%
  \ifvmode\leavevmode\fi
  \vadjust{\hbox to\hsize{\hss\vbox to0pt{\copy\wrap@box\vss}}}%
  \global\wrap@indent=\wd\wrap@box\global\advance\wrap@indent by \wrapmargin
  \global\wrap@till=#2\relax
  \global\let\wrap@par\par
  \global\def\par{\wrap@par
    \parshape=0\relax
    \global\hangindent=-\wrap@indent
    \ifnum \prevgraf < \wrap@till
      \global\advance\wrap@till by-\prevgraf
      \global\prevgraf=0\relax
      \global\hangafter = -\wrap@till
    \else
      \global\hangindent=0pt\relax
      \global\hangafter =0\relax
      \global\let\par\wrap@par
    \fi}%
  \wrap@rightshape{#1}{#2}}
\long\def\Wrap#1#2(#3-#4)#5\endWrap{%
  \setbox\wrap@box\vbox{\hsize#2\relax#5}%
  \ifx#1\left
    \wrap@left{#3}{#4}%
  \else
    \wrap@right{#3}{#4}%
  \fi
  \DN@{\ifx\next\par\DN@\par{}\else\DN@{\noindent}\fi\next@}\FN@\next@}

\csname restore at\endcsname
