Eskil

Diff
Login

Differences From Artifact [a8e9684bb6]:

To Artifact [2c6fee35ef]:


14
15
16
17
18
19
20

21

22
23
24
25
26
27
28


29
30
31
32
33
34
35
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28


29
30
31
32
33
34
35
36
37







+

+





-
-
+
+







#----------------------------------------------------------------------
# the next line restarts using wish \
exec wish "$0" "$@"

package provide app-diff 1.0
package require Tk
catch {package require textSearch}

if {[catch {package require psballoon}]} {
    # Add a dummy if it does not exists.
    proc addBalloon {args} {}
} else {
    namespace import -force psballoon::addBalloon
}

set debug 0
set diffver "Version 1.9.8  2003-08-13"
set debug 1
set diffver "Version 1.9.8+  2003-08-19"
set tmpcnt 0
set tmpfiles {}
set thisscript [file join [pwd] [info script]]
set thisdir [file dirname $thisscript]
set ::diff(cvsExists) [expr {![string equal [auto_execok cvs] ""]}]
set ::diff(diffexe) diff
set ::diff(thisexe) [list [info nameofexecutable] $thisscript]
47
48
49
50
51
52
53
54

55
56
57
58
59
60
61
49
50
51
52
53
54
55

56
57
58
59
60
61
62
63







-
+







        set ::diff(tmpdir) c:/
    } else {
        set ::diff(tmpdir) .
    }
}

# Support for FreeWrap.
if {[info proc ::freewrap::unpack] != ""} {
if {[info procs ::freewrap::unpack] != ""} {
    set debug 0
    set thisdir [pwd]
    set thisscript ""
    set ::diff(thisexe) [list [info nameofexecutable]]
    # If diff.exe is wrapped, copy it so we can use it.
    set apa [::freewrap::unpack /diff.exe]
    if {$apa != ""} {
69
70
71
72
73
74
75
76






















77
78
79
80
81
82
83
71
72
73
74
75
76
77

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106







-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    if {!$::diff(cvsExists) && [file exists "c:/bin/cvs.exe"]} {
        set env(PATH) "$env(PATH);c:\\bin"
        auto_reset
        set ::diff(cvsExists) [expr {![string equal [auto_execok cvs] ""]}]
    }
}

proc cleanupAndExit {} {
proc cleanupAndExit {top} {
    if {[catch {
        if {$top != "all"} {
            set i [lsearch $top $::diff(diffWindows)]
            if {$i >= 0} {
                set ::diff(diffWindows) [lreplace $::diff(diffWindows) $i $i]
            }
            destroy $top
            array unset ::diff $top,*
            
            # Any windows remaining?
            if {[llength $::diff(diffWindows)] > 0} {
                return
            }
        }
    } errMsg]} {
        # A security thing to make sure we can exit.
        tk_messageBox -icon error -title "Diff Error" -message \
                "An error occured in the close process.\n$errMsg\n\
                (This is a bug)\nTerminating application." -type ok
    }

    if {$::diff(diffexe) != "diff"} {
        catch {file delete $::diff(diffexe)}
    }
    cleartmp
    exit
}

220
221
222
223
224
225
226

227
228
229
230
231
232
233
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257







+








        set res1 [concat $left1 [list $mid1] $right1]
        set res2 [concat $left2 [list $mid2] $right2]
    }
}

# Experiment using DiffUtil
##syntax compareLinesX x x n n x?
proc compareLinesX {line1 line2 res1Name res2Name {test 0}} {
    global Pref
    upvar $res1Name res1
    upvar $res2Name res2

    set args "$Pref(ignore)\
            [expr {($Pref(lineparsewords) && !$test) ? "-word" : ""}]"
630
631
632
633
634
635
636
637
638


639
640
641
642

643
644
645

646
647

648
649

650
651

652
653
654
655
656

657
658
659
660
661
662


663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686





687
688
689
690
691
692
693
694
695
696

697
698

699
700
701


702
703
704
705
706


707
708
709
710
711


712
713
714


715
716
717
718
719
720
721
722

723
724
725
726
727
728
729
730
731
732
733

734
735
736
737
738
739
740
741
742
743

744
745
746


747
748

749
750
751
752
753
754
755
756

757
758
759


760
761
762
763
764
765

766
767
768


769
770
771
772
773
774
775
776
777
778
779
780
781

782
783
784
785
786
787
788
789
790
791
792
793


794
795
796
797
798
799
800

801
802
803
804
805
806
807
808

809
810
811
812
813
814
815
654
655
656
657
658
659
660


661
662
663
664
665

666
667
668

669
670

671
672

673
674

675
676
677
678
679

680
681
682
683
684


685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705





706
707
708
709
710
711
712
713
714
715
716
717
718
719

720
721

722
723


724
725
726
727
728


729
730
731
732
733


734
735
736


737
738
739
740
741
742
743
744
745

746
747
748
749
750
751
752
753
754
755
756

757
758
759
760
761
762
763
764
765
766

767
768


769
770
771

772
773
774
775
776
777
778
779

780
781


782
783
784
785
786
787
788

789
790


791
792
793
794
795
796
797
798
799
800
801
802
803
804

805
806
807
808
809
810
811
812
813
814
815


816
817
818
819
820
821
822
823

824
825
826
827
828
829
830
831

832
833
834
835
836
837
838
839







-
-
+
+



-
+


-
+

-
+

-
+

-
+




-
+




-
-
+
+



















-
-
-
-
-
+
+
+
+
+









-
+

-
+

-
-
+
+



-
-
+
+



-
-
+
+

-
-
+
+







-
+










-
+









-
+

-
-
+
+

-
+







-
+

-
-
+
+





-
+

-
-
+
+












-
+










-
-
+
+






-
+







-
+







            incr t2
        }
    }
    return $apa
}

# Insert lineno and text
proc insert {n line text {tag {}}} {
    .ft$n.tt insert end "$text\n" $tag
proc insert {top n line text {tag {}}} {
    $::diff($top,wDiff$n) insert end "$text\n" $tag
    if {$tag != ""} {
        set tag "hl$::HighLightCount $tag"
    }
    .ft$n.tl insert end [myforml $line] $tag
    $::diff($top,wLine$n) insert end [myforml $line] $tag
}

proc emptyline {n {highlight 1}} {
proc emptyline {top n {highlight 1}} {
    if {$highlight} {
        .ft$n.tl insert end "\n" hl$::HighLightCount
        $::diff($top,wLine$n) insert end "\n" hl$::HighLightCount
    } else {
        .ft$n.tl insert end "\n"
        $::diff($top,wLine$n) insert end "\n"
    }
    .ft$n.tt insert end "\n"
    $::diff($top,wDiff$n) insert end "\n"
}

# Insert one line in each text widget.
# Mark them as changed, and optionally parse them.
proc insertMatchingLines {line1 line2} {
proc insertMatchingLines {top line1 line2} {
    global doingLine1 doingLine2 Pref

    if {$::diff(filter) != ""} {
        if {[regexp $::diff(filter) $line1]} {
            insert 1 $doingLine1 $line1
            insert 2 $doingLine2 $line2
            insert $top 1 $doingLine1 $line1
            insert $top 2 $doingLine2 $line2
            incr doingLine1
            incr doingLine2
            set ::diff(filterflag) 1
            return
        }
        set ::diff(filterflag) 0
    }

    if {$Pref(parse) != 0} {
        compareLines $line1 $line2 res1 res2
        if {$::diff(diffutil)} {
            compareLinesX $line1 $line2 xres1 xres2
            if {$res1 != $xres1 || $res2 != $xres2} {
                tk_messageBox -title Mismatch! \
                        -message ":$res1:\n:$res2:\n:$xres1:\n:$xres2:"
            }
        }
        set dotag 0
        set n [maxabs [llength $res1] [llength $res2]]
        .ft1.tl insert end [myforml $doingLine1] "hl$::HighLightCount change"
        .ft2.tl insert end [myforml $doingLine2] "hl$::HighLightCount change"
        set new1 new1
        set new2 new2
        set change change
        $::diff($top,wLine1) insert end [myforml $doingLine1] "hl$::HighLightCount change"
        $::diff($top,wLine2) insert end [myforml $doingLine2] "hl$::HighLightCount change"
        set new1 "new1"
        set new2 "new2"
        set change "change"
        foreach i1 $res1 i2 $res2 {
            incr n -1
            if {$dotag} {
                if {$n == 1 && $Pref(marklast)} {
                    lappend new1 last
                    lappend new2 last
                    lappend change last
                }
                if {$i1 == ""} {
                    .ft2.tt insert end $i2 $new2
                    $::diff($top,wDiff2) insert end $i2 $new2
                } elseif {$i2 == ""} {
                    .ft1.tt insert end $i1 $new1
                    $::diff($top,wDiff1) insert end $i1 $new1
                } else {
                    .ft1.tt insert end $i1 $change
                    .ft2.tt insert end $i2 $change
                    $::diff($top,wDiff1) insert end $i1 $change
                    $::diff($top,wDiff2) insert end $i2 $change
                }
                set dotag 0
            } else {
                .ft1.tt insert end $i1
                .ft2.tt insert end $i2
                $::diff($top,wDiff1) insert end $i1
                $::diff($top,wDiff2) insert end $i2
                set dotag 1
            }
        }
        .ft1.tt insert end "\n"
        .ft2.tt insert end "\n"
        $::diff($top,wDiff1) insert end "\n"
        $::diff($top,wDiff2) insert end "\n"
    } else {
        insert 1 $doingLine1 $line1 change
        insert 2 $doingLine2 $line2 change
        insert $top 1 $doingLine1 $line1 "change"
        insert $top 2 $doingLine2 $line2 "change"
    }
    incr doingLine1
    incr doingLine2
}

# Insert two blocks of lines in the compare windows.
# Returns number of lines used to display the blocks
proc insertMatchingBlocks {block1 block2} {
proc insertMatchingBlocks {top block1 block2} {
    global doingLine1 doingLine2

    set apa [compareBlocks $block1 $block2]

    set t1 0
    set t2 0
    foreach c $apa {
        if {$c == "c"} {
            set textline1 [lindex $block1 $t1]
            set textline2 [lindex $block2 $t2]
            insertMatchingLines $textline1 $textline2
            insertMatchingLines $top $textline1 $textline2
            incr t1
            incr t2
        }
        if {$c == "C"} {
	    # This is two lines that the block matching considered
	    # too different to use line parsing on them.
	    # Marked the whole line as deleted/inserted
            set textline1 [lindex $block1 $t1]
            set textline2 [lindex $block2 $t2]
            .ft1.tl insert end [myforml $doingLine1] \
            $::diff($top,wLine1) insert end [myforml $doingLine1] \
                    "hl$::HighLightCount change"
            .ft1.tt insert end "$textline1\n" new1
            .ft2.tl insert end [myforml $doingLine2] \
            $::diff($top,wDiff1) insert end "$textline1\n" new1
            $::diff($top,wLine2) insert end [myforml $doingLine2] \
                    "hl$::HighLightCount change"
            .ft2.tt insert end "$textline2\n" new2
            $::diff($top,wDiff2) insert end "$textline2\n" new2
            incr doingLine1
            incr doingLine2
            incr t1
            incr t2
        }
        if {$c == "d"} {
            set bepa [lindex $block1 $t1]
            .ft1.tl insert end [myforml $doingLine1] \
            $::diff($top,wLine1) insert end [myforml $doingLine1] \
                    "hl$::HighLightCount change"
            .ft1.tt insert end "$bepa\n" new1
            emptyline 2
            $::diff($top,wDiff1) insert end "$bepa\n" new1
            emptyline $top 2
            incr doingLine1
            incr t1
        }
        if {$c == "a"} {
            set bepa [lindex $block2 $t2]
            .ft2.tl insert end [myforml $doingLine2] \
            $::diff($top,wLine2) insert end [myforml $doingLine2] \
                    "hl$::HighLightCount change"
            .ft2.tt insert end "$bepa\n" new2
            emptyline 1
            $::diff($top,wDiff2) insert end "$bepa\n" new2
            emptyline $top 1
            incr doingLine2
            incr t2
        }
    }
    return [llength $apa]
}

# Process one of the change/add/delete blocks reported by diff.
# ch1 is a file channel for the left file
# ch2 is a file channel for the right file
# n1/n2 is the number of lines involved
# line1/line2 says on what lines this block starts
proc dotext {ch1 ch2 n1 n2 line1 line2} {
proc dotext {top ch1 ch2 n1 n2 line1 line2} {
    global doingLine1 doingLine2 Pref mapMax changesList

    if {$n1 == 0 && $n2 == 0} {
        # All blocks have been processed. Continue until end of file.
        # If "only diffs" is on, just display a couple of context lines.
        set limit -1
        if {$Pref(onlydiffs) == 1} {
            set limit $Pref(context)
        }
	# Consider any total limit on displayed lines.
        if {$::diff(limitlines)} {
            set limit [expr {$::diff(limitlines) - $mapMax}]
        if {$::diff($top,limitlines)} {
            set limit [expr {$::diff($top,limitlines) - $mapMax}]
            if {$limit < 0} {
                set limit 0
            }
        }
        set t 0
        while {[gets $ch2 apa] != -1} {
            insert 2 $doingLine2 $apa
            insert $top 2 $doingLine2 $apa
            incr doingLine2
            incr mapMax
            incr t
            if {$limit >= 0 && $t >= $limit} break
        }
        set t 0
        while {[gets $ch1 apa] != -1} {
            insert 1 $doingLine1 $apa
            insert $top 1 $doingLine1 $apa
            incr doingLine1
            incr t
            if {$limit >= 0 && $t >= $limit} break
        }
        return
    }

827
828
829
830
831
832
833
834
835


836
837
838
839


840
841
842
843
844
845

846
847
848
849
850

851
852
853
854





855
856
857
858
859
860
861
862
863
864
865
866

867
868
869
870
871
872
873
851
852
853
854
855
856
857


858
859
860
861


862
863
864
865
866
867
868

869
870
871
872
873
874
875




876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891

892
893
894
895
896
897
898
899







-
-
+
+


-
-
+
+





-
+





+
-
-
-
-
+
+
+
+
+











-
+







    }
    set t 0
    while {$doingLine1 < $line1} {
        gets $ch1 apa
        gets $ch2 bepa
        if {$limit < 0 || ($t < $limit && $doingLine1 > $limit) || \
                ($line1 - $doingLine1) <= $limit} {
            insert 1 $doingLine1 $apa
            insert 2 $doingLine2 $bepa
            insert $top 1 $doingLine1 $apa
            insert $top 2 $doingLine2 $bepa
            incr mapMax
        } elseif {$t == $limit} {
            emptyline 1 0
            emptyline 2 0
            emptyline $top 1 0
            emptyline $top 2 0
            incr mapMax
        }
        incr doingLine1
        incr doingLine2
        incr t
        if {$::diff(limitlines) && $mapMax > $::diff(limitlines)} {
        if {$::diff($top,limitlines) && $mapMax > $::diff($top,limitlines)} {
            return
        }
    }
    # This should not happen unless something is wrong...
    if {$doingLine2 != $line2} {
        $::diff($top,wDiff1) insert end \
        .ft1.tt insert end "**Bad alignment here!! $doingLine2 $line2**\n"
        .ft2.tt insert end "**Bad alignment here!! $doingLine2 $line2**\n"
        .ft1.tl insert end "\n"
        .ft2.tl insert end "\n"
                "**Bad alignment here!! $doingLine2 $line2**\n"
        $::diff($top,wDiff2) insert end \
                "**Bad alignment here!! $doingLine2 $line2**\n"
        $::diff($top,wLine1) insert end "\n"
        $::diff($top,wLine2) insert end "\n"
    }

    # Process the block

    if {$n1 == $n2 && ($n1 == 1 || $Pref(parse) < 2)} {
        # Never do block parsing for one line blocks.
        # If block parsing is turned off, only do line parsing for
        # blocks of equal size.
        for {set t 0} {$t < $n1} {incr t} {
            gets $ch1 textline1
            gets $ch2 textline2
            insertMatchingLines $textline1 $textline2
            insertMatchingLines $top $textline1 $textline2
        }
        if {$::diff(filter) != "" &&  $::diff(filterflag)} {

        } else {
            lappend changesList $mapMax $n1 change $line1 $n1 $line2 $n2
        }
        incr mapMax $n1
881
882
883
884
885
886
887
888

889
890
891
892
893
894
895
896
897

898
899
900
901
902

903
904
905
906
907

908
909
910
911
912
913
914

915
916
917
918
919
920
921
922
923
924
925


926
927
928
929


930
931
932

933
934
935
936

937
938
939


940
941
942
943
944

945
946
947


948
949
950
951
952
953

954
955
956
957


958
959
960
961



962
963

964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982

983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001

1002
1003
1004
1005


1006
1007
1008
1009
1010

1011
1012
1013
1014
1015


1016
1017
1018
1019
1020
1021



1022
1023
1024
1025
1026
1027
1028
907
908
909
910
911
912
913

914
915
916
917
918
919
920
921
922

923
924
925
926
927

928
929
930
931
932

933
934
935
936
937
938
939

940
941
942
943
944
945
946
947
948
949


950
951
952
953


954
955
956
957

958
959
960
961

962
963
964

965
966
967
968
969
970

971
972
973

974
975
976
977
978
979
980

981
982
983


984
985
986



987
988
989
990

991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009

1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028

1029
1030
1031


1032
1033
1034
1035
1036
1037

1038
1039
1040
1041


1042
1043
1044
1045
1046



1047
1048
1049
1050
1051
1052
1053
1054
1055
1056







-
+








-
+




-
+




-
+






-
+









-
-
+
+


-
-
+
+


-
+



-
+


-
+
+




-
+


-
+
+





-
+


-
-
+
+

-
-
-
+
+
+

-
+


















-
+


















-
+


-
-
+
+




-
+



-
-
+
+



-
-
-
+
+
+







                lappend block1 $apa
            }
            set block2 {}
            for {set t 0} {$t < $n2} {incr t} {
                gets $ch2 apa
                lappend block2 $apa
            }
            set apa [insertMatchingBlocks $block1 $block2]
            set apa [insertMatchingBlocks $top $block1 $block2]

            lappend changesList $mapMax $apa change \
                    $line1 $n1 $line2 $n2
            incr mapMax $apa
        } else {
            # No extra parsing at all.
            for {set t 0} {$t < $n1} {incr t} {
                gets $ch1 apa
                insert 1 $doingLine1 $apa $tag1
                insert $top 1 $doingLine1 $apa $tag1
                incr doingLine1
            }
            for {set t 0} {$t < $n2} {incr t} {
                gets $ch2 apa
                insert 2 $doingLine2 $apa $tag2
                insert $top 2 $doingLine2 $apa $tag2
                incr doingLine2
            }
            if {$n1 <= $n2} {
                for {set t $n1} {$t < $n2} {incr t} {
                    emptyline 1
                    emptyline $top 1
                }
                lappend changesList $mapMax $n2 $tag2 \
                        $line1 $n1 $line2 $n2
                incr mapMax $n2
            } elseif {$n2 < $n1} {
                for {set t $n2} {$t < $n1} {incr t} {
                    emptyline 2
                    emptyline $top 2
                }
                lappend changesList $mapMax $n1 $tag1 \
                        $line1 $n1 $line2 $n2
                incr mapMax $n1
            }
        }
    }
}

proc enableRedo {} {
    .mf.m entryconfigure 0 -state normal
proc enableRedo {top} {
    $top.mf.m entryconfigure 0 -state normal
}

proc disableRedo {} {
    .mf.m entryconfigure 0 -state disabled
proc disableRedo {top} {
    $top.mf.m entryconfigure 0 -state disabled
}

proc busyCursor {} {
proc busyCursor {top} {
    global oldcursor oldcursor2
    if {![info exists oldcursor]} {
        set oldcursor [. cget -cursor]
        set oldcursor2 [.ft1.tt cget -cursor]
        set oldcursor2 [$::diff($top,wDiff1) cget -cursor]
    }
    . config -cursor watch
    foreach w {.ft1.tl .ft1.tt .ft2.tl .ft2.tt} {
    foreach item {wLine1 wDiff1 wLine2 wDiff2} {
        set w $::diff($top,$item)
        $w config -cursor watch
    }
}

proc normalCursor {} {
proc normalCursor {top} {
    global oldcursor oldcursor2
    . config -cursor $oldcursor
    foreach w {.ft1.tl .ft1.tt .ft2.tl .ft2.tt} {
    foreach item {wLine1 wDiff1 wLine2 wDiff2} {
        set w $::diff($top,$item)
        $w config -cursor $oldcursor2
    }
}

# Read a conflict file and extract the two versions.
proc prepareConflict {} {
proc prepareConflict {top} {
    global diff Pref

    set diff(leftFile) [tmpfile]
    set diff(rightFile) [tmpfile]
    set diff($top,leftFile) [tmpfile]
    set diff($top,rightFile) [tmpfile]

    set ch1 [open $diff(leftFile) w]
    set ch2 [open $diff(rightFile) w]
    set ch [open $diff(conflictFile) r]
    set ch1 [open $diff($top,leftFile) w]
    set ch2 [open $diff($top,rightFile) w]
    set ch [open $diff($top,conflictFile) r]

    set diff(conflictDiff) {}
    set diff($top,conflictDiff) {}
    set leftLine 1
    set rightLine 1
    set state both
    set rightName ""
    set leftName ""
    while {[gets $ch line] != -1} {
        if {[string match <<<<<<* $line]} {
            set state right
            regexp {<*\s*(.*)} $line -> rightName
            set start2 $rightLine
        } elseif {[string match ======* $line] && $state == "right"} {
            set state left
            set end2 [expr {$rightLine - 1}]
            set start1 $leftLine
        } elseif {[string match >>>>>>* $line] && $state == "left"} {
            set state both
            regexp {>*\s*(.*)} $line -> leftName
            set end1 [expr {$leftLine - 1}]
            lappend diff(conflictDiff) $start1,${end1}c$start2,$end2
            lappend diff($top,conflictDiff) $start1,${end1}c$start2,$end2
        } elseif {$state == "both"} {
            puts $ch1 $line
            puts $ch2 $line
            incr leftLine
            incr rightLine
        } elseif {$state == "left"} {
            puts $ch1 $line
            incr leftLine
        } else {
            puts $ch2 $line
            incr rightLine
        }
    }
    close $ch
    close $ch1
    close $ch2

    if {$leftName == "" && $rightName == ""} {
        set leftName "No Conflict: [file tail $diff(conflictFile)]"
        set leftName "No Conflict: [file tail $diff($top,conflictFile)]"
        set rightName $leftName
    }
    set diff(leftLabel) $leftName
    set diff(rightLabel) $rightName
    set diff($top,leftLabel) $leftName
    set diff($top,rightLabel) $rightName
    update idletasks
}

# Clean up after a conflict diff.
proc cleanupConflict {} {
proc cleanupConflict {top} {
    global diff Pref

    cleartmp
    set diff(rightFile) $diff(conflictFile)
    set diff(leftFile) $diff(conflictFile)
    set diff($top,rightFile) $diff($top,conflictFile)
    set diff($top,leftFile) $diff($top,conflictFile)
}

# Display one chunk from a patch file
proc displayOnePatch {leftLines rightLines leftLine rightLine} {
    emptyline 1
    emptyline 2
proc displayOnePatch {top leftLines rightLines leftLine rightLine} {
    emptyline $top 1
    emptyline $top 2

    set leftlen [llength $leftLines]
    set rightlen [llength $rightLines]

    set leftc 0
    set rightc 0
    set lblock {}
1079
1080
1081
1082
1083
1084
1085
1086

1087
1088
1089
1090
1091
1092


1093
1094
1095
1096
1097
1098
1099
1100


1101
1102
1103
1104
1105
1106
1107


1108
1109
1110
1111
1112
1113
1114
1115
1116

1117
1118
1119
1120


1121
1122
1123

1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136

1137
1138
1139
1140
1141
1142
1143
1107
1108
1109
1110
1111
1112
1113

1114
1115
1116
1117
1118


1119
1120
1121
1122
1123
1124
1125
1126


1127
1128
1129
1130
1131
1132
1133


1134
1135
1136
1137
1138
1139
1140
1141
1142
1143

1144
1145
1146


1147
1148
1149
1150

1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163

1164
1165
1166
1167
1168
1169
1170
1171







-
+




-
-
+
+






-
-
+
+





-
-
+
+








-
+


-
-
+
+


-
+












-
+







            }
            continue
        }
        # No change block anymore. If one just ended, display it.
        if {[llength $lblock] > 0 || [llength $rblock] > 0} {
            set ::doingLine1 $lblockl
            set ::doingLine2 $rblockl
            incr ::mapMax [insertMatchingBlocks $lblock $rblock]
            incr ::mapMax [insertMatchingBlocks $top $lblock $rblock]
            set lblock {}
            set rblock {}
        }
        if {$lmode == "" && $rmode == ""} {
            insert 1 $lline $lstr
            insert 2 $rline $rstr
            insert $top 1 $lline $lstr
            insert $top 2 $rline $rstr
            incr leftc
            incr rightc
            incr ::mapMax
            continue
        }
        if {$lmode == "-"} {
            insert 1 $lline $lstr new1
            emptyline 2
            insert $top 1 $lline $lstr new1
            emptyline $top 2
            incr leftc
            incr ::mapMax
            continue
        }
        if {$rmode == "+"} {
            insert 2 $rline $rstr new2
            emptyline 1
            insert $top 2 $rline $rstr new2
            emptyline $top 1
            incr rightc
            incr ::mapMax
            continue
        }
    }
}

# Read a patch file and display it
proc displayPatch {} {
proc displayPatch {top} {
    global diff Pref changesList mapMax

    set diff(leftLabel) "Patch $diff(patchFile): old"
    set diff(rightLabel) "Patch $diff(patchFile): new"
    set diff($top,leftLabel) "Patch $diff($top,patchFile): old"
    set diff($top,rightLabel) "Patch $diff($top,patchFile): new"
    update idletasks

    set ch [open $diff(patchFile) r]
    set ch [open $diff($top,patchFile) r]

    set style ""
    set divider "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-"

    set leftLine 1
    set rightLine 1
    set leftLines {}
    set rightLines {}
    set state none
    while {[gets $ch line] != -1} {
        if {[string match ======* $line]} {
            if {$state != "none"} {
                displayOnePatch $leftLines $rightLines $leftLine $rightLine
                displayOnePatch $top $leftLines $rightLines $leftLine $rightLine
            }
            set leftLines {}
            set rightLines {}
            set state none
            continue
        }
        # Detect the first line in a -c style diff
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161




1162
1163
1164
1165
1166
1167
1168
1169
1170




1171
1172
1173
1174
1175
1176

1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188

1189
1190
1191
1192
1193
1194
1195
1179
1180
1181
1182
1183
1184
1185




1186
1187
1188
1189
1190
1191
1192
1193
1194




1195
1196
1197
1198
1199
1200
1201
1202
1203

1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215

1216
1217
1218
1219
1220
1221
1222
1223







-
-
-
-
+
+
+
+





-
-
-
-
+
+
+
+





-
+











-
+







        if {$state == "none" && [regexp {^---} $line]} {
            set state newfile
            set style u
            set leftRE {^---\s+(.*)$}
            set rightRE {^\+\+\+\s+(.*)$}
        }
        if {$state == "newfile" && [regexp $leftRE $line -> sub]} {
            emptyline 1
            insert 1 "" $divider
            insert 1 "" $sub
            insert 1 "" $divider
            emptyline $top 1
            insert $top 1 "" $divider
            insert $top 1 "" $sub
            insert $top 1 "" $divider
            lappend ::changesList $mapMax 4 change 0 0 0 0
            incr mapMax 4
            continue
        }
        if {$state == "newfile" && [regexp $rightRE $line -> sub]} {
            emptyline 2
            insert 2 "" $divider
            insert 2 "" $sub
            insert 2 "" $divider
            emptyline $top 2
            insert $top 2 "" $divider
            insert $top 2 "" $sub
            insert $top 2 "" $divider
            continue
        }
        # A new section in a -u style diff
        if {[regexp {^@@\s+-(\d+),\d+\s+\+(\d+),} $line -> sub1 sub2]} {
            if {$state == "both"} {
                displayOnePatch $leftLines $rightLines $leftLine $rightLine
                displayOnePatch $top $leftLines $rightLines $leftLine $rightLine
            }
            set state both
            set leftLine $sub1
            set rightLine $sub2
            set leftLines {}
            set rightLines {}
            continue
        }
        # A new section in a -c style diff
        if {[regexp {^\*\*\*\*\*} $line]} {
            if {$state == "right"} {
                displayOnePatch $leftLines $rightLines $leftLine $rightLine
                displayOnePatch $top $leftLines $rightLines $leftLine $rightLine
            }
            set leftLines {}
            set rightLines {}
            set state left
            continue
        }
        # We are in the left part of a -c style diff
1240
1241
1242
1243
1244
1245
1246
1247

1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1268
1269
1270
1271
1272
1273
1274

1275
1276
1277
1278

1279
1280
1281
1282
1283
1284
1285







-
+



-







                lappend rightLines [list $leftLine "+" $str]
                incr rightLine
            }
            continue
        }
    }
    if {$state != "none"} {
        displayOnePatch $leftLines $rightLines $leftLine $rightLine
        displayOnePatch $top $leftLines $rightLines $leftLine $rightLine
    }

    close $ch

}

# Get a CVS revision
proc execCvsUpdate {filename outfile args} {
    set old ""
    set dir [file dirname $filename]
    if {$dir != "."} {
1273
1274
1275
1276
1277
1278
1279
1280

1281
1282
1283
1284
1285
1286
1287


1288
1289
1290


1291
1292
1293
1294
1295
1296
1297
1298



1299
1300
1301
1302



1303
1304
1305



1306
1307
1308
1309
1310
1311
1312
1313



1314
1315
1316
1317



1318
1319
1320



1321
1322
1323
1324
1325
1326
1327
1328


1329
1330
1331
1332
1333
1334





1335
1336
1337
1338
1339






1340
1341
1342
1343
1344
1345
1346
1347
1348

1349
1350
1351
1352
1353


1354
1355
1356
1357
1358
1359
1360
1361





1362
1363
1364
1365
1366
1367
1368
1369
1370





1371
1372
1373
1374
1375

1376
1377
1378
1379
1380
1381


1382
1383
1384

1385
1386
1387

1388
1389
1390


1391
1392
1393
1394
1395
1396
1397
1398


1399
1400
1401

1402
1403
1404
1405
1406
1407
1408





1409
1410
1411
1412
1413


1414
1415
1416

1417
1418
1419
1420
1421
1422

1423
1424
1425
1426


1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440


1441
1442
1443
1444
1445
1446


1447
1448
1449

1450
1451
1452

1453
1454
1455
1456
1457
1458


1459
1460
1461
1462
1463
1464
1465
1466
1467
1468

1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487

1488
1489
1490

1491
1492
1493
1494

1495
1496
1497
1498

1499
1500
1501
1502
1503

1504
1505
1506
1507

1508
1509
1510
1511

1512
1513
1514
1515
1516


1517
1518
1519
1520
1521


1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532



1533
1534
1535
1536
1537
1538



1539
1540
1541
1542
1543




1544
1545
1546
1547


1548
1549
1550
1551
1552
1553
1554




1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565











1566
1567

1568
1569
1570
1571
1572
1573
1574
1575


1576
1577
1578

1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597

1598
1599
1600
1601


1602
1603
1604
1605
1606
1607
1608
1609
1610




1611
1612
1613
1614
1615

1616
1617
1618

1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636


1637
1638
1639
1640
1641
1642
1643
1644
1645

1646
1647
1648
1649
1650
1651




1652
1653
1654
1655
1656


1657
1658
1659
1660
1661




1662
1663
1664
1665
1666
1667

1668
1669
1670
1671
1672




1673
1674
1675
1676
1677


1678
1679
1680
1681
1682




1683
1684
1685
1686
1687
1688

1689
1690
1691
1692



1693
1694
1695
1696

1697
1698
1699
1700



1701
1702
1703
1704
1705
1706
1707




1708
1709
1710
1711



1712
1713
1714
1715
1716
1717
1718




1719
1720
1721


1722
1723
1724
1725

1726
1727
1728
1729
1730
1731
1732






1733
1734
1735
1736

1737
1738
1739
1740
1741
1742
1743






1744
1745
1746
1747

1748
1749
1750
1751
1752




1753
1754
1755
1756
1757
1758
1759
1760
1761

1762
1763
1764

1765
1766
1767

1768
1769
1770
1771


1772
1773

1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784

1785
1786
1787
1788
1789
1790
1791
1792
1793

1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808




1809
1810
1811
1812


1813
1814
1815
1816
1817
1818
1819
1300
1301
1302
1303
1304
1305
1306

1307
1308
1309
1310
1311
1312


1313
1314
1315


1316
1317
1318
1319
1320
1321
1322



1323
1324
1325
1326



1327
1328
1329
1330


1331
1332
1333
1334
1335
1336
1337
1338



1339
1340
1341
1342



1343
1344
1345
1346


1347
1348
1349
1350
1351
1352
1353
1354
1355


1356
1357
1358





1359
1360
1361
1362
1363
1364




1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378

1379
1380
1381
1382


1383
1384
1385
1386
1387





1388
1389
1390
1391
1392
1393
1394
1395
1396





1397
1398
1399
1400
1401
1402
1403
1404
1405

1406
1407
1408
1409
1410


1411
1412
1413
1414

1415
1416
1417

1418
1419
1420

1421
1422
1423
1424
1425
1426
1427
1428


1429
1430
1431
1432

1433
1434
1435
1436




1437
1438
1439
1440
1441
1442
1443
1444


1445
1446
1447
1448

1449
1450
1451
1452
1453
1454

1455
1456
1457


1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471


1472
1473
1474
1475
1476
1477


1478
1479
1480
1481

1482
1483
1484

1485
1486
1487
1488
1489


1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500

1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519

1520
1521
1522

1523
1524
1525
1526

1527
1528
1529
1530

1531
1532
1533
1534
1535

1536
1537
1538
1539

1540
1541
1542
1543

1544
1545
1546
1547
1548

1549
1550
1551
1552
1553
1554

1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565


1566
1567
1568
1569
1570
1571



1572
1573
1574
1575




1576
1577
1578
1579
1580
1581


1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594











1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606

1607
1608
1609
1610
1611
1612
1613
1614

1615
1616
1617
1618

1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637

1638
1639
1640


1641
1642
1643
1644
1645
1646
1647
1648
1649


1650
1651
1652
1653
1654
1655
1656
1657

1658
1659
1660

1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678

1679
1680
1681
1682
1683
1684
1685
1686
1687
1688

1689
1690
1691




1692
1693
1694
1695
1696
1697
1698
1699

1700
1701
1702




1703
1704
1705
1706
1707
1708
1709
1710
1711

1712
1713




1714
1715
1716
1717
1718
1719
1720
1721

1722
1723
1724




1725
1726
1727
1728
1729
1730
1731
1732
1733

1734
1735



1736
1737
1738
1739
1740
1741

1742
1743



1744
1745
1746
1747
1748
1749




1750
1751
1752
1753
1754



1755
1756
1757
1758
1759
1760




1761
1762
1763
1764
1765


1766
1767
1768
1769
1770

1771
1772






1773
1774
1775
1776
1777
1778
1779
1780
1781

1782
1783






1784
1785
1786
1787
1788
1789
1790
1791
1792

1793
1794




1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806

1807
1808
1809

1810
1811
1812

1813
1814
1815


1816
1817
1818

1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829

1830
1831
1832
1833
1834
1835
1836
1837
1838

1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850




1851
1852
1853
1854
1855
1856


1857
1858
1859
1860
1861
1862
1863
1864
1865







-
+





-
-
+
+

-
-
+
+





-
-
-
+
+
+

-
-
-
+
+
+

-
-
+
+
+





-
-
-
+
+
+

-
-
-
+
+
+

-
-
+
+
+






-
-
+
+

-
-
-
-
-
+
+
+
+
+

-
-
-
-
+
+
+
+
+
+








-
+



-
-
+
+



-
-
-
-
-
+
+
+
+
+




-
-
-
-
-
+
+
+
+
+




-
+




-
-
+
+


-
+


-
+


-
+
+






-
-
+
+


-
+



-
-
-
-
+
+
+
+
+



-
-
+
+


-
+





-
+


-
-
+
+












-
-
+
+




-
-
+
+


-
+


-
+




-
-
+
+









-
+


















-
+


-
+



-
+



-
+




-
+



-
+



-
+




-
+
+




-
+
+









-
-
+
+
+



-
-
-
+
+
+

-
-
-
-
+
+
+
+


-
-
+
+







+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+

-
+







-
+
+


-
+


















-
+


-
-
+
+







-
-
+
+
+
+




-
+


-
+

















-
+
+








-
+


-
-
-
-
+
+
+
+




-
+
+

-
-
-
-
+
+
+
+





-
+

-
-
-
-
+
+
+
+




-
+
+

-
-
-
-
+
+
+
+





-
+

-
-
-
+
+
+



-
+

-
-
-
+
+
+



-
-
-
-
+
+
+
+

-
-
-
+
+
+



-
-
-
-
+
+
+
+

-
-
+
+



-
+

-
-
-
-
-
-
+
+
+
+
+
+



-
+

-
-
-
-
-
-
+
+
+
+
+
+



-
+

-
-
-
-
+
+
+
+








-
+


-
+


-
+


-
-
+
+

-
+










-
+








-
+











-
-
-
-
+
+
+
+


-
-
+
+








    if {$old != ""} {
        cd $old
    }
}

# Prepare for RCS/CVS diff. Checkout copies of the versions needed.
proc prepareRCS {} {
proc prepareRCS {top} {
    global diff Pref

    set revs {}

    # Search for revision options
    if {$Pref(doptrev1) != ""} {
        lappend revs $Pref(doptrev1)
    if {$diff($top,doptrev1) != ""} {
        lappend revs $diff($top,doptrev1)
    }
    if {$Pref(doptrev2) != ""} {
        lappend revs $Pref(doptrev2)
    if {$diff($top,doptrev2) != ""} {
        lappend revs $diff($top,doptrev2)
    }

    switch [llength $revs] {
        0 {
            # Compare local file with latest version.
            set diff(leftFile) [tmpfile]
            set diff(rightLabel) $diff(RCSFile)
            set diff(rightFile) $diff(RCSFile)
            set diff($top,leftFile) [tmpfile]
            set diff($top,rightLabel) $diff($top,RCSFile)
            set diff($top,rightFile) $diff($top,RCSFile)

            if {$diff(mode) == "CVS"} {
                set diff(leftLabel) "$diff(RCSFile) (CVS)"
                execCvsUpdate $diff(RCSFile) $diff(leftFile)
            if {$diff($top,mode) == "CVS"} {
                set diff($top,leftLabel) "$diff($top,RCSFile) (CVS)"
                execCvsUpdate $diff($top,RCSFile) $diff($top,leftFile)
            } else {
                set diff(leftLabel) "$diff(RCSFile) (RCS)"
                catch {exec co -p [file nativename $diff(RCSFile)] > $diff(leftFile)}
                set diff($top,leftLabel) "$diff($top,RCSFile) (RCS)"
                catch {exec co -p [file nativename $diff($top,RCSFile)] \
                        > $diff($top,leftFile)}
            }
        }
        1 {
            # Compare local file with specified version.
            set r [lindex $revs 0]
            set diff(leftFile) [tmpfile]
            set diff(rightLabel) $diff(RCSFile)
            set diff(rightFile) $diff(RCSFile)
            set diff($top,leftFile) [tmpfile]
            set diff($top,rightLabel) $diff($top,RCSFile)
            set diff($top,rightFile) $diff($top,RCSFile)

            if {$diff(mode) == "CVS"} {
                set diff(leftLabel) "$diff(RCSFile) (CVS $r)"
                execCvsUpdate $diff(RCSFile) $diff(leftFile) -r $r
            if {$diff($top,mode) == "CVS"} {
                set diff($top,leftLabel) "$diff($top,RCSFile) (CVS $r)"
                execCvsUpdate $diff($top,RCSFile) $diff($top,leftFile) -r $r
            } else {
                set diff(leftLabel) "$diff(RCSFile) (RCS $r)"
                catch {exec co -p$r [file nativename $diff(RCSFile)] > $diff(leftFile)}
                set diff($top,leftLabel) "$diff($top,RCSFile) (RCS $r)"
                catch {exec co -p$r [file nativename $diff($top,RCSFile)] \
                        > $diff($top,leftFile)}
            }
        }
        default {
            # Compare the two specified versions.
            set r1 [lindex $revs 0]
            set r2 [lindex $revs 1]
            set diff(leftFile) [tmpfile]
            set diff(rightFile) [tmpfile]
            set diff($top,leftFile) [tmpfile]
            set diff($top,rightFile) [tmpfile]

            if {$diff(mode) == "CVS"} {
                set diff(leftLabel) "$diff(RCSFile) (CVS $r1)"
                set diff(rightLabel) "$diff(RCSFile) (CVS $r2)"
                execCvsUpdate $diff(RCSFile) $diff(leftFile) -r $r1
                execCvsUpdate $diff(RCSFile) $diff(rightFile) -r $r2
            if {$diff($top,mode) == "CVS"} {
                set diff($top,leftLabel) "$diff($top,RCSFile) (CVS $r1)"
                set diff($top,rightLabel) "$diff($top,RCSFile) (CVS $r2)"
                execCvsUpdate $diff($top,RCSFile) $diff($top,leftFile) -r $r1
                execCvsUpdate $diff($top,RCSFile) $diff($top,rightFile) -r $r2
            } else {
                set diff(leftLabel) "$diff(RCSFile) (RCS $r1)"
                set diff(rightLabel) "$diff(RCSFile) (RCS $r2)"
                catch {exec co -p$r1 [file nativename $diff(RCSFile)] > $diff(leftFile)}
                catch {exec co -p$r2 [file nativename $diff(RCSFile)] > $diff(rightFile)}
                set diff($top,leftLabel) "$diff($top,RCSFile) (RCS $r1)"
                set diff($top,rightLabel) "$diff($top,RCSFile) (RCS $r2)"
                catch {exec co -p$r1 [file nativename $diff($top,RCSFile)] \
                        > $diff($top,leftFile)}
                catch {exec co -p$r2 [file nativename $diff($top,RCSFile)] \
                        > $diff($top,rightFile)}
            }
        }
    }
    # Make sure labels are updated before processing starts
    update idletasks
}

# Clean up after a RCS/CVS diff.
proc cleanupRCS {} {
proc cleanupRCS {top} {
    global diff Pref

    cleartmp
    set diff(rightFile) $diff(RCSFile)
    set diff(leftFile) $diff(RCSFile)
    set diff($top,rightFile) $diff($top,RCSFile)
    set diff($top,leftFile) $diff($top,RCSFile)
}

# Prepare for a diff by creating needed temporary files
proc prepareFiles {} {
    if {$::diff(mode) == "RCS" || $::diff(mode) == "CVS"} {
        prepareRCS
    } elseif {[string match "conflict*" $::diff(mode)]} {
        prepareConflict
proc prepareFiles {top} {
    if {$::diff($top,mode) == "RCS" || $::diff($top,mode) == "CVS"} {
        prepareRCS $top
    } elseif {[string match "conflict*" $::diff($top,mode)]} {
        prepareConflict $top
    }
}

# Clean up after a diff
proc cleanupFiles {} {
    if {$::diff(mode) == "RCS" || $::diff(mode) == "CVS"} {
        cleanupRCS
    } elseif {[string match "conflict*" $::diff(mode)]} {
        cleanupConflict
proc cleanupFiles {top} {
    if {$::diff($top,mode) == "RCS" || $::diff($top,mode) == "CVS"} {
        cleanupRCS $top
    } elseif {[string match "conflict*" $::diff($top,mode)]} {
        cleanupConflict $top
    }
}

# Main diff function.
proc doDiff {} {
proc doDiff {top} {
    global diff Pref
    global doingLine1 doingLine2
    global mapMax changesList

    if {$diff(mode) == "" && ($diff(leftOK) == 0 || $diff(rightOK) == 0)} {
        disableRedo
    if {$diff($top,mode) == "" && ($diff($top,leftOK) == 0 || $diff($top,rightOK) == 0)} {
        disableRedo $top
        return
    } else {
        enableRedo
        enableRedo $top
    }

    busyCursor
    busyCursor $top

    # Clear up everything before starting processing
    foreach w {.ft1.tl .ft1.tt .ft2.tl .ft2.tt} {
    foreach item {wLine1 wDiff1 wLine2 wDiff2} {
        set w $::diff($top,$item)
        $w configure -state normal
        $w delete 1.0 end
    }
    set changesList {}
    set mapMax 0
    set ::HighLightCount 0
    highLightChange -1
    drawMap -1
    highLightChange $top -1
    drawMap $top -1
    # Display a star during diff execution, to know when the internal
    # processing starts, and when the label is "valid".
    set ::diff(eqLabel) "*"
    set ::diff($top,eqLabel) "*"

    update idletasks

    if {$diff(mode) == "patch"} {
        displayPatch
        drawMap -1
        foreach w {.ft1.tl .ft2.tl} {
    if {$diff($top,mode) == "patch"} {
        displayPatch $top
        drawMap $top -1
        foreach item {wLine1 wLine2} {
            set w $::diff($top,$item)
            $w configure -state disabled
        }
        update idletasks
        .ft2.tl see 1.0
        normalCursor
        $::diff($top,wLine2) see 1.0
        normalCursor $top
        return
    } else {
        prepareFiles
        prepareFiles $top
    }

    # Run diff and parse the result.
    if {$::diff(diffutil)} {
        set differr [catch {eval DiffUtil::diffFiles $Pref(ignore) \
                \$diff(leftFile) \$diff(rightFile)} diffres]
                \$diff($top,leftFile) \$diff($top,rightFile)} diffres]
    } else {
        set differr [catch {eval exec \$::diff(diffexe) \
                $Pref(dopt) $Pref(ignore) \
                \$diff(leftFile) \$diff(rightFile)} diffres]
                $diff($top,dopt) $Pref(ignore) \
                \$diff($top,leftFile) \$diff($top,rightFile)} diffres]
    }
    set apa [split $diffres "\n"]
    set result {}
    foreach i $apa {
        if {[string match {[0-9]*} $i]} {
            lappend result $i
        }
    }

    # In conflict mode we can use the diff information collected when
    # parsing the conflict file. This makes sure the blocks in the conflict
    # file become change-blocks during merge.
    if {$diff(mode) == "conflictPure"} {
        set result $diff(conflictDiff)
    if {$diff($top,mode) == "conflictPure"} {
        set result $diff($top,conflictDiff)
    }

    if {[llength $result] == 0} {
        if {$differr == 1} {
            .ft1.tt insert end $diffres
            normalCursor
            $::diff($top,wDiff1) insert end $diffres
            normalCursor $top
            return
        } else {
            set ::diff(eqLabel) "="
            set ::diff($top,eqLabel) "="
        }
    } else {
        set ::diff(eqLabel) " "
        set ::diff($top,eqLabel) " "
    }
    # Update the equal label immediately for better feedback
    update idletasks

    set ch1 [open $diff(leftFile)]
    set ch2 [open $diff(rightFile)]
    set ch1 [open $diff($top,leftFile)]
    set ch2 [open $diff($top,rightFile)]
    if {$::tcl_platform(platform) == "windows" && $Pref(crlf)} {
        fconfigure $ch1 -translation crlf
        fconfigure $ch2 -translation crlf
    }
    set doingLine1 1
    set doingLine2 1
    set t 0
    foreach i $result {
        if {![regexp {(.*)([acd])(.*)} $i -> l c r]} {
            .ft1.tt insert 1.0 "No regexp match for $i\n"
            $::diff($top,wDiff1) insert 1.0 "No regexp match for $i\n"
        } else {
            if {[regexp {([0-9]+),([0-9]+)} $l apa start stop]} {
                set n1 [expr {$stop - $start + 1}]
                set line1 $start
            } else {
                set n1 1
                set line1 $l
            }
            if {[regexp {([0-9]+),([0-9]+)} $r apa start stop]} {
                set n2 [expr {$stop - $start + 1}]
                set line2 $start
            } else {
                set n2 1
                set line2 $r
            }
            switch $c {
                a {
                    # lucka i left, new i right
                    dotext $ch1 $ch2 0 $n2 [expr {$line1 + 1}] $line2
                    dotext $top $ch1 $ch2 0 $n2 [expr {$line1 + 1}] $line2
                }
                c {
                    dotext $ch1 $ch2 $n1 $n2 $line1 $line2
                    dotext $top $ch1 $ch2 $n1 $n2 $line1 $line2
                }
                d {
                    # lucka i right, new i left
                    dotext $ch1 $ch2 $n1 0 $line1 [expr {$line2 + 1}]
                    dotext $top $ch1 $ch2 $n1 0 $line1 [expr {$line2 + 1}]
                }
            }
        }
        if {$::diff(limitlines) && $::mapMax > $::diff(limitlines)} {
        if {$::diff($top,limitlines) && $::mapMax > $::diff($top,limitlines)} {
            break
        }
        if {[incr t] >= 10} {
	    update idletasks
	    .ft2.tl see end
	    $::diff($top,wLine2) see end
	    update idletasks
            set t 0
        }
        bindHighlight
        bindHighlight $top
        incr ::HighLightCount
    }

    dotext $ch1 $ch2 0 0 0 0
    dotext $top $ch1 $ch2 0 0 0 0

    # Make sure all text widgets have the same number of lines.
    # The common y scroll doesn't work well if not.
    set max 0.0
    foreach w {.ft1.tl .ft1.tt .ft2.tl .ft2.tt} {
    foreach item {wLine1 wDiff1 wLine2 wDiff2} {
        set w $::diff($top,$item)
        if {[$w index end] > $max} {
            set max [$w index end]
        }
    }
    foreach w {.ft1.tl .ft1.tt .ft2.tl .ft2.tt} {
    foreach item {wLine1 wDiff1 wLine2 wDiff2} {
        set w $::diff($top,$item)
        set d [expr {int($max) - int([$w index end])}]
        for {set t 0} {$t < $d} {incr t} {
            $w insert end \n
        }
    }

    close $ch1
    close $ch2

    drawMap -1
    foreach w {.ft1.tl .ft2.tl} {
    drawMap $top -1
    foreach item {wLine1 wLine2} {
        set w $::diff($top,$item)
        $w configure -state disabled
    }
    update idletasks
    .ft2.tl see 1.0
    normalCursor
    showDiff 0
    $::diff($top,wLine2) see 1.0
    normalCursor $top
    showDiff $top 0

    cleanupFiles
    if {[string match "conflict*" $diff(mode)]} {
        if {$::diff(eqLabel) != "="} {
            makeMergeWin
    cleanupFiles $top
    if {[string match "conflict*" $diff($top,mode)]} {
        if {$::diff($top,eqLabel) != "="} {
            makeMergeWin $top
        }
    }
    if {$diff(printFile) != ""} {
        after idle {doPrint 1 ; cleanupAndExit}
    if {$diff($top,printFile) != ""} {
        after idle "doPrint $top 1 ; cleanupAndExit all"
    }
}

# This is the entrypoint to do a diff via DDE or Send
proc remoteDiff {file1 file2} {
    global diff

    # Maybe create a new window? FIXA

    set top [lindex $::diff(diffWindows) end]

    set diff(leftDir) [file dirname $file1]
    set diff(leftFile) $file1
    set diff(leftLabel) $file1
    set diff(leftOK) 1
    set diff(rightDir) [file dirname $file2]
    set diff(rightFile) $file2
    set diff(rightLabel) $file2
    set diff(rightOK) 1
    set diff(mode) ""
    wm deiconify .
    raise .
    set diff($top,leftDir) [file dirname $file1]
    set diff($top,leftFile) $file1
    set diff($top,leftLabel) $file1
    set diff($top,leftOK) 1
    set diff($top,rightDir) [file dirname $file2]
    set diff($top,rightFile) $file2
    set diff($top,rightLabel) $file2
    set diff($top,rightOK) 1
    set diff($top,mode) ""
    wm deiconify $top
    raise $top
    update
    doDiff
    doDiff $top
}

#####################################
# Highlight and navigation stuff
#####################################

# Scroll windows to next/previous diff
proc findDiff {delta} {
proc findDiff {top delta} {
    # FIXA: currenthighlight per top
    global CurrentHighLight

    showDiff [expr {$CurrentHighLight + $delta}]
    showDiff $top [expr {$CurrentHighLight + $delta}]
}

# Scroll a text window to view a certain range, and possibly some
# lines before and after.
proc seeText {w si ei} {
    $w see $ei
    $w see $si
    $w see $si-5lines
    $w see $ei+5lines
    if {[llength [$w bbox $si]] == 0} {
        $w yview $si-5lines
    }
    if {[llength [$w bbox $ei]] == 0} {
        $w yview $si
    }
}

# Highlight a diff
proc highLightChange {n} {
proc highLightChange {top n} {
    global CurrentHighLight changesList
    if {[info exists CurrentHighLight] && $CurrentHighLight >= 0} {
        .ft1.tl tag configure hl$CurrentHighLight -background {}
        .ft2.tl tag configure hl$CurrentHighLight -background {}
        $::diff($top,wLine1) tag configure hl$CurrentHighLight -background {}
        $::diff($top,wLine2) tag configure hl$CurrentHighLight -background {}
    }
    set CurrentHighLight $n
    if {$CurrentHighLight < 0} {
        set CurrentHighLight -1
    } elseif {$CurrentHighLight * 7 >= [llength $changesList]} {
        set CurrentHighLight [expr {[llength $changesList] / 7}]
    } else {
        .ft1.tl tag configure hl$CurrentHighLight -background yellow
        .ft2.tl tag configure hl$CurrentHighLight -background yellow
        $::diff($top,wLine1) tag configure hl$CurrentHighLight \
                -background yellow
        $::diff($top,wLine2) tag configure hl$CurrentHighLight \
                -background yellow
    }
}

# Highlight a diff and scroll windows to it.
proc showDiff {num} {
proc showDiff {top num} {
    global CurrentHighLight changesList

    highLightChange $num
    highLightChange $top $num

    set line1 [lindex $changesList [expr {$CurrentHighLight * 7}]]

    if {$CurrentHighLight < 0} {
        set line1 1.0
        set line2 1.0
    } elseif {$line1 == ""} {
        set line1 end
        set line2 end
    } else {
        set line2 [expr {$line1 + \
                [lindex $changesList [expr {$CurrentHighLight * 7 + 1}]]}]
        incr line1
        set line1 $line1.0
        set line2 $line2.0
    }

    foreach w {.ft1.tl .ft1.tt .ft2.tl .ft2.tt} {
    foreach item {wLine1 wDiff1 wLine2 wDiff2} {
        set w $::diff($top,$item)
        seeText $w $line1 $line2
    }
}

#####################################
# File dialog stuff
#####################################

proc doOpenLeft {{forget 0}} {
proc doOpenLeft {top {forget 0}} {
    global diff

    if {!$forget && [info exists diff(leftDir)]} {
        set initDir $diff(leftDir)
    } elseif {[info exists diff(rightDir)]} {
        set initDir $diff(rightDir)
    if {!$forget && [info exists diff($top,leftDir)]} {
        set initDir $diff($top,leftDir)
    } elseif {[info exists diff($top,rightDir)]} {
        set initDir $diff($top,rightDir)
    } else {
        set initDir [pwd]
    }

    set apa [tk_getOpenFile -title "Select left file" -initialdir $initDir]
    set apa [tk_getOpenFile -title "Select left file" -initialdir $initDir \
            -parent $top]
    if {$apa != ""} {
        set diff(leftDir) [file dirname $apa]
        set diff(leftFile) $apa
        set diff(leftLabel) $apa
        set diff(leftOK) 1
        set diff($top,leftDir) [file dirname $apa]
        set diff($top,leftFile) $apa
        set diff($top,leftLabel) $apa
        set diff($top,leftOK) 1
        return 1
    }
    return 0
}

proc doOpenRight {{forget 0}} {
proc doOpenRight {top {forget 0}} {
    global diff
    if {!$forget && [info exists diff(rightDir)]} {
        set initDir $diff(rightDir)
    } elseif {[info exists diff(leftDir)]} {
        set initDir $diff(leftDir)
    if {!$forget && [info exists diff($top,rightDir)]} {
        set initDir $diff($top,rightDir)
    } elseif {[info exists diff($top,leftDir)]} {
        set initDir $diff($top,leftDir)
    } else {
        set initDir [pwd]
    }

    set apa [tk_getOpenFile -title "Select right file" -initialdir $initDir]
    set apa [tk_getOpenFile -title "Select right file" -initialdir $initDir \
            -parent $top]
    if {$apa != ""} {
        set diff(rightDir) [file dirname $apa]
        set diff(rightFile) $apa
        set diff(rightLabel) $apa
        set diff(rightOK) 1
        set diff($top,rightDir) [file dirname $apa]
        set diff($top,rightFile) $apa
        set diff($top,rightLabel) $apa
        set diff($top,rightOK) 1
        return 1
    }
    return 0
}

proc openLeft {} {
proc openLeft {top} {
    global diff
    if {[doOpenLeft]} {
        set diff(mode) ""
        doDiff
    if {[doOpenLeft $top]} {
        set diff($top,mode) ""
        doDiff $top
    }
}

proc openRight {} {
proc openRight {top} {
    global diff
    if {[doOpenRight]} {
        set diff(mode) ""
        doDiff
    if {[doOpenRight $top]} {
        set diff($top,mode) ""
        doDiff $top
    }
}

proc openConflict {} {
    global diff
    if {[doOpenRight]} {
        set diff(mode) "conflict"
proc openConflict {top} {
    global diff Pref
    if {[doOpenRight $top]} {
        set diff($top,mode) "conflict"
        set Pref(ignore) " "
        set diff(conflictFile) $diff(rightFile)
        set diff(mergeFile) ""
        doDiff
        set diff($top,conflictFile) $diff($top,rightFile)
        set diff($top,mergeFile) ""
        doDiff $top
    }
}

proc openPatch {} {
    global diff
    if {[doOpenLeft]} {
        set diff(mode) "patch"
proc openPatch {top} {
    global diff Pref
    if {[doOpenLeft $top]} {
        set diff($top,mode) "patch"
        set Pref(ignore) " "
        set diff(patchFile) $diff(leftFile)
        doDiff
        set diff($top,patchFile) $diff($top,leftFile)
        doDiff $top
    }
}

proc openRCS {} {
proc openRCS {top} {
    global diff
    if {[doOpenRight]} {
        set diff(mode) "RCS"
        set diff(RCSFile) $diff(rightFile)
        set diff(leftLabel) "RCS"
        set diff(leftOK) 0
        doDiff
    if {[doOpenRight $top]} {
        set diff($top,mode) "RCS"
        set diff($top,RCSFile) $diff($top,rightFile)
        set diff($top,leftLabel) "RCS"
        set diff($top,leftOK) 0
        doDiff $top
    }
}

proc openCVS {} {
proc openCVS {top} {
    global diff
    if {[doOpenRight]} {
        set diff(mode) "CVS"
        set diff(RCSFile) $diff(rightFile)
        set diff(leftLabel) "CVS"
        set diff(leftOK) 0
        doDiff
    if {[doOpenRight $top]} {
        set diff($top,mode) "CVS"
        set diff($top,RCSFile) $diff($top,rightFile)
        set diff($top,leftLabel) "CVS"
        set diff($top,leftOK) 0
        doDiff $top
    }
}

proc openBoth {forget} {
proc openBoth {top forget} {
    global diff
    if {[doOpenLeft]} {
        if {[doOpenRight $forget]} {
            set diff(mode) ""
            doDiff
    if {[doOpenLeft $top]} {
        if {[doOpenRight $top $forget]} {
            set diff($top,mode) ""
            doDiff $top
        }
    }
}

#####################################
# Map stuff
#####################################

proc drawMap {newh} {
proc drawMap {top newh} { # FIXA top
    global mapMax Pref changesList

    set oldh [map cget -height]
    set oldh [map$top cget -height]
    if {$oldh == $newh} return

    map blank
    map$top blank
    if {![info exists changesList] || [llength $changesList] == 0} return

    set w [winfo width .c]
    set h [winfo height .c]
    set w [winfo width $top.c]
    set h [winfo height $top.c]
    set x2 [expr {$w - 1}]
    map configure -width $w -height $h
    map$top configure -width $w -height $h
    incr h -1
    foreach {start length type dum1 dum2 dum3 dum4} $changesList {
        set y1 [expr {$start * $h / $mapMax + 1}]
        if {$y1 < 1} {set y1 1}
        if {$y1 > $h} {set y1 $h}
        set y2 [expr {($start + $length) * $h / $mapMax + 1}]
        if {$y2 < 1} {set y2 1}
        if {$y2 <= $y1} {set y2 [expr {$y1 + 1}]}
        if {$y2 > $h} {set y2 $h}
        incr y2
        map put $Pref(color$type) -to 1 $y1 $x2 $y2
        map$top put $Pref(color$type) -to 1 $y1 $x2 $y2
    }
}

######################################
# Merge stuff
#####################################

# Get all data from the files to merge
proc collectMergeData {} {
proc collectMergeData {top} {
    global diff
    global changesList mergeSelection
    global leftMergeData rightMergeData

    set leftMergeData {}
    set rightMergeData {}

    if {![info exists changesList]} {
        set changesList {}
    }

    if {$diff(mode) == "RCS" || $diff(mode) == "CVS"} {
        prepareRCS
    } elseif {[string match "conflict*" $diff(mode)]} {
        prepareConflict
    if {$diff($top,mode) == "RCS" || $diff($top,mode) == "CVS"} {
        prepareRCS $top
    } elseif {[string match "conflict*" $diff($top,mode)]} {
        prepareConflict $top
    }

    set ch1 [open $diff(leftFile) r]
    set ch2 [open $diff(rightFile) r]
    set ch1 [open $diff($top,leftFile) r]
    set ch2 [open $diff($top,rightFile) r]
    set doingLine1 1
    set doingLine2 1
    set changeNo 0
    foreach {start length type line1 n1 line2 n2} $changesList {
        set data1 {}
        set data2 {}
        while {$doingLine1 < $line1} {
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868




1869
1870
1871
1872
1873

1874
1875
1876

1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897

1898
1899
1900
1901
1902
1903

1904
1905
1906

1907
1908
1909
1910
1911
1912
1913
1914
1915
1916

1917
1918
1919
1920
1921

1922
1923
1924
1925

1926
1927
1928

1929
1930
1931
1932
1933

1934
1935
1936
1937


1938
1939
1940
1941
1942

1943
1944
1945
1946

1947
1948
1949
1950
1951
1952
1953
1904
1905
1906
1907
1908
1909
1910




1911
1912
1913
1914
1915
1916
1917
1918

1919
1920
1921

1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942

1943
1944
1945
1946
1947
1948

1949
1950
1951

1952
1953
1954
1955
1956
1957
1958
1959
1960
1961

1962
1963
1964
1965
1966

1967
1968
1969
1970

1971
1972
1973

1974
1975
1976
1977
1978

1979
1980
1981


1982
1983
1984
1985
1986
1987

1988
1989
1990
1991

1992
1993
1994
1995
1996
1997
1998
1999







-
-
-
-
+
+
+
+




-
+


-
+




















-
+





-
+


-
+









-
+




-
+



-
+


-
+




-
+


-
-
+
+




-
+



-
+







    }
    lappend leftMergeData $data1
    lappend rightMergeData $data2

    close $ch1
    close $ch2

    if {$diff(mode) == "RCS" || $diff(mode) == "CVS"} {
        cleanupRCS
    } elseif {[string match "conflict*" $diff(mode)]} {
        cleanupConflict
    if {$diff($top,mode) == "RCS" || $diff($top,mode) == "CVS"} {
        cleanupRCS $top
    } elseif {[string match "conflict*" $diff($top,mode)]} {
        cleanupConflict $top
    }
}

# Fill up the merge window with the initial version of merged files.
proc fillMergeWindow {} {
proc fillMergeWindow {top} {
    global mergeSelection leftMergeData rightMergeData curMergeSel curMerge

    set w .merge.t
    set w $top.merge.t
    $w delete 1.0 end
    set marks {}
    set t 0
    foreach {commLeft diffLeft} $leftMergeData \
            {commRight diffRight} $rightMergeData {
        $w insert end $commRight
        if {![info exists mergeSelection($t)]} continue
        $w mark set merges$t insert
        $w mark gravity merges$t left
        $w insert end $diffRight merge$t
        lappend marks mergee$t [$w index insert]
        set mergeSelection($t) 2
        incr t
    }
    foreach {mark index} $marks {
        $w mark set $mark $index
    }
    set curMerge 0
    set curMergeSel 2
    $w tag configure merge0 -foreground red
    showDiff 0
    showDiff $top 0
    update
    seeText $w merges0 mergee0
}

# Move to and highlight another diff.
proc nextMerge {delta} {
proc nextMerge {top delta} {
    global mergeSelection curMergeSel curMerge leftMergeData

    set w .merge.t
    set w $top.merge.t
    $w tag configure merge$curMerge -foreground ""

    set curMerge [expr {$curMerge + $delta}]
    if {$curMerge < 0} {set curMerge 0}
    if {$curMerge >= ([llength $leftMergeData] / 2)} {
        set curMerge [expr {[llength $leftMergeData] / 2 - 1}]
    }
    set curMergeSel $mergeSelection($curMerge)
    $w tag configure merge$curMerge -foreground red
    showDiff $curMerge
    showDiff $top $curMerge
    seeText $w merges$curMerge mergee$curMerge
}

# Select a merge setting for all diffs.
proc selectMergeAll {new} {
proc selectMergeAll {top new} {
    global leftMergeData curMerge curMergeSel
    set end [expr {[llength $leftMergeData] / 2}]
    for {set t 0} {$t < $end} {incr t} {
        selectMerge2 $t $new
        selectMerge2 $top $t $new
    }
    set curMergeSel $new
    set w .merge.t
    set w $top.merge.t
    seeText $w merges$curMerge mergee$curMerge
}

# Change merge setting fo current diff.
proc selectMerge {} {
proc selectMerge {top} {
    global curMergeSel curMerge

    set w .merge.t
    selectMerge2 $curMerge $curMergeSel
    set w $top.merge.t
    selectMerge2 $top $curMerge $curMergeSel
    seeText $w merges$curMerge mergee$curMerge
}

# Change merge setting for a diff.
proc selectMerge2 {no new} {
proc selectMerge2 {top no new} {
    global mergeSelection
    global leftMergeData rightMergeData

    set w .merge.t
    set w $top.merge.t
    # Delete current string
    $w delete merges$no mergee$no

    set mergeSelection($no) $new

    set i [expr {$no * 2 + 1}]
    set diffLeft [lindex $leftMergeData $i]
1961
1962
1963
1964
1965
1966
1967
1968

1969
1970
1971

1972
1973
1974
1975
1976
1977





1978
1979
1980
1981
1982
1983
1984

1985
1986
1987

1988
1989
1990
1991


1992
1993
1994
1995

1996
1997
1998

1999
2000
2001
2002
2003
2004
2005
2006


2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018

2019
2020

2021
2022

2023
2024
2025
2026



2027
2028
2029


2030
2031
2032
2033
2034
2035
2036






2037
2038
2039
2040



2041
2042
2043
2044
2045
2046
2047
2048


2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065


2066
2067
2068
2069
2070
2071
2072
2007
2008
2009
2010
2011
2012
2013

2014
2015
2016

2017
2018





2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029

2030
2031
2032

2033
2034
2035


2036
2037
2038
2039
2040

2041
2042
2043

2044
2045
2046
2047
2048
2049
2050


2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063

2064
2065

2066
2067

2068
2069



2070
2071
2072
2073


2074
2075
2076






2077
2078
2079
2080
2081
2082
2083



2084
2085
2086
2087
2088
2089
2090
2091
2092


2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109


2110
2111
2112
2113
2114
2115
2116
2117
2118







-
+


-
+

-
-
-
-
-
+
+
+
+
+






-
+


-
+


-
-
+
+



-
+


-
+






-
-
+
+











-
+

-
+

-
+

-
-
-
+
+
+

-
-
+
+

-
-
-
-
-
-
+
+
+
+
+
+

-
-
-
+
+
+






-
-
+
+















-
-
+
+







        $w insert merges$no $diffLeft merge$no
    } elseif {$mergeSelection($no) == 2} {
        $w insert merges$no $diffRight merge$no
    }
}

# Save the merge result.
proc saveMerge {} {
proc saveMerge {top} {
    global diff

    set w .merge.t
    set w $top.merge.t

    if {$diff(mergeFile) == ""} {
        if {[info exists diff(rightDir)]} {
            set initDir $diff(rightDir)
        } elseif {[info exists diff(leftDir)]} {
            set initDir $diff(leftDir)
    if {$diff($top,mergeFile) == ""} {
        if {[info exists diff($top,rightDir)]} {
            set initDir $diff($top,rightDir)
        } elseif {[info exists diff($top,leftDir)]} {
            set initDir $diff($top,leftDir)
        } else {
            set initDir [pwd]
        }

        set apa [tk_getSaveFile -title "Save merge file" -initialdir $initDir]
        if {$apa == ""} return
        set diff(mergeFile) $apa
        set diff($top,mergeFile) $apa
    }

    set ch [open $diff(mergeFile) w]
    set ch [open $diff($top,mergeFile) "w"]
    puts -nonewline $ch [$w get 1.0 end-1char]
    close $ch
    tk_messageBox -parent .merge -icon info -type ok -title "Diff" \
            -message "Saved merge to file $diff(mergeFile)."
    tk_messageBox -parent $top.merge -icon info -type ok -title "Diff" \
            -message "Saved merge to file $diff($top,mergeFile)."
}

# Close merge window and clean up.
proc closeMerge {} {
proc closeMerge {top} {
    global mergeSelection leftMergeData rightMergeData

    destroy .merge
    destroy $top.merge
    set leftMergeData {}
    set rightMergeData {}
    array unset mergeSelection
}

# Create a window to display merge result.
proc makeMergeWin {} {
    set w .merge
proc makeMergeWin {top} {
    set w $top.merge
    if {![winfo exists $w]} {
        toplevel $w
    } else {
        eval destroy [winfo children $w]
    }

    wm title $w "Merge result"

    frame $w.f

    radiobutton $w.f.rb1 -text "LR" -value 12 -variable curMergeSel \
            -command selectMerge
            -command "selectMerge $top"
    radiobutton $w.f.rb2 -text "L"  -value 1  -variable curMergeSel \
            -command selectMerge
            -command "selectMerge $top"
    radiobutton $w.f.rb3 -text "R"  -value 2  -variable curMergeSel \
            -command selectMerge
            -command "selectMerge $top"
    radiobutton $w.f.rb4 -text "RL" -value 21 -variable curMergeSel \
            -command selectMerge
    bind $w <Key-Left>  {focus .merge ; set curMergeSel 1 ; selectMerge}
    bind $w <Key-Right> {focus .merge ; set curMergeSel 2 ; selectMerge}
            -command "selectMerge $top"
    bind $w <Key-Left>  "focus $top.merge; set curMergeSel 1; selectMerge $top"
    bind $w <Key-Right> "focus $top.merge; set curMergeSel 2; selectMerge $top"

    button $w.f.bl -text "All L" -command {selectMergeAll 1}
    button $w.f.br -text "All R" -command {selectMergeAll 2}
    button $w.f.bl -text "All L" -command "selectMergeAll $top 1"
    button $w.f.br -text "All R" -command "selectMergeAll $top 2"

    button $w.f.b1 -text "Prev" -command {nextMerge -1}
    button $w.f.b2 -text "Next" -command {nextMerge 1}
    bind $w <Key-Down> {focus .merge ; nextMerge 1}
    bind $w <Key-Up>   {focus .merge ; nextMerge -1}
    bind $w <Shift-Key-Down> {focus .merge ; nextMerge 10}
    bind $w <Shift-Key-Up>   {focus .merge ; nextMerge -10}
    button $w.f.b1 -text "Prev" -command "nextMerge $top -1"
    button $w.f.b2 -text "Next" -command "nextMerge $top 1"
    bind $w <Key-Down> "focus $top.merge ; nextMerge $top 1"
    bind $w <Key-Up>   "focus $top.merge ; nextMerge $top -1"
    bind $w <Shift-Key-Down> "focus $top.merge ; nextMerge $top 10"
    bind $w <Shift-Key-Up>   "focus $top.merge ; nextMerge $top -10"

    button $w.f.bs -text "Save" -command saveMerge
    button $w.f.bq -text "Close" -command closeMerge
    wm protocol $w WM_CLOSE_WINDOW closeMerge
    button $w.f.bs -text "Save" -command "saveMerge $top"
    button $w.f.bq -text "Close" -command "closeMerge $top"
    wm protocol $w WM_CLOSE_WINDOW "closeMerge $top"

    grid $w.f.rb1 $w.f.rb2 $w.f.rb3 $w.f.rb4 x $w.f.b1 $w.f.b2 x \
            $w.f.bl $w.f.br x x x $w.f.bs $w.f.bq
    grid columnconfigure $w.f {4 7 10 12} -minsize 10
    grid columnconfigure $w.f 10 -weight 1

    if {[string match conflict* $::diff(mode)]} {
        checkbutton $w.f.bm -text "Pure" -variable diff(mode) \
    if {[string match conflict* $::diff($top,mode)]} {
        checkbutton $w.f.bm -text "Pure" -variable diff($top,mode) \
                -onvalue "conflictPure" -offvalue "conflict" -command {doDiff}
        grid $w.f.bm -row 0 -column 11
    }

    text $w.t -width 80 -height 20 -xscrollcommand "$w.sbx set" \
            -yscrollcommand "$w.sby set" -font myfont
    scrollbar $w.sbx -orient horizontal -command "$w.t xview"
    scrollbar $w.sby -orient vertical   -command "$w.t yview"

    grid $w.f   -      -sticky news -row 0
    grid $w.t   $w.sby -sticky news
    grid $w.sbx x      -sticky we
    grid columnconfigure $w 0 -weight 1
    grid rowconfigure $w 1 -weight 1

    collectMergeData
    fillMergeWindow
    collectMergeData $top
    fillMergeWindow $top
}

#####################################
# Printing stuff
#####################################

# Format a line number for printing
2140
2141
2142
2143
2144
2145
2146
2147
2148


2149
2150
2151
2152


2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163




2164
2165
2166
2167
2168
2169
2170
2186
2187
2188
2189
2190
2191
2192


2193
2194
2195
2196


2197
2198
2199
2200
2201
2202
2203
2204
2205




2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216







-
-
+
+


-
-
+
+







-
-
-
-
+
+
+
+







        set n [expr {(- $i - $index - 1) % 8 + 1}]
        set text [string replace $text $i $i [format %${n}s ""]]
    }
    return $text
}

# Main print function
proc printDiffs {{quiet 0}} {
    busyCursor
proc printDiffs {top {quiet 0}} {
    busyCursor $top
    update idletasks
    set tmpFile [file nativename ~/tcldiff.enscript]
    if {$::diff(printFile) != ""} {
        set tmpFile2 [file nativename $::diff(printFile)]
    if {$::diff($top,printFile) != ""} {
        set tmpFile2 [file nativename $::diff($top,printFile)]
    } else {
        set tmpFile2 [file nativename ~/tcldiff.ps]
    }

    set lines1 {}
    set lines2 {}

    set tdump1 [.ft1.tt dump -tag -text 1.0 end]
    set tdump2 [.ft2.tt dump -tag -text 1.0 end]
    set lineNo1 [processLineno .ft1.tl]
    set lineNo2 [processLineno .ft2.tl]
    set tdump1 [$::diff($top,wDiff1) dump -tag -text 1.0 end]
    set tdump2 [$::diff($top,wDiff2) dump -tag -text 1.0 end]
    set lineNo1 [processLineno $::diff($top,wLine1)]
    set lineNo2 [processLineno $::diff($top,wLine2)]

    foreach tdump [list $tdump1 $tdump2] \
            lineName {lines1 lines2} wrapName {wrap1 wrap2} \
            lineNo [list $lineNo1 $lineNo2] {
        set lines {}
        set wraps {}
        set line [lindex $lineNo 0]
2273
2274
2275
2276
2277
2278
2279
2280
2281


2282
2283
2284
2285
2286


2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304

2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317

2318
2319
2320
2321
2322

2323
2324
2325
2326
2327
2328

2329
2330
2331
2332
2333
2334
2335
2319
2320
2321
2322
2323
2324
2325


2326
2327
2328
2329
2330


2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349

2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362

2363
2364
2365
2366
2367

2368
2369
2370
2371
2372
2373

2374
2375
2376
2377
2378
2379
2380
2381







-
-
+
+



-
-
+
+

















-
+












-
+




-
+





-
+







    close $ch

    if {$::tcl_platform(platform) == "windows" &&\
            ![info exists ::env(ENSCRIPT_LIBRARY)]} {
        set ::env(ENSCRIPT_LIBRARY) [pwd]
    }
    set enscriptCmd [list enscript -2jcre]
    if {![regexp {^(.*)( \(.*?\))$} $::diff(leftLabel) -> lfile lrest]} {
        set lfile $::diff(leftLabel)
    if {![regexp {^(.*)( \(.*?\))$} $::diff($top,leftLabel) -> lfile lrest]} {
        set lfile $::diff($top,leftLabel)
        set lrest ""
    }
    set lfile [file tail $lfile]$lrest
    if {![regexp {^(.*)( \(.*?\))$} $::diff(rightLabel) -> rfile rrest]} {
        set rfile $::diff(rightLabel)
    if {![regexp {^(.*)( \(.*?\))$} $::diff($top,rightLabel) -> rfile rrest]} {
        set rfile $::diff($top,rightLabel)
        set rrest ""
    }
    set rfile [file tail $rfile]$rrest

    lappend enscriptCmd "--header=$lfile|Page \$% of \$=|$rfile"
    if {$::prettyPrint != ""} {
        lappend enscriptCmd -E$::prettyPrint
    }
    lappend enscriptCmd -p $tmpFile2 $tmpFile

    if {[catch {eval exec $enscriptCmd} result]} {
        if {[string index $result 0] != "\["} {
            tk_messageBox -message "Enscript error: $result"
            return
        }
    }

    normalCursor
    normalCursor $top
    if {!$quiet} {
        destroy .dp
        toplevel .dp
        wm title .dp "Diff Print"
        button .dp.b -text Close -command {destroy .dp}
        label .dp.l -anchor w -justify left -text "The following files have\
                been created:\n\n$tmpFile\nInput file to enscript.\
                \n\n$tmpFile2\nCreated with\
                '[lrange $enscriptCmd 0 end-3] \\\n             \
                [lrange $enscriptCmd end-2 end]'" \
                -font "Courier 8"
        pack .dp.b -side bottom
        pack .dp.l -side top
        pack .dp.l -side "top"
    }
}

# Create a print dialog.
proc doPrint {{quiet 0}} {
proc doPrint {top {quiet 0}} {
    if {![info exists ::grayLevel1]} {
        set ::grayLevel1 0.6
        set ::grayLevel2 0.8
    }
    if {$quiet} {
        printDiffs 1
        printDiffs $top 1
        return
    }

    destroy .pr
    toplevel .pr
    wm title .pr "Print diffs"

2352
2353
2354
2355
2356
2357
2358
2359

2360
2361
2362
2363
2364
2365
2366
2398
2399
2400
2401
2402
2403
2404

2405
2406
2407
2408
2409
2410
2411
2412







-
+







    frame .pr.f
    radiobutton .pr.r1 -text "No Syntax" -variable prettyPrint -value ""
    radiobutton .pr.r2 -text "VHDL" -variable prettyPrint -value "vhdl"
    radiobutton .pr.r3 -text "Tcl"  -variable prettyPrint -value "tcl"
    radiobutton .pr.r4 -text "C"    -variable prettyPrint -value "c"

    button .pr.b1 -text Print -width 7 \
            -command {destroy .pr; update; printDiffs}
            -command "destroy .pr; update; printDiffs $top"
    button .pr.b2 -text Cancel -width 7 \
            -command {destroy .pr}

    grid .pr.l1 - -sticky we
    grid .pr.l2 - -sticky we
    grid .pr.s1 - -sticky we
    grid .pr.s2 - -sticky we
2379
2380
2381
2382
2383
2384
2385
2386
2387


2388
2389
2390
2391
2392

2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408

2409
2410
2411

2412
2413
2414

2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431

2432
2433

2434
2435

2436
2437
2438
2439
2440
2441



2442
2443
2444
2445



2446
2447
2448
2449
2450
2451
2452
2453
2454

2455
2456
2457


2458
2459
2460
2461
2462
2463

2464

2465

2466
2467
2468

2469
2470
2471
2472

2473
2474
2475

2476
2477
2478

2479
2480
2481

2482
2483
2484
2485
2486
2487


2488
2489
2490
2491





2492
2493

2494
2495

2496
2497
2498


2499
2500


2501
2502
2503
2504

2505
2506
2507

2508
2509
2510

2511
2512
2513


2514
2515
2516
2517
2518


2519
2520
2521
2522
2523
2524

2525
2526
2527
2528

2529
2530
2531
2532
2533
2534
2535


2536
2537

2538
2539

2540
2541
2542
2543
2544

2545

2546
2547
2548
2549
2550

2551
2552
2553
2554

2555
2556

2557
2558
2559

2560
2561
2562
2563
2564
2565




2566
2567
2568
2569
2570

2571
2572
2573
2574
2575
2576
2577
2578

2579
2580
2581
2582
2583
2584
2585



2586
2587

2588
2589
2590
2591
2592
2593
2594




2595
2596
2597
2598

2599
2600

2601
2602

2603
2604

2605
2606

2607
2608
2609
2610

2611
2612
2613
2614
2615
2616
2617
2618
2619
2620

2621
2622
2623

2624
2625

2626
2627
2628
2629
2630
2631
2632

2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652


2653
2654
2655
2656


2657
2658
2659
2660


2661
2662
2663
2664



2665
2666
2667
2668
2669



2670
2671
2672
2673
2674
2675
2676



2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689



2690
2691
2692
2693
2694
2695
2696







2697
2698

2699
2700
2701
2702
2703
2704
2705





2706
2707
2708
2709
2710
2711
2712
2425
2426
2427
2428
2429
2430
2431


2432
2433
2434
2435
2436
2437

2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453

2454
2455
2456

2457
2458
2459

2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476

2477
2478

2479
2480

2481
2482
2483
2484



2485
2486
2487
2488



2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499

2500
2501


2502
2503
2504
2505
2506
2507
2508

2509
2510
2511

2512
2513
2514

2515
2516
2517
2518

2519
2520
2521

2522
2523
2524

2525
2526
2527

2528
2529
2530
2531
2532


2533
2534
2535
2536


2537
2538
2539
2540
2541
2542

2543
2544

2545
2546


2547
2548
2549

2550
2551
2552
2553
2554

2555
2556
2557

2558
2559
2560

2561
2562


2563
2564
2565
2566
2567


2568
2569
2570
2571
2572
2573
2574

2575
2576
2577
2578

2579
2580
2581
2582
2583
2584


2585
2586
2587

2588
2589

2590
2591
2592
2593
2594
2595
2596

2597
2598
2599
2600
2601

2602
2603
2604
2605

2606
2607

2608
2609
2610

2611
2612
2613




2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630

2631
2632
2633
2634
2635



2636
2637
2638
2639

2640
2641
2642
2643




2644
2645
2646
2647
2648
2649
2650

2651
2652

2653
2654

2655
2656

2657
2658

2659
2660

2661

2662
2663
2664
2665
2666
2667
2668
2669
2670
2671

2672
2673
2674

2675
2676

2677
2678
2679
2680
2681
2682
2683

2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702


2703
2704
2705
2706


2707
2708
2709
2710


2711
2712
2713
2714


2715
2716
2717
2718
2719
2720


2721
2722
2723
2724
2725
2726
2727



2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746







2747
2748
2749
2750
2751
2752
2753


2754
2755
2756
2757
2758



2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770







-
-
+
+




-
+















-
+


-
+


-
+
















-
+

-
+

-
+



-
-
-
+
+
+

-
-
-
+
+
+








-
+

-
-
+
+





-
+

+
-
+


-
+



-
+


-
+


-
+


-
+




-
-
+
+


-
-
+
+
+
+
+

-
+

-
+

-
-
+
+

-
+
+



-
+


-
+


-
+

-
-
+
+



-
-
+
+





-
+



-
+





-
-
+
+

-
+

-
+





+
-
+




-
+



-
+

-
+


-
+


-
-
-
-
+
+
+
+





+







-
+




-
-
-
+
+
+

-
+



-
-
-
-
+
+
+
+



-
+

-
+

-
+

-
+

-
+

-

-
+









-
+


-
+

-
+






-
+


















-
-
+
+


-
-
+
+


-
-
+
+


-
-
+
+
+



-
-
+
+
+




-
-
-
+
+
+













+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
+




-
-
-
+
+
+
+
+







    set raw [binary format I $p]
    binary scan $raw B* bin
    set bin [string trimleft [string range $bin 0 end-8] 0][string range $bin end-7 end]
    set pat [string map {0 . 1 ,} $bin]
    return $pat
}

proc runAlign {} {
    if {![info exists ::diff(aligns)] || [llength $::diff(aligns)] == 0} {
proc runAlign {top} {
    if {![info exists ::diff($top,aligns)] || [llength $::diff($top,aligns)] == 0} {
        return
    }

    set pattern 0
    foreach align $::diff(aligns) {
    foreach align $::diff($top,aligns) {
        foreach {lline rline level} $align break

        set pre {}
        set post {}
        for {set t 1} {$t <= $level} {incr t} {
            lappend pre [formatAlignPattern $pattern]
            incr pattern
            lappend post [formatAlignPattern $pattern]
            incr pattern
        }

        set fix1($lline) [list [join $pre \n] [join $post \n]]
        set fix2($rline) [list [join $pre \n] [join $post \n]]
    }

    prepareFiles
    prepareFiles $top
    foreach n {1 2} src {leftFile rightFile} {
        set tmp [tmpfile]
        set f$n $tmp
        set f($n) $tmp
        set cho [open $tmp w]
        #puts $cho hej
        set chi [open $::diff($src) r]
        set chi [open $::diff($top,$src) r]
        set lineNo 1
        while {[gets $chi line] >= 0} {
            if {[info exists fix${n}($lineNo)]} {
                foreach {pre post} [set fix${n}($lineNo)] break
                puts $cho $pre
                puts $cho $line
                puts $cho $post
            } else {
                puts $cho $line
            }
            incr lineNo
        }
        close $cho
        close $chi
    }
    # FIXA : detta tar bort tmpfiles
    cleanupFiles
    cleanupFiles $top

    catch {eval exec $::diff(thisexe) \$f1 \$f2 &}
    catch {eval exec $::diff(thisexe) [list $f(1) $f(2)] &}

    set ::diff(aligns) ""
    set ::diff($top,aligns) ""
}

# Mark a line as aligned.
proc markAlign {n line text} {
    set ::diff(align$n) $line
    set ::diff(aligntext$n) $text
proc markAlign {top n line text} {
    set ::diff($top,align$n) $line
    set ::diff($top,aligntext$n) $text

    if {[info exists ::diff(align1)] && [info exists ::diff(align2)]} {
        set level 1
        if {![string equal $::diff(aligntext1) $::diff(aligntext2)]} {
    if {[info exists ::diff($top,align1)] && [info exists ::diff($top,align2)]} {
        set level 2
        if {![string equal $::diff($top,aligntext1) $::diff($top,aligntext2)]} {
            set apa [tk_messageBox -icon question -title "Align" -type yesno \
                    -message "Those lines are not equal.\nReally align them?"]
            if {$apa != "yes"} {
                return
            }
            set level 3
        }

        lappend ::diff(aligns) [list $::diff(align1) $::diff(align2) $level]
        lappend ::diff($top,aligns) [list $::diff($top,align1) $::diff($top,align2) $level]

        unset ::diff(align1)
        unset ::diff(align2)
        unset ::diff($top,align1)
        unset ::diff($top,align2)
    }
}

# Called by popup menus over row numbers to add command for alignment.
# Returns 1 of nothing added.
proc alignMenu {m n x y} {
proc alignMenu {m top n x y} {
    # Get the row that was clicked
    set w $::diff($top,wLine$n)
    set index [.ft$n.tl index @$x,$y]
    set index [$w index @$x,$y]
    set row [lindex [split $index "."] 0]

    set data [.ft$n.tl get $row.0 $row.end]
    set data [$w get $row.0 $row.end]
    if {![regexp {\d+} $data line]} {
        return 1
    }
    set text [.ft$n.tt get $row.0 $row.end]
    set text [$::diff($top,wDiff$n) get $row.0 $row.end]

    set other [expr {$n == 1 ? 2 : 1}]
    if {![info exists ::diff(align$other)]} {
    if {![info exists ::diff($top,align$other)]} {
        set label "Mark line for alignment"
    } else {
        set label "Align with line $::diff(align$other) on other side"
        set label "Align with line $::diff($top,align$other) on other side"
    }

    .lpm add command -label $label -command [list markAlign $n $line $text]
    .lpm add command -label $label -command [list markAlign $top $n $line $text]
    return 0
}


proc hlSelect {hl} {
    highLightChange $hl
proc hlSelect {top hl} {
    highLightChange $top $hl
}

proc hlSeparate {n hl} {
    set ::diff(separate$n) $hl
proc hlSeparate {top n hl} {
    set ::diff($top,separate$n) $hl
    set wd $::diff($top,wDiff$n)
    set wl $::diff($top,wLine$n)

    if {$hl == ""} {
        set range [.ft$n.tt tag ranges sel]
        set range [$wd tag ranges sel]
    } else {
        set range [.ft$n.tl tag ranges hl$::diff(separate$n)]
        set range [$wl tag ranges hl$::diff($top,separate$n)]
    }
    set text [eval .ft$n.tt get $range]
    set ::diff(separatetext$n) $text
    set text [eval $wd get $range]
    set ::diff($top,separatetext$n) $text

    if {[info exists ::diff(separate1)] && [info exists ::diff(separate2)]} {
    if {[info exists ::diff($top,separate1)] && \
            [info exists ::diff($top,separate2)]} {
        set f1 [tmpfile]
        set f2 [tmpfile]
        set ch [open $f1 w]
        puts $ch $::diff(separatetext1)
        puts $ch $::diff($top,separatetext1)
        close $ch
        set ch [open $f2 w]
        puts $ch $::diff(separatetext2)
        puts $ch $::diff($top,separatetext2)
        close $ch

        catch {eval exec $::diff(thisexe) \$f1 \$f2 &}
        catch {eval exec $::diff($top,thisexe) [list $f1 $f2] &}

        unset ::diff(separate1)
        unset ::diff(separate2)
        unset ::diff($top,separate1)
        unset ::diff($top,separate2)
    }
}

proc hlPopup {n hl X Y x y} {
    if {[info exists ::diff(nopopup)] && $::diff(nopopup)} return
proc hlPopup {top n hl X Y x y} {
    if {[info exists ::diff($top,nopopup)] && $::diff($top,nopopup)} return
    destroy .lpm
    menu .lpm -tearoff 0

    if {$hl != ""} {
        .lpm add command -label "Select" \
                -command [list hlSelect $hl]
                -command [list hlSelect $top $hl]
    }

    set other [expr {$n == 1 ? 2 : 1}]
    if {![info exists ::diff(separate$other)]} {
    if {![info exists ::diff($top,separate$other)]} {
        set label "Mark for Separate Diff"
    } else {
        set label "Separate Diff"
    }

    .lpm add command -label $label -command [list hlSeparate $n $hl]
    alignMenu .lpm $n $x $y
    .lpm add command -label $label -command [list hlSeparate $top $n $hl]
    alignMenu .lpm $top $n $x $y

    set ::diff(nopopup) 1
    set ::diff($top,nopopup) 1
    tk_popup .lpm $X $Y
    after idle {after 1 {set ::diff(nopopup) 0}}
    after idle [list after 1 [list set ::diff($top,nopopup) 0]]

    return
}

proc rowPopup {w X Y x y} {
    set top [winfo toplevel $w]
    if {[info exists ::diff(nopopup)] && $::diff(nopopup)} return
    if {[info exists ::diff($top,nopopup)] && $::diff($top,nopopup)} return
    destroy .lpm
    menu .lpm -tearoff 0

    regexp {\d+} $w n
    if {[alignMenu .lpm $n $x $y]} {
    if {[alignMenu .lpm $top $n $x $y]} {
        return
    }

    set ::diff(nopopup) 1
    set ::diff($top,nopopup) 1
    tk_popup .lpm $X $Y
    after idle {after 1 {set ::diff(nopopup) 0}}
    after idle [list after 1 [list set ::diff($top,nopopup) 0]]
}

proc bindHighlight {} {
proc bindHighlight {top} {
    set tag hl$::HighLightCount
    foreach n {1 2} {
        .ft$n.tl tag bind $tag <ButtonPress-3> \
                "hlPopup $n $::HighLightCount %X %Y %x %y ; break"
        .ft$n.tl tag bind $tag <ButtonPress-1> \
                "hlSelect $::HighLightCount"
        $::diff($top,wLine$n) tag bind $tag <ButtonPress-3> \
                "hlPopup $top $n $::HighLightCount %X %Y %x %y ; break"
        $::diff($top,wLine$n) tag bind $tag <ButtonPress-1> \
                "hlSelect $top $::HighLightCount"
    }
}

proc zoomRow {w X Y x y} {
    global Pref
    set top [winfo toplevel $w]
    # Get the row that was clicked
    set index [$w index @$x,$y]
    set row [lindex [split $index "."] 0]

    # Check if it is selected
    if {[lsearch [$w tag names $index] sel] >= 0} {
        regexp {\d+} $w n
        hlPopup $n "" $X $Y $x $y
        hlPopup $top $n "" $X $Y $x $y
        return
    }

    # Extract the data
    set data1 [.ft1.tt dump -tag -text $row.0 $row.end]
    set data2 [.ft2.tt dump -tag -text $row.0 $row.end]
    if {[llength $data1] == 0 && [llength $data2] == 0} return
    set data(1) [$::diff($top,wDiff1) dump -tag -text $row.0 $row.end]
    set data(2) [$::diff($top,wDiff2) dump -tag -text $row.0 $row.end]
    if {[llength $data(1)] == 0 && [llength $data(2)] == 0} return

    set font [.ft1.tt cget -font]
    set font [$::diff($top,wDiff1) cget -font]
    set wx $X
    set wy [expr {$Y + 4}]

    destroy .balloon
    toplevel .balloon -bg black
    wm withdraw .balloon
    wm overrideredirect .balloon 1
    destroy $top.balloon
    toplevel $top.balloon -bg black
    wm withdraw $top.balloon
    wm overrideredirect $top.balloon 1

    set wid 0
    foreach x {1 2} {
        text .balloon.t$x -relief flat -font $font -bg #ffffaa -fg black \
        text $top.balloon.t$x -relief flat -font $font -bg #ffffaa -fg black \
                -padx 2 -pady 0 -height 1 -wrap word
        .balloon.t$x tag configure new1 -foreground $Pref(colornew1) \
        $top.balloon.t$x tag configure new1 -foreground $Pref(colornew1) \
                -background $Pref(bgnew1)
        .balloon.t$x tag configure change -foreground $Pref(colorchange) \
        $top.balloon.t$x tag configure change -foreground $Pref(colorchange) \
                -background $Pref(bgchange)
        .balloon.t$x tag configure new2 -foreground $Pref(colornew2) \
        $top.balloon.t$x tag configure new2 -foreground $Pref(colornew2) \
                -background $Pref(bgnew2)
        pack .balloon.t$x -side top -padx 1 -pady 1 -fill both -expand 1
        pack $top.balloon.t$x -side "top" -padx 1 -pady 1 -fill both -expand 1

        set data [set data$x]
        set tags {}
        foreach {key value index} $data {
        foreach {key value index} $data($x) {
            if {$key == "tagon"} {
                lappend tags $value
                set tags [lsort -unique $tags]
            } elseif {$key == "tagoff"} {
                set i [lsearch $tags $value]
                if {$i >= 0} {
                    set tags [lreplace $tags $i $i]
                }
            } else {
                .balloon.t$x insert end $value $tags
                $top.balloon.t$x insert end $value $tags
            }
        }
        set text [.balloon.t$x get 1.0 1.end]
        set text [$top.balloon.t$x get 1.0 1.end]
        regsub -all "\t" $text "        " text
        .balloon.t$x configure -width [string length $text]
        $top.balloon.t$x configure -width [string length $text]
    }

    # Let geometry requests propagate
    update idletasks

    # Is the balloon within the diff window?
    set wid [winfo reqwidth .balloon]
    set wid [winfo reqwidth $top.balloon]
    if {$wid + $wx > [winfo rootx .] + [winfo width .]} {
        # No.
        # Center on diff window
        set wx [expr {([winfo width .] - $wid) / 2 + [winfo rootx .]}]
        if {$wx < 0} {set wx 0}
        # Is the balloon not within the screen?
        if {$wx + $wid > [winfo screenwidth .]} {
            # Center in screen
            set wx [expr {([winfo screenwidth .] - $wid) / 2}]
            if {$wx < 0} {set wx 0}
        }
    }

    # Does the balloon fit within the screen?
    if {$wid > [winfo screenwidth .]} {
        # How many rows does it take?
        set rows [expr {ceil(double($wid) / [winfo screenwidth .])}]
        # Add rows and fill screen width
        .balloon.t1 configure -height $rows
        .balloon.t2 configure -height $rows
        $top.balloon.t1 configure -height $rows
        $top.balloon.t2 configure -height $rows
        # Let geometry requests propagate
        update idletasks
        wm geometry .balloon \
                [winfo screenwidth .]x[winfo reqheight .balloon]
        wm geometry $top.balloon \
                [winfo screenwidth .]x[winfo reqheight $top.balloon]
        set wx 0
    }
    wm geometry .balloon +$wx+$wy
    wm deiconify .balloon
    wm geometry $top.balloon +$wx+$wy
    wm deiconify $top.balloon
}

proc unzoomRow {} {
    destroy .balloon
proc unzoomRow {w} {
    set top [winfo toplevel $w]
    destroy $top.balloon
}

# Procedures for common y-scroll
proc my_yview args {
    foreach w {.ft1.tl .ft1.tt .ft2.tl .ft2.tt} {
proc my_yview {top args} {
    foreach item {wLine1 wDiff1 wLine2 wDiff2} {
        set w $::diff($top,$item)
        eval $w yview $args
    }
}

proc my_yscroll args {
    eval .sby set $args
    my_yview moveto [lindex $args 0]
proc my_yscroll {top args} {
    eval $top.sby set $args
    my_yview $top moveto [lindex $args 0]
}

# Reconfigure font
proc chFont {} {
    global Pref

    font configure myfont -size $Pref(fontsize) -family $Pref(fontfamily)
}

# Change color settings
proc applyColor {} {
    global Pref

    foreach top $::diff(diffWindows) {
        foreach item {wLine1 wDiff1 wLine2 wDiff2} {
            set w $::diff($top,$item)
    foreach w {.tl .tt} {
        .ft1$w tag configure new1 -foreground $Pref(colornew1) \
                -background $Pref(bgnew1)
        .ft1$w tag configure change -foreground $Pref(colorchange) \
                -background $Pref(bgchange)
        .ft2$w tag configure new2 -foreground $Pref(colornew2) \
                -background $Pref(bgnew2)

            $w tag configure new1 -foreground $Pref(colornew1) \
                    -background $Pref(bgnew1)
            $w tag configure change -foreground $Pref(colorchange) \
                    -background $Pref(bgchange)
            $w tag configure new2 -foreground $Pref(colornew2) \
                    -background $Pref(bgnew2)
        .ft2$w tag configure change -foreground $Pref(colorchange) \
                -background $Pref(bgchange)
        }
    }
}

# Scroll text windows
proc scroll {n what} {
    if {![string match ".ft?.tt" [focus]]} {
        .ft1.tt yview scroll $n $what
proc scroll {top n what} {
    # Do not scroll if focus is in a text window.
    # This is for scroll bindings in the toplevel.
    if {[winfo class [focus]] != "Text"} {
        $::diff($top,wDiff1) yview scroll $n $what
    }
}

# Emulate a label that:
# 1 : Displays the right part of the text if there isn't enough room
# 2 : Justfify text to the left if there is enough room.
# 3 : Does not try to allocate space according to its contents
2725
2726
2727
2728
2729
2730
2731
2732

2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
















2744
2745
2746


2747
2748
2749


2750
2751


2752
2753


2754
2755
2756
2757
2758
2759






2760
2761
2762







2763
2764


2765
2766
2767


2768
2769
2770
2771
2772









2773
2774
2775
2776
2777
2778
2779
2780







2781
2782

2783
2784
2785
2786


2787
2788
2789
2790



2791
2792

2793
2794

2795
2796

2797
2798

2799
2800
2801
2802
2803



2804
2805

2806
2807
2808
2809
2810
2811




2812
2813
2814
2815



2816
2817

2818
2819
2820


2821
2822

2823
2824
2825
2826
2827




2828
2829


2830
2831
2832
2833
2834
2835




2836
2837
2838


2839
2840
2841
2842
2843
2844
2845
2846
2847
2848









2849
2850


2851
2852
2853


2854
2855
2856
2857
2858
2859
2860
2861


2862
2863
2864



2865
2866
2867
2868




2869
2870
2871
2872
2873
2874
2875








2876
2877
2878



2879
2880
2881
2882




2883
2884
2885
2886
2887
2888







2889
2890
2891
2892
2893



2894
2895
2896
2897


2898
2899
2900
2901




2902
2903
2904
2905
2906
2907



2908
2909
2910

2911
2912

2913
2914
2915
2916
2917
2918
2919
2920
2921
2922







2923
2924
2925
2926



2927
2928
2929
2930
2931
2932





2933
2934
2935
2936




2937
2938
2939


2940
2941

2942
2943
2944

2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966



























2967
2968

2969
2970
2971
2972
2973
2974
2975
2783
2784
2785
2786
2787
2788
2789

2790
2791
2792
2793
2794
2795
2796
2797




2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814


2815
2816
2817


2818
2819
2820

2821
2822
2823

2824
2825
2826
2827




2828
2829
2830
2831
2832
2833



2834
2835
2836
2837
2838
2839
2840
2841

2842
2843
2844
2845

2846
2847
2848




2849
2850
2851
2852
2853
2854
2855
2856
2857
2858







2859
2860
2861
2862
2863
2864
2865
2866

2867
2868
2869


2870
2871
2872



2873
2874
2875
2876

2877
2878

2879
2880

2881
2882

2883
2884
2885



2886
2887
2888
2889

2890
2891
2892




2893
2894
2895
2896
2897



2898
2899
2900
2901

2902
2903


2904
2905
2906

2907
2908




2909
2910
2911
2912
2913

2914
2915
2916
2917




2918
2919
2920
2921
2922


2923
2924
2925
2926








2927
2928
2929
2930
2931
2932
2933
2934
2935
2936

2937
2938
2939


2940
2941
2942
2943
2944
2945




2946
2947
2948


2949
2950
2951
2952
2953


2954
2955
2956
2957
2958






2959
2960
2961
2962
2963
2964
2965
2966
2967


2968
2969
2970
2971
2972


2973
2974
2975
2976
2977





2978
2979
2980
2981
2982
2983
2984
2985
2986



2987
2988
2989
2990
2991


2992
2993
2994



2995
2996
2997
2998
2999
3000
3001



3002
3003
3004
3005
3006

3007
3008

3009
3010
3011
3012







3013
3014
3015
3016
3017
3018
3019
3020



3021
3022
3023
3024





3025
3026
3027
3028
3029
3030



3031
3032
3033
3034
3035


3036
3037
3038

3039
3040
3041

3042
3043





















3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071

3072
3073
3074
3075
3076
3077
3078
3079







-
+







-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

-
-
+
+

-
-
+
+

-
+
+

-
+
+


-
-
-
-
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+

-
+
+


-
+
+

-
-
-
-
+
+
+
+
+
+
+
+
+

-
-
-
-
-
-
-
+
+
+
+
+
+
+

-
+


-
-
+
+

-
-
-
+
+
+

-
+

-
+

-
+

-
+


-
-
-
+
+
+

-
+


-
-
-
-
+
+
+
+

-
-
-
+
+
+

-
+

-
-
+
+

-
+

-
-
-
-
+
+
+
+

-
+
+


-
-
-
-
+
+
+
+

-
-
+
+


-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+

-
+
+

-
-
+
+




-
-
-
-
+
+

-
-
+
+
+


-
-
+
+
+
+

-
-
-
-
-
-
+
+
+
+
+
+
+
+

-
-
+
+
+


-
-
+
+
+
+

-
-
-
-
-
+
+
+
+
+
+
+


-
-
-
+
+
+


-
-
+
+

-
-
-
+
+
+
+



-
-
-
+
+
+


-
+

-
+



-
-
-
-
-
-
-
+
+
+
+
+
+
+

-
-
-
+
+
+

-
-
-
-
-
+
+
+
+
+

-
-
-
+
+
+
+

-
-
+
+

-
+


-
+

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

-
+







    if {[info tclversion] >= 8.4} {
        $w configure -state readonly -readonlybackground $bg
    }

    set i [lsearch $args -textvariable]
    if {$i >= 0} {
	set var [lindex $args [expr {$i + 1}]]
	uplevel #0 "trace variable $var w \
	uplevel \#0 "trace variable $var w \
		{after idle {$w xview end} ;#}"
    }
}

# Build the main window
proc makeDiffWin {} {
    global Pref tcl_platform debug
    eval destroy [winfo children .]

    option add *Menu.tearOff 0
    wm protocol . WM_DELETE_WINDOW cleanupAndExit
    
    # Locate a free toplevel name
    if {[info exists ::diff(topDiffCnt)]} {
        set t $::diff(topDiffCnt)
    } else {
        set t 0
    }
    while {[winfo exists .diff$t]} {
        incr t
    }
    set top .diff$t
    lappend ::diff(diffWindows) $top

    toplevel $top
    wm title $top "Diff"
    wm protocol $top WM_DELETE_WINDOW [list cleanupAndExit $top]

    frame .f
    grid .f - - - -row 0 -sticky news
    frame $top.f
    grid $top.f -row 0 -columnspan 4 -sticky news

    menubutton .mf -text File -underline 0 -menu .mf.m
    menu .mf.m
    menubutton $top.mf -text File -underline 0 -menu $top.mf.m
    menu $top.mf.m
    if {$debug == 1} {
        .mf.m add command -label "Redo Diff" -underline 5 -command doDiff
        $top.mf.m add command -label "Redo Diff" -underline 5 \
                -command [list doDiff $top]
    } else {
        .mf.m add command -label "Redo Diff" -underline 5 -command doDiff \
        $top.mf.m add command -label "Redo Diff" -underline 5 \
                -command [list doDiff $top] \
                -state disabled
    }
    .mf.m add separator
    .mf.m add command -label "Open Both..." -underline 0 -command {openBoth 0}
    .mf.m add command -label "Open Both (forget)..." -command {openBoth 1}
    .mf.m add command -label "Open Left File..." -command openLeft
    $top.mf.m add separator
    $top.mf.m add command -label "Open Both..." -underline 0 \
            -command [list openBoth $top 0]
    $top.mf.m add command -label "Open Both (forget)..." \
            -command [list openBoth $top 1]
    $top.mf.m add command -label "Open Left File..." \
    .mf.m add command -label "Open Right File..." -command openRight
    .mf.m add command -label "Open Conflict File..." -command openConflict
    .mf.m add command -label "Open Patch File..." -command openPatch
            -command [list openLeft $top]
    $top.mf.m add command -label "Open Right File..." \
            -command [list openRight $top]
    $top.mf.m add command -label "Open Conflict File..." \
            -command [list openConflict $top]
    $top.mf.m add command -label "Open Patch File..." \
            -command [list openPatch $top]
    if {$tcl_platform(platform) == "unix"} {
        .mf.m add command -label "RCSDiff..." -underline 0 -command openRCS
        $top.mf.m add command -label "RCSDiff..." -underline 0 \
                -command [list openRCS $top]
    }
    if {$::diff(cvsExists)} {
        .mf.m add command -label "CVSDiff..." -underline 0 -command openCVS
        $top.mf.m add command -label "CVSDiff..." -underline 1 \
                -command [list openCVS $top]
    }
    .mf.m add separator
    .mf.m add command -label "Print..." -underline 0 -command doPrint
    .mf.m add separator
    .mf.m add command -label "Quit" -underline 0 -command cleanupAndExit
    $top.mf.m add separator
    $top.mf.m add command -label "Print..." -underline 0 \
            -command [list doPrint $top]
    $top.mf.m add separator
    $top.mf.m add command -label "Close" -underline 0 \
            -command [list cleanupAndExit $top]
    $top.mf.m add separator
    $top.mf.m add command -label "Quit" -underline 0 \
            -command {cleanupAndExit all}

    menubutton .mo -text "Options" -underline 0 -menu .mo.m
    menu .mo.m
    .mo.m add cascade -label "Font" -underline 0 -menu .mo.mf
    .mo.m add cascade -label "Ignore" -underline 0 -menu .mo.mi
    .mo.m add cascade -label "Parse" -underline 0 -menu .mo.mp
    .mo.m add command -label "Colours..." -underline 0 -command makePrefWin
    .mo.m add checkbutton -label "Diffs only" -variable Pref(onlydiffs)
    menubutton $top.mo -text "Options" -underline 0 -menu $top.mo.m
    menu $top.mo.m
    $top.mo.m add cascade -label "Font" -underline 0 -menu $top.mo.mf
    $top.mo.m add cascade -label "Ignore" -underline 0 -menu $top.mo.mi
    $top.mo.m add cascade -label "Parse" -underline 0 -menu $top.mo.mp
    $top.mo.m add command -label "Colours..." -underline 0 -command makePrefWin
    $top.mo.m add checkbutton -label "Diffs only" -variable Pref(onlydiffs)
    if {$tcl_platform(platform) == "windows"} {
        .mo.m add checkbutton -label "Force crlf translation" \
        $top.mo.m add checkbutton -label "Force crlf translation" \
                -variable Pref(crlf)
    }
    .mo.m add separator
    .mo.m add command -label "Save default" -command saveOptions
    $top.mo.m add separator
    $top.mo.m add command -label "Save default" -command saveOptions

    menu .mo.mf
    .mo.mf add command -label "Select..." -command makeFontWin
    .mo.mf add radiobutton -label 6 -variable Pref(fontsize) -value 6 \
    menu $top.mo.mf
    $top.mo.mf add command -label "Select..." -command makeFontWin
    $top.mo.mf add radiobutton -label 6 -variable Pref(fontsize) -value 6 \
            -command chFont
    .mo.mf add radiobutton -label 7 -variable Pref(fontsize) -value 7 \
    $top.mo.mf add radiobutton -label 7 -variable Pref(fontsize) -value 7 \
            -command chFont
    .mo.mf add radiobutton -label 8 -variable Pref(fontsize) -value 8 \
    $top.mo.mf add radiobutton -label 8 -variable Pref(fontsize) -value 8 \
            -command chFont
    .mo.mf add radiobutton -label 9 -variable Pref(fontsize) -value 9 \
    $top.mo.mf add radiobutton -label 9 -variable Pref(fontsize) -value 9 \
            -command chFont
    .mo.mf add radiobutton -label 10 -variable Pref(fontsize) -value 10 \
    $top.mo.mf add radiobutton -label 10 -variable Pref(fontsize) -value 10 \
            -command chFont

    menu .mo.mi
    .mo.mi add radiobutton -label "Nothing" -variable Pref(ignore) -value " "
    .mo.mi add radiobutton -label "Space changes (-b)" -variable Pref(ignore) \
    menu $top.mo.mi
    $top.mo.mi add radiobutton -label "Nothing" -variable Pref(ignore) -value " "
    $top.mo.mi add radiobutton -label "Space changes (-b)" -variable Pref(ignore) \
            -value "-b"
    .mo.mi add radiobutton -label "All spaces (-w)" -variable Pref(ignore) \
    $top.mo.mi add radiobutton -label "All spaces (-w)" -variable Pref(ignore) \
            -value "-w"

    menu .mo.mp
    .mo.mp add radiobutton -label "Nothing" -variable Pref(parse) -value 0
    .mo.mp add radiobutton -label "Lines" -variable Pref(parse) -value 1
    .mo.mp add radiobutton -label "Blocks (small)" -variable Pref(parse) \
    menu $top.mo.mp
    $top.mo.mp add radiobutton -label "Nothing" -variable Pref(parse) -value 0
    $top.mo.mp add radiobutton -label "Lines" -variable Pref(parse) -value 1
    $top.mo.mp add radiobutton -label "Blocks (small)" -variable Pref(parse) \
            -value 2
    .mo.mp add radiobutton -label "Blocks" -variable Pref(parse) -value 3
    .mo.mp add separator
    .mo.mp add radiobutton -label "Characters" -variable Pref(lineparsewords) \
    $top.mo.mp add radiobutton -label "Blocks" -variable Pref(parse) -value 3
    $top.mo.mp add separator
    $top.mo.mp add radiobutton -label "Characters" -variable Pref(lineparsewords) \
            -value "0"
    .mo.mp add radiobutton -label "Words" -variable Pref(lineparsewords) \
    $top.mo.mp add radiobutton -label "Words" -variable Pref(lineparsewords) \
            -value "1"
    .mo.mp add separator
    .mo.mp add checkbutton -label "Use 2nd stage" \
    $top.mo.mp add separator
    $top.mo.mp add checkbutton -label "Use 2nd stage" \
            -variable Pref(extralineparse)
    .mo.mp add checkbutton -label "Mark last" -variable Pref(marklast)
    $top.mo.mp add checkbutton -label "Mark last" -variable Pref(marklast)

    menubutton .ms -text Search -underline 0 -menu .ms.m
    menu .ms.m
    if {[info proc textSearch::searchMenu] != ""} {
        textSearch::searchMenu .ms.m
    menubutton $top.ms -text Search -underline 0 -menu $top.ms.m
    menu $top.ms.m
    if {[info procs textSearch::searchMenu] != ""} {
        textSearch::searchMenu $top.ms.m
    } else {
        .ms.m add command -label "Text search not available" -state disabled
        $top.ms.m add command -label "Text search not available" \
                -state disabled
    }

    menubutton .mh -text Help -underline 0 -menu .mh.m
    menu .mh.m
    .mh.m add command -label "Help" -command makeHelpWin
    .mh.m add command -label "About" -command makeAboutWin
    menubutton $top.mh -text Help -underline 0 -menu $top.mh.m
    menu $top.mh.m
    $top.mh.m add command -label "Help" -command makeHelpWin
    $top.mh.m add command -label "About" -command makeAboutWin

    label .lo -text "Diff Options"
    addBalloon .lo "Options passed to the external diff.\nNote\
    label $top.lo -text "Diff Options"
    addBalloon $top.lo "Options passed to the external diff.\nNote\
            that options for ignoring whitespace are available in\
            the Options menu."
    entry .eo -width 6 -textvariable Pref(dopt)
    label .lr1 -text "Rev 1"
    addBalloon .lr1 "Revision number for CVS/RCS diff."
    entry .er1 -width 6 -textvariable Pref(doptrev1)
    label .lr2 -text "Rev 2"
    addBalloon .lr2 "Revision number for CVS/RCS diff."
    entry .er2 -width 6 -textvariable Pref(doptrev2)
    button .bfp -text "Prev Diff" -relief raised -command {findDiff -1} \
    entry $top.eo -width 6 -textvariable diff($top,dopt)
    label $top.lr1 -text "Rev 1"
    addBalloon $top.lr1 "Revision number for CVS/RCS diff."
    entry $top.er1 -width 6 -textvariable diff($top,doptrev1)
    label $top.lr2 -text "Rev 2"
    addBalloon $top.lr2 "Revision number for CVS/RCS diff."
    entry $top.er2 -width 6 -textvariable diff($top,doptrev2)
    button $top.bfp -text "Prev Diff" -relief raised \
            -command [list findDiff $top -1] \
            -underline 0 -padx 15
    button .bfn -text "Next Diff" -relief raised -command {findDiff 1} \
    button $top.bfn -text "Next Diff" -relief raised \
            -command [list findDiff $top 1] \
            -underline 0 -padx 15
    bind . <Alt-n> {findDiff 1}
    bind . <Alt-p> {findDiff -1}
    bind $top <Alt-n> [list findDiff $top 1]
    bind $top <Alt-p> [list findDiff $top -1]

    catch {font delete myfont}
    font create myfont -family $Pref(fontfamily) -size $Pref(fontsize)

    #label .l1 -textvariable diff(leftLabel) -anchor e -width 10
    #label .l2 -textvariable diff(rightLabel) -anchor e -width 10
    fileLabel .l1 -textvariable diff(leftLabel)
    fileLabel .l2 -textvariable diff(rightLabel)
    fileLabel $top.l1 -textvariable diff($top,leftLabel)
    fileLabel $top.l2 -textvariable diff($top,rightLabel)

    frame .ft1 -borderwidth 2 -relief sunken
    text .ft1.tl -height 40 -width 5 -wrap none -yscrollcommand my_yscroll \
    frame $top.ft1 -borderwidth 2 -relief sunken
    text $top.ft1.tl -height 40 -width 5 -wrap none \
            -yscrollcommand [list my_yscroll $top] \
            -font myfont -borderwidth 0 -padx 0 -highlightthickness 0 \
            -takefocus 0
    text .ft1.tt -height 40 -width 80 -wrap none -yscrollcommand my_yscroll \
            -xscrollcommand ".sbx1 set" -font myfont -borderwidth 0 -padx 1 \
    text $top.ft1.tt -height 40 -width 80 -wrap none \
            -yscrollcommand [list my_yscroll $top] \
            -xscrollcommand [list $top.sbx1 set] \
            -font myfont -borderwidth 0 -padx 1 \
            -highlightthickness 0
    frame .ft1.f -width 2 -height 2 -bg lightgray
    pack .ft1.tl -side left -fill y
    pack .ft1.f -side left -fill y
    pack .ft1.tt -side right -fill both -expand 1
    scrollbar .sby -orient vertical -command "my_yview"
    scrollbar .sbx1 -orient horizontal -command ".ft1.tt xview"
    frame $top.ft1.f -width 2 -height 2 -bg lightgray
    pack $top.ft1.tl -side left -fill y
    pack $top.ft1.f -side left -fill y
    pack $top.ft1.tt -side right -fill both -expand 1
    scrollbar $top.sby -orient vertical -command [list my_yview $top]
    scrollbar $top.sbx1 -orient horizontal -command [list $top.ft1.tt xview]
    set ::diff($top,wLine1) $top.ft1.tl
    set ::diff($top,wDiff1) $top.ft1.tt

    frame .ft2 -borderwidth 2 -relief sunken
    text .ft2.tl -height 60 -width 5 -wrap none -yscrollcommand my_yscroll \
    frame $top.ft2 -borderwidth 2 -relief sunken
    text $top.ft2.tl -height 60 -width 5 -wrap none \
            -yscrollcommand [list my_yscroll $top] \
            -font myfont -borderwidth 0 -padx 0 -highlightthickness 0 \
            -takefocus 0
    text .ft2.tt -height 60 -width 80 -wrap none -yscrollcommand my_yscroll \
            -xscrollcommand ".sbx2 set" -font myfont -borderwidth 0 -padx 1 \
    text $top.ft2.tt -height 60 -width 80 -wrap none \
            -yscrollcommand [list my_yscroll $top] \
            -xscrollcommand [list $top.sbx2 set] \
            -font myfont -borderwidth 0 -padx 1 \
            -highlightthickness 0
    frame .ft2.f -width 2 -height 2 -bg lightgray
    pack .ft2.tl -side left -fill y
    pack .ft2.f -side left -fill y
    pack .ft2.tt -side right -fill both -expand 1
    scrollbar .sbx2 -orient horizontal -command ".ft2.tt xview"
    frame $top.ft2.f -width 2 -height 2 -bg lightgray
    pack $top.ft2.tl -side left -fill y
    pack $top.ft2.f -side left -fill y
    pack $top.ft2.tt -side right -fill both -expand 1
    scrollbar $top.sbx2 -orient horizontal -command [list $top.ft2.tt xview]
    set ::diff($top,wLine2) $top.ft2.tl
    set ::diff($top,wDiff2) $top.ft2.tt

    # Set up a tag for incremental search bindings
    if {[info proc textSearch::enableSearch] != ""} {
        textSearch::enableSearch .ft1.tt -label ::diff(isearchLabel)
        textSearch::enableSearch .ft2.tt -label ::diff(isearchLabel)
    if {[info procs textSearch::enableSearch] != ""} {
        textSearch::enableSearch $top.ft1.tt -label ::diff($top,isearchLabel)
        textSearch::enableSearch $top.ft2.tt -label ::diff($top,isearchLabel)
    }

    label .le -textvariable ::diff(eqLabel) -width 1
    addBalloon .le "* means external diff is running.\n= means files do\
    label $top.le -textvariable ::diff($top,eqLabel) -width 1
    addBalloon $top.le "* means external diff is running.\n= means files do\
            not differ.\nBlank means files differ."
    label .ls -width 1 -pady 0 -padx 0 -textvariable ::diff(isearchLabel)
    addBalloon .ls "Incremental search indicator"
    canvas .c -width 6 -bd 0 -selectborderwidth 0 -highlightthickness 0
    label $top.ls -width 1 -pady 0 -padx 0 \
            -textvariable ::diff($top,isearchLabel)
    addBalloon $top.ls "Incremental search indicator"
    canvas $top.c -width 6 -bd 0 -selectborderwidth 0 -highlightthickness 0


    applyColor
    .ft1.tt tag configure last -underline 1
    .ft2.tt tag configure last -underline 1
    foreach w {.ft1.tt .ft2.tt} {
    $top.ft1.tt tag configure last -underline 1
    $top.ft2.tt tag configure last -underline 1
    foreach w [list $top.ft1.tt $top.ft2.tt] {
        $w tag raise sel
        bind $w <ButtonPress-3> "zoomRow %W %X %Y %x %y"
        bind $w <ButtonRelease-3> "unzoomRow"
        bind $w <ButtonRelease-3> "unzoomRow %W"
    }
    foreach w {.ft1.tl .ft2.tl} {
    foreach w [list $top.ft1.tl $top.ft2.tl] {
        bind $w <ButtonPress-3> "rowPopup %W %X %Y %x %y"
    }

    grid .l1   .le -    .l2   -row 1 -sticky news
    grid .ft1  .c  .sby .ft2  -row 2 -sticky news
    grid .sbx1 .ls -    .sbx2 -row 3 -sticky news
    grid columnconfigure . {0 3} -weight 1
    grid rowconfigure . 2 -weight 1
    grid .c -pady [expr {[.sby cget -width] + 2}]
    grid .ls -sticky ""
    grid $top.l1   $top.le -        $top.l2   -row 1 -sticky news
    grid $top.ft1  $top.c  $top.sby $top.ft2  -row 2 -sticky news
    grid $top.sbx1 $top.ls -        $top.sbx2 -row 3 -sticky news
    grid columnconfigure $top {0 3} -weight 1
    grid rowconfigure $top 2 -weight 1
    grid $top.c -pady [expr {[$top.sby cget -width] + 2}]
    grid $top.ls -sticky ""
    
    image create photo map
    .c create image 0 0 -anchor nw -image map
    bind .c <Configure> {drawMap %h}
    image create photo map$top
    $top.c create image 0 0 -anchor nw -image map$top
    bind $top.c <Configure> [list drawMap $top %h]

    bind . <Key-Up> {scroll -1 u}
    bind . <Key-Down> {scroll 1 u}
    bind . <Key-Prior> {scroll -1 p}
    bind . <Key-Next> {scroll 1 p}
    bind . <Key-Escape> {focus .}
    bind $top <Key-Up>    [list scroll $top -1 u]
    bind $top <Key-Down>  [list scroll $top  1 u]
    bind $top <Key-Prior> [list scroll $top -1 p]
    bind $top <Key-Next>  [list scroll $top  1 p]
    bind $top <Key-Escape> [list focus $top]

    pack .mf .mo .ms .mh -in .f -side left
    pack .bfn -in .f -side right -padx {3 6}
    pack .bfp .er2 .lr2 .er1 .lr1 .eo .lo -in .f -side right -padx 3
    pack $top.mf $top.mo $top.ms $top.mh -in $top.f -side left
    pack $top.bfn -in $top.f -side right -padx {3 6}
    pack $top.bfp $top.er2 $top.lr2 $top.er1 $top.lr1 $top.eo $top.lo \
            -in $top.f -side right -padx 3
    if {$debug == 1} {
        menubutton .md -text Debug -menu .md.m
        menu .md.m
        menubutton $top.md -text Debug -menu $top.md.m
        menu $top.md.m
        if {$tcl_platform(platform) == "windows"} {
            .md.m add checkbutton -label Console -variable consolestate \
            $top.md.m add checkbutton -label Console -variable consolestate \
                    -onvalue show -offvalue hide \
                    -command {console $consolestate}
            .md.m add separator
            $top.md.m add separator
        }
        .md.m add radiobutton -label "Context 2" -variable ::Pref(context) -value 2
        .md.m add radiobutton -label "Context 5" -variable ::Pref(context) -value 5
        .md.m add radiobutton -label "Context 10" -variable ::Pref(context) -value 10
        .md.m add radiobutton -label "Context 20" -variable ::Pref(context) -value 20
        .md.m add separator
        .md.m add checkbutton -label Wrap -variable wrapstate -onvalue char\
                -offvalue none -command {.ft1.tt configure -wrap $wrapstate ;\
                .ft2.tt configure -wrap $wrapstate}
        .md.m add command -label "Merge" -command {makeMergeWin}
        .md.m add command -label "Date Filter" -command {set ::diff(filter) {^Date}}
        .md.m add command -label "Align" -command {runAlign}
        .md.m add separator
        .md.m add command -label "Reread Source" -command {source $thisscript}
        .md.m add separator
        .md.m add command -label "Redraw Window" -command {makeDiffWin}
        .md.m add separator
        .md.m add command -label "Normal Cursor" -command {normalCursor}
        .md.m add separator
        .md.m add command -label "Evalstats" -command {evalstats}
        .md.m add command -label "_stats" -command {parray _stats}
        .md.m add command -label "Nuisance" -command {makeNuisance \
        $top.md.m add radiobutton -label "Context 2" \
                -variable ::Pref(context) -value 2
        $top.md.m add radiobutton -label "Context 5" \
                -variable ::Pref(context) -value 5
        $top.md.m add radiobutton -label "Context 10" \
                -variable ::Pref(context) -value 10
        $top.md.m add radiobutton -label "Context 20" \
                -variable ::Pref(context) -value 20
        $top.md.m add separator
        $top.md.m add checkbutton -label Wrap -variable wrapstate \
                -onvalue char -offvalue none -command \
                "$top.ft1.tt configure -wrap \$wrapstate ;\
                $top.ft2.tt configure -wrap \$wrapstate"
        $top.md.m add command -label "Merge" -command [list makeMergeWin $top]
        $top.md.m add command -label "Date Filter" -command {set ::diff(filter) {^Date}}
        $top.md.m add command -label "Align" -command [list runAlign $top]
        $top.md.m add separator
        $top.md.m add command -label "Reread Source" -command {source $thisscript}
        $top.md.m add separator
        $top.md.m add command -label "Redraw Window" -command {makeDiffWin}
        $top.md.m add separator
        $top.md.m add command -label "Normal Cursor" \
                -command [list normalCursor $top]
        $top.md.m add separator
        $top.md.m add command -label "Evalstats" -command {evalstats}
        $top.md.m add command -label "_stats" -command {parray _stats}
        $top.md.m add command -label "Nuisance" -command {makeNuisance \
                "It looks like you are trying out the debug menu."}
        pack .md -in .f -side left -padx 20
        pack $top.md -in $top.f -side left -padx 20
    }
}

# Set new preferences.
proc applyPref {} {
    global Pref TmpPref

3453
3454
3455
3456
3457
3458
3459



3460
3461
3462
3463




3464
3465
3466
3467
3468



3469
3470
3471
3472
3473
3474
3475
3476
3477
3478

3479
3480

3481
3482

3483
3484
3485

3486
3487
3488
3489
3490
3491
3492
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566




3567
3568
3569
3570
3571
3572



3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584

3585
3586

3587
3588

3589
3590
3591

3592
3593
3594
3595
3596
3597
3598
3599







+
+
+
-
-
-
-
+
+
+
+


-
-
-
+
+
+









-
+

-
+

-
+


-
+







}
}

proc parseCommandLine {} {
    global diff Pref
    global argv argc tcl_platform

    
    set top [lindex $::diff(diffWindows) end]

    set diff(leftOK) 0
    set diff(rightOK) 0
    set diff(mode) ""
    set diff(printFile) ""
    set diff($top,leftOK) 0
    set diff($top,rightOK) 0
    set diff($top,mode) ""
    set diff($top,printFile) ""
    set noautodiff 0
    set autobrowse 0
    set diff(mergeFile) ""
    set diff(conflictFile) ""
    set diff(limitlines) 0
    set diff($top,mergeFile) ""
    set diff($top,conflictFile) ""
    set diff($top,limitlines) 0

    if {$argc == 0} return

    set files ""
    set nextArg ""
    set revNo 1
    foreach arg $argv {
        if {$nextArg != ""} {
            if {$nextArg == "mergeFile"} {
                set diff(mergeFile) [file join [pwd] $arg]
                set diff($top,mergeFile) [file join [pwd] $arg]
            } elseif {$nextArg == "printFile"} {
                set diff(printFile) [file join [pwd] $arg]
                set diff($top,printFile) [file join [pwd] $arg]
            } elseif {$nextArg == "revision"} {
                set Pref(doptrev$revNo) $arg
                set diff($top,doptrev$revNo) $arg
                incr revNo
            } elseif {$nextArg == "limitlines"} {
                set diff(limitlines) $arg
                set diff($top,limitlines) $arg
            }
            set nextArg ""
            continue
        }
        if {$arg == "-w"} {
            set Pref(ignore) "-w"
        } elseif {$arg == "--help"} {
3515
3516
3517
3518
3519
3520
3521
3522

3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537

3538
3539
3540

3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561







3562
3563
3564
3565
3566
3567
3568
3569
3570
3571







3572
3573

3574
3575

3576
3577
3578
3579
3580
3581
3582
3583
3584
3585







3586
3587

3588
3589

3590
3591
3592
3593
3594
3595
3596
3597
3598







3599
3600
3601

3602
3603

3604
3605
3606
3607
3608
3609
3610
3611
3612
3613




3614
3615
3616
3617
3618
3619




3620
3621

3622
3623

3624
3625
3626
3627
3628
3629
3630
3631
3632







3633
3634
3635

3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650











3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667

3668
3669
3670
3671
3672
3673
3674
3675
3676
3677

3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701

3702



3703
3704

3705
3706
3707
3622
3623
3624
3625
3626
3627
3628

3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643

3644
3645
3646

3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661







3662
3663
3664
3665
3666
3667
3668
3669
3670
3671







3672
3673
3674
3675
3676
3677
3678
3679

3680
3681

3682
3683
3684
3685







3686
3687
3688
3689
3690
3691
3692
3693

3694
3695

3696
3697
3698







3699
3700
3701
3702
3703
3704
3705
3706
3707

3708
3709

3710
3711
3712
3713
3714
3715
3716




3717
3718
3719
3720
3721
3722




3723
3724
3725
3726
3727

3728
3729

3730
3731
3732







3733
3734
3735
3736
3737
3738
3739
3740
3741

3742
3743
3744
3745
3746











3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772


3773

3774
3775
3776
3777
3778
3779
3780
3781

3782
3783



3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804

3805
3806
3807
3808
3809
3810
3811
3812
3813







-
+














-
+


-
+














-
-
-
-
-
-
-
+
+
+
+
+
+
+



-
-
-
-
-
-
-
+
+
+
+
+
+
+

-
+

-
+



-
-
-
-
-
-
-
+
+
+
+
+
+
+

-
+

-
+


-
-
-
-
-
-
-
+
+
+
+
+
+
+


-
+

-
+






-
-
-
-
+
+
+
+


-
-
-
-
+
+
+
+

-
+

-
+


-
-
-
-
-
-
-
+
+
+
+
+
+
+


-
+




-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+















-
-
+
-








-
+

-
-
-




















+
-
+
+
+


+



        } elseif {$arg == "-limit"} {
            set nextArg limitlines
        } elseif {$arg == "-nodiff"} {
            set noautodiff 1
        } elseif {$arg == "-browse"} {
            set autobrowse 1
        } elseif {$arg == "-conflict"} {
            set diff(mode) "conflict"
            set diff($top,mode) "conflict"
            set Pref(ignore) " "
        } elseif {$arg == "-print"} {
            set nextArg printFile
        } elseif {$arg == "-server"} {
            if {$tcl_platform(platform) == "windows"} {
                dde servername Diff
            } else {
                tk appname Diff
            }
        } elseif {$arg == "-o"} {
            set nextArg mergeFile
        } elseif {$arg == "-r"} {
            set nextArg revision
        } elseif {[string range $arg 0 1] == "-r"} {
            set Pref(doptrev$revNo) [string range $arg 2 end]
            set diff($top,doptrev$revNo) [string range $arg 2 end]
            incr revNo
        } elseif {[string range $arg 0 0] == "-"} {
            set Pref(dopt) "$Pref(dopt) $arg"
            append diff($top,dopt) " $arg"
        } else {
            set apa [glob -nocomplain [file join [pwd] $arg]]
            if {$apa == ""} {
                puts "Ignoring argument: $arg"
            } else {
                lappend files [lindex $apa 0]
            }
        }
    }

    set len [llength $files]
    if {$len == 1} {
        set fullname [file join [pwd] [lindex $files 0]]
        set fulldir [file dirname $fullname]
        if {$diff(mode) == "conflict"} {
            set diff(conflictFile) $fullname
            set diff(rightDir) $fulldir
            set diff(rightOK) 1
            set diff(rightLabel) $fullname
            set diff(leftLabel) $fullname
            after idle doDiff
        if {$diff($top,mode) == "conflict"} {
            set diff($top,conflictFile) $fullname
            set diff($top,rightDir) $fulldir
            set diff($top,rightOK) 1
            set diff($top,rightLabel) $fullname
            set diff($top,leftLabel) $fullname
            after idle [list doDiff $top]
            return
        } elseif {!$autobrowse && \
                [llength [glob -nocomplain [file join $fulldir RCS]]]} {
            set diff(mode) "RCS"
            set diff(rightDir) $fulldir
            set diff(RCSFile) $fullname
            set diff(rightLabel) $fullname
            set diff(rightFile) $fullname
            set diff(rightOK) 1
            set diff(leftLabel) "RCS"
            set diff($top,mode) "RCS"
            set diff($top,rightDir) $fulldir
            set diff($top,RCSFile) $fullname
            set diff($top,rightLabel) $fullname
            set diff($top,rightFile) $fullname
            set diff($top,rightOK) 1
            set diff($top,leftLabel) "RCS"
            if {$noautodiff} {
                enableRedo
                enableRedo $top
            } else {
                after idle doDiff
                after idle [list doDiff $top]
            }
        } elseif {!$autobrowse && \
                [llength [glob -nocomplain [file join $fulldir CVS]]]} {
            set diff(mode) "CVS"
            set diff(rightDir) $fulldir
            set diff(RCSFile) $fullname
            set diff(rightLabel) $fullname
            set diff(rightFile) $fullname
            set diff(rightOK) 1
            set diff(leftLabel) "CVS"
            set diff($top,mode) "CVS"
            set diff($top,rightDir) $fulldir
            set diff($top,RCSFile) $fullname
            set diff($top,rightLabel) $fullname
            set diff($top,rightFile) $fullname
            set diff($top,rightOK) 1
            set diff($top,leftLabel) "CVS"
            if {$noautodiff} {
                enableRedo
                enableRedo $top
            } else {
                after idle doDiff
                after idle [list doDiff $top]
            }
        } else {
            set diff(leftDir) $fulldir
            set diff(leftFile) $fullname
            set diff(leftLabel) $fullname
            set diff(leftOK) 1
            if {[string match "*.diff" $fullname]} {
                set diff(mode) "patch"
                set diff(patchFile) $fullname
            set diff($top,leftDir) $fulldir
            set diff($top,leftFile) $fullname
            set diff($top,leftLabel) $fullname
            set diff($top,leftOK) 1
            if {[regexp {\.(diff|patch)$} $fullname]} {
                set diff($top,mode) "patch"
                set diff($top,patchFile) $fullname
                set autobrowse 0
                if {$noautodiff} {
                    enableRedo
                    enableRedo $top
                } else {
                    after idle doDiff
                    after idle [list doDiff $top]
                }
            }
        }
    } elseif {$len >= 2} {
        set fullname [file join [pwd] [lindex $files 0]]
        set fulldir [file dirname $fullname]
        set diff(leftDir) $fulldir
        set diff(leftFile) $fullname
        set diff(leftLabel) $fullname
        set diff(leftOK) 1
        set diff($top,leftDir) $fulldir
        set diff($top,leftFile) $fullname
        set diff($top,leftLabel) $fullname
        set diff($top,leftOK) 1
        set fullname [file join [pwd] [lindex $files 1]]
        set fulldir [file dirname $fullname]
        set diff(rightDir) $fulldir
        set diff(rightFile) $fullname
        set diff(rightLabel) $fullname
        set diff(rightOK) 1
        set diff($top,rightDir) $fulldir
        set diff($top,rightFile) $fullname
        set diff($top,rightLabel) $fullname
        set diff($top,rightOK) 1
        if {$noautodiff} {
            enableRedo
            enableRedo $top
        } else {
            after idle doDiff
            after idle [list doDiff $top]
        }
    }
    if {$autobrowse && (!$diff(leftOK) || !$diff(rightOK))} {
        if {!$diff(leftOK) && !$diff(rightOK)} {
            openBoth 0
        } elseif {!$diff(leftOK)} {
            openLeft
        } elseif {!$diff(rightOK)} {
            openRight
    if {$autobrowse && (!$diff($top,leftOK) || !$diff($top,rightOK))} {
        if {!$diff($top,leftOK) && !$diff($top,rightOK)} {
            openBoth $top 0
        } elseif {!$diff($top,leftOK)} {
            openLeft $top
        } elseif {!$diff($top,rightOK)} {
            openRight $top
        }
        # If we cancel the second file and detect CVS, ask about it.
        if {$diff(leftOK) && !$diff(rightOK) && \
        if {$diff($top,leftOK) && !$diff($top,rightOK) && \
                [llength [glob -nocomplain [file join $fulldir CVS]]]} {

            if {[tk_messageBox -title Diff -icon question \
                    -message "Do CVS diff?" -type yesno] == "yes"} {
                set fulldir $diff(leftDir)
                set fullname $diff(leftFile)
                set diff(leftOK) 0
                set diff(mode) "CVS"
                set diff(rightDir) $fulldir
                set diff(RCSFile) $fullname
                set diff(rightLabel) $fullname
                set diff(rightFile) $fullname
                set diff(rightOK) 1
                set diff(leftLabel) "CVS"
                after idle doDiff
                set fulldir $diff($top,leftDir)
                set fullname $diff($top,leftFile)
                set diff($top,leftOK) 0
                set diff($top,mode) "CVS"
                set diff($top,rightDir) $fulldir
                set diff($top,RCSFile) $fullname
                set diff($top,rightLabel) $fullname
                set diff($top,rightFile) $fullname
                set diff($top,rightOK) 1
                set diff($top,leftLabel) "CVS"
                after idle [list doDiff $top]
            }
        }
    }
}

proc saveOptions {} {
    global Pref

    if {[catch {set ch [open "~/.diffrc" w]} err]} {
        tk_messageBox -icon error -title "File error" -message \
                "Error when trying to save preferences:\n$err"
        return
    }

    foreach i [array names Pref] {
        if {![string match "dopt*" $i]} {
            puts $ch [list set Pref($i) $Pref($i)]
        puts $ch [list set Pref($i) $Pref($i)]
        }
    }
    close $ch
}

proc getOptions {} {
    global Pref

    set Pref(fontsize) 9
    set Pref(fontfamily) courier
    set Pref(fontfamily) Courier
    set Pref(ignore) "-b"
    set Pref(dopt) ""
    set Pref(doptrev1) ""
    set Pref(doptrev2) ""
    set Pref(parse) 2
    set Pref(lineparsewords) "0"
    set Pref(extralineparse) 1
    set Pref(colorchange) red
    set Pref(colornew1) darkgreen
    set Pref(colornew2) blue
    set Pref(bgchange) #ffe0e0
    set Pref(bgnew1) #a0ffa0
    set Pref(bgnew2) #e0e0ff
    set Pref(onlydiffs) 0
    set Pref(context) 2
    set Pref(crlf) 0
    set Pref(marklast) 1
    set ::diff(filter) ""

    if {[file exists "~/.diffrc"]} {
        source "~/.diffrc"
    }
}


if {![winfo exists .f]} {
if {![info exists gurkmeja]} {
    set gurkmeja 1
    option add *Menu.tearOff 0
    getOptions
    makeDiffWin
    wm withdraw .
    update idletasks
    parseCommandLine
}