
 /****************************************************************************
 **									    **
 **		-- TEdi --    (c) 21.4.88 by TAA			    **
 **									    **
 sub4************************************************************************/


 /******  Globals  */

extern struct System System;
extern struct File *Wfile;
extern struct Line *Wline;
extern struct Window *w;
extern UBYTE Wpos,Wlen;
extern int Wy,Wx,p2flag,Status;
extern char *Wbuf,*bmp;


 /******  SubRoutines  */


Ctrl27()
{
ModLine();
System.Anf.Line  = Wline;
System.Anf.pos   = Wpos;
System.Fileindex = System.Active;
System.Status    = 0;
System.Linenr    = Wfile->ActLine;
killbuffer();
Update();
}


Ctrl29()
{
struct Line *End,*Anf,*Help,*Ha,*K;
UBYTE Epo,Apo,j;
int i,a,b;

if (System.Status != 0) { DisplayBeep(0); return(0); }
a   = System.Linenr;
b   = Wfile->ActLine;
Apo = System.Anf.pos;
Epo = Wpos;
if (System.Fileindex != System.Active) { DisplayBeep(0); return(0); }
if ((a == b) && (Epo == Apo)) { DisplayBeep(0); return(0); }
Anf = System.Anf.Line;
End = Wline;
if ((a > b) || ((a == b) && (Epo < Apo)))
	{
	End = Anf;
	Anf = Wline;
	i   = b;
	b   = a;
	a   = i;
	Epo = Apo;
	Apo = Wpos;
	}
j = Apo;
K = Anf;
for(i=a;i < b;i++)
	{
	Ha         = AllocMem(sizeof(struct Line),MEMF_PUBLIC);
	if (i == a) System.buffer = Ha;
	else Help->next = Ha;
	Ha->len    = K->len-j;
	Ha->string = AllocMem(Ha->len,MEMF_PUBLIC);
	cpystr(K->string+j,Ha->len,Ha->string);
	Help       = Ha;
	K          = K->next;
	j          = 0;
	}
if (Epo != 0)
	{
	Ha         = AllocMem(sizeof(struct Line),MEMF_PUBLIC);
	if (a != b) Help->next = Ha;
	else { j   = Apo; System.buffer = Ha; }
	Ha->next   = 0;
	Ha->len    = Epo-j;
	Ha->string = AllocMem(Epo-j,MEMF_PUBLIC);
	cpystr(K->string+j,Epo-j,Ha->string);
	}
else	Ha->next = 0;
System.Status = 1;
Update();
}


CtrlP()
{
struct Line *a,*b,*tail,*data;
int l,i=0,f=0;

if (System.buffer == 0) { DisplayBeep(0); return(0); }
cursor(0);
ModLine();
l    = Wlen-Wpos;
cpystr(Wline->string+Wpos,l,bmp);
data = System.buffer;
tail = Wline->next;
cpystr(data->string,data->len,Wbuf+Wpos);
Wlen = data->len+Wpos;
ModLine();
a    = Wline;
b    = a;
while(data->next != 0)
	{
	data      = data->next;
	b         = AllocMem(sizeof(struct Line),MEMF_PUBLIC);
	a->next   = b;
	b->prev   = a;
	b->string = AllocMem(data->len,MEMF_PUBLIC);
	cpystr(data->string,data->len,b->string);
	b->len    = data->len;
	a         = b;
	i++;
	f         = 1;
	}
if(data->string[data->len-1] == 10)
	{
	b          = AllocMem(sizeof(struct Line),MEMF_PUBLIC);
	a->next    = b;
	b->prev    = a;
	b->next    = tail;
	if (tail != 0) tail->prev = b;
	else Wfile->last = b;
	b->string  = AllocMem(l,MEMF_PUBLIC);
	cpystr(bmp,l,b->string);
	b->len     = l;
	i++;
	Wpos       = 0;
	}
else	if (f == 1)
		{
		FreeMem(b->string,b->len);
		b->len     = data->len+l;
		b->string  = AllocMem(b->len,MEMF_PUBLIC);
		cpystr(data->string,data->len,b->string);
		cpystr(bmp,l,b->string+data->len);
		b->next    = tail;
		if (tail != 0) tail->prev = b;
		else Wfile->last = b;
		Wpos       = data->len;
		}
	else
		{
		Wpos = Wlen;
		cpystr(bmp,l,Wbuf+Wlen);
		Wlen += l;
		ModLine();
		}
Wfile->SFlag     = 0;
Wfile->AnzLines += i;
Wfile->ActLine  += i;
Wline            = b;
Wlen             = b->len;
cpystr(Wline->string,Wlen,Wbuf);
Wy              += i;
if(Wy > size()-1) Wy = size()-1;
SPage(Wline,Wy);
}


CtrlK()
{
struct Line *End,*Anf,*Ki,*K;
UBYTE Epo,Apo;
int i=0,a,b;

if (System.Status != 0) { DisplayBeep(0); return(0); }
a   = System.Linenr;
b   = Wfile->ActLine;
Apo = System.Anf.pos;
Epo = Wpos;
if (System.Fileindex != System.Active) { DisplayBeep(0); return(0); }
if ((a == b) && (Epo == Apo)) { DisplayBeep(0); return(0); }
Anf = System.Anf.Line;
End = Wline;
if ((a > b) || ((a == b) && (Epo < Apo)))
	{
	End = Anf;
	Anf = Wline;
	i   = b;
	b   = a;
	a   = i;
	Epo = Apo;
	Apo = Wpos;
	}
Ctrl29();
cursor(0);
System.Status = 2;
if (Anf == End)
	{
	cpystr(Wbuf+Epo,Wlen-Epo,Wbuf+Apo);
	Wlen -= (Epo-Apo);
	Wpos  = Apo;
	ModLine();
	PrtLine(Wpos,Wy,Wbuf,Wlen,1);
	}
else
	{
	K         = Anf->next;
	while(K != End)
		{
		Ki = K;
		K  = K->next;
		killine(Ki);
		i++;
		}
	K = K->next;
	Wline     = Anf;
	Wlen      = Wline->len;
	cpystr(Wline,Wlen,Wbuf);
	cpystr(End->string+Epo,End->len-Epo,Wbuf+Apo);
	Wlen      = Apo+(End->len-Epo);
	ModLine();
	killine(End);
	i++;
	if (K != 0) K->prev   = Anf;
	else Wfile->last = Anf;
	Anf->next = K;
	Wpos      = Apo;
	Wfile->ActLine  -= i;
	Wfile->AnzLines -= i;
	Wy       -= i;
	if (Wy < 0) Wy = 0;
	SPage(Wline,Wy);
	}
Wfile->SFlag = 0;
Update();
}
	

 /******  EndofTextFile  */

