Quantcast
Viewing all articles
Browse latest Browse all 2

Answer by LukasPJ for Highlight variables prefixed with % and $ lstlisting

I expanded on Jubobs solution and by lots of research on the internet came up with a solution that handled this case.

\lstnewenvironment{TorqueScript}{\lstset{ style=TS }\inTStrue}{\inTSfalse}\makeatletter% ``state variables''\newif\ifincomment\incommentfalse\newif\ifinstring\instringfalse\newif\ifinTS% --- patch to automatically highlight identifier starting by @% (only outside strings and comments, though) ---\lst@AddToHook{Output}{\@ddedToOutput}\lst@AddToHook{Endgroup}{\incommentfalse\instringfalse}% local variables\newif\if@identifierStartsByDollar@\newif\if@identifierStartsByPercent@\newcount\currentchar\def\splitfirstchar#1{\@splitfirstchar#1\@nil}\def\@splitfirstchar#1#2\@nil{\gdef\@testChar{#1}\gdef\@restTestChar{#2}}\def\@testChar%{%    % copy the first token in \the\lst@token to \@testChar    \expandafter\splitfirstchar\expandafter{\the\lst@token}%    %    % reset switch    \@identifierStartsByPercent@false%    \@identifierStartsByDollar@false%    %    % equality test    \ifthenelse{\equal{`\@testChar}{`\textdollar}}{\@identifierStartsByDollar@true}{}%    \ifthenelse{\equal{`\@testChar}{`\%}}{\@identifierStartsByPercent@true}{}%    %    % apply class style if not within string or comment    \ifinTS    \ifincomment    \else    \ifinstring    \else    \if@identifierStartsByPercent@    % Handle specialcase "%this"    \ifnum\pdf@strcmp{\@restTestChar}{this}=0    \def\lst@thestyle{\color{thisColor}}%    \else    \def\lst@thestyle{\color{variableColor}}%    \fi    \fi    \if@identifierStartsByDollar@    \def\lst@thestyle{\color{globalColor}}%    \fi    \fi    \fi    \fi}\let\@ddedToOutput\@testChar\makeatother

With that code which is largely just an expansion upon Jubobs code, I was able to get the following result:

Image may be NSFW.
Clik here to view.
Success

Most notable difference is that I had to use:

\ifthenelse{\equal{`\@testChar}{`\%}}{\@identifierStartsByPercent@true}{}%

Because the \ifnum would complain about "Improper alphabetic character".

Also worth to notice is the \ifinTS I added to make sure this didn't affect other languages.


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>