#define ALT_C   174
#define CTRL_N  14

#define NO      0
#define YES     1


int reached_eof;


init()
{
  assign_key("compile",    ALT_C);
  assign_key("next_error", CTRL_N);
}

compile()
{
  string currfile;
  int    errcode, i;
  
  currfile = filename();

  writefile(currfile);

  if ((i = index(currfile, ".")) > 0)
    currfile = substr(currfile, 1, i - 1);

  errcode =
    os_command(sprintf("lc1 -ccuw -d -ml -i\lc\ %s > errs", currfile));
  if (!errcode)
    errcode = os_command(sprintf("lc2 %s", currfile));
  message(sprintf("The error code from the compile was %d", errcode));
  get_tty_char();

  next_error();
}

next_error()
{
  int id, old_id;
  int n;
  string cl, foo;

  old_id = currbuf();

  if ((id = find_buffer("errs")) == 0)
  {
    id = create_buffer("errs");
    reached_eof = NO;
  }
  id = setcurrbuf(id);

  if (reached_eof == NO && fsearch("[0-9][0-9]* Error") > 0)
  {
    n = atoi(substr(cl = currline(), currcol(), 5));
    gobol();
    if (!down())
      reached_eof = YES;
    show_buffer(old_id);
    goline(n);
    message(cl);
    get_tty_char();
  }

  else
  {
    foo = get_tty_str("NO MORE ERRORS");
    delete_buffer(id);
    show_buffer(old_id);
  }
}
