# Copyright (C) 1999 - Martin Strauss - under terms of GPL
##############################################################
proc spell {E mode spell_start spell_end} {
 global T spell_not spell_array spell_yes spell_flag
 if $spell_flag {
  set spell_flag 0 
  switch $mode add {
   if {[lsearch -exact [$E tag names insert] spell] != -1} {
    set ksp1 [$E search -backwards -regexp -- $spell_yes insert {insert linestart}]
    if {$ksp1 == ""} {set ksp1 "insert linestart"} else {append ksp1 +1c}
    set ksp2 [$E search -forwards -regexp -- $spell_yes insert {insert lineend}]
    if {$ksp2 == ""} {set ksp2 "insert lineend"}
    set I [$E get $ksp1 $ksp2]
    switch [tk_messageBox -parent $E -type okcancel -icon question -message $T(spell-1)$I$T(spell-2)] ok {
     if ![info exists spell_array($I)] {
      set spell_array($I) O
      set I_s "spell + [string length $I] chars"
      set I_s1 "spell + [expr [string length $I] +1 ] chars"
      set nowindex [$E search -- $I $spell_start $spell_end]
      while {$nowindex != ""} {
       $E mark set spell $nowindex
       set ksp1 [$E get spell-1c spell]
       set ksp2 [$E get $I_s $I_s1]
       if {[regexp -- $spell_yes $ksp1] != 0} {
        if {[regexp -- $spell_yes $ksp2] != 0} {
         $E tag remove spell spell $I_s
        }
       }
       set nowindex [$E search -- $I spell+1c $spell_end]
      }
     }
    } cancel {
     switch [tk_messageBox -parent $E -type okcancel -icon question -message $T(spell-3)] ok {
      $E mark set insert "insert wordstart"
      InitSearch
      .wsearch.text1 delete 0 end
      .wsearch.text1 insert 0 $I
      .wsearch.text2 delete 0 end
      .wsearch.text2 insert 0 $I
     }
    } 
   }
  } show {
  $E tag remove spell $spell_start $spell_end
  set last ""
  set LISTE [lsort [split [$E get $spell_start $spell_end] $spell_yes]]
  foreach I $LISTE {
   if {[string compare $last $I]!=0} {
    if {[regexp -- $spell_not $I] == 0} {
     if {![info exists spell_array($I)]} {
      if {$I != ""} {
       set I_s "spell + [string length $I] chars"
       set I_s1 "spell + [expr [string length $I] +1 ] chars"
       set nowindex [$E search -- $I $spell_start $spell_end]
       while {$nowindex != ""} {
        $E mark set spell $nowindex
        set ksp1 [$E get spell-1c spell]
        set ksp2 [$E get $I_s $I_s1]
        if {[regexp -- $spell_yes $ksp1] != 0} {
         if {[regexp -- $spell_yes $ksp2] != 0} {
          $E tag add spell spell $I_s
         }
        }
        set nowindex [$E search -- $I spell+1c $spell_end]
       }
   }}}}
   set last $I
  }
 }
 set spell_flag 1
}}
proc spell_load {} {
 global spellfile spellfileown language I_befehl spell_array
 if [file exists $spellfileown$language] then {
  set f [open $spellfileown$language r]
  set LISTE [gets $f]
  close $f
  foreach I $LISTE {set spell_array($I) O}
 }
 if [file exists $spellfile$language] then {
  set f [open $spellfile$language r]
  set LISTE [gets $f]
  close $f
  foreach I $LISTE {set spell_array($I) G}
 }
}
proc spell_save {} {
 global spellfileown language spell_array
 set LISTE ""
 set L [array get spell_array]
 foreach {I V} $L {if {$V == "O"} {lappend LISTE $I}}
 set f [open $spellfileown$language w]
 puts $f [lsort $LISTE]
 close $f
}
