+ $pos = $lth = 0;
+ } elsif ($r eq KEY_UP || $r eq "\020") {
+ if ($kpos > 0) {
+ --$kpos;
+ $inbuf = $kh[$kpos];
+ $pos = $lth = length $inbuf;
+ } else {
+ beep();
+ }
+ } elsif ($r eq KEY_DOWN || $r eq "\016") {
+ if ($kpos < @kh - 1) {
+ ++$kpos;
+ $inbuf = $kh[$kpos];
+ $pos = $lth = length $inbuf;
+ } else {
+ beep();
+ }
+ } elsif ($r eq KEY_PPAGE || $r eq "\032") {
+ if ($spos > 0 && @sh > $pagel) {
+# my ($i, $l);
+# for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
+# $l = measure($sh[$spos]);
+# $i += $l;
+# --$spos if $i <= $pagel;
+# }
+ $spos -= $pagel+int($pagel/2);
+ $spos = 0 if $spos < 0;
+ $inscroll = 1;
+ show_screen();
+ } else {
+ beep();
+ }
+ } elsif ($r eq KEY_NPAGE || $r eq "\026") {
+ if ($inscroll && $spos < @sh) {
+# my ($i, $l);
+# for ($i = 0; $i <= $pagel && $spos < @sh; ) {
+# $l = measure($sh[$spos]);
+# $i += $l;
+# ++$spos if $i <= $pagel && $spos < @sh;
+# }
+
+ dbg("NPAGE sp:$spos $sh:". scalar @sh . " pl: $pagel") if isdbg('console');
+ $spos += int($pagel/2);
+ if ($spos > @sh - $pagel) {
+ $spos = @sh - $pagel;
+ }
+ show_screen();
+ if ($spos >= @sh) {
+ $spos = @sh;
+ $inscroll = 0;
+ }
+ } else {
+ beep();
+ }
+ } elsif ($r eq KEY_LEFT || $r eq "\002") {
+ if ($pos > 0) {
+ --$pos;
+ } else {
+ beep();
+ }
+ } elsif ($r eq KEY_RIGHT || $r eq "\006") {
+ if ($pos < $lth) {
+ ++$pos;
+ } else {
+ beep();
+ }
+ } elsif ($r eq KEY_HOME || $r eq "\001") {
+ $pos = 0;
+ } elsif ($r eq KEY_END || $r eq "\005") {
+ $pos = $lth;
+ } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
+ if ($pos > 0) {
+ my $a = substr($inbuf, 0, $pos-1);
+ my $b = substr($inbuf, $pos) if $pos < $lth;
+ $b = "" unless $b;
+
+ $inbuf = $a . $b;
+ --$lth;
+ --$pos;
+ } else {
+ beep();
+ }
+ } elsif ($r eq KEY_DC || $r eq "\004") {
+ if ($pos < $lth) {
+ my $a = substr($inbuf, 0, $pos);
+ my $b = substr($inbuf, $pos+1) if $pos < $lth;
+ $b = "" unless $b;
+
+ $inbuf = $a . $b;
+ --$lth;
+ } else {
+ beep();
+ }
+ } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
+ doresize();
+ return;
+ } elsif ($r eq "\x12" || $r eq "\x0c") {
+ dbg("REDRAW called") if isdbg('console');
+ doresize();
+ return;
+ } elsif ($r eq "\013") {
+ $inbuf = substr($inbuf, 0, $pos);
+ $lth = length $inbuf;
+ } elsif (defined $r && is_pctext($r)) {
+ # move the top screen back to the bottom if you type something
+
+ if ($inscroll && $spos < @sh) {
+ $spos = @sh - $pagel;
+ $inscroll = 0;
+ show_screen();
+ }
+
+ # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
+
+ # insert the character into the keyboard buffer
+ if ($pos < $lth) {
+ my $a = substr($inbuf, 0, $pos);
+ my $b = substr($inbuf, $pos);
+ $inbuf = $a . $r . $b;
+ } else {
+ $inbuf .= $r;
+ }
+ $pos++;
+ $lth++;