# File rtex.rb, line 504
        def listings_mix_references( template = @text )
                #This method scan a program file for the string TeXlabel{<label>} or LABEL{<label>} and stores the linenumber.
                #The string can be redefined by a regular expression, $1 must be the label.
                def scan_source( source, r = /[(TeXlabel)|(LABEL)]\{(.*?)?\}/ )
                        Logger.add(4, self.class, "Read File #{source} for cross-references")
                        labels       = Hash.new( 0 )
                        if !File.exist?( source )
                                Logger.add(3, self.class,  "Sourcecode #{source} nicht gefunden" )
                                return labels
                        end
                        #extend this Hash with a method get_label
                        File.open(source){ |f|
                                linenumber = 0
                                f.each{|l|
                                        linenumber += 1
                                        while l =~ r       #Perhaps there are mor then one label on a line
                                                labels[$1] = linenumber
                                                l.sub!( r ,'')    #delete label for next Regex-check
                                        end
                        #Idee für ABAP: FORM/Endform automatisch mit Labels versehen?
                                }
                        }
                        Logger.add(5, self.class,  "Found labels in #{source}: #{labels.inspect}")
                        return labels
                end 
                def get_label( hash, key, source )
                        pos = hash.fetch(key, 0)
                        Logger.add(3, self.class,  "Label #{key} not found in #{source}") if pos == 0
                        return pos
                end
                #If this script is running more the once, the inputlistings would increase.
                #So we clear it here to a "virgin" state.
                @text.gsub!( 
                        /%(lstinputlisting\[.*?\]\{(.*?)\})\n.lstinputlisting\[.*?\]\{.*?\}/m,
                        '\\\\\1'
                        )
                #\ABAPinput is a macro used in a privat package of the author ;-)
                @text.gsub!( 
                        /%(ABAPinput\[.*?\]\{(.*?)\})\n.ABAPinpu\[.*?\]\{.*?\}/m,
                        '\\\\\1'
                        )
                
                sources = Hash.new()  #all Sources
                #Replace all from/tolabe-listings with the new  first/lastline version.
                #Remember the original labels in a TeX-comment (needed next time, the programm could change.
                r = Regexp.new('\\\\(lstinputlisting|ABAPinput)\[.*(?:fromlabel=(.*?))(,.*)*(?:tolabel=(.*?))(,.*)*\]{(.*)}')
                while m = r.match(@text)
                        old  = m[0]
                        source = m[6]
                        Logger.add(5, self.class, "Found lstinputlisting #{source}")
                        if ! sources.has_key?(source)
                                sources[source] = scan_source( source )
                        end
                        pos1 = get_label(sources[source], m[2], source ) + 1
                        pos2 = get_label(sources[source], m[4], source ) - 1
                        new  = '\\' + m[1] + '[' + 'firstline=' + pos1.to_s + m[3] +
                                        'lastline=' + pos2.to_s + m[5].to_s + "]{" + source  + '}'
                        Logger.add(5, self.class,  "Command old:\t#{old}\nCommand new\t#{new}" )
                        @text = m.pre_match + "%#{old[1..-1]}\n" +   new + m.post_match
                end
                return @text
        end