#!/usr/local/bin/wish8.0 -f
global tkisp_ed
set tkisp_ed(lib) /u/ec/raines/prog/tk/tkispell
set tkisp_ed(bindlib) /u/ec/raines/prog/tk/tkBind
set tkisp_ed(version) 4.0

###########################################################################
#
#   TkIspell -- A Tk/Tcl interface to Ispell
#	    		by Paul Raines (raines@bohr.physics.upenn.edu)
#
#   Note: this file is only a simple editor to demonstrate use of ispell.tk
#
#
# INSTALLATION:
#    TkIspell can be installed through the Makefile. Otherwise, do the
#    following:
#
#    (1) edit the first line above to reflect the location of wish.
#    (2) edit the third line above to reflect the location of utils.tk,
#	ispell.tk, bindings.tk and tclIndex.
#    (3) edit the ispell.tk file and follow the Installation 
#	instructions there.
#
#    This files contains a simple editor that calls the tkispell library.
#    It is only intended to demostrate the use of that library and is
#    not meant to be a general purpose editor. This program gives you
#    a basic Tk text widget with a row of buttons on the bottom to
#    read and write files and call the tkispell checker.
#
# CHANGES:
# v1.6	fix bug with topgetopt being left out of tclIndex
#       use new and improved bindings.tk
#
# v1.6p1 fix bug with leaving in bind_textscrollset
#
# v1.6p2 Added personalization so users can pass ispell options
#
# v1.6p3 updated to tkmail-1.6p5 versions of libraries
#
# v2.0beta1  updated to new pack command
#
# v2.0  updated to latest ispell.tk and utils.tk libraries
#		from TkMail4.0beta9

# globals
set tkisp_ed(file) ""
set tkisp_ed(top) .tkisp_ed
set tkisp_ed(modified) 0

# PERSONALIZABLES
# full path to main dictionary
  set tkisp_ed(set,dict) default
# 1 if to assume TeX mode, 0 for normal mode
  set tkisp_ed(set,tex) 0
# 1 to start at top of file, 0 to start at cursor position
  set tkisp_ed(set,start) 1
# full path to personal dictionary to use
  set tkisp_ed(set,pdict) default
# whether to correct all occurance of mispelling first time seen
  set tkisp_ed(set,crtall) 0
# addition options to pass to ispell (i.e. "-C -S")
  set tkisp_ed(set,addopts) ""

# Source user settings
if {[file exists $env(HOME)/.tkispell]} {
    source $env(HOME)/.tkispell
}

proc quitapp { } {
  global tkisp_ed

  if $tkisp_ed(modified) {
    if [ut:getok -master $tkisp_ed(top) -oklabel Yes -nolabel No -prompt \
        "Save changes to [file tail $tkisp_ed(file)]?"] {
      savefile $tkisp_ed(file) $tkisp_ed(top).tframe.txt $tkisp_ed(top).tframe.msg
    }
  }

  exit
}

proc readfile { filename tw msg} {
  global tkisp_ed

  set tkisp_ed(file) $filename
  if {![file exists $filename]} then {
    $msg configure -text "File does not exist"
    set tkisp_ed(file) ""
    return 0
  } else {
    $msg configure -text "File: $filename"
    $tw delete 1.0 end
    $tw insert end  [exec cat $filename]
    $tw insert end "\n"
    $tw mark set insert 1.0
    set tkisp_ed(modified) 0
    return 1
  }
}

proc savefile {filename tw msg} {
    global tkisp_ed

    if {![llength $filename]} {return 0}

    if {[file exists $filename] && \
	    ![string match $tkisp_ed(file) $filename]} {
	if {![tkgetokay "Replace existing file?" $tkisp_ed(top)]} {return 0}
    }

    exec cat > $filename << [$tw get 0.0 end]
    set tkisp_ed(file) $filename
    $msg configure -text "Saved file $filename"
    return 1
}

if {[info exists auto_path]} {
    lappend auto_path $tkisp_ed(lib)
} else {
    set auto_path $tkisp_ed(lib)
}

source $tkisp_ed(lib)/utils.tk
source $tkisp_ed(lib)/ispell.tk

# if the enhanced text.tcl is not the default and exists, source it
if {[info proc tkBindDefVar]==""} { 
  if {[file exists $tkisp_ed(bindlib)/bindxtnd.tcl]} {
    foreach key [bind Text] { bind Text $key {} }
    foreach key [bind Entry] { bind Entry $key {} }
    source $tkisp_ed(bindlib)/bindxtnd.tcl
    source $tkisp_ed(bindlib)/text.tcl 
    source $tkisp_ed(bindlib)/entry.tcl
    bind TextCX <Control-c> { exit }
  }
}

wm withdraw .
toplevel $tkisp_ed(top) -class TkIspell
wm title $tkisp_ed(top) "TkIspell v$tkisp_ed(version)"

frame $tkisp_ed(top).tframe
scrollbar $tkisp_ed(top).tframe.tscr \
    -command "$tkisp_ed(top).tframe.txt yview"
text $tkisp_ed(top).tframe.txt \
	-yscroll "$tkisp_ed(top).tframe.tscr set" \
	-setgrid true -width 80 -height 24
label $tkisp_ed(top).tframe.msg -text "No file" -relief raised

frame $tkisp_ed(top).bb
button $tkisp_ed(top).bb.ispell -text Ispell \
    -command {
      global tkisp_ed
      set opts [list $tkisp_ed(set,dict) $tkisp_ed(set,tex) \
		    $tkisp_ed(set,start) $tkisp_ed(set,pdict) \
		    $tkisp_ed(set,crtall) $tkisp_ed(set,addopts)]
      tkispell_text $tkisp_ed(top).tframe.txt $opts
    }
button $tkisp_ed(top).bb.file -text Open \
    -command {readfile [ut:fsbox -prompt {Read File:} \
			    -grab 1 -master $tkisp_ed(top)] \
		  $tkisp_ed(top).tframe.txt $tkisp_ed(top).tframe.msg}
button $tkisp_ed(top).bb.save -text Save \
    -command {savefile $tkisp_ed(file) $tkisp_ed(top).tframe.txt $tkisp_ed(top).tframe.msg}
button $tkisp_ed(top).bb.saveas -text "Save As" \
    -command {savefile [ut:fsbox -prompt {Save File:} \
			    -grab 1  -master $tkisp_ed(top)] \
		  $tkisp_ed(top).tframe.txt $tkisp_ed(top).tframe.msg}
button $tkisp_ed(top).bb.quit -text Quit -command quitapp

pack $tkisp_ed(top).bb.ispell $tkisp_ed(top).bb.file \
    $tkisp_ed(top).bb.save $tkisp_ed(top).bb.saveas \
    $tkisp_ed(top).bb.quit -side left -expand true -fill both

pack $tkisp_ed(top).tframe.msg -side top -fill x
pack $tkisp_ed(top).tframe.tscr -side left -fill y
pack $tkisp_ed(top).tframe.txt -side left -expand true -fill both

pack $tkisp_ed(top).tframe -side top -expand true -fill both
pack $tkisp_ed(top).bb -side top -fill x

set file [lindex $argv 0]
if {[llength $file]} {
    readfile $file $tkisp_ed(top).tframe.txt $tkisp_ed(top).tframe.msg
}

# Doesn't work with Itcl
rename $tkisp_ed(top).tframe.txt $tkisp_ed(top).tframe.txt_text

proc $tkisp_ed(top).tframe.txt {cmd args} {
  global tkisp_ed
  if {$cmd == "insert" || $cmd == "delete"} {
    set tkisp_ed(modified) 1
  }
  uplevel #0 "$tkisp_ed(top).tframe.txt_text $cmd $args"
}

$tkisp_ed(top).tframe.tscr configure -command "$tkisp_ed(top).tframe.txt_text yview"

