                  { --- Text alteration subroutines --- }

procedure insert_char(ch:char);   { inserts character at position index }
var i,j:integer;
begin
if text_length<TMAX then
    begin
    for i:=text_length downto index do text[i+1]:=text[i];
    text[index]:=ch;
    text_length:=text_length+1;
    if block_flag then
        begin
        if index<block_begin then block_begin:=block_begin+1;
        if index<block_end then block_end:=block_end+1;
        end;
    if ch=CR then
        begin
        n_lines:=n_lines+1;
        find_page_bottom_line;
        find_page_bottom_index;
        display_rest_page(index,cx,cy);
        end
    else
        begin
        if index<page_bottom_index then page_bottom_index:=page_bottom_index+1;
        display_rest_line(index,cx,cy);
    end;
    cursor_right;
    end;
end; { insert_char }

procedure delete_chars(n:integer);  { deletes n characters at position index }
var i,m,new_length,cr_count:integer;
begin
m:=min(n,text_length-index);
if (m>0) then
    begin
    cr_count:=0;
    for i:=index to index+m-1 do
        if eol(i) then cr_count:=cr_count+1;
    new_length:=text_length-m;
    if index<=new_length then
        for i:=index to new_length do text[i]:=text[i+m];
    text_length:=max(index-1,new_length);
    if block_flag then
        begin
        if index<block_begin then block_begin:=block_begin-m;
        if index<=block_end then block_end:=block_end-m;
        end;
    cx:=min(cx,cd_bol(index)+1);
    if cr_count>0 then
        begin
        n_lines:=n_lines-cr_count;
        find_page_bottom_line;
        find_page_bottom_index;
        display_rest_page(index,cx,cy)
        end
    else
        begin
        if index<page_bottom_index then page_bottom_index:=page_bottom_index-m;
        display_rest_line(index,cx,cy);
        end;
    end;
end; { delete_chars }
