/*
	e screen editor

	(C) G. Nigel Gilbert, MICROLOGY, 1981

	August-December 1981

	FILE: etvi

	FUNCTIONS: terminal,gotoxy,deleteline,linedelete,delpage,
			insertline,begdim,enddim,keytranslate,displayhelp

	PURPOSE: terminal dependent screen control functions

	NOTE this file is specific to the TeleVideo range of terminals.
	It may be used as a model for others
*/

#include "e.h"

/* control key codes, here for convenience in defining terminal sequences*/

#define CTRLA	0x01	/*	SOH */
#define CTRLB	0x02	/*	STX */
#define CTRLC	0x03	/*	ETX */
#define CTRLD	0x04	/*	EOT */
#define CTRLE	0x05	/*	ENQ */
#define CTRLF	0x06	/*	ACK */
#define CTRLG	0x07	/*	BEL */
#define CTRLH	0x08	/*	BS  */
#define CTRLI	0x09	/*	HT  */
#define CTRLJ	0x0a	/*	LF  */
#define CTRLK	0x0b	/*	VT  */
#define CTRLL	0x0c	/*	FF  */
#define CTRLM	0x0d	/*	CR  */
#define CTRLN	0x0e	/*	SO  */
#define CTRLO	0x0f	/*	SI  */
#define CTRLP	0x10	/*	DLE */
#define CTRLQ	0x11	/*	DC1 */
#define CTRLR	0x12	/*	DC2 */
#define CTRLS	0x13	/*	DC3 */
#define CTRLT	0x14	/*	DC4 */
#define CTRLU	0x15	/*	NAK */
#define CTRLV	0x16	/*	SYN */
#define CTRLW	0x17	/*	ETB */
#define CTRLX	0x18	/*	CAN */
#define CTRLY	0x19	/*	EM  */
#define CTRLZ	0x1a	/*	SUB */
#define SOH	0x01
#define STX	0x02
#define ETX	0x03
#define EOT	0x04
#define ENQ	0x05
#define ACK	0x06
#define BEL	0x07
#define BS 	0x08
#define HT 	0x09
#define LF 	0x0a
#define VT 	0x0b
#define FF 	0x0c
#define RETURN 	0x0d
#define SO 	0x0e
#define SI 	0x0f
#define DLE	0x10
#define DC1	0x11
#define DC2	0x12
#define DC3	0x13
#define DC4	0x14
#define NAK	0x15
#define SYN	0x16
#define ETB	0x17
#define CAN	0x18
#define EM 	0x19
#define SUB	0x1a
#define ESC 	0x1b
#define FS  	0x1c
#define GS  	0x1d
#define RS  	0x1e
#define US  	0x1f
#define DEL 	0x7f

/*-------------------------OUTPUT TO SCREEN---------------------------*/



/* screen control characters for TVI912C, ADM31 */
#define BEGINDIM ')'
#define ENDDIM '('
#define CRSADDR '='
#define INSLINE 'E'
#define DELLINE 'T'	/*delete to spaces*/
#define LINDEL 'R'	/*delete and close up*/
#define PAGEDEL 'Y'

terminal()	/*display name of terminal*/
{
	puts("|for TVI-912/920");
}

gotoxy(x,y)	/*move cursor to column x, line y {top left is (0,0) */
int x,y;
{
	putch(ESC);
	putch(CRSADDR);
	putch(y+32);
	putch(x+32);
}

deleteline(x,y)	/*clear to spaces all characters on line y from column x
			to the right edge of the screen, inclusive.
			Leave cursor on left most blank */
int x, y;
{
	gotoxy(x,y);
	putch(ESC);
	putch(DELLINE);
	gotoxy(x,y);

}

linedelete(y)	/*delete the line y.  All following lines move up one.
			Leave cursor at start of line y */
int y;
{
	gotoxy(0,y);
	putch(ESC);
	putch(LINDEL);
}

delpage(y)	/*clear to spaces line y and all following lines.  Leave
			cursor at the start of line y */
int y;
{
	gotoxy(0,y);
	putch(ESC);
	putch(PAGEDEL);
}

insertline()	/*move all lines below line on which cursor is, down one,
			losing last line.  New cursor line is blank, with
			cursor at start of line */
{
	putch(ESC);
	putch(INSLINE);
}

begdim()	/*display all subsequent characters at half intensity*/
{
	putch(ESC);
	putch(BEGINDIM);
}

enddim()	/* display all subsequent characters at full intensity */
{
	putch(ESC);
	putch(ENDDIM);
}


/*----------------------INPUT FROM KEYBOARD-----------------------------*/


keytranslate()	/*defines the terminal key codes which perform
			the editor commands */
{
/* each tran[xxxx]= should be set to the code emitted by the terminal
key which will perform the indicated action.  The recommended (control)
key assignments are shown in round brackets.
	Some terminals precede their codes by a lead-in character
(eg the Hazeltine uses the tilde).  This char should be assigned to
tran[LEADIN].  If there is no lead-in character, set tran[LEADIN] to zero.
	'e' will ignore the leadin character if tran[LEADIN] is non-zero,
but will set the parity bit on the next character. All other chars from the
keyboard will already have any parity bits removed as they are read in.  Thus
codes with lead-ins should be entered in the table below as code+0x80, or
more clearly, as code+PARBIT.
	For example, suppose that one is coding the Hazeltine 1420, which
generates a tilde,^L (0x0d) sequence when the cursor up key is pressd.  To
recognise this sequence, set tran[LEADIN] to tilde (0x7e) and set
tran[UPKEY] to 0x0d+PARBIT.
*/

	tran[LEADIN]=0;		/*lead-in character, 0 if not used*/
	tran[DOWNKEY]= 0x0a;	/*cursor down */
	tran[UPKEY]= 0x0b;	/*cursor up*/
	tran[LEFTKEY]= 0x08;	/*cursor left*/
	tran[RIGHTKEY]= 0x0c;	/*cursor right*/
	tran[RIGHTWKEY]= 0x04;	/*cursor right one word (D) */
	tran[LEFTWKEY]= 0x13;	/*cursor left one word (S) */
	tran[EOLKEY]= 0x05;	/*cursor to end of line (E) */
	tran[BOLKEY]= 0x02;	/*cursor to beginning of line (B) */
	tran[UPPAGE]= 0x17;	/*scroll up a page (W) */
	tran[DOWNPAGE]= 0x1a;	/*scroll down a page (Z) */
	tran[BOFKEY]= 0x15;	/*cursor to beginning of file (U) */
	tran[HOMEKEY]= 0x1e;	/*cursor to end of file (HOME) */
	tran[DELLEFT]= 0x7f;	/*delete char to left of cursor (DEL) */
	tran[DELRIGHT]= 0x07;	/*delete char under cursor (G) */
	tran[DELLNKEY]= 0x19;	/*delete cursor line (Y) */
	tran[DELWDKEY]= 0x14;	/*delete word to right of cursor (T) */
	tran[JUMPKEY]= 0x18;	/*jump to (X) */
	tran[CRSTILL]= 0x0e;	/*insert newline after cursor (N) */
	tran[QUITKEY]= 0x11;	/*quit (Q) */
	tran[ENVIRKEY]= 0x03;	/*edit and file context (C) */
	tran[FINDKEY]= 0x06;	/*find (F) */
	tran[ALTERKEY]= 0x01;	/*alter (A) */
	tran[BLOCKKEY]= 0x0f;	/*block operations (O) */
	tran[RDFILEKEY]= 0x10;	/*read a file (P) */
	tran[REPKEY]= 0x12;	/*repeat last find/alter (R) */
	tran[HELPKEY]= 0x16;	/*display help menu (V) */
	tran[OOPSKEY]= 0x1c;	/*restore unedited line (\) */
	tran[TAB]= 0x09;	/*tab (I) */
	tran[RETRIEVE]= 0x12;	/*retrieve (R)*/
	tran[CR]= 0x0d;		/*return*/
	tran[ESCKEY]=0x1b;	/*escape, the magic key (ESC)*/
}

/*-------------------------------HELP MENU-------------------------------*/

 
displayhelp()	/*display the help menu.  There must be (helplines-1)
			lines of menu here (see terminal function,above) */
{
	puts("CURSOR:  ^B|=beginning of line|  ^S|=left word|  ^D|=right word|  ^E|=end of line|\n");
	puts("MOVE:    ^W|=up a screen|        ^Z|=down a screen|  ^U|=top,|  HOME|=bottom of file|\n");
	puts("FIND:    ^F|=find|               ^R|=repeat last find/alter|    ^A|=alter|\n");
	puts("DELETE:  DEL|=char left|         ^G|=char right|     ^T|=word|    ^Y|=line|  ^\\|=oops\n");
	puts("   ^     ^Q|=quit|  ^P|=read file| ^O|=write,print,shift,move,copy,or delete blOck\n");
	puts(" <- ->   ^X|=jump to line|  ^N|=iNsert a new line   |^C|=context |^V|=view menu off/on|\n");
	puts("---v------------------------------------------------------------------------");
}


/*---------------------------------END---------------------------------------*/
WNPAGE]= 0x1a;	/*scroll down a page (Z) */
	tran[BOFKEY]= 0x15;	/*cursor to beginni