Codit/Macro.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 137 CC 12 SCAR 17.1
CC
12
Cov
0%
start
12.0
stop
12.0
countref
6.0
CRAP 5.5
1 package App::Codit::Macro;
2  
3 =head1 NAME
4  
5 App::Codit::Macro - Little applets for line to line text tasks.
6  
7 =cut
8  
9 use strict;
10 use warnings;
11 use vars qw ($VERSION);
12 $VERSION = '0.19';
13 use Tk;
14  
15 =head1 SYNOPSIS
16  
17 my $macro = $app->mdi->macroInit($docname, 'macroname', ['Some method', $obj]);
18 $macro->start;
19  
20 =head1 DESCRIPTION
21  
22 B<App::Codit::Macro> creates a task that calls the callback on a line to line basis.
23 It comes in handy for scanning and or modifying a document.
24  
25 The callback receives the document textmanagers object and the line number as parameters.
26  
27 The extension CoditMDI uses it for showing tabs and spaces, removing trailing spaces and fix indentation.
28  
29 =head1 METHODS
30  
31 =over 4
32  
33 =cut
34  
35 sub new {
36 my ($class, $mdi, $name, $doc, $call) = @_;
37 my $self = {
38 CALL => $mdi->CreateCallback(@$call),
39 DOC => $doc,
40 COUNTREF => undef,
41 INTERVAL => 1,
42 LAST => undef,
43 LINE => 1,
44 MDI => $mdi,
45 NAME => $name,
46 REMAIN => 0,
47 WIDG => $mdi->docGet($doc),
48 };
49 bless $self, $class;
50 return $self
51 }
52  
53 =item B<busy>
54  
55 Returns true when the macro is running.
56  
57 =cut
58  
59 sub busy {
60 my $self = shift;
61 return $self->dem->jobExists($self->jobname)
62 }
63  
64 sub call { return $_[0]->{CALL} }
65  
66 sub countref {
67 my $self = shift;
68 $self->{COUNTREF} = shift if @_;
69 return $self->{COUNTREF}
70 }
71  
72 sub cycle {
73 my $self = shift;
74 my $line = $self->line;
75 $self->call->execute($self->widg, $line);
76 $line ++;
77 if ($line > $self->lastline) {
78 $self->stop;
79 } else {
80 $self->line($line);
81 }
82 my $c = $self->countref;
83 $$c ++
84 }
85  
86 sub dem {
87 my $self = shift;
88 return $self->mdi->extGet('Daemons');
89 }
90  
91 sub doc { return $_[0]->{DOC} }
92  
93 =item B<interval>I<(?$cycles?)>
94  
95 Default value 1. It specifies the interval duration for the Daemons extension.
96  
97 =cut
98  
99 sub interval {
100 my $self = shift;
101 $self->{INTERVAL} = shift if @_;
102 return $self->{INTERVAL}
103 }
104  
105 sub jobname {
106 my $self = shift;
107 return $self->doc . $self->name;
108 }
109  
110 =item B<last>I<(?$line?)>
111  
112 Default value undef. Specifies the last line to be handled.
113 If you leave it unset the last line will be the last line of the document.
114  
115 =cut
116  
117 sub last {
118 my $self = shift;
119 $self->{LAST} = shift if @_;
120 return $self->{LAST}
121 }
122  
123 sub lastline {
124 my $self = shift;
125 my $last = $self->last;
126 return $last if defined $last;
127 my $widg = $self->widg;
128 return $widg->linenumber('end - 1c');
129 }
130  
131 =item B<line>I<(?$line?)>
132  
133 Holds the line number that will be processed in the next cycle.
134 Default value before the call to start is 1.
135  
136 =cut
137  
138 sub line {
139 my $self = shift;
140 $self->{LINE} = shift if @_;
141 return $self->{LINE}
142 }
143  
144 sub mdi { return $_[0]->{MDI} }
145  
146 sub name { return $_[0]->{NAME} }
147  
148 =item B<start>
149  
150 Starts the macro.
151  
152 =cut
153  
154 sub start {
155 my $self = shift;
156 return if $self->busy;
157 $self->dem->jobAdd($self->jobname, $self->interval, 'cycle', $self);
158  
159 my $mdi = $self->mdi;
160  
161 my $count = 0;
162 $self->countref(\$count);
163  
164 my $last = $self->last;
165 unless (defined $last) {
166 my $w = $mdi->docGet($self->doc);
167 $last = $w->linenumber('end - 1c');
168 }
169  
170 my $size = $last - $self->line;
171 $mdi->progressAdd($self->jobname, $self->name, $size, \$count);
172 }
173  
174 =item B<stop>
175  
176 Stops the macro. It is called after the last line is processed. However, you
177 may call it to interrupt the macro.
178  
179 =cut
180  
181 sub stop {
182 my $self = shift;
183 return unless $self->busy;
184 $self->dem->jobRemove($self->jobname);
185 my $mdi = $self->mdi;
186 $mdi->macroRemove($self->doc, $self->name) unless $self->remain;
187 $mdi->progressRemove($self->jobname);
188 }
189  
190 =item B<remain>I<(?$flag?)>
191  
192 Default value false. If you set it the macro will not be removed
193 from memory after it finishes.
194  
195 =cut
196  
197 sub remain {
198 my $self = shift;
199 $self->{REMAIN} = shift if @_;
200 return $self->{REMAIN}
201 }
202  
203 sub widg { return $_[0]->{WIDG} }
204  
205 =back
206  
207 =head1 LICENSE
208  
209 Same as Perl.
210  
211 =head1 AUTHOR
212  
213 Hans Jeuken (hanje at cpan dot org)
214  
215 =head1 BUGS AND CAVEATS
216  
217 If you find any bugs, please report them here L<https://github.com/haje61/App-Codit/issues>.
218  
219 =head1 SEE ALSO
220  
221 =over 4
222  
223 =item L<Tk::AppWindow::BaseClasses::Callback>
224  
225 =item L<Tk::AppWindow::Ext::Daemons>
226  
227 =item L<App::Codit::Exit::CoditMDI>
228  
229 =back
230  
231 =cut
232