#!/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 {Painteth -- Color various expressions in text widgets

This program teaches text widgets to paint certain expressions, like quoted
strings, or C comments, or special words.

} $TH_Bindings_Help {

Widgets of Painteth

Regexpressions Menu

This is a list of expressions that will get taught to a remote text widget
whenever the user requests painting. Only expressions that have their
checkbutton turned on get taught.


'Add to Menu' Button

This is a feature that allows you to add new expressions to the Regexpressions
menu. A regexpression consists of a name (such as STRING, COMMENT, etc.), and a
regular expression that contains the pattern to be painted. There is often a
subpattern in the pattern that should be painted. (For example, a Tcl variable
could be found by looking for a dollar sign ($) and a word, but only the word
should get painted). A special case occurs for words like 'set' that should be
painted, but only if they are not part of a larger word (like 'unset').
Finally, some configurations for painting should be given, (like 'foreground
blue').


Expression Name Entry

The name of an expression to be painted. This is the tag name, and it gets
shown at the Index menu of the text widget if you are using taggeth.


Pattern Entry

This is any regular expression which contains the item being painted. A widget
will search for all occurrences of this pattern for painting.


Subpattern Entry

This is a subset of the Pattern Entry, and it indicates which subset of the
pattern to paint. If left blank, the entire pattern is painted.


'Word' Checkbutton

If set, indicates that Pattern should be painted only if it is an autonomous
word (like 'set') and not a substring in a larger word (like 'set' in 'unset').


Configuration Entry

This indicates how text painted should appear. Any options permissible to Tk's
tag configure command may be used here.


Test Tag Entry

This is for testing new regular expressions. Painth has a very rudimentary
facility for extending the set of regular expressions that get painted. When
testing regular expressions, a TEST tag is used to highlight examples of the
regular expression in the text. This entry specifies the TEST tag
configurations.

} $TH_Frame_Help {

The regular expressions are searched using Tk's text search command, which only 
works on regular expressions within a single line of text.}


# Gives app all the code necessary to do our functions.
proc teach_code {} {
  global Class TH_Dir App Widget Paint_Test_Tag
  if {![file exists "$TH_Dir/lib/paint.[set Class].tcl"]} {return ""}

  include_files [list paint.$Class.tcl "th_[set Class]_paint_region"] \
	[list busy.tcl "th_busy"]
  teach_frame_code

  foreach regexpression [form_menu_selected Regexpressions] {
    do_cmd "th_Misc_paint_add $Widget $regexpression\n" 0
  }
  do_cmd_set TH(Paint,Test,Tag) $Paint_Test_Tag
}

# 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/paint.[set Class].tcl"]} {
    set bindings $Bindings(Paint)} else {return ""}
  return [widget_frame_bindings $bindings]
}

proc paint_regexp {} {
  global Paint_Word Paint_Pattern Paint_Subpattern
  if $Paint_Word {
    return [list "\(^|\[^A-Za-z0-9_\]\)$Paint_Pattern\(\$|\[^A-Za-z0-9_\]\)" [list $Paint_Pattern]]
  } else {return [list $Paint_Pattern $Paint_Subpattern]}
}

create_form_menu Regexpressions
for {set i 0} {$i <= 2} {incr i} {set Menu(regexpressions,$i) 0}

pack [frame .paint] -side top -fill x
create_form_entry .paint.tag "Expression Name" Paint_Tag {STRING}
create_form_entry .paint.pattern "Pattern" Paint_Pattern {"[^"]*"}
pack [frame .paint.subexp] -side top -fill x
create_form_checkbutton .paint.subexp.word "Word" Paint_Word 0 right
create_form_entry .paint.subexp.pattern "Subpattern" Paint_Subpattern {}
pack configure .paint.subexp.word -expand no
create_form_entry .paint.config "Configuration" Paint_Config {-foreground red}
pack [button .paint.add -text "Add to Menu" -command {
  add_to_form_menu Regexpressions [list [.paint.tag.e get] \
	[concat [.paint.tag.e get] [paint_regexp] \
	[.paint.config.e get]]]}] -fill x

create_form_entry .testtag "Test Tag" Paint_Test_Tag {-foreground grey -relief raised}
