%D \module
%D   [     file=t-vim,
%D      version=2011.02.05,
%D        title=\CONTEXT\ User Module,
%D     subtitle=Vim syntax highlighting,
%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 / Vim syntax highlighting}

\startmodule[vim]

\unprotect

\usemodule[filter]

% We specify colors in hex so
\setupcolor[hex]

\startinterface all
  \setinterfaceconstant {syntax}      {syntax} 
\stopinterface

\def\????vimtyping{@@@@vimtyping}

\installparameterhandler \????vimtyping {vimtyping}
\installsetuphandler     \????vimtyping {vimtyping}

\def\definevimtyping
  {\dodoubleargument\dodefinevimtyping}

\def\dodefinevimtyping[#1][#2]% 
  {\getparameters[\????vimtyping#1][\s!parent=\????vimtyping,#2]%
   \edef\currentvimtyping{#1}%
   \defineexternalfilter[#1][\s!parent=\????vimtyping#1]%
  \setvalue{type#1file}{\dodoubleargument\doprocessexternalfilterfile[#1]}%
  }

\startsetups[vimsetup]
  \edef\currentvimtyping{\currentexternalfilter}%
  \edef\currentvimalternative{\externalfilterparameter\c!alternative}%
  \let\SYN\vimsyntax
  \def\NL{\strut}% TODO: Add line numbering
  \def\tab##1%
    {\dorecurse{\externalfilterparameter\c!tab}
    {\space}}%
  \doifinset{\externalfilterparameter\c!option}{\v!packed}
    {\setupwhitespace[\v!none,\v!flexible]}%
  \setcatcodetable\externalfilterwritecatcodes
\stopsetups

\def\vimfiltercommand
  {vim -u NONE % don't read global config file
       -e % run in ex mode
       -s % silent
       -C % set compatible
       -n % no swap file
       -c "set tabstop=\externalfilterparameter\c!tab" %
       -c "syntax on" %
       -c "set syntax=\externalfilterparameter\c!syntax" %
       -c "let contextstartline=\externalfilterparameter\c!start" %
       -c "let  contextstopline=\externalfilterparameter\c!stop" %
       -c "source kpse:2context.vim" %
       -c "qa" %
       \externalfilterinputfile\space
       \externalfilteroutputfile}

\def\startvimalternative
  {\dosingleargument\dostartvimalternative}

\def\dostartvimalternative[#1]%
  {\pushmacro\currentvimalternative
   \edef\currentvimalternative{#1}}

\def\stopvimalternative
  {\popmacro\currentvimalternative}

\def\setvimsyntax
  {\doquadrupleargument\dosetvimsyntax}

\def\dosetvimsyntax[#1][#2][#3][#4]% name color style command
  {\def\dodosetupvimsyntax##1%
    { \doifsomething{#2}
      {\definecolor[\????vimtyping:\currentvimalternative:##1:color] [#2]%
       \getparameters[\????vimtyping::\currentvimalternative::##1]
                     [\c!color={\????vimtyping:\currentvimalternative:##1:color}]}
     \getparameters[\????vimtyping::\currentvimalternative::##1]
                   [\c!style=#3,
                    \c!command=#4]}%
     \processcommalist[#1]\dodosetupvimsyntax}


\def\vimsyntax[#1]#2% style content
  {\dostartattributes{\????vimtyping::\currentvimalternative::Normal}\c!style\c!color\empty%
   \dostartattributes{\????vimtyping::\currentvimalternative::#1}\c!style\c!color\empty%
   \getvalue{\????vimtyping::\currentvimalternative::#1\c!command}{#2}%
   \dostopattributes
   \dostopattributes}

\startvimalternative[pscolor]
  \setvimsyntax [Normal]    [\maintextcolor][\tttf]

  \setvimsyntax 
    [Constant,Character,Boolean,Float]  
    [h=007068]      

  \setvimsyntax [Number]    [h=907000]

  \setvimsyntax
    [Identifier, Function]
    [h=a030a0]      

  \setvimsyntax 
    [Statement,Conditional,Repeat,Label,Operator,Keyword,Exception] 
    [h=2060a8]      

  \setvimsyntax 
    [PreProc, Include, Define, Macro, PreCondit] 
    [h=009030]

  \setvimsyntax 
    [Type,StorageClass, Structure, Typedef]      
    [h=0850a0]      

  \setvimsyntax [Special]   [h=907000]
  \setvimsyntax [SpecialKey][h=1050a0]

  \setvimsyntax
    [Tag, Delmiter]

  \setvimsyntax 
    [Comment, SpecialComment]
    [h=606000]

  \setvimsyntax
    [Debug,Ignore]

  \setvimsyntax [Todo]      [h=e0e090]
  \setvimsyntax [Error]     [h=c03000]
  \setvimsyntax [Underlined][h=6a5acd][][\underbar]

\stopvimalternative

%TODO
%   \setvimsyntax [id] [bg=, fc=, style=, command=] == \localframed[options]
\startvimalternative[blackandwhite]
  \setvimsyntax [Normal]    [\maintextcolor][\tttf]

  \setvimsyntax 
    [Constant,Character,Boolean,Float,Number,Identifier,Function]  

  \setvimsyntax 
    [Statement,Conditional,Repeat,Label,Operator,Keyword,Exception] 
    [][][\bold]

  \setvimsyntax 
    [PreProc, Include, Define, Macro, PreCondit] 
    [][][\bold]

  \setvimsyntax 
    [Type,StorageClass, Structure, Typedef]      
    [][][\bold]

  \setvimsyntax [Special, SpecialKey]

  \setvimsyntax [Tag, Delmiter]

  \setvimsyntax 
    [Comment, SpecialComment]
    [][][\italic]

  \setvimsyntax
    [Debug,Ignore]

  \setvimsyntax [Todo]      [][][\inframed]
  \setvimsyntax [Error]     [][][\overstrike]
  \setvimsyntax [Underlined][][][\underbar]

\stopvimalternative

\setupvimtyping
  [\c!tab=4,
   \c!start=1,
   \c!stop=0,
   \c!syntax=context,
   \c!alternative=pscolor,
   \c!before=,
   \c!after=,
   \c!filtercommand=\vimfiltercommand,
   \c!continue=yes,
   \c!read=\v!yes,
   \c!readcommand=\ReadFile,
   \c!output=\externalfilterbasefile.vimout,
   \c!setups=vimsetup,
   \c!option=\v!packed, % Could be a list
   \s!parent=\????externalfilter,
  ]

\protect

\stopmodule

