From 1587c8f9fb856582e9390b2f1e1fd50c0b72b7c4 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Sun, 26 Sep 2010 00:35:14 +0200 Subject: vim_mode: Add support for t,T,f,T. --- vim-mode/vim_mode.pl | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'vim-mode/vim_mode.pl') diff --git a/vim-mode/vim_mode.pl b/vim-mode/vim_mode.pl index af7366b..e36c41e 100644 --- a/vim-mode/vim_mode.pl +++ b/vim-mode/vim_mode.pl @@ -102,6 +102,11 @@ my $operators = { 'c' => { func => \&cmd_operator_c }, 'd' => { func => \&cmd_operator_d }, + # char movement, works like an operator + 'f' => { func => \&cmd_movement_f }, + 't' => { func => \&cmd_movement_t }, + 'F' => { func => \&cmd_movement_F }, + 'T' => { func => \&cmd_movement_T }, }; # vi-moves like w,b; they move the cursor and may get combined with an @@ -173,6 +178,53 @@ sub cmd_movement_l { _input_pos($pos); } +sub cmd_movement_f { + my ($count, $pos, $char) = @_; + + $pos = _next_occurrence(_input(), $char, $count, $pos); + if ($pos != -1) { + _input_pos($pos); + } +} +sub cmd_movement_t { + my ($count, $pos, $char) = @_; + + $pos = _next_occurrence(_input(), $char, $count, $pos); + if ($pos != -1) { + _input_pos($pos - 1); + } +} +sub cmd_movement_F { + my ($count, $pos, $char) = @_; + + my $input = reverse _input(); + $pos = _next_occurrence($input, $char, $count, length($input) - $pos - 1); + if ($pos != -1) { + _input_pos(length($input) - $pos - 1); + } +} +sub cmd_movement_T { + my ($count, $pos, $char) = @_; + + my $input = reverse _input(); + $pos = _next_occurrence($input, $char, $count, length($input) - $pos - 1); + if ($pos != -1) { + _input_pos(length($input) - $pos - 1 + 1); + } +} +# Find $count-th next occurrence of $char. +sub _next_occurrence { + my ($input, $char, $count, $pos) = @_; + + while ($count-- > 0) { + $pos = index $input, $char, $pos + 1; + if ($pos == -1) { + return -1; + } + } + return $pos; +} + sub cmd_movement_w { my ($count, $pos) = @_; @@ -408,6 +460,16 @@ sub handle_command { print "Processing numeric prefix: $char" if DEBUG; handle_numeric_prefix($char); + # Special case for f,t,F,T as they take an additional argument + } elsif ($operator and + ($operator eq 'f' or $operator eq 't' or + $operator eq 'F' or $operator eq 'T')) { + $numeric_prefix = 1 if not $numeric_prefix; + $operators->{$operator}->{func} + ->($numeric_prefix, _input_pos(), $char); + $operator = undef; + $numeric_prefix = undef; + } elsif (exists $operators->{$char}) { print "Processing operator: $char" if DEBUG; -- cgit v1.2.3