#! perl

use POSIX ();

my $termios = new POSIX::Termios;

sub on_init {
   my ($self) = @_;

   $self->{enabled} = 1;

   push @{ $self->{term}{option_popup_hook} }, sub {
      ("readline" => $self->{enabled}, sub { $self->{enabled} = shift })
   };

   ()
}

sub on_button_press {
   my ($self, $event) = @_;

   return
      if $self->current_screen || $self->hidden_cursor || !$self->{enabled};

   $termios->getattr ($self->pty_fd)
      or return;

   return
      if $termios->getlflag & &POSIX::ICANON;

   my ($row, $col) = $self->screen_cur;
   my $line = $self->line ($row);
   my $cur = $line->offset_of ($row, $col);
   my $ofs = $line->offset_of ($event->{row}, $event->{col});

   if ($ofs >= 0 && $ofs < $line->l) {
      my $diff = $ofs - $cur;
      my $move;

      if ($diff < 0) {
         ($ofs, $cur) = ($cur, $ofs);
         $move = "\x1b[D";
      } else {
         $move = "\x1b[C";
      }

      my $skipped = substr $line->t, $cur, $ofs - $cur;
      $skipped =~ s/\x{ffff}//g;

      $self->tt_write ($move x length $skipped);
   }

   ()
}
