File Coverage

lib/Kephra/Document.pm
Criterion Covered Total %
statement 6 120 5.0
branch 0 32 0.0
condition 0 38 0.0
subroutine 2 18 11.1
pod 0 12 0.0
total 8 220 3.6


line stmt bran cond sub pod time code
1             package Kephra::Document;
2             our $VERSION = '0.53';
3            
4 1     1   1270 use strict;
  1         2  
  1         37  
5 1     1   5 use warnings;
  1         2  
  1         2157  
6            
7 0     0     sub _file_config { Kephra::File::_config() }
8             sub _new_if_allowed {
9             # new(empty), add(open) restore(open session)
10 0     0     my $mode = shift;
11 0           my $ep = Kephra::App::EditPanel::_ref();
12 0           my $file = Kephra::Document::Data::get_file_path();
13 0           my $old_doc_nr= Kephra::Document::Data::current_nr();
14 0           my $new_doc_nr= Kephra::Document::Data::get_value('buffer');
15 0           my $config = _file_config()->{open};
16            
17             # check settings
18             # in single doc mode close previous doc first
19 0 0         if ( $config->{single_doc} == 1 ) {
20 0           Kephra::File::close_current();
21 0           return 0;
22             }
23 0 0         unless ( $mode eq 'new' ) {
24 0 0 0       if ($ep->GetText eq '' and $ep->GetModify == 0 and (not $file or not -e $file)){
      0        
      0        
25 0 0 0       return $old_doc_nr
      0        
26             if ($config->{into_empty_doc} == 1)
27             or ($config->{into_only_empty_doc} == 1 and $new_doc_nr == 1 );
28             }
29             }
30             # still there? good, now we make a new document
31 0           Kephra::Document::Data::create_slot($new_doc_nr);
32 0           Kephra::App::TabBar::add_edit_tab($new_doc_nr);
33 0           Kephra::App::EditPanel::apply_settings_here
34             ( Kephra::Document::Data::_ep($new_doc_nr) );
35 0           Kephra::Document::Data::inc_value('buffer');
36 0           return $new_doc_nr;
37             }
38            
39             sub _load_file_in_buffer {
40 0     0     my $file = shift;
41 0   0       my $doc_nr = shift || Kephra::Document::Data::current_nr();
42 0           my $ep = Kephra::Document::Data::_ep($doc_nr);
43 0 0 0       return unless -r $file and Kephra::App::EditPanel::is( $ep );
44 0           $ep->ClearAll();
45             # retrieve if utf is set
46 0           Kephra::Document::Data::set_file_path($file, $doc_nr);
47 0 0         if (Kephra::File::IO::open_buffer($doc_nr) ){
48 0           Kephra::File::_remember_save_moment($doc_nr);
49 0           $ep->EmptyUndoBuffer;
50 0           $ep->SetSavePoint;
51 0           Kephra::Document::Data::inc_value('loaded');
52             }
53             }
54             #
55             sub new { # make document empty and reset all document properties to default
56 0     0 0   my $old_nr = Kephra::Document::Data::current_nr();
57 0           my $doc_nr = _new_if_allowed('new');
58 0           Kephra::Document::Data::set_previous_nr( $old_nr );
59 0           Kephra::Document::Data::set_current_nr( $doc_nr );
60 0           Kephra::App::TabBar::raise_tab_by_doc_nr($doc_nr);
61 0           &reset($doc_nr);
62 0           Kephra::EventTable::trigger('document.new');
63             }
64            
65             sub reset { # restore once opened file from its settings
66 0     0 0   my $doc_nr = Kephra::Document::Data::validate_doc_nr(shift);
67 0 0         $doc_nr = Kephra::Document::Data::current_nr() unless defined $doc_nr;
68 0           my $ep = Kephra::Document::Data::_ep( $doc_nr );
69 0           Kephra::Document::Property::set_readonly(0, $doc_nr);
70 0           $ep->ClearAll;
71 0           $ep->EmptyUndoBuffer;
72 0           $ep->SetSavePoint;
73 0           Kephra::Document::Data::set_attributes_to_default($doc_nr, '');
74 0           Kephra::Document::Data::evaluate_attributes($doc_nr);
75 0           Kephra::App::Window::refresh_title();
76 0           Kephra::App::TabBar::refresh_label($doc_nr);
77 0           Kephra::App::StatusBar::refresh_all_cells();
78 0           Kephra::Edit::Marker::delete_doc($doc_nr);
79 0           Kephra::App::EditPanel::Margin::autosize_line_number($ep, $doc_nr);
80             }
81            
82            
83             sub restore { # add newly opened file from known settings
84 0     0 0   my %file_settings = %{ shift; };
  0            
85 0           my $file = $file_settings{file_path};
86 0           my $config = _file_config();
87 0 0         if ( -e $file ) {
88             # open only text files and empty files
89 0 0 0       return if $config->{open}{only_text} == 1 and -B $file;
90             # check if file is already open and goto this already opened
91 0 0 0       return if $config->{open}{each_once} == 1
92             and Kephra::Document::Data::file_already_open($file);
93 0           my $doc_nr = _new_if_allowed('restore');
94 0           $file_settings{ep_ref} = Kephra::Document::Data::_ep($doc_nr);
95 0           Kephra::Document::Data::set_all_attributes(\%file_settings, $doc_nr);
96 0           _load_file_in_buffer($file, $doc_nr);
97 0           Kephra::Document::Data::set_current_nr($doc_nr);
98 0           Kephra::Document::Data::set_file_path($file, $doc_nr);
99 0           Kephra::Document::Data::evaluate_attributes($doc_nr);
100 0           Kephra::App::TabBar::raise_tab_by_doc_nr($doc_nr);
101 0           return $doc_nr;
102             }
103 0           return -1;
104             }
105            
106            
107             sub add { # create a new document if settings allow it
108 0     0 0   my $file = shift;
109 0           my $config = _file_config();
110 0           my $old_nr = Kephra::Document::Data::current_nr();
111 0 0 0       if ( defined $file and -e $file ) {
112 0           $file = Kephra::Config::standartize_path_slashes( $file );
113             # open only text files and empty files
114             # return if -B $file and $config->{open}{only_text} == 1;
115             # check if file is already open and goto this already opened
116 0           my $other_nr = Kephra::Document::Data::nr_from_file_path($file);
117 0 0 0       return Kephra::Document::Change::to_nr( $other_nr )
118             if $config->{open}{each_once} == 1 and $other_nr > -1;
119             # save constantly changing settings
120 0           Kephra::Document::Data::update_attributes();
121             # create new edit panel
122 0   0       my $doc_nr = _new_if_allowed('add') || 0;
123             # return because settings didn't allow new doc
124 0 0 0       return if $doc_nr > 0 and $doc_nr == $old_nr;
125 0           Kephra::Document::Data::set_current_nr($doc_nr);
126 0           Kephra::Document::Data::set_previous_nr($old_nr);
127             # load default settings for doc attributes
128 0           Kephra::Document::Data::set_attributes_to_default($doc_nr, $file);
129 0           _load_file_in_buffer($file, $doc_nr);
130 0           Kephra::Document::Property::convert_EOL(), Kephra::File::_save_nr($doc_nr)
131 0 0         unless Kephra::Document::Data::get_attribute{'EOL',$doc_nr} eq 'auto';
132 0           Kephra::Document::Data::evaluate_attributes($doc_nr);
133 0           Kephra::App::Window::refresh_title();
134 0           Kephra::App::TabBar::raise_tab_by_doc_nr($doc_nr);
135 0           Kephra::App::EditPanel::Margin::autosize_line_number();
136 0           Kephra::EventTable::trigger('document.new');
137 0           Kephra::EventTable::trigger('document.list');
138             }
139             }
140            
141             # document wide coverter
142 0     0 0   sub convert_indent2tabs { _edit( \&Kephra::Edit::Convert::indent2tabs )}
143 0     0 0   sub convert_indent2spaces { _edit( \&Kephra::Edit::Convert::indent2spaces)}
144 0     0 0   sub convert_spaces2tabs { _edit( \&Kephra::Edit::Convert::spaces2tabs )}
145 0     0 0   sub convert_tabs2spaces { _edit( \&Kephra::Edit::Convert::tabs2spaces )}
146 0     0 0   sub del_trailing_spaces { _edit( \&Kephra::Edit::Format::del_trailing_spaces)}
147            
148 0     0 0   sub save_state {
149             }
150 0     0 0   sub restore_styte {
151             }
152             #
153             sub _edit {
154 0     0     my $coderef = shift;
155 0 0         return unless ref $coderef eq 'CODE';
156 0           Kephra::Edit::_save_positions();
157 0           Kephra::Edit::Select::all();
158 0           &$coderef();
159 0           Kephra::Edit::_restore_positions();
160 0           1;
161             }
162            
163             sub do_with_all {
164 0     0 0   my $code = shift;
165 0 0         return unless ref $code eq 'CODE';
166 0           my $nr = Kephra::Document::Data::current_nr();
167 0           my $attr = Kephra::Document::Data::_attributes();
168 0           Kephra::Document::Data::update_attributes();
169 0           for ( @{ Kephra::Document::Data::all_nr() } ) {
  0            
170 0           Kephra::Document::Data::set_current_nr($_);
171 0           &$code( $attr->[$_] );
172             }
173 0           Kephra::Document::Data::set_current_nr($nr);
174 0           Kephra::Document::Data::evaluate_attributes($nr);
175             }
176            
177             1;
178            
179             =head1 NAME
180            
181             Kephra::Document - general doc functions
182            
183             =head1 DESCRIPTION
184            
185            
186            
187             =cut