+ $pos = $lth = 0;
+ } elsif ($r eq KEY_UP || $r eq "\020") {
+ if ($khistpos > 0) {
+ --$khistpos;
+ $inbuf = $khistory[$khistpos];
+ $pos = $lth = length $inbuf;
+ } else {
+ beep();
+ }
+ } elsif ($r eq KEY_DOWN || $r eq "\016") {
+ if ($khistpos < @khistory - 1) {
+ ++$khistpos;
+ $inbuf = $khistory[$khistpos];
+ $pos = $lth = length $inbuf;
+ } else {
+ beep();
+ }
+ } elsif ($r eq KEY_PPAGE || $r eq "\032") {
+ if ($spos > 0) {
+ my ($i, $l);
+ for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
+ $l = measure($shistory[$spos]);
+ $i += $l;
+ $spos-- if $i <= $pagel;
+ }
+ $spos = 0 if $spos < 0;
+ show_screen();
+ } else {
+ beep();
+ }
+ } elsif ($r eq KEY_NPAGE || $r eq "\026") {
+ if ($spos < @shistory - 1) {
+ my ($i, $l);
+ for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
+ $l = measure($shistory[$spos]);
+ $i += $l;
+ $spos++ if $i <= $pagel;
+ }
+ $spos = @shistory if $spos >= @shistory - 1;
+ show_screen();
+ } 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") {
+ do_resize();
+ return;
+ } elsif (defined $r && is_pctext($r)) {
+ # move the top screen back to the bottom if you type something
+ if ($spos < @shistory) {
+ $spos = @shistory;
+ 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++;
+ } elsif ($r eq "\014" || $r eq "\022") {
+ touchwin(curscr, 1);
+ refresh(curscr);
+ return;
+ } elsif ($r eq "\013") {
+ $inbuf = substr($inbuf, 0, $pos);
+ $lth = length $inbuf;