\startmodule  IEEE

\usemodule  [abr-aditya]
\usemodule  [bib]

% Bib Setup {{{

\setuppublications    [alternative=ieee]

\defineblank[ieeebibblank]
            [0.3\baselineskip plus 0.1\baselineskip minus 0.1\baselineskip]

\defineblank[ieeebibitemblank][0pt plus .5pt]

\setuppublicationlist 
  [before={\blank[ieeebibitemblank]}]

\def\initializepubslist
  {\switchtobodyfont[8pt]%
   \setupinterlinespace[line=9pt]%
   \setupwhitespace[none]%
   \blank[ieeebibblank]%
   % There should be a more ConTeXtish way!
   % \sloppy
   \tolerance 9999
   \emergencystretch 3em
   \hfuzz .5\points
   \vfuzz\hfuzz
   \clubpenalty4000
   \widowpenalty4000
   \interlinepenalty500
    \sfcode`\.=1000}

% }}}

% Fonts {{{

\definealternativestyle [emph]     [\em]     []

\loadmapfile[dstroke.map]

\starttypescript [math] [modern,computer-modern,latin-modern,ams] [size]
  \definebodyfont [17.3pt,14.4pt,12pt][mm]
                 [mb=dsrom12 sa 1,
                   mc=rsfs10 sa 1]
  \definebodyfont [11pt,10pt,9pt] [mm]
                  [mb=dsrom10 sa 1,
                   mc=rsfs10 sa 1]
  \definebodyfont [8pt,7pt] [mm]
                  [mb=dsrom8 sa 1,
                   mc=rsfs7 sa 1]
  \definebodyfont [6pt,5pt,4pt] [mm]
                  [mb=dsrom8 sa 1,
                   mc=rsfs5 sa 1]
\stoptypescript

\definefamilysynonym [default] [doublestroke]   [mb]
\definefamilysynonym [default] [scriptfamily] [mc]

\def\dstroke {\fam\purefamily  {doublestroke}}
\def\scr     {\fam\purefamily{scriptfamily}} 


\def\doublestroke#1{{\dstroke#1}}
\def\script      #1{{\scr#1}}    


\startmode[mkii]
\usetypescriptfile  [type-gyr]

\starttypescript [mainface] [texnansi,ec,qx,t5,default]

  \definetypeface [mainface] [rm] [serif] [times]     [default] [encoding=\typescripttwo]
  \definetypeface [mainface] [ss] [sans]  [helvetica] [default] [encoding=\typescripttwo]
  \definetypeface [mainface] [tt] [mono]  [courier]   [default] [encoding=\typescripttwo]
  \definetypeface [mainface] [mm] [math]  [modern]    [modern]  [encoding=\typescripttwo]

  \quittypescriptscanning

\stoptypescript
\stopmode

\startmode[mkiv]
\starttypescript [mainface] 

  \definetypeface [mainface] [rm] [serif] [times]     [default] 
  \definetypeface [mainface] [ss] [sans]  [helvetica] [default] 
  \definetypeface [mainface] [tt] [mono]  [courier]   [default] 
  \definetypeface [mainface] [mm] [math]  [modern]    [modern]  

  \quittypescriptscanning

\stoptypescript
\stopmode

\doifmode{final}{\usetypescript[serif][hanging][normal]}

\doifmode{mkii}{\usetypescript[mainface][texnansi]}
\doifmode{mkiv}{\usetypescript[mainface][features=default]}

\doifmode{final}{\setupalign   [hanging]}

\setupbodyfont[mainface,10pt]

\definefont [sectionfont]    [Times-Caps]
\definefont [titlefont]      [Regular at 24pt]
\let\os=\sectionfont

\setupbodyfontenvironment[default]
                         [em=italic]



% The natural width of space in Times is smaller.
% \def\raggedspaceamount{0.25em}


% Change footnote font size to 8pt.
\setupfootnotes[bodyfont=8pt,width=\textwidth,rule=]
\setupfootnotes[location=lastcolumn]
% }}}

% Section Formatting {{{

\defineblank  [section:before] [1.5ex plus 1.5ex minus 0.5ex]
\defineblank  [section:after]  [0.7ex plus 1ex minus 0ex]

\setupsection [section-3]
              [bodypartconversion=Romannumerals,
              appendixconversion=Characters]

\setupsection [section-4]
              [bodypartconversion=Character,
               previousnumber=no]

\setupsection [section-5]
              [bodypartconversion=numbers,
               previousnumber=no]

\setupsection [section-6]
              [bodypartconversion=character,
               previousnumber=no]

\setuplabeltext [section={{},{.}},
                 subsection={{},{.}},
                 appendix={{Appendix },{.}}]

\setupsectionblock [appendix]  [page=]

\setupheads[indentnext=yes]

\setuphead[section,subject]
          [  align=middle,
                   style=\sectionfont,
           appendixlabel=appendix,
              placehead=yes,
                  before={\blank[section:before]},
                   after={\blank[section:after]},
          ]

\setuphead[subsection]
          [ align=normal,
            style=italic,
           before={\blank[section:before]},
            after={\blank[section:after]},
          ]

\setuphead  [subsubject]  [style=italic,alternative=text,distance=1ex]

%  }}}

% Page Layout {{{
\setuppapersize[letter][letter]

\setuplayout[
                 width=middle,
                height=middle,
              location=middle,
              topspace=.75in,
           bottomspace=0.75in,
          bottomdistance=.25in,
                  bottom=.25in,
             backspace=0.75in,
              cutspace=0.75in,
            leftmargin=0in,
           rightmargin=0in,
    leftmargindistance=0in,
   rightmargindistance=0in,
                header=0in,
                footer=0in,
         headerdistace=0in,
        footerdistance=.15in,
      ]


% No page numbering
\setuppagenumbering[location=]

\setupcolumns[n=2,distance=0.2in,column=lastcolumn]

% }}}

% Variables {{{
\setvariables[article][title=]
\setvariables[article][author=]
\setvariables[article][institute=]
\setvariables[article][email=]

% }}}

% Title {{{
\setuplabeltext[abstract=Abstract,keyword=Keyword]

\startsetups  IEEE:abstract:setup
  \switchtobodyfont[9pt]
\stopsetups

\startsetups IEEE:title:after
    \startalignment[middle]
    \doifsomething{\getvariable{article}{author}}
     \bgroup
      \blank[big]
      \tfa\setupinterlinespace
      \getvariable{article}{author}
      \endgraf\egroup
     \doifsomething{\getvariable{article}{institute}}
     {
     \let\\\crlf
     \blank[small]
     \tf\setupinterlinespace
     \getvariable{article}{institute}
     }
   \stopalignment
   \blank[1.4em]
\stopsetups

\startsetups IEEE:headerandfooters
  \doifsomething{\getvariable{article}{bottom}}
  {\setupbottomtexts
    [\getvariable{article}{bottom}]
  \setupbottomtexts
    [][pagenumber]}
\stopsetups

\startsetups IEEE:interaction
  \setupinteraction
   [
       title={\getvariable{article}{title}},
    subtitle={\getvariable{article}{conference}},
      author={\getvariable{article}{author}}]
\stopsetups
\setuphead  [title]
            [align={middle,broad},
             style=\titlefont,
             before={\blank[force,0.2em]},after=]
\def\article
  {\dosingleargument\doarticle}

\def\doarticle[#1]%
  {\setvariables[article][#1]
   \title{\getvariable{article}{title}}
   \setups[IEEE:title:after]
   \setups[IEEE:headerandfooters]
   \setups[IEEE:interaction]
   \startcolumns
   \appendtoks \stopcolumns \to \everystoptext}

\definestartstop[abstract]
  [before={\bgroup\blank[small]\setups[IEEE:abstract:setup]
          \bold\bgroup{\bolditalic \labeltext{abstract}}---\,},
   after={\egroup\endgraf\egroup\blank[small]}]

% }}}

% Theorems and Proofs {{{

\unprotect
\setvalue{@@descriptionIEEE}[#1]#2%
  {\@@dostartdescription[#1]{\def\\{\crlf}}{#2}%
   \doindenting
   \ifhbox\@@descriptionbox\unhcopy\else\copy\fi\@@descriptionbox
   \hskip\!!widthb % toegevoegd
   \@@dodescription}

\setvalue{@@descriptionIEEEproof}[#1]#2%
  {\@@dostartdescription[#1]{\def\\{\crlf}}{#2}%
   \noindent\hskip 2em
   \ifhbox\@@descriptionbox\unhcopy\else\copy\fi\@@descriptionbox
   \hskip\!!widthb % toegevoegd
   \@@dodescription}
\protect

\setupenumerations
  [location=IEEE,
   sectionnumber=no,
   stopper=:\ ,
   headstyle=italic,
   titlestyle=italic,
   titledistance=1ex,
   title=yes,
   style=normal,
   before={\blank[2pt plus 1pt minus 1pt]},
   after={\blank[2pt plus 1pt minus 1pt]}]
   

\defineenumeration [problem]    [text=Problem]
\defineenumeration [definition] [text=Definition]
\defineenumeration [theorem]    [text=Theorem]
\defineenumeration [lemma]      [text=Lemma]
\defineenumeration [corollary]  [text=Corollary]
\defineenumeration [example]    [text=Example]

\defineenumeration
  [proof]
  [   location=IEEEproof,
          text=Proof,
     headstyle=italic,
    titlestyle=italic,
      distance=1ex,
         style=normal,
        number=no,
     titleleft=,
    titleright=,
       stopper=.,
   closesymbol=\math{\square},
  ]

\definedescription
  [remark]
  [stopper=:,width=broad,location=IEEE]


% }}}

% Itemize {{{
\setupitemize[each][packed,joinedup]

\setupitemgroup
  [itemize]
  [each]
  [margin=1pc,
   distance=0pt,
   itemalign=left,
   headstyle=italic,
   indentnext=no]

\setupitemgroup
  [itemize]
  [each]
  [broad,fit]

% }}}

% Formulas {{{

\optimizedisplayspacingtrue

\defineblank[displayskip][1.5ex plus 4pt minus 2pt]

\setupsubformulas [separator=]
\setupformulas    [indentnext=auto,numberstyle=normal,
                   spacebefore=displayskip,
                   spaceafter=displayskip]

\definereferenceformat  [doeqref]   [left=(,right=)]
\def\eqref[#1]{{\normal \doeqref[#1]}}

\usemodule  [mathsets]
\definemathset[EXP] [text={\doublestroke{E}}]
\definemathset[PR]  [text=\Pr,left=(,right=)]
\definemathset[IND] [text={\doublestroke{1}},left={[},right={]}]
\definemathset[PSP] [text={\doublestroke{P}},left={(},right={)}]
\definemathset[SET]

%D These should go to the core.   

\definemathsymbol [lvert] [open]  [sy] ["6A] [ex] ["0C]
\definemathsymbol [rvert] [close] [sy] ["6A] [ex] ["0C]

% Do not know if there is a mistake in core-mat, but this does look better.
\unexpanded\def\dointertext#1%
  {\penalty\postdisplaypenalty
   \vbox{\forgetall\noindent#1\par}%
   \penalty\predisplaypenalty}

\unexpanded\def\stackrel#1#2%
    {\mathrel{\mathop{#2}\limits^{#1}}}

% Float and Figures {{{

\usemodule      [float-test]
\setupcaptions
    [headstyle=normal,stopper=.,distance=1ex,
     style={\switchtobodyfont[small]}]
\setupfloat     [figure]  [indentnext=auto]

\setuplabeltext [figure=Fig~]

%}}}

% MP Settings {{{
\startMPinclusions
% Arrowhead Modifications for TAOCP. Copied from some webpage of Knuth. 
% These arrows are better in appearance than the default mp arrows.
ahangle := 35;
ahlength := 6;

vardef arrowhead expr p =
  save q, e, f, g; path q; pair e; pair f; pair g;
  e = point length p of p;
  q = gobble(p shifted -e cutafter makepath(pencircle scaled 2ahlength))
    cuttings;
  f = point 0 of (q rotated 0.5ahangle) shifted e;
  g = point length q of (reverse q rotated -0.5ahangle) shifted e;
  f .. {dir (angle direction length q of (q rotated 0.5ahangle) - 0.3ahangle)}e
    & e{dir (angle direction 0 of ((reverse q) rotated -0.5ahangle)+0.3ahangle)} .. g
enddef;

def _finarr text t =
  draw _apth t;
  draw arrowhead _apth t        % do not fill
enddef;

def _findarr text t =
  draw _apth t;
  draw arrowhead _apth withpen currentpen t;
  draw arrowhead reverse _apth withpen currentpen t
enddef;
\stopMPinclusions

% }}}

% Setups {{{
\setupindenting[small,yes]
\setupwhitespace[none]

\setuptolerance[vertical,tolerant]

% General Macros <<<
\def\IE{i.e.}
\def\EG{e.g.}

\stopmodule

% vim: nowrap
