| 1 | package Tk::TextHighlight; | ||
| 2 | |||
| 3 | 1 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | $VERSION = '2.2'; | ||
| 5 | 0 | ▶ | use Tk qw(Ev); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use strict; | ||
| 7 | use Storable; | ||
| 8 | use File::Basename; | ||
| 9 | |||
| 10 | my $blockHighlight = 0; #USED TO PREVENT RECURSIVE CALLS TO RE-HIGHLIGHT! | ||
| 11 | my $nodoEvent = 0; #USED TO PREVENT REPEATING (RUN-AWAY) SCROLLING! | ||
| 12 | our ($readonly, $TEXTWIDGET, $haveSuperText); | ||
| 13 | |||
| 14 | #NOTE: TextHighlight SUPPORTS USING EITHER SuperText, OR TextUndo (ROText IF USING ROTextOnly SYMLINK). | ||
| 15 | # SuperText USES Text (NOT TextUndo). THE OPTIONAL ROSuperText SYMLINK (uses ROText) | ||
| 16 | # IS *NOT* (AND CAN NOT BE) USED HERE! | ||
| 17 | BEGIN { | ||
| 18 | $haveSuperText = 0; | ||
| 19 | $readonly = (__FILE__ =~ /RO/o) ? 1 : 0; | ||
| 20 | $TEXTWIDGET = 'Tk::Text::SuperText'; #FIRST TRY SuperText: | ||
| 21 | my $evalstr = "use $TEXTWIDGET; \$haveSuperText = 1; 1"; | ||
| 22 | eval $evalstr; | ||
| 23 | unless ($haveSuperText) { #IF SuperText NOT INSTALLED, THEN TRY TextUndo (or ROText): | ||
| 24 | $TEXTWIDGET = $readonly ? 'Tk::ROText' : 'Tk::TextUndo'; | ||
| 25 | $evalstr = "use $TEXTWIDGET; \$haveSuperText = 1; 1"; | ||
| 26 | eval $evalstr; | ||
| 27 | } | ||
| 28 | die "e:TextHighlight could not load required widget ($TEXTWIDGET) ($@)!\n" if ($@); | ||
| 29 | $evalstr = "use base ('Tk::Derived', '$TEXTWIDGET')"; | ||
| 30 | eval $evalstr; | ||
| 31 | die "e:TextHighlight could not load base widget ($TEXTWIDGET) ($@)!\n" if ($@); | ||
| 32 | }; | ||
| 33 | |||
| 34 | Construct Tk::Widget 'TextHighlight'; | ||
| 35 | |||
| 36 | my %syntaxcomments = ( #ALTERNATE COMMENT CHARACTERS FOR SELECTED LANGUAGES (CAN ADD MORE HERE): | ||
| 37 | 'Kate::C' => '/*', | ||
| 38 | 'Kate::C++' => '/*', | ||
| 39 | 'Kate::CPP' => '/*', | ||
| 40 | 'HTML' => '<!--', | ||
| 41 | 'Kate::HTML' => '<!--', | ||
| 42 | 'Kate::Modula-2' => '(*', | ||
| 43 | 'Kate::Pascal' => '(*', | ||
| 44 | 'Kate::XML' => '<!--', | ||
| 45 | ); | ||
| 46 | |||
| 47 | sub Populate { | ||
| 48 | my ($cw,$args) = @_; | ||
| 49 | $args->{'-noPopupMenu'} = 0 unless (defined $args->{'-noPopupMenu'}); | ||
| 50 | $cw->{'-noPopupMenu'} = $args->{'-noPopupMenu'}; | ||
| 51 | #REMOVE ARGS THAT SUBWIDGETS CAN'T HANDLE: | ||
| 52 | my $superargsOK; #THIS INITIALIZATION REQUIRES 2 LINES (my $superargsOK = $args) IS REFERENCE & WILL OVERWRITE LATTER!: | ||
| 53 | %{$superargsOK} = %{$args}; | ||
| 54 | foreach my $badarg (qw(-autoindent -smartindent -match -matchoptions -indentchar | ||
| 55 | -disablemenu -commentchar -colorinf -colored -syntax -rules -updatecall | ||
| 56 | -noRulesMenu -noSyntaxMenu -noRulesEditMenu -noSaveRulesMenu -noPlugInit | ||
| 57 | -noPopupMenu -highlightInBackground -syntaxcomments)) { | ||
| 58 | delete ($superargsOK->{$badarg}) if (defined($superargsOK->{$badarg})); | ||
| 59 | } | ||
| 60 | if ($TEXTWIDGET =~ /SuperText/) { | ||
| 61 | #ADD ONE THAT IS DIFFERENT (OVERRIDDEN BY OURS): | ||
| 62 | $superargsOK->{'-matchingcouples'} = $args->{'-match'} if (defined $args->{'-match'}); | ||
| 63 | } else { | ||
| 64 | foreach my $badarg (qw(-matchingcouples -showmatching -ansicolor)) { | ||
| 65 | delete ($superargsOK->{$badarg}) if (defined($superargsOK->{$badarg})); | ||
| 66 | delete ($args->{$badarg}) if (defined($args->{$badarg})); #-ansicolor MUST ALSO BE REMOVED HERE - DON'T KNOW WHY?! | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | $cw->SUPER::Populate($superargsOK); | ||
| 71 | |||
| 72 | my %configSpecs = ( | ||
| 73 | -autoindent => [qw/PASSIVE autoindent Autoindent/, 0], | ||
| 74 | -smartindent => [qw/PASSIVE undef undef/, 1], | ||
| 75 | -match => [qw/PASSIVE match Match/, '[]{}()'], | ||
| 76 | -matchoptions => [qw/METHOD matchoptions Matchoptions/, | ||
| 77 | [-foreground => 'yellow', -background => 'red']], #SuperText'S IS white ON blue, BUT CLASHES W/TOO MANY RULES. | ||
| 78 | -indentchar => [qw/PASSIVE indentchar Indentchar/, "\t"], | ||
| 79 | -disablemenu => [qw/PASSIVE disablemenu Disablemenu/, 0], | ||
| 80 | -commentchar => [qw/PASSIVE commentchar Commentchar/, "#"], | ||
| 81 | -syntaxcomments => [qw/PASSIVE undef undef/, 0], #JWT: ADDED v2 FEATURE. | ||
| 82 | -colorinf => [qw/PASSIVE undef undef/, []], | ||
| 83 | -colored => [qw/PASSIVE undef undef/, 0], | ||
| 84 | -syntax => [qw/PASSIVE syntax Syntax/, 'None'], | ||
| 85 | -rules => [qw/PASSIVE undef undef/, undef], | ||
| 86 | -rulesdir => [qw/PASSIVE rulesdir Rulesdir/, ''], | ||
| 87 | -updatecall => [qw/PASSIVE undef undef/, sub {}], | ||
| 88 | -noRulesMenu => [qw/PASSIVE undef undef/, 0], #JWT: ADDED FEATURE. | ||
| 89 | -noSyntaxMenu => [qw/PASSIVE undef undef/, 0], #JWT: ADDED FEATURE. | ||
| 90 | -noRulesEditMenu => [qw/PASSIVE undef undef/, 0], #JWT: ADDED FEATURE. | ||
| 91 | -noSaveRulesMenu => [qw/PASSIVE undef undef/, 0], #JWT: ADDED FOR BACKWARD COMPATABILITY. | ||
| 92 | -noPlugInit => [qw/PASSIVE undef undef/, 0], #JWT: ADDED FOR BACKWARD COMPATABILITY. | ||
| 93 | -noPopupMenu => [qw/PASSIVE noPopupMenu NoPopupMenu/, 0], #JWT: ADDED v2 FEATURE. | ||
| 94 | -highlightInBackground => [qw/PASSIVE undef undef/, 0], #JWT: SELF-EXPLANATORY. | ||
| 95 | -readonly => ['METHOD','readOnly','ReadOnly',$readonly], #JWT: ADDED v2 FEATURE. | ||
| 96 | #DO NOT SPECIFY A DEFAULT BG COLOR HERE, IT MESSES UP RULES / SCREEN-SIZE INITIALIZATION! | ||
| 97 | #WE LET background() & highlightPlugInit() METHODS TAKE CARE OF IT AT THE PROPER TIME!: | ||
| 98 | -background => ['METHOD','background','Background', undef], | ||
| 99 | DEFAULT => [ 'SELF' ], | ||
| 100 | ); | ||
| 101 | if ($TEXTWIDGET =~ /SuperText/) { | ||
| 102 | $configSpecs{'-showmatching'} = ['PASSIVE','showMatching','ShowMatching',0]; #WE HANDLE THIS! | ||
| 103 | #DON'T LET 'EM DISPLAY MATCHES BY DEFAULT WE DON'T (DISTRACTING, BUT USERS CAN OVERRIDE), | ||
| 104 | #AS THEIR DEFAULT INCLUDES MORE NON-MIRRORED CHARS, LIKE QUOTES & GRAVS!: | ||
| 105 | $configSpecs{'-matchingcouples'} = ['METHOD','matchingCouples','MatchingCouples',"[]{}()"]; | ||
| 106 | } | ||
| 107 | $cw->ConfigSpecs(%configSpecs); | ||
| 108 | |||
| 109 | $cw->bind('<Configure>', sub { $cw->highlightVisual }); | ||
| 110 | $cw->bind('<Return>', sub { $cw->doAutoIndent(1) }); #HANDLE THESE WITH OUR FUNCTIONS!: | ||
| 111 | $cw->bind('<Shift-Return>', sub { $cw->doAutoIndent(0) }); | ||
| 112 | $cw->bind('<Control-p>', sub { $cw->jumpToMatchingChar(0) }); | ||
| 113 | $cw->bind('<Control-P>', sub { $cw->jumpToMatchingChar(1) }); | ||
| 114 | $cw->markSet('match', '0.0'); | ||
| 115 | $cw->bind('<ButtonRelease-2>', sub { $cw->pastePrimaryAtMouse() }); | ||
| 116 | if ($cw->{'-noPopupMenu'}) { | ||
| 117 | $cw->bind('<ButtonPress-3>', ['extendSelect']); | ||
| 118 | } else { | ||
| 119 | $cw->bind('<ButtonPress-3>', ['PostPopupMenu', Ev('X'), Ev('Y')]); | ||
| 120 | $cw->bind('<Control-Key-m>', sub { $cw->PostPopupMenu($cw->pointerx, $cw->pointery) }); | ||
| 121 | } | ||
| 122 | $cw->bind('<Shift-ButtonPress-3>', ['extendSelect']); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub readonly | ||
| 126 | { | ||
| 127 | my ($w,$val) = @_; | ||
| 128 | |||
| 129 | #NOTE: $readonly IS HARD-CODED BASED ON STARTUP MODULE, $w->{READONLY} IS SET | ||
| 130 | #HERE FOR THE WIDGET, BUT IS *NOT* ALWAYS SET INITIALLY: | ||
| 131 | |||
| 132 | $w->{READONLY} = $readonly unless (defined $w->{READONLY}); | ||
| 133 | if ($readonly) { | ||
| 134 | return $readonly unless (defined $val); | ||
| 135 | $w->{READONLY} = $readonly; | ||
| 136 | $w->adjustMenuState(); | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | return $w->{READONLY} unless (defined $val); | ||
| 140 | |||
| 141 | my $prevval = $w->{READONLY}; | ||
| 142 | $w->{READONLY} = $val; | ||
| 143 | $w->adjustMenuState() if ($val != $prevval); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub background { | ||
| 147 | my ($w,$val) = @_; | ||
| 148 | |||
| 149 | return $w->{'background'} unless (defined $val); | ||
| 150 | |||
| 151 | #IF CHANGING BACKGROUND, MUST RESET RULE COLORS TO FIX/PREVENT COLOR | ||
| 152 | #CONTRAST ILLEGABILITIES (RULES W/FG COLOR SAME AS WINDOW BACKGROUND) | ||
| 153 | # **EXCEPT** WHEN BACKGROUND IS INITIALLY SET (BEFORE RULES ARE INITIALIZED)!!!: | ||
| 154 | Tk::configure($w, "-background", $val); | ||
| 155 | if (defined $w->{'background'}) { #AVOID RESETTING RULES ON INITIAL STARTUP (WINDOW IGNORES WIDTH/HEIGHT SETTINGS)! | ||
| 156 | $w->configure('-rules' => undef); | ||
| 157 | $w->highlightPlug; | ||
| 158 | } | ||
| 159 | $w->{'background'} = $val; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub jumpToMatchingChar #ADDED 20060630 JWT TO CAUSE ^p TO WORK LIKE VI & SUPERTEXT - JUMP TO MATCHING CHARACTER! | ||
| 163 | { | ||
| 164 | my $cw = shift; | ||
| 165 | my $selectbetween = shift || 0; | ||
| 166 | |||
| 167 | $cw->markSet('insert', $cw->index('insert')); | ||
| 168 | my $pm = -1; | ||
| 169 | eval { $pm = $cw->index('MyMatch'); }; | ||
| 170 | if ($pm >= 0) | ||
| 171 | { | ||
| 172 | my $prevMatch = $cw->index('insert'); | ||
| 173 | $prevMatch .= '.0' unless ($prevMatch =~ /\./o); | ||
| 174 | $cw->markSet('insert', $cw->index('MyMatch')); | ||
| 175 | $cw->see('insert'); | ||
| 176 | $cw->markSet('MyMatch', $prevMatch); | ||
| 177 | if ($selectbetween) { | ||
| 178 | my @ranges = $cw->tagRanges('sel'); | ||
| 179 | my $idx = $cw->index('insert'); | ||
| 180 | if (@ranges && ($cw->compare($idx,'==',$cw->index('sel.first')) | ||
| 181 | || $cw->compare($idx,'==',$cw->index('sel.last - 1c')))) { | ||
| 182 | #SELECT EXCLUDING BRACES (WAS ALREADY SELECTED & INCLUDING BRACES): | ||
| 183 | $cw->tagRemove('sel','1.0','end'); | ||
| 184 | $cw->compare($prevMatch,'<',$cw->index('insert')) | ||
| 185 | ? $cw->tagAdd('sel','MyMatch + 1c','insert') | ||
| 186 | : $cw->tagAdd('sel','insert + 1c','MyMatch'); | ||
| 187 | } else { | ||
| 188 | #SELECT INCLUDING BRACES: | ||
| 189 | $cw->tagRemove('sel','1.0','end'); | ||
| 190 | $cw->compare($prevMatch,'<',$cw->index('insert')) | ||
| 191 | ? $cw->tagAdd('sel','MyMatch','insert + 1c') | ||
| 192 | : $cw->tagAdd('sel','insert','MyMatch + 1c'); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | sub ClassInit | ||
| 199 | { | ||
| 200 | my ($class,$w) = @_; | ||
| 201 | |||
| 202 | $class->SUPER::ClassInit($w); | ||
| 203 | |||
| 204 | # reset default Tk::Text binds: | ||
| 205 | $w->bind($class, '<Control-p>', sub {} ); #OVERRIDE BINDINGS WE HANDLE EXCLUSIVELY!: | ||
| 206 | $w->bind($class, '<Control-P>', sub {} ); #ADDED SELECT BETWEEN BRACES (INCLUSIVE). | ||
| 207 | $w->bind($class, '<Key-Return>', sub {} ) unless ($TEXTWIDGET =~ /SuperText/); | ||
| 208 | $w->bind($class, '<Key-BackSpace>', 'Backspace' ); #MAKE CONSISTANT BETWEEN ALL SUPPORTED TEXT WIDGETS. | ||
| 209 | $w->bind($class, '<Key-space>', 'Space' ); #MAKE CONSISTANT BETWEEN ALL SUPPORTED TEXT WIDGETS. | ||
| 210 | $w->bind($class, '<Alt-Tab>', 'insertTabChar' ); #ADDED TO ALLOW INSERTION OF TABS! | ||
| 211 | $w->bind($class, '<Tab>', 'insertTab' ); #ADDED TO ALLOW INSERTION OF TABS OR SPACES! | ||
| 212 | $w->bind($class, '<ButtonRelease-2>', ''); #CHECK READONLY STATUS BEFORE PASTING SELECTION! | ||
| 213 | return $class; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub clipboardCopy { | ||
| 217 | my $cw = shift; | ||
| 218 | my @ranges = $cw->tagRanges('sel'); | ||
| 219 | if (@ranges) { | ||
| 220 | $cw->SUPER::clipboardCopy(@_); | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | sub beginUndoBlock | ||
| 225 | { | ||
| 226 | my $cw = shift; | ||
| 227 | |||
| 228 | if ($TEXTWIDGET =~ /SuperText/) #GROUP CHANGES FOR UNDO (DIFFERENT FN NAMES USED). | ||
| 229 | { | ||
| 230 | $cw->_BeginUndoBlock; | ||
| 231 | } | ||
| 232 | elsif ($TEXTWIDGET =~ /TextUndo/) | ||
| 233 | { | ||
| 234 | $cw->addGlobStart; | ||
| 235 | } | ||
| 236 | #OTHERWISE NO-OP (UNDO/REDO NOT SUPPORTED). | ||
| 237 | } | ||
| 238 | |||
| 239 | sub endUndoBlock | ||
| 240 | { | ||
| 241 | my $cw = shift; | ||
| 242 | |||
| 243 | if ($TEXTWIDGET =~ /SuperText/) #ADDED 20080411 TO GROUP CHANGES FOR UNDO. | ||
| 244 | { | ||
| 245 | $cw->_EndUndoBlock; | ||
| 246 | } | ||
| 247 | elsif ($TEXTWIDGET =~ /TextUndo/) | ||
| 248 | { | ||
| 249 | eval $cw->addGlobEnd; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | sub doAutoIndent { #WE HANDLE AUTOINDENT NOW FOR EVERYONE!: | ||
| 253 | my $cw = shift; | ||
| 254 | |||
| 255 | my $doAutoIndent = $cw->cget('-autoindent') ? shift : 0; | ||
| 256 | if ($cw->{READONLY}) { #JUST POSITION CURSOR ON NEXT LINE (INDENTED IF NEEDED): | ||
| 257 | my $marginlen = 1; | ||
| 258 | if ($doAutoIndent) { | ||
| 259 | my $margin = $cw->get('insert lineend + 1 char', 'insert lineend + 1 char lineend'); | ||
| 260 | $marginlen = length($1) + 1 if ($margin =~ /^(\s+)/o); | ||
| 261 | } | ||
| 262 | $cw->SetCursor($cw->index("insert lineend + $marginlen char")); | ||
| 263 | $cw->see('insert linestart'); | ||
| 264 | Tk->break; | ||
| 265 | return; | ||
| 266 | } | ||
| 267 | |||
| 268 | my $i = $cw->index('insert linestart'); | ||
| 269 | my $begin = $cw->linenumber($i); | ||
| 270 | my $insertStuff = "\n"; | ||
| 271 | my $s = $cw->get("$i", "$i lineend"); | ||
| 272 | $cw->beginUndoBlock; | ||
| 273 | #if ($s =~ /\S/o) #JWT: UNCOMMENT TO CAUSE SUBSEQUENT BLANK LINES TO NOT BE AUTOINDENTED. | ||
| 274 | #{ | ||
| 275 | #$s =~ /^(\s+)/; #JWT:CHGD. TO NEXT 20060701 TO FIX "e" BEING INSERTED INTO LINE WHEN AUTOINDENT ON?! | ||
| 276 | $s =~ /^(\s*)/o; | ||
| 277 | if ($doAutoIndent) { | ||
| 278 | my $thisindent = defined($1) ? $1 : ''; | ||
| 279 | if ($cw->cget('-smartindent')) { #TRY TO DO INTELLIGENT (CODE-FRIENDLY) INDENTING BASE ON CURRENT & NEXT LINE: | ||
| 280 | my $s2 = ''; | ||
| 281 | my $thisindlen = length($thisindent); | ||
| 282 | my $cc = $cw->cget('-commentchar') || "\x02"; #MUST BE A NON-EMPTY STRING IN NEXT REGEX!: | ||
| 283 | eval "\$s2 = \$cw->get('insert + 1 line linestart', 'insert + 1 line lineend')"; | ||
| 284 | $s2 =~ /^(\s*)/o; | ||
| 285 | my $nextindent = defined($1) ? $1 : ''; | ||
| 286 | my $nextindlen = length($nextindent); | ||
| 287 | my $indentchar = $cw->cget('-indentchar'); #NORMALLY A TAB OR MULTIPLE SPACES (== 1 INDENTATION). | ||
| 288 | if ($s =~ /[\{\[\(]\s*(?:\Q$cc\E.*)?$/o) { #CURRENT LINE ENDS IN AN OPENING BRACE (INDENT AT LEAST 1 INDENTATION): | ||
| 289 | $insertStuff .= ($nextindlen > $thisindlen) ? $nextindent : "$thisindent$indentchar"; | ||
| 290 | } else { #NORMAL LINE (KEEP SAME INDENT UNLESS NEXT LINE FURTHER INDENTED): | ||
| 291 | my $afterStuff = $cw->get('insert', "$i lineend"); | ||
| 292 | $insertStuff .= ($nextindlen < $thisindlen || $s2 =~ /^\s*[\}\]\)]/o) ? $thisindent : $nextindent; | ||
| 293 | if (length $afterStuff) { #WE HIT <Enter> IN MIDDLE OF A LINE: | ||
| 294 | if ($afterStuff =~ /^\s*[\}\]\)]/o) { #WE HIT <Enter> ON A CLOSING BRACE: | ||
| 295 | $insertStuff =~ s/$indentchar//; | ||
| 296 | } elsif ($cw->get('insert - 1c') =~ /[\{\[\(]/o) { | ||
| 297 | $insertStuff .= $indentchar; | ||
| 298 | } | ||
| 299 | } | ||
| 300 | } | ||
| 301 | } else { #JUST DO IT LIKE SuperText: | ||
| 302 | $insertStuff .= $thisindent; | ||
| 303 | } | ||
| 304 | } | ||
| 305 | #} #JWT: UNCOMMENT TO CAUSE SUBSEQUENT BLANK LINES TO NOT BE AUTOINDENTED. | ||
| 306 | $cw->insert('insert', $insertStuff); | ||
| 307 | $cw->endUndoBlock; | ||
| 308 | $cw->see('insert linestart'); | ||
| 309 | $cw->highlightCheck($begin, $cw->linenumber('end')); | ||
| 310 | } | ||
| 311 | |||
| 312 | sub highlight { | ||
| 313 | my ($cw, $begin, $end) = @_; | ||
| 314 | # return $begin if ($blockHighlight); #PREVENT RECURSIVE CALLING WHILST ALREADY REHIGHLIGHTING! | ||
| 315 | $blockHighlight = 1; | ||
| 316 | $end = $begin + 1 unless (defined $end); | ||
| 317 | #save selection and cursor position | ||
| 318 | #1 my @sel = $cw->tagRanges('sel'); | ||
| 319 | #1 my $cursor = $cw->index('insert'); | ||
| 320 | #go over the source code line by line. | ||
| 321 | while ($begin < $end) { | ||
| 322 | $cw->highlightLine($begin); | ||
| 323 | $begin++; #move on to next line. | ||
| 324 | }; | ||
| 325 | #restore original cursor and selection | ||
| 326 | #1 $cw->markSet('insert', $cursor); | ||
| 327 | #1 if ($sel[0]) { | ||
| 328 | #1 $cw->tagRaise('sel'); #JWT:REMOVED 20060703 SO THAT HIGHLIGHTING STAYS ON SELECTED STUFF AFTER SELECTION MOVES OVER UNTAGGED TEXT. | ||
| 329 | #1 }; | ||
| 330 | $blockHighlight = 0; | ||
| 331 | return $begin; | ||
| 332 | } | ||
| 333 | |||
| 334 | sub highlightCheck { | ||
| 335 | my ($cw, $begin, $end) = @_; | ||
| 336 | my $col = $cw->cget('-colored'); | ||
| 337 | my $cli = $cw->cget('-colorinf'); | ||
| 338 | if ($begin <= $col) { | ||
| 339 | #The operation occurred in an area that was highlighted already | ||
| 340 | if ($begin < $end) { | ||
| 341 | #it was a multiline operation, so highlighting is not reliable anymore | ||
| 342 | #restart hightlighting from the beginning of the operation. | ||
| 343 | $cw->highlightPurge($begin); | ||
| 344 | } else { | ||
| 345 | #just re-highlight the modified line. | ||
| 346 | my $hlt = $cw->highlightPlug; | ||
| 347 | my $i = $cli->[$begin]; | ||
| 348 | $cw->highlight($begin); | ||
| 349 | #the proces ended inside a multiline token. try to fix it. | ||
| 350 | $cw->highlightPurge($begin) | ||
| 351 | if (($col < $cw->linenumber('end')) and (not $hlt->stateCompare($i))); | ||
| 352 | } | ||
| 353 | $cw->matchCheck; | ||
| 354 | } else { | ||
| 355 | $cw->highlightVisual; | ||
| 356 | } | ||
| 357 | } | ||
| 358 | |||
| 359 | sub highlightLine { | ||
| 360 | my ($cw, $num) = @_; | ||
| 361 | my $hlt = $cw->highlightPlug; | ||
| 362 | my $cli = $cw->cget('-colorinf'); | ||
| 363 | my $k = $cli->[$num - 1]; | ||
| 364 | $hlt->stateSet(@$k); | ||
| 365 | # remove all existing tags in this line | ||
| 366 | my $begin = "$num.0"; my $end = $cw->index("$num.0 lineend"); | ||
| 367 | my $rl = $hlt->rules; | ||
| 368 | foreach my $tn (@$rl) { | ||
| 369 | $cw->tagRemove($tn->[0], $begin, $end); | ||
| 370 | } | ||
| 371 | my $txt = $cw->get($begin, $end); #get the text to be highlighted | ||
| 372 | #print "--HL:stateSet(".join('|',@$k).") line=$txt=\n"; | ||
| 373 | my @v; | ||
| 374 | if ($txt) { #if the line is not empty | ||
| 375 | my $pos = 0; | ||
| 376 | my $start = 0; | ||
| 377 | my @h = $hlt->highlight("$txt\n"); #JWT: ADDED "\n" TO MAKE KATE WORK! | ||
| 378 | while (@h ne 0) { | ||
| 379 | $start = $pos; | ||
| 380 | $pos += shift @h; | ||
| 381 | my $tag = shift @h; | ||
| 382 | $cw->tagAdd($tag, "$num.$start", "$num.$pos"); | ||
| 383 | }; | ||
| 384 | $cw->DoOneEvent(2) unless ($nodoEvent | ||
| 385 | || !$cw->cget('-highlightInBackground')); #DON'T PREVENT USER-INTERACTION WHILE RE-HILIGHTING! | ||
| 386 | }; | ||
| 387 | $cli->[$num] = [ $hlt->stateGet ]; | ||
| 388 | } | ||
| 389 | |||
| 390 | sub highlightPlug { | ||
| 391 | my $cw = shift; | ||
| 392 | my $plug = $cw->Subwidget('formatter'); | ||
| 393 | my $syntax = $cw->cget('-syntax'); | ||
| 394 | $syntax =~ s/\:\:.*$//o; | ||
| 395 | my $rules = $cw->cget('-rules'); | ||
| 396 | if (not defined($plug)) { | ||
| 397 | $plug = $cw->highlightPlugInit; | ||
| 398 | } elsif (ref($syntax)) { | ||
| 399 | $plug = $cw->highlightPlugInit if ($syntax ne $plug); | ||
| 400 | } elsif ($syntax ne $plug->syntax) { | ||
| 401 | $cw->rulesDelete; | ||
| 402 | $plug = $cw->highlightPlugInit; | ||
| 403 | $cw->highlightPurge(1); | ||
| 404 | } elsif (defined($rules)) { | ||
| 405 | # if ($rules ne $plug->rules) { #JWT: CHGD TO NEXT TO PREVENT INFINITE RECURSION WHEN "None" HIGHLIGHTER IS USED! | ||
| 406 | if ($#{$rules} >= 0 && $rules ne $plug->rules) { | ||
| 407 | $cw->rulesDelete; | ||
| 408 | $plug->rules($rules); | ||
| 409 | $cw->rulesConfigure; | ||
| 410 | $cw->highlightPurge(1); | ||
| 411 | } | ||
| 412 | } else { | ||
| 413 | $cw->rulesDelete; | ||
| 414 | $cw->highlightPlugInit; | ||
| 415 | $cw->highlightPurge(1); | ||
| 416 | } | ||
| 417 | return $plug | ||
| 418 | } | ||
| 419 | |||
| 420 | sub highlightPlugInit { | ||
| 421 | my $cw = shift; | ||
| 422 | my $syntax = $cw->cget('-syntax'); | ||
| 423 | if (not defined($cw->cget('-rules'))) { $cw->rulesFetch }; | ||
| 424 | my $plug; | ||
| 425 | my $lang = ''; | ||
| 426 | if (ref($syntax)) { | ||
| 427 | $plug = $syntax; | ||
| 428 | } else { | ||
| 429 | $cw->{'SYNTAX'} = $syntax; | ||
| 430 | if ($cw->cget('-syntaxcomments')) { | ||
| 431 | $cw->configure('-commentchar' => (defined $syntaxcomments{$syntax}) | ||
| 432 | ? $syntaxcomments{$syntax} : '#') | ||
| 433 | } | ||
| 434 | $lang = $1 if ($syntax =~ s/\:\:(.*)$//o); | ||
| 435 | my @opt = (); | ||
| 436 | if (my $rules = $cw->cget('-rules')) { | ||
| 437 | push(@opt, $rules); | ||
| 438 | } | ||
| 439 | my $evalStr = "require Tk::TextHighlight::$syntax; \$plug = new Tk::TextHighlight::$syntax(" | ||
| 440 | . ($lang ? "'$lang', " : '') . "\@opt);"; | ||
| 441 | eval $evalStr; | ||
| 442 | #JWT: ADDED UNLESS 20060703 TO PROPERLY INITIALIZE RULES FROM PLUGIN, IF NO .rules FILE DEFINED. | ||
| 443 | unless ($@ || !defined($plug) || !defined($plug->rules) | ||
| 444 | || $cw->cget('-noPlugInit')) | ||
| 445 | { | ||
| 446 | my $rules = $plug->rules; | ||
| 447 | $cw->configure(-rules => \@$rules); | ||
| 448 | } | ||
| 449 | } | ||
| 450 | $cw->Advertise('formatter', $plug); | ||
| 451 | $cw->rulesConfigure; | ||
| 452 | #DOESN'T WORK!: my $bg = $cw->cget('-background'); | ||
| 453 | my $bg; | ||
| 454 | #WARNING: WE *MUST* HAVE A BACKGROUND HERE, ELSE RULES GO ALL WANKEY!: | ||
| 455 | if ($cw->{Configure}{'-background'}) { | ||
| 456 | $bg = $cw->{Configure}{'-background'}; | ||
| 457 | } else { | ||
| 458 | my $Palette = $cw->Palette; #THE PALETTE MAY'VE ALREADY BEEN SET W/O CALLING CONFIGURE TO SET THE BACKGROUND! | ||
| 459 | $bg = (defined $Palette->{'background'}) ? $Palette->{'background'} : '#d9d9d9'; | ||
| 460 | } | ||
| 461 | |||
| 462 | my ($red, $green, $blue) = $cw->rgb($bg); #JWT: NEXT 11 ADDED 20070802 TO PREVENT INVISIBLE TEXT! | ||
| 463 | my @rgb = sort {$b <=> $a} ($red, $green, $blue); | ||
| 464 | my $max = $red + 1.5*$green + 0.5*$blue; #USE SAME RULE AS Tk::setPalette. | ||
| 465 | my $daytime = 1; | ||
| 466 | my $currentrules = $plug->rules; | ||
| 467 | my $TwilightThreshold = defined($Tk::Widget::TwilightThreshold) | ||
| 468 | ? $Tk::Widget::TwilightThreshold : 100000; | ||
| 469 | |||
| 470 | if ($max <= $TwilightThreshold) { #IF BG COLOR IS DARK ENOUGH, FORCE RULES WITH NORMAL BLACK- | ||
| 471 | $daytime = 0; #FOREGROUND TO WHITE TO AVOID COLOR CONTRAST ILLEGABILITIES. | ||
| 472 | #print "-NIGHT 65!\n"; | ||
| 473 | for (my $k=0;$k<=$#{$currentrules};$k++) | ||
| 474 | { | ||
| 475 | $cw->setRule($currentrules->[$k]->[0],$currentrules->[$k]->[1],'white') | ||
| 476 | if ($currentrules->[$k]->[2] eq 'black'); | ||
| 477 | } | ||
| 478 | } | ||
| 479 | for (my $k=0;$k<=$#{$currentrules};$k++) | ||
| 480 | { | ||
| 481 | #RULE FOREGROUND COLOR == BACKGROUND, CHANGE TO BLACK OR WHITE TO KEEP READABLE! | ||
| 482 | $cw->setRule($currentrules->[$k]->[0],$currentrules->[$k]->[1],($daytime ? 'black' : 'white')) | ||
| 483 | if (defined($currentrules->[$k]->[2]) and $currentrules->[$k]->[2] eq $bg); | ||
| 484 | } | ||
| 485 | $cw->update; | ||
| 486 | unless ($cw->cget('-noSyntaxMenu') || $cw->cget('-noRulesMenu')) #JWT: ADDED TO ENSURE VIEW RADIO-BUTTON PROPERLY INITIALIZED/SET. | ||
| 487 | { | ||
| 488 | my @kateMenus; | ||
| 489 | my $ViewSyntaxMenu = $cw->menu->entrycget('View','-menu')->entrycget('Syntax','-menu'); | ||
| 490 | my $lastMenuIndex = $ViewSyntaxMenu->index('end'); | ||
| 491 | |||
| 492 | #WE MUST FETCH THE VARIABLE REFERENCE USED BY THE "View" MENU RADIO-BUTTONS SO | ||
| 493 | #THAT OUR NEW RADIO BUTTONS SHARE SAME VARIABLE (OTHERWISE, WILL HAVE >1 LIT AT | ||
| 494 | #SAME TIME! | ||
| 495 | |||
| 496 | my $var; | ||
| 497 | foreach my $i (0..$lastMenuIndex) | ||
| 498 | { | ||
| 499 | if ($ViewSyntaxMenu->type($i) =~ /radiobutton/o) | ||
| 500 | { | ||
| 501 | $var = $ViewSyntaxMenu->entrycget($i, '-variable'); | ||
| 502 | tie $$var,'Tk::Configure',$cw,'-syntax'; | ||
| 503 | $$var = $lang ? ($syntax.'::'.$lang) : $syntax unless (ref $syntax); | ||
| 504 | last; | ||
| 505 | } | ||
| 506 | } | ||
| 507 | } | ||
| 508 | #IF NO BACKGROUND HAS BEEN SET, USE THE ONE WE SET ABOVE FOR THE RULES & SET IT NOW!: | ||
| 509 | $cw->configure('-background' => $bg) unless ($cw->{Configure}{'-background'}); | ||
| 510 | return $plug; | ||
| 511 | } | ||
| 512 | |||
| 513 | sub highlightPlugList { | ||
| 514 | my $cw = shift; | ||
| 515 | my @ml = (); | ||
| 516 | my $haveKate = 0; | ||
| 517 | foreach my $d (@INC) { | ||
| 518 | my @fl = <$d/Tk/TextHighlight/*.pm>; | ||
| 519 | foreach my $file (@fl) { | ||
| 520 | my ($name, $path, $suffix) = fileparse($file, "\.pm"); | ||
| 521 | if ($name eq 'Kate') { #JWT:ADDED THIS PART OF CONDITIONAL 20160118: | ||
| 522 | eval 'use Syntax::Highlight::Engine::Kate; $haveKate = 1; 1' unless ($haveKate); | ||
| 523 | if ($haveKate) { | ||
| 524 | unless (grep { ($name eq $_) } @ml) { push(@ml, $name); }; | ||
| 525 | } | ||
| 526 | #CHGD. TO NEXT 20160119: } elsif (($name ne 'None') and ($name ne 'Template')) { | ||
| 527 | } elsif ($name !~ /^(?:None|Template|RulesEditor)/o) { | ||
| 528 | #avoid duplicates | ||
| 529 | unless (grep { ($name eq $_) } @ml) { push(@ml, $name); }; | ||
| 530 | } | ||
| 531 | } | ||
| 532 | } | ||
| 533 | return sort @ml; | ||
| 534 | } | ||
| 535 | |||
| 536 | sub highlightPurge { | ||
| 537 | my ($cw, $line) = @_; | ||
| 538 | $cw->configure('-colored' => $line); | ||
| 539 | my $cli = $cw->cget('-colorinf'); | ||
| 540 | splice(@$cli, $line) if (@$cli); | ||
| 541 | $cw->highlightVisual; | ||
| 542 | } | ||
| 543 | |||
| 544 | sub highlightVisual { | ||
| 545 | my $cw = shift; | ||
| 546 | return if ($blockHighlight); | ||
| 547 | my $end = $cw->visualend; | ||
| 548 | my $col = $cw->cget('-colored'); | ||
| 549 | if ($col < $end) { | ||
| 550 | $col = $cw->highlight($col, $end); | ||
| 551 | $cw->configure(-colored => $col); | ||
| 552 | } | ||
| 553 | $cw->matchCheck; | ||
| 554 | } | ||
| 555 | |||
| 556 | sub linenumber { | ||
| 557 | my ($cw, $index) = @_; | ||
| 558 | if (not defined($index)) { $index = 'insert'; } | ||
| 559 | my $id = $cw->index($index); | ||
| 560 | my ($line, $pos ) = split(/\./o, $id); | ||
| 561 | return $line; | ||
| 562 | } | ||
| 563 | |||
| 564 | sub matchCheck { | ||
| 565 | my $cw = shift; | ||
| 566 | my $c = $cw->get('insert', 'insert + 1 chars'); | ||
| 567 | my $p = $cw->index('match'); | ||
| 568 | if ($p ne '0.0') { | ||
| 569 | $cw->tagRemove('Match', $p, "$p + 1 chars"); | ||
| 570 | $cw->markSet('match', '0.0'); | ||
| 571 | $cw->markUnset('MyMatch'); | ||
| 572 | } | ||
| 573 | if ($c && $c =~ /\S/o) { #DON'T BOTHER IF CHAR IS EMPTY OR WHITESPACE. | ||
| 574 | my $v = $cw->cget('-match'); | ||
| 575 | my $p = index($v, $c); | ||
| 576 | if ($p ne -1) { #a character in '-match' has been detected. | ||
| 577 | #JWT:ADDED NEXT 2 (INCL CONDITION) 20240729: | ||
| 578 | my $cprev = $cw->get('insert - 1 chars', 'insert'); | ||
| 579 | unless ($cprev =~ /\\/o) { #AVOID MATCHING ESCAPED BRACES, IE. IN REGICES: | ||
| 580 | my $count = 0; | ||
| 581 | my $found = 0; | ||
| 582 | #ADDED NEXT 2 20240701 TO AVOID MATCHING BRACES IN COMMENTS: | ||
| 583 | my $inTags = join('|', $cw->tagNames('insert')); | ||
| 584 | unless ($inTags =~ /(?:Comment|String)/o) { | ||
| 585 | if ($p % 2) { | ||
| 586 | my $m = substr($v, $p - 1, 1); | ||
| 587 | $cw->matchFind('-backwards', $c, $m, | ||
| 588 | $cw->index('insert'), | ||
| 589 | # $cw->index('@0,0'), #CHGD. TO NEXT 20060630 TO PERMIT ^p JUMPING TO MATCHING CHAR OUTSIDE VISIBLE AREA. | ||
| 590 | $cw->index('0.0'), | ||
| 591 | ); | ||
| 592 | } else { | ||
| 593 | my $m = substr($v, $p + 1, 1); | ||
| 594 | $cw->matchFind('-forwards', $c, $m, | ||
| 595 | $cw->index('insert + 1 chars'), | ||
| 596 | # $cw->index($cw->visualend . '.0 lineend'), #CHGD. TO NEXT 20060630 TO PERMIT ^p JUMPING TO MATCHING CHAR OUTSIDE VISIBLE AREA. | ||
| 597 | $cw->index('end'), | ||
| 598 | ); | ||
| 599 | } | ||
| 600 | } | ||
| 601 | } | ||
| 602 | } | ||
| 603 | } | ||
| 604 | $cw->updateCall; | ||
| 605 | } | ||
| 606 | |||
| 607 | sub matchFind { | ||
| 608 | my ($cw, $dir, $char, $ochar, $start, $stop) = @_; | ||
| 609 | #first of all remove a previous match highlight; | ||
| 610 | my $pattern = "\\$char|\\$ochar"; | ||
| 611 | my $found = 0; | ||
| 612 | my $count = 0; | ||
| 613 | while ((not $found) and (my $i = $cw->search( | ||
| 614 | $dir, '-regexp', '-nocase', '--', $pattern, $start, $stop | ||
| 615 | ))) { | ||
| 616 | my $k = $cw->get($i, "$i + 1 chars"); | ||
| 617 | # print "found $k at $i and count is $count\n"; | ||
| 618 | if ($k eq $ochar) { | ||
| 619 | #JWT:ADDED ALL "inTags" LINES BELOW 20240701 TO AVOID MATCHING BRACES IN COMMENTS: | ||
| 620 | my $inTags = join('|', $cw->tagNames($i)); | ||
| 621 | if ($count > 0) { | ||
| 622 | #JWT:ADDED NEXT AND ALTERED NEXT TEST: 20240729 TO AVOID MATCHING ESCAPED BRACES: | ||
| 623 | my $kprev = $cw->get("$i - 1 chars", "$i"); | ||
| 624 | $count-- unless ($inTags =~ /(?:Comment|String)/o || $kprev =~ /\\/o); | ||
| 625 | $start = ($dir eq '-forwards') ? $cw->index("$i + 1 chars") : $i; | ||
| 626 | } else { | ||
| 627 | #JWT:ADDED NEXT AND ALTERED NEXT TEST: 20240729 TO AVOID MATCHING ESCAPED BRACES: | ||
| 628 | my $kprev = $cw->get("$i - 1 chars", "$i"); | ||
| 629 | if ($inTags =~ /(?:Comment|String)/o || $kprev =~ /\\/o) { | ||
| 630 | $start = ($dir eq '-forwards') ? $cw->index("$i + 1 chars") : $i; | ||
| 631 | } else { | ||
| 632 | # print "Found !!!\n"; | ||
| 633 | $cw->markSet('match', $i); | ||
| 634 | $cw->tagAdd('Match', $i, "$i + 1 chars"); | ||
| 635 | $cw->markSet('MyMatch', $i); | ||
| 636 | $cw->tagRaise('Match'); | ||
| 637 | $found = 1; | ||
| 638 | } | ||
| 639 | } | ||
| 640 | } elsif ($k eq $char) { | ||
| 641 | my $inTags = join('|', $cw->tagNames($i)); | ||
| 642 | #JWT:ADDED NEXT AND ALTERED NEXT TEST: 20240729 TO AVOID MATCHING ESCAPED BRACES: | ||
| 643 | my $kprev = $cw->get("$i - 1 chars", "$i"); | ||
| 644 | $count++ unless ($inTags =~ /(?:Comment|String)/o || $kprev =~ /\\/o); | ||
| 645 | $start = ($dir eq '-forwards') ? $cw->index("$i + 1 chars") : $i; | ||
| 646 | } elsif ($i eq $start) { #JWT:THIS *SEEMS* TO NEVER HAPPEN, BUT PREVENTS POTENTIAL INFINITE LOOP, PERHAPS? | ||
| 647 | $found = 1; | ||
| 648 | } | ||
| 649 | } | ||
| 650 | } | ||
| 651 | |||
| 652 | sub matchoptions { | ||
| 653 | my $cw = shift; | ||
| 654 | if (my $o = shift) { | ||
| 655 | my @op = (); | ||
| 656 | @op = (ref $o) ? @$o : split(/\s+/o, $o); | ||
| 657 | $cw->tagConfigure('Match', @op); | ||
| 658 | } | ||
| 659 | } | ||
| 660 | |||
| 661 | sub PostPopupMenu { | ||
| 662 | my $cw = shift; | ||
| 663 | my @r = $cw->SUPER::PostPopupMenu(@_) | ||
| 664 | unless ($cw->cget('-disablemenu') || $cw->cget('-noPopupMenu')); | ||
| 665 | } | ||
| 666 | |||
| 667 | sub rulesConfigure { | ||
| 668 | my $cw = shift; | ||
| 669 | if (my $plug = $cw->Subwidget('formatter')) { | ||
| 670 | my $rules = $plug->rules; | ||
| 671 | foreach my $k (@$rules) { | ||
| 672 | $cw->tagConfigure(@$k); | ||
| 673 | } | ||
| 674 | $cw->configure(-colored => 1, -colorinf => [[ $plug->stateGet]]); | ||
| 675 | } | ||
| 676 | } | ||
| 677 | |||
| 678 | sub setRule #ADDED 20060530 JWT TO PERMIT CHANGING INDIVIDUAL RULES. | ||
| 679 | { | ||
| 680 | my $cw = shift; | ||
| 681 | my @rule = @_; | ||
| 682 | |||
| 683 | if (my $plug = $cw->Subwidget('formatter')) | ||
| 684 | { | ||
| 685 | my $rules = $plug->rules; | ||
| 686 | my @r = @$rules; | ||
| 687 | for (my $k=0;$k<=$#r;$k++) | ||
| 688 | { | ||
| 689 | @{$r[$k]} = @rule if ($rule[0] eq $r[$k]->[0]); | ||
| 690 | } | ||
| 691 | $cw->configure(-rules => \@r); | ||
| 692 | } | ||
| 693 | } | ||
| 694 | |||
| 695 | sub rulesDelete { | ||
| 696 | my $cw = shift; | ||
| 697 | if (my $plug = $cw->Subwidget('formatter')) { | ||
| 698 | my $rules = $plug->rules; | ||
| 699 | foreach my $r (@$rules) { | ||
| 700 | $cw->tagDelete($r->[0]); | ||
| 701 | } | ||
| 702 | } | ||
| 703 | } | ||
| 704 | |||
| 705 | sub rulesEdit { | ||
| 706 | my $cw = shift; | ||
| 707 | require Tk::TextHighlight::RulesEditor; | ||
| 708 | $cw->RulesEditor( | ||
| 709 | -class => 'Toplevel', | ||
| 710 | ); | ||
| 711 | } | ||
| 712 | |||
| 713 | sub rulesFetch { | ||
| 714 | my $cw = shift; | ||
| 715 | my $dir = $cw->cget('-rulesdir'); | ||
| 716 | my $syntax = $cw->cget('-syntax'); | ||
| 717 | $cw->configure(-rules => undef); | ||
| 718 | # print "rulesFetch called\n"; | ||
| 719 | my $result = 0; | ||
| 720 | if ($dir and (-e "$dir/$syntax.rules")) { | ||
| 721 | my $file = "$dir/$syntax.rules"; | ||
| 722 | # print "getting $file\n"; | ||
| 723 | if (my $rl = retrieve("$dir/$syntax.rules")) { | ||
| 724 | # print "configuring\n"; | ||
| 725 | $cw->configure(-rules => $rl); | ||
| 726 | $result = 1; | ||
| 727 | } | ||
| 728 | } | ||
| 729 | return $result; | ||
| 730 | } | ||
| 731 | |||
| 732 | sub rulesSave { | ||
| 733 | my $cw = shift; | ||
| 734 | my $dir = $cw->cget('-rulesdir'); | ||
| 735 | # print "rulesSave called\n"; | ||
| 736 | if (defined($dir) && $dir) { | ||
| 737 | my $syntax = $cw->cget('-syntax'); | ||
| 738 | my $file = "$dir/$syntax.rules"; | ||
| 739 | store($cw->cget('-rules'), $file); | ||
| 740 | } | ||
| 741 | } | ||
| 742 | |||
| 743 | sub scan { | ||
| 744 | my $cw = shift; | ||
| 745 | my @r = $cw->SUPER::scan(@_); | ||
| 746 | $cw->highlightVisual; | ||
| 747 | return @r; | ||
| 748 | } | ||
| 749 | |||
| 750 | sub selectionModify { | ||
| 751 | my ($cw, $char, $mode) = @_; | ||
| 752 | #mode==1 means UN(comment|indent). | ||
| 753 | my @ranges = $cw->tagRanges('sel'); | ||
| 754 | if (@ranges eq 2) { | ||
| 755 | $cw->beginUndoBlock; | ||
| 756 | if ($cw->compare($ranges[0],'>',$ranges[1])) { | ||
| 757 | my $x = $ranges[1]; | ||
| 758 | $ranges[1] = $ranges[0]; | ||
| 759 | $ranges[0] = $x; | ||
| 760 | } | ||
| 761 | my $start = $cw->index($ranges[0]); | ||
| 762 | my $end = $cw->index($ranges[1]); | ||
| 763 | eval #MAKE SURE WE PUT MARKS AROUND WHAT WILL BE DELETED AND REPLACED: | ||
| 764 | { | ||
| 765 | $cw->markSet('selstartmk',"$start"); | ||
| 766 | $cw->markGravity('selstartmk','left'); | ||
| 767 | $cw->markSet('selendmk',"$end"); | ||
| 768 | $cw->markGravity('selendmk','right'); | ||
| 769 | }; | ||
| 770 | my $endchar = ''; #SET FOR COMMENTS HAVING A STARTING AND ENDING PAIR, IE. /* & */: | ||
| 771 | if ($char =~ m#^[\/\(]\*$#o) { #C, C++, CSS, PASCAL & MODULA-II: | ||
| 772 | ($endchar = $char) =~ s#(.)(.)#$2$1#; | ||
| 773 | $endchar =~ s/\(/\)/o; | ||
| 774 | } elsif ($char =~ m#\<\!\-\-#o) { #HTML, XML: | ||
| 775 | $endchar = '-->'; | ||
| 776 | } | ||
| 777 | $cw->tagDelete('sel'); | ||
| 778 | my $partialLine = ($cw->linenumber($start) == $cw->linenumber($end)) ? 1 : 0; | ||
| 779 | if ($partialLine) { #LESS THAN AN ENTIRE SINGLE LINE IS SELECTED: | ||
| 780 | my $commentstring = $cw->get($start, $end); | ||
| 781 | $cw->delete("$start", "$end"); | ||
| 782 | if ($mode && $char eq $cw->cget('-commentchar')) { | ||
| 783 | $commentstring =~ s/\Q$char\E ?//; | ||
| 784 | $commentstring =~ s/ ?\Q$endchar\E ?// if ($endchar); | ||
| 785 | } else { | ||
| 786 | my $pad = ($endchar ? ' ' : ''); | ||
| 787 | $commentstring = "$char$pad$commentstring$pad$endchar"; | ||
| 788 | } | ||
| 789 | $cw->insert("$start", $commentstring); | ||
| 790 | } elsif ($endchar) { #ENCLOSE SELECTION IN COMMENT BRACKETS (IE. /* <SELECTION> */): | ||
| 791 | my $commentstring = $cw->get($start, $end); | ||
| 792 | $cw->delete("$start", "$end"); | ||
| 793 | if ($mode) { | ||
| 794 | if ($commentstring =~ /\Q$char\E.+\Q$endchar\E/s) { | ||
| 795 | $commentstring =~ s/\Q$char\E\n?//s; | ||
| 796 | $commentstring =~ s/\Q$endchar\E\n?/$1/s; | ||
| 797 | } | ||
| 798 | $cw->insert("$start", $commentstring); | ||
| 799 | } else { | ||
| 800 | $cw->insert("$start linestart", "$char\n$commentstring$endchar\n"); | ||
| 801 | } | ||
| 802 | } else { #PREPEND COMMENT (OR INDENT) STRING TO EACH LINE SELECTED: | ||
| 803 | while ($cw->compare($start, "<", $end)) { | ||
| 804 | if ($mode) { | ||
| 805 | $cw->delete("$start linestart", "$start linestart + 1 chars") | ||
| 806 | if ($cw->get("$start linestart", "$start linestart + 1 chars") eq $char); | ||
| 807 | } else { | ||
| 808 | $cw->insert("$start linestart", $char); | ||
| 809 | } | ||
| 810 | $start = $cw->index("$start + 1 lines"); | ||
| 811 | } | ||
| 812 | } | ||
| 813 | $cw->tagAdd('sel', 'selstartmk', 'selendmk'); #RESTORE SELECTION TO THE STUFF UPDATED. | ||
| 814 | $cw->endUndoBlock; | ||
| 815 | } | ||
| 816 | } | ||
| 817 | |||
| 818 | # SelectTo -- | ||
| 819 | # This procedure is invoked to extend the selection, typically when | ||
| 820 | # dragging it with the mouse. Depending on the selection mode (character, | ||
| 821 | # word, line) it selects in different-sized units. This procedure | ||
| 822 | # ignores mouse motions initially until the mouse has moved from | ||
| 823 | # one character to another or until there have been multiple clicks. | ||
| 824 | # | ||
| 825 | # Arguments: | ||
| 826 | # w - The text window in which the button was pressed. | ||
| 827 | # index - Index of character at which the mouse button was pressed. | ||
| 828 | sub SelectToMouse #INTERNAL METHOD: | ||
| 829 | { | ||
| 830 | my ($w, $index, $mode)= @_; | ||
| 831 | $Tk::selectMode = $mode if defined ($mode); | ||
| 832 | my $cur = $w->index($index); | ||
| 833 | my $anchor = $w->index('insert'); | ||
| 834 | eval "my \$a = \$w->index('anchor')"; | ||
| 835 | return if ($@); | ||
| 836 | |||
| 837 | $Tk::mouseMoved = ($w->compare($cur,'!=',$anchor)) ? 1 : 0; | ||
| 838 | $Tk::selectMode = 'char' unless (defined $Tk::selectMode); | ||
| 839 | $mode = $Tk::selectMode; | ||
| 840 | my ($first,$last); | ||
| 841 | if ($mode eq 'char') { | ||
| 842 | if ($w->compare($cur,'<','anchor')) { | ||
| 843 | $first = $cur; | ||
| 844 | $last = 'anchor'; | ||
| 845 | } elsif ($w->compare($cur,'==','anchor')) { | ||
| 846 | return; | ||
| 847 | } else { | ||
| 848 | $first = 'anchor'; | ||
| 849 | $last = $cur | ||
| 850 | } | ||
| 851 | } elsif ($mode eq 'word') { | ||
| 852 | if ($w->compare($cur,'<','anchor')) { | ||
| 853 | $first = $w->index("$cur wordstart"); | ||
| 854 | $last = $w->index('anchor - 1c wordend') | ||
| 855 | } else { | ||
| 856 | $first = $w->index('anchor wordstart'); | ||
| 857 | $last = $w->index("$cur wordend") | ||
| 858 | } | ||
| 859 | } elsif ($mode eq 'line') { | ||
| 860 | if ($w->compare($cur,'<','anchor')) { | ||
| 861 | $first = $w->index("$cur linestart"); | ||
| 862 | $last = $w->index('anchor - 1c lineend + 1c') | ||
| 863 | } else { | ||
| 864 | $first = $w->index('anchor linestart'); | ||
| 865 | $last = $w->index("$cur lineend + 1c") | ||
| 866 | } | ||
| 867 | } | ||
| 868 | if ($Tk::mouseMoved || $Tk::selectMode ne 'char') { | ||
| 869 | $w->tagRemove('sel','1.0',$first); | ||
| 870 | $w->tagAdd('sel',$first,$last); | ||
| 871 | $w->tagRemove('sel',$last,'end'); | ||
| 872 | $w->idletasks; | ||
| 873 | } | ||
| 874 | } | ||
| 875 | |||
| 876 | sub extendSelect { | ||
| 877 | my ($w) = @_; | ||
| 878 | my $Ev = $w->XEvent; | ||
| 879 | $w->SelectToMouse($Ev->xy,'char'); | ||
| 880 | } | ||
| 881 | |||
| 882 | sub syntax { | ||
| 883 | my $cw = shift; | ||
| 884 | if (@_) { | ||
| 885 | my $name = shift; | ||
| 886 | my $fm; | ||
| 887 | eval ("require Tk::TextHighlight::$name; \$fm = new Tk::TextHighlight::$name(\$cw);"); | ||
| 888 | $cw->Advertise('formatter', $fm); | ||
| 889 | $cw->{'SYNTAX'} = $name; | ||
| 890 | if ($cw->cget('-syntaxcomments')) { | ||
| 891 | $cw->configure('-commentchar' => (defined $syntaxcomments{$name}) | ||
| 892 | ? $syntaxcomments{$name} : '#') | ||
| 893 | } | ||
| 894 | } | ||
| 895 | return $cw->{'SYNTAX'}; | ||
| 896 | } | ||
| 897 | |||
| 898 | sub yview { | ||
| 899 | my $cw = shift; | ||
| 900 | my @r = (); | ||
| 901 | if (@_) { | ||
| 902 | @r = $cw->SUPER::yview(@_); | ||
| 903 | if ($_[1] > 0) { #ONLY RE-HIGHLIGHT IF SCROLLING DOWN (PREV. LINES ALREADY HIGHLIGHTED)! | ||
| 904 | my ($p) = caller; | ||
| 905 | $nodoEvent = 1 if ($p =~ /scroll/io); #THIS PREVENTS REPEATING (RUN-AWAY) SCROLLING! | ||
| 906 | $cw->highlightVisual; | ||
| 907 | } | ||
| 908 | } else { | ||
| 909 | @r = $cw->SUPER::yview; | ||
| 910 | } | ||
| 911 | return @r; | ||
| 912 | } | ||
| 913 | |||
| 914 | sub see { | ||
| 915 | my $cw = shift; | ||
| 916 | my @r = $cw->SUPER::see(@_); | ||
| 917 | $cw->highlightVisual; | ||
| 918 | return @r | ||
| 919 | } | ||
| 920 | |||
| 921 | sub updateCall { | ||
| 922 | my $cw = shift; | ||
| 923 | my $call = $cw->cget('-updatecall'); | ||
| 924 | &$call; | ||
| 925 | $nodoEvent = 0; | ||
| 926 | } | ||
| 927 | |||
| 928 | sub EditMenuItems { #INTERNAL METHOD: | ||
| 929 | my $cw = shift; | ||
| 930 | if ($TEXTWIDGET =~ /SuperText/) { #SUPERTEXT SUPPORTS UNDO/REDO, BUT DOESN'T DO MENUS: | ||
| 931 | return [ | ||
| 932 | ["command"=>'Undo', -command => [$cw => 'undo']], | ||
| 933 | ["command"=>'Redo', -command => [$cw => 'redo']], | ||
| 934 | "-",@{$cw->SUPER::EditMenuItems}, | ||
| 935 | ["command"=>'Select', -command => [$cw => 'extendSelect']], | ||
| 936 | "-", | ||
| 937 | ["command"=>'Comment', -command => [$cw => 'selectionComment']], | ||
| 938 | ["command"=>'Uncomment', -command => [$cw => 'selectionUnComment']], | ||
| 939 | "-", | ||
| 940 | ["command"=>'Indent', -command => [$cw => 'selectionIndent']], | ||
| 941 | ["command"=>'Unindent', -command => [$cw => 'selectionUnIndent']], | ||
| 942 | ]; | ||
| 943 | } else { | ||
| 944 | return [ | ||
| 945 | @{$cw->SUPER::EditMenuItems}, | ||
| 946 | ["command"=>'Select', -command => [$cw => 'extendSelect']], | ||
| 947 | "-", | ||
| 948 | ["command"=>'Comment', -command => [$cw => 'selectionComment']], | ||
| 949 | ["command"=>'Uncomment', -command => [$cw => 'selectionUnComment']], | ||
| 950 | "-", | ||
| 951 | ["command"=>'Indent', -command => [$cw => 'selectionIndent']], | ||
| 952 | ["command"=>'Unindent', -command => [$cw => 'selectionUnIndent']], | ||
| 953 | ]; | ||
| 954 | } | ||
| 955 | } | ||
| 956 | |||
| 957 | sub ViewMenuItems { #INTERNAL METHOD: | ||
| 958 | my $cw = shift; | ||
| 959 | my $s; | ||
| 960 | tie $s,'Tk::Configure',$cw,'-syntax'; | ||
| 961 | my @stx = ('None', $cw->highlightPlugList); | ||
| 962 | my @rad = (['command' => 'Reset', -command => sub { | ||
| 963 | $cw->configure('-rules' => undef); | ||
| 964 | $cw->highlightPlug; | ||
| 965 | }]); | ||
| 966 | foreach my $n (@stx) { | ||
| 967 | push(@rad, [ | ||
| 968 | 'radiobutton' => $n, | ||
| 969 | -variable => \$s, | ||
| 970 | -value => $n, | ||
| 971 | -command => sub { | ||
| 972 | $cw->configure('-rules' => undef); | ||
| 973 | $cw->highlightPlug; | ||
| 974 | } | ||
| 975 | ]); | ||
| 976 | } | ||
| 977 | my $dir = $cw->cget('-rulesdir'); | ||
| 978 | my $syntax = $cw->cget('-syntax'); | ||
| 979 | my $menuExt = \@{$cw->SUPER::ViewMenuItems}; | ||
| 980 | unless ($cw->cget('-noRulesMenu')) | ||
| 981 | { | ||
| 982 | push (@{$menuExt}, | ||
| 983 | ['cascade'=>'Syntax', | ||
| 984 | -menuitems => [@rad], | ||
| 985 | ]) unless ($cw->cget('-noSyntaxMenu')); | ||
| 986 | push (@{$menuExt}, | ||
| 987 | ['command'=>'ReHighlight', | ||
| 988 | -command => sub { $cw->highlightPlugInit }, | ||
| 989 | ]); | ||
| 990 | push (@{$menuExt}, | ||
| 991 | ['command'=>'Rules Editor', | ||
| 992 | -command => sub { $cw->rulesEdit }, | ||
| 993 | ]) unless ($cw->cget('-noRulesEditMenu')); | ||
| 994 | push (@{$menuExt}, | ||
| 995 | ['command'=>'Save Rules', | ||
| 996 | -command => sub { $cw->rulesSave }, | ||
| 997 | ]) if (!$cw->cget('-noSaveRulesMenu') && $dir | ||
| 998 | && (-w $dir)); | ||
| 999 | } | ||
| 1000 | return $menuExt; | ||
| 1001 | } | ||
| 1002 | |||
| 1003 | sub adjustMenuState #INTERNAL METHOD: | ||
| 1004 | { | ||
| 1005 | my $w = shift; | ||
| 1006 | |||
| 1007 | my $state = $w->{READONLY} ? 'disabled' : 'normal'; | ||
| 1008 | my $EditSyntaxMenu = $w->menu->entrycget('Edit','-menu'); | ||
| 1009 | eval "\$EditSyntaxMenu->entryconfigure('Cut', -state => \$state)"; | ||
| 1010 | eval "\$EditSyntaxMenu->entryconfigure('Paste', -state => \$state)"; | ||
| 1011 | eval "\$EditSyntaxMenu->entryconfigure('Undo', -state => \$state)"; | ||
| 1012 | eval "\$EditSyntaxMenu->entryconfigure('Redo', -state => \$state)"; | ||
| 1013 | eval "\$EditSyntaxMenu->entryconfigure('Comment', -state => \$state)"; | ||
| 1014 | eval "\$EditSyntaxMenu->entryconfigure('Uncomment', -state => \$state)"; | ||
| 1015 | eval "\$EditSyntaxMenu->entryconfigure('Indent', -state => \$state)"; | ||
| 1016 | eval "\$EditSyntaxMenu->entryconfigure('Unindent', -state => \$state)"; | ||
| 1017 | my $SearchSyntaxMenu = $w->menu->entrycget('Search','-menu'); | ||
| 1018 | eval "\$SearchSyntaxMenu->entryconfigure('Replace', -state => \$state)"; | ||
| 1019 | } | ||
| 1020 | |||
| 1021 | sub visualend { | ||
| 1022 | my $cw = shift; | ||
| 1023 | my $end = $cw->linenumber('end - 1 chars'); | ||
| 1024 | my ($first, $last) = $cw->Tk::Text::yview; | ||
| 1025 | my $vend = int($last * $end) + 2; | ||
| 1026 | $vend = $end if ($vend > $end); | ||
| 1027 | return $vend; | ||
| 1028 | } | ||
| 1029 | |||
| 1030 | sub fetchKateInfo #FETCH LISTS OF KATE LANGUAGES AND FILE EXTENSION PATTERNS W/O KATE: | ||
| 1031 | { | ||
| 1032 | #IT IS NECESSARY TO FETCH THIS INFORMATION W/O USING KATE METHODS SINCE WE MAY NOT | ||
| 1033 | #HAVE CREATED A KATE OBJECT WHEN THIS IS NEEDED! | ||
| 1034 | #We return 3 hash-references: 1st can be passed to addkate2viewmenu() to add the | ||
| 1035 | #Kate languages to the Syntax.View menu. the keys are "Kate::language" and the | ||
| 1036 | #values are what's needed to instantiate Kate for that language. the 2nd is | ||
| 1037 | #a list of file-extension pattern suitable for matching against file-names and | ||
| 1038 | #the values are the reccomended Kate language for that file-extension. | ||
| 1039 | |||
| 1040 | my $cw = shift; | ||
| 1041 | |||
| 1042 | my (%sectionHash, %extHash, %syntaxHash); | ||
| 1043 | |||
| 1044 | foreach my $i (@INC) | ||
| 1045 | { | ||
| 1046 | if (-e "$i/Syntax/Highlight/Engine/Kate.pm" | ||
| 1047 | && open KATE, "$i/Syntax/Highlight/Engine/Kate.pm") | ||
| 1048 | { | ||
| 1049 | my $inExtensions = 0; | ||
| 1050 | my $inSyntaxes = 0; | ||
| 1051 | my $inSections = 0; | ||
| 1052 | while (<KATE>) | ||
| 1053 | { | ||
| 1054 | chomp; | ||
| 1055 | $inExtensions = 1 if (/\$self\-\>\{\'extensions\'\}\s*\=\s*\{/o); | ||
| 1056 | $inSections = 1 if (/\$self\-\>\{\'sections\'\}\s*\=\s*\{/o); | ||
| 1057 | $inSyntaxes = 1 if (/\$self\-\>\{\'syntaxes\'\}\s*\=\s*\{/o); | ||
| 1058 | if ($inSections) | ||
| 1059 | { | ||
| 1060 | if (/\'([^\']+)\'\s*\=\>\s*\[/o) | ||
| 1061 | { | ||
| 1062 | $inSections = $1; | ||
| 1063 | @{$sectionHash{$inSections}} = (); | ||
| 1064 | } | ||
| 1065 | elsif (/\'([^\']+)\'\s*\,/o) | ||
| 1066 | { | ||
| 1067 | push (@{$sectionHash{$inSections}}, $1); | ||
| 1068 | } | ||
| 1069 | elsif (/\}\;/o) | ||
| 1070 | { | ||
| 1071 | $inSections = 0; | ||
| 1072 | } | ||
| 1073 | } | ||
| 1074 | elsif ($inExtensions) | ||
| 1075 | { | ||
| 1076 | if (/\'([^\']+)\'\s*\=\>\s*\[\'([^\']+)\'/o) | ||
| 1077 | { | ||
| 1078 | my $one = '^'.$1.'$'; | ||
| 1079 | my $two = $2; | ||
| 1080 | $one =~ s/\./\\\./o; | ||
| 1081 | $one =~ s/\*/\.\*/go; | ||
| 1082 | $extHash{$one} = "Kate::$two"; | ||
| 1083 | } | ||
| 1084 | elsif (/\}\;/o) | ||
| 1085 | { | ||
| 1086 | $inExtensions = 0; | ||
| 1087 | } | ||
| 1088 | } | ||
| 1089 | elsif ($inSyntaxes) | ||
| 1090 | { | ||
| 1091 | if (/\'([^\']+)\'\s*\=\>\s*\[\'([^\']+)\'/o) | ||
| 1092 | { | ||
| 1093 | $syntaxHash{$1} = $2; | ||
| 1094 | } | ||
| 1095 | elsif (/\}\;/o) | ||
| 1096 | { | ||
| 1097 | $inSyntaxes = 0; | ||
| 1098 | close KATE; | ||
| 1099 | last; | ||
| 1100 | } | ||
| 1101 | } | ||
| 1102 | } | ||
| 1103 | close KATE; | ||
| 1104 | last; | ||
| 1105 | } | ||
| 1106 | } | ||
| 1107 | return (\%sectionHash, \%extHash, \%syntaxHash); | ||
| 1108 | } | ||
| 1109 | |||
| 1110 | sub addKate2ViewMenu #ADD ALL KATE-LANGUAGES AS OPTIONS TO THE "View" MENU: | ||
| 1111 | { | ||
| 1112 | my $cw = shift; | ||
| 1113 | my $sectionHash = shift; | ||
| 1114 | |||
| 1115 | return if ($cw->cget('-noRulesMenu') || $cw->cget('-noSyntaxMenu')); | ||
| 1116 | |||
| 1117 | my $ViewSyntaxMenu = $cw->menu->entrycget('View','-menu')->entrycget('Syntax','-menu'); | ||
| 1118 | my $lastMenuIndex = $ViewSyntaxMenu->index('end'); | ||
| 1119 | |||
| 1120 | #WE MUST FETCH THE VARIABLE REFERENCE USED BY THE "View" MENU RADIO-BUTTONS SO | ||
| 1121 | #THAT OUR NEW RADIO BUTTONS SHARE SAME VARIABLE (OTHERWISE, WILL HAVE >1 LIT AT | ||
| 1122 | #SAME TIME! | ||
| 1123 | |||
| 1124 | my $var; | ||
| 1125 | my $kateIndx = 'end'; | ||
| 1126 | foreach my $i (0..$lastMenuIndex) | ||
| 1127 | { | ||
| 1128 | if ($ViewSyntaxMenu->type($i) =~ /radiobutton/o) | ||
| 1129 | { | ||
| 1130 | $var = $ViewSyntaxMenu->entrycget($i, '-variable'); | ||
| 1131 | tie $$var,'Tk::Configure',$cw,'-syntax'; | ||
| 1132 | if ($ViewSyntaxMenu->entrycget($i, '-label') eq 'Kate') | ||
| 1133 | { | ||
| 1134 | $ViewSyntaxMenu->delete($i); #REMOVE THE "Kate" ENTRY, SINCE WE'RE ADDING KATE STUFF SEPARATELY! | ||
| 1135 | #UNCOMMENT TO INSERT KATE MENUS IN ALPHABETICAL ORDER IN VIEW MENU: $kateIndx = $i; #SAVE IT'S MENU-LOCATION SO WE CAN INSERT THE KATE MENU TREE THERE. | ||
| 1136 | last; | ||
| 1137 | } | ||
| 1138 | } | ||
| 1139 | } | ||
| 1140 | |||
| 1141 | #NOW ADD OUR "KATE" RADIO-BUTTONS! | ||
| 1142 | |||
| 1143 | my ($nextMenu, $menuTitle); | ||
| 1144 | foreach my $sect (sort keys %{$sectionHash}) | ||
| 1145 | { | ||
| 1146 | $nextMenu = $ViewSyntaxMenu->Menu; | ||
| 1147 | foreach my $lang (@{$sectionHash->{$sect}}) | ||
| 1148 | { | ||
| 1149 | $menuTitle = "Kate::$lang"; | ||
| 1150 | $nextMenu->radiobutton( -label => $menuTitle, | ||
| 1151 | -variable => $var, | ||
| 1152 | -value => $menuTitle, | ||
| 1153 | -command => sub | ||
| 1154 | { | ||
| 1155 | $cw->configure('-rules' => undef); | ||
| 1156 | $cw->highlightPlug; | ||
| 1157 | } | ||
| 1158 | ); | ||
| 1159 | } | ||
| 1160 | $ViewSyntaxMenu->insert($kateIndx, 'cascade', | ||
| 1161 | -label => "Kate: $sect...", | ||
| 1162 | -menu => $nextMenu); | ||
| 1163 | ++$kateIndx if ($kateIndx =~ /^\d/o); | ||
| 1164 | } | ||
| 1165 | } | ||
| 1166 | |||
| 1167 | sub getViewMenu #ADDED 2024624 TO ALLOW CALLING PROGRAM TO INCLUDE IN THEIR OWN CUSTOM MENUS: | ||
| 1168 | { | ||
| 1169 | my $cw = shift; | ||
| 1170 | |||
| 1171 | return $cw->menu->entrycget('View','-menu'); | ||
| 1172 | } | ||
| 1173 | |||
| 1174 | sub Load { | ||
| 1175 | my ($cw,$filename) = @_; | ||
| 1176 | return 0 unless (defined($filename) && -r $filename); | ||
| 1177 | if (open(my $fid,"<$filename")) { | ||
| 1178 | $cw->MainWindow->Busy; | ||
| 1179 | $cw->EmptyDocument; | ||
| 1180 | $cw->insert('end',$_) while (<$fid>); | ||
| 1181 | close($fid); | ||
| 1182 | |||
| 1183 | $cw->markSet('insert' => '1.0'); | ||
| 1184 | $cw->MainWindow->Unbusy; | ||
| 1185 | return 1; | ||
| 1186 | } | ||
| 1187 | return 0; | ||
| 1188 | } | ||
| 1189 | |||
| 1190 | #NEEDED FOR CLEARING ENTIRE DOCUMENTS PROGRAMATICALLY IN READ-ONLY VIEWERS (ROText IS OK WITH THIS!): | ||
| 1191 | sub EmptyDocument { | ||
| 1192 | my $cw = shift; | ||
| 1193 | |||
| 1194 | my $rostatus = $cw->{READONLY}; | ||
| 1195 | $cw->{READONLY} = 0; | ||
| 1196 | $cw->delete('0.0','end'); | ||
| 1197 | $cw->{READONLY} = $rostatus; | ||
| 1198 | $cw->ResetUndo; | ||
| 1199 | #WARNING, DOESN'T SEEM TO EXECUTE ANYTHING AFTER THE ResetUndo?!: | ||
| 1200 | } | ||
| 1201 | |||
| 1202 | #FUNCTIONS NOT USED IN THE READ-ONLY VERSION: | ||
| 1203 | |||
| 1204 | sub clipboardCut { | ||
| 1205 | my $cw = shift; | ||
| 1206 | return if ($cw->{READONLY}); | ||
| 1207 | |||
| 1208 | my @ranges = $cw->tagRanges('sel'); | ||
| 1209 | $cw->SUPER::clipboardCut(@_) if (@ranges); | ||
| 1210 | } | ||
| 1211 | |||
| 1212 | sub clipboardPaste { | ||
| 1213 | my $cw = shift; | ||
| 1214 | return if ($cw->{READONLY}); | ||
| 1215 | |||
| 1216 | my @ranges = $cw->tagRanges('sel'); | ||
| 1217 | if (@ranges) { | ||
| 1218 | $cw->tagRemove('sel', '1.0', 'end'); | ||
| 1219 | return; | ||
| 1220 | } | ||
| 1221 | $cw->SUPER::clipboardPaste(@_); | ||
| 1222 | } | ||
| 1223 | |||
| 1224 | sub delete { | ||
| 1225 | my $cw = shift; | ||
| 1226 | return if ($cw->{READONLY}); | ||
| 1227 | |||
| 1228 | my $begin = $_[0]; | ||
| 1229 | $begin = (defined $begin) ? $cw->linenumber($begin) | ||
| 1230 | : $cw->linenumber('insert'); | ||
| 1231 | my $end = $_[1]; | ||
| 1232 | $end = (defined $end) ? $cw->linenumber($end) : $begin; | ||
| 1233 | $cw->SUPER::delete(@_); | ||
| 1234 | $cw->highlightCheck($begin, $end); | ||
| 1235 | } | ||
| 1236 | |||
| 1237 | sub insert { | ||
| 1238 | my $cw = shift; | ||
| 1239 | my $pos = shift; | ||
| 1240 | ##NOTE: CAN'T CHECK FOR READONLY HERE B/C FILES ARE INITIALLY LOADED USING THIS. | ||
| 1241 | # AND $cw->{READONLY} IS *NOT* ALWAYS INITIATED YET IF USING RO*text.pl MODULES!: | ||
| 1242 | |||
| 1243 | $pos = $cw->index($pos); | ||
| 1244 | my $begin = $cw->linenumber("$pos - 1 chars"); | ||
| 1245 | $cw->SUPER::insert($pos, @_); | ||
| 1246 | $cw->highlightCheck($begin, $cw->linenumber('insert lineend')); | ||
| 1247 | #WE'LL GO AHEAD AND INITIALIZE IT HERE IF NOT ALREADY (WILL ALREADY BE IF CALLED FROM $cw->Load())! | ||
| 1248 | $cw->{READONLY} = $readonly unless (defined $cw->{READONLY}); | ||
| 1249 | } | ||
| 1250 | |||
| 1251 | sub Insert { | ||
| 1252 | my $cw = shift; | ||
| 1253 | return if ($cw->{READONLY}); | ||
| 1254 | |||
| 1255 | $cw->beginUndoBlock; | ||
| 1256 | $cw->SUPER::Insert(@_); | ||
| 1257 | $cw->endUndoBlock; | ||
| 1258 | $cw->see('insert'); | ||
| 1259 | } | ||
| 1260 | |||
| 1261 | sub InsertKeypress { | ||
| 1262 | my ($cw,$char) = @_; | ||
| 1263 | return if ($cw->{READONLY}); | ||
| 1264 | |||
| 1265 | if ($char ne '') { | ||
| 1266 | my $index = $cw->index('insert'); | ||
| 1267 | my $line = $cw->linenumber($index); | ||
| 1268 | if ($char =~ /^\S$/o and !$cw->OverstrikeMode and !$cw->tagRanges('sel')) { | ||
| 1269 | my $undo_item = $cw->getUndoAtIndex(-1); | ||
| 1270 | if (defined($undo_item) && | ||
| 1271 | ($undo_item->[0] eq 'delete') && | ||
| 1272 | ($undo_item->[2] == $index)) | ||
| 1273 | { | ||
| 1274 | $cw->Tk::Text::insert($index,$char); | ||
| 1275 | $undo_item->[2] = $cw->index('insert'); | ||
| 1276 | $cw->highlightCheck($line, $line); | ||
| 1277 | $cw->see('insert'); #ADDED 20060703 TO ALLOW USER TO SEE WHAT HE'S TYPING PAST END OF LINE (THIS IS BROKEN IN TEXTUNDO TOO). | ||
| 1278 | return; | ||
| 1279 | } | ||
| 1280 | } | ||
| 1281 | $cw->addGlobStart; | ||
| 1282 | $cw->Tk::Text::InsertKeypress($char); | ||
| 1283 | $cw->addGlobEnd; | ||
| 1284 | } | ||
| 1285 | } | ||
| 1286 | |||
| 1287 | sub selectionComment { | ||
| 1288 | my $cw = shift; | ||
| 1289 | return if ($cw->{READONLY}); | ||
| 1290 | |||
| 1291 | $cw->selectionModify($cw->cget('-commentchar'), 0); | ||
| 1292 | } | ||
| 1293 | |||
| 1294 | sub selectionIndent { | ||
| 1295 | my $cw = shift; | ||
| 1296 | return if ($cw->{READONLY}); | ||
| 1297 | |||
| 1298 | $cw->selectionModify($cw->cget('-indentchar'), 0); | ||
| 1299 | } | ||
| 1300 | |||
| 1301 | sub selectionUnComment { | ||
| 1302 | my $cw = shift; | ||
| 1303 | return if ($cw->{READONLY}); | ||
| 1304 | |||
| 1305 | $cw->selectionModify($cw->cget('-commentchar'), 1); | ||
| 1306 | } | ||
| 1307 | |||
| 1308 | sub selectionUnIndent { | ||
| 1309 | my $cw = shift; | ||
| 1310 | return if ($cw->{READONLY}); | ||
| 1311 | |||
| 1312 | $cw->selectionModify($cw->cget('-indentchar'), 1); | ||
| 1313 | } | ||
| 1314 | |||
| 1315 | sub insertTab | ||
| 1316 | { | ||
| 1317 | my ($w) = @_; | ||
| 1318 | return if ($w->{READONLY}); | ||
| 1319 | |||
| 1320 | $w->Insert($w->cget('-indentchar')); | ||
| 1321 | $w->focus; | ||
| 1322 | $w->break; | ||
| 1323 | } | ||
| 1324 | |||
| 1325 | sub insertTabChar | ||
| 1326 | { | ||
| 1327 | my ($w) = @_; | ||
| 1328 | return if ($w->{READONLY}); | ||
| 1329 | |||
| 1330 | $w->Insert("\t"); | ||
| 1331 | $w->focus; | ||
| 1332 | $w->break; | ||
| 1333 | } | ||
| 1334 | |||
| 1335 | sub Backspace { #HANDLE THIS OURSELVES FOR CONSISTANCY AMONG TEXT-WIDGETS: | ||
| 1336 | my $w = shift; | ||
| 1337 | |||
| 1338 | my $selected = ''; | ||
| 1339 | eval { $selected = $w->get('sel.first', 'sel.last') }; | ||
| 1340 | if ($w->{READONLY}) { | ||
| 1341 | if (length($selected) > 0) { | ||
| 1342 | $w->tagRemove('sel','0.0','end'); | ||
| 1343 | } else { #ONLY BACK UP A CHARACTER IF NO SELECTION CLEARED: | ||
| 1344 | $w->SetCursor('insert - 1c'); | ||
| 1345 | $w->see('insert'); | ||
| 1346 | } | ||
| 1347 | } else { | ||
| 1348 | if (length($selected) > 0) { | ||
| 1349 | my $prev_insert = $w->index('insert'); | ||
| 1350 | #DELETE SELECTION ONLY IF CURSOR IS WITHIN OR ABUTS IT, OTHERWISE CLEAR IT: | ||
| 1351 | if ($w->compare($prev_insert,'>=',$w->index('sel.first')) | ||
| 1352 | && $w->compare($prev_insert,'<=',$w->index('sel.last'))) { | ||
| 1353 | $w->deleteSelected; | ||
| 1354 | } else { | ||
| 1355 | $w->tagRemove('sel','0.0','end'); | ||
| 1356 | } | ||
| 1357 | } else { #ONLY DELETE PREV. CHARACTER IF NO SELECTION CLEARED: | ||
| 1358 | $w->delete('insert - 1c') if ($w->compare('insert','!=','1.0')); | ||
| 1359 | $w->see('insert'); | ||
| 1360 | } | ||
| 1361 | } | ||
| 1362 | $w->break; | ||
| 1363 | } | ||
| 1364 | |||
| 1365 | sub Space { #HANDLE THIS OURSELVES FOR CONSISTANCY AMONG TEXT-WIDGETS: | ||
| 1366 | my $w = shift; | ||
| 1367 | |||
| 1368 | if ($w->{READONLY}) { | ||
| 1369 | my $selected = ''; | ||
| 1370 | eval { $selected = $w->get('sel.first', 'sel.last') }; | ||
| 1371 | if (length($selected) > 0) { | ||
| 1372 | $w->tagRemove('sel','0.0','end'); | ||
| 1373 | } else { #ONLY ADVANCE A CHARACTER IF NO SELECTION CLEARED: | ||
| 1374 | $w->SetCursor('insert + 1c'); | ||
| 1375 | $w->see('insert'); | ||
| 1376 | } | ||
| 1377 | } else { | ||
| 1378 | $w->Insert(' '); | ||
| 1379 | $w->see('insert'); | ||
| 1380 | } | ||
| 1381 | $w->break; | ||
| 1382 | } | ||
| 1383 | |||
| 1384 | sub pastePrimaryAtMouse { | ||
| 1385 | my $w = shift; | ||
| 1386 | return if ($w->{READONLY}); | ||
| 1387 | |||
| 1388 | my $ev = $w->XEvent; | ||
| 1389 | |||
| 1390 | Tk::catch { $w->insert($ev->xy,$w->SelectionGet);} unless ($Tk::mouseMoved); | ||
| 1391 | } | ||
| 1392 | |||
| 1393 | sub ResetUndo | ||
| 1394 | { | ||
| 1395 | shift->SUPER::resetUndo; | ||
| 1396 | } | ||
| 1397 | |||
| 1398 | 1 | ||
| 1399 | |||
| 1400 | __END__ | ||