#!/afs/ece/usr/tcl/bin/wish -f

set Bind_Keyword [file tail [info script]]
source "[file dirname [info script]]/../aux/frame.tcl"

# Help text.
set Help "" ; append Help {Parenth -- Add bindings to help keep parentheses balanced.

This program teaches text and entry widgets to indicate when parentheses,
brackets, etc. are unbalanced, and to maniplate parenthetical expressions.

} $TH_Bindings_Help {

Widgets of Parenth

The Select Checkbutton

When a matching open paren is found, that open paren can be highlighted. If this
button is on, it gets the selection. If this button is off, it does not get
selected, but for a Text widget, it receives a 'paren' tag, which may have other
highlighting features. If this button is off, open parens are not highlighted
for Entry widgets. Paren highlighting disappears after a few seconds.


Show Local Text Checkbutton

When a matching open paren is found, if it is not on the screen, highlighting
won't help (unless you happen to scroll over it in a few seconds), so the text
on the same line as the open paren up to the paren can be flashed in a label
below the widget. If this button is on, the local text is flashed; if this
button is off, the local text is not shown.


Paren Checkbuttons "() [] {} <>"

These indicate which parens should be checked when teaching a remote program.
You can change which ones are checked later in the remote program, but these
buttons allow you to specify which ones get checked on startup.


Maximum Message String Length Scale

If the text local to a matching open paren is printed during a paren check, that
string can be very long, depending on how long the line is before the matching
open paren. If the string is longer than n, which is the value in this scale,
only the last n characters before the open paren are shown.


Matching Paren Tag Configuration Entry

If the Select checkbutton is off, then open parens in a text widget get tagged
for a few seconds when their matching close paren is entered. The entry
specifies the configurations of this tag.

} $TH_Frame_Help {
}


# Gives app all the code necessary to do our functions.
proc teach_code {} {
  if {[widget_bindings] == ""} {return ""}
  global Class Paren_Select Paren_Tag Widget
  include_files [list browse.$Class.tcl "th_[set Class]_select_range"] \
	[list edit.$Class.tcl "th_[set Class]_delete_range"] \
	[list search.$Class.tcl "th_[set Class]_string_forward"] \
	[list paren.$Class.tcl "th_[set Class]_left_exp"] \
	{paren.Misc.tcl th_Misc_paren_prompt}
  teach_frame_code

  if $Paren_Select {set tag "sel"} else {set tag "paren"}
  do_cmd "set TH(Paren,Select,$Class) $tag\n" 0

  if {($Class == "Text") && !$Paren_Select} {
      do_cmd "set TH(Paren,Text,Tag) \{$Paren_Tag\}\n" 0
  }

  foreach item {Paren Bracket Brace Angle} {
    global Exp_$item
    do_cmd "set TH(Exp,$item) [set Exp_$item]\n" 0
    do_cmd "set TH(Exp,$item,$Widget) [set Exp_$item]\n" 0
  }
  foreach key {<parenright> <braceright> <bracketright> <greater>>} {
    do_cmd "bind $Class $key \{th_Misc_paren_reeval %W ; eval \[bind %W $key\]\}\n" 0
  }
  global Msg_Length Paren_Show
  do_cmd "set TH(Paren,Length) $Msg_Length\n" 0
  do_cmd "set TH(Paren,Show,$Class) $Paren_Show\n" 0
}

# For a widget, returns the appropriate bindings. (They will depend on the
# widget)
proc widget_bindings {} {
  global TH_Dir Bindings Class
  set bindings ""
  if {[file exists "$TH_Dir/lib/paren.[set Class].tcl"]} {
    set bindings $Bindings(Paren)}
  return [widget_frame_bindings $bindings]
}


create_form_entry .pt "Matching Paren Tag Configuration" Paren_Tag \
             "-background red"
create_form_scale .parenlen "Maximum Message String Length" Msg_Length 30 \
             -from 5 -to 50
create_form_checkbutton .parenframe "Select Found Paren" Paren_Select 0 left
create_form_checkbutton .parenshow "Show Local Text" Paren_Show 1 left
frame .pexp ; pack .pexp -fill x -expand no -side right
create_form_checkbutton .pexp.paren "()" Exp_Paren 1 left
create_form_checkbutton .pexp.bracket "\[\]" Exp_Bracket 1 left
create_form_checkbutton .pexp.brace "\{\}" Exp_Brace 1 left
create_form_checkbutton .pexp.angle "<>" Exp_Angle 0 left


