more as eTcl plugin

Richard Suchenwirth 2006-01-23 - In my series of mini-tools for eTcl, here's an approximation to the more tool to display one file in a text window read-only. The whole file is not read at once, but on demand: initially, and each time you press <Down>, a screenful (approximately - for long lines their extent is estimated) is loaded and displayed at end. The window is shortlived - once it loses focus, it gets destroyed.

 proc more filename {
   set f [open $filename]
   catch {wce siphide} ;# hide keyboard
   set w [toplevel .[clock clicks]]
   wm title $w [file tail  $filename]
   bind $w <FocusOut> "catch {close $f}; destroy $w"
   bind $w <Up>       "$w.t yview scroll -1 page"
   bind $w <Down>     "more'page $w.t $f; $w.t yview scroll 1 page"
   text $w.t -wrap word -height 20 -width 38
   $w.t tag config blue -foreground blue
   grid $w.t -sticky news
   more'page $w.t $f
   focus -force $w
 }

#-- Page presentation

 proc more'page {t f} {
   if [eof $f] return
   set nmax 20
   set n 0
   $t config -state normal
   while {[gets $f line]>=0} {
      $t insert end $line\n
      set ln [expr {[string length $line]/38+1}]
       if {[incr n $ln]>$nmax} break
  }
   $t insert end [expr {[eof $f]? "EOF": "more..\n"}] blue
   $t config -state disabled
 }

See IDE: from cathedral to patchwork bazaar for what I do this for.


Category File