Eskil

Diff
Login

Differences From Artifact [59b97016fd]:

To Artifact [87546644c9]:


66
67
68
69
70
71
72
73

74
75
76
77
78
79
80
66
67
68
69
70
71
72

73
74
75
76
77
78
79
80







-
+







#                               Added CVS support.
#
#-----------------------------------------------
# the next line restarts using wish \
exec wish "$0" "$@"

set debug 1
set diffver "Version 1.8b  000914"
set diffver "Version 1.8b  001103"
set tmpcnt 0
set tmpfiles {}
set thisscript [file join [pwd] [info script]]
set thisdir [file dirname $thisscript]

if {$tcl_platform(platform) == "windows"} {
    cd $thisdir
363
364
365
366
367
368
369


370
371
372
373
374
375
376
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378







+
+







        compareMidString $mid1 $mid2 mid1 mid2 $test
        set res1 [eval lreplace \$res1 1 1 $mid1]
        set res2 [eval lreplace \$res2 1 1 $mid2]
    }
}

# Compare two lines and rate how much they resemble each other.
# This has never worked well. Some day I'll sit down, think this through,
# and come up with a better algorithm.
proc compareLines2 {line1 line2} {
    compareLines $line1 $line2 res1 res2 1

    # Collect identical pieces and different pieces
    set sames {}
    set diffs1 {}
    set diffs2 {}
402
403
404
405
406
407
408
409



410
411
412
413
414
415
416
404
405
406
407
408
409
410

411
412
413
414
415
416
417
418
419
420







-
+
+
+







    }
#    puts "S $sumsame D $sumdiff1 D $sumdiff2"
    return [expr {$sumsame - [maxabs $sumdiff1 $sumdiff2]}]
}

# Decide how to display change blocks
# This tries to match the lines that resemble each other and put them
# next to each other. The algorithm for doing it would need some work.
# next to each other.
# As the previous procedure, this would need a complete rework and a
# better algorithm.
proc compareblocks {block1 block2} {
    set size1 [llength $block1]
    set size2 [llength $block2]

    if {$size1 * $size2 > 1000} {
        puts "Diff warning: Analyzing a large block. ($size1 $size2)"
        update idletasks
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
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
874
875
876
877
878
879
880
881
882
883
884
885
886
781
782
783
784
785
786
787
































































































788
789
790
791
792
793
794







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







                        $line1 $n1 $line2 $n2
                incr mapMax $n1
            }
        }
    }
}

# Scroll windows to next diff
proc findNext {} {
    set i [.ft1.tt index @0,0+1line]
    set n1 [.ft1.tt tag nextrange new1 $i]
    set c1 [.ft1.tt tag nextrange change $i]
    set i [.ft2.tt index @0,0+1line]
    set n2 [.ft2.tt tag nextrange new2 $i]
    set c2 [.ft2.tt tag nextrange change $i]

    # Below, the 'list' command is not used because I want the list
    # "flattened" before the sort.
    set apa [lsort -dictionary "$n1 $c1 $n2 $c2"]

    if {[llength $apa] != 0} {
        set apa [lindex $apa 0]
    } else {
        set apa end
    }

    foreach w {.ft1.tl .ft1.tt .ft2.tl .ft2.tt} {
        $w yview $apa
    }
}

# Scroll windows to previous diff
proc findPrev {} {
    set i [.ft1.tt index @0,0]
    set n1 [.ft1.tt tag prevrange new1 $i]
    set c1 [.ft1.tt tag prevrange change $i]
    set i [.ft2.tt index @0,0]
    set n2 [.ft2.tt tag prevrange new2 $i]
    set c2 [.ft2.tt tag prevrange change $i]

    # Below, the 'list' command is not used because I want the list
    # "flattened" before the sort.
    set apa [lsort -decreasing -dictionary "$n1 $c1 $n2 $c2"]
    if {[llength $apa] != 0} {
        set apa [lindex $apa 1]
    } else {
        set apa 1.0
    }

    foreach w {.ft1.tl .ft1.tt .ft2.tl .ft2.tt} {
        $w yview $apa
    }
}

# Scroll windows to next/previous diff
proc findDiff {delta} {
    global CurrentHighLight

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

# Scroll windows to diff
proc showDiff {num} {
    global CurrentHighLight changesList

    highLightChange $num

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

    foreach w {.ft1.tl .ft1.tt .ft2.tl .ft2.tt} {
        $w see $line2
        $w see $line1
        $w see $linep
        $w see $linen
        if {[llength [$w bbox $line1]] == 0} {
            $w yview $linep
        }
        if {[llength [$w bbox $line2]] == 0} {
            $w yview $line1
        }
    }
}

proc enableRedo {} {
    .mf.m entryconfigure 1 -state normal
}

proc disableRedo {} {
    .mf.m entryconfigure 1 -state disabled
}
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
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

1057
1058
1059
1060
1061
1062
1063
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
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
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
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
1029


1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040

1041
1042
1043
1044
1045
1046
1047
1048







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

-
+





+
















+
-
-
-
+
+
+

-
-
+
+

-
+

-
-
+
+



+

-
-
-
+
+
+

-
-
+
+

-
+

-
-
+
+



+


-
-
+
+

-
-
-
+
+
+

-
+

-
+

-
-
-
-
+
+
+
+



+




+

-
+


-
-
+
+




+

-
-
+
+


-
+


















-
+

+
+


+

-
+








+
+
+
+
+
+
+













-
-
+
+









-
+







    global oldcursor oldcursor2
    . config -cursor $oldcursor
    foreach w {.ft1.tl .ft1.tt .ft2.tl .ft2.tt} {
        $w config -cursor $oldcursor2
    }
}

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

    set diff(leftFile) [tmpfile]
    set diff(rightFile) [tmpfile]

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

    set diff(conflictDiff) {}
    set leftLine 1
    set rightLine 1
    set state both
    while {[gets $ch line] != -1} {
        if {[string match <<<<<<* $line]} {
            set state right
            regexp {<*\s*(.*)} $line -> rightName
            set start2 $rightLine
        } elseif {[string match ======* $line]} {
            set state left
            set end2 [expr {$rightLine - 1}]
            set start1 $leftLine
        } elseif {[string match >>>>>>* $line]} {
            set state both
            regexp {>*\s*(.*)} $line -> leftName
            set end1 [expr {$leftLine - 1}]
            lappend diff(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
    set diff(leftLabel) $leftName
    set diff(rightLabel) $rightName
    update idletasks
}

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

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

# Prepare for RCS/CVS diff. Checkout copies of the versions needed.
proc prepareRCS {} {
    global leftFile rightFile RCSFile leftLabel rightLabel Pref RCSmode
    global diff Pref

    set revs {}
    set opts {}
    set Pref(old_dopt) $Pref(dopt)

    # Search for revision options
    set nextIsRev 0
    foreach opt $Pref(dopt) {
        if {$nextIsRev} {
            lappend revs $opt
            set nextIsRev 0
        } elseif {[string equal "-r" $opt]} {
            set nextIsRev 1
        } elseif {[string match "-r*" $opt]} {
            lappend revs [string range $opt 2 end]
        } else {
            lappend opts $opt
        }
    }

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

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

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

            if {$RCSmode == 2} {
                set leftLabel "$RCSFile (CVS $r1)"
                set rightLabel "$RCSFile (CVS $r2)"
            if {$diff(mode) == "CVS"} {
                set diff(leftLabel) "$diff(RCSFile) (CVS $r1)"
                set diff(rightLabel) "$diff(RCSFile) (CVS $r2)"
                catch {exec cvs update -p -r $r1 \
                        [file nativename $RCSFile] > $leftFile}
                        [file nativename $diff(RCSFile)] > $diff(leftFile)}
                catch {exec cvs update -p -r $r2 \
                        [file nativename $RCSFile] > $rightFile}
                        [file nativename $diff(RCSFile)] > $diff(rightFile)}
            } else {
                set leftLabel "$RCSFile (RCS $r1)"
                set rightLabel "$RCSFile (RCS $r2)"
                catch {exec co -p$r1 [file nativename $RCSFile] > $leftFile}
                catch {exec co -p$r2 [file nativename $RCSFile] > $rightFile}
                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)}
            }
        }
    }
    # Make sure labels are updated before processing starts
    update idletasks
    set Pref(dopt) $opts
}

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

    cleartmp
    set rightFile $RCSFile
    set leftFile $RCSFile
    set diff(rightFile) $diff(RCSFile)
    set diff(leftFile) $diff(RCSFile)
    set Pref(dopt) $Pref(old_dopt)
    unset Pref(old_dopt)
}

# Main diff function.
proc doDiff {} {
    global leftFile rightFile leftOK rightOK
    global eqLabel RCSmode Pref doingLine1 doingLine2
    global diff Pref
    global eqLabel doingLine1 doingLine2
    global mapMax changesList

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

    busyCursor

    foreach w {.ft1.tl .ft1.tt .ft2.tl .ft2.tt} {
        $w configure -state normal
        $w delete 1.0 end
    }
    set changesList {}
    set mapMax 0
    highLightChange -1

    update idletasks

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

    # Run diff and parse the result.
    set differr [catch {eval exec \$::diffexe $Pref(dopt) $Pref(ignore) \
            \$leftFile \$rightFile} diffres]
            \$diff(leftFile) \$diff(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 {[llength $result] == 0} {
        if {$differr == 1} {
            .ft1.tt insert end $diffres
            normalCursor
            return
        } else {
            set eqLabel "="
        }
    } else {
        set eqLabel " "
    }

    set ch1 [open $leftFile]
    set ch2 [open $rightFile]
    set ch1 [open $diff(leftFile)]
    set ch2 [open $diff(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
    set ::HighLightCount 0
    foreach i $result {
        if {![regexp {(.*)([acd])(.*)} $i apa l c r]} {
        if {![regexp {(.*)([acd])(.*)} $i -> l c r]} {
            .ft1.tt 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
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
1172
1173
1174
1175
1176
1177
1178
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
1224

1225
1226
1227
1228
1229




1230
1231
1232
1233
1234
1235

1236
1237
1238

1239
1240
1241
1242
1243
1244
1245
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
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
1172
1173
1174
1175
1176
1177
1178
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
1224




1225
1226
1227
1228
1229
1230
1231
1232
1233
1234





1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245




1246
1247
1248
1249
1250
1251
1252
1253
1254
1255

1256
1257

1258
1259
1260
1261
1262
1263

1264
1265
1266
1267
1268
1269

1270
1271
1272
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







-
-
-
-







+
+
+
+
+
+
+
+




-
+
-

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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+




-
+

-
-
-
-
+
+
+
+






-
-
-
-
+
+
+
+






-
-
-
-
-
+
+
+
+
+






-
-
-
-
+
+
+
+






-
+

-
+





-
+

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





-
+

-
-
-
-
+
+
+
+





-
+

-
-
-
-
+
+
+
+





-
+


-
+







            $w insert end \n
        }
    }

    close $ch1
    close $ch2

    if {$RCSmode} {
        cleanupRCS
    }

    drawMap -1
    foreach w {.ft1.tl .ft2.tl} {
        $w configure -state disabled
    }
    update idletasks
    .ft2.tl see 1.0
    normalCursor
    showDiff 0

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

# This is the entrypoint to do a diff via DDE or Send
proc remoteDiff {file1 file2} {
    global leftFile rightFile leftOK rightOK
    global diff
    global leftDir rightDir leftLabel rightLabel

    set leftDir [file dirname $file1]
    set leftFile $file1
    set leftLabel $file1
    set leftOK 1
    set rightDir [file dirname $file2]
    set rightFile $file2
    set rightLabel $file2
    set rightOK 1
    set RCSmode 0
    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 .
    update
    doDiff
}

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

# Scroll windows to next/previous diff
proc findDiff {delta} {
    global CurrentHighLight

    showDiff [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} {
    global CurrentHighLight changesList
    if {[info exists CurrentHighLight] && $CurrentHighLight >= 0} {
        .ft1.tl tag configure hl$CurrentHighLight -background {}
        .ft2.tl 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
    }
}

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

    highLightChange $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} {
        seeText $w $line1 $line2
    }
}

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

proc doOpenLeft {{forget 0}} {
    global leftFile leftDir rightDir leftOK leftLabel
    global diff

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

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

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

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

proc openLeft {} {
    global RCSmode
    global diff
    if {[doOpenLeft]} {
        set RCSmode 0
        set diff(mode) ""
        doDiff
    }
}

proc openRight {} {
    global RCSmode
    global diff
    if {[doOpenRight]} {
        set diff(mode) ""
        doDiff
    }
}
        set RCSmode 0

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

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

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

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

#####################################
# Map stuff
1269
1270
1271
1272
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
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







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

+
-
+
+

-
-
+
+





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






-
+




-
+


-
-
+
+





-
+




-
+


-
-
+
+






-
+



-
-
+


-
-
+
+
+
+
+



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

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

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







        if {$y2 > $h} {set y2 $h}
        incr y2
        map put $Pref(color$type) -to 1 $y1 $x2 $y2
    }
}

######################################

# Merge stuff
proc highLightChange {n} {
    global CurrentHighLight changesList
    if {[info exists CurrentHighLight] && $CurrentHighLight >= 0} {
        .ft1.tl tag configure hl$CurrentHighLight -background {}
        .ft2.tl 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
    }
}

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

    set leftData {}
    set rightData {}
    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
    }

    set ch1 [open $leftFile r]
    set ch2 [open $rightFile r]
    set doingLine1 0
    set doingLine2 0
    set ch1 [open $diff(leftFile) r]
    set ch2 [open $diff(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} {
            gets $ch1 apa
            lappend data1 $apa
            append data1 $apa\n
            incr doingLine1
        }
        while {$doingLine2 < $line2} {
            gets $ch2 apa
            lappend data2 $apa
            append data2 $apa\n
            incr doingLine2
        }
        lappend leftData [join $data1 \n]
        lappend rightData [join $data2 \n]
        lappend leftMergeData $data1
        lappend rightMergeData $data2

        set data1 {}
        set data2 {}
        for {set t 0} {$t < $n1} {incr t} {
            gets $ch1 apa
            lappend data1 $apa
            append data1 $apa\n
            incr doingLine1
        }
        for {set t 0} {$t < $n2} {incr t} {
            gets $ch2 apa
            lappend data2 $apa
            append data2 $apa\n
            incr doingLine2
        }
        lappend leftData [join $data1 \n]
        lappend rightData [join $data2 \n]
        lappend leftMergeData $data1
        lappend rightMergeData $data2
        set mergeSelection($changeNo) 2
        incr changeNo
    }
    set data1 {}
    set data2 {}
    while {[gets $ch1 apa] != -1} {
        lappend data1 $apa
        append data1 $apa\n
        incr doingLine1
    }
    while {[gets $ch2 apa] != -1} {
        gets $ch2 apa
        lappend data2 $apa
        append data2 $apa\n
        incr doingLine2
    }
    lappend leftData [join $data1 \n]
    lappend rightData [join $data2 \n]
    lappend leftMergeData $data1
    lappend rightMergeData $data2

    puts [list Left: $leftMergeData]
    puts [list Right: $rightMergeData]

    close $ch1
    close $ch2
}

proc makeMergeWin {} {

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

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

    set w .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
    update
    seeText $w merges0 mergee0
}

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

    set w .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
    seeText $w merges$curMerge mergee$curMerge
}

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

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

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

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

    set w .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]
    set diffRight [lindex $rightMergeData $i]

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

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

    set w .merge.t

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

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

    set ch [open $diff(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)."
}

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

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

    toplevel .merge

}

# Create a window to display merge result.
proc makeMergeWin {} {
    set w .merge
    set geometry ""
    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
    radiobutton $w.f.rb2 -text "L"  -value 1  -variable curMergeSel \
            -command selectMerge
    radiobutton $w.f.rb3 -text "R"  -value 2  -variable curMergeSel \
            -command selectMerge
    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}

    button $w.f.bl -text "All L" -command {selectMergeAll 1}
    button $w.f.br -text "All R" -command {selectMergeAll 2}
    checkbutton $w.f.bm -text "Pure" -variable diff(mode) \
            -onvalue "conflictPure" -offvalue "conflict" -command {doDiff}

    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}

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

    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 $w.f.bm x $w.f.bs $w.f.bq
    grid columnconfigure $w.f {4 7 10 12} -minsize 10
    grid columnconfigure $w.f 10 -weight 1

    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
}

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

# Format a line number for printing
1437
1438
1439
1440
1441
1442
1443

1444
1445
1446
1447
1448
1449
1450
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706







+







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

# Main print function
proc printDiffs {} {
    busyCursor
    update idletasks
    set tmpFile [file nativename ~/tcldiff.enscript]
    set tmpFile2 [file nativename ~/tcldifftmp.ps]
    set tmpFile3 [file nativename ~/tcldiff.ps]

1580
1581
1582
1583
1584
1585
1586

1587
1588
1589
1590
1591
1592
1593
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850







+







            \n\n$tmpFile2\nCreated with 'enscript -c -B -e -p $tmpFile2\
            $tmpFile'\n\n$tmpFile3\nCreated with 'mpage -bA4 -a2 $tmpFile2 >\
            $tmpFile3'" -font "Courier 8"
    pack .dp.b -side bottom
    pack .dp.l -side top
}

# Create a print dialog.
proc doPrint {} {
    
    destroy .pr
    toplevel .pr
    wm title .pr "Print diffs"

    label .pr.l1 -justify left -text "The print function is just on an\
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
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







+











+






+















+








}

#####################################
# GUI stuff
#####################################

# Procedures for common y-scroll
proc my_yview args {
    foreach w {.ft1.tl .ft1.tt .ft2.tl .ft2.tt} {
        eval $w yview $args
    }
}

proc my_yscroll args {
    eval .sby set $args
    my_yview 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 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)
        .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
    }
}

# Build the main window
1680
1681
1682
1683
1684
1685
1686

1687
1688
1689
1690
1691
1692
1693
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955







+







                -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
    .mf.m add command -label "Open Right File" -command openRight
    .mf.m add command -label "Open Conflict File" -command openConflict
    if {$tcl_platform(platform) == "unix"} {
        .mf.m add command -label "RCSDiff" -underline 0 -command openRCS
        .mf.m add command -label "CVSDiff" -underline 0 -command openCVS
        .mf.m add separator
        .mf.m add command -label "Print" -underline 0 -command doPrint
    }
    .mf.m add separator
1752
1753
1754
1755
1756
1757
1758
1759
1760


1761
1762
1763
1764
1765
1766
1767
2014
2015
2016
2017
2018
2019
2020


2021
2022
2023
2024
2025
2026
2027
2028
2029







-
-
+
+







    button .bfp -text "Prev Diff" -relief raised -command {findDiff -1}
    entry .eo -width 10 -textvariable Pref(dopt)
    label .lo -text "Diff Options"

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

    label .l1 -textvariable leftLabel -anchor e -width 10
    label .l2 -textvariable rightLabel -anchor e -width 10
    label .l1 -textvariable diff(leftLabel) -anchor e -width 10
    label .l2 -textvariable diff(rightLabel) -anchor e -width 10

    frame .ft1 -borderwidth 2 -relief sunken
    text .ft1.tl -height 40 -width 5 -wrap none -yscrollcommand my_yscroll \
            -font myfont -borderwidth 0 -padx 0 -highlightthickness 0
    text .ft1.tt -height 40 -width 80 -wrap none -yscrollcommand my_yscroll \
            -xscrollcommand ".sbx1 set" -font myfont -borderwidth 0 -padx 0 \
            -highlightthickness 0
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
1866
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
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138







+
+
+
+










-
+
+







+







+











+









+
+







        menu .md.m
        if {$tcl_platform(platform) == "windows"} {
            .md.m add checkbutton -label Console -variable consolestate \
                    -onvalue show -offvalue hide \
                    -command {console $consolestate}
            .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 "Stack trace" -command {bgerror Debug}
        .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 \
                "It looks like you are trying out the debug menu."}
        pack .mf .mo .mh .md -in .f -side left
    } else {
        pack .mf .mo .mh -in .f -side left
    }
    pack .bfn .bfp .eo .lo -in .f -side right
}

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

    array set Pref [array get TmpPref]
    applyColor
}

# Update test color fields.
proc testColor {} {
    global TmpPref

    .pr.fc.t1 tag configure change -foreground $TmpPref(colorchange) \
            -background $TmpPref(bgchange)
    .pr.fc.t2 tag configure new1 -foreground $TmpPref(colornew1) \
            -background $TmpPref(bgnew1)
    .pr.fc.t3 tag configure new2 -foreground $TmpPref(colornew2) \
            -background $TmpPref(bgnew2)
}

# Color dialog.
proc selColor {name} {
    global TmpPref

    set t [tk_chooseColor -parent .pr -initialcolor $TmpPref($name)]
    if {$t != ""} {
        set TmpPref($name) $t
    }
}

# Create a windoe for changing preferences.
# Currently only colors are changed in this dialog.
proc makePrefWin {} {
    global Pref TmpPref

    array set TmpPref [array get Pref]

    destroy .pr

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
2184
2185
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
2217
2218
2219
2220
2221
2222







+











+











+







            -sticky nsew -padx 1 -pady 1
    grid columnconfigure .pr.fc {1 3} -weight 1

    pack .pr.fc -side top -fill x
    pack .pr.b1 .pr.b2 .pr.b3 -side left -expand 1 -fill x
}

# Change font preference
proc applyFont {} {
    global Pref TmpPref

    set Pref(fontsize) $TmpPref(fontsize)

    set i [lindex [.fo.lb curselection] 0]
    set Pref(fontfamily) [.fo.lb get $i]

    chFont
}

# Update example font
proc exampleFont {} {
    global TmpPref
    set i [lindex [.fo.lb curselection] 0]
    set TmpPref(fontfamily) [.fo.lb get $i]

    font configure tmpfont -family $TmpPref(fontfamily)
    if {[regexp {^[0-9]+$} $TmpPref(fontsize)]} {
        font configure tmpfont -size $TmpPref(fontsize)
    }
}

# Font dialog
proc makeFontWin {} {
    global Pref TmpPref FontCache

    destroy .fo
    toplevel .fo
    wm title .fo "Select Font"

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
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

2336
2337
2338
2339
2340
2341
2342
2343
2344
2345







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







-
+








-
+
+
+








    exampleFont
}

#####################################
# Help and startup functions
#####################################

proc makeNuisance {{str {Hi there!}}} {
    global thisdir

    if {[lsearch [image names] nuisance] < 0} {
        set file [file join $thisdir Nuisance.gif]
        if {![file exists $file]} return
        image create photo nuisance -file $file
    }

    destroy .nui
    toplevel .nui
    wm transient .nui .
    wm geometry .nui +400+400
    wm title .nui ""
    label .nui.l -image nuisance
    pack .nui.l
    wm protocol .nui WM_DELETE_WINDOW {destroy .nui2 .nui}
    update
    
    destroy .nui2
    toplevel .nui2 -bg yellow
    wm transient .nui2 .nui
    wm overrideredirect .nui2 1
    wm title .nui2 ""
    label .nui2.l -text "$str\nDo you want help?" -justify left -bg yellow
    button .nui2.b -text "No, get out of my face!" -command {destroy .nui2 .nui} -bg yellow
    pack .nui2.l .nui2.b -side top -fill x
    wm geometry .nui2 +[expr {405 + [winfo width .nui]}]+400
}

proc makeAboutWin {} {
    global diffver
    destroy .ab

    toplevel .ab
    wm title .ab "About Diff.tcl"
    text .ab.t -width 45 -height 8 -wrap word
    text .ab.t -width 40 -height 11 -wrap word
    button .ab.b -text "Close" -command "destroy .ab"
    pack .ab.b -side bottom
    pack .ab.t -side top -expand y -fill both

    .ab.t insert end "A Tcl/Tk frontend to diff\n\n"
    .ab.t insert end "$diffver\n"
    .ab.t insert end "Made by Peter Spjuth\n"
    .ab.t insert end "E-Mail: peter.spjuth@space.se\n\n"

    .ab.t insert end "Credits:\n"
    .ab.t insert end "Ideas for scrollbar map and merge function\n"
    .ab.t insert end "taken from TkDiff\n"
}

proc makeHelpWin {} {
    global Pref
    destroy .he

    toplevel .he
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
2360
2361
2362
2363
2364
2365
2366








2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
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







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


-
+










-
+
+




-

+
+



-
+
+







    .he.t tag configure ul -underline 1

    .he.t insert end {\

} "" {Commands} ul {

File Menu
  Redo Diff      : Run diff again on the same files.
  Open Both      : Select two files, run diff.
  Open Left File : Select a file for left window, run diff
  Open Right File: Select a file for right window, run diff
  RCSDiff        : (UNIX only) Select one file and diff like rcsdiff.
  Print          : (UNIX only) Experimental print function.
                   It currently creates a postscript file ~/tcldiff.ps
  Quit           : Guess
  Redo Diff         : Run diff again on the same files.
  Open Both         : Select two files, run diff.
  Open Left File    : Select a file for left window, run diff
  Open Right File   : Select a file for right window, run diff
  Open Conflict File: Select a file containing conflicts such as from
                      a CVS merge.
  RCSDiff           : (UNIX only) Select one file and diff like rcsdiff.
  CVSDiff           : (UNIX only) Select one file and diff like cvs diff.
  Print             : (UNIX only) Experimental print function.
                      It currently creates a postscript file ~/tcldiff.ps
  Quit              : Guess

Options Menu
  Font     : Select font ant fontsize for the two main text windows
  Font     : Select font and fontsize for the two main text windows
  Ignore   : Diff options for handling whitespace
  Parse    : Additional parsing made by diff.tcl to improve the display.
             See examples below.
             Nothing: No parsing made.
             Lines  : When there is a changed block with the same number
                      of lines in both right and left files, diff.tcl
                      compares corresponding lines and tries to highlight
                      only the part that has been changed.
             Blocks : When the number of lines in a changed block is not
                      the same in both files, diff.tcl tries to find lines
                      that look the same and place them abreast.
                      that look the same and place them abreast. The "small"
                      version do not parse big blocks to avoid long runs.
             The Char and Word options selects if the line parsing should
             highlight full words only, or check single characters.
             2nd stage  : More thorough parsing of a line.
             Mark last  : Last change of a line is underlined
  Diffs only : Only differing lines will be displayed.
  Colours  : Choose highlight colours.
  Diffs only : Only differing lines will be displayed.
  Force crlf translation : (Windows only) Use crlf mode when reading files.
  Save default: Save current option settings in ~/.diffrc

Diff Options Field: Any text written here will be passed to diff.
                    In RCS mode, any -r options will be used to select versions.
                    In RCS/CVS mode, any -r options will be used to select
                    versions.

Prev Diff Button: Scrolls to the previous differing block, or to the top
                  if there are no more diffs.
Next Diff Button: Scrolls to the next differing block, or to the bottom
                  if there are no more diffs.

Equal sign: Above the vertical scrollbar, a "=" will appear if the files
2172
2173
2174
2175
2176
2177
2178

2179

2180
2181
2182
2183
2184
2185



2186
2187


2188
2189
2190
2191

2192









2193
2194
2195
2196
2197
2198
2199
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







+
-
+
-
-

-
-
-
+
+
+


+
+




+

+
+
+
+
+
+
+
+
+







.he.t insert end {4: NET '/I$1/N$1457' IC2-11 IC6-7   3: NET '/I$1/N$1457' IC2-11 IC6-7
}
.he.t insert end {5: } change {NET '/I$1/N$1458' } "" {IC2-10} change {         } "" {4: } change {NET '/I$1/N$1458' } "" {IC2-9} change "\n"

}

proc parseCommandLine {} {
    global diff Pref
    global argv argc Pref RCSmode RCSFile tcl_platform
    global argv argc tcl_platform
    global rightDir rightFile rightOK rightLabel
    global leftDir leftFile leftOK leftLabel

    set leftOK 0
    set rightOK 0
    set RCSmode 0
    set diff(leftOK) 0
    set diff(rightOK) 0
    set diff(mode) ""
    set noautodiff 0
    set autobrowse 0
    set diff(mergeFile) ""
    set diff(conflictFile) ""

    if {$argc == 0} return

    set files ""
    set nextArg ""
    foreach arg $argv {
        if {$nextArg != ""} {
            if {$nextArg == "mergeFile"} {
                set diff(mergeFile) [file join [pwd] $arg]
            } elseif {$nextArg == "revision"} {
                set Pref(dopt) "$Pref(dopt) -r$arg"
            }
            set nextArg ""
            continue
        }
        if {$arg == "-w"} {
            set Pref(ignore) "-w"
        } elseif {$arg == "-b"} {
            set Pref(ignore) "-b"
        } elseif {$arg == "-noignore"} {
            set Pref(ignore) " "
        } elseif {$arg == "-noparse"} {
2210
2211
2212
2213
2214
2215
2216



2217
2218
2219
2220
2221
2222




2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237

2238








2239

2240
2241
2242
2243
2244
2245
2246
2247







2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261







2262
2263
2264
2265
2266
2267
2268
2269
2270
2271




2272
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
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







+
+
+






+
+
+
+














-
+

+
+
+
+
+
+
+
+
-
+

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







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






-
-
-
-
+
+
+
+




-
-
-
-
+
+
+
+


-
-
-
-
+
+
+
+






-
-
+
+

-
+

-
+







            set Pref(extralineparse) 1
        } elseif {$arg == "-no2nd"} {
            set Pref(extralineparse) 0
        } elseif {$arg == "-nodiff"} {
            set noautodiff 1
        } elseif {$arg == "-browse"} {
            set autobrowse 1
        } elseif {$arg == "-conflict"} {
            set diff(mode) "conflict"
            set Pref(ignore) " "
        } elseif {$arg == "-server"} {
            if {$tcl_platform(platform) == "unix"} {
                tk appname Diff
            } else {
                dde servername Diff
            }
        } elseif {$arg == "-o"} {
            set nextArg mergeFile
        } elseif {$arg == "-r"} {
            set nextArg revision
        } elseif {[string range $arg 0 0] == "-"} {
            set Pref(dopt) "$Pref(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] $files]
        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
            return
        if {!$autobrowse && \
        } elseif {!$autobrowse && \
                [llength [glob -nocomplain [file join $fulldir RCS]]]} {
            set RCSmode 1
            set rightDir $fulldir
            set RCSFile $fullname
            set rightLabel $fullname
            set rightFile $fullname
            set rightOK 1
            set leftLabel "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"
            if {$noautodiff} {
                enableRedo
            } else {
                after idle doDiff
            }
        } elseif {!$autobrowse && \
                [llength [glob -nocomplain [file join $fulldir CVS]]]} {
            set RCSmode 2
            set rightDir $fulldir
            set RCSFile $fullname
            set rightLabel $fullname
            set rightFile $fullname
            set rightOK 1
            set leftLabel "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"
            if {$noautodiff} {
                enableRedo
            } else {
                after idle doDiff
            }
        } else {
            set leftDir $fulldir
            set leftFile $fullname
            set leftLabel $fullname
            set leftOK 1
            set diff(leftDir) $fulldir
            set diff(leftFile) $fullname
            set diff(leftLabel) $fullname
            set diff(leftOK) 1
        }
    } elseif {$len >= 2} {
        set fullname [file join [pwd] [lindex $files 0]]
        set fulldir [file dirname $fullname]
        set leftDir $fulldir
        set leftFile $fullname
        set leftLabel $fullname
        set leftOK 1
        set diff(leftDir) $fulldir
        set diff(leftFile) $fullname
        set diff(leftLabel) $fullname
        set diff(leftOK) 1
        set fullname [file join [pwd] [lindex $files 1]]
        set fulldir [file dirname $fullname]
        set rightDir $fulldir
        set rightFile $fullname
        set rightLabel $fullname
        set rightOK 1
        set diff(rightDir) $fulldir
        set diff(rightFile) $fullname
        set diff(rightLabel) $fullname
        set diff(rightOK) 1
        if {$noautodiff} {
            enableRedo
        } else {
            after idle doDiff
        }
    }
    if {$autobrowse && (!$leftOK || !$rightOK)} {
        if {!$leftOK && !$rightOK} {
    if {$autobrowse && (!$diff(leftOK) || !$diff(rightOK))} {
        if {!$diff(leftOK) && !$diff(rightOK)} {
            openBoth 0
        } elseif {!$leftOK} {
        } elseif {!$diff(leftOK)} {
            openLeft
        } elseif {!$rightOK} {
        } elseif {!$diff(rightOK)} {
            openRight
        }
    }
}

proc saveOptions {} {
    global Pref