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

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 20 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 213 CC 28 SCAR 45.1
CC
28
Cov
0%
rl_bind_keyseq
210.0
bind_parsed_keyseq
72.0
classify
42.0
CRAP 91.1
1 package Term::ReadLine::Perl5::OO::Keymap;
2 use strict; use warnings;
3 eval "use rlib '.' "; # rlib is now optional
4 use Term::ReadLine::Perl5::Common;
5  
6 # For extra debug messages
7 my $DEBUG = $ENV{'KEYMAP_DEBUG'};
8  
9 sub new {
10 my ($class, $name, $default) = @_;
11 my $self = {
12 name => $name,
13 default => canonic_command_function($default),
14 function => [],
15 };
16 bless $self, $class;
17 return $self;
18 }
19  
20 # A GNU ReadLine function
21 sub rl_make_bare_keymap() {
22 __PACKAGE__->new();
23 }
24  
25 sub lookup_key($$) {
26 my ($self, $key) = @_;
27 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 my ($self, $keyseq_list, $function, $loc_str) = @_;
51 $loc_str = '' unless $loc_str;
52 my $bad = 0;
53 my @keys = @{$keyseq_list};
54 # make sure $key is set to an int if undefined.
55 my $key = shift(@keys) || 0;
56 my $func_tuple = $self->{function}[$key];
57 # use Data::Printer;
58 # p $func_tuple;
59 # p $key;
60 # p $function;
61 if (@keys) {
62 my $next_keymap;
63 if (defined($func_tuple) && $func_tuple->[0] eq 'PrefixMeta') {
64 # Good - extending an existing meta map.
65 $next_keymap = $func_tuple->[1];
66 } else {
67 if (defined($func_tuple) && $^W) {
68 my $mess =
69 sprintf("Warning%s: Rebinding char #%s from [%s] " .
70 "to meta in keymap %s\n",
71 $loc_str, $key, $func_tuple->[1], $self->{name});
72 warn $mess;
73 }
74 }
75 $self->{function}[$key] = ['F_PrefixMeta', $next_keymap];
76 return $next_keymap->bind_parsed_keyseq(\@keys, $function, $loc_str);
77 }
78  
79 if (defined($func_tuple) && $func_tuple->[0] ne 'F_PrefixMeta' &&
80 $function ne 'PrefixMeta') {
81 if ($^W) {
82 my $mess =
83 sprintf("Warning%s: Rebinding char #%s to " .
84 "non-meta (%s) in keymap %s\n",
85 $loc_str, $key, $function, $self->{name});
86 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 my $function_name =
93 ($function =~ /Term::ReadLine::Perl5::OO::Keymap/) ?
94 'PrefixMeta' : $function;
95  
96 $self->{function}[$key] = [$function_name, $function];
97 # p $self->{function}[$key];
98 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 my ($self, $keyseq, $func, $location_msg) = @_;
164 $location_msg = '' unless $location_msg;
165 $func = canonic_command_function($func);
166  
167 # print "sequence [$keyseq] func [$func]\n"; ##DEBUG
168  
169 my @keys = ();
170 if ($keyseq =~ m/"((?:\\.|[^\\])*)"/s) {
171 # A new-style binding.
172 @keys = unescape("$1");
173 } else {
174 # An old-style binding... only one key (or Meta+key)
175 my $new_keyseq = $keyseq;
176 my $is_ctrl = ($new_keyseq =~ s{\b(C|Control|CTRL)-}{}i);
177 if ($keyseq =~ s{\b(M|Meta)-}{}i) {
178 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 $new_keyseq =~ s/.*-(.)/$1/;
184 if ($new_keyseq =~ /^(space|spc)$/i) { $new_keyseq = ' '; }
185 elsif ($new_keyseq =~ /^(rubout|del)$/i) { $new_keyseq = "\x7f"; }
186 elsif ($new_keyseq =~ /^tab$/i) { $new_keyseq = "\t"; }
187 elsif ($new_keyseq =~ /^(return|ret)$/i) { $new_keyseq = "\r"; }
188 elsif ($new_keyseq =~ /^(newline|lfd)$/i) { $new_keyseq = "\n"; }
189 elsif ($new_keyseq =~ /^(escape|esc)$/i) { $new_keyseq = "\e"; }
190 elsif (length($new_keyseq) > 1) {
191 warn "Warning$location_msg: strange binding [$keyseq->$new_keyseq]\n"
192 if $^W;
193 }
194 my $key = ord($new_keyseq);
195 $key = Term::ReadLine::Perl5::Common::ctrl($key) if $is_ctrl;
196 push(@keys, $key);
197 }
198  
199 # Now do the mapping of the sequence represented in @keys
200 printf("rl_bind_keyseq(%s->%s, %s)\n",
201 $keyseq, $func, join(', ', @keys)) if $DEBUG;
202 $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 my $self = shift;
214 my ($keyseq, $func);
215 while (defined($keyseq = shift(@_)) &&
216 defined($func = shift(@_))) {
217 $self->rl_bind_keyseq($keyseq, $func);
218 }
219 }
220  
221 sub classify($) {
222 my $ord = shift;
223 return 'C-' . chr($ord+96) if $ord <= 26;
224 return chr($ord) if $ord >= 33 && $ord < 127;
225 return 'DEL' if $ord == 127;
226 return "' '" if $ord == 32;
227 return "ESC" if $ord == 27;
228 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 my $function_name = shift;
242 $function_name = "\l$function_name";
243 $function_name =~ s/([a-z])([A-Z])/$1-\l$2/g;
244 $function_name;
245 }
246  
247 sub inspect($) {
248 my ($self, $prefix) = @_;
249 my @results = ();
250 my @continue = ();
251 for (my $i=0; $i<=127; $i++) {
252 my $command_name = $self->{function}[$i][0];
253 next unless defined($command_name);
254 push @results, sprintf("%s%s\t%s\n", $prefix,
255 classify($i),
256 gnu_command_function($command_name));
257 push @continue, $i if $command_name eq 'PrefixMeta';
258 }
259 return (\@results, \@continue);
260 }
261  
262 # GNU Emacs Meta Key bindings
263 sub EmacsMetaKeymap() {
264 my $keymap = __PACKAGE__->new('EmacsMeta', undef);
265 $keymap->bind_keys(
266 'k', 'unix-line-rubout',
267 );
268 return $keymap
269 }
270  
271 # GNU Emacs Key binding
272 sub EmacsKeymap() {
273 my $keymap = __PACKAGE__->new('Emacs', 'self-insert');
274 $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 return $keymap
299 }
300  
301 # Vi input mode key bindings.
302 sub ViKeymap() {
303 my $keymap = __PACKAGE__->new('vi', 'self-insert');
304 $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 return $keymap;
316 };
317  
318 unless (caller) {
319 # foreach my $keymap (EmacsKeymap(), EmacsMetaKeymap(), ViKeymap()) {
320 foreach my $keymap (EmacsKeymap()) {
321 my ($results, $continue) = $keymap->inspect('');
322 foreach my $line (@{$results}) {
323 print $line;
324 }
325 print '=' x 30, "\n";
326 }
327 }
328  
329 1;