Herbert gave a good answer for this if you can use the multicol
package. If you cannot, or don't want to, here's one way to do it. I'm not claiming that this is the best way to do it, or even that it works in all cases, but at least with my limited testing, it seems to work.
\documentclass[twocolumn]{article}
\usepackage{afterpage}
\usepackage{listings}
\usepackage{lipsum} % Just for dummy text.
\makeatletter
\newbox\loc@box
\newenvironment{longonecolumn}{%
\ifvoid\loc@box\else
\errmessage{Too many longonecolumn environments too
close together.}%
\fi
\global\setbox\loc@box\vbox\bgroup
\hsize\textwidth
\columnwidth\hsize
\linewidth\hsize
\@twocolumnfalse
}{%
\egroup
\afterpage\loc@placebox
}
\newcommand*\loc@placebox{%
\if@firstcolumn
\loc@placebox@
\else
\afterpage\loc@placebox
\fi
}
\newcommand*\loc@placebox@{%
\onecolumn
\begingroup
\loop\ifdim\ht\loc@box>\vsize
\setbox\z@\vsplit\loc@box to\vsize
\unvbox\z@
\vfill
\pagebreak
\repeat
\ifdim\ht\loc@box>\dbltopfraction\vsize
\unvbox\loc@box
\vfill
\twocolumn\relax
\else
\twocolumn[\unvbox\loc@box\vspace\dbltextfloatsep]%
\fi
\endgroup
\global\setbox\loc@box\box\voidb@x % Probably not needed
}
\makeatother
\begin{document}
\lipsum
\lipsum[1-3]
\begin{longonecolumn}
\begin{lstlisting}[caption=Here's my
caption,basicstyle=\ttfamily,numbers=left]
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
Here is a full width line of text; it just keeps going. Whee!!!
\end{lstlisting}
\end{longonecolumn}
\lipsum
\end{document}
The idea here is that the longonecolumn
environment packages its argument up into a single box \loc@box
. It then uses TeX's page breaking algorithm via \vsplit
to break the box into \vsize
sized chunks and then lets those chunks fit on the page naturally. After there is only a single chunk of size at most \vsize
, it checks if it fits in the amount of space double column figures are allowed to take up. If it doesn't, then the last chunk also gets its own page. Otherwise, it gets put at the top of a normal two column page in the same way that \maketitle
puts the title information at the top of the page.