%
%	pad.sty
%		written by S.Kuramochi  1994,1995,1996
%		e-mail: satoshi@ueda.info.waseda.ac.jp
%
\typeout{pad.sty written by S.Kuramochi v1.1 <Jul 8,1996>.}

\newdimen\padboxsep	% the amount of space inserted between 
			% the frame of a PAD and its contents
\newdimen\padboxrule	% the width of lines in a PAD
\newdimen\padcolsep	% the minimum width between columns
\padboxsep=3pt
\padboxrule=.4pt
\padcolsep=1ex

\newdimen\@padhta	% the height of the part stuck out to above
\newdimen\@padhtb	% the height of the body of a PAD
\newdimen\@padhtc	% the height of the part stuck out to below

\newcount\@tempcntc
\newcount\@tempcntd
\newdimen\@tempdimc
\newdimen\@tempdimd
\newbox\@tempboxb
\newbox\@tempboxc


%
% \padboxss, \padsubss	to use \shortstack in \padbox, \padsub
%			These are here for convenience.
%
\def\padboxss{\@ifstar{\@ixpadboxss}{\@ipadboxss}}
\def\@ipadboxss#1{\padbox{\shortstack[l]{#1}}}
\def\@ixpadboxss#1{\padbox*{\shortstack[l]{#1}}}

\def\padsubss{\@ifstar{\@ixpadsubss}{\@ipadsubss}}
\def\@ipadsubss#1{\padsub{\shortstack[l]{#1}}}
\def\@ixpadsubss#1{\padsub*{\shortstack[l]{#1}}}


%
% \padbox	draw a frame for procedure
%
% #1	text
%
\def\padbox{\@ifstar{\@ixpadbox}{\@ipadbox}}

\def\@ipadbox#1{%
  \leavevmode
  \setbox\@tempboxa=\hbox{#1}%
  \@tempdima=2\padboxrule
  \advance\@tempdima by \padboxsep
  \advance\@tempdima by \dp\@tempboxa
  \@padbox
}

\def\@ixpadbox#1{%
  \leavevmode
  \setbox\@tempboxa=\hbox{#1}%
  \@tempdima=2\padboxrule
  \advance\@tempdima by \padboxsep
  \advance\@tempdima by \dp\@tempboxa
  \lower\@tempdima\hbox{\@padbox}%
}

\def\@padbox{%
  \@tempdimb=\@tempdima
  \advance\@tempdimb by \ht\@tempboxa
  \advance\@tempdimb by \padboxsep
  \advance\@tempdimb by 2\padboxrule
  \vbox{%
    \hrule\@height2\padboxrule
    \hbox{%
      \vrule\@width2\padboxrule
      \hskip\padboxsep
      \vbox{%
        \vskip\padboxsep
        \box\@tempboxa
        \vskip\padboxsep
      }%
      \hskip\padboxsep
      \vrule\@width2\padboxrule
    }%
    \hrule\@height2\padboxrule
  }%
  \global\@padhta=0pt%
  \global\@padhtb=\@tempdimb
  \global\@padhtc=0pt%
}


%
% \padsub	draw a frame for subroutine
%
% #1	text
%
\def\padsub{\@ifstar{\@ixpadsub}{\@ipadsub}}

\def\@ipadsub#1{%
  \leavevmode
  \setbox\@tempboxa=\hbox{#1}%
  \@tempdima=2\padboxrule
  \advance\@tempdima by \padboxsep
  \advance\@tempdima by \dp\@tempboxa
  \@padsub
}

\def\@ixpadsub#1{%
  \leavevmode
  \setbox\@tempboxa=\hbox{#1}%
  \@tempdima=2\padboxrule
  \advance\@tempdima by \padboxsep
  \advance\@tempdima by \dp\@tempboxa
  \lower\@tempdima\hbox{\@padsub}%
}

\def\@padsub{%
  \@tempdimb=\@tempdima%
  \advance\@tempdimb by \ht\@tempboxa
  \advance\@tempdimb by \padboxsep
  \advance\@tempdimb by 2\padboxrule
  \vbox{%
    \hrule\@height2\padboxrule
    \hbox{%
      \vrule\@width2\padboxrule
      \hskip\padboxsep
      \vrule\@width\padboxrule
      \hskip\padboxsep
      \vbox{%
        \vskip\padboxsep
        \box\@tempboxa
        \vskip\padboxsep
      }%
      \hskip\padboxsep
      \vrule\@width\padboxrule
      \hskip\padboxsep
      \vrule\@width2\padboxrule
    }%
    \hrule\@height2\padboxrule
  }%
  \global\@padhta=0pt%
  \global\@padhtb=\@tempdimb
  \global\@padhtc=0pt%
}


\newcount\@padseqlv	% nesting level of \padseq
\@padseqlv=0

%
% \padseq	draw a sequence of procedures
%
% #1	procedures
%
\def\padseq{\@ifstar{\@ixpadseq}{\@ipadseq}}

\def\@ipadseq#1{\@padseq{#1}{0}}
\def\@ixpadseq#1{\@padseq{#1}{1}}

\def\@padseq#1#2{%
  \leavevmode
  \advance\@padseqlv by 1\relax
  % the flag indicates now processing the first procedure in the \padseq
  \expandafter\ifx \csname @padseqf\the\@padseqlv\endcsname \relax
    \expandafter\newcount\csname @padseqf\the\@padseqlv\endcsname
    \expandafter\newdimen\csname @padseqhta\the\@padseqlv\endcsname
  \fi
  \csname @padseqf\the\@padseqlv\endcsname=1%
  \csname @padseqhta\the\@padseqlv\endcsname=0pt%
  \setbox\@tempboxa=\vbox{%
    \bgroup
      \baselineskip=-1pt%
      \lineskip=0pt%
      \let\\\@padseqcr
      \halign{##\unskip\hss\cr #1\crcr}%
    \egroup
  }%
  \@tempdima=\ht\@tempboxa
  \advance\@tempdima by \dp\@tempboxa
  \@tempdimb=.5\@tempdima
  \advance\@tempdimb by -.5ex%	% adjust baseline
  \ifnum #2=1\relax \lower\@tempdimb \fi
  \vbox{%
    \box\@tempboxa
    \hrule \@height 0pt%	% ?
    \ifnum \csname @padseqf\the\@padseqlv\endcsname=0\relax
      \vskip-\@padhtc \vskip-\@padhtb \vskip-\@padhta
      \hbox{\vrule \@width\padboxrule \@height\@padhta}%
      \vskip\@padhtb \vskip\@padhtc
    \fi
  }%
  \ifnum \csname @padseqf\the\@padseqlv\endcsname=0\relax
    \global\@padhta=\csname @padseqhta\the\@padseqlv\endcsname
  \else
    \advance\csname @padseqhta\the\@padseqlv\endcsname by \@padhta
    \global\@padhta=\csname @padseqhta\the\@padseqlv\endcsname
  \fi
  \global\@padhtb=\@tempdima
  \global\advance\@padhtb by -\@padhta
  \global\advance\@padhtb by -\@padhtc
  \advance\@padseqlv by -1\relax
}

\def\@padseqcr{\@ifstar{\@ixpadseqcr}{\@ixpadseqcr}}
\def\@ixpadseqcr{\@ifnextchar[{\@ipadseqcr}{\@ipadseqcr[0pt]}}

\def\@ipadseqcr[#1]{%
    \cr
    \ifcase \csname @padseqf\the\@padseqlv\endcsname	% not first
      \noalign{\vskip-\@padhtc \vskip-\@padhtb \vskip-\@padhta}%
      \vrule \@width\padboxrule \@height\@padhta
      \cr
      \noalign{\vskip\@padhtb}%
      \@tempdima=\@padhtc
      \advance\@tempdima by \padcolsep
      \advance\@tempdima by #1\relax
      \vrule \@width\padboxrule \@height\@tempdima
      \cr
      \ifdim #1<0pt \noalign{\vskip#1\relax} \cr \fi
    \or	% first
      \noalign{\vskip-\@padhtc}%
      \@tempdima=\@padhtc
      \advance\@tempdima by \padcolsep
      \advance\@tempdima by #1\relax
      \vrule \@width\padboxrule \@height\@tempdima
      \global\advance\csname @padseqhta\the\@padseqlv\endcsname by \@padhta
      \global\csname @padseqf\the\@padseqlv\endcsname=0%
      \cr
      \ifdim #1<0pt \noalign{\vskip#1\relax} \cr \fi
    \or % first and comment
      \@tempdima=\padcolsep
      \advance\@tempdima by #1\relax
      \@tempdimb=\@tempdima
      \advance\@tempdimb by \@padhta
      \advance\@tempdimb by \@padhtb
      \advance\@tempdimb by \@padhtc
      \global\advance\csname @padseqhta\the\@padseqlv\endcsname by \@tempdimb
      \vrule \@width 0pt \@height\@tempdima
      \global\csname @padseqf\the\@padseqlv\endcsname=1%
      \cr
      \ifdim #1<0pt \noalign{\vskip#1\relax} \cr \fi
    \fi
    \ignorespaces
}


%
% \padcond	draw a frame for selection
%
% #1	condition
% #2	procedure to be executed if condition is true
% #3	procedure to be executed if condition is false
%
\def\padcond{\@ifstar{\@ixpadcond}{\@ipadcond}}
\def\@ipadcond#1#2#3{\@padcond{#1}{#2}{#3}{0}}
\def\@ixpadcond#1#2#3{\@padcond{#1}{#2}{#3}{1}}

\def\@padcond#1#2#3#4{%
  \leavevmode
  \@tempcnta=\ifx\relax#2\relax 0 \else 1 \fi
  \@tempcntb=\ifx\relax#3\relax 0 \else 1 \fi
  \ifnum\@tempcnta=\@tempcntb
    \ifnum\@tempcnta=0%		% has no proc
      \@padcondhead{0}{0}{0pt}{#4}{#1}%
    \else			% has both
      \setbox\@tempboxb=\hbox{#2}%
      \@tempdima=.5\@padhtb	% \@tempdima:\@padhta
      \@tempdimb=\@tempdima	% \@tempdimb:\@padhtb
      \advance\@tempdima by \@padhta
      \advance\@tempdimb by \@padhtc
      \setbox\@tempboxc\hbox{#3}%
      \advance\@tempdimb by \padcolsep
      \advance\@tempdimb by \@padhta
      \@tempdimc=.5\@padhtb	% \@tempdimc:\@padhtc
      \advance\@tempdimb by \@tempdimc
      \advance\@tempdimc by \@padhtc
%
      \setbox\@tempboxa
        =\hbox{\@padcondhead{1}{1}{\@tempdimb}{#4}{#1}}%
      \@tempdimd=\@padhtb	% \@tempdimd:space adjustment
      \advance\@tempdimd by -\@tempdimb
      \global\@padhtb=\@padhtb
%
      \@tempdimb=\dp\@tempboxa	% \@tempdimb:lower
      \advance\@tempdimb by \@tempdimc
      \ifnum #4=1\relax \lower\@tempdimb \fi
      \hbox{%
        \vbox{%
          \vskip\@tempdima
          \box\@tempboxa
          \vskip\@tempdimc
        }%
        \vbox{%
          \box\@tempboxb
          \vskip\padcolsep
          \vskip\@tempdimd
          \box\@tempboxc
          \vskip 0pt%
        }%
      }%
      \global\@padhta=\@tempdima
      \global\@padhtc=\@tempdimc
    \fi
  \else				% has one
    \setbox\@tempboxa
      =\hbox{\@padcondhead{\@tempcnta}{\@tempcntb}{0pt}{#4}{#1}}%
    \@tempdimc=\@padhtb%	% \@tempdimc:\@padcondhead's height
    \setbox\@tempboxb=\hbox \ifnum\@tempcnta=1\relax {#2}\else {#3}\fi
    \ifnum \@tempcnta=1\relax
      \@tempdima=\@padhta
      \@tempdimb=\@padhtc
    \else
      \@tempdima=\@padhtc
      \@tempdimb=\@padhta
    \fi
    \advance\@tempdima by .5\@padhtb
    \advance\@tempdimb by .5\@padhtb
    \advance\@tempdimb by -\@tempdimc
    \@tempdimd=\dp\@tempboxa	% \@tempdimd:lower
    \ifnum \@tempcnta=1\relax
      \ifdim \@tempdimb>0pt \advance\@tempdimd by \@tempdimb \fi
    \else
      \advance\@tempdimd by \@tempdima
    \fi
    \ifnum #4=1\relax \lower\@tempdimd \fi
    \hbox{%
      \vbox{%
        \ifnum \@tempcnta=1\relax
          \vskip\@tempdima
          \box\@tempboxa
          \vskip \ifdim \@tempdimb>0pt \@tempdimb \else 0pt \fi
        \else
          \vskip \ifdim \@tempdimb>0pt \@tempdimb \else 0pt \fi
          \box\@tempboxa
          \vskip\@tempdima
        \fi
      }%
      \vbox{%
        \ifnum \@tempcnta=1\relax
          \box\@tempboxb
          \vskip \ifdim \@tempdimb<0pt -\@tempdimb \else 0pt \fi
        \else
          \vskip \ifdim \@tempdimb<0pt -\@tempdimb \else 0pt \fi
          \box\@tempboxb
          \vskip 0pt%
        \fi
      }%
    }%
    \global\@padhtb=\@tempdimc
    \ifnum \@tempcnta=1\relax
      \global\@padhta=\@tempdima
      \global\@padhtc=\ifdim \@tempdimb>0pt \@tempdimb \else 0pt \fi
    \else
      \global\@padhta=\ifdim \@tempdimb>0pt \@tempdimb \else 0pt \fi
      \global\@padhtc=\@tempdima
    \fi
  \fi
}


\newcount\@padx		% slope
\newcount\@pady

%
% \padcond's head
%
% #1	has true procedure
% #2	has false procedure
% #3	height
% #4	whether to adjust baseline
% #5	condition
%
\long\def\@padcondhead#1#2#3#4#5{%
  \leavevmode%
  \@padx=1 \@pady=6\relax
  \@tempdimd=#3%		% \@tempdimd:#3
  \setbox\@tempboxa=\hbox{#5}%
  \@tempdima=2\padboxrule	% \@tempdima:lower
  \advance\@tempdima by \padboxsep
  \advance\@tempdima by \dp\@tempboxa
%
  \@tempdimb=\ht\@tempboxa	% \@tempdimb:whole height
  \advance\@tempdimb by \dp\@tempboxa
  \advance\@tempdimb by 4\padboxrule
  \advance\@tempdimb by 2\padboxsep
  \ifdim \@tempdimb<\@tempdimd \@tempdimb=\@tempdimd \fi
  \ifdim \@tempdimb<23pt%	% 23pt:minimum height
    \@tempdimc=23pt%		% \@tempdimc:interpolation height
    \@tempdimb=23pt%
  \else
    \@tempdimc=\@tempdimb
  \fi
  \advance\@tempdimc by -\ht\@tempboxa
  \advance\@tempdimc by -\dp\@tempboxa
  \advance\@tempdimc by -4\padboxrule
  \advance\@tempdimc by -2\padboxsep
  \global\@padhta=0pt%
  \global\@padhtb=\@tempdimb
  \global\@padhtc=0pt%
  \advance\@tempdima by .5\@tempdimc
  \ifnum #4=1\relax \lower\@tempdima \fi
  \hbox{%
    \vbox{%
      \hrule\@height2\padboxrule
      \hbox{%
        \vrule\@width2\padboxrule
        \hskip\padboxsep
        \vbox{%
          \@tempdimc=.5\@tempdimc
          \advance\@tempdimc by \padboxsep
          \vskip\@tempdimc
          \box\@tempboxa
          \vskip\@tempdimc
        }%
        \hskip\padboxsep
        \unitlength=1sp%
        \thicklines
        \advance\@tempdimb by -\@wholewidth
        \@tempdimb=.5\@tempdimb
        \@tempcnta=\@tempdimb		% \@tempcnta:y
        \multiply\@tempdimb by \@padx
        \divide\@tempdimb by \@pady
        \@tempcntb=\@tempdimb		% \@tempcntb:x
        \begin{picture}(\@tempcntb,0)(0,0)%
          \put(0,\@tempcnta){\line(\@padx,-\@pady){\@tempcntb}}%
          \advance\@tempcnta by -\@halfwidth
          \put(0,\@tempcnta){\line(\@padx,\@pady){\@tempcntb}}%
        \end{picture}%
      }%
      \hrule\@height2\padboxrule
    }%
    \vbox{%
      \ifnum 1=#1\relax \hrule \@height\padboxrule \@width 1em%
      \else \vskip\padboxrule \fi
      \vskip\@tempdimb \vskip-2\padboxrule
      \ifnum 1=#2\relax \hrule \@height\padboxrule \@width 1em%
      \else \vskip\padboxrule \fi
    }%
  }%
}


%
% \padwhile	draw a frame for pre-conditional loop
%
% #1	condition
% #2	procedure
%
\def\padwhile{\@ifstar{\@ixpadwhile}{\@ipadwhile}}
\def\@ipadwhile#1#2{%
  \ifx\relax#2\relax \@padwhilehead{#1}{0}%
  \else \@padloop{\@padwhilehead{#1}{0}}{#2}{0} \fi}
\def\@ixpadwhile#1#2{%
  \ifx\relax#2\relax \@padwhilehead{#1}{1}%
  \else \@padloop{\@padwhilehead{#1}{1}}{#2}{1} \fi}


%
% \padrepeat	draw a frame for post-conditional loop
%
% #1	condition
% #2	procedure
%
\def\padrepeat{\@ifstar{\@ixpadrepeat}{\@ipadrepeat}}
\def\@ipadrepeat#1#2{%
  \ifx\relax#2\relax \@padrepeathead{#1}{0}%
  \else \@padloop{\@padrepeathead{#1}{0}}{#2}{0} \fi}
\def\@ixpadrepeat#1#2{%
  \ifx\relax#2\relax \@padrepeathead{#1}{1}%
  \else \@padloop{\@padrepeathead{#1}{1}}{#2}{1} \fi}


%
% \padwhile's head
%
\def\@padwhilehead#1#2{%
  \leavevmode
  \setbox\@tempboxa\hbox{#1}%
  \@tempdima=\dp\@tempboxa
  \advance\@tempdima by 2\padboxrule
  \advance\@tempdima by \padboxsep
  \ifnum #2=1\relax \lower\@tempdima \fi
  \vbox{%
    \hrule\@height2\padboxrule
    \hbox{%
      \vrule\@width2\padboxrule
      \hskip\padboxsep%
      \vrule\@width\padboxrule
      \hskip\padboxsep
      \vbox{%
        \vskip\padboxsep
        \box\@tempboxa
        \vskip\padboxsep
      }%
      \hskip\padboxsep
      \hskip-\padboxrule
      \vrule\@width2\padboxrule
    }%
    \hrule\@height2\padboxrule
  }%
}


%
% \padrepeat's head
%
\def\@padrepeathead#1#2{%
  \leavevmode
  \setbox\@tempboxa=\hbox{#1}%
  \@tempdima=\dp\@tempboxa
  \advance\@tempdima by 2\padboxrule
  \advance\@tempdima by \padboxsep
  \ifnum #2=1\relax \lower\@tempdima \fi
  \vbox{%
    \hrule\@height2\padboxrule
    \hbox{%
      \vrule\@width2\padboxrule
      \hskip\padboxsep
      \vbox{%
        \vskip\padboxsep
        \box\@tempboxa
        \vskip\padboxsep
      }%
      \hskip\padboxsep
      \vrule\@width\padboxrule
      \hskip\padboxsep
      \hskip-\padboxrule
      \vrule\@width2\padboxrule
    }%
    \hrule\@height2\padboxrule
  }%
}


%
% for \padwhile, \padrepeat
%
\def\@padloop#1#2#3{%
  \leavevmode
  \setbox\@tempboxa\hbox{#1}%
  \setbox\@tempboxb\hbox{#2}%
  \@tempdima=.5\@padhtb%	% \@tempdima:\@padhta
  \@tempdimb=\ht\@tempboxa%	% \@tempdimb:\@padhtb
  \advance\@tempdimb by \dp\@tempboxa
  \advance\@tempdima by -.5\@tempdimb
  \@tempdimc=\@tempdima%	% \@tempdimc:\@padhtc
  \advance\@tempdima by \@padhta
  \advance\@tempdimc by \@padhtc
%
  \@tempdimd=\dp\@tempboxa
  \advance\@tempdimd by \@tempdimc
  \ifnum #3=1\relax \lower\@tempdimd \fi
  \hbox{%
    \vbox{%
      \vskip \ifdim \@tempdima>0pt \@tempdima \else 0pt \fi
      \box\@tempboxa
      \vskip \ifdim \@tempdimc>0pt \@tempdimc \else 0pt \fi
    }%
    \vbox{%
      \hrule \@height\padboxrule \@width 1em%
      \vskip \ifdim \@tempdimc<0pt -\@tempdimc \else \@tempdimc \fi
      \vskip .5 \ifdim \@tempdimc<0pt \@padhtb \else \@tempdimb \fi
    }%
    \vbox{%
      \vskip \ifdim \@tempdima<0pt -\@tempdima \else 0pt \fi
      \box\@tempboxb
      \vskip \ifdim \@tempdimc<0pt -\@tempdimc \else 0pt \fi
    }%
  }%
  \global\@padhta=\ifdim \@tempdima>0pt \@tempdima \else 0pt \fi
  \global\@padhtb=\@tempdimb
  \global\@padhtc=\ifdim \@tempdimc>0pt \@tempdimc \else 0pt \fi
}


%
% \padmessage	write a message (use in \padseq)
%
% #1	message
%
\def\padmessage#1{%
  \leavevmode
  \setbox\@tempboxa=\hbox{#1}%
  \global\@padhtb=\ht\@tempboxa
  \global\advance\@padhtb by \dp\@tempboxa
  \hbox{%
    \ifnum \@padseqlv>0\relax
      \ifnum \csname @padseqf\the\@padseqlv\endcsname=0\relax
        \vrule\@width\padboxrule
        \hskip\padboxsep
      \else
        \global\csname @padseqf\the\@padseqlv\endcsname=2\relax
      \fi
    \fi
    \box\@tempboxa
  }%
  \global\@padhta=0pt%
  \global\@padhtc=0pt%
}


%
% \padcomment	write a comment
%
% #1	comment
%
\def\padcomment{\@ifstar{\@ixpadcomment}{\@ipadcomment}}
\def\@ipadcomment#1{\@padcomment{#1}{0}}
\def\@ixpadcomment#1{\@padcomment{#1}{1}}

\def\@padcomment#1#2{%
  \leavevmode
  \setbox\@tempboxa=\hbox{#1}%
  \@tempdima=\ht\@tempboxa
  \advance\@tempdima by \dp\@tempboxa
%  \@tempdimc=.5\@padhtb		% lower
%  \advance\@tempdimc by -.5ex%
  \@tempdimb=\@padhtb
  \advance\@tempdimb by -\@tempdima
%  \@tempdimc=.5\@tempdimb
%  \advance\@tempdimc by \@padhtc%
%  \ifnum #2=1\relax \lower\@tempdimc \fi
  \hbox{%
    \hskip\padboxsep
    \vbox{%
      \vskip .5\@tempdimb
      \box\@tempboxa
      \vskip .5\@tempdimb
      \vskip\@padhtc
    }%
  }%
%  \ifdim \@tempdimb<0pt%
%    \global\advance\@padhta by -.5\@tempdimb
%    \global\advance\@padhtc by -.5\@tempdimb
%  \fi
}


\newcount\@padcasecount	% nesting level of \padcase
\@padcasecount=0
\newcount\@padcasecnt	% number of cases
\@padcasecnt=0
\newcount\@padi		% loop counter
\newcount\i

%
% \padcase	draw a frame for selection
%
% #1	common part of the condition
% #2	case label ':' procedure ',' ...
%
\def\padcase{\@ifstar{\@ixpadcase}{\@ipadcase}}

\def\@ipadcase#1#2{\@padcase{#1}{#2}{0}}
\def\@ixpadcase#1#2{\@padcase{#1}{#2}{1}}

\def\@padcase#1#2#3{%
  \leavevmode
  \advance\@padcasecount by 1\relax
  \expandafter\ifx \csname @padcasecase\the\@padcasecount\endcsname \relax
    \expandafter\newbox\csname @padcasecase\the\@padcasecount\endcsname
    \expandafter\newbox\csname @padcaseproc\the\@padcasecount\endcsname
    \expandafter\newdimen\csname @padcasehta\the\@padcasecount\endcsname
    \expandafter\newdimen\csname @padcasehtb\the\@padcasecount\endcsname
    \expandafter\newdimen\csname @padcasehtc\the\@padcasecount\endcsname
    \expandafter\newdimen\csname @padwd\the\@padcasecount\endcsname
  \fi						% max width of case labels
  \csname @padcasehtb\the\@padcasecount\endcsname=0pt%
  \csname @padwd\the\@padcasecount\endcsname=0pt%
  \newcount\@@padcasecnt
  \@@padcasecnt=\@padcasecnt
  \@padcasecnt=0%
  \@padfor\@padcasevar:=#2 \do{%
    \global\advance\@padcasecnt by 1\relax
    \@padcasepara\@padcasevar\@padcasepend
    \expandafter\ifx
      \csname @padcasecase\the\@padcasecount @\the\@padcasecnt\endcsname
      \relax
      \global\expandafter\newbox
        \csname @padcasecase\the\@padcasecount @\the\@padcasecnt\endcsname
      \global\expandafter\newbox
        \csname @padcaseproc\the\@padcasecount @\the\@padcasecnt\endcsname
      \global\expandafter\newdimen
        \csname @padcaseht\the\@padcasecount @\the\@padcasecnt\endcsname
    \fi
    \expandafter\setbox
      \csname @padcaseproc\the\@padcasecount @\the\@padcasecnt\endcsname
      =\hbox{\box\csname @padcaseproc\the\@padcasecount\endcsname}%
    \expandafter\setbox
      \csname @padcasecase\the\@padcasecount @\the\@padcasecnt\endcsname
      =\hbox{\box\csname @padcasecase\the\@padcasecount\endcsname}%
    \ifnum \@padcasecnt=1\relax
      \csname @padcasehta\the\@padcasecount\endcsname=.5\@padhtb
      \csname @padcaseht\the\@padcasecount @\the\@padcasecnt\endcsname
        =.5\@padhtb
      \advance\csname @padcasehta\the\@padcasecount\endcsname by \@padhta
      \advance\csname @padcaseht\the\@padcasecount @\the\@padcasecnt\endcsname
        by \@padhtc
    \else
      \advance\@padcasecnt by -1\relax
      \advance\csname @padcaseht\the\@padcasecount @\the\@padcasecnt\endcsname
        by \@padhta
      \advance\csname @padcaseht\the\@padcasecount @\the\@padcasecnt\endcsname
        by .5\@padhtb
      \advance\csname @padcaseht\the\@padcasecount @\the\@padcasecnt\endcsname
        by \padcolsep
      \advance\csname @padcaseht\the\@padcasecount @\the\@padcasecnt\endcsname
        by 2\padboxrule		% 2?
      \advance\csname @padcasehtb\the\@padcasecount\endcsname
       by \csname @padcaseht\the\@padcasecount @\the\@padcasecnt\endcsname
      \advance\@padcasecnt by 1\relax
      \csname @padcaseht\the\@padcasecount @\the\@padcasecnt\endcsname
        =.5\@padhtb
      \advance\csname @padcaseht\the\@padcasecount @\the\@padcasecnt\endcsname
        by \@padhtc
    \fi
  }%
  \@tempcnta=\@padcasecnt
  \advance\@tempcnta by -1%
  \@padi=1\relax
  \@whilenum \@padi<\@padcasecnt
  \do{%
    % height adjustment (too short to draw lines)
    \ifdim 21pt>%
      \csname @padcaseht\the\@padcasecount @\the\@padi\endcsname
      \expandafter\ifx
        \csname @padcaseext\the\@padcasecount @\the\@padi\endcsname \relax
        \expandafter\newdimen
          \csname @padcaseext\the\@padcasecount @\the\@padi\endcsname
      \fi
      \@tempdima=21pt%
      \advance\@tempdima
        by -\csname @padcaseht\the\@padcasecount @\the\@padi\endcsname
      \csname @padcaseext\the\@padcasecount @\the\@padi\endcsname=\@tempdima
      \csname @padcaseht\the\@padcasecount @\the\@padi\endcsname=21pt%
      \advance\csname @padcasehtb\the\@padcasecount\endcsname by \@tempdima
      \@padx=3 \@pady=5\relax
    \else
      \@tempdima
        =\csname @padcaseht\the\@padcasecount @\the\@padi\endcsname
      % adjusting the slope of lines
      \ifdim 42pt>\@tempdima          \@padx=3 \@pady=5\relax
      \else \ifdim 50.4pt>\@tempdima  \@padx=1 \@pady=2\relax
      \else \ifdim 63pt>\@tempdima    \@padx=2 \@pady=5\relax
      \else \ifdim 75.6pt>\@tempdima  \@padx=1 \@pady=3\relax
      \else \ifdim 100.8pt>\@tempdima \@padx=1 \@pady=4\relax
      \else \ifdim 126pt>\@tempdima   \@padx=1 \@pady=5\relax
      \else                           \@padx=1 \@pady=6\relax
      \fi\fi\fi\fi\fi\fi
    \fi
    \@tempdima
      =\csname @padcaseht\the\@padcasecount @\the\@padi\endcsname
    % height adjustment (case label)
    \setbox\@tempboxa=\hbox{\expandafter\copy
      \csname @padcasecase\the\@padcasecount @\the\@padi\endcsname}%
    \ifnum \@padi=1%
      \@tempdimb=\ht\@tempboxa
      \advance\@tempdimb by \dp\@tempboxa
      \advance\@tempdimb by \padboxsep
      \advance\@tempdimb by 2\padboxrule
    \else
      \@tempdimb=.5\ht\@tempboxa
      \advance\@tempdimb by .5\dp\@tempboxa
    \fi
    \advance\@padi by 1%
    \setbox\@tempboxa=\hbox{\expandafter\copy
      \csname @padcasecase\the\@padcasecount @\the\@padi\endcsname}%
    \advance\@padi by -1%
    \advance\@tempdimb by 1.5\padcolsep	% 1.5?
    \ifnum \@padi=\@tempcnta
      \advance\@tempdimb by \ht\@tempboxa
      \advance\@tempdimb by \dp\@tempboxa
      \advance\@tempdimb by \padboxsep
      \advance\@tempdimb by 2\padboxrule
    \else
      \advance\@tempdimb by .5\ht\@tempboxa
      \advance\@tempdimb by .5\dp\@tempboxa
    \fi
    \ifdim \@tempdimb>\@tempdima
      \expandafter\ifx
        \csname @padcaseext\the\@padcasecount @\the\@padi\endcsname \relax
        \expandafter\newdimen
          \csname @padcaseext\the\@padcasecount @\the\@padi\endcsname
        \csname @padcaseext\the\@padcasecount @\the\@padi\endcsname=0pt%
      \fi
      \@tempdimc=\@tempdimb
      \advance\@tempdimc by -\@tempdima
      \advance\csname @padcaseext\the\@padcasecount @\the\@padi\endcsname
        by \@tempdimc
      \csname @padcaseht\the\@padcasecount @\the\@padi\endcsname=\@tempdimb
      \advance\csname @padcasehtb\the\@padcasecount\endcsname by \@tempdimc
      \@tempdima=\@tempdimb
    \fi
    % the width of slanted line
    \advance\@tempdima by \@wholewidth
    \@tempdima=.5\@tempdima
    \multiply\@tempdima by \@padx
    \divide\@tempdima by \@pady
    \ifdim \@tempdima>\csname @padwd\the\@padcasecount\endcsname
      \global\csname @padwd\the\@padcasecount\endcsname=\@tempdima
    \fi
    \advance\@padi by 1\relax
  }%
  \csname @padcasehtc\the\@padcasecount\endcsname
    =\csname @padcaseht\the\@padcasecount @\the\@padcasecnt\endcsname
  % head
  \ifnum #3=1\relax
    \setbox\@tempboxa=\hbox{#1}%
    \@tempdima=-\ht\@tempboxa
    \ifdim \@tempdima=0pt \@tempdima=-1ex \fi	% adjust
    \advance\@tempdima by \dp\@tempboxa
    \advance\@tempdima by \csname @padcasehtb\the\@padcasecount\endcsname
    \@tempdima=.5\@tempdima
    \advance\@tempdima by \csname @padcasehtc\the\@padcasecount\endcsname
    \lower\@tempdima
  \fi
  \hbox{%
    \vbox{%
      \vskip\csname @padcasehta\the\@padcasecount\endcsname
      \hrule\@height2\padboxrule
      \hbox{%
        \vrule\@width2\padboxrule
        \hskip\padboxsep
        \vbox{%
          \vskip\padboxsep
          \hbox{%
            % variable
            \vbox{%
              \ifnum #3=0\relax \setbox\@tempboxa=\hbox{#1} \fi
              \@tempdima=\csname @padcasehtb\the\@padcasecount\endcsname
              \@tempdima=.5\@tempdima
              \advance\@tempdima by -.5\ht\@tempboxa
              \advance\@tempdima by -.5\dp\@tempboxa
              \advance\@tempdima by -\padboxsep
              \advance\@tempdima by -2\padboxrule
              \vskip\@tempdima
              \box\@tempboxa
              \vskip\@tempdima
            }%
            % cases
            \baselineskip=-1pt%
            \lineskip=0pt%
            \@tempcnta=\@padcasecnt
            \global\advance\@padcasecnt by 1%
            \vbox{%
              \i=1\relax
              \@whilenum \i<\@padcasecnt
              \do{%
                \ifnum \i=1\relax
                  \setbox\@tempboxa=\hbox{\expandafter\box
                    \csname @padcasecase\the\@padcasecount @\the\i\endcsname}%
                  \@tempdima
                    =\csname @padcaseht\the\@padcasecount @\the\i\endcsname
                  \advance\@tempdima by -\ht\@tempboxa
                  \advance\@tempdima by -\dp\@tempboxa
                  \advance\@tempdima by -2\padboxrule
                  \advance\@tempdima by -\padboxsep
                \else
                  \box\@tempboxa
                  \setbox\@tempboxa=\hbox{\expandafter\box
                    \csname @padcasecase\the\@padcasecount @\the\i\endcsname}%
                  \advance\@tempdima by -.5\ht\@tempboxa
                  \advance\@tempdima by -.5\dp\@tempboxa
                  \vskip\@tempdima
                  \ifnum \i=\@tempcnta
                    \@tempdima=0pt%
                  \else
                    \@tempdima
                      =\csname @padcaseht\the\@padcasecount @\the\i\endcsname
                  \fi
                  \advance\@tempdima by -.5\ht\@tempboxa
                  \advance\@tempdima by -.5\dp\@tempboxa
                \fi
                \advance\i by 1\relax
              }%
%             \advance\@tempdima by -.5\ht\@tempboxa
%             \advance\@tempdima by -.5\dp\@tempboxa
              \advance\@tempdima by -\padboxsep
              \advance\@tempdima by -2\padboxrule
              \vskip\@tempdima
              \box\@tempboxa
              \vskip 0pt%
            }%
          }%
          \vskip\padboxsep
        }%
        \hskip.5\padboxsep
        \hskip\csname @padwd\the\@padcasecount\endcsname
      }%
      \hrule\@height2\padboxrule
      \vskip\csname @padcasehtc\the\@padcasecount\endcsname
    }%
    % slanted lines & horizontal lines
    \vbox{%
      \vskip\csname @padcasehta\the\@padcasecount\endcsname
      \i=1%
      \@tempcnta=\@padcasecnt
      \advance\@tempcnta by -2%
      \@whilenum \i<\@padcasecnt
      \do{%
        \hrule \@height\padboxrule \@width 1em%
        \ifnum \i>\@tempcnta
        \else
          \hbox{%
            \@tempdima=\csname @padcaseht\the\@padcasecount @\the\i\endcsname
            \ifdim 42pt>\@tempdima          \@padx=3 \@pady=5\relax
            \else \ifdim 50.4pt>\@tempdima  \@padx=1 \@pady=2\relax
            \else \ifdim 63pt>\@tempdima    \@padx=2 \@pady=5\relax
            \else \ifdim 75.6pt>\@tempdima  \@padx=1 \@pady=3\relax
            \else \ifdim 100.8pt>\@tempdima \@padx=1 \@pady=4\relax
            \else \ifdim 126pt>\@tempdima   \@padx=1 \@pady=5\relax
            \else                           \@padx=1 \@pady=6\relax
            \fi\fi\fi\fi\fi\fi
            \unitlength=1sp%
            \thicklines
%           \advance\@tempdima by \@wholewidth
            \ifnum\i=\@tempcnta \advance\@tempdima by -\@wholewidth \fi
            \@tempcntd=\@tempdima
            \@tempdima=.5\@tempdima
            \@tempcnta=\@tempdima
            \multiply\@tempdima by \@padx
            \divide\@tempdima by \@pady
            \@tempcntb=\@tempdima%		% width
            \begin{picture}(0,0)(\@tempcntb,\@tempcntd)%
              \put(0,\@tempcnta){\line(\@padx,-\@pady){\@tempcntb}}%
%             \advance\@tempcnta by -\@wholewidth
              \put(0,\@tempcnta){\line(\@padx,\@pady){\@tempcntb}}%
            \end{picture}%
          }%
        \fi
        \@tempdima=\csname @padcaseht\the\@padcasecount @\the\i\endcsname
        \advance\@tempdima by -\padboxrule
        \ifnum\i=\@tempcnta \advance\@tempdima by -\padboxrule \fi
        \vskip\@tempdima
        \advance\i by 1%
      }%
      \vskip\@wholewidth
    }%
    % procedures
    \baselineskip=-1pt%
    \lineskip=0pt%
    \vbox{%
      \i=1\relax
      \@whilenum \i<\@padcasecnt
      \do{%
        \expandafter\box
          \csname @padcaseproc\the\@padcasecount @\the\i\endcsname
        \vskip\padcolsep
        \expandafter\ifx
          \csname @padcaseext\the\@padcasecount @\the\i\endcsname \relax
        \else
          \vskip\csname @padcaseext\the\@padcasecount @\the\i\endcsname
          \global\csname @padcaseext\the\@padcasecount @\the\i\endcsname=0pt%
          % reset
        \fi
        \vskip2\padboxrule	% ?
        \advance\i by 1%
      }%
      \vskip-\padcolsep
      \vskip-2\padboxrule	% ?
    }%
  }%
  \global\@padhta=\csname @padcasehta\the\@padcasecount\endcsname
  \global\@padhtb=\csname @padcasehtb\the\@padcasecount\endcsname
  \global\@padhtc=\csname @padcasehtc\the\@padcasecount\endcsname
  \global\@padcasecnt=\@@padcasecnt
  \global\advance\@padcasecount by -1%
}


\def\@padcasepara#1\@padcasepend{%
  \@tempcnta=1%
  \@@padfor\@tempa:=#1\do{%
    \ifnum\@tempcnta=1%
      \global\expandafter\setbox
        \csname @padcasecase\the\@padcasecount\endcsname=\hbox{\mbox{\@tempa}}%
    \else
      \global\expandafter\setbox
        \csname @padcaseproc\the\@padcasecount\endcsname=\hbox{\mbox{\@tempa}}%
    \fi
    \advance\@tempcnta by 1%
  }%
}


% from latex.tex
% just replaced '\edef' with '\def'
%\def\@padfor#1:=#2\do#3{\edef\@fortmp{#2}\ifx\@fortmp\@empty \else
\def\@padfor#1:=#2\do#3{\def\@fortmp{#2}\ifx\@fortmp\@empty \else
    \expandafter\@padforloop#2,\@nil,\@nil\@@#1{#3}\fi}

\def\@padforloop#1,#2,#3\@@#4#5{\def#4{#1}\ifx #4\@nnil \else
       #5\def#4{#2}\ifx #4\@nnil \else#5\@padiforloop #3\@@#4{#5}\fi\fi}

\def\@padiforloop#1,#2\@@#3#4{\def#3{#1}\ifx #3\@nnil 
       \let\@nextwhile=\@fornoop \else
      #4\relax\let\@nextwhile=\@iforloop\fi\@nextwhile#2\@@#3{#4}}

% from latex.tex
% just replaced ',' with ':'
\def\@@padfor#1:=#2\do#3{\def\@fortmp{#2}\ifx\@fortmp\@empty \else
    \expandafter\@@padforloop#2:\@nil:\@nil\@@#1{#3}\fi}

\def\@@padforloop#1:#2:#3\@@#4#5{\def#4{#1}\ifx #4\@nnil \else
       #5\def#4{#2}\ifx #4\@nnil \else#5\@@padiforloop #3\@@#4{#5}\fi\fi}

\def\@@padiforloop#1:#2\@@#3#4{\def#3{#1}\ifx #3\@nnil 
       \let\@nextwhile=\@fornoop \else
      #4\relax\let\@nextwhile=\@@iforloop\fi\@nextwhile#2\@@#3{#4}}


%
% \padsetsize
%
\def\padsetsize#1{%
  \leavevmode
  \setbox\@tempboxa=\hbox{#1}%
  \setbox\@tempboxa=\hbox{\lower\@padhtc \box\@tempboxa}%
  \ht\@tempboxa=\@padhtb
  \dp\@tempboxa=0pt%
  \box\@tempboxa
  \global\@padhta=0pt%
  \global\@padhtc=0pt%
}


%
% \padtest
%
\def\padtest#1{%
  \leavevmode
  \setbox\@tempboxb=\hbox{#1}%
  \setbox\@tempboxa
  =\vbox{%
    \hrule \@height2\padboxrule \@width 30em%
    \vskip-2\padboxrule
    \vskip\@padhta
    \hrule \@height\padboxrule \@width 30em%
    \vskip-\padboxrule
    \vskip\@padhtb
    \hrule \@height\padboxrule \@width 30em%
    \vskip-\padboxrule
    \vskip\@padhtc
    \hrule \@height2\padboxrule \@width 30em%
    \vskip-2\padboxrule
  }%
  \wd\@tempboxa=0pt%
  \vbox{%
    \box\@tempboxb
    \vskip-\@padhta \vskip-\@padhtb \vskip-\@padhtc
    \box\@tempboxa
  }%
}


%
% \savepad	save a PAD
%
% #1	name
% #2	PAD to be saved
%
\def\savepad#1#2{%
  \edef\@tempa##1{\expandafter\@cdr\string #1##1\@nil}%
  \expandafter\newbox\csname\@tempa{}\endcsname
  \expandafter\newdimen\csname\@tempa{@padhta}\endcsname
  \expandafter\newdimen\csname\@tempa{@padhtb}\endcsname
  \expandafter\newdimen\csname\@tempa{@padhtc}\endcsname
  \setbox\csname\@tempa{}\endcsname=\hbox{#2}%
  \csname\@tempa{@padhta}\endcsname=\@padhta
  \csname\@tempa{@padhtb}\endcsname=\@padhtb
  \csname\@tempa{@padhtc}\endcsname=\@padhtc
}


%
% \usepad	draw a PAD saved by \savepad
%
% #1	name
%
\def\usepad#1{%
  \edef\@tempa##1{\expandafter\@cdr\string #1##1\@nil}%
  \copy\csname\@tempa{}\endcsname
  \global\@padhta=\csname\@tempa{@padhta}\endcsname
  \global\@padhtb=\csname\@tempa{@padhtb}\endcsname
  \global\@padhtc=\csname\@tempa{@padhtc}\endcsname
}


\newcount\@padlabflag	% whether it is \padlabel
\@padlabflag=1

%
% \padlabel	draw a label
%
% #1	label
%
\def\padlabel{%
  \leavevmode
  \@tempdimb=2\fboxsep
  \advance \@tempdimb by 2\fboxrule
  \@ifstar{\@ixpadlabel}{\@ipadlabel}%
}

\def\@ipadlabel#1{%
  \setbox \@tempboxa=\hbox{#1}%
  \advance\@tempdimb by \ht\@tempboxa
  \advance\@tempdimb by \dp\@tempboxa
  \ifdim \@tempdimb<16pt \@tempdimb=16pt \fi
  \@padlabel
}

\def\@ixpadlabel#1{%
  \setbox \@tempboxa=\hbox{#1}%
  \@tempdima=.5\@tempdimb
  \advance\@tempdimb by \ht\@tempboxa
  \advance\@tempdimb by \dp\@tempboxa
  \advance\@tempdima by \dp\@tempboxa
  \ifdim \@tempdimb<16pt%
    \@tempdimc=16pt%
    \advance\@tempdimc by -\@tempdimb
    \advance\@tempdima by .5\@tempdimc
    \@tempdimb=16pt%
  \fi
  \lower\@tempdima\hbox{\@padlabel}%
}

\def\@padlabel{%
  \@tempdima=\wd\@tempboxa
  \advance \@tempdima by 2\fboxsep
  \advance \@tempdima by 2\fboxrule
  \hbox{%
    \ifnum \@padlabflag=1\relax
      \ifnum \@padseqlv>0\relax
        \ifnum \csname @padseqf\the\@padseqlv\endcsname=0\relax
          \vrule\@width\padboxrule
          \hskip\padboxsep
        \else
          \global\csname @padseqf\the\@padseqlv\endcsname=2\relax
        \fi
      \fi
    \fi
    \@tempcnta=\@tempdima
    \@tempcntb=\@tempdimb
    \@tempdima=-.5\@tempdima
    \@tempdimb=-.5\@tempdimb
    \@tempcntc=\@tempdima
    \@tempcntd=\@tempdimb
    \unitlength=1sp%
    \begin{picture}(\@tempcnta,\@tempcntb)(\@tempcntc,\@tempcntd)%
      \put(0,0){\oval(\@tempcnta,\@tempcntb)}%
      \put(0,0){\makebox(0,0){\box\@tempboxa}}%
    \end{picture}%
  }%
  \global\@padhta=0pt%
  \global\@padhtb=\@tempdimb
  \global\@padhtc=0pt%
}


%
% \padref	draw a frame for reference to a label
%
% #1	label
%
\def\padref#1{%
  \@padlabflag=0\relax
  \padlabel*{#1}
  \@padlabflag=1\relax
}


\endinput
