blib/lib/Tk/Text/SuperText.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Tk::Text::SuperText;
2
3 2 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 0 ▶use Tk 804.030 qw(Ev);
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use Tk::Text;
6 use Tk::Derived;
7
8
9 #+20010117 JWT TextANSIColor support
10 my $ansicolor = 0;
11 eval 'use Term::ANSIColor; 1' or $ansicolor = -1;
12 #+
13
14 use Carp;
15 use strict;
16 use warnings;
17 use vars qw($VERSION @ISA @EXPORT);
18
19 @EXPORT = qw(
20 mouseSetInsert mouseSelect mouseSelectWord mouseSelectLine mouseSelectAdd mouseSelectChar
21 mouseSelectAddWord mouseSelectAddLine mouseSelectAutoScan mouseSelectAutoScanStop
22 mouseMoveInsert mouseRectSelection mouseMovePageTo mouseMovePage mousePasteSelection
23 moveLeft selectLeft selectRectLeft moveLeftWord selectLeftWord
24 moveRight selectRight selectRectRight moveRightWord selectRightWord moveUp selectUp
25 selectRectUp moveUpParagraph selectUpParagraph moveDown selectDown selectRectDown
26 moveDownParagraph selectDownParagraph moveLineStart selectToLineStart moveTextStart
27 selectToTextStart moveLineEnd selectToLineEnd moveTextEnd selectToTextEnd movePageUp
28 selectToPageUp movePageLeft movePageDown selectToPageDown movePageRight
29 setSelectionMark selectToMark selectAll selectionShiftLeft selectionShiftLeftTab
30 selectionShiftRight selectionShiftRightTab ins enter autoIndentEnter
31 noAutoIndentEnter del backSpace deleteToWordStart deleteToWordEnd deleteToLineStart
32 deleteToLineEnd deleteWord deleteLine insertControlCode focusNext focusPrev
33 flashMatchingChar removeMatch findMatchingChar jumpToMatchingChar escape tab
34 leftTab copy cut paste inlinePaste undo redo destroy keyPress menuSelect noOP
35 );
36
37 $VERSION = '0.13';
38 @ISA = qw(Tk::Derived Tk::Text Exporter);
39
40 use base qw(Tk::Text);
41
42 Construct Tk::Widget 'SuperText';
43
44 my (%fgcolors, %bgcolors, $clear, $code_bold, $code_uline, @colors);
45
46 #+20010117 JWT TextANSIColor support
47 unless ($ansicolor == -1)
48 {
49 $clear = color('clear'); # Code to reset control codes
50 $code_bold = color('bold');
51 $code_uline= color('underline');
52 @colors = qw/black red green yellow blue magenta cyan white/;
53 for (@colors)
54 {
55 my $fg = color($_);
56 my $bg = color("on_$_");
57
58 $fgcolors{$fg} = "ANSIfg$_";
59 $bgcolors{$bg} = "ANSIbg$_";
60 }
61 }
62 #+
63
64 # returns an hash with the default events and key binds
65 sub DefaultEvents {
66 my (%events);
67
68 %events = (
69 'MouseSetInsert' => ['<1>'],
70 'MouseSelect' => ['<B1-Motion>'],
71 'MouseSelectWord' => ['<Double-1>'],
72 'MouseSelectLine' => ['<Triple-1>'],
73 'MouseSelectChar' => ['<ButtonRelease-3>'], #ADDED 1999/07 by JWT TO CAUSE RIGHT BUTTON TO EXTEND SELECT!
74 'MouseSelectAdd' => ['<Shift-1>'],
75 'MouseSelectAddWord' => ['<Double-Shift-1>'],
76 'MouseSelectAddLine' => ['<Triple-Shift-1>'],
77 'MouseSelectAutoScan' => ['<B1-Leave>'],
78 'MouseSelectAutoScanStop' => ['<B1-Enter>','<ButtonRelease-1>'],
79 'MouseMoveInsert' => ['<Alt-1>'],
80 'MouseRectSelection' => ['<Control-B1-Motion>'],
81 'MouseMovePageTo' => ['<2>'],
82 'MouseMovePage' => ['<B2-Motion>'],
83 'MousePasteSelection' => ['<ButtonRelease-2>'],
84
85 'MoveLeft' => ['<Left>'],
86 'SelectLeft' => ['<Shift-Left>'],
87 'SelectRectLeft' => ['<Shift-Alt-Left>'],
88 'MoveLeftWord' => ['<Control-Left>'],
89 'SelectLeftWord' => ['<Shift-Control-Left>'],
90 'MoveRight' => ['<Right>'],
91 'SelectRight' => ['<Shift-Right>'],
92 'SelectRectRight' => ['<Shift-Alt-Right>'],
93 'MoveRightWord' => ['<Control-Right>'],
94 'SelectRightWord' => ['<Shift-Control-Right>'],
95 'MoveUp' => ['<Up>'],
96 'SelectUp' => ['<Shift-Up>'],
97 'SelectRectUp' => ['<Shift-Alt-Up>'],
98 'MoveUpParagraph' => ['<Control-Up>'],
99 'SelectUpParagraph' => ['<Shift-Control-Up>'],
100 'MoveDown' => ['<Down>'],
101 'SelectDown' => ['<Shift-Down>'],
102 'SelectRectDown' => ['<Shift-Alt-Down>'],
103 'MoveDownParagraph' => ['<Control-Down>'],
104 'SelectDownParagraph' => ['<Shift-Control-Down>'],
105 'MoveLineStart' => ['<Home>'],
106 'SelectToLineStart' => ['<Shift-Home>'],
107 'MoveTextStart' => ['<Control-Home>'],
108 'SelectToTextStart' => ['<Shift-Control-Home>'],
109 'MoveLineEnd' => ['<End>'],
110 'SelectToLineEnd' => ['<Shift-End>'],
111 'MoveTextEnd' => ['<Control-End>'],
112 'SelectToTextEnd' => ['<Shift-Control-End>'],
113 'MovePageUp' => ['<Prior>'],
114 'SelectToPageUp' => ['<Shift-Prior>'],
115 'MovePageLeft' => ['<Control-Prior>'],
116 'MovePageDown' => ['<Next>'],
117 'SelectToPageDown' => ['<Shift-Next>'],
118 'MovePageRight' => ['<Control-Next>'],
119 'SetSelectionMark' => ['<Control-space>','<Select>'],
120 'SelectToMark' => ['<Shift-Control-space>','<Shift-Select>'],
121 #=20010117 JWT selection extensions
122 # 'SelectAll' => ['<Control-a>'],
123 'SelectAll' => ['<Triple-1><Button-1>','<Control-a>','<Control-slash>'],
124 #=
125 'SelectionShiftLeft' => ['<Control-comma>'],
126 'SelectionShiftLeftTab' => ['<Control-Alt-comma>'],
127 'SelectionShiftRight' => ['<Control-period>'],
128 'SelectionShiftRightTab' => ['<Control-Alt-period>'],
129
130 'Ins' => ['<Insert>'],
131 'Enter' => ['<Return>'],
132 'AutoIndentEnter' => ['<Control-Return>'],
133 'NoAutoIndentEnter' => ['<Shift-Return>'],
134 'Del' => ['<Delete>'],
135 #-1999/07/11 alexiob@dlevel.com - Fixed win32 BackSpace bug thanks to Jim Turner
136 # 'BackSpace' => ['<BackSpace>'],
137 'DeleteToWordStart' => ['<Shift-BackSpace>'],
138 'DeleteToWordEnd' => ['<Shift-Delete>'],
139 'DeleteToLineStart' => ['<Alt-BackSpace>'],
140 'DeleteToLineEnd' => ['<Alt-Delete>'],
141 'DeleteWord' => ['<Control-BackSpace>'],
142 'DeleteLine' => ['<Control-Delete>'],
143
144 'InsertControlCode' => ['<Control-Escape>'],
145
146 'FocusNext' => ['<Control-Tab>'],
147 'FocusPrev' => ['<Shift-Control-Tab>'],
148
149 'FlashMatchingChar' => ['<Control-b>'],
150 'RemoveMatch' => ['<Control-B>'],
151 'FindMatchingChar' => ['<Control-j>'],
152 'JumpToMatchingChar' => ['<Control-J>'],
153 #+20010117 JWT fix
154 'JumpToMatchingChar' => ['<Control-p>'],
155 #+
156 'Escape' => ['<Escape>'],
157 'Tab' => ['<Tab>'],
158 'LeftTab' => ['<Shift-Tab>'],
159 'Copy' => ['<Control-c>'],
160 'Cut' => ['<Control-x>'],
161 'Paste' => ['<Control-v>'],
162 'InlinePaste' => ['<Control-V>'],
163 'Undo' => ['<Control-z>'],
164 'Redo' => ['<Control-Z>'],
165
166 'Destroy' => ['<Destroy>'],
167
168 'KeyPress' => ['<KeyPress>'],
169 'MenuSelect' => ['<Alt-KeyPress>'],
170
171 'NoOP' => ['<Control-KeyPress>']
172 );
173
174 return \%events;
175 } # /DefaultEvents
176
177 sub ClassInit
178 {
179 my ($class,$w) = @_;
180
181 $class->SUPER::ClassInit($w);
182
183 # reset default Tk::Text binds
184 $class->RemoveTextBinds($w);
185
186 return $class;
187 }
188
189 sub Populate
190 {
191 #+20010117 JWT TextANSIColor support
192 my ($w,$args) = @_;
193
194 $w->{ansicolor} = 0;
195 $w->{ansicolor} = delete ($args->{-ansicolor}) if (defined($args->{-ansicolor}));
196 #+
197
198 $w->SUPER::Populate($args);
199
200 # and set configuration parameters defaults
201 $w->ConfigSpecs(
202 '-readonly' => ['METHOD','readOnly','ReadOnly',0],
203 '-indentmode' => ['PASSIVE','indentMode','IndentMode','auto'],
204 #+20010117 JWT TextANSIColor support
205 '-ansicolor' => ['PASSIVE','ansicolor','ansicolor',undef],
206 #+
207 '-undodepth' => ['PASSIVE','undoDepth','UndoDepth',undef],
208 '-redodepth' => ['PASSIVE','redoDepth','RedoDepth',undef],
209 '-showmatching' => ['PASSIVE','showMatching','ShowMatching',1],
210 '-matchhighlighttime' => ['PASSIVE','matchHighlightTime','MatchHighlightTime',1400],
211 '-matchforeground' => ['METHOD','matchForeground','MatchForeground','white'],
212 '-matchbackground' => ['METHOD','matchBackground','MatchBackground','blue'],
213 '-matchingcouples' => ['METHOD','matchingCouples','MatchingCouples',"//[]{}()<>\\\\''``\"\""],
214 '-insertmode' => ['METHOD','insertMode','InsertMode','insert'],
215 '-foreground' => ['SELF','foreground','Foreground',$w->cget('-foreground')],
216 );
217 # set default key binds and events
218 $w->bindDefault;
219 # set undo block flag
220 $w->{UNDOBLOCK}=0;
221
222 #+20010117 JWT TextANSIColor support
223 if ($w->{ansicolor})
224 {
225 # Setup tags
226 # colors
227 for (@colors)
228 {
229 $w->tagConfigure("ANSIfg$_", -foreground => $_);
230 $w->tagConfigure("ANSIbg$_", -background => $_);
231 }
232 # Underline
233 $w->tagConfigure("ANSIul", -underline => 1);
234 $w->tagConfigure("ANSIbd", -font => [-weight => "bold" ]);
235 }
236 #+
237 }
238
239 # callbacks for options management
240
241 sub matchforeground
242 {
243 my ($w,$val) = @_;
244
245 if(!defined $val) {return $w->tagConfigure('match','-foreground');}
246 $w->tagConfigure('match','-foreground' => $val);
247 }
248
249 sub matchbackground
250 {
251 my ($w,$val) = @_;
252
253 if(!defined $val) {return $w->tagConfigure('match','-background');}
254 $w->tagConfigure('match','-background' => $val);
255 }
256
257 sub matchingcouples
258 {
259 my ($w,$val) = @_;
260 my ($i,$dir);
261
262
263 if(!defined $val) {return $w->{MATCHINGCOUPLES_STRING};}
264 $w->{MATCHINGCOUPLES_STRING}=$val;
265
266 $w->{MATCHINGCOUPLES}={} unless exists $w->{MATCHINGCOUPLES};
267 for($i=0;$i<length($val);$i++) {
268 $dir=($i % 2 ? -1 : 1);
269 if($dir == -1 && (substr($val,$i,1) eq substr($val,$i+$dir,1))) {next;}
270 $w->{MATCHINGCOUPLES}->{substr($val,$i,1)}=[substr($val,$i+$dir,1),$dir];
271 }
272 }
273
274 sub insertmode
275 {
276 my ($w,$val) = @_;
277
278 if(!defined $val) {return $w->{INSERTMODE};}
279 $w->{INSERTMODE}=$val;
280 }
281
282 # Read-only affects editing commands, not application insert/delete calls.
283 sub readonly
284 {
285 my ($w,$val) = @_;
286 return $w->{READONLY} || 0 unless @_ > 1;
287 $w->{READONLY} = $val ? 1 : 0;
288 delete $w->{ASCIICODE} if $w->{READONLY};
289 return $w->{READONLY};
290 }
291
292 # Tk::Text uses Insert for interactive insertion (including replacement of sel).
293 sub Insert
294 {
295 my $w = shift;
296 return if $w->cget('-readonly');
297 return $w->SUPER::Insert(@_);
298 }
299
300 # Also protect the inherited edit menu and search/replace dialog.
301 sub ReplaceSelectionsWith
302 {
303 my $w = shift;
304 return if $w->cget('-readonly');
305 return $w->SUPER::ReplaceSelectionsWith(@_);
306 }
307
308 sub clipboardCut
309 {
310 my $w = shift;
311 return if $w->cget('-readonly');
312 return $w->SUPER::clipboardCut(@_);
313 }
314
315 # insertion and deletion functions intereptors
316
317 sub insert
318 {
319 my ($w,$index,$str,@tags) = @_;
320 my $s = $w->index($index);
321 my $i;
322
323 # for line start hack
324 $w->{LINESTART}=0;
325
326 $w->markSet('undopos' => $s);
327 # insert ascii code
328 if((exists $w->{ASCIICODE}) && $w->{ASCIICODE} == 1) {
329 if(($str ge ' ') && ($str le '?')) {$i=-0x20;}
330 else {$i=0x7f-0x40;}
331 $str=sprintf('%c',ord($str) + $i);
332 $w->{ASCIICODE} = 0;
333 }
334 # manage overwrite mode,NOT optimal for undo,but... hey who uses overwrite mode???
335 if($w->{INSERTMODE} eq 'overwrite') {
336 $w->_BeginUndoBlock;
337 if($w->compare($s,'<',$w->index("$s lineend"))) {$w->delete($s);}
338 }
339
340 #-20010117 JWT TextANSIColor support
341 # $w->SUPER::insert($s,$str,@tags);
342 #-
343 #+20010117 JWT TextANSIColor support
344 if ($w->{ansicolor})
345 {
346 #$w->SUPER::insert($s,$str,@tags); #JWT:01042001: REPL. W/NEXT LINES FOR TEXTANSICOLOR!
347 my (@userstuff) = ($str,@tags);
348 my ($pos) = $s;
349
350 # This is the array containing text and tags pairs
351 # We pass this to SUPER::insert
352 # as (POS, string, [tags], string, [tags]....)
353 # insert_array contains string,[tags] pairs
354 my @insert_array = ();
355
356 # Need to loop over @userstuff
357 # extracting out the text string and any user supplied tags.
358 # note that multiple sets of text strings and tags can be supplied
359 # as arguments to the insert() method, and we have to process
360 # each set in turn.
361 # Use an old-fashioned for since we have to extract two items at
362 # a time
363
364 for (my $i=0; $i <= $#userstuff; $i += 2)
365 {
366
367 my $text = $userstuff[$i];
368 my $utags = $userstuff[$i+1];
369
370 # Store the usertags in an array, expanding the
371 # array ref if required
372 my @taglist = ();
373 if (ref($utags) eq 'ARRAY')
374 {
375 @taglist = @{$utags};
376 }
377 else
378 {
379 @taglist = ($utags);
380 }
381
382 # Split the string on control codes
383 # returning the codes as well as the strings between
384 # the codes
385 # Note that this pattern also checks for the case when
386 # multiple escape codes are embedded together separated
387 # by semi-colons.
388 my @split = split /(\e\[(?:\d{1,2};?)+m)/, $text;
389 # Array containing the tags to use with the insertion
390 # Note that this routine *always* assumes the colors are reset
391 # after the last insertion. ie it does not allow the colors to be
392 # remembered between calls to insert().
393 my @ansitags = ();
394
395 # Current text string
396 my $cur_text = undef;
397
398 # Now loop over the split strings
399 for my $part (@split)
400 {
401
402 # If we have a plain string, just store it
403 if ($part !~ /^\e/)
404 {
405 $cur_text = $part;
406 }
407 else
408 {
409 # We have an escape sequence
410 # Need to store the current string with required tags
411 # Include the ansi tags and the user-supplied tag list
412 push(@insert_array, $cur_text, [@taglist, @ansitags])
413 if defined $cur_text;
414
415 # There is no longer a 'current string'
416 $cur_text = undef;
417
418 # The escape sequence can have semi-colon separated bits
419 # in it. Need to strip off the \e[ and the m. Split on
420 # semi-colon and then reconstruct before comparing
421 # We know it matches \e[....m so use substr
422
423 # Only bother if we have a semi-colon
424
425 my @escs = ($part);
426 if ($part =~ /;/)
427 {
428 my $strip = substr($part, 2, length($part) - 3);
429
430 # Split on ; (overwriting @escs)
431 @escs = split(/;/,$strip);
432
433 # Now attach the correct escape sequence
434 foreach (@escs) { $_ = "\e[${_}m" }
435 }
436
437 # Loop over all the escape sequences
438 for my $esc (@escs)
439 {
440
441 # Check what type of escape
442 if ($esc eq $clear)
443 {
444 # Clear all escape sequences
445 @ansitags = ();
446 }
447 elsif (exists $fgcolors{$esc})
448 {
449 # A foreground color has been specified
450 push(@ansitags, $fgcolors{$esc});
451 }
452 elsif (exists $bgcolors{$esc})
453 {
454 # A background color
455 push(@ansitags, $bgcolors{$esc});
456 }
457 elsif ($esc eq $code_bold)
458 {
459 # Boldify
460 push(@ansitags, "ANSIbd");
461 }
462 elsif ($esc eq $code_uline)
463 {
464 # underline
465 push(@ansitags, "ANSIul");
466 }
467 else
468 {
469 print "Unrecognised control code - ignoring\n";
470 foreach (split //, $esc)
471 {
472 print ord($_) . ": $_\n";
473 }
474 }
475 }
476 }
477 }
478 # If we still have a current string, push that onto the array
479 push(@insert_array, $cur_text, [@taglist, @ansitags])
480 if defined $cur_text;
481 }
482 # Finally, insert the string
483 $w->SUPER::insert($pos, @insert_array)
484 if $#insert_array > 0;
485 }
486 else
487 {
488 $w->SUPER::insert($s,$str,@tags); #JWT:01042001: REPL. W/NEXT LINES FOR TEXTANSICOLOR!
489 }
490 #+
491
492 # match coupled chars
493 if((!defined $w->tag('ranges','sel')) && $w->cget('-showmatching') == 1) {
494 if(exists $w->{MATCHINGCOUPLES}->{$str}) {
495 # calculate visible zone and search only in this one
496 my ($l,$c) = split('\.',$w->index('end'));
497 my ($slimit,$elimit) = $w->yview;
498
499 $slimit=int($l*$slimit)+1;
500 $slimit="$slimit.0";
501 $elimit=int($l*$elimit);
502 $elimit="$elimit.0";
503 my $i=$w->_FindMatchingChar($str,$s,$slimit,$elimit);
504 if(defined $i) {
505 my $sel = Tk::catch {$w->tag('nextrange','match','1.0','end');};
506 if(defined $sel) {$w->tag('remove','match','match.first');}
507 $w->tag('add','match',$i,$w->index("$i + 1 c"));
508 my $t=$w->cget('-matchhighlighttime');
509 if($t != 0) {$w->after($t,[\&removeMatch,$w,$i]);}
510 }
511 }
512 }
513
514 # combine 'trivial ' inserts into clumps
515 if((length($str) == 1) && ($str ne "\n")) {
516 my $t = $w->_TopUndo;
517 if($t && $t->[0] =~ /delete$/ && $w->compare($t->[2],'==',$s)) {
518 $t->[2] = $w->index('undopos');
519 return;
520 }
521 }
522 $w->_AddUndo('delete',$s,$w->index('undopos'));
523 # for undo blocks
524 if($w->{INSERTMODE} eq 'overwrite') {
525 $w->_EndUndoBlock;
526 }
527 }
528
529 sub delete
530 {
531 my $w = shift;
532 my $str = $w->get(@_);
533 my $s = $w->index(shift);
534
535 $w->{LINESTART}=0;
536 $w->SUPER::delete($s,@_);
537 $w->_AddUndo('insert',$s,$str);
538 }
539
540
541 # used for removing match tag after some time
542 # here so Tk::After doesn't complain
543 sub removeMatch
544 {
545 my ($w,$i) = @_;
546
547 if(defined $i) {$w->tag('remove','match',$i);}
548 else {$w->tag('remove','match','1.0','end');}
549 }
550
551
552 #+20010117 JWT TextANSIColor support
553 #sub get
554 #{
555 # my $self= shift; # The widget reference
556 # return $self->SUPER::get(@_);
557 #}
558
559 sub getansi
560 {
561 my $self= shift; # The widget reference
562 my (@args) = @_;
563 return $self->get(@args) unless ($self->{ansicolor});
564
565 my $i;
566 my (@xdump);
567 my $tagflag = 0;
568 my $res = '';
569
570 @xdump = $self->dump(@args);
571 for ($i=0;$i<=$#xdump;$i+=3)
572 {
573 if ($xdump[$i] eq 'tagon')
574 {
575 if ($xdump[$i+1] =~ /^ANSIfg(\w+)/)
576 {
577 $res .= color($1);
578 $tagflag = 1;
579 }
580 elsif ($xdump[$i+1] =~ /^ANSIbg(\w+)/)
581 {
582 $res .= color("on_$1");
583 $tagflag = 1;
584 }
585 elsif ($xdump[$i+1] =~ /^ANSIbd/)
586 {
587 $res .= color('bold');
588 $tagflag = 1;
589 }
590 elsif ($xdump[$i+1] =~ /^ANSIul/)
591 {
592 $res .= color('underline');
593 $tagflag = 1;
594 }
595 #$res .= $xdump[$i+4] if ($xdump[$i+3] eq 'text');
596 }
597 if ($tagflag && $xdump[$i] eq 'tagoff')
598 {
599 $res .= color('reset');
600 $tagflag = 0;
601 }
602 if ($xdump[$i] eq 'text')
603 {
604 $res .= $xdump[$i+1];
605 }
606 };
607 return $res;
608 }
609 #+
610
611 # clipboard methods that must be overriden for rectangular selections
612
613 sub deleteSelected
614 {
615 my $w = shift;
616 return if $w->cget('-readonly');
617
618 if(!defined $Tk::selectionType || ($Tk::selectionType eq 'normal')) {
619 $w->SUPER::deleteSelected;
620 } elsif ($Tk::selectionType eq 'rect') {
621 my ($sl,$sc) = split('\.',$w->index('sel.first'));
622 my ($el,$ec) = split('\.',$w->index('sel.last'));
623 my ($i,$x);
624
625 # delete only text in the rectangular selection range
626 $w->_BeginUndoBlock;
627 for($i=$sl;$i<=$el;$i++) {
628 my ($l,$c) = split('\.',$w->index("$i.end"));
629 # check if selection is too right (??) for this line
630 if($sc > $c) {next;}
631 # and clip selection
632 if($ec <= $c) {$x=$ec;}
633 else { $x=$c;}
634
635 $w->delete($w->index("$i.$sc"),$w->index("$i.$x"));
636 }
637 $w->_EndUndoBlock;
638 }
639 }
640
641 sub getSelected
642 {
643 my $w = shift;
644
645 if(!defined $Tk::selectionType || ($Tk::selectionType eq 'normal')) {
646 return $w->SUPER::getSelected;
647 } elsif ($Tk::selectionType eq 'rect') {
648 my ($sl,$sc) = split('\.',$w->index('sel.first'));
649 my ($el,$ec) = split('\.',$w->index('sel.last'));
650 my ($i,$x);
651 my ($sel,$str);
652
653 $sel="";
654
655 # walk throught all the selected lines and add a sel tag
656 for($i=$sl;$i<=$el;$i++) {
657 my ($l,$c) = split('\.',$w->index("$i.end"));
658 # check if selection is too much to the right
659 if($sc > $c) {next;}
660 # or clif if too wide
661 if($ec <= $c) {$x=$ec;}
662 else { $x=$c;}
663 $str=$w->get($w->index("$i.$sc"),$w->index("$i.$x"));
664 # add a new line if not the last line
665 if(substr($str,-1,1) ne "\n") {
666 $str=$str."\n";
667 }
668 $sel=$sel.$str;
669 }
670 return $sel;
671 }
672 }
673
674 # redefine SetCursor for parentheses highlight
675 sub SetCursor
676 {
677 my $w = shift;
678 my $str;
679
680 $w->SUPER::SetCursor(@_);
681
682 if((!defined $w->tag('ranges','sel')) && $w->cget('-showmatching') == 1) {
683 if(exists $w->{MATCHINGCOUPLES}->{$str=$w->get('insert','insert + 1c')}) {
684 # calculate visible zone and search only in this one
685 my ($l,$c) = split('\.',$w->index('end'));
686 my ($slimit,$elimit) = $w->yview;
687
688 $slimit=int($l*$slimit)+1;
689 $slimit="$slimit.0";
690 $elimit=int($l*$elimit);
691 $elimit="$elimit.0";
692 my $i=$w->_FindMatchingChar($str,'insert',$slimit,$elimit);
693 if(defined $i) {
694 my $sel = Tk::catch {$w->tag('nextrange','match','1.0','end');};
695 if(defined $sel) {$w->tag('remove','match','match.first');}
696 $w->tag('add','match',$i,$w->index("$i + 1c"));
697 my $t=$w->cget('-matchhighlighttime');
698 if($t != 0) {$w->after($t,[\&removeMatch,$w,$i]);}
699 }
700 }
701 }
702 }
703
704 # redefine Button1for parentheses highlight
705 sub Button1
706 {
707 my $w = shift;
708
709 $w->SUPER::Button1(@_);
710
711 if((!defined $w->tag('ranges','sel')) && $w->cget('-showmatching') == 1) {
712 my $str = $w->get('insert', 'insert + 1c');
713 if(defined $str && exists $w->{MATCHINGCOUPLES}->{$str}) {
714 # calculate visible zone and search only in this one
715 my ($l,$c) = split('\.',$w->index('end'));
716 my ($slimit,$elimit) = $w->yview;
717
718 $slimit=int($l*$slimit)+1;
719 $slimit="$slimit.0";
720 $elimit=int($l*$elimit);
721 $elimit="$elimit.0";
722 my $i=$w->_FindMatchingChar($str,'insert',$slimit,$elimit);
723 if(defined $i) {
724 my $sel = Tk::catch {$w->tag('nextrange','match','1.0','end');};
725 if(defined $sel) {$w->tag('remove','match','match.first');}
726 $w->tag('add','match',$i,$w->index("$i + 1c"));
727 my $t=$w->cget('-matchhighlighttime');
728 if($t != 0) {$w->after($t,[\&removeMatch,$w,$i]);}
729 }
730 }
731 }
732 }
733
734 # remove default Tk::Text key binds
735 sub RemoveTextBinds
736 {
737 my ($class,$w) = @_;
738 my (@binds) = $w->bind($class);
739
740 foreach $b (@binds) {
741 #=1999/07/11 alexiob@dlevel.com - Fixed win32 BackSpace bug thanks to Jim Turner
742 # $w->bind($class,$b,"");
743 $w->bind($class,$b,"") unless ($b =~ /Key-BackSpace/);
744 }
745 }
746
747 # bind default keys with default events
748 sub bindDefault
749 {
750 my $w = shift;
751 my $events = $w->DefaultEvents;
752
753 foreach my $e (keys %$events) {
754 $w->eventAdd("<<$e>>",@{$$events{$e}});
755 $w->bind($w,"<<$e>>",lcfirst($e));
756 }
757 #+1999/07/11 alexiob@dlevel.com - Fixed win32 BackSpace bug thanks to Jim Turner
758 $w->bind("<Key-BackSpace>", sub {Tk->break;});
759 }
760
761 # delete all event binds,specified event bind
762 sub bindDelete
763 {
764 my ($w,$event,@triggers) = @_;
765
766 if(!$event) {
767 # delete all events binds
768 my ($e);
769
770 foreach $e (%{$w->DefaultEvents}) {
771 $w->eventDelete($e);
772 }
773 return;
774 }
775 $w->eventDelete($event,@triggers);
776 }
777
778 # Key binding Events subs
779
780 sub _BeginUndoBlock
781 {
782 my $w = shift;
783
784 $w->_AddUndo('#_BlockEnd_#');
785 }
786
787 sub _EndUndoBlock
788 {
789 my $w = shift;
790
791 $w->_AddUndo('#_BlockBegin_#');
792 }
793
794 # resets undo and redo buffers
795 sub resetUndo
796 {
797 my $w = shift;
798
799 delete $w->{UNDO};
800 delete $w->{REDO};
801 }
802
803 # undo last operation
804 sub undo
805 {
806 my ($w) = @_;
807 return 0 if $w->cget('-readonly');
808 my $s;
809 my $op;
810 my @args;
811 my $block = 0;
812
813 if(exists $w->{UNDO}) {
814 if(@{$w->{UNDO}}) {
815 # undo loop
816 while(1) {
817 # retrive undo command
818 my ($op,@args) = Tk::catch{@{pop(@{$w->{UNDO}})};};
819
820 if($op eq '#_BlockBegin_#') {
821 $w->_AddRedo('#_BlockEnd_#');
822 $block=1;
823 next;
824 } elsif($op eq '#_BlockEnd_#') {
825 $w->_AddRedo('#_BlockBegin_#');
826 return 1;
827 }
828 # convert for redo
829 if($op =~ /insert$/) {
830 # get current insert position
831 $s = $w->index($args[0]);
832 # mark for getting the with of the insertion
833 $w->markSet('redopos' => $s);
834 } elsif ($op =~ /delete$/) {
835 # save text and position
836 my $str = $w->get(@args);
837 $s = $w->index($args[0]);
838
839 $w->_AddRedo('insert',$s,$str);
840 }
841 # execute undo command
842 $w->$op(@args);
843 $w->SetCursor($args[0]);
844 # insert redo command
845 if($op =~ /insert$/) {
846 $w->_AddRedo('delete',$s,$w->index('redopos'));
847 }
848 if($block == 0) {return 1;}
849 }
850 }
851 }
852 $w->bell;
853 return 0;
854 }
855
856 # redo last undone operation
857 sub redo
858 {
859 my ($w) = @_;
860 return 0 if $w->cget('-readonly');
861 my $block = 0;
862
863 if(exists $w->{REDO}) {
864 if(@{$w->{REDO}}) {
865 while(1) {
866 my ($op,@args) = Tk::catch{@{pop(@{$w->{REDO}})};};
867
868 if($op eq '#_BlockBegin_#') {
869 $w->_AddUndo('#_BlockEnd_#');
870 $block=1;
871 next;
872 } elsif($op eq '#_BlockEnd_#') {
873 $w->_AddUndo('#_BlockBegin_#');
874 return 1;
875 }
876 $op =~ s/^SUPER:://;
877 $w->$op(@args);
878 $w->SetCursor($args[0]);
879 if($block == 0) {return 1;}
880 }
881 }
882 }
883 $w->bell;
884 return 0;
885 }
886
887 # add an undo command to the undo stack
888 sub _AddUndo
889 {
890 my ($w,$op,@args) = @_;
891 my ($usize,$udepth);
892
893 $w->{UNDO} = [] unless(exists $w->{UNDO});
894 # check for undo depth limit
895 $usize = @{$w->{UNDO}} + 1;
896 $udepth = $w->cget('-undodepth');
897
898 if(defined $udepth) {
899 if($udepth == 0) {return;}
900 if($usize >= $udepth) {
901 # free oldest undo sequence
902 $udepth=$usize - $udepth + 1;
903 splice(@{$w->{UNDO}},0,$udepth);
904 }
905 }
906 if($op =~ /^#_/) {push(@{$w->{UNDO}},[$op]);}
907 else {push(@{$w->{UNDO}},['SUPER::'.$op,@args]);}
908 }
909
910 # return the last added undo command
911 sub _TopUndo
912 {
913 my ($w) = @_;
914
915 return undef unless (exists $w->{UNDO});
916 return $w->{UNDO}[-1];
917 }
918
919 # add a new redo command to the redo stack
920 sub _AddRedo
921 {
922 my ($w,$op,@args) = @_;
923 my ($rsize,$rdepth);
924
925 $w->{REDO} = [] unless(exists $w->{REDO});
926
927 # check for undo depth limit
928 $rsize = @{$w->{REDO}} + 1;
929 $rdepth = $w->cget('-undodepth');
930
931 if(defined $rdepth) {
932 if($rdepth == 0) {return;}
933 if($rsize >= $rdepth) {
934 # free oldest undo sequence
935 $rdepth=$rsize - $rdepth + 1;
936 splice(@{$w->{REDO}},0,$rdepth);
937 }
938 }
939 if($op =~ /^#_/) {push(@{$w->{REDO}},[$op]);}
940 else {push(@{$w->{REDO}},['SUPER::'.$op,@args]);}
941 }
942
943 # manage mouse normal and rectangular selections for char,word or line mode
944 # overrides standard Tk::Text->SelectTo method
945 sub SelectTo
946 {
947 my $w = shift;
948 my $index = shift;
949 $Tk::selectMode = shift if (@_);
950 my $cur = $w->index($index);
951 my $anchor = Tk::catch{$w->index('anchor')};
952
953 # check for mouse movement
954 if(!defined $anchor) {
955 $w->markSet('anchor',$anchor=$cur);
956 $Tk::mouseMoved=0;
957 } elsif($w->compare($cur,"!=",$anchor)) {
958 $Tk::mouseMoved=1;
959 }
960 $Tk::selectMode='char' unless(defined $Tk::selectMode);
961
962 my $mode = $Tk::selectMode;
963 my ($first,$last);
964
965 # get new selection limits
966 if($mode eq 'char') {
967 if($w->compare($cur,"<",'anchor')) {
968 $first=$cur;
969 $last='anchor';
970 } else {
971 $first='anchor';
972 $last=$cur;
973 }
974 } elsif($mode eq 'word') {
975 if($w->compare($cur,"<",'anchor')) {
976 $first = $w->index("$cur wordstart");
977 $last = $w->index("anchor - 1c wordend");
978 } else {
979 $first=$w->index("anchor wordstart");
980 $last=$w->index("$cur wordend");
981 }
982 } elsif($mode eq 'line') {
983 if($w->compare($cur,"<",'anchor')) {
984 $first=$w->index("$cur linestart");
985 $last=$w->index("anchor - 1c lineend + 1c");
986 } else {
987 $first=$w->index("anchor linestart");
988 $last=$w->index("$cur lineend + 1c");
989 }
990 }
991 # update selection
992 if($Tk::mouseMoved || $Tk::selectMode ne 'char') {
993 if((!defined $Tk::selectionType) || ($Tk::selectionType eq 'normal')) {
994 # simple normal selection
995 $w->tag('remove','sel','1.0',$first);
996 $w->tag('add','sel',$first,$last);
997 $w->tag('remove','sel',$last,'end');
998 $w->idletasks;
999 } elsif($Tk::selectionType eq 'rect') {
1000 my ($sl,$sc) = split('\.',$w->index($first));
1001 my ($el,$ec) = split('\.',$w->index($last));
1002 my $i;
1003
1004 # swap min,max x,y coords
1005 if($sl >= $el) {($sl,$el)=($el,$sl);}
1006 if($sc >= $ec) {($sc,$ec)=($ec,$sc);}
1007
1008 $w->tag('remove','sel','1.0','end');
1009 # add a selection tag to all the selected lines
1010 # FIXME: the selection's right limit is the line lenght of the line where mouse is on.BAD!!!
1011 for($i=$sl;$i<=$el;$i++) {
1012 $w->tag('add','sel',"$i.$sc","$i.$ec");
1013 }
1014 $w->idletasks;
1015 }
1016 }
1017 }
1018
1019 sub mouseSetInsert
1020 {
1021 my $w = shift;
1022 my $ev = $w->XEvent;
1023
1024 $w->{LINESTART}=0;
1025 $w->Button1($ev->x,$ev->y);
1026 }
1027
1028 sub mouseSelect
1029 {
1030 my $w = shift;
1031 my $ev = $w->XEvent;
1032
1033 $Tk::selectionType='normal';
1034 $Tk::x=$ev->x;
1035 $Tk::y=$ev->y;
1036 $w->SelectTo($ev->xy);
1037 }
1038
1039 sub mouseSelectWord
1040 {
1041 my $w = shift;
1042 my $ev = $w->XEvent;
1043
1044 $Tk::selectionType='normal';
1045 $w->SelectTo($ev->xy,'word');
1046 Tk::catch {$w->markSet('insert',"sel.first")};
1047 }
1048
1049 sub mouseSelectLine
1050 {
1051 my $w = shift;
1052 my $ev = $w->XEvent;
1053
1054 $Tk::selectionType='normal';
1055 $w->SelectTo($ev->xy,'line');
1056 Tk::catch {$w->markSet('insert',"sel.first")};
1057 }
1058
1059 #+20010117 JWT cause right button to extend select
1060 sub mouseSelectChar
1061 {
1062 my $w = shift;
1063 my $ev = $w->XEvent;
1064
1065 $Tk::selectionType='normal';
1066 $w->SelectTo($ev->xy,'char');
1067 Tk::catch {$w->markSet('insert',"sel.first")};
1068 }
1069 #+
1070
1071 sub mouseSelectAdd
1072 {
1073 my $w = shift;
1074 my $ev = $w->XEvent;
1075
1076 $Tk::selectionType='normal';
1077 $w->ResetAnchor($ev->xy);
1078 $w->SelectTo($ev->xy,'char');
1079 }
1080
1081 sub mouseSelectAddWord
1082 {
1083 my $w = shift;
1084 my $ev = $w->XEvent;
1085
1086 $Tk::selectionType='normal';
1087 $w->SelectTo($ev->xy,'word');
1088 }
1089
1090 sub mouseSelectAddLine
1091 {
1092 my $w = shift;
1093 my $ev = $w->XEvent;
1094
1095 $Tk::selectionType='normal';
1096 $w->SelectTo($ev->xy,'line');
1097 }
1098
1099 sub mouseSelectAutoScan
1100 {
1101 my $w = shift;
1102 my $ev = $w->XEvent;
1103
1104 $Tk::selectionType='normal';
1105 $Tk::x=$ev->x;
1106 $Tk::y=$ev->y;
1107 $w->AutoScan;
1108 }
1109
1110 sub mouseSelectAutoScanStop
1111 {
1112 my $w = shift;
1113
1114 $w->CancelRepeat;
1115 }
1116
1117 sub mouseMoveInsert
1118 {
1119 my $w = shift;
1120 my $ev = $w->XEvent;
1121
1122 $Tk::selectionType='normal';
1123 $w->markSet('insert',$ev->xy);
1124 }
1125
1126 sub mouseRectSelection
1127 {
1128 my $w = shift;
1129 my $ev = $w->XEvent;
1130
1131 $Tk::selectionType='rect';
1132 $Tk::x=$ev->x;
1133 $Tk::y=$ev->y;
1134 $w->SelectTo($ev->xy);
1135 }
1136
1137 sub mouseMovePageTo
1138 {
1139 my $w = shift;
1140 my $ev = $w->XEvent;
1141
1142 $w->Button2($ev->x,$ev->y);
1143 }
1144
1145 sub mouseMovePage
1146 {
1147 my $w = shift;
1148 my $ev = $w->XEvent;
1149
1150 $w->Motion2($ev->x,$ev->y);
1151 }
1152
1153 sub mousePasteSelection
1154 {
1155 my $w = shift;
1156 return if $w->cget('-readonly');
1157 my $ev = $w->XEvent;
1158
1159 if(!$Tk::mouseMoved) {
1160 Tk::catch { $w->insert($ev->xy,$w->SelectionGet);};
1161 }
1162 }
1163
1164 sub KeySelect
1165 {
1166 my $w = shift;
1167 my $new = shift;
1168 my ($first,$last);
1169 if(!defined $w->tag('ranges','sel')) {
1170 # No selection yet
1171 $w->markSet('anchor','insert');
1172 if($w->compare($new,"<",'insert')) {
1173 $w->tag('add','sel',$new,'insert');
1174 } else {
1175 $w->tag('add','sel','insert',$new);
1176 }
1177 } else {
1178 # Selection exists
1179 if($w->compare($new,"<",'anchor')) {
1180 $first=$new;
1181 $last='anchor';
1182 } else {
1183 $first='anchor';
1184 $last=$new;
1185 }
1186 if((!defined $Tk::selectionType) || ($Tk::selectionType eq 'normal')) {
1187 $w->tag('remove','sel','1.0',$first);
1188 $w->tag('add','sel',$first,$last);
1189 $w->tag('remove','sel',$last,'end');
1190 } elsif($Tk::selectionType eq 'rect') {
1191 my ($sl,$sc) = split('\.',$w->index($first));
1192 my ($el,$ec) = split('\.',$w->index($last));
1193 my $i;
1194
1195 # swap min,max x,y coords
1196 if($sl >= $el) {($sl,$el)=($el,$sl);}
1197 if($sc >= $ec) {($sc,$ec)=($ec,$sc);}
1198
1199 $w->tag('remove','sel','1.0','end');
1200 # add a selection tag to all the selected lines
1201 # FIXME: the selection's right limit is the line lenght of the line where mouse is on.BAD!!!
1202 for($i=$sl;$i<=$el;$i++) {
1203 $w->tag('add','sel',"$i.$sc","$i.$ec");
1204 }
1205 }
1206 }
1207 $w->markSet('insert',$new);
1208 $w->see('insert');
1209 $w->idletasks;
1210 }
1211
1212 sub moveLeft
1213 {
1214 my $w = shift;
1215
1216 $w->{LINESTART}=0;
1217 $w->SetCursor($w->index("insert - 1c"));
1218 }
1219
1220 sub selectLeft
1221 {
1222 my $w = shift;
1223
1224 $w->{LINESTART}=0;
1225 $Tk::selectionType='normal';
1226 $w->KeySelect($w->index("insert - 1c"));
1227 }
1228
1229 sub selectRectLeft
1230 {
1231 my $w = shift;
1232
1233 $w->{LINESTART}=0;
1234 $Tk::selectionType='rect';
1235 $w->KeySelect($w->index("insert - 1c"));
1236 }
1237
1238 sub moveLeftWord
1239 {
1240 my $w = shift;
1241
1242 $w->{LINESTART}=0;
1243 $w->SetCursor($w->index("insert - 1c wordstart"));
1244 }
1245
1246 sub selectLeftWord
1247 {
1248 my $w = shift;
1249
1250 $w->{LINESTART}=0;
1251 $Tk::selectionType='normal';
1252 $w->KeySelect($w->index("insert - 1c wordstart"));
1253 }
1254
1255 sub moveRight
1256 {
1257 my $w = shift;
1258
1259 $w->{LINESTART}=0;
1260 $w->SetCursor($w->index("insert + 1c"));
1261 }
1262
1263 sub selectRight
1264 {
1265 my $w = shift;
1266
1267 $w->{LINESTART}=0;
1268 $Tk::selectionType='normal';
1269 $w->KeySelect($w->index("insert + 1c"));
1270 }
1271
1272 sub selectRectRight
1273 {
1274 my $w = shift;
1275
1276 $w->{LINESTART}=0;
1277 $Tk::selectionType='rect';
1278 $w->KeySelect($w->index("insert + 1c"));
1279 }
1280
1281 sub moveRightWord
1282 {
1283 my $w = shift;
1284
1285 $w->{LINESTART}=0;
1286 $w->SetCursor($w->index("insert + 1c wordend"));
1287 }
1288
1289 sub selectRightWord
1290 {
1291 my $w = shift;
1292
1293 $w->{LINESTART}=0;
1294 $Tk::selectionType='normal';
1295 $w->KeySelect($w->index("insert wordend"));
1296 }
1297
1298 sub moveUp
1299 {
1300 my $w = shift;
1301
1302 $w->{LINESTART}=0;
1303 $w->SetCursor($w->UpDownLine(-1));
1304 }
1305
1306 sub selectUp
1307 {
1308 my $w = shift;
1309
1310 $w->{LINESTART}=0;
1311 $Tk::selectionType='normal';
1312 $w->KeySelect($w->UpDownLine(-1));
1313 }
1314
1315 sub selectRectUp
1316 {
1317 my $w = shift;
1318
1319 $w->{LINESTART}=0;
1320 $Tk::selectionType='rect';
1321 $w->KeySelect($w->UpDownLine(-1));
1322 }
1323
1324 sub moveUpParagraph
1325 {
1326 my $w = shift;
1327
1328 $w->{LINESTART}=0;
1329 $w->SetCursor($w->PrevPara('insert'));
1330 }
1331
1332 sub selectUpParagraph
1333 {
1334 my $w = shift;
1335
1336 $w->{LINESTART}=0;
1337 $Tk::selectionType='normal';
1338 $w->KeySelect($w->PrevPara('insert'));
1339 }
1340
1341 sub moveDown
1342 {
1343 my $w = shift;
1344
1345 $w->{LINESTART}=0;
1346 $w->SetCursor($w->UpDownLine(1));
1347 }
1348
1349 sub selectDown
1350 {
1351 my $w = shift;
1352
1353 $w->{LINESTART}=0;
1354 $Tk::selectionType='normal';
1355 $w->KeySelect($w->UpDownLine(1));
1356 }
1357
1358 sub selectRectDown
1359 {
1360 my $w = shift;
1361
1362 $w->{LINESTART}=0;
1363 $Tk::selectionType='rect';
1364 $w->KeySelect($w->UpDownLine(1));
1365 }
1366
1367 sub moveDownParagraph
1368 {
1369 my $w = shift;
1370
1371 $w->{LINESTART}=0;
1372 $w->SetCursor($w->NextPara('insert'));
1373 }
1374
1375 sub selectDownParagraph
1376 {
1377 my $w = shift;
1378
1379 $w->{LINESTART}=0;
1380 $Tk::selectionType='normal';
1381 $w->KeySelect($w->NextPara('insert'));
1382 }
1383
1384 sub moveLineStart
1385 {
1386 my $w = shift;
1387
1388 if(exists $w->{LINESTART} && $w->{LINESTART} == 1) {
1389 $w->SetCursor('insert linestart');
1390 $w->{LINESTART}=0;
1391 } else {
1392 $w->{LINESTART}=1;
1393 my $str = $w->get('insert linestart','insert lineend');
1394 my $i=0;
1395
1396 if($str =~ /^(\s+)(\S*)/) {
1397 if($2) {$i=length($1);}
1398 else {$i=0};
1399 }
1400 $w->SetCursor("insert linestart + $i c");
1401 }
1402 }
1403
1404 sub selectToLineStart
1405 {
1406 my $w = shift;
1407
1408 $w->{LINESTART}=0;
1409 $Tk::selectionType='normal';
1410 $w->KeySelect('insert linestart');
1411 }
1412
1413 sub moveTextStart
1414 {
1415 my $w = shift;
1416
1417 $w->{LINESTART}=0;
1418 $w->SetCursor('1.0');
1419 }
1420
1421 sub selectToTextStart
1422 {
1423 my $w = shift;
1424
1425 $w->{LINESTART}=0;
1426 $Tk::selectionType='normal';
1427 $w->KeySelect('1.0');
1428 }
1429
1430 sub moveLineEnd
1431 {
1432 my $w = shift;
1433
1434 $w->{LINESTART}=0;
1435 $w->SetCursor('insert lineend');
1436 }
1437
1438 sub selectToLineEnd
1439 {
1440 my $w = shift;
1441
1442 $w->{LINESTART}=0;
1443 $Tk::selectionType='normal';
1444 $w->KeySelect('insert lineend');
1445 }
1446
1447 sub moveTextEnd
1448 {
1449 my $w = shift;
1450
1451 $w->{LINESTART}=0;
1452 $w->SetCursor('end - 1c');
1453 }
1454
1455 sub selectToTextEnd
1456 {
1457 my $w = shift;
1458
1459 $w->{LINESTART}=0;
1460 $Tk::selectionType='normal';
1461 $w->KeySelect('end - 1c');
1462 }
1463
1464 sub ScrollPages
1465 {
1466 my ($w,$count) = @_;
1467 my ($l,$c) = $w->index('end');
1468 my ($slimit,$elimit) = $w->yview;
1469 # get current page top and bottom line coords
1470 $slimit=int($l*$slimit)+1;
1471 $slimit="$slimit.0";
1472 $elimit=int($l*$elimit);
1473 $elimit="$elimit.0";
1474 # position insert cursor at text begin/end if the text is scrolled to begin/end
1475 if($count < 0 && $w->compare($slimit,'<=','1.0')) {return('1.0');}
1476 elsif($count >= 0 && $w->compare($elimit,'>=','end')) {return($w->index('end'));}
1477 else {return $w->SUPER::ScrollPages($count);}
1478 }
1479
1480 sub movePageUp
1481 {
1482 my $w = shift;
1483
1484 $w->{LINESTART}=0;
1485 $w->SetCursor($w->ScrollPages(-1));
1486 }
1487
1488 sub selectToPageUp
1489 {
1490 my $w = shift;
1491
1492 $w->{LINESTART}=0;
1493 $Tk::selectionType='normal';
1494 $w->KeySelect($w->ScrollPages(-1));
1495 }
1496
1497 sub movePageLeft
1498 {
1499 my $w = shift;
1500
1501 $w->{LINESTART}=0;
1502 $w->xview('scroll',-1,'page');
1503 }
1504
1505 sub movePageDown
1506 {
1507 my $w = shift;
1508
1509 $w->{LINESTART}=0;
1510 $w->SetCursor($w->ScrollPages(1));
1511 }
1512
1513 sub selectToPageDown
1514 {
1515 my $w = shift;
1516
1517 $w->{LINESTART}=0;
1518 $Tk::selectionType='normal';
1519 $w->KeySelect($w->ScrollPages(1));
1520 }
1521
1522 sub movePageRight
1523 {
1524 my $w = shift;
1525
1526 $w->{LINESTART}=0;
1527 $w->xview('scroll',1,'page');
1528 }
1529
1530 sub setSelectionMark
1531 {
1532 my $w = shift;
1533
1534 $w->{LINESTART}=0;
1535 $w->markSet('anchor','insert');
1536 }
1537
1538 sub selectToMark
1539 {
1540 my $w = shift;
1541
1542 $w->{LINESTART}=0;
1543 $Tk::selectionType='normal';
1544 $w->SelectTo('insert','char');
1545 }
1546
1547 sub selectAll
1548 {
1549 my $w = shift;
1550
1551 $w->{LINESTART}=0;
1552 $Tk::selectionType='normal';
1553 $w->tag('add','sel','1.0','end');
1554 }
1555
1556 sub selectionShiftLeft
1557 {
1558 my $w = shift;
1559
1560 $w->{LINESTART}=0;
1561 $w->_SelectionShift(" ","left");
1562 }
1563
1564 sub selectionShiftLeftTab
1565 {
1566 my $w = shift;
1567
1568 $w->{LINESTART}=0;
1569 $w->_SelectionShift("\t","left");
1570 }
1571
1572 sub selectionShiftRight
1573 {
1574 my $w = shift;
1575
1576 $w->{LINESTART}=0;
1577 $w->_SelectionShift(" ","right");
1578 }
1579
1580 sub selectionShiftRightTab
1581 {
1582 my $w = shift;
1583
1584 $w->{LINESTART}=0;
1585 $w->_SelectionShift("\t","right");
1586 }
1587
1588 sub _SelectionShift
1589 {
1590 my ($w,$type,$dir) = @_;
1591 return if $w->cget('-readonly');
1592
1593 if((!defined $type) || (!defined $dir)) {return;}
1594 if(!defined $w->tag('ranges','sel')) {return;}
1595
1596 my ($sline,$scol) = split('\.',$w->index('sel.first'));
1597 my ($eline,$ecol) = split('\.',$w->index('sel.last'));
1598
1599 my $col;
1600 if($Tk::selectionType eq 'rect') {$col=$scol;}
1601 else {$col=0;}
1602
1603 if($ecol == 0) {$eline--;}
1604
1605 my $s;
1606 $w->_BeginUndoBlock;
1607 if($dir eq "left") {
1608 if($scol != 0) {$scol--;}
1609 $w->delete("$sline.$scol");
1610 for(my $i=$sline+1;$i <= $eline;$i++) {
1611 $s="$i.$scol";
1612 if($w->compare($s,'==',$w->index("$s lineend"))) {next;}
1613 $w->delete("$i.$scol");
1614 $w->idletasks;
1615 }
1616 } elsif($dir eq "right") {
1617 $w->insert("$sline.$scol",$type);
1618 for(my $i=$sline+1;$i <= $eline;$i++) {
1619 # $w->insert("$i.$scol",$type);
1620 $s="$i.$scol";
1621 $w->markSet('undopos' => $s);
1622 $w->SUPER::insert($s,$type);
1623 $w->_AddUndo('delete',$s,$w->index('undopos'));
1624 $w->idletasks;
1625 }
1626 }
1627 $w->_EndUndoBlock;
1628 }
1629
1630 sub ins
1631 {
1632 my $w = shift;
1633 return if $w->cget('-readonly');
1634
1635 $w->{LINESTART}=0;
1636 if($w->{INSERTMODE} eq 'insert') {$w->{INSERTMODE}='overwrite';}
1637 elsif($w->{INSERTMODE} eq 'overwrite') {$w->{INSERTMODE}='insert';}
1638 }
1639
1640 sub enter
1641 {
1642 my $w = shift;
1643 return if $w->cget('-readonly');
1644
1645 $w->_BeginUndoBlock;
1646 Tk::catch {$w->Insert("\n")};
1647 if($w->cget('-indentmode') eq 'auto') {
1648 $w->_AutoIndent;
1649 }
1650 $w->_EndUndoBlock;
1651 }
1652
1653 sub autoIndentEnter
1654 {
1655 my $w = shift;
1656 return if $w->cget('-readonly');
1657
1658 $w->_BeginUndoBlock;
1659 Tk::catch {$w->Insert("\n")};
1660 $w->_AutoIndent;
1661 $w->_EndUndoBlock;
1662 }
1663
1664 sub noAutoIndentEnter
1665 {
1666 my $w = shift;
1667 return if $w->cget('-readonly');
1668
1669 Tk::catch {$w->Insert("\n")};
1670 }
1671
1672 sub _AutoIndent
1673 {
1674 my $w = shift;
1675 return if $w->cget('-readonly');
1676 my ($line,$col) = split('\.',$w->index('insert'));
1677
1678 # no autoindent for first line
1679 if($line == 1) {return;}
1680 $line--;
1681 my $s=$w->get("$line.0","$line.end");
1682 if($s =~ /^(\s+)(\S*)/) {$s=$1;}
1683 else {$s='';}
1684 if($2) {
1685 $w->insert('insert linestart',$s);
1686 }
1687 }
1688
1689 sub del
1690 {
1691 my $w = shift;
1692
1693 $w->Delete;
1694 }
1695
1696 # overrides Tk::Text->Delete method
1697 sub Delete
1698 {
1699 my $w = shift;
1700 return if $w->cget('-readonly');
1701 my $sel = Tk::catch {$w->tag('nextrange','sel','1.0','end');};
1702
1703 if(defined $sel) {
1704 $w->deleteSelected;
1705 } else {
1706 $w->delete('insert');
1707 $w->see('insert');
1708 }
1709 }
1710
1711 sub backSpace
1712 {
1713 my $w = shift;
1714
1715 $w->Backspace;
1716 }
1717
1718 # overrides Tk::Text->Backspace method
1719 sub Backspace
1720 {
1721 my $w = shift;
1722 return if $w->cget('-readonly');
1723 my $sel = Tk::catch {$w->tag('nextrange','sel','1.0','end');};
1724
1725 if(defined $sel) {
1726 $w->deleteSelected;
1727 } elsif($w->compare('insert',"!=",'1.0')) {
1728 $w->delete('insert - 1c');
1729 $w->see('insert');
1730 }
1731 }
1732
1733 sub deleteToWordStart
1734 {
1735 my $w = shift;
1736 return if $w->cget('-readonly');
1737
1738 if($w->compare('insert','==','insert wordstart')) {
1739 $w->delete('insert - 1c');
1740 } else {
1741 $w->delete('insert wordstart','insert');
1742 }
1743 }
1744
1745 sub deleteToWordEnd
1746 {
1747 my $w = shift;
1748 return if $w->cget('-readonly');
1749
1750 if($w->compare('insert','==','insert wordend')) {
1751 $w->delete('insert');
1752 } else {
1753 $w->delete('insert','insert wordend');
1754 }
1755 }
1756
1757 sub deleteToLineStart
1758 {
1759 my $w = shift;
1760 return if $w->cget('-readonly');
1761
1762 if($w->compare('insert','==','1.0')) {return;}
1763 if($w->compare('insert','==','insert linestart')) {
1764 $w->delete('insert - 1c');
1765 } else {
1766 $w->delete('insert linestart','insert');
1767 }
1768 }
1769
1770 sub deleteToLineEnd
1771 {
1772 my $w = shift;
1773 return if $w->cget('-readonly');
1774
1775 if($w->compare('insert','==','insert lineend')) {
1776 $w->delete('insert');
1777 } else {
1778 $w->delete('insert','insert lineend');
1779 }
1780 }
1781
1782 sub deleteWord
1783 {
1784 my $w = shift;
1785 return if $w->cget('-readonly');
1786
1787 $w->delete('insert wordstart','insert wordend');
1788 }
1789
1790 sub deleteLine
1791 {
1792 my $w = shift;
1793 return if $w->cget('-readonly');
1794
1795 $w->delete('insert linestart','insert lineend + 1c');
1796 $w->markSet('insert','insert linestart');
1797 }
1798
1799 sub insertControlCode
1800 {
1801 my $w = shift;
1802 return if $w->cget('-readonly');
1803
1804 $w->{LINESTART}=0;
1805 $w->{ASCIICODE} = 1;
1806 }
1807
1808 #sub focusNext
1809 #{
1810 # my $w = shift;
1811 #
1812 # $w->focusNext;
1813 #}
1814 #
1815 #sub focusPrev
1816 #{
1817 # my $w = shift;
1818 #
1819 # $w->focusPrev;
1820 #}
1821
1822 # find a matching char for the given one
1823 sub _FindMatchingChar
1824 {
1825 my ($w,$sc,$pos,$slimit,$elimit) = @_;
1826 my $mc = ${$w->{MATCHINGCOUPLES}->{$sc}}[0]; # char to search
1827
1828 if(!defined $mc) {return undef;}
1829
1830 my $dir = ${$w->{MATCHINGCOUPLES}->{$sc}}[1]; # forward or backward search
1831 my $spos=($dir == 1 ? $w->index("$pos + $dir c") : $w->index($pos));
1832 my $d=1;
1833 my ($p,$c);
1834 my $match;
1835
1836 if($dir == 1) { # forward search
1837 $match="[\\$mc|\\$sc]+";
1838 for($p=$spos;$w->compare($p,'<',$elimit);$p=$w->index("$p + 1c")) {
1839 $p=$w->SUPER::search('-forwards','-regex','--',$match,$p,$elimit);
1840 if(!defined $p) {return undef;}
1841 $c=$w->get($p);
1842 if($c eq $mc) {
1843 $d--;
1844 if($d == 0) {
1845 return $p;
1846 }
1847 } elsif($c eq $sc) {
1848 $d++;
1849 }
1850 Tk::DoOneEvent(Tk::DONT_WAIT);
1851 }
1852 } else { # backward search
1853 $match="[\\$sc|\\$mc]+";
1854 for($p=$spos;$w->compare($p,'>=',$slimit);) {
1855 $p=$w->SUPER::search('-backwards','-regex','--',$match,$p,$slimit);
1856 if(!defined $p) {return undef;}
1857 $c=$w->get($p);
1858 if($c eq $mc) {
1859 $d--;
1860 if($d == 0) {
1861 return $p;
1862 }
1863 } elsif($c eq $sc) {
1864 $d++;
1865 }
1866 if($w->compare($p,'==','1.0')) {return undef;}
1867 Tk::DoOneEvent(Tk::DONT_WAIT);
1868 }
1869 }
1870 return undef;
1871 }
1872
1873 sub flashMatchingChar
1874 {
1875 my $w = shift;
1876 my $s = $w->index('insert');
1877 my $str = $w->get('insert');
1878
1879 if(exists $w->{MATCHINGCOUPLES}->{$str}) {
1880 my $i=$w->_FindMatchingChar($str,$s,"1.0","end");
1881 if(defined $i) {
1882 my $sel = Tk::catch {$w->tag('nextrange','match','1.0','end');};
1883 if(defined $sel) {$w->tag('remove','match','match.first');}
1884 $w->tag('add','match',$i,$w->index("$i + 1c"));
1885 my $t=$w->cget('-matchhighlighttime');
1886 if($t != 0) {$w->after($t,[\&removeMatch,$w,$i]);}
1887 return $i;
1888 }
1889 }
1890 return undef;
1891 }
1892
1893 sub findMatchingChar
1894 {
1895 my $w = shift;
1896 my $i = $w->flashMatchingChar;
1897
1898 if(defined $i) {$w->see($i);}
1899 }
1900
1901 sub jumpToMatchingChar
1902 {
1903 my $w = shift;
1904 my $i = $w->flashMatchingChar;
1905
1906 if(defined $i) {$w->SetCursor($i);}
1907 }
1908
1909
1910 sub escape
1911 {
1912 my $w = shift;
1913 $w->tag('remove','sel','1.0','end');
1914 }
1915
1916 sub tab
1917 {
1918 my $w = shift;
1919 return if $w->cget('-readonly');
1920
1921 $w->Insert("\t");
1922 $w->focus;
1923 $w->break;
1924 }
1925
1926 sub leftTab
1927 {
1928 }
1929
1930 sub copy
1931 {
1932 my $w = shift;
1933
1934 Tk::catch{$w->clipboardCopy;};
1935 }
1936
1937 sub cut
1938 {
1939 my $w = shift;
1940 return if $w->cget('-readonly');
1941
1942 Tk::catch{$w->clipboardCut;};
1943 $w->see('insert');
1944 }
1945
1946 sub paste
1947 {
1948 my $w = shift;
1949 return if $w->cget('-readonly');
1950
1951 Tk::catch{$w->clipboardPaste;};
1952 $w->see('insert');
1953 }
1954
1955 sub inlinePaste
1956 {
1957 my $w = shift;
1958 return if $w->cget('-readonly');
1959 my ($l,$c) = split('\.',$w->index('insert'));
1960 my $str;
1961 my $f=0;
1962 Tk::catch{$str=$w->clipboardGet;};
1963
1964 if($str eq "") {return;}
1965 $w->_BeginUndoBlock;
1966 while($str =~ /(.*)\n+/g) {
1967 $w->insert("$l.$c",$1);
1968 if($f == 0) {
1969 my ($el,$ec) = split('\.',$w->index('end'));
1970 if($l == $el) {
1971 $w->insert('end',"\n");
1972 $f=1;
1973 }
1974 } else {$w->insert('end',"\n");}
1975 $l++;
1976 $w->idletasks;
1977 }
1978 $w->_EndUndoBlock;
1979 $w->see('insert');
1980 }
1981
1982 sub destroy
1983 {
1984 my $w = shift;
1985
1986 $w->Destroy;
1987 }
1988
1989 sub keyPress
1990 {
1991 my $w = shift;
1992 return if $w->cget('-readonly');
1993 my $ev = $w->XEvent;
1994
1995 $w->Insert($ev->A);
1996 }
1997
1998 sub menuSelect
1999 {
2000 my $w = shift;
2001 #NOTE: (JWT) ALSO FIXED IN auto/Tk/Text/SuperText/menuSelect.al!!!!!
2002
2003 #+20010117 JWT don't do these 2 lines in windows
2004 unless ($^O =~ /Win/)
2005 {
2006 my $ev = $w->XEvent;
2007
2008 $w->TraverseToMenu($ev->K);
2009 }
2010 #+
2011 }
2012
2013 sub noOP
2014 {
2015 my $w = shift;
2016 $w->NoOp;
2017 }
2018
2019 1;
2020 __END__
↑ Summary