#!/afs/ece/usr/tcl/bin/wisha -f
#
# Elsbeth startup file
#

# If you move this file, you should change the value of the following varialbe
# to the TH source directory. (something like ~/src/th)
set TH_Dir "[file dirname [info script]]/.."


# This file does not have the text widget, or the startup procedures; those
# are in els. Other than those, this file contains only the functions necessary
# upon startup.

set Els "$TH_Dir/bin/els"
set Elsbeth_Help_File "$TH_Dir/aux/elsbeth.help"
set Elsbeth_Bindings "$TH_Dir/aux/elsbeth.bindings.tcl"

# Only add bindings if elsbeth.bindings.tcl exists, otherwise just do els.
if {[file exists $Elsbeth_Bindings]} {

set App_Name [string toupper [string index [file tail [info script]] 0]]
append App_Name [string tolower [string range [file tail [info script]] 1 end]]


proc elsbeth_new_text {tl} {

# Create scrollbar
  scrollbar $tl.s -relief raised -command "$tl.t yview"
  $tl.t configure -yscrollcommand "$tl.s set"
  pack $tl.s -in $tl -side right -fill y

  # Create inactive entry with interpreter name in it.
  entry $tl.t_fm.interp -relief sunken -width 7
  $tl.t_fm.interp insert end [winfo name .]
  $tl.t_fm.interp view [expr [string length [winfo name .]] -7]
  $tl.t_fm.interp configure -state disabled
  foreach binding {B1-Motion A-ButtonPress-1} {
    bind $tl.t_fm.interp $binding {%W select from 0 ; %W select to end}}
  pack $tl.t_fm.interp -side left

  # Create quit button
  button $tl.t_fm.quit -text "Quit" -command "elsbeth_destroy_text $tl.t"
  pack $tl.t_fm.quit -side right -expand yes -fill x
  update
  pack propagate $tl.t_fm 0

  # Add menus
  global Elsbeth_Instance_Code TH auto_path
  regsub -all %TL $Elsbeth_Instance_Code $tl code
  eval $code

  # Bring up file/path names
  th_file_update_widgets $tl.t
  elsbeth_recall_marks $tl.t
  elsbeth_ensure_no_duplicates $tl.t
  if {![winfo exists $tl.t]} {return}

  set TH(Search,Incremental,$tl.t) 1

  if {[string match "elsbeth*" [winfo name .]]} {
    elsbeth_add_module elsbeth $tl.t
  }

  # Add an optional module if the filename warrants it.
  if {[string match "*| make*" $TH(File,$tl.t)]} {
    elsbeth_add_module errorth $tl.t
  }
  switch [file extension $TH(File,$tl.t)] {
    ".txt" {elsbeth_add_module paragrath $tl.t
  } ".tcl" - ".t" - ".tk" {elsbeth_add_module tclth $tl.t
  } ".c" - ".h" - ".cc" - ".C" - ".H" {elsbeth_add_module cth $tl.t
  } ".lisp" - ".lsp" - ".l" - ".scm" {elsbeth_add_module lispth $tl.t
  } ".html" - ".HTML" {elsbeth_add_module paragrath $tl.t
                       elsbeth_add_module htmlth $tl.t
}}}

# Ensures no other interp is editing the same file I am editing.
proc elsbeth_ensure_no_duplicates {w} {
  global TH
  # If we're looking at a pipe output, or no file, don't bother.
  if {[catch "set TH(File,$w)"] || ($TH(File,$w) == "") ||
      [string match \|* $TH(File,$w)]} {return}

  foreach tl [winfo children .] {
    set w2 "$tl.t"
    if {![winfo exists $w2]} {continue}
    # Oh...it's just me.
    if {($w2 == $w)} {continue}
    # Maybe he's looking at a different file.
    if {[catch "set TH(File,$w2)"]} {continue}
    if {($TH(File,$w) != $TH(File,$w2))} {continue}
    # Oh well...gotta resolve it.
    elsbeth_resolve_mutual_edit_conflict $w $w2
}}

proc elsbeth_resolve_mutual_edit_conflict {w w2} {
  global TH
  set msg "$TH(File,$w) is being editied in another window. What should I do?"
  set choice [tk_dialog .conf "Conflict Dialog" $msg warning \
          0 "Quit" "Read another file" "Kill other window" "Nothing"]
  switch $choice {
    0 {elsbeth_destroy_text $w
  } 1 {th_read_file_prompt $w
  } 2 {elsbeth_destroy_text $w2
}}}


# New window w is up. Add the marks to it (if any)
proc elsbeth_recall_marks {w} {
  global Elsbeth_Marks TH
  if {[string match "*| *" $TH(File,$w)]} {return}
  if {[catch "set Elsbeth_Marks($TH(File,$w))" marks]} {return}
  set l [llength $marks]
  eval $w tag configure mark $TH(Mark,Tag)
  for {set i 0} {$i < $l} {incr i 2} {
    eval $w tag add mark [lrange $marks $i [expr $i+1]]
}}

# Read the elsbeth.marks file, and load the marks
proc elsbeth_load_marks {} {
  global env
  if {[file exists $env(HOME)/.th/elsbeth.marks]} {
    uplevel #0 source $env(HOME)/.th/elsbeth.marks
  }

  foreach tl [winfo children .] {
    if {[string match ".sym*" $tl]} {
      elsbeth_recall_marks $tl.t
  }}

# Remove marks from files that don't exist.
  global Elsbeth_Marks
  foreach name [array names Elsbeth_Marks] {
    if {![file exists $name]} {unset Elsbeth_Marks($name)}}
}
# So we don't get to thinking Elsbeth_Marks isn't an array.
set Elsbeth_Marks(1) 2 ; unset Elsbeth_Marks(1)
elsbeth_load_marks

# Window w is about to go. Save its marks.
proc elsbeth_remember_marks {w} {
  global Elsbeth_Marks TH
  if {[set marks [$w tag ranges mark]] != ""} {
    set Elsbeth_Marks($TH(File,$w)) $marks
}}

# Save the elsbeth.marks file
proc elsbeth_save_marks {} {
  global env Elsbeth_Marks
  if {![file isdir $env(HOME)/.th]} {return}
  if {[catch {array names Elsbeth_Marks} files]} {set files ""}

  set f [open $env(HOME)/.th/elsbeth.marks "w"]
  puts $f "foreach item \{"
  foreach file $files {puts $f "\{$file $Elsbeth_Marks($file)\}"}
  puts $f "\} {set Elsbeth_Marks(\[lindex \$item 0\]) \[lrange \$item 1 end\]}"
  close $f
}

proc elsbeth_destroy_text {w} {
  th_confirm_save $w "catch \"elsbeth_save_marks ; els_destroy_text $w\""
}

source $Elsbeth_Bindings
}

source $Els
if {[file exists $Elsbeth_Bindings]} {
wm title . Elsbeth
wm iconname . Elsbeth

# This forces the window module to load, so we can overwrite some of its procs.
th_toplevel .

# Don't iconify w if it is in toplevel window, unless its alone.
proc th_iconify_window {w} {
  set p [winfo parent $w]
  if {([winfo class $p] == "Toplevel")} {
    wm iconify $p
  } else {
    if {([llength [pack slaves .]] == 1)} {
      wm iconify .
    } else {
      pack forget $p
  }}
  els_focus_new [winfo class [winfo parent $w]]
}

proc th_raise_window {w} {
  set p [winfo parent $w]
  if {([winfo class $p] == "Toplevel")} {
    wm deiconify $p ; raise $p
  } else {
    wm deiconify . ; raise .
    pack $p -side top -expand yes -fill x
    if {[set s [lindex [pack slaves .] 0]] != ""} {
      pack $p -before $s
}}}

set TH(Gradual) 1 ; set TH(Pipe,Enabled) 1

# Extend th_load_file to do a handful of elsbeth-type goodies.
set code [info body th_load_file]
proc th_load_file {w {insert_flag 0}} [concat $code { ;
  if {!$insert_flag} {    elsbeth_recall_marks $w  }
  elsbeth_ensure_no_duplicates $w
}]

# Extend th's write_file to change window titles, and check for duplicates.
set code [info body th_write_file]
proc th_write_file {w} [concat $code { ; 
  els_update_window_title [winfo parent $w] $TH(File,$w) ;
  elsbeth_ensure_no_duplicates $w
}]

# Extend th's confirm-save to remember the marks, whether or not file is saved.
set code [info body th_confirm_save]
proc th_confirm_save {w cmd} [concat { ;
  elsbeth_remember_marks $w
; } $code]
}


