| 1 | #! perl | ||
| 2 | |||
| 3 | use v5.26; | ||
| 4 | use Object::Pad; | ||
| 5 | use utf8; | ||
| 6 | |||
| 7 | class ChordPro::Wx::EditorPanel | ||
| 8 | :repr(HASH) | ||
| 9 | :does( ChordPro::Wx::PanelRole ) | ||
| 10 | :isa( ChordPro::Wx::EditorPanel_wxg ); | ||
| 11 | |||
| 12 | use Wx qw[:everything]; | ||
| 13 | use Wx::Locale gettext => '_T'; | ||
| 14 | |||
| 15 | use ChordPro::Files; | ||
| 16 | use ChordPro::Wx::Config; | ||
| 17 | use ChordPro::Wx::Utils; | ||
| 18 | use ChordPro::Utils qw( max demarkup plural ); | ||
| 19 | use ChordPro::Paths; | ||
| 20 | |||
| 21 | # WhoamI | ||
| 22 | field $panel :accessor = "editor"; | ||
| 23 | |||
| 24 | # Just fill in the defaults. | ||
| 25 | sub BUILDARGS( $class, $parent=undef, $id=wxID_ANY, | ||
| 26 | $pos=wxDefaultPosition, $size=wxDefaultSize, | ||
| 27 | $style=0, $name="" ) { | ||
| 28 | return( $parent, $id, $pos, $size, $style, $name ); | ||
| 29 | } | ||
| 30 | |||
| 31 | BUILD { | ||
| 32 | # By default the TextCtrl on MacOS substitutes smart quotes and dashes. | ||
| 33 | # Note that OSXDisableAllSmartSubstitutions requires an augmented | ||
| 34 | # version of wxPerl. | ||
| 35 | $self->{t_editor}->OSXDisableAllSmartSubstitutions; | ||
| 36 | |||
| 37 | # Setup WebView, if possible. | ||
| 38 | $self->setup_webview if $::options->{webview}//1; | ||
| 39 | |||
| 40 | # Single pane. | ||
| 41 | $self->unsplit; | ||
| 42 | |||
| 43 | $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | ################ ################ | ||
| 47 | |||
| 48 | method name() { "Editor" } | ||
| 49 | |||
| 50 | ################ API Functions ################ | ||
| 51 | |||
| 52 | method refresh() { | ||
| 53 | |||
| 54 | $self->setup_logger; | ||
| 55 | |||
| 56 | $self->update_menubar( M_EDITOR ); | ||
| 57 | |||
| 58 | # Flush pending messages. | ||
| 59 | if ( $state{msgs} ) { | ||
| 60 | $self->log( 'I', $_ ) for @{$state{msgs}}; | ||
| 61 | $state{msgs} = []; | ||
| 62 | } | ||
| 63 | $self->log( 'I', "Using " . | ||
| 64 | ( $state{have_stc} | ||
| 65 | ? "styled" : "basic") . " text editor" ); | ||
| 66 | |||
| 67 | $state{have_webview} = ref($self->{webview}) eq 'Wx::WebView'; | ||
| 68 | $self->log( 'I', "Using " . | ||
| 69 | ( $state{have_webview} | ||
| 70 | ? "embedded" : "external") . " PDF viewer" ); | ||
| 71 | |||
| 72 | if ( $state{from_songbook} ) { | ||
| 73 | $self->{bmp_songbook}->Show(1); | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | $self->{bmp_songbook}->Show(0); | ||
| 77 | } | ||
| 78 | $self->{sz_toolbar}->Layout; | ||
| 79 | |||
| 80 | my $mod = $self->{t_editor}->IsModified; | ||
| 81 | $self->{t_editor}->refresh; | ||
| 82 | |||
| 83 | $self->setup_messages_ctxmenu; | ||
| 84 | $self->previewtooltip; | ||
| 85 | $self->messagestooltip; | ||
| 86 | $self->{t_editor}->SetModified($mod); | ||
| 87 | |||
| 88 | $self->refresh_messages; | ||
| 89 | |||
| 90 | if ( $state{have_stc} ) { | ||
| 91 | Wx::Event::EVT_STC_CHARADDED( $self, $self->{t_editor}->GetId, | ||
| 92 | $self->can("OnCharAdded") ); | ||
| 93 | Wx::Event::EVT_STC_CLIPBOARD_PASTE( $self, $self->{t_editor}->GetId, | ||
| 94 | $self->can("OnClipBoardPaste") ); | ||
| 95 | } | ||
| 96 | $self->set_focus; | ||
| 97 | } | ||
| 98 | |||
| 99 | method openfile( $file, $checked=0, $actual=undef ) { | ||
| 100 | $actual //= $file; | ||
| 101 | |||
| 102 | # Bypass test when already checked. TODO? | ||
| 103 | unless ( $checked || fs_test( 'fr', $file ) ) { | ||
| 104 | $self->log( 'W', "Error opening $file: $!",); | ||
| 105 | my $md = Wx::MessageDialog->new | ||
| 106 | ( $self, | ||
| 107 | "Error opening $file: $!", | ||
| 108 | "File open error", | ||
| 109 | wxOK | wxICON_ERROR ); | ||
| 110 | my $ret = $md->ShowModal; | ||
| 111 | $md->Destroy; | ||
| 112 | return; | ||
| 113 | } | ||
| 114 | if ( my $f = fs_load($file) ) { | ||
| 115 | |||
| 116 | # Check for filelist. | ||
| 117 | if ( $f->[0] =~ m;^//\s*chordpro\s+songbook;i ) { | ||
| 118 | $f->[0] = $file; | ||
| 119 | return $self->GetParent->select_mode("sbexport")->load_filelist($f) | ||
| 120 | } | ||
| 121 | |||
| 122 | # This has the (desired) sideeffect that all newlines | ||
| 123 | # are now \n . | ||
| 124 | $self->{t_editor}->SetText(join("\n",@$f)."\n"); | ||
| 125 | $self->{t_editor}->DiscardEdits; | ||
| 126 | } | ||
| 127 | else { | ||
| 128 | $self->log( 'W', "Error opening $file: $!",); | ||
| 129 | my $md = Wx::MessageDialog->new | ||
| 130 | ( $self, | ||
| 131 | "Error opening $file: $!", | ||
| 132 | "File load error", | ||
| 133 | wxOK | wxICON_ERROR ); | ||
| 134 | $md->ShowModal; | ||
| 135 | $md->Destroy; | ||
| 136 | return; | ||
| 137 | } | ||
| 138 | #### TODO: Get rid of selection on Windows | ||
| 139 | |||
| 140 | if ( $state{have_stc} ) { | ||
| 141 | my $stc = $self->{t_editor}; | ||
| 142 | my @t = split( /\n/, $stc->GetText ); | ||
| 143 | my $max = -1; | ||
| 144 | for ( @t ) { | ||
| 145 | $max = max( $max, length($_) ); | ||
| 146 | } | ||
| 147 | $stc->SetScrollWidth($max); | ||
| 148 | $stc->SetScrollWidthTracking(1) | ||
| 149 | if $stc->can("SetScrollWidthTracking"); | ||
| 150 | } | ||
| 151 | else { | ||
| 152 | $self->{t_editor}->ShowPosition(0); # doesn't work? | ||
| 153 | } | ||
| 154 | $self->{t_editor}->SetFocus; | ||
| 155 | |||
| 156 | if ( $actual =~ /^\s+.*\s+$/ ) { | ||
| 157 | $state{currentfile} = undef; | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | $state{currentfile} = $actual; | ||
| 161 | use List::Util qw(uniq); | ||
| 162 | @{$state{recents}} = uniq( $file, @{$state{recents}} ); | ||
| 163 | } | ||
| 164 | if ( $self->{t_editor}->GetText =~ /^\{\s*t(?:itle)?[: ]+([^\}]*)\}/m ) { | ||
| 165 | my $title = demarkup($1); | ||
| 166 | my $n = $self->{t_editor}->GetLineCount; | ||
| 167 | $self->log( 'S', "Loaded: $title (" . plural($n, " line") . ")"); | ||
| 168 | $self->{l_status}->SetLabel($title); | ||
| 169 | $self->{l_status}->SetToolTip($file); | ||
| 170 | } | ||
| 171 | else { | ||
| 172 | my $n = $self->{t_editor}->GetLineCount; | ||
| 173 | $self->{l_status}->SetLabel(fn_basename($file)); | ||
| 174 | $self->{l_status}->SetToolTip($file); | ||
| 175 | $self->log( 'S', "Loaded: $file (" . plural($n, " line") . ")"); | ||
| 176 | } | ||
| 177 | if ( $state{have_stc} && $preferences{expert} ) { | ||
| 178 | $self->log( 'S', "Line endings: " . | ||
| 179 | (qw(CRLF CR LF))[$self->{t_editor}->GetEOLMode] ); | ||
| 180 | } | ||
| 181 | $self->GetParent->SetTitle( $state{windowtitle} = $actual); | ||
| 182 | |||
| 183 | if ( $self->{sw_lr}->IsSplit ) { | ||
| 184 | $self->{sw_lr}->Unsplit(undef); | ||
| 185 | $self->previewtooltip; | ||
| 186 | } | ||
| 187 | if ( $self->{sw_tb}->IsSplit ) { | ||
| 188 | $self->{sw_tb}->Unsplit(undef); | ||
| 189 | $self->messagestooltip; | ||
| 190 | } | ||
| 191 | $self->prv->discard if $self->prv; | ||
| 192 | |||
| 193 | return 1; | ||
| 194 | } | ||
| 195 | |||
| 196 | method newfile( $file = undef ) { | ||
| 197 | my $title = "New Song"; | ||
| 198 | |||
| 199 | if ( defined($file) ) { | ||
| 200 | my $t = $file; | ||
| 201 | $t =~ s/\.\w+//; | ||
| 202 | $t =~ s/_/ /g; | ||
| 203 | $t =~ s/\s+/ /g; | ||
| 204 | $t =~ s/^\s+//; | ||
| 205 | $t =~ s/\s+$//; | ||
| 206 | $title = join( " ", map { ucfirst($_) } split( ' ', $t ) ); | ||
| 207 | } | ||
| 208 | else { | ||
| 209 | delete $state{currentfile}; | ||
| 210 | } | ||
| 211 | |||
| 212 | $state{windowtitle} = $title; | ||
| 213 | $self->{l_status}->SetLabel($title); | ||
| 214 | my $content = "{title: $title}"; | ||
| 215 | $self->{t_editor}->SetText($content); | ||
| 216 | |||
| 217 | my $file = $preferences{tmplfile}; | ||
| 218 | if ( $file && $preferences{enable_tmplfile} ) { | ||
| 219 | $self->log( 'I', "Loading template $file" ); | ||
| 220 | if ( fs_test( fr => $file ) && $self->{t_editor}->LoadFile($file) ) { | ||
| 221 | $content = ""; | ||
| 222 | } | ||
| 223 | else { | ||
| 224 | $self->log( 'E', "Cannot open template $file: $!" ); | ||
| 225 | } | ||
| 226 | } | ||
| 227 | else { | ||
| 228 | $content = "{title: $title}"; | ||
| 229 | } | ||
| 230 | |||
| 231 | for ( $self->{t_editor} ) { | ||
| 232 | $content =~ s/[\n\r]*\Z//; | ||
| 233 | $_->SetText($content) if length($content); | ||
| 234 | $_->DocumentEnd; | ||
| 235 | $_->NewLine; | ||
| 236 | $_->SetSelection(0,0); | ||
| 237 | $_->SetFocus; | ||
| 238 | } | ||
| 239 | |||
| 240 | $self->log( 'S', "New song: $title"); | ||
| 241 | if ( $state{have_stc} && $preferences{expert} ) { | ||
| 242 | $self->log( 'S', "Line endings: " . | ||
| 243 | (qw(CRLF CR LF))[$self->{t_editor}->GetEOLMode] ); | ||
| 244 | } | ||
| 245 | $state{windowtitle} = $title; | ||
| 246 | $self->{l_status}->SetLabel($title); | ||
| 247 | $self->{l_status}->SetToolTip($state{currentfile}); | ||
| 248 | if ( $self->{sw_lr}->IsSplit ) { | ||
| 249 | $self->{sw_lr}->Unsplit(undef); | ||
| 250 | $self->previewtooltip; | ||
| 251 | } | ||
| 252 | if ( $self->{sw_tb}->IsSplit ) { | ||
| 253 | $self->{sw_tb}->Unsplit(undef); | ||
| 254 | $self->messagestooltip; | ||
| 255 | } | ||
| 256 | $self->prv->discard if $self->prv; | ||
| 257 | |||
| 258 | 1; | ||
| 259 | } | ||
| 260 | |||
| 261 | method check_source_saved() { | ||
| 262 | # Do we need saving? | ||
| 263 | return 1 unless ( $self->{t_editor} && $self->{t_editor}->IsModified ); | ||
| 264 | |||
| 265 | # Do we have a filename? | ||
| 266 | if ( $state{currentfile} ) { | ||
| 267 | my $md = Wx::MessageDialog->new | ||
| 268 | ( $self, | ||
| 269 | "File " . fn_basename($state{currentfile}) . " has been changed.\n". | ||
| 270 | "Do you want to save your changes?", | ||
| 271 | "File has changed", | ||
| 272 | 0 | wxCANCEL | wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION ); | ||
| 273 | my $ret = $md->ShowModal; | ||
| 274 | $md->Destroy; | ||
| 275 | return if $ret == wxID_CANCEL; | ||
| 276 | if ( $ret == wxID_YES ) { | ||
| 277 | $self->save_file( $state{currentfile} ); | ||
| 278 | } | ||
| 279 | else { | ||
| 280 | $self->{t_editor}->SetModified(0); | ||
| 281 | } | ||
| 282 | } | ||
| 283 | else { | ||
| 284 | my $md = Wx::MessageDialog->new | ||
| 285 | ( $self, | ||
| 286 | "Do you want to save your changes?", | ||
| 287 | "Contents has changed", | ||
| 288 | 0 | wxCANCEL | wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION ); | ||
| 289 | my $ret = $md->ShowModal; | ||
| 290 | $md->Destroy; | ||
| 291 | return if $ret == wxID_CANCEL; | ||
| 292 | if ( $ret == wxID_YES ) { | ||
| 293 | $self->save_file; | ||
| 294 | } | ||
| 295 | else { | ||
| 296 | $self->{t_editor}->SetModified(0); | ||
| 297 | } | ||
| 298 | } | ||
| 299 | return 1; | ||
| 300 | } | ||
| 301 | |||
| 302 | method save_file( $file = undef ) { | ||
| 303 | while ( 1 ) { | ||
| 304 | unless ( defined $file && $file ne "" ) { | ||
| 305 | my $cf = $state{currentfile} // "Untitled"; | ||
| 306 | my $fd = Wx::FileDialog->new | ||
| 307 | ($self, _T("Choose output file"), | ||
| 308 | fn_dirname($cf), fn_basename($cf), | ||
| 309 | "*".$preferences{chordproext}, | ||
| 310 | 0|wxFD_SAVE|wxFD_OVERWRITE_PROMPT, | ||
| 311 | wxDefaultPosition); | ||
| 312 | my $ret = $fd->ShowModal; | ||
| 313 | if ( $ret == wxID_OK ) { | ||
| 314 | $file = $fd->GetPath; | ||
| 315 | } | ||
| 316 | $fd->Destroy; | ||
| 317 | } | ||
| 318 | return unless defined $file; | ||
| 319 | |||
| 320 | # On macOS Catalina DMG STC seems to have problems saving. | ||
| 321 | my $fd = fs_open( $file, '>:utf8' ); | ||
| 322 | $self->{t_editor}->ConvertEOLs(wxSTC_EOL_LF); | ||
| 323 | my $t = $self->{t_editor}->GetText; | ||
| 324 | # $self->log( 'I', ChordPro::Utils::as($t)); | ||
| 325 | $t .= "\n" unless $t =~ /\n$/; | ||
| 326 | if ( $fd | ||
| 327 | and print $fd $t | ||
| 328 | and $fd->close ) { | ||
| 329 | $self->{t_editor}->SetModified(0); | ||
| 330 | $state{currentfile} = $file; | ||
| 331 | $state{windowtitle} = $file; | ||
| 332 | $self->log( 'S', "Saved: $file" ); | ||
| 333 | use List::Util qw(uniq); | ||
| 334 | @{$state{recents}} = uniq( $file, @{$state{recents}} ); | ||
| 335 | return; | ||
| 336 | } | ||
| 337 | |||
| 338 | my $md = Wx::MessageDialog->new | ||
| 339 | ( $self, | ||
| 340 | "Cannot save to $file\n$!", | ||
| 341 | "Error saving file", | ||
| 342 | 0 | wxOK | wxICON_ERROR); | ||
| 343 | $md->ShowModal; | ||
| 344 | $md->Destroy; | ||
| 345 | undef $file; | ||
| 346 | } | ||
| 347 | } | ||
| 348 | |||
| 349 | method preview( $args, %opts ) { | ||
| 350 | use ChordPro::Wx::Preview; | ||
| 351 | $self->prv //= ChordPro::Wx::Preview->new( panel => $self ); | ||
| 352 | |||
| 353 | my $mod = $self->{t_editor}->IsModified; | ||
| 354 | my $preview_cho = $self->prv->preview_cho; | ||
| 355 | unlink($preview_cho); | ||
| 356 | my $fd; | ||
| 357 | # The text that we get from the editor can have CRLF line endings, | ||
| 358 | # that on Windows will result in double line ends. Write with | ||
| 359 | # 'raw' layer. | ||
| 360 | require Encode; | ||
| 361 | if ( $fd = fs_open( $preview_cho, '>:raw' ) | ||
| 362 | and print $fd ( Encode::encode_utf8($self->{t_editor}->GetText) ) | ||
| 363 | and close($fd) ) { | ||
| 364 | $self->prv->preview( $args, %opts ); | ||
| 365 | $self->previewtooltip; | ||
| 366 | } | ||
| 367 | else { | ||
| 368 | $self->log( 'E', "$preview_cho: $!" ); | ||
| 369 | } | ||
| 370 | } | ||
| 371 | |||
| 372 | method check_preview_saved() { | ||
| 373 | # Do not ask for preview save. It's regenerated easily. | ||
| 374 | return 1; | ||
| 375 | |||
| 376 | return 1 unless $self->prv && $self->prv->unsaved_preview; | ||
| 377 | |||
| 378 | my $md = Wx::MessageDialog->new | ||
| 379 | ( $self, | ||
| 380 | "Do you want to save the preview as PDF?", | ||
| 381 | "Preview", | ||
| 382 | 0 | wxCANCEL | wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION ); | ||
| 383 | my $ret = $md->ShowModal; | ||
| 384 | $md->Destroy; | ||
| 385 | |||
| 386 | return 0 if $ret == wxID_CANCEL; | ||
| 387 | $self->prv->discard, return 1 if $ret == wxID_NO; # don't save | ||
| 388 | return $self->prv->save; | ||
| 389 | 1; | ||
| 390 | } | ||
| 391 | |||
| 392 | method embrace( $pre, $post, $nl = 1 ) { | ||
| 393 | my $ctrl = $self->{t_editor}; | ||
| 394 | |||
| 395 | my ( $from, $to ) = $ctrl->GetSelection; | ||
| 396 | my $have_selection = $from != $to; | ||
| 397 | |||
| 398 | if ( $have_selection ) { | ||
| 399 | my $text = $ctrl->GetSelectedText; | ||
| 400 | chomp($text); | ||
| 401 | $text = $pre . $text . $post; | ||
| 402 | $ctrl->Replace( $from, $to, $text ); | ||
| 403 | my $pos = $ctrl->GetCurrentPos; | ||
| 404 | $ctrl->SetSelection( $pos, $pos ); | ||
| 405 | } | ||
| 406 | else { | ||
| 407 | if ( $nl ) { | ||
| 408 | $nl = $self->nl; | ||
| 409 | my $ln = $ctrl->GetCurrentLine; | ||
| 410 | my $line = $ctrl->GetLine($ln); | ||
| 411 | #warn("LINE[$ln]: »$line«\n"); | ||
| 412 | if ( $line =~ /\R\z/ ) { | ||
| 413 | # Terminated | ||
| 414 | #warn("LINE[$ln]: terminated\n"); | ||
| 415 | } | ||
| 416 | else { | ||
| 417 | $ctrl->LineEnd; | ||
| 418 | $ctrl->NewLine; | ||
| 419 | $ctrl->CharLeft; | ||
| 420 | $ln = $ctrl->GetCurrentLine; | ||
| 421 | $line = $ctrl->GetLine($ln); | ||
| 422 | #warn("LINE[$ln]: »$line«\n"); | ||
| 423 | } | ||
| 424 | if ( $line eq $nl ) { | ||
| 425 | #warn("LINE[$ln]: empty\n"); | ||
| 426 | # Empty line | ||
| 427 | } | ||
| 428 | else { | ||
| 429 | $ctrl->LineEnd; | ||
| 430 | $ctrl->NewLine; | ||
| 431 | $ln = $ctrl->GetCurrentLine; | ||
| 432 | $line = $ctrl->GetLine($ln); | ||
| 433 | #warn("LINE[$ln]: »$line«\n"); | ||
| 434 | } | ||
| 435 | } | ||
| 436 | $ctrl->AddText($pre); | ||
| 437 | my $pos = $ctrl->GetCurrentPos; | ||
| 438 | $ctrl->AddText($post); | ||
| 439 | $ctrl->SetSelection( $pos, $pos ); | ||
| 440 | } | ||
| 441 | } | ||
| 442 | |||
| 443 | method nl() { | ||
| 444 | ("\r\n","\r","\n")[ $self->{t_editor}->GetEOLMode ]; | ||
| 445 | } | ||
| 446 | |||
| 447 | method embrace_directive($dir) { | ||
| 448 | $self->embrace( "{$dir: ", "}" ); | ||
| 449 | } | ||
| 450 | |||
| 451 | method embrace_section($section) { | ||
| 452 | |||
| 453 | unless ( defined($section) ) { | ||
| 454 | my $dialog = Wx::TextEntryDialog->new | ||
| 455 | ( $self, "Enter section name", | ||
| 456 | "", | ||
| 457 | "tab" ); | ||
| 458 | |||
| 459 | return if $dialog->ShowModal != wxID_OK; | ||
| 460 | $section = $dialog->GetValue; | ||
| 461 | } | ||
| 462 | |||
| 463 | my $nl = $self->nl; | ||
| 464 | $self->embrace( "{start_of_$section}$nl", | ||
| 465 | $nl."{end_of_$section}" ); | ||
| 466 | } | ||
| 467 | |||
| 468 | method save_preferences() { 1 } | ||
| 469 | |||
| 470 | method update_preferences() { | ||
| 471 | $self->refresh; | ||
| 472 | } | ||
| 473 | |||
| 474 | method set_focus { | ||
| 475 | $self->{t_editor}->SetFocus; | ||
| 476 | } | ||
| 477 | |||
| 478 | ################ Event Handlers (alphabetic order) ################ | ||
| 479 | |||
| 480 | method OnInsertSymbol($event) { | ||
| 481 | unless ( $preferences{enable_insert_symbols} ) { | ||
| 482 | my $md = Wx::MessageDialog->new | ||
| 483 | ( undef, | ||
| 484 | "Note: some symbols might not display properly in PDF". | ||
| 485 | " output, even if visible in the Editor. Make sure that". | ||
| 486 | " the symbols you use are supported by your output fonts.\n". | ||
| 487 | "\n". | ||
| 488 | "Continue and suppress future warnings?", | ||
| 489 | "Advanced operation warning", | ||
| 490 | wxYES_NO|wxNO_DEFAULT|wxICON_WARNING|wxDIALOG_NO_PARENT ); | ||
| 491 | return unless $md->ShowModal == wxID_YES; | ||
| 492 | $preferences{enable_insert_symbols} = 1; | ||
| 493 | |||
| 494 | } | ||
| 495 | my $ctrl = $self->{t_editor}; | ||
| 496 | state $sym = "\x{2665}"; | ||
| 497 | my $d = Wx::SymbolPickerDialog->new( $sym, "", "", $self ); | ||
| 498 | if ( $d->ShowModal == wxID_OK ) { | ||
| 499 | $ctrl->AddText( $sym = $d->GetSymbol ); | ||
| 500 | } | ||
| 501 | } | ||
| 502 | |||
| 503 | method OnA2Crd($event) { | ||
| 504 | |||
| 505 | my $ctrl = $self->{t_editor}; | ||
| 506 | my ( $from, $to ) = $ctrl->GetSelection; | ||
| 507 | my $have_selection = $from != $to; | ||
| 508 | $ctrl->ConvertEOLs(wxSTC_EOL_LF); | ||
| 509 | my $text = $have_selection ? $ctrl->GetSelectedText : $ctrl->GetText; | ||
| 510 | |||
| 511 | require ChordPro::A2Crd; | ||
| 512 | $::options->{nosysconfig} = 1; | ||
| 513 | $::options->{nouserconfig} = 1; | ||
| 514 | $::options->{noconfig} = 1; | ||
| 515 | $::options->{fragment} = $have_selection; | ||
| 516 | |||
| 517 | # Often text that is pasted from web has additional newlines. | ||
| 518 | $text =~ s/^\n+//; | ||
| 519 | if ( $text =~ m/(.+\n\n)+/ ) { | ||
| 520 | $text =~ s/(.+\n)\n/$1/g; | ||
| 521 | } | ||
| 522 | |||
| 523 | my $cho = join | ||
| 524 | ( "\n", | ||
| 525 | @{ ChordPro::A2Crd::a2crd | ||
| 526 | ( { lines => [ split( /\n/, $text ) ] } ) } ) . "\n"; | ||
| 527 | |||
| 528 | if ( $have_selection ) { | ||
| 529 | $ctrl->Replace( $from, $to, $cho ); | ||
| 530 | } | ||
| 531 | else { | ||
| 532 | $ctrl->Clear; | ||
| 533 | $ctrl->SetText($cho); | ||
| 534 | } | ||
| 535 | $ctrl->SetCurrentPos($from); | ||
| 536 | } | ||
| 537 | |||
| 538 | method OnCharAdded( $event ) { | ||
| 539 | my $stc = $self->{t_editor}; | ||
| 540 | my $key = $event->GetKey; | ||
| 541 | return unless chr($key) =~ /[\]\n :\}]/; | ||
| 542 | |||
| 543 | #warn("KEY: ", sprintf("%d 0x%x (%c)\n", $key, $key, $key )); | ||
| 544 | my $ln = $stc->GetCurrentLine; | ||
| 545 | my $line = $stc->GetLine($ln); | ||
| 546 | #$stc->CallTipShow( $stc->GetCurrentPos, "LINE: »$line«"); | ||
| 547 | if ( $key eq ord("]") ) { | ||
| 548 | # Complete a chord. | ||
| 549 | my $pos = $stc->PositionBefore($stc->GetCurrentPos); | ||
| 550 | my $p0 = $stc->BraceMatch($pos); | ||
| 551 | return if $p0 < $stc->PositionFromLine($ln); | ||
| 552 | $p0 = $stc->PositionAfter($p0); | ||
| 553 | my $t = $stc->GetTextRange( $p0, $pos ); | ||
| 554 | return if $t =~ /\s/; | ||
| 555 | if ( $t =~ s/(^|\/)([a-hu])/sprintf("%s%s", $1, uc($2))/ge ) { | ||
| 556 | $stc->SetSelection( $p0, $pos ); | ||
| 557 | $stc->ReplaceSelection($t); | ||
| 558 | $stc->CharRight; | ||
| 559 | } | ||
| 560 | } | ||
| 561 | |||
| 562 | elsif ( $key eq ord("\n") ) { | ||
| 563 | # Move newline before trailing } to next line. | ||
| 564 | if ( $line =~ /^\}(\r\n|\r|\n)?\Z/ ) { | ||
| 565 | my $nl = $self->nl; | ||
| 566 | my $pos = $stc->GetCurrentPos; | ||
| 567 | $stc->SetSelection( $stc->PositionBefore($pos), | ||
| 568 | $stc->PositionAfter($pos) ); | ||
| 569 | $stc->CharRightExtend if length($nl) == 2; | ||
| 570 | $stc->ReplaceSelection("}" . $nl); | ||
| 571 | } | ||
| 572 | elsif ( $ln > 1 && $stc->GetLine($ln-1) =~ /^\{\s*start_of_(\w+).*\}$/ ) { | ||
| 573 | $stc->InsertText( -1, "\n{end_of_$1}" ); | ||
| 574 | } | ||
| 575 | } | ||
| 576 | |||
| 577 | elsif ( $key eq ord(" ") || $key eq ord(":") || $key eq ord("}") ) { | ||
| 578 | my $pos0 = $stc->PositionFromLine($ln); | ||
| 579 | my $pos = $stc->GetCurrentPos; | ||
| 580 | my $txt = $stc->GetTextRange( $pos0, $pos ); | ||
| 581 | if ( $txt =~ /^\s*(\{\s*)(\w+)(-\w+!?)?([ :\}])$/ | ||
| 582 | && | ||
| 583 | ( my $c = $state{rti}{directive_abbrevs}{$2} ) ) { | ||
| 584 | $stc->SetSelection( $pos0, $pos ); | ||
| 585 | $stc->ReplaceSelection( $1.$c.($3//""). | ||
| 586 | ($4 eq "}" ? "}" : ": " ) ); | ||
| 587 | } | ||
| 588 | } | ||
| 589 | } | ||
| 590 | |||
| 591 | method OnClearDiagnosticFlags($event) { | ||
| 592 | return unless $state{have_stc}; | ||
| 593 | $self->{t_editor}->AnnotationClearAll; | ||
| 594 | } | ||
| 595 | |||
| 596 | method OnClipBoardPaste($event) { | ||
| 597 | my $text = $event->GetString; | ||
| 598 | $text =~ s/^\n+//; | ||
| 599 | if ( $text =~ m/(.+\n\n)+/ ) { | ||
| 600 | $text =~ s/(.+\n)\n/$1/g; | ||
| 601 | } | ||
| 602 | $event->SetString($text); | ||
| 603 | } | ||
| 604 | |||
| 605 | method OnCloseSection($event) { | ||
| 606 | my $stc = $self->{t_editor}; | ||
| 607 | my $ln = $stc->GetCurrentLine; | ||
| 608 | my $closed = ""; | ||
| 609 | my $did; | ||
| 610 | while ( $ln > 0 ) { | ||
| 611 | $ln--; | ||
| 612 | my $line = $stc->GetLine($ln); | ||
| 613 | if ( $line =~ /^\{(\s*)start_of_(\w+(?:-\w*!?)?)/ ) { | ||
| 614 | if ( $2 eq $closed ) { | ||
| 615 | $closed = ""; | ||
| 616 | next; | ||
| 617 | } | ||
| 618 | $stc->AddText( $self->nl ) | ||
| 619 | if $stc->GetColumn($stc->GetCurrentPos); | ||
| 620 | $stc->AddText( "{$1end_of_$2}" . $self->nl ); | ||
| 621 | $did++; | ||
| 622 | last; | ||
| 623 | } | ||
| 624 | elsif ( $line =~ /^\{(\s*)so(\w(?:-\w*!?)?)/ ) { | ||
| 625 | if ( $2 eq $closed ) { | ||
| 626 | $closed = ""; | ||
| 627 | next; | ||
| 628 | } | ||
| 629 | $stc->AddText( $self->nl ) | ||
| 630 | if $stc->GetColumn($stc->GetCurrentPos); | ||
| 631 | $stc->AddText( "{$1eo$2}" . $self->nl ); | ||
| 632 | $did++; | ||
| 633 | last; | ||
| 634 | } | ||
| 635 | elsif ( $line =~ /^\{(\s*)end_of_(\w+(?:-\w*!?)?)/ ) { | ||
| 636 | $closed = $2; | ||
| 637 | } | ||
| 638 | elsif ( $line =~ /^\{(\s*)so(\w(?:-\w*!?)?)/ ) { | ||
| 639 | $closed = $2; | ||
| 640 | } | ||
| 641 | } | ||
| 642 | return if $did; | ||
| 643 | $stc->CallTipShow( $stc->GetCurrentPos, "No open section to close" ); | ||
| 644 | } | ||
| 645 | |||
| 646 | method OnCopy($event) { | ||
| 647 | $self->{t_editor}->Copy; | ||
| 648 | } | ||
| 649 | |||
| 650 | method OnCut($event) { | ||
| 651 | $self->{t_editor}->Cut; | ||
| 652 | } | ||
| 653 | |||
| 654 | method OnDelete($event) { | ||
| 655 | my ( $from, $to ) = $self->{t_editor}->GetSelection; | ||
| 656 | $self->{t_editor}->Remove( $from, $to ) if $from < $to; | ||
| 657 | } | ||
| 658 | |||
| 659 | method OnExternalEditor($event) { | ||
| 660 | my $editor = $ENV{VISUAL} // $ENV{EDITOR}; | ||
| 661 | $self->alert( 0, "No external editor specified" ), return unless $editor; | ||
| 662 | my $e = $self->{t_editor}; | ||
| 663 | my $pos = $e->GetCurrentPos; | ||
| 664 | my $mod = $e->IsModified; | ||
| 665 | |||
| 666 | # Save in temp file and call editor. | ||
| 667 | use File::Temp qw(tempfile); | ||
| 668 | ( undef, my $file ) = tempfile( SUFFIX => $preferences{chordproext}, | ||
| 669 | OPEN => 0 ); | ||
| 670 | $e->SaveFile($file); | ||
| 671 | my @st = stat($file); | ||
| 672 | $self->log( 'I', "Running $editor on $file (" . | ||
| 673 | plural( $e->GetLineCount, " line" ) . ", " . | ||
| 674 | plural( $st[7], " byte" ) . ")" ); | ||
| 675 | ::sys( $editor, $file ); | ||
| 676 | |||
| 677 | if ( (stat($file))[7] == $st[7] && (stat(_))[9] == $st[9] ) { | ||
| 678 | $self->log( 'I', "Running $editor did not make changes" ); | ||
| 679 | $self->alert( 0, "No changes from external editor" ); | ||
| 680 | } | ||
| 681 | else { | ||
| 682 | $e->LoadFile($file); | ||
| 683 | $self->log( 'I', "Updated editor from $file (" . | ||
| 684 | plural( $e->GetLineCount, " line" ) . ", " . | ||
| 685 | plural( (stat(_))[7], " byte" ) . ")" ); | ||
| 686 | $mod = 1; | ||
| 687 | # Clear selection and set insertion point. | ||
| 688 | $e->SetSelection( $pos, $pos ); | ||
| 689 | $e->EmptyUndoBuffer; | ||
| 690 | } | ||
| 691 | unlink($file); | ||
| 692 | |||
| 693 | $e->SetModified($mod); | ||
| 694 | $e->SetFocus; | ||
| 695 | } | ||
| 696 | |||
| 697 | # Experimental. | ||
| 698 | # This approach is a bit clumsy and prone to errors. | ||
| 699 | method OnExternalEditorSync($event) { | ||
| 700 | my $editor = $ENV{VISUAL} // $ENV{EDITOR}; | ||
| 701 | $self->alert( 0, "No external editor specified" ), return unless $editor; | ||
| 702 | my $e = $self->{t_editor}; | ||
| 703 | my $pos = $e->GetCurrentPos; | ||
| 704 | my $mod = $e->IsModified; | ||
| 705 | |||
| 706 | # Save in temp file and call editor. | ||
| 707 | use File::Temp qw(tempfile); | ||
| 708 | ( undef, my $file ) = tempfile( SUFFIX => $preferences{chordproext}, | ||
| 709 | OPEN => 0 ); | ||
| 710 | $e->SaveFile($file); | ||
| 711 | my @st = stat($file); | ||
| 712 | $self->log( 'I', "Running $editor on $file (" . | ||
| 713 | plural( $e->GetLineCount, " line" ) . ", " . | ||
| 714 | plural( $st[7], " byte" ) . ")" ); | ||
| 715 | |||
| 716 | my $cmd = sprintf("%s %s", $editor, qquote($file) ); | ||
| 717 | my $edit = Wx::Process::Open( $cmd ); | ||
| 718 | |||
| 719 | unless ( $edit ) { | ||
| 720 | $self->log( 'I', "Cannot start editor"); | ||
| 721 | return; | ||
| 722 | } | ||
| 723 | |||
| 724 | my $pid = $edit->GetPid; | ||
| 725 | $self->log( 'I', "Editor pid = $pid"); | ||
| 726 | |||
| 727 | my $did; | ||
| 728 | while ( Wx::Process::Exists($pid) ) { | ||
| 729 | next if (stat($file))[7] == $st[7] && (stat(_))[9] == $st[9]; | ||
| 730 | @st = stat(_); | ||
| 731 | $e->LoadFile($file); | ||
| 732 | $self->log( 'I', "Updated editor from $file (" . | ||
| 733 | plural( $e->GetLineCount, " line" ) . ", " . | ||
| 734 | plural( $st[7], " byte" ) . ")" ); | ||
| 735 | $mod = 1; | ||
| 736 | # Clear selection and set insertion point. | ||
| 737 | $e->SetSelection( $pos, $pos ); | ||
| 738 | $e->EmptyUndoBuffer; | ||
| 739 | $did++; | ||
| 740 | } | ||
| 741 | continue { | ||
| 742 | wxTheApp->Yield; | ||
| 743 | sleep(1); | ||
| 744 | } | ||
| 745 | |||
| 746 | if ( !$did && (stat($file))[7] == $st[7] && (stat(_))[9] == $st[9] ) { | ||
| 747 | $self->log( 'I', "Running $editor did not make changes" ); | ||
| 748 | $self->alert( 0, "No changes from external editor" ); | ||
| 749 | } | ||
| 750 | unlink($file); | ||
| 751 | |||
| 752 | $e->SetModified($mod); | ||
| 753 | $e->SetFocus; | ||
| 754 | } | ||
| 755 | |||
| 756 | method OnPaste($event) { | ||
| 757 | $self->{t_editor}->Paste; | ||
| 758 | } | ||
| 759 | |||
| 760 | method OnRedo($event) { | ||
| 761 | $self->{t_editor}->CanRedo && $self->{t_editor}->Redo; | ||
| 762 | } | ||
| 763 | |||
| 764 | method OnSave($event) { | ||
| 765 | return unless $self->{t_editor}->IsModified | ||
| 766 | || !defined $state{currentfile}; | ||
| 767 | $self->save_file( $state{currentfile} ) | ||
| 768 | } | ||
| 769 | |||
| 770 | method OnSaveAs { | ||
| 771 | $self->save_file; | ||
| 772 | } | ||
| 773 | |||
| 774 | method OnSongbook { | ||
| 775 | return unless $self->check_source_saved; | ||
| 776 | $self->GetParent->select_mode("sbexport"); | ||
| 777 | } | ||
| 778 | |||
| 779 | method OnText($event) { | ||
| 780 | $self->{t_editor}->SetModified(1); | ||
| 781 | } | ||
| 782 | |||
| 783 | method OnUndo($event) { | ||
| 784 | $self->{t_editor}->Undo; | ||
| 785 | } | ||
| 786 | |||
| 787 | #### Insertions | ||
| 788 | |||
| 789 | method OnInsertTitle($event) { | ||
| 790 | $self->embrace_directive("title"); | ||
| 791 | } | ||
| 792 | |||
| 793 | method OnInsertSubtitle($event) { | ||
| 794 | $self->embrace_directive("subtitle"); | ||
| 795 | } | ||
| 796 | |||
| 797 | method OnInsertKey($event) { | ||
| 798 | $self->embrace_directive("key"); | ||
| 799 | } | ||
| 800 | |||
| 801 | method OnInsertArtist($event) { | ||
| 802 | $self->embrace_directive("artist"); | ||
| 803 | } | ||
| 804 | |||
| 805 | method OnInsertChorus($event) { | ||
| 806 | $self->embrace_section("chorus"); | ||
| 807 | } | ||
| 808 | |||
| 809 | method OnInsertVerse($event) { | ||
| 810 | $self->embrace_section("verse"); | ||
| 811 | } | ||
| 812 | |||
| 813 | method OnInsertGrid($event) { | ||
| 814 | $self->embrace_section("grid"); | ||
| 815 | } | ||
| 816 | |||
| 817 | method OnInsertTab($event) { | ||
| 818 | $self->embrace_section("tab"); | ||
| 819 | } | ||
| 820 | |||
| 821 | method OnInsertSection($event) { | ||
| 822 | $self->embrace_section(undef); | ||
| 823 | } | ||
| 824 | |||
| 825 | 1; | ||
| 826 |