function post:boolean;
VAR G:INTEGER;

procedure down;forward;
PROCEDURE update;
var c:string[3];
    s:byte;
    tt:byte;
begin

if msgm[6].used then begin
                     {updates currentline}
                     s:=textattr;
                     savexy;
                     goxy(msgm[6].x,msgm[6].y);
                     if currentline in [1..9] then c:='00'+strr(currentline) else
                     if currentline in [10..99] then c:='0'+strr(currentline) else
                     if currentline in [100..255] then c:=strr(currentline);
                     ansicolor(msgm[6].Back shl 4 + msgm[6].Fore);
                     swrite(c);
                     goxy(Savedx,savedy);
                     ansicolor(s);
                     end;
if msgm[7].used then begin
                     {updates X pos}
                     if xpos=81 then begin
                                     xpos:=1;
                                     down;
                                     end;
                     s:=textattr;
                     savexy;
                     goxy(msgm[7].x,msgm[7].y);
                     c:='00';
                     if xpos in [1..9] then c:='0'+strr(xpos) else
                     if xpos in [10..80] then c:=strr(xpos);
                     ansicolor(msgm[7].Back shl 4 + msgm[7].Fore);
                     swrite(c);
                     goxy(Savedx,savedy);
                     ansicolor(s);
                     end;
if msgm[8].used then begin
                     {updates insert}
                     s:=textattr;
                     savexy;
                     goxy(msgm[9].x,msgm[9].y);
                     ansicolor(msgm[9].Back shl 4 + msgm[9].Fore);
                     swrite(yn(insert));
                     goxy(Savedx,savedy);
                     ansicolor(s);
                     end;
end;

procedure killchar(t:boolean);
var s:string;
    y,G:INTEGER;

begin
if (currentline=1) and (xpos=0) then exit;
if t=true then dec(xpos);
if xpos=0 then begin
               if currentline=1 then exit;
               dec(currentline);
               messx:=80;
               dec(messy);
               move(currentline,s,80);
               xpos:=findlast(currentline);
               messx:=xpos;
               m_buff^[currentline][xpos]{.c}:=#0;
               goxy(messx,messy);
               Swrite(' '+#8);
               goxy(messx,messy);
               xpos:=findlast(currentline)+1;
               update;
               exit;
               end;
               messx:=xpos;
               m_buff^[currentline][xpos]{.c}:=#0;
               messx:=xpos;
               goxy(messx,messy);
               Swrite(' '+#8);
               FOR G:=XPOS TO 79 DO m_buff^[CURRENTLINE][G]:=m_buff^[CURRENTLINE][G+1];
               GOXY(1,MESSY);
               m_buff^[CURRENTLINE][80]{.C}:=#0;
               goxy(1,messy);
               for y:=1 to 80 do Swrite(m_buff^[CURRENTLINE][y]{.c});
               swrite(#27+'[K');
               goxy(messx,messy);
               update;
               end;

procedure toggleinsert;
begin
if insert=true then insert:=false else insert:=true;
update;
end;

procedure down;
var i:byte;
begin
if currentline=255 then exit;
if messy=msgm[5].y then begin
                        clearbox;
                        savexy;
                        goxy(1,msgm[4].y);
                        display(currentline-BOT,currentline-1);
                        inc(currentline);
                        goxy(1,msgm[5].y);
                        xpos:=1;
                        end else
                        begin
                        messx:=1;
                        inc(messy);
                        inc(currentline);
                        goxy(1,messy);
                        xpos:=1;
                        end;
end;
procedure up;
var i:byte;
begin
if currentline=1 then exit;
if messy=msgm[4].y then begin
                        clearbox;
                        savexy;
                        goxy(1,msgm[4].y);
                        display(currentline-1,currentline+BOT);
                        dec(currentline);
                        goxy(1,msgm[4].y);
                        xpos:=1;
                        end else
                        begin
                        dec(messy);
                        dec(currentline);
                        goxy(1,messy);
                        xpos:=1;
                        end;
                        UPDATE;
end;

procedure left;
begin
if (currentline=1) and (xpos=1) then exit;
dec(xpos);
if xpos=0 then begin
               if messy=msgm[4].y then
                 begin
                 up;
                 messx:=1;
                 xpos:=findlast(currentline)+1;
                 messx:=xpos;
                 goxy(messx,messy);
                 exit;
                 end;
               dec(currentline);
               dec(messy);
               messx:=1;
               xpos:=findlast(currentline)+1;
               goxy(xpos,messy);
               end
                 else
                   begin
                   messx:=xpos;
                   goxy(messx,messy);
                   end;
                    UPDATE;
               end;

procedure right;
begin
if (currentline=255) and (xpos=79) then exit;
inc(xpos);
if xpos{=80}>=findlast(currentline)+2{} then begin
               if messy=msgm[5].y then
                 begin
                 down;
                 update;
                 messx:=1;
                 exit;
                 end;
               inc(currentline);
               messx:=1;
               inc(messy);
               move(currentline,s,80);
               xpos:=1;
               goxy(xpos,messy);
               end
                 else
                   begin
                   messx:=xpos;
                   goxy(xpos,messy);
                   end;
                    UPDATE;
               end;

PROCEDURE COMMAND;
VAR G:CHAR;
    ss:byte;
    quote:String[80];
    st,sp:integer;

BEGIN
savexy;

IF XPOS>1 THEN BEGIN;
               m_buff^[CURRENTLINE][XPOS]:='/';
               SWRITE('/');
               INC(XPOS);
               UPDATE;
               EXIT;
               END;
{repeat}
SAVEXY;
GOXY(MSGM[8].X,MSGM[8].Y);
G:=SREADKEY;

case upcase(g) of

'S':begin
      currentndx.usernote:=user.usernote;
      currentndx.datetime:=date;
      currentndx.numlines:=totallines;
      write_body(currentbase.datafile,currentndx);
      Write_Ndx (currentbase.datafile,currentndx);
      messagedone:=true;
      post:=true;
    end;

'Q':begin
    read_messages(false);

    end;

'A':begin
    messagedone:=true;
    post:=false;
    end;

#27: goxy(savedx,savedy);


END;
{until (upcase(g) in ['S',#27,'A','Q']) OR ((Not Carrier) AND (Not Localon)) or (hungup) or (loggedoff);;}
END;

var count:byte;
    C2:CHAR;
    y:integer;
begin
bot:=(msgM[5].y-msgM[4].y)-1;
messx:=msgm[4].x;
messy:=msgm[4].y;
goxy(messx,messy);
currentline:=1;
s:='';
xpos:=1;
DISPLAY(currentline,currentline+25);
GOXY(MSGM[4].X,MSGM[4].Y);
repeat

    REPEAT
    UNTIL (DATAAVAILABLE) or (keypressed) OR ((Not Carrier) AND (Not Localon)) or (hungup) or (loggedoff);;

    IF DATAAVAILABLE THEN c:=CHR(GETCHAR) ELSE

    IF KEYPRESSED THEN C:=READKEY;
    CASE C OF

    #0 : BEGIN
            C2:=READKEY;
            CASE C2 OF
             #72:up;
             #80:begin; down; update; end;
             #75:left;
             #77:right;
             #83:KILLCHAR(false);
         END;
         END;
     ^V:toggleinsert;
    #13:begin; down; update; end;
     #8:KILLCHAR(TRUE);
    '/':command;
    #27: BEGIN
           C:=SREADKEY;
           IF C='[' THEN C:=SREADKEY;
           if c='A' then up;
           if c='B' then begin; down; update; end;
           if c='C' then right;
           If c='D' then left;
           END;
        else

        if (currentline<256) then
           begin

        if insert then
             begin
             move(m_buff^[currentline][xpos],m_buff^[currentline][xpos+1],80-xpos);
             savexy;
             goxy(1,messy);
             for y:=1 to 80 do Swrite(m_buff^[currentline][y]{.c});
             goxy(savedx,savedy);
           end;

     Swrite(C);
     m_buff^[currentline][xpos]:=c;
     inc(xpos);
     update;
     end;
end;

until (messageDONE) OR ((Not Carrier) AND (Not Localon)) or (hungup) or (loggedoff);;
end;
