I don't know of an automatic way to fix the issue, but it is not hard to find a semi-automatic one. Look the following example, which suitable modifications of yours:
Code: Select all
\documentclass{book}
\usepackage{listings}
\lstset{%
language=[Sharp]C,
frame=single,
numbers=left,
breaklines=true,
breakautoindent=true,
xleftmargin={\ifodd\value{page}1em\else-\marginparwidth\fi},
xrightmargin={\ifodd\value{page}-\marginparwidth\else1em\fi}%
}
% page layout setting
\usepackage[
paperwidth=15cm,
paperheight=10cm,
twoside,
outer=15mm,
inner=15mm,
includemp=true,
marginparwidth=30mm,
marginparsep=5mm%
]{geometry}
%to show the boundary of textarea, marginsep area, and marginpar area
\newcommand*{\MARKER}{%
{\noindent\strut\vrule\hrulefill~half~\hrulefill\vrule\hrulefill~half~\hrulefill\vrule}%
\marginpar{\strut\vrule\hrulefill~marginpar~\hrulefill\vrule}}
\newcommand\StartNewPage{\end{lstlisting}
\newpage
\begin{lstlisting}[name=Longlisting,frame=lr,escapechar=\%]
}
\newcommand\StartLastPage{\end{lstlisting}
\newpage
\begin{lstlisting}[name=Longlisting,frame=blr,escapechar=\%]
}
\begin{document}
\MARKER
\begin{lstlisting}[name=Longlisting,firstnumber=1,frame=tlr,escapechar=\%]
using System;
public class Foo
{
public static void Main()
{
Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
Console.WriteLine("However, is there any better typesetting system?");
Console.WriteLine("No, there isn't.");
Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
Console.WriteLine("However, is there any better typesetting system?");
Console.WriteLine("No, there isn't.");%\StartNewPage%
Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
Console.WriteLine("However, is there any better typesetting system?");
Console.WriteLine("No, there isn't.");
Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
Console.WriteLine("However, is there any better typesetting system?");
Console.WriteLine("No, there isn't.");
Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
Console.WriteLine("However, is there any better typesetting system?");
Console.WriteLine("No, there isn't.");%\StartNewPage%
Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
Console.WriteLine("However, is there any better typesetting system?");
Console.WriteLine("No, there isn't.");
Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
Console.WriteLine("However, is there any better typesetting system?");
Console.WriteLine("No, there isn't.");
Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
Console.WriteLine("However, is there any better typesetting system?");
Console.WriteLine("No, there isn't.");%\StartLastPage%
Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
Console.WriteLine("However, is there any better typesetting system?");
Console.WriteLine("No, there isn't.");
}
}
\end{lstlisting}
\MARKER
\end{document}
As you can see, you have to insert \StartNewPage and \StartLastPage, followed by an empty line, at points where you want a page break. Start the lstlisting environment with the options used above. If % is a character included in the code, change it by other suitable character.
It is not an optimal solution, but at least it works.
Perhaps you may contact the author of the listings package and request a "twoside" option, so the values of xleftmargin and xrightmargin are reversed at each page break.