XText.pm untested

filter: Statementstmt 0.0% 0 / 1357 Branchbran 0.0% 0 / 386 Conditioncond 0.0% 0 / 57 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 0.0% 0 / 122 Podpod 0.0% 0 / 109 totaltotal 0.0% 0 / 2069 CC 194 SCAR 36.8
CC
194
Cov
0%
RecordUndo
182.0
FindNext
156.0
uncomment
156.0
CRAP 39.5
1 package Tk::XText;
2  
3 =head1 NAME
4  
5 Tk::XText - Extended Text widget
6  
7 =cut
8  
9 use strict;
10 use warnings;
11 use Carp;
12 use vars qw($VERSION);
13 $VERSION = '0.69';
14  
15 use Tk;
16 use Math::Round;
17 use Text::Patch;
18 require Tk::DialogBox;
19 require Tk::LabFrame;
20 require Tk::Spinbox;
21 require Tk::HList;
22  
23 use base qw(Tk::Derived Tk::Text);
24 Construct Tk::Widget 'XText';
25  
26 #boilerplating for auto complete facilities
27 my %delimhash = ( '.', 1, '(', 1, ')', 1, ':', 1, '!', 1, '+', 1, ',', 1, '-', 1, '<', 1, '=',
28 1, '>', 1, '%', 1, '&', 1, '*', 1, '"', 1, '\'', 1, '/', 1, ';', 1, '?', 1, '[', 1, ']', 1,
29 '^', 1, '{', 1, '|', 1, '}', 1, '~', 1, '\\', 1, '$', 1, '@', 1, '#', 1, '`', 1, ' ', 1, "\t", 1
30 );
31  
32 =head1 SYNOPSIS
33  
34 require Tk::XText;
35 my $text= $window->XText(@options)->pack;
36  
37 =head1 DESCRIPTION
38  
39 B<Tk::XText> inherits L<Tk::Text>. It adds an advanced Undo/Redo stack,
40 facilities for indenting and unindenting, commenting and uncommenting,
41 autocomplete and autoindent, loading, saving and exporting.
42  
43 It's main purpose is to serve as the text widget in the L<Tk::CodeText>
44 mega widget.
45  
46 The options and methods below are only documented if they are not available
47 within the L<Tk::CodeText> context. Otherwise see there.
48  
49 =head1 OPTIONS
50  
51 =over 4
52  
53 =item Name: B<acPopSize>
54  
55 =item Class: B<AcPopSize>
56  
57 =item Switch: B<-acpopsize>
58  
59 .
60  
61 =item Name: B<acScanSize>
62  
63 =item Class: B<AcScanSize>
64  
65 =item Switch: B<-acscansize>
66  
67 .
68  
69 =item Name: B<activeDelay>
70  
71 =item Class: B<ActiveDelay>
72  
73 =item Switch: B<-activedelay>
74  
75 .
76  
77 =item Name: B<autoBrackets>
78  
79 =item Class: B<AutoBrackets>
80  
81 =item Switch: B<-autobrackets>
82  
83 .
84  
85 =item Name: B<autoComplete>
86  
87 =item Class: B<AutoComplete>
88  
89 =item Switch: B<-autocomplete>
90  
91 .
92  
93 =item Name: B<autoIndent>
94  
95 =item Class: B<AutoIndent>
96  
97 =item Switch: B<-autoindent>
98  
99 .
100  
101 =item Switch: B<-contextmenu>
102  
103 .
104  
105 =item Switch: B<-escapepressed>
106  
107 Callback. Called when the user presses the escape key.
108  
109 =item Switch: B<-findoptions>
110  
111 .
112  
113 =item Switch: B<-findandreplacecall>
114  
115 .
116  
117 =item Name: B<indentStyle>
118  
119 =item Class: B<IndentStyle>
120  
121 =item Switch: B<-indentstyle>
122  
123 .
124  
125 =item Switch: B<-keyreleasecall>
126  
127 Callback. Called when the user releases a key. Gets the I<Ev(A)> value of the event as parameter.
128  
129 =item Switch: B<-logcall>
130  
131 .
132  
133 =item Name: B<match>
134  
135 =item Class: B<Match>
136  
137 =item Switch: B<-match>
138  
139 .
140  
141 =item Switch: B<-menuitems>
142  
143 .
144  
145 =item Switch: B<-matchoptions>
146  
147 .
148  
149 =item Switch: B<-mlcommentend>
150  
151 The end string of a multi line comment.
152  
153 =item Switch: B<-mlcommentstart>
154  
155 The start string of a multi line comment.
156  
157 =item Switch: B<-modifycall>
158  
159 This callback is called every time text is modified.
160 used in L<Tk::Codetext> for adjusting line numbers and
161 triggering syntax highlighting/code folding.
162  
163 =item Switch: B<-readonly>
164  
165 .
166  
167 =item Switch: B<-slcomment>
168  
169 The start string of a single line comment.
170  
171 =back
172  
173 =cut
174  
175 =head1 EVENTS AND KEYBINDINGS
176  
177 Besides the events of l<Tk::Text>, this module responds to
178 the following events and key bindings:
179  
180 Event: Key:
181 <<Find>> CONTROL+F
182 <<Replace>> CONTROL+R
183 <<Indent>> CONTROL+J
184 <<UnIndent>> CONTROL+SHIFT+J or CONTROL+TAB
185 <<Comment>> CONTROL+G
186 <<UnComment>> CONTROL+SHIFT+G
187 <<Undo>> CONTROL+Z
188 <<Redo>> CONTROL+SHIFT+Z
189 <<UpperCase>> CONTROL+I
190 <<LowerCase>> CONTROL+SHIFT+I
191  
192 Further more, CONTROL+A selects all text.
193  
194 =head1 METHODS
195  
196 =over 4
197  
198 =cut
199  
200 sub Populate {
201 my ($self, $args) = @_;
202 $self->SUPER::Populate($args);
203  
204 $self->{FINDCLEARENABLED} = 1;
205  
206 #undo/redo stack
207 $self->{BUFFER} = '';
208 $self->{BUFFERMODE} = '';
209 $self->{BUFFERMODIFIED} = 0;
210 $self->{BUFFERSTART} = '1.0';
211 $self->{BUFFERREPLACE} = '';
212 $self->{REDOSTACK} = [];
213 $self->{UNDOSTACK} = [];
214  
215 #autoComplete
216 $self->{ACLINE} = 1;
217 $self->{DOCPOOL} = {};
218 $self->{RUNNING} = 0;
219 $self->{TRIGGERWORD} = '';
220  
221 $self->ResetRedo;
222 $self->ResetUndo;
223  
224
225 my $choicepop = $self->Toplevel;
226 $choicepop->overrideredirect(1);
227 $choicepop->withdraw;
228 $self->{POP} = $choicepop;
229 my $bindsub = $self->bind('<Button-1>');
230 if ($bindsub) {
231 $self->{'bind_sub'} = $bindsub;
232 $self->bind('<Button-1>', sub {
233 $bindsub->Call;
234 $self->acPopDown;
235 });
236 } else {
237 $self->bind('<Button-1>', [$self, 'acPopDown'] );
238 }
239  
240 my $lb = $choicepop->HList(
241 -borderwidth => 1,
242 -highlightthickness => 0,
243 -browsecmd => ['acSelect', $self],
244 -relief => 'raised',
245 -selectmode => 'single',
246 )->pack(-expand => 1, -fill => 'both');
247 $lb->bind('<Escape>', [$self, 'acPopDown']);
248 $lb->bind('<Return>', [$self, 'acSelect']);
249 $self->{LISTBOX} = $lb;
250  
251 $self->ConfigSpecs(
252 -activedelay => ['PASSIVE', 'activeDelay', 'ActiveDelay', 300],
253 -acpopsize => ['PASSIVE', 'acPopSize', 'AcPopSize', 5],
254 -acscansize => ['PASSIVE', 'acScanSize', 'AcScanSize', 5],
255 -autobrackets => ['PASSIVE', 'autoBrackets', 'AutoBrackets', ''],
256 -autocomplete => ['PASSIVE', 'autoComplete', 'AutoComplete', ''],
257 -autoindent => ['PASSIVE', 'autoIndent', 'AutoIndent', 0],
258 -contextmenu => ['PASSIVE'],
259 -escapepressed => ['CALLBACK', undef, undef, sub {}],
260 -findandreplacecall => ['PASSIVE'],
261 -findoptions => ['METHOD', undef, undef, [-background => '#C0FFC0', -foreground => '#000000']],
262 -indentstyle => ['PASSIVE', 'indentStyle', 'IndentStyle', "tab"],
263 -keyreleasecall => ['CALLBACK', undef, undef, sub {}],
264 -logcall => ['CALLBACK', undef, undef, sub {}],
265 -match => ['PASSIVE', 'match', 'Match', '[]{}()'],
266 -matchoptions => ['METHOD', undef, undef, [-background => '#0000FF', -foreground => '#FFFF00']],
267 -menuitems => ['PASSIVE'],
268 -mlcommentend => ['PASSIVE'],
269 -mlcommentstart => ['PASSIVE'],
270 -modifycall => ['CALLBACK', undef, undef, sub {}],
271 -readonly => ['PASSIVE', undef, undef, 0],
272 -slcomment => ['PASSIVE'],
273 -yscrollcmd => ['CALLBACK', undef, undef, sub {}],
274 DEFAULT => [ 'SELF' ],
275 );
276 $self->eventAdd('<<Find>>', '<Control-f>');
277 $self->eventAdd('<<Replace>>', '<Control-r>');
278 $self->eventAdd('<<Indent>>', '<Control-j>');
279 $self->eventAdd('<<Indent>>', '<Control-j>');
280 $self->eventAdd('<<UnIndent>>', '<Control-J>');
281 $self->eventAdd('<<Comment>>', '<Control-g>');
282 $self->eventAdd('<<UnComment>>', '<Control-G>');
283 $self->eventAdd('<<Undo>>', '<Control-z>');
284 $self->eventAdd('<<Redo>>', '<Control-Z>');
285 $self->eventAdd('<<UpperCase>>', '<Control-i>');
286 $self->eventAdd('<<LowerCase>>', '<Control-I>');
287 $self->bind('<KeyRelease>', [$self, 'KeyReleased', Ev('A')]);
288 $self->bind('<ButtonRelease-1>', 'matchCheck');
289 $self->bind('<Control-a>', 'selectAll');
290 $self->bind('<Button-2>', [ $self, 'Button2', Ev('x'), Ev('y') ]);
291 $self->bind('<B2-Motion>', [ $self, 'Button2Motion', Ev('x'), Ev('y') ]);
292 $self->bind('<ButtonRelease-2>', [ $self, 'Button2Release', Ev('x'), Ev('y') ]);
293 $self->markSet('match', '0.0');
294 $self->after(10, ['DoPostConfig', $self]);
295 }
296  
297 sub acFillList {
298 my $self = shift;
299  
300 #first some show stoppers
301 my $word = $self->acGetWord;
302 return unless defined $word;
303 my @choices = $self->acGetChoices($word);
304 return unless @choices;
305  
306
307 #fill the list
308 my $lb = $self->acListbox;
309 $lb->deleteAll;
310 for (@choices) {
311 $lb->add($_, -text => $_);
312 }
313 $lb->selectionSet($choices[0]);
314 $self->acGeometry;
315 return @choices;
316 }
317  
318 sub acGeometry {
319 my $self = shift;
320 my $lb = $self->acListbox;
321 my @choices = $lb->infoChildren('');
322 my @coord = $self->bbox($self->index('insert'));
323 if (@coord) {
324 #calculate position of the popup
325 my $x = $coord[0] + $self->rootx;
326 my $y = $coord[1] + $coord[3] + $self->rooty + 2;
327
328 #calculate size of the popup
329 my $longest = '';
330 for (@choices) {
331 $longest = $_ if length($_) > length($longest);
332 }
333 my $font = $lb->cget('-font');
334 my $width = $self->fontMeasure($font, $longest) + 10;
335  
336 my $size = $self->fontActual($font, '-size');
337 my $lineheight = int($self->fontMetrics($font, '-linespace') * 1.35);
338  
339 my $items = @choices;
340 my $height = ($items * $lineheight) + 2;
341  
342 $self->acPop->geometry($width . "x$height+$x+$y");
343 return 1
344 }
345 return 0
346 }
347  
348 sub acGetChoices {
349 my ($self, $word) = @_;
350 my $data = $self->acPool;
351 my @choices = ();
352 for (keys %$data) {
353 my $test = $_;
354 next if length($test) < length($word);
355 if ($test ne $word) {
356 push @choices, $test if lc(substr($test, 0, length($word))) eq lc($word);
357 }
358 }
359 @choices = sort {uc($a) cmp uc($b)} @choices;
360 return @choices
361 }
362  
363 sub acGetIndexes {
364 my $self = shift;
365  
366 #find starting point
367 my $ins = $self->index('insert');
368 my $start = $ins;
369 while ((not exists $delimhash{$self->get("$start - 1c", $start)}) and (not $start =~ /\.0$/)) {
370 $start = $self->index("$start - 1c");
371 }
372
373 #find end point
374 my $end = $ins;
375 my $lineend = $self->index("$end lineend");
376 while ((not exists $delimhash{$self->get($end, "$end + 1c")}) and ($end ne $lineend)) {
377 $end = $self->index("$end + 1c");
378 }
379  
380 return ($start, $end);
381 }
382  
383 sub acGetWord {
384 my $self = shift;
385 my $ins = $self->index('insert');
386 my $line = $self->get("$ins linestart", $ins);
387 if (($line =~ /([a-z0-9_]+)$/i) and (length($1) >= $self->cget('-acpopsize'))) {
388 return $1;
389 }
390 return undef
391 }
392  
393 sub acListbox { return $_[0]->{LISTBOX} }
394  
395 sub acLine {
396 my $self = shift;
397 $self->{ACLINE} = shift if @_;
398 return $self->{ACLINE}
399 }
400  
401 sub acPool { return $_[0]->{DOCPOOL} }
402  
403 sub acPop { return $_[0]->{POP} }
404  
405 sub acPopDown {
406 my $self = shift;
407 my $pop = $self->acPop;
408 return unless $pop->ismapped;
409 $pop->withdraw;
410 $pop->parent->grabRelease;
411 $self->focus();
412 if (ref $self->{'_BE_grabinfo'} eq 'CODE') {
413 $self->{'_BE_grabinfo'}->();
414 delete $self->{'_BE_grabinfo'};
415 }
416 }
417  
418 sub acPostChoices {
419 my ($self, $name) = @_;
420  
421 #first some show stoppers
422 my $word = $self->acGetWord;
423 unless (defined $word) {
424 $self->acPopDown;
425 return
426 }
427 my @choices = $self->acFillList;
428 return unless @choices;
429
430 #pop this thing
431 my $pop = $self->acPop ;
432 unless ($pop->ismapped) {
433 if ($self->acGeometry) {
434 $pop->deiconify;
435 $pop->raise;
436 }
437 }
438 }
439  
440 sub acRunning {
441 my $self = shift;
442 $self->{RUNNING} = shift if @_;
443 return $self->{RUNNING}
444 }
445  
446 sub acScan {
447 my $self = shift;
448  
449 unless ($self->cget('-autocomplete')) {
450 $self->acScanEnd;
451 return
452 }
453
454 my $end = $self->linenumber('end - 1c');
455 my $count = 0;
456 my $line = $self->acLine;
457 $line = 1 unless defined $line;
458 my $data = $self->acPool;
459 while ($count < 100) {
460 #end job if done
461 if ($line > $end) {
462 $self->acLine(1);
463 $self->acScanEnd;
464 return
465 }
466  
467 #skip line if it holds the insert cursor;
468 my $insline = $self->linenumber($self->index('insert'));
469 if ($insline eq $line) {
470 $line ++;
471 $count ++;
472 next
473 }
474  
475 #scan line
476 my $content = $self->get("$line.0", "$line.0 lineend");
477 while ($content ne '') {
478 if ($content =~ s/^([a-z0-9_]+)//i) {
479 my $word = $1;
480 if (length($word) >= $self->cget('-acscansize')) {
481 $data->{$word} = 1;
482 }
483 } else {
484 $content =~ s/^.//;
485 }
486 }
487  
488 $line++;
489 $count ++;
490 }
491 $self->acLine($line);
492 $self->after(50, ['acScan', $self]);
493 }
494  
495 sub acScanEnd {
496 my $self = shift;
497 return unless $self->acRunning;
498 my $data = $self->acPool;
499 if (defined $data) {
500 for (keys %$data) {
501 delete $data->{$_} if $data->{$_} == 0;
502 }
503 }
504 $self->acRunning(0);
505 }
506  
507 sub acScanStart {
508 my $self = shift;
509 return unless $self->cget('-autocomplete');
510 return if $self->acRunning;
511 my $data = $self->acPool;
512 if (defined $data) {
513 for (keys %$data) {
514 $data->{$_} = 0;
515 }
516 }
517 $self->acRunning(1);
518 $self->acScan;
519 }
520  
521 sub acSelect {
522 my $self = shift;
523 $self->acPopDown;
524 my ( $select ) = $self->acListbox->infoSelection;
525 return unless defined $select;
526 my ($start, $end) = $self->acGetIndexes;
527
528 #replace with select
529 $self->replace($start, $end, $select);
530 }
531  
532 sub acSettings {
533 my $self = shift;
534  
535 my $delay = $self->cget('-activedelay');
536 my $popsize = $self->cget('-acpopsize');
537 my $scansize = $self->cget('-acscansize');
538  
539 my $db = $self->DialogBox(
540 -title => 'Auto complete',
541 -buttons => ['Ok', 'Cancel'],
542 );
543 my @padding = (-padx => 2, -pady => 2);
544 my $f = $db->LabFrame(
545 -label => 'Settings',
546 -labelside => 'acrosstop',
547 )->pack(-expand => 1, -fill => 'both', @padding);
548 my $row = 0;
549 for (
550 ['Pop delay', \$delay],
551 ['Pop size', \$popsize],
552 ['Scan size', \$scansize],
553 ) {
554 my ($text, $var) = @$_;
555 $f->Label(
556 -anchor => 'e',
557 -text => "$text:",
558 )->grid(@padding, -row => $row, -column => 0, -sticky => 'ew');
559 $f->Spinbox(
560 -textvariable => $var,
561 )->grid(@padding, -row => $row, -column => 1, -sticky => 'ew');
562 $row ++
563 }
564 my $answer = $db->Show(-popover => $self);
565 if ($answer eq 'Ok') {
566 for (
567 ['-activedelay', $delay],
568 ['-acpopsize', $popsize],
569 ['-acscansize', $scansize],
570 ) {
571 my ($option, $value) = @$_;
572 if ($value =~ /^\d+$/) { #only digits
573 $self->configure($option, $value);
574 } else {
575 $self->log("Illegal value for $option", 'error')
576 }
577 }
578 }
579 $db->destroy;
580 }
581  
582 sub activate {
583 my ($self, $key) = @_;
584 return unless $self->cget('-autocomplete');
585  
586 my $word = $self->acGetWord;
587  
588 #all kinds of conditions for not proceeding
589 if ($key eq '') {
590 $self->activateCancel;
591 return
592 }
593  
594 unless (defined $word) {
595 $self->acTriggerWord('');
596 return;
597 }
598  
599 my $pop = $self->acPop;
600 if ($pop->ismapped) {
601 #pop down choice list if no word or choices to the word is found
602 if (defined $word) {
603 my @choices = $self->acGetChoices($word);
604 if (@choices) {
605 $self->acFillList;
606 } else {
607 $self->acPopDown unless @choices
608 }
609 } else {
610 $self->acPopDown
611 }
612 return
613 }
614  
615 my $tword = $self->acTriggerWord;
616 $tword = quotemeta($tword);
617 return if ($tword ne '') and ($word =~ /^$tword/);
618 $self->acTriggerWord($word);
619 $self->acFillList;
620  
621 $self->{'active_id'} = $self->after($self->cget('-activedelay'), ['acPostChoices', $self]);
622 }
623  
624 sub activateCancel {
625 my $self = shift;
626 my $id = $self->{'active_id'};
627 $self->afterCancel($id) if defined $id;
628 delete $self->{'active_id'};
629 }
630  
631 sub acTriggerWord {
632 my $self = shift;
633 $self->{TRIGGERWORD} = shift if @_;
634 return $self->{TRIGGERWORD}
635 }
636  
637 sub arrowDown {
638 my ($self, $index) = @_;
639 if ($self->acPop->ismapped) {
640 my $lb = $self->acListbox;
641 my @children = $lb->infoChildren;
642 my $last = pop @children;
643 my ($sel) = $lb->infoSelection;
644 return if $sel eq $last;
645 my $next = $lb->infoNext($sel);
646 $lb->selectionClear;
647 $lb->selectionSet($next);
648 } else {
649 $self->SetCursor($index);
650 }
651 }
652  
653 sub arrowUp {
654 my ($self, $index) = @_;
655 if ($self->acPop->ismapped) {
656 my $lb = $self->acListbox;
657 my @children = $lb->infoChildren;
658 my $first = shift @children;
659 my ($sel) = $lb->infoSelection;
660 return if $sel eq $first;
661 my $next = $lb->infoPrev($sel);
662 $lb->selectionClear;
663 $lb->selectionSet($next);
664 } else {
665 $self->SetCursor($index);
666 }
667 }
668  
669 sub Backspace {
670 my $self = shift;
671 return if $self->cget('-readonly');
672 if ($self->compare('insert','!=','1.0')) { #We are not at the start of the text
673 $self->RecordUndo('backspace', $self->editModified);
674 if ($self->selectionExists) {
675 $self->SUPER::delete('sel.first','sel.last');
676 } else {
677 $self->SUPER::delete('insert-1c')
678 }
679 $self->modifiedCall('insert');
680 }
681 }
682  
683 sub bindRdOnly {
684 my ($class,$mw) = @_;
685  
686 $mw->bind($class,'<Meta-B1-Motion>','NoOp');
687 $mw->bind($class,'<Meta-1>','NoOp');
688 $mw->bind($class,'<Alt-KeyPress>','NoOp');
689 $mw->bind($class,'<Meta-KeyPress>','NoOp');
690 $mw->bind($class,'<Control-KeyPress>','NoOp');
691 $mw->bind($class,'<Escape>','EscapePressed');
692
693 $mw->bind($class,'<1>',['Button1',Ev('x'),Ev('y')]);
694 $mw->bind($class,'<B1-Motion>','B1_Motion' ) ;
695 $mw->bind($class,'<B1-Leave>','B1_Leave' ) ;
696 $mw->bind($class,'<B1-Enter>','CancelRepeat');
697 $mw->bind($class,'<ButtonRelease-1>','CancelRepeat');
698 $mw->bind($class,'<Control-1>',['markSet','insert',Ev('@')]);
699
700 $mw->bind($class,'<Double-1>','selectWord' ) ;
701 $mw->bind($class,'<Triple-1>','selectLine' ) ;
702 $mw->bind($class,'<Shift-1>','adjustSelect' ) ;
703 $mw->bind($class,'<Double-Shift-1>',['SelectTo',Ev('@'),'word']);
704 $mw->bind($class,'<Triple-Shift-1>',['SelectTo',Ev('@'),'line']);
705
706 $mw->bind($class,'<Left>',['SetCursor',Ev('index','insert-1c')]);
707 $mw->bind($class,'<Shift-Left>',['KeySelect',Ev('index','insert-1c')]);
708 $mw->bind($class,'<Control-Left>',['SetCursor',Ev('index','insert-1c wordstart')]);
709 $mw->bind($class,'<Shift-Control-Left>',['KeySelect',Ev('index','insert-1c wordstart')]);
710  
711 $mw->bind($class,'<Right>',['SetCursor',Ev('index','insert+1c')]);
712 $mw->bind($class,'<Shift-Right>',['KeySelect',Ev('index','insert+1c')]);
713 $mw->bind($class,'<Control-Right>',['SetCursor',Ev('index','insert+1c wordend')]);
714 $mw->bind($class,'<Shift-Control-Right>',['KeySelect',Ev('index','insert wordend')]);
715
716 $mw->bind($class,'<Up>', ['arrowUp', Ev('UpDownLine',-1)]);
717 $mw->bind($class,'<Shift-Up>',['KeySelect',Ev('UpDownLine',-1)]);
718 $mw->bind($class,'<Control-Up>',['SetCursor',Ev('PrevPara','insert')]);
719 $mw->bind($class,'<Shift-Control-Up>',['KeySelect',Ev('PrevPara','insert')]);
720
721 $mw->bind($class,'<Down>', ['arrowDown', Ev('UpDownLine',1)]);
722 $mw->bind($class,'<Shift-Down>',['KeySelect',Ev('UpDownLine',1)]);
723 $mw->bind($class,'<Control-Down>',['SetCursor',Ev('NextPara','insert')]);
724 $mw->bind($class,'<Shift-Control-Down>',['KeySelect',Ev('NextPara','insert')]);
725  
726 $mw->bind($class,'<Home>',['HomeEndKey', 0]);
727 $mw->bind($class,'<Shift-Home>',['KeySelect','insert linestart']);
728 $mw->bind($class,'<Control-Home>',['SetCursor','1.0']);
729 $mw->bind($class,'<Control-Shift-Home>',['KeySelect','1.0']);
730  
731 $mw->bind($class,'<End>',['HomeEndKey', 1]);
732 $mw->bind($class,'<Shift-End>',['KeySelect','insert lineend']);
733 $mw->bind($class,'<Control-End>',['SetCursor','end-1char']);
734 $mw->bind($class,'<Control-Shift-End>',['KeySelect','end-1char']);
735  
736 $mw->bind($class,'<Prior>',['SetCursor',Ev('ScrollPages',-1)]);
737 $mw->bind($class,'<Shift-Prior>',['KeySelect',Ev('ScrollPages',-1)]);
738 $mw->bind($class,'<Control-Prior>',['xview','scroll',-1,'page']);
739  
740 $mw->bind($class,'<Next>',['SetCursor',Ev('ScrollPages',1)]);
741 $mw->bind($class,'<Shift-Next>',['KeySelect',Ev('ScrollPages',1)]);
742 $mw->bind($class,'<Control-Next>',['xview','scroll',1,'page']);
743  
744 $mw->bind($class,'<Shift-Tab>', 'NoOp'); # Needed only to keep <Tab> binding from triggering; does not have to actually do anything.
745 $mw->bind($class,'<Control-Tab>','focusNext');
746 $mw->bind($class,'<Control-Shift-Tab>','focusPrev');
747  
748 $mw->bind($class,'<Control-space>',['markSet','anchor','insert']);
749 $mw->bind($class,'<Select>',['markSet','anchor','insert']);
750 $mw->bind($class,'<Control-Shift-space>',['SelectTo','insert','char']);
751 $mw->bind($class,'<Shift-Select>',['SelectTo','insert','char']);
752 $mw->bind($class,'<Control-slash>','selectAll');
753 $mw->bind($class,'<Control-backslash>','unselectAll');
754  
755 $mw->bind($class,'<Destroy>','Destroy');
756 $mw->bind($class,'<FocusOut>','acPopDown');
757 $mw->bind($class,'<Configure>','acGeometry');
758 $mw->bind($class, '<3>', ['PostPopupMenu', Ev('X'), Ev('Y')]);
759 $mw->bind($class, '<KeyRelease>', ['KeyReleased', Ev('A')]);
760 $mw->YMouseWheelBind($class);
761 $mw->XMouseWheelBind($class);
762  
763 $mw->MouseWheelBind($class);
764
765 return $class;
766 }
767  
768 sub Buffer {
769 my $self = shift;
770 $self->{BUFFER} = shift if @_;
771 return $self->{BUFFER}
772 }
773  
774 sub BufferMode {
775 my $self = shift;
776 $self->{BUFFERMODE} = shift if @_;
777 return $self->{BUFFERMODE}
778 }
779  
780 sub BufferModified {
781 my $self = shift;
782 $self->{BUFFERMODIFIED} = shift if @_;
783 return $self->{BUFFERMODIFIED}
784 }
785  
786 sub BufferReplace {
787 my $self = shift;
788 $self->{BUFFERREPLACE} = shift if @_;
789 return $self->{BUFFERREPLACE}
790 }
791  
792 sub BufferStart {
793 my $self = shift;
794 $self->{BUFFERSTART} = shift if @_;
795 return $self->{BUFFERSTART}
796 }
797  
798 sub Button2 {
799 my ($self, $x, $y) = @_;
800 my $cursor = $self->cget('-cursor');
801 $self->configure(-cursor => 'fleur');
802 $self->{'mouse_pos'} = [$x, $y, $cursor];
803 }
804  
805 sub Button2Motion {
806 my ($self, $x, $y) = @_;
807  
808 my $mousepos = $self->{'mouse_pos'};
809 my ($mx, $my, $cursor) = @$mousepos;
810 $self->{'mouse_pos'} = [$x, $y, $cursor];
811  
812 my $dy = $my - $y;
813  
814 $self->yviewScroll(-$dy, 'units');
815 $self->Callback('-yscrollcmd', $x, $y);
816 }
817  
818 sub Button2Release {
819 my $self = shift;
820 my $mousepos = $self->{'mouse_pos'};
821 $self->configure(-cursor => $mousepos->[2]);
822 delete $self->{'mouse_pos'};
823 }
824  
825 =item B<canUndo>
826  
827 =cut
828  
829 sub canUndo {
830 my $self = shift;
831 my $stack = $self->UndoStack;
832 return ((@$stack > 0) or ($self->Buffer ne ''));
833 }
834  
835 =item B<canRedo>
836  
837 =cut
838  
839 sub canRedo {
840 my $stack = $_[0]->RedoStack;
841 return (@$stack > 0)
842 }
843  
844 sub caseChange {
845 my ($self, $upper) = @_;
846 $upper = 1 unless defined $upper;
847 my @sel = $self->tagRanges('sel');
848 my $begin;
849 my $end;
850 if (@sel) {
851 $begin = shift @sel;
852 $end = shift @sel;
853 } else {
854 $begin = $self->index('insert');
855 $end = $self->index("$begin + 1c");
856 }
857 my $text = $self->get($begin, $end);
858 if ($text ne '') {
859 if ($upper) {
860 $text = uc($text)
861 } else {
862 $text = lc($text)
863 }
864 $self->replace($begin, $end, $text);
865 }
866 }
867  
868 sub ClassInit {
869 my ($class,$mw) = @_;
870 $class->bindRdOnly($mw);
871
872 $mw->bind($class,'<Tab>', 'insertTab');
873 $mw->bind($class,'<Return>', 'returnPressed');
874 $mw->bind($class,'<Delete>','Delete');
875 $mw->bind($class,'<BackSpace>','Backspace');
876 $mw->bind($class,'<Insert>', 'ToggleInsertMode' ) ;
877 $mw->bind($class,'<KeyPress>',['InsertKeypress',Ev('A')]);
878 $class->clipboardOperations($mw,'Copy', 'Cut', 'Paste');
879
880 $mw->bind($class, '<<Find>>', ['findandreplacepopup', 1]);
881 $mw->bind($class, '<<Replace>>', ['findandreplacepopup', 0]);
882 $mw->bind($class, '<<Comment>>', 'comment');
883 $mw->bind($class, '<<Comment>>', 'comment');
884 $mw->bind($class, '<<UnComment>>', 'uncomment');
885 $mw->bind($class, '<<Indent>>', 'indent');
886 $mw->bind($class, '<<UnIndent>>', 'unindent');
887 $mw->bind($class, '<<Undo>>', 'undo');
888 $mw->bind($class, '<<Redo>>', 'redo');
889 $mw->bind($class, '<<UpperCase>>', ['caseChange', 1]);
890 $mw->bind($class, '<<LowerCase>>', ['caseChange', 0]);
891 return $class
892 }
893  
894 =item B<clear>
895  
896 =cut
897  
898 sub clear {
899 my $self = shift;
900 $self->SUPER::delete('1.0', 'end');
901  
902 $self->ResetRedo;
903 $self->ResetUndo;
904  
905 $self->Buffer('');
906 $self->BufferMode('');
907 $self->BufferModified(0);
908 $self->BufferReplace('');
909 $self->BufferStart('1.0');
910 $self->editModified(0);
911 $self->OverstrikeMode(0);
912  
913 $self->modifiedCall('1.0');
914 }
915  
916 sub clearModified {
917 my $self = shift;
918 $self->Flush;
919 $self->editModified(0);
920 $self->BufferModified(0);
921 my $r = $self->RedoStack;
922 my $u = $self->UndoStack;
923 for (@$r, @$u) { $_->{'modified'} = 1 }
924 }
925  
926 #preventing copy and cut from sending empty string to the clipboard
927 sub clipboardCopy {
928 my $self = shift;
929 $self->SUPER::clipboardCopy(@_) if $self->tagRanges('sel');
930 }
931
932 sub clipboardCut {
933 my $self = shift;
934 return $self->clipboardCopy(@_) if $self->cget('-readonly');
935 $self->SUPER::clipboardCut(@_) if $self->tagRanges('sel');
936 }
937
938 sub clipboardPaste {
939 my $self = shift;
940 return if $self->cget('-readonly');
941 $self->SUPER::clipboardPaste(@_);
942 }
943
944 =item B<comment>
945  
946 =cut
947  
948 sub comment {
949 my $self = shift;
950 return if $self->cget('-readonly');
951 my $slstart = $self->cget('-slcomment');
952 my $mlend = $self->cget('-mlcommentend');
953 my $mlstart = $self->cget('-mlcommentstart');
954 my $modified = $self->editModified;
955 if ($self->CommentType eq 'multi') {
956 #multi line operation
957 if ((defined $mlend) and (defined $mlstart)) {
958 my ($rb, $re) = $self->tagRanges('sel');
959 my $old = $self->get($rb, $re);
960 $self->SUPER::insert($rb, $mlstart);
961 $self->SUPER::insert($re, $mlend);
962 my $len = length $mlend;
963 $re = $self->index("$re + $len chars");
964 my $new = $self->get($rb, $re);
965 $self->unselectAll;
966 $self->tagAdd('sel',$rb, $re);
967 $self->RecordUndo('replace', $modified, $rb, $old, $new);
968 $self->modifiedCall($rb);
969 my $lines = $self->linenumber($re) - $self->linenumber($rb);
970 $self->log("Commented $lines lines");
971 } elsif (defined $slstart) {
972 $self->selectionModify($slstart, 0, 'Commented');
973 }
974 } else {
975 #single line operation
976 my $begin = $self->index('insert linestart');
977 my $end = $self->index("$begin lineend");
978 my $old = $self->get($begin, $end);
979 if (defined $slstart) {
980 $self->SUPER::insert($begin, $slstart);
981 } elsif ((defined $mlend) and (defined $mlstart)) {
982 $self->SUPER::insert($end, $mlend);
983 $self->SUPER::insert($begin, $mlstart);
984 }
985 my $new = $self->get($begin, "$begin lineend");
986 $self->RecordUndo('replace', $modified, $begin, $old, $new);
987 $self->modifiedCall($begin);
988 }
989 }
990  
991 sub CommentType {
992 my $self = shift;
993 if ($self->selectionExists) {
994 my $mode = 'single'; #does the selection span over multiple lines?
995 my ($rb, $re) = $self->tagRanges('sel');
996 $mode = 'multi' if ($self->linenumber($rb) < $self->linenumber($re));
997 return $mode
998 }
999 return 'single'
1000 }
1001  
1002 sub delete {
1003 my $self = shift;
1004 my $begin = $_[0];
1005 $begin = 'insert' unless defined $begin;
1006 $begin = $self->index($begin);
1007 my $string = $self->get(@_);
1008 $self->RecordUndo('delete', $self->editModified, $begin, $string);
1009 $self->SUPER::delete(@_);
1010 $self->modifiedCall($begin);
1011 }
1012  
1013 sub DoPostConfig {
1014 my $self = shift;
1015 my $string = '00000000';
1016 my $length = $self->fontMeasure($self->cget('-font'), $string);
1017 my $iw = $self->cget('-insertwidth');
1018 $self->{'ins_width_ins'} = $iw;
1019 $self->{'ins_width_ovr'} = int($length/8);
1020 }
1021  
1022 sub EditMenuItems {
1023 my $self = shift;
1024 return (
1025 ["command"=>'~Copy',
1026 -accelerator => 'CTRL+C',
1027 -command => [$self => 'clipboardCopy']
1028 ],
1029 ["command"=>'C~ut',
1030 -accelerator => 'CTRL+X',
1031 -command => [$self => 'clipboardCut']
1032 ],
1033 ["command"=>'~Paste',
1034 -accelerator => 'CTRL+V',
1035 -command => [$self => 'clipboardPaste']
1036 ],
1037 "-",
1038 ["command"=>'~Undo',
1039 -accelerator => 'CTRL+Z',
1040 -command => [$self => 'undo']
1041 ],
1042 ["command"=>'~Redo',
1043 -accelerator => 'CTRL+SHIFT+Z',
1044 -command => [$self => 'redo']
1045 ],
1046 "-",
1047 ["command"=>'C~omment',
1048 -accelerator => 'CTRL+G',
1049 -command => [$self => 'comment']
1050 ],
1051 ["command"=>'U~ncomment',
1052 -accelerator => 'CTRL+SHIFT+G',
1053 -command => [$self => 'uncomment']
1054 ],
1055 "-",
1056 ["command"=>'~Indent',
1057 -accelerator => 'CTRL+J',
1058 -command => [$self => 'indent']
1059 ],
1060 ["command"=>'Unin~dent',
1061 -accelerator => 'CTRL+SHIFT+J',
1062 -command => [$self => 'unindent']
1063 ],
1064 "-",
1065 ["command"=>'U~pper case',
1066 -accelerator => 'CTRL+U',
1067 -command => [$self => 'caseChange', 1]
1068 ],
1069 ["command"=>'~Lower case',
1070 -accelerator => 'ALT+U',
1071 -command => [$self => 'caseChange', 0]
1072 ],
1073 );
1074 }
1075  
1076 sub EmptyDocument { $_[0]->clear }
1077  
1078 sub EscapePressed {
1079 my $self = shift;
1080 if ($self->acPop->ismapped) {
1081 $self->acPopDown;
1082 } else {
1083 $self->unselectAll;
1084 $self->Callback('-escapepressed');
1085 }
1086 }
1087  
1088 sub FindAll {
1089 my ($self, $mode, $case, $pattern) = @_;
1090 $self->FindClear;
1091 my $search = $self->FindExpression($mode, $case, $pattern);
1092 return unless defined $search;
1093 my @all;
1094 for (1 .. $self->linenumber('end - 1c')) {
1095 my @hits = $self->FindInLine($_, $search);
1096 for (@hits) {
1097 push @all, $_;
1098 my ($begin, $end) = @$_;
1099 $self->tagAdd('Find', $begin, $end);
1100 $self->tagRaise('Find');
1101 $self->tagRaise('sel');
1102 }
1103 }
1104 my $hits = @all;
1105 $self->log("$hits hits");
1106 return @all
1107 }
1108  
1109 sub FindandReplace {
1110 my ($self, $mode, $case, $find, $replace) = @_;
1111 my $insert = $self->index('insert');
1112 my $line = $self->linenumber($insert);
1113 my $search = $self->FindExpression($mode, $case, $find);
1114 my @hits = $self->FindInLine($line, $search);
1115 for (@hits) {
1116 my ($begin, $end, $captures) = @$_;
1117 if ($begin eq $insert) {
1118 if (@$captures) {
1119 my $num = 1;
1120 for (@$captures) {
1121 my $cap = $_;
1122 $replace =~ s/\$$num/$cap/g;
1123 $num ++
1124 }
1125 }
1126 $self->replace($begin, $end, $replace);
1127 last;
1128 }
1129 }
1130 $self->FindNext('-forward', $mode, $case, $find);
1131 }
1132  
1133 sub FindandReplaceAll {
1134 my ($self, $mode, $case, $find, $replace) = @_;
1135 my @hits = $self->FindAll($mode, $case, $find);
1136 for (@hits) {
1137 my ($begin, $end, $captures) = @$_;
1138 my $repl = $replace;
1139 if (@$captures) {
1140 my $num = 1;
1141 for (@$captures) {
1142 my $cap = $_;
1143 $repl =~ s/\$$num/$cap/g;
1144 $num ++
1145 }
1146 }
1147 $self->replace($begin, $end, $repl);
1148 }
1149 $self->FindClear;
1150 }
1151  
1152 sub findandreplacepopup {
1153 my $self = shift;
1154 my $val = $_[0];
1155 my $call = $self->cget('-findandreplacecall');
1156 if (defined $call) {
1157 &$call(@_);
1158 } else {
1159 $self->SUPER::findandreplacepopup(@_)
1160 }
1161 }
1162  
1163 sub FindClear {
1164 my $self = shift;
1165 $self->tagRemove('Find', '1.0', 'end');
1166 }
1167  
1168 sub FindExpression {
1169 my ($self, $mode, $case, $search) = @_;
1170 my $srch;
1171 if ($mode eq '-exact') {
1172 $search = quotemeta($search)
1173 } else {
1174 eval "qr/$search/";
1175 my $error = $@;
1176 if ($error ne '') {
1177 $error =~ s/\n//;
1178 $self->log($error, 'error');
1179 return ()
1180 }
1181 }
1182 if ($case eq '-case') {
1183 $srch = qr/$search/
1184 } else {
1185 $srch = qr/$search/i
1186 }
1187 return $srch;
1188 }
1189  
1190 sub FindInLine {
1191 my ($self, $linenum, $search) = @_;
1192 my $line = $self->get("$linenum.0", "$linenum.0 lineend");
1193 my @hits = ();
1194 while ($line =~ /($search)/g) {
1195 my $end = pos($line);
1196 my $begin = $end - length($1);
1197 my @cap = ();
1198 if ($#- > 1) {
1199 no strict 'refs';
1200 @cap = map {$$_} 2 .. $#-;
1201 }
1202 push @hits, ["$linenum.$begin", "$linenum.$end", \@cap];
1203 }
1204 return @hits;
1205 }
1206  
1207 sub FindNext {
1208 my ($self, $direction, $mode, $case, $pattern, $first) = @_;
1209 # print "FindNext $direction, $mode, $case, $pattern\n";
1210 $first = 1 unless defined $first;
1211 my $search = $self->FindExpression($mode, $case, $pattern);
1212 return unless defined $search;
1213 # print "search $search\n";
1214 $self->FindClear;
1215 my $pos = $self->index('insert');
1216 my $start = $self->linenumber($pos);
1217 my $last = $self->linenumber('end - 1c');
1218 if ($direction eq '-forward') {
1219 for ($self->linenumber($pos) .. $last) {
1220 my $linenum = $_;
1221 my @hits = $self->FindInLine($linenum, $search);
1222 if (@hits) {
1223 # print "we have hits\n";
1224 while ((@hits) and ($self->compare($pos, '>=', $hits[0]->[0]))) { shift @hits }
1225 if (@hits) {
1226 my $hit = $hits[0];
1227 $self->FindTag($hit);
1228 $self->goTo($hit->[0]);
1229 return $hit->[0]
1230 }
1231 }
1232 }
1233 $self->markSet('insert', '1.0') if $first;
1234
1235 } else {
1236 for (reverse ('1.0' .. $self->linenumber($pos))) {
1237 my $linenum = $_;
1238 my @hits = $self->FindInLine($linenum, $search);
1239 if (@hits) {
1240 while ((@hits) and ($self->compare($pos, '<=', $hits[@hits - 1]->[0]))) { pop @hits }
1241 if (@hits) {
1242 my $hit = $hits[@hits - 1];
1243 $self->FindTag($hit);
1244 $self->goTo($hit->[0]);
1245 return $hit->[0]
1246 }
1247 }
1248 }
1249 $self->markSet('insert', 'end - 1c') if $first;
1250 }
1251 #retry from begin or end of document
1252 if ($first) {
1253 my $hit = $self->FindNext($direction, $mode, $case, $pattern, 0);
1254 return $hit if defined $hit;
1255 $self->markSet('insert', $pos);
1256 }
1257 return undef
1258 }
1259  
1260 sub findoptions {
1261 my $self = shift;
1262 if (my $o = shift) {
1263 my @op = ();
1264 if (ref($o)) {
1265 @op = @$o;
1266 } else {
1267 @op = split(/\s+/, $o);
1268 }
1269 $self->tagConfigure('Find', @op);
1270 }
1271 }
1272  
1273 sub FindTag {
1274 my ($self, $hit) = @_;
1275 my ($begin, $end) = @$hit;
1276 $self->tagAdd('Find', $begin, $end);
1277 $self->tagRaise('Find');
1278 $self->tagRaise('sel');
1279 }
1280  
1281 sub FindValidateReg {
1282 my ($self, $regexp) = @_;
1283 eval "qr/$regexp/";
1284 my $error = $@;
1285 if ($error) {
1286 $error =~ s/\n//;
1287 $self->log($error, 'error');
1288 return ''
1289 }
1290 return 1
1291 }
1292  
1293 sub Flush {
1294 my $self = shift;
1295 my $buf = $self->Buffer;
1296 my $rbuf = $self->BufferReplace;
1297 if ($buf ne '') {
1298 my $mode = $self->BufferMode;
1299 my $start = $self->BufferStart;
1300 my $bmod = $self->BufferModified;
1301 if ($mode eq 'backspace') {
1302 $self->PushUndoRaw('delete', $bmod, $start, $buf);
1303 } elsif ($mode eq 'replace') {
1304 $self->PushUndoRaw($mode, $bmod, $start, $buf, $rbuf);
1305 } else {
1306 $self->PushUndoRaw($mode, $bmod, $start, $buf);
1307 }
1308 $self->Buffer('');
1309 $self->BufferReplace('');
1310 $self->BufferMode('');
1311 $self->BufferModified($self->editModified);
1312 $self->BufferStart($self->index('insert'));
1313 }
1314 }
1315  
1316 my %flushkeys = (
1317 "\t" => 1,
1318 "\n" => 1,
1319 " " => 1
1320 );
1321  
1322 sub FlushConditional {
1323 my ($self, $pos, $key) = @_;
1324 $pos = $self->index($pos);
1325 my $mode = $self->BufferMode;
1326 my $start = $self->BufferStart;
1327 my $len = length($self->Buffer);
1328 my $icmoved = 0;
1329 if ($mode eq 'backspace') {
1330 $icmoved = ($start ne $pos);
1331 } elsif ($mode eq 'delete') {
1332 $icmoved = ($start ne $pos)
1333 } elsif ($mode eq 'insert') {
1334 $icmoved = ($self->index("$start + $len chars") ne $pos)
1335 } elsif ($mode eq 'replace') {
1336 $len = length($self->BufferReplace);
1337 $icmoved = ($self->index("$start + $len chars") ne $pos)
1338 } else {
1339 carp "illegal buffermode '$mode'\n";
1340 }
1341 if ((exists $flushkeys{$key}) or ($icmoved)) {
1342 $self->Flush;
1343 $self->BufferMode($mode);
1344 return 1
1345 }
1346 return 0
1347 }
1348  
1349 =item B<getFontInfo>
1350  
1351 =cut
1352  
1353 sub getFontInfo {
1354 my $self = shift;
1355 my $f = $self->cget('-font');
1356 my %inf = ();
1357 my @opt = qw(-family -size -weight -slant -underline -overstrike);
1358 for (@opt) {
1359 $inf{$_} = $self->fontActual($f, $_)
1360 }
1361 return \%inf
1362 }
1363  
1364 =item B<goTo>
1365  
1366 =cut
1367  
1368 sub goTo {
1369 my ($self, $pos) = @_;
1370 $self->SetCursor($pos);
1371 }
1372  
1373 sub HomeEndKey {
1374 my ($self, $flag) = @_;
1375 my $index = $self->index('insert');
1376 my $text = $self->get("$index linestart", "$index lineend");
1377 $index =~ /^(\d+)\.(\d+)/;
1378 my $pos = $2;
1379 my $line = $1;
1380 my $spaces = '';
1381 if ($text =~ /^(\s+)/) { $spaces = $1 }
1382 $spaces = length($spaces);
1383 if ($spaces) {
1384 if ($flag) { #End key = 1, Home key = 0
1385 if ($pos < $spaces) {
1386 $self->SetCursor("$line.$spaces");
1387 return
1388 }
1389 } else {
1390 if ($pos > $spaces) {
1391 $self->SetCursor("$line.$spaces");
1392 return
1393 }
1394 }
1395 }
1396 if ($flag) {
1397 $self->SetCursor("$index lineend");
1398 } else {
1399 $self->SetCursor("$index linestart");
1400 }
1401 }
1402  
1403 =item B<indent>
1404  
1405 =cut
1406  
1407 sub indent {
1408 my $self = shift;
1409 return if $self->cget('-readonly');
1410 my $ichar = $self->indentString;
1411 if ($self->selectionExists) {
1412 $self->selectionModify($ichar, 0, 'Indented');
1413 } else {
1414 my $begin = $self->index('insert linestart');
1415 my $old = $self->get($begin, "$begin lineend");
1416 my $new = $ichar . $old;
1417 $self->RecordUndo('replace', $self->editModified, $begin, $old, $new);
1418 $self->SUPER::insert($begin, $ichar);
1419 $self->modifiedCall($begin);
1420 }
1421 }
1422  
1423 sub indentString {
1424 my $self = shift;
1425 my $style = $self->cget('-indentstyle');
1426 my $ichar = '';
1427 if ($style eq 'tab') {
1428 $ichar = "\t";
1429 } else {
1430 my $str = '';
1431 for (0 .. $style) {
1432 $ichar = $ichar . ' '
1433 }
1434 }
1435 return $ichar;
1436 }
1437  
1438 sub insert {
1439 my ($self, $pos, $string) = @_;
1440 $pos = $self->index($pos);
1441 $self->RecordUndo('insert', $self->editModified,$pos, $string);
1442 $self->SUPER::insert($pos, $string);
1443 $self->modifiedCall($pos);
1444 }
1445  
1446 sub Insert {
1447 my ($self, $string) = @_;
1448 return if $self->cget('-readonly');
1449 if (($string eq "\n") and ($self->cget('-autoindent'))) {
1450 my $ins = $self->index('insert');
1451 my $i = $self->index('insert linestart');
1452 my $s = $self->get($i, "$i lineend");
1453 if ($s =~ /^(\s+)/) {
1454 my $tabs = $1;
1455 my $lt = length($tabs);
1456 my ($inxl, $insp) = split /\./, $ins;
1457 $tabs = substr($tabs, 0, $insp);
1458 $string = "$string$tabs";
1459 }
1460 }
1461 $self->SUPER::Insert($string);
1462 }
1463  
1464 my %brackets = (
1465 '(' => ')',
1466 '{' => '}',
1467 '[' => ']',
1468 '"' => '"',
1469 "'" => "'",
1470 );
1471  
1472 sub InsertKeypress {
1473 my ($self, $char) = @_;
1474 return unless length($char);
1475 return if $self->cget('-readonly');
1476 my $index = $self->index('insert');
1477  
1478 my $ab = $self->cget('-autobrackets');
1479 my $ex = exists $brackets{$char};
1480 my $dobrackets = ((exists $brackets{$char}) and ($self->cget('-autobrackets')));
1481 $char = $char . $brackets{$char} if $dobrackets;
1482  
1483 if ($self->OverstrikeMode) {
1484 my $current = $self->get('insert');
1485 $current = '' if $current eq "\n";
1486 $self->RecordUndo('replace', $self->editModified, $index, $current, $char);
1487 $self->SUPER::delete($index) unless $current eq '';
1488 $self->SUPER::insert($index, $char);
1489 $self->modifiedCall($index);
1490 } else {
1491 $self->Insert($char);
1492 }
1493 $self->goTo($self->index('insert - 1c')) if $dobrackets;
1494 }
1495  
1496 sub insertTab {
1497 my $self = shift;
1498 return if $self->cget('-readonly');
1499 if ($self->selectionExists) {
1500 $self->indent;
1501 } else {
1502 $self->SUPER::insertTab;
1503 }
1504 }
1505  
1506 sub KeyReleased {
1507 my ($self, $key) = @_;
1508 $self->matchCheck;
1509 $self->activate($key);
1510 $self->Callback('-keyreleasecall', $key);
1511 }
1512  
1513 =item B<linenumber>
1514  
1515 =cut
1516  
1517 sub linenumber {
1518 my ($self, $index) = @_;
1519 $index = 'insert' unless defined $index;
1520 my $id = $self->index($index);
1521 my ($line, $pos ) = split(/\./, $id);
1522 return $line;
1523 }
1524  
1525 =item B<load>
1526  
1527 =cut
1528  
1529 sub load {
1530 my ($self, $file) = @_;
1531  
1532 unless (open INFILE, '<', $file) {
1533 warn "cannot open $file";
1534 return 0
1535 };
1536 $self->clear;
1537 while (my $line = <INFILE>) {
1538 $self->SUPER::insert('end', $line);
1539 }
1540 close INFILE;
1541 $self->goTo('1.0');
1542 $self->editModified(0);
1543 $self->modifiedCall('1.0');
1544 $self->log("Loaded $file");
1545 return 1
1546 }
1547  
1548 =item B<log>
1549  
1550 =cut
1551  
1552 sub log {
1553 my $self = shift;
1554 $self->Callback('-logcall', @_);
1555 }
1556  
1557 sub matchCheck {
1558 my $self = shift;
1559 my $c = $self->get('insert - 1 chars', 'insert');
1560 my $p = $self->index('match');
1561 if ($p ne '0.0') {
1562 $self->tagRemove('Match', $p, "$p + 1 chars");
1563 $self->markSet('match', '0.0');
1564 }
1565 if ($c) {
1566 my $v = $self->cget('-match');
1567 my $p = index($v, $c);
1568 if ($p ne -1) { #a character in '-match' has been detected.
1569 my $count = 0;
1570 my $found = 0;
1571 if ($p % 2) {
1572 my $m = substr($v, $p - 1, 1);
1573 $self->matchFind('-backwards', $c, $m,
1574 $self->index('insert - 1 chars'),
1575 $self->index('@0,0'),
1576 );
1577 } else {
1578 my $m = substr($v, $p + 1, 1);
1579 $self->matchFind('-forwards', $c, $m,
1580 $self->index('insert'),
1581 $self->index($self->visualEnd . '.0 lineend'),
1582 );
1583 }
1584 }
1585 }
1586 }
1587  
1588 sub matchFind {
1589 my ($self, $dir, $char, $ochar, $start, $stop) = @_;
1590 #first of all remove a previous match highlight;
1591 my $pattern = "\\$char|\\$ochar";
1592 my $found = 0;
1593 my $count = 0;
1594 while ((not $found) and (my $i = $self->search(
1595 $dir, '-regexp', '-nocase', '--', $pattern, $start, $stop
1596 ))) {
1597 my $k = $self->get($i, "$i + 1 chars");
1598 if ($k eq $ochar) {
1599 if ($count > 0) {
1600 $count--;
1601 if ($dir eq '-forwards') {
1602 $start = $self->index("$i + 1 chars");
1603 } else {
1604 $start = $i;
1605 }
1606 } else {
1607 $self->markSet('match', $i);
1608 $self->tagAdd('Match', $i, "$i + 1 chars");
1609 $self->tagRaise('Match');
1610 $found = 1;
1611 }
1612 } elsif ($k eq $char) {
1613 $count++;
1614 if ($dir eq '-forwards') {
1615 $start = $self->index("$i + 1 chars");
1616 } else {
1617 $start = $i;
1618 }
1619 } elsif ($i eq $start) {
1620 $found = 1;
1621 }
1622 }
1623 }
1624  
1625 sub matchoptions {
1626 my $self = shift;
1627 if (my $o = shift) {
1628 my @op = ();
1629 if (ref($o)) {
1630 @op = @$o;
1631 } else {
1632 @op = split(/\s+/, $o);
1633 }
1634 $self->tagConfigure('Match', @op);
1635 }
1636 }
1637  
1638 sub modifiedCall {
1639 my ($self, $index) = @_;
1640 $self->Callback('-modifycall', $index);
1641 $self->acScanStart;
1642 }
1643  
1644 sub OverstrikeMode {
1645 my ($self, $mode) = @_;
1646 $self->{'OVERSTRIKE_MODE'} = 0 unless exists $self->{'OVERSTRIKE_MODE'};
1647 if (defined $mode) {
1648 if ($mode) {
1649 $self->configure('-insertwidth', $self->{'ins_width_ovr'})
1650 } else {
1651 my $iw = $self->{'ins_width_ins'};
1652 $iw = 2 unless defined $iw;
1653 $self->configure('-insertwidth', $iw)
1654 }
1655 $self->{'OVERSTRIKE_MODE'} = $mode;
1656 }
1657 return $self->{'OVERSTRIKE_MODE'};
1658 }
1659  
1660 =item B<patchdiff>I($patch, ?$style?)
1661  
1662 =cut
1663  
1664 sub patchdiff {
1665 my ($self, $patch, $style) = @_;
1666 $style = 'Unified' unless defined $style;
1667 my $type = ref $patch;
1668 if ($type eq 'SCALAR') {
1669 $patch = $$patch
1670 } else {
1671 my $file = $patch;
1672 $patch = '';
1673 if (open my $fh, "<$file") {
1674 while (my $line = <$fh>) {
1675 $patch = $patch . $line
1676 }
1677 close $fh
1678 } else {
1679 $self->log("Cannot open patch file $file", 'error');
1680 return
1681 }
1682 }
1683 my $old = $self->get('0.0', 'end - 1c');
1684 my $new = patch( $old, $patch, { STYLE => $style } );
1685 my $modified = $self->editModified;
1686 $self->SUPER::delete('0.0', 'end');
1687 $self->SUPER::insert('0.0', $new, $old);
1688 $self->RecordUndo('replace', $modified, '0.0', $old, $new);
1689 $self->modifiedCall('0.0');
1690 $self->log("Patch applied");
1691 }
1692  
1693 sub PostPopupMenu {
1694 my ($self, $x, $y) = @_;
1695 my $menu = $self->cget('-contextmenu');
1696 unless (defined $menu) {
1697 my $items = $self->cget('-menuitems');
1698 return unless defined $items;
1699 $menu = $self->Menu(
1700 -tearoff => 0,
1701 -menuitems => $items,
1702 );
1703 $menu->bind('<Leave>', [$menu, 'unpost']);
1704 $self->configure(-contextmenu => $menu);
1705 }
1706 $menu->post($x - 2, $y - 2);
1707 }
1708  
1709 sub PullUndo {
1710 my $self = shift;
1711 my $stack = $self->UndoStack;
1712 return shift(@$stack);
1713 }
1714  
1715 sub PullRedo {
1716 my $self = shift;
1717 my $stack = $self->RedoStack;
1718 return shift(@$stack);
1719 }
1720  
1721 sub PushUndo {
1722 my $self = shift;
1723 my $stack = $self->UndoStack;
1724 unshift(@$stack, @_);
1725 }
1726  
1727 sub PushUndoRaw {
1728 my ($self, $mode, $modified, @content) = @_;
1729 my %undo = (
1730 content => \@content,
1731 mode => $mode,
1732 modified => $modified,
1733 );
1734  
1735 my @ranges = $self->tagRanges('sel');
1736 $undo{'selection'} = \@ranges if (@ranges eq 2);
1737  
1738 $self->PushUndo(\%undo);
1739 }
1740  
1741 sub PushRedo {
1742 my $self = shift;
1743 my $stack = $self->RedoStack;
1744 unshift(@$stack, @_);
1745 }
1746  
1747 sub RecordUndo {
1748 my ($self, $mode, $modified, @content) = @_;
1749
1750 $self->ResetRedo;
1751  
1752 if ($mode eq 'backspace') {
1753 if ($self->selectionExists) {
1754 $self->Flush;
1755 my @ranges = $self->tagRanges('sel');
1756 my $text = $self->get(@ranges);
1757 $self->PushUndoRaw('delete', $modified, $ranges[0], $text);
1758 } else {
1759 my $bufmode = $self->BufferMode;
1760 $self->Flush if $mode ne $bufmode;
1761 $self->BufferMode($mode);
1762  
1763  
1764 my $end = $self->index('insert');
1765 my $begin = $self->index("$end - 1c");
1766 my $char = $self->get($begin, $end);
1767 if ($char ne '') {
1768 $self->FlushConditional($end, $char);
1769 my $buf = $self->Buffer;
1770 $buf = "$char$buf";
1771 $self->Buffer($buf);
1772 $self->BufferStart($begin);
1773 }
1774 }
1775 } elsif (($mode eq 'delete') or ($mode eq 'insert')) {
1776 my ($pos, $text) = @content;
1777 $pos = $self->index($pos);
1778  
1779 if (length($text) > 1) {
1780 $self->Flush;
1781 $self->PushUndoRaw($mode, $modified, $pos, $text);
1782 } else {
1783 my $bufmode = $self->BufferMode;
1784 $self->Flush if $mode ne $bufmode;
1785 $self->BufferMode($mode);
1786  
1787  
1788 $self->BufferStart($pos) if $self->FlushConditional($pos, $text);
1789 my $buf = $self->Buffer;
1790 $buf = "$buf$text";
1791 $self->Buffer($buf);
1792 }
1793 } elsif ($mode eq 'replace') {
1794 my ($pos, $old, $new) = @content;
1795 $pos = $self->index($pos);
1796  
1797 if ((length($new) > 1) or ($self->selectionExists)) {
1798 $self->Flush;
1799 $self->PushUndoRaw($mode, $modified, $pos, $old, $new);
1800 } else {
1801 my $bufmode = $self->BufferMode;
1802 $self->Flush if $mode ne $bufmode;
1803 $self->BufferMode($mode);
1804  
1805 $self->BufferStart($pos) if $self->FlushConditional($pos, $new);
1806 my $buf = $self->Buffer;
1807 my $rbuf = $self->BufferReplace;
1808 $buf = "$buf$old";
1809 $rbuf = "$rbuf$new";
1810 $self->Buffer($buf);
1811 $self->BufferReplace($rbuf);
1812 }
1813 }
1814 }
1815  
1816 =item B<redo>
1817  
1818 =cut
1819  
1820 sub redo {
1821 my $self = shift;
1822 return if $self->cget('-readonly');
1823 $self->Flush;
1824 if ($self->canRedo) {
1825 my $o = $self->PullRedo;
1826 $self->PushUndo($o);
1827 my $mod = $self->editModified;
1828  
1829 my $mode = $o->{'mode'};
1830 if ($mode eq 'insert') {
1831 my $content = $o->{'content'};
1832 my ($pos, $text) = @$content;
1833 my $len = length($text);
1834 $self->SUPER::insert($pos, $text);
1835 $self->markSet('insert', $self->index("$pos + $len chars"));
1836 $self->see('insert');
1837 } elsif ($mode eq 'delete') {
1838 my $content = $o->{'content'};
1839 my ($pos, $text) = @$content;
1840 my $len = length($text);
1841 $self->SUPER::delete($pos, "$pos + $len chars");
1842 $self->markSet('insert', $pos);
1843 $self->see('insert');
1844 } elsif ($mode eq 'replace') {
1845 my $content = $o->{'content'};
1846 my ($pos, $old, $new) = @$content;
1847 my $len = length($old);
1848 $self->SUPER::delete($pos, "$pos + $len chars");
1849 $self->SUPER::insert($pos, $new);
1850 my $lnew = length($new);
1851 $self->markSet('insert', "$pos + $lnew chars");
1852 $self->see('insert');
1853 } else {
1854 carp "invalid redo mode $mode, should be 'delete', 'insert', or 'replace'\n";
1855 }
1856 $self->editModified($o->{'redo_modified'});
1857 $self->BufferModified($o->{'redo_modified'});
1858 $o->{'modified'} = $mod;
1859 if (my $sel = $o->{'selection'}) {
1860 $self->unselectAll;
1861 $self->tagAdd('sel',@$sel);
1862 }
1863 my $pos = $o->{'content'}->[0];
1864 $self->modifiedCall($pos);
1865 }
1866 }
1867  
1868 sub RedoStack {
1869 return $_[0]->{REDOSTACK}
1870 }
1871  
1872 =item B<replace>I<$begin, $end, $string)>
1873  
1874 =cut
1875  
1876 sub replace {
1877 my ($self, $begin, $end, $replace) = @_;
1878 my $orig = $self->get($begin, $end);
1879 my $modified = $self->editModified;
1880 $self->SUPER::delete($begin, $end);
1881 $self->SUPER::insert($begin, $replace);
1882 $self->RecordUndo('replace', $modified, $begin, $orig, $replace);
1883 $self->modifiedCall($begin);
1884 }
1885  
1886 sub ReplaceSelectionsWith {
1887 my ($self,$new_text ) = @_;
1888  
1889 my @ranges = $self->tagRanges('sel');
1890 my $range_total = @ranges;
1891  
1892 # if nothing selected, then ignore
1893 if ($range_total == 0) {return};
1894  
1895 # insert marks where selections are located
1896 # marks will move with text even as text is inserted and deleted
1897 # in a previous selection.
1898 for (my $i=0; $i<$range_total; $i++) {
1899 $self->markSet('mark_sel_'.$i => $ranges[$i]);
1900 }
1901  
1902 # for every selected mark pair, insert new text and delete old text
1903 my ($first, $last);
1904 for (my $i=0; $i<$range_total; $i=$i+2) {
1905 $first = $self->index('mark_sel_'.$i);
1906 $last = $self->index('mark_sel_'.($i+1));
1907  
1908 my $old = $self->get($first, $last);
1909 $self->RecordUndo('replace', $self->editModified, $first, $old, $new_text);
1910 $self->SUPER::insert($last, $new_text);
1911 $self->SUPER::delete($first, $last);
1912 $self->modifiedCall($first);
1913  
1914 }
1915 ############################################################
1916 # set the insert cursor to the end of the last insertion mark
1917 $self->markSet('insert',$self->index('mark_sel_'.($range_total-1)));
1918  
1919 # delete the marks
1920 for (my $i=0; $i<$range_total; $i++) {
1921 $self->markUnset('mark_sel_'.$i);
1922 }
1923 }
1924  
1925 sub ResetRedo {
1926 $_[0]->{REDOSTACK} = [];
1927 }
1928  
1929 sub ResetUndo {
1930 $_[0]->{UNDOSTACK} = [];
1931 }
1932  
1933 sub returnPressed {
1934 my $self = shift;
1935 my $pop = $self->acPop;
1936 if ($pop->ismapped) {
1937 $self->acSelect
1938 } else {
1939 $self->Insert("\n");
1940 }
1941 }
1942  
1943 =item B<save>
1944  
1945 =cut
1946  
1947 sub save {
1948 my ($self, $file) = @_;
1949 if ($self->saveExport($file)) {
1950 $self->clearModified;
1951 $self->log("Saved $file");
1952 return 1
1953 }
1954 return 0
1955 }
1956  
1957 =item B<saveExport>I<($file)>
1958  
1959 =cut
1960  
1961 sub saveExport {
1962 my ($self, $file) = @_;
1963  
1964 unless (open OUTFILE, '>', $file) {
1965 warn "cannot open $file";
1966 return 0
1967 };
1968 my $last = $self->linenumber('end - 1c');
1969 my $end = $self->index('end - 1c');
1970 for (1 .. $last) {
1971 my $linestart = "$_.0";
1972  
1973 my $lineend = $self->index("$linestart lineend + 1c");
1974 $lineend = $end if $self->compare($end,'<',$lineend);
1975  
1976 my $line = $self->get($linestart, $lineend);
1977 print OUTFILE $line;
1978 }
1979 close OUTFILE;
1980 return 1
1981 }
1982  
1983 sub see {
1984 my $self = shift;
1985 $self->SUPER::see(@_);
1986 $self->Callback('-yscrollcmd', @_);
1987 }
1988  
1989 #fix for selectAll of Tk::Text.
1990 sub selectAll {
1991 my $self = shift;
1992 # $self->tagAdd('sel','1.0','end');
1993 $self->tagAdd('sel','1.0','end - 1c');
1994 }
1995  
1996 =item B<selectionExists>
1997  
1998 =cut
1999  
2000 sub selectionExists {
2001 my $self = shift;
2002 my @ranges = $self->tagRanges('sel');
2003 return @ranges > 1
2004 }
2005  
2006 sub selectionModify {
2007 my ($self, $char, $mode, $operation) = @_;
2008 my @ranges = $self->tagRanges('sel');
2009 my $start = $ranges[0];
2010 my $end = $self->index($ranges[1]);
2011 my $len = length($char);
2012 my $old = $self->get(@ranges);
2013 my $modified = $self->editModified;
2014 while ($self->compare($start, "<", $end)) {
2015 if ($mode) {
2016 if ($self->get("$start linestart", "$start linestart + $len chars") eq $char) {
2017 $self->SUPER::delete("$start linestart", "$start linestart + $len chars");
2018 }
2019 } else {
2020 $self->SUPER::insert("$start linestart", $char)
2021 }
2022 $start = $self->index("$start + 1 lines");
2023 }
2024 $self->unselectAll;
2025 $self->tagAdd('sel', @ranges);
2026 my $new = $self->get(@ranges);
2027 $self->RecordUndo('replace', $modified, $ranges[0], $old, $new);
2028 $self->modifiedCall($ranges[0]);
2029 my $lines = $self->linenumber($ranges[1]) - $self->linenumber($ranges[0]);
2030 $self->log("$operation $lines lines");
2031 }
2032  
2033  
2034 =item B<uncomment>
2035  
2036 =cut
2037  
2038 sub uncomment {
2039 my $self = shift;
2040 return if $self->cget('-readonly');
2041 my $slstart = $self->cget('-slcomment');
2042 my $mlstart = $self->cget('-mlcommentstart');
2043 my $mlend = $self->cget('-mlcommentend');
2044 my $lend = length($mlend) if defined $mlend;
2045 my $lstart = length($mlstart) if defined $mlstart;
2046 my $modified = $self->editModified;
2047 if ($self->CommentType eq 'multi') {
2048 my ($rb, $re) = $self->tagRanges('sel');
2049 $rb = $self->index("$rb linestart");
2050 $re = $self->index("$re lineend");
2051 my $old = $self->get($rb, $re);
2052 if ((defined $mlstart) and(defined $mlend)) {
2053 if (($old =~ /^$mlstart/) and ($old =~ /$mlend$/)){
2054 $self->SUPER::delete($rb, "$rb + $lstart chars");
2055 $self->SUPER::delete("$re - $lend chars", $re);
2056 my $new = $self->get($rb, $re);
2057 $self->unselectAll;
2058 $self->tagAdd('sel', $rb, $re);
2059 $self->RecordUndo('replace', $modified, $rb, $old, $new);
2060 $self->modifiedCall($rb);
2061 my $lines = $self->linenumber($re) - $self->linenumber($rb);
2062 $self->log("Uncommented $lines lines");
2063 }
2064 } elsif (defined $slstart) {
2065 $self->selectionModify($slstart, 1, 'Uncommented')
2066 }
2067 } else {
2068 my $rb = $self->index('insert linestart');
2069 my $re = $self->index('insert lineend');
2070 my $old = $self->get($rb, $re);
2071 if (defined($slstart)) {
2072 $lstart = length $slstart;
2073 my $linebegin = substr($old, 0, $lstart);
2074 $self->SUPER::delete($rb, "$rb + $lstart chars") if $linebegin eq $slstart;
2075 } elsif ((defined $mlstart) and(defined $mlend)) {
2076 my $s = quotemeta($mlstart);
2077 my $e = quotemeta($mlend);
2078 if (($old =~ /^$s/) and ($old =~ /$e$/)){
2079 $self->SUPER::delete('insert linestart', "insert linestart + $lstart chars");
2080 $self->SUPER::delete( "insert lineend - $lend chars", 'insert lineend');
2081 }
2082 }
2083 my $new = $self->get($rb, "$rb lineend");
2084 $self->RecordUndo('replace', $modified, $rb, $old, $new);
2085 $self->modifiedCall($rb);
2086 }
2087 }
2088  
2089 =item B<undo>
2090  
2091 =cut
2092  
2093 sub undo {
2094 my $self = shift;
2095 return if $self->cget('-readonly');
2096 if ($self->canUndo) {
2097 $self->Flush;
2098  
2099 my $o = $self->PullUndo;
2100 my $mod = $self->editModified;
2101 $self->PushRedo($o);
2102  
2103 my $mode = $o->{'mode'};
2104 if ($mode eq 'delete') {
2105 my $content = $o->{'content'};
2106 my ($pos, $text) = @$content;
2107 my $len = length($text);
2108 $self->SUPER::insert($pos, $text);
2109 $self->markSet('insert', $self->index("$pos + $len chars"));
2110 $self->see('insert');
2111 } elsif ($mode eq 'insert') {
2112 my $content = $o->{'content'};
2113 my ($pos, $text) = @$content;
2114 my $len = length($text);
2115 $self->SUPER::delete($pos, "$pos + $len chars");
2116 $self->markSet('insert', $pos);
2117 $self->see('insert');
2118 } elsif ($mode eq 'replace') {
2119 my $content = $o->{'content'};
2120 my ($pos, $old, $new) = @$content;
2121 my $len = length($new);
2122 $self->SUPER::delete($pos, "$pos + $len chars");
2123 $self->SUPER::insert($pos, $old);
2124 my $lold = length($old);
2125 $self->markSet('insert', $self->index("$pos + $lold chars"));
2126 $self->see('insert');
2127 } else {
2128 carp "invalid undo mode '$mode"."', should be 'delete', 'insert', or 'replace'\n";
2129 }
2130 $self->editModified($o->{'modified'});
2131 $self->BufferModified($o->{'modified'});
2132 $o->{'redo_modified'} = $mod;
2133 if (my $sel = $o->{'selection'}) {
2134 $self->unselectAll;
2135 $self->tagAdd('sel',@$sel);
2136 }
2137 my $pos = $o->{'content'}->[0];
2138 $self->modifiedCall($pos);
2139 }
2140 }
2141  
2142 sub UndoStack {
2143 return $_[0]->{UNDOSTACK}
2144 }
2145  
2146 =item B<unindent>
2147  
2148 =cut
2149  
2150 sub unindent {
2151 my $self = shift;
2152 return if $self->cget('-readonly');
2153 my $ichar = $self->indentString;
2154 if ($self->selectionExists) {
2155 $self->selectionModify($ichar, 1, 'Unindent');
2156 } else {
2157 my $modified = $self->editModified;
2158 my $index = $self->index('insert');
2159 my $start = $self->index('insert linestart');
2160 my $len = length($ichar);
2161 my $string = $self->get($start, $index);
2162 my $old = $self->get($start, "$start lineend");
2163 if ($string =~ /^$ichar/) {
2164 $self->SUPER::delete($start, "$start + $len" . "c");
2165 }
2166 my $new = $self->get($start, "$start lineend");
2167 $self->RecordUndo('replace', $modified, $start, $old, $new);
2168 $self->modifiedCall($start);
2169 }
2170 }
2171  
2172 =item B<visualBegin>
2173  
2174 =cut
2175  
2176 sub visualBegin {
2177 my $self = shift;
2178 return $self->linenumber('@0,0');
2179 }
2180  
2181 =item B<visualEnd>
2182  
2183 =cut
2184  
2185 sub visualEnd {
2186 my $self = shift;
2187 my $height = $self->height;
2188 return $self->linenumber('@0,' . $height);
2189 }
2190  
2191 sub xview {
2192 my $self = shift;
2193 $self->SUPER::xview(@_);
2194 $self->acGeometry;
2195 }
2196  
2197 sub yview {
2198 my $self = shift;
2199 my $r = $self->SUPER::yview(@_);
2200 $self->Callback('-yscrollcmd', @_);
2201 $self->acGeometry;
2202 }
2203  
2204 =back
2205  
2206 =head1 AUTHOR
2207  
2208 =over 4
2209  
2210 =item Hans Jeuken (hanje at cpan dot org)
2211  
2212 =back
2213  
2214 =cut
2215  
2216 =head1 BUGS
2217  
2218 Unknown. If you find any, please contact the author.
2219  
2220 =cut
2221  
2222 =head1 TODO
2223  
2224 =over 4
2225  
2226  
2227 =back
2228  
2229 =cut
2230  
2231 =head1 SEE ALSO
2232  
2233 =over 4
2234  
2235 =item L<Tk::Text>
2236  
2237 =back
2238  
2239 =cut
2240  
2241 1;
2242  
2243 __END__