\RequirePackage{xcolor}
\RequirePackage{listings}

\lstdefinelanguage{pseudocode}
{morekeywords={procedure,function,end,loop,return,to,local,Value,fail,if,then,do,for,else,elif,foreach,in,true,false,
 inputs,returns,persistent,repeat,until,while},
  morecomment=[s]{(*}{*)},
  morecomment=[s]{/*}{*/},   % preferred over (* ... *)
  morecomment=[s][\sl]{<}{>}
}

\lstdefinelanguage[sTeX]{TeX}[LaTeX]{TeX}%
  {moretexcs={[2]definiendum,definame,symref,symname,comp,
      importmodule,usemodule
      symdef,symdecl,notation,sref},
   morekeywords={smodule,sdefinition,sassertion,sparagraph,
     inputref,
     sproblem,hint,exnote,solution,gnote,
     sproof,spfsketch,spfstep,spfcases,spfcase,justification,premise}}

\definecolor{backcolor}{gray}{.96}

\lstset{columns=fullflexible,basicstyle=\ttfamily}
\lstset{frame=none,numbers=none,lineskip=-.7ex}%,aboveskip=-1em,belowskip=-2em}
\lstset{keywordstyle=[2]\color{red},keywordstyle=[3]\color{blue}}
\lstset{keywordstyle=[4]\color{green},keywordstyle=[5]\color{magenta}}
\lstset{emphstyle=[2]\color{red},emphstyle=[3]\color{blue}}
\lstset{emphstyle=[4]\color{green},emphstyle=[5]\color{magenta}}
\lstset{commentstyle=\color{brown}}
\lstset{backgroundcolor=\color{backcolor}}
\newcommand{\lstkeycolor}[2]{\ifcase#1\textbf{#1}\or\red{#2}\or\blue{#2}\or\green{#2}\or\magenta{#2}\fi}
\define@key{stexlisting}{id}{\def\sref@id{#1}}
\define@key{stexlisting}{id}{\def\stexlst@id{#1}}

\definecolor{semanticmacrocolor}{HTML}{008800}

\lstdefinelanguage{sTeX}{
  sensitive=true,
  numbers=left,
  numbersep=3pt,
  xleftmargin=3pt,
  alsoother={$()$},
  alsoletter={\\},
  escapechar=|,
  %moredelim = [s][\itshape]{$}{$},
  %moredelim = [s][\itshape\bfseries]{\\[}{\\]},
  classoffset=0,keywordstyle=\bfseries,morekeywords={
      \\begin,\\end,\\ExplSyntaxOn,\\ExplSyntaxOff,\\documentclass,
      \\usepackage,\\def,\\[,\\],\\else,\\fi,
      \\newcommand, \\renewcommand, \\let,\$
  },	
  classoffset=1,keywordstyle=\itshape\color{semanticmacrocolor},morekeywords={
    % semantic macros
  },	
  classoffset=2,keywordstyle=\color{blue},morekeywords={
    \\symdecl,\\symdef,\\notation,\\vardef,\\varseq,
    \\textsymdecl, \\renamedecl, \\assign, \\setnotation,
    \\STEXexport
  },
  classoffset=3,keywordstyle=\color{orange},morekeywords={
    \\importmodule,\\usemodule,\\libinput,\\inputref,\\mhinput,
    \\libusepackage,\\addmhbibresource,\\ifinputref,\\copymod,
    \\ifstexhtml, \\IfInputref,\\requiremodule,\\realize,\\usestructure
  },
  classoffset=4,keywordstyle=\color{purple},morekeywords={
    \\definiendum,\\definame,\\Definame,\\symref,\\symname,\\comp,\\this,
    \\compemph,\\definiens,\\svar,\\infprec,\\neginfprec,\\ellipses,
    \\Symname,\\arg,\\yield,\\assumption,\\spfstep,\\eqstep,\\conclude,
    \\symref,\\sn,\\sr,\\sns,\\Sn,\\Sns,\\defnotation,\\symuse,\\maincomp,
    \\argsep,\\argmap,\\argarraymap,\\conclusion
  },
  classoffset=5,keywordstyle=\color{magenta},morekeywords={
    smodule,sdefinition,sassertion,sparagraph,sexample,
    sproof,subproof,spfcases,spfcase,spfstep,spfcomment,spfblock,
    sproblem,subproblem,solution,hint,exnote,mcb,scb,
    \\spfidea,\\spfsketch,\\spfcasesketch,\\sproofend,
    \\spfjust,\\premise,\\justarg,\\inlinedef,\\inlineex,\\inlineass,
    \\scc,\\mcc,\\objective,
    \\varbind,
    copymodule,interpretmodule,mathstructure,sexample,extstructure,
    extstructure*,realization
  },
  classoffset=6,keywordstyle=\color{cyan},morekeywords={
    sfragment,blindfragment,\\skipfragment,
    \\currentsectionlevel,\\Currentsectionlevel,\\setsectionlevel,
    \\prematurestop,\\afterprematurestop,
    \\setSGvar,\\useSGvar,\\ifSGvar,\\sref,\\sreflabel,\\extref,\\srefsetin,
    \\srefsym,\\srefsymuri
  },
  classoffset=7,keywordstyle=\color{brown},morekeywords={
    frame,note,\\ifnotes,\\setslidelogo,\\setsource,\\setlicensing
    slides,notes,sectocframes,frameimages,fiboxed,
    nparagraph, nparagraph, ndefinition, nexample, nsproof, nassertion,
    \\frameimage,\\mhframeimage,\\textwarning,
    \\activateexcursion,\\printexcursion,\\excursionref,\\excursiongroup
  },
  classoffset=8,keywordstyle=\color{red},morekeywords={
    frame,note
  },
  classoffset=9,keywordstyle=\color{gray},morekeywords={
    % variables
  }
}

\ExplSyntaxOn

\lstnewenvironment{stexcode}[1][]{
  \_lststex_parse_args:n{#1}
  \exp_args:No \lstset\l_lststex_return_tl
}{}

\newcommand\stexcodeinline[1][]{
  \_lststex_parse_args:n{#1}
  \exp_args:Nno \use:nn{\lstinline[} \l_lststex_return_tl ]
}

\keys_define:nn{ lststex }{
  symbols  .code:n = {
    \clist_map_inline:nn{#1}{
      \clist_put_right:Nn \l_lststex_symbols_clist {##1}
      \clist_put_right:Nn \l_lststex_symbols_clist {\\##1}
    }
  },
  variables  .code:n = {
    \clist_map_inline:nn{#1}{
      \clist_put_right:Nn \l_lststex_variables_clist {##1}
      \clist_put_right:Nn \l_lststex_variables_clist {\\##1}
    }
  },
  unknown   .code:n = {
    \str_if_empty:NTF \l_keys_key_str {
      \tl_if_empty:nTF{#1}{
        \exp_args:NNo \clist_put_right:Nn \l_lststex_pass_on_args_clist \l_keys_key_tl
      }{
        \exp_args:NNx \clist_put_right:Nn \l_lststex_pass_on_args_clist {
          \l_keys_key_tl = \exp_not:n{{#1}}
        }
      }
    }{
      \tl_if_empty:nTF{#1}{
        \exp_args:NNo \clist_put_right:Nn \l_lststex_pass_on_args_clist \l_keys_key_str
      }{
        \exp_args:NNx \clist_put_right:Nn \l_lststex_pass_on_args_clist {
          \l_keys_key_str = \exp_not:n{{#1}}
        }
      }
    }
  }
}

\cs_new_protected:Nn \_lststex_parse_args:n {
  \clist_clear:N \l_lststex_pass_on_args_clist
  \clist_put_right:Nn \l_lststex_pass_on_args_clist {language=sTeX}
  \clist_clear:N \l_lststex_symbols_clist
  \clist_clear:N \l_lststex_variables_clist
  \keys_set:nn { lststex }{ #1 }
  \tl_set:Nx \l_lststex_return_tl {
    \exp_args:No \exp_not:n \l_lststex_pass_on_args_clist
    \clist_if_empty:NF \l_lststex_symbols_clist {
      ,classoffset=1,morekeywords={
        \exp_args:No \exp_not:n \l_lststex_symbols_clist
      }
    }
    \clist_if_empty:NF \l_lststex_variables_clist {
      ,classoffset=9,morekeywords={
        \exp_args:No \exp_not:n \l_lststex_variables_clist
      }
    }
  }
}

\ExplSyntaxOff

% LocalWords:  lstdefinelanguage morekeywords morecomment moretexcs defin sref
% LocalWords:  twindef atwindef definalt twindefalt atwindefalt termref lstset
% LocalWords:  basicstyle scriptsize fullflexible lineskip aboveskip belowskip
% LocalWords:  keywordstyle keywordstyle newcommand lstkeycolor ifcase textbf
% LocalWords:  stexlisting stexlst sproof spfstep symdef spfcases spfcase tt
% LocalWords:  texcsstyle emphstyle emphstyle ttfamily
