Updated 2014-01-18 10:40:22 by dkf

George Peter Staplin: I wrote this simple editor as an experiment. It saves automatically, and has a unique feature. The unique feature is that all lines indented with spaces receive coloring that reflects their depth. (The editor seems usable enough. I am using it to construct this wiki page.)

License: OLL


 proc highlight {t laststart lastend} {
  foreach {pat col} $::patcol {
   $t tag remove $col $laststart $lastend
  }
  set start [$t index @0,0]
  set end [$t index "[$t index @0,[winfo height $t]] lineend"]
  
  foreach {pat col} $::patcol {
   set s $start
   while {"" ne [set found [$t search -count length -regexp $pat $s $end]]} {
    $t tag add $col $found [set s [$t index "$found + $length chars"]]
    $t tag configure $col -background $col
   }
  }
  after 500 [list highlight $t $start $end]
 }
 
 proc get.line s {
  string range $s 0 [expr {[string first . $s] - 1}]
 }
 
 proc update.lines w {
  set start [$w.text index @0,0]
  set height [winfo height $w.text]
 
  $w.lines delete 1.0 end
  array set metrics [font metrics [$w.lines cget -font]]
 
  set incr_by [expr {$metrics(-linespace) / 2}]
  set last_idx end
  set last_line end
 
  #When $last_idx ne $idx we have a wrap or a new line
  for {set y 0} {$y < $height} {incr y $incr_by} {
   set idx [$w.text index @0,$y]
 
   if {$idx ne $last_idx} {
    if {$last_line ne [set l [get.line $idx]]} {
     $w.lines insert end $l\n
     set last_line $l
    } else {
     $w.lines insert end \n
    }
   }
   set last_idx $idx
  }
 
  $w.lines config -width [expr {[string length $last_line] + 1}]
 }
 
 proc create.gui w {
  scrollbar $w.yscroll -command "[list update.lines $w] ; [list $w.text yview]"
  text $w.lines -width 3 -takefocus 0
  text $w.text -yscrollcommand "[list update.lines $w] ; [list $w.yscroll set]" -wrap word
  
  grid $w.yscroll -row 0 -column 0 -sticky ns
  grid $w.lines -row 0 -column 1 -sticky ns
  grid $w.text -row 0 -column 2 -sticky news
 
  bindtags $w.lines {}
 }
 
 proc save {t name} {
  if {![$t edit modified]} {
   after 1000 [list save $t $name]  
   return 
  }
 
  puts -nonewline [set fd [open $name w]] [$t get 1.0 end-1c]
  close $fd
 
  $t edit modified 0
  after 1000 [list save $t $name]
 }
 
 proc syntax {} {
  puts stderr "syntax is: [info script] filename"
  exit 1
 }
 
 proc main {argc argv} {
  if {1 != $argc} {
   syntax
  }
 
  create.gui ""
  grid rowconfigure . 0 -weight 100
  grid columnconfigure . 2 -weight 100
 
  .text insert end [read [set fd [open [lindex $argv 0] r]]]; close $fd
 
  for {set i 1} {$i < 12} {incr i} {
   set c [format %2.2x [expr {$i * 21}]]
   lappend ::patcol ^[string repeat " " $i] #[set c][set c]aa
  }
  highlight .text 1.0 end
  update.lines ""
  save .text [lindex $argv 0]
 }
 main $::argc $::argv

Comments edit