colouring line by line ?

Michael Smith smithm at netapps.com.au
Tue Jun 23 10:41:11 CEST 2009


On Tue, 23 Jun 2009 09:49:27 +0200
Jenô Balaskó <jeno.balasko at ericsson.com> wrote:

> Hi,
> 
> we developed a cource code coverage analyzer program. It lists for all file the coverage line by line.
> e.g foo.txt:1525:20  means line 1525 was used 20 times. It would be useful to read the file coloured according this result, e.g  the not used lines would be grey, the very often used lines green.
> This is our business, I'm interested only if there is any support in nedit for this kind of problem.
> 
> Thanks in advance
> Attila Jeno Balasko
> 

I have a set of scripts and macro files which highlight differences against cvs and hg. They could be adapted to your requirements.

This script invokes nc and checks for differences against the version control system. It accepts arguments of the form file:line which is output by grep -n


#! /bin/ksh
# name: nb
for i in $*; 
do
    file_name=`echo $i | awk -v FS=: '{ print $1 }' -`
    line_number=`echo $i | awk -v FS=: '{ print $2 }' -`
    
    if [ -d $file_name ]
    then
        echo $file_name "is a directory"
    else    
        diff=`$NEDIT_HOME/get-cvs-diff $file_name`
#        echo $diff

        if [ -z "$line_number" ]
        then
            param=$diff
        else
            param=$diff"+l_"$line_number"_0"
        fi

        if [ ! -z "$param" ]
        then
            cmd="-do mark_diff(\"$param\")"
        else
            cmd=
        fi

#        echo "["$cmd"]"
        $NEDIT_HOME/nc $cmd $file_name
    fi
done

This script outputs encoded diff information:

#! /bin/ksh
# name: get-cvs-diff
dir=`echo $1 | awk -v FS=/ '{ for(i=1;i<NF;i++) printf("%s/",$i) }' -`
cvsdir=$dir"CVS"
if [ -d $cvsdir ]
then    
    cvs status $1 | egrep "Needs Merge|Locally Modified" > /dev/null
    if [ $? -eq 0 ]
    then
        MACHINE_KIND=`uname -s`
        if [ "$MACHINE_KIND"=="OSF1" ]
        then
            tmpfile="/tmp/np.tempfile"
        else
            tmpfile=`mktemp /tmp/np.XXXXXX`
        fi
        awk -f $NEDIT_HOME/line-starts.awk $1 > $tmpfile
        cvs diff $1 | egrep -v "[\<\>\-]" | egrep "^[0-9].*" >> $tmpfile
        awk -f $NEDIT_HOME/analyse-change.awk $tmpfile
        rm -f $tmpfile
    fi
else
    if [ -x `which hg` ]
    then
        hg root > /dev/null
        if [ $? -eq 0 ]
        then
            MACHINE_KIND=`uname -s`
            if [ "$MACHINE_KIND"=="OSF1" ]
            then
                tmpfile="/tmp/np.tempfile"
            else
                tmpfile=`mktemp /tmp/np.XXXXXX`
            fi
            awk -f $NEDIT_HOME/line-starts.awk $1 > $tmpfile
            hg diff $1 >> $tmpfile
            awk -f $NEDIT_HOME/analyse-change-hg.awk $tmpfile
#            cat $tmpfile
            rm -f $tmpfile
        fi
    fi
fi

This is line-starts.awk. Highlighting works per character so you need to associate lines with character indexes.

BEGIN {
    total = 0
}

{
    printf("line-start,%s,%s\n",FNR,total)
    total = total + length + 1
}

END {
    printf("line-start,%s,%s\n",FNR+1,total)
}

This is analyse-change.awk. It checks for diffs against cvs and outputs
stuff which is eaten by an nedit macro.

BEGIN {
    FS=","
}

/line-start[,][0-9]*[,][0-9]*/ {
#    print $2 " " $3
    line_starts[$2]=$3
}

/[0-9]*[,][0-9]*[c][0-9]*[,][0-9]*/ {
    n = split($2,range,"c")
    printf ("+c_%d_%d",line_starts[range[2]],line_starts[$3 + 1])
}

#980,984c988
/^[0-9]*[,][0-9]*[c][0-9]*$/ {
    n = split($2,range,"c")
    printf ("+c_%d_%d",line_starts[range[2]],line_starts[range[2]+1])
}

#1676a1692
/^[0-9]*[a][0-9]*$/ {
    n = split($1,range,"a")
    printf ("+a_%d_%d",line_starts[range[2]],line_starts[range[2]+1])
}

#193a194,197
/^[0-9]*[a][0-9]*[,][0-9]*$/ {
    n = split($1,range,"a")
    printf ("+a_%d_%d",line_starts[range[2]],line_starts[$2 + 1])
}

#846c853,867
/^[0-9]*[c][0-9]*[,][0-9]*$/ {
    n = split($1,range,"c")
    printf ("+c_%d_%d",line_starts[range[2]],line_starts[$2 + 1])
}

#457c504
/^[0-9]*[c][0-9]*$/ {
    n = split($1,range,"c")
    printf ("+c_%d_%d",line_starts[range[2]],line_starts[range[2]+1])
}

function print_count()
{
    if(inplus == 1)
    {
        if(minuslines > 0)
        {
            printf
("+c_%d_%d",line_starts[plusstart+1],line_starts[plusstart+minuslines+1])
minuslines = 0 }
        printf
("+a_%d_%d",line_starts[plusstart+minuslines+1],line_starts[plusstart+pluslines+1])
inplus = 0 }
}

This is the version for hg


BEGIN {
    FS=","
}

/line-start[,][0-9]*[,][0-9]*/ {
#    print $2 " " $3
    line_starts[$2]=$3
    count = 0
    next
}

# Add 6 lines after 109
#@@ -109,6 +109,10 @@
#/[@@]{2}\s-[0-9]+[,][0-9]+ +[0-9]+[,][0-9]+\s[@@]{2}/ {

/^[\@]+[.]*[\@]+/ {
#    print "match: " $0
    split($0,fields," ")
    n = split(fields[2],range1,"[,]")
    n = split(fields[3],range2,"[,]")
#    print "0: " $0
#    print "fields[2]: " fields[2]
#    print "fields[3]: " fields[3]
#    print "range1[2]: " range1[2]
 #   print "range2[1]: " range2[1]
    lines = range1[2] + 0
    start = range2[1] + 0
    line = range2[1] - 1
    count = range2[2];
    minuslines = 0
#    printf("count: %d\n",count)
#    printf ("+c_%d_%d\n",line_starts[start],line_starts[start+lines])
}

/^[\+]+/ {
    if(count > 0)
    {
        if(inplus == 0)
        {
            inplus = 1
            plusstart = line
            pluslines = 1
        }
        else
        {
            pluslines++
        }
#        print "PLUS: line " line " " $0
    }
    line++
}

/^[\-]+/ {
    if(count > 0)
    {
        minuslines++
    }
}

/^[\ ]+/ {
    print_count()
    line++
}

{
#    print "count: " count
#    print "inplus: " inplus
#    print "plusstart: " plusstart
#    print "pluslines: " pluslines
    if(count > 0)
    {
        count--
    }
    else
    {
        print_count()
    }
}

END {
    print_count()
}

This is highlight.nm. It defines the mark_diff function which parses the output of the above scripts and puts backlight into the window.

# Calculate line start points
define calculate_line_starts
{
    $line_starts = $empty_array
    line_number = 1
    $line_starts[line_number] = 0
    end_line = 0
    while(end_line != -1)
    {
        end_line = search("^.*$",$line_starts[line_number] + 1,"regex")
        if(end_line != -1)
        {
            line_number = line_number + 1
            $line_starts[line_number] = end_line
        }
    }
}

#Jump to the specified line
define mark_line
{
    line_command=$1 ",0"
    goto_line_number(line_command)
    end=search("$",$cursor,"regex")
    nextline=search("^",end,"regex")
    if(nextline == -1) nextline = end
    rangesets=rangeset_get_by_name("entry-points")
    if(rangesets[] == 0)
    {
        rangeset=rangeset_create()
        rangeset_set_name(rangeset,"entry-points")
        rangeset_set_color(rangeset,"LightBlue")
    }
    else
    {
        rangeset=rangesets[0]
    }
    rangeset_add(rangeset,$cursor,nextline)
}

#mark differences
define mark_diff
{
    addra=rangeset_get_by_name("addr")
    cddra=rangeset_get_by_name("cddr")
    rangeset_destroy(addra)
    rangeset_destroy(cddra)
    if(length($1) > 0)
    {
        addr = rangeset_create()
        cddr = rangeset_create()
        rangeset_set_name(addr,"addr")
        rangeset_set_name(cddr,"cddr")
        rangeset_set_color(addr,"LightYellow1")
        rangeset_set_color(cddr,"LightPink")
        lines=split($1,"+")
        for(line in lines)
        {
            details=split(lines[line],"_")
            if(details[0] == "a")
            {
                rangeset_add(addr,details[1],details[2])
            }
            if(details[0] == "c")
            {
                rangeset_add(cddr,details[1],details[2])
            }
            if(details[0] == "l")
            {
                mark_line(details[1])
            }
        }
    }
}

#Search for blocks to be hidden in the input file and fold them
define hide_blocks
{
    rangeset_defined = 0
    start_block = 0
    end_block = 0
    while(start_block != -1 && end_block != -1)
    {
        start_block = search("--CONTENT*PROTECTED",end_block)
        if(start_block != -1)
        {
            end_block = search("--CONTENT*UNPROTECTED",start_block)
            if(end_block == -1)
            {
                loop = start_block
                while(loop != -1)
                {
                    loop = search("^.*$",loop + 1,"regex")
                    if(loop != -1)
                    {
                        end_block = loop
                    }
                }
            }
            if(end_block != -1)
            {
                next_line = search("^.*$",end_block + 1,"regex")
                if(next_line == -1)
                {
                    next_line = end_block
                }
                if(rangeset_defined == 0)
                {
                    r = rangeset_create()
                    rangeset_defined = 1
                }
                rangeset_add(r,start_block,next_line)
            }
        }
    }
    if(rangeset_defined != 0)
    {
        rangeset_set_color(r,"violet")
        rangeset_set_folded(r,1)
    }
}


Clearly I had a lot more time on my hands when I wrote this stuff.
-- 
Michael Smith
Network Applications
www.netapps.com.au   | +61 (0) 416 062 898
Web Hosting          | Internet Services
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.nedit.org/pipermail/discuss/attachments/20090623/743d1859/signature.bin


More information about the Discuss mailing list