| 1 | package Tk::CodeText; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Tk::CodeText - Programmer's Swiss army knife Text widget. | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use vars qw($VERSION); | ||
| 12 | $VERSION = '0.69'; | ||
| 13 | |||
| 14 | use base qw(Tk::Derived Tk::Frame); | ||
| 15 | |||
| 16 | use Tk::CodeText::Kamelon; | ||
| 17 | use Tk; | ||
| 18 | use Tie::Watch; | ||
| 19 | require Tk::CodeText::StatusBar; | ||
| 20 | require Tk::CodeText::TagsEditor; | ||
| 21 | require Tk::CodeText::Theme; | ||
| 22 | require Tk::DialogBox; | ||
| 23 | require Tk::Font; | ||
| 24 | require Tk::XText; | ||
| 25 | require Tk::YAMessage; | ||
| 26 | |||
| 27 | |||
| 28 | Construct Tk::Widget 'CodeText'; | ||
| 29 | |||
| 30 | my @defaultattributes = ( | ||
| 31 | 'Alert' => [-background => '#DB7C47', -foreground => '#FFFFFF'], | ||
| 32 | 'Annotation' => [-foreground => '#5A5A5A'], | ||
| 33 | 'Attribute' => [-foreground => '#00B900', -weight => 'bold'], | ||
| 34 | 'BaseN' => [-foreground => '#0000A9'], | ||
| 35 | 'BuiltIn' => [-foreground => '#B500E6'], | ||
| 36 | 'Char' => [-foreground => '#FF00FF'], | ||
| 37 | 'Comment' => [foreground => '#5A5A5A', -slant => 'italic'], | ||
| 38 | 'CommentVar' => [-foreground => '#5A5A5A', -slant => 'italic', -weight => 'bold'], | ||
| 39 | 'Constant' => [-foreground => '#0000FF', -weight => 'bold'], | ||
| 40 | 'ControlFlow' => [-foreground => '#0062AD'], | ||
| 41 | 'DataType' => [-foreground => '#0080A8', -weight => 'bold'], | ||
| 42 | 'DecVal' => [-foreground => '#9C4E2B'], | ||
| 43 | 'Documentation' => [-foreground => '#7F5A41', -slant => 'italic'], | ||
| 44 | 'Error' => [-background => '#FF0000', -foreground => '#FFFF00'], | ||
| 45 | 'Extension' => [-foreground => '#9A53D1'], | ||
| 46 | 'Float' => [-foreground => '#9C4E2B', -weight => 'bold'], | ||
| 47 | 'Function' => [-foreground => '#008A00'], | ||
| 48 | 'Import' => [-foreground => '#950000', -slate => 'italic'], | ||
| 49 | 'Information' => [foreground => '#5A5A5A', -weight => 'bold'], | ||
| 50 | 'Keyword' => [-weight => 'bold'], | ||
| 51 | 'Normal' => [], | ||
| 52 | 'Operator' => [-foreground => '#85530E'], | ||
| 53 | 'Others' => [-foreground => '#FF6200'], | ||
| 54 | 'Preprocessor' => [-slant => 'italic'], | ||
| 55 | 'RegionMarker' => [-background => '#00CFFF'], | ||
| 56 | 'SpecialChar' => [-foreground => '#9A53D1'], | ||
| 57 | 'SpecialString' => [-foreground => '#FF4449'], | ||
| 58 | 'String' => [-foreground => '#FF0000'], | ||
| 59 | 'Variable' => [-foreground => '#0000FF', -weight => 'bold'], | ||
| 60 | 'VerbatimString' => [-foreground => '#FF4449', -weight => 'bold'], | ||
| 61 | 'Warning' => [-background => '#FFFF00', -foreground => '#FF0000'], | ||
| 62 | ); | ||
| 63 | |||
| 64 | my $minusimg = '#define indicatorclose_width 11 | ||
| 65 | #define indicatorclose_height 11 | ||
| 66 | static unsigned char indicatorclose_bits[] = { | ||
| 67 | 0xff, 0x07, 0x01, 0x04, 0x01, 0x04, 0x01, 0x04, 0x01, 0x04, 0xfd, 0x05, | ||
| 68 | 0x01, 0x04, 0x01, 0x04, 0x01, 0x04, 0x01, 0x04, 0xff, 0x07 }; | ||
| 69 | '; | ||
| 70 | |||
| 71 | my $plusimg = '#define indicatoropen_width 11 | ||
| 72 | #define indicatoropen_height 11 | ||
| 73 | static unsigned char indicatoropen_bits[] = { | ||
| 74 | 0xff, 0x07, 0x01, 0x04, 0x21, 0x04, 0x21, 0x04, 0x21, 0x04, 0xfd, 0x05, | ||
| 75 | 0x21, 0x04, 0x21, 0x04, 0x21, 0x04, 0x01, 0x04, 0xff, 0x07 }; | ||
| 76 | '; | ||
| 77 | |||
| 78 | |||
| 79 | =head1 SYNOPSIS | ||
| 80 | |||
| 81 | require Tk::CodeText; | ||
| 82 | my $text= $window->CodeText(@options)->pack; | ||
| 83 | |||
| 84 | =head1 DESCRIPTION | ||
| 85 | |||
| 86 | B<Tk::CodeText> aims to be a Scintilla like text widget for Perl/Tk. | ||
| 87 | |||
| 88 | Version 0.40 of B<Tk::CodeText> was re-written from scratch. This version and all later | ||
| 89 | versions are not backwards compatible with version 0.3.4 and earlier. | ||
| 90 | |||
| 91 | It leans heavily on L<Syntax::Kamelon>. | ||
| 92 | |||
| 93 | It features: | ||
| 94 | |||
| 95 | =over 4 | ||
| 96 | |||
| 97 | =item line numbers on display | ||
| 98 | |||
| 99 | =item code folding | ||
| 100 | |||
| 101 | =item status bar | ||
| 102 | |||
| 103 | The status bar has document info and tools for setting tab size, indent style and syntax | ||
| 104 | |||
| 105 | =item advanced word based undo/redo stack | ||
| 106 | |||
| 107 | It keeps track of the last saving point and selections | ||
| 108 | |||
| 109 | =item syntax highlighting in many languages and formats. | ||
| 110 | |||
| 111 | =item commenting and uncommenting blocks and lines | ||
| 112 | |||
| 113 | =item indenting and unindenting blocks and lines | ||
| 114 | |||
| 115 | =item automatic indentation | ||
| 116 | |||
| 117 | =item autocomplete | ||
| 118 | |||
| 119 | =item matching of nested {}, () and [] pairs | ||
| 120 | |||
| 121 | =back | ||
| 122 | |||
| 123 | =head1 OPTIONS | ||
| 124 | |||
| 125 | =over 4 | ||
| 126 | |||
| 127 | =item Name: B<acPopSize> | ||
| 128 | |||
| 129 | =item Class: B<AcPopSize> | ||
| 130 | |||
| 131 | =item Switch: B<-acpopsize> | ||
| 132 | |||
| 133 | The length of the typed string to trigger autocomplete into making suggestions. Default value 5. | ||
| 134 | |||
| 135 | =item Name: B<acScanSize> | ||
| 136 | |||
| 137 | =item Class: B<AcScanSize> | ||
| 138 | |||
| 139 | =item Switch: B<-acscansize> | ||
| 140 | |||
| 141 | The minimal length of a word to be included in the autocomplete word database. Default value 5. | ||
| 142 | |||
| 143 | =item Name: B<activeDelay> | ||
| 144 | |||
| 145 | =item Class: B<ActiveDelay> | ||
| 146 | |||
| 147 | =item Switch: B<-activedelay> | ||
| 148 | |||
| 149 | The waiting time in miliseconds before an autocomplete pop up can occur. Default value 300. | ||
| 150 | |||
| 151 | =item Name: B<autoBrackets> | ||
| 152 | |||
| 153 | =item Class: B<AutoBrackets> | ||
| 154 | |||
| 155 | =item Switch: B<-autobrackets> | ||
| 156 | |||
| 157 | Boolean. Default value I<false>. Enables or disables autocomplete. | ||
| 158 | |||
| 159 | =item Name: B<autoComplete> | ||
| 160 | |||
| 161 | =item Class: B<AutoComplete> | ||
| 162 | |||
| 163 | =item Switch: B<-autocomplete> | ||
| 164 | |||
| 165 | Boolean. Default value I<false>. Enables or disables autocomplete. | ||
| 166 | |||
| 167 | =item Name: B<autoIndent> | ||
| 168 | |||
| 169 | =item Class: B<AutoIndent> | ||
| 170 | |||
| 171 | =item Switch: B<-autoindent> | ||
| 172 | |||
| 173 | By default 0. If set the text will be indented to the | ||
| 174 | level and style of the previous line. | ||
| 175 | |||
| 176 | =item Name: B<bookmarkColor> | ||
| 177 | |||
| 178 | =item Class: B<BookmarkColor> | ||
| 179 | |||
| 180 | =item Switch: B<-bookmarkcolor> | ||
| 181 | |||
| 182 | Default value #71D0CC. Background color for the line number label | ||
| 183 | of a bookmarked line. | ||
| 184 | |||
| 185 | =item Name: B<bookmarkSize> | ||
| 186 | |||
| 187 | =item Class: B<BookmarkSize> | ||
| 188 | |||
| 189 | =item Switch: B<-bookmarksize> | ||
| 190 | |||
| 191 | Default value 20. length of the label for bookmark entries | ||
| 192 | in the bookmarks menu. | ||
| 193 | |||
| 194 | =item Name: B<configDir> | ||
| 195 | |||
| 196 | =item Class: B<ConfigDir> | ||
| 197 | |||
| 198 | =item Switch: B<-configdir> | ||
| 199 | |||
| 200 | An empty string by default. If set to an existing folder that folder will be used | ||
| 201 | for config files. Currently there is only one of those: The recent colors for the | ||
| 202 | TagsEditor. | ||
| 203 | |||
| 204 | =item Switch: B<-contextmenu> | ||
| 205 | |||
| 206 | Reference to a Tk::Menu object that is used as context menu. | ||
| 207 | If you do not specify it, the B<-menuitems> option is checked. | ||
| 208 | |||
| 209 | =item Switch: B<-disablemenu> | ||
| 210 | |||
| 211 | By default 0. If set the right-click context menu is disabled. | ||
| 212 | |||
| 213 | =item Switch: B<-findoptions> | ||
| 214 | |||
| 215 | Default: [-background => '#C0FFC0', -foreground => '#000000'](light green and black). | ||
| 216 | Specifies the options for the find tag. | ||
| 217 | |||
| 218 | =item Name: B<highlightInterval> | ||
| 219 | |||
| 220 | =item Class: B<HighlightInterval> | ||
| 221 | |||
| 222 | =item Switch: B<-highlightinterval> | ||
| 223 | |||
| 224 | By default 1 milisecond. Highlighting is done on a | ||
| 225 | line by line basis. This is the time between lines. | ||
| 226 | |||
| 227 | =item Name: B<indentStyle> | ||
| 228 | |||
| 229 | =item Class: B<IndentStyle> | ||
| 230 | |||
| 231 | =item Switch: B<-indentstyle> | ||
| 232 | |||
| 233 | Default value 'tab'. You can also set it to a number. | ||
| 234 | In that case an indent will be the number of spaces. | ||
| 235 | |||
| 236 | =item Name: B<linesPerCycle> | ||
| 237 | |||
| 238 | =item Class: B<LinesPerCycle> | ||
| 239 | |||
| 240 | =item Switch: B<-linespercycle> | ||
| 241 | |||
| 242 | Default value 10. It specifies how many lines Tk::CodeText will Highlight in one cycle. | ||
| 243 | You can tone it down if the application responds sluggish. | ||
| 244 | |||
| 245 | =item Switch: B<-match> | ||
| 246 | |||
| 247 | Default value '[]{}()'. Specifies which items to match | ||
| 248 | against nested occurrences. | ||
| 249 | |||
| 250 | =item Switch: B<-matchoptions> | ||
| 251 | |||
| 252 | Default: [-background => 'blue', -foreground => 'yellow']. | ||
| 253 | Specifies the options for the match tag. | ||
| 254 | |||
| 255 | =item Switch: B<-menuitems> | ||
| 256 | |||
| 257 | Specify the menu items for the left-click popup menu. | ||
| 258 | By default set to undef, meaning no popup menu. | ||
| 259 | |||
| 260 | =item Switch: B<-minusimg> | ||
| 261 | |||
| 262 | Image used for the collapse state of a folding point. | ||
| 263 | By default it is a bitmap defined in this module. | ||
| 264 | |||
| 265 | =item Switch: B<-mmodifiedcall> | ||
| 266 | |||
| 267 | Callback called whenever text is modified. It gets | ||
| 268 | the location index as parameter. | ||
| 269 | |||
| 270 | =item Switch: B<-plusimg> | ||
| 271 | |||
| 272 | Image used for the expand state of a folding point. | ||
| 273 | By default it is a bitmap defined in this module. | ||
| 274 | |||
| 275 | =item Switch: B<-readonly> | ||
| 276 | |||
| 277 | Default value 0. If you set it to 1 the user will not be | ||
| 278 | able to make modifications. | ||
| 279 | |||
| 280 | =item Switch: B<-saveimage> | ||
| 281 | |||
| 282 | The icon image used to indicate the text is modified on the status bar. | ||
| 283 | By default it is an internally defined xpm. | ||
| 284 | |||
| 285 | =item Switch: B<-scrollbars> | ||
| 286 | |||
| 287 | Default value 'osoe'. Specifies if and how scrollbars | ||
| 288 | are to be used. If you set it to an ampty string no | ||
| 289 | scrollbars will be created. See also L<Tk::Scrolled>. | ||
| 290 | |||
| 291 | Only available at create time. | ||
| 292 | |||
| 293 | =item Name: B<showSpaces> | ||
| 294 | |||
| 295 | =item Class: B<ShowSpaces> | ||
| 296 | |||
| 297 | =item Switch: B<-showspaces> | ||
| 298 | |||
| 299 | Default value 0. If set the leading and trailing spaces on each line | ||
| 300 | will be highlighted in their own back ground color. See also the | ||
| 301 | B<-spacebackground> and B<-tabbackground> options. | ||
| 302 | |||
| 303 | =item Name: B<spaceBackground> | ||
| 304 | |||
| 305 | =item Class: B<SpaceBackground> | ||
| 306 | |||
| 307 | =item Switch: B<-spacebackground> | ||
| 308 | |||
| 309 | Default value #0098C2. The background color shown to leading and | ||
| 310 | trailing spaces when the B<-showspaces> option is set. | ||
| 311 | |||
| 312 | =item Switch: B<-statusinterval> | ||
| 313 | |||
| 314 | By default 200 ms. Update interval for the status bar. | ||
| 315 | |||
| 316 | =item Name: B<showFolds> | ||
| 317 | |||
| 318 | =item Class: B<ShowFolds> | ||
| 319 | |||
| 320 | =item Switch: B<-showfolds> | ||
| 321 | |||
| 322 | Default value 1. If cleared the folding markers | ||
| 323 | will be hidden. | ||
| 324 | |||
| 325 | =item Name: B<showNumbers> | ||
| 326 | |||
| 327 | =item Class: B<ShowNumbers> | ||
| 328 | |||
| 329 | =item Switch: B<-shownumbers> | ||
| 330 | |||
| 331 | Default value 1. If cleared the line numbers | ||
| 332 | will be hidden. | ||
| 333 | |||
| 334 | =item Name: B<showStatus> | ||
| 335 | |||
| 336 | =item Class: B<ShowStatus> | ||
| 337 | |||
| 338 | =item Switch: B<-showstatus> | ||
| 339 | |||
| 340 | Default value 1. If cleared the status bar | ||
| 341 | will be hidden. | ||
| 342 | |||
| 343 | =item Name: B<syntax> | ||
| 344 | |||
| 345 | =item Class: B<Syntax> | ||
| 346 | |||
| 347 | =item Switch: B<-syntax> | ||
| 348 | |||
| 349 | Default value 'None'. Sets and returns the currently | ||
| 350 | used syntax definition. | ||
| 351 | |||
| 352 | =item Name: B<tabBackground> | ||
| 353 | |||
| 354 | =item Class: B<TabBackground> | ||
| 355 | |||
| 356 | =item Switch: B<-tabbackground> | ||
| 357 | |||
| 358 | Default value #B5C200. The background color shown to leading and | ||
| 359 | trailing spaces when the B<-showspaces> option is set. | ||
| 360 | |||
| 361 | =item Switch: B<-themefile> | ||
| 362 | |||
| 363 | Default value undef. Sets and loads a theme file with tags information | ||
| 364 | for highlighting. A call to cget returns the name of the loaded theme file. | ||
| 365 | See also L<Tk::CodeText::Theme>. | ||
| 366 | |||
| 367 | =item Switch: B<-xmlfolder> | ||
| 368 | |||
| 369 | XML folder to use for L<Syntax::Kamelon> | ||
| 370 | |||
| 371 | Only available at create time. | ||
| 372 | |||
| 373 | =back | ||
| 374 | |||
| 375 | =cut | ||
| 376 | |||
| 377 | =head1 METHODS | ||
| 378 | |||
| 379 | =over 4 | ||
| 380 | |||
| 381 | =cut | ||
| 382 | |||
| 383 | sub Populate { | ||
| 384 | my ($self,$args) = @_; | ||
| 385 | |||
| 386 | my $scrollbars = delete $args->{'-scrollbars'}; | ||
| 387 | $scrollbars = 'soe' unless defined $scrollbars; | ||
| 388 | |||
| 389 | my $theme = delete $args->{'-theme'}; | ||
| 390 | unless (defined $theme) { | ||
| 391 | $theme = Tk::CodeText::Theme->new; | ||
| 392 | $theme->put(@defaultattributes); | ||
| 393 | } | ||
| 394 | |||
| 395 | my @ko = ( | ||
| 396 | formatter => ['Base', | ||
| 397 | foldingdepth => 'all', | ||
| 398 | ], | ||
| 399 | ); | ||
| 400 | my $xmldir = delete $args->{'-xmlfolder'}; | ||
| 401 | push @ko, 'xmlfolder', $xmldir if defined $xmldir; | ||
| 402 | |||
| 403 | $self->SUPER::Populate($args); | ||
| 404 | |||
| 405 | $self->{COLORINF} = []; | ||
| 406 | $self->{COLORED} = 1; | ||
| 407 | $self->{FOLDBUTTONS} = {}; | ||
| 408 | $self->{FOLDINF} = []; | ||
| 409 | $self->{FOLDSSHOWN} = []; | ||
| 410 | $self->{FOLDSVISIBLE} = 0; | ||
| 411 | $self->{KAMELON} = Tk::CodeText::Kamelon->new($self, @ko); | ||
| 412 | $self->{HIGHLIGHTINTERVAL} = 1; | ||
| 413 | $self->{LINESPERCYCLE} = 10; | ||
| 414 | $self->{LOOPACTIVE} = 0; | ||
| 415 | $self->{NOHIGHLIGHTING} = 1; | ||
| 416 | $self->{NUMBERSVISIBLE} = 0; | ||
| 417 | $self->{NUMBERINF} = []; | ||
| 418 | $self->{POSTCONFIG} = 0; | ||
| 419 | $self->{SHOWSPACES} = 0; | ||
| 420 | $self->{SPACESCOMPLETED} = 1; | ||
| 421 | $self->{SPACESLOOPACTIVE} = 0; | ||
| 422 | $self->{STATUSVISIBLE} = 0; | ||
| 423 | $self->{SYNTAX} = 'None'; | ||
| 424 | $self->{THEME} = $theme; | ||
| 425 | $self->{SAVEFIRSTVISIBLE} = 1; | ||
| 426 | $self->{SAVELASTVISIBLE} = 1; | ||
| 427 | |||
| 428 | #create editor frame | ||
| 429 | my $ef = $self->Frame( | ||
| 430 | -relief => 'sunken', | ||
| 431 | -borderwidth => 2, | ||
| 432 | )->pack( | ||
| 433 | -expand => 1, | ||
| 434 | -fill => 'both', | ||
| 435 | ); | ||
| 436 | |||
| 437 | #create the frame for the line numbers | ||
| 438 | my $numbers = $ef->Frame( | ||
| 439 | -width => 40, | ||
| 440 | ); | ||
| 441 | |||
| 442 | #create the frame for code folding | ||
| 443 | my $folds = $ef->Frame( | ||
| 444 | -width => 18, | ||
| 445 | ); | ||
| 446 | $folds->bind('<ButtonRelease-3>', [$self, 'foldsMenuPop', Ev('X'), Ev('Y')]); | ||
| 447 | my $fmenu = $folds->Menu( | ||
| 448 | -tearoff => 0, | ||
| 449 | -menuitems => [ | ||
| 450 | [command => 'Collapse All', -command => ['foldCollapseAll', $self]], | ||
| 451 | [command => 'Expand All', -command => ['foldExpandAll', $self]], | ||
| 452 | ], | ||
| 453 | ); | ||
| 454 | $fmenu->bind('<Leave>', [$fmenu, 'unpost']); | ||
| 455 | |||
| 456 | #create the textwidget | ||
| 457 | my @opt = ( | ||
| 458 | -escapepressed => ['FindClose', $self], | ||
| 459 | -findandreplacecall => sub { $self->FindAndOrReplace(@_) }, | ||
| 460 | -modifycall => ['OnModify', $self], | ||
| 461 | -relief => 'flat', | ||
| 462 | -scrollbars => $scrollbars, | ||
| 463 | -yscrollcmd => sub { | ||
| 464 | $self->foldsCheck; | ||
| 465 | $self->lnumberCheck; | ||
| 466 | $self->bookmarkCheck; | ||
| 467 | }, | ||
| 468 | ); | ||
| 469 | my $text; | ||
| 470 | if ($scrollbars eq '') { | ||
| 471 | $text = $ef->XText(@opt) | ||
| 472 | } else { | ||
| 473 | $text = $ef->Scrolled('XText', @opt) | ||
| 474 | } | ||
| 475 | $text->pack(-side => 'left', -expand =>1, -fill => 'both'); | ||
| 476 | |||
| 477 | #create the find and replace panel | ||
| 478 | my @pack = (-side => 'left', -padx => 2, -pady => 2); | ||
| 479 | my $sandr = $self->Frame; | ||
| 480 | $self->Advertise(SandR => $sandr); | ||
| 481 | |||
| 482 | #searchframe | ||
| 483 | my $case = '-case'; | ||
| 484 | my $find = ''; | ||
| 485 | my $reg = '-exact'; | ||
| 486 | my $replace = ''; | ||
| 487 | |||
| 488 | my $rframe; #the variable for the replaceframe must exist | ||
| 489 | my $sframe = $sandr->Frame->pack(-fill => 'x'); | ||
| 490 | $sframe->Label( | ||
| 491 | -anchor => 'e', | ||
| 492 | -text => 'Find', | ||
| 493 | -width => 7, | ||
| 494 | )->pack(@pack); | ||
| 495 | my $e = $sframe->Entry( | ||
| 496 | -textvariable => \$find, | ||
| 497 | )->pack(@pack, -expand => 1, -fill => 'x'); | ||
| 498 | $e->bind('<Escape>', [$self, 'FindClose']); | ||
| 499 | $e->bind('<Return>', sub { $self->FindNext('-forward', $reg, $case, $find) }); | ||
| 500 | $sframe->Button( | ||
| 501 | -text => 'Next', | ||
| 502 | -command => sub { $self->FindNext('-forward', $reg, $case, $find) }, | ||
| 503 | )->pack(@pack); | ||
| 504 | $sframe->Button( | ||
| 505 | -text => 'Previous', | ||
| 506 | -command => sub { $self->FindNext('-backward', $reg, $case, $find) }, | ||
| 507 | )->pack(@pack); | ||
| 508 | $sframe->Button( | ||
| 509 | -text => 'All', | ||
| 510 | -command => sub { $self->FindAll($reg, $case, $find) }, | ||
| 511 | )->pack(@pack); | ||
| 512 | $sframe->Button( | ||
| 513 | -text => 'Clear', | ||
| 514 | -command => sub { $self->FindClear }, | ||
| 515 | )->pack(@pack); | ||
| 516 | $sframe->Checkbutton( | ||
| 517 | -text => 'Case', | ||
| 518 | -onvalue => '-case', | ||
| 519 | -offvalue => '-nocase', | ||
| 520 | -variable => \$case, | ||
| 521 | )->pack(@pack); | ||
| 522 | $sframe->Checkbutton( | ||
| 523 | -text => 'Reg', | ||
| 524 | -onvalue => '-regexp', | ||
| 525 | -offvalue => '-exact', | ||
| 526 | -variable => \$reg, | ||
| 527 | )->pack(@pack); | ||
| 528 | $sframe->Button( | ||
| 529 | -text => 'Close', | ||
| 530 | -command => ['FindClose', $self], | ||
| 531 | )->pack(@pack); | ||
| 532 | |||
| 533 | #replaceframe | ||
| 534 | $rframe = $sandr->Frame; | ||
| 535 | $rframe->Label( | ||
| 536 | -anchor => 'e', | ||
| 537 | -text => 'Replace', | ||
| 538 | -width => 7, | ||
| 539 | )->pack(@pack); | ||
| 540 | $self->Advertise(Replace => $rframe); | ||
| 541 | my $r = $rframe->Entry( | ||
| 542 | -textvariable => \$replace, | ||
| 543 | )->pack(@pack, -expand => 1, -fill => 'x'); | ||
| 544 | $r->bind('<Escape>', [$self, 'FindClose']); | ||
| 545 | $r->bind('<Return>', sub { | ||
| 546 | $self->ReplaceSelectionsWith($replace) if $self->SelectionExists; | ||
| 547 | $self->FindNext('-forward', $reg, $case, $find); | ||
| 548 | }); | ||
| 549 | $rframe->Button( | ||
| 550 | -text => 'Replace', | ||
| 551 | -command => sub { $self->FindandReplace($reg, $case, $find, $replace) }, | ||
| 552 | )->pack(@pack); | ||
| 553 | $rframe->Button( | ||
| 554 | -text => 'Skip', | ||
| 555 | -command => sub { $self->FindNext('-forward', $reg, $case, $find) }, | ||
| 556 | )->pack(@pack); | ||
| 557 | $rframe->Button( | ||
| 558 | -text => 'Replace all', | ||
| 559 | -command => sub { $self->FindandReplaceAll($reg, $case, $find, $replace) }, | ||
| 560 | )->pack(@pack); | ||
| 561 | |||
| 562 | #create the statusbar | ||
| 563 | my $statusbar = $self->StatusBar( | ||
| 564 | -widget => $self, | ||
| 565 | ); | ||
| 566 | $self->after(10, ['updateStatus', $statusbar]); | ||
| 567 | #create progressbar for loading and saving | ||
| 568 | $self->Advertise(XText => $text); | ||
| 569 | $self->Advertise(Numbers => $numbers); | ||
| 570 | $self->Advertise(Folds => $folds); | ||
| 571 | $self->Advertise(Statusbar => $statusbar); | ||
| 572 | $self->Advertise(Foldsmenu => $fmenu); | ||
| 573 | $self->Advertise(FindEntry => $e); | ||
| 574 | |||
| 575 | # hack for getting proper bitmap foreground | ||
| 576 | my $l = $self->Label; | ||
| 577 | my $fg = $l->cget('-foreground'); | ||
| 578 | $l->destroy; | ||
| 579 | |||
| 580 | $text->tagConfigure('Hidden', -elide => 1); | ||
| 581 | $text->tagConfigure('Space', -background => 'blue'); | ||
| 582 | $text->tagConfigure('Tab', -background => 'blue'); | ||
| 583 | |||
| 584 | $self->ConfigSpecs( | ||
| 585 | -bookmarkcolor => [qw/PASSIVE bookmarkColor BookmarkColor/, '#71D0CC'], | ||
| 586 | -bookmarksize => [qw/PASSIVE bookmarkSize BookmarkSize/, 20], | ||
| 587 | -configdir => [qw/PASSIVE configdir ConfigDir/, ''], | ||
| 588 | -font => [qw/METHOD font Font/, 'Monospace 10'], | ||
| 589 | -highlightinterval => [qw/METHOD highlightInterval HighlightInterval/, 1], | ||
| 590 | -linespercycle => [qw/METHOD linesPerCycle LinesPerCycle/, 10], | ||
| 591 | -minusimg => ['PASSIVE', undef, undef, $self->Bitmap( | ||
| 592 | -data => $minusimg, | ||
| 593 | -foreground => $fg, | ||
| 594 | )], | ||
| 595 | -modifiedcall => ['CALLBACK', undef, undef, sub {}], | ||
| 596 | -plusimg => ['PASSIVE', undef, undef, $self->Bitmap( | ||
| 597 | -data => $plusimg, | ||
| 598 | -foreground => $fg, | ||
| 599 | )], | ||
| 600 | -position => ['METHOD'], | ||
| 601 | -saveimage => [$statusbar], | ||
| 602 | -showfolds => [qw/METHOD showFolds ShowFolds/, 1], | ||
| 603 | -shownumbers => [qw/METHOD showNumers ShowNumbers/, 1], | ||
| 604 | -showspaces => [qw/METHOD showSpaces ShowSpaces/, 0], | ||
| 605 | -showstatus => [qw/METHOD showStatus ShowStatus/, 1], | ||
| 606 | -syntax => [qw/METHOD syntax Syntax/, 'None'], | ||
| 607 | -statusinterval => [$statusbar], | ||
| 608 | -spacebackground => [qw/METHOD spaceBackground SpaceBackground/, '#0098C2'], | ||
| 609 | -tabbackground => [qw/METHOD tabBackground TabBackground/, '#B5C200'], | ||
| 610 | -themefile => ['METHOD'], | ||
| 611 | DEFAULT => [ $text ], | ||
| 612 | ); | ||
| 613 | |||
| 614 | $self->Delegates( | ||
| 615 | DEFAULT => $text, | ||
| 616 | ); | ||
| 617 | |||
| 618 | |||
| 619 | $self->after(10, sub { | ||
| 620 | $self->{POSTCONFIG} = 1; | ||
| 621 | $self->themeUpdate; | ||
| 622 | $self->lnumberCheck(1); | ||
| 623 | }); | ||
| 624 | } | ||
| 625 | |||
| 626 | =item B<bookmarkAdd>I<(?$line?)> | ||
| 627 | |||
| 628 | Bookmarks line number I<$line?>. If you do not specify I<$line?>, the line that has | ||
| 629 | the insert cursor is bookmarked. | ||
| 630 | |||
| 631 | =cut | ||
| 632 | |||
| 633 | sub bookmarkAdd { | ||
| 634 | my ($self, $line) = @_; | ||
| 635 | $line = $self->linenumber('insert') unless defined $line; | ||
| 636 | return if $self->bookmarked($line); | ||
| 637 | $self->tagAdd('BOOKMARK', "$line.0", "$line.0 lineend"); | ||
| 638 | } | ||
| 639 | |||
| 640 | sub bookmarkCheck { | ||
| 641 | my $self = shift; | ||
| 642 | my $numframe = $self->Subwidget('Numbers'); | ||
| 643 | my $nbg = $numframe->cget('-background'); | ||
| 644 | my $bbg = $self->cget('-bookmarkcolor'); | ||
| 645 | |||
| 646 | my $nimf = $self->{NUMBERINF}; | ||
| 647 | for (@$nimf) { | ||
| 648 | my $lab = $_; | ||
| 649 | my $line = $lab->cget('-text'); | ||
| 650 | if ($self->bookmarked($line)) { | ||
| 651 | $lab->configure('-background', $bbg); | ||
| 652 | } else { | ||
| 653 | $lab->configure('-background', $nbg); | ||
| 654 | } | ||
| 655 | } | ||
| 656 | } | ||
| 657 | |||
| 658 | =item B<bookmarked>I<($line)> | ||
| 659 | |||
| 660 | Returns true if line number I<$line?> is bookmarked. | ||
| 661 | |||
| 662 | =cut | ||
| 663 | |||
| 664 | sub bookmarked { | ||
| 665 | my ($self, $line) = @_; | ||
| 666 | my @range = $self->tagNextrange('BOOKMARK', "$line.0", "$line.0 lineend"); | ||
| 667 | return @range eq 2 | ||
| 668 | } | ||
| 669 | |||
| 670 | sub bookmarkGo { | ||
| 671 | my ($self, $line) = @_; | ||
| 672 | return unless $self->bookmarked($line); | ||
| 673 | $self->goTo("$line.0"); | ||
| 674 | } | ||
| 675 | |||
| 676 | =item B<bookmarkList> | ||
| 677 | |||
| 678 | Returns a list of all bookmarked line numbers in the text. | ||
| 679 | |||
| 680 | =cut | ||
| 681 | |||
| 682 | sub bookmarkList { | ||
| 683 | my $self = shift; | ||
| 684 | my @list = (); | ||
| 685 | my @ranges = $self->tagRanges('BOOKMARK'); | ||
| 686 | while (@ranges) { | ||
| 687 | my $begin = shift @ranges; | ||
| 688 | push @list, $self->linenumber($begin); | ||
| 689 | shift @ranges; | ||
| 690 | } | ||
| 691 | return @list | ||
| 692 | } | ||
| 693 | |||
| 694 | sub bookmarkMenuItems { | ||
| 695 | my $self = shift; | ||
| 696 | my @items = ( | ||
| 697 | [command => '~Add bookmark', | ||
| 698 | -command => ['bookmarkNew', $self], | ||
| 699 | ], | ||
| 700 | [command => '~Remove bookmark', | ||
| 701 | -command => ['bookmarkRemove', $self], | ||
| 702 | ], | ||
| 703 | [command => '~Remove all bookmarks', | ||
| 704 | -command => ['bookmarkRemoveAll', $self], | ||
| 705 | ], | ||
| 706 | [separator => ''], | ||
| 707 | [command => '~Next bookmark', | ||
| 708 | -command => ['bookmarkNext', $self], | ||
| 709 | ], | ||
| 710 | [command => '~Previous bookmark', | ||
| 711 | -command => ['bookmarkPrev', $self], | ||
| 712 | ], | ||
| 713 | [separator => ''], | ||
| 714 | ); | ||
| 715 | return @items | ||
| 716 | } | ||
| 717 | |||
| 718 | sub bookmarkMenuPop { | ||
| 719 | my ($self, $menu, $bmentry) = @_; | ||
| 720 | #find bookmark submenu | ||
| 721 | my $submenu; | ||
| 722 | for(0 ..$menu->index('end')) { | ||
| 723 | if ($menu->type($_) eq 'cascade') { | ||
| 724 | my $label = $menu->entrycget($_, '-label'); | ||
| 725 | if ($label eq $bmentry) { | ||
| 726 | $submenu = $menu->entrycget($_, '-menu'); | ||
| 727 | last; | ||
| 728 | } | ||
| 729 | } | ||
| 730 | } | ||
| 731 | |||
| 732 | #refresh the bookmarks menu | ||
| 733 | if (defined $submenu) { | ||
| 734 | #find first entry that is a bookmark | ||
| 735 | my $first; | ||
| 736 | for(0 ..$submenu->index('end')) { | ||
| 737 | next unless $submenu->type($_) eq 'command'; | ||
| 738 | my $label = $submenu->entrycget($_, '-label'); | ||
| 739 | if ($label =~ /^\d+/) { | ||
| 740 | $first = $_; | ||
| 741 | last; | ||
| 742 | } | ||
| 743 | } | ||
| 744 | #delete all bookmarks from the menu | ||
| 745 | $submenu->delete($first, 'end') if defined $first; | ||
| 746 | #add all current bookmarks | ||
| 747 | my @bookmarks = $self->bookmarkList; | ||
| 748 | for (@bookmarks) { | ||
| 749 | my $mark = $_; | ||
| 750 | $submenu->add('command', | ||
| 751 | -command => sub { $self->bookmarkGo($mark) }, | ||
| 752 | -label => "$mark - " . $self->bookmarkText($mark), | ||
| 753 | ); | ||
| 754 | } | ||
| 755 | } else { | ||
| 756 | warn "Submenu $bmentry not found" | ||
| 757 | } | ||
| 758 | } | ||
| 759 | |||
| 760 | =item B<bookmarkNew> | ||
| 761 | |||
| 762 | Same as B<bookmarkAdd> except it updates the visible bookmarks | ||
| 763 | in the line number column. | ||
| 764 | |||
| 765 | =cut | ||
| 766 | |||
| 767 | sub bookmarkNew { | ||
| 768 | my $self = shift; | ||
| 769 | $self->bookmarkAdd(@_); | ||
| 770 | $self->bookmarkCheck; | ||
| 771 | } | ||
| 772 | |||
| 773 | =item B<bookmarkNext>I<(?$line?)> | ||
| 774 | |||
| 775 | Jumps to the next bookmark relative to line number I<$line>. | ||
| 776 | If you do not specify I<$line?>, the jump is made from the insert cursor position. | ||
| 777 | |||
| 778 | =cut | ||
| 779 | |||
| 780 | sub bookmarkNext { | ||
| 781 | my ($self, $line) = @_; | ||
| 782 | $line = $self->linenumber('insert') unless defined $line; | ||
| 783 | my @list = $self->bookmarkList; | ||
| 784 | for (@list) { | ||
| 785 | my $next = $_; | ||
| 786 | if ($next > $line) { | ||
| 787 | $self->bookmarkGo($next); | ||
| 788 | return | ||
| 789 | } | ||
| 790 | } | ||
| 791 | } | ||
| 792 | |||
| 793 | =item B<bookmarkPrev>I<(?$line?)> | ||
| 794 | |||
| 795 | Jumps to the previous bookmark relative to line number I<$line?>. | ||
| 796 | If you do not specify I<$line?>, the jump is made from the insert cursor position. | ||
| 797 | |||
| 798 | =cut | ||
| 799 | |||
| 800 | sub bookmarkPrev { | ||
| 801 | my ($self, $line) = @_; | ||
| 802 | $line = $self->linenumber('insert') unless defined $line; | ||
| 803 | my @list = $self->bookmarkList; | ||
| 804 | for (reverse @list) { | ||
| 805 | my $prev = $_; | ||
| 806 | if ($prev < $line) { | ||
| 807 | $self->bookmarkGo($prev); | ||
| 808 | return | ||
| 809 | } | ||
| 810 | } | ||
| 811 | } | ||
| 812 | |||
| 813 | =item B<bookmarkRemove>I<(?$line?)> | ||
| 814 | |||
| 815 | Removes the bookmark at I<$line?>. | ||
| 816 | If you do not specify I<$line?>, the bookmark of the line that holds the insert cursor position is removed. | ||
| 817 | |||
| 818 | =cut | ||
| 819 | |||
| 820 | sub bookmarkRemove { | ||
| 821 | my $self = shift; | ||
| 822 | $self->bookmarkRemoveForce(@_); | ||
| 823 | $self->bookmarkCheck; | ||
| 824 | } | ||
| 825 | |||
| 826 | =item B<bookmarkRemoveAll> | ||
| 827 | |||
| 828 | Removes all bookmarks. | ||
| 829 | |||
| 830 | =cut | ||
| 831 | |||
| 832 | sub bookmarkRemoveAll { | ||
| 833 | my $self = shift; | ||
| 834 | my @list = $self->bookmarkList; | ||
| 835 | for (@list) { | ||
| 836 | $self->bookmarkRemove($_); | ||
| 837 | } | ||
| 838 | $self->bookmarkCheck; | ||
| 839 | } | ||
| 840 | |||
| 841 | sub bookmarkRemoveForce { | ||
| 842 | my ($self, $line) = @_; | ||
| 843 | $line = $self->linenumber('insert') unless defined $line; | ||
| 844 | return unless $self->bookmarked($line); | ||
| 845 | $self->tagRemove('BOOKMARK', "$line.0", "$line.0 lineend"); | ||
| 846 | } | ||
| 847 | |||
| 848 | sub bookmarkText { | ||
| 849 | my ($self, $line) = @_; | ||
| 850 | my $text = $self->get("$line.0", "$line.0 lineend"); | ||
| 851 | $text =~ s/^\s+//; #remove leading spaces | ||
| 852 | my $max = $self->cget('-bookmarksize'); | ||
| 853 | $text = substr($text, 0, $max) if length($text) > $max; | ||
| 854 | return $text | ||
| 855 | } | ||
| 856 | |||
| 857 | =item B<canUndo> | ||
| 858 | |||
| 859 | Returns true if the undo stack has content. | ||
| 860 | |||
| 861 | =cut | ||
| 862 | |||
| 863 | =item B<canRedo> | ||
| 864 | |||
| 865 | Returns true if the redo stack has content. | ||
| 866 | |||
| 867 | =cut | ||
| 868 | |||
| 869 | =item B<clear> | ||
| 870 | |||
| 871 | Delets all text. Clears the undo and redo stack. Clears the modified flag. | ||
| 872 | Resets hightlighting to syntax 'None' | ||
| 873 | |||
| 874 | =cut | ||
| 875 | |||
| 876 | sub clear { | ||
| 877 | my $self = shift; | ||
| 878 | $self->Subwidget('XText')->clear; | ||
| 879 | $self->Kamelon->Reset; | ||
| 880 | $self->configure(-syntax => 'None'); | ||
| 881 | } | ||
| 882 | |||
| 883 | sub Colored { | ||
| 884 | my $self = shift; | ||
| 885 | $self->{COLORED} = shift if @_; | ||
| 886 | return $self->{COLORED} | ||
| 887 | } | ||
| 888 | |||
| 889 | sub ColorInf { | ||
| 890 | my $self = shift; | ||
| 891 | $self->{COLORINF} = shift if @_; | ||
| 892 | return $self->{COLORINF} | ||
| 893 | } | ||
| 894 | |||
| 895 | =item B<comment> | ||
| 896 | |||
| 897 | Comments the current line or selection. | ||
| 898 | |||
| 899 | =cut | ||
| 900 | |||
| 901 | sub contentCheck { | ||
| 902 | my $self = shift; | ||
| 903 | $self->lnumberCheck; | ||
| 904 | # $self->foldsCheck; | ||
| 905 | $self->bookmarkCheck; | ||
| 906 | } | ||
| 907 | |||
| 908 | sub FindAndOrReplace { | ||
| 909 | my ($self, $flag) = @_; | ||
| 910 | my $geosave = $self->toplevel->geometry; | ||
| 911 | my $sandr = $self->Subwidget('SandR'); | ||
| 912 | if ($flag) { | ||
| 913 | $self->Subwidget('Replace')->packForget | ||
| 914 | } else { | ||
| 915 | $self->Subwidget('Replace')->pack( | ||
| 916 | -fill => 'x', | ||
| 917 | ); | ||
| 918 | } | ||
| 919 | my $sb = $self->Subwidget('Statusbar'); | ||
| 920 | my @op; | ||
| 921 | push @op, -before => $sb if $sb->ismapped; | ||
| 922 | $sandr->pack(@op, | ||
| 923 | -fill => 'x', | ||
| 924 | ); | ||
| 925 | $self->Subwidget('FindEntry')->focus; | ||
| 926 | $self->toplevel->geometry($geosave); | ||
| 927 | } | ||
| 928 | |||
| 929 | sub FindClose { | ||
| 930 | my $self = shift; | ||
| 931 | $self->FindClear; | ||
| 932 | $self->Subwidget('XText')->focus; | ||
| 933 | $self->Subwidget('SandR')->packForget; | ||
| 934 | $self->tagRemove('Find', '1.0', 'end'); | ||
| 935 | } | ||
| 936 | |||
| 937 | =item B<fixIndent> | ||
| 938 | |||
| 939 | Works on the entire text unless a selection is set. In that case works only on the selection. | ||
| 940 | Checks all lines in the working range for incorrect indentation and attempts to repair. | ||
| 941 | |||
| 942 | =cut | ||
| 943 | |||
| 944 | sub fixIndent { | ||
| 945 | my ($self, $icon) = @_; | ||
| 946 | my ($begin, $end) = $self->getRange; | ||
| 947 | |||
| 948 | #pop the dialog | ||
| 949 | my @padding = (-padx => 10, -pady => 10); | ||
| 950 | my $q = $self->{POPSPACESPERTABS}; | ||
| 951 | unless (defined $q) { | ||
| 952 | $q = $self->YADialog( | ||
| 953 | -title => 'Fix indent', | ||
| 954 | -buttons => [qw(Ok Cancel)], | ||
| 955 | ); | ||
| 956 | $q->Label(-image => $icon)->pack(-side => 'left', @padding) if defined $icon; | ||
| 957 | my $f = $q->Frame->pack(-side => 'left', @padding); | ||
| 958 | $f->Label( | ||
| 959 | -anchor => 'w', | ||
| 960 | -text => 'Spaces per tab', | ||
| 961 | )->pack(-fill => 'x', -padx => 2, -pady => 2); | ||
| 962 | my $e = $f->Entry->pack(-fill => 'x', -padx => 2, -pady => 2); | ||
| 963 | $self->{POPENTRY} = $e; | ||
| 964 | $e->focus; | ||
| 965 | $e->bind('<Return>', sub { | ||
| 966 | $q->{PRESSED} = 'Ok' | ||
| 967 | }); | ||
| 968 | $self->{POPSPACESPERTABS} = $q; | ||
| 969 | } | ||
| 970 | |||
| 971 | my $answer = $q->Show(-popover => $self->toplevel); | ||
| 972 | if ($answer eq 'Ok') { | ||
| 973 | my $spaces_per_tab = $self->{POPENTRY}->get; | ||
| 974 | return unless $spaces_per_tab =~ /^\d+$/; | ||
| 975 | |||
| 976 | for ($begin .. $end) { | ||
| 977 | my $line = $_; | ||
| 978 | my $b = $self->index("$line.0"); | ||
| 979 | my $e = $self->index("$line.0 lineend"); | ||
| 980 | my $text = $self->get($b, $e); | ||
| 981 | |||
| 982 | if ($text =~ /^([\s|\t]+)/) { | ||
| 983 | my $spaces = $1; | ||
| 984 | my $s = 0; | ||
| 985 | my $pos = 0; | ||
| 986 | my $itext = ''; | ||
| 987 | $itext = "$itext " while length($itext) ne $spaces_per_tab; | ||
| 988 | while ($spaces ne '') { | ||
| 989 | my $char = substr $spaces, 0, 1, ''; | ||
| 990 | if ($self->cget('-indentstyle') eq 'tab') { | ||
| 991 | if ($char eq "\t") { | ||
| 992 | $s = 0; | ||
| 993 | } else { | ||
| 994 | $s ++; | ||
| 995 | if ($s eq $spaces_per_tab) { | ||
| 996 | my $linepos = $self->index("$b + $pos c"); | ||
| 997 | $self->replace($linepos, "$linepos + $s c", "\t"); | ||
| 998 | $s = 0; | ||
| 999 | $pos ++; | ||
| 1000 | } | ||
| 1001 | } | ||
| 1002 | } else { | ||
| 1003 | if ($char eq "\t") { | ||
| 1004 | my $linepos = $self->index("$b + $pos c"); | ||
| 1005 | $self->replace($linepos, "$linepos + 1 c", $itext); | ||
| 1006 | $pos = $pos + length($itext) - 1; | ||
| 1007 | } | ||
| 1008 | $pos ++ | ||
| 1009 | } | ||
| 1010 | } | ||
| 1011 | } | ||
| 1012 | } | ||
| 1013 | } | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | sub foldButton { | ||
| 1017 | my ($self, $line) = @_; | ||
| 1018 | my $folds = $self->Kamelon->Formatter->Folds; | ||
| 1019 | my $fbuttons = $self->FoldButtons; | ||
| 1020 | unless (exists $fbuttons->{$line}) { | ||
| 1021 | my $data = $folds->{$line}; | ||
| 1022 | my @opt = (); | ||
| 1023 | my $state; | ||
| 1024 | if ($self->isHidden($line + 1)) { | ||
| 1025 | push @opt, -image => $self->cget('-plusimg'); | ||
| 1026 | $state = 'collapsed'; | ||
| 1027 | } else { | ||
| 1028 | push @opt, -image => $self->cget('-minusimg'); | ||
| 1029 | $state = 'expanded'; | ||
| 1030 | } | ||
| 1031 | my $b = $self->Subwidget('Folds')->Button(@opt, | ||
| 1032 | -command => ['foldFlip', $self, $line], | ||
| 1033 | -relief => 'flat', | ||
| 1034 | ); | ||
| 1035 | $fbuttons->{$line} = { | ||
| 1036 | button => $b, | ||
| 1037 | data => $data, | ||
| 1038 | state => $state, | ||
| 1039 | }; | ||
| 1040 | } | ||
| 1041 | return $fbuttons->{$line}; | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | sub FoldButtons { | ||
| 1045 | my $self = shift; | ||
| 1046 | $self->{FOLDBUTTONS} = shift if @_; | ||
| 1047 | return $self->{FOLDBUTTONS} | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | sub foldCollapse { | ||
| 1051 | my ($self, $line) = @_; | ||
| 1052 | my $data = $self->FoldButtons->{$line}; | ||
| 1053 | $data->{'state'} = 'collapsed'; | ||
| 1054 | $data->{'button'}->configure(-image => $self->cget('-plusimg')); | ||
| 1055 | my $end = $data->{'data'}->{'end'}; | ||
| 1056 | $line ++; | ||
| 1057 | while ($line <= $end) { | ||
| 1058 | $self->hideLine($line); | ||
| 1059 | $line ++; | ||
| 1060 | } | ||
| 1061 | $self->lnumberCheck; | ||
| 1062 | $self->foldsCheck; | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | =item B<foldCollapseAll> | ||
| 1066 | |||
| 1067 | Collapses all folding points. | ||
| 1068 | |||
| 1069 | =cut | ||
| 1070 | |||
| 1071 | sub foldCollapseAll { | ||
| 1072 | my $self = shift; | ||
| 1073 | my $folds = $self->Kamelon->Formatter->Folds; | ||
| 1074 | for (sort keys %$folds) { | ||
| 1075 | $self->foldButton($_); #just make sure a fold button exists | ||
| 1076 | $self->foldCollapse($_); | ||
| 1077 | } | ||
| 1078 | } | ||
| 1079 | |||
| 1080 | sub foldExpand { | ||
| 1081 | my ($self, $line) = @_; | ||
| 1082 | my $data = $self->FoldButtons->{$line}; | ||
| 1083 | $data->{'state'} = 'expanded'; | ||
| 1084 | $data->{'button'}->configure(-image => $self->cget('-minusimg')); | ||
| 1085 | $self->lnumberCheck; | ||
| 1086 | my $end = $data->{'data'}->{'end'}; | ||
| 1087 | $line ++; | ||
| 1088 | while ($line <= $end) { | ||
| 1089 | $self->showLine($line); | ||
| 1090 | my $nested = $self->FoldButtons->{$line}; | ||
| 1091 | if (defined $nested) { | ||
| 1092 | $self->foldExpand($line) unless ($nested->{'state'} eq 'collapsed'); | ||
| 1093 | $line = $nested->{'data'}->{'end'}; | ||
| 1094 | $self->showLine($line); | ||
| 1095 | } else { | ||
| 1096 | $line ++ | ||
| 1097 | } | ||
| 1098 | } | ||
| 1099 | $self->foldsCheck; | ||
| 1100 | } | ||
| 1101 | |||
| 1102 | =item B<foldExpandAll> | ||
| 1103 | |||
| 1104 | Expands all folding points. | ||
| 1105 | |||
| 1106 | =cut | ||
| 1107 | |||
| 1108 | sub foldExpandAll { | ||
| 1109 | my $self = shift; | ||
| 1110 | my $folds = $self->Kamelon->Formatter->Folds; | ||
| 1111 | for (sort keys %$folds) { | ||
| 1112 | $self->foldButton($_); #just make sure a fold button exists | ||
| 1113 | $self->foldExpand($_); | ||
| 1114 | } | ||
| 1115 | } | ||
| 1116 | |||
| 1117 | sub foldFlip { | ||
| 1118 | my ($self, $line) = @_; | ||
| 1119 | my $data = $self->FoldButtons->{$line}; | ||
| 1120 | if ($data->{'state'} eq 'collapsed') { | ||
| 1121 | $self->foldExpand($line); | ||
| 1122 | } elsif ($data->{'state'} eq 'expanded') { | ||
| 1123 | $self->foldCollapse($line); | ||
| 1124 | } | ||
| 1125 | } | ||
| 1126 | |||
| 1127 | sub foldsCheck { | ||
| 1128 | my $self = shift; | ||
| 1129 | return unless $self->cget('-showfolds'); | ||
| 1130 | |||
| 1131 | my $line = $self->visualBegin; | ||
| 1132 | my $last = $self->visualEnd; | ||
| 1133 | |||
| 1134 | return if $self->Colored < $last; | ||
| 1135 | |||
| 1136 | my $folds = $self->Kamelon->Formatter->Folds; | ||
| 1137 | my $fframe = $self->Subwidget('Folds'); | ||
| 1138 | my $btns = $self->FoldButtons; | ||
| 1139 | |||
| 1140 | #clear out out of sight fold buttons | ||
| 1141 | for (keys %$btns) { | ||
| 1142 | if (($_ < $self->visualBegin) or ($_ > $self->visualEnd)) { | ||
| 1143 | my $b = delete $btns->{$_}; | ||
| 1144 | $b->{'button'}->destroy; | ||
| 1145 | } | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | my @shown = (); | ||
| 1149 | while ($line <= $last) { | ||
| 1150 | while ($self->isHidden($line)) { $line ++ } | ||
| 1151 | if (exists $folds->{$line}) { | ||
| 1152 | #vertical alignment with the line | ||
| 1153 | my ( $x, $y, $wi, $he ) = $self->dlineinfo("$line.0"); | ||
| 1154 | my $but = $self->foldButton($line)->{'button'}; | ||
| 1155 | my $bh = $but->reqheight; | ||
| 1156 | my $delta = int(($he - $bh) / 2); | ||
| 1157 | $but->place(-x => 0, -y => $y + $delta); | ||
| 1158 | push @shown, $but; | ||
| 1159 | } elsif (exists $btns->{$line}) { | ||
| 1160 | my $b = delete $btns->{$line}; | ||
| 1161 | $b->{'button'}->destroy; | ||
| 1162 | } else { | ||
| 1163 | } | ||
| 1164 | $line ++; | ||
| 1165 | } | ||
| 1166 | $self->{FOLDSSHOWN} = \@shown; | ||
| 1167 | } | ||
| 1168 | |||
| 1169 | sub foldsClear { | ||
| 1170 | my $self = shift; | ||
| 1171 | my $shown = $self->{FOLDSSHOWN}; | ||
| 1172 | for (@$shown) { | ||
| 1173 | $_->placeForget; | ||
| 1174 | } | ||
| 1175 | $self->{FOLDSSHOWN} = []; | ||
| 1176 | } | ||
| 1177 | |||
| 1178 | sub foldsMenuPop { | ||
| 1179 | my ($self, $x, $y) = @_; | ||
| 1180 | $self->Subwidget('Foldsmenu')->post($x - 2, $y - 2); | ||
| 1181 | } | ||
| 1182 | |||
| 1183 | sub font { | ||
| 1184 | my $self = shift; | ||
| 1185 | my $xt = $self->Subwidget('XText'); | ||
| 1186 | if (@_) { | ||
| 1187 | my $new = shift; | ||
| 1188 | $xt->configure('-font', $new); | ||
| 1189 | my $inf = $self->{NUMBERINF}; | ||
| 1190 | for (@$inf) { | ||
| 1191 | $_->configure('-font', $new); | ||
| 1192 | } | ||
| 1193 | $self->update if $self->{POSTCONFIG}; | ||
| 1194 | $self->lnumberCheck(1); | ||
| 1195 | } | ||
| 1196 | return $xt->cget('-font'); | ||
| 1197 | } | ||
| 1198 | |||
| 1199 | |||
| 1200 | |||
| 1201 | =item B<fontCompose>I<($font, %options)> | ||
| 1202 | |||
| 1203 | Returns a new font based on $font. | ||
| 1204 | The keys -family -size -weight -slant are supported | ||
| 1205 | |||
| 1206 | =cut | ||
| 1207 | |||
| 1208 | sub fontCompose { | ||
| 1209 | my ($self, $font, %options) = @_; | ||
| 1210 | my $family = $self->fontActual($font, '-family'); | ||
| 1211 | my $size = $self->fontActual($font, '-size'); | ||
| 1212 | my $weight = ''; | ||
| 1213 | my $slant = ''; | ||
| 1214 | $slant = $options{'-slant'} if exists $options{'-slant'}; | ||
| 1215 | $weight = $options{'-weight'} if exists $options{'-weight'}; | ||
| 1216 | $slant = 'roman' if $slant eq ''; | ||
| 1217 | $weight = 'normal' if $weight eq ''; | ||
| 1218 | return $self->Font( | ||
| 1219 | -family => $family, | ||
| 1220 | -size => $size, | ||
| 1221 | -slant => $slant, | ||
| 1222 | -weight => $weight, | ||
| 1223 | ); | ||
| 1224 | } | ||
| 1225 | |||
| 1226 | =item B<getFontInfo> | ||
| 1227 | |||
| 1228 | Returns info about the font used in the text widget. | ||
| 1229 | The info is a hash with keys -family -size -weight -slant -underline -overstrike. | ||
| 1230 | |||
| 1231 | =cut | ||
| 1232 | |||
| 1233 | =item B<getRange> | ||
| 1234 | |||
| 1235 | Checks for a selection and returns the line numbers of the begin and the end. If no selection is set | ||
| 1236 | it returns 1 and the line number of the last line. | ||
| 1237 | |||
| 1238 | =cut | ||
| 1239 | |||
| 1240 | sub getRange { | ||
| 1241 | my $self = shift; | ||
| 1242 | my $begin = 1; | ||
| 1243 | my $end = $self->linenumber('end - 1c'); | ||
| 1244 | my @sel = $self->tagRanges('sel'); | ||
| 1245 | if (@sel) { | ||
| 1246 | $begin = $self->linenumber(shift @sel); | ||
| 1247 | $end = $self->linenumber(shift @sel); | ||
| 1248 | } | ||
| 1249 | return ($begin, $end); | ||
| 1250 | } | ||
| 1251 | |||
| 1252 | |||
| 1253 | =item B<goTo>I<($index)> | ||
| 1254 | |||
| 1255 | Sets the insert cursor to $index. | ||
| 1256 | |||
| 1257 | =cut | ||
| 1258 | |||
| 1259 | sub hideLine { | ||
| 1260 | my ($self, $line) = @_; | ||
| 1261 | $self->tagAdd('Hidden', "$line.0", "$line.0 lineend + 1c"); | ||
| 1262 | } | ||
| 1263 | |||
| 1264 | sub highlightCheck { | ||
| 1265 | my ($self, $pos) = @_; | ||
| 1266 | return if $self->NoHighlighting; | ||
| 1267 | my $line = $self->linenumber($pos); | ||
| 1268 | my $colored = $self->Colored; | ||
| 1269 | $self->highlightPurge($line) if $line <= $self->Colored; | ||
| 1270 | } | ||
| 1271 | |||
| 1272 | sub highlightinterval { | ||
| 1273 | my $self = shift; | ||
| 1274 | $self->{HIGHLIGHTINTERVAL} = shift if @_; | ||
| 1275 | return $self->{HIGHLIGHTINTERVAL} | ||
| 1276 | } | ||
| 1277 | |||
| 1278 | sub highlightLine { | ||
| 1279 | my ($self, $num) = @_; | ||
| 1280 | my $kam = $self->Kamelon; | ||
| 1281 | $kam->LineNumber($num); | ||
| 1282 | my $xt = $self->Subwidget('XText'); | ||
| 1283 | my $begin = "$num.0"; | ||
| 1284 | my $end = $xt->index("$num.0 lineend + 1c"); | ||
| 1285 | my $cli = $self->ColorInf; | ||
| 1286 | my $k = $cli->[$num - 1]; | ||
| 1287 | $kam->StateSet(@$k); | ||
| 1288 | my $txt = $xt->get($begin, $end); #get the text to be highlighted | ||
| 1289 | if ($txt ne '') { #if the line is not empty | ||
| 1290 | my $pos = 0; | ||
| 1291 | my $start = 0; | ||
| 1292 | my @h = $kam->ParseRaw($txt); | ||
| 1293 | while (@h ne 0) { | ||
| 1294 | $start = $pos; | ||
| 1295 | $pos += length(shift @h); | ||
| 1296 | my $tag = shift @h; | ||
| 1297 | $xt->tagAdd($tag, "$num.$start", "$num.$pos"); | ||
| 1298 | }; | ||
| 1299 | $xt->tagRaise('Find'); | ||
| 1300 | $xt->tagRaise('Space'); | ||
| 1301 | $xt->tagRaise('Tab'); | ||
| 1302 | $xt->tagRaise('sel'); | ||
| 1303 | }; | ||
| 1304 | $cli->[$num] = [ $kam->StateGet ]; | ||
| 1305 | # $self->foldsCheck if $num eq $self->visualEnd; | ||
| 1306 | } | ||
| 1307 | |||
| 1308 | sub highlightLoop { | ||
| 1309 | my $self = shift; | ||
| 1310 | if ($self->NoHighlighting) { | ||
| 1311 | $self->LoopActive(0); | ||
| 1312 | return | ||
| 1313 | } | ||
| 1314 | my $xt = $self->Subwidget('XText'); | ||
| 1315 | my $lpc = $self->cget('-linespercycle'); | ||
| 1316 | my $colored = $self->Colored; | ||
| 1317 | $self->highlightRemove($colored, $colored + $lpc); | ||
| 1318 | for (1 .. $lpc) { | ||
| 1319 | my $colored = $self->Colored; | ||
| 1320 | if ($colored <= $xt->linenumber('end - 1c')) { | ||
| 1321 | $self->LoopActive(1); | ||
| 1322 | $self->highlightLine($colored); | ||
| 1323 | $colored ++; | ||
| 1324 | $self->Colored($colored); | ||
| 1325 | } else { | ||
| 1326 | $self->after(100, ['foldsCheck', $self]); | ||
| 1327 | $self->LoopActive(0); | ||
| 1328 | } | ||
| 1329 | last unless $self->LoopActive; | ||
| 1330 | } | ||
| 1331 | $self->after($self->highlightinterval, ['highlightLoop', $self]) if $self->LoopActive; | ||
| 1332 | } | ||
| 1333 | |||
| 1334 | sub highlightPurge { | ||
| 1335 | my ($self, $line, $remove) = @_; | ||
| 1336 | $line = 1 unless defined $line; | ||
| 1337 | $remove = 0 unless defined $remove; | ||
| 1338 | |||
| 1339 | #purge highlightinfo | ||
| 1340 | $self->highlightRemove($line) if $remove; | ||
| 1341 | $self->Colored($line); | ||
| 1342 | my $cli = $self->ColorInf; | ||
| 1343 | if (@$cli) { splice(@$cli, $line) }; | ||
| 1344 | |||
| 1345 | #purge folds | ||
| 1346 | my $folds = $self->Kamelon->Formatter->Folds; | ||
| 1347 | for (keys %$folds) { | ||
| 1348 | delete $folds->{$_} if $_ >= $line | ||
| 1349 | } | ||
| 1350 | |||
| 1351 | $self->highlightLoop unless $self->LoopActive; | ||
| 1352 | } | ||
| 1353 | |||
| 1354 | sub highlightRemove { | ||
| 1355 | my ($self, $begin, $end) = @_; | ||
| 1356 | $begin = 1 unless defined $begin; | ||
| 1357 | $end = $self->linenumber('end') unless defined $end; | ||
| 1358 | $begin = "$begin.0"; | ||
| 1359 | $end = $self->index("$end.0 lineend"); | ||
| 1360 | |||
| 1361 | for ($self->tags) { | ||
| 1362 | $self->tagRemove($_, $begin, $end) | ||
| 1363 | } | ||
| 1364 | } | ||
| 1365 | |||
| 1366 | =item B<indent> | ||
| 1367 | |||
| 1368 | Indents the current line or selection. | ||
| 1369 | |||
| 1370 | =item B<isHidden>I<($line)> | ||
| 1371 | |||
| 1372 | Returns true if $line is hidden by a colde fold. | ||
| 1373 | |||
| 1374 | =cut | ||
| 1375 | |||
| 1376 | sub isHidden { | ||
| 1377 | my ($self, $line) = @_; | ||
| 1378 | my @names = $self->tagNames("$line.0"); | ||
| 1379 | my $hit = grep({ $_ eq 'Hidden'} @names); | ||
| 1380 | return $hit; | ||
| 1381 | } | ||
| 1382 | |||
| 1383 | sub Kamelon { | ||
| 1384 | return $_[0]->{KAMELON} | ||
| 1385 | } | ||
| 1386 | |||
| 1387 | =item B<linenumber>I<($index)> | ||
| 1388 | |||
| 1389 | Returns the line number of $index. | ||
| 1390 | |||
| 1391 | =cut | ||
| 1392 | |||
| 1393 | sub linespercycle { | ||
| 1394 | my $self = shift; | ||
| 1395 | $self->{LINESPERCYCLE} = shift if @_; | ||
| 1396 | return $self->{LINESPERCYCLE} | ||
| 1397 | } | ||
| 1398 | |||
| 1399 | |||
| 1400 | =item B<lineVisible>I<($line)> | ||
| 1401 | |||
| 1402 | Returns true if line $line is visible on the display. | ||
| 1403 | |||
| 1404 | =cut | ||
| 1405 | |||
| 1406 | sub lineVisible { | ||
| 1407 | my ($self, $line) = @_; | ||
| 1408 | my $first = $self->visualBegin; | ||
| 1409 | my $last = $self->visualEnd; | ||
| 1410 | return (($line >= $first) and ($line <= $last)) | ||
| 1411 | } | ||
| 1412 | |||
| 1413 | sub lnumberCheck { | ||
| 1414 | my ($self, $force) = @_; | ||
| 1415 | $force = 0 unless defined $force; | ||
| 1416 | |||
| 1417 | my $line = $self->visualBegin; | ||
| 1418 | my $last = $self->visualEnd; | ||
| 1419 | |||
| 1420 | my $sb = $self->SaveFirstVisible; | ||
| 1421 | my $se = $self->SaveLastVisible; | ||
| 1422 | |||
| 1423 | unless ($force) { | ||
| 1424 | return if ($sb eq $line) and ($last eq $se); | ||
| 1425 | } | ||
| 1426 | return unless $self->{POSTCONFIG}; | ||
| 1427 | return unless $self->cget('-shownumbers'); | ||
| 1428 | |||
| 1429 | $self->SaveFirstVisible($line); | ||
| 1430 | $self->SaveLastVisible($last); | ||
| 1431 | my $widget = $self->Subwidget('XText'); | ||
| 1432 | my $count = 0; | ||
| 1433 | my $font = $widget->cget('-font'); | ||
| 1434 | |||
| 1435 | my $nimf = $self->{NUMBERINF}; | ||
| 1436 | my $numframe = $self->Subwidget('Numbers'); | ||
| 1437 | |||
| 1438 | while ($line <= $last) { | ||
| 1439 | while ($self->isHidden($line)) { $line ++ } | ||
| 1440 | my ( $x, $y, $wi, $he ) = $self->dlineinfo("$line.0"); | ||
| 1441 | |||
| 1442 | #create a number label if it does not yet exist; | ||
| 1443 | unless (defined $nimf->[$count]) { | ||
| 1444 | my $l = $numframe->Label( | ||
| 1445 | -justify => 'right', | ||
| 1446 | -anchor => 'ne', | ||
| 1447 | -font => $font, | ||
| 1448 | -borderwidth => 0, | ||
| 1449 | ); | ||
| 1450 | push @$nimf, $l; | ||
| 1451 | } | ||
| 1452 | |||
| 1453 | #configure and position the number label | ||
| 1454 | my $lab = $nimf->[$count]; | ||
| 1455 | $lab->configure( | ||
| 1456 | -text => $line, | ||
| 1457 | -width => length($last), | ||
| 1458 | ); | ||
| 1459 | $lab->placeForget if $lab->ismapped; | ||
| 1460 | $lab->place(-x => 0, -y => $y); | ||
| 1461 | $line ++; | ||
| 1462 | $count ++; | ||
| 1463 | } | ||
| 1464 | |||
| 1465 | my $numwidth = $nimf->[$count - 1]->reqwidth; | ||
| 1466 | $numframe->configure(-width => $numwidth); | ||
| 1467 | |||
| 1468 | #remove redundant nummber labels | ||
| 1469 | while (defined $nimf->[$count]) { | ||
| 1470 | my $l = pop @$nimf; | ||
| 1471 | $l->placeForget; | ||
| 1472 | $l->destroy; | ||
| 1473 | } | ||
| 1474 | } | ||
| 1475 | |||
| 1476 | =item B<load>I<($file)> | ||
| 1477 | |||
| 1478 | Clears the text widget and loads $file. | ||
| 1479 | Returns 1 if successfull. | ||
| 1480 | |||
| 1481 | =cut | ||
| 1482 | |||
| 1483 | sub load{ | ||
| 1484 | my ($self, $file) = @_; | ||
| 1485 | if ($self->Subwidget('XText')->load($file)) { | ||
| 1486 | my $syntax = $self->Kamelon->SuggestSyntax($file); | ||
| 1487 | $self->configure(-syntax => $syntax) if defined $syntax; | ||
| 1488 | $self->after(500, ['contentCheck', $self]); | ||
| 1489 | return 1 | ||
| 1490 | } | ||
| 1491 | return 0 | ||
| 1492 | } | ||
| 1493 | |||
| 1494 | sub LoopActive { | ||
| 1495 | my $self = shift; | ||
| 1496 | $self->{LOOPACTIVE} = shift if @_; | ||
| 1497 | return $self->{LOOPACTIVE} | ||
| 1498 | } | ||
| 1499 | |||
| 1500 | sub NoHighlighting { | ||
| 1501 | my $self = shift; | ||
| 1502 | $self->{NOHIGHLIGHTING} = shift if @_; | ||
| 1503 | return $self->{NOHIGHLIGHTING} | ||
| 1504 | } | ||
| 1505 | |||
| 1506 | sub OnModify { | ||
| 1507 | my ($self, $index) = @_; | ||
| 1508 | $self->highlightCheck($index); | ||
| 1509 | $self->bookmarkCheck; | ||
| 1510 | $self->spacesCheck($index); | ||
| 1511 | $self->Callback('-modifiedcall', $index); | ||
| 1512 | } | ||
| 1513 | |||
| 1514 | =item B<patchdiff>I($patch, ?$style?) | ||
| 1515 | |||
| 1516 | Applies a patch to the text. I<$patch> can either be | ||
| 1517 | a refrence to a string or a filen name. | ||
| 1518 | |||
| 1519 | I<$style> is optional. By default it is set to 'Unified'. | ||
| 1520 | |||
| 1521 | The operation registers as a single undo event. See also L<Text::Patch>. | ||
| 1522 | |||
| 1523 | =cut | ||
| 1524 | |||
| 1525 | sub position { | ||
| 1526 | my ($self, $pos) = @_; | ||
| 1527 | if (defined $pos) { | ||
| 1528 | $self->goTo($pos); | ||
| 1529 | $self->see($pos); | ||
| 1530 | } | ||
| 1531 | return $self->index('insert'); | ||
| 1532 | } | ||
| 1533 | |||
| 1534 | =item B<redo> | ||
| 1535 | |||
| 1536 | Redoes the last undo. | ||
| 1537 | |||
| 1538 | =item B<replace>I<$begin, $end, $string)> | ||
| 1539 | |||
| 1540 | Replaces the text from index I<$begin> to I<$end> with the text in I<$string>. | ||
| 1541 | Counts for one event in the undo stack. | ||
| 1542 | |||
| 1543 | =cut | ||
| 1544 | |||
| 1545 | =item B<removeTrailingSpaces> | ||
| 1546 | |||
| 1547 | Works on the entire text unless a selection is set. In that case works only on the selection. | ||
| 1548 | Checks all lines in the working range for trailing spaces or tabs and removes them. | ||
| 1549 | |||
| 1550 | =cut | ||
| 1551 | |||
| 1552 | sub removeTrailingSpaces { | ||
| 1553 | my $self = shift; | ||
| 1554 | my ($begin, $end) = $self->getRange; | ||
| 1555 | for ($begin .. $end) { | ||
| 1556 | my $line = $_; | ||
| 1557 | my $b = $self->index("$line.0"); | ||
| 1558 | my $e = $self->index("$line.0 lineend"); | ||
| 1559 | my $text = $self->get($b, $e); | ||
| 1560 | if ($text =~ /(\s+)$/) { | ||
| 1561 | my $spaces = $1; | ||
| 1562 | my $l = length($spaces); | ||
| 1563 | $self->delete("$e - $l c", $e); | ||
| 1564 | } | ||
| 1565 | } | ||
| 1566 | } | ||
| 1567 | |||
| 1568 | =item B<save>I<($file)> | ||
| 1569 | |||
| 1570 | Saves the text into $file. Returns 1 if successfull. | ||
| 1571 | |||
| 1572 | =item B<saveExport>I<($file)> | ||
| 1573 | |||
| 1574 | Same as save, except it does not clear the modified flag. | ||
| 1575 | |||
| 1576 | =cut | ||
| 1577 | |||
| 1578 | sub SaveFirstVisible { | ||
| 1579 | my $self = shift; | ||
| 1580 | $self->{SAVEFIRSTVISIBLE} = shift if @_; | ||
| 1581 | return $self->{SAVEFIRSTVISIBLE} | ||
| 1582 | } | ||
| 1583 | |||
| 1584 | sub SaveLastVisible { | ||
| 1585 | my $self = shift; | ||
| 1586 | $self->{SAVELASTVISIBLE} = shift if @_; | ||
| 1587 | return $self->{SAVELASTVISIBLE} | ||
| 1588 | } | ||
| 1589 | |||
| 1590 | =item B<selectionExists> | ||
| 1591 | |||
| 1592 | Returns true if a selection exists | ||
| 1593 | |||
| 1594 | =cut | ||
| 1595 | |||
| 1596 | sub showfolds { | ||
| 1597 | my ($self, $flag) = @_; | ||
| 1598 | my $f = $self->Subwidget('Folds'); | ||
| 1599 | if (defined $flag) { | ||
| 1600 | if ($flag) { | ||
| 1601 | my $before = $self->Subwidget('XText'); | ||
| 1602 | $f->pack( | ||
| 1603 | -side => 'left', | ||
| 1604 | -before => $before, | ||
| 1605 | -fill => 'y', | ||
| 1606 | ); | ||
| 1607 | $self->{FOLDSVISIBLE} = 1; | ||
| 1608 | $self->foldsCheck; | ||
| 1609 | } else { | ||
| 1610 | $self->{FOLDSVISIBLE} = 0; | ||
| 1611 | $f->packForget; | ||
| 1612 | } | ||
| 1613 | } | ||
| 1614 | return $self->{FOLDSVISIBLE} | ||
| 1615 | } | ||
| 1616 | |||
| 1617 | sub showLine { | ||
| 1618 | my ($self, $line) = @_; | ||
| 1619 | $self->tagRemove('Hidden', "$line.0", "$line.0 lineend + 1c"); | ||
| 1620 | } | ||
| 1621 | |||
| 1622 | sub shownumbers { | ||
| 1623 | my ($self, $flag) = @_; | ||
| 1624 | my $f = $self->Subwidget('Numbers'); | ||
| 1625 | if (defined $flag) { | ||
| 1626 | if ($flag) { | ||
| 1627 | my $before = $self->Subwidget('XText'); | ||
| 1628 | $before = $self->Subwidget('Folds') if $self->{FOLDSVISIBLE}; | ||
| 1629 | $f->pack( | ||
| 1630 | -side => 'left', | ||
| 1631 | -before => $before, | ||
| 1632 | -fill => 'y', | ||
| 1633 | ); | ||
| 1634 | $self->{NUMBERSVISIBLE} = 1; | ||
| 1635 | $self->lnumberCheck; | ||
| 1636 | } else { | ||
| 1637 | $f->packForget; | ||
| 1638 | $self->{NUMBERSVISIBLE} = 0; | ||
| 1639 | } | ||
| 1640 | } | ||
| 1641 | return $self->{NUMBERSVISIBLE} | ||
| 1642 | } | ||
| 1643 | |||
| 1644 | sub showspaces { | ||
| 1645 | my $self = shift; | ||
| 1646 | if (@_) { | ||
| 1647 | $self->{SHOWSPACES} = shift; | ||
| 1648 | $self->spacesPurge(1, 1) if $self->{POSTCONFIG}; | ||
| 1649 | } | ||
| 1650 | return $self->{SHOWSPACES} | ||
| 1651 | } | ||
| 1652 | |||
| 1653 | sub showstatus { | ||
| 1654 | my ($self, $flag) = @_; | ||
| 1655 | my $f = $self->Subwidget('Statusbar'); | ||
| 1656 | if (defined $flag) { | ||
| 1657 | if ($flag) { | ||
| 1658 | $f->pack( | ||
| 1659 | -fill => 'x', | ||
| 1660 | ); | ||
| 1661 | $self->{STATUSVISIBLE} = 1; | ||
| 1662 | $f->updateStatus; | ||
| 1663 | } else { | ||
| 1664 | $f->packForget; | ||
| 1665 | $self->{STATUSVISIBLE} = 0; | ||
| 1666 | } | ||
| 1667 | } | ||
| 1668 | return $self->{STATUSVISIBLE}; | ||
| 1669 | } | ||
| 1670 | |||
| 1671 | sub spacebackground { | ||
| 1672 | my $self = shift; | ||
| 1673 | if (@_) { | ||
| 1674 | $self->tagConfigure('Space', -background => shift); | ||
| 1675 | } | ||
| 1676 | return $self->tagCget('Space', '-background'); | ||
| 1677 | } | ||
| 1678 | |||
| 1679 | sub spacesCheck { | ||
| 1680 | my ($self, $pos) = @_; | ||
| 1681 | return unless $self->cget('-showspaces'); | ||
| 1682 | my $line = $self->linenumber($pos); | ||
| 1683 | my $completed = $self->spacesCompleted; | ||
| 1684 | $self->spacesPurge($line) if $line <= $self->spacesCompleted; | ||
| 1685 | } | ||
| 1686 | |||
| 1687 | sub spacesCompleted { | ||
| 1688 | my $self = shift; | ||
| 1689 | $self->{SPACESCOMPLETED} = shift if @_; | ||
| 1690 | return $self->{SPACESCOMPLETED} | ||
| 1691 | } | ||
| 1692 | |||
| 1693 | sub spacesLine { | ||
| 1694 | my ($self, $line) = @_; | ||
| 1695 | my $begin = $self->index("$line.0"); | ||
| 1696 | my $end = $self->index("$begin lineend"); | ||
| 1697 | my $text = $self->get($begin, $end); | ||
| 1698 | for ('Space', 'Tab') { | ||
| 1699 | $self->tagRemove($_, $begin, $end) | ||
| 1700 | } | ||
| 1701 | if ($text =~ /^([\s|\t]+)/) { | ||
| 1702 | my $spaces = $1; | ||
| 1703 | my $count = 0; | ||
| 1704 | while ($spaces ne '') { | ||
| 1705 | my $char = substr $spaces, 0, 1, ''; | ||
| 1706 | my $next = $count + 1; | ||
| 1707 | $self->tagAdd('Space', "$begin + $count c", "$begin + $next c") if $char eq ' '; | ||
| 1708 | $self->tagAdd('Tab', "$begin + $count c", "$begin + $next c") if $char eq "\t"; | ||
| 1709 | $count ++ | ||
| 1710 | } | ||
| 1711 | } | ||
| 1712 | if ($text =~ /(\s+)$/) { | ||
| 1713 | my $spaces = $1; | ||
| 1714 | my $l = length($spaces); | ||
| 1715 | $end = $self->index("$end - $l c"); | ||
| 1716 | my $count = 0; | ||
| 1717 | while ($spaces ne '') { | ||
| 1718 | my $char = substr $spaces, 0, 1, ''; | ||
| 1719 | my $next = $count + 1; | ||
| 1720 | $self->tagAdd('Space', "$end + $count c", "$end + $next c") if $char eq ' '; | ||
| 1721 | $self->tagAdd('Tab', "$end + $count c", "$end + $next c") if $char eq "\t"; | ||
| 1722 | $count ++ | ||
| 1723 | } | ||
| 1724 | } | ||
| 1725 | $self->tagRaise('Space'); | ||
| 1726 | $self->tagRaise('Tab'); | ||
| 1727 | $self->tagRaise('Find'); | ||
| 1728 | $self->tagRaise('sel'); | ||
| 1729 | } | ||
| 1730 | |||
| 1731 | sub spacesLoop { | ||
| 1732 | my $self = shift; | ||
| 1733 | unless ($self->cget('-showspaces')) { | ||
| 1734 | $self->spacesLoopActive(0); | ||
| 1735 | return | ||
| 1736 | } | ||
| 1737 | my $xt = $self->Subwidget('XText'); | ||
| 1738 | my $lpc = $self->cget('-linespercycle'); | ||
| 1739 | my $complete = $self->spacesCompleted; | ||
| 1740 | $self->spacesRemove($complete, $complete + $lpc); | ||
| 1741 | for (1 .. $lpc) { | ||
| 1742 | my $complete = $self->spacesCompleted; | ||
| 1743 | if ($complete <= $xt->linenumber('end - 1c')) { | ||
| 1744 | $self->spacesLoopActive(1); | ||
| 1745 | $self->spacesLine($complete); | ||
| 1746 | $complete ++; | ||
| 1747 | $self->spacesCompleted($complete); | ||
| 1748 | } else { | ||
| 1749 | $self->spacesLoopActive(0); | ||
| 1750 | } | ||
| 1751 | last unless $self->spacesLoopActive; | ||
| 1752 | } | ||
| 1753 | $self->after($self->highlightinterval, ['spacesLoop', $self]) if $self->spacesLoopActive; | ||
| 1754 | } | ||
| 1755 | |||
| 1756 | sub spacesLoopActive { | ||
| 1757 | my $self = shift; | ||
| 1758 | $self->{SPACESLOOPACTIVE} = shift if @_; | ||
| 1759 | return $self->{SPACESLOOPACTIVE} | ||
| 1760 | } | ||
| 1761 | |||
| 1762 | sub spacesPurge { | ||
| 1763 | my ($self, $line, $remove) = @_; | ||
| 1764 | $line = 1 unless defined $line; | ||
| 1765 | $remove = 0 unless defined $remove; | ||
| 1766 | |||
| 1767 | $self->spacesRemove($line) if $remove; | ||
| 1768 | $self->spacesCompleted($line); | ||
| 1769 | |||
| 1770 | $self->spacesLoop unless $self->spacesLoopActive; | ||
| 1771 | } | ||
| 1772 | |||
| 1773 | sub spacesRemove { | ||
| 1774 | my ($self, $begin, $end) = @_; | ||
| 1775 | $begin = 1 unless defined $begin; | ||
| 1776 | $end = $self->linenumber('end') unless defined $end; | ||
| 1777 | $begin = "$begin.0"; | ||
| 1778 | $end = $self->index("$end.0 lineend"); | ||
| 1779 | |||
| 1780 | for ('Space', 'Tab') { | ||
| 1781 | $self->tagRemove($_, $begin, $end) | ||
| 1782 | } | ||
| 1783 | } | ||
| 1784 | |||
| 1785 | sub syntax { | ||
| 1786 | my ($self, $new) = @_; | ||
| 1787 | my $kam = $self->Kamelon; | ||
| 1788 | if (defined($new)) { | ||
| 1789 | $self->NoHighlighting(1); | ||
| 1790 | $self->highlightPurge(1, 1); | ||
| 1791 | $self->Subwidget('XText')->configure( | ||
| 1792 | -mlcommentend => undef, | ||
| 1793 | -mlcommentstart => undef, | ||
| 1794 | -slcomment => undef, | ||
| 1795 | ); | ||
| 1796 | unless ($new eq 'None') { | ||
| 1797 | $kam->Syntax($new); | ||
| 1798 | my $idx = $kam->GetIndexer; | ||
| 1799 | $self->Subwidget('XText')->configure( | ||
| 1800 | -mlcommentend => $idx->InfoMLCommentEnd($new), | ||
| 1801 | -mlcommentstart => $idx->InfoMLCommentStart($new), | ||
| 1802 | -slcomment => $idx->InfoSLComment($new), | ||
| 1803 | ); | ||
| 1804 | $self->NoHighlighting(0); | ||
| 1805 | $self->Colored(0); | ||
| 1806 | $self->ColorInf([ [$kam->StateGet] ]); | ||
| 1807 | $self->highlightLoop unless $self->LoopActive; | ||
| 1808 | } | ||
| 1809 | $self->{SYNTAX} = $new; | ||
| 1810 | } | ||
| 1811 | return $self->{SYNTAX} | ||
| 1812 | } | ||
| 1813 | |||
| 1814 | sub tabbackground { | ||
| 1815 | my $self = shift; | ||
| 1816 | if (@_) { | ||
| 1817 | $self->tagConfigure('Tab', -background => shift); | ||
| 1818 | } | ||
| 1819 | return $self->tagCget('Tab', '-background'); | ||
| 1820 | } | ||
| 1821 | |||
| 1822 | =item B<tags> | ||
| 1823 | |||
| 1824 | Returns the Kamelon list of AvailableAttributes. | ||
| 1825 | |||
| 1826 | =cut | ||
| 1827 | |||
| 1828 | sub tags { | ||
| 1829 | return $_[0]->Kamelon->AvailableAttributes | ||
| 1830 | } | ||
| 1831 | |||
| 1832 | =item B<theme> | ||
| 1833 | |||
| 1834 | Returns a reference to the current theme object. | ||
| 1835 | See also L<Tk::CodeText::Theme> | ||
| 1836 | |||
| 1837 | =cut | ||
| 1838 | |||
| 1839 | sub theme { | ||
| 1840 | return $_[0]->{THEME} | ||
| 1841 | } | ||
| 1842 | |||
| 1843 | =item B<themeDialog> | ||
| 1844 | |||
| 1845 | Initiates a dialog for editing the colors and font information for highlighting. | ||
| 1846 | |||
| 1847 | =cut | ||
| 1848 | |||
| 1849 | sub themeDialog { | ||
| 1850 | my $self = shift; | ||
| 1851 | my $theme = $self->theme; | ||
| 1852 | my $dialog = $self->DialogBox( | ||
| 1853 | -title => 'Theme editor', | ||
| 1854 | -buttons => ['Ok', 'Cancel'], | ||
| 1855 | -default_button => 'Ok', | ||
| 1856 | -cancel_button => 'Cancel', | ||
| 1857 | ); | ||
| 1858 | my $historyfile; | ||
| 1859 | my $config = $self->cget('-configdir'); | ||
| 1860 | $historyfile = "$config/recent_colors"; | ||
| 1861 | my $editor = $dialog->add('TagsEditor', | ||
| 1862 | -defaultbackground => $self->Subwidget('XText')->cget('-background'), | ||
| 1863 | -defaultforeground => $self->Subwidget('XText')->cget('-foreground'), | ||
| 1864 | -defaultfont => $self->Subwidget('XText')->cget('-font'), | ||
| 1865 | -historyfile => $historyfile, | ||
| 1866 | -relief => 'sunken', | ||
| 1867 | -borderwidth => 2, | ||
| 1868 | -width => 62, | ||
| 1869 | )->pack(-expand => 1, -fill => 'both', -padx => 2, -pady => 2); | ||
| 1870 | my $toolframe = $dialog->add('Frame', | ||
| 1871 | )->pack(-fill => 'x'); | ||
| 1872 | $toolframe->Button( | ||
| 1873 | -command => sub { | ||
| 1874 | my $file = $self->getSaveFile( | ||
| 1875 | -filetypes => [ | ||
| 1876 | ['Highlight Theme' => '.ctt'], | ||
| 1877 | ], | ||
| 1878 | ); | ||
| 1879 | $editor->save($file) if defined $file; | ||
| 1880 | }, | ||
| 1881 | -text => 'Save', | ||
| 1882 | )->pack(-side => 'left', -padx => 5, -pady => 5); | ||
| 1883 | $toolframe->Button( | ||
| 1884 | -text => 'Load', | ||
| 1885 | -command => sub { | ||
| 1886 | my $file = $self->getOpenFile( | ||
| 1887 | -filetypes => [ | ||
| 1888 | ['Highlight Theme' => '.ctt'], | ||
| 1889 | ], | ||
| 1890 | ); | ||
| 1891 | if (defined $file) { | ||
| 1892 | my $obj = Tk::CodeText::Theme->new; | ||
| 1893 | $obj->load($file); | ||
| 1894 | $editor->put($obj->get); | ||
| 1895 | $editor->updateAll | ||
| 1896 | } | ||
| 1897 | }, | ||
| 1898 | )->pack(-side => 'left', -padx => 5, -pady => 5); | ||
| 1899 | |||
| 1900 | $editor->put($theme->get); | ||
| 1901 | my $button = $dialog->Show(-popover => $self); | ||
| 1902 | if ($button eq 'Ok') { | ||
| 1903 | $theme->put($editor->get); | ||
| 1904 | $self->themeUpdate; | ||
| 1905 | } | ||
| 1906 | $dialog->destroy; | ||
| 1907 | } | ||
| 1908 | |||
| 1909 | sub themefile { | ||
| 1910 | my $self = shift; | ||
| 1911 | if (@_) { | ||
| 1912 | my $file = shift; | ||
| 1913 | if ((defined $file) and (-e $file)) { | ||
| 1914 | $self->theme->load($file); | ||
| 1915 | #the ->after is necessary here, at create time the widget would not yet return the | ||
| 1916 | #correct font information to configure the tags correctly. | ||
| 1917 | #TODO: find a solution for this. | ||
| 1918 | $self->after(1, ['themeUpdate', $self]);; | ||
| 1919 | } | ||
| 1920 | $self->{THEMEFILE} = $file; | ||
| 1921 | } | ||
| 1922 | return $self->{THEMEFILE}; | ||
| 1923 | } | ||
| 1924 | |||
| 1925 | sub themeUpdate { | ||
| 1926 | my $self = shift; | ||
| 1927 | my $theme = $self->theme; | ||
| 1928 | my @values = $theme->get; | ||
| 1929 | my $font = $self->cget('-font'); | ||
| 1930 | my $bg = $self->Subwidget('XText')->cget('-background'); | ||
| 1931 | my $fg = $self->Subwidget('XText')->cget('-foreground'); | ||
| 1932 | for ($theme->tagList) { $self->tagDelete($_) } | ||
| 1933 | while (@values) { | ||
| 1934 | my $tag = shift @values; | ||
| 1935 | my $options = shift @values; | ||
| 1936 | my %opt = @$options; | ||
| 1937 | my $nbg = $bg; | ||
| 1938 | my $nfg = $fg; | ||
| 1939 | my $nfont = $font; | ||
| 1940 | $nbg = $opt{'-background'} if exists $opt{'-background'}; | ||
| 1941 | $nfg = $opt{'-foreground'} if exists $opt{'-foreground'}; | ||
| 1942 | $nfont = $self->fontCompose($nfont, -slant => $opt{'-slant'}) if exists $opt{'-slant'}; | ||
| 1943 | $nfont = $self->fontCompose($nfont, -weight => $opt{'-weight'}) if exists $opt{'-weight'}; | ||
| 1944 | $self->tagConfigure($tag, | ||
| 1945 | -background => $nbg, | ||
| 1946 | -foreground => $nfg, | ||
| 1947 | -font => $nfont, | ||
| 1948 | ); | ||
| 1949 | } | ||
| 1950 | $self->highlightPurge(1); | ||
| 1951 | } | ||
| 1952 | |||
| 1953 | =item B<uncomment> | ||
| 1954 | |||
| 1955 | Uncomments the current line or selection. | ||
| 1956 | |||
| 1957 | =item B<undo> | ||
| 1958 | |||
| 1959 | Undoes the last edit operation. | ||
| 1960 | |||
| 1961 | =item B<unindent> | ||
| 1962 | |||
| 1963 | Unintents the current line or selection | ||
| 1964 | |||
| 1965 | =cut | ||
| 1966 | |||
| 1967 | sub ViewMenuItems { | ||
| 1968 | my $self = shift; | ||
| 1969 | |||
| 1970 | my $a; | ||
| 1971 | tie $a, 'Tk::Configure', $self, '-autoindent'; | ||
| 1972 | my $b; | ||
| 1973 | tie $b, 'Tk::Configure', $self, '-autobrackets'; | ||
| 1974 | my $c; | ||
| 1975 | tie $c, 'Tk::Configure', $self, '-autocomplete'; | ||
| 1976 | my $d; | ||
| 1977 | tie $d, 'Tk::Configure', $self, '-showspaces'; | ||
| 1978 | my $f; | ||
| 1979 | tie $f, 'Tk::Configure', $self, '-showfolds'; | ||
| 1980 | my $n; | ||
| 1981 | tie $n, 'Tk::Configure', $self, '-shownumbers'; | ||
| 1982 | my $s; | ||
| 1983 | tie $s, 'Tk::Configure', $self, '-showstatus'; | ||
| 1984 | |||
| 1985 | my $v = $self->cget('-wrap'); | ||
| 1986 | Tie::Watch->new( | ||
| 1987 | -variable => \$v, | ||
| 1988 | -store => sub { | ||
| 1989 | my ($watch, $value) = @_; | ||
| 1990 | $watch->Store($value); | ||
| 1991 | $self->configure(-wrap => $v); | ||
| 1992 | $self->contentCheck; | ||
| 1993 | }, | ||
| 1994 | ); | ||
| 1995 | |||
| 1996 | my @values = (-onvalue => 1, -offvalue => 0); | ||
| 1997 | my $match = $self->cget('-match'); | ||
| 1998 | my $curlies = ''; | ||
| 1999 | $curlies = '{}' if $match =~ /\{\}/; | ||
| 2000 | my $paren = ''; | ||
| 2001 | $paren = '()' if $match =~ /\(\)/; | ||
| 2002 | my $brackets = ''; | ||
| 2003 | $brackets = '[]' if $match =~ /\[\]/; | ||
| 2004 | my @opt = ( | ||
| 2005 | -command => sub { $self->configure('-match', $curlies . $paren . $brackets) }, | ||
| 2006 | -offvalue => '', | ||
| 2007 | ); | ||
| 2008 | my @items = ( | ||
| 2009 | [checkbutton => '~Auto indent', @values, -variable => \$a], | ||
| 2010 | [checkbutton => '~Auto brackets', @values, -variable => \$b], | ||
| 2011 | [checkbutton => 'A~uto complete', @values, -variable => \$c], | ||
| 2012 | [checkbutton => '~Show spaces and tabs', @values, -variable => \$d], | ||
| 2013 | ['cascade'=> '~Wrap', -tearoff => 0, -menuitems => [ | ||
| 2014 | [radiobutton => 'Word', -variable => \$v, -value => 'word'], | ||
| 2015 | [radiobutton => 'Character', -variable => \$v, -value => 'char'], | ||
| 2016 | [radiobutton => 'None', -variable => \$v, -value => 'none'], | ||
| 2017 | ]], | ||
| 2018 | ['cascade'=> '~Match', -tearoff => 0, -menuitems => [ | ||
| 2019 | [checkbutton => '() Parenthesis', @opt, -variable => \$paren, -onvalue => '()'], | ||
| 2020 | [checkbutton => '{} Curlies', @opt, -variable => \$curlies, -onvalue => '{}'], | ||
| 2021 | [checkbutton => '[] Brackets',@opt, -variable => \$brackets, -onvalue => '[]'], | ||
| 2022 | ]], | ||
| 2023 | [command => 'Au~to complete settings', -command => [acSettings => $self]], | ||
| 2024 | [command => '~Colors', -command => [themeDialog => $self]], | ||
| 2025 | 'separator', | ||
| 2026 | [checkbutton => 'Code ~folds', @values, -variable => \$f], | ||
| 2027 | [checkbutton => '~Line numbers', @values, -variable => \$n], | ||
| 2028 | [checkbutton => '~Status bar', @values, -variable => \$s], | ||
| 2029 | 'separator', | ||
| 2030 | [command => '~Fix indentation', -command => [fixIndent => $self]], | ||
| 2031 | [command => '~Remove trailing spaces', -command => [removeTrailingSpaces => $self]], | ||
| 2032 | ); | ||
| 2033 | return @items | ||
| 2034 | } | ||
| 2035 | |||
| 2036 | =item B<visualBegin> | ||
| 2037 | |||
| 2038 | Returns the line number of the first visible line. | ||
| 2039 | |||
| 2040 | =cut | ||
| 2041 | |||
| 2042 | =item B<visualEnd> | ||
| 2043 | |||
| 2044 | Returns the line number of the last visible line. | ||
| 2045 | |||
| 2046 | =cut | ||
| 2047 | |||
| 2048 | =back | ||
| 2049 | |||
| 2050 | =head1 EXECUTABLES | ||
| 2051 | |||
| 2052 | This module packs an executable, B<codetext>. It is a simple Notepad like editor but with all the features | ||
| 2053 | of B<Tk::CodeText>. Type 'codetext -h' in the command line for options. | ||
| 2054 | |||
| 2055 | =head1 AUTHOR | ||
| 2056 | |||
| 2057 | Hans Jeuken (hanje at cpan dot org) | ||
| 2058 | |||
| 2059 | =head1 BUGS AND CAVEATS | ||
| 2060 | |||
| 2061 | Matching {}, [] and () does not take strings with matchable symbols into account. | ||
| 2062 | |||
| 2063 | If you find any, please contact the author. | ||
| 2064 | |||
| 2065 | =head1 SEE ALSO | ||
| 2066 | |||
| 2067 | =over 4 | ||
| 2068 | |||
| 2069 | =item L<Tk::Text> | ||
| 2070 | |||
| 2071 | =item L<Tk::XText> | ||
| 2072 | |||
| 2073 | =item L<Syntax::Kamelon::Syntaxes> | ||
| 2074 | |||
| 2075 | =back | ||
| 2076 | |||
| 2077 | =cut | ||
| 2078 | |||
| 2079 | 1; | ||
| 2080 | |||
| 2081 | __END__ |