| 1 | package App::Codit::Ext::CoditMDI; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | App::Codit::Ext::CoditMDI - Multiple Document Interface for App::Codit | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use Carp; | ||
| 12 | use vars qw($VERSION); | ||
| 13 | $VERSION = '0.23'; | ||
| 14 | |||
| 15 | use base qw( Tk::AppWindow::Ext::MDI ); | ||
| 16 | |||
| 17 | #require Tk::AppWindow::PluginsForm; | ||
| 18 | require App::Codit::CoditTagsEditor; | ||
| 19 | require App::Codit::Macro; | ||
| 20 | require Tk::YADialog; | ||
| 21 | |||
| 22 | my @navcontextmenu = ( | ||
| 23 | [ 'menu_normal', undef, '~Close', 'doc_close', 'document-close', '*CTRL+SHIFT+O'], | ||
| 24 | [ 'menu_normal', undef, '~Delete', 'doc_delete_dialog', 'edit-delete'], | ||
| 25 | [ 'menu_separator', undef, 'c1'], | ||
| 26 | [ 'menu_normal', undef, '~Collapse all', 'nav_collapse'], | ||
| 27 | [ 'menu_normal', undef, '~Expand all', 'nav_expand'], | ||
| 28 | ); | ||
| 29 | |||
| 30 | =head1 SYNOPSIS | ||
| 31 | |||
| 32 | my $app = new App::Codit(@options, | ||
| 33 | -extensions => ['CoditMDI'], | ||
| 34 | ); | ||
| 35 | $app->MainLoop; | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | Inherits L<Tk::AppWindow::Ext::MDI>. | ||
| 40 | |||
| 41 | This is a specially crafted multiple document interface for l<App::Codit>. | ||
| 42 | |||
| 43 | =head1 CONFIG VARIABLES | ||
| 44 | |||
| 45 | =over 4 | ||
| 46 | |||
| 47 | =item B<-doc_autobrackets> I<hookable> | ||
| 48 | |||
| 49 | Sets and returns the autobrackets option of the currently selected document. | ||
| 50 | |||
| 51 | =item B<-doc_autocomplete> I<hookable> | ||
| 52 | |||
| 53 | Sets and returns the autocomplete option of the currently selected document. | ||
| 54 | |||
| 55 | =item B<-doc_autoindent> I<hookable> | ||
| 56 | |||
| 57 | Sets and returns the autoindent option of the currently selected document. | ||
| 58 | |||
| 59 | =item B<-doc_wrap> I<hookable> | ||
| 60 | |||
| 61 | Sets and returns the wrap option of the currently selected document. | ||
| 62 | |||
| 63 | =item B<-doc_view_folds> I<hookable> | ||
| 64 | |||
| 65 | Sets and returns the showfolds option of the currently selected document. | ||
| 66 | |||
| 67 | =item B<-doc_view_numbers> I<hookable> | ||
| 68 | |||
| 69 | Sets and returns the shownumbers option of the currently selected document. | ||
| 70 | |||
| 71 | =item B<-doc_view_status> I<hookable> | ||
| 72 | |||
| 73 | Sets and returns the showstatus option of the currently selected document. | ||
| 74 | |||
| 75 | =item B<-doc_wrap> | ||
| 76 | |||
| 77 | Sets and returns the wrap option of the currently selected document. | ||
| 78 | |||
| 79 | =back | ||
| 80 | |||
| 81 | =head1 COMMANDS | ||
| 82 | |||
| 83 | =over 4 | ||
| 84 | |||
| 85 | =item B<bookmark_add> | ||
| 86 | |||
| 87 | Adds the current line to the bookmarks list. | ||
| 88 | |||
| 89 | =item B<bookmark_clear> | ||
| 90 | |||
| 91 | Clears all bookmarks in the current document. | ||
| 92 | |||
| 93 | =item B<bookmark_fill> | ||
| 94 | |||
| 95 | Called when the bookmark menu is popped. It fills it with | ||
| 96 | the bookmarks in the current document. | ||
| 97 | |||
| 98 | =item B<bookmark_next> | ||
| 99 | |||
| 100 | Jump to the next bookmark in the current document. | ||
| 101 | |||
| 102 | =item B<bookmark_prev> | ||
| 103 | |||
| 104 | Jump to the previous bookmark in the current document. | ||
| 105 | |||
| 106 | =item B<bookmark_remove> | ||
| 107 | |||
| 108 | Removes the bookmark on the current line, if any. | ||
| 109 | |||
| 110 | =item B<doc_autobracket> | ||
| 111 | |||
| 112 | Sets and returns the autobracket option of the currently selected document. | ||
| 113 | |||
| 114 | =item B<doc_autoindent> | ||
| 115 | |||
| 116 | Sets and returns the autoindent option of the currently selected document. | ||
| 117 | |||
| 118 | =item B<doc_case_lower> | ||
| 119 | |||
| 120 | If there is a selection it turns it to lower case. | ||
| 121 | Else it only turns the character at the insert position to lower case. | ||
| 122 | |||
| 123 | =item B<doc_case_upper> | ||
| 124 | |||
| 125 | If there is a selection it turns it to upper case. | ||
| 126 | Else it only turns the character at the insert position to upper case. | ||
| 127 | |||
| 128 | =item B<doc_delete> | ||
| 129 | |||
| 130 | Closes the current selected document and deletes the document file from disk. | ||
| 131 | Use with caution. | ||
| 132 | |||
| 133 | =item B<doc_delete_dialog> | ||
| 134 | |||
| 135 | Same as I<doc_delete> except it first asks nicely if you really want to do that. | ||
| 136 | |||
| 137 | =item B<doc_find> | ||
| 138 | |||
| 139 | Pops up the search bar in the currently selected document. | ||
| 140 | |||
| 141 | =item B<doc_fix_indent> | ||
| 142 | |||
| 143 | Asks for the number of spaces per tab and attempts | ||
| 144 | to reformat the indentation taking the indentstyle into account. | ||
| 145 | |||
| 146 | If a selection exists it will do this for the selection, otherwise it | ||
| 147 | will scan the whole document. | ||
| 148 | |||
| 149 | =item B<doc_get_sel> | ||
| 150 | |||
| 151 | Returns the begin and end index of the current selection. | ||
| 152 | |||
| 153 | =item B<doc_get_text> I<$begin>, I<$end> | ||
| 154 | |||
| 155 | Returns the text in the current selected document from index $begin to index $end. | ||
| 156 | |||
| 157 | =item B<doc_patch> | ||
| 158 | |||
| 159 | Invokes a dialog where you can select a diff file to patch the current document. | ||
| 160 | |||
| 161 | =item B<doc_remove_trailing> | ||
| 162 | |||
| 163 | Removes spaces at the end of each line. | ||
| 164 | |||
| 165 | If a selection exists it will do this for the selection, otherwise it | ||
| 166 | will scan the whole document. | ||
| 167 | |||
| 168 | =item B<doc_replace> | ||
| 169 | |||
| 170 | Pops up the search and replace bar in the currently selected document. | ||
| 171 | |||
| 172 | =item B<doc_wrap> | ||
| 173 | |||
| 174 | Sets and returns the wrap option of the currently selected document. | ||
| 175 | |||
| 176 | =item B<edit_delete>, I<$begin>, I<$end> | ||
| 177 | |||
| 178 | Deletes text in the currently selected document. It takes two indices as parameters. | ||
| 179 | |||
| 180 | =item B<edit_insert>, I<$index>, I<$text> | ||
| 181 | |||
| 182 | Inserts text in the currently selected document. It takes an index and a string as parameters. | ||
| 183 | |||
| 184 | =item B<edit_replace>, I<$begin>, I<$end>, I<$text> | ||
| 185 | |||
| 186 | Replaces text in the currently selected document. It takes two indices and a text as parameters. | ||
| 187 | |||
| 188 | =item B<key_released>, I<$doc>, I<$key> | ||
| 189 | |||
| 190 | Dummy command only meant for hooking on by plugins. Called every time a visible character | ||
| 191 | key was pressed. | ||
| 192 | |||
| 193 | =item B<modified>, I<$doc>, I<$index> | ||
| 194 | |||
| 195 | Called every time you make an edit, it gets a document name and an index as parameters. | ||
| 196 | It checks if there are any macros that should be restarted. | ||
| 197 | Many plugins hook on to this command. | ||
| 198 | |||
| 199 | =back | ||
| 200 | |||
| 201 | =head1 METHODS | ||
| 202 | |||
| 203 | =over 4 | ||
| 204 | |||
| 205 | =cut | ||
| 206 | |||
| 207 | sub new { | ||
| 208 | my $class = shift; | ||
| 209 | my $self = $class->SUPER::new(@_); | ||
| 210 | # $self->Require('Navigator'); | ||
| 211 | $self->{MACROS} = {}; | ||
| 212 | $self->{SHOWSPACES} = 0; | ||
| 213 | $self->{FIXINDENTSPACES} = 3; | ||
| 214 | |||
| 215 | my $nav = $self->Subwidget('NAVTREE'); | ||
| 216 | |||
| 217 | $self->configInit( | ||
| 218 | -doc_autoindent => ['docAutoIndent', $self], | ||
| 219 | -doc_autobrackets => ['docAutoBrackets', $self], | ||
| 220 | -doc_autocomplete=> ['docAutoComplete', $self], | ||
| 221 | -doc_show_spaces => ['docShowSpaces', $self], | ||
| 222 | -doc_view_folds => ['docViewFolds', $self], | ||
| 223 | -doc_view_numbers => ['docViewNumbers', $self], | ||
| 224 | -doc_view_status => ['docViewStatus', $self], | ||
| 225 | -doc_wrap => ['docWrap', $self], | ||
| 226 | ); | ||
| 227 | $self->cmdConfig( | ||
| 228 | bookmark_add => ['bookmarkAdd', $self], | ||
| 229 | bookmark_clear => ['bookmarkClear', $self], | ||
| 230 | bookmark_fill => ['bookmarkFill', $self], | ||
| 231 | bookmark_next => ['bookmarkNext', $self], | ||
| 232 | bookmark_prev => ['bookmarkPrev', $self], | ||
| 233 | bookmark_remove => ['bookmarkRemove', $self], | ||
| 234 | doc_autoindent => ['docAutoIndent', $self], | ||
| 235 | doc_case_lower => ['docCaseLower', $self], | ||
| 236 | doc_case_upper => ['docCaseUpper', $self], | ||
| 237 | doc_delete => ['docDelete', $self], | ||
| 238 | doc_delete_dialog => ['docDeleteDialog', $self], | ||
| 239 | doc_find => ['docPopFindReplace', $self, 1], | ||
| 240 | doc_fix_indent => ['docFixIndent', $self], | ||
| 241 | doc_get_sel => ['docGetSel', $self], | ||
| 242 | doc_get_text => ['docGetText', $self], | ||
| 243 | doc_patch => ['docPatch', $self], | ||
| 244 | doc_remove_trailing => ['docRemoveTrailing', $self], | ||
| 245 | doc_replace => ['docPopFindReplace', $self, 0], | ||
| 246 | doc_wrap => ['docWrap', $self], | ||
| 247 | edit_delete => ['editDelete', $self], | ||
| 248 | edit_insert => ['editInsert', $self], | ||
| 249 | edit_replace => ['editReplace', $self], | ||
| 250 | key_released => ['keyReleased', $self], | ||
| 251 | modified => ['contentModified', $self], | ||
| 252 | nav_collapse => ['navCollapse', $self], | ||
| 253 | nav_expand => ['navExpand', $self], | ||
| 254 | ); | ||
| 255 | return $self; | ||
| 256 | } | ||
| 257 | |||
| 258 | sub _mcr { return $_[0]->{MACROS} } | ||
| 259 | |||
| 260 | sub bookmarkAdd { | ||
| 261 | my $self = shift; | ||
| 262 | my $doc = $self->docSelected; | ||
| 263 | if (defined $doc) { | ||
| 264 | my $w = $self->docGet($doc)->CWidg; | ||
| 265 | $w->bookmarkNew | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | sub bookmarkClear { | ||
| 270 | my $self = shift; | ||
| 271 | my $doc = $self->docSelected; | ||
| 272 | if (defined $doc) { | ||
| 273 | my $w = $self->docGet($doc)->CWidg; | ||
| 274 | $w->bookmarkRemoveAll; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | sub bookmarkFill { | ||
| 279 | my $self = shift; | ||
| 280 | my $mnu = $self->extGet('MenuBar'); | ||
| 281 | my ($menu, $index) = $mnu->FindMenuEntry('Bookmarks'); | ||
| 282 | my $submenu = $menu->entrycget($index, '-menu'); | ||
| 283 | my $i = $submenu->index('end'); | ||
| 284 | if ($i > 7) { | ||
| 285 | $submenu->delete(8, 'last'); | ||
| 286 | } | ||
| 287 | my $doc = $self->docSelected; | ||
| 288 | if (defined $doc) { | ||
| 289 | my $w = $self->docGet($doc)->CWidg; | ||
| 290 | my @list = $w->bookmarkList; | ||
| 291 | for (@list) { | ||
| 292 | $submenu->add('command', | ||
| 293 | -label => "$_ - " . $w->bookmarkText($_), | ||
| 294 | -command => ['bookmarkGo', $w, $_], | ||
| 295 | ); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | sub bookmarkNext { | ||
| 301 | my $self = shift; | ||
| 302 | my $doc = $self->docSelected; | ||
| 303 | if (defined $doc) { | ||
| 304 | my $w = $self->docGet($doc)->CWidg; | ||
| 305 | $w->bookmarkNext | ||
| 306 | } | ||
| 307 | } | ||
| 308 | |||
| 309 | sub bookmarkPrev { | ||
| 310 | my $self = shift; | ||
| 311 | my $doc = $self->docSelected; | ||
| 312 | if (defined $doc) { | ||
| 313 | my $w = $self->docGet($doc)->CWidg; | ||
| 314 | $w->bookmarkPrev | ||
| 315 | } | ||
| 316 | } | ||
| 317 | |||
| 318 | sub bookmarkRemove { | ||
| 319 | my $self = shift; | ||
| 320 | my $doc = $self->docSelected; | ||
| 321 | if (defined $doc) { | ||
| 322 | my $w = $self->docGet($doc)->CWidg; | ||
| 323 | $w->bookmarkRemove | ||
| 324 | } | ||
| 325 | } | ||
| 326 | |||
| 327 | sub CmdDocClose { | ||
| 328 | my ($self, $name) = @_; | ||
| 329 | $name = $self->docSelected unless defined $name; | ||
| 330 | $self->macroRemoveAll($name); | ||
| 331 | my $result = $self->SUPER::CmdDocClose($name); | ||
| 332 | if ($result) { | ||
| 333 | $self->after(100, sub { | ||
| 334 | my @list = $self->docFullList; | ||
| 335 | $self->cmdExecute('doc_new') unless @list; | ||
| 336 | }); | ||
| 337 | } | ||
| 338 | return $result | ||
| 339 | } | ||
| 340 | |||
| 341 | sub CmdDocNew { | ||
| 342 | my $self = shift; | ||
| 343 | my $result = $self->SUPER::CmdDocNew(@_); | ||
| 344 | $self->disposeUntitled if $result; | ||
| 345 | return $result | ||
| 346 | } | ||
| 347 | |||
| 348 | #sub CmdDocOpen { | ||
| 349 | # my $self = shift; | ||
| 350 | # my $result = $self->SUPER::CmdDocOpen(@_); | ||
| 351 | # $self->disposeUntitled if $result; | ||
| 352 | # return $result | ||
| 353 | #} | ||
| 354 | |||
| 355 | sub contentModified { | ||
| 356 | my $self = shift; | ||
| 357 | my ($doc, $index) = @_; | ||
| 358 | if ($self->docShowSpaces) { | ||
| 359 | my $macro = $self->macroGet($doc, 'space'); | ||
| 360 | if (defined $macro) { | ||
| 361 | my $line = $macro->widg->linenumber($index); | ||
| 362 | $macro->line($line); | ||
| 363 | $macro->start; | ||
| 364 | } | ||
| 365 | } | ||
| 366 | return @_; | ||
| 367 | } | ||
| 368 | |||
| 369 | sub ContextMenu { | ||
| 370 | my $self = shift; | ||
| 371 | return $self->extGet('MenuBar')->menuContext($self->GetAppWindow, | ||
| 372 | [ 'menu_normal', undef, '~Copy', '<Control-c>', 'edit-copy', '*CTRL+C'], | ||
| 373 | [ 'menu_normal', undef, 'C~ut', '<Control-x>', 'edit-cut', '*CTRL+X'], | ||
| 374 | [ 'menu_normal', undef, '~Paste', '<Control-v>', 'edit-paste', '*CTRL+V'], | ||
| 375 | [ 'menu_separator', undef, 'c1'], | ||
| 376 | [ 'menu_normal', undef, '~Select all', '<Control-a>', 'edit-select-all', '*CTRL+A'], | ||
| 377 | [ 'menu_separator', undef, 'c2'], | ||
| 378 | [ 'menu_normal', undef, 'Co~mment', '<Control-g>', undef, '*CTRL+G'], | ||
| 379 | [ 'menu_normal', undef, '~Uncomment', '<Control-G>', undef, '*CTRL+SHIFT+G'], | ||
| 380 | [ 'menu_separator', undef, 'c3' ], | ||
| 381 | [ 'menu_normal', undef, '~Indent', '<Control-j>', 'format-indent-more','*CTRL+J'], | ||
| 382 | [ 'menu_normal', undef, 'Unin~dent', '<Control-J>', 'format-indent-less','*CTRL+SHIFT+J'], | ||
| 383 | [ 'menu_separator', undef, 'c4' ], | ||
| 384 | [ 'menu_check', undef, 'A~uto indent', undef, '-doc_autoindent', undef, 0, 1], | ||
| 385 | [ 'menu_radio_s', undef, '~Wrap', [qw/char word none/], 'text-wrap', '-doc_wrap'], | ||
| 386 | ) | ||
| 387 | } | ||
| 388 | |||
| 389 | sub CreateContentHandler { | ||
| 390 | my ($self, $name, $parent) = @_; | ||
| 391 | my $h = $self->SUPER::CreateContentHandler($name, $parent); | ||
| 392 | $h->Name($name); | ||
| 393 | return $h; | ||
| 394 | } | ||
| 395 | |||
| 396 | sub CreateInterface { | ||
| 397 | my $self = shift; | ||
| 398 | my $w = $self->WorkSpace; | ||
| 399 | $w->gridColumnconfigure(0, -weight => 1); | ||
| 400 | $w->gridRowconfigure(0, -weight => 1); | ||
| 401 | $self->{INTERFACE} = $w->YANoteBook( | ||
| 402 | -image => $self->getArt('document-multiple', 16), | ||
| 403 | -indicatorimage => $self->getArt('document-save', 16), | ||
| 404 | -selecttabcall => ['cmdExecute', $self, 'doc_select'], | ||
| 405 | -closetabcall => ['cmdExecute', $self, 'doc_close'], | ||
| 406 | )->grid(-row => 0, -column => 0, -sticky => 'nsew'); | ||
| 407 | } | ||
| 408 | |||
| 409 | sub deferredOpen { | ||
| 410 | my ($self, $name) = @_; | ||
| 411 | croak 'Name not defined' unless defined $name; | ||
| 412 | my $options = $self->deferredOptions($name); | ||
| 413 | my $bookmarks = delete $options->{'bookmarks'}; | ||
| 414 | $self->SUPER::deferredOpen($name); | ||
| 415 | $self->after(100, sub { | ||
| 416 | if (defined $bookmarks) { | ||
| 417 | my $w = $self->docGet($name)->CWidg; | ||
| 418 | while ($bookmarks =~ s/^(\d+)\s//) { | ||
| 419 | $w->bookmarkNew($1); | ||
| 420 | } | ||
| 421 | } | ||
| 422 | }); | ||
| 423 | } | ||
| 424 | |||
| 425 | sub disposeUntitled { | ||
| 426 | my $self = shift; | ||
| 427 | my @list = $self->docListDisplayed; | ||
| 428 | my $untitled = $list[0]; | ||
| 429 | if ((@list eq 2) and ($untitled =~ /^Untitled/)){ | ||
| 430 | return if -e $untitled; | ||
| 431 | return if $self->docModified($untitled); | ||
| 432 | $self->cmdExecute('doc_close', $untitled); | ||
| 433 | } | ||
| 434 | } | ||
| 435 | |||
| 436 | sub docAutoIndent { | ||
| 437 | my $self = shift; | ||
| 438 | return $self->docOption('-contentautoindent', @_); | ||
| 439 | } | ||
| 440 | |||
| 441 | sub docAutoBrackets { | ||
| 442 | my $self = shift; | ||
| 443 | return $self->docOption('-contentautobrackets', @_); | ||
| 444 | } | ||
| 445 | |||
| 446 | sub docAutoComplete { | ||
| 447 | my $self = shift; | ||
| 448 | return $self->docOption('-contentautocomplete', @_); | ||
| 449 | } | ||
| 450 | |||
| 451 | sub docCase { | ||
| 452 | my ($self, $upper) = @_; | ||
| 453 | $upper = 1 unless defined $upper; | ||
| 454 | my $widg = $self->docWidget; | ||
| 455 | return unless defined $widg; | ||
| 456 | $widg->caseChange($upper); | ||
| 457 | } | ||
| 458 | |||
| 459 | sub docCaseLower { $_[0]->docCase(0) } | ||
| 460 | |||
| 461 | sub docCaseUpper { $_[0]->docCase(1) } | ||
| 462 | |||
| 463 | sub docDelete { | ||
| 464 | my ($self, $name) = @_; | ||
| 465 | my $r = 1; | ||
| 466 | $name = $self->docSelected unless defined $name; | ||
| 467 | if (defined $name) { | ||
| 468 | $r = $self->cmdExecute('doc_close', $name) if $self->docExists($name); | ||
| 469 | unlink $name if $r; | ||
| 470 | } | ||
| 471 | return $name if $r; | ||
| 472 | return '' | ||
| 473 | } | ||
| 474 | |||
| 475 | sub docDeleteDialog { | ||
| 476 | my ($self, $name) = @_; | ||
| 477 | $name = $self->docSelected unless defined $name; | ||
| 478 | if (defined $name) { | ||
| 479 | my $answer = $self->popDialog( | ||
| 480 | 'Deleting file', | ||
| 481 | "Are you sure you want to delete\n$name?", | ||
| 482 | 'dialog-warning', | ||
| 483 | 'Yes', 'No', | ||
| 484 | ); | ||
| 485 | return $self->cmdExecute('doc_delete', $name) if $answer eq 'Yes'; | ||
| 486 | } | ||
| 487 | return '' | ||
| 488 | } | ||
| 489 | |||
| 490 | sub docFixIndent { | ||
| 491 | my ($self, $name) = @_; | ||
| 492 | $name = $self->docSelected unless defined $name; | ||
| 493 | my $widg = $self->docGet($name)->CWidg; | ||
| 494 | $widg->fixIndent | ||
| 495 | |||
| 496 | } | ||
| 497 | |||
| 498 | sub docGetSel { | ||
| 499 | my $self = shift; | ||
| 500 | my $doc = $self->docSelected; | ||
| 501 | return unless defined $doc; | ||
| 502 | return $self->docGet($doc)->tagRanges('sel'); | ||
| 503 | } | ||
| 504 | |||
| 505 | sub docGetText { | ||
| 506 | my $self = shift; | ||
| 507 | my $doc = $self->docSelected; | ||
| 508 | return unless defined $doc; | ||
| 509 | return $self->docGet($doc)->get(@_); | ||
| 510 | } | ||
| 511 | |||
| 512 | sub docPatch { | ||
| 513 | my $self = shift; | ||
| 514 | my $dialog = $self->YADialog( | ||
| 515 | -title => 'Patch', | ||
| 516 | -buttons => ['Ok', 'Cancel'], | ||
| 517 | ); | ||
| 518 | |||
| 519 | my @padding = (-padx => 2, -pady => 2); | ||
| 520 | my $f = $dialog->Frame->pack(-fill => 'x'); | ||
| 521 | $f->Label( | ||
| 522 | -text => 'File:', | ||
| 523 | -width => 6, | ||
| 524 | -anchor => 'e', | ||
| 525 | )->pack(-side => 'left'); | ||
| 526 | my $e = $f->Entry( | ||
| 527 | -width => 40, | ||
| 528 | )->pack(@padding, -side => 'left'); | ||
| 529 | $f->Button( | ||
| 530 | -relief => 'flat', | ||
| 531 | -image => $self->getArt('document-open', 22), | ||
| 532 | -text => 'Select', | ||
| 533 | -command => sub { | ||
| 534 | my ($file) = $self->pickFileOpen; | ||
| 535 | if (defined $file) { | ||
| 536 | $e->delete('0', 'end'); | ||
| 537 | $e->insert('end', $file) | ||
| 538 | } | ||
| 539 | }, | ||
| 540 | )->pack(@padding, -side => 'left'); | ||
| 541 | |||
| 542 | my $m = $dialog->Frame->pack(-fill => 'x'); | ||
| 543 | $m->Label( | ||
| 544 | -text => 'Style:', | ||
| 545 | -width => 6, | ||
| 546 | -anchor => 'e', | ||
| 547 | )->pack(-side => 'left'); | ||
| 548 | my $style = 'Unified'; | ||
| 549 | my $mb = $m->Menubutton( | ||
| 550 | -anchor => 'w', | ||
| 551 | -textvariable => \$style, | ||
| 552 | )->pack(@padding, -side => 'left', -fill => 'x'); | ||
| 553 | my @menu = (); | ||
| 554 | for ('Unified', 'Context', 'OldStyle') { | ||
| 555 | my $operation = $_; | ||
| 556 | push @menu, [command => $operation, | ||
| 557 | -command => sub { $style = $operation }, | ||
| 558 | ]; | ||
| 559 | } | ||
| 560 | $mb->configure(-menu => $mb->Menu( | ||
| 561 | -menuitems => \@menu, | ||
| 562 | )); | ||
| 563 | |||
| 564 | my $result = $dialog->show( | ||
| 565 | -popover => $self->toplevel, | ||
| 566 | ); | ||
| 567 | if ($result eq 'Ok') { | ||
| 568 | my $file = $e->get; | ||
| 569 | my $txt = $self->docWidget; | ||
| 570 | $txt->patchdiff($file, $style) if -e $file; | ||
| 571 | } | ||
| 572 | $dialog->destroy | ||
| 573 | } | ||
| 574 | |||
| 575 | sub docOption { | ||
| 576 | my $self = shift; | ||
| 577 | my $item = shift; | ||
| 578 | croak 'Option is not defined' unless defined $item; | ||
| 579 | return if $self->configMode; | ||
| 580 | my $sel = $self->docSelected; | ||
| 581 | return unless defined $sel; | ||
| 582 | my $doc = $self->docGet($sel); | ||
| 583 | if (@_) { | ||
| 584 | $doc->configure($item, shift); | ||
| 585 | } | ||
| 586 | return $doc->cget($item); | ||
| 587 | } | ||
| 588 | |||
| 589 | sub docPopFindReplace { | ||
| 590 | my ($self, $flag) = @_; | ||
| 591 | my $sel = $self->docSelected; | ||
| 592 | return unless defined $sel; | ||
| 593 | my $doc = $self->docGet($sel); | ||
| 594 | $doc->CWidg->FindAndOrReplace($flag); | ||
| 595 | } | ||
| 596 | |||
| 597 | sub docRemoveTrailing { | ||
| 598 | my ($self, $name) = @_; | ||
| 599 | $name = $self->docSelected unless defined $name; | ||
| 600 | my $widg = $self->docGet($name)->CWidg; | ||
| 601 | $widg->removeTrailingSpaces; | ||
| 602 | } | ||
| 603 | |||
| 604 | sub docSelect { | ||
| 605 | my ($self, $name) = @_; | ||
| 606 | return if $self->selectDisabled; | ||
| 607 | $self->SUPER::docSelect($name); | ||
| 608 | } | ||
| 609 | |||
| 610 | sub docShowSpaces { | ||
| 611 | my $self = shift; | ||
| 612 | return $self->docOption('-contentshowspaces', @_); | ||
| 613 | } | ||
| 614 | |||
| 615 | sub docSelectFirst { | ||
| 616 | my $self = shift; | ||
| 617 | my $sel = $self->docSelected; | ||
| 618 | unless (defined $sel) { | ||
| 619 | my @list = $self->docFullList; | ||
| 620 | $self->cmdExecute('doc_select', $list[0]) if @list; | ||
| 621 | } | ||
| 622 | } | ||
| 623 | |||
| 624 | sub docViewFolds { | ||
| 625 | my $self = shift; | ||
| 626 | return $self->docOption('-showfolds', @_); | ||
| 627 | } | ||
| 628 | |||
| 629 | sub docViewNumbers { | ||
| 630 | my $self = shift; | ||
| 631 | return $self->docOption('-shownumbers', @_); | ||
| 632 | } | ||
| 633 | |||
| 634 | sub docViewStatus { | ||
| 635 | my $self = shift; | ||
| 636 | return $self->docOption('-showstatus', @_); | ||
| 637 | } | ||
| 638 | |||
| 639 | =item B<docWidget> | ||
| 640 | |||
| 641 | Returns a reference to the Tk::CodeText widget | ||
| 642 | of the current selected document. Returns undef | ||
| 643 | if no document is selected. | ||
| 644 | |||
| 645 | =cut | ||
| 646 | |||
| 647 | sub docWidget { | ||
| 648 | my $self = shift; | ||
| 649 | my $name = $self->docSelected; | ||
| 650 | return undef unless defined $name; | ||
| 651 | my $doc = $self->docGet($name); | ||
| 652 | return undef unless defined $doc; | ||
| 653 | return $doc->CWidg; | ||
| 654 | } | ||
| 655 | |||
| 656 | sub docWrap { | ||
| 657 | my $self = shift; | ||
| 658 | return $self->docOption('-contentwrap', @_); | ||
| 659 | } | ||
| 660 | |||
| 661 | =item B<editDelete>I<($begin, $end)> | ||
| 662 | |||
| 663 | Deletes text in the currently selected document. It takes two indices as parameters. | ||
| 664 | |||
| 665 | =cut | ||
| 666 | |||
| 667 | sub editDelete { | ||
| 668 | my $self = shift; | ||
| 669 | my $doc = $self->docSelected; | ||
| 670 | return unless defined $doc; | ||
| 671 | $self->docGet($doc)->delete(@_); | ||
| 672 | } | ||
| 673 | |||
| 674 | |||
| 675 | =item B<editInsert>I<($index, $text)> | ||
| 676 | |||
| 677 | Inserts text in the currently selected document. It takes an index and the text as parameters. | ||
| 678 | |||
| 679 | =cut | ||
| 680 | |||
| 681 | sub editInsert { | ||
| 682 | my $self = shift; | ||
| 683 | my $doc = $self->docSelected; | ||
| 684 | return unless defined $doc; | ||
| 685 | $self->docGet($doc)->insert(@_); | ||
| 686 | } | ||
| 687 | |||
| 688 | =item B<editReplace>I<($begin, $end, $text)> | ||
| 689 | |||
| 690 | Inserts text in the currently selected document. It takes indices $begin and $end and the text as parameters. | ||
| 691 | |||
| 692 | =cut | ||
| 693 | |||
| 694 | sub editReplace { | ||
| 695 | my $self = shift; | ||
| 696 | my $doc = $self->docSelected; | ||
| 697 | return unless defined $doc; | ||
| 698 | $self->docGet($doc)->replace(@_); | ||
| 699 | } | ||
| 700 | |||
| 701 | sub keyReleased { | ||
| 702 | # my ($self, $name, $key) = @_; | ||
| 703 | } | ||
| 704 | |||
| 705 | =back | ||
| 706 | |||
| 707 | Macros are callbacks executed in the background. For each line in the document the macro is linked to, | ||
| 708 | the callback is executed with a reference to the text widget and the line number as parameter. | ||
| 709 | the macro ends after the last line has been processed. Codit uses macro callback to do tasks like show | ||
| 710 | leading and trailing tabs and spaces and reparing indentation. | ||
| 711 | |||
| 712 | =over 4 | ||
| 713 | |||
| 714 | =item B<macroGet>I<($doc, $name)> | ||
| 715 | |||
| 716 | Returns a reference to the macro object $name belonging to $doc. | ||
| 717 | |||
| 718 | =cut | ||
| 719 | |||
| 720 | sub macroGet { | ||
| 721 | my ($self, $doc, $name) = @_; | ||
| 722 | my $mcr = $self->_mcr; | ||
| 723 | return unless exists $mcr->{$doc}; | ||
| 724 | my $l = $mcr->{$doc}; | ||
| 725 | for (@$l) { | ||
| 726 | my $n = $_->name; | ||
| 727 | return $_ if $_->name eq $name | ||
| 728 | } | ||
| 729 | return undef | ||
| 730 | } | ||
| 731 | |||
| 732 | =item B<macroInit>I<($doc, $name, $call)> | ||
| 733 | |||
| 734 | Creates a new macro object $name for $doc with $call as callback. | ||
| 735 | |||
| 736 | =cut | ||
| 737 | |||
| 738 | sub macroInit { | ||
| 739 | my ($self, $doc, $name, $call) = @_; | ||
| 740 | unless (defined $self->macroGet($doc, $name)) { | ||
| 741 | my $macro = App::Codit::Macro->new($self, $name, $doc, $call); | ||
| 742 | my $mcr = $self->_mcr; | ||
| 743 | $mcr->{$doc} = [] unless exists $mcr->{$doc}; | ||
| 744 | my $l = $mcr->{$doc}; | ||
| 745 | push @$l, $macro; | ||
| 746 | return $macro; | ||
| 747 | } | ||
| 748 | warn "macro $name for $doc already exists"; | ||
| 749 | return undef | ||
| 750 | } | ||
| 751 | |||
| 752 | =item B<macroList>I<($doc)> | ||
| 753 | |||
| 754 | Returns a list with the objects of loaded macros for $doc. | ||
| 755 | |||
| 756 | =cut | ||
| 757 | |||
| 758 | sub macroList { | ||
| 759 | my ($self, $doc) = @_; | ||
| 760 | my $mcr = $self->_mcr; | ||
| 761 | return unless exists $mcr->{$doc}; | ||
| 762 | my $l = $mcr->{$doc}; | ||
| 763 | return @$l; | ||
| 764 | } | ||
| 765 | |||
| 766 | =item B<macroRemove>I<($doc, $name)> | ||
| 767 | |||
| 768 | Removes macro $name for $doc from the stack. | ||
| 769 | |||
| 770 | =cut | ||
| 771 | |||
| 772 | sub macroRemove { | ||
| 773 | my ($self, $doc, $name) = @_; | ||
| 774 | if (defined $self->macroGet($doc, $name)) { | ||
| 775 | my $mcr = $self->_mcr; | ||
| 776 | my $l = $mcr->{$doc}; | ||
| 777 | my @list = @$l; | ||
| 778 | my $count = 0; | ||
| 779 | for (@list) { | ||
| 780 | if ($_->name eq $name) { | ||
| 781 | $_->stop; | ||
| 782 | last; | ||
| 783 | } else { | ||
| 784 | $count ++; | ||
| 785 | } | ||
| 786 | } | ||
| 787 | splice @list, $count, 1; | ||
| 788 | if (@list) { | ||
| 789 | $mcr->{$doc} = \@list; | ||
| 790 | } else { | ||
| 791 | delete $mcr->{$doc} | ||
| 792 | } | ||
| 793 | return | ||
| 794 | } | ||
| 795 | warn "macro $name for $doc does not exist" | ||
| 796 | } | ||
| 797 | |||
| 798 | =item B<macroRemoveAll>I<($doc)> | ||
| 799 | |||
| 800 | Removes all macros for $doc from the stack. | ||
| 801 | |||
| 802 | =cut | ||
| 803 | |||
| 804 | sub macroRemoveAll { | ||
| 805 | my ($self, $doc) = @_; | ||
| 806 | my @list = $self->macroList($doc); | ||
| 807 | for (@list) { | ||
| 808 | $self->macroRemove($doc, $_->name); | ||
| 809 | } | ||
| 810 | } | ||
| 811 | |||
| 812 | sub MenuItems { | ||
| 813 | my $self = shift; | ||
| 814 | |||
| 815 | #creating the context menu for the navigator tree | ||
| 816 | $self->after(1000, ['navContextMenu', $self]); | ||
| 817 | |||
| 818 | my @items = $self->SUPER::MenuItems; | ||
| 819 | return (@items, | ||
| 820 | [ 'menu', undef, '~Edit'], | ||
| 821 | [ 'menu_normal', 'Edit::', '~Copy', '<Control-c>', 'edit-copy', '*CTRL+C'], | ||
| 822 | [ 'menu_normal', 'Edit::', 'C~ut', '<Control-x>', 'edit-cut', '*CTRL+X'], | ||
| 823 | [ 'menu_normal', 'Edit::', '~Paste', '<Control-v>', 'edit-paste', '*CTRL+V'], | ||
| 824 | [ 'menu_separator', 'Edit::', 'e1' ], | ||
| 825 | [ 'menu_normal', 'Edit::', 'U~ndo', '<Control-z>', 'edit-undo', '*CTRL+Z'], | ||
| 826 | [ 'menu_normal', 'Edit::', '"~Redo', '<Control-Z>', 'edit-redo', '*CTRL+SHIFT+Z'], | ||
| 827 | [ 'menu_separator', 'Edit::', 'e2'], | ||
| 828 | [ 'menu_normal', 'Edit::', 'Co~mment', '<Control-g>', undef, '*CTRL+G'], | ||
| 829 | [ 'menu_normal', 'Edit::', '~Uncomment', '<Control-G>', undef, '*CTRL+SHIFT+G'], | ||
| 830 | [ 'menu_separator', 'Edit::', 'e3' ], | ||
| 831 | [ 'menu_normal', 'Edit::', '~Indent', '<Control-j>', 'format-indent-more','*CTRL+J'], | ||
| 832 | [ 'menu_normal', 'Edit::', 'Unin~dent', '<Control-J>', 'format-indent-less','*CTRL+SHIFT+J'], | ||
| 833 | [ 'menu_separator', 'Edit::', 'e4' ], | ||
| 834 | [ 'menu_normal', 'Edit::', 'U~pper case', 'doc_case_upper', 'format-text-uppercase','*CTRL+I'], | ||
| 835 | [ 'menu_normal', 'Edit::', '~Lower case', 'doc_case_lower', 'format-text-lowercase','*CTL+SHIFT+I'], | ||
| 836 | [ 'menu_separator', 'Edit::', 'e5' ], | ||
| 837 | [ 'menu_normal', 'Edit::', '~Select all', '<Control-a>', 'edit-select-all','*CTRL+A'], | ||
| 838 | |||
| 839 | [ 'menu', undef, '~Bookmarks', 'bookmark_fill'], | ||
| 840 | [ 'menu_normal', 'Bookmarks::', '~Add bookmark', 'bookmark_add', 'bookmark_new', 'CTRL+B',], | ||
| 841 | [ 'menu_normal', 'Bookmarks::', '~Remove bookmark', 'bookmark_remove', 'bookmark_remove', 'CTRL+SHIFT+B',], | ||
| 842 | [ 'menu_normal', 'Bookmarks::', '~Clear bookmarks', 'bookmark_clear', undef], | ||
| 843 | [ 'menu_separator', 'Bookmarks::', 'b1' ], | ||
| 844 | [ 'menu_normal', 'Bookmarks::', '~Previous bookmark', 'bookmark_prev', 'bookmark_previous'], | ||
| 845 | [ 'menu_normal', 'Bookmarks::', '~Next bookmark', 'bookmark_next', 'bookmark_next'], | ||
| 846 | [ 'menu_separator', 'Bookmarks::', 'b2' ], | ||
| 847 | |||
| 848 | [ 'menu_separator', 'View::', 'v1' ], | ||
| 849 | [ 'menu_check', 'View::', 'Show ~folds', undef, '-doc_view_folds', undef, 0, 1], | ||
| 850 | [ 'menu_check', 'View::', 'Show ~line numbers', undef, '-doc_view_numbers', undef, 0, 1], | ||
| 851 | [ 'menu_check', 'View::', 'Show ~document status',undef, '-doc_view_status', undef, 0, 1], | ||
| 852 | [ 'menu_separator', 'View::', 'v2' ], | ||
| 853 | [ 'menu_check', 'View::', 'Show ~spaces and tabs','show-spaces', '-doc_show_spaces', undef, 0, 1], | ||
| 854 | |||
| 855 | [ 'menu', undef, '~Tools'], | ||
| 856 | [ 'menu_normal', 'Tools::', '~Find', 'doc_find', 'edit-find', '*CTRL+F',], | ||
| 857 | [ 'menu_normal', 'Tools::', '~Replace', 'doc_replace', 'edit-find-replace','*CTRL+R',], | ||
| 858 | [ 'menu_separator', 'Tools::', 't1' ], | ||
| 859 | [ 'menu_check', 'Tools::', 'A~uto indent', undef, '-doc_autoindent', undef, 0, 1], | ||
| 860 | [ 'menu_check', 'Tools::', 'A~uto brackets', undef, '-doc_autobrackets', undef, 0, 1], | ||
| 861 | [ 'menu_check', 'Tools::', 'A~uto complete', undef, '-doc_autocomplete', undef, 0, 1], | ||
| 862 | [ 'menu_radio_s', 'Tools::', '~Wrap', [qw/char word none/], undef, '-doc_wrap'], | ||
| 863 | [ 'menu_separator', 'Tools::', 't1' ], | ||
| 864 | [ 'menu_normal', 'Tools::', '~Patch', 'doc_patch',], | ||
| 865 | [ 'menu_normal', 'Tools::', 'R~emove trailing spaces', 'doc_remove_trailing',], | ||
| 866 | [ 'menu_normal', 'Tools::', 'F~ix indentation', 'doc_fix_indent',], | ||
| 867 | ); | ||
| 868 | } | ||
| 869 | |||
| 870 | sub navCollapse { | ||
| 871 | my $self = shift; | ||
| 872 | my $tree = $self->Subwidget('NAVTREE'); | ||
| 873 | $tree->collapseAll; | ||
| 874 | } | ||
| 875 | |||
| 876 | sub navContextMenu { | ||
| 877 | my $self = shift; | ||
| 878 | my $nav = $self->Subwidget('NAVTREE'); | ||
| 879 | my @items = @navcontextmenu; | ||
| 880 | |||
| 881 | #checking if Git is loaded; | ||
| 882 | my $git = $self->extGet('Plugins')->plugGet('Git'); | ||
| 883 | push @items, [ 'menu_normal', 'c1', '~Add to project', 'git_add', 'git-icon',] if defined $git; | ||
| 884 | |||
| 885 | my $stack = $self->extGet('MenuBar')->menuStack(@items); | ||
| 886 | $nav->configure('-contextmenu', $stack); | ||
| 887 | } | ||
| 888 | |||
| 889 | sub navExpand { | ||
| 890 | my $self = shift; | ||
| 891 | my $tree = $self->Subwidget('NAVTREE'); | ||
| 892 | $tree->expandAll; | ||
| 893 | } | ||
| 894 | |||
| 895 | sub SettingsPage { | ||
| 896 | my $self = shift; | ||
| 897 | my ($first) = $self->docList; | ||
| 898 | my $doc = $self->docGet($first); | ||
| 899 | return () unless defined $doc; | ||
| 900 | my $themefile = $doc->cget('-highlight_themefile'); | ||
| 901 | my $historyfile = $self->extGet('ConfigFolder')->ConfigFolder . '/color_history'; | ||
| 902 | my @opt = ( | ||
| 903 | -applycall => sub { | ||
| 904 | my $themefile = shift; | ||
| 905 | my @list = $self->docList; | ||
| 906 | for (@list) { | ||
| 907 | my $d = $self->docGet($_); | ||
| 908 | $d->configure(-highlight_themefile => $themefile); | ||
| 909 | $d->configureTags; | ||
| 910 | } | ||
| 911 | }, | ||
| 912 | -defaultbackground => $doc->cget('-contentbackground'), | ||
| 913 | -defaultforeground => $doc->cget('-contentforeground'), | ||
| 914 | -defaultfont => $doc->CWidg->Subwidget('XText')->cget('-font'), | ||
| 915 | -historyfile => $historyfile, | ||
| 916 | -extension => $self, | ||
| 917 | -themefile => $themefile, | ||
| 918 | ); | ||
| 919 | return ( | ||
| 920 | 'Highlighting' => ['CoditTagsEditor', @opt] | ||
| 921 | ) | ||
| 922 | } | ||
| 923 | |||
| 924 | sub ToolItems { | ||
| 925 | my $self = shift; | ||
| 926 | my @items = $self->SUPER::ToolItems; | ||
| 927 | return (@items, | ||
| 928 | # type label cmd icon help | ||
| 929 | [ 'tool_separator' ], | ||
| 930 | [ 'tool_button', 'Copy', '<Control-c>', 'edit-copy', 'Copy selected text to clipboard'], | ||
| 931 | [ 'tool_button', 'Cut', '<Control-x>', 'edit-cut', 'Move selected text to clipboard'], | ||
| 932 | [ 'tool_button', 'Paste', '<Control-v>', 'edit-paste', 'Paste clipboard content into document'], | ||
| 933 | [ 'tool_separator' ], | ||
| 934 | [ 'tool_button', 'Undo', '<Control-z>', 'edit-undo', 'Undo last action'], | ||
| 935 | [ 'tool_button', 'Redo', '<Control-Z>', 'edit-redo', 'Cancel undo'], | ||
| 936 | ); | ||
| 937 | } | ||
| 938 | |||
| 939 | =back | ||
| 940 | |||
| 941 | =head1 LICENSE | ||
| 942 | |||
| 943 | Same as Perl. | ||
| 944 | |||
| 945 | =head1 AUTHOR | ||
| 946 | |||
| 947 | Hans Jeuken (hanje at cpan dot org) | ||
| 948 | |||
| 949 | =head1 BUGS | ||
| 950 | |||
| 951 | Unknown. If you find any, please report them here L<https://github.com/haje61/App-Codit/issues>. | ||
| 952 | |||
| 953 | =head1 SEE ALSO | ||
| 954 | |||
| 955 | =over 4 | ||
| 956 | |||
| 957 | =item L<App::Codit::Macro> | ||
| 958 | |||
| 959 | =item L<Tk::AppWindow::Ext::MDI> | ||
| 960 | |||
| 961 | =back | ||
| 962 | |||
| 963 | =cut | ||
| 964 | |||
| 965 | 1; |