lib/Term/ReadLine/Perl5/Common.pm

filter: Statementstmt 68.7% 44 / 64 Branchbran 56.2% 9 / 16 Conditioncond 33.3% 2 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 78.9% 15 / 19 Podpod 80.0% 4 / 5 totaltotal 65.7% 75 / 114 CC 11 SCAR 15.5
CC
11
Cov
64%
rl_tilde_expand
20.0
__ANON__
6.0
unescape
4.0
CRAP 4.7
« blib/lib/Term/ReadLine/Perl5/OO/History.pm ↑ Summary blib/lib/Term/ReadLine/Perl5/Common.pm »
1 package Term::ReadLine::Perl5::Common;
2 12 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
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<Term::ReadLine::Perl5::OO> and non-OO L<Term::ReadLine::Perl5::readline> routines of
12 L<Term::ReadLine::Common>
13  
14 =cut
15  
16 12 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use vars qw(@EXPORT @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 @ISA = qw(Exporter);
19 12 @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 =cut
31  
32 sub F_Ding($) {
33 0 ▶ my $term_OUT = shift;
Subroutine F_Ding: not called CC=1 SCAR=6.9
Pod documented
34 0 local $\ = '';
35 0 local $OUTPUT_RECORD_SEPARATOR = '';
36 0 print $term_OUT "\007";
37 0 return; # Undefined return value
38 }
39  
40 =head2 Internal Functions
41  
42 =head3 ctrl
43  
44 B<ctrl>(I<$ord>)
45  
46 Returns the ordinal number for the corresponding control code.
47  
48 For example I<ctrl(ord('a'))> returns the ordinal for I<Ctrl-A>
49 or 1. I<ctrl(ord('A'))> does the same thing.
50  
51 =cut
52  
53 sub ctrl {
54 558 ▶ $_[0] ^ (($_[0]>=ord('a') && $_[0]<=ord('z')) ? 0x60 : 0x40);
Subroutine ctrl: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] >= 97 && $_[0] <= 122 ? : 502 56 558
Condition66%2/3
$_[0] >= 97 and $_[0] <= 122
!ll&&!rl&&r
560502
MC/DC50%1/2
$_[0] >= 97 and $_[0] <= 122
$_[0] >= 97 $_[0] <= 122
Truth table66%2/3
A and B
A= $_[0] >= 97B= $_[0] <= 122
ABresult
0X0
100
111
55 }
56  
57 =head3 rl_tilde_expand
58  
59 rl_tilde_expand($prefix) => list of usernames
60  
61 Returns a list of completions that begin with the given prefix,
62 I<$prefix>. This only works if we have I<getpwent()> available.
63  
64 =cut
65  
66 sub rl_tilde_expand($) {
67 0 ▶ my $prefix = shift;
Subroutine rl_tilde_expand: not called CC=4 SCAR=30.0
Pod documented
68 0 my @matches = ();
69 0 setpwent();
70 0 ▶ while (my @fields = (getpwent)[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@fields) = (getpwent)[0]) 0 0 0
71 0 ▶ push @matches, $fields[0]
Branch: 1 branch
BranchTrueFalseTotal
if $prefix eq "" or $prefix eq substr($fields[0], 0, length $prefix) 0 0 0
Condition0%0/3
$prefix eq "" or $prefix eq substr($fields[0], 0, length $prefix)
l!l&&r!l&&!r
000
MC/DC0%0/2
$prefix eq "" or $prefix eq substr($fields[0], 0, length $prefix)
$prefix eq "" $prefix eq substr($fields[0], 0, length $prefix)
Truth table0%0/3
A or B
A= $prefix eq ""B= $prefix eq substr($fields[0], 0, length $prefix)
ABresult
1X1
011
000
72 if ( $prefix eq ''
73 || $prefix eq substr($fields[0], 0, length($prefix)) );
74 }
75 0 setpwent();
76 0 @matches;
77 }
78  
79 =head3 unescape
80  
81 unescape($string) -> List of keys
82  
83 This internal function that takes I<$string> possibly containing
84 escape sequences, and converts to a series of octal keys.
85  
86 It has special rules for dealing with readline-specific escape-sequence
87 commands.
88  
89 New-style key bindings are enclosed in double-quotes.
90 Characters are taken verbatim except the special cases:
91  
92 \C-x Control x (for any x)
93 \M-x Meta x (for any x)
94 \e Escape
95 \* Set the keymap default (JP: added this)
96 (must be the last character of the sequence)
97 \x x (unless it fits the above pattern)
98  
99 Special case "\C-\M-x", should be treated like "\M-\C-x".
100  
101 =cut
102  
103 my @ESCAPE_REGEXPS = (
104 # Ctrl-meta <x>
105 1 ▶ [ qr/^\\C-\\M-(.)/, sub { ord("\e"), ctrl(ord(shift)) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
106 # Meta <e>
107 1 ▶ [ qr/^\\(M-|e)/, sub { ord("\e") } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
108 # Ctrl <x>
109 1 ▶ [ qr/^\\C-(.)/, sub { ctrl(ord(shift)) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
110 # hex value
111 1 ▶ [ qr/^\\x([0-9a-fA-F]{2})/, sub { hex(shift) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
112 # octal value
113 2 ▶ [ qr/^\\([0-7]{3})/, sub { oct(shift) } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
114 # default
115 0 ▶ [ qr/^\\\*$/, sub { 'default'; } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
116 # EOT (Ctrl-D)
117 1 ▶ [ qr/^\\d/, sub { 4 } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
118 # Backspace
119 1 ▶ [ qr/\\b/, sub { 0x7f } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
120 # Escape Sequence
121 [ qr/\\(.)/,
122 sub {
123 0 ▶ my $chr = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
124 0 ▶ ord(($chr =~ /^[afnrtv]$/) ? eval(qq("\\$chr")) : $chr);
Branch: 1 branch
BranchTrueFalseTotal
$chr =~ /^[afnrtv]$/ ? : 0 0 0
125 12 } ],
126 );
127  
128 sub unescape($) {
129 8 ▶ my $key = shift;
Subroutine unescape: called CC=4 SCAR=13.9
Pod documented
130 8 my @keys;
131  
132 8 ▶ CHAR: while (length($key) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $key > 0) 9 8 17
133 9 foreach my $command (@ESCAPE_REGEXPS) {
134 44 my $regex = $command->[0];
135 44 ▶ if ($key =~ s/^$regex//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ s/^$regex//) 8 36 44
136 8 push @keys, $command->[1]->($1);
137 8 next CHAR;
138 }
139 }
140 1 push @keys, ord($key);
141 1 substr($key,0,1) = '';
142 }
143 @keys
144 8 }
145  
146 # Canonicalize command function names according to these rules:
147 #
148 # * names have start with an uppercase letter
149 # * a dash followed by a letter gets turned into the uppercase letter with
150 # the dash removed.
151 #
152 # Examples:
153 # yank => Yank
154 # beginning-of-line => BeginningOfLine
155 sub canonic_command_function($) {
156 3065 ▶ my $function_name = shift;
Subroutine canonic_command_function: called CC=2 SCAR=6.9
Pod undocumented
157 3065 ▶ return undef unless defined($function_name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $function_name 2 3063 3065
158 3063 $function_name = "\u$function_name";
159 3063 $function_name =~ s/-(.)/\u$1/g;
160 3063 $function_name;
161 }
162  
163 12 ▶unless (caller) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 12 12
164 0 foreach my $word (qw(yank BeginningOfLine beginning-of-line)) {
165 0 printf("'%s' canonicalizes to '%s'\n",
166 $word, canonic_command_function($word));
167 }
168  
169 0 foreach my $word (qw(\C-w \C-\M-a \M-e \x10 \007 \010 \d \b)) {
170 0 my @unescaped = unescape($word);
171 0 print "unescape($word) is ", join(', ', @unescaped), "\n";
172 }
173 }
174  
175 12 1;
« blib/lib/Term/ReadLine/Perl5/OO/History.pm ↑ Summary blib/lib/Term/ReadLine/Perl5/Common.pm »