In addition to the other answers, I'd like to share what I came up with when trying to answer a similar question asked on the LaTeX IRC channel. Defining a new environment for just breaking code lines at arbitrary points seems a bit unfortunate, as you lose all the nice extra features the listings package provides.
As mentioned in the comments to earlier answers, line breaking seems to depend on the listing's language setting. So it seems natural to just define a new, plain language style, without any keywords and such, which can break lines at arbitrary characters.
I couldn't find out how listings exactly determines possible line break positions. From playing with the alsoletter
/alsodigit
/alsoother
options, it seems that breaks are allowed only at the boundary of two different character classes; sequences of characters of the same class will never be broken, apparently. This means that we cannot use those options to define a new style but have to (mis)use the literate
trick instead:
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\makeatletter
\def\@tempA#1#2\@end{%
\@tempA@{#1}%
\ifx\relax#2\relax
\else
\@tempA#2\@end
\fi
}
\def\@tempA@#1{{\noexpand#1}{{\char`\noexpand#1 \allowbreak}}1 }
\edef\@tempB{\noexpand\lstdefinelanguage{logfile}{%
columns=fixed,%
keepspaces=true,%
breaklines=true,%
literate=\@tempA 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!()*+,-./:;<=>?@[]_|`^"'\&\$\\\~\#\%\{\}\@end
}}
\@tempB
\makeatother
\begin{document}
\newlength\lstbasewidth
\settowidth\lstbasewidth{\ttfamily\small X}
\lstset{
basicstyle=\ttfamily\small,
language=logfile,
breakindent=4\lstbasewidth,
basewidth=\lstbasewidth,
postbreak=\llap{\scriptsize\textcolor{blue}{$\hookrightarrow$}\kern0.25em}
}
\begin{lstlisting}
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !"#$%&'()*+,-./
0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !"#$%&'()*+,-./0123456789:;<=>?
@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
\end{lstlisting}
\end{document}
Output

The example defines new language style logfile
(showing log files with long lines in it was what was originally asked for in the channel). The idea is quite simple: Map each printable character x
to an entry {x}{{\char`x \allowbreak}}1
in the literate
option list. To avoid a lot of boilerplate code, we use the \@tempA
macro that traverses a list of characters and expands each to the given entry format, and \@tempB
to finally define the new style with the fully expanded entry list.
The setup that happens in the document body just adds some visual indications where line breaks happened, with arrows and a fixed indentation width of 4 characters. Note that the style hasn't been tested well yet, especially in conjunction with different packages or extended character sets.
\lstset{breaklines=true,breakatwhitespace=false}
should have worked, but don't seem to me for some reason.