1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
|
=head1 NAME
Tk::CodeText - a TextUndo widget with syntax highlighting capabilities
=head1 SYNOPSIS
=over 4
use Tk;
require Tk::CodeText;
my $m = new MainWindow;
my $e = $m->Scrolled('CodeText',
-disablemenu => 1,
-syntax => 'Perl',
-scrollbars => 'se',
)->pack(-expand => 1, -fill => 'both');
$m->configure(-menu => $e->menu);
$m->MainLoop;
=back
=head1 DESCRIPTION
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.
Syntax highlighting is done through a plugin approach. 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. If you have written a plugin and it works, if you send it to me, i
will be happy to include it in the next release of Tk::CodeText. Currently there is
support for B<Perl>, B<Pod>, B<Bash>, B<HTML> and B<Xresources>.
=head1 OPTIONS
=over 4
=item Name: B<autoindent>
=item Class: B<Autoindent>
=item Switch: B<-autoindent>
Boolean, when you press the enter button, should the next line begin at the
same position as the current line or not. By default B<false>.
=item Name: B<commentchar>
=item Class: B<Commentchar>
=item Switch: B<-commentchar>
By default "#".
=item Name: B<disablemenu>
=item Class: B<Disablemenu>
=item Switch: B<-disablemenu>
Boolean, by default 0. In case you don't want the menu under the
right mouse button to pop up.
=item Name: B<indentchar>
=item Class: B<Indentchar>
=item Switch: B<-indentchar>
By default "\t".
=item Name: B<match>
=item Class: B<Match>
=item Switch: B<-match>
string of pairs for brace/bracket/curlie etc matching. If this description
doesn't make anything clear, don't worry, the default setting will:
'[]{}()'
if you don't want matching to be available, simply set it to ''.
=item Name: B<matchoptions>
=item Class: B<Matchoptions>
=item Switch: B<-matchoptions>
Options list for the tag 'Match'. By default:
[-background => 'red', -foreground => 'yellow']
You can also specify this option as a space separated string. Might come in
handy for your Xresource files.
"-background red -foreground yellow"
=item Name: not available
=item Class: not available
=item Switch B<-rules>
Specify the color and font options for highlighting. You specify a list
looking a bit like this.
[
['Tagname1', @options1],
['Tagname2', @options2],
]
The names of the tags are depending on the syntax that is highlighted.
See the language modules for more information about this data structure.
=item Name: rulesdir
=item Class: Rulesdir
=item Switch B<-rulesdir>
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.
=item Name: B<syntax>
=item Class: B<Syntax>
=item Switch: B<-syntax>
Specifies the language for highlighting. At this moment the possible
values are B<None>, B<HTML>, B<Perl>, B<Pod> and B<Xresources>.
By default B<None>
Alternatively it is possible to specify a reference to your independent plugin.
=item Name: Not available
=item Class: Not available
=item Switch: B<-updatecall>
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.
=back
There are some undocumented options. They are used internally.
It is propably best to leave them alone.
=cut
=head1 METHODS
=over 4
=item B<doAutoIndent>
Checks the indention of the previous line and indents
the line where the cursor is equally deep.
=item B<highlight>(I<$begin>, I<$end>);
Does syntax highlighting on the section of text indicated by $begin and $end.
$begin and $end are linenumbers not indexes!
=item B<highlightCheck>>(I<$begin>, I<$end>);
An insert or delete has taken place affecting the section of text between $begin and $end.
B<highlightCheck> is being called after and insert or delete operation. $begin and $end (again
linenumbers, not indexes) indicate the section of text affected. B<highlightCheck> checks what
needs to be highlighted again and does the highlighting.
=item B<highlightLine>(I<$line>);
Does syntax highlighting on linenumber $line.
=item B<highlightPlug>
Checks wether the appropriate highlight plugin has been loaded. If none or the wrong
one is loaded, it loads the correct plugin. It returns a reference to the plugin loaded.
It also checks wether the rules have changed. If so, it restarts highlighting
from the beginning of the text.
=item B<highlightPlugInit>
Loads and initalizes a highlighting plugin. First it checks the value of the B<-syntax> option
to see which plugin should be loaded. Then it checks wether a set of rules is defined to this plugin
in the B<-rules> option. If not, it tries to obtain a set of rules from disk using B<rulesFetch>.
If this fails as well it will use the hardcoded rules from the syntax plugin.
=item B<highlightPurge>(I<$line>);
Tells the widget that the text from linenumber $line to the end of the text is not to be considered
highlighted any more.
=item B<highlightVisual>
Calls B<visualEnd> to see what part of the text is visible on the display, and adjusts highlighting
accordingly.
=item B<linenumber>(I<$index>);
Returns the linenumber part of an index. You may also specify indexes like 'end' or 'insert' etc.
=item B<matchCheck>
Checks wether the character that is just before the 'insert'-mark should be matched, and if so
should it match forwards or backwards. It then calls B<matchFind>.
=item B<matchFind>(I<$direction>, I<$char>, I<$match>, I<$start>, I<$stop>);
Matches $char to $match, skipping nested $char/$match pairs, and displays the match found (if any).
=item B<rulesEdit>
Pops up a window that enables the user to set the color and font options
for the current syntax.
=item B<rulesFetch>
Checks wether the file
$text->cget('-rulesdir') . '/' . $text->cget('-syntax') . '.rules'
exists, and if so attempts to load this as a set of rules.
=item B<rulesSave>
Saves the currently loaded rules as
$text->cget('-rulesdir') . '/' . $text->cget('-syntax') . '.rules'
=item B<selectionComment>
Comment currently selected text.
=item B<selectionIndent>
Indent currently selected text.
=item B<selectionModify>
Used by the other B<selection...> methods to do the actual work.
=item B<selectionUnComment>
Uncomment currently selected text.
=item B<selectionUnIndent>
Unindent currently selected text.
=back
=head1 SYNTAX HIGHLIGHTING
This section is a brief description of how the syntax highlighting process
works.
B<Initiating plugin>
The highlighting plugin is only then initiated when it is needed. When some
highlighting needs to be done, the widget calls B<highlightPlug> to retrieve
a reference to the plugin.
B<highlightPlug> checks wether a plugin is present. Next it will check whether
the B<-rules> option has been specified or wether the B<-rules> option has changed.
If no rules are specified in B<-rules>, it will look for a pathname
in the B<-rulesdir> option. If that is found it will try to load a file
called '*.rules', where * is the value of B<-syntax>.
If no plugin is present, or the B<-syntax> option has changed value,
B<highlightPlug> 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.
B<Changing the rules>
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.
There are two ways to change the rules.
You can invoke the B<rulesEdit> method, which is also available through the
B<View> 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 B<-rulesdir> is specified, the rules will be saved on disk as
I<rulesdir/syntax.rules>.
You can also use B<configure> 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 B<configure> it is wise to call B<highlightPlug>.
B<Highlighting text>
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.
Highlighting is done on a line to line basis. At the end of each line the
highlighting status is saved in the list in B<-colorinf>, so when highlighting
the next line, the B<highlight> method of B<CodeText> will know how to begin.
The line that needs highlighting is offered to the B<highlight> method of
the plugin. This method returns a list of offset and tagname pairs.
Take for example the following line of perl code.
my $mother = 'older than i am';
The B<highlight> method of the Perl plugin will return the following list;
(2 => 'Reserved', #'my' is a reserved word
1 => 'DEFAULT', #Space
7 => 'Variable', #$mother
1 => 'DEFAULT', #Space
1 => 'Operator', #'='
1 => 'DEFAULT', #Space
17 => 'String', #'older than i am'
1 => 'DEFAULT',) #;
The B<highlight> 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.
=cut
=head1 WRITING PLUGINS
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.
B<From scratch>
If you choose to build a plugin completely from scratch, your module needs
to meet the following requirements.
- 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.
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.
B<Inheriting Tk::CodeText::Template>
For many highlighting problems Tk::CodeText::Template
provides a nice basis to start from. Your code
could look like this:
package Tk::CodeText::MySyntax;
use strict;
use base('Tk::CodeText::Template');
sub new {
my ($proto, $wdg, $rules) = @_;
my $class = ref($proto) || $proto;
Next, specify the set of hardcoded rules.
if (not defined($rules)) {
$rules = [
['Tagname1', -foreground => 'red'],
['Tagname1', -foreground => 'red'],
];
};
Call the constructor of Tk::CodeText::Template and bless your
object.
my $self = $class->SUPER::new($rules);
So now we have the SUPER class avalable and we can start defining
a couple of things.
You could add a couple of lists, usefull for keywords etc.
$self->lists({
'Keywords' => ['foo', 'bar'],
'Operators' => ['and', 'or'],
});
For every tag you have to define a corresponding callback like this.
$self->callbacks({
'Tagname1' => \&Callback1,
'Tagname2' => \&Callback2,
});
You have to define a default tagname like this:
$self->stackPush('Tagname1');
Perhaps do a couple of other things but in the end, wrap up the new method.
bless ($self, $class);
return $self;
}
Then you need define the callbacks that are mentioned in the B<callbacks>
hash. When you just start writing your plugin i suggest you make them look
like this:
sub callback1 {
my ($self $txt) = @_;
return $self->parserError($txt); #for debugging your later additions
}
Later you add matching statements inside these callback methods. For instance,
if you want I<callback1> to parse spaces it is going to look like this:
sub callback1 {
my ($self $txt) = @_;
if ($text =~ s/^(\s+)//) { #spaces
$self->snippetParse($1, 'Tagname1'); #the tagname here is optional
return $text;
}
return $self->parserError($txt); #for debugging your later additions
}
If I<callback1> is the callback that is called by default, you have to add
the mechanism for checking lists to it. Hnce, the code will look like this:
sub callback1 {
my ($self $txt) = @_;
if ($text =~ s/^(\s+)//) { #spaces
$self->snippetParse($1, 'Tagname1'); #the tagname here is optional
return $text;
}
if ($text =~ s/^([^$separators]+)//) { #fetching a bare part
if ($self->tokenTest($1, 'Reserved')) {
$self->snippetParse($1, 'Reserved');
} elsif ($self->tokenTest($1, 'Keyword')) {
$self->snippetParse($1, 'Keyword');
} else { #unrecognized text
$self->snippetParse($1);
}
return $text
}
return $self->parserError($txt); #for debugging your later additions
}
Have a look at the code of Tk::CodeText::Bash. Things should clear up.
And then, last but not least, you need a B<syntax> method.
B<Using another module as basis>
An example of this approach is the Perl syntax module.
Also with this approach you will have to meet the minimum criteria
as set out in the B<From scratch> section.
=cut
=head1 AUTHOR
=over 4
=item Hans Jeuken (haje@toneel.demon.nl)
=back
=cut
=head1 BUGS
Unknown. If you find any, please contact the author.
=cut
=head1 TODO
=over 4
=item Find and eliminate bugs.
=item Add additional language modules. I am going to need help on this one.
=item Optimize highlighting methods.
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.
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 B<highlight> and B<highlightLine> as well as
the code of the language modules.
=back
=cut
=head1 SEE ALSO
=over 4
=item B<Tk::Text>, B<Tk::TextUndo>, B<Tk::CodeText::None>, B<Tk::CodeText::Perl>
B<Tk::CodeText::HTML>
=back
=cut
|