Eskil

Check-in [b4728d8ec9]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Complete rework or directory diff window.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: b4728d8ec93787a7768c6d14b63c3550af48805f
User & Date: peter 2007-12-26 16:52:23.000
Context
2007-12-26
16:53
Cleaned up directory diff preferences. check-in: a8638dcafc user: peter tags: trunk
16:52
Complete rework or directory diff window. check-in: b4728d8ec9 user: peter tags: trunk
16:50
Changed file sorting to always be dictionary order regardless of platform. Cleaned up preferences. Copied BrowseDir from dirdiff. Added destructor to cancel idle callback. Handle status update for identical libraries. Added dirdiff toplevel. check-in: 0a3dda7c38 user: peter tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/dirdiff.tcl.
20
21
22
23
24
25
26


27





28

29
30


31




32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#
#----------------------------------------------------------------------
# $Revision$
#----------------------------------------------------------------------

# Compare file names
proc FStrCmp {s1 s2} {


    # On Unix filenames are case sensitive





    if {$::tcl_platform(platform) eq "unix"} {

	return [string compare $s1 $s2]
    }


    string compare -nocase $s1 $s2




}

# Sort file names
proc Fsort {l} {
    if {$::tcl_platform(platform) eq "unix"} {
	return [lsort $l]
    }
    # Case insensitive on windows
    lsort -dictionary $l
}

# Compare two files or dirs
# Return true if equal
proc CompareFiles {file1 file2} {
    global Pref







>
>
|
>
>
>
>
>
|
>
|
|
>
>
|
>
>
>
>




<
<
<
<







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49




50
51
52
53
54
55
56
#
#----------------------------------------------------------------------
# $Revision$
#----------------------------------------------------------------------

# Compare file names
proc FStrCmp {s1 s2} {
    # Equality is based on platform's standard
    # Order is dictionary order

    # Exact equal is equal regardless of platform.
    if {$s1 eq $s2} {
        return 0
    }
    # Accept case insensitive equality on windows
    if {$::tcl_platform(platform) eq "windows"} {
        if {[string equal -nocase $s1 $s2]} {
            return 0
        }
    }
    # FIXA: What's the case on Mac?
    
    if {[lindex [lsort -dictionary [list $s1 $s2]] 0] eq $s1} {
        return -1
    }
    return 1
}

# Sort file names
proc Fsort {l} {




    lsort -dictionary $l
}

# Compare two files or dirs
# Return true if equal
proc CompareFiles {file1 file2} {
    global Pref
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
    # Same type?
    set isdir1 [FileIsDirectory $file1]
    set isdir2 [FileIsDirectory $file2]
    if {$isdir1 != $isdir2} {
	return 0
    }
    # If contents is not checked, same size is enough to be equal
    if {$stat1(size) == $stat2(size) && $Pref(comparelevel) == 0} {
        return 1
    }
    set ignorekey $Pref(dir,ignorekey)
    # Different size is enough when doing binary compare
    if {$stat1(size) != $stat2(size) && $Pref(comparelevel) eq "1b" \
        && !$ignorekey} {
        return 0
    }
    # Same size and time is always considered equal
    if {$stat1(size) == $stat2(size) && $stat1(mtime) == $stat2(mtime)} {
	return 1
    }
    # Don't check further if contents should not be checked
    if {$Pref(comparelevel) == 0} {
        return 0
    }
    # Don't check further if any is a directory
    if {$isdir1 || $isdir2} {
        # Consider dirs equal until we implement something recursive
	return 1
    }

    switch $Pref(comparelevel) {
        1b -
        1 { # Check contents internally
            set bufsz 65536
            set eq 1
            set ch1 [open $file1 r]
            set ch2 [open $file2 r]
            if {$Pref(comparelevel) eq "1b"} {
                fconfigure $ch1 -translation binary
                fconfigure $ch2 -translation binary
            }
            if {$ignorekey} {
                # Assume that all keywords are in the first block
                set f1 [read $ch1 $bufsz]
                set f2 [read $ch2 $bufsz]







|




|








|








|
|





|







64
65
66
67
68
69
70
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
107
108
    # Same type?
    set isdir1 [FileIsDirectory $file1]
    set isdir2 [FileIsDirectory $file2]
    if {$isdir1 != $isdir2} {
	return 0
    }
    # If contents is not checked, same size is enough to be equal
    if {$stat1(size) == $stat2(size) && $Pref(dir,comparelevel) == 0} {
        return 1
    }
    set ignorekey $Pref(dir,ignorekey)
    # Different size is enough when doing binary compare
    if {$stat1(size) != $stat2(size) && $Pref(dir,comparelevel) == 2 \
        && !$ignorekey} {
        return 0
    }
    # Same size and time is always considered equal
    if {$stat1(size) == $stat2(size) && $stat1(mtime) == $stat2(mtime)} {
	return 1
    }
    # Don't check further if contents should not be checked
    if {$Pref(dir,comparelevel) == 0} {
        return 0
    }
    # Don't check further if any is a directory
    if {$isdir1 || $isdir2} {
        # Consider dirs equal until we implement something recursive
	return 1
    }

    switch $Pref(dir,comparelevel) {
        2 -
        1 { # Check contents internally
            set bufsz 65536
            set eq 1
            set ch1 [open $file1 r]
            set ch2 [open $file2 r]
            if {$Pref(dir,comparelevel) == 2} {
                fconfigure $ch1 -translation binary
                fconfigure $ch2 -translation binary
            }
            if {$ignorekey} {
                # Assume that all keywords are in the first block
                set f1 [read $ch1 $bufsz]
                set f2 [read $ch2 $bufsz]
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
            }
            if {![eof $ch1] || ![eof $ch2]} {
                set eq 0
            }
            close $ch1
            close $ch2
        }
        2 { # Simple external diff
            set eq [expr {![catch {exec $::util(diffexe) -q $file1 $file2}]}]
        }
        3 { # Ignore space
            set eq [expr {![catch {exec $::util(diffexe) -q -w $file1 $file2}]}]
        }
        4 { # Ignore case
            set eq [expr {![catch {exec $::util(diffexe) -q -i $file1 $file2}]}]
        }
    }
    return $eq
}

# Display two files in the directory windows and set up info for
# interacting with them.
# diff: Do they differ.
# level: Depth in a recursive run.
# The values in infoFiles are:
# 1 = noLeft, 2 = noRight, 4 = left is dir, 8 = right is dir, 16 = diff
proc ListFiles {df1 df2 diff level} {
    global dirdiff Pref

    lappend dirdiff(leftFiles) $df1
    lappend dirdiff(rightFiles) $df2
    set info [expr {$diff? 16 : 0}]
    if {$df1 eq ""} {
        incr info 1
    }
    if {$df2 eq ""} {
        incr info 2
    }
    if {$df1 != ""} {
	set f1 [file split $df1]
	set i [expr {[llength $f1] - $level - 1}]
	set f1 [eval file join [lrange $f1 $i end]]
    }
    if {$df2 != ""} {
	set f2 [file split $df2]
	set i [expr {[llength $f2] - $level - 1}]
	set f2 [eval file join [lrange $f2 $i end]]
    }

    if {[FileIsDirectory $df1]} {
	append f1 /
        incr info 4
    }
    if {[FileIsDirectory $df2]} {
	append f2 /
        incr info 8
    }

    set maptag [expr {$diff ? "change" : ""}]
    if {$df1 eq ""} {
	set tag2 new2
        set maptag new2
    } elseif {!$diff} {
	set tag2 ""
    } else {
        if {$info & 8} {
            set tag2 changed
            set maptag ""
        } else {
            set tag2 change
        }
    }

    if {$df2 eq ""} {
	set tag1 new1
        set maptag new1
    } elseif {!$diff} {
	set tag1 ""
    } else {
        if {$info & 4} {
            set tag1 changed
            set maptag ""
        } else {
            set tag1 change
        }
    }
    addChange .dirdiff 1 $maptag 0 0 0 0

    if {$df2 eq ""} {
	$dirdiff(wRight) insert end \n
    } else {
        if {[catch {set size [file size $df2]}]} {
            set size -1
            set mtime 0
            lappend tag2 invalid
        } else {
            set mtime [file mtime $df2]
        }
	$dirdiff(wRight) insert end [format "%-30s %8d %16s\n" \
                $f2 $size \
		[clock format $mtime -format "%Y-%m-%d %H:%M"]] \
		$tag2
    }
    if {$df1 eq ""} {
	$dirdiff(wLeft) insert end \n
    } else {
        if {[catch {set size [file size $df1]}]} {
            set size -1
            set mtime 0
            lappend tag1 invalid
        } else {
            set mtime [file mtime $df1]
        }
	$dirdiff(wLeft) insert end [format "%-30s %8d %16s\n" \
                $f1 $size \
		[clock format $mtime -format "%Y-%m-%d %H:%M"]] \
		$tag1
    }
    lappend dirdiff(infoFiles) $info
}

# Returns the contents of a directory as a sorted list of file tails.
proc DirContents {dir} {
    global Pref
    set files [glob -tails -directory $dir -nocomplain * {.[a-zA-Z]*}]

    if {$Pref(dir,onlyrev)} {
        # FIXA: move to rev and make general for other systems







<
<
|
<
<
<
<
<
<
<



<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







128
129
130
131
132
133
134


135







136
137
138





































































































139
140
141
142
143
144
145
            }
            if {![eof $ch1] || ![eof $ch2]} {
                set eq 0
            }
            close $ch1
            close $ch2
        }


    }







    return $eq
}






































































































# Returns the contents of a directory as a sorted list of file tails.
proc DirContents {dir} {
    global Pref
    set files [glob -tails -directory $dir -nocomplain * {.[a-zA-Z]*}]

    if {$Pref(dir,onlyrev)} {
        # FIXA: move to rev and make general for other systems
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
    }

    set files2 {}
    foreach file $files {
        set full [file join $dir $file]
        # Apply filters
        if {[FileIsDirectory $full]} {
            if {$Pref(nodir)} continue
            if {[llength $Pref(dir,incdirs)] == 0} {
                set allowed 1
            } else {
                set allowed 0
                foreach pat $Pref(dir,incdirs) {
                    if {[string match $pat $file]} {
                        set allowed 1







<







163
164
165
166
167
168
169

170
171
172
173
174
175
176
    }

    set files2 {}
    foreach file $files {
        set full [file join $dir $file]
        # Apply filters
        if {[FileIsDirectory $full]} {

            if {[llength $Pref(dir,incdirs)] == 0} {
                set allowed 1
            } else {
                set allowed 0
                foreach pat $Pref(dir,incdirs) {
                    if {[string match $pat $file]} {
                        set allowed 1
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424




425


426
427




428





429
430
431
432
433
434
435
436
437

438
439
440
441

442

443


444
445

446



447
448
449
450
451




452






453
454


455
456




457
458

459
460
461
462
463
464
465
466
467


468
469
470

471







472

473
474
475

476
477

478
479
480
481
482
483








484
485
486
487
488
489
490
491
492
493

494
495
496
497
498
499
500



501
502
503
504
505
506
507
508


509
510
511
512














513
514
515
516


517
518
519
520






521
522

523

524
525



526

527

528



529
530
531
532
533
534
535
536


537
538
539
540
541


542
543
544
545



546



547










548
549



550
551

552
553
554
555
556



557
558
559


560
561
562

563
564

565

566






567
568

569








570

571


572

573
574



575
576
577

578

579

580





581









582




583
584
585




586
587

588
589




590
591
592



593

594

595
596






597






598
599













600
601
602
603

604
605

606
607





608
609





610


611
612
613
614






615
616


617
618
619
620
621




622


623
624
625


626
627

628
629
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
        }
        lappend files2 $file
    }

    return [Fsort $files2]
}

# Compare two directories.
proc CompareDirs {dir1 dir2 {level 0}} {
    global Pref
    set files1 [DirContents $dir1]
    set files2 [DirContents $dir2]

    set len1 [llength $files1]
    set len2 [llength $files2]

    set p1 0
    set p2 0
    while 1 {
	if {$p1 < $len1 && $p2 < $len2} {
	    set f1 [lindex $files1 $p1]
	    set df1 [file join $dir1 $f1]
	    set f2 [lindex $files2 $p2]
	    set df2 [file join $dir2 $f2]
            set apa [FStrCmp $f1 $f2]
            if {$apa == 0} {
                set apa [expr {- [FileIsDirectory $df1] \
                               + [FileIsDirectory $df2]}]
            }
	    switch -- $apa {
		0 {
		    set diff [expr {![CompareFiles $df1 $df2]}]
		    if {$diff || !$Pref(dir,onlydiffs)} {
			ListFiles $df1 $df2 $diff $level
		    }
                    # Optionally recurse
		    if {[FileIsDirectory $df1] && [FileIsDirectory $df2] && \
			    $Pref(recursive)} {
			CompareDirs $df1 $df2 [expr {$level + 1}]
		    }
		    incr p1
		    incr p2
		}
		-1 {
		    ListFiles $df1 "" 0 $level
		    incr p1
		}
		1 {
		    ListFiles "" $df2 0 $level
		    incr p2
		}
	    }
	} elseif {$p1 < $len1 && $p2 >= $len2} {
	    set f1 [lindex $files1 $p1]
	    ListFiles [file join $dir1 $f1] "" 0 $level
	    incr p1
	} elseif {$p1 >= $len1 && $p2 < $len2} {
	    set f2 [lindex $files2 $p2]
	    ListFiles "" [file join $dir2 $f2] 0 $level
	    incr p2
	} else {
	    break
	}
    }
}

# Run the directory comparison
proc doDirCompare {} {
    global dirdiff
    if {![FileIsDirectory $dirdiff(leftDir)]} return
    if {![FileIsDirectory $dirdiff(rightDir)]} return
    set dirdiff(leftFiles)  {}
    set dirdiff(rightFiles) {}
    set dirdiff(infoFiles)  {}
    set dirdiff(leftMark)   ""
    set dirdiff(rightMark)  ""

    set tail1 [file tail $dirdiff(leftDir)]
    set tail2 [file tail $dirdiff(rightDir)]
    if {$tail1 eq $tail2} {
        wm title .dirdiff "Eskil Dir: $tail1"
    } else {
        wm title .dirdiff "Eskil Dir: $tail1 vs $tail2"
    }

    $dirdiff(wLeft) delete 1.0 end
    $dirdiff(wRight) delete 1.0 end
    set top .dirdiff
    busyCursor $top
    clearMap $top
    update idletasks
    CompareDirs $dirdiff(leftDir) $dirdiff(rightDir)
    normalCursor .dirdiff
    drawMap $top -1
}

# Pick a directory for compare
proc BrowseDir {dirVar entryW} {
    global Pref
    upvar "#0" $dirVar dir

    set newdir $dir
    while {$newdir != "." && ![FileIsDirectory $newdir]} {
        set newdir [file dirname $newdir]
    }
    set newdir [tk_chooseDirectory -initialdir $newdir -title "Select Directory"]
    if {$newdir != ""} {
        set dir $newdir
        $entryW xview end
    }
    if {$Pref(autocompare)} doDirCompare
}





# This is called when double clicking on a file in


# the directory compare window
proc SelectFile {w x y} {




    global dirdiff Pref






    set row [expr {int([$w index @$x,$y]) - 1}]
    set lf [lindex $dirdiff(leftFiles) $row]
    set rf [lindex $dirdiff(rightFiles) $row]
    set i [lindex $dirdiff(infoFiles) $row]
    if {($i & 12) == 12} { # Both are dirs
        set dirdiff(leftDir) $lf
        set dirdiff(rightDir) $rf
        .dirdiff.e1 xview end

        .dirdiff.e2 xview end
        if {$Pref(autocompare)} doDirCompare
    } elseif {$i & 4} { # Left is dir
        set dirdiff(leftDir) $lf

        .dirdiff.e1 xview end

        if {$Pref(autocompare)} doDirCompare


    } elseif {$i & 8} { # Right is dir
        set dirdiff(rightDir) $rf

        .dirdiff.e2 xview end



        if {$Pref(autocompare)} doDirCompare
    } elseif {($i & 3) == 0} { # Both exists
        # Open a diff window for them
        newDiff $lf $rf
    }




}







# Bring up a context menu on a file.


proc DirRightClick {w x y X Y} {
    global dirdiff Pref





    set row [expr {int([$w index @$x,$y]) - 1}]

    set lf [lindex $dirdiff(leftFiles) $row]
    set rf [lindex $dirdiff(rightFiles) $row]
    set i [lindex $dirdiff(infoFiles) $row]

    set m .dirdiff.popup
    destroy $m
    menu $m
    if {($i & 12) == 12} { # Both are dirs
        $m add command -label "Compare Directories" -command "


            [list set dirdiff(leftDir) $lf]
            [list set dirdiff(rightDir) $rf]
            [list if \$Pref(autocompare) "after idle doDirCompare"]

        "







    }

    if {$i & 4 && $w eq $dirdiff(wLeft)} { # Left is dir
        $m add command -label "Step down left directory" -command "
            [list set dirdiff(leftDir) $lf]

            [list if \$Pref(autocompare) "after idle doDirCompare"]
        "

    }
    if {$i & 8 && $w eq $dirdiff(wRight)} { # Right is dir
        $m add command -label "Step down right directory" -command "
            [list set dirdiff(rightDir) $rf]
            [list if \$Pref(autocompare) "after idle doDirCompare"]
        "








    }
    if {($i & 12) == 0 && ($i & 3) == 0} { # Neither is dir, Both exists
        $m add command -label "Compare Files" -command [list \
                newDiff $lf $rf]
    }
    if {$w eq $dirdiff(wLeft) && ($i & 13) == 0} {
        $m add command -label "Copy File" \
                -command [list CopyFile $row right]
        $m add command -label "Edit File" \
                -command [list EditFile $row left]

        $m add command -label "Mark File" \
                -command [list set ::dirdiff(leftMark) $lf]
	if {$::dirdiff(rightMark) != ""} {
	    $m add command -label "Compare with $::dirdiff(rightMark)" \
		    -command [list newDiff $lf $::dirdiff(rightMark)]
	}
    }



    if {$w eq $dirdiff(wRight) && ($i & 14) == 0} {
        $m add command -label "Copy File" \
                -command [list CopyFile $row left]
        $m add command -label "Edit File" \
                -command [list EditFile $row right]
        $m add command -label "Mark File" \
                -command [list set ::dirdiff(rightMark) $rf]
	if {$::dirdiff(leftMark) != ""} {


	    $m add command -label "Compare with $::dirdiff(leftMark)" \
		    -command [list newDiff $::dirdiff(leftMark) $rf]
	}
    }















    tk_popup $m $X $Y
}



# Copy a file from one directory to the other
proc CopyFile {row to} {
    global dirdiff Pref







    if {$to eq "left"} {
        set src [lindex $dirdiff(rightFiles) $row]

        set n [expr {[string length $dirdiff(rightDir)] + 1}]

        set dst [file join $dirdiff(leftDir) [string range $src $n end]]
    } elseif {$to eq "right"} {



        set src [lindex $dirdiff(leftFiles) $row]

        set n [expr {[string length $dirdiff(leftDir)] + 1}]

        set dst [file join $dirdiff(rightDir) [string range $src $n end]]



    } else {
        error "Bad to argument to CopyFile: $to"
    }

    if {[file exists $dst]} {
        if {[tk_messageBox -icon question -title "Overwrite file?" -message \
                "Copy\n$src\noverwriting\n$dst ?" -type yesno] eq "yes"} {
            file copy -force $src $dst


        }
    } else {
        if {[tk_messageBox -icon question -title "Copy file?" -message \
                "Copy\n$src\nto\n$dst ?" -type yesno] eq "yes"} {
            file copy $src $dst


        }
    }
}




# Bring up an editor to display a file.



proc EditFile {row from} {










    global dirdiff Pref




    if {$from eq "left"} {
        set src [file join $dirdiff(leftDir) [lindex $dirdiff(leftFiles) $row]]

    } elseif {$from eq "right"} {
        set src [file join $dirdiff(rightDir) [lindex $dirdiff(rightFiles) $row]]
    } else {
        error "Bad from argument to EditFile: $from"
    }




    locateEditor ::util(editor)
    exec $::util(editor) $src &


}

# Go up one level in directory hierarchy.

# 0 = both
proc UpDir {{n 0}} {

    global dirdiff Pref

    switch $n {






        0 {
            set dirdiff(leftDir) [file dirname $dirdiff(leftDir)]

            set dirdiff(rightDir) [file dirname $dirdiff(rightDir)]








            .dirdiff.e1 xview end

            .dirdiff.e2 xview end


            if {$Pref(autocompare)} doDirCompare

        }
        1 {



            set dirdiff(leftDir) [file dirname $dirdiff(leftDir)]
            .dirdiff.e1 xview end
            if {$Pref(autocompare)} doDirCompare

        }

        2 {

            set dirdiff(rightDir) [file dirname $dirdiff(rightDir)]





            .dirdiff.e2 xview end









            if {$Pref(autocompare)} doDirCompare




        }
    }
}





# Create directory diff window.

proc makeDirDiffWin {{redraw 0}} {
    global Pref dirdiff





    set top .dirdiff
    if {[winfo exists $top] && [winfo toplevel $top] eq $top} {



        if {$redraw} {

            eval destroy [winfo children $top]

        } else {
            raise $top






            focus -force $top






            return
        }













    } else {
        destroy $top
        toplevel $top
        lappend ::diff(diffWindows) $top

    }


    wm title $top "Eskil Dir"
    wm protocol $top WM_DELETE_WINDOW [list cleanupAndExit $top]






    frame $top.fm





    frame $top.fe1


    frame $top.fe2

    menu $top.m
    $top configure -menu $top.m







    $top.m add cascade -menu $top.m.mf -label "File" -underline 0


    menu $top.m.mf
    $top.m.mf add command -label "Compare" -underline 1 \
            -command doDirCompare
    $top.m.mf add separator
    $top.m.mf add command -label "Close" -underline 0 \




            -command [list cleanupAndExit $top]


    $top.m.mf add separator
    $top.m.mf add command -label "Quit" -underline 0 \
            -command [list cleanupAndExit all]



    $top.m add cascade -menu $top.m.mo -label "Preferences" -underline 0

    menu $top.m.mo
    $top.m.mo add command -label "Prefs..." -command makeDirDiffPrefWin
    $top.m.mo add checkbutton -variable Pref(recursive) -label "Recursive"
    $top.m.mo add cascade -label "Check" -menu $top.m.mo.mc
    $top.m.mo add checkbutton -variable Pref(dir,onlydiffs) -label "Diffs Only"
    $top.m.mo add checkbutton -variable Pref(nodir)    -label "No Directory"
    $top.m.mo add checkbutton -variable Pref(autocompare) -label "Auto Compare"
















    menu $top.m.mo.mc
    $top.m.mo.mc add radiobutton -variable Pref(comparelevel) -value 0 \

            -label "Do not check contents"
    $top.m.mo.mc add radiobutton -variable Pref(comparelevel) -value 1 \
            -label "Internal compare"
    $top.m.mo.mc add radiobutton -variable Pref(comparelevel) -value 1b \


            -label "Internal compare (bin)"
    $top.m.mo.mc add radiobutton -variable Pref(comparelevel) -value 2 \




            -label "Use Diff"


    $top.m.mo.mc add radiobutton -variable Pref(comparelevel) -value 3 \


            -label "Diff, ignore blanks"
    $top.m.mo.mc add radiobutton -variable Pref(comparelevel) -value 4 \
            -label "Diff, ignore case"
    $top.m.mo.mc add checkbutton -variable Pref(dir,ignorekey) \



            -label "Ignore \$Keyword:\$"












    $top.m add cascade -label "Tools" -underline 0 -menu $top.m.mt
    menu $top.m.mt
    $top.m.mt add command -label "New Diff Window" -underline 0 \
            -command makeDiffWin
    $top.m.mt add command -label "Clip Diff" -underline 0 \
            -command makeClipDiffWin
    if {$::tcl_platform(platform) eq "windows"} {
        if {![catch {package require registry}]} {
            $top.m.mt add separator


            $top.m.mt add command -label "Setup Registry" -underline 6 \

                    -command makeRegistryWin

        }




    }

    $top.m add cascade -label "Help" -underline 0 -menu $top.m.help

    menu $top.m.help
    $top.m.help add command -label "Tutorial" -command makeTutorialWin \

            -underline 0
    $top.m.help add command -label "About" -command makeAboutWin -underline 0





















    if {$::debug} {
        $top.m add cascade -label "Debug" -menu $top.m.md -underline 0
        menu $top.m.md
        if {$::tcl_platform(platform) eq "windows"} {
            $top.m.md add checkbutton -label "Console" -variable consolestate \
                    -onvalue show -offvalue hide -command {console $consolestate}
            $top.m.md add separator
        }

        $top.m.md add command -label "Reread Source" -underline 0 \


                -command {EskilRereadSource}
        $top.m.md add separator

        $top.m.md add command -label "Redraw Window" -command {makeDirDiffWin 1}


    }





    button $top.bu -text "Up Both" -command UpDir -underline 0 -padx 10


    bind $top <Alt-u> "$top.bu invoke"


    button $top.bc -text "Compare" -command doDirCompare -underline 0 -padx 10

    bind $top <Alt-c> "$top.bc invoke"
    pack $top.bc $top.bu -in $top.fm -side right
    pack $top.bu -padx 6






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






    entry $top.e1 -textvariable dirdiff(leftDir)





    button $top.bu1 -text "Up" -padx 10 -command {UpDir 1}

    button $top.bb1 -text "Browse"  -padx 10 \



            -command [list BrowseDir dirdiff(leftDir) $top.e1]

    $top.e1 xview end
    entry $top.e2 -textvariable dirdiff(rightDir)




    button $top.bu2 -text "Up" -padx 10 -command {UpDir 2}


    button $top.bb2 -text "Browse" -padx 10 \


            -command [list BrowseDir dirdiff(rightDir) $top.e2]
    $top.e2 xview end

    bind $top.e1 <Return> doDirCompare
    bind $top.e2 <Return> doDirCompare













    pack $top.bb1 $top.bu1 -in $top.fe1 -side right -pady 1

    pack $top.bu1 -padx 6
    pack $top.e1 -in $top.fe1 -side left -fill x -expand 1
    pack $top.bb2 $top.bu2 -in $top.fe2 -side right -pady 1
    pack $top.bu2 -padx 6
    pack $top.e2 -in $top.fe2 -side left -fill x -expand 1





    text $top.t1 -height 40 -width 60 -wrap none -font myfont \
	    -xscrollcommand "$top.sbx1 set" -takefocus 0
    scrollbar $top.sby -orient vertical

    scrollbar $top.sbx1 -orient horizontal -command "$top.t1 xview"


    text $top.t2 -height 40 -width 60 -wrap none -font myfont \
	    -xscrollcommand "$top.sbx2 set" -takefocus 0

    scrollbar $top.sbx2 -orient horizontal -command "$top.t2 xview"
    commonYScroll $top.sby $top.t1 $top.t2
    set map [createMap $top]



    bind $top.t1 <Double-Button-1> "after idle SelectFile $top.t1 %x %y"
    bind $top.t2 <Double-Button-1> "after idle SelectFile $top.t2 %x %y"

    bind $top.t1 <Button-3> "DirRightClick $top.t1 %x %y %X %Y"
    bind $top.t2 <Button-3> "DirRightClick $top.t2 %x %y %X %Y"








    set dirdiff(wLeft)  $top.t1



    set dirdiff(wRight) $top.t2


    set dirdiff(wY) $top.sby

    # Interact better with diff by setting these



    set ::widgets($top,wDiff1) $top.t1




    set ::widgets($top,wDiff2) $top.t2


    applyColor




    grid $top.fm   -    -    -         -sticky we
    grid $top.fe1  x    x    $top.fe2  -sticky we
    grid $top.t1   $map $top.sby $top.t2   -sticky news
    grid $top.sbx1 x    x    $top.sbx2 -sticky we




    grid $map -pady [expr {[$top.sby cget -width] + 2}]

    grid rowconfigure    $top  2    -weight 1
    grid columnconfigure $top {0 3} -weight 1


}

# Transfer preferences from dialog to real settings
proc ApplyDirDiffPref {} {
    foreach item {
        recursive
        dir,onlydiffs
        nodir
        autocompare
        comparelevel
        dir,ignorekey
        dir,onlyrev
    } {
        set ::Pref($item) $::TmpPref($item)
    }
    # Handle preferences that must be a list
    foreach item {







<
<
<
<
<
|
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
















<


>
>
>
>
|
>
>
|
|
>
>
>
>
|
>
>
>
>
>
|
<
<
<
<
<
<
<
|
>
|
<
|
<
>
|
>
|
>
>
|
|
>
|
>
>
>
|
|
|
|
|
>
>
>
>
|
>
>
>
>
>
>

<
>
>
|
|
>
>
>
>

<
>
|
<
<
|
<
|
<
|
<
>
>
|
<
<
>
|
>
>
>
>
>
>
>
|
>
|
<
<
>
|
|
>
|
<
|
|
|
|
>
>
>
>
>
>
>
>
|
<
<
|

|
<
<
<
<
>
|
|
<
<
<
|
|
>
>
>
|
|
|
<
|
<
|
|
>
>
|
<
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
|
|
>
>
|
|
|

>
>
>
>
>
>
|
|
>
|
>
|
|
>
>
>
|
>
|
>
|
>
>
>
|
|
|

|
|
|
|
>
>
|
|
|
|
|
>
>
|
|
|

>
>
>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
|

>
>
>
|
<
>
|
<
|
<
|
>
>
>
|
|
|
>
>
|

<
>
|
<
>
|
>
|
>
>
>
>
>
>
|
<
>
|
>
>
>
>
>
>
>
>
|
>
|
>
>
|
>
|
|
>
>
>
|
|
|
>

>
|
>
|
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
|
>
>
>
>


|
>
>
>
>

|
>
|
|
>
>
>
>

|
<
>
>
>
|
>
|
>
|
|
>
>
>
>
>
>
|
>
>
>
>
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
<
>
|
|
>
|
<
>
>
>
>
>
|
|
>
>
>
>
>
|
>
>
|
|
<
<
>
>
>
>
>
>
|
<
>
>
|
<
<
|
<
>
>
>
>
|
>
>
|
<
|
>
>
|
<
>
|
<
<
<
<
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
|
>
|
|
|
|
>
>
|
<
>
>
>
>
|
>
>
|
>
>
|
<
|
<
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>

|
|
|
|
<
|
|
<
|
>
>
|
>
|
>
|
>
>
>
>
|
|
<
>
|
<
>
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
<
<
<
<
|
|
>
|
>
>
|
|
>
|
>
>
|
>
>

>
>
|
>
>
|
>
>
|
>
|
<
|
>
>
>
>
>

<
<
>
>
>
>

>
|
>
>
>
>
>
|
>
|
>
>
>
|
>
|
<
>
>
>
>
|
>
>
|
>
>
|
|
>
|
<
>
>
>
>
>
>
>
|
>
>
>
>
>
|
>
|
<
<
|
|
>
>

>
>
|
|
<
>
|
>
>
|
|
>
|
<
<
>
>
|
<
<
>
|
<
>
>
>
>
|
>
>
>
|
>
>
>
|
>
>
|
>
|
>
>
>
|
>
>
>
>
|
>
|
<
>
>
>
|
<
<
<
<
>
>
>
|
<
|
<
<
>
|





<
<
<
<
|







211
212
213
214
215
216
217





218


219

















































220










221

















222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259







260
261
262

263

264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

294
295
296
297
298
299
300
301
302

303
304


305

306

307

308
309
310


311
312
313
314
315
316
317
318
319
320
321
322


323
324
325
326
327

328
329
330
331
332
333
334
335
336
337
338
339
340


341
342
343




344
345
346



347
348
349
350
351
352
353
354

355

356
357
358
359
360

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

378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454

455
456

457

458
459
460
461
462
463
464
465
466
467
468

469
470

471
472
473
474
475
476
477
478
479
480
481

482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550

551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588



589
590
591
592
593

594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610


611
612
613
614
615
616
617

618
619
620


621

622
623
624
625
626
627
628
629

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
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
        }
        lappend files2 $file
    }

    return [Fsort $files2]
}






# Bring up an editor to display a file.


proc EditFile {file} {

















































    locateEditor ::util(editor)










    exec $::util(editor) $file &

















}

# Pick a directory for compare
proc BrowseDir {dirVar entryW} {
    global Pref
    upvar "#0" $dirVar dir

    set newdir $dir
    while {$newdir != "." && ![FileIsDirectory $newdir]} {
        set newdir [file dirname $newdir]
    }
    set newdir [tk_chooseDirectory -initialdir $newdir -title "Select Directory"]
    if {$newdir != ""} {
        set dir $newdir
        $entryW xview end
    }

}

snit::widget DirCompare {
    component tree
    component hsb
    component vsb

    option -leftdir  -default "" -configuremethod SetDirOption
    option -rightdir -default "" -configuremethod SetDirOption

    variable AfterId
    variable IdleQueue
    variable IdleQueueArr
    variable leftMark ""
    variable rightMark ""

    constructor {args} {
        install tree using ttk::treeview $win.tree -height 20 \
                -columns {type status leftfull leftname leftsize leftdate rightfull rightname rightsize rightdate} \
                -displaycolumns {leftname leftsize leftdate rightname rightsize rightdate}
        if {[tk windowingsystem] ne "aqua"} {
            install vsb using ttk::scrollbar $win.vsb -orient vertical \







                    -command "$tree yview"
            install hsb using ttk::scrollbar $win.hsb -orient horizontal \
                    -command "$tree xview"

        } else {

            install vsb using scrollbar $win.vsb -orient vertical \
                    -command "$tree yview"
            install hsb using scrollbar $win.hsb -orient horizontal \
                    -command "$tree xview"
        }
        $tree configure -yscroll "$vsb set" -xscroll "$hsb set"

        set AfterId ""
        set IdleQueue {}

        $tree heading \#0 -text "Structure"
        $tree heading leftname -text "Name"
        $tree heading leftsize -text "Size"
        $tree heading leftdate -text "Date"
        $tree heading rightname -text "Name"
        $tree heading rightsize -text "Size"
        $tree heading rightdate -text "Date"

        $tree column leftsize  -stretch 0 -width 70 -anchor e
        $tree column rightsize -stretch 0 -width 70 -anchor e
        $tree column leftdate  -stretch 0 -width 120
        $tree column rightdate -stretch 0 -width 120

        $tree tag configure unknown -foreground grey
        $tree tag configure empty   -foreground grey
        $tree tag configure equal   -foreground {}
        $tree tag configure new     -foreground green
        $tree tag configure old     -foreground blue
        $tree tag configure change  -foreground red


        bind $tree <<TreeviewOpen>> "[mymethod UpdateDirNode] \[%W focus\]"
        bind $tree <Button-3> "[mymethod ContextMenu] %x %y %X %Y"
        bind $tree <Double-ButtonPress-1> "[mymethod DoubleClick] %x %y"

        grid $tree $vsb -sticky nsew
        grid $hsb         -sticky nsew
        grid columnconfigure $win 0 -weight 1
        grid rowconfigure    $win 0 -weight 1


        $self configurelist $args
        #$self ReStart


    }

    destructor {

        if {$AfterId ne ""} {

            after cancel $AfterId
        }
        set AfterId ""


    }

    method SetDirOption {option value} {
        set options($option) $value
        if {$options(-leftdir) ne "" && \
                [file isdirectory $options(-leftdir)] && \
                $options(-rightdir) ne "" && \
                [file isdirectory $options(-rightdir)]} {
            after idle [mymethod ReStart]
        }
    }



    method ReStart {} {
        # Delete all idle processing
        if {$AfterId ne ""} {
            after cancel $AfterId
        }

        set AfterId ""
        set IdleQueue {}
        array unset IdleQueueArr
        
        # Fill in clean root data
        $tree delete [$tree children {}]
        $tree set {} type directory
        $self SetNodeStatus {} empty
        $tree set {} leftfull   $options(-leftdir)
        $tree set {} leftname   [file tail $options(-leftdir)]
        $tree set {} rightfull  $options(-rightdir)
        $tree set {} rightname  [file tail $options(-rightdir)]



        $self UpdateDirNode {}
    }





    # Format a time stamp for display
    proc FormatDate {date} {
        clock format $date -format "%Y-%m-%d %H:%M:%S"



    }

    # Remove all equal nodes from tree
    method PruneEqual {} {
        set todo [$tree children {}]
        while {[llength $todo] > 0} {
            set todoNow $todo
            set todo {}

            foreach node $todoNow {

                set status [$tree set $node status]
                if {$status eq "equal"} {
                    $tree delete $node
                } else {
                    eval lappend todo [$tree children $node]

                }
            }
        }
    }

    # Open or close all directories in the tree view
    method OpenAll {{state 1}} {
        set todo [$tree children {}]
        while {[llength $todo] > 0} {
            set todoNow $todo
            set todo {}
            foreach node $todoNow {
                set children [$tree children $node]
                if {[llength $children] > 0} {
                    $tree item $node -open $state
                    eval lappend todo $children
                }

            }
        }
    }

    # Copy a file from one directory to the other
    method CopyFile {node from} {
        global dirdiff Pref

        set lf [$tree set $node leftfull]
        set rf [$tree set $node rightfull]
        set parent [$tree parent $node]
        set lp [$tree set $parent leftfull]
        set rp [$tree set $parent rightfull]

        if {$from eq "left"} {
            set src $lf
            if {$rf ne ""} {
                set dst $rf
            } elseif {$rp ne ""} {
                set dst [file join $rp [file tail $src]]
            } else {
                return
            }
        } elseif {$from eq "right"} {
            set src $rf
            if {$lf ne ""} {
                set dst $lf
            } elseif {$lp ne ""} {
                set dst [file join $lp [file tail $src]]
            } else {
                return
            }
        } else {
            error "Bad to argument to CopyFile: $to"
        }

        if {[file exists $dst]} {
            if {[tk_messageBox -icon question -title "Overwrite file?" -message \
                    "Copy\n$src\noverwriting\n$dst ?" -type yesno] eq "yes"} {
                file copy -force $src $dst
                # FIXA: update file info in tree too
                $self SetNodeStatus $node equal
            }
        } else {
            if {[tk_messageBox -icon question -title "Copy file?" -message \
                    "Copy\n$src\nto\n$dst ?" -type yesno] eq "yes"} {
                file copy $src $dst
                # FIXA: update file info in tree too
                $self SetNodeStatus $node equal
            }
        }
    }

    # React on double-click
    method DoubleClick {x y} {
        set node [$tree identify row $x $y]

        set lf [$tree set $node leftfull]
        set rf [$tree set $node rightfull]
        set type [$tree set $node type]

        # On a file that exists on both sides, start a file diff
        if {$type eq "file" && $lf ne "" && $rf ne ""} {
            newDiff $lf $rf
            # Stop the default bindings from running
            break
        }
    }

    # Bring up a context menu on a file.
    method ContextMenu {x y X Y} {
        #global dirdiff Pref

        set node [$tree identify row $x $y]
        set col [$tree identify column $x $y]
        set colname [$tree column $col -id]


        set lf [$tree set $node leftfull]
        set rf [$tree set $node rightfull]

        set type [$tree set $node type]


        set m $win.popup
        destroy $m
        menu $m
        
        if {$col eq "#0"} {
            $m add command -label "Prune equal" -command [mymethod PruneEqual]
            $m add command -label "Expand all" -command [mymethod OpenAll]
            $m add command -label "Collaps all" -command [mymethod OpenAll 0]
        }


        if {$type eq "file" && $lf ne "" && $rf ne ""} {
            # Files, both exist

            $m add command -label "Compare Files" -command [list \
                    newDiff $lf $rf]
        }
        if {[string match left* $colname] && $lf ne ""} {
            $m add command -label "Copy File" \
                    -command [mymethod CopyFile $node left]
            $m add command -label "Edit File" \
                    -command [list EditFile $lf]
            $m add command -label "Mark File" \
                    -command [list set [myvar leftMark] $lf]
            if {$rightMark != ""} {

                $m add command -label "Compare with $rightMark" \
                        -command [list newDiff $lf $rightMark]
            }
        } elseif {[string match right* $colname] && $rf ne ""} {
            $m add command -label "Copy File" \
                    -command [mymethod CopyFile $node right]
            $m add command -label "Edit File" \
                    -command [list EditFile $rf]
            $m add command -label "Mark File" \
                    -command [list set [myvar rightMark] $rf]
            if {$leftMark != ""} {
                $m add command -label "Compare with $leftMark" \
                        -command [list newDiff $leftMark $rf]
            }
        }

        tk_popup $m $X $Y
    }

    method AddNodeToIdle {node} {
        if {[info exists IdleQueueArr($node)]} { return }
        lappend IdleQueue $node
        set IdleQueueArr($node) 1

        if {$AfterId eq ""} {
            set AfterId [after 1 [mymethod UpdateIdle]]
        }
    }
    method UpdateIdle {} {
        set AfterId "X"

        set pre [clock clicks -milliseconds]
        while {[llength $IdleQueue] > 0} {
            set node [lindex $IdleQueue 0]
            set IdleQueue [lrange $IdleQueue 1 end]
            unset IdleQueueArr($node)

            if {[$tree set $node type] ne "directory"} {
                $self UpdateFileNode $node
            } else {
                $self UpdateDirNode $node
            }
            # Work for at least 20 ms to keep things efficient
            set post [clock clicks -milliseconds]
            if {($post - $pre) > 20} break
        }

        if {[llength $IdleQueue] > 0} {
            set AfterId [after 1 [mymethod UpdateIdle]]
        } else {
            set AfterId ""
        }
    }

    method SetNodeStatus {node status} {
        $tree set $node status $status
        $tree item $node -tags $status
        #puts "Set [$tree item $node -text] to $status"

        # Loop through children to update parent
        set parent [$tree parent $node]
        if {$parent eq ""} { return }

        # If this is only present on one side, there is no need to update
        set lf [$tree set $parent leftfull]
        set rf [$tree set $parent rightfull]
        if {$lf eq "" || $rf eq ""} { return }

        set pstatus equal

        foreach child [$tree children $parent] {
            set status [$tree set $child status]
            switch $status {
                unknown {
                    set pstatus unknown
                    break
                }
                new - old - change {
                    set pstatus change
                }
            }
        }
        #puts "Setting parent [$tree set $parent leftname] to $pstatus"
        $self SetNodeStatus $parent $pstatus
    }

    method UpdateDirNode {node} {
        if {[$tree set $node type] ne "directory"} {
            return
        }
        if {[$tree set $node status] ne "empty"} {
            #puts "Dir [$tree set $node leftfull] already done"
            return
        }
        $tree delete [$tree children $node]

        set leftfull [$tree set $node leftfull]
        set rightfull [$tree set $node rightfull]
        $self CompareDirs $leftfull $rightfull $node
    }

    method UpdateFileNode {node} {
        set leftfull [$tree set $node leftfull]
        set rightfull [$tree set $node rightfull]
        set equal [CompareFiles $leftfull $rightfull]
        if {$equal} {
            $self SetNodeStatus $node equal
        } else {



            $self SetNodeStatus $node change
        }
            
        #$self CompareDirs $leftfull $rightfull $node


        #$self SetNodeStatus $node unknown
        #$tree set $node leftfull
        #$tree set $node leftname
        #$tree set $node rightfull
        #$tree set $node rightname
    }

    # List files under a directory node
    # Returns status for the new node
    method ListFiles {df1 df2 node} {
        if {$df1 ne ""} {
            set type [file type $df1]
            set name [file tail $df1]
        } else {
            set type [file type $df2]
            set name [file tail $df2]
        }


        if {[catch {file stat $df1 stat1}]} {
            set size1 ""
            set time1 ""
        } else {
            set size1 $stat1(size)
            set time1 [FormatDate $stat1(mtime)]
        }

        if {[catch {file stat $df2 stat2}]} {
            set size2 ""
            set time2 ""


        } else {

            set size2 $stat2(size)
            set time2 [FormatDate $stat2(mtime)]
        }
        if {$type eq "directory"} {
            set values [list $type unknown \
                    $df1 "" "" "" \
                    $df2 "" "" ""]
        } else {

            set values [list $type unknown \
                    $df1 [file tail $df1] $size1 $time1 \
                    $df2 [file tail $df2] $size2 $time2]
        }

        set id [$tree insert $node end -text $name \
                -values $values]




        if {$type eq "directory"} {

            ## Make it so that this node is openable
            $tree insert $id 0 -text dummy ;# a dummy
            $tree item $id -text $name/
            $self SetNodeStatus $id empty
            $self AddNodeToIdle $id
        } elseif {$size1 == $size2 && \
                $time1 == $time2} {
            $self SetNodeStatus $id equal
        } elseif {$size1 == ""} {
            $self SetNodeStatus $id new
        } elseif {$size2 == ""} {
            $self SetNodeStatus $id old
        } else {
            $self SetNodeStatus $id unknown
            $self AddNodeToIdle $id
        }

        return [$tree set $id status]
    }

    # Compare two directories.
    method CompareDirs {dir1 dir2 node} {
        global Pref
        if {$dir1 eq ""} {
            set files1 {}
        } else {

            set files1 [DirContents $dir1]
        }
        if {$dir2 eq ""} {
            set files2 {}
        } else {
            set files2 [DirContents $dir2]
        }

        set len1 [llength $files1]
        set len2 [llength $files2]


        set p1 0

        set p2 0
        set status_change 0
        set status_unknown 0
        while 1 {
            if {$p1 < $len1 && $p2 < $len2} {
                set f1 [lindex $files1 $p1]
                set df1 [file join $dir1 $f1]
                set f2 [lindex $files2 $p2]
                set df2 [file join $dir2 $f2]
                set apa [FStrCmp $f1 $f2]
                if {$apa == 0} {
                    # Equal names, separate them if not the same type
                    set apa [expr {- [FileIsDirectory $df1] \
                                   + [FileIsDirectory $df2]}]
                }

                switch -- $apa {
                    0 {
                        set sts [$self ListFiles $df1 $df2 $node]
                        incr p1

                        incr p2
                        if {$sts eq "unknown"} {

                            set status_unknown 1
                        }
                    }
                    -1 {
                        $self ListFiles $df1 "" $node
                        incr p1
                        set status_change 1
                    }
                    1 {
                        $self ListFiles "" $df2 $node
                        incr p2
                        set status_change 1
                    }
                }

            } elseif {$p1 < $len1 && $p2 >= $len2} {
                set f1 [lindex $files1 $p1]

                $self ListFiles [file join $dir1 $f1] "" $node
                incr p1

                set status_change 1
            } elseif {$p1 >= $len1 && $p2 < $len2} {
                set f2 [lindex $files2 $p2]
                $self ListFiles "" [file join $dir2 $f2] $node
                incr p2
                set status_change 1
            } else {
                break
            }
        }
        if {$dir1 eq ""} {
            set status new
        } elseif {$dir2 eq ""} {
            set status old
        } elseif {$status_change} {
            set status change
        } elseif {$status_unknown} {
            set status unknown
        } else {
            set status equal
        }






        $self SetNodeStatus $node $status
    }
}

snit::widget DirDiff {
    hulltype toplevel
    component tree

    constructor {args} {
        lappend ::diff(diffWindows) $win
        wm title $win "Eskil Dir"
        wm protocol $win WM_DELETE_WINDOW [list cleanupAndExit $win]

        install tree using DirCompare $win.dc -leftdir $::dirdiff(leftDir) \
                -rightdir $::dirdiff(rightDir)

        frame $win.fe1
        frame $win.fe2

        menu $win.m
        $hull configure -menu $win.m

        $win.m add cascade -menu $win.m.mf -label "File" -underline 0
        menu $win.m.mf
        $win.m.mf add command -label "Compare" -underline 1 \
                -command [mymethod DoDirCompare] -accelerator "Alt-c"
        bind $win <Alt-c> [mymethod DoDirCompare]

        $win.m.mf add separator
        $win.m.mf add command -label "Close" -underline 0 \
                -command [list cleanupAndExit $win]
        $win.m.mf add separator
        $win.m.mf add command -label "Quit" -underline 0 \
                -command [list cleanupAndExit all]



        $win.m add cascade -menu $win.m.mo -label "Preferences" -underline 0
        menu $win.m.mo
        $win.m.mo add command -label "Prefs..." -command makeDirDiffPrefWin
        $win.m.mo add cascade -label "Check" -menu $win.m.mo.mc

        menu $win.m.mo.mc
        $win.m.mo.mc add radiobutton -variable Pref(dir,comparelevel) -value 0 \
                -label "Do not check contents"
        $win.m.mo.mc add radiobutton -variable Pref(dir,comparelevel) -value 1 \
                -label "Normal compare"
        $win.m.mo.mc add radiobutton -variable Pref(dir,comparelevel) -value 2 \
                -label "Binary compare"
        $win.m.mo.mc add checkbutton -variable Pref(dir,ignorekey) \
                -label "Ignore \$Keyword:\$"
        
        $win.m add cascade -label "Tools" -underline 0 -menu $win.m.mt
        menu $win.m.mt
        $win.m.mt add command -label "New Diff Window" -underline 0 \
                -command makeDiffWin
        $win.m.mt add command -label "Clip Diff" -underline 0 \
                -command makeClipDiffWin

        if {$::tcl_platform(platform) eq "windows"} {
            if {![catch {package require registry}]} {
                $win.m.mt add separator
                $win.m.mt add command -label "Setup Registry" -underline 6 \
                        -command makeRegistryWin
            }
        }
        
        $win.m add cascade -label "Help" -underline 0 -menu $win.m.help
        menu $win.m.help
        $win.m.help add command -label "Tutorial" -command makeTutorialWin \
                -underline 0
        $win.m.help add command -label "About" -command makeAboutWin -underline 0
        

        if {$::debug} {
            $win.m add cascade -label "Debug" -menu $win.m.md -underline 0
            menu $win.m.md
            if {$::tcl_platform(platform) eq "windows"} {
                $win.m.md add checkbutton -label "Console" -variable consolestate \
                        -onvalue show -offvalue hide -command {console $consolestate}
                $win.m.md add separator
            }
            $win.m.md add command -label "Reread Source" -underline 0 \
                    -command {EskilRereadSource}
            $win.m.md add separator
            $win.m.md add command -label "Redraw Window" -command {makeDirDiffWin 1}
        }
        
        button $win.bu -text "Up Both" -command [mymethod UpDir] \
                -underline 0 -padx 15


        bind $win <Alt-u> "$win.bu invoke"
        
        #catch {font delete myfont}
        #font create myfont -family $Pref(fontfamily) -size $Pref(fontsize)

        entry $win.e1 -textvariable dirdiff(leftDir)
        button $win.bu1 -text "Up" -padx 10 -command [mymethod UpDir 1]
        button $win.bb1 -text "Browse"  -padx 10 \
                -command "[list BrowseDir dirdiff(leftDir) $win.e1]

                          [mymethod DoDirCompare]"
        $win.e1 xview end
        entry $win.e2 -textvariable dirdiff(rightDir)
        button $win.bu2 -text "Up" -padx 10 -command [mymethod UpDir 2]
        button $win.bb2 -text "Browse" -padx 10 \
                -command "[list BrowseDir dirdiff(rightDir) $win.e2]
                          [mymethod DoDirCompare]"
        $win.e2 xview end


        bind $win.e1 <Return> [mymethod DoDirCompare]
        bind $win.e2 <Return> [mymethod DoDirCompare]
        


        pack $win.bb1 $win.bu1 -in $win.fe1 -side right -pady 1
        pack $win.bu1 -padx 6

        pack $win.e1 -in $win.fe1 -side left -fill x -expand 1
        pack $win.bb2 $win.bu2 -in $win.fe2 -side right -pady 1
        pack $win.bu2 -padx 6
        pack $win.e2 -in $win.fe2 -side left -fill x -expand 1
        
        grid $win.fe1  $win.bu $win.fe2  -sticky we
        grid $tree     -       -         -sticky news
        grid $win.bu -padx 6

        grid rowconfigure    $win  2    -weight 1
        grid columnconfigure $win {0 2} -weight 1
    }

    method DoDirCompare {} {
        $tree configure -leftdir $::dirdiff(leftDir) \
                -rightdir $::dirdiff(rightDir)
    }

    # Go up one level in directory hierarchy.
    # 0 = both
    method UpDir {{n 0}} {
        global dirdiff Pref
        switch $n {
            0 {
                set dirdiff(leftDir) [file dirname $dirdiff(leftDir)]
                set dirdiff(rightDir) [file dirname $dirdiff(rightDir)]
                .dirdiff.e1 xview end
                .dirdiff.e2 xview end
            }

            1 {
                set dirdiff(leftDir) [file dirname $dirdiff(leftDir)]
                .dirdiff.e1 xview end
            }




            2 {
                set dirdiff(rightDir) [file dirname $dirdiff(rightDir)]
                .dirdiff.e2 xview end
            }

        }


        $self DoDirCompare
    }
}

# Transfer preferences from dialog to real settings
proc ApplyDirDiffPref {} {
    foreach item {




        dir,comparelevel
        dir,ignorekey
        dir,onlyrev
    } {
        set ::Pref($item) $::TmpPref($item)
    }
    # Handle preferences that must be a list
    foreach item {
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
        raise $top
        focus -force $top
        return
    } else {
        destroy $top
        toplevel $top -padx 3 -pady 3
        foreach item {
            recursive
            dir,onlydiffs
            nodir
            autocompare
            comparelevel
            dir,ignorekey
            dir,incfiles
            dir,exfiles
            dir,incdirs
            dir,exdirs
            dir,onlyrev
        } {
            set ::TmpPref($item) $::Pref($item)
        }
    }

    wm title $top "Eskil Directory Preferences"

    set check [labelframe $top.check -text "Check" -padx 3 -pady 3]
    radiobutton $check.rb1 -variable TmpPref(comparelevel) -value 0 \
            -text "Do not check contents"
    radiobutton $check.rb2 -variable TmpPref(comparelevel) -value 1 \
            -text "Internal compare"
    radiobutton $check.rb3 -variable TmpPref(comparelevel) -value 1b \
            -text "Internal compare (bin)"
    radiobutton $check.rb4 -variable TmpPref(comparelevel) -value 2 \
            -text "Use Diff"
    radiobutton $check.rb5 -variable TmpPref(comparelevel) -value 3 \
            -text "Diff, ignore blanks"
    radiobutton $check.rb6 -variable TmpPref(comparelevel) -value 4 \
            -text "Diff, ignore case"
    grid $check.rb1 $check.rb4 -sticky w -padx 3 -pady 3
    grid $check.rb2 $check.rb5 -sticky w -padx 3 -pady 3
    grid $check.rb3 $check.rb6 -sticky w -padx 3 -pady 3
    grid columnconfigure $check {0 1 2} -uniform a -weight 1

    set opts [labelframe $top.opts -text "Options" -padx 3 -pady 3]
    checkbutton $opts.cb1 -variable TmpPref(dir,ignorekey) \
            -text "Ignore \$Keyword:\$"
    checkbutton $opts.cb2 -variable TmpPref(recursive)     -text "Recursive"
    checkbutton $opts.cb3 -variable TmpPref(dir,onlydiffs) -text "Diffs Only"
    checkbutton $opts.cb4 -variable TmpPref(nodir)         -text "No Directory"
    checkbutton $opts.cb5 -variable TmpPref(autocompare)   -text "Auto Compare"
    eval pack [winfo children $opts] -side top -anchor w 

    set filter [labelframe $top.filter -text "Filter" -padx 3 -pady 3]
    label $filter.l1 -text "Include Files" -anchor w
    entry $filter.e1 -width 20 -textvariable TmpPref(dir,incfiles)
    label $filter.l2 -text "Exclude Files" -anchor w
    entry $filter.e2 -width 20 -textvariable TmpPref(dir,exfiles)







<
<
<
<
|














|

|
|
|
<
<
|
<
<
<
<
|
|
|





<
<
<
<







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
        raise $top
        focus -force $top
        return
    } else {
        destroy $top
        toplevel $top -padx 3 -pady 3
        foreach item {




            dir,comparelevel
            dir,ignorekey
            dir,incfiles
            dir,exfiles
            dir,incdirs
            dir,exdirs
            dir,onlyrev
        } {
            set ::TmpPref($item) $::Pref($item)
        }
    }

    wm title $top "Eskil Directory Preferences"

    set check [labelframe $top.check -text "Check" -padx 3 -pady 3]
    radiobutton $check.rb1 -variable TmpPref(dir,comparelevel) -value 0 \
            -text "Do not check contents"
    radiobutton $check.rb2 -variable TmpPref(dir,comparelevel) -value 1 \
            -text "Normal compare"
    radiobutton $check.rb3 -variable TmpPref(dir,comparelevel) -value 2 \


            -text "Binary compare"




    grid $check.rb1 -sticky w -padx 3 -pady 3
    grid $check.rb2 -sticky w -padx 3 -pady 3
    grid $check.rb3 -sticky w -padx 3 -pady 3
    grid columnconfigure $check {0 1 2} -uniform a -weight 1

    set opts [labelframe $top.opts -text "Options" -padx 3 -pady 3]
    checkbutton $opts.cb1 -variable TmpPref(dir,ignorekey) \
            -text "Ignore \$Keyword:\$"




    eval pack [winfo children $opts] -side top -anchor w 

    set filter [labelframe $top.filter -text "Filter" -padx 3 -pady 3]
    label $filter.l1 -text "Include Files" -anchor w
    entry $filter.e1 -width 20 -textvariable TmpPref(dir,incfiles)
    label $filter.l2 -text "Exclude Files" -anchor w
    entry $filter.e2 -width 20 -textvariable TmpPref(dir,exfiles)
887
888
889
890
891
892
893





    
    grid $top.l1 $top.e1 -sticky we
    grid $top.l2 $top.e2 -sticky we
    grid columnconfigure $top 1 -weight 1
    grid rowconfigure    $top 2 -weight 1
    
}












>
>
>
>
>
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
    
    grid $top.l1 $top.e1 -sticky we
    grid $top.l2 $top.e2 -sticky we
    grid columnconfigure $top 1 -weight 1
    grid rowconfigure    $top 2 -weight 1
    
}

proc makeDirDiffWin {{redraw 0}} {
    DirDiff .dirdiff
}