<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Tk::CodeText - a TextUndo widget with syntax highlighting capabilities</title>
<link rev="made" href="mailto:feedback@suse.de" />
</head>

<body style="background-color: white">

<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<li><a href="#options">OPTIONS</a></li>
	<li><a href="#methods">METHODS</a></li>
	<li><a href="#syntax_highlighting">SYNTAX HIGHLIGHTING</a></li>
	<li><a href="#writing_plugins">WRITING PLUGINS</a></li>
	<li><a href="#author">AUTHOR</a></li>
	<li><a href="#bugs">BUGS</a></li>
	<li><a href="#todo">TODO</a></li>
	<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Tk::CodeText - a TextUndo widget with syntax highlighting capabilities</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
 use Tk;
 require Tk::CodeText;</pre>
<pre>
 my $m = new MainWindow;</pre>
<pre>
 my $e = $m-&gt;Scrolled('CodeText',
        -disablemenu =&gt; 1,
        -syntax =&gt; 'Perl',
        -scrollbars =&gt; 'se',
 )-&gt;pack(-expand =&gt; 1, -fill =&gt; 'both');</pre>
<pre>
 $m-&gt;configure(-menu =&gt; $e-&gt;menu);
 $m-&gt;MainLoop;</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Tk::CodeText inherits Tk::TextUndo and all its options and methods. Besides
syntax highlighting, methods are provided for commenting and uncommenting
as well as indenting and unindenting a selected area, matching pairs of braces, brackets and
brackets and curlies and automatic indenting of new lines.</p>
<p>Syntax highlighting is done through a plugin approach. Currently there is
support for <strong>Perl</strong>, <strong>Pod</strong>,  <strong>HTML</strong> and <strong>Xresources</strong>. Adding languages 
is a matter of writing plugin modules. Theoretically this is not limited to programming languages. 
The plugin approach could also provide the possibility for grammar or spell checking in spoken 
languages.</p>
<p>
</p>
<hr />
<h1><a name="options">OPTIONS</a></h1>
<dl>
<dt><strong><a name="item_name%3a_autoindent">Name: <strong>autoindent</strong></a></strong><br />
</dt>
<dt><strong><a name="item_class%3a_autoindent">Class: <strong>Autoindent</strong></a></strong><br />
</dt>
<dt><strong><a name="item_switch%3a_%2dautoindent">Switch: <strong>-autoindent</strong></a></strong><br />
</dt>
<dd>
Boolean, when you press the enter button, should the next line begin at the
same position as the current line or not. By default <strong>false</strong>.
</dd>
<p></p>
<dt><strong><a name="item_name%3a_commentchar">Name: <strong>commentchar</strong></a></strong><br />
</dt>
<dt><strong><a name="item_class%3a_commentchar">Class: <strong>Commentchar</strong></a></strong><br />
</dt>
<dt><strong><a name="item_switch%3a_%2dcommentchar">Switch: <strong>-commentchar</strong></a></strong><br />
</dt>
<dd>
By default ``#''.
</dd>
<p></p>
<dt><strong><a name="item_name%3a_disablemenu">Name: <strong>disablemenu</strong></a></strong><br />
</dt>
<dt><strong><a name="item_class%3a_disablemenu">Class: <strong>Disablemenu</strong></a></strong><br />
</dt>
<dt><strong><a name="item_switch%3a_%2ddisablemenu">Switch: <strong>-disablemenu</strong></a></strong><br />
</dt>
<dd>
Boolean, by default 0. In case you don't want the menu under the
right mouse button to pop up.
</dd>
<p></p>
<dt><strong><a name="item_name%3a_indentchar">Name: <strong>indentchar</strong></a></strong><br />
</dt>
<dt><strong><a name="item_class%3a_indentchar">Class: <strong>Indentchar</strong></a></strong><br />
</dt>
<dt><strong><a name="item_switch%3a_%2dindentchar">Switch: <strong>-indentchar</strong></a></strong><br />
</dt>
<dd>
By default ``\t''.
</dd>
<p></p>
<dt><strong><a name="item_name%3a_match">Name: <strong>match</strong></a></strong><br />
</dt>
<dt><strong><a name="item_class%3a_match">Class: <strong>Match</strong></a></strong><br />
</dt>
<dt><strong><a name="item_switch%3a_%2dmatch">Switch: <strong>-match</strong></a></strong><br />
</dt>
<dd>
string of pairs for brace/bracket/curlie etc matching. If this description
doesn't make anything clear, don't worry, the default setting will:
</dd>
<dd>
<pre>
 '[]{}()'</pre>
</dd>
<dd>
<p>if you don't want matching to be available, simply set it to ''.</p>
</dd>
<p></p>
<dt><strong><a name="item_name%3a_matchoptions">Name: <strong>matchoptions</strong></a></strong><br />
</dt>
<dt><strong><a name="item_class%3a_matchoptions">Class: <strong>Matchoptions</strong></a></strong><br />
</dt>
<dt><strong><a name="item_switch%3a_%2dmatchoptions">Switch: <strong>-matchoptions</strong></a></strong><br />
</dt>
<dd>
Options list for the tag 'Match'. By default:
</dd>
<dd>
<pre>
 [-background =&gt; 'red', -foreground =&gt; 'yellow']</pre>
</dd>
<dd>
<p>You can also specify this option as a space separated string. Might come in
handy for your Xresource files.</p>
</dd>
<dd>
<pre>
 &quot;-background red -foreground yellow&quot;</pre>
</dd>
<p></p>
<dt><strong><a name="item_name%3a_not_available">Name: not available</a></strong><br />
</dt>
<dt><strong><a name="item_class%3a_not_available">Class: not available</a></strong><br />
</dt>
<dt><strong><a name="item_switch_%2drules">Switch <strong>-rules</strong></a></strong><br />
</dt>
<dd>
Specify the color and font options for highlighting. You specify a list
looking a bit like this.
</dd>
<dd>
<pre>
 [
     ['Tagname1', @options1],
     ['Tagname2', @options2],
 ]</pre>
</dd>
<dd>
<p>The names of the tags are depending on the syntax that is highlighted. 
See the language modules for more information about this data structure.</p>
</dd>
<p></p>
<dt><strong><a name="item_name%3a_rulesdir">Name: rulesdir</a></strong><br />
</dt>
<dt><strong><a name="item_class%3a_rulesdir">Class: Rulesdir</a></strong><br />
</dt>
<dt><strong><a name="item_switch_%2drulesdir">Switch <strong>-rulesdir</strong></a></strong><br />
</dt>
<dd>
Specify the directory where this widget stores its coloring defenitions.
Files in this directory are stored as ``HTML.rules'', ``Perl.rules'' etc.
By default it is set to '', which means that when you switch syntax
the highlighting rules are not loaded or stored. The hard coded defaults
in the language modules will be used.
</dd>
<p></p>
<dt><strong><a name="item_name%3a_syntax">Name: <strong>syntax</strong></a></strong><br />
</dt>
<dt><strong><a name="item_class%3a_syntax">Class: <strong>Syntax</strong></a></strong><br />
</dt>
<dt><strong><a name="item_switch%3a_%2dsyntax">Switch: <strong>-syntax</strong></a></strong><br />
</dt>
<dd>
Specifies the language for highlighting. At this moment the possible
values are <strong>None</strong>, <strong>HTML</strong>, <strong>Perl</strong>, <strong>Pod</strong> and <strong>Xresources</strong>. 
By default <strong>None</strong>
</dd>
<dd>
<p>Alternatively it is possible to specify a reference to your independent plugin.</p>
</dd>
<p></p>
<dt><strong><a name="item_name%3a_not_available">Name: Not available</a></strong><br />
</dt>
<dt><strong><a name="item_class%3a_not_available">Class: Not available</a></strong><br />
</dt>
<dt><strong><a name="item_switch%3a_%2dupdatecall">Switch: <strong>-updatecall</strong></a></strong><br />
</dt>
<dd>
Here you can specify a callback that will be executed whenever the insert
cursor has moved or text has been modified, so your application can keep
track of position etc. Don't make this callback to heavy, the widget will
get sluggish quickly.
</dd>
<p></p></dl>
<p>There are some undocumented options. They are used internally. 
It is propably best to leave them alone.</p>
<p>
</p>
<hr />
<h1><a name="methods">METHODS</a></h1>
<dl>
<dt><strong><a name="item_doautoindent"><strong>doAutoIndent</strong></a></strong><br />
</dt>
<dt><strong><a name="item_highlight"><strong>highlight</strong></a></strong><br />
</dt>
<dt><strong><a name="item_highlightcheck"><strong>highlightCheck</strong></a></strong><br />
</dt>
<dt><strong><a name="item_highlightline"><strong>highlightLine</strong></a></strong><br />
</dt>
<dt><strong><a name="item_highlightplug"><strong>highlightPlug</strong></a></strong><br />
</dt>
<dt><strong><a name="item_highlightpluginit"><strong>highlightPlugInit</strong></a></strong><br />
</dt>
<dt><strong><a name="item_highlightpurge"><strong>highlightPurge</strong></a></strong><br />
</dt>
<dt><strong><a name="item_highlightvisual"><strong>highlightVisual</strong></a></strong><br />
</dt>
<dt><strong><a name="item_linenumber"><strong>linenumber</strong></a></strong><br />
</dt>
<dt><strong><a name="item_matchcheck"><strong>matchCheck</strong></a></strong><br />
</dt>
<dt><strong><a name="item_matchfind"><strong>matchFind</strong></a></strong><br />
</dt>
<dt><strong><a name="item_rulesedit"><strong>rulesEdit</strong></a></strong><br />
</dt>
<dd>
Pops up a window that enables the user the set the color and font options
for the current syntax.
</dd>
<p></p>
<dt><strong><a name="item_rulesfetch"><strong>rulesFetch</strong></a></strong><br />
</dt>
<dt><strong><a name="item_rulessave"><strong>rulesSave</strong></a></strong><br />
</dt>
<dt><strong><a name="item_selectioncomment"><strong>selectionComment</strong></a></strong><br />
</dt>
<dt><strong><a name="item_selectionindent"><strong>selectionIndent</strong></a></strong><br />
</dt>
<dt><strong><a name="item_selectionmodify"><strong>selectionModify</strong></a></strong><br />
</dt>
<dt><strong><a name="item_selectionuncomment"><strong>selectionUnComment</strong></a></strong><br />
</dt>
<dt><strong><a name="item_selectionunindent"><strong>selectionUnIndent</strong></a></strong><br />
</dt>
</dl>
<p>
</p>
<hr />
<h1><a name="syntax_highlighting">SYNTAX HIGHLIGHTING</a></h1>
<p>This section is a brief description of how the syntax highlighting process
works.</p>
<p><strong>Initiating plugin</strong></p>
<p>The highlighting plugin is only then initiated when it is needed. When some
highlighting needs to be done, the widget calls <strong>highlightPlug</strong> to retrieve
a reference to the plugin.</p>
<p><strong>highlightPlug</strong> checks wether a plugin is present. Next it will check whether  the <strong>-rules</strong> option has been 
specified or whter the <strong>-rules</strong> option has changed. If no rules are specified in <strong>-rules</strong>, it will look for a pathname
in the <strong>-rulesdir</strong> option. If that is found it will try to load a file called '*.rules', where * is the value of <strong>-syntax</strong>.</p>
<p>If no plugin is present, or the <strong>-syntax</strong> option has changed value,
<strong>highlightPlug</strong> loads the plugin. and constructs optionally giving it 
a reference to the found rules as parameter. if no rules
are specified, the plugin will use its internal hardcoded defaults.</p>
<p><strong>Changing the rules</strong></p>
<p>A set of rules is a list, containing lists of tagnames, followed by options. 
If you want to see what they look like, you can have a look at the constructors
of each plugin module. Every plugin has a fixed set of tagnames it can handle.</p>
<p>There are two ways to change the rules.</p>
<p>You can invoke the <strong>rulesEdit</strong> method, which is also available through the 
<strong>View</strong> menu. The result is a popup in which you can specify color and font
options for each tagname. After pressing 'Ok', the edited rules will be applied.
If <strong>-rulesdir</strong> is specified, the rules will be saved on disk as
<em>rulesdir/syntax.rules</em>.</p>
<p>You can also use <strong>configure</strong> to specify a new set of rules. In this you have
ofcause more freedom to use all available tag options. For more details about
those there is a nice section about tag options in the Tk::Text documentation.
After the call to <strong>configure</strong> it is wise to call <strong>highlightPlug</strong>.</p>
<p><strong>Highlighting text</strong></p>
<p>Syntax highlighting is done in a lazy manor. Only that piece of text is
highlighted that is needed to present the user a pretty picture. This is
done to minimize use of system resources. Highlighting is running on the
foreground. Jumping directly to the end of a long fresh loaded textfile may
very well take a couple of seconds.</p>
<p>Highlighting is done on a line to line basis. At the end of each line the
highlighting status is saved in the list in <strong>-colorinf</strong>, so when highlighting
the next line, the <strong>highlight</strong> method of <strong>CodeText</strong> will know how to begin.</p>
<p>The line that needs highlighting is offered to the <strong>highlight</strong> method of
the plugin. This method returns a list of offset and tagname pairs.
Take for example the following line of perl code.</p>
<pre>
 my $mother = 'older than i am';</pre>
<p>The <strong>highlight</strong> method of the Perl plugin will return the following list;</p>
<pre>
 (2 =&gt; 'Reserved',    #'my' is a reserved word
  1 =&gt; 'DEFAULT',     #Space
  7 =&gt; 'Variable',    #$mother
  1 =&gt; 'DEFAULT',     #Space
  1 =&gt; 'Operator',    #'='
  1 =&gt; 'DEFAULT',     #Space
  17 =&gt; 'String',     #'older than i am'
  1 =&gt; 'DEFAULT',)    #;</pre>
<p>The <strong>highlight</strong> method of CodeText will then mark positions 0 to 2 as 
'Reserved', positions 2 to 3 as 'DEFAULT', positions 3 to 10 as 'Variable',
etcetera.</p>
<p>
</p>
<hr />
<h1><a name="writing_plugins">WRITING PLUGINS</a></h1>
<p>After writing a couple of plugins myself i have come to a couple of guidelines
about how to set them up. If you are interested in adding support for your
own syntax highlighting problem or language this section is of interest to you.</p>
<p><strong>From scratch</strong></p>
<p>If you choose to build a plugin completely from scratch, your module needs
to meet the following requirements.</p>
<pre>
 - If you want to write a formal addition to Tk::CodeText, 
   your plugin must be in the namespace 
   Tk::CodeText::YourSyntax.
 - The constructor is called 'new', and it should accept 
   a reference a reference to a list of rules as parameters.
 - The following methods will be called upon by Tk::CodeText: 
     highlight, stateCompare, rules, setSate, 
     getState, syntax.</pre>
<p>More information about those methods is available in the documentation of
Tk::CodeText::None and Tk::CodeText::Template. Good luck, you're on your own now.</p>
<p><strong>Inheriting Tk::CodeText::Template</strong></p>
<p>For many not too complicated highlighting problems Tk::CodeText::Template
provides a nice basis to start from. I have been able to write to
easily write support for HTML, Pod  and Xresources files. Anyway, your code
should look like this:</p>
<pre>
 package Tk::CodeText::MySyntax;
 
 use strict;
 use base('Tk::CodeText::Template');
 
 sub new {
    my ($proto, $wdg, $rules) = @_;
    my $class = ref($proto) || $proto;</pre>
<p>Next, specify the set of hardcoded rules.</p>
<pre>
    if (not defined($rules)) {
       $rules =  [
          ['Tagname1', -foreground =&gt; 'red'],
          ['Tagname1', -foreground =&gt; 'red'],
       ];
    };</pre>
<p>Call the constructor of Tk::CodeText::Template and bless your
object.</p>
<pre>
    my $self = $class-&gt;SUPER::new($wdg, $rules);
    bless ($self, $class);</pre>
<p>Perhaps do a couple of other things</p>
<pre>
    #$self-&gt;listAdd('listname', value1, value2 ....)</pre>
<pre>
    return $self;
 }</pre>
<p>Then you need a <strong>highlight</strong> method. This method will be given a line of text
as parameter by CodeText.</p>
<pre>
 sub highlight {
    my ($self $txt) = @_;</pre>
<p>Reset everything so that there is a clean beginning;</p>
<pre>
    $cw-&gt;snippet('');
    my $out = $self-&gt;out;
    @$out = ();</pre>
<p>Here comes the difficult part, where you are on your own. Perhaps you can
draw some inspiration from existing highlighting plugins. The trick is to
repeatedly build a text snippet using the <strong>snippet</strong> and/or <strong>snippetAppend</strong>
methods of Tk::CodeText::Template. Whenever you have a snippet, you want to
assign a tag to, you call <strong>snippetParse</strong>. The methods <strong>stack</strong>, <strong>stackPush</strong>,
and <strong>stackPush</strong> are used to keep track of the current, possibly nested,
tag. <strong>snippetParse</strong> pushes the length of the snippet and the tagname that
is on top of the stack to the @$out array. For more information read the
documentation of Tk::CodeText::Template.</p>
<p>After you're done highlighting, you still have to return your result;</p>
<pre>
    return @$out;
 }</pre>
<p>And then, last but not least, you need a <strong>syntax</strong> method.</p>
<pre>
 sub syntax {
    my $self = shift;
    return 'MySyntax'
 }
 1;</pre>
<p><strong>Using another module as basis</strong></p>
<p>An example of this approach is the Perl syntax module.</p>
<p>Also with this approach you will have to meet the minimum criteria
as set out in the <strong>From scratch</strong> section.</p>
<p>
</p>
<hr />
<h1><a name="author">AUTHOR</a></h1>
<dl>
<dt><strong><a name="item_jeuken">Hans Jeuken (<a href="mailto:haje@toneel.demon.nl">haje@toneel.demon.nl</a>)</a></strong><br />
</dt>
</dl>
<p>
</p>
<hr />
<h1><a name="bugs">BUGS</a></h1>
<p>Unknown.</p>
<p>
</p>
<hr />
<h1><a name="todo">TODO</a></h1>
<dl>
<dt><strong><a name="item_find_and_eliminate_bugs%2e">Find and eliminate bugs.</a></strong><br />
</dt>
<dt><strong><a name="item_improve_documentation%2e">Improve documentation.</a></strong><br />
</dt>
<dt><strong><a name="item_add_additional_language_modules%2e_i_am_going_to_n">Add additional language modules. I am going to need help on this one.</a></strong><br />
</dt>
<dt><strong><a name="item_optimize_highlighting_methods%2e">Optimize highlighting methods.</a></strong><br />
</dt>
<dd>
Syntax highlighting consumes a lot of system resources. Squeezing every bit
of performance out of it might make it less sluggish on slow systems, like
a pentium at 100Mhz.
</dd>
<dd>
<p>If you're interested in this module, please have a look at the code 
and point out to me where I might have overlooked something. Of special
interest are the CodeText methods <strong>highlight</strong> and <strong>highlightLine</strong> as well as
the code of the language modules.</p>
</dd>
<p></p>
<dt><strong><a name="item_add_variable_options_for_linenumber%2c_position%2c">Add variable options for linenumber, position, filename etc.</a></strong><br />
</dt>
<dt><strong><a name="item_make_the_rules_editor_fail_safe%2e">Make the rules editor fail safe.</a></strong><br />
</dt>
</dl>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<dl>
<dt><strong><a name="item_tk%3a%3atext%2c_tk%3a%3atextundo%2c_tk%3a%3acodete"><strong>Tk::Text</strong>, <strong>Tk::TextUndo</strong>, <strong>Tk::CodeText::None</strong>, <strong>Tk::CodeText::Perl</strong>
<strong>Tk::CodeText::HTML</strong></a></strong><br />
</dt>
</dl>

</body>

</html>
