This code fits quite nicely my needs:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{lipsum}
\usepackage{hyperref}
\usepackage{lstlinebgrd}
\definecolor{myComment}{rgb}{0.0,0.5,0.0}
\definecolor{myKeyword}{rgb}{0.0,0.0,0.5}
\lstset
{
aboveskip=\bigskipamount,
belowskip=\bigskipamount,
frame=tb,
framesep=2pt,
basicstyle=\scriptsize\ttfamily,
keywordstyle=\color{myKeyword},
commentstyle=\color{myComment}\itshape,
captionpos=b,
showstringspaces=false,
fontadjust=true,
language=Delphi,
breaklines=true,
numbers=left,
numberstyle=\tiny,
linebackgroundcolor={\ifodd\value{lstnumber}\color{gray!25}\fi},
numbersep=5pt,
escapeinside={(*@}{@*)},
}
\newcommand\coderef[1]{%
$\Rightarrow$~\ref{#1}:~~%%
}%
\usepackage{hyperref}
\begin{document}
\begin{lstlisting}
program FileTest;
var
myFile: file of Integer;
begin
Assign(myFile, 'c:/test.ext'); (*@\label{FileTest-1}@*)
Rewrite (myFile); (*@\label{FileTest-2}@*)
Write(myFile, 1); (*@\label{FileTest-3}@*)
Write(myFile, 2);
Close(myFile); (*@\label{FileTest-4}@*)
end.
\end{lstlisting}
\coderef{FileTest-1} \lipsum[1]
\coderef{FileTest-2} \lipsum[2]
\coderef{FileTest-3} \lipsum[3]
\coderef{FileTest-4} \lipsum[4]
\end{document}
But in some cases it could be nice to repeat the whole line of code instead of the line number. I think that it's somewhat feasible with external source files, by using
\lstinputlisting[firstline=x,lastline=x]
though I haven't investigated further. But is it feasible with sources in a lstlisting environment? And how?


