I want code in parentheses and in brackets to appear in different colors. In the following document, the words brackets again appear in green as intended, and outside again is black as intended, but stretches into the margin of the page.
If I uncomment the line ,breaklines=true, the line is broken between brackets and again, however, these words and the following are red, i.e. as if a (-)-delimited section was detected but then the closing ) was ignored.
What is going on here?
Addition: The problem occurs also if there is no line break present, e.g. if I shorten all words in the text to their first letter. The problem goes away in any case if I pick any other kind of delimiter instead of (), e.g. {}.
\documentclass{article}
\usepackage{color}
\usepackage{listings}
\lstdefinelanguage{new}{
moredelim=*[s][\color{red}]{(}{)},
moredelim=*[s][\color{green}]{[}{]},
}
\lstset{
language=new
%,breaklines=true
}
\begin{document}
\begin{figure}
\begin{lstlisting}
outside(in paren) [in brackets (paren+brackets) brackets again] outside again
\end{lstlisting}
\end{figure}
\end{document}

\lst@Def. Is there any reason why you use those\expandafter's and not\patchcmd{\lsthk@SelectCharTable}{...}{}{}{}? – egreg Oct 01 '12 at 11:01\patchcmd{\lsthk@SelectCharTable}would be possible as well. I’ve used the alternative formulation since this is the way the control sequence name of theSelectCharTablehook is constructed by\lst@AddToHook. – mhp Oct 01 '12 at 11:21\lst@Defis completely mysterious to me. – mhp Oct 01 '12 at 11:26