#!/afs/ece/usr/tcl/bin/wish -f
# The next line is executed by most shells, but not Tcl \
wish $0 $*


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

# Help text.
set Help "" ; append Help {Cth -- Add keybindings for standard C function/comment management.

This program teaches text widgets about C functions and comments. It is
assumed that the widget will contain C or C++ code.

} $TH_Bindings_Help {

Widgets of Cth
} $TH_Frame_Help {

The heuristic for finding a function's boundaries are: A function begins with
a type (such as int or char*). (Previous line ends with a close brace or
whitespace or is empty) A function contains some stuff in parentheses and
follows with an open brace. The function ends with said open brace's matching
close brace. If a function does not follow this format, the bounding
procedures will screw up on it.

The width used for comment formatting is the width of the text widget, which
may not necessarily be the actual window size.

This module will not work well if the parenth module is not already running on
the widget.}


# Gives app all the code necessary to do our functions.
proc teach_code {} {
  global Widget Class App
  if {$Class != "Text"} {return ""}
  include_files {c.tcl th_fn_begin} \
	{modes.tcl th_Text_tag_regions} \
	{edit.Text.tcl th_Text_indent} \
	{edit.Misc.tcl th_indent} \
	{browse.Text.tcl th_Text_select_range}

  if {[catch "send $App lsearch \\\$TH(Paren,Left,$Widget) /*" result] || \
	($result == -1)} {
    do_cmd "th_Misc_paren_add $Widget {/*} {*/} 0\n" 0}
  if {[catch "send $App lsearch \\\$TH(Paren,Left,$Widget) #if" result] || \
	($result == -1)} {
    do_cmd "th_Misc_paren_add $Widget {#if} {#endif}\n" 0}

  do_cmd_set TH(Paint,Hook,$Widget) "th_Text_tag_regions $Widget FUNCTION th_fn_begin th_fn_end th_fn_next \$start \$end ; th_Text_tag_regions $Widget COMMENT th_ccomment_begin th_ccomment_end th_ccomment_next \$start \$end"
  do_cmd "$Widget tag configure FUNCTION -background MidnightBlue\n" 0
  do_cmd "th_Misc_paint_add $Widget TYPE [list {(^|[\{\(\,;])[ 	]*[A-Za-z0-9_]+\** +(\**|&)[A-Za-z]}] [list {[A-Za-z0-9_]+}] -foreground green\n" 0
  do_cmd "th_Misc_paint_add $Widget TYPE [list {\([A-Za-z0-9_]+ *\*?\) *[A-Za-z(]}] [list {[0-9A-Za-z_]+}] -foreground green\n" 0
  do_cmd "th_Misc_paint_add $Widget TYPE [list {(^|[^A-Za-z0-9_])[A-Za-z0-9_]+::}] [list {[0-9A-Za-z_]+}] -foreground green\n" 0
  do_cmd "th_Misc_paint_add $Widget KEYWORD [list {(^|[^A-Za-z0-9])(return|case|default|switch|if|while|for|do|else|goto|new|delete)($|[^A-Za-z0-9])}] [list {[A-Za-z0-9_]+}] -foreground red\n" 0
  do_cmd "th_Misc_paint_add $Widget STRING [list {"[^"]*"}] {} -foreground yellow\n" 0
  do_cmd "th_Misc_paint_add $Widget STRING [list {'(.|\\.|\\[0-9]*)'}] {} -foreground yellow\n" 0
  do_cmd "th_Misc_paint_add $Widget MACRO [list {#.*}] {} -foreground cyan\n" 0
  do_cmd "th_Misc_paint_add $Widget COMMENT [list {//.*}] {} -foreground wheat\n" 0
  do_cmd "$Widget tag raise sel\n" 0
}

# For a widget, returns the appropriate bindings. (They will depend on the
# widget)
proc widget_bindings {} {
  global Bindings Class
  if {$Class != "Text"} {return ""}
  return [widget_frame_bindings $Bindings(C)]
}
