# Manually break lines like auto-breaking in lstlisting

I would like to decide where the long line should break, but with actions pre-/postbreak and without advancing in line numbering.

I've tried that one:

\documentclass{article}
\usepackage{color, xcolor, listings}
\def\postbreak{\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookrightarrow\space}}}
\lstset{postbreak=\postbreak,
breaklines=true, breakatwhitespace=true,
basicstyle=\small\ttfamily,
xleftmargin=2em,xrightmargin=1em,
numbers=left,frame=single
}
\begin{document}
\begin{lstlisting}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent id mattis velit, ut ultricies massa.
In lacus libero, porttitor quis urna id.
\end{lstlisting}
\begin{lstlisting}[escapeinside={@}{@}]
Lorem ipsum dolor sit amet,@\newline\postbreak@ consectetur adipiscing elit.@\newline\postbreak@ Praesent id mattis velit,@\newline\postbreak@ ut ultricies massa.
In lacus libero, porttitor quis urna id.
\end{lstlisting}
\begin{lstlisting}
Lorem ipsum dolor sit amet,       consectetur adipiscing elit.        Praesent id mattis velit,           ut ultricies massa.
In lacus libero, porttitor quis urna id.
\end{lstlisting}
\end{document}


but it brokes line spacing and frames around listing.

The first box is what I get normally, second box is what I get, when I try to put linebreaks, and third is what I would like to get, but without placing spaces by hand in the listing (as it can broke, when some styles change)

-
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – Jubobs Mar 11 '14 at 12:06
Thank you for your comment, but both answers are not exactly what I need. But OK. I saw that the problem is connected with some bug in package. – faramir Mar 11 '14 at 15:21

Building on Mike Renfro's answer, you can use the mdframed package to produce the frame as the original one, not including line numbers:

\documentclass{article}
\usepackage{xcolor, listings}
\usepackage[framemethod=tikz]{mdframed}

\def\postbreak{%
\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookrightarrow\space}}}

\lstset{postbreak=\postbreak,
breaklines=true, breakatwhitespace=true,
basicstyle=\small\ttfamily,
xleftmargin=2em,xrightmargin=1em,
numbers=left
}

\mdfdefinestyle{myframe}
{
hidealllines=true,
singleextra={
\draw
([xshift=16pt]O|-P) --
([xshift=16pt]O) --
(P|-O) --
(P) -- cycle;
},
firstextra={
\draw
([xshift=16pt]O) --
([xshift=16pt]O|-P) --
(P) --
(P|-O);
},
secondextra={
\draw
([xshift=16pt]O|-P) --
([xshift=16pt]O) --
(P|-O) --
(P);
},
middleextra={
\draw
([xshift=16pt]O|-P) --
([xshift=16pt]O);
\draw
(P|-O) --
(P);
},
innerleftmargin=0pt,
innerrightmargin=3pt,
innertopmargin=0pt,
innerbottommargin=0pt,
rightmargin=7pt,
}
\surroundwithmdframed[style=myframe]{lstlisting}

\begin{document}

\begin{lstlisting}[escapeinside={@}{@}]
Lorem ipsum dolor sit amet,@\newline\postbreak@ consectetur adipiscing elit.@\newline\postbreak@ Praesent id mattis velit,@\newline\postbreak@ ut ultricies massa.
In lacus libero, porttitor quis urna id.
\end{lstlisting}

\end{document}


-
Thank you. Seems like I want. Now I saw, that the problem is not with my latex code, but with "frame=single" or "frame=lines". Why it is not working properly? Why it changes the height of lines? – faramir Mar 2 '14 at 16:40

Working from Formatting source listings with listings & framed packages, if you don't mind the frame going around the line numbers, you can use the framed package and keep your frames and linebreaks intact.

\usepackage{framed}


...

\begin{framed}
\begin{lstlisting}[escapeinside={@}{@}]
Lorem ipsum dolor sit amet,@\newline\postbreak@ consectetur adipiscing elit.@\newline\postbreak@ Praesent id mattis velit,@\newline\postbreak@ ut ultricies massa.
In lacus libero, porttitor quis urna id.
\end{lstlisting}
\end{framed}


-