%D \module
%D   [     file=t-syntax-groups,
%D      version=2011.06.12,
%D        title=\CONTEXT\ User Module,
%D     subtitle=Syntax highlighting support,
%D       author=Aditya Mahajan,
%D         date=\currentdate,
%D    copyright=Aditya Mahajan,
%D        email=adityam <at> umich <dot> edu,
%D      license=Simplified BSD License]

\writestatus{loading}{ConTeXt User Module / Syntax Highlighting Support}

% Colors are specified in hex; in MkII the hex mode needs to be activated. 
\doifmode\s!mkii
    {\setupcolor[hex]}

\startmodule    [syntax-group]
\usemodule      [module-catcodes]
  
\unprotectmodulecatcodes

\def\colorscheme::name {}

\def\syntaxgroup::id          {syntaxgroup}
\def\syntaxgroup::namespace   {@@@@\colorscheme::name\syntaxgroup::id}
\def\syntaxgroup::name        {}

\installparameterhandler  \syntaxgroup::namespace \syntaxgroup::id

\def\definesyntaxgroup
    {\dodoubleargument\syntaxgroup::define}

\starttexdefinition syntaxgroup::define [#1][#2]
  % #1 list name
  % #2 options
  \doifassignmentelse{#2}
  {
    \def\syntaxgroup::get_parameters##1%
    {
         \edef\syntaxgroup::name {##1}
         \getparameters[\syntaxgroup::namespace##1]
                       [\c!color=,\c!style=,\c!command=,#2]
         \doifsomething{\syntaxgroupparameter\c!color}
          {
             \expanded{\definecolor[\syntaxgroup::namespace-##1-color]
                                   [\syntaxgroupparameter\c!color]}
             \getparameters[\syntaxgroup::namespace##1][\c!color=\syntaxgroup::namespace-##1-color]
          }
    }
  }{
    \def\syntaxgroup::get_parameters##1%
    {
        \copyparameters[\syntaxgroup::namespace##1][\syntaxgroup::namespace#2]
                       [\c!color,\c!style,\c!command]
    }
  }
        
  \processcommalist[#1]\syntaxgroup::get_parameters
\stoptexdefinition

\def\startcolorscheme%
    {\dosingleargument\colorscheme::start}

\starttexdefinition colorscheme::start [#1]
     \pushmacro\colorscheme::name
     \setcolorscheme{#1}
     \getparameters[\syntaxgroup::namespace][\c!color=,\c!style=,\c!command=]
\stoptexdefinition

\def\stopcolorscheme
    {\popmacro\colorscheme::name}

\def\setcolorscheme#1%
    {\edef\colorscheme::name{#1}}

\starttexdefinition syntaxgroup [#1]#2
  % #1 = style
  % #2 = content
      \begingroup
      \edef\syntaxgroup::name{#1}%
      \syntaxgroupparameter\c!command
      {
        \dostartattributes{\syntaxgroup::namespace #1}\c!style\c!color
            #2
        \dostopattributes
      }
      \endgroup
\stoptexdefinition

\def\currentsyntaxgroup   {\syntaxgroup::name}

\protectmodulecatcodes
\stopmodule
