/********************************************************************/
/* COMMENTS - macro for the ME text editor to handle comments       */
/*              when programming in C.                              */
/*                                                                  */
/*   Written 1/23/87 by Marc Adler of Magma Software Systems        */
/*                                    138 - 23 Hoover Ave.          */
/*                                    Jamaica, NY  11435            */
/*                                                                  */
/********************************************************************/


#define ALT_A   158
#define ALT_C   174

int  CommentColumn;

init()
{
  CommentColumn = 55;
  assign_key("align_file", ALT_A);
  assign_key("do_comment", ALT_C);
}

/* align_file - aligns all comments in a file */
align_file()
{
  int  lastln, i;
  int  startcol;

  lastln = lastlinenum();
  gobof();

  for (i = 1;  i <= lastln;  i = i + 1)
  {
    if ((startcol = index(currline(), "/*")) > 1  &&  /* has a cmnt already */
      /* If the comment is the only thing on the line, then don't touch it. */
        !is_line_blank(substr(currline(), 1, startcol - 1)))
      do_comment();
    gobol();
    down();
  }
}

/* do_comment - inserts a comment if none already in a line */
/*              or else aligns the comment at CommentColumn */
do_comment()
{
  string cl;
  int    startcol, textcol;

  if ((startcol = index(currline(), "/*")) > 0)     /* has a cmnt already */
  {
    setcol(startcol);           /* move to the comment */
    if (startcol < CommentColumn)
      /* slide the comment over till it hits the comment column */
      insert(repstr(" ", CommentColumn - startcol));
    else
    {
      /* slide the comment leftwards */
      save_position();
      /* move the cursor left until we reach a non-blank column */
      left();
      while (currchar() == ' ' && !isbol())
        left();
      textcol = currcol();      /* save the column # of the last char */
      restore_position();
      
      /* Delete leftwards until we reach the starting column */
      while (startcol > textcol + 3 && startcol > CurrentColumn)
      {
        backspace();
        startcol--;
      }
    } /* if startcol < CommentCol */
  } /* if startcol = index() */

  else                  /* No comment in the line yet, so insert one */
  {
    cl = currline();               /* make a copy of the current line*/
    if (substr(cl, strlen(cl), 1) == " ")
    {
      rtrim(cl);                   /* get rid of the trailing blanks */
      gobol();                     /*  on the current line           */
      deleol();
      insert(cl);
    }
    
    if (currcol() < CommentColumn) /* move to the comment column */
      setcol(CommentColumn);
    insert("/*  */");              /* put the comment at the end of the ln */
    left();  left();  left();      /* move to just after the open comment  */
  }
}

is_line_blank(str)
  string str;
{
  int i;
  int len;

  len = strlen(str);

  for (i = 1;  i <= len;  i++)
    if (substr(str, i, 1) != " ")
      return 0;
  return 1;
}
