lib/Term/ReadLine/Perl5/OO/Keymap.pm

filter: Statementstmt 53.2% 58 / 109 Branchbran 35.0% 21 / 60 Conditioncond 25.0% 5 / 20 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 62.5% 10 / 16 Podpod 25.0% 3 / 12 totaltotal 42.7% 98 / 229 CC 37 SCAR 34.0
CC
37
Cov
44%
classify
56.0
bind_parsed_keyseq
51.0
rl_bind_keyseq
28.5
CRAP 30.0
« lib/Term/ReadLine/Perl5.pm ↑ Summary blib/lib/Term/ReadLine/Perl5/TermCap.pm »
1 package Term::ReadLine::Perl5::OO::Keymap;
2 10 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶eval "use rlib '.' "; # rlib is now optional
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Term::ReadLine::Perl5::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # For extra debug messages
7 10 my $DEBUG = $ENV{'KEYMAP_DEBUG'};
8  
9 sub new {
10 4 ▶ my ($class, $name, $default) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
11 4 my $self = {
12 name => $name,
13 default => canonic_command_function($default),
14 function => [],
15 };
16 4 bless $self, $class;
17 4 return $self;
18 }
19  
20 # A GNU ReadLine function
21 sub rl_make_bare_keymap() {
22 0 ▶ __PACKAGE__->new();
Subroutine rl_make_bare_keymap: not called CC=1 SCAR=6.9
Pod undocumented
23 }
24  
25 sub lookup_key($$) {
26 0 ▶ my ($self, $key) = @_;
Subroutine lookup_key: not called CC=1 SCAR=6.9
Pod undocumented
27 0 return $self->{function};
28 }
29  
30 =head2 bind_parsed_keyseq
31  
32 #B<bind_parsed_keyseq>(I<$keyseq_list> I<$function>, I<$keyseq_str>)
33  
34 Actually inserts the binding for given I<$keyseq_list>
35 to I<$function> into the keymap object. I<$keyseq_list> is
36 reference an list of character ordinals.
37  
38 If C<sequence> is more than one element long, all but the last will
39 cause meta maps to be created. The name will be derived from
40 $<keyseq_str>.
41  
42 I<$Function> will have an implicit I<F_> prepended to it.
43  
44 0 is returned if there is no error.
45  
46 =cut
47  
48 sub bind_parsed_keyseq($$;$)
49 {
50 4 ▶ my ($self, $keyseq_list, $function, $loc_str) = @_;
Subroutine bind_parsed_keyseq: called CC=13 SCAR=39.3
Pod documented
51 4 ▶ $loc_str = '' unless $loc_str;
Branch: 1 branch
BranchTrueFalseTotal
unless $loc_str 4 0 4
52 4 my $bad = 0;
53 4 my @keys = @{$keyseq_list};
54 # make sure $key is set to an int if undefined.
55 4 ▶ my $key = shift(@keys) || 0;
Condition50%1/2
shift @keys || 0
l!l
40
MC/DC0%0/1
shift @keys || 0
shift @keys
Truth table50%1/2
A
A= shift @keys
Aresult
11
00
56 4 my $func_tuple = $self->{function}[$key];
57 # use Data::Printer;
58 # p $func_tuple;
59 # p $key;
60 # p $function;
61 4 ▶ if (@keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys) 0 4 4
62 0 my $next_keymap;
63 0 ▶ if (defined($func_tuple) && $func_tuple->[0] eq 'PrefixMeta') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $func_tuple and $func_tuple->[0] eq 'PrefixMeta') { } 0 0 0
Condition0%0/3
defined $func_tuple and $func_tuple->[0] eq "PrefixMeta"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $func_tuple and $func_tuple->[0] eq "PrefixMeta"
defined $func_tuple $func_tuple->[0] eq "PrefixMeta"
Truth table0%0/3
A and B
A= defined $func_tupleB= $func_tuple->[0] eq "PrefixMeta"
ABresult
0X0
100
111
64 # Good - extending an existing meta map.
65 0 $next_keymap = $func_tuple->[1];
66 } else {
67 0 ▶ if (defined($func_tuple) && $^W) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $func_tuple and $^W) 0 0 0
Condition0%0/3
defined $func_tuple and $^W
!ll&&!rl&&r
000
MC/DC0%0/2
defined $func_tuple and $^W
defined $func_tuple $^W
Truth table0%0/3
A and B
A= defined $func_tupleB= $^W
ABresult
0X0
100
111
68 my $mess =
69 sprintf("Warning%s: Rebinding char #%s from [%s] " .
70 "to meta in keymap %s\n",
71 0 $loc_str, $key, $func_tuple->[1], $self->{name});
72 0 warn $mess;
73 }
74 }
75 0 $self->{function}[$key] = ['F_PrefixMeta', $next_keymap];
76 0 return $next_keymap->bind_parsed_keyseq(\@keys, $function, $loc_str);
77 }
78  
79 4 ▶ if (defined($func_tuple) && $func_tuple->[0] ne 'F_PrefixMeta' &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $func_tuple and $func_tuple->[0] ne "F_PrefixMeta" and $function ne "PrefixMeta") 0 4 4
Condition33%2/6
defined $func_tuple and $func_tuple->[0] ne "F_PrefixMeta" and $function ne "PrefixMeta"
!ll&&!rl&&r
400
defined $func_tuple and $func_tuple->[0] ne "F_PrefixMeta"
!ll&&!rl&&r
400
MC/DC0%0/3
defined $func_tuple and $func_tuple->[0] ne "F_PrefixMeta" and $function ne "PrefixMeta"
defined $func_tuple $func_tuple->[0] ne "F_PrefixMeta" $function ne "PrefixMeta"
Truth table25%1/4
A and B and C
A= defined $func_tupleB= $func_tuple->[0] ne "F_PrefixMeta"C= $function ne "PrefixMeta"
ABCresult
0XX0
10X0
1100
1111
80 $function ne 'PrefixMeta') {
81 0 ▶ if ($^W) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^W) 0 0 0
82 my $mess =
83 sprintf("Warning%s: Rebinding char #%s to " .
84 "non-meta (%s) in keymap %s\n",
85 0 $loc_str, $key, $function, $self->{name});
86 0 warn $mess;
87 }
88 }
89  
90 # FIXME 2nd arg really should be code ref when it is not
91 # a keymap. And do better than =~.
92 4 ▶ my $function_name =
Branch: 1 branch
BranchTrueFalseTotal
$function =~ /Term::ReadLine::Perl5::OO::Keymap/ ? : 2 2 4
93 ($function =~ /Term::ReadLine::Perl5::OO::Keymap/) ?
94 'PrefixMeta' : $function;
95  
96 4 $self->{function}[$key] = [$function_name, $function];
97 # p $self->{function}[$key];
98 4 return $bad;
99 }
100  
101 =head3 rl_bind_keyseq
102  
103 B<rl_bind_keyseq>(I<$keyspec>, I<$function>)
104  
105 Bind the key sequence represented by the string I<keyseq> to the
106 function function, beginning in the current keymap. This makes new
107 keymaps as necessary. The return value is non-zero if keyseq is
108 invalid. I<$keyspec> should be the name of key sequence in one of two
109 forms:
110  
111 Old (GNU readline documented) form:
112  
113 M-x to indicate Meta-x
114 C-x to indicate Ctrl-x
115 M-C-x to indicate Meta-Ctrl-x
116 x simple char x
117  
118 where I<x> above can be a single character, or the special:
119  
120 special means
121 -------- -----
122 space space ( )
123 spc space ( )
124 tab tab (\t)
125 del delete (0x7f)
126 rubout delete (0x7f)
127 newline newline (\n)
128 lfd newline (\n)
129 ret return (\r)
130 return return (\r)
131 escape escape (\e)
132 esc escape (\e)
133  
134 New form:
135 "chars" (note the required double-quotes)
136  
137 where each char in the list represents a character in the sequence, except
138 for the special sequences:
139  
140 \\C-x Ctrl-x
141 \\M-x Meta-x
142 \\M-C-x Meta-Ctrl-x
143 \\e escape.
144 \\x x (if not one of the above)
145  
146  
147 C<$function> should be in the form C<BeginningOfLine> or C<beginning-of-line>.
148  
149 It is an error for the function to not be known....
150  
151 As an example, the following lines in .inputrc will bind one's xterm
152 arrow keys:
153  
154 "\e[[A": previous-history
155 "\e[[B": next-history
156 "\e[[C": forward-char
157 "\e[[D": backward-char
158  
159 =cut
160  
161 sub rl_bind_keyseq($$$;$)
162 {
163 4 ▶ my ($self, $keyseq, $func, $location_msg) = @_;
Subroutine rl_bind_keyseq: called CC=14 SCAR=33.5
Pod documented
164 4 ▶ $location_msg = '' unless $location_msg;
Branch: 1 branch
BranchTrueFalseTotal
unless $location_msg 4 0 4
165 4 $func = canonic_command_function($func);
166  
167 # print "sequence [$keyseq] func [$func]\n"; ##DEBUG
168  
169 4 my @keys = ();
170 4 ▶ if ($keyseq =~ m/"((?:\\.|[^\\])*)"/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keyseq =~ /"((?:\\.|[^\\])*)"/s) { } 0 4 4
171 # A new-style binding.
172 0 @keys = unescape("$1");
173 } else {
174 # An old-style binding... only one key (or Meta+key)
175 4 my $new_keyseq = $keyseq;
176 4 my $is_ctrl = ($new_keyseq =~ s{\b(C|Control|CTRL)-}{}i);
177 4 ▶ if ($keyseq =~ s{\b(M|Meta)-}{}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keyseq =~ s/\b(M|Meta)-//i) 0 4 4
178 0 push(@keys, ord("\e"));
179 }
180  
181 # Isolate key part. This matches GNU's implementation.
182 # If the key is '-', be careful not to delete it!
183 4 $new_keyseq =~ s/.*-(.)/$1/;
184 4 ▶ if ($new_keyseq =~ /^(space|spc)$/i) { $new_keyseq = ' '; }
Branch: 7 branches
BranchTrueFalseTotal
if ($new_keyseq =~ /^(space|spc)$/i) { } 0 4 4
elsif ($new_keyseq =~ /^(rubout|del)$/i) { } 0 4 4
elsif ($new_keyseq =~ /^tab$/i) { } 0 4 4
elsif ($new_keyseq =~ /^(return|ret)$/i) { } 0 4 4
elsif ($new_keyseq =~ /^(newline|lfd)$/i) { } 0 4 4
elsif ($new_keyseq =~ /^(escape|esc)$/i) { } 2 2 4
elsif (length $new_keyseq > 1) { } 0 2 2
185 0 elsif ($new_keyseq =~ /^(rubout|del)$/i) { $new_keyseq = "\x7f"; }
186 0 elsif ($new_keyseq =~ /^tab$/i) { $new_keyseq = "\t"; }
187 0 elsif ($new_keyseq =~ /^(return|ret)$/i) { $new_keyseq = "\r"; }
188 0 elsif ($new_keyseq =~ /^(newline|lfd)$/i) { $new_keyseq = "\n"; }
189 2 elsif ($new_keyseq =~ /^(escape|esc)$/i) { $new_keyseq = "\e"; }
190 elsif (length($new_keyseq) > 1) {
191 0 ▶ warn "Warning$location_msg: strange binding [$keyseq->$new_keyseq]\n"
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
192 if $^W;
193 }
194 4 my $key = ord($new_keyseq);
195 4 ▶ $key = Term::ReadLine::Perl5::Common::ctrl($key) if $is_ctrl;
Branch: 1 branch
BranchTrueFalseTotal
if $is_ctrl 0 4 4
196 4 push(@keys, $key);
197 }
198  
199 # Now do the mapping of the sequence represented in @keys
200 4 ▶ printf("rl_bind_keyseq(%s->%s, %s)\n",
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 4 4
201 $keyseq, $func, join(', ', @keys)) if $DEBUG;
202 4 $self->bind_parsed_keyseq(\@keys, $func);
203 }
204  
205 =head3 bind_keys
206  
207 Accepts an array as pairs ($keyspec, $function, [$keyspec, $function]...).
208 and maps the associated bindings to the current KeyMap.
209 =cut
210  
211 sub bind_keys
212 {
213 4 ▶ my $self = shift;
Subroutine bind_keys: called CC=3 SCAR=11.0
Pod documented
214 4 my ($keyseq, $func);
215 4 ▶ while (defined($keyseq = shift(@_)) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined($keyseq = shift @_) and defined($func = shift @_)) 4 4 8
Condition66%2/3
defined($keyseq = shift @_) and defined($func = shift @_)
!ll&&!rl&&r
404
MC/DC50%1/2
defined($keyseq = shift @_) and defined($func = shift @_)
defined($keyseq = shift @_) defined($func = shift @_)
Truth table66%2/3
A and B
A= defined($keyseq = shift @_)B= defined($func = shift @_)
ABresult
0X0
100
111
216 defined($func = shift(@_))) {
217 4 $self->rl_bind_keyseq($keyseq, $func);
218 }
219 }
220  
221 sub classify($) {
222 0 ▶ my $ord = shift;
Subroutine classify: not called CC=7 SCAR=40.3
Pod undocumented
223 0 ▶ return 'C-' . chr($ord+96) if $ord <= 26;
Branch: 1 branch
BranchTrueFalseTotal
if $ord <= 26 0 0 0
224 0 ▶ return chr($ord) if $ord >= 33 && $ord < 127;
Branch: 1 branch
BranchTrueFalseTotal
if $ord >= 33 and $ord < 127 0 0 0
Condition0%0/3
$ord >= 33 and $ord < 127
!ll&&!rl&&r
000
MC/DC0%0/2
$ord >= 33 and $ord < 127
$ord >= 33 $ord < 127
Truth table0%0/3
A and B
A= $ord >= 33B= $ord < 127
ABresult
0X0
100
111
225 0 ▶ return 'DEL' if $ord == 127;
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 127 0 0 0
226 0 ▶ return "' '" if $ord == 32;
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 32 0 0 0
227 0 ▶ return "ESC" if $ord == 27;
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 27 0 0 0
228 0 return $ord;
229 }
230  
231 # Turn command function names into their GNU Readline equivalet according to
232 # these rules:
233 #
234 # * names start with a lowercase letter
235 # * a lowercase followed by an Uppercase letter gets turned into lower-case - lower-case
236 #
237 # Examples:
238 # Yank => yank
239 # BeginningOfLine => beginning-of-line
240 sub gnu_command_function($) {
241 0 ▶ my $function_name = shift;
Subroutine gnu_command_function: not called CC=1 SCAR=6.9
Pod undocumented
242 0 $function_name = "\l$function_name";
243 0 $function_name =~ s/([a-z])([A-Z])/$1-\l$2/g;
244 0 $function_name;
245 }
246  
247 sub inspect($) {
248 0 ▶ my ($self, $prefix) = @_;
Subroutine inspect: not called CC=4 SCAR=30.0
Pod undocumented
249 0 my @results = ();
250 0 my @continue = ();
251 0 ▶ for (my $i=0; $i<=127; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 127) 0 0 0
252 0 my $command_name = $self->{function}[$i][0];
253 0 ▶ next unless defined($command_name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command_name 0 0 0
254 0 push @results, sprintf("%s%s\t%s\n", $prefix,
255 classify($i),
256 gnu_command_function($command_name));
257 0 ▶ push @continue, $i if $command_name eq 'PrefixMeta';
Branch: 1 branch
BranchTrueFalseTotal
if $command_name eq "PrefixMeta" 0 0 0
258 }
259 0 return (\@results, \@continue);
260 }
261  
262 # GNU Emacs Meta Key bindings
263 sub EmacsMetaKeymap() {
264 2 ▶ my $keymap = __PACKAGE__->new('EmacsMeta', undef);
Subroutine EmacsMetaKeymap: called CC=1 SCAR=0.0
Pod undocumented
265 2 $keymap->bind_keys(
266 'k', 'unix-line-rubout',
267 );
268 2 return $keymap
269 }
270  
271 # GNU Emacs Key binding
272 sub EmacsKeymap() {
273 2 ▶ my $keymap = __PACKAGE__->new('Emacs', 'self-insert');
Subroutine EmacsKeymap: called CC=1 SCAR=0.0
Pod undocumented
274 2 $keymap->bind_keys(
275 # 'C-a', 'beginning-of-line',
276 # 'C-b', 'backward-char',
277 # 'C-c', 'interrupt',
278 # 'C-d', 'delete-char',
279 # 'C-e', 'end-of-line',
280 # 'C-f', 'forward-char',
281 # 'C-h', 'backward-delete-char',
282 # 'C-j', 'accept-line',
283 # 'C-k', 'kill-line',
284 # 'C-l', 'clear-screen',
285 # 'C-m', 'accept-line',
286 # 'C-n', 'next-history',
287 # 'C-p', 'previous-history',
288 # 'C-r', 'reverse-search-history',
289 # 'C-t', 'transpose-chars',
290 # 'C-u', 'unix-line-discard',
291 # 'C-w', 'unix-word-rubout',
292 # 'C-z', 'suspend',
293 'ESC', EmacsMetaKeymap,
294 # 'DEL', 'backward-delete-char',
295 # 'ESC-b', 'backward-char',
296  
297 );
298 2 return $keymap
299 }
300  
301 # Vi input mode key bindings.
302 sub ViKeymap() {
303 0 ▶ my $keymap = __PACKAGE__->new('vi', 'self-insert');
Subroutine ViKeymap: not called CC=1 SCAR=6.9
Pod undocumented
304 0 $keymap->bind_keys(
305 # "\e", 'ViEndInsert',
306 'C-c', 'interrupt',
307 'C-h', 'backward-delete-char',
308 'C-u', 'unix-line-discard',
309 # 'C-v', 'quoted-insert',
310 'C-w', 'unix-word-rubout',
311 'DEL', 'backward-delete-char',
312 # "\n", 'ViAcceptInsert',
313 # "\r", 'ViAcceptInsert',
314 );
315 0 return $keymap;
316 };
317  
318 10 ▶unless (caller) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 10 10
319 # foreach my $keymap (EmacsKeymap(), EmacsMetaKeymap(), ViKeymap()) {
320 0 foreach my $keymap (EmacsKeymap()) {
321 0 my ($results, $continue) = $keymap->inspect('');
322 0 foreach my $line (@{$results}) {
323 0 print $line;
324 }
325 0 print '=' x 30, "\n";
326 }
327 }
328  
329 10 1;
« lib/Term/ReadLine/Perl5.pm ↑ Summary blib/lib/Term/ReadLine/Perl5/TermCap.pm »