File Coverage

lib/Kephra/App/Panel/Notepad.pm
Criterion Covered Total %
statement 6 111 5.4
branch 0 58 0.0
condition 0 18 0.0
subroutine 2 19 10.5
pod 0 9 0.0
total 8 215 3.7


line stmt bran cond sub pod time code
1             package Kephra::App::Panel::Notepad;
2             our $VERSION = '0.16';
3            
4 1     1   1086 use strict;
  1         2  
  1         35  
5 1     1   6 use warnings;
  1         2  
  1         1798  
6            
7             my $global_notepad;
8             my $local_notepad;
9             sub _global_ref {
10 0 0   0     $global_notepad = ref $_[0] eq 'Wx::StyledTextCtrl' ? $_[0] : $global_notepad
11             }
12             sub _local_ref {
13 0 0   0     $local_notepad = ref $_[0] eq 'Wx::StyledTextCtrl' ? $_[0] : $local_notepad
14             } #document.current.number.changed
15             sub _ref {
16 0     0     _global_ref()
17             }
18 0     0     sub _local_splitter {}
19 0     0     sub _config { Kephra::API::settings()->{app}{panel}{notepad} }
20 0     0     sub _splitter{ $Kephra::app{splitter}{right} }
21            
22             sub create {
23 0     0 0   my $win = Kephra::App::Window::_ref();
24 0           my $notepad;
25 0 0         if ( _global_ref()) {$notepad = _global_ref()}
  0            
26             else {
27 0           my $color = \&Kephra::Config::color;
28 0           my $indicator = Kephra::App::EditPanel::_config()->{indicator};
29 0           my $config = _config();
30 0           $notepad = Wx::StyledTextCtrl->new( $win, -1, [-1,-1], [-1,-1] );
31 0           $notepad->SetWrapMode(&Wx::wxSTC_WRAP_WORD);
32 0           $notepad->SetScrollWidth(210);
33 0           $notepad->SetMarginWidth(0,0);
34 0           $notepad->SetMarginWidth(1,0);
35 0           $notepad->SetMarginWidth(2,0);
36 0           $notepad->SetMargins(3,3);
37 0           $notepad->SetCaretPeriod( $indicator->{caret}{period} );
38 0           $notepad->SetCaretWidth( $indicator->{caret}{width} );
39 0           $notepad->SetCaretForeground( &$color( $indicator->{caret}{color} ) );
40 0 0         if ( $indicator->{selection}{fore_color} ne '-1' ) {
41 0           $notepad->SetSelForeground
42             ( 1, &$color( $indicator->{selection}{fore_color} ) );
43             }
44 0           $notepad->SetSelBackground( 1, &$color( $indicator->{selection}{back_color}));
45 0           $notepad->StyleSetFont( &Wx::wxSTC_STYLE_DEFAULT, Wx::Font->new
46             (_config()->{font_size}, &Wx::wxDEFAULT, &Wx::wxNORMAL, &Wx::wxNORMAL, 0,
47             _config()->{font_family})
48             );
49 0           $notepad->SetTabWidth(4);
50 0           $notepad->SetIndent(4);
51 0           $notepad->SetHighlightGuide(4);
52            
53             # load content
54 0           my $file_name = $config->{content_file};
55 0 0         if ($file_name) {
56 0           $file_name = Kephra::Config::filepath($file_name);
57 0 0         if (-e $file_name){
58 0           open my $FILE, '<', $file_name;
59 0           $notepad->AppendText( $_ ) while <$FILE>;
60             }
61             }
62             }
63 0           $notepad->Show( get_visibility() );
64            
65             Wx::Event::EVT_KEY_DOWN( $notepad, sub {
66 0     0     my ( $fi, $event ) = @_;
67 0           my $key = $event->GetKeyCode;
68 0           my $ep = Kephra::App::EditPanel::_ref();
69 0 0 0       if ($key == &Wx::WXK_ESCAPE ) {
    0          
    0          
    0          
    0          
    0          
    0          
70 0           Wx::Window::SetFocus( $ep );
71             } elsif ($key == &Wx::WXK_UP ) {
72 0 0 0       Kephra::Edit::selection_move_up($notepad)
73             if $event->ControlDown and $event->AltDown;
74             } elsif ($key == &Wx::WXK_DOWN){
75 0 0 0       Kephra::Edit::selection_move_down($notepad)
76             if $event->ControlDown and $event->AltDown;
77             } elsif ($key == &Wx::WXK_F3 ) {
78 0 0         if ($event->ControlDown) {
79 0           my $sel = $notepad->GetSelectedText;
80 0 0         $event->ShiftDown
81             ? Kephra::Edit::Search::set_replace_item($sel)
82             : Kephra::Edit::Search::set_find_item($sel);
83             }
84             } elsif ($key == &Wx::WXK_F5) {
85 0           my ( $sel_beg, $sel_end ) = $notepad->GetSelection;
86 0           Kephra::App::Panel::Output::ensure_visibility();
87 0 0         my $code = $sel_beg == $sel_end
88             ? $notepad->GetText
89             : $notepad->GetSelectedText;
90 0           my $result;
91 0           my $interpreter = _config->{eval_with};
92 0 0 0       if (not defined $interpreter or $interpreter eq 'eval') {
93 0           $result = eval $code;
94 0 0         $result = $@ if $@;
95             } else {
96 0           $result = `$interpreter $code`;
97             }
98 0           Kephra::App::Panel::Output::say($result);
99             } elsif ($key == &Wx::WXK_F12) {
100 0           Wx::Window::SetFocus( $ep );
101 0 0         switch_visibility() if $event->ControlDown;
102             } elsif ($key == 70 and $event->ControlDown) {# F
103 0           Kephra::CommandList::run_cmd_by_id('view-searchbar-goto');
104             }
105 0           $event->Skip;
106 0           });
107            
108             Kephra::EventTable::add_call( 'app.splitter.right.changed',__PACKAGE__,sub {
109 0 0 0 0     if ( get_visibility() and not _splitter()->IsSplit() ) {
110 0           show( 0 );
111 0           return;
112             }
113 0           save_size();
114 0           });
115            
116 0           _global_ref($notepad);
117 0           $notepad;
118             }
119            
120 0     0 0   sub get_visibility { _config()->{visible} }
121 0     0 0   sub switch_visibility { show( get_visibility() ^ 1 ) }
122             sub show {
123 0     0 0   my $visibile = shift;
124 0           my $config = _config();
125 0 0         $visibile = $config->{visible} unless defined $visibile;
126 0           my $win = Kephra::App::Window::_ref();
127 0           my $main_panel = $Kephra::app{panel}{main};
128 0           my $notepad = _ref();
129 0           my $splitter = _splitter();
130 0 0         if ($visibile) {
131 0           $splitter->SplitVertically( $main_panel, $notepad );
132 0           $splitter->SetSashPosition( -1*$config->{size}, 1);
133             } else {
134 0           $splitter->Unsplit();
135 0           $splitter->Initialize( $main_panel );
136             }
137 0           $notepad->Show( $visibile );
138 0           $win->Layout;
139 0           $config->{visible} = $visibile;
140 0           Kephra::EventTable::trigger('panel.notepad.visible');
141             }
142            
143             sub note {
144 0     0 0   show( 1 );
145 0           Wx::Window::SetFocus( _ref() );
146             }
147            
148             sub append {
149 0     0 0   my $text = shift;
150 0 0 0       return unless defined $text and $text;
151 0           my $np = _ref();
152 0 0         $text = "\n" . $text if $np->GetLength();
153 0           $np->AppendText( $text );
154             }
155            
156 0     0 0   sub append_selection { append( Kephra::Edit::get_selection() ) }
157            
158             sub save {
159 0     0 0   my $file_name = _config()->{content_file};
160 0 0         if ($file_name) {
161 0           $file_name = Kephra::Config::filepath($file_name);
162 0 0         if (open my $FH, '>', $file_name) {print $FH _ref()->GetText;}
  0            
163             }
164 0           save_size();
165             }
166            
167             sub save_size {
168 0     0 0   my $splitter = _splitter();
169 0 0         return unless $splitter->IsSplit();
170 0           my $ww=Kephra::App::Window::_ref()->GetSize->GetWidth;
171 0           _config()->{size} = -1*($ww-($ww-$splitter->GetSashPosition));
172             }
173              
174            
175             1;