I am trying to customize a listings style to automatically highlight my code exactly (or sufficiently close to) how my installation of Eclipse/PyDev does. I would like to be able to use "class" and ":" as a set of delimiters, such that anything found between "class" and ":" will be formatted in a certain way. Specifically, I want "class" to have the "keyword" color, everything between "class" and ":" to be either the "classc" color or to inherit its formatting from other definitions ("Base" should be "classc"-colored and "(object)" should be "code"-colored).
I have attempted to achieve this by implementing a macro that reformats the text between the delimiters while also reprinting the delimiters with different formatting.
The first line of the given output is almost exactly what I desire, except that for some reason the ":" is appearing between Base and (object) instead of after (object). That is, the coloring is correct, but the line should be "class Base(object):".
The indention in the second line of output seems to be causing another problem. Again, the coloring is mostly correct here, but I expect the output to be "def new_function(self):". Technically, I also want "self" to have the "keyword" coloring, but I assume that's a different issue.
\documentclass{article}
\usepackage{listings, color}
\definecolor{keyword}{RGB}{221,40,103}
\definecolor{classc}{RGB}{18,144,195}
\definecolor{code}{RGB}{217,232,247}
\definecolor{bg}{RGB}{35,35,35}
\definecolor{function}{RGB}{167,236,33}
\newcommand{\classHighlight}[1]{{\ttfamily\color{keyword}class\ }{\bfseries\color{classc}#1}{:}}
\newcommand{\functionHighlight}[1]{{\ttfamily\color{keyword}def\ }{\bfseries\color{function}#1}{:}}
\lstdefinestyle{myPython}{
language = Python,
basicstyle = \color{code}\ttfamily,
morekeywords=[1]{self,None,True,False,class,def},
moredelim={*[is][\classHighlight]{class\ }{:}},
moredelim={*[is][\functionHighlight]{def\ }{:}},
moredelim={*[s][\color{code}]{(}{)}},
backgroundcolor = \color{bg},
}
\lstset{style=myPython}
\begin{document}
\begin{lstlisting}
class Base(object):
def new_function(self):
function contents
\end{lstlisting}
\end{document}
When I remove the "i" tags and run with the following lines, I see output that I find even more confusing:
moredelim={*[s][\classHighlight]{class\ }{:}},
moredelim={*[s][\functionHighlight]{def\ }{:}},