#!/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.th_frame.interp -relief sunken -width 7
  $tl.th_frame.interp insert end [winfo name .]
  $tl.th_frame.interp view [expr [string length [winfo name .]] -7]
  $tl.th_frame.interp configure -state disabled
  foreach binding {B1-Motion A-ButtonPress-1} {
    bind $tl.th_frame.interp $binding {%W select from 0 ; %W select to end}}
  pack $tl.th_frame.interp -side left

  # Create quit button
  button $tl.th_frame.quit -text "Quit" -command "elsbeth_destroy_text $tl.t"
  pack $tl.th_frame.quit -side right -expand yes -fill x
  update
  pack propagate $tl.th_frame 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_ensure_no_duplicates $tl.t
  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 {[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
}}}

# 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]} {

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

# Use els's load file command instead of th's, els's has gradual reading.
proc th_load_file {w {insert_flag 0}} {
  els_load_file $w $insert_flag
  elsbeth_ensure_no_duplicates $w
}

# Extend els's load file routine to remember the marks that were used.
set code [info body els_load_file]
proc els_load_file {w {insert_flag 0}} [concat $code { ;
  if $insert_flag {return}
  elsbeth_recall_marks $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 toplevel $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]

elsbeth_load_marks
}


