File Coverage

lib/Kephra/Edit/Convert.pm
Criterion Covered Total %
statement 6 80 7.5
branch 0 12 0.0
condition n/a
subroutine 2 23 8.7
pod 0 12 0.0
total 8 127 6.3


line stmt bran cond sub pod time code
1             package Kephra::Edit::Convert;
2             our $VERSION = '0.10';
3            
4 1     1   1188 use strict;
  1         2  
  1         32  
5 1     1   5 use warnings;
  1         2  
  1         1395  
6             # wrapper method for the always same preparation and afterwork
7             sub _default {
8 0     0     my $action = shift;
9 0           return until ref $action eq 'CODE';
10 0           my $ep = Kephra::App::EditPanel::_ref();
11 0           Kephra::EventTable::freeze_group('edit');
12 0           my ($begin, $end) = $ep->GetSelection;
13 0           Kephra::Edit::_save_positions();
14 0           $ep->BeginUndoAction;
15 0 0         $ep->SelectAll if $begin == $end;
16 0           &$action( $ep );
17 0           $ep->EndUndoAction;
18 0           Kephra::Edit::_restore_positions();
19 0           Kephra::EventTable::thaw_group('edit');
20             }
21            
22             # perform regexes on selection
23             sub _tr {
24 0     0     my ($dir, @arg) = @_;
25 0           my ($fi, $ti);
26 0 0         ($fi, $ti) = $dir eq 'fore' ? (0,1) : (1,0);
27             _default( sub {
28 0     0     my $ep = shift;
29 0           my $text = $ep->GetSelectedText();
30 0           $text =~ s/$_->[$fi]/$_->[$ti]/g for @arg;
31 0           $ep->ReplaceSelection($text);
32 0           } );
33             }
34             #
35             # external calls
36             #
37 0     0 0   sub upper_case {_default( sub{ shift->CmdKeyExecute(&Wx::wxSTC_CMD_UPPERCASE) } )}
  0     0      
38 0     0 0   sub lower_case {_default( sub{ shift->CmdKeyExecute(&Wx::wxSTC_CMD_LOWERCASE) } )}
  0     0      
39             sub title_case {_default( sub{
40 0     0     my $ep = shift;
41 0           my ($sel_end, $pos) = ($ep->GetSelectionEnd, 0);
42 0           $ep->SetCurrentPos( $ep->GetSelectionStart - 1 );
43 0           while () {
44 0           $ep->CmdKeyExecute(&Wx::wxSTC_CMD_WORDRIGHT);
45 0           $pos = $ep->GetCurrentPos;
46 0 0         last if $sel_end <= $pos;
47 0           $ep->SetSelection( $pos, $pos + 1 );
48 0           $ep->CmdKeyExecute(&Wx::wxSTC_CMD_UPPERCASE);
49             }
50 0     0 0   } )}
51            
52             sub sentence_case { _default( sub{
53 0     0     my $ep = shift;
54 0           my $line;
55 0           my ($sel_end, $pos) = ($ep->GetSelectionEnd, 0);
56 0           $ep->SetCurrentPos( $ep->GetSelectionStart() - 1 );
57 0           while () {
58 0           $ep->CmdKeyExecute(&Wx::wxSTC_CMD_WORDRIGHT);
59 0           $pos = $ep->GetCurrentPos;
60 0           $line = $ep->LineFromPosition($pos);
61 0 0         if ($pos == $ep->GetLineEndPosition( $ep->LineFromPosition($pos) )) {
62 0           $ep->CmdKeyExecute(&Wx::wxSTC_CMD_WORDRIGHT);
63 0           $pos = $ep->GetCurrentPos;
64             }
65 0 0         last if $sel_end <= $pos;
66 0           $ep->SetSelection( $pos, $pos + 1 );
67 0           $ep->CmdKeyExecute(&Wx::wxSTC_CMD_UPPERCASE);
68 0           $ep->SetCurrentPos( $pos + 1 );
69 0           $ep->SearchAnchor;
70 0 0         last if $ep->SearchNext( 0, "." ) == -1 ;
71             }
72 0     0 0   } )}
73             #
74             #
75             #
76 0     0     sub _tabs2spaces { [' ' x Kephra::App::EditPanel::_ref()->GetTabWidth, "\t"] }
77 0     0 0   sub spaces2tabs { _tr('fore', _tabs2spaces()) }
78 0     0 0   sub tabs2spaces { _tr('back', _tabs2spaces()) }
79             #
80             #
81             #
82 0     0 0   sub indent2tabs { _indention(1) }
83 0     0 0   sub indent2spaces { _indention(0) }
84             sub _indention {
85 0     0     my $indention = shift;
86 0           my $ep = Kephra::App::EditPanel::_ref();
87 0           my ($begin, $end) = $ep->GetSelection;
88 0           my $use_tabs = $ep->GetUseTabs;
89 0           my $i;
90 0           $ep->SetUseTabs($indention);
91 0           $ep->BeginUndoAction();
92 0           for ($ep->LineFromPosition($begin) .. $ep->LineFromPosition($end)) {
93 0           $i = $ep->GetLineIndentation($_);
94 0           $ep->SetLineIndentation( $_, $i + 1 );
95 0           $ep->SetLineIndentation( $_, $i );
96             }
97 0           $ep->EndUndoAction;
98 0           $ep->SetUseTabs($use_tabs);
99             }
100             #
101             # HTML enteties
102             #
103             my $space2entety = [' ',' '];
104             my @char2entity = (
105             ['à','à'],['á','á'],['â','â'],['ä','ä'],
106             ['À','À'],['Á','Á'],['Â','Â'],['Ä','Ä'],
107             ['ã','ã'],['å','å'],['Ã','Ã'],['Å','Å'],
108             ['æ','æ'], ['Æ','Æ'],['ç','ç'],['Ç','Ç'],
109             ['è','è'],['é','é'],['ê','ê'],['ë','ë'],
110             ['È','È'],['É','É'],['Ê','Ê'],['Ë','Ë'],
111             ['ð','ð'], ['Ð','Ð'],
112             ['ì','ì'],['í','í'],['î','î'],['ï','ï'],
113             ['Ì','Ì'],['Í','Í'],['Î','Î'],['Ï','Ï'],
114             ['µ','µ'], ['ñ','ñ'],['Ñ','ñ'],
115             ['ò','ò'],['ó','ó'],['ô','ô'],['ö','ö'],
116             ['Ò','Ò'],['Ó','Ó'],['Ô','Ô'],['Ö','Ö'],
117             ['õ','õ'],['ø','ø'],['Õ','Õ'],['Ø','Ø'],
118             ['ù','ù'],['ú','ú'],['û','û'],['ü','ü'],
119             ['Ù','Ù'],['Ú','Ú'],['Û','Û'],['Ü','Ü'],
120             ['ý','ý'],['Ý','Ý'],['ÿ','ÿ'],
121             ['þ','þ'], ['Þ','Þ'], ['ß','ß'],
122             ['Š','¦'],['Ž','´'], ['ž','¸'],['š','¨'],
123             ['·','·'],['¯','¯'],
124             ['«','«'], ['»','»'], ['¡','¡'],['¿','¿'],
125             ['±','±'],['×','×'], ['÷','÷'],
126             ['¬','¬'], ['°','°'], ['º','º'], ['ª','ª'],
127             ['¹','¹'], ['²','²'], ['³','³'],
128             ['Œ','¼'],['œ','½'],['Ÿ','¾'],
129             ['€','¤'],['¢','¢'], ['£','£'],['¥','¥'],
130             ['§','§'], ['¶','¶'], ['©','©'], ['®','®'],
131             );
132 0     0 0   sub spaces2entities { _tr('fore', $space2entety) }
133 0     0 0   sub entities2spaces { _tr('back', $space2entety) }
134 0     0 0   sub chars2entities { _tr('fore', @char2entity) }
135 0     0 0   sub entities2chars { _tr('back', @char2entity) }
136            
137             1;
138             __END__