summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl/lib/Tk/TextEdit.pm
blob: e1ff20b6149879de4e944a13a3461503fae7e4a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# Copyright (c) 1999 Greg Bartels. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.

# Special thanks to Nick Ing-Simmons for pushing a lot of
# my text edit functionality into Text.pm and TextUndo.pm
# otherwise, this module would have been monstrous.

# Andy Worhal had it wrong, its "fifteen megabytes of fame"
#	-Greg Bartels

package Tk::TextEdit;


use vars qw($VERSION);
$VERSION = '4.004'; # $Id: //depot/Tkutf8/Tk/TextEdit.pm#4 $

use Tk qw (Ev);
use AutoLoader;

use Text::Tabs;

use base qw(Tk::TextUndo);

Construct Tk::Widget 'TextEdit';

#######################################################################
#######################################################################
sub ClassInit
{
 my ($class,$mw) = @_;
 $class->SUPER::ClassInit($mw);

 $mw->bind($class,'<F5>', 'IndentSelectedLines');
 $mw->bind($class,'<F6>', 'UnindentSelectedLines');

 $mw->bind($class,'<F7>', 'CommentSelectedLines');
 $mw->bind($class,'<F8>', 'UncommentSelectedLines');

 return $class;
}

# 8 horizontal pixels in the "space" character in default font.
my $tab_multiplier = 8;

sub debug_code_f1
{
 my $w=shift;
}

sub debug_code_f2
{
 my $w=shift;
}

#######################################################################
#######################################################################
sub InitObject
{
 my ($w) = @_;
 $w->SUPER::InitObject;

 $w->{'INDENT_STRING'} = "\t";   #  Greg mode=>"\t",   Nick mode=>" "
 $w->{'LINE_COMMENT_STRING'} = "#";   #  assuming perl comments

 my %pair_descriptor_hash =
	(
	'PARENS' => [ 'multiline', '(', ')', "[()]" ],
	'CURLIES' => [ 'multiline', '{', '}', "[{}]" ],
	'BRACES' => [ 'multiline', '[', ']', "[][]" ],
	'DOUBLEQUOTE' => [ 'singleline', "\"","\"" ],
	'SINGLEQUOTE' => [ 'singleline', "'","'" ],
	);

 $w->{'HIGHLIGHT_PAIR_DESCRIPTOR_HASH_REF'}=\%pair_descriptor_hash;

 $w->tagConfigure
  ('CURSOR_HIGHLIGHT_PARENS', -foreground=>'white', -background=>'violet');
 $w->tagConfigure
  ('CURSOR_HIGHLIGHT_CURLIES', -foreground=>'white', -background=>'blue');
 $w->tagConfigure
  ('CURSOR_HIGHLIGHT_BRACES', -foreground=>'white', -background=>'purple');
 $w->tagConfigure
  ('CURSOR_HIGHLIGHT_DOUBLEQUOTE', -foreground=>'black', -background=>'green');
 $w->tagConfigure
  ('CURSOR_HIGHLIGHT_SINGLEQUOTE', -foreground=>'black', -background=>'grey');

 $w->tagConfigure('BLOCK_HIGHLIGHT_PARENS', -background=>'red');
 $w->tagConfigure('BLOCK_HIGHLIGHT_CURLIES', -background=>'orange');
 $w->tagConfigure('BLOCK_HIGHLIGHT_BRACES', -background=>'red');
 $w->tagConfigure('BLOCK_HIGHLIGHT_DOUBLEQUOTE', -background=>'red');
 $w->tagConfigure('BLOCK_HIGHLIGHT_SINGLEQUOTE', -background=>'red');

 $w->tagRaise('BLOCK_HIGHLIGHT_PARENS','CURSOR_HIGHLIGHT_PARENS');
 $w->tagRaise('BLOCK_HIGHLIGHT_CURLIES','CURSOR_HIGHLIGHT_CURLIES');
 $w->tagRaise('BLOCK_HIGHLIGHT_BRACES','CURSOR_HIGHLIGHT_BRACES');
 $w->tagRaise('BLOCK_HIGHLIGHT_DOUBLEQUOTE','CURSOR_HIGHLIGHT_DOUBLEQUOTE');
 $w->tagRaise('BLOCK_HIGHLIGHT_SINGLEQUOTE','CURSOR_HIGHLIGHT_SINGLEQUOTE');

 $w->{'UPDATE_WIDGET_PERIOD'}=300;  # how much time between each call.
 $w->{'WINDOW_PLUS_AND_MINUS_VALUE'}=80;
 $w->SetGUICallbackIndex(0);
 $w->schedule_next_callback;

}

#######################################################################

sub cancel_current_gui_callback_and_restart_from_beginning
{
 my ($w)=@_;
 if(defined($w->{'UPDATE_WIDGET_AFTER_REFERENCE'}))
  {$w->{'UPDATE_WIDGET_AFTER_REFERENCE'}->cancel();}
 $w->SetGUICallbackIndex(0);

 $w->schedule_next_callback;
}

sub schedule_next_callback
{
 my ($w)=@_;
 return if $w->NoMoreGUICallbacksToCall; #stops infinite recursive call.
 $w->{'UPDATE_WIDGET_AFTER_REFERENCE'} = $w->after
   ($w->{'UPDATE_WIDGET_PERIOD'},
    sub
    {
    $w->CallNextGUICallback;
    $w->schedule_next_callback;
    }
   );

}


#######################################################################
# use these methods to pass the TextEdit widget an anonymous array
# of code references.
# any time the widget changes that requires the display to be updated,
# then these code references will be scheduled in sequence for calling.
# splitting them up allows them to be prioritized by order,
# and prevents the widget from "freezing" too long if they were
# one large callback. scheduling them apart allows the widget time
# to respond to user inputs.
#######################################################################
sub SetGUICallbacks
{
 my ($w,$callback_array_ref) = @_;
 $w->{GUI_CALLBACK_ARRAY_REF}=$callback_array_ref;
 $w->SetGUICallbackIndex(0);
}

sub GetGUICallbacks
{
 return shift->{GUI_CALLBACK_ARRAY_REF};
}

sub SetGUICallbackIndex
{
 my ($w, $val)=@_;
 $w->{GUI_CALLBACK_ARRAY_INDEX}=$val;
}

sub GetGUICallbackIndex
{
 return shift->{GUI_CALLBACK_ARRAY_INDEX};
}

sub IncrementGUICallbackIndex
{
 shift->{GUI_CALLBACK_ARRAY_INDEX} += 1;
}

sub NoMoreGUICallbacksToCall
{
 my ($w) = @_;
 return 0 unless defined ($w->{GUI_CALLBACK_ARRAY_REF});
 return 0 unless defined ($w->{GUI_CALLBACK_ARRAY_INDEX});
 my $arr_ref = $w->{GUI_CALLBACK_ARRAY_REF};
 my $arr_ind = $w->{GUI_CALLBACK_ARRAY_INDEX};
 return $arr_ind >= @$arr_ref;
}

sub CallNextGUICallback
{
 my ($w) = @_;
 return if $w->NoMoreGUICallbacksToCall;
 my $arr_ref = $w->{GUI_CALLBACK_ARRAY_REF};
 my $arr_ind = $w->{GUI_CALLBACK_ARRAY_INDEX};
  &{$arr_ref->[$arr_ind]};
 $w->IncrementGUICallbackIndex;
}


#######################################################################
#######################################################################

sub insert
{
 my $w = shift;
 $w->SUPER::insert(@_);
 $w->cancel_current_gui_callback_and_restart_from_beginning;
}

sub delete
{
 my $w = shift;
 $w->SUPER::delete(@_);
 $w->cancel_current_gui_callback_and_restart_from_beginning;
}

sub SetCursor
{
 my $w = shift;
 $w->SUPER::SetCursor(@_);
 $w->cancel_current_gui_callback_and_restart_from_beginning;
}

sub OverstrikeMode
{
 my ($w,$mode) = @_;
 if (defined($mode))
  {
  $w->SUPER::OverstrikeMode($mode);
  $w->cancel_current_gui_callback_and_restart_from_beginning;
  }
 return $w->SUPER::OverstrikeMode;
}


#######################################################################
# use yview on scrollbar to get fractional coordinates.
# scale this by the total length of the text to find the
# approximate start line of widget and end line of widget.
#######################################################################
sub GetScreenWindowCoordinates
{
 my $w = shift;
 my ($top_frac, $bot_frac) = $w->yview;
 my $end_index = $w->index('end');
 my ($lines,$columns) = split (/\./,$end_index);
 my $window = $w->{'WINDOW_PLUS_AND_MINUS_VALUE'};
 my $top_line = int(($top_frac * $lines) - $window);
 $top_line = 0 if ($top_line < 0);
 my $bot_line = int(($bot_frac * $lines) + $window);
 $bot_line = $lines if ($bot_line > $lines);
 my $top_index = $top_line . '.0';
 my $bot_index = $bot_line . '.0';

 $_[0] = $top_index;
 $_[1] = $bot_index;
}

########################################################################
# take two indices as inputs.
# if they are on the same line or same column (accounting for tabs)
# then return 1
# else return 0
# (assume indices passed in are in line.column format)
########################################################################
sub IndicesLookGood
{
 my ($w, $start, $end, $singleline) = @_;

 return 0 unless ( (defined($start)) and (defined($end)));

 my ($start_line, $start_column) = split (/\./,$start);
 my ($end_line,   $end_column)   = split (/\./,$end);

 ##########################
 # good if on the same line
 ##########################
 return 1 if ($start_line == $end_line);

 ##########################
 # if not on same line and its a singleline, its bad
 ##########################
 return 0 if $singleline;


 # get both lines, convert the tabs to spaces, and get the new column.
 # see if they line up or not.
 my $string;
 $string = $w->get($start_line.'.0', $start_line.'.0 lineend');
 $string = substr($string, 0, $start_column+1);
 $string = expand($string);
 $start_column = length($string);

 $string = $w->get($end_line.'.0', $end_line.'.0 lineend');
 $string = substr($string, 0, $end_column +1);
 $string = expand($string);
 $end_column = length($string);

 ##########################
 # good if on the same column (adjusting for tabs)
 ##########################
 return 1 if ($start_column == $end_column);

 # otherwise its bad
 return 0;
}

########################################################################
# if searching backward, count paranthesis until find a start parenthesis
# which does not have a forward match.
#
# (<= search backward will return this index
#    ()
#      START X HERE
#   ( (  )  () )
# )<== search forward will return this index
#
# if searching forward, count paranthesis until find a end parenthesis
# which does not have a rearward match.
########################################################################
sub searchForBaseCharacterInPair
{
 my
  (
   $w, $top_index, $searchfromindex, $bot_index,
   $direction, $startchar, $endchar, $charpair
  )=@_;
 my ($plus_one_char, $search_end_index, $index_offset, $done_index);
 if ($direction eq '-forward')
  {
  $plus_one_char = $endchar;
  $search_end_index = $bot_index;
  $index_offset = ' +1c';
  $done_index = $w->index('end');
  }
 else
  {
  $plus_one_char = $startchar;
  $search_end_index = $top_index;
  $index_offset = '';
  $done_index = '1.0';
  }

 my $at_done_index = 0;
 my $count = 0;
 my $char;
 while(1)
  {
  $searchfromindex = $w->search
   ($direction, '-regexp', $charpair, $searchfromindex, $search_end_index );

  last unless(defined($searchfromindex));
  $char = $w->get($searchfromindex, $w->index($searchfromindex.' +1c'));
  if ($char eq $plus_one_char)
   {$count += 1;}
  else
   {$count -= 1;}
  last if ($count==1);
  # boundary condition exists when first char in widget is the match char
  # need to be able to determine if search tried to go past index '1.0'
  # if so, set index to undef and return.
  if ( $at_done_index )
   {
   $searchfromindex = undef;
   last;
   }
  $at_done_index = 1 if ($searchfromindex eq $done_index);
  $searchfromindex=$w->index($searchfromindex . $index_offset);
  }
 return $searchfromindex;
}

########################################################################
# highlight a character pair that most closely brackets the cursor.
# allows you to pick and choose which ones you want to do.
########################################################################

sub HighlightParenthesisAroundCursor
{
 my ($w)=@_;
 $w->HighlightSinglePairBracketingCursor
  ( '(', ')', '[()]', 'CURSOR_HIGHLIGHT_PARENS','BLOCK_HIGHLIGHT_PARENS',0);
}

sub HighlightCurlyBracesAroundCursor
{
 my ($w)=@_;
 $w->HighlightSinglePairBracketingCursor
  ( '{', '}', '[{}]', 'CURSOR_HIGHLIGHT_CURLIES','BLOCK_HIGHLIGHT_CURLIES',0);
}

sub HighlightBracesAroundCursor
{
 my ($w)=@_;
 $w->HighlightSinglePairBracketingCursor
  ( '[', ']','[][]', 'CURSOR_HIGHLIGHT_BRACES','BLOCK_HIGHLIGHT_BRACES',0);
}

sub HighlightDoubleQuotesAroundCursor
{
 my ($w)=@_;
 $w->HighlightSinglePairBracketingCursor
  ( "\"", "\"", "\"", 'CURSOR_HIGHLIGHT_DOUBLEQUOTE','BLOCK_HIGHLIGHT_DOUBLEQUOTE',1);
}

sub HighlightSingleQuotesAroundCursor
{
 my ($w)=@_;
 $w->HighlightSinglePairBracketingCursor
  ( "'", "'", "'", 'CURSOR_HIGHLIGHT_SINGLEQUOTE','BLOCK_HIGHLIGHT_SINGLEQUOTE',1);
}

########################################################################
# highlight all the character pairs that most closely bracket the cursor.
########################################################################
sub HighlightAllPairsBracketingCursor
{
 my ($w)=@_;
 $w->HighlightParenthesisAroundCursor;
 $w->HighlightCurlyBracesAroundCursor;
 $w->HighlightBracesAroundCursor;
 $w->HighlightDoubleQuotesAroundCursor;
 $w->HighlightSingleQuotesAroundCursor;
}

########################################################################
# search for a pair of matching characters that bracket the
# cursor and tag them with the given tagname.
# startchar might be '['
# endchar would then be ']'
# tagname is a name of a tag, which has already been
# configured to highlight however the user wants them to behave.
# error tagname is the tag to highlight the chars with if there
# is a problem of some kind.
# singleline indicates whether the character pairs must occur
# on a single line. quotation marks are single line characters usually.
########################################################################
sub HighlightSinglePairBracketingCursor
{
 my
  (
   $w, $startchar, $endchar, $charpair,
   $good_tagname, $bad_tagname, $single_line
  ) = @_;
 $single_line=0 unless defined($single_line);
 $w->tagRemove($good_tagname, '1.0','end');
 $w->tagRemove($bad_tagname, '1.0','end');
 my $top_index; my $bot_index;
 my $cursor = $w->index('insert');
 if ($single_line)
  {
  $top_index = $w->index($cursor.' linestart');
  $bot_index = $w->index($cursor.' lineend');
  }
 else
  {
  $w->GetScreenWindowCoordinates($top_index, $bot_index);
  }

 # search backward for the startchar
 #  $top_index, $searchfromindex, $bot_index,
 #  $direction, $startchar, $endchar, $charpair

 my $startindex = $w->searchForBaseCharacterInPair
  (
   $top_index, $cursor, $bot_index,
   '-backward', $startchar, $endchar, $charpair
  );

 # search forward for the endchar
 my $endindex = $w->searchForBaseCharacterInPair
  (
   $top_index, $cursor, $bot_index,
   '-forward', $startchar, $endchar, $charpair
  );
 return unless ((defined $startindex) and (defined $endindex));

 my $final_tag = $bad_tagname;
 if ($w->IndicesLookGood( $startindex, $endindex, $single_line))
  {
  $final_tag = $good_tagname;
  }

 $w->tagAdd($final_tag, $startindex, $w->index($startindex.'+1c') );
 $w->tagAdd($final_tag,   $endindex, $w->index(  $endindex.'+1c') );
}

####################################################################
sub IndentSelectedLines
{
 my($w)=@_;
 $w->insertStringAtStartOfSelectedLines($w->{'INDENT_STRING'});
}

sub UnindentSelectedLines
{
 my($w)=@_;
 $w->deleteStringAtStartOfSelectedLines($w->{'INDENT_STRING'});
}

sub CommentSelectedLines
{
 my($w)=@_;
 $w->insertStringAtStartOfSelectedLines($w->{'LINE_COMMENT_STRING'});
}

sub UncommentSelectedLines
{
 my($w)=@_;
 $w->deleteStringAtStartOfSelectedLines($w->{'LINE_COMMENT_STRING'});
}


1;
__END__