2010-08-12 20:31:08 -04:00
|
|
|
#! perl -w
|
|
|
|
# Author: Bert Muennich
|
|
|
|
# Website: http://www.github.com/muennich/urxvt-perls
|
2010-08-19 11:12:03 -04:00
|
|
|
# Version: git-20100819
|
2010-08-12 20:31:08 -04:00
|
|
|
# License: GPLv2
|
|
|
|
|
|
|
|
# Use keyboard shortcuts to select and copy text.
|
|
|
|
|
|
|
|
# Usage: put the following lines in your .Xdefaults:
|
2010-08-14 10:47:53 -04:00
|
|
|
# URxvt.perl-ext-common: ...,keyboard-select
|
|
|
|
# URxvt.keysym.M-Escape: perl:keyboard-select:activate
|
2010-08-12 20:31:08 -04:00
|
|
|
|
|
|
|
# Use Meta-Escape to activate selection mode, then use the following keys:
|
2010-08-18 05:11:37 -04:00
|
|
|
# h/j/k/l: Move cursor left/down/up/right (also with arrow keys)
|
|
|
|
# g/G/0/^/$/H/M/L: More vi-like cursor movement keys
|
2010-08-18 18:54:56 -04:00
|
|
|
# Ctrl-f/b: Scroll down/up one screen
|
|
|
|
# Ctrl-d/u: Scroll down/up half a screen
|
2010-08-18 05:11:37 -04:00
|
|
|
# v/V/Ctrl-v: Toggle normal/linewise/blockwise selection
|
|
|
|
# y/Return: Copy selection to primary buffer, Return: deactivate afterwards
|
|
|
|
# q/Escape: Deactivate keyboard selection mode
|
2010-08-12 20:31:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
sub on_user_command {
|
|
|
|
my ($self, $cmd) = @_;
|
|
|
|
|
|
|
|
if ($cmd eq 'keyboard-select:activate') {
|
|
|
|
if (not $self->{active}) {
|
|
|
|
activate($self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub key_press {
|
2010-08-19 11:12:03 -04:00
|
|
|
my ($self, $event, $keysym, $char) = @_;
|
|
|
|
|
|
|
|
if ($self->{move_to} && $char !~ m/[\x00-\x1f\x80-\xaf]/ &&
|
|
|
|
length($char) > 0) {
|
|
|
|
$self->{move_char} = $self->locale_decode($char);
|
|
|
|
move_to($self, $self->{move_to});
|
|
|
|
$self->{move_to} = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2010-08-12 20:31:08 -04:00
|
|
|
|
2010-08-18 05:11:37 -04:00
|
|
|
if ($keysym == 0xff1b || lc($char) eq 'q') {
|
2010-08-12 20:31:08 -04:00
|
|
|
deactivate($self);
|
2010-08-14 10:47:53 -04:00
|
|
|
} elsif ($char eq 'y' || $keysym == 0xff0d) {
|
2010-08-13 14:18:41 -04:00
|
|
|
if ($self->{select}) {
|
2010-08-18 12:48:19 -04:00
|
|
|
if ($self->{select} eq 'b') {
|
|
|
|
$self->selection($self->{selection});
|
|
|
|
$self->selection_grab($event->{time});
|
|
|
|
} else {
|
|
|
|
my ($br, $bc, $er, $ec) = calc_span($self);
|
|
|
|
$ec = $self->line($er)->l if $self->{select} eq 'l';
|
|
|
|
$self->selection_beg($br, $bc);
|
|
|
|
$self->selection_end($er, $ec);
|
|
|
|
$self->selection_make($event->{time});
|
|
|
|
}
|
|
|
|
if ($char eq 'y') {
|
|
|
|
if ($self->{select} ne 'b') {
|
|
|
|
$self->selection_beg(1, 0);
|
|
|
|
$self->selection_end(1, 0);
|
|
|
|
}
|
|
|
|
$self->{select} = '';
|
|
|
|
$self->want_refresh();
|
|
|
|
} else {
|
|
|
|
deactivate($self);
|
|
|
|
}
|
2010-08-12 20:31:08 -04:00
|
|
|
}
|
|
|
|
} elsif ($char eq 'V') {
|
2010-08-13 14:18:41 -04:00
|
|
|
toggle_select($self, 'l');
|
2010-08-12 20:31:08 -04:00
|
|
|
} elsif ($char eq 'v') {
|
|
|
|
if ($event->{state} & urxvt::ControlMask) {
|
2010-08-13 14:18:41 -04:00
|
|
|
toggle_select($self, 'b');
|
2010-08-12 20:31:08 -04:00
|
|
|
} else {
|
2010-08-13 14:18:41 -04:00
|
|
|
toggle_select($self, 'n');
|
2010-08-12 20:31:08 -04:00
|
|
|
}
|
|
|
|
} elsif ($char eq 'k' || $keysym == 0xff52) {
|
2010-08-14 10:47:53 -04:00
|
|
|
move_cursor($self, 'k');
|
2010-08-12 20:31:08 -04:00
|
|
|
} elsif ($char eq 'j' || $keysym == 0xff54) {
|
2010-08-14 10:47:53 -04:00
|
|
|
move_cursor($self, 'j');
|
2010-08-12 20:31:08 -04:00
|
|
|
} elsif ($char eq 'h' || $keysym == 0xff51) {
|
2010-08-14 10:47:53 -04:00
|
|
|
move_cursor($self, 'h');
|
2010-08-12 20:31:08 -04:00
|
|
|
} elsif ($char eq 'l' || $keysym == 0xff53) {
|
2010-08-14 10:47:53 -04:00
|
|
|
move_cursor($self, 'l');
|
2010-08-18 05:11:37 -04:00
|
|
|
} elsif ('gG0^$HML' =~ m/\Q$char\E/ ||
|
2010-08-18 18:54:56 -04:00
|
|
|
('fbdu' =~ m/\Q$char\E/ && $event->{state} & urxvt::ControlMask)) {
|
2010-08-14 10:47:53 -04:00
|
|
|
move_cursor($self, $char);
|
2010-08-19 11:12:03 -04:00
|
|
|
} elsif (lc($char) eq 'f') {
|
|
|
|
$self->{move_to} = $char eq 'F' ? -1 : 1;
|
|
|
|
} elsif ($char eq ';' || $char eq ',') {
|
|
|
|
move_to($self, $char eq ',' ? -1 : 1) if $self->{move_char};
|
2010-08-12 20:31:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub move_cursor {
|
2010-08-14 10:47:53 -04:00
|
|
|
my ($self, $key) = @_;
|
2010-08-18 05:11:37 -04:00
|
|
|
my ($cr, $cc) = $self->screen_cur();
|
|
|
|
my $line = $self->line($cr);
|
2010-08-12 20:31:08 -04:00
|
|
|
|
2010-08-18 18:54:56 -04:00
|
|
|
if ($key eq 'k' && $line->beg > $self->top_row) {
|
|
|
|
$cr = $line->beg - 1;
|
|
|
|
} elsif ($key eq 'j' && $line->end < $self->nrow - 1) {
|
|
|
|
$cr = $line->end + 1;
|
|
|
|
} elsif ($key eq 'h' && $self->{offset} > 0) {
|
|
|
|
$self->{offset} = $line->l - 1 if $self->{offset} >= $line->l;
|
|
|
|
--$self->{offset};
|
|
|
|
$self->{dollar} = 0;
|
|
|
|
} elsif ($key eq 'l' && $self->{offset} < $line->l - 1) {
|
|
|
|
++$self->{offset};
|
|
|
|
} elsif ($key eq 'f' || $key eq 'd') {
|
|
|
|
my $vs = $self->view_start() +
|
|
|
|
($key eq 'd' ? $self->nrow / 2 : $self->nrow - 1);
|
|
|
|
$vs = 0 if $vs > 0;
|
|
|
|
$cr += $vs - $self->view_start($vs);
|
|
|
|
} elsif ($key eq 'b' || $key eq 'u') {
|
|
|
|
my $vs = $self->view_start() -
|
|
|
|
($key eq 'u' ? $self->nrow / 2 : $self->nrow - 1);
|
|
|
|
$vs = $self->top_row if $vs < $self->top_row;
|
|
|
|
$cr += $vs - $self->view_start($vs);
|
|
|
|
} elsif ($key eq 'g') {
|
|
|
|
($cr, $self->{offset}) = ($self->top_row, 0);
|
|
|
|
$self->{dollar} = 0;
|
|
|
|
} elsif ($key eq 'G') {
|
|
|
|
($cr, $self->{offset}) = ($self->nrow - 1, 0);
|
|
|
|
$self->{dollar} = 0;
|
|
|
|
} elsif ($key eq '0') {
|
|
|
|
$self->{offset} = 0;
|
|
|
|
$self->{dollar} = 0;
|
|
|
|
} elsif ($key eq '^') {
|
|
|
|
my $ltxt = $self->special_decode($line->t);
|
|
|
|
while ($ltxt =~ s/^( *)\t/$1 . " " x (8 - length($1) % 8)/e) {}
|
|
|
|
$self->{offset} = $ltxt =~ m/^ +/ ? $+[0] : 0;
|
|
|
|
$self->{dollar} = 0;
|
|
|
|
} elsif ($key eq '$') {
|
|
|
|
$self->{offset} = $line->l - 1;
|
|
|
|
$self->{dollar} = 1;
|
|
|
|
} elsif ($key eq 'H') {
|
|
|
|
$cr = $self->view_start();
|
|
|
|
} elsif ($key eq 'M') {
|
|
|
|
$cr = $self->view_start() + $self->nrow / 2;
|
|
|
|
} elsif ($key eq 'L') {
|
|
|
|
$cr = $self->view_start() + $self->nrow - 1;
|
|
|
|
}
|
2010-08-12 20:31:08 -04:00
|
|
|
|
2010-08-18 18:54:56 -04:00
|
|
|
$line = $self->line($cr);
|
|
|
|
$self->{offset} = $line->l - 1 if $self->{dollar};
|
|
|
|
($cr, $cc) = $line->coord_of($self->{offset} < $line->l ? $self->{offset} :
|
|
|
|
$line->l - 1);
|
|
|
|
$self->screen_cur($cr, $cc);
|
|
|
|
|
|
|
|
# scroll the current cursor position into visible area
|
|
|
|
if ($cr < $self->view_start()) {
|
|
|
|
$self->view_start($cr);
|
|
|
|
} elsif ($cr >= $self->view_start() + $self->nrow) {
|
|
|
|
$self->view_start($cr - $self->nrow + 1);
|
2010-08-12 20:31:08 -04:00
|
|
|
}
|
2010-08-18 18:54:56 -04:00
|
|
|
|
|
|
|
status_area($self);
|
|
|
|
$self->want_refresh();
|
2010-08-12 20:31:08 -04:00
|
|
|
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-19 11:12:03 -04:00
|
|
|
sub move_to {
|
|
|
|
my ($self, $dir) = @_;
|
|
|
|
my ($cr, $cc) = $self->screen_cur();
|
|
|
|
my $line = $self->line($cr);
|
|
|
|
my $found = 0;
|
|
|
|
|
|
|
|
print "move_to: " . $self->{move_char} . " (" . ord($self->locale_decode($self->{move_char})) . ")\n";
|
|
|
|
|
|
|
|
if ($dir > 0 && $self->{offset} < $line->l - 1) {
|
|
|
|
my $text = substr($line->t, $self->{offset} + 1);
|
|
|
|
if ($text =~ m/\Q$self->{move_char}\E/) {
|
|
|
|
$self->{offset} += $-[0] + 1;
|
|
|
|
$found = 1;
|
|
|
|
}
|
|
|
|
} elsif ($dir < 0 && $self->{offset} > 0) {
|
|
|
|
my $text = substr($line->t, 0, $self->{offset});
|
|
|
|
if ($text =~ m/.*(\Q$self->{move_char}\E)/) {
|
|
|
|
$self->{offset} += $-[1] - length($text);
|
|
|
|
$found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($found) {
|
|
|
|
$self->{dollar} = 0;
|
|
|
|
$self->screen_cur($line->coord_of($self->{offset}));
|
|
|
|
$self->want_refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-18 12:48:19 -04:00
|
|
|
sub tt_write {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub refresh {
|
|
|
|
my ($self) = @_;
|
|
|
|
my $reverse_cursor = $self->{select} ne 'l';
|
|
|
|
my ($cr, $cc) = $self->screen_cur();
|
|
|
|
|
|
|
|
if ($self->{select}) {
|
|
|
|
my ($br, $bc, $er, $ec) = calc_span($self);
|
|
|
|
|
|
|
|
if ($self->{select} eq 'b') {
|
|
|
|
delete $self->{selection} if $self->{selection};
|
|
|
|
my $r = $br;
|
|
|
|
while ($r <= $er) {
|
|
|
|
my $line = $self->line($r);
|
|
|
|
if ($bc < $line->l) {
|
2010-08-18 18:54:56 -04:00
|
|
|
$ec = $line->l if $self->{dollar};
|
2010-08-18 12:48:19 -04:00
|
|
|
$self->{selection} .= substr($line->t, $bc, $ec - $bc);
|
|
|
|
my ($br, $bc) = $line->coord_of($bc);
|
|
|
|
my ($er, $ec) = $line->coord_of($ec <= $line->l ? $ec : $line->l);
|
|
|
|
$self->scr_xor_span($br, $bc, $er, $ec, urxvt::RS_RVid);
|
|
|
|
} elsif ($r == $cr) {
|
|
|
|
$reverse_cursor = 0;
|
|
|
|
}
|
|
|
|
$self->{selection} .= "\n" if $line->end < $er;
|
|
|
|
$r = $line->end + 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$self->scr_xor_span($br, $bc, $er, $ec, urxvt::RS_RVid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($reverse_cursor) {
|
|
|
|
# make the cursor visible again
|
|
|
|
$self->scr_xor_span($cr, $cc, $cr, $cc + 1, urxvt::RS_RVid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-12 20:31:08 -04:00
|
|
|
sub activate {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
$self->{active} = 1;
|
2010-08-18 18:54:56 -04:00
|
|
|
|
2010-08-13 14:18:41 -04:00
|
|
|
$self->{select} = "";
|
2010-08-18 18:54:56 -04:00
|
|
|
$self->{dollar} = 0;
|
2010-08-19 11:12:03 -04:00
|
|
|
$self->{move_to} = 0;
|
|
|
|
$self->{move_char} = '';
|
2010-08-12 20:31:08 -04:00
|
|
|
|
2010-08-18 05:11:37 -04:00
|
|
|
($self->{oldcr}, $self->{oldcc}) = $self->screen_cur();
|
|
|
|
$self->{old_view_start} = $self->view_start();
|
|
|
|
$self->{old_pty_ev_events} = $self->pty_ev_events(urxvt::EV_NONE);
|
|
|
|
|
|
|
|
my $line = $self->line($self->{oldcr});
|
|
|
|
$self->{offset} = $line->offset_of($self->{oldcr}, $self->{oldcc});
|
2010-08-12 20:31:08 -04:00
|
|
|
|
|
|
|
$self->selection_beg(1, 0);
|
|
|
|
$self->selection_end(1, 0);
|
|
|
|
|
|
|
|
$self->enable(
|
|
|
|
key_press => \&key_press,
|
|
|
|
refresh_begin => \&refresh,
|
|
|
|
refresh_end => \&refresh,
|
|
|
|
tt_write => \&tt_write,
|
|
|
|
);
|
|
|
|
|
2010-08-18 05:11:37 -04:00
|
|
|
$self->{overlay_len} = 0;
|
2010-08-15 18:43:48 -04:00
|
|
|
status_area($self);
|
|
|
|
|
2010-08-12 20:31:08 -04:00
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub deactivate {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
$self->selection_beg(1, 0);
|
|
|
|
$self->selection_end(1, 0);
|
|
|
|
|
2010-08-15 18:43:48 -04:00
|
|
|
delete $self->{overlay} if $self->{overlay};
|
2010-08-18 12:48:19 -04:00
|
|
|
delete $self->{selection} if $self->{selection};
|
2010-08-15 18:43:48 -04:00
|
|
|
|
2010-08-12 20:31:08 -04:00
|
|
|
$self->disable("key_press", "refresh_begin", "refresh_end", "tt_write");
|
2010-08-18 05:11:37 -04:00
|
|
|
$self->screen_cur($self->{oldcr}, $self->{oldcc});
|
|
|
|
$self->view_start($self->{old_view_start});
|
|
|
|
$self->pty_ev_events($self->{old_pty_ev_events});
|
2010-08-12 20:31:08 -04:00
|
|
|
|
|
|
|
$self->want_refresh();
|
|
|
|
|
|
|
|
$self->{active} = 0;
|
|
|
|
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-15 18:43:48 -04:00
|
|
|
sub status_area {
|
|
|
|
my ($self) = @_;
|
2010-08-18 05:11:37 -04:00
|
|
|
my ($stat, $stat_len);
|
2010-08-15 18:43:48 -04:00
|
|
|
|
|
|
|
if ($self->{select}) {
|
2010-08-18 05:11:37 -04:00
|
|
|
$stat = "V";
|
|
|
|
if ($self->{select} ne 'n') {
|
|
|
|
$stat .= uc($self->{select});
|
2010-08-15 18:43:48 -04:00
|
|
|
}
|
2010-08-18 05:11:37 -04:00
|
|
|
$stat .= " -- ";
|
2010-08-15 18:43:48 -04:00
|
|
|
}
|
2010-08-18 05:11:37 -04:00
|
|
|
|
2010-08-16 11:01:12 -04:00
|
|
|
if ($self->top_row == 0) {
|
2010-08-18 05:11:37 -04:00
|
|
|
$stat .= "All";
|
2010-08-16 11:01:12 -04:00
|
|
|
} elsif ($self->view_start() == $self->top_row) {
|
2010-08-18 05:11:37 -04:00
|
|
|
$stat .= "Top";
|
2010-08-16 11:01:12 -04:00
|
|
|
} elsif ($self->view_start() == 0) {
|
2010-08-18 05:11:37 -04:00
|
|
|
$stat .= "Bot";
|
2010-08-16 11:01:12 -04:00
|
|
|
} else {
|
2010-08-18 18:54:56 -04:00
|
|
|
$stat .= sprintf("%2d%%",
|
2010-08-16 11:01:12 -04:00
|
|
|
($self->top_row - $self->view_start) * 100 / $self->top_row);
|
|
|
|
}
|
2010-08-18 05:11:37 -04:00
|
|
|
|
|
|
|
$stat_len = length($stat);
|
2010-08-16 11:01:12 -04:00
|
|
|
|
2010-08-18 05:11:37 -04:00
|
|
|
if (!$self->{overlay} || $self->{overlay_len} != $stat_len) {
|
2010-08-15 18:43:48 -04:00
|
|
|
delete $self->{overlay} if $self->{overlay};
|
2010-08-18 05:11:37 -04:00
|
|
|
$self->{overlay} = $self->overlay(-1, -1, $stat_len, 1,
|
2010-08-15 18:43:48 -04:00
|
|
|
urxvt::OVERLAY_RSTYLE, 0);
|
2010-08-18 05:11:37 -04:00
|
|
|
$self->{overlay_len} = $stat_len;
|
2010-08-15 18:43:48 -04:00
|
|
|
}
|
|
|
|
|
2010-08-18 05:11:37 -04:00
|
|
|
$self->{overlay}->set(0, 0, $self->special_encode($stat));
|
2010-08-15 18:43:48 -04:00
|
|
|
$self->{overlay}->show();
|
|
|
|
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-13 14:18:41 -04:00
|
|
|
sub toggle_select {
|
2010-08-12 20:31:08 -04:00
|
|
|
my ($self, $mode) = @_;
|
|
|
|
|
2010-08-18 18:54:56 -04:00
|
|
|
if ($self->{select} eq $mode) {
|
|
|
|
$self->{select} = '';
|
|
|
|
} else {
|
|
|
|
if (not $self->{select}) {
|
|
|
|
($self->{ar}, $self->{ac}) = $self->screen_cur();
|
2010-08-12 20:31:08 -04:00
|
|
|
}
|
2010-08-18 18:54:56 -04:00
|
|
|
$self->{select} = $mode;
|
2010-08-12 20:31:08 -04:00
|
|
|
}
|
|
|
|
|
2010-08-18 18:54:56 -04:00
|
|
|
status_area($self);
|
|
|
|
$self->want_refresh();
|
|
|
|
|
2010-08-12 20:31:08 -04:00
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub calc_span {
|
2010-08-13 14:18:41 -04:00
|
|
|
my ($self) = @_;
|
2010-08-18 05:11:37 -04:00
|
|
|
my ($cr, $cc) = $self->screen_cur();
|
2010-08-12 20:31:08 -04:00
|
|
|
my ($br, $bc, $er, $ec);
|
|
|
|
|
2010-08-18 12:48:19 -04:00
|
|
|
if ($self->{select} eq 'b') {
|
|
|
|
$br = $self->line($cr)->beg;
|
|
|
|
$bc = $self->line($cr)->offset_of($cr, $cc);
|
|
|
|
$er = $self->line($self->{ar})->beg;
|
|
|
|
$ec = $self->line($self->{ar})->offset_of($self->{ar}, $self->{ac});
|
|
|
|
($br, $er) = ($er, $br) if $br > $er;
|
|
|
|
($bc, $ec) = ($ec, $bc) if $bc > $ec;
|
2010-08-12 20:31:08 -04:00
|
|
|
} else {
|
2010-08-18 12:48:19 -04:00
|
|
|
if ($cr < $self->{ar}) {
|
|
|
|
($br, $bc, $er, $ec) = ($cr, $cc, $self->{ar}, $self->{ac});
|
|
|
|
} elsif ($cr > $self->{ar}) {
|
|
|
|
($br, $bc, $er, $ec) = ($self->{ar}, $self->{ac}, $cr, $cc);
|
|
|
|
} else {
|
|
|
|
($br, $er) = ($cr, $cr);
|
|
|
|
($bc, $ec) = $cc < $self->{ac} ? ($cc, $self->{ac}) : ($self->{ac}, $cc);
|
|
|
|
}
|
2010-08-12 20:31:08 -04:00
|
|
|
}
|
|
|
|
|
2010-08-13 14:18:41 -04:00
|
|
|
if ($self->{select} eq 'l') {
|
2010-08-18 05:11:37 -04:00
|
|
|
($br, $er) = ($self->line($br)->beg, $self->line($er)->end);
|
|
|
|
($bc, $ec) = (0, $self->ncol);
|
2010-08-12 20:31:08 -04:00
|
|
|
} else {
|
2010-08-13 05:05:37 -04:00
|
|
|
++$ec;
|
2010-08-12 20:31:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ($br, $bc, $er, $ec);
|
|
|
|
}
|