package require Tk

scrollbar .vs -command ".text yview"
text .text -yscrollcommand ".vs set" \
   -bg white -font {"luxi mono" 12 bold} \
   -undo true  -autoseparators false \
   -maxundo 50 \
   -width 42 \
   -exportselection true



grid .text .vs -sticky nesw

frame .buttons
grid .buttons -sticky ew

button .buttons.c -text "Copy" -command copy
button .buttons.p -text "Paste" -command paste
button .buttons.u -text "Undo"  -state disabled \
   -command {.text edit undo; teilen ""}

grid .buttons.c .buttons.p .buttons.u  -padx 10 -sticky e

grid columnconfigure . 0 -weight 2
grid columnconfigure . 1 -weight 0 -minsize 20
grid rowconfigure . 0 -weight 2
grid rowconfigure . 1 -weight 1 -minsize 50

bind .text <KeyPress> "teilen %K"
text tag configure rot -foreground red -underline true
text tag configure blau -foreground blue
set lastIndex 0.0
set lastTag rot
proc teilen {key} {
   puts "teilen $key"
   switch -exact $key {
       minus -
       space -
       Tab -
       Return {
           puts "sep"
           .text edit separator
           switch $::lastTag {
               rot {set ::lastTag blau}
               blau {set ::lastTag rot}
           }
           .text tag add $::lastTag $::lastIndex [.text index current]
           set ::lastIndex [.text index current]
       }
   }

   if {[.text edit modified]} {
       .buttons.u conf -state normal
   } else {
       .buttons.u conf -state disabled
   }
}
set clipboard ""
proc copy {} {
   puts copy

   if {[catch {
       set ::clipboard [.text get [lindex [ .text tag ranges sel ] 0] \
                          [lindex [ .text tag ranges sel ] 1] ]
   }]} {
       set ::clipboard ""
   }
}

proc paste {} {
   puts paste

   .text tag add $::lastTag $::lastIndex [.text index current]
   set ::lastIndex [.text index current]

   .text insert [.text index current] $::clipboard

   .text tag add $::lastTag $::lastIndex [.text index current]
   set ::lastIndex [.text index current]

}