« blib/lib/Text/Editor/Vip/Buffer/Plugins/Movements.pm
↑ Summary
blib/lib/Text/Editor/Vip/CommandBlock.pm »
| 1 | |||||||||||||||
| 2 | package Text::Editor::Vip::Buffer::Plugins::GetWord ; | ||||||||||||||
| 3 | |||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 6 | ▶ | use warnings ; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | BEGIN | ||||||||||||||
| 8 | 6 | ▶ | { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 6 | ▶ | use Exporter (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 6 | ▶ | use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 6 | $VERSION = 0.01; | |||||||||||||
| 13 | 6 | @ISA = qw (Exporter); | |||||||||||||
| 14 | 6 | @EXPORT = qw (); | |||||||||||||
| 15 | 6 | @EXPORT_OK = qw (); | |||||||||||||
| 16 | 6 | %EXPORT_TAGS = (); | |||||||||||||
| 17 | } | ||||||||||||||
| 18 | |||||||||||||||
| 19 | =head1 NAME | ||||||||||||||
| 20 | |||||||||||||||
| 21 | Text::Editor::Vip::Buffer::Plugins::GetWord- Vip::Buffer pluggin | ||||||||||||||
| 22 | |||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||
| 24 | |||||||||||||||
| 25 | $buffer->Insert(' word1 word_2 3 word4') ; | ||||||||||||||
| 26 | $buffer->SetModificationCharacter(1) ; | ||||||||||||||
| 27 | is($buffer->GetCurrentWord() , 'word1', 'GetCurrentWord') ; | ||||||||||||||
| 28 | |||||||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||||||
| 30 | |||||||||||||||
| 31 | Plugin for Vip::Buffer. | ||||||||||||||
| 32 | |||||||||||||||
| 33 | =head1 FUNCTIONS | ||||||||||||||
| 34 | |||||||||||||||
| 35 | =cut | ||||||||||||||
| 36 | |||||||||||||||
| 37 | #------------------------------------------------------------------------------- | ||||||||||||||
| 38 | |||||||||||||||
| 39 | sub GetAlphanumericFilter | ||||||||||||||
| 40 | 96 | ▶ | { | ||||||||||||
|
Subroutine
GetAlphanumericFilter: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||
| 41 | |||||||||||||||
| 42 | =head2 GetAlphanumericFilter | ||||||||||||||
| 43 | |||||||||||||||
| 44 | Returns the regex set with L<SetAlphanumericFilter> or the default regex B<qr![a-zA-Z_0-9]+!>. | ||||||||||||||
| 45 | |||||||||||||||
| 46 | =cut | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 96 | my $buffer = shift ; | |||||||||||||
| 49 | |||||||||||||||
| 50 | return | ||||||||||||||
| 51 | ( | ||||||||||||||
| 52 | 96 | ▶ | $buffer->{'Text::Editor::Vip::Buffer::Plugins::GetWord::ALPHANUMERIC_FILTER'} | ||||||||||||
|
Condition50%1/2
$buffer->{'Text::Editor::Vip::Buffer::Plugins::GetWord::ALPHANUMERIC_FILTER'} || qr/[a-zA-Z_0-9]+/
MC/DC0%0/1
$buffer->{'Text::Editor::Vip::Buffer::Plugins::GetWord::ALPHANUMERIC_FILTER'} || qr/[a-zA-Z_0-9]+/
$buffer->{'Text::Editor::Vip::Buffer::Plugins::GetWord::ALPHANUMERIC_FILTER'}
Truth table50%1/2
A
A= $buffer->{'Text::Editor::Vip::Buffer::Plugins::GetWord::ALPHANUMERIC_FILTER'}
| |||||||||||||||
| 53 | || qr![a-zA-Z_0-9]+! | ||||||||||||||
| 54 | ) ; | ||||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | #------------------------------------------------------------------------------- | ||||||||||||||
| 58 | |||||||||||||||
| 59 | sub SetAlphanumericFilter | ||||||||||||||
| 60 | 0 | ▶ | { | ||||||||||||
|
Subroutine
SetAlphanumericFilter: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 61 | |||||||||||||||
| 62 | =head2 SetAlphanumericFilter | ||||||||||||||
| 63 | |||||||||||||||
| 64 | Sets the regex used by this plugin. | ||||||||||||||
| 65 | |||||||||||||||
| 66 | =cut | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 0 | my $buffer = shift ; | |||||||||||||
| 69 | 0 | $buffer->{'Text::Editor::Vip::Buffer::Plugins::GetWord::ALPHANUMERIC_FILTER'} = shift ; | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | #------------------------------------------------------------------------------- | ||||||||||||||
| 73 | |||||||||||||||
| 74 | sub GetFirstWord | ||||||||||||||
| 75 | 5 | ▶ | { | ||||||||||||
|
Subroutine
GetFirstWord: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||
| 76 | |||||||||||||||
| 77 | =head2 GetFirstWord | ||||||||||||||
| 78 | |||||||||||||||
| 79 | Returns the first word matching the word regex in the B<line> passed as argument or the current | ||||||||||||||
| 80 | modification line if none. | ||||||||||||||
| 81 | |||||||||||||||
| 82 | $buffer->GetFirstWord(125) ; | ||||||||||||||
| 83 | $buffer->GetFirstWord() ; #in the current line | ||||||||||||||
| 84 | |||||||||||||||
| 85 | Returns the word( or undef if none found) and its position in the line. | ||||||||||||||
| 86 | |||||||||||||||
| 87 | =cut | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 5 | my $buffer = shift ; | |||||||||||||
| 90 | 5 | my $line_index = shift ; | |||||||||||||
| 91 | |||||||||||||||
| 92 | 5 | ▶ | $line_index = $buffer->GetModificationLine() unless defined $line_index ; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | |||||||||||||||
| 94 | 5 | my $current_line_text = $buffer->GetLineText($line_index) ; | |||||||||||||
| 95 | 5 | my $character_regex = $buffer->GetAlphanumericFilter() ; | |||||||||||||
| 96 | |||||||||||||||
| 97 | 5 | $current_line_text =~ /^(\W*)($character_regex)/ ; | |||||||||||||
| 98 | |||||||||||||||
| 99 | 5 | ▶ | my $length = defined $1 ? length($1) : defined($2) ? 0 : undef ; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 100 | |||||||||||||||
| 101 | 5 | return($2, $length) ; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | #------------------------------------------------------------------------------- | ||||||||||||||
| 105 | |||||||||||||||
| 106 | sub GetPreviousWord | ||||||||||||||
| 107 | 6 | ▶ | { | ||||||||||||
|
Subroutine
GetPreviousWord: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 108 | |||||||||||||||
| 109 | =head2 GetPreviousWord | ||||||||||||||
| 110 | |||||||||||||||
| 111 | Returns the previous word matching the word regex in the the current modification line if none. | ||||||||||||||
| 112 | Returns the word( or undef if none found) and its position in the line. | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =cut | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 6 | my $buffer = shift ; | |||||||||||||
| 117 | |||||||||||||||
| 118 | 6 | my $text = $buffer->GetLineText($buffer->GetModificationLine()) ; | |||||||||||||
| 119 | |||||||||||||||
| 120 | #what if current character is outside the text length? | ||||||||||||||
| 121 | #my $corrected_selection_start_character = $selection_start_character < $line_length ? $selection_start_character : $line_length ; | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 6 | my $current_character_index = $buffer->GetModificationCharacter() ; | |||||||||||||
| 124 | 6 | ▶ | $current_character_index = length($text) if $current_character_index > length($text) ; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 125 | |||||||||||||||
| 126 | 6 | my $character_regex = $buffer->GetAlphanumericFilter() ; | |||||||||||||
| 127 | |||||||||||||||
| 128 | 6 | my $left_side = reverse substr($text, 0, $current_character_index) ; | |||||||||||||
| 129 | |||||||||||||||
| 130 | 6 | $left_side =~ /^(\W*)($character_regex)/ ; | |||||||||||||
| 131 | |||||||||||||||
| 132 | 6 | ▶ | my $previous_word = reverse $2 if defined $2 ; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | |||||||||||||||
| 134 | 6 | my $position ; | |||||||||||||
| 135 | 6 | ▶ | if(defined $previous_word) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 136 | { | ||||||||||||||
| 137 | 5 | ▶ | my $non_word_length = defined $1 ? length($1) : 0 ; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 138 | |||||||||||||||
| 139 | 5 | $position = $current_character_index - ($non_word_length + length($previous_word)) ; | |||||||||||||
| 140 | } | ||||||||||||||
| 141 | |||||||||||||||
| 142 | |||||||||||||||
| 143 | |||||||||||||||
| 144 | 6 | return($previous_word, $position) ; | |||||||||||||
| 145 | } | ||||||||||||||
| 146 | |||||||||||||||
| 147 | #------------------------------------------------------------------------------- | ||||||||||||||
| 148 | |||||||||||||||
| 149 | sub GetCurrentWord | ||||||||||||||
| 150 | 13 | ▶ | { | ||||||||||||
|
Subroutine
GetCurrentWord: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||
| 151 | |||||||||||||||
| 152 | =head2 GetCurrentWord | ||||||||||||||
| 153 | |||||||||||||||
| 154 | Returns the word at the modification position or undef if no word is found. | ||||||||||||||
| 155 | |||||||||||||||
| 156 | =cut | ||||||||||||||
| 157 | |||||||||||||||
| 158 | 13 | my $buffer = shift ; | |||||||||||||
| 159 | |||||||||||||||
| 160 | 13 | my $modification_character = $buffer->GetModificationCharacter() ; | |||||||||||||
| 161 | |||||||||||||||
| 162 | 13 | my $current_line_text = $buffer->GetLineText($buffer->GetModificationLine()) ; | |||||||||||||
| 163 | 13 | my $current_line_length = length($current_line_text) ; | |||||||||||||
| 164 | |||||||||||||||
| 165 | 13 | ▶ | return if $modification_character > $current_line_length ; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 166 | |||||||||||||||
| 167 | 12 | my $character_regex = $buffer->GetAlphanumericFilter() ; | |||||||||||||
| 168 | 12 | my $current_character = substr($current_line_text, $modification_character, 1) ; | |||||||||||||
| 169 | |||||||||||||||
| 170 | 12 | my $current_word ; | |||||||||||||
| 171 | 12 | my $cursor_is_at_the_end_of_the_word = 1 ; | |||||||||||||
| 172 | |||||||||||||||
| 173 | 12 | ▶ | if($current_character =~ /$character_regex/) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 174 | { | ||||||||||||||
| 175 | 9 | $current_word = $current_character ; | |||||||||||||
| 176 | |||||||||||||||
| 177 | 9 | ▶ | for(my $character_index = $modification_character - 1 ; $character_index >= 0 ; $character_index--) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 178 | { | ||||||||||||||
| 179 | 7 | $current_character = substr($current_line_text, $character_index, 1) ; | |||||||||||||
| 180 | |||||||||||||||
| 181 | 7 | ▶ | if($current_character =~ /$character_regex/) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 182 | { | ||||||||||||||
| 183 | 4 | $current_word = $current_character . $current_word ; | |||||||||||||
| 184 | } | ||||||||||||||
| 185 | else | ||||||||||||||
| 186 | { | ||||||||||||||
| 187 | # not character | ||||||||||||||
| 188 | 3 | last ; | |||||||||||||
| 189 | } | ||||||||||||||
| 190 | } | ||||||||||||||
| 191 | |||||||||||||||
| 192 | 9 | ▶ | for(my $character_index = $modification_character + 1 ; $character_index < $current_line_length ; $character_index++) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 193 | { | ||||||||||||||
| 194 | 34 | $current_character = substr($current_line_text, $character_index, 1) ; | |||||||||||||
| 195 | |||||||||||||||
| 196 | 34 | ▶ | if($current_character =~ /$character_regex/) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 197 | { | ||||||||||||||
| 198 | 26 | $current_word .= $current_character ; | |||||||||||||
| 199 | 26 | $cursor_is_at_the_end_of_the_word = 0 ; | |||||||||||||
| 200 | } | ||||||||||||||
| 201 | else | ||||||||||||||
| 202 | { | ||||||||||||||
| 203 | # not character | ||||||||||||||
| 204 | 8 | last ; | |||||||||||||
| 205 | } | ||||||||||||||
| 206 | } | ||||||||||||||
| 207 | } | ||||||||||||||
| 208 | #else | ||||||||||||||
| 209 | # not on a character | ||||||||||||||
| 210 | |||||||||||||||
| 211 | 12 | return($current_word) ; | |||||||||||||
| 212 | } | ||||||||||||||
| 213 | |||||||||||||||
| 214 | #------------------------------------------------------------------------------- | ||||||||||||||
| 215 | |||||||||||||||
| 216 | sub GetPreviousAlphanumeric | ||||||||||||||
| 217 | 4 | ▶ | { | ||||||||||||
|
Subroutine
GetPreviousAlphanumeric: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 218 | |||||||||||||||
| 219 | =head2 GetPreviousAlphanumeric | ||||||||||||||
| 220 | |||||||||||||||
| 221 | Get all the characters matching the aphanumeric regex from the current position and backwards. | ||||||||||||||
| 222 | |||||||||||||||
| 223 | =cut | ||||||||||||||
| 224 | |||||||||||||||
| 225 | 4 | my $buffer = shift ; | |||||||||||||
| 226 | |||||||||||||||
| 227 | # Get all string contents from 0 to the cursor position and flip it round | ||||||||||||||
| 228 | 4 | my $line = reverse substr | |||||||||||||
| 229 | ( | ||||||||||||||
| 230 | $buffer->GetLineText($buffer->GetModificationLine()) | ||||||||||||||
| 231 | , 0 | ||||||||||||||
| 232 | , $buffer->GetModificationCharacter() | ||||||||||||||
| 233 | ) ; | ||||||||||||||
| 234 | |||||||||||||||
| 235 | 4 | my $alphanumeric_filter = $buffer->GetAlphanumericFilter() ; | |||||||||||||
| 236 | 4 | my ($prefix) = $line =~ /($alphanumeric_filter)/ ; | |||||||||||||
| 237 | |||||||||||||||
| 238 | # !! reverse of undef is defined. | ||||||||||||||
| 239 | 4 | ▶ | if(defined $prefix) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 240 | { | ||||||||||||||
| 241 | 3 | return(reverse $prefix) ; | |||||||||||||
| 242 | } | ||||||||||||||
| 243 | else | ||||||||||||||
| 244 | { | ||||||||||||||
| 245 | 1 | return(undef) ; | |||||||||||||
| 246 | } | ||||||||||||||
| 247 | } | ||||||||||||||
| 248 | |||||||||||||||
| 249 | #------------------------------------------------------------------------------- | ||||||||||||||
| 250 | |||||||||||||||
| 251 | sub GetNextAlphanumeric | ||||||||||||||
| 252 | 4 | ▶ | { | ||||||||||||
|
Subroutine
GetNextAlphanumeric: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 253 | |||||||||||||||
| 254 | =head2 GetNextAlphanumeric | ||||||||||||||
| 255 | |||||||||||||||
| 256 | Get all the characters matching the aphanumeric regex from the current position. | ||||||||||||||
| 257 | |||||||||||||||
| 258 | =cut | ||||||||||||||
| 259 | |||||||||||||||
| 260 | 4 | my $buffer = shift ; | |||||||||||||
| 261 | |||||||||||||||
| 262 | 4 | my $modification_character = $buffer->GetModificationCharacter() ; | |||||||||||||
| 263 | |||||||||||||||
| 264 | 4 | my $current_line_text = $buffer->GetLineText($buffer->GetModificationLine()) ; | |||||||||||||
| 265 | 4 | my $current_line_length = length($current_line_text) ; | |||||||||||||
| 266 | |||||||||||||||
| 267 | 4 | ▶ | return if $modification_character > $current_line_length ; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 268 | |||||||||||||||
| 269 | 3 | my $line = substr | |||||||||||||
| 270 | ( | ||||||||||||||
| 271 | $buffer->GetLineText($buffer->GetModificationLine()) | ||||||||||||||
| 272 | , $modification_character | ||||||||||||||
| 273 | ) ; | ||||||||||||||
| 274 | |||||||||||||||
| 275 | 3 | my $alphanumeric_filter = $buffer->GetAlphanumericFilter() ; | |||||||||||||
| 276 | 3 | my ($postfix) = $line =~ /($alphanumeric_filter)/ ; | |||||||||||||
| 277 | |||||||||||||||
| 278 | 3 | return($postfix) ; | |||||||||||||
| 279 | } | ||||||||||||||
| 280 | |||||||||||||||
| 281 | #------------------------------------------------------------------------------- | ||||||||||||||
| 282 | |||||||||||||||
| 283 | 6 | 1 ; | |||||||||||||
| 284 | |||||||||||||||
| 285 | =head1 AUTHOR | ||||||||||||||
| 286 | |||||||||||||||
| 287 | Khemir Nadim ibn Hamouda | ||||||||||||||
| 288 | CPAN ID: NKH | ||||||||||||||
| 289 | mailto:nadim@khemir.net | ||||||||||||||
| 290 | http:// no web site | ||||||||||||||
| 291 | |||||||||||||||
| 292 | =head1 COPYRIGHT | ||||||||||||||
| 293 | |||||||||||||||
| 294 | This program is free software; you can redistribute | ||||||||||||||
| 295 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||
| 296 | |||||||||||||||
| 297 | The full text of the license can be found in the | ||||||||||||||
| 298 | LICENSE file included with this module. | ||||||||||||||
| 299 | |||||||||||||||
| 300 | =cut | ||||||||||||||