[Tex/LaTex] Visual separation when using linerange in listings

listings

I have read through the listings documentation and couldn't find an answer to the following question:
How do I insert some sort of visual feedback in a listings listing when using the linerange option. It works fine on my source code files, but the only visible feedback that there is a gap in the listed code is the change in numbering. I would like some more visual feedback, e.g. a line or maybe some sort of waves that I come across in some books on programming.
Any idea on how to achieve this?

Best Answer

One way to do this is to split the listings for each line range, and place an ornament in between them. There are many options as to what to use as a separator. To illustrate, I have used an ornament from this question in between the listings and another from this question on how to insert a border at the end of the listings.

enter image description here

Here is the code:

\documentclass{article}
\usepackage{filecontents}
\usepackage{listings}

%------------------------ Used for ornament between listings
% Ornament from https://tex.stackexchange.com/questions/11320/end-of-paragraph-with-ornament
\usepackage{pifont,fourier-orns}% These are needed only for the ornament 

\newcommand\crulefill[1][1ex]{\leavevmode\leaders\hrule depth \dimexpr-#1+0.4pt height #1\hfill\kern0pt}
\newcommand\ornline[2][1ex]{\trivlist\item\crulefill[#1]#2\crulefill[#1]\endtrivlist}

%------------------------ Used for ornament at end of listings
%https://tex.stackexchange.com/questions/30973/how-do-i-insert-a-border-below-text/30979#30979
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{xparse}% http://ctan.org/pkg/xparse
\NewDocumentCommand{\myrule}{O{1pt} O{3pt} O{black}}{%
  \par\nobreak % don't break a page here
  \kern\the\prevdepth % don't take into account the depth of the preceding line
  \kern#2 % space before the rule
  {\color{#3}\hrule height #1 width\hsize} % the rule
  \kern#2 % space after the rule
  \nointerlineskip % no additional space after the rule
}
%------------------------

\begin{filecontents*}{foo.java}
 public int nextInt(int n) {
     if (n<=0)
        throw new IllegalArgumentException("n must be positive");

     if ((n & -n) == n)  // i.e., n is a power of 2
         return (int)((n * (long)next(31)) >> 31);

     int bits, val;
     do {
         bits = next(31);
         val = bits % n;
     } while(bits - val + (n-1) < 0);
     return val;
 }
\end{filecontents*}

\lstdefinestyle{MyListStyle} {numbers=left, language=Java}

\begin{document}
\lstinputlisting[style=MyListStyle,linerange={2-6},firstnumber=2]{foo.java}
\ornline[0.6ex]{\decoone}

\lstinputlisting[style=MyListStyle,linerange={9-11},firstnumber=9]{foo.java}
\myrule[5pt][5pt][orange]
\end{document}