keyboard-select: [wWbB] mappings

This commit is contained in:
Bert 2010-08-26 09:52:38 +02:00
parent be65aec919
commit feb4f17eb1
2 changed files with 59 additions and 33 deletions

View File

@ -24,7 +24,7 @@ After installing, put the following lines in your .Xdefaults:
Use Meta-Escape to activate selection mode, then use the following keys: Use Meta-Escape to activate selection mode, then use the following keys:
h/j/k/l: Move cursor left/down/up/right (also with arrow keys) h/j/k/l: Move cursor left/down/up/right (also with arrow keys)
g/G/0/^/$/H/M/L/f/F/;/,: More vi-like cursor movement keys g/G/0/^/$/H/M/L/f/F/;/,/w/W/b/B: More vi-like cursor movement keys
Ctrl-f/b: Scroll down/up one screen Ctrl-f/b: Scroll down/up one screen
Ctrl-d/u: Scroll down/up half a screen Ctrl-d/u: Scroll down/up half a screen
v/V/Ctrl-v: Toggle normal/linewise/blockwise selection v/V/Ctrl-v: Toggle normal/linewise/blockwise selection

View File

@ -1,7 +1,7 @@
#! perl -w #! perl -w
# Author: Bert Muennich # Author: Bert Muennich
# Website: http://www.github.com/muennich/urxvt-perls # Website: http://www.github.com/muennich/urxvt-perls
# Version: git-20100821 # Version: git-20100822
# License: GPLv2 # License: GPLv2
# Use keyboard shortcuts to select and copy text. # Use keyboard shortcuts to select and copy text.
@ -12,7 +12,7 @@
# Use Meta-Escape to activate selection mode, then use the following keys: # Use Meta-Escape to activate selection mode, then use the following keys:
# h/j/k/l: Move cursor left/down/up/right (also with arrow keys) # h/j/k/l: Move cursor left/down/up/right (also with arrow keys)
# g/G/0/^/$/H/M/L/f/F/;/,: More vi-like cursor movement keys # g/G/0/^/$/H/M/L/f/F/;/,/w/W/b/B: More vi-like cursor movement keys
# Ctrl-f/b: Scroll down/up one screen # Ctrl-f/b: Scroll down/up one screen
# Ctrl-d/u: Scroll down/up half a screen # Ctrl-d/u: Scroll down/up half a screen
# v/V/Ctrl-v: Toggle normal/linewise/blockwise selection # v/V/Ctrl-v: Toggle normal/linewise/blockwise selection
@ -22,6 +22,18 @@
use strict; use strict;
sub on_start{
my ($self) = @_;
$self->{patterns}{'w'} = qr/\w[^\w\s]|\W\w|\s\S/;
$self->{patterns}{'W'} = qr/\s\S/;
$self->{patterns}{'b'} = qr/.*(?:\w[^\w\s]|\W\w|\s\S)/;
$self->{patterns}{'B'} = qr/.*\s\S/;
()
}
sub on_user_command { sub on_user_command {
my ($self, $cmd) = @_; my ($self, $cmd) = @_;
@ -44,9 +56,10 @@ sub key_press {
$self->{move_to} = 0; $self->{move_to} = 0;
status_area($self); status_area($self);
} elsif (length($char) > 0) { } elsif (length($char) > 0) {
$self->{move_char} = $key;
$self->{move_to} = 0; $self->{move_to} = 0;
move_to($self, $self->{move_dir}, 1); $self->{patterns}{'f-1'} = qr/^.*\Q$key\E/;
$self->{patterns}{'f+1'} = qr/^.+?\Q$key\E/;
move_to($self, ';');
status_area($self); status_area($self);
} }
return 1; return 1;
@ -100,16 +113,8 @@ sub key_press {
$self->{move_to} = 1; $self->{move_to} = 1;
$self->{move_dir} = $key eq 'F' ? -1 : 1; $self->{move_dir} = $key eq 'F' ? -1 : 1;
status_area($self, $key); status_area($self, $key);
} elsif ($key eq ';' && $self->{move_char}) { } elsif (';,wWbB' =~ m/\Q$key\E/) {
move_to($self, $self->{move_dir}, 1); move_to($self, $key);
} elsif ($key eq ',' && $self->{move_char}) {
move_to($self, $self->{move_dir} * -1, 1);
} elsif ($key eq 'w') {
move_to($self, 1, 0, '(\w[^\w\s])|(\W\w)');
} elsif ($key eq 'W') {
move_to($self, 1, 0, '\s+.');
#} elsif ($key eq 'b') {
# move_to($self, -1, 0, '(\w[^\w\s])|(\W\w)');
} }
return 1; return 1;
@ -167,9 +172,9 @@ sub move_cursor {
} }
$line = $self->line($cr); $line = $self->line($cr);
$self->{offset} = $line->l - 1 if $self->{dollar}; $self->{offset} = $line->l - 1 if $self->{dollar} or
($cr, $cc) = $line->coord_of($self->{offset} < $line->l ? $self->{offset} : $self->{offset} >= $line->l;
$line->l - 1); ($cr, $cc) = $line->coord_of($self->{offset});
$self->screen_cur($cr, $cc); $self->screen_cur($cr, $cc);
# scroll the current cursor position into visible area # scroll the current cursor position into visible area
@ -187,21 +192,30 @@ sub move_cursor {
sub move_to { sub move_to {
my ($self, $dir, $offs, $pattern) = @_; my ($self, $key) = @_;
my ($cr, $cc) = $self->screen_cur(); my ($cr, $cc) = $self->screen_cur();
my $line = $self->line($cr); my $line = $self->line($cr);
my ($dir, $pattern);
my ($wrap, $found) = (0, 0); my ($wrap, $found) = (0, 0);
if ($pattern) { if ($key eq ';' || $key eq ',') {
$wrap = 1; $dir = $self->{move_dir} * ($key eq ',' ? -1 : 1);
$pattern = $self->{patterns}{sprintf('f%+d', $dir)};
return if not $pattern;
} else { } else {
$pattern = "\Q$self->{move_char}\E"; if (lc($key) eq 'w') {
$dir = 1;
} else {
$dir = -1;
}
$pattern = $self->{patterns}{$key};
$wrap = 1;
} }
if ($dir > 0) { if ($dir > 0) {
my $text = substr($line->t, $self->{offset} + $offs); my $text = substr($line->t, $self->{offset});
if ($text =~ m/$pattern/) { if ($text =~ m/$pattern/) {
$self->{offset} += $+[0] - 1 + $offs; $self->{offset} += $+[0] - 1;
$found = 1; $found = 1;
} elsif ($wrap && $line->end + 1 < $self->nrow) { } elsif ($wrap && $line->end + 1 < $self->nrow) {
$cr = $line->end + 1; $cr = $line->end + 1;
@ -210,15 +224,22 @@ sub move_to {
$found = 1; $found = 1;
} }
} elsif ($dir < 0) { } elsif ($dir < 0) {
NEXT: my $text = substr($line->t, 0, $self->{offset} + 1 - $offs); NEXT: my $text = substr($line->t, 0, $self->{offset});
if ($text =~ m/.*$pattern/) { print "\$text: $text\n";
$self->{offset} += $+[0] - $offs - length($text); if ($text =~ m/$pattern/) {
print "matched: $&\n";
$self->{offset} += $+[0] - length($text) - 1;
$found = 1; $found = 1;
} elsif ($wrap && $line->beg > $self->top_row) { } elsif ($wrap) {
$cr = $line->beg - 1; if ($self->{offset} > 0) {
$line = $self->line($cr); $self->{offset} = 0;
$self->{offset} = $line->l - 1; $found = 1;
goto NEXT; } elsif ($line->beg > $self->top_row) {
$cr = $line->beg - 1;
$line = $self->line($cr);
$self->{offset} = $line->l;
goto NEXT;
}
} }
} }
@ -284,7 +305,6 @@ sub activate {
$self->{select} = ""; $self->{select} = "";
$self->{dollar} = 0; $self->{dollar} = 0;
$self->{move_to} = 0; $self->{move_to} = 0;
$self->{move_char} = '';
($self->{oldcr}, $self->{oldcc}) = $self->screen_cur(); ($self->{oldcr}, $self->{oldcc}) = $self->screen_cur();
$self->{old_view_start} = $self->view_start(); $self->{old_view_start} = $self->view_start();
@ -303,6 +323,12 @@ sub activate {
tt_write => \&tt_write, tt_write => \&tt_write,
); );
if ($self->{offset} >= $line->l) {
$self->{offset} = $line->l - 1;
$self->screen_cur($line->coord_of($self->{offset}));
$self->want_refresh();
}
$self->{overlay_len} = 0; $self->{overlay_len} = 0;
status_area($self); status_area($self);