%D \module
%D   [     file=t-vim,
%D      version=2011.08.10,
%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> ieee <dot> org,
%D      license=Simplified BSD License]

\writestatus{loading}{ConTeXt User Module / Vim syntax highlighting}

\startmodule    [vim]
\usemodule      [filter]
\usemodule      [syntax-highlight]  
\usemodule      [syntax-groups]
\usemodule      [module-catcodes]

\unprotectmodulecatcodes

\def\vimtyping::id          {vimtyping}
\def\vimtyping::namespace   {@@@@\vimtyping::id}
\def\vimtyping::name        {}

\installparameterhandler \vimtyping::namespace \vimtyping::id
\installsetuphandler     \vimtyping::namespace \vimtyping::id

\def\definevimtyping
  {\dodoubleargument\vimtyping::define}

\starttexdefinition vimtyping::define [#1][#2]
    \getparameters[\vimtyping::namespace#1][\s!parent=\vimtyping::namespace,#2]

    \edef\vimtyping::name{#1}

    \definesyntaxhighlighting[#1][\s!parent=\vimtyping::namespace#1]
\stoptexdefinition

% Mode to testing the dev version of 2context script.
\doifmodeelse{vim-dev}
  {\def\vimtyping::script_name{2context.vim}}
  {\def\vimtyping::script_name{kpse:2context.vim}}

\def\vimtyping::filter_command
  {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 "let strip=\getvalue{\vimtyping::id-\c!strip-\externalfilterparameter\c!strip}" %
       -c "let highlight=[\externalfilterparameter\c!highlight]" %
       -c "source \vimtyping::script_name" %
       -c "qa" %
       \externalfilterinputfile\space
       \externalfilteroutputfile}

\setvalue{\vimtyping::id-\c!strip-\v!off}{0}
\setvalue{\vimtyping::id-\c!strip-\v!on}{1}

% Undocumented ... but useful if the user makes a mistake
\setvalue{\vimtyping::id-\c!strip-\v!no}{0}
\setvalue{\vimtyping::id-\c!strip-\v!yes}{1}

\setupvimtyping
  [% \c!tab=4,
   % \c!start=1,
   % \c!stop=0,
   % \c!syntax=context,
   % \c!alternative=pscolor,
   % \c!before=,
   % \c!after=,
   % \c!style=\tttf,
   % \c!color=,
   \c!strip=\v!off, 
   \c!highlight=,
   \c!highlightcolor=lightgray,
   \c!filtercommand=\vimtyping::filter_command,
   % \c!continue=yes,
   % \c!read=\v!yes,
   % \c!readcommand=\syntaxhighlighting::read_command,
   \c!output=\externalfilterbasefile.vimout,
   % \c!setups=syntaxhighlighting::setup,
   % \c!option=\v!packed, % Could be a list
   \s!parent=\syntaxhighlighting::namespace,
   % % Numbering options
   % \c!numbering=\v!no,
   % \c!number\c!start=1,
   % \c!number\c!step=1,
   % \c!number\c!continue=\v!no,
   % \c!numberconversion=\v!numbers,
   % \c!number\c!method=\v!first,
   % \c!number\c!location=\v!left,
   % \c!numberstyle=\ttx,
   % \c!numbercolor=,
   % \c!number\c!width=2em,
   % \c!number\c!left=,
   % \c!number\c!right=,
   % \c!number\c!command=,
   % \c!number\c!distance=0.5em,
   % \c!number\c!align=\v!flushright,
  ]

\def\currentvimtyping  {\vimtyping::name}

\startsetups[vim-minor-groups]
    \definesyntaxgroup
        [SpecialComment]
        [Comment]

    \definesyntaxgroup
        [String,Character,Number,Boolean,Float]
        [Constant]

    \definesyntaxgroup
        [Function]
        [Identifier]

    \definesyntaxgroup
        [Condition,Repeat,Label,Operator,Keyword,Exception]
        [Statement]

    \definesyntaxgroup
        [Include,Define,Macro,PreCondit]
        [Preproc]

    \definesyntaxgroup
        [StorateClass,Structure,Typedef]
        [Type]

    \definesyntaxgroup
        [SpecialChar,Delimiter,Debug]
        [Special]
\stopsetups

\startcolorscheme[pscolor]
    % Vim Preferred groups
    \definesyntaxgroup 
        [Constant]  
        [\c!color={h=007068}]

    \definesyntaxgroup
        [Identifier]
        [\c!color={h=a030a0}]

    \definesyntaxgroup 
        [Statement] 
        [\c!color={h=2060a8}]

    \definesyntaxgroup 
        [PreProc] 
        [\c!color={h=009030}]

    \definesyntaxgroup 
        [Type]      
        [\c!color={h=0850a0}]

    \definesyntaxgroup 
        [Special] 
        [\c!color={h=907000}]

    \definesyntaxgroup 
        [Comment]
        [\c!color={h=606000}]

    \definesyntaxgroup
         [Ignore]

    \definesyntaxgroup 
        [Todo]      
        [\c!color={h=800000}]

    \definesyntaxgroup 
        [Error] 
        [\c!color={h=c03000}]

    \definesyntaxgroup 
        [Underlined]
        [\c!color={h=6a5acd},
         \c!command=\underbar]

    \setups{vim-minor-groups}

    \definesyntaxgroup
        [Number]
        [\c!color={h=907000}]
\stopcolorscheme

\startcolorscheme[blackandwhite]
    \definesyntaxgroup 
        [Constant]  

    \definesyntaxgroup
        [Identifier]

    \definesyntaxgroup 
        [Statement] 
        [\c!style=bold]

    \definesyntaxgroup 
        [PreProc] 
        [\c!style=bold]

    \definesyntaxgroup 
        [Type]      
        [\c!style=bold]

    \definesyntaxgroup 
        [Special] 

    \definesyntaxgroup 
        [Comment]
        [\c!style=italic]

    \definesyntaxgroup
         [Ignore]

    \definesyntaxgroup 
        [Todo]      
        [\c!command=\inframed]

    \definesyntaxgroup 
        [Error] 
        [\c!command=\overstrike]

    \definesyntaxgroup 
        [Underlined]
        [\c!command=\underbar]

    \setups{vim-minor-groups}

\stopcolorscheme
\protectmodulecatcodes

\stopmodule

