\newif\ifoutput
\newtoks\outputpretest
 
\edef\oldoutput{\the\output} % copy old output routine
\output={\the\outputpretest \ifoutput \oldoutput \fi}
 
\outputpretest={\outputtrue}% default "outputpretest" routine
 
 
% Define an alternate "outputpretest" routine --------------------------------
 
\newbox\pagebox
\newdimen\pageht
 
\newif\iftryingfig    \tryingfigfalse
\newif\ifdoingfig     \doingfigfalse
\newif\ifpageafterfig \pageafterfigfalse
 
\def\dofigtest{%
     \ifnum\outputpenalty=-10001
          \setbox\pagebox=\vbox{\unvbox255}%
          \global\pageht=\ht\pagebox
          \outputfalse
          \unvbox\pagebox
     \else
          \outputtrue
          \ifdoingfig
               \pageafterfigtrue
          \fi
     \fi
}
 
 
% Define the "tryfig" macro for testing figure placement ---------------------
 
\newdimen\startpageht
\newdimen\htdone \htdone=0pt
 
\edef\oldeverypar{\the\everypar}
\everypar={\tryfig \oldeverypar}
 
\def\tryfig{% "\global" is used in case user does any grouping
     \iftryingfig
          {\everypar={\relax}\setbox0=\lastbox
               \parindent=\wd0 \parskip=0pt \par
               \penalty-10001 \leavevmode}%
          \dimen0=\vsize \advance\dimen0 by -\pageht
          \advance\dimen0 by -2\baselineskip
          \ifdim\dimen0>\ht\figbox
               \dimen0=0.3\baselineskip \vrule depth \dimen0 width 0pt
               \vadjust{\kern -\dimen0 \vtop to \dimen0{\baselineskip=\dimen0
                         \vss \vbox to 1ex{\hbox to \hsize{%
                                   \hss \copy\figbox}\vss}\null}}%
               \global\tryingfigfalse
               \global\doingfigtrue
               \global\startpageht=\pageht
               \global\htdone=0pt
               \dohang
          \fi
     \else
          \ifdoingfig
               {\everypar={\relax}\setbox0=\lastbox
                    \parindent=\wd0 \parskip=0pt \par
                    \penalty-10001 \leavevmode}%
               \global\htdone=\pageht
               \global\advance\htdone by -\startpageht
               \ifpageafterfig
                    \global\doingfigfalse
               \else
                    \ifdim\htdone<\ht\figbox
                         \dohang
                    \else
                         \global\doingfigfalse
                    \fi
               \fi
          \else
               \global\outputpretest={\outputtrue}%
          \fi
     \fi
}
 
 
% Define macro to activate the figure insertion macros -----------------------
 
\def\rightinsert{%
     \outputpretest={\dofigtest}
     \tryingfigtrue \doingfigfalse \pageafterfigfalse
}
 
 
% Define macro to calculate and set hanging indent values --------------------
 
\newcount\hangcount
\newdimen\figgutter % gap between text and figure
 
\def\dohang{%
     \dimen0=\ht\figbox \advance\dimen0 by -\htdone
     \advance\dimen0 by 1.49\baselineskip \hangcount=\dimen0
     \divide\hangcount by \baselineskip
     \dimen0=\wd\figbox \advance\dimen0 by \figgutter
     \global\hangafter=-\hangcount \global\hangindent=-\dimen0
}