2010-08-12 20:31:08 -04:00
|
|
|
#! perl -w
|
|
|
|
# Author: Bert Muennich
|
|
|
|
# Website: http://www.github.com/muennich/urxvt-perls
|
2010-08-16 14:10:52 -04:00
|
|
|
# Version: 1.0
|
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-14 10:47:53 -04:00
|
|
|
# h/j/k/l: move cursor left/down/up/right (also with arrow keys)
|
2010-08-16 14:10:52 -04:00
|
|
|
# g/G/0/^/$/H/M/L: more vi-like cursor movement keys
|
|
|
|
# Ctrl-f/b: move cursor down/up one screen
|
|
|
|
# Ctrl-d/u: move cursor down/up half a screen
|
2010-08-14 10:47:53 -04:00
|
|
|
# v/V/Ctrl-v: toggle normal/linewise/blockwise selection
|
|
|
|
# y,Return: copy selected text to primary buffer and quit selection mode
|
2010-08-16 14:10:52 -04:00
|
|
|
# Escape: cancel 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 {
|
|
|
|
my ($self, $event, $keysym) = @_;
|
|
|
|
my $char = chr($keysym);
|
|
|
|
|
|
|
|
if ($keysym == 0xff1b) {
|
|
|
|
# escape
|
|
|
|
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}) {
|
|
|
|
my ($br, $bc, $er, $ec) = calc_span($self);
|
2010-08-12 20:31:08 -04:00
|
|
|
$self->selection_beg($br, $bc);
|
|
|
|
$self->selection_end($er, $ec);
|
2010-08-13 14:18:41 -04:00
|
|
|
$self->selection_make($event->{time}, $self->{select} eq 'b');
|
2010-08-12 20:31:08 -04:00
|
|
|
deactivate($self);
|
|
|
|
}
|
|
|
|
} 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-15 14:03:21 -04:00
|
|
|
} elsif ('fbdu' =~ m/\Q$char\E/ && $event->{state} & urxvt::ControlMask) {
|
|
|
|
move_cursor($self, $char);
|
2010-08-15 10:22:21 -04:00
|
|
|
} elsif ('gG0^$HML' =~ m/\Q$char\E/) {
|
2010-08-14 10:47:53 -04:00
|
|
|
move_cursor($self, $char);
|
2010-08-12 20:31:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub tt_write {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub refresh {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
2010-08-13 14:18:41 -04:00
|
|
|
if ($self->{select}) {
|
2010-08-12 20:31:08 -04:00
|
|
|
my ($br, $bc, $er, $ec) = calc_span($self);
|
|
|
|
|
2010-08-13 14:18:41 -04:00
|
|
|
if ($self->{select} eq 'b') {
|
2010-08-12 20:31:08 -04:00
|
|
|
$self->scr_xor_rect($br, $bc, $er, $ec, urxvt::RS_RVid);
|
|
|
|
} else {
|
|
|
|
$self->scr_xor_span($br, $bc, $er, $ec, urxvt::RS_RVid);
|
2010-08-13 14:18:41 -04:00
|
|
|
}
|
2010-08-13 05:05:37 -04:00
|
|
|
|
2010-08-13 14:18:41 -04:00
|
|
|
if ($self->{select} ne 'l') {
|
|
|
|
# make the cursor visible again
|
|
|
|
$self->scr_xor_span($self->{cr}, $self->{cc},
|
|
|
|
$self->{cr}, $self->{cc} + 1, urxvt::RS_RVid);
|
2010-08-12 20:31:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub move_cursor {
|
2010-08-14 10:47:53 -04:00
|
|
|
my ($self, $key) = @_;
|
2010-08-12 20:31:08 -04:00
|
|
|
|
|
|
|
if ($self->{active}) {
|
2010-08-14 10:47:53 -04:00
|
|
|
if ($key eq 'k' && $self->{cr} > $self->top_row) {
|
|
|
|
--$self->{cr};
|
|
|
|
} elsif ($key eq 'j' && $self->{cr} < $self->nrow - 1) {
|
|
|
|
++$self->{cr};
|
|
|
|
} elsif ($key eq 'h' && $self->{cc} > 0) {
|
|
|
|
--$self->{cc};
|
|
|
|
} elsif ($key eq 'l' && $self->{cc} < $self->ncol - 1) {
|
|
|
|
++$self->{cc};
|
2010-08-15 14:03:21 -04:00
|
|
|
} elsif ($key eq 'f' || $key eq 'd') {
|
|
|
|
$self->{cr} += $key eq 'd' ? ($self->nrow - 1) / 2 : $self->nrow - 1;
|
|
|
|
$self->{cr} = $self->nrow - 1 if $self->{cr} > $self->nrow;
|
|
|
|
} elsif ($key eq 'b' || $key eq 'u') {
|
|
|
|
$self->{cr} -= $key eq 'u' ? ($self->nrow - 1) / 2 : $self->nrow - 1;
|
|
|
|
$self->{cr} = $self->top_row if $self->{cr} < $self->top_row;
|
2010-08-14 10:47:53 -04:00
|
|
|
} elsif ($key eq 'g') {
|
|
|
|
($self->{cr}, $self->{cc}) = ($self->top_row, 0);
|
|
|
|
} elsif ($key eq 'G') {
|
|
|
|
($self->{cr}, $self->{cc}) = ($self->nrow - 1, 0);
|
|
|
|
} elsif ($key eq '0') {
|
|
|
|
$self->{cc} = 0;
|
|
|
|
} elsif ($key eq '^') {
|
2010-08-15 10:22:21 -04:00
|
|
|
my $ltxt = $self->special_decode($self->line($self->{cr})->t);
|
|
|
|
while ($ltxt =~ s/^( *)\t/$1 . " " x (8 - length($1) % 8)/e) {}
|
|
|
|
$self->{cc} = $ltxt =~ m/^ +/ ? $+[0] : 0;
|
2010-08-14 10:47:53 -04:00
|
|
|
} elsif ($key eq '$') {
|
|
|
|
my $ltxt = $self->line($self->{cr})->t;
|
|
|
|
$self->{cc} = ($ltxt =~ m/$/ ? $-[0] : $self->ncol) - 1;
|
|
|
|
} elsif ($key eq 'H') {
|
|
|
|
$self->{cr} = $self->view_start();
|
|
|
|
} elsif ($key eq 'M') {
|
|
|
|
$self->{cr} = $self->view_start() + $self->nrow / 2;
|
|
|
|
} elsif ($key eq 'L') {
|
|
|
|
$self->{cr} = $self->view_start() + $self->nrow - 1;
|
2010-08-12 20:31:08 -04:00
|
|
|
}
|
|
|
|
|
2010-08-14 10:47:53 -04:00
|
|
|
$self->screen_cur($self->{cr}, $self->{cc});
|
|
|
|
|
2010-08-13 14:18:41 -04:00
|
|
|
# scroll the current cursor position into visible area
|
2010-08-12 20:31:08 -04:00
|
|
|
if ($self->{cr} < $self->view_start()) {
|
|
|
|
$self->view_start($self->{cr});
|
|
|
|
} elsif ($self->{cr} >= $self->view_start() + $self->nrow) {
|
|
|
|
$self->view_start($self->{cr} - $self->nrow + 1);
|
|
|
|
}
|
|
|
|
|
2010-08-15 18:43:48 -04:00
|
|
|
status_area($self);
|
2010-08-12 20:31:08 -04:00
|
|
|
$self->want_refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub activate {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
$self->{active} = 1;
|
2010-08-16 14:10:52 -04:00
|
|
|
|
2010-08-13 14:18:41 -04:00
|
|
|
$self->{select} = "";
|
2010-08-15 18:43:48 -04:00
|
|
|
$self->{overlay_len} = 0;
|
2010-08-12 20:31:08 -04:00
|
|
|
|
|
|
|
($self->{cr}, $self->{cc}) = $self->screen_cur();
|
|
|
|
$self->{oldcr} = $self->{cr};
|
|
|
|
$self->{oldcc} = $self->{cc};
|
|
|
|
|
|
|
|
$self->selection_beg(1, 0);
|
|
|
|
$self->selection_end(1, 0);
|
|
|
|
|
|
|
|
$self->{view_start} = $self->view_start();
|
|
|
|
$self->{pty_ev_events} = $self->pty_ev_events(urxvt::EV_NONE);
|
|
|
|
|
|
|
|
$self->enable(
|
|
|
|
key_press => \&key_press,
|
|
|
|
refresh_begin => \&refresh,
|
|
|
|
refresh_end => \&refresh,
|
|
|
|
tt_write => \&tt_write,
|
|
|
|
);
|
|
|
|
|
2010-08-15 18:43:48 -04:00
|
|
|
status_area($self);
|
|
|
|
|
2010-08-12 20:31:08 -04:00
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub deactivate {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
$self->screen_cur($self->{oldcr}, $self->{oldcc});
|
|
|
|
|
|
|
|
$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-12 20:31:08 -04:00
|
|
|
$self->disable("key_press", "refresh_begin", "refresh_end", "tt_write");
|
|
|
|
$self->view_start($self->{view_start});
|
|
|
|
$self->pty_ev_events($self->{pty_ev_events});
|
|
|
|
|
|
|
|
$self->want_refresh();
|
|
|
|
|
|
|
|
$self->{active} = 0;
|
|
|
|
|
|
|
|
()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-15 18:43:48 -04:00
|
|
|
sub status_area {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my $row = $self->{cr} < 0 ?
|
|
|
|
$self->{cr} - $self->top_row : abs($self->top_row) + $self->{cr};
|
2010-08-16 11:01:12 -04:00
|
|
|
my $text = sprintf("%d,%d ", $row + 1, $self->{cc} + 1);
|
2010-08-15 18:43:48 -04:00
|
|
|
|
|
|
|
if ($self->{select}) {
|
|
|
|
my ($br, $bc, $er, $ec) = calc_span($self);
|
2010-08-16 11:01:12 -04:00
|
|
|
if ($self->{select} eq 'b') {
|
|
|
|
$text .= sprintf("%dx%d ", $er - $br + 1, $ec - $bc);
|
2010-08-15 18:43:48 -04:00
|
|
|
} else {
|
2010-08-16 11:01:12 -04:00
|
|
|
if ($br == $er) {
|
|
|
|
$text .= sprintf("%d ", $ec - $bc);
|
|
|
|
} else {
|
|
|
|
$text .= sprintf("%d ", $er - $br + 1);
|
|
|
|
}
|
2010-08-15 18:43:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-16 11:01:12 -04:00
|
|
|
if ($self->top_row == 0) {
|
|
|
|
$text .= "All";
|
|
|
|
} elsif ($self->view_start() == $self->top_row) {
|
|
|
|
$text .= "Top";
|
|
|
|
} elsif ($self->view_start() == 0) {
|
|
|
|
$text .= "Bot";
|
|
|
|
} else {
|
|
|
|
$text .= sprintf("%2d%",
|
|
|
|
($self->top_row - $self->view_start) * 100 / $self->top_row);
|
|
|
|
}
|
|
|
|
|
2010-08-15 18:43:48 -04:00
|
|
|
my $text_len = length($text);
|
|
|
|
|
|
|
|
if ($self->{overlay_len} != $text_len) {
|
|
|
|
delete $self->{overlay} if $self->{overlay};
|
|
|
|
$self->{overlay} = $self->overlay(-1, -1, $text_len, 1,
|
|
|
|
urxvt::OVERLAY_RSTYLE, 0);
|
|
|
|
$self->{overlay_len} = $text_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{overlay}->set(0, 0, $self->special_encode($text));
|
|
|
|
$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) = @_;
|
|
|
|
|
|
|
|
if ($self->{active}) {
|
2010-08-13 14:18:41 -04:00
|
|
|
if ($self->{select} eq $mode) {
|
|
|
|
$self->{select} = '';
|
2010-08-12 20:31:08 -04:00
|
|
|
} else {
|
2010-08-13 14:18:41 -04:00
|
|
|
if (not $self->{select}) {
|
|
|
|
$self->{ar} = $self->{cr};
|
|
|
|
$self->{ac} = $self->{cc};
|
|
|
|
}
|
|
|
|
$self->{select} = $mode;
|
2010-08-12 20:31:08 -04:00
|
|
|
}
|
|
|
|
|
2010-08-15 18:43:48 -04:00
|
|
|
status_area($self);
|
2010-08-13 14:18:41 -04:00
|
|
|
$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-12 20:31:08 -04:00
|
|
|
my ($br, $bc, $er, $ec);
|
|
|
|
|
|
|
|
if ($self->{cr} < $self->{ar}) {
|
|
|
|
$br = $self->{cr};
|
|
|
|
$bc = $self->{cc};
|
|
|
|
$er = $self->{ar};
|
|
|
|
$ec = $self->{ac};
|
|
|
|
} elsif ($self->{cr} > $self->{ar}) {
|
|
|
|
$br = $self->{ar};
|
|
|
|
$bc = $self->{ac};
|
|
|
|
$er = $self->{cr};
|
|
|
|
$ec = $self->{cc};
|
|
|
|
} else {
|
|
|
|
$br = $self->{cr};
|
|
|
|
$bc = $self->{cc} < $self->{ac} ? $self->{cc} : $self->{ac};
|
|
|
|
$er = $self->{cr};
|
|
|
|
$ec = $self->{cc} > $self->{ac} ? $self->{cc} : $self->{ac};
|
|
|
|
}
|
|
|
|
|
2010-08-13 14:18:41 -04:00
|
|
|
if ($self->{select} eq 'l') {
|
2010-08-12 20:31:08 -04:00
|
|
|
$bc = 0;
|
|
|
|
$ec = $self->ncol;
|
|
|
|
} else {
|
2010-08-13 14:18:41 -04:00
|
|
|
if ($self->{select} eq 'b') {
|
|
|
|
($br, $er) = ($er, $br) if $br > $er;
|
|
|
|
($bc, $ec) = ($ec, $bc) if $bc > $ec;
|
|
|
|
}
|
2010-08-13 05:05:37 -04:00
|
|
|
++$ec;
|
2010-08-12 20:31:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ($br, $bc, $er, $ec);
|
|
|
|
}
|