File Coverage

lib/Term/ReadLine/Perl5/Common.pm
Criterion Covered Total %
statement 32 44 72.7
branch 6 8 75.0
condition 2 6 33.3
subroutine 8 10 80.0
pod 4 5 80.0
total 52 73 71.2


line stmt bran cond sub pod time code
1             package Term::ReadLine::Perl5::Common;
2 12     12   53144 use strict; use warnings;
  12     12   57  
  12         357  
  12         73  
  12         35  
  12         352  
3 12     12   3118 use English;
  12         29599  
  12         84  
4              
5             =head1 NAME
6              
7             Term::ReadLine::Perl5::Common
8              
9             =head1 DESCRIPTION
10              
11             A non-OO package which contains commmon routines for the OO (L and non-OO L routines of
12             L
13              
14             =cut
15              
16 12     12   5489 use Exporter;
  12         49  
  12         455  
17 12     12   70 use vars qw(@EXPORT @ISA);
  12         26  
  12         11616  
18             @ISA = qw(Exporter);
19             @EXPORT = qw(ctrl unescape canonic_command_function);
20              
21             =head1 SUBROUTINES
22              
23             =head2 Key-Binding Functions
24              
25             =head3 F_Ding
26              
27             Ring the bell.
28              
29             Should do something with I<$var_PreferVisibleBel> here, but what?
30              
31             =cut
32              
33             sub F_Ding($) {
34 0     0 1 0 my $term_OUT = shift;
35 0         0 local $\ = '';
36 0         0 local $OUTPUT_RECORD_SEPARATOR = '';
37 0         0 print $term_OUT "\007";
38 0         0 return; # Undefined return value
39             }
40              
41             =head2 Internal Functions
42              
43             =head3 ctrl
44              
45             B(I<$ord>)
46              
47             Returns the ordinal number for the corresponding control code.
48              
49             For example I returns the ordinal for I
50             or 1. I does the same thing.
51              
52             =cut
53              
54             sub ctrl {
55 705 100 66 705 1 4226 $_[0] ^ (($_[0]>=ord('a') && $_[0]<=ord('z')) ? 0x60 : 0x40);
56             }
57              
58             =head3 rl_tilde_expand
59              
60             rl_tilde_expand($prefix) => list of usernames
61              
62             Returns a list of completions that begin with the given prefix,
63             I<$prefix>. This only works if we have I available.
64              
65             =cut
66              
67             sub rl_tilde_expand($) {
68 0     0 1 0 my $prefix = shift;
69 0         0 my @matches = ();
70 0         0 setpwent();
71 0         0 while (my @fields = (getpwent)[0]) {
72 0 0 0     0 push @matches, $fields[0]
73             if ( $prefix eq ''
74             || $prefix eq substr($fields[0], 0, length($prefix)) );
75             }
76 0         0 setpwent();
77 0         0 @matches;
78             }
79              
80             =head3 unescape
81              
82             unescape($string) -> List of keys
83              
84             This internal function that takes I<$string> possibly containing
85             escape sequences, and converts to a series of octal keys.
86              
87             It has special rules for dealing with readline-specific escape-sequence
88             commands.
89              
90             New-style key bindings are enclosed in double-quotes.
91             Characters are taken verbatim except the special cases:
92              
93             \C-x Control x (for any x)
94             \M-x Meta x (for any x)
95             \e Escape
96             \* Set the keymap default (JP: added this)
97             (must be the last character of the sequence)
98             \x x (unless it fits the above pattern)
99              
100             Special case "\C-\M-x", should be treated like "\M-\C-x".
101              
102             =cut
103              
104             my @ESCAPE_REGEXPS = (
105             # Ctrl-meta
106             [ qr/^\\C-\\M-(.)/, sub { ord("\e"), ctrl(ord(shift)) } ],
107             # Meta
108             [ qr/^\\(M-|e)/, sub { ord("\e") } ],
109             # Ctrl
110             [ qr/^\\C-(.)/, sub { ctrl(ord(shift)) } ],
111             # hex value
112             [ qr/^\\x([0-9a-fA-F]{2})/, sub { hex(shift) } ],
113             # octal value
114             [ qr/^\\([0-7]{3})/, sub { oct(shift) } ],
115             # default
116             [ qr/^\\\*$/, sub { 'default'; } ],
117             # EOT (Ctrl-D)
118             [ qr/^\\d/, sub { 4 } ],
119             # Backspace
120             [ qr/\\b/, sub { 0x7f } ],
121             # Escape Sequence
122             [ qr/\\(.)/,
123             sub {
124             my $chr = shift;
125             ord(($chr =~ /^[afnrtv]$/) ? eval(qq("\\$chr")) : $chr);
126             } ],
127             );
128              
129             sub unescape($) {
130 8     8 1 3944 my $key = shift;
131 8         18 my @keys;
132              
133 8         22 CHAR: while (length($key) > 0) {
134 9         19 foreach my $command (@ESCAPE_REGEXPS) {
135 44         96 my $regex = $command->[0];
136 44 100       570 if ($key =~ s/^$regex//) {
137 8         28 push @keys, $command->[1]->($1);
138 8         32 next CHAR;
139             }
140             }
141 1         3 push @keys, ord($key);
142 1         3 substr($key,0,1) = '';
143             }
144             @keys
145 8         24 }
146              
147             # Canonicalize command function names according to these rules:
148             #
149             # * names have start with an uppercase letter
150             # * a dash followed by a letter gets turned into the uppercase letter with
151             # the dash removed.
152             #
153             # Examples:
154             # yank => Yank
155             # beginning-of-line => BeginningOfLine
156             sub canonic_command_function($) {
157 3817     3817 0 7602 my $function_name = shift;
158 3817 100       7841 return undef unless defined($function_name);
159 3816         7963 $function_name = "\u$function_name";
160 3816         9424 $function_name =~ s/-(.)/\u$1/g;
161 3816         9448 $function_name;
162             }
163              
164             unless (caller) {
165             foreach my $word (qw(yank BeginningOfLine beginning-of-line)) {
166             printf("'%s' canonicalizes to '%s'\n",
167             $word, canonic_command_function($word));
168             }
169              
170             foreach my $word (qw(\C-w \C-\M-a \M-e \x10 \007 \010 \d \b)) {
171             my @unescaped = unescape($word);
172             print "unescape($word) is ", join(', ', @unescaped), "\n";
173             }
174             }
175              
176             1;