File Coverage

lib/Kephra/Document/Property.pm
Criterion Covered Total %
statement 6 146 4.1
branch 0 90 0.0
condition 0 42 0.0
subroutine 2 34 5.8
pod 0 26 0.0
total 8 338 2.3


line stmt bran cond sub pod time code
1             package Kephra::Document::Property;
2             our $VERSION = '0.04';
3            
4 1     1   4372 use strict;
  1         2  
  1         46  
5 1     1   6 use warnings;
  1         3  
  1         2181  
6             # some internal shortcut helper
7 0     0     sub _ep_ref { Kephra::Document::Data::_ep($_[0]) }
8 0     0     sub _doc_nr { Kephra::Document::Data::valid_or_current_doc_nr($_[0]) }
9 0     0     sub _is_current { $_[0] == Kephra::Document::Data::current_nr() }
10 0     0     sub _get_attr { Kephra::Document::Data::get_attribute(@_) }
11 0     0     sub _set_attr { Kephra::Document::Data::set_attribute(@_) }
12             #
13             # general API for single and multiple values (getter/setter)
14             #
15             sub get {
16 0     0 0   my $property = shift;
17 0           my $doc_nr = _doc_nr(shift);
18 0 0         return if $doc_nr < 0;
19 0 0         if (not ref $property) { _get_attr($property, $doc_nr) }
  0 0          
20             elsif (ref $property eq 'ARRAY') {
21 0           my @result;
22 0           push @result, _get_attr($_, $doc_nr) for @$property;
23 0           \@result;
24             }
25             }
26             sub _set{
27 0     0     my ($key, $v) = @_;
28 0 0         return unless defined $v;
29 0 0         return set_codepage($v) if $key eq 'codepage';
30 0 0         return set_EOL_mode($v) if $key eq 'EOL';
31 0 0         return set_readonly($v) if $key eq 'readonly';
32 0 0         return set_syntaxmode($v)if $key eq 'syntaxmode';
33 0 0         return set_tab_size($v) if $key eq 'tab_size';
34 0 0         return set_tab_mode($v) if $key eq 'tab_use';
35             }
36             sub set {
37 0 0 0 0 0   if (not ref $_[0] and defined $_[1]){_set(@_)}
  0 0          
  0            
38 0           elsif ( ref $_[0] eq 'HASH') {_set($_, $_[0]->{$_}) for keys %{$_[0]}}
39             }
40 0     0 0   sub get_file { _get_attr('file_path') }
41             sub set_file {
42 0     0 0   my ($file, $nr) = @_;
43 0           $nr = _doc_nr($nr);
44 0 0         return if $nr < 0;
45 0           Kephra::Document::Data::set_file_path($file, $nr);
46 0           Kephra::App::TabBar::refresh_label($nr);
47 0 0         Kephra::App::Window::refresh_title() if _is_current($nr);
48             }
49             #
50             # property specific API
51             #
52             #
53             # syntaxmode
54 0     0 0   sub get_syntaxmode { _get_attr('syntaxmode') }
55 0     0 0   sub set_syntaxmode { Kephra::Document::SyntaxMode::set(@_) }
56             #
57             #
58 0     0 0   sub get_codepage { _get_attr('codepage', $_[0]) }
59             sub set_codepage {
60 0     0 0   my ($new_value, $doc_nr) = @_;
61 0           $doc_nr = _doc_nr($doc_nr);
62 0 0 0       return if $doc_nr < 0 or not defined $new_value;
63 0           my $old_value = get_codepage($doc_nr);
64 0           my $ep = _ep_ref($doc_nr);
65 0 0 0       if ($old_value eq 'ascii' and $new_value eq 'utf8'){
    0 0        
66             #unless (Encode::is_utf8($ep->GetText())) {
67 0           Kephra::Document::Data::update_attributes($doc_nr);
68 0           eval {
69             #Encode::encode('ascii');
70 0           $ep->SetText( Encode::decode('utf8', $ep->GetText()) );
71             };
72             #print "$@\n";
73 0           Kephra::Document::Data::evaluate_attributes($doc_nr);
74             #}
75             #print Encode::is_utf8($ep->GetText())."\n";
76             }
77             elsif ($old_value eq 'utf8' and $new_value eq 'ascii') {
78 0           Kephra::Document::Data::update_attributes($doc_nr);
79 0           $ep->SetText( Encode::encode('utf8', $ep->GetText()) );
80 0           Kephra::Document::Data::evaluate_attributes($doc_nr);
81             }
82             #print "ask auto $mode\n";
83             #$ep->SetCodePage( &Wx::wxSTC_CP_UTF8 );
84 0           _set_attr('codepage', $new_value, $doc_nr);
85 0           Kephra::App::StatusBar::codepage_info($new_value);
86             }
87             sub switch_codepage {
88 0 0   0 0   set_codepage( get_codepage( _doc_nr() ) eq 'utf8' ? 'ascii' : 'utf8' );
89             }
90             #
91             # tab size
92 0     0 0   sub get_tab_size { _get_attr('tab_size', $_[0]) }
93             sub set_tab_size {
94 0     0 0   my ($size, $nr) = @_;
95 0           $nr = _doc_nr($nr);
96 0 0 0       return if not $size or $nr < 0;
97 0           my $ep = _ep_ref();
98 0           $ep->SetTabWidth($size);
99 0           $ep->SetIndent($size);
100 0           $ep->SetHighlightGuide($size);
101 0           _set_attr('tab_size', $size, $nr);
102             }
103             #
104             # tab use
105 0     0 0   sub get_tab_mode { _get_attr('tab_use', $_[0]) }
106             sub set_tab_mode {
107 0     0 0   my $mode = shift;
108 0           my $nr = _doc_nr(shift);
109 0 0         return if $nr < 0;
110 0           my $ep = _ep_ref();
111 0 0         if ($mode eq 'auto') {
112 0 0         if ($ep->GetTextLength){
113 0           my $line;
114 0           for my $lnr (0 .. $ep->GetLineCount()-1){
115 0           $line = $ep->GetLine($lnr);
116 0 0         if ($line =~ /^( |\t)/) {
117 0 0         $mode = $1 eq ' ' ? 0 : 1;
118 0           last;
119             }
120             }
121             }
122             else {
123 0           $mode = Kephra::File::_config()->{defaultsettings}{new}{tab_use};
124             }
125             }
126 0           $ep->SetUseTabs($mode);
127 0           _set_attr('tab_use', $mode, $nr);
128 0 0         Kephra::App::StatusBar::tab_info() if _is_current($nr);
129             }
130 0     0 0   sub set_tabs_hard { set_tab_mode(1) }
131 0     0 0   sub set_tabs_soft { set_tab_mode(0) }
132 0 0   0 0   sub switch_tab_mode{ get_tab_mode() ? set_tab_mode(0) : set_tab_mode(1) }
133             #
134             # EOL
135 0     0 0   sub EOL_length { _get_attr('EOL_length') }
136 0     0 0   sub get_EOL_mode { _get_attr('EOL') }
137             sub set_EOL_mode {
138 0     0 0   my $mode = shift;
139 0 0         return unless defined $mode;
140 0 0         if ($mode eq 'OS') {
141 0 0         if (&Wx::wxMSW) {$mode = 'cr+lf'}
  0 0          
  0            
142 0           elsif (&Wx::wxMAC) {$mode = 'cr' }
143             else {$mode = 'lf' }
144             }
145 0 0         $mode = detect_EOL_mode() if $mode eq 'auto';
146 0           my $ep = _ep_ref();
147 0           my $eoll = 1;
148 0 0 0       if ( $mode eq 'cr+lf'or $mode eq 'win') {$ep->SetEOLMode(&Wx::wxSTC_EOL_CRLF);
  0 0 0        
  0            
149 0           $eoll = 2;
150             }
151 0           elsif ( $mode eq 'cr'or $mode eq 'mac') {$ep->SetEOLMode(&Wx::wxSTC_EOL_CR) }
152             else {$ep->SetEOLMode(&Wx::wxSTC_EOL_LF) }
153 0           _set_attr('EOL', $mode);
154 0           _set_attr('EOL_length', $eoll);
155 0           Kephra::App::StatusBar::EOL_info($mode);
156             }
157            
158             sub convert_EOL {
159 0   0 0 0   my $mode = shift || get_EOL_mode();
160 0           my $doc_nr = _doc_nr(shift);
161 0           my $ep = _ep_ref($doc_nr);
162 0 0         $mode = Kephra::File::_config()->{defaultsettings}{EOL_open} unless $mode;
163 0 0         $mode = detect_EOL_mode() if $mode eq 'auto';
164 0           Kephra::EventTable::freeze_group('edit');
165 0 0 0       if ($mode eq 'cr+lf' or $mode eq 'win') {$ep->ConvertEOLs(&Wx::wxSTC_EOL_CRLF)}
  0 0 0        
  0            
166 0           elsif ($mode eq 'cr' or $mode eq 'mac' ) {$ep->ConvertEOLs(&Wx::wxSTC_EOL_CR)}
167             else {$ep->ConvertEOLs(&Wx::wxSTC_EOL_LF)}
168 0           Kephra::EventTable::thaw_group('edit');
169 0           set_EOL_mode($mode);
170             }
171            
172             sub detect_EOL_mode {
173 0     0 0   my $ep = _ep_ref();
174 0           my $end_pos = $ep->PositionFromLine(1);
175 0           my $begin_pos = $end_pos - 3;
176 0 0         $begin_pos = 0 if $begin_pos < 0;
177 0           my $text = $ep->GetTextRange( $begin_pos, $end_pos );
178            
179 0 0         if ( length($text) < 1 ) { return 'auto' }
  0            
180             else {
181             #print "win \n" if $text =~ /\r\n/;
182             #return 'cr+lf' if $text =~ /\r\n/;
183             #return 'cr' if $text =~ /\r/;
184             #return 'lf' if $text =~ /\n/;
185 0           return 'auto';
186             }
187             }
188             #
189             # write protection
190 0     0 0   sub get_readonly { _get_attr('readonly') }
191             sub set_readonly {
192 0     0 0   my $status = shift;
193 0           my $ep = _ep_ref();
194 0 0 0       if (not $status or $status eq 'off' ) {
    0 0        
    0 0        
195 0           $ep->SetReadOnly(0);
196 0           $status = 'off';
197             } elsif ( $status eq 'on' or $status eq '1' ) {
198 0           $ep->SetReadOnly(1);
199 0           $status = 'on';
200             } elsif ( $status eq 'protect' or $status eq '2' ) {
201 0           my $file = Kephra::Document::Data::get_file_path();
202 0 0 0       if ( $file and not -w $file ) {$ep->SetReadOnly(1)}
  0            
  0            
203             else {$ep->SetReadOnly(0)}
204 0           $status = 'protect';
205             }
206 0           _set_attr('readonly', $status);
207 0 0         $status = $ep->GetReadOnly ? 1 : 0;
208 0           _set_attr('editable', $status);
209 0 0         Kephra::App::TabBar::refresh_current_label()
210             if Kephra::App::TabBar::_config()->{info_symbol};
211             }
212 0     0 0   sub set_readonly_on { set_readonly('on') }
213 0     0 0   sub set_readonly_off { set_readonly('off') }
214 0     0 0   sub set_readonly_protect { set_readonly('protect') }
215            
216             1;
217             __END__