突出显示以 % 和 $ lstlisting 为前缀的变量

突出显示以 % 和 $ lstlisting 为前缀的变量

我正在尝试获得与此相当的语法高亮: 示例突出显示

(同样适用于以 $ 为前缀的变量)但我对以 ​​% 为前缀的标识符有问题。我尝试使用

moredelim=*[s][\color{variableColor}]{\%}{\ }

这有一个明显的问题,如果你用点分隔关键字,它不会停止突出显示。我也尝试了 Jubobs 提出的解决方案这里但我无法让它与 % 或 $ 标记一起工作。

这是我当前的代码:

\lstnewenvironment{TorqueScript}{\lstset{ style=TS }}{}

\definecolor{variableColor}{HTML}{AA7700}
\definecolor{globalColor}{HTML}{FF1493}
%\colorlet{globalColor}{ForestGreen!100}
\colorlet{commentSColor}{ForestGreen!100}
\colorlet{commentMColor}{ForestGreen!100}
\colorlet{stringColor}{Blue!100}
\colorlet{tagColor}{Blue!80!black}
\definecolor{concatColor}{HTML}{008200}
\colorlet{thisColor}{Red!100}
\colorlet{identifierColor}{Blue}
\definecolor{datablocksColor}{HTML}{444444}
\definecolor{declarationColor}{HTML}{006699}
\colorlet{functionColor}{white!30!black}

\lstdefinelanguage{TorqueScript}{
    basicstyle=\ttfamily,
    showstringspaces=false
    sensitive=false,
    keywords=[0]{if, else, },
    keywords=[1]{StaticShapeData, ParticleEmitterData, ParticleEmitterNodeData, ParticleData, ParticleEmitterNode,ClientGroup, SimGroup, SimObject,
        GuiControl, GuiBitmapBorderControl, GuiBitmapControl, GuiControlProfile, GuiTextListCtrl, GuiScrollCtrl, GuiTextCtrl, GuiPanel, PlayerData, Material, StaticShapeData,}
    keywords=[2]{SPC,@,TAB},
    keywords=[3]{this},
    keywords=[4]{delete, messageboxok, exec, echo, getcount, commandtoclient, commandtoserver, schedule, getObject, addObject, bind, getRowNumById, addRow, sortNumerical, clearSelection, setRowById, removeRowById, getWord, setWord, getWordCount, getField, setField, StripMLControlChars}, 
    keywords=[5]{function, datablock, singleton, new},
    morestring=[s][\color{stringColor}]{"}{"},
    morestring=[s][\color{tagColor}]{'}{'},
    morecomment=[l][\color{commentSColor}]{//},
    morecomment=[s][\color{commentMColor}]{/*}{*/},
    % Variables
    moredelim=*[s][\color{variableColor}]{\%}{\ },
    moredelim=*[s][\color{globalColor}]{\$}{\ },
}

\lstdefinestyle{TS}{
    language=TorqueScript,
    % Actual keywords
    keywordstyle=[0]{\color{identifierColor}},
    % Datablocks
    keywordstyle=[1]{\color{datablocksColor}},
    % Concatenators
    keywordstyle=[2]{\color{concatColor}},
    % this
    keywordstyle=[3]{\color{thisColor}},
    % functions
    keywordstyle=[4]{\color{functionColor}},
    % Actual keywords
    keywordstyle=[5]{\bfseries \color{declarationColor}},
%
    xleftmargin=\parindent,
    %identifierstyle=\color{blue}
}

答案1

我扩展了 Jubobs 解决方案,并通过大量互联网研究找到了处理这种情况的解决方案。

\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

通过该代码(主要是对 Jubobs 代码的扩展),我能够得到以下结果:

成功

最显着的区别是我必须使用:

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

因为\ifnum会抱怨“不正确的字母字符”。

还值得注意的是,\ifinTS我添加了以确保这不会影响其他语言。

相关内容