place symbol or some command on page break within lstlisting [duplicate]

Whenever I have a page break splitting my lstlisting on different pages, I would like to have a symbol or generally spoken some command inserted automatically. I can accomplish that manually by using the lstlisting's escape character:

\begin{lstlisting}[language=cs]
class MyClass
{
// assume this is on one page

´
\begin{flushright}
$\blacktriangledown$
\end{flushright}
´

// assume this is on the other page
}
\end{lstlisting}


However, when I add enough text before the listing, the symbol won't be placed at the right spot anymore. It will get moved to the next page where it doesn't make sense anymore. To tackle that I need some dynamic placement of some user-defined latex to be inserted right at the spot where a page break splits my lstlisting apart.

Update: There is a formfeed Parameter that can be set within lstset. It defaults to \bigbreak and even though inserting \bigbreak in escaped mode within the listing results in the same visual appearance of a split of the listing like it happens at page breaks, changing formfeed doesn't make a change in the output, really unfortunately.

Is there a solution to it?

-

marked as duplicate by clemens, Werner, mafp, barbara beeton, egregJun 5 '13 at 20:50

Can you transform your code snippet into a minimal working example (MWE)? –  hpesoj626 Feb 28 '13 at 22:11
Possible duplicate: "How to show a hint when lstlisting is breaking page?" –  Heiko Oberdiek Jun 5 '13 at 17:38

To signify a code listing continued from the prior page, I get my MWE to stick a \nabla (inverted triangle) at the right top of a page where the lstlisting breaks across a page boundary. I had to use a David Carlisle trick to make afterpage and listings play nice together (empty page afterpage listings). I also temporarily modify the definition of \thepage to put the same symbol at the end of the page where the listing breaks.

However, in both cases, the user must choose to insert them, only if he notices the code breaking across a page. So, in that sense, they are not automated. However, it is an improvement over what the questioner started with, in that he had to literally place his symbol at the exact line of the page break to get the desired look. At least with this solution, adding one line of text will not, generally, screw up the result, as it would with his original approach.

\documentclass{article}
\usepackage{afterpage}
\usepackage{listings}
\let\svthepage\thepage
\begin{document}

Normal text

% http://tex.stackexchange.com/questions/78794/empty-page-afterpage-listings
\def\thepage{\hfill\svthepage\hfill$\nabla$}
\setbox0\vbox{
\afterpage{\hfill$\nabla$}
\begin{lstlisting}%[language=cs]
class MyClass
{
// assume this is on one page

// assume this is on one page

// assume this is on one page

// assume this is on one page

// assume this is on one page

// assume this is on one page

// assume this is on one page

// assume this is on one page

// assume this is on one page

// assume this is on one page

// assume this is on one page

// assume this is on the other page

// assume this is on the other page

// assume this is on the other page
}
\end{lstlisting}
}
\unvbox0

\let\thepage\svthepage

Back to normal text

\end{document}


-