procedure capture_on;
begin
  check_if_in_help;
  prompt_color;
  get_file_name(capture_file_name,
                20,aux_line,'Capture',1,1,80,24,3);
  if (capture_file_name = '')
  then
    begin
      gotoxy(20,aux_line); aux_color; ClrEol;
    end
  else
  begin
    assign(capture_file,capture_file_name);
    {$I-}
    rewrite(capture_file);
    if (IOresult <> 0)
    then begin
           gotoxy(20,aux_line); status_color;
           write('Unable to open ',capture_file_name,^G^G^G);
           delay(2000);
           gotoxy(20,aux_line); aux_color; ClrEol;
           close(capture_file);
         end;
    capture := TRUE;
    sho_status;
    capture_pointer := 0;
  end;
end;

procedure write_capture_block;
var write_result : integer;
begin
  {$I-}
  blockwrite(capture_file,capture_buffer,16,write_result);
  capture_pointer := 0;
  if write_result = 0 then
  begin
    close(capture_file);
    gotoxy(20,aux_line);status_color;
    write('File Write Error !!!');
    delay(2000);
    gotoxy(20,aux_line); aux_color; ClrEol;
    capture := FALSE;
  end;
end;

procedure capture_off;
begin
  capture_buffer[capture_pointer] := ^Z;
  write_capture_block;
  close(capture_file);
  capture := FALSE;
  gotoxy(20,aux_line); aux_color; ClrEol;
  sho_status;
end;

procedure capture_char(c : char);
begin
  capture_buffer[capture_pointer] := c;
  capture_pointer := capture_pointer + 1;
  if capture_pointer > 2047 then
    write_capture_block;
end;

procedure update_buffer(c : char);
begin
  rcv_buffer[rcv_cnt] := c;
  rcv_cnt := (rcv_cnt + 1) AND $7FF;
end;

procedure show_inp(st: char);
var n,p: integer;
begin
  if st = #00 then exit;
  window(1, inp_start_line, 80, inp_end_line );
  receive_color;
  if (xin > scrn_width) OR (st = #13) then
  begin
    n := 0;
    if (st in ['!'..'z']) then
    begin
      p := rcv_cnt;
      repeat
        n := n + 1;
        p := (p-1) AND $7FF;
      until (rcv_buffer[p] = ' ');
      p := p + 1;
      n := n - 1;
      if (xin > scrn_width) AND (yin = rcv_bottom)
      then begin
             gotoxy(scrn_width - n, yin-1);
             ClrEol;
           end
      else begin
             gotoxy(scrn_width - n, yin);
             ClrEol;
           end;
    end;
    xin := 1;
    yin := yin + 1;
    if (yin > rcv_bottom) then
    begin
      yin := rcv_bottom;
      if (st = #13) then writeln;
    end;
    gotoxy(xin,yin);
    while (n > 0) do
    begin
      write(rcv_buffer[p]);
      p := (p+1) AND $7FF;
      n := n - 1;
      xin := xin + 1;
    end;
    if (capture = TRUE) then
    begin
      capture_char(#13);
      capture_char(#10);
    end;
    update_buffer(#13);
    update_buffer(#10);
  end;
  gotoxy(xin,yin);
  if (NOT (st in [#10,#13])) then
  begin
    write(st);
    if (capture = TRUE) then capture_char(st);
  end;
  if (st in [' '..'z']) then
  begin
    xin := xin + 1;
    update_buffer(st);
  end;
  reset_cursor;
  if (st = #32) AND (mode = CW)
  then begin
         rcv_stat;
         disp_rcv_wpm;
       end;
end;

procedure rcvg;
begin
  if char_ready then show_inp(kam_in);
end;

