#! /bin/sh
#This is script for management of dictionary. It works with bash script "mova".
#ver. 4.0 (C) E.S.Cymbalyuk 1999. Under GNU GPL. No Warranty.
#The latest versions see on www.chat.ru/~mueller_dic or www.geocities.org/mueller_dic\
exec wish "$0" "$@"

#Default Directory for "share/mova/.movarc", if absent "~/.movarc"
set DIR /usr/local/
set OPT -W
set DIARY .mova

switch $argc 0 {} 1 {set OPT_ARG [lindex $argv 0]} 2 {
set OPT_ARG [lindex $argv 0]; set DIC_ARG [lindex $argv 1]} default {
puts "You used excrescent parameters with movaTK."}

catch {set OPT $OPT_ARG}
#catch {encoding system identity}
#catch {encoding system iso8859-1}
catch {encoding system koi8-r}

proc GetOptions {} {
global DIR DIC DIR_TMP DIC_ARG FONT_FIND FONT_TEXT FONT_D FONT_DG FONT_IPA
if {([catch {set rcfile [open "~/.movarc_${DIC_ARG}" "r"]} Error] &&
[catch {set rcfile [open "${DIR}share/mova/.movarc_${DIC_ARG}" "r"]}  Error] &&
[catch {set rcfile [open "~/.movarc" "r"]} Error] &&
[catch {set rcfile [open "${DIR}share/mova/.movarc" "r"]} Error] ) != 0} {
set FONT_FIND -*-*-bold-r-*-*-17-*-*-*-*-*-koi8-r
set FONT_TEXT -*-*-medium-r-*-*-17-*-*-*-*-*-koi8-r
set FONT_D -*-*-medium-o-*-*-17-*-*-*-*-*-koi8-r
set FONT_DG -*-*-bold-o-*-*-17-*-*-*-*-*-koi8-r
set FONT_IPA -*-silsophiaipa-*-r-*-17-*-*-*-*-*-*-*
set DIR /usr/local/
set DIC Mueller7GPL.koi
set DIR_TMP /tmp/
} else {
gets $rcfile FONT_FIND
gets $rcfile FONT_TEXT
gets $rcfile FONT_D
gets $rcfile FONT_DG
gets $rcfile FONT_IPA
gets $rcfile DIR
gets $rcfile DIR_TMP
gets $rcfile DIC
close $rcfile}
catch {set DIC $DIC_ARG}
wm title . "$DIC     MOVA-4.0 (C) E.S.Cymbalyuk 1999; GPL"
wm focusmodel . passive
tk appname tk$DIC}

proc WordLight {} {
global word
if {$word==""} {return}
set a  1.0
set b  1.0
set c  1.0
set d  1.0
set e  1.0
set f  1.0
set g  1.0
set w  1.0
while {[set a [.text search -count len -regexp {[a-zA-Z]+} $a end]]!=""} {
.text tag add english_word $a "$a + $len char"
set a [.text index "$a +$len char"]}
while {[set g [.text search -count len -regexp {^.+  } $g end]]!=""} {
.text tag add entry_word $g "$g + $len char"
set g [.text index "$g +$len char"]}
while {[set b [.text search -count len -nocase -- $word $b end]]!=""} {
.text tag add searched_word $b "$b + $len char"
set b [.text index "$b +$len char"]}
while {[set d [.text search -count len -regexp {\_[^ ]+[\.:]} $d end]]!=""} {
.text tag add duty_word $d "$d + $len char"
set d [.text index "$d +$len char"]}
while {[set e [.text search -count len -regexp {\_[a-zA-Z-]+[\.:]} $e end]]!=""} {
.text tag add duty_grammar $e "$e + $len char"
set e [.text index "$e +$len char"]}
while {[set f [.text search {_} $f end]]!=""} {
.text tag add underscore $f "$f +1 char"
set f [.text index "$f +1 char"]}
while {[set w [.word search -count len -nocase -- $word $w end]]!=""} {
.word tag add searched_word $w "$w + $len char"
set w [.word index "$w +$len char"]}
while {[set c [.text search -count len -regexp {\[[^\ ]+\]} $c end]]!=""} {
.text tag add phonetic $c "$c + $len char"
set c [.text index "$c +$len char"]}
focus .scroll.scroll}

proc Fresh {} {
global u word FONT_FIND FONT_TEXT FONT_D FONT_DG FONT_IPA
destroy .text
destroy .scroll
set u 1.0
text .text -background white -state normal -width 50 -height 15 -font $FONT_TEXT -yscrollcommand ".scroll.scroll set"
frame .scroll
button .scroll.bottom -relief raised -text "" -command {
if {[catch {set wo [selection get]} error] != 0} {bell -displayof .
} else {set word $wo}
.text tag delete find_word
if { [set u [.text search -count len -nocase -- $word $u ]]!="" } {
.text tag add find_word $u "$u + $len char"
set u [.text index "$u +$len char"]
.text tag configure find_word -background yellow
.text see $u}}
button .scroll.top -relief raised -text "" -command {
if {[catch {set wo [selection get]} error] != 0} { bell -displayof .
} else {set word $wo}
.text tag delete find_word
if {[set u [.text search -backwards -count len -nocase -- $word $u ]]!=""} {
.text tag add find_word $u "$u + $len char"
.text tag configure find_word -background yellow
.text see $u}}
scrollbar .scroll.scroll -command ".text yview"
pack .text -side left -fill x -expand yes
pack .scroll -side left -fill y -expand no
pack .scroll.top -side top -fill y -expand no
pack .scroll.scroll -side top -fill both -expand yes
pack .scroll.bottom -side top -fill y -expand no
.text tag configure english_word -foreground blue
.text tag configure entry_word -font $FONT_FIND -foreground blue
.text tag configure duty_word -font $FONT_D -foreground darkgreen
.text tag configure duty_grammar -font $FONT_DG
.text tag configure searched_word -foreground brown
.text tag configure phonetic -font $FONT_IPA -foreground red
.text tag configure underscore -foreground white}

proc Call_hash_dic {arg hash_word} {
global word index_length index_dic DIR DIC DIR_TMP
set in [open "${DIR}share/dict/$DIC" "r"]
set out [open "$DIR_TMP${DIC}.tmp" "w"]
seek $in $index_dic($hash_word) start
fcopy $in $out -size $index_length($hash_word)
close $in
close $out
if {[catch {set fd [open "|${DIR}bin/mova $arg $word -d $DIR_TMP${DIC}.tmp" "r"]} error] != 0} {
puts "Can't run ${DIR}bin/mova"
.text insert 0.0 "Can't run ${DIR}bin/mova\n"
} else {
.word insert 0.0 "$word\n\n"
.word tag delete searched_word
.word tag configure searched_word -foreground brown
.text insert end "[read $fd]"
close $fd}
catch {file delete "$DIR_TMP${DIC}.tmp"}}

proc Call_dic {arg} {
global word searched_word DIR DIC
if {[catch {set fd [open "|${DIR}bin/mova $arg $word -d ${DIR}share/dict/$DIC" "r"]} error] != 0} {
puts "Can't run ${DIR}bin/mova"
.text insert 0.0 "Can't run ${DIR}bin/mova\n"
} else {.word insert 0.0 "$word\n\n"
.word tag delete searched_word
.word tag configure searched_word -foreground brown
.text insert end "[read $fd]"
close $fd}}

proc Mova {arg} {
global word argv0 index_dic
if {[catch {set word [selection get]} error] != 0} {set word " "
bell -displayof .
puts "Let us mark a word by the mouse and then run:\n$argv0 OPT DICTIONARY\nWhere OPT is \"-W\" or \"-B\" or \"-S\" or \"-T\". DICTIONARY - name of dictionary file (if the one was't set, you will use default dictionary from \"~/.movarc\"). Let us run \"mova\" for explanation of OPT.\n"}
set word [string tolower [string trimleft $word "       \"\(\{"]]
set hash_word [string index $word 0][string index $word 1]
Fresh
if {([catch {set offset_dic $index_dic($hash_word)} error] != 0) || [string match $arg "-S"] || [string match $arg "-T"]} {Call_dic $arg}
if {([catch {set offset_dic $index_dic($hash_word)} error] == 0) && ([string match $arg "-W"] || [string match $arg "-B"])} {
Call_hash_dic $arg $hash_word}}

proc Read_hash {} {
global index_length index_dic DIR DIC
if {[catch {set hash_file [open "${DIR}share/dict/${DIC}.hash" "r"]} error] == 0} {
set old_offset 0
while {[eof $hash_file]==0} {
gets $hash_file line
set hash_dic [string index $line 0][string index $line 1]
set offset [string trimleft $line $hash_dic]
set index_dic($hash_dic) $old_offset
set index_length($hash_dic) [expr $offset - $old_offset]
set old_offset $offset}
close $hash_file}}

proc Change_dic {} {
global index_length index_dic FONT_FIND
GetOptions
.entry configure -font $FONT_FIND
.word configure -font $FONT_FIND
catch {unset index_dic}
catch {unset index_length}
Read_hash}

proc PutDiary {} {
global word DIARY
if {[catch {set fd [open "~/$DIARY" "r"]} Error] != 0} {
.text insert end "no ~/$DIARY"
} else {
.text delete 0.0 end
.text insert end "[read $fd]"
set word " "
WordLight
.button_upper.save configure -text "Rewrite \"$DIARY\""
.button_upper.add configure -text "Add to \"$DIARY\""}}


GetOptions
if {[catch {set fd [open "|/bin/ls -1 ${DIR}share/dict/" "r"]} error] != 0} {
puts "Can't run /bin/ls"
} else {
set Files "[read $fd]"
close $fd
foreach files $Files {if {[ string match *.hash $files ] == 0} {lappend Dictionaries $files}}}
set Diaries {.mova}
if {[catch {set fd [open "|/bin/ls -a -1 " "r"]} error] != 0} {
puts "Can't run /bin/ls"
} else {
set Files "[read $fd]"
close $fd
foreach files $Files {if {[ string match .mova_* $files ] != 0} {lappend Diaries $files}}}

entry .entry -background #fafa33 -state normal -width 52 -textvariable word -font $FONT_FIND
bind .entry <Return> ".entry selection range 0 end; Mova {-W}; WordLight"
bind .entry <Shift-Return> ".entry selection range 0 end; Mova {-B}; WordLight"
bind .entry <Control-Return> ".entry selection range 0 end; Mova {-S}; WordLight"
bind .entry <Alt-Return> ".entry selection range 0 end; Mova {-T}; WordLight"
text .word -state normal -width 15 -height 15 -wrap word -font $FONT_FIND

frame .button_upper
set m .button_upper.dic.m
menubutton .button_upper.dic -relief flat -background #22fafa -text Dictionary -underline 0 -menu $m
menu $m
foreach dic $Dictionaries {$m add radiobutton -indicator 0 -label "$dic" -variable DIC_ARG -value $dic -command {Change_dic}}

button .button_upper.help -relief flat -background #22fafa -text Help -command {Fresh
if {[catch {set fd [open "${DIR}share/mova/Usage_koi.txt" "r"]} Error] != 0} {
.text insert end "no ${DIR}share/mova/Usage_koi.txt"
} else {
.text insert end "[read $fd]"
set word " "}}

button .button_upper.new -relief flat -background #22fafa -text "New Diary" -command {
if { [catch {set DIARY [selection get]} error] != 0} { set word " "
bell -displayof .
.text insert 0.0 "Let us mark a name of diary by the mouse and then press this button.\n\n"
} else {
set DIARY .mova_$DIARY
.button_upper.save configure -text "Rewrite \"$DIARY\""
.button_upper.add configure -text "Add to \"$DIARY\""}}

button .button_upper.save -relief flat -background #22fafa -text "Rewrite \"$DIARY\"" -command {
set fd [open "~/$DIARY" "w"]
puts $fd [.text get 0.0 end]
close $fd
set a 0
foreach fil $Diaries {if {[string match $DIARY $fil] != 0} {set a 1}}
if {$a == 0} {
lappend Diaries $DIARY
.button_upper.read.r add radiobutton -indicator 0 -label "$DIARY" -variable DIARY -value $DIARY -command {Fresh; PutDiary}}}

button .button_upper.add -relief flat -background #22fafa -text "Add to \"$DIARY\"" -command {
set fd [open "~/$DIARY" "a"]
puts $fd [.text get 0.0 end]
close $fd
set a 0
foreach fil $Diaries {if {[string match $DIARY $fil] != 0} {set a 1}}
if {$a == 0} {
lappend Diaries $DIARY
.button_upper.read.r add radiobutton -indicator 0 -label "$DIARY" -variable DIARY -value $DIARY -command {Fresh; PutDiary}}}

set r .button_upper.read.r
menubutton .button_upper.read -relief flat -background #22fafa -text "Read Diary" -underline 0 -menu $r
menu $r
foreach dia $Diaries {$r add radiobutton -indicator 0 -label "$dia" -variable DIARY -value $dia -command {Fresh; PutDiary}}

frame .button_bottom
button .button_bottom.mova -text "      ^Word      " -command {Mova {-W}; WordLight}
button .button_bottom.mova_part -text "     ^Symbols    " -command {Mova {-B}; WordLight}
button .button_bottom.mova_in_string -text " Tandem of Words " -command {Mova {-S}; WordLight}
button .button_bottom.mova_part_in_string -text "Tandem of Symbols" -command {Mova {-T}; WordLight}

pack .button_bottom -side bottom -fill x
pack .button_upper -side top -fill x
pack .entry -side top -fill x
pack .button_upper.help .button_upper.dic .button_upper.new .button_upper.add .button_upper.save .button_upper.read -side left -expand yes -fill both
pack .button_bottom.mova .button_bottom.mova_part .button_bottom.mova_in_string .button_bottom.mova_part_in_string -side left -expand yes -fill both
pack .word -side left -expand yes -fill both

Read_hash
Mova $OPT
WordLight
bind all <Escape> {destroy .}
#End
