Edit.pm untested

filter: Statementstmt 0.0% 0 / 2072 Branchbran 0.0% 0 / 1114 Conditioncond 0.0% 0 / 464 MC/DCmcdc 0.0% 0 / 274 Subroutinesub 0.0% 0 / 159 Podpod 0.0% 0 / 149 totaltotal 0.0% 0 / 4232 CC 558 SCAR 59.0
CC
558
Cov
0%
on_paint
2070.0
set_selection
1806.0
set_line
1190.0
CRAP 364.9
1 ################################################################################
2 # This is CodeManager
3 # Copyright 2009-2013 by Waldemar Biernacki
4 # http://codemanager.sao.pl\n" .
5 #
6 # License statement:
7 #
8 # This program/library is free software; you can redistribute it
9 # and/or modify it under the same terms as Perl itself.
10 #
11 # Last modified (DMYhms): 14-01-2013 07:15:21.
12 ################################################################################
13  
14 use strict;
15 use warnings;
16  
17 package bt;
18 use constant CUA => 0;
19 use constant Vertical => 1;
20 use constant Horizontal => 2;
21  
22 package Prima::CodeManager::Edit;
23 use vars qw(@ISA);
24 @ISA = qw(Prima::Widget Prima::MouseScroller Prima::GroupScroller);
25  
26 use Prima::Const;
27 use Prima::Classes;
28 use Prima::ScrollBar;
29 use Prima::IntUtils;
30  
31 {
32 my %RNT = (
33 %{Prima::Widget-> notification_types()},
34 ParseSyntax => nt::Action,
35 );
36  
37 sub notification_types { return \%RNT; }
38 }
39  
40 #-------------------------------------------------------------------------------
41  
42 sub profile_default
43 {
44 my %def = %{$_[ 0]-> SUPER::profile_default};
45 my $font = $_[ 0]-> get_default_font;
46  
47 return {
48 %def,
49 accelItems => [
50 # navigation
51 [ CursorDown => 0, 0, kb::Down , sub{$_[0]-> cursor_down}],
52 [ CursorUp => 0, 0, kb::Up , sub{$_[0]-> cursor_up}],
53 [ CursorLeft => 0, 0, kb::Left , sub{$_[0]-> cursor_left}],
54 [ CursorRight => 0, 0, kb::Right , sub{$_[0]-> cursor_right}],
55 [ PageUp => 0, 0, kb::PgUp , sub{$_[0]-> cursor_pgup}],
56 [ PageDown => 0, 0, kb::PgDn , sub{$_[0]-> cursor_pgdn}],
57 [ Home => 0, 0, kb::Home , sub{$_[0]-> cursor_home}],
58 [ End => 0, 0, kb::End , sub{$_[0]-> cursor_end}],
59 [ CtrlPageUp => 0, 0, kb::PgUp|km::Ctrl , sub{$_[0]-> cursor_cpgup}],
60 [ CtrlPageDown => 0, 0, kb::PgDn|km::Ctrl , sub{$_[0]-> cursor_cpgdn}],
61 [ CtrlHome => 0, 0, kb::Home|km::Ctrl , sub{$_[0]-> cursor_chome}],
62 [ CtrlEnd => 0, 0, kb::End |km::Ctrl , sub{$_[0]-> cursor_cend}],
63 [ WordLeft => 0, 0, kb::Left |km::Ctrl , sub{$_[0]-> word_left}],
64 [ WordRight => 0, 0, kb::Right|km::Ctrl , sub{$_[0]-> word_right}],
65 [ ShiftCursorDown => 0, 0, km::Shift|kb::Down , q(cursor_shift_key)],
66 [ ShiftCursorUp => 0, 0, km::Shift|kb::Up , q(cursor_shift_key)],
67 [ ShiftCursorLeft => 0, 0, km::Shift|kb::Left , q(cursor_shift_key)],
68 [ ShiftCursorRight => 0, 0, km::Shift|kb::Right , q(cursor_shift_key)],
69 [ ShiftPageUp => 0, 0, km::Shift|kb::PgUp , q(cursor_shift_key)],
70 [ ShiftPageDown => 0, 0, km::Shift|kb::PgDn , q(cursor_shift_key)],
71 [ ShiftHome => 0, 0, km::Shift|kb::Home , q(cursor_shift_key)],
72 [ ShiftEnd => 0, 0, km::Shift|kb::End , q(cursor_shift_key)],
73 [ ShiftCtrlPageUp => 0, 0, km::Shift|kb::PgUp|km::Ctrl , q(cursor_shift_key)],
74 [ ShiftCtrlPageDown => 0, 0, km::Shift|kb::PgDn|km::Ctrl , q(cursor_shift_key)],
75 [ ShiftCtrlHome => 0, 0, km::Shift|kb::Home|km::Ctrl , q(cursor_shift_key)],
76 [ ShiftCtrlEnd => 0, 0, km::Shift|kb::End |km::Ctrl , q(cursor_shift_key)],
77 [ ShiftWordLeft => 0, 0, km::Shift|kb::Left |km::Ctrl , q(cursor_shift_key)],
78 [ ShiftWordRight => 0, 0, km::Shift|kb::Right|km::Ctrl , q(cursor_shift_key)],
79 [ Insert => 0, 0, kb::Insert , sub {$_[0]-> insertMode(!$_[0]-> insertMode)}],
80 # edit keys
81 [ Delete => 0, 0, kb::Delete, sub {
82 return if $_[0]-> {readOnly};
83 $_[0]-> has_selection ? $_[0]-> delete_block : $_[0]-> delete_char;
84 }],
85 [ Backspace => 0, 0, kb::Backspace, sub {$_[0]-> back_char unless $_[0]-> {readOnly}}],
86 [ DeleteChunk => 0, 0, '^Y', sub {$_[0]-> delete_current_chunk unless $_[0]-> {readOnly}}],
87 [ DeleteToEnd => 0, 0, '^E', sub {$_[0]-> delete_to_end unless $_[0]-> {readOnly}}],
88 [ DupLine => 0, 0, '^K', sub {$_[0]-> insert_line($_[0]-> cursorY, $_[0]-> get_line($_[0]-> cursorY)) unless $_[0]-> {readOnly}}],
89 [ DeleteBlock => 0, 0, '@D', sub {$_[0]-> delete_block unless $_[0]-> {readOnly}}],
90 [ SplitLine => 0, 0, kb::Enter, sub {$_[0]-> split_line if !$_[0]-> {readOnly} && $_[0]-> {wantReturns}}],
91 [ SplitLine2 => 0, 0, km::Ctrl|kb::Enter,sub {$_[0]-> split_line if !$_[0]-> {readOnly} && !$_[0]-> {wantReturns}}],
92 # block keys
93 [ CancelBlock => 0, 0, '@U', q(cancel_block)],
94 [ MarkVertical => 0, 0, '@B', q(mark_vertical)],
95 [ MarkHorizontal => 0, 0, '@L', q(mark_horizontal)],
96 [ CopyBlock => 0, 0, '@C', q(copy_block)],
97 [ OvertypeBlock => 0, 0, '@O', q(overtype_block)],
98 # clipboard keys
99 [ Cut => 0, 0, km::Shift|kb::Delete, q(cut)],
100 [ Copy => 0, 0, km::Ctrl |kb::Insert, q(copy)],
101 [ Paste => 0, 0, km::Shift|kb::Insert, q(paste)],
102 [ CutMS => 0, 0, '^X', q(cut)],
103 [ CopyMS => 0, 0, '^C', q(copy)],
104 [ PasteMS => 0, 0, '^V', q(paste)],
105 # undo
106 [ Undo => 0, 0, '^Z', q(undo)],
107 [ Undo => 0, 0, km::Alt|kb::Backspace, q(undo)],
108 # [ Redo => 0, 0, '^R', q(redo)],
109 ],
110 autoIndent => 1,
111 autoHScroll => 1,
112 autoVScroll => 1,
113 blockType => bt::CUA,
114 borderWidth => 1,
115 cursorSize => [ $::application-> get_default_cursor_width, $font-> { height}],
116 cursorVisible => 1,
117 cursorX => 0,
118 cursorY => 0,
119 cursorWrap => 0,
120 insertMode => 0,
121 hiliteNumbers => cl::Green,
122 hiliteQStrings => cl::LightBlue,
123 hiliteQQStrings => cl::LightBlue,
124 hiliteIDs => [[qw(
125 abs accept alarm atan2 bind binmode bless caller chdir chmod chomp chop chown
126 chr chroot close closedir connect continue cos crypt defined
127 delete die do dump each endgrent endhostent endnetent endprotoent endpwent
128 endservent eof eval exec exists exit exp fcntl fileno flock for fork format
129 formline getc getgrent getgrgid getgrnam gethostbyaddr gethostbyname gethostent
130 getlogin getnetbyaddr getnetbyname getnetent getpeername getpgrp getppid
131 getpriority getprotobyname getprotobynumber getprotoent getpwent getpwnam
132 getpwuid getservbyname getservbyport getservent getsockname getsockopt glob
133 gmtime goto grep hex if import index int ioctl join keys kill last lc lcfirst
134 length link listen local localtime log lstat m map mkdir msgctl msgget msgrcv
135 msgsnd my next no oct open opendir ord our pack package pipe pop pos print
136 printf prototype push q qq qr quotemeta qw qx rand read readdir readline
137 readlink readpipe recv redo ref rename require reset return reverse rewinddir
138 rindex rmdir s scalar seek seekdir select semctl semget semop send setgrent
139 sethostent setnetent setpgrp setpriority setprotoent setpwent setservent
140 setsockopt shift shmctl shmget shmread shmwrite shutdown sin sleep socket
141 socketpair sort splice split sprintf sqrt srand stat study sub substr symlink
142 syscall sysopen sysread sysseek system syswrite tell telldir tie tied time
143 times tr truncate uc ucfirst umask undef unless unlink unpack unshift untie use
144 utime values vec wait waitpid wantarray warn while write y
145 )], cl::Blue, cl::White],
146 hiliteChars => ['~!@#$%^&*()+-=[]{};:\'"\\|?.,<>/`', cl::Blue, cl::White],
147 hiliteREs => [ '(#.*)$', {color => cl::Gray},],
148 hiliteCase => 0,
149 hiliteStyl => 0,
150 hiliteBlok => [],
151 hScroll => 0,
152 markers => [],
153 modified => 0,
154 offset => 0,
155 pointerType => cr::Text,
156 persistentBlock => 0,
157 readOnly => 0,
158 selection => [0, 0, 0, 0],
159 selStart => [0, 0],
160 selEnd => [0, 0],
161 selectable => 1,
162 syntaxHilite => 0,
163 tabIndent => 4,
164 textRef => undef,
165 topLine => 0,
166 vScroll => 0,
167 undoLimit => 1000,
168 wantTabs => 1,
169 wantReturns => 1,
170 widgetClass => wc::Edit,
171 wordDelimiters => ".()\"'\`,$@!%^&*{}[]?/|;:<>-= \xff\t#",
172 wordWrap => 0,
173  
174 lineSpace => 0,
175 wheelRows => 1,
176 exportHTML => 0,
177 }
178 }
179  
180 #-------------------------------------------------------------------------------
181  
182 sub profile_check_in
183 {
184 my ( $self, $p, $default) = @_;
185 $self-> SUPER::profile_check_in( $p, $default);
186  
187 $self->{hiliteCase} = $p->{hiliteCase}; $self->{hiliteCase} += 0; $self->{hiliteCase} = 0 unless $self->{hiliteCase};
188 $self->{hiliteStyl} = $p->{hiliteStyl}; $self->{hiliteStyl} += 0; $self->{hiliteStyl} = 0 unless $self->{hiliteStyl};
189 $self->{hiliteBlok} = $p->{hiliteBlok}; $self->{hiliteBlok} = [] unless $self->{hiliteBlok};
190  
191 $self->{lineSpace} = $p->{lineSpace} || 0;
192 $self->{wheelRows} = $p->{wheelRows} || 1;
193 $self->{exportHTML} = $p->{exportHTML}|| 0;
194  
195 if ( exists( $p-> { selection}))
196 {
197 my $s = $p-> {selection};
198 $p-> {selStart} = [$$s[0], $$s[1]];
199 $p-> {selEnd } = [$$s[2], $$s[3]];
200 }
201 $p-> { text} = '' if exists( $p-> {textRef});
202 $p-> {autoHScroll} = 0 if exists $p-> {hScroll};
203 $p-> {autoVScroll} = 0 if exists $p-> {vScroll};
204 }
205  
206 #-------------------------------------------------------------------------------
207  
208 sub init
209 {
210 my $self = shift;
211  
212 for ( qw( autoIndent topLine offset resetDisabled blockType persistentBlock
213 tabIndent readOnly wantReturns wantTabs
214 ))
215 { $self-> {$_} = 1; }
216 for ( qw( wordWrap hScroll vScroll rows maxLineCount maxLineLength maxLineWidth
217 scrollTransaction maxLine maxChunk capLen cursorY cursorX cursorWrap
218 cursorXl cursorYl syntaxHilite hiliteNumbers hiliteQStrings hiliteQQStrings
219 notifyChangeLock modified borderWidth autoHScroll autoVScroll blockShiftMark
220 ))
221 { $self-> {$_} = 0;}
222 $self-> { insertMode} = $::application-> insertMode;
223 for ( qw( markers lines chunkMap hiliteIDs hiliteChars hiliteREs)) { $self-> {$_} = []}
224 for ( qw( selStart selEnd selStartl selEndl)) { $self-> {$_} = [0,0]}
225 $self-> {defcw} = $::application-> get_default_cursor_width;
226 my %profile = $self-> SUPER::init(@_);
227 $self-> setup_indents;
228 $self-> {undo} = [];
229 $self-> {redo} = [];
230 $profile{selection} = [@{$profile{selStart}}, @{$profile{selEnd}}];
231 for ( qw( hiliteNumbers hiliteQStrings hiliteQQStrings hiliteIDs hiliteChars hiliteREs
232 autoHScroll autoVScroll
233 textRef syntaxHilite autoIndent persistentBlock blockType hScroll vScroll borderWidth
234 topLine tabIndent readOnly offset wordDelimiters wantTabs wantReturns
235 wordWrap cursorWrap markers undoLimit))
236 { $self-> $_( $profile{ $_}); }
237 delete $self-> {resetDisabled};
238 $self-> {uChange} = 0;
239 $self-> reset;
240 $self-> selection( @{$profile{selection}});
241 for ( qw( cursorX cursorY))
242 { $self-> $_( $profile{ $_}); }
243 $self-> reset_scrolls;
244 $self-> {modified} = 0;
245  
246 $self-> {hiliteBlok_modified} = 0;
247 $self-> area;
248  
249 return %profile;
250 }
251  
252 #-------------------------------------------------------------------------------
253  
254 sub area
255 {
256 my ( $self, $it0, $rows ) = ( shift, shift, shift );
257  
258 return unless @{$self->{hiliteBlok}};
259  
260 $it0 ||= 0;
261 $rows ||= scalar @{$self-> {lines}};
262  
263 $self->{hiliteBlok_modified} = 0 if $it0 == 0 && $rows == scalar @{$self-> {lines}};
264  
265 #print "area( $it0 , $rows )\n";
266  
267 my $lev = 0;
268 $lev = $self->{hiliteBlok_value}->[$it0] if $it0 > 0;
269  
270 splice(@{$self->{hiliteBlok_value}},$it0) if $self->{hiliteBlok_value} && @{$self->{hiliteBlok_value}} > $it0;
271 my $con = \$self->text;
272  
273 my $blok_beg = $self->{hiliteBlok}->[0];
274 my $blok_end = $self->{hiliteBlok}->[1];
275  
276 for (my $ite = $it0 ; $ite < $it0 + $rows + 2 ; $ite++ ) { # $$con =~ m/^(.*)$/mg ) {
277 my $row = $self-> {lines}->[$ite] || '';
278 if ( $row =~ /($blok_beg|$blok_end)/ || $lev ) {
279 $lev = 1;
280 $self->{hiliteBlok_value}->[$ite] = 1;
281 }
282 $lev = 0 if $row =~ /$blok_end/;
283 }
284 }
285  
286 #-------------------------------------------------------------------------------
287 #-------------------------------------------------------------------------------
288 #-------------------------------------------------------------------------------
289  
290 sub reset
291 {
292 my $self = $_[0];
293 return if $self-> {resetDisabled};
294 my @a = ( $self-> {indents}-> [0], $self-> {indents}-> [1]);
295 my @size = $self-> get_active_area(2);
296 my $cw = $self-> {defcw};
297 my $ti = $self-> {tabIndent};
298 my $uC = $self-> {uChange};
299 my $mw;
300 $size[0] -= $cw;
301 if ( $uC < 2) {
302 $self-> {fixed} = $self-> font-> pitch == fp::Fixed;
303 $self-> {averageWidth} = $self-> font-> width;
304 $mw = $self-> {averageWidth};
305 $self-> {maxFixedLength} = int( $size[0] / $mw);
306 $self-> {tabs} = ' 'x$ti;
307 }
308  
309 # changes that apply to string output must issue recalculation here.
310 #
311 # Calculating wrap chunks ( if necessary) and build chunkMap.
312 # chunkMap is actual only in wordWrap = 1 mode; it maps lines to real lines.
313 # it's structure is: [ subline offset, subline length, line index].
314  
315 if ( $self-> {wordWrap}) {
316 if ( $uC < 2) {
317 my $twOpts = tw::WordBreak|tw::CalcTabs|tw::NewLineBreak|tw::ReturnChunks;
318 my @chunkMap;
319 $self-> begin_paint_info;
320 $#chunkMap = scalar @{$self-> {lines}} * 2;
321 @chunkMap = ();
322 my $j = 0;
323 for ( @{$self-> {lines}})
324 {
325 my $i;
326 my $breaks = $self-> text_wrap( $_, $size[0], $twOpts, $ti);
327 for ( $i = 0; $i < scalar @{$breaks} / 2; $i++)
328 {
329 # push( @chunkMap, $$breaks[$i * 2]);
330 # push( @chunkMap, $$breaks[$i * 2 + 1]);
331 # push( @chunkMap, $j);
332 push( @chunkMap, $$breaks[$i * 2], $$breaks[$i * 2 + 1], $j);
333 }
334 $j++;
335 }
336 $self-> end_paint_info;
337 $self-> {chunkMap} = \@chunkMap;
338 $self-> {maxLineWidth} = $size[0];
339 }
340 } else {
341  
342 # fast ( but not exact) calculation of maximal line width.
343 if ( $uC == 0) {
344 my $max = 0;
345 my $maxLinesCount = 0;
346 for ( @{$self-> {lines}}) {
347 my $l = length( $_);
348 $max = $l, $maxLinesCount = 0 if $max < $l;
349 $maxLinesCount++ if $l == $max;
350 }
351 $self-> {maxLineLength} = $max;
352 $self-> {maxLineCount} = $maxLinesCount;
353 }
354 if ( $uC < 2) {
355 $self-> {maxLineWidth} = $self-> {maxLineLength} * $mw;
356 }
357 }
358 my $fh = $self-> font-> height + $self->{lineSpace};
359 $self-> {rows} = int($size[1] / $fh);
360 my $yTail = $size[1] - $self-> {rows} * $fh;
361  
362 if ( $uC < 2) {
363 $self-> {maxLine} = scalar @{$self-> {lines}} - 1;
364 $self-> {maxChunk} = $self-> {wordWrap} ? (scalar @{$self-> {chunkMap}}/3-1) : $self-> {maxLine};
365 $self-> {yTail} = ( $yTail > 0) ? 1 : 0;
366 # updating selections
367 $self-> selection( @{$self-> {selStart}}, @{$self-> {selEnd}});
368 # updating cursor
369 $self-> cursor( $self-> cursor);
370 my $chunk = $self-> get_chunk( $self-> {cursorYl});
371 my $x = $self-> {cursorXl};
372 $self-> {cursorAtX} = $self-> get_chunk_width( $chunk, 0, $x);
373 $self-> {cursorInsWidth} = $self-> get_chunk_width( $chunk, $x, 1);
374 }
375 # positioning cursor
376 my $cx = $a[0] + $self-> {cursorAtX} - $self-> {offset};
377 my $cy = $a[1] + $yTail + ($self-> {rows} - $self-> {cursorYl} + $self-> {topLine } - 1) * $fh;
378 my $xcw = $self-> {insertMode} ? $cw : $self-> {cursorInsWidth};
379 my $ycw = $fh;
380 $ycw -= $a[1] - $cy, $cy = $a[1] if $cy < $a[1];
381 $xcw = $size[0] + $a[0] - $cx - 1 if $cx + $xcw >= $size[0] + $a[0];
382 $self-> cursorVisible( $xcw > 0);
383 if ( $xcw > 0) {
384 $self-> cursorPos( $cx, $cy);
385 $self-> cursorSize( $xcw, $ycw);
386 }
387 $self-> {uChange} = 0;
388  
389 }
390  
391 #-------------------------------------------------------------------------------
392  
393 sub reset_cursor
394 {
395 my $self = $_[0];
396 $self-> {uChange} = 2;
397 $self-> reset;
398 $self-> {uChange} = 0;
399 }
400  
401 #-------------------------------------------------------------------------------
402  
403 sub reset_render
404 {
405 my $self = $_[0];
406 $self-> {uChange} = 1;
407 $self-> reset;
408 $self-> {uChange} = 0;
409 }
410  
411 #-------------------------------------------------------------------------------
412  
413 sub reset_scrolls
414 {
415 my $self = $_[0];
416 return if $self-> {resetDisabled};
417 if ( $self-> {scrollTransaction} != 1) {
418 $self-> vScroll( $self-> {maxChunk} >= $self-> {rows}) if $self-> {autoVScroll};
419 $self-> {vScrollBar}-> set(
420 max => $self-> {maxChunk} - $self-> {rows} + 1,
421 pageStep => $self-> {rows},
422 whole => $self-> {maxChunk} + 1,
423 partial => $self-> {rows},
424 value => $self-> {topLine },
425 ) if $self-> {vScroll};
426 }
427 if ( $self-> {scrollTransaction} != 2) {
428 my $w = $self-> width - $self-> {indents}-> [0] - $self-> {indents}-> [2];
429 my $lw = $self-> {maxLineWidth};
430 if ( $self-> {autoHScroll}) {
431 my $hs = ( $lw > $w) ? 1 : 0;
432 if ( $hs != $self-> {hScroll}) {
433 $self-> hScroll( $hs);
434 $w = $self-> width - $self-> {indents}-> [0] - $self-> {indents}-> [2];
435 }
436 }
437 $self-> {hScrollBar}-> set(
438 max => $self-> {wordWrap} ? 0 : $lw - $w,
439 whole => $lw < $w ? $w : $lw,
440 value => $self-> {offset},
441 partial => $w,
442 pageStep => $lw / 5,
443 step => $self-> font-> width,
444 ) if $self-> {hScroll};
445 }
446 }
447  
448 #-------------------------------------------------------------------------------
449  
450 sub VScroll_Change
451 {
452 my ( $self, $scr) = @_;
453 return if $self-> {scrollTransaction};
454 $self-> {scrollTransaction} = 1;
455 $self-> topLine ( $scr-> value);
456 $self-> {scrollTransaction} = 0;
457  
458 #print "scroll\n" if $self-> {hiliteBlok_modified};
459 $self-> area if $self-> {hiliteBlok_modified};
460  
461 }
462  
463 #-------------------------------------------------------------------------------
464  
465 sub HScroll_Change
466 {
467 my ( $self, $scr) = @_;
468 return if $self-> {scrollTransaction};
469 $self-> {scrollTransaction} = 2;
470 $self-> offset( $scr-> value);
471 $self-> {scrollTransaction} = 0;
472 }
473  
474 #-------------------------------------------------------------------------------
475  
476 sub reset_syntax
477 {
478 my $self = $_[0];
479 if ( $self-> {syntaxHilite}) {
480 my ( $notifier, @notifyParms) = $self-> get_notify_sub(q(ParseSyntax));
481 my @syntax;
482 $#syntax = $self-> {maxLine};
483 @syntax = ();
484 my $i = 0;
485 $self-> push_event;
486 for ( @{$self-> {lines}}) {
487 my $sref = undef;
488 $notifier-> ( @notifyParms, $_, $sref);
489 push( @syntax, $sref);
490 last if $i++ > 50; # test speed...
491 }
492 $self-> pop_event;
493 $self-> {syntax} = \@syntax;
494 } else {
495 $self-> {syntax} = undef;
496 }
497 }
498  
499 #-------------------------------------------------------------------------------
500  
501 sub reset_syntaxer
502 {
503 my $self = $_[0];
504 unless ( $self-> {hiliteREs} ) {
505 $self-> {syntaxer} = sub {$_[2]=[];};
506 } else {
507 my $ic = $self->{hiliteCase} ? 'i' : '';
508 my @doers;
509 my $rest = 'push @a, $l, cl::Fore if $l; $l = 0;';
510 if ($self-> {hiliteREs}) {
511 my $i;
512  
513 for ($i = 0; $i < scalar @{$self-> {hiliteREs}} - 1; $i+=2) {
514 next unless ref $self-> {hiliteREs}-> [$i+1] eq 'HASH';
515 my $re = $self-> {hiliteREs}-> [$i];
516 push @doers, "/\\G$re/gc$ic && do { " .
517 $rest . 'push @a, length($1), ' .
518 $self-> {hiliteREs}-> [$i+1]-> {color} ."; redo; };\n";
519 }
520 }
521  
522 $self-> {syntaxer} = eval(<<SYNTAXER);
523 sub {
524 my ( \$self, \$line) = \@_;
525 my \@a;
526 my \$l = 0;
527 \$_ = \$line; study;
528 {
529 @doers
530 /\\G(.)/gc && do { \$l++; redo; };
531 }
532 $rest
533 \$_[2] = \\\@a;
534 };
535 SYNTAXER
536 }
537 }
538  
539 #-------------------------------------------------------------------------------
540  
541 sub wstaw_tab_15 {
542 my ( $line, $from, $len, $tab, $pos ) = @_;
543 $line = '' unless defined $line;
544 $from = 0 unless $from;
545 $len = 0 unless $len;
546 my $chunk = substr($line.(' 'x($len+$from)),$from,$len);
547 my $sm = '';
548 $chunk = '' unless defined $chunk;
549  
550 $line = substr($line,0,$from);
551 while ( $line =~ /^([^\t]*)\t/g ) {
552 $tab = ' 'x(4-(length($1)%4));
553 $line =~ s/\t/$tab/;
554 }
555 my $fl = length($line);
556 while ( $chunk =~ /^([^\t]*)\t/g ) {
557 $tab = ' 'x(3-(($fl+length($1))%4));
558 $chunk =~ s/\t/\.$tab/;
559 $sm = '.';
560 }
561 return ( $chunk, $sm );
562 }
563  
564 #-------------------------------------------------------------------------------
565  
566 sub wstaw_tab {
567 my ( $row, $pos ) = @_;
568 while($row =~ /^([^\t]*)\t/g ) {
569 my $tab = ' 'x(4-(length($1)%4));
570 $row =~ s/\t/$tab/;
571 }
572 return $row;
573 }
574  
575 #-------------------------------------------------------------------------------
576 my $_HTML_FILE = undef;
577 my $_HTML_LAST_COLOR = 0;
578 my %_HTML_COLOR;
579 #-------------------------------------------------------------------------------
580  
581  
582 sub draw_colorchunk
583 {
584 my ( $self, $canvas, $chunk, $i, $x, $y, $clr, $which_color ) = @_;
585  
586 #print "draw_colorchunk=$self\n";
587  
588 my $sd = $self-> {syntax}-> [$i];
589 unless ( defined $sd) {
590 $self-> notify(q(ParseSyntax), $chunk, $sd);
591 $self-> {syntax}-> [$i] = $sd;
592 }
593 #return;
594  
595 my $backColor = 0;
596 my $fontColor = 0;
597 my $fontStyle = 0;
598 my $fontSize = 0;
599  
600 my $ofs = 0;
601 my $cc;
602  
603 for ( my $j = 0; $j < scalar @{$sd} - 1; $j += 2 ) {
604 my $xd = $self-> get_chunk_width ( $chunk, $ofs, $$sd[$j], \$cc);
605 ( $cc, undef ) = wstaw_tab_15 ( $chunk, $ofs, $$sd[$j], $self->{tabs}, 1 );
606 for ( my $f = 0 ; $f < scalar @{$self-> {hiliteREs}}; $f += 2 ) {
607 next unless ref $self-> {hiliteREs}->[$f+1] eq 'HASH';
608 my $wzor = $self-> {hiliteREs}-> [$f];
609 if ( $cc =~ /$wzor/ ) {
610  
611 if ( $self-> {hiliteREs}-> [$f+1]-> {backColor} ) {
612 $canvas-> color ( $self-> {hiliteREs}-> [$f+1]-> {backColor} );
613 $canvas-> bar (
614 $x, $y - int( 0.5 * $self->{lineSpace} ),
615 $x + $xd, $y + $self-> font-> height + $self->{lineSpace} - int( 0.5 * $self->{lineSpace} ) );
616 $backColor = 1;
617 }
618  
619 if ( $self->{hiliteREs}-> [$f+1]-> {style} ) {
620 $canvas-> set ( font => { style => $self-> {hiliteREs}-> [$f+1]-> {style} } );
621 $fontStyle = 1;
622 }
623  
624 if ( $self->{hiliteREs}->[$f+1]->{size} ) {
625 $canvas-> set ( font => { size => $self-> {hiliteREs}->[$f+1]->{size} });
626 $fontSize = 1;
627 }
628 }
629 last if $backColor && $self-> {hiliteREs}-> [$f+1]-> {last};
630 }
631  
632 my $tekscik = $cc;
633 my $kolorek = $$sd[$j+1];
634  
635 if ( $self->{hiliteBlok_value}->[$i] ) {
636 if ( $self->{hiliteBlok}->[3] ) {
637 $canvas-> color ( $self->{hiliteBlok}->[3] );
638 $canvas-> bar (
639 $x, $y - int( 0.5 * $self->{lineSpace} ),
640 $x + $xd, $y + $self-> font-> height + $self->{lineSpace} - int( 0.5 * $self->{lineSpace} ),
641 );
642 $backColor = 1;
643 }
644 $canvas-> color ( $self->{hiliteBlok}->[2] );
645 $kolorek = $self->{hiliteBlok}->[2];
646  
647 } else {
648 $canvas-> color (( $$sd[$j+1] == cl::Fore) ? $clr : $$sd[$j+1] );
649 $kolorek = $$sd[$j+1] == cl::Fore ? $clr : $$sd[$j+1];
650 }
651  
652 if ( $_HTML_FILE ) {
653 $tekscik =~ s/>/gt;/g;
654 $tekscik =~ s/</lt;/g;
655 my $html_kolor = substr('000000'.sprintf("%x",$kolorek),-6);
656 if ( $html_kolor eq '000000' ) {
657 print $_HTML_FILE $cc;
658 } else {
659 print $_HTML_FILE '<span style="color:#'.$html_kolor.'">'.$cc.'</span>';
660 }
661 }
662 $canvas-> text_out( $cc, $x, $y);
663 $canvas-> set( font => { style => fs::Normal }) if $fontStyle;
664 $canvas-> backColor( cl::Normal ) if $backColor;
665 $canvas-> color( cl::Fore );
666 $x += $xd;
667 $ofs += $$sd[$j];
668 }
669  
670 }
671  
672 #-------------------------------------------------------------------------------
673 #-------------------------------------------------------------------------------
674 #-------------------------------------------------------------------------------
675  
676 sub on_paint
677 {
678 # local variables definition area
679 my ( $self, $canvas) = @_;
680  
681 #print "on_paint\n";
682  
683 my @size = $canvas-> size;
684 my @clr = $self-> enabled ?
685 ( $self-> color, $self-> backColor) :
686 ( $self-> disabledColor, $self-> disabledBackColor);
687 my @sclr = ( $self-> hiliteColor, $self-> hiliteBackColor);
688  
689 my ( $bw, $fh, $tl, $lc, $rc, $ofs, $yt, $tabs, $cw, $bt, $issel, $sh, $sx) =
690 ( $self-> {borderWidth}, $self-> font-> height + $self->{lineSpace},
691 $self-> {topLine}, $self-> {maxChunk}+1, $self-> {rows}, $self-> {offset},
692 $self-> {yTail}, $self-> {tabs}, $self-> {defcw}, $self-> {blockType},
693 $self-> has_selection, $self-> {syntaxHilite}, $self-> {syntax},
694 );
695 my @a = $self-> get_active_area( 0, @size);
696  
697 my $numer = $Prima::CodeManager::developer{notes} ? $Prima::CodeManager::developer{notes}->pageIndex : 0;
698  
699 if ( $Prima::CodeManager::developer{notes} && $Prima::CodeManager::developer{"notes_$numer"} && $self eq $Prima::CodeManager::developer{"notes_$numer"} ) {
700 if ( $Prima::CodeManager::developer{ "numer_$numer" } ) {
701 my $numbers = '';
702 for ( $self->{topLine} + 1 .. $self->{topLine} + $self->{rows} + 2 ) {
703 $numbers .= "$_\n";
704  
705 # if ( $self->{hiliteBlok_value}->[$_-1] ) {
706 # $numbers .= $_.':'.$self->{hiliteBlok_value}->[$_-1]."\n";
707 # } else {
708 # $numbers .= "$_ \n";
709 # }
710  
711 }
712 $Prima::CodeManager::developer{ "numer_$numer"}->set( text => $numbers );
713 }
714 }
715  
716 if ( $self->{exportHTML} > 0 ) {
717 open ( $_HTML_FILE, ">CodeManager.html" );
718  
719 print $_HTML_FILE '<pre style="font-family:Courier New;font-size:9pt;">'."\n";
720 print $_HTML_FILE '<a href="http://sao.pl" style="color:#cccccc;" target="NewWindow">Coloured by CodeManager</a>'."\n\n";
721 }
722  
723 ### $self->{hiliteBlokColorIndex} = -1;
724  
725  
726 # if ( scalar @{$self->{hiliteBlok}} ) {
727 # my $tresc = '';
728 # my $row = $self-> {topLine} - 1;
729 # while ( $row >= 0 ) {
730 # $tresc = @{$self-> {lines}}[$row];
731 # my $nr = 0;
732 ### while ( $self->{hiliteBgCo}->[ 4 * $nr ] ) {
733 # if ( $tresc =~ /$self->{hiliteBlok}->[4*$nr]/ ) {
734 # $self->{hiliteBlokColorIndex} = $nr;
735 # last;
736 # }
737 # if ( $tresc =~ /$self->{hiliteBlok}->[4*$nr+1]/ ) {
738 # $self->{hiliteBlokColorIndex} = -1;
739 # last;
740 # }
741 ### $nr++;
742 ### }
743 # $row--;
744 # }
745 # }
746  
747  
748 # drawing sheet
749 my @clipRect = $self-> clipRect;
750 if (
751 $clipRect[0] > $a[0] &&
752 $clipRect[1] > $a[1] &&
753 $clipRect[2] < $a[2] &&
754 $clipRect[3] < $a[3]
755 ) {
756 $canvas-> color( $clr[ 1]);
757 $canvas-> clipRect( $a[0], $a[1], $a[2] - 1, $a[3] - 1);
758  
759 $canvas-> bar( 0, 0, @size);
760 } else {
761 $self-> draw_border( $canvas, $clr[1], @size);
762 $canvas-> clipRect( $a[0], $a[1], $a[2] - 1, $a[3] - 1);
763 }
764 $canvas-> color( $clr[0]);
765 my $i;
766 my $y = $a[3] - $fh;
767 my $lim = int(( $a[3] - $clipRect[1]) / $fh) + $tl + 1;
768 {
769 my $fx = int(( $a[3] - $clipRect[3]) / $fh) + $tl;
770 $fx = $tl if $fx < $tl;
771 $y -= ( $fx - $tl) * $fh;
772 $tl = $fx;
773 }
774 $lim = $lc if $lim > $lc;
775 my $x = $a[0] - $ofs;
776  
777 # painting selection
778 my @sel;
779 my @cuaXs;
780 if ( $issel) {
781 @sel = (@{$self-> {selStartl}}, @{$self-> {selEndl}});
782 if ( $bt == bt::CUA) {
783 @cuaXs = (
784 $a[0] - $ofs + $self-> get_chunk_width(
785 $self-> get_chunk( $sel[1]), 0, $sel[0]
786 ),
787 $a[0] - $ofs + $self-> get_chunk_width(
788 $self-> get_chunk( $sel[3]), 0, $sel[2]
789 )
790 );
791 my $cSet = 0;
792 if ( $sel[1] == $sel[3]) {
793 if ( $sel[1] >= $tl && $sel[ 1] < $lim) {
794 $cSet = 1;
795 $canvas-> color( $sclr[ 1]);
796 $canvas-> bar(
797 $cuaXs[0], $y - $fh * ( $sel[1] - $tl - 1) - 1,
798 $cuaXs[1]-1, $y - $fh * ( $sel[1] - $tl)
799 );
800 }
801 } else {
802 if ( $sel[1] >= $tl && $sel[ 1] < $lim) {
803 $cSet = 1;
804 $canvas-> color( $sclr[ 1]);
805 $canvas-> bar(
806 $cuaXs[0], $y - $fh * ( $sel[1] - $tl - 1) - 1,
807 $size[0], $y - $fh * ( $sel[1] - $tl)
808 );
809 }
810 if ( $sel[3] >= $tl && $sel[ 3] < $lim) {
811 $canvas-> color( $sclr[ 1]) unless $cSet;
812 $cSet = 1;
813 $canvas-> bar(
814 0, $y - $fh * ( $sel[3] - $tl - 1) - 1,
815 $cuaXs[1]-1, $y - $fh * ( $sel[3] - $tl)
816 );
817 }
818 if (
819 $sel[3] -1 > $sel[1] &&
820 ( $sel[1] + 1 < $lim || $sel[ 3] - 1 >= $tl)
821 ) {
822 $canvas-> color( $sclr[ 1]) unless $cSet;
823 $cSet = 1;
824 $canvas-> bar(
825 0, $y - $fh * ( $sel[1] - $tl) - 1,
826 $size[0], $y - $fh * ( $sel[3] - $tl - 1)
827 );
828 }
829 }
830 $canvas-> color( $clr[0]) if $cSet;
831 } elsif ( $bt == bt::Horizontal) {
832 if ( $sel[1] < $lim || $sel[ 3] >= $tl) {
833 $canvas-> color( $sclr[ 1]);
834 $canvas-> bar( 0, $y - $fh * ( $sel[1] - $tl - 1) - 1, $size[0], $y - $fh * ( $sel[3] - $tl));
835 # painting horizontal block lines, if available
836 $canvas-> color( $sclr[0]);
837 my ($from, $to) = (
838 ( $tl > $sel[1]) ? $tl : $sel[1],
839 ( $lim < ($sel[3]+1)) ? $lim : ( $sel[3] + 1)
840 );
841 my $horz_y = $y - ( $from - $tl) * $fh;
842 for ( $i = $from; $i < $to; $i++)
843 {
844 my $c = $self-> get_chunk( $i);
845 # $c =~ s/\t/$tabs/g;
846 $c = wstaw_tab ( $c, 2 );
847 $canvas-> text_out( $c, $x, $horz_y);
848 $horz_y -= $fh;
849 }
850 $canvas-> color( $clr[0]);
851 }
852 }
853 }
854 my $cSet = 0;
855  
856 my $which_color = 0;
857  
858 # painting lines
859  
860 my $l_beg = $tl;
861 my $l_end = $lim;
862 if ( $self->{exportHTML} > 0 ) {
863 $l_beg = 0;
864 $l_end = $lc;
865 }
866  
867 for ( $i = $l_beg; $i < $l_end; $i++)
868 {
869 my $c = $self-> get_chunk( $i);
870  
871 if ( $issel && $i >= $sel[1] && $i <= $sel[3])
872 {
873 # painting selected lines
874 if ( $bt == bt::CUA) {
875 if ( $sel[1] == $sel[3])
876 {
877 my $cl = $sel[2] - length( $c);
878 $c .= ' 'x$cl if $cl > 0;
879 my $lc;
880 if ( $sh) {
881  
882 $self-> draw_colorchunk(
883 $canvas, $c, $i,
884 $x, $y, $clr[0],
885 $which_color
886 );
887  
888 } else {
889 $lc = substr( $c, 0, $sel[0]);
890  
891 # $lc =~ s/\t/$tabs/g;
892 $lc = wstaw_tab ( $lc, 3 );
893 $canvas-> text_out( $lc, $x, $y);
894  
895 $lc = substr( $c, $sel[2], length($c));
896  
897 # $lc =~ s/\t/$tabs/g;
898 $lc = wstaw_tab ( $lc, 4 );
899 $canvas-> text_out( $lc, $cuaXs[1], $y);
900 }
901 $lc = substr( $c, $sel[0], $sel[2] - $sel[0]);
902  
903 # $lc =~ s/\t/$tabs/g;
904 $lc = wstaw_tab ( $lc, 5 );
905 $canvas-> color( $sclr[0]);
906 $canvas-> text_out( $lc, $cuaXs[0], $y);
907 $canvas-> color( $clr[0]);
908 } elsif ( $i == $sel[1]) {
909 my $cl = $sel[0] - length( $c);
910 $c .= ' 'x$cl if $cl > 0;
911 my $lc;
912 if ( $sh) {
913  
914 $self-> draw_colorchunk(
915 $canvas, $c, $i,
916 $x, $y, $clr[0],
917 $which_color
918 );
919 } else {
920 $lc = substr( $c, 0, $sel[0]);
921  
922 # $lc =~ s/\t/$tabs/g;
923 $lc = wstaw_tab ( $lc, 6 );
924 $canvas-> text_out( $lc, $x, $y);
925 }
926 $canvas-> color( $sclr[0]);
927 $lc = substr( $c, $sel[0], length( $c));
928  
929 # $lc =~ s/\t/$tabs/g;
930 $lc = wstaw_tab ( $lc, 7 );
931 $canvas-> text_out( $lc, $cuaXs[0], $y);
932 $cSet = 1;
933 } elsif ( $i == $sel[3]) {
934 my $cl = $sel[2] - length( $c);
935 $c .= ' 'x$cl if $cl > 0;
936 if ( $sh) {
937  
938 $self-> draw_colorchunk(
939 $canvas, $c, $i,
940 $x, $y, $clr[0],
941 $which_color
942 );
943  
944 } else {
945 $canvas-> color( $clr[0]);
946 $lc = substr( $c, $sel[2], length( $c));
947  
948 # $lc =~ s/\t/$tabs/g;
949 $lc = wstaw_tab ( $lc, 8 );
950 $canvas-> text_out( $lc, $cuaXs[1], $y);
951 }
952 $canvas-> color( $sclr[0]);
953 my $lc = substr( $c, 0, $sel[2]);
954  
955 # $lc =~ s/\t/$tabs/g;
956 $lc = wstaw_tab ( $lc, 9 );
957 $canvas-> text_out( $lc, $x, $y);
958 $canvas-> color( $clr[0]);
959 } else {
960 # $c =~ s/\t/$tabs/g;
961 $c = wstaw_tab ( $c, 10 );
962 $canvas-> color( $sclr[0]) unless $cSet;
963 $cSet = 1;
964 $canvas-> text_out( $c, $x, $y);
965 }
966 } elsif ( $bt == bt::Vertical) {
967 my @vXs = (
968 $self-> get_chunk_width( $c, 0, $sel[0]) - $ofs + $a[0],
969 $self-> get_chunk_width( $c, 0, $sel[2]) - $ofs + $a[0]
970 );
971 $canvas-> color( $sclr[1]);
972 $canvas-> bar( $vXs[0], $y, $vXs[1]-1, $y + $fh - 1);
973 $canvas-> color( $clr[0]);
974 my $cl = $sel[2] - length( $c);
975 $c .= ' 'x$cl if $cl > 0;
976 my $lc;
977 if ( $sh) {
978  
979 $self-> draw_colorchunk(
980 $canvas, $c, $i,
981 $x, $y, $clr[0],
982 $which_color
983 );
984 } else {
985 $lc = substr( $c, 0, $sel[0]);
986  
987 # $lc =~ s/\t/$tabs/g;
988 $lc = wstaw_tab ( $lc, 11 );
989 $canvas-> text_out( $lc, $x, $y);
990 $lc = substr( $c, $sel[2], length($c));
991  
992 # $lc =~ s/\t/$tabs/g;
993 $lc = wstaw_tab ( $lc, 12 );
994 $canvas-> text_out( $lc, $vXs[1], $y);
995 }
996 $lc = substr( $c, $sel[0], $sel[2] - $sel[0]);
997  
998 # $lc =~ s/\t/$tabs/g;
999 $lc = wstaw_tab ( $lc, 13 );
1000 $canvas-> color( $sclr[0]);
1001 $canvas-> text_out( $lc, $vXs[0], $y);
1002 $canvas-> color( $clr[0]);
1003 }
1004 } else {
1005 # painting syntaxed lines
1006 if ( $sh) {
1007 $self-> draw_colorchunk( $canvas, $c, $i, $x, $y, $clr[0], $which_color );
1008 } else {
1009 # painting normal lines
1010 # $c =~ s/\t/$tabs/g;
1011 $c = wstaw_tab ( $c, 14 );
1012 $canvas-> color( $clr[0]);
1013 $canvas-> text_out( $c, $x, $y);
1014 }
1015 }
1016 #print "$fh\n";
1017 $y -= $fh;
1018 print $_HTML_FILE "\n" if $self->{exportHTML} > 0;
1019 }
1020  
1021 if ( $self->{exportHTML} > 0 ) {
1022 print $_HTML_FILE "</pre>\n";
1023 close ( $_HTML_FILE );
1024  
1025 undef $_HTML_FILE;
1026 }
1027  
1028 $self-> {exportHTML} = 0 if $self->{exportHTML} < 0;
1029 $self-> {exportHTML} = -1 if $self->{exportHTML} > 0;
1030  
1031 $self-> on_paint( $canvas ) if $self->{exportHTML} < 0;
1032  
1033 }
1034  
1035 #-----------------------------------------------------------------------
1036  
1037 sub point2xy
1038 {
1039 my ( $self, $x, $y) = @_;
1040 my ( $fh, $ofs, $avg, @a) = (
1041 $self-> font-> height + $self->{lineSpace}, $self-> {offset},
1042  
1043 $self-> {averageWidth}, $self-> get_active_area
1044 );
1045 my ( $rx, $ry, $inBounds);
1046 $inBounds = !( $x <= $a[0] || $x > $a[2] || $y < $a[1] || $y > $a[3]);
1047 $x -= $a[0];
1048 $y -= $a[1];
1049 my ( $w, $h) = ( $a[2] - $a[0], $a[3] - $a[1]);
1050  
1051 $y = $h + $fh if $y > $h;
1052 $y = -$fh if $y < 0;
1053 $x = $w + $avg * 2 if $x > $w + $avg * 2;
1054 $x = - $avg * 2 if $x < - $avg * 2;
1055 $ry = int(( $h - $y) / $fh) + $self-> {topLine };
1056 $ry = 0 if $ry < 0;
1057 $ry = $self-> {maxChunk} if $ry > $self-> {maxChunk};
1058 $rx = 0;
1059  
1060 my $chunk = $self-> get_chunk( $ry);
1061 my $cl = ( $w + $ofs) / ($self-> get_text_width(' ')||1);
1062 $chunk .= ' 'x$cl;
1063 if ( $ofs + $x > 0)
1064 {
1065 my $ofsx = $ofs + $x;
1066 $ofsx = $self-> {maxLineWidth} if $ofsx > $self-> {maxLineWidth};
1067 $rx = $self-> text_wrap( $chunk, $ofsx,
1068  
1069 tw::CalcTabs|tw::BreakSingle|tw::ReturnFirstLineLength, $self-> {tabIndent});
1070 }
1071 return $self-> make_physical( $rx, $ry), $inBounds;
1072 }
1073  
1074 #------------------------------------------------------------------------------
1075  
1076 sub on_mousedown
1077 {
1078 my ( $self, $btn, $mod, $x, $y) = @_;
1079 return if $self-> {mouseTransaction};
1080 return if $btn != mb::Left && $btn != mb::Middle;
1081 my @xy = $self-> point2xy( $x, $y);
1082 return unless $xy[2];
1083 $self-> cursor( @xy);
1084  
1085 if ( $btn == mb::Middle) {
1086 my $cp = $::application-> bring('Primary');
1087 return if !$cp || $self-> {readOnly};
1088 $self-> insert_text( $cp-> text, 0);
1089 $self-> clear_event;
1090 return;
1091 }
1092  
1093 $self-> {mouseTransaction} = 1;
1094 if ( $self-> {persistentBlock} && $self-> has_selection) {
1095 $self-> {mouseTransaction} = 2;
1096 } else {
1097 $self-> start_block unless exists $self-> {anchor};
1098 }
1099 $self-> capture(1);
1100 $self-> clear_event;
1101 }
1102  
1103 #------------------------------------------------------------------------------
1104  
1105 sub on_mouseup
1106 {
1107 my ( $self, $btn, $mod, $x, $y) = @_;
1108 return unless $self-> {mouseTransaction};
1109 return if $btn != mb::Left;
1110 $self-> capture(0);
1111 $self-> end_block unless $self-> {mouseTransaction} == 2;
1112 $self-> {mouseTransaction} = undef;
1113 $self-> clear_event;
1114  
1115 return if $self-> {writeOnly} || !$self-> has_selection;
1116 my $cp = $::application-> bring('Primary');
1117 $cp-> text( $self-> get_selected_text) if $cp;
1118 }
1119  
1120 sub on_mousemove
1121 {
1122 my ( $self, $mod, $x, $y) = @_;
1123 return unless $self-> {mouseTransaction};
1124 my @xy = $self-> point2xy( $x, $y);
1125 $self-> clear_event;
1126 if ( $xy[2])
1127 {
1128 $self-> scroll_timer_stop;
1129 } else {
1130 $self-> scroll_timer_start unless $self-> scroll_timer_active;
1131 return unless $self-> scroll_timer_semaphore;
1132 $self-> scroll_timer_semaphore(0);
1133 }
1134 $self-> {delayPanning} = 1;
1135 $self-> blockShiftMark(1);
1136 $self-> cursor( @xy);
1137 $self-> blockShiftMark(0);
1138 $self-> update_block unless $self-> {mouseTransaction} == 2;
1139 $self-> realize_panning;
1140 }
1141  
1142 sub on_mousewheel
1143 {
1144 my ( $self, $mod, $x, $y, $z) = @_;
1145 # $z = int( $z/120);
1146 $self-> {wheelRows} += 0;
1147 $self-> {wheelRows} = 1 unless $self-> {wheelRows};
1148 $self-> {wheelRows} = 1 if $self-> {wheelRows} < 1;
1149  
1150 $z = int( $self-> {wheelRows} * $z/ 120 );
1151 $z *= $self-> {rows} if $mod & km::Ctrl;
1152 my $newTop = $self-> topLine - $z;
1153 my $maxTop = $self-> {maxChunk} - $self-> {rows} + 1;
1154 $self-> topLine ( $newTop > $maxTop ? $maxTop : $newTop);
1155 $self-> clear_event;
1156 # $self-> repaint;
1157 }
1158  
1159 sub on_mouseclick
1160 {
1161 my ( $self, $btn, $mod, $x, $y, $dbl) = @_;
1162  
1163 return if $self-> {mouseTransaction};
1164 return if $btn != mb::Left;
1165 my @xy = $self-> point2xy( $x, $y);
1166 return unless $xy[2];
1167 my $s = $self-> get_line( $xy[1]);
1168 $self-> clear_event;
1169  
1170 if ( !$dbl) {
1171 if ( $self-> {doubleclickTimer}) {
1172 $self-> {doubleclickTimer}-> destroy;
1173 delete $self-> {doubleclickTimer};
1174 $self-> selection( 0, $xy[1], length $s, $xy[1]);
1175 }
1176 return;
1177 }
1178  
1179 $self-> cancel_block;
1180 $self-> cursor( @xy);
1181  
1182 my $p = $xy[0];
1183 my $sl = length $s;
1184 my ($l,$r);
1185  
1186 return unless $sl;
1187  
1188 $p = $sl-1 if $p >= $sl;
1189 my $word = quotemeta($self-> {wordDelimiters});
1190 my $nonword = "[$word]";
1191 $word = "[^$word]";
1192 if ( substr($s,$p,1) =~ /$word/) {
1193 substr($s,0,$p) =~ /($word*)$/;
1194 $l = $p - length $1;
1195 substr($s,$p) =~ /^($word*)/;
1196 $r = $p + length $1;
1197 } else {
1198 substr($s,0,$p) =~ /($nonword*)$/;
1199 $l = $p - length $1;
1200 substr($s,$p) =~ /^($nonword*)/;
1201 $r = $p + length $1;
1202 }
1203  
1204 $self-> selection( $l, $xy[1], $r, $xy[1]);
1205 $self-> {doubleclickTimer} = Prima::Timer-> create( onTick => sub{
1206 $self-> {doubleclickTimer}-> destroy;
1207 delete $self-> {doubleclickTimer};
1208 }) unless $self-> {doubleclickTimer};
1209 $self-> {doubleclickTimer}-> timeout(
1210  
1211 Prima::Application-> get_system_value( sv::DblClickDelay)
1212 );
1213 $self-> {doubleclickTimer}-> start;
1214 }
1215  
1216 ##########################################################################
1217  
1218 sub on_keydown
1219 {
1220 my ( $self, $code, $key, $mod, $repeat) = @_;
1221  
1222 # $self->{hiliteBlok_modified} = 1;
1223  
1224 # my @cs = $self-> cursor;
1225 # my $curr_row = $self->{topLine} + $cs[1];
1226 # my $blok_beg = $self->{hiliteBlok}->[0];
1227 # my $blok_end = $self->{hiliteBlok}->[1];
1228  
1229 # my ( $prev_type, $next_type ) = ( 0, 0 );
1230 # $prev_type = 1 if $self-> get_line( $cs[1] ) =~ /($blok_beg|blok_end)/;
1231  
1232 # my $modify = 0;
1233 # for (my $i = $cs[1] ; $i < $cs[1] + $self->{rows} ; $i++ ) {
1234 # $modify = 1 if $self-> get_line( $i ) && (
1235 # $self-> get_line( $i ) =~ /($blok_beg|blok_end)/ || $self->{hiliteBlok_value}->[ $i ]
1236 # );
1237 # last if $modify
1238 # }
1239  
1240 if ($key == kb::Enter || $key == kb::Backspace || $key == kb::Return ||
1241 $key == kb::Tab || $key == kb::Delete
1242 ) {
1243 $self-> area( $self->{topLine}, $self->{rows} ) ;
1244 # if ( $modify ) {
1245 # $self->{hiliteBlok_modified} = 1;
1246 # }
1247 }
1248  
1249 return if $self-> {readOnly};
1250 return if $mod & km::DeadKey;
1251  
1252 $mod &= ( km::Shift|km::Ctrl|km::Alt);
1253 $self-> notify(q(MouseUp),0,0,0) if $self-> {mouseTransaction};
1254 if ( $key == kb::Tab && !$self-> {wantTabs}) {
1255 return unless $mod & km::Ctrl;
1256 $mod &= ~km::Ctrl;
1257 }
1258  
1259  
1260 if (
1261 ( $code >= ord(' ') || ( $code == ord("\t"))) &&
1262 (( $mod & (km::Alt | km::Ctrl)) == 0) &&
1263 (( $key == kb::NoKey) || ( $key == kb::Space) || ( $key == kb::Tab))
1264 ) {
1265  
1266 { #wb:
1267 $self-> delete_block if $self-> has_selection;
1268 }
1269  
1270 my @cs = $self-> cursor;
1271 my $c = $self-> get_line( $cs[1] );
1272 my $l = 0;
1273  
1274 $self-> begin_undo_group;
1275 if ( $self-> insertMode) {
1276 $l = $cs[0] - length( $c), $c .= ' ' x $l if length( $c) < $cs[ 0];
1277 substr( $c, $cs[0], 0) = chr($code) x $repeat;
1278 $self-> set_line( $cs[1], $c, q(add), $cs[0], $l + $repeat);
1279  
1280 } else {
1281 $l = $cs[0] - length( $c) + $repeat, $c .= ' ' x $l
1282 if length( $c) < $cs[ 0] + $repeat;
1283  
1284 substr( $c, $cs[0], $repeat) = chr($code) x $repeat;
1285 $self-> set_line( $cs[1], $c, q(overtype));
1286 }
1287  
1288 # $next_type = 1 if $self-> get_line( $cs[1] ) =~ /($blok_beg|blok_end)/;
1289 # $self-> area;
1290  
1291 $self-> cursor( $cs[0] + $repeat, $cs[1]);
1292 $self-> end_undo_group;
1293 $self-> clear_event;
1294 }
1295 }
1296  
1297 sub on_fontchanged
1298 {
1299 my $self = $_[0];
1300 $self-> reset_render;
1301 $self-> reset_scrolls;
1302 }
1303  
1304 sub on_size
1305 {
1306 my $self = $_[0];
1307 $self-> reset_render;
1308 $self-> reset_scrolls;
1309 }
1310  
1311 sub on_enable { $_[0]-> repaint; }
1312 sub on_disable { $_[0]-> repaint; }
1313  
1314 sub on_enter
1315 {
1316 my $self = $_[0];
1317 $self-> insertMode( $::application-> insertMode);
1318 }
1319  
1320 #----------------------------------------------------------------------------------------
1321  
1322 sub on_change
1323 {
1324 my ( $self ) = @_;
1325  
1326 $self-> {modified} = 1;
1327 $self-> {hiliteBlok_modified} = 1;
1328  
1329 if ( $Prima::CodeManager::developer{notes} ) {
1330 my $nr = $Prima::CodeManager::developer{notes}->pageIndex;
1331  
1332 $Prima::CodeManager::list_of_files[ $nr ] = '*'.$Prima::CodeManager::list_of_files[ $nr ] if $Prima::CodeManager::list_of_files[ $nr ] !~ /^\*/;
1333 $Prima::CodeManager::developer{notes}->set_tabs( @Prima::CodeManager::list_of_files );
1334 }
1335  
1336 #print "on_change\n";
1337  
1338 # if ( $self->{hiliteBlok_modified} ) {
1339 # $self-> area;
1340 # on_paint ( $self, $self );
1341 # }
1342 # $self->{hiliteBlok_modified} = 0;
1343  
1344 if ( $self-> {hiliteBlok_modified} && @{$self->{hiliteBlok}} ) {
1345 $self-> area( $self->{topLine}, $self->{rows} ) ;
1346 # on_paint ( $self, $self );
1347 }
1348  
1349 # on_paint ( $self, $self ) if scalar @{$self->{hiliteBlok}};
1350 }
1351  
1352 #----------------------------------------------------------------------------------------
1353  
1354 sub on_parsesyntax {
1355 $_[2] = [] unless $_[2];
1356 $_[0]-> {syntaxer}-> (@_);
1357 }
1358  
1359 sub set_block_type
1360 {
1361 my ( $self, $bt) = @_;
1362 return if $bt == $self-> {blockType};
1363 $self-> push_group_undo_action( 'blockType', $self-> {blockType});
1364 $self-> {blockType} = $bt;
1365 return unless $self-> has_selection;
1366 $self-> reset_render;
1367 $self-> repaint;
1368 }
1369  
1370 #----------------------------------------------------------------------------------------
1371  
1372 sub set_text_ref
1373 {
1374 my ( $self, $ref) = @_;
1375 return unless defined $ref;
1376 $self-> {capLen} = length( $$ref);
1377 $#{$self-> {lines}} = $self-> {capLen} / 40;
1378 @{$self-> {lines}} = ();
1379 @{$self-> {lines}} = split( "\n", $$ref." ");
1380 $self-> {maxLine} = scalar @{$self-> {lines}}-1;
1381  
1382 $self-> reset_syntax;
1383 $self-> reset_scrolls;
1384 if ( !$self-> {resetDisabled}) {
1385 $self-> lock;
1386 $self-> selection(0,0,0,0);
1387 $self-> reset;
1388 $self-> cursor($self-> {cursorX}, $self-> {cursorY});
1389 $self-> unlock;
1390 $self-> notify(q(Change));
1391 $self-> reset_scrolls;
1392 }
1393 }
1394  
1395 sub text
1396 {
1397 unless ($#_) {
1398 my $hugeScalarRef = $_[0]-> textRef;
1399 return $$hugeScalarRef;
1400 } else {
1401 $_[0]-> textRef( \$_[1]);
1402 }
1403 }
1404  
1405 sub get_text_ref
1406 {
1407 my $self = $_[0];
1408 my $hugeScalar = join( "\n", @{$self-> {lines}});
1409 return \$hugeScalar;
1410 }
1411  
1412 sub get_chunk
1413 {
1414 my ( $self, $index) = @_;
1415 my $ck = $self-> {lines};
1416 return '' if $self->{maxLine} < 0;
1417 Carp::confess($index) if $index > $self-> {maxChunk};
1418 if ( $self-> {wordWrap}) {
1419 my $cm = $self-> {chunkMap};
1420 return substr( $$ck[ $$cm[ $index * 3 + 2]], $$cm[ $index * 3], $$cm[ $index * 3 + 1]);
1421 } else {
1422 return $$ck[ $index];
1423 }
1424 }
1425  
1426 sub get_line
1427 {
1428 my ( $self, $index) = @_;
1429 return $self-> {maxLine} >= 0 ? $self-> {lines}-> [$index] : '';
1430 }
1431  
1432 sub get_line_ext
1433 {
1434 my ( $self, $index) = @_;
1435 return '' if $self-> {maxLine} < 0;
1436 return $self-> {lines}-> [ $self-> {wordWrap} ?
1437  
1438 ( $self-> {chunkMap}-> [ $index * 3 + 2]) :
1439  
1440 $index
1441 ];
1442 }
1443  
1444 sub get_line_dimension
1445 {
1446 my ( $self, $y) = @_;
1447 return $y, 1 unless $self-> {wordWrap};
1448 ( undef, $y) = $self-> make_logical( 0, $y);
1449 my ($ret, $ix, $cm) = ( 0, $y * 3 + 2, $self-> {chunkMap});
1450 $ret++, $ix += 3 while $$cm[ $ix] == $y;
1451 return $y, $ret;
1452 }
1453  
1454 sub get_chunk_org
1455 {
1456 my ( $self, $index) = @_;
1457 return $index unless $self-> {wordWrap};
1458 my $cm = $self-> {chunkMap};
1459 my $y = $$cm[ $index * 3 + 2];
1460 $index-- while $y == $$cm[ $index * 3 + 2];
1461 return $index + 1;
1462 }
1463  
1464 sub get_chunk_end
1465 {
1466 my ( $self, $index) = @_;
1467 return $index unless $self-> {wordWrap};
1468 my $cm = $self-> {chunkMap};
1469 my $y = $$cm[ $index * 3 + 2];
1470 my $maxY = $self-> {maxChunk};
1471 return -1 if $maxY < 0;
1472 $index++ while $index <= $maxY && $y == $$cm[ $index * 3 + 2];
1473 return $index - 1;
1474 }
1475  
1476 sub get_chunk_width
1477 {
1478 my ( $self, $chunk, $from, $len, $retC) = @_;
1479 my $line = $chunk;
1480 my $cl;
1481 $cl = $from + $len - length( $chunk) + 1;
1482 $chunk .= ' 'x$cl if $cl >= 0;
1483 $chunk = substr( $chunk, $from, $len);
1484 # $chunk =~ s/\t/$self->{tabs}/g;
1485 # $chunk = wstaw_tab ( $chunk, 15 );
1486 my $sm = '';
1487 ( $chunk, $sm ) = wstaw_tab_15 ( $line, $from, $len, $self->{tabs}, 15 );
1488 $$retC = $chunk if $retC;
1489 return $self-> {fixed} ?
1490  
1491 ( length( $chunk) * $self-> {averageWidth}) :
1492  
1493 $self-> get_text_width( $chunk);
1494 }
1495  
1496 sub has_selection
1497 {
1498 my @s = $_[0]-> selection;
1499 return !(($s[0] == $s[2]) && ( $s[1] == $s[3]));
1500 }
1501  
1502 sub realize_panning
1503 {
1504 delete $_[0]-> {delayPanning};
1505 for ( qw( topLine offset)) {
1506 my $c = 'delay_' . $_;
1507 next unless defined $_[0]-> {$c};
1508 $_[0]-> $_( $_[0]-> {$c});
1509 delete $_[0]-> {$c};
1510 }
1511 }
1512  
1513 sub set_cursor
1514 {
1515 my ( $self, $x, $y) = @_;
1516 my ( $ox, $oy) = ($self-> {cursorX}, $self-> {cursorY});
1517 my $maxY = $self-> {maxLine};
1518 $y = $maxY if $y < 0 || $y > $maxY;
1519 $y = 0 if $y < 0; # ??
1520 my $line = $self-> get_line( $y);
1521 $x = length( $line) if $x < 0;
1522 my ( $lx, $ly) = $self-> make_logical( $x, $y);
1523 my ( $olx, $oly) = ( $self-> {cursorXl}, $self-> {cursorYl});
1524 $self-> {cursorXl} = $lx;
1525 $self-> {cursorYl} = $ly;
1526 return if $y == $oy and $x == $ox and $lx == $olx and $ly == $oly;
1527 my ( $tl, $r, $yt) = ( $self-> {topLine }, $self-> {rows}, $self-> {yTail});
1528 if ( $ly < $tl) {
1529 $self-> topLine ( $ly);
1530 } elsif ( $ly >= $tl + $r) {
1531 my $nfc = $ly - $r + 1;
1532 $self-> topLine ( $nfc);
1533 }
1534 my $chunk = $self-> get_chunk( $ly);
1535 my $atX = $self-> get_chunk_width( $chunk, 0, $lx);
1536 my $deltaX = $self-> get_chunk_width( $chunk, $lx, 1);
1537 my $actualWidth = $self-> width -
1538  
1539 $self-> {indents}-> [0] -
1540  
1541 $self-> {indents}-> [2] -
1542  
1543 $self-> {defcw};
1544 my $ofs = $self-> {offset};
1545 my $avg = $self-> {averageWidth};
1546 if ( $atX < $ofs) {
1547 my $nofs = $atX;
1548 $self-> offset( $nofs - $avg);
1549 } elsif ( $atX >= $ofs + $actualWidth - $deltaX) {
1550 my $nofs = $atX - $actualWidth + $deltaX;
1551 $nofs = $ofs + $avg if $nofs - $ofs < $avg;
1552 $self-> offset( $nofs);
1553 }
1554  
1555 # check if last undo record contains cursor movements only, so these movements
1556 # can be grouped
1557  
1558 my $undo = 1;
1559 if ( !$self-> {undo_in_action} && @{$self-> {undo}} && @{$self-> {undo}-> [-1]}) {
1560 my $ok = 1;
1561  
1562 for ( @{$self-> {undo}-> [-1]}) {
1563 $ok = 0, last if $$_[0] ne 'cursor';
1564 }
1565 $undo = 0 if $ok;
1566 }
1567  
1568 $self-> push_undo_action( 'cursor', $self-> {cursorX}, $self-> {cursorY}) if $undo;
1569 $self-> {cursorX} = $x;
1570 $self-> {cursorY} = $y;
1571 $self-> {cursorAtX} = $atX;
1572 $self-> {cursorInsWidth} = $deltaX;
1573  
1574 $self-> reset_cursor;
1575 $self-> cancel_block
1576  
1577 if !$self-> {blockShiftMark} && !$self-> {persistentBlock};
1578  
1579 }
1580  
1581 sub set_top_line
1582 {
1583 my ( $self, $tl) = @_;
1584 $tl = $self-> {maxChunk} if $tl >= $self-> {maxChunk};
1585 $tl = 0 if $tl < 0;
1586 return if $self-> {topLine } == $tl;
1587 if ( $self-> {delayPanning}) {
1588 $self-> {delay_topLine } = $tl;
1589 return;
1590 }
1591 my $dt = $tl - $self-> {topLine };
1592 $self-> push_group_undo_action( 'topLine', $self-> {topLine});
1593 $self-> {topLine } = $tl;
1594 if ( $self-> {vScroll} && $self-> {scrollTransaction} != 1) {
1595 $self-> {scrollTransaction} = 1;
1596 $self-> {vScrollBar}-> value( $tl);
1597 $self-> {scrollTransaction} = 0;
1598 }
1599 $self-> reset_cursor;
1600 $self-> scroll( 0, $dt * ($self-> font-> height + $self->{lineSpace}),
1601 clipRect => [ $self-> get_active_area]);
1602 }
1603  
1604 sub reset_indents
1605 {
1606 my ( $self) = @_;
1607 $self-> reset_render;
1608 $self-> reset_scrolls;
1609 $self-> repaint;
1610 }
1611  
1612 sub set_hilite_numbers
1613 {
1614 my $self = $_[0];
1615 $self-> {hiliteNumbers} = $_[1];
1616 if ( $self-> {syntaxHilite}) {
1617 $self-> reset_syntaxer;
1618 $self-> repaint;
1619 }
1620 }
1621  
1622 sub set_hilite_q_strings
1623 {
1624 my $self = $_[0];
1625 $self-> {hiliteQStrings} = $_[1];
1626 if ( $self-> {syntaxHilite}) {
1627 $self-> reset_syntaxer;
1628 $self-> repaint;
1629 }
1630 }
1631  
1632 #-------------------------------------------------------------------------------
1633  
1634 sub set_hilite_qq_strings
1635 {
1636 my $self = $_[0];
1637 $self-> {hiliteQQStrings} = $_[1];
1638 if ( $self-> {syntaxHilite}) {
1639 $self-> reset_syntaxer;
1640 $self-> repaint;
1641 }
1642 }
1643  
1644 #-------------------------------------------------------------------------------
1645  
1646 sub set_hilite_ids
1647 {
1648 my ($self, $hi) = @_;
1649 if ( $hi) {
1650 push @{$hi}, cl::Fore if scalar @{$hi} / 2 != 0;
1651 $hi = [@{$hi}];
1652 }
1653 $self-> {hiliteIDs} = $hi;
1654 if ( $self-> {syntaxHilite}) {
1655 $self-> reset_syntaxer;
1656 $self-> repaint;
1657 }
1658 }
1659  
1660 #-------------------------------------------------------------------------------
1661  
1662 sub set_hilite_chars
1663 {
1664 my ($self, $hi) = @_;
1665 if ( $hi) {
1666 push @{$hi}, cl::Fore if scalar @{$hi} / 2 != 0;
1667 $hi = [@{$hi}];
1668 }
1669 $self-> {hiliteChars} = $hi;
1670 if ( $self-> {syntaxHilite}) {
1671 $self-> reset_syntaxer;
1672 $self-> repaint;
1673 }
1674 }
1675  
1676 #-------------------------------------------------------------------------------
1677  
1678 sub set_hilite_res
1679 {
1680 my ($self, $hi) = @_;
1681 if ( $hi) {
1682 push @{$hi}, cl::Fore if scalar @{$hi} / 2 != 0;
1683 $hi = [@{$hi}];
1684 }
1685 $self-> {hiliteREs} = $hi;
1686 if ( $self-> {syntaxHilite}) {
1687 $self-> reset_syntaxer;
1688 $self-> repaint;
1689 }
1690 }
1691  
1692 sub set_insert_mode
1693 {
1694 my ( $self, $insert) = @_;
1695 my $oi = $self-> {insertMode};
1696 $self-> {insertMode} = $insert;
1697 $self-> reset_cursor if $oi != $insert;
1698 $::application-> insertMode( $insert);
1699 $self-> push_group_undo_action( 'insertMode', $oi) if $oi != $insert;
1700 }
1701  
1702 #-------------------------------------------------------------------------------
1703  
1704 sub set_offset
1705 {
1706 my ( $self, $offset) = @_;
1707 $offset = 0 if $offset < 0;
1708 $offset = 0 if $self-> {wordWrap};
1709 return if $self-> {offset} == $offset;
1710 if ( $self-> {delayPanning}) {
1711 $self-> {delay_offset} = $offset;
1712 return;
1713 }
1714 my $dt = $offset - $self-> {offset};
1715 $self-> push_group_undo_action( 'offset', $self-> {offset});
1716 $self-> {offset} = $offset;
1717 if ( $self-> {hScroll} && $self-> {scrollTransaction} != 2) {
1718 $self-> {scrollTransaction} = 2;
1719 $self-> {hScrollBar}-> value( $offset);
1720 $self-> {scrollTransaction} = 0;
1721 }
1722 $self-> reset_cursor;
1723 $self-> scroll( -$dt, 0,
1724 clipRect => [ $self-> get_active_area]);
1725 }
1726  
1727 #-------------------------------------------------------------------------------
1728  
1729 sub set_selection
1730 {
1731 my ( $self, $sx, $sy, $ex, $ey) = @_;
1732 my $maxY = $self-> {maxLine};
1733 my ( $osx, $osy, $oex, $oey) = $self-> selection;
1734 my $onsel = ( $osx == $oex && $osy == $oey);
1735 if ( $maxY < 0) {
1736 $self-> {selStart} = [0,0];
1737 $self-> {selEnd} = [0,0];
1738 $self-> {selStartl} = [0,0];
1739 $self-> {selEndl } = [0,0];
1740 $self-> repaint unless $onsel;
1741 return;
1742 }
1743 $sy = $maxY if $sy < 0 || $sy > $maxY;
1744 $ey = $maxY if $ey < 0 || $ey > $maxY;
1745 ( $sy, $ey, $sx, $ex) = ( $ey, $sy, $ex, $sx) if $sy > $ey;
1746 $osx = $oex = $sx, $osy = $oey = $ey if $onsel;
1747 if ( $sx == $ex && $sy == $ey) {
1748 $osy = $maxY if $osy < 0 || $osy > $maxY;
1749 $oey = $maxY if $oey < 0 || $oey > $maxY;
1750 $sx = $ex = $osx;
1751 $sy = $ey = $osy;
1752 }
1753 my ($firstChunk, $lastChunk) = ( $self-> get_line( $sy), $self-> get_line( $ey));
1754 my ($fcl, $lcl) = ( length( $firstChunk), length( $lastChunk));
1755 my $bt = $self-> {blockType};
1756 $sx = $fcl if ( $bt != bt::Vertical && $sx > $fcl) || ( $sx < 0);
1757 $ex = $lcl if ( $bt != bt::Vertical && $ex > $lcl) || ( $ex < 0);
1758 ( $sx, $ex) = ( $ex, $sx) if $sx > $ex && (( $sy == $ey && $bt == bt::CUA) || ( $bt == bt::Vertical));
1759 my ( $lsx, $lsy) = $self-> make_logical( $sx, $sy);
1760 my ( $lex, $ley) = $self-> make_logical( $ex, $ey);
1761 ( $lsx, $lex) = ( $lex, $lsx) if $lsx > $lex && (( $lsy == $ley && $bt == bt::CUA) || ( $bt == bt::Vertical));
1762 $sy = $ey if $sx == $ex and $bt == bt::Vertical;
1763 my ( $_osx, $_osy) = @{$self-> {selStartl}};
1764 my ( $_oex, $_oey) = @{$self-> {selEndl}};
1765 $self-> {selStart} = [ $sx, $sy];
1766 $self-> {selStartl} = [ $lsx, $lsy];
1767 $self-> {selEnd} = [ $ex, $ey];
1768 $self-> {selEndl} = [ $lex, $ley];
1769 return if $sx == $osx && $ex == $oex && $sy == $osy && $ey == $oey;
1770 return if $sx == $ex && $sy == $ey && $onsel;
1771 $self-> push_group_undo_action('selection', $osx, $osy, $oex, $oey);
1772 ( $osx, $osy, $oex, $oey) = ( $_osx, $_osy, $_oex, $_oey);
1773 ( $sx, $sy) = @{$self-> {selStartl}};
1774 ( $ex, $ey) = @{$self-> {selEndl}};
1775 $osx = $oex = $sx, $osy = $oey = $ey if $onsel;
1776 if (( $osy > $ey && $oey > $ey) || ( $oey < $sy && $oey < $sy))
1777 {
1778 $self-> repaint;
1779 return;
1780 }
1781 # connective selection
1782 my ( $start, $end);
1783 if ( $bt == bt::CUA || ( $sx == $osx && $ex == $oex)) {
1784 if ( $sy == $osy) {
1785 if ( $ey == $oey) {
1786 if ( $sx == $osx) {
1787 $start = $end = $ey;
1788 } elsif ( $ex == $oex) {
1789 $start = $end = $sy;
1790 } else {
1791 ($start, $end) = ( $sy, $ey);
1792 }
1793 } else {
1794 ( $start, $end) = ( $ey < $oey) ? ( $ey, $oey) : ( $oey, $ey);
1795 }
1796 } elsif ( $ey == $oey) {
1797 ( $start, $end) = ( $sy < $osy) ? ( $sy, $osy) : ( $osy, $sy);
1798 } else {
1799 $start = ( $sy < $osy) ? $sy : $osy;
1800 $end = ( $ey > $oey) ? $ey : $oey;
1801 }
1802 } else {
1803 $start = ( $sy < $osy) ? $sy : $osy;
1804 $end = ( $ey > $oey) ? $ey : $oey;
1805 }
1806 my ( $ofs, $tl, $fh, $r, $yT) = (
1807 $self-> {offset}, $self-> {topLine },
1808  
1809 $self-> font-> height + $self->{lineSpace}, $self-> {rows},
1810  
1811 $self-> {yTail}
1812 );
1813 my @a = $self-> get_active_area( 0);
1814 return if $end < $tl || $start >= $tl + $r + $yT;
1815 if ( $start == $end && $bt == bt::CUA) {
1816 # single connective line paint
1817 my $chunk;
1818 my ( $xstart, $xend);
1819 if ( $sx == $osx) {
1820 ( $xstart, $xend) = ( $ex < $oex) ? ( $ex, $oex) : ( $oex, $ex);
1821 } elsif ( $ex == $oex) {
1822 ( $xstart, $xend) = ( $sx < $osx) ? ( $sx, $osx) : ( $osx, $sx);
1823 } else {
1824 $xstart = ( $sx < $osx) ? $sx : $osx;
1825 $xend = ( $ex > $oex) ? $ex : $oex;
1826 }
1827 unless ( $self-> {wordWrap}) {
1828 if ( $start == $sy) {
1829 $chunk = $firstChunk;
1830 } elsif ( $start == $ey) {
1831 $chunk = $lastChunk;
1832 } else {
1833 $chunk = $self-> get_chunk( $start);
1834 }
1835 } else {
1836 $chunk = $self-> get_chunk( $start);
1837 }
1838 $self-> invalidate_rect(
1839 $a[0] - $ofs + $self-> get_chunk_width( $chunk, 0, $xstart) - 1,
1840 $a[3] - $fh * ( $start - $tl + 1),
1841 $a[0] - $ofs + $self-> get_chunk_width( $chunk, 0, $xend),
1842 $a[3] - $fh * ( $start - $tl)
1843 );
1844 } else {
1845 # general connected lines paint
1846 $self-> invalidate_rect(
1847 $a[0], $a[3] - $fh * ( $end - $tl + 1),
1848 $a[2], $a[3] - $fh * ( $start - $tl),
1849 );
1850 }
1851 }
1852  
1853 #-------------------------------------------------------------------------------
1854  
1855 sub set_tab_indent
1856 {
1857 my ( $self, $ti) = @_;
1858 $ti = 0 if $ti < 0;
1859 $ti = 256 if $ti > 256;
1860 return if $ti == $self-> {tabIndent};
1861 $self-> {tabIndent} = $ti;
1862 $self-> reset;
1863 $self-> repaint;
1864 }
1865  
1866 #-------------------------------------------------------------------------------
1867  
1868 sub set_syntax_hilite
1869 {
1870 my ( $self, $sh) = @_;
1871 $sh = 0 if $self-> {wordWrap};
1872 return if $sh == $self-> {syntaxHilite};
1873 $self-> {syntaxHilite} = $sh;
1874 $self-> reset_syntaxer if $sh;
1875 $self-> reset_syntax;
1876 $self-> repaint;
1877 }
1878  
1879 #-------------------------------------------------------------------------------
1880  
1881 sub set_word_wrap
1882 {
1883 my ( $self, $ww) = @_;
1884 return if $ww == $self-> {wordWrap};
1885 $self-> {wordWrap} = $ww;
1886 $self-> syntaxHilite(0) if $ww;
1887 $self-> reset;
1888 $self-> reset_scrolls;
1889 $self-> repaint;
1890 }
1891  
1892 #-------------------------------------------------------------------------------
1893  
1894 sub cut
1895 {
1896 my $self = $_[0];
1897 return if $self-> {readOnly};
1898 $self-> begin_undo_group;
1899 $self-> copy;
1900 $self-> delete_block;
1901 $self-> end_undo_group;
1902  
1903 $self-> area( $self->{topLine}, $self->{rows} ) if @{$self->{hiliteBlok}};
1904 }
1905  
1906 #-------------------------------------------------------------------------------
1907  
1908 sub copy
1909 {
1910 my $self = $_[0];
1911 my $text = $self-> get_selected_text;
1912 $::application-> Clipboard-> text($text) if defined $text;
1913 }
1914  
1915 #-------------------------------------------------------------------------------
1916  
1917 sub get_selected_text
1918 {
1919 my $self = $_[0];
1920 return undef unless $self-> has_selection;
1921 my @sel = $self-> selection;
1922 my $text = '';
1923 my $bt = $self-> blockType;
1924  
1925 if ( $bt == bt::CUA) {
1926 #print "$sel[1] == $sel[3]\n";
1927 if ( $sel[1] == $sel[3]) {
1928 $text = substr( $self-> get_line( $sel[1]), $sel[0], $sel[2] - $sel[0]);
1929 } else {
1930 my $c = $self-> get_line( $sel[1]);
1931 $text = substr( $c, $sel[0], length( $c) - $sel[0])."\n";
1932 my $i;
1933 for ( $i = $sel[1] + 1; $i < $sel[3]; $i++) {
1934 $text .= $self-> get_line( $i)."\n";
1935 }
1936 $c = $self-> get_line( $sel[3]);
1937 $text .= substr( $c, 0, $sel[2]);
1938 }
1939 } elsif ( $bt == bt::Horizontal) {
1940 my $i;
1941 for ( $i = $sel[1]; $i <= $sel[3]; $i++) {
1942 $text .= $self-> get_line( $i)."\n";
1943 }
1944 } else {
1945 my $i;
1946 for ( $i = $sel[1]; $i <= $sel[3]; $i++) {
1947 my $c = $self-> get_line( $i);
1948 my $cl = $sel[2] - length( $c);
1949 $c .= ' 'x$cl if $cl > 0;
1950 $text .= substr($c, $sel[0], $sel[2] - $sel[0])."\n";
1951 }
1952 chomp( $text);
1953 }
1954 #print "|$text|\n";
1955 return $text;
1956 }
1957  
1958 #-------------------------------------------------------------------------------
1959  
1960 sub lock_change
1961 {
1962 my ( $self, $lock) = @_;
1963 $lock = $lock ? 1 : -1;
1964 $self-> {notifyChangeLock} += $lock;
1965 $self-> {notifyChangeLock} = 0 if $lock > 0 && $self-> {notifyChangeLock} < 0;
1966 $self-> notify(q(Change)) if $self-> {notifyChangeLock} == 0 && $lock < 0;
1967 }
1968  
1969 #-------------------------------------------------------------------------------
1970  
1971 sub change_locked
1972 {
1973 my $self = $_[0];
1974 return $self-> {notifyChangeLock} != 0;
1975 }
1976  
1977 #-------------------------------------------------------------------------------
1978  
1979 sub insert_text
1980 {
1981 my ( $self, $s, $hilite) = @_;
1982  
1983 #print "$s\n";
1984  
1985 return if $self-> {readOnly};
1986 return if !defined($s) or length( $s) == 0;
1987  
1988 $self-> begin_undo_group;
1989 $self-> cancel_block unless $self-> {blockType} == bt::CUA;
1990 my @cs = $self-> cursor;
1991 my @xy = @cs;
1992  
1993 my @ln = split( "\n", $s, -1);
1994 # my $zzz = join '|', @ln;
1995 #print "<|$zzz|>$cs[1]\n";
1996 # pop @ln unless length $ln[-1];
1997 $s = $self-> get_line( $cs[1]);
1998  
1999 #cursor position shift to end of the line if is greater than it:
2000 $cs[0] = length( $s) if $cs[0] > length( $s);
2001  
2002 my $cl = $cs[0] - length( $s);
2003 # $s .= ' 'x$cl if $cl > 0;
2004 # $cl = 0 if $cl < 0;
2005 $self-> lock_change(1);
2006  
2007 if ( scalar @ln == 1) {
2008 substr( $s, $cs[0], 0) = $ln[0];
2009 $self-> set_line( $cs[1], $s, q(add), $cs[0], $cl + length( $ln[0]));
2010 $self-> selection( $cs[0], $cs[1], $cs[0] + length( $ln[0]), $cs[1])
2011 if $hilite && $self-> {blockType} == bt::CUA;
2012 @xy = ( $xy[0] + length($ln[0]) ,$xy[1] );
2013  
2014 } else {
2015 my $spl0 = substr( $s, 0, $cs[0]);
2016 my $spl1 = substr( $s, $cs[0] );
2017 # substr( $s, $cs[0], length( $s) - $cs[0] ) = $ln[0];
2018 # substr( $s, $cs[0], 0 ) = $ln[0];
2019 $self-> lock;
2020 $self-> set_line( $cs[1], $spl0.$ln[0] );
2021 shift @ln;
2022 # $self-> insert_line( $cs[1] + 1, (@ln, $spl));
2023 $self-> insert_line( $cs[1] + 1, @ln);
2024 $self-> set_line( $cs[1] + scalar @ln, $ln[(scalar @ln) -1 ].$spl1 );
2025 $self-> selection( $cs[0], $cs[1], length( $ln[-1]), $cs[1]+scalar(@ln))
2026 if $hilite && $self-> {blockType} == bt::CUA;
2027 $self-> unlock;
2028 @xy = ( length( $ln[(scalar @ln) -1 ] ), $cs[1] + scalar @ln );
2029 }
2030 $self-> lock_change(0);
2031 $self-> end_undo_group;
2032  
2033 $self-> area( $self->{topLine}, $self->{rows} ) if @{$self->{hiliteBlok}};
2034  
2035 $self-> cursor( @xy );
2036 }
2037  
2038 sub insert_text_orig
2039 {
2040 my ( $self, $s, $hilite) = @_;
2041 return if !defined($s) or length( $s) == 0;
2042 $self-> begin_undo_group;
2043 $self-> cancel_block unless $self-> {blockType} == bt::CUA;
2044 my @cs = $self-> cursor;
2045 my @ln = split( "\n", $s, -1);
2046 pop @ln unless length $ln[-1];
2047 $s = $self-> get_line( $cs[1]);
2048 my $cl = $cs[0] - length( $s);
2049 $s .= ' 'x$cl if $cl > 0;
2050 $cl = 0 if $cl < 0;
2051 $self-> lock_change(1);
2052 if ( scalar @ln == 1) {
2053 substr( $s, $cs[0], 0) = $ln[0];
2054 $self-> set_line( $cs[1], $s, q(add), $cs[0], $cl + length( $ln[0]));
2055 $self-> selection( $cs[0], $cs[1], $cs[0] + length( $ln[0]), $cs[1])
2056 if $hilite && $self-> {blockType} == bt::CUA;
2057 } else {
2058 my $spl = substr( $s, $cs[0], length( $s) - $cs[0]);
2059 substr( $s, $cs[0], length( $s) - $cs[0]) = $ln[0];
2060 $self-> lock;
2061 $self-> set_line( $cs[1], $s);
2062 shift @ln;
2063 $self-> insert_line( $cs[1] + 1, (@ln, $spl));
2064 $self-> selection( $cs[0], $cs[1], length( $ln[-1]), $cs[1]+scalar(@ln))
2065 if $hilite && $self-> {blockType} == bt::CUA;
2066 $self-> unlock;
2067 }
2068 $self-> lock_change(0);
2069 $self-> end_undo_group;
2070 }
2071  
2072 sub paste
2073 {
2074 my $self = $_[0];
2075 return if $self-> {readOnly};
2076  
2077 #print "|".$::application-> Clipboard-> text."|\n";
2078 { #wb:
2079 $self-> delete_block if $self-> has_selection;
2080 }
2081  
2082 $self-> insert_text( $::application-> Clipboard-> text, 1);
2083  
2084 $self-> area( $self->{topLine}, $self->{rows} ) if @{$self->{hiliteBlok}};
2085  
2086 }
2087  
2088 sub make_logical
2089 {
2090 my ( $self, $x, $y) = @_;
2091 return (0,0) if $self-> {maxChunk} < 0;
2092 return $x, $y unless $self-> {wordWrap};
2093 my $maxY = $self-> {maxLine};
2094 $y = $maxY if $y > $maxY || $y < 0;
2095 $y = 0 if $y < 0;
2096 my $l = length( $self-> {lines}-> [$y]);
2097 $x = $l if $x < 0 || $x > $l;
2098 $x = 0 if $x < 0;
2099 my $cm = $self-> {chunkMap};
2100 my $r;
2101 ( $l, $r) = ( 0, $self-> {maxChunk} + 1);
2102 my $i = int($r / 2);
2103 my $kk = 0;
2104 while (1) {
2105 my $acd = $$cm[$i * 3 + 2];
2106 last if $acd == $y;
2107 $acd > $y ? $r : $l = $i;
2108 $i = int(( $l + $r) / 2);
2109 if ( $kk++ > 200) {
2110 # print "bcs dump to $y\n";
2111 ( $l, $r) = ( 0, $self-> {maxChunk} + 1);
2112 $i = int($r / 2);
2113 for ( $kk = 0; $kk < 7; $kk++) {
2114 my $acd = $$cm[$i * 3 + 2];
2115 # print "i:$i [$l $r] f() = $acd\n";
2116 $acd > $y ? $r : $l = $i;
2117 $i = int(( $l + $r) / 2);
2118 }
2119 die;
2120 last;
2121 }
2122 }
2123 $y = $i;
2124 $i*= 3;
2125 $i-= 3, $y-- while $$cm[ $i] != 0;
2126 $i+= 3, $y++ while $x > $$cm[ $i] + $$cm[ $i + 1];
2127 $x -= $$cm[ $i];
2128 return $x, $y;
2129 }
2130  
2131 sub make_physical
2132 {
2133 my ( $self, $x, $y) = @_;
2134 return (0,0) if $self-> {maxLine} < 0;
2135 return $x, $y unless $self-> {wordWrap};
2136 my $maxY = $self-> {maxChunk};
2137 $y = $maxY if $y > $maxY || $y < 0;
2138 $y = 0 if $y < 0;
2139 my $cm = $self-> {chunkMap};
2140 my ( $ofs, $l, $nY) = ( $$cm[ $y * 3], $$cm[ $y * 3 + 1], $$cm[ $y * 3 + 2]);
2141 $x = $l if $x < 0 || $x > $l;
2142 $x = 0 if $x < 0;
2143 return $x + $ofs, $nY;
2144 }
2145  
2146 sub start_block
2147 {
2148 my $self = $_[0];
2149 return if exists $self-> {anchor};
2150 my $blockType = $_[1] || $self-> {blockType};
2151 $self-> selection(0,0,0,0);
2152 $self-> blockType( $blockType);
2153 $self-> {anchor} = [ $self-> {cursorX}, $self-> {cursorY}];
2154 }
2155  
2156 sub update_block
2157 {
2158 my $self = $_[0];
2159 return unless exists $self-> {anchor};
2160 $self-> selection( @{$self-> {anchor}}, $self-> {cursorX}, $self-> {cursorY});
2161 }
2162  
2163 sub end_block
2164 {
2165 my $self = $_[0];
2166 return unless exists $self-> {anchor};
2167 my @anchor = @{$self-> {anchor}};
2168 delete $self-> {anchor};
2169 $self-> selection( @anchor, $self-> {cursorX}, $self-> {cursorY});
2170 }
2171  
2172 sub cancel_block
2173 {
2174 delete $_[0]-> {anchor};
2175 $_[0]-> selection(0,0,0,0);
2176 }
2177  
2178 sub set_marking
2179 {
2180 my ( $self, $mark, $blockType) = @_;
2181 return if $mark == exists $self-> {anchor};
2182 $mark ? $self-> start_block( $blockType || $self-> {blockType}) : $self-> end_block;
2183 }
2184  
2185 #wb
2186 sub kursor_w_dol {
2187 my ( $line, $vis_len_prev ) = @_;
2188  
2189 $line = '' unless $line;
2190  
2191 return $vis_len_prev unless $line =~ /\t/;
2192  
2193 $line = $line.(' 'x($vis_len_prev));
2194  
2195 my $dlu = 0;
2196 my $res = 0;
2197 while ( $line =~ /(.)/g ) {
2198 last if $dlu >= $vis_len_prev;
2199 if ( $1 eq "\t" ) {
2200 $dlu += 3 - ( $dlu % 4 );
2201 }
2202 $dlu++;
2203 $res++;
2204 }
2205 return $res;
2206 }
2207  
2208 sub cursor_down
2209 {
2210 my $d = $_[1] || 1;
2211 #print "cursor_down\n";
2212 my $l1 = $_[0]->get_line( $_[0]-> {cursorYl} );
2213 my $l2 = $_[0]->get_line( $_[0]-> {cursorYl} + $d );
2214  
2215 ($l1,undef) = wstaw_tab_15( $l1, 0, $_[0]-> {cursorX}, '', 0 );
2216 my $len2 = kursor_w_dol( $l2,length($l1));
2217 $_[0]-> cursorLog( $len2, $_[0]-> {cursorYl} + $d);
2218  
2219 $_[0]-> area( $_[0]->{topLine}, $_[0]->{rows} ) if $_[0]-> {hiliteBlok_modified} && @{$_[0]->{hiliteBlok}};
2220 }
2221  
2222 sub cursor_up
2223 {
2224 #print "cursor_up\n";
2225 return if $_[0]-> {cursorYl} == 0;
2226  
2227 my $d = $_[1] || 1;
2228  
2229 my $l1 = $_[0]->get_line( $_[0]-> {cursorYl} );
2230 my $l2 = $_[0]->get_line( $_[0]-> {cursorYl} - $d );
2231 ($l1,undef) = wstaw_tab_15( $l1, 0, $_[0]-> {cursorX}, '', 0 );
2232 my $len2 = kursor_w_dol( $l2,length($l1));
2233  
2234 my ( $x, $y) = $_[0]-> make_physical( $len2, $_[0]-> {cursorYl} - $d);
2235 # my ( $x, $y) = $_[0]-> make_physical( $_[0]-> {cursorXl}, $_[0]-> {cursorYl} - $d);
2236 $y = 0 if $y < 0;
2237 $_[0]-> cursor( $x, $y);
2238  
2239 $_[0]-> area( $_[0]->{topLine}, $_[0]->{rows} ) if $_[0]-> {hiliteBlok_modified} && @{$_[0]->{hiliteBlok}};
2240 }
2241  
2242 sub cursor_left
2243 {
2244 my $d = $_[1] || 1;
2245 my $x = $_[0]-> cursorX;
2246 if ( $x - $d >= 0) {
2247 $_[0]-> cursorX( $x - $d)
2248 } elsif ( $_[0]-> {cursorWrap}) {
2249 if ( $d == 1) {
2250 my $y = $_[0]-> cursorY - 1;
2251 $_[0]-> cursor( -1, $y < 0 ? 0 : $y);
2252 } else {
2253 $_[0]-> cursor_left( $d - 1);
2254 }
2255 } else {
2256 $_[0]-> cursorX( 0);
2257 }
2258 }
2259  
2260 sub cursor_right
2261 {
2262 my $d = $_[1] || 1;
2263 my $x = $_[0]-> cursorX;
2264 if ( $_[0]-> {cursorWrap} || $_[0]-> {wordWrap}) {
2265 my $y = $_[0]-> cursorY;
2266 if ( $x + $d > length( $_[0]-> get_line( $y))) {
2267 if ( $d == 1) {
2268 $_[0]-> cursor( 0, $y + 1) if $y < $_[0]-> {maxLine};
2269 } else {
2270 $_[0]-> cursor_right( $d - 1);
2271 }
2272 } else {
2273 $_[0]-> cursorX( $x + $d);
2274 }
2275 } else {
2276 $_[0]-> cursorX( $x + $d);
2277 }
2278 }
2279  
2280 sub cursor_home
2281 {
2282 my ($spaces) = ($_[0]-> get_line( $_[0]-> cursorY) =~ /^([s\t]*)/);
2283 $_[0]-> begin_undo_group;
2284 $_[0]-> offset(0);
2285 $_[0]-> cursorX(0);
2286 $_[0]-> end_undo_group;
2287 }
2288  
2289 sub cursor_end
2290 {
2291 my ($nonspaces) = ($_[0]-> get_line( $_[0]-> cursorY) =~ /^(.*?)[\s\t]*$/);
2292 $_[0]-> cursorX( length $nonspaces);
2293  
2294 }
2295  
2296 sub cursor_cend {
2297 $_[0]-> cursor( 0, -1 );
2298 $_[0]-> cursor_end;
2299 #print "cursor_cend\n";
2300  
2301 $_[0]-> area if $_[0]-> {hiliteBlok_modified} && @{$_[0]->{hiliteBlok}};
2302 }
2303  
2304 sub cursor_chome {
2305 $_[0]-> cursor( 0, 0 );
2306 #print "cursor_chome\n";
2307  
2308 $_[0]-> area if $_[0]-> {hiliteBlok_modified} && @{$_[0]->{hiliteBlok}};
2309 }
2310 #----------------------------------------------
2311 sub cursor_cpgup
2312 {
2313 my @xy = (0,0);
2314 #print "cursor_cpgup\n";
2315 if ( $_[0]->{cursorYl} > $_[0]->topLine ) {
2316 @xy = ( $_[0]-> {cursorXl}, $_[0]->topLine );
2317 }
2318 $_[0]-> cursorLog( @xy );
2319  
2320 $_[0]-> area if $_[0]-> {hiliteBlok_modified} && @{$_[0]->{hiliteBlok}};
2321 }
2322 #----------------------------------------------
2323 sub cursor_cpgdn
2324 {
2325 my @xy = ( 0, -1 );
2326 #print "cursor_cpgdn\n";
2327 if ( $_[0]->{cursorYl} < $_[0]->topLine + $_[0]->{rows} - 1 ) {
2328 @xy = ( $_[0]-> {cursorXl}, $_[0]->topLine + $_[0]->{rows} - 1 );
2329 }
2330 $_[0]-> cursorLog( @xy );
2331  
2332 $_[0]-> area if $_[0]-> {hiliteBlok_modified} && @{$_[0]->{hiliteBlok}};
2333  
2334 # my $cy = $_[0]->{cursorYl} == $_[0]->topLine + $_[0]->{rows} - 1 ? 0 : $_[0]->topLine + $_[0]->{rows};
2335 # $_[0]-> cursorLog( $_[0]-> {cursorXl}, $cy - 1 );
2336 }
2337 #----------------------------------------------
2338 sub cursor_pgup
2339 {
2340 my $d = $_[1] || 1;
2341 my $i;
2342 my @xy = $_[0]->cursor;
2343 for ( $i = 0; $i < $d; $i++) {
2344 my ( $tl, $r) = ($_[0]-> topLine , $_[0]-> {rows} );
2345  
2346 my $cy = $tl - $r;
2347  
2348 $_[0]-> cursorLog( $_[0]-> {cursorXl}, $cy < 0 ? 0 : $cy);
2349 $_[0]-> cursorLog( $_[0]-> {cursorXl}, $xy[1] < $r ? 0 : $xy[1] - $r );
2350  
2351 $_[0]-> area( $_[0]->{topLine}, $_[0]->{rows} ) if $_[0]-> {hiliteBlok_modified} && @{$_[0]->{hiliteBlok}};
2352 }
2353 }
2354 #----------------------------------------------
2355  
2356 sub cursor_pgdn {
2357 my $d = $_[1] || 1;
2358 my $i;
2359 my @xy = $_[0]->cursor;
2360 for ( $i = 0; $i < $d; $i++) {
2361 my ( $tl, $r) = ($_[0]-> topLine , $_[0]-> {rows} - 1);
2362  
2363 my $cy = $tl + 2*$r + 1;
2364  
2365 $_[0]-> cursorLog( $_[0]-> {cursorXl}, $cy);
2366 $_[0]-> cursorLog( $_[0]-> {cursorXl}, $xy[1] + $r + 1 );
2367  
2368 $_[0]-> area( $_[0]->{topLine}, $_[0]->{rows} ) if $_[0]-> {hiliteBlok_modified} && @{$_[0]->{hiliteBlok}};
2369 }
2370 }
2371  
2372 ##############################################
2373 ##############################################
2374  
2375 sub word_right
2376 {
2377 my $self = $_[0];
2378 my $d = $_[1] || 1;
2379 my $i;
2380 for ( $i = 0; $i < $d; $i++) {
2381 my ( $x, $y, $w, $delta, $maxY) = (
2382  
2383 $self-> cursorX, $self-> cursorY,
2384  
2385 $self-> wordDelimiters, 0, $self-> {maxLine}
2386 );
2387 my $line = $self-> get_line( $y);
2388 my $clen = length( $line);
2389 if ($self-> {cursorWrap}) {
2390 while ( $x >= $clen) {
2391 $y++;
2392 return if $y > $maxY;
2393 $x = 0;
2394 $line = $self-> get_line( $y);
2395 $clen = length( $line);
2396 }
2397 }
2398 my $cl = $x - $clen + 1;
2399 $line .= ' 'x$cl if $cl >= 0;
2400 unless ($w =~ quotemeta substr $line, $x, 1) {
2401 $delta++ while ( $w !~ quotemeta substr $line, $x + $delta, 1) &&
2402 $x + $delta < $clen;
2403 }
2404 if ( $x + $delta < $clen) {
2405 $delta++ while ( $w =~ quotemeta substr $line, $x + $delta, 1) &&
2406 $x + $delta < $clen;
2407 }
2408 $self-> cursor( $x + $delta, $y);
2409 }
2410 }
2411  
2412 sub word_left
2413 {
2414 my $self = $_[0];
2415 my $d = $_[1] || 1;
2416 my $i;
2417 for ( $i= 0;$i<$d; $i++) {
2418 my ( $x, $y, $w, $delta) =
2419 ( $self-> cursorX, $self-> cursorY, $self-> wordDelimiters, 0);
2420 my $line = $self-> get_line( $y);
2421 my $clen = length( $line);
2422 if ($self-> {cursorWrap}) {
2423 while ( $x == 0) {
2424 $y--;
2425 $y = 0, last if $y < 0;
2426 $line = $self-> get_line( $y);
2427 $x = $clen = length( $line);
2428 }
2429 }
2430 my $cl = $x - $clen + 1;
2431 $line .= ' 'x$cl if $cl >= 0;
2432 if ( $w =~ quotemeta( substr( $line, $x - 1, 1)))
2433 {
2434 $delta-- while (( $w =~ quotemeta( substr( $line, $x + $delta - 1, 1))) &&
2435 ( $x + $delta > 0))
2436 }
2437 if ( $x + $delta > 0)
2438 {
2439 $delta-- while (!( $w =~ quotemeta( substr( $line, $x + $delta - 1, 1))) &&
2440 ( $x + $delta > 0))
2441 }
2442 $self-> cursor( $x + $delta, $y);
2443 }
2444 }
2445  
2446 sub cursor_shift_key
2447 {
2448 my ( $self, $menuItem) = @_;
2449 $self-> begin_undo_group;
2450 $self-> start_block unless exists $self-> {anchor};
2451 $menuItem =~ s/Shift//;
2452 my $action = $self-> accelTable-> action( $menuItem);
2453 $action = $self-> can( $action, 0) unless ref $action;
2454 $self-> {delayPanning} = 1;
2455 $self-> blockShiftMark(1);
2456 $action-> ( @_);
2457 $self-> blockShiftMark(0);
2458 $self-> selection( @{$self-> {anchor}}, $self-> {cursorX}, $self-> {cursorY});
2459 $self-> realize_panning;
2460 $self-> end_undo_group;
2461 }
2462  
2463 sub blockShiftMark
2464 {
2465 return $_[0]-> {blockShiftMark} unless $#_;
2466 my ( $self, $mark) = @_;
2467 return if $self-> {blockShiftMark} == $mark;
2468 $self-> push_group_undo_action( 'blockShiftMark', $self-> {blockShiftMark});
2469 $self-> {blockShiftMark} = $mark;
2470 }
2471  
2472 sub mark_vertical
2473 {
2474 my $self = $_[0];
2475 if ( exists $self-> {anchor})
2476 {
2477 $self-> update_block;
2478 delete $self-> {restorePersistentBlock}, $self-> persistentBlock(0)
2479  
2480 if $self-> {restorePersistentBlock};
2481 } else {
2482 $self-> blockType( bt::Vertical);
2483 $self-> {restorePersistentBlock} = 1
2484  
2485 unless $self-> persistentBlock;
2486 $self-> persistentBlock( 1);
2487 $self-> cursor_shift_key(q(ShiftCursorRight));
2488 }
2489 }
2490  
2491 sub mark_horizontal
2492 {
2493 my $self = $_[0];
2494 if ( exists $self-> {anchor})
2495 {
2496 $self-> update_block;
2497 delete $self-> {restorePersistentBlock}, $self-> persistentBlock(0)
2498  
2499 if $self-> {restorePersistentBlock};
2500 } else {
2501 $self-> blockType( bt::Horizontal);
2502 $self-> {restorePersistentBlock} = 1 unless $self-> persistentBlock;
2503 $self-> persistentBlock( 1);
2504 $self-> start_block;
2505 $self-> selection(
2506  
2507 $self-> make_physical( 0, $self-> {cursorYl}),
2508 $self-> make_physical( -1, $self-> {cursorYl})
2509 );
2510 }
2511 }
2512  
2513 sub set_line
2514 {
2515 my ( $self, $y, $line, $operation, $from, $len) = @_;
2516 my $maxY = $self-> {maxLine};
2517 $self-> insert_empty_line(0), $y = $maxY = $self-> {maxLine} if $maxY < 0;
2518 return if $y > $maxY || $y < 0;
2519 my ( $newDim, $oldDim, $ry) = (0,0,0);
2520 my ( $fh, $tl, $ofs, @a) = (
2521 $self-> font-> height + $self-> {lineSpace}, $self-> {topLine }, $self-> {offset}, $self-> get_active_area,
2522 );
2523 my @sz = ( $a[2] - $a[0], $a[3] - $a[1]);
2524 my ( $_from, $_to);
2525 $self-> begin_undo_group;
2526 $self-> push_undo_action( 'set_line', $y, $self-> {lines}-> [$y]);
2527 if ( $self-> {wordWrap}) {
2528 my $breaks = $self-> text_wrap(
2529  
2530 $line,
2531  
2532 $sz[0] - $self-> {defcw},
2533  
2534 tw::WordBreak|tw::CalcTabs|tw::NewLineBreak|tw::ReturnChunks,
2535  
2536 $self-> {tabIndent}
2537 );
2538 my @chunkMap;
2539 ( undef, $ry) = $self-> make_logical( 0, $y);
2540 my ($ix, $cm) = ( $ry * 3 + 2, $self-> {chunkMap});
2541 my $max_ix = $self-> {maxChunk} * 3 + 2;
2542 $oldDim++, $ix += 3 while $ix <= $max_ix && $$cm[ $ix] == $y;
2543 $newDim = scalar @{$breaks} / 2;
2544 my $i;
2545 for ( $i = 0; $i < $newDim; $i++) {
2546 push( @chunkMap, $$breaks[$i * 2], $$breaks[$i * 2 + 1], $y);
2547 }
2548 splice( @{$cm}, $ry * 3, $oldDim * 3, @chunkMap);
2549  
2550 $self-> {lines}-> [$y] = $line;
2551 $self-> {maxChunk} -= $oldDim - $newDim;
2552 if ( $oldDim == $newDim) {
2553 ( $_from, $_to) = ( $ry, $ry + $oldDim);
2554 } else {
2555 $self-> vScroll( $self-> {maxChunk} >= $self-> {rows})
2556  
2557 if $self-> {autoVScroll};
2558 $self-> topLine(0) if $self-> {maxChunk} < $self-> {rows};
2559 $self-> {vScrollBar}-> set(
2560 max => $self-> {maxChunk} - $self-> {rows} + 1,
2561 whole => $self-> {maxChunk} + 1,
2562 ) if $self-> {vScroll};
2563 }
2564 } else {
2565 my ( $oldL, $newL) = ( length( $self-> {lines}-> [$y]), length( $line));
2566 $self-> {lines}-> [$y] = $line;
2567 if ( $oldL == $self-> {maxLineLength} || $newL > $self-> {maxLineLength})
2568 {
2569 my $needReset = 0;
2570 if ( $newL != $oldL) {
2571 if ( $oldL == $self-> {maxLineLength}) {
2572 $self-> {maxLineCount}--;
2573 $needReset = $self-> {maxLineCount} <= 0;
2574 }
2575 if ( $newL > $self-> {maxLineLength}) {
2576 $self-> {maxLineLength} = $newL;
2577 $self-> {maxLineCount} = 1;
2578 $self-> {maxLineWidth} = $newL * $self-> {averageWidth};
2579 $needReset = 0;
2580 }
2581 $self-> reset if $needReset;
2582 }
2583 my $lw = $self-> {maxLineWidth};
2584 if ( $self-> {autoHScroll}) {
2585 my $hs = ( $lw > $sz[0] ) ? 1 : 0;
2586 if ( $hs != $self-> {hScroll}) {
2587 $self-> hScroll( $hs);
2588 @a = $self-> get_active_area;
2589 @sz = ( $a[2] - $a[0], $a[3] - $a[1]);
2590 }
2591 }
2592 $self-> {hScrollBar}-> set(
2593 max => $lw - $sz[0],
2594 whole => $lw,
2595 partial => $sz[0],
2596 ) if $self-> {hScroll};
2597 }
2598 $_from = $_to = $y;
2599 }
2600  
2601 $self-> {syntax}-> [$y] = undef;
2602  
2603 if ( defined $operation && $self-> has_selection) {
2604 if ( $operation ne q(overtype)) {
2605 $len *= -1 if $operation eq q(delete);
2606 my @sel = $self-> selection;
2607 if ( $self-> {blockType} == bt::CUA) {
2608 if ( $sel[3] == $sel[1] && $sel[1] == $y) {
2609 $sel[0] += $len if $from < $sel[0];
2610 if ( $from < $sel[2]) {
2611 $sel[2] += $len;
2612 @sel=(0,0,0,0) if $sel[2] <= $from;
2613 }
2614 } elsif ( $sel[1] == $y && $from < $sel[0]) {
2615 $sel[0] += $len;
2616 } elsif ( $sel[3] == $y && $from < $sel[2]) {
2617 $sel[2] += $len;
2618 }
2619 $sel[0] = 0 if $sel[0] < 0;
2620 $sel[2] = 0 if $sel[2] < 0;
2621 } elsif ( $newDim != $oldDim) {
2622 my @selE = @{$self-> {selEndl}};
2623 $selE[1] -= $oldDim - $newDim;
2624 ($sel[2], $sel[3]) = $self-> make_physical( @selE);
2625 @sel = (0,0,0,0) if $sel[3] < $sel[1];
2626 }
2627 $self-> selection( @sel);
2628 delete $self-> {anchor};
2629 $self-> cancel_block unless $self-> has_selection;
2630 }
2631 } else {
2632 $self-> cancel_block;
2633 }
2634  
2635 if ( defined $_to)
2636 {
2637 $self-> invalidate_rect(
2638 $a[0], $a[3] - $fh * ( $_to - $tl + 1),
2639 $a[2], $a[3] - $fh * ( $_from - $tl)
2640 );
2641 } else {
2642 $self-> repaint;
2643 }
2644 $self-> cursor( $self-> cursor);
2645 $self-> end_undo_group;
2646 $self-> notify(q(Change)) unless $self-> {notifyChangeLock};
2647  
2648 }
2649  
2650 sub insert_empty_line
2651 {
2652 my ( $self, $y, $len) = @_;
2653 my $maxY = $self-> {maxLine};
2654 $len ||= 1;
2655 return if $y > $maxY + 1 || $y < 0 || $len == 0;
2656 my $ly;
2657 $self-> push_undo_action('delete_line', $y, $len);
2658 if ( $self-> {wordWrap}) {
2659 if ( $y > $maxY) {
2660 $ly = $self-> {maxChunk} + 1;
2661 } else {
2662 ( undef, $ly) = $self-> make_logical( 0, $y);
2663 }
2664 my ($i, $maxC, $cm) = ( 0, $self-> {maxChunk}, $self-> {chunkMap});
2665 if ( $y <= $maxY) {
2666 splice( @{$cm}, $ly * 3, 0, ( 0, 0, $y) x $len);
2667 for ( $i = $ly + 1; $i < $ly + $len; $i++) {
2668  
2669 $$cm[ $i * 3 + 2] += $i - $ly;
2670  
2671 }
2672 for ( $i = $ly + $len; $i <= $maxC + $len; $i++) {
2673  
2674 $$cm[ $i * 3 + 2] += $len;
2675  
2676 }
2677 } else {
2678 push( @{$cm}, ( 0, 0, $y)x$len);
2679 for ( $i = $ly; $i < $ly + $len; $i++) {
2680  
2681 $$cm[ $i * 3 + 2] += $i - $ly;
2682  
2683 }
2684 }
2685 $self-> {maxChunk} += $len;
2686 } else {
2687 $self-> {maxChunk} += $len;
2688 $ly = $y;
2689 $self-> {maxLineCount} += $len if $self-> {maxLineLength} == 0;
2690 }
2691 for (@{$self-> {markers}}) {
2692  
2693 $$_[1] += $len if $$_[1] >= $y;
2694  
2695 }
2696 splice( @{$self-> {lines}}, $y, 0, ('') x $len);
2697 $self-> {maxLine} += $len;
2698 splice( @{$self-> {syntax}}, $y, 0, ([0,cl::Black]) x $len)
2699  
2700 if $self-> {syntaxHilite};
2701  
2702 if ( $self-> has_selection) {
2703 my @sel = $self-> selection;
2704 unless ( $sel[3] < $y) {
2705 $sel[1] += $len if $sel[1] >= $y;
2706 $sel[3] += $len;
2707 }
2708 $self-> selection( @sel);
2709 delete $self-> {anchor};
2710 }
2711 my ( $tl, $rc, $yt, $fh, @a) = (
2712 $self-> {topLine }, $self-> {rows}, $self-> {yTail},
2713 $self-> font-> height + $self-> {lineSpace}, $self-> get_active_area,
2714 );
2715 if (
2716  
2717 $y < $tl + $rc + $yt - 1 &&
2718  
2719 $y + $len > $tl &&
2720  
2721 $y <= $maxY &&
2722  
2723 !$self-> has_selection
2724 ) {
2725 $self-> scroll( 0, -$fh * $len,
2726 confineRect => [ @a[0..2], $a[3] - $fh * ( $y - $tl)]);
2727 }
2728 $self-> vScroll( $self-> {maxChunk} >= $self-> {rows}) if $self-> {autoVScroll};
2729 $self-> {vScrollBar}-> set(
2730 max => $self-> {maxChunk} - $self-> {rows} + 1,
2731 whole => $self-> {maxChunk} + 1,
2732 partial => $self-> {rows},
2733 ) if $self-> {vScroll};
2734 return $ly;
2735 }
2736  
2737 #--------------------------------------------------------------------------------------
2738  
2739 sub insert_line
2740 {
2741 my ( $self, $y, @lines) = @_;
2742 my $len = scalar @lines;
2743 my $maxY = $self-> {maxLine};
2744 return if $y > $maxY + 1 || $y < 0 || $len == 0;
2745 my $i;
2746 $self-> begin_undo_group;
2747 $self-> insert_empty_line( $y, $len);
2748 $self-> lock_change(1);
2749 for ( $i = 0; $i < $len; $i++) {
2750 $self-> set_line( $y + $i, $lines[ $i], q(add), 0, length( $lines[ $i]));
2751 }
2752 $self-> lock_change(0);
2753 $self-> end_undo_group;
2754 }
2755  
2756 sub delete_line
2757 {
2758 my ( $self,$y,$len) = @_;
2759 my $maxY = $self-> {maxLine};
2760 $len ||= 1;
2761 return if $y > $maxY || $y < 0 || $len == 0;
2762  
2763 $self-> begin_undo_group;
2764 for ( my $i=0; $i < $len; $i++) {
2765 $self-> push_undo_action( 'set_line', $y+$i, $self-> {lines}-> [$y+$i]);
2766 }
2767 $self-> push_undo_action( 'insert_empty_line', $y, $len);
2768  
2769 $len = $maxY - $y + 1 if $y + $len > $maxY + 1;
2770 my ( $lx, $ly) = (0,0);
2771 if ( $self-> {wordWrap}) {
2772 ( $lx, $ly) = $self-> make_logical( 0, $y);
2773 $lx = 0;
2774 my ($i, $maxC, $cm) = ($ly, $self-> {maxChunk}, $self-> {chunkMap});
2775 $lx++, $i++ while ( $i <= $maxC) and ( $$cm[ $i * 3 + 2] <= ( $y + $len - 1));
2776 splice( @{$cm}, $ly * 3, $lx * 3);
2777 $self-> {maxChunk} -= $lx;
2778 for ( $i = $ly; $i <= $maxC - $lx; $i++) { $$cm[ $i * 3 + 2] -= $len; }
2779 } else {
2780 $self-> {maxChunk} -= $len;
2781 }
2782  
2783 my @removed = splice( @{$self-> {lines}}, $y, $len);
2784 splice( @{$self-> {syntax}}, $y, $len) if $self-> {syntaxHilite};
2785 for (@{$self-> {markers}}) {
2786 $$_[1] -= $len if $$_[1] >= $y;
2787 $$_[1] = 0 if $$_[1] < 0;
2788 }
2789  
2790 $self-> {maxLine} -= $len;
2791 if ( $self-> has_selection) {
2792 my @sel = (@{$self-> {selStartl}}, @{$self-> {selEndl}});
2793 if ( $sel[3] >= $ly) {
2794 if ( $sel[1] >= $ly) {
2795 $sel[1] -= $lx;
2796 $sel[0] = 0, $sel[1] = $ly if $sel[1] < $ly;
2797 }
2798 $sel[3] -= $lx;
2799 $sel[2] = 0, $sel[3] = $ly if $sel[3] < $ly;
2800 }
2801 $self-> selection(
2802  
2803 $self-> make_physical($sel[0], $sel[1]),
2804  
2805 $self-> make_physical($sel[2], $sel[3])
2806 );
2807 delete $self-> {anchor};
2808 $self-> cancel_block unless $self-> has_selection;
2809 }
2810  
2811 $self-> vScroll( $self-> {maxChunk} >= $self-> {rows}) if $self-> {autoVScroll};
2812 $self-> topLine(0) if $self-> {maxChunk} < $self-> {rows};
2813 $self-> {vScrollBar}-> set(
2814 max => $self-> {maxChunk} - $self-> {rows} + 1,
2815 whole => $self-> {maxChunk} + 1,
2816 ) if $self-> {vScroll};
2817  
2818 unless ( $self-> {wordWrap}) {
2819 my $mlv = $self-> {maxLineLength};
2820 for ( @removed) {
2821 $self-> {maxLineCount}-- if length($_) == $mlv;
2822 if ( $self-> {maxLineCount} <= 0) {
2823 $self-> reset;
2824 my $lw = $self-> {maxLineWidth};
2825 my $w = $self-> width -
2826  
2827 $self-> {indents}-> [0] -
2828  
2829 $self-> {indents}-> [2];
2830 if ( $self-> {autoHScroll}) {
2831 my $hs = ( $lw > $w) ? 1 : 0;
2832 if ( $hs != $self-> {hScroll}) {
2833 $self-> hScroll( $hs);
2834 $w = $self-> width -
2835  
2836 $self-> {indents}-> [0] -
2837  
2838 $self-> {indents}-> [2];
2839 }
2840 }
2841 $self-> {hScrollBar}-> set(
2842 max => $lw - $w,
2843 whole => $lw,
2844 partial => $w,
2845 ) if $self-> {hScroll};
2846 last;
2847 }
2848 }
2849 }
2850 $self-> cursor( $self-> cursor);
2851 $self-> end_undo_group;
2852 $self-> repaint;
2853 $self-> notify(q(Change)) unless $self-> {notifyChangeLock};
2854 }
2855  
2856 sub delete_chunk
2857 {
2858 my ( $self, $y, $len) = @_;
2859 my $maxY = $self-> {maxChunk};
2860 $len ||= 1;
2861 return if $y > $maxY || $y < 0 || $len == 0;
2862  
2863 $self-> delete_line( $y, $len), return unless $self-> {wordWrap};
2864 $len = $maxY - $y + 1 if $y + $len > $maxY + 1;
2865 return if $len == 0;
2866  
2867 my $cm = $self-> {chunkMap};
2868 my $psy = $$cm[ $y * 3 + 2];
2869 my $pey = $$cm[($y + $len - 1) * 3 + 2];
2870 my $start = $$cm[ $y * 3];
2871 my $end = $$cm[($y + $len - 1) * 3] + $$cm[($y + $len - 1) * 3 + 1];
2872 if ( $psy == $pey) {
2873 my $c = $self-> {lines}-> [$psy];
2874 $self-> delete_line( $psy), return
2875  
2876 if $start == 0 && $end == length( $c);
2877 substr( $c, $start, $end - $start) = '';
2878 $self-> set_line( $psy, $c, q(delete), $start, $end - $start);
2879 return;
2880 }
2881 $self-> lock;
2882 my ( $sy, $ey) = ( $psy, $pey);
2883 my $c;
2884 $self-> begin_undo_group;
2885 $self-> lock_change(1);
2886 if ( $start > 0) {
2887 $c = $self-> {lines}-> [$psy];
2888 my $cs = length( $c) - $start + 1;
2889 substr( $c, $start, $cs) = '';
2890 $self-> set_line( $psy, $c, q(delete), $start, $cs);
2891 $sy++;
2892 }
2893 $c = $self-> {lines}-> [$pey];
2894 if ( $end < length( $c)) {
2895 substr( $c, 0, $end) = '';
2896 $self-> set_line( $pey, $c, q(delete), 0, $end);
2897 $ey--;
2898 }
2899 $self-> delete_line( $sy, $ey - $sy + 1) if $ey >= $sy;
2900 $self-> cursor( $self-> {cursorX}, $psy);
2901 $self-> unlock;
2902 $self-> lock_change(0);
2903 $self-> end_undo_group;
2904 }
2905  
2906 sub delete_text
2907 {
2908 my ( $self, $x, $y, $len) = @_;
2909 my $maxY = $self-> {maxLine};
2910 $y = $maxY if $y < 0;
2911 return if $y > $maxY || $y < 0;
2912  
2913 my $c = $self-> {lines}-> [ $y];
2914 my $l = length( $c);
2915 $x = $l if $x < 0;
2916 return if $x < 0;
2917  
2918 if ( $x == $l) {
2919 return if $y == $maxY;
2920 $self-> lock_change(1);
2921 $self-> begin_undo_group;
2922 $self-> set_line( $y, $self-> get_line( $y) . $self-> get_line( $y + 1));
2923 $self-> delete_line( $y + 1);
2924 $self-> end_undo_group;
2925 $self-> lock_change(0);
2926 return;
2927 }
2928 $len = $l - $x if $len + $x >= $l;
2929 return if $len <= 0;
2930 substr( $c, $x, $len) = '';
2931 $self-> set_line( $y, $c, q(delete), $x, $len);
2932 }
2933  
2934 sub delete_char
2935 {
2936 my $self = $_[0];
2937 $self-> delete_text( $self-> cursor, $_[1] || 1);
2938 }
2939  
2940 sub back_char
2941 {
2942 my $self = $_[0];
2943  
2944 {#wb
2945 if ( $self-> has_selection ) {
2946 $self-> delete_block;
2947 return;
2948 }
2949 }
2950  
2951 my @c = $self-> cursor;
2952 my $d = $_[1] || 1;
2953  
2954 $self-> begin_undo_group;
2955 if ( $c[0] >= $d) {
2956 $self-> delete_text( $c[0] - $d, $c[1], $d);
2957 $self-> cursorX( $c[0] - $d);
2958 } elsif ( $c[1] > 0) {
2959 $self-> cursor( -1, $c[1] - 1);
2960 $self-> delete_text( -1, $c[1] - 1);
2961 }
2962 $self-> end_undo_group;
2963 }
2964  
2965 sub delete_current_chunk
2966 {
2967 my $self = $_[0];
2968 $self-> delete_chunk( $self-> {cursorYl});
2969 }
2970  
2971 sub delete_to_end
2972 {
2973 my $self = $_[0];
2974 my @cs = $self-> cursor;
2975 my $c = $self-> get_line( $cs[1]);
2976 return if $cs[ 0] > length( $c);
2977  
2978 $self-> set_line( $cs[1], substr( $c, 0, $cs[0]), q(delete), $cs[0], length( $c) - $cs[0]);
2979 }
2980  
2981 sub delete_block
2982 {
2983 my $self = $_[0];
2984 return unless $self-> has_selection;
2985  
2986 $self-> begin_undo_group;
2987 $self-> push_undo_action('selection', $self-> selection);
2988  
2989 my @sel = ( @{$self-> {selStartl}}, @{$self-> {selEndl}});
2990 my $bt = $self-> {blockType};
2991  
2992 if ( $bt == bt::Horizontal) {
2993 $self-> delete_chunk( $sel[1], $sel[3] - $sel[1] + 1);
2994 } elsif ( $bt == bt::CUA) {
2995 my $c;
2996 my @sel = ( @{$self-> {selStart}}, @{$self-> {selEnd}});
2997  
2998 #print "1=$sel[1] / 3= $sel[3]\n";
2999  
3000 if ( $sel[1] == $sel[3]) {
3001 $c = $self-> get_line( $sel[1]);
3002 substr( $c, $sel[0], $sel[2] - $sel[0]) = '';
3003 $self-> set_line( $sel[1], $c);
3004 } else {
3005 my ( $from, $len) = ( $sel[1], $sel[3] - $sel[1]);
3006  
3007 my $res = substr( $self-> get_line( $from), 0, $sel[0]);
3008 $c = $self-> get_line( $sel[3]);
3009 $sel[2] = length( $c) if $sel[2] > length( $c);
3010 $res .= substr( $c, $sel[2], length( $c) - $sel[2]);
3011  
3012 # } elsif ( $sel[3] < $self-> {maxLine}) {
3013 # $res .= $self-> get_line( $sel[3] + 1);
3014 # }
3015  
3016 $self-> lock_change(1);
3017 $self-> delete_line( $from + 1, $len) if $len > 0;
3018 $self-> set_line( $from, $res);
3019  
3020 $self-> lock_change(0);
3021 }
3022 } else {
3023 my @sel = ( @{$self-> {selStart}}, @{$self-> {selEnd}});
3024 my $len = $sel[2] - $sel[0];
3025 my $i;
3026 $self-> lock_change(1);
3027 $self-> lock;
3028 for ( $i = $sel[1]; $i <= $sel[3]; $i++)
3029 {
3030 my $c = $self-> get_line( $i);
3031 if ( $c ne '') {
3032 substr( $c, $sel[0], $len) = '';
3033 $self-> set_line( $i, $c);
3034 }
3035 }
3036 $self-> unlock;
3037 $self-> lock_change(0);
3038 }
3039  
3040 $self-> cursorLog( $sel[0], $sel[1]);
3041 $self-> cancel_block;
3042 $self-> end_undo_group;
3043 }
3044  
3045 sub copy_block
3046 {
3047 my $self = $_[0];
3048 return if
3049  
3050 $self-> {readOnly} ||
3051  
3052 $self-> {blockType} == bt::CUA ||
3053  
3054 $self-> {wordWrap} ||
3055  
3056 !$self-> has_selection;
3057  
3058 my @sel = $self-> selection;
3059 $self-> lock_change(0);
3060 $self-> lock;
3061 $self-> begin_undo_group;
3062 if ( $self-> {blockType} == bt::Horizontal) {
3063 my @lines;
3064 my $i;
3065 for ( $i = $sel[1]; $i <= $sel[3]; $i++) {
3066  
3067 push @lines, $self-> get_line( $i);
3068 }
3069 $self-> insert_line( $self-> cursorY, @lines);
3070 } else {
3071 my @lines;
3072 my $i;
3073 for ( $i = $sel[1]; $i <= $sel[3]; $i++) {
3074 my $c = $self-> get_line( $i);
3075 $c .= ' ' x ($sel[2]-length($c))
3076  
3077 if length($c) < $sel[2];
3078 push( @lines, substr( $c, $sel[0], $sel[2]-$sel[0]));
3079 }
3080 my @cs = $self-> cursor;
3081 for ( $i = $cs[1]; $i < $cs[1] + scalar @lines; $i++) {
3082 my $c = $self-> get_line( $i);
3083 $c .= ' 'x($cs[0]-length($c)) if length($c) < $cs[0];
3084 substr( $c, $cs[0], 0) = $lines[ $i - $cs[1]];
3085 $self-> set_line( $i, $c);
3086 }
3087 }
3088 $self-> end_undo_group;
3089 $self-> unlock;
3090 $self-> lock_change(1);
3091 }
3092  
3093 sub overtype_block
3094 {
3095 my $self = $_[0];
3096 return if
3097  
3098 $self-> {readOnly} ||
3099  
3100 $self-> {blockType} == bt::CUA ||
3101  
3102 $self-> {wordWrap} ||
3103  
3104 !$self-> has_selection;
3105  
3106 my @sel = $self-> selection;
3107 $self-> lock_change(0);
3108 $self-> lock;
3109 $self-> begin_undo_group;
3110 if ( $self-> {blockType} == bt::Horizontal) {
3111 my $i;
3112 for ( $i = $sel[1]; $i <= $sel[3]; $i++) {
3113 $self-> set_line( $i, $self-> get_line( $i));
3114 }
3115 } else {
3116 my @lines;
3117 my $i;
3118 for ( $i = $sel[1]; $i <= $sel[3]; $i++) {
3119 my $c = $self-> get_line( $i);
3120 $c .= ' ' x ($sel[2]-length($c)) if length($c) < $sel[2];
3121 push( @lines, substr( $c, $sel[0], $sel[2]-$sel[0]));
3122 }
3123 my @cs = $self-> cursor;
3124 my $bx = $sel[3] - $sel[1] + 1;
3125 for ( $i = $cs[1]; $i < $cs[1] + scalar @lines; $i++) {
3126 my $c = $self-> get_line( $i);
3127 $c .= ' ' x ($cs[0]-length($c))
3128  
3129 if length($c) < $cs[0];
3130 substr( $c, $cs[0], $bx) = $lines[ $i - $cs[1]];
3131 $self-> set_line( $i, $c);
3132 }
3133 }
3134 $self-> end_undo_group;
3135 $self-> unlock;
3136 $self-> lock_change(1);
3137 }
3138  
3139 sub split_line
3140 {
3141 my $self = $_[0];
3142 my @cs = $self-> cursor;
3143 my $c = $self-> get_line( $cs[1]);
3144 $c .= ' 'x($cs[0]-length($c)) if length($c) < $cs[0];
3145 my ( $old, $new) = ( substr( $c, 0, $cs[0]), substr( $c, $cs[0], length( $c) - $cs[0]));
3146 $self-> lock_change(1);
3147 $self-> begin_undo_group;
3148 $self-> set_line( $cs[1], $old, q(delete), $cs[0], length( $c) - $cs[0]);
3149 my $cshift = 0;
3150 if ( $self-> {autoIndent}) {
3151 my $i = 0;
3152 my $add = '';
3153 for ( $i = 0; $i < length( $old); $i++) {
3154 my $c = substr( $old, $i, 1);
3155 last if $c ne ' ' and $c ne '\t';
3156 $add .= $c;
3157 }
3158 $new = $add.$new, $cshift = length( $add)
3159  
3160 if length( $add) < length( $old);
3161 }
3162 $self-> insert_line( $cs[1]+1, $new);
3163 $self-> cursor( $cshift, $cs[1] + 1);
3164 $self-> end_undo_group;
3165 $self-> lock_change(0);
3166 }
3167  
3168 sub begin_undo_group
3169  
3170 {
3171  
3172 my $self = $_[0];
3173 return if !$self-> {undoLimit};
3174 if ( $self-> {undo_in_action}) {
3175 push @{$self-> {redo}}, [] unless $self-> {grouped_undo}++;
3176 } else {
3177 push @{$self-> {undo}}, [] unless $self-> {grouped_undo}++;
3178 $self-> {redo} = [] if !$self-> {redo_in_action};
3179 }
3180 }
3181  
3182 sub end_undo_group
3183 {
3184 my $self = $_[0];
3185 return if !$self-> {undoLimit};
3186  
3187 my $ref = $self-> {undo_in_action} ? 'redo' : 'undo';
3188 $self-> {grouped_undo}-- if $self-> {grouped_undo} > 0;
3189 # skip last record if empty
3190 pop @{$self-> {$ref}}
3191  
3192 if !$self-> {grouped_undo} &&
3193  
3194 @{$self-> {$ref}} &&
3195  
3196 0 == @{$self-> {$ref}-> [-1]};
3197 shift @{$self-> {$ref}} if @{$self-> {$ref}} > $self-> {undoLimit};
3198 }
3199  
3200 sub push_undo_action
3201 {
3202 my $self = shift;
3203 return if !$self-> {undoLimit};
3204  
3205 my $ref = $self-> {undo_in_action} ? 'redo' : 'undo';
3206 my $action = [ @_ ];
3207 if ( $self-> {grouped_undo}) {
3208 push @{$self-> {$ref}-> [-1]}, $action;
3209 } else {
3210 push @{$self-> {$ref}}, [ $action ];
3211 shift @{$self-> {$ref}} if @{$self-> {$ref}} > $self-> {undoLimit};
3212 $self-> {redo} = []
3213  
3214 if !$self-> {redo_in_action} && !$self-> {undo_in_action};
3215 }
3216 }
3217  
3218 sub push_group_undo_action
3219 {
3220 my $self = shift;
3221 return if !$self-> {undoLimit};
3222 my $ref = $self-> {undo_in_action} ? 'redo' : 'undo';
3223 return $self-> push_undo_action(@_) if $self-> {grouped_undo};
3224  
3225 push @{$self-> {$ref}}, [] unless @{$self-> {$ref}};
3226 $self-> {grouped_undo} = 1;
3227 $self-> push_undo_action(@_);
3228 $self-> {grouped_undo} = 0;
3229 }
3230  
3231 sub undo
3232 {
3233 my $self = $_[0];
3234 return if $self-> {undo_in_action} || !$self-> {undoLimit};
3235 return unless @{$self-> {undo}};
3236 my $group = pop @{$self-> {undo}};
3237 return unless $group && @$group;
3238  
3239 $self-> {undo_in_action} = 1;
3240 $self-> begin_undo_group;
3241 for ( reverse @$group) {
3242 my ( $method, @params) = @$_;
3243 next unless $self-> can($method);
3244 $self-> $method( @params);
3245 }
3246 $self-> end_undo_group;
3247 $self-> {undo_in_action} = 0;
3248 }
3249  
3250 sub redo
3251 {
3252 my $self = $_[0];
3253  
3254 return if !$self-> {undoLimit};
3255 return unless @{$self-> {redo}};
3256 my $group = pop @{$self-> {redo}};
3257 return unless $group && @$group;
3258  
3259 $self-> {redo_in_action} = 1;
3260 $self-> begin_undo_group;
3261 for ( reverse @$group) {
3262 my ( $method, @params) = @$_;
3263 next unless $self-> can($method);
3264 $self-> $method( @params);
3265 }
3266 $self-> end_undo_group;
3267 $self-> {redo_in_action} = 0;
3268 }
3269  
3270 sub undoLimit
3271 {
3272 return $_[0]-> {undoLimit} unless $#_;
3273  
3274 my ( $self, $ul) = @_;
3275 $self-> {undoLimit} = $ul if $ul >= 0;
3276 splice @{$self-> {undo}}, 0, $ul - @{$self-> {undo}} if @{$self-> {undo}} > $ul;
3277 }
3278  
3279 sub find
3280 {
3281 my ( $self, $line, $x, $y, $replaceLine, $options) = @_;
3282 $x ||= 0;
3283 $y ||= 0;
3284 my $maxY = $self-> {maxLine};
3285 return if $y > $maxY || $maxY < 0;
3286  
3287 $line = '('.quotemeta( $line).')' unless $options & fdo::RegularExpression;
3288 $replaceLine = quotemeta( $replaceLine), $replaceLine =~ s[\\(\$\d+)][$1]g
3289 if !($options & fdo::RegularExpression) && defined $replaceLine;
3290 $y = $maxY if $y < 0;
3291 my $c = $self-> get_line( $y);
3292 my ( $l, $b, $len, $subLine, $re, $re2, $esub);
3293 $x = length( $c) if $x < 0;
3294 $re = '/';
3295 $re .= '\\b' if $options & fdo::WordsOnly;
3296 $re .= "$line";
3297 $re .= '\\b' if $options & fdo::WordsOnly;
3298 $re .= '/';
3299 $re2 = '';
3300 $re2.= 'i' unless $options & fdo::MatchCase;
3301  
3302 unless ( $options & fdo::BackwardSearch) {
3303 $l = 0;
3304 $subLine = substr( $c, 0, $x);
3305 substr( $c, 0, $x) = '';
3306 $esub = eval(<<FINDER);
3307 sub {
3308 \$_ = \$c;
3309 while ( 1) {
3310 if ( $re$re2) {
3311 \$l = length(\$`);
3312 \$len = length(\$&);
3313 s$re$replaceLine/$re2 if defined \$replaceLine;
3314 return \$l+\$x, \$y, \$len, \$subLine.\$_;
3315 }
3316 \$y++;
3317 return if \$y > \$maxY;
3318 \$_ = \$self-> get_line( \$y);
3319 \$subLine = '';
3320 \$l = \$x = 0;
3321 }
3322 }
3323 FINDER
3324 } else {
3325 $re2 .= 'g';
3326 $l = $b = undef;
3327 $subLine = substr( $c, $x, length( $c) - $x);
3328 substr( $c, $x, length( $c) - $x) = '';
3329 $esub = eval(<<FINDER);
3330 sub {
3331 \$_ = \$c;
3332 while ( 1) {
3333 \$l = \$b, \$b = pos(\$_) while $re$re2;
3334 if ( defined \$b) {
3335 \$l ||= 0;
3336 pos = \$l;
3337 \$l = $re$re2;
3338 \$len = length(\$&);
3339 \$l = pos(\$_) - \$len;
3340 substr(\$_, \$l, \$len) = \"$replaceLine\" if defined \$replaceLine;
3341 pos = undef;
3342 return \$l, \$y, \$len, \$_.\$subLine;
3343 }
3344 \$y--;
3345 return if \$y < 0;
3346 \$_ = \$self-> get_line( \$y);
3347 \$subLine = '';
3348  
3349 }
3350 FINDER
3351 }
3352 return $esub-> ();
3353 }
3354  
3355 sub add_marker
3356 {
3357 my ( $self, $x, $y) = @_;
3358 push( @{$self-> {markers}}, [$x, $y]);
3359 }
3360  
3361 sub delete_marker
3362 {
3363 my ( $self, $index) = @_;
3364 return if $index > scalar @{$self-> {markers}};
3365 splice( @{$self-> {markers}}, $index, 1);
3366 }
3367  
3368 sub select_all { $_[0]-> selection(0,0,-1,-1); }
3369  
3370 sub autoIndent {($#_)?($_[0]-> {autoIndent} = $_[1]) :return $_[0]-> {autoIndent } }
3371 sub blockType {($#_)?($_[0]-> set_block_type ( $_[1])) :return $_[0]-> {blockType} }
3372 sub cursor {($#_)?($_[0]-> set_cursor ($_[1],$_[2])) :return $_[0]-> {cursorX},$_[0]-> {cursorY}}
3373 sub cursorLog {($#_)?($_[0]-> set_cursor ($_[0]-> make_physical($_[1],$_[2]))) :return $_[0]-> {cursorXl},$_[0]-> {cursorYl}}
3374 sub cursorX {($#_)?($_[0]-> set_cursor ($_[1],$_[0]-> {cursorY})):return $_[0]-> {cursorX} }
3375 sub cursorY {($#_)?($_[0]-> set_cursor ($_[0]-> {q(cursorX)},$_[1])):return $_[0]-> {cursorY} }
3376 sub cursorWrap {($#_)?($_[0]-> {cursorWrap }=$_[1]) :return $_[0]-> {cursorWrap }}
3377 sub topLine {($#_)?($_[0]-> set_top_line ( $_[1])) :return $_[0]-> {topLine } }
3378 sub hiliteNumbers {($#_)?$_[0]-> set_hilite_numbers ($_[1]) :return $_[0]-> {hiliteNumbers} }
3379 sub hiliteQStrings {($#_)?$_[0]-> set_hilite_q_strings($_[1]) :return $_[0]-> {hiliteQStrings} }
3380 sub hiliteQQStrings {($#_)?$_[0]-> set_hilite_qq_strings($_[1]) :return $_[0]-> {hiliteQQStrings} }
3381 sub hiliteChars {($#_)?$_[0]-> set_hilite_chars ($_[1]) :return $_[0]-> {hiliteChars } }
3382 sub hiliteIDs {($#_)?$_[0]-> set_hilite_ids ($_[1]) :return $_[0]-> {hiliteIDs } }
3383 sub hiliteREs {($#_)?$_[0]-> set_hilite_res ($_[1]) :return $_[0]-> {hiliteREs } }
3384 sub insertMode {($#_)?($_[0]-> set_insert_mode ( $_[1])) :return $_[0]-> {insertMode} }
3385 sub mark {($#_)?(shift-> set_marking ( @_ )) :return exists $_[0]-> {anchor} }
3386 sub markers {($#_)?($_[0]-> {markers} = [@{$_[1]}]) :return $_[0]-> {markers } }
3387 sub modified {($#_)?($_[0]-> {modified } = $_[1]) :return $_[0]-> {modified } }
3388 sub offset {($#_)?($_[0]-> set_offset ( $_[1])) :return $_[0]-> {offset } }
3389 sub persistentBlock {($#_)?($_[0]-> {persistentBlock}=$_[1]) :return $_[0]-> {persistentBlock}}
3390 sub readOnly {($#_)?($_[0]-> {readOnly } = $_[1]) :return $_[0]-> {readOnly } }
3391 sub selection {($#_)? shift-> set_selection (@_) : return (@{$_[0]-> {selStart}},@{$_[0]-> {selEnd}})}
3392 sub selStart {($#_)? $_[0]-> set_selection ($_[1],$_[2],@{$_[0]-> {selEnd}}): return @{$_[0]-> {'selStart'}}}
3393 sub selEnd {($#_)? $_[0]-> set_selection (@{$_[0]-> {'selStart'}},$_[1],$_[2]):return @{$_[0]-> {'selEnd'}}}
3394 sub syntaxHilite {($#_)? $_[0]-> set_syntax_hilite($_[1]) :return $_[0]-> {syntaxHilite}}
3395 sub tabIndent {($#_)?$_[0]-> set_tab_indent( $_[1]) :return $_[0]-> {tabIndent} }
3396 sub textRef {($#_)?$_[0]-> set_text_ref ( $_[1]) :return $_[0]-> get_text_ref; }
3397 sub wantTabs {($#_)?($_[0]-> {wantTabs} = $_[1]) :return $_[0]-> {wantTabs} }
3398 sub wantReturns {($#_)?($_[0]-> {wantReturns} = $_[1]) :return $_[0]-> {wantReturns} }
3399 sub wordDelimiters {($#_)?($_[0]-> {wordDelimiters}= $_[1]) :return $_[0]-> {wordDelimiters}}
3400 sub wordWrap {($#_)?($_[0]-> set_word_wrap( $_[1])) :return $_[0]-> {wordWrap } }
3401  
3402 1;
3403  
3404 __END__