My question is very similar to Peter Grill's, How to detect a tab character when processing a literate in lstlistings?
I'm trying to count the number of ^^M characters encountered in my listing behind the scenes, using literate as follows:
literate={\^^M}{{\ProcessNewline}}1,
where the whole purpose of \ProcessNewline is to increment a counter and insert a ^^M character (in order to effect a line break in the listing as if I wasn't messing around with literate at all).
Incrementing the counter is not a problem, but my attempts at inserting a ^^M character back have been infructuous. What I think was my best shot is shown in the code below, but I get a TeX capacity exceeded error.
What am I doing wrong and what should I do?
\documentclass{article}
\usepackage{listings}
\newcounter{foo}
\setcounter{foo}{0}
% the following four lines are adapted from Martin Scharrer's lstautogobble package
\begingroup
\catcode `\^^M=\active%
\gdef\activenl{^^M}% Active CR (ASCII 13) ↙character which is used as line break
\endgroup
\newcommand*{\ProcessNewline}{%
\stepcounter{foo}%
% \activenl% You'll get an error if you uncomment his line
}%
\lstset{%
literate={\^^M}{{\ProcessNewline}}1,
}
\begin{document}
\begin{lstlisting}
one
two
three
\end{lstlisting}
\end{document}