/*****************************************************************************/
/* COMPRESS -  implements a simple compressed viewer for source files        */
/*   When you press the COMPRESS command (ALT-C is this implementation),     */
/* all source lines which have some non-blank characters in the first        */
/* 'CompressLevel' characters of the line are displayed. The obly legal      */
/* keys in compressed viewing mode are UP, DOWN, PGUP, PGDN, ESC (exits      */
/* compressed mode), + (increases the CompressLevel) and - (decreases the    */
/* CompressLevel).                                                           */
/*                                                                           */
/* Written by Marc Adler  Magma Systems   1/5/88                             */
/*                                                                           */
/*****************************************************************************/

#include mekeys.h

#define TAB_LEVEL 2     /* Put your indentation level here */

int CompressLevel;

init()
{
  CompressLevel = TAB_LEVEL;
  assign_key("compress", ALT_C);
}

compress()
{
  old_buf = currbuf();
  save_position();

  /* Create a new buffer which will hold the compressed view */
start:
  new_buf = create_buffer("THE COMPRESSED FILE");
  gobof();
  show_buffer(new_buf);
  explode_window();
  setcurrbuf(old_buf);

  /* Go thru the lines of the old buffer and pick all lines whose 1st */
  /* non-blank character starts at a column less than CompressLevel.  */
  while (1)
  {
    cl = currline();
    message("Processing line [%d]", currlinenum());

    if (ltrim(substr(cl, 1, CompressLevel)) != "")
    {
      /* Transfer the line to the new buffer */
      setcurrbuf(new_buf);
      fast_insert(cl);  insert("\r");
      gobol();
    }
    setcurrbuf(old_buf);
    if (!down())  break;
  }
  
  /* Display the compressed file listing */
  setcurrbuf(new_buf);
  gobof();
  refresh();

  message(
"Use UP,DOWN,PGUP,PGDN to move, + to increase level, - to decrease, ESC exits");

  while ((c = get_tty_char()) != CTRL_D && c != ESC)
  {
    if (c == _UP || c == _DOWN || c == PGUP || c == PGDN)       /* navigate */
      command(c);
    else if (c == '+')          /* increase compress level */
    {
      CompressLevel += TAB_LEVEL;
      delete_buffer(new_buf);
      setcurrbuf(old_buf);
      goto start;
    }
    else if (c == '-' && CompressLevel > TAB_LEVEL)             /* decrease */
    {
      CompressLevel -= TAB_LEVEL;
      delete_buffer(new_buf);
      setcurrbuf(old_buf);
      goto start;
    }
  }

  /* Get rid of the compress buffer and go back to the original buffer */
  unexplode_window();
  delete_buffer(new_buf);
  setcurrbuf(old_buf);
  restore_position();
}

