| 1 | 1 | ▶ | use v5.12; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | package Kephra::App::Editor; | ||
| 5 | our @ISA = 'Wx::StyledTextCtrl'; | ||
| 6 | 0 | ▶ | use Wx qw/ :everything /; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Wx::STC; | ||
| 8 | use Wx::DND; | ||
| 9 | #use Wx::Scintilla; | ||
| 10 | use Kephra::App::Editor::Edit; | ||
| 11 | use Kephra::App::Editor::Events; | ||
| 12 | use Kephra::App::Editor::Goto; | ||
| 13 | use Kephra::App::Editor::Move; | ||
| 14 | use Kephra::App::Editor::Position; | ||
| 15 | use Kephra::App::Editor::Property; | ||
| 16 | use Kephra::App::Editor::Select; | ||
| 17 | use Kephra::App::Editor::SyntaxMode; | ||
| 18 | use Kephra::App::Editor::Tool; | ||
| 19 | use Kephra::App::Editor::View; | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my( $class, $parent, $style) = @_; | ||
| 23 | my $self = $class->SUPER::new( $parent, -1,[-1,-1],[-1,-1] ); | ||
| 24 | Kephra::App::Editor::Events::mount( $self ); | ||
| 25 | $self->SetWordChars('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._$@%&*\\'); | ||
| 26 | $self->SetAdditionalCaretsBlink( 1 ); | ||
| 27 | $self->SetAdditionalCaretsVisible( 1 ); | ||
| 28 | $self->SetAdditionalSelectionTyping( 1 ); | ||
| 29 | $self->SetIndentationGuides( &Wx::wxSTC_WS_VISIBLEAFTERINDENT ); # wxSTC_WS_VISIBLEAFTERINDENT 2 wxSTC_WS_VISIBLEALWAYS 1 | ||
| 30 | # $self->SetAdditionalSelAlpha( 1 ); | ||
| 31 | $self->SetScrollWidth(300); | ||
| 32 | $self->SetYCaretPolicy( &Wx::wxSTC_CARET_SLOP, 5); | ||
| 33 | $self->SetVisiblePolicy( &Wx::wxSTC_VISIBLE_SLOP, 5); | ||
| 34 | $self->set_margin; | ||
| 35 | $self->load_font; # before setting highlighting | ||
| 36 | Kephra::App::Editor::SyntaxMode::apply( $self, 'perl' ); | ||
| 37 | $self->{'change_pos'} = $self->{'change_prev'} = -1; | ||
| 38 | $self->del_caret_pos_cache; | ||
| 39 | return $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub window { $_[0]->GetParent } | ||
| 43 | |||
| 44 | sub apply_config { | ||
| 45 | my ($self, $config) = @_; | ||
| 46 | my $config_value = $config->get_value('document'); | ||
| 47 | $self->set_tab_size( $config_value->{'tab_size'} ); | ||
| 48 | $self->set_tab_usage( !$config_value->{'soft_tabs'} ); | ||
| 49 | $self->set_EOL( $config_value->{'line_ending'} ); | ||
| 50 | |||
| 51 | $config_value = $config->get_value('view'); | ||
| 52 | $self->set_zoom_level( $config_value->{'zoom_level'} ); | ||
| 53 | $self->toggle_view_whitespace unless $config_value->{'whitespace'} == $self->view_whitespace_mode; | ||
| 54 | $self->toggle_view_eol unless $config_value->{'line_ending'} == $self->view_eol_mode; | ||
| 55 | $self->toggle_view_indent_guide unless $config_value->{'indent_guide'} == $self->indent_guide_mode; | ||
| 56 | $self->toggle_view_right_margin unless $config_value->{'right_margin'} == $self->right_margin_mode; | ||
| 57 | $self->toggle_view_line_nr_margin unless $config_value->{'line_nr_margin'}== $self->line_nr_margin_mode; | ||
| 58 | $self->toggle_view_marker_margin unless $config_value->{'marker_margin'} == $self->marker_margin_mode; | ||
| 59 | $self->toggle_view_caret_line unless $config_value->{'caret_line'} == $self->view_caret_line_mode; | ||
| 60 | $self->toggle_view_line_wrap unless $config_value->{'line_wrap'} == $self->line_wrap_mode; | ||
| 61 | $self->window->toggle_full_screen unless $config_value->{'full_screen'} == $self->window->IsFullScreen; | ||
| 62 | |||
| 63 | $config_value = $config->get_value('editor'); | ||
| 64 | $self->{ $_ } = $config_value->{ $_ } for qw/change_pos change_prev/; | ||
| 65 | $self->init_marker( @{$config_value->{'marker'}} ); | ||
| 66 | $self->SetSelection( $config_value->{'caret_pos'}, $config_value->{'caret_pos'}); | ||
| 67 | $self->EnsureCaretVisible; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub save_config { | ||
| 71 | my ($self, $config) = @_; | ||
| 72 | $config->set_value( { soft_tabs => !$self->{'tab_usage'}, | ||
| 73 | indention_size => $self->{'tab_size'}, | ||
| 74 | line_ending => $self->get_EOL, | ||
| 75 | # encoding => 'utf-8', | ||
| 76 | } , 'document'); | ||
| 77 | $config->set_value( { change_pos => $self->{'change_pos'}, | ||
| 78 | change_prev => $self->{'change_prev'}, | ||
| 79 | caret_pos => $self->GetCurrentPos, | ||
| 80 | marker => [ $self->marker_lines ], | ||
| 81 | } , 'editor'); | ||
| 82 | $config->set_value( { whitespace => $self->view_whitespace_mode, | ||
| 83 | line_ending => $self->view_eol_mode, | ||
| 84 | indent_guide => $self->indent_guide_mode, | ||
| 85 | right_margin => $self->right_margin_mode, | ||
| 86 | line_nr_margin => $self->line_nr_margin_mode, | ||
| 87 | marker_margin => $self->marker_margin_mode, | ||
| 88 | caret_line => $self->view_caret_line_mode, | ||
| 89 | line_wrap => $self->line_wrap_mode, | ||
| 90 | zoom_level => $self->get_zoom_level, | ||
| 91 | full_screen => int $self->window->IsFullScreen, | ||
| 92 | } , 'view'); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub load_font { | ||
| 96 | my ($self, $font) = @_; | ||
| 97 | my ( $fontweight, $fontstyle ) = ( &Wx::wxNORMAL, &Wx::wxNORMAL ); | ||
| 98 | $font = { | ||
| 99 | family => $^O eq 'darwin' ? 'Andale Mono' : 'Courier New', # old default | ||
| 100 | # family => 'DejaVu Sans Mono', # new # Courier New | ||
| 101 | size => $^O eq 'darwin' ? 13 : 11, | ||
| 102 | style => 'normal', | ||
| 103 | weight => 'normal', | ||
| 104 | } unless defined $font; | ||
| 105 | #my $font = _config()->{font}; | ||
| 106 | $fontweight = &Wx::wxLIGHT if $font->{weight} eq 'light'; | ||
| 107 | $fontweight = &Wx::wxBOLD if $font->{weight} eq 'bold'; | ||
| 108 | $fontstyle = &Wx::wxSLANT if $font->{style} eq 'slant'; | ||
| 109 | $fontstyle = &Wx::wxITALIC if $font->{style} eq 'italic'; | ||
| 110 | my $wx_font = Wx::Font->new( | ||
| 111 | $font->{size}, &Wx::wxDEFAULT, $fontstyle, $fontweight, 0, $font->{family}, &Wx::wxFONTENCODING_DEFAULT | ||
| 112 | ); | ||
| 113 | $self->StyleSetFont( &Wx::wxSTC_STYLE_DEFAULT, $wx_font ) if $wx_font->Ok > 0; | ||
| 114 | # wxFONTENCODING_ISO8859_1 | ||
| 115 | # wxFONTENCODING_UTF8 | ||
| 116 | # wxFONTENCODING_UTF16(L|BE) | ||
| 117 | } | ||
| 118 | |||
| 119 | sub is_empty { not $_[0]->GetTextLength } | ||
| 120 | |||
| 121 | sub new_text { | ||
| 122 | my ($self, $content, $soft) = @_; | ||
| 123 | return unless defined $content; | ||
| 124 | $self->SetText( $content ); | ||
| 125 | $self->EmptyUndoBuffer unless defined $soft; | ||
| 126 | $self->SetSavePoint; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub bracelight{ | ||
| 130 | my ($self, $pos) = @_; | ||
| 131 | my $char_before = $self->GetTextRange( $pos-1, $pos ); | ||
| 132 | my $char_after = $self->GetTextRange( $pos, $pos + 1); | ||
| 133 | if ( $char_before eq '(' or $char_before eq ')' | ||
| 134 | or $char_before eq '{' or $char_before eq '}' | ||
| 135 | or $char_before eq '[' or $char_before eq ']' ) { | ||
| 136 | my $mpos = $self->BraceMatch( $pos - 1 ); | ||
| 137 | $mpos != &Wx::wxSTC_INVALID_POSITION | ||
| 138 | ? $self->BraceHighlight($pos - 1, $mpos) | ||
| 139 | : $self->BraceBadLight($pos - 1); | ||
| 140 | } elsif ($char_after eq '(' or $char_after eq ')' | ||
| 141 | or $char_after eq '{' or $char_after eq '}' | ||
| 142 | or $char_after eq '[' or $char_after eq ']'){ | ||
| 143 | my $mpos = $self->BraceMatch( $pos ); | ||
| 144 | $mpos != &Wx::wxSTC_INVALID_POSITION | ||
| 145 | ? $self->BraceHighlight($pos, $mpos) | ||
| 146 | : $self->BraceBadLight($pos); | ||
| 147 | } else { $self->BraceHighlight(-1, -1); $self->BraceBadLight( -1 ) } | ||
| 148 | } | ||
| 149 | |||
| 150 | sub new_line { | ||
| 151 | my ($self) = @_; | ||
| 152 | my $pos = $self->GetCurrentPos; | ||
| 153 | my $char_before = $self->GetTextRange( $pos-1, $pos ); | ||
| 154 | my $char_after = $self->GetTextRange( $pos, $pos + 1); | ||
| 155 | $self->NewLine; | ||
| 156 | my $l = $self->GetCurrentLine; | ||
| 157 | my $i = $self->GetLineIndentation( $l - 1 ); | ||
| 158 | |||
| 159 | if ($char_before eq '{' and $char_after eq '}'){ # postion braces when around caret | ||
| 160 | $self->NewLine; | ||
| 161 | $self->SetLineIndentation( $self->GetCurrentLine, $i ); | ||
| 162 | $i+= $self->{'tab_size'} | ||
| 163 | } else { | ||
| 164 | # ignore white space left of caret | ||
| 165 | while ($char_before eq ' ' and $self->LineFromPosition( $pos ) == $l-1){ | ||
| 166 | $pos--; | ||
| 167 | $char_before = $self->GetTextRange( $pos-1, $pos ); | ||
| 168 | } | ||
| 169 | if ($char_before eq '{') { $i+= $self->{'tab_size'} } | ||
| 170 | elsif ($char_before eq '}') { | ||
| 171 | my $mpos = $self->BraceMatch( $pos - 1 ); | ||
| 172 | if ($mpos != &Wx::wxSTC_INVALID_POSITION){ | ||
| 173 | $i = $self->GetLineIndentation( $self->LineFromPosition( $mpos ) ); | ||
| 174 | } else { | ||
| 175 | $i-= $self->{'tab_size'}; | ||
| 176 | $i = 0 if $i < 0; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | } | ||
| 180 | $self->SetLineIndentation( $l, $i ); | ||
| 181 | $self->GotoPos( $self->GetLineIndentPosition( $l ) ); | ||
| 182 | } | ||
| 183 | |||
| 184 | sub escape { | ||
| 185 | my ($self) = @_; | ||
| 186 | #my ($start_pos, $end_pos) = $self->GetSelection; | ||
| 187 | my $win = $self->GetParent; | ||
| 188 | #if ($start_pos != $end_pos) { return $self->GotoPos ( $self->GetCurrentPos ) } | ||
| 189 | if ($win->IsFullScreen) { return $win->toggle_full_screen}; | ||
| 190 | # 3. focus to edit field | ||
| 191 | } | ||
| 192 | |||
| 193 | sub GetLastPosition { $_[0]->GetTextLength } | ||
| 194 | |||
| 195 | sub set_caret_pos_cache { | ||
| 196 | my ($self, $name, $pos) = @_; | ||
| 197 | return unless defined $name; | ||
| 198 | $self->{'caret_cache'} = {name => $name, pos => [$self->GetSelection]}; | ||
| 199 | $self->{'caret_cache'}{'pos'} = [$pos] if defined $pos; | ||
| 200 | } | ||
| 201 | sub get_caret_pos_cache { | ||
| 202 | my ($self, $name) = @_; | ||
| 203 | return unless defined $name and $self->{'caret_cache'}{'name'} eq $name; | ||
| 204 | return @{$self->{'caret_cache'}{'pos'}} if int @{$self->{'caret_cache'}{'pos'}}; | ||
| 205 | } | ||
| 206 | sub del_caret_pos_cache { $_[0]->{'caret_cache'} = {name => '', pos => []} } | ||
| 207 | |||
| 208 | #~ sub sel { | ||
| 209 | #~ my ($self) = @_; | ||
| 210 | #~ my $pos = $self->GetCurrentPos; | ||
| 211 | #~ say $self->GetStyleAt( $pos); | ||
| 212 | #~ } | ||
| 213 | |||
| 214 | 1; | ||