# simple editor text f; const string word_break:=" ,"; proc open_file is string name; bool done; repeat putline( "file:" ); @getline(name); done := f.open_rw( name ); if not done then putline("file not found\n"); end if; until done; end proc; func char get_command(ref int low, high) is string line; string command, arg1, arg2; low := 0; high := 0; repeat putline("* "); @getline( line ); until line <> ""; command := line.gettok(word_break); arg1 := line.gettok(word_break); if arg1 <> "" then arg2 := line.gettok(word_break); low := arg1.to_int; end if; if arg2<> "" then high := arg2.to_int; end if; return command[1]; end func; proc fix_parms(ref int low, high) is if low <= 0 then if high == 0 then high := f.lines; end if; low := 1; end if; if high == 0 then high := low; end if; if high > f.lines then high := f.lines; end if; end proc; proc list( int low, high) is fix_parms(low, high); for int i:=upto(low, high) do putline( i.to_string + ": "+ f[i] + "\n" ); end for; end proc; proc do_insert(int where; string x) is if where > f.lines then f.append(x); else f.insert(where, x); end if; end proc; proc ins(int low) is string s, line; if low == 0 then putline("missing parameter\n"); return; end if; repeat putline("> "); @getline( line ); if line <> "." then do_insert(low, line); low := low + 1; end if; until line == "."; end proc; proc del(int low, high) is if low == 0 and high ==0 then putline("missing parameters\n"); return; end if; if low < 0 or low >f.lines then putline("invalid parameters\n"); end if; if high == 0 then high := low; end if; for int i:=upto(low, high) do f.delete(low); end for; end proc; proc replace(int low, high) is string line; if low <= 0 or low > f.lines then putline("Missing or illegal paramater\n"); return; end if; if high == 0 then high := low; end if; repeat putline("> "); @getline(line); if line <> "." then if low > f.lines then f.append(line); else if low > high then f.insert(low, line); else f[low] := line; end if; end if; end if; low := low + 1; until line == "."; end proc; proc join( int low, high) is if low <= 0 or low > f.lines or high <= 0 or high > f.lines then putline("illegal or invalid parameter\n"); return; end if; f.join(low, high); end proc; proc main is char action; int low, high; open_file; repeat action := get_command( low, high ); if action == 'l' then list(low, high); elsif action == 'd' then del(low, high); elsif action == 'i' then ins(low); elsif action == 'r' then replace(low, high); elsif action == 'j' then join(low, high); elsif action == '?' then putline(" commands:\n"); putline(" d low [high] - delete lines low [- high]\n"); putline(" i where - insert at line. end with .\n"); putline(" j low high - join lines low and high\n"); putline(" l [low] [high]\n"); putline(" r low [high] - replace low [to high]. end with .\n"); elsif action <> 'q' then putline("Illegal command. Type ? for help\n"); end if; until action == 'q'; if not f.close then putline("i/o error. Changes may be inconsistent\n"); end if; end proc;