/************************************************************************
 *                                                                      *
 *                      copyright Richard Bornat 1981                   *
 *                                                                      *
 ************************************************************************/

int haz_special(typein, c)
register int typein;
register char c;
/* returns the itt arrow-key code corresponding
 * to hazeltine leadin - ch combination
 * If a dud combination, puts second character back in the buffer
 * and returns type TWO.
 * If a valid combination, returns the corresponding character
 * (via negative result).
 */
 { register char ch;

    if (c!='~') editerror("character %o given to haz_special", c);
    else
    if (typein)
     {  ch = ttyin();

        switch(ch)
         {  case 014:   return (mode==INMODE ? UP : IGN);
            case 013:   return (mode==INMODE ? DOWN : IGN);
            case 022:   return(SEND);
            default:    ttyback(ch); /* replace the character */
         }
      }
    /* fall through on unrecognised characters, and always on input */

    return(TWO);
 }

/* move the cursor on the hazeltine - four characters */
haz_move(srow, scol)
register int srow, scol;
 {  /* tilde, DC1, column, row */
    ttyout('~'); ttyout(021);
    ttyout(scol + (scol<=30 ? 96 : 0));
    ttyout(srow + 96);
 }

char haz_intwo(c)
register char c;
 { return (c=='~' ? '^' : c); }

char haz_outtwo(c)
register char c;
 { return (c=='^' ? '~' : c); }

#define TER_ROWS 24
#define TER_COLS 80

struct TERMINAL haz_ter =
 {  /* nrows, ncols - size of the screen */
    TER_ROWS, TER_COLS,

    /* up, left, c_MODIFY, c_CONTROL, c_BLOB - single characters */
   { 0, '\010', '@', '^'+0200, '`' },
        /* onechar */

    /* rollup, pushdown, pushup, pulldown, pullup STRINGS */
    /* and hardscroll boolean */
   { "\n", "~\032", 0, 0, "~\023", true },
        /* scrseq */

    /* scrollup, scrolldown PROCEDURES */
   scr2_up, scr2_down,

    /* splitdown, splitup, joindown, joinup STRINGS */
    { 0, 0, 0, 0 },
        /* splitseq */

    /* split_row, join_row PROCEDURES */
    split0, join0,

    /* clearscreen, *clearhead, *cleartail STRINGS */
   { "~\034", 0, "~\017" },
        /* clrseq */

    /* clr_screen, clr_head, clr_tail PROCEDURES */
    clrs1, clrh0, clrt1,

    /* inspace, makespace, rubout, erase STRINGS */
    { 0, 0, 0, 0 },
        /* charseq */

    /* ins_char, del_char PROCEDURES */
    insc0, delc0,

    /* bright, normal STRINGS */
    { "~\037", "~\031" },
        /* vidseq */

    /* in_two, out_two PROCEDURES */
    haz_intwo, haz_outtwo,

    /* move PROCEDURE and nmove integer */
    haz_move, 4,

    /* setup, special, pad PROCEDURES */
    no_int, haz_special, no_int

 };

/* table of characters for input (INMODE) mode */
char  ictab[] =
 {      /* 000 - 007, null - ^G */
        IGN, WRIGHT, WLEFT, IGN, IGN, DOWN, TAILERASE, WERASE,
        /* 010 - 017, ^H - ^O */
        LEFT, TAB, NL, LSTART, LEND, CR, BOTSCREEN, TOPSCREEN,
        /* 020 - 027, ^P - ^W */
        RIGHT, RIGHT, ERASE, UP, IGN, LEFT, SEND, WRUBOUT,
        /* 030 - 037, ^X - ^_ */
        HEADRUBOUT, CHMODE, CONTROL, ENTERedit, IGN, LINEERASE, IGN, IGN,
        SPACE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, SPECIAL, RUBOUT
 };

/* table of characters for edit (EDMODE) mode. Much the same
 * as ictab, except for the absence of UP, DOWN, TOPSCREEN,
 * BOTSCREEN and LINEERASE. CR or LF are also COMMAND in this mode
 */
char  ectab[] =
 {      /* 000 - 007, null - ^G */
        IGN, WRIGHT, WLEFT, IGN, IGN, IGN, TAILERASE, WERASE,
        /* 010 - 017, ^H - ^O */
        LEFT, TAB, COMMAND, LSTART, LEND, COMMAND, IGN, IGN,
        /* 020 - 027, ^P - ^W */
        RIGHT, RIGHT, ERASE, IGN, IGN, LEFT, SEND, WRUBOUT,
        /* 030 - 037, ^X - ^_ */
        HEADRUBOUT, CHMODE, CONTROL, ENTERedit, IGN, IGN, IGN, IGN,
        SPACE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, ONE, ONE,
        ONE, ONE, ONE, ONE, ONE, ONE, SPECIAL, RUBOUT
 };
