[Tex/LaTex] How to use a colon in lstlisting keywords

csskeywordslistings

for the use of CSS code I've defined the following style:

\lstdefinelanguage{CSS}{
    sensitive=true,
    alsodigit={-},
    keywords={transform, transition-property, transition-duration, transition-timing-functio},              
    morecomment=[l]{//},
    morecomment=[s]{/*}{*/},
    morestring=[b]',
    morestring=[b]"
}

In CSS3 transform can be a keyword and a property. Therefore, both variations are shown in blue in my document.

Screenshot:

s

Keywords always end with a colon. Can this rule be used to prevent the properties being marked as keyword?

MWE:

\documentclass{article}

\usepackage{listings}
\usepackage{color}

\definecolor{lightgray}{rgb}{0.95, 0.95, 0.95}
\definecolor{darkgray}{rgb}{0.4, 0.4, 0.4}
\definecolor{purple}{rgb}{0.65, 0.12, 0.82}

\lstdefinelanguage{CSS}{
    keywords={color,background-image,margin,padding,font,weight,display,position,top,left,right,bottom,list,style,border,size,white,space,min,width, transform, transition, transition-property, transition-duration, transition-timing-function},
    alsodigit={-},
    sensitive=true,
    morecomment=[l]{//},
    morecomment=[s]{/*}{*/},
    morestring=[b]',
    morestring=[b]"
}

\lstset{%
    % General design
    backgroundcolor=\color{lightgray},
    basicstyle={\small\ttfamily},   
    frame=l,
    % Code design
    identifierstyle=\color{black},
    keywordstyle=\color{blue}\bfseries,
    ndkeywordstyle=\color{greenCode}\bfseries,
    stringstyle=\color{ocherCode}\ttfamily,
    commentstyle=\color{darkgray}\ttfamily,
    % Code
    language={CSS},
    tabsize=2,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    extendedchars=true,
    breaklines=true,
    % line-numbers
    xleftmargin={0.75cm},
    numbers=left,
    stepnumber=1,
    firstnumber=1,
    numberfirstline=true,
}

\begin{document}
  \begin{lstlisting}
        #box {
            transition-property: transform;
            transition-duration: 2s;
            transition-timing-function: linear;
      }
  \end{lstlisting}
\end{document}

Best Answer

My current solution is to put a colon at the end of all the keywords and to use alsoletter={-} and alsodigit={:}.

Example:

\lstdefinelanguage{CSS}{
    keywords={transform:, transition-property:, transition-duration:, transition-timing-function:},
    sensitive=true,
    morecomment=[l]{//},
    morecomment=[s]{/*}{*/},
    morestring=[b]',
    morestring=[b]",        
    alsoletter={-},
    alsodigit={:}
}

It is very annoying but it works. It would be better if one could define everything as a keyword that is between a space and a colon.

Related Question