+# Procedures for manipulating the file list window at the
+# bottom right of the overall window.
+proc init_flist {first} {
+ global cflist cflist_top cflist_bot selectedline difffilestart
+
+ $cflist conf -state normal
+ $cflist delete 0.0 end
+ if {$first ne {}} {
+ $cflist insert end $first
+ set cflist_top 1
+ set cflist_bot 1
+ $cflist tag add highlight 1.0 "1.0 lineend"
+ } else {
+ catch {unset cflist_top}
+ }
+ $cflist conf -state disabled
+ set difffilestart {}
+}
+
+proc add_flist {f} {
+ global flistmode cflist
+
+ $cflist conf -state normal
+ if {$flistmode eq "flat"} {
+ $cflist insert end "\n$f"
+ }
+ $cflist conf -state disabled
+}
+
+proc sel_flist {w x y} {
+ global flistmode ctext difffilestart cflist cflist_top
+
+ if {![info exists cflist_top]} return
+ set l [lindex [split [$w index "@$x,$y"] "."] 0]
+ if {$flistmode eq "flat"} {
+ if {$l == 1} {
+ $ctext yview 1.0
+ } else {
+ catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
+ }
+ highlight_flist $l
+ }
+}
+
+proc scrolltext {f0 f1} {
+ global cflist_top
+
+ .ctop.cdet.left.sb set $f0 $f1
+ if {[info exists cflist_top]} {
+ highlight_flist $cflist_top
+ }
+}
+
+# Given an index $tl in the $ctext window, this works out which line
+# of the $cflist window displays the filename whose patch is shown
+# at the given point in the $ctext window. $ll is a hint about which
+# line it might be, and is used as the starting point of the search.
+proc ctext_index {tl ll} {
+ global ctext difffilestart
+
+ while {$ll >= 2 && [$ctext compare $tl < \
+ [lindex $difffilestart [expr {$ll - 2}]]]} {
+ incr ll -1
+ }
+ set nfiles [llength $difffilestart]
+ while {$ll - 1 < $nfiles && [$ctext compare $tl >= \
+ [lindex $difffilestart [expr {$ll - 1}]]]} {
+ incr ll
+ }
+ return $ll
+}
+
+proc highlight_flist {ll} {
+ global ctext cflist cflist_top cflist_bot difffilestart
+
+ if {![info exists difffilestart] || [llength $difffilestart] == 0} return
+ set ll [ctext_index [$ctext index @0,1] $ll]
+ set lb $cflist_bot
+ if {$lb < $ll} {
+ set lb $ll
+ }
+ set y [expr {[winfo height $ctext] - 2}]
+ set lb [ctext_index [$ctext index @0,$y] $lb]
+ if {$ll != $cflist_top || $lb != $cflist_bot} {
+ $cflist tag remove highlight $cflist_top.0 "$cflist_bot.0 lineend"
+ for {set l $ll} {$l <= $lb} {incr l} {
+ $cflist tag add highlight $l.0 "$l.0 lineend"
+ }
+ set cflist_top $ll
+ set cflist_bot $lb
+ }
+}
+
+# Code to implement multiple views
+