
\import{ref.zmm}
\import{ctr.zmm}

\: iref#2, lref#2, enref#2, aref#2.
\: evn{spacing}
\: evn{itemize}   \item \items \intermezzo \itemskip
\: bf#1 it#1 v#1 tt#1
\: ftinc#2 ftdec#2
\: httpref#1 sibref#2
\: (deprecated) par car cpar#1 ccar#1
\: par#1 car#1 cpar#2 ccar#2
\: verbatim#1 verbatix#1

\${html}{\set{{modes}{vw}}{}{

   {_spacer#2}    {\<*spacer type=block width=\1 height=\2>}
   {_anch#2}      {\<a name="\1">{\2}}
   {iref#2}       {\<a class="intern" href="#\1">{\2}}
   {lref#2}       {\<a class="local" href="\1">{\2}}
   {aref#2}       {\<a class="extern" href="\1">{\2}}
   {enref#2}      {\<a name="\1">{\2}}
   {iref_quiet#2} {\<a class="quiet" href="#\1">{\2}}
   {lref_quiet#2} {\<a class="quiet" href="\1">{\2}}

    }
}


\${roff}{\set{{modes}{vw}}{}{

   {iref#2}    {\it{\2}}
   {aref#2}    {\it{\2}}
   {lref#2}    {\it{\2}}
   {enref#2}   {\2}

   }
}


\formatted{
   \env{spacing}{
      {top}{0}         \: not yet functional.
      {bottom}{0}      \: not yet functional.
      {left}{0}        \: only for indent.
      {right}{0}       \: only for shrinking.
   }{
      \${html}{
         \<div style="
            \if{\$top}{margin-top:\$top\,em;}{}
            \if{\$bottom}{margin-bottom:\$bottom\,em;}{}
            \if{\$left}{margin-left:\$left\,em;}{}
            \if{\$right}{margin-right:\$right\,em;}{}
         ">
      }
      \${roff}{
         \if{\$left}{\@{\N.in +\&{\$left}m\N}}{}
         \if{\$right}{\@{\N.ll -\&{\$right}m\N}}{}
      }
   }{
      \${html}{\</div>}
      \${roff}{
         \if{\$left}{\@{\N.in -}\$left\@{m\N}}{}
         \if{\$right}{\@{\N.ll +}\$right\@{m\N}}{}
      }
   }
}

\switch{\__device__}{
   {html}{

         \set{{modes}{vw}}{}{
{bf#1}      {\<b>{\1}}
{it#1}      {\<i>{\1}}
{v#1}       {\<tt>{\1}}
{vq#1}      {\<tt>{\1}}
{m#1}       {\<tt>{\1}}
{tt#1}      {\<tt>{\1}}

{ftinc#2}   {\<span style="font-size:120%">{\2}}
{ftdec#2}   {\<span style="font-size:80%">{\2}}

{httpref#1} {\<a class="extern" href="\1">{\1}}
{httpref#2} {\<a class="extern" href="\1">{\2}}
{sibref#1}  {\<a class="local" href="\1.html">{\1}}
{sibref#2}  {\<a class="local" href="\1.html">{\2}}
{sibref#3}  {\<a class="local" href="\1.html#\2">{\3}}
         }}

   {roff}{
         \set{{modes}{vw}}{}{
{bf#1}      {\@{\\fB}\1\@{\\fP}}
{it#1}      {\@{\\fI}\1\@{\\fP}}
{v#1}       {\@{\\fC}\1\@{\\fP}}
{vq#1}      {'\v{\1}'}
{m#1}       {\@{\\fC}\1\@{\\fP}}
{tt#1}      {\@{\\fC}\1\@{\\fP}}

{ftinc#2}   {\@{\\s+\1}\2\@{\\s-\1}}
{ftdec#2}   {\@{\\s-\1}\2\@{\\s+\1}}

{httpref#1} {\1}
{httpref#2} {\1}  \: the url-encoded thing.
{sibref#1}  {\bf{\1}}
{sibref#2}  {\bf{\2}}
{sibref#3}  {\bf{\3}}
         }}
}

\switch{\__device__}{
   {html}{
      \special{}          \: remove existing definitions.
      \special{
         {60}  {&lt;}      \: less than sign
         {62}  {&gt;}      \: greater than sign
         {38}  {&amp;}     \: ampersand

         {-1} {&nbsp;}     \: the zoem escape \~
         {-2} {<br>}       \: the zoem escape \|
         {-3} {&mdash;}    \: the zoem escape \-
      }

      \constant{
         {itembullet}{<span class="itembullet">&bull;</span>}
      }
   }
   {roff}{
      \special{}          \: remove existing definitions.
      \special{
         {34}  {"}         \: double quote is special in second layer only
         {34}  {""}        \: double quote is special in second layer only
         {46}  {\\&.}      \: a dot.
         {96}  {\\&`}      \: backquote.
         {39}  {\\&'}      \: quote

         {92}  {\\e}       \: backslash (default troff escape character).
                           \: escaping by backslash may fail in
                           \: diversions [for old roffs].

         {-1} {\\ \\&}        \: the zoem escape \~
         {-2} {\!N.br\!N}  \: the zoem escape \|
         {-3} {\\-}        \: the zoem escape \-
      }
      \constant{
         {itembullet}{\\(bu}
      }
   }
}

\formatted{

   \env{itemize}{
      {interitem}{0}        \: space between items in ems
      {flow}{cascade}       \: item and description on successive lines.
      {textindent}{2}       \: (html device: text-margin offset)
      {itemmargin}{1}       \: (html device: right-aligned item)
      {mark}{\*{itembullet}}
      {margintop}{1}        \: NOTE anonalous unit (ems). mq.
      {align}{left}         \: the way item is aligned.
      {lp}{}                \: left parenthesis
      {rp}{}                \: right parenthesis
      {type}{mark}          \: affects \item. mark/roman/abc/arabic
      {itemcount}{1}
      {fontsize}{100}       \: css percent-style sizing.
      {class_item}{}
      {class_itemtext}{}
      {class_itemize}{}
      {class_all}{}
   }{

      \${html}{\<div class="\$class_all itemize \get{itemize}{$class_itemize}" \:/
                     style="margin-top:\$margintop\,em; font-size:\$fontsize\,%">
              }
      \${roff}{ \if{\$margintop}{\@{\P}}{}  }
   }{
      \_itemend
      \${html}{\</div>}
   }

         \: fixme: <div>+<br> to introduce space is ugly.
         \: tricky to add margin to next item though.
         \:
   \def{_itemskip}{
      \if{\let{\$itemcount > 1 && \$interitem}}{
         \${html}{\<div style="margin-top:0em">{\~}}
         \${roff}{\@{\P}}
      }{}
   }
   \def{itemskip}{\_itemskip}

   \def{intermezzo#1}{

      \_itemend \_itemskip

      \${html}{\<div style="position:relative; margin-left:0em">\1}
      \${roff}{\1}
   }

   \def{item#1}{

      \_itemend   \_itemskip

      \${html}{\<div class="\$class_all item_\get{itemize}{$flow} \get{itemize}{$class_item}">{
         \<div class="\$class_all item_\get{itemize}{$align}align nowrap" \get{itemize}{$style_align}>{\1}
      }}
      \${roff}{
         \if{\cmp{eq}{\get{itemize}{$align}}{left}}
            {\@{\+{2}.ZI }\$w3\@{m "}\1\@{"\+{1}}}
            {\@{\+{2}.ZJ }\$w1\@{m }\$w2\@{m "}\1\@{"\+{1}}}
      }

      \_itemdef   \_iteminc
   }

   \def{items#1}{

      \if{\cmp{eq}{\get{itemize}{$flow}}{compact}}{
         \write{stderr}{txt}{\`{<}itemize: compact not supported with items#1\|\`{>}}
         \'exit
      }{}

      \_itemend   \_itemskip

      \set{_items}{\1}

      \${html}{
         \apply{_#1{\<div class="\$class_all item_cascade \get{itemize}{$class_item} item_\get{itemize}{$align}align nowrap" \$style_align>{\!1}}
         }{\_items}
      }
      \${roff}{
         \: compact/not compact is accounted for by \_itemdef.
         \apply{
            _#1{
               \if{\cmp{eq}{\get{itemize}{$align}}{left}}
                  {\@{\N\+{2}.ZI }\$w3\@{m "}\!1\@{"\+{1}}}
                  {\@{\N\+{2}.ZJ }\$w1\@{m }\$w2\@{m "}\!1\@{"\+{1}}}
               \@{\N\\&\N'in -}\$w3\@{m\N}
            }
         }{
            \_items
         }
         \@{\N'in\s+}\$w3\@{m\N}
      }
      \_itemdef   \_iteminc
   }

   \def{item}{

      \_itemend   \_itemskip

      \${html}{\<div class="\$class_all item_\$flow \get{itemize}{$class_item}">{
                  \<div class="\$class_all item_\get{itemize}{$align}align" \$style_align>{\_putitemmark}}
              }
      \${roff}{
         \if{\cmp{eq}{\get{itemize}{$align}}{left}}
            {\@{\N.ZI }\$w3\@{m \"}\_putitemmark\@{\"}}
            {\@{\N.ZJ }\$w1\@{m }\$w2\@{m }"\_putitemmark"}
      }
      \_itemdef   \_iteminc
   }

   \def{_itemdef}{
      \${html}{\<div class="\$class_all item_text \get{itemize}{$class_itemtext}" \:/
                  style="margin-left:\get{itemize}{$textindent}\,em">
              }
      \${roff}{
         \if{\cmp{eq}{\get{itemize}{$flow}}{compact}}
            {\@{\N}}
            {\@{\N\\&\N.br\N}}
      }
   }

   \def{_itemend}{            \:  |<---w1---->|w2|, w3 = w1 + w2
      \set{{modes}{xv}}{}{
         {$w3}{\$textindent}
         {$w2}{\let{\$textindent - \$itemmargin}}
         {$w1}{\$itemmargin}
      }
      \set{{modes}{x}{dict}{itemize}}{$style_align}{
         \if{\cmp{eq}{\get{itemize}{$align}}{right}}{style="right:-\get{itemize}{$itemmargin}em"}{}
      }
      \if{\let{\$itemcount > 1}}{
         \${html}{\</div>}
         \${roff}{\@{\N.in\s-}\$w3\@{m\N}}
      }{}
   }
   
   \def{_iteminc}{\setx{$itemcount}{\let{\$itemcount+1}}}

   \def{_putitemmark}{
      \get{itemize}{$lp}
      \switch{\$type}{
         {mark}{\$mark}
         {roman}{\textmap{{number}{roman}}{\$itemcount}}
         {ROMAN}{\textmap{{number}{roman}{word}{ucase}}{\$itemcount}}
         {abc}{\textmap{{number}{alpha}}{\$itemcount}}
         {ABC}{\textmap{{number}{alpha}{word}{ucase}}{\$itemcount}}
         {arabic}{\$itemcount}
         {?}
      }
      \get{itemize}{$rp}
   }
}


\'${roff}{
   \'def{"generic::troff-defs"}{
\@{\N
.po 2m
.de ZI
.\\" Zoem Indent/Itemize macro I.
.br
'in +\\\\$1
.nr xa 0
.nr xa -\\\\$1
.nr xb \\\\$1
.nr xb -\\\\w'\\\\$2'
\\h'|\\\\n(xau'\\\\$2\\h'\\\\n(xbu'\\\\
..
.de ZJ
.br
.\\" Zoem Indent/Itemize macro II.
'in +\\\\$1
'in +\\\\$2
.nr xa 0
.nr xa -\\\\$2
.nr xa -\\\\w'\\\\$3'
.nr xb \\\\$2
\\h'|\\\\n(xau'\\\\$3\\h'\\\\n(xbu'\\\\
..\N}
   }
}

\formatted{

   \switch{\__device__}{

      {html}{

\: plain ole style paragraph (deprecated)

         \def{par}{\<*p>}

\: ole style carry on paragraph. more or less html specific; it seems to be
\: needed after e.g. verbatim environment. deprecated.

         \def{car}{
            \<*p style="margin-top:0em; margin-bottom:0em">
         }

\: ole style caption paragraph. deprecated.

         \def{cpar#1}{
            \<*p>
            \bf{\1}
            \|
         }

\: old style caption carry on paragraph.

         \def{ccar#1}{
            \<*p style="margin-top:0em; margin-bottom:0em">
            \bf{\1}
            \|
         }


\: paragraph.

         \def{par#1}{
            \<p style="margin-bottom:0">{\1}
         }

\: carry on paragraph. more or less html specific; it seems to be needed
\: after e.g. verbatim environment. only  use if spacing matters to you,
\: otherwise better suffer and wait  for more generic (html client side)
\: solutions.

         \def{car#1}{
            \<p style="margin-top:0em; margin-bottom:0em">{\1}
         }

\: caption paragraph.

         \def{cpar#2}{
            \<p style="margin-bottom:0">{
               \bf{\1}\|
               \2
            }
         }

\: caption carry on paragraph.

         \def{ccar#2}{
            \<p style="margin-top:0em; margin-bottom:0em">{
               \bf{\1}\|
               \2
            }
         }

         \def{verbatim#1}{
            \: \@{\N<div class=copy>\w}\1
            \@{\N<pre>\w}\1
            \@{\W</pre>\N}
         }
         \def{verbatix#1}{\verbatim{\1}}
      }

      {roff}{
         \set{{modes}{vw}}{}{
            {par}       {\@{\P}}
            {car}       {\@{\N}}
            {cpar#1}    {\@{\P}\bf{\1}\@{\N}\|}
            {ccar#1}    {\@{\N}\bf{\1}\@{\N}\|}

            {par#1}     {\@{\P}\1}
            {car#1}     {\@{\N}\1}
            {cpar#2}    {\@{\P}\bf{\1}\|\2\@{\N}}
            {ccar#2}    {\@{\N}\bf{\1}\|\2\@{\N}}
         }

         \: the non-non-breaking variant of verbatim

         \def{verbatix#1}{
            \@{\P.nf \\fC\N\w}
            \1
            \@{\W\N.fi \\fR\P}
         }

         \def{verbatim#1}{
            \@{\P.di ZV\N.in 0\N.nf \\fC\N\w}
            \1
            \@{\W\N.fi \\fR\N.in\N.di\N.ne \\n(dnu\N}
            \@{\N.nf \\fC\N.ZV\N.fi \\fR\P}
         }

         \: Jan van der Steen is to blame for this :)

         \: .di ZV      new diversion named ZV.
         \: .in 0       set indent to 0.
         \: .nf \fC     no fill (no adjustment), fixed width font?

         \: .fi \fR     fill on, Roman font.
         \: .in         previous indent.
         \: .di         end of diversion (ZV).

         \: .ne \n(dnu  need 'dn' vspace (vsize of most recent diversion).
         \: .nf \fC     set no fill mode, fixed width font.
         \: .ZV         do diversion.
         \: .fi \fR     fill mode on, Roman font.

      }
   }
}

