/*
 *       buchstabeE.c             © by Martin Steppler
 */

struct Gadget ArrowGads[2], VertSlider;
struct Image Image3;
extern InitSlider();

Buchstabe(c)
UBYTE c;
{
   int len=0, i, r, st, st2, e, e2=FALSE, e3, bloflag=0;

   i=SL(s,z); st=SC(s,z,i);
   if(i-st < x) {                      /* x größer string */
spg1: if(!(c==32 && (stil==0 || stil==4 || stil==6))) {
         BelAend(s,z,st,x+st,i);
         for(len=i;len<x+st-1; len++) a[s] [z] [len]=32;
         a[s] [z] [len++] = c; a[s] [z] [len] = EOS;
         r=Testen(stil);
         if(!((!r && !stil) || oflag)) {
            if(x==MAX && ein==FALSE) {
               if(a[s] [s] [x+r-2]<9) {
                  len=stil; if(!len) len=8; a[s] [z] [x+r-2] = len;
               }
               else SpeicherStil(x+st-1, ++st, r);
            }
            else SpeicherStil(x+st-1, ++st, r);
         }
      }
      if(x==MAX && ein==TRUE) {
         if(spe) {
            if(c!=32) { i=x+st; bloflag=5; goto spg2; }
            x=MIX; YZPlus();
            x=0; return();
         }
         if(c!=32) {
            for(len=x+st-1; len>=0;len--) {
               if(a[s] [z] [len] == 32) {
                  x = len+2-st; break;
               }
            }
         }
         if(x>MAX || len<0 || x<MIX) x=MAX;
         e=s; e2=z; Flagge = 256; CarRet();
         if(ausri>LINKS) { e3=s; i=z; s=e; z=e2; meld=2; y--;
            st=Ausrichtung(ausri); meld=FALSE;
            if(st) SonderDruck(y, SL(s,z), 0);
            else   NormalDruck(y, SL(s,z), 0);
            s=e3; z=i; y++;
         }
         if(c==32) x=0;
      }
   }
   else if(ein==TRUE) {                    /* x im string */
      if(i-st==MAX) {        /* Zeile voll */
spg2:    e=x; e2=st;
         for(len=i-1; len>=0; len--) {
            if(a[s] [z] [len] <9) e2--;
            if(a[s] [z] [len] == 32) {
               x = len+2-e2; break;
            }
         }
         if(x>MAX ||len<0) { Flagge=501; x=e; }  /* voll ohne space */
         else if(x>e) Flagge=e;                  /* Crsr vor  Wort   */
         else Flagge = e-x+501;                  /* Crsr im   Wort   */
         if(!zpe && !spe) {          /* zum erstenmal */
yabbadabba: e2=CarRet(); if(meld==TRUE) return(0);
            zpe=z; spe=s+1;
            if(e2) YZMinus();
            i=SL(s,z); st=SC(s,z,i);
            if(i-st<x || bloflag==5) { bloflag=0; goto spg1; }
         }
         else {                      /* nicht zum erstenmal */
            len=SL(--spe,zpe); st2=SC(spe,zpe,len);
            if(i-st-x+1+len-st2>MAX) { meld=FALSE; goto yabbadabba; }
            r=e=e3=meld=0;
            if(st) { e=x; x=MAX; Testen(stil); x=e; e=0;
               if(a[spe] [zpe] [0] != letztes && a[spe] [zpe] [0] <9)
                  e3=a[spe] [zpe] [0];
               else if(a[spe] [zpe] [0] >8 && letztes !=8) {
                  e3=8; meld=1; st2++;
               }
               else if(a[spe] [zpe] [0] == letztes) e=1;
               else if(a[spe] [zpe] [0] >8 && letztes == 8) {
                  e3=9;
               }
               r=Testen(stil);
            }
            st2+=(st-r); if(r) { meld++; st2++; } if(e) st2--;
            BelAend(spe,zpe,st2,len-e+i-x-r+2+meld,len);
            for(e2=len; e2>=0; e2--)
               a[spe] [zpe] [e2+i-e-x-r+2+meld] = a[spe] [zpe] [e2];
            if(r) {
               a[spe] [zpe] [0] = letztes;
               if(meld==2) { meld=1; len++; }
               if(e3<9 && !e) a[spe] [zpe] [i+meld-x-r+2] = e3;
            }
            else if(!e && e3==8) {
               a[spe] [zpe] [i-x-r+2] = e3; len++; meld=FALSE;
            }
            a[spe] [zpe] [i+meld-x-r+1] = 32;
            len+=(i-e-x-r+2+meld);
            for(e2=i-1; e2>=x+r-1; e2--)
               a[spe] [zpe] [e2-x-r+1+meld] = a[s] [z] [e2];
            meld=FALSE;
            if(x>off && (x-MAX1<off)) {
               SetAPen(RP, 0);
               RectFill(RP, ((x-off)*8-8), y*8-8, RRAND, y*8-1);
               SetAPen(RP, 1);
            }
            for(e2=x+r-2; e2>=0; e2--) {
               if(a[s] [z] [e2] < 9) r--;
               else if(a[s] [z] [e2] == 32) x--;
               else break;
            }
            a[s] [z] [x+r-1] = EOS;
            if(x+r-1!=i) BelAend(s, z, r, x+r-1, i);
            i=x+r-1; st=r;
            s=spe; z=zpe; y++; x=((e3=Flagge)>500) ? (Flagge-499):(Flagge+1);
            Flagge=FALSE; e=(x>MAX) ? TRUE:FALSE; x-=e;
            e2 = (bloflag==5) ? TRUE:FALSE; bloflag=0;
            if(st2) SonderDruck(y, len, 0);
            else    NormalDruck(y, len, 0);
            if(!e) x--;
            if(e2) { spe=zpe=0; goto spg3; }
            if(e3<500) { YZMinus(); spe++; if(i-st<x) goto spg1; }
            else { spe=zpe=0; i=len; st=st2; }
         }
      }
      r=Testen(stil);
      BelAend(s,z,st,i+1,i);
      if(r) {
         if(a[s] [z] [x+r-2] <9) {
            if(!((a[s] [z] [x+r-2]==8&&!stil)||a[s] [z] [x+r-2]==stil))
               r--;
         }
      }
      if(i>x-1+r) {
         for(len=i; len>x-1+r; len--) a[s] [z] [len] = a[s] [z] [len-1];
      }
      a[s] [z] [x-1+r] = c; a[s] [z] [i+1] = EOS;
      st=StilPruefungII(i+1, st);
      if(meld==100) return();
      if(x<=off)   { SText(); meld=FALSE; }
      else if(x-MAX1+1>=off) { x++; SText(); x--; meld=FALSE; }
      else if(st) SonderDruck(y, SL(s,z), x);
      else {
         a[s] [z] +=(x-1-off);
         NormalDruck(y, SL(s,z), x-1-off  );
         a[s] [z] -=(x-1-off);
      }
spg3: ;
   }
   else {              /* im string und ein == FALSE */
      r = Testen(stil);
      a[s] [z] [x-1+r] = c;
      StilPruefung(i, r, st);
   }
}
StilPruefung(i, r, st)
int i, r, st;
{
   int len=stil;

   if(!((!r && !stil) || oflag)) {
      if(!len) len=8;
      if(r) {
         if(a[s] [z] [x+r-2] < 9) a[s] [z] [x+r-2] = len;
         else SpeicherStil(i++, ++st, r++);
      }
      else SpeicherStil(i++, ++st, r++);
      if(i-st>x) {
         if(a[s] [z] [x+r] > 8) {
            stil=letztes; x++; SpeicherStil(i,++st,r); stil=len; x--;
            if(stil==8) stil=0;
         }
      }
   }
}
BelAend(ss, zz, st, neu, alt) /* BelegungAendern */
int ss, zz, st, neu, alt;
{
   UBYTE *buf;

   if(!(buf = (UBYTE *) AllocMem(neu+2, MEMF_CLEAR)))
      SpeicherMangel();
   strcpy(buf, a[ss] [zz]);
   if(!a[ss] [zz] [0]) alt=0;
   FreeMem(a[ss] [zz], alt+2);
   a[ss] [zz]         = buf;
   a[ss] [zz] [neu+1] = st;
}
ErstBelAend(ss, zz, st, neu, e, f) /* BelegungAendern & ErstBel */
int ss, zz, st, neu, e, f;
{
   if(!(a[ss] [zz] = (UBYTE *) AllocMem(neu+2, MEMF_CLEAR)))
      SpeicherMangel();
   strcpy(a[ss] [zz], a[e] [f]);
   a[ss] [zz] [neu+1] = st;
}
ErstBel(ss, zz)             /* ErstBelegung */
int ss, zz;
{
   if(!(a[ss] [zz] = (UBYTE *) AllocMem(2, MEMF_CLEAR)))
      SpeicherMangel();
   a[ss] [zz] [0] = a[ss] [zz] [1] = EOS;
}
StilPruefungII(i, st)
int i, st;
{
   int len=0, r;

   r=Testen(stil);
   if(!((!r && !stil) || oflag)) {
      SpeicherStil(i, ++st, r++);
      if(i+1-st>x) {
         if(a[s] [z] [x+r] > 8) {
            len=stil; stil=letztes; if(!stil) stil=8; x++;
            SpeicherStil(i+1,++st,r); stil=len; x--;
         }
      }
   }
   return(st);
}
SText()                /* Schnelltext */
{
   int yalt=y, salt=s, zalt=z;

   if(y>MIY) CrsrTop();
   FastText();
   if(!meld || (meld && y>yalt)) y=yalt;
   s=salt; z=zalt;
}
NeueGroesse()
{
   MAY=(Fenster->Height-15)/8;
   YKOD=MAY*8-1;
   y=MIY;
   Image3.Height = VertSlider.Height = Fenster->Height-41-27;
   ArrowGads[0].TopEdge = Fenster->Height-27;
   ArrowGads[1].TopEdge = Fenster->Height-18;
   RefreshGList(&VertSlider, Fenster, NULL, -1);
   InitSlider(&VertSlider);
   SetAPen(RP, 0);
   RectFill(RP, 0, 32, RRAND, Fenster->GZZHeight);
   SetAPen(RP, 1);
   SText(); ZSDisplay();
}
NormalDruck(yk, ln, xk)
int yk, ln, xk;
{
   if(x<=off)           { off=( x      /MAX1  )*OFF1; SText(); }
   else if(x-MAX1>=off) { off=((x-MAX1)/OFF1+1)*OFF1; SText(); }
   else if(ln>off) {
      if(stil) StilAus();
      if(blo) { if(blo==2 || (blo>2 && BloPruef())) SetAPen(RP, ROT); }
      if(off) a[s] [z] += off;
      Move(RP, xk*8, yk*8-2);
      if(ln<off+MAX1) Text(RP, a[s] [z], ln-off);
      else            Text(RP, a[s] [z], MAX1-1);
      if(off) a[s] [z] -= off;
      if(blo) SetAPen(RP, WEISS);
      if(stil) StilAn();
   }
}
Cursor(n)
int n;
{
   static int xalt, yalt, nalt;

   if(n==2) { nalt=n; xalt=x; yalt=y; }
   else if(!((xalt==(x-off-1)*8) && (yalt=y*8-2) && (n==nalt))) {
      SetAPen(RP, 3);             /* 0 Aus 1 An  */
      SetDrMd(RP, INVERSVID|COMPLEMENT);
      if(!(!n && !nalt)) {
         if(!n||(n && n==nalt)) {
             Move(RP, xalt, yalt);
             Text(RP, " ", 1);
         }
         if(n) {
            xalt=(x-off-1)*8; yalt=y*8-2;
            Move(RP, xalt, yalt);
            Text(RP, " ", 1);
         }
         nalt=n;
         SetAPen(RP, 1);
         SetDrMd(RP, JAM2);
      }
   }
}
FastText()
{
   int len, ab=0, brel;

   SetAPen(RP, 0);
   RectFill(RP, 0, 32, RRAND, YKOD+2);
   SetAPen(RP, 1);
   meld=FALSE; y=MIY;
   if(blo==2 || blo==6 || blo==4) {
      if(blo==4) blo=2;
      if((brel=BloRel(blo))<MAY-MIY && brel>0 && blo==2) ab=1; /* hi ru sp*/
      else if(brel>0) ab=(blo==2) ? 2:3; /* hin runter / hoch */
      else if(brel<0) { ab=4; brel*=(-1); }  /* hin hoch sprung */
      else ab=5;
      blo=0;
   }
   for(len=0; len<=MAY-MIY; len++) {
      if(ab) {
         if((ab==2&&len==MAY-MIY)||(ab==3&&!len)||(ab==1&&len>=brel)
             ||(ab==4&&len<=brel)||ab==5) SetDrMd(RP, COMPLEMENT);
      }
      Rolling();
      if(ab) SetDrMd(RP, JAM2);
      if(len<MAY-MIY)       YZPlus();
      if(meld == TRUE) { sprintf(str, "%s", buffer); break; }
   }
   if(ab) blo=2;
   if(meld==TRUE) { sprintf(buffer, "%s", str); }
}
SonderDruck(len, ii, pos)
int len, ii, pos;
{
   int q,p,i,r;

   if(x<=off)           { off=( x      /MAX1  )*OFF1; SText(); }
   else if(x-MAX1>=off) { off=((x-MAX1)/OFF1+1)*OFF1; SText(); }
   else if(ii>off) {
      i = SC(s,z,ii);
      if(i==1 && a[s] [z] [0]<9) {
         p=stil; stil=a[s] [z] [0]; if(stil==8) stil=0;
         StilAn(); if(pos) pos-=(off+1); stil=0;
         a[s] [z] += (pos+1);
         NormalDruck(len, ii-(pos+1), pos);
         stil=p; StilAn(); a[s] [z] -= (pos+1);
      }
      else {
         if(blo) {
            if(blo==2 || (blo>2 && BloPruef())) SetAPen(RP, ROT);
         }
         p=stil; stil=i=q=0; buffer[i] = EOS;
         if(pos || off) {
            if(off && !pos) { q=x; x=off+1; }
            r=Testen(stil); pos=x+r-1; stil=letztes;
            if(q) { x=q; q=0; }
            else q=x-1-off;
         }
         for(r=pos; r<=ii-1; r++) {
            if(a[s] [z] [r] <9) {
               if(i) {
                  buffer[i] = EOS;
                  TeilAusdruck(q,len,i);
                  q+=i; i=0;
               }
               stil = a[s] [z] [r]; if(stil==8) stil=0;
            }
            else buffer[i++] = a[s] [z] [r];
            if(r==ii-1) {
               buffer[i]=EOS; TeilAusdruck(q, len, i);
            }
         }
         StilAus(); stil=p; StilAn(); buffer[0] = EOS;
         if(blo) SetAPen(RP, WEISS);
      }
   }
}
TeilAusdruck(q, len, lenbuf)
int q, len, lenbuf;
{
   StilAn();
   Move(RP, q*8, len*8-2);
   Text(RP, buffer, lenbuf);
}
BloRel(mode)    /* entfernen wir uns von der Blockmarkierung ? */
int mode;
{
   static int altcpos = 0;
   int bpos = (bas*(SEITE+1)+baz), cpos = (s*(SEITE+1)+z), rtwert=FALSE;
   if(cpos==bpos && !mode) rtwert=1;
   else if(cpos>bpos) {
      if(cpos<altcpos)  rtwert=cpos-bpos; /* hin */
   }
   else {
      if(cpos>altcpos) rtwert=bpos-cpos;  /* hin */
      else if(mode==6 && bpos-cpos<MAY-MIY) rtwert=cpos-bpos; /* phasensprung */
   }
   altcpos=cpos; if(mode==2) altcpos+=MAY-MIY;
   return(rtwert);
}
CrsrTop()
{
   int len;

   for(len=y;len>MIY;len--)
      YZMinus();
}
CrsrHoch()
{
   int len, fahne = FALSE, altblo=0;

   if(blo==2) { blo=0; altblo=1; }
   for(len=0; len<MAY-MIY; len++)
   {
      y=MIY+1;
      YZMinus();
      if(meld==TRUE) {
         fahne = TRUE;
         meld = FALSE;
         break;
      }
   }
   y=MIY;      if(altblo) blo=6;
   FastText(); if(altblo) blo=0;
   CrsrTop();  if(altblo) blo=2;
   if(fahne == TRUE) {
      meld = TRUE;
      sprintf(buffer, "%s", FehlerText[0]);
   }
}
CrsrBot()
{
   int len;
   for(len=y;len<MAY;len++) {
      YZPlus();
      if(meld == TRUE)
         break;
   }
}

Hochrollen()
{
   if(blo==2) {
      if(BloRel(0)) {
         if(--z<0) { z=SEITE; s--; } blo=0;
         SetAPen(RP, 0); RectFill(RP, 0, y*8-8, RRAND, y*8-1);
         SetAPen(RP, 1); Rolling();
         blo=2; if(++z>SEITE) { z=0; s++; }
      }
   }
   ScrollRaster(RP, 0, 8, 0, 32, RRAND, YKOD);   /* 632 */
   Rolling();     /* MAY */
}
Runterrollen()
{
   if(blo==2) {
      if(BloRel(0)) {
         if(++z>SEITE) { z=0; s++; } blo=0;
         SetAPen(RP, 0); RectFill(RP, 0, y*8-8, RRAND, y*8-1);
         SetAPen(RP, 1); Rolling();
         blo=2; if(--z<0) { z=SEITE; s--; }
      }
   }
   ScrollRaster(RP, 0, -8, 0, 32, RRAND, YKOD);  /* 632 */
   Rolling();
}
Rollen()
{
   ScrollRaster(RP, 0, -8, 0, (y-1)*8, RRAND, YKOD);
   Rolling();
}
Rolling()
{
   int i;

   if(i=SL(s,z)) {
      if(a[s] [z] [i+1]) SonderDruck(y, i, 0);
      else NormalDruck(y, i, 0);
   }
}
ZeileLoeschen()
{
   int xalt=x;
   if(ze==z && se==s) {
     x=1;
     RestLoeschen(0);
     x=xalt;
   }
   else
      BSpaceII();
}
BloPruef()
{
   if(bas<s && s<bes)
      return(TRUE);
   if(bas==s && baz<=z) {
      if(bes>s)
         return(TRUE);
      if(bes==s && bez>=z)
         return(TRUE);
   }
   if(bes==s && bez>=z && bas<s)
      return(TRUE);
   return(FALSE);
}
RestLoeschen(exmeld)
int exmeld;
{
   int len,r,i=SL(s,z);

   r=Testen(stil); a[s] [z] [x+r-1] = EOS;
   if((len=x+r-1)>0) {
      for(len=x+r-2;len>0;len--){
         if(a[s] [z] [len] == 32) a[s] [z] [len] = EOS;
         else if(a[s] [z] [len] <9) { r--; a[s] [z] [len] = EOS; }
         else break;
      }
      len++;
   }
   BelAend(s, z, r, len, i);
   if(exmeld!=500) {
      SetAPen(RP, 0);
      RectFill(RP, ((x-off)*8-8), y*8-8, RRAND, y*8-1);
      SetAPen(RP, 1);
   }
}
CarRet()
{
   int len, r, i, e=FALSE, st, exmeld=meld;

   if((MAS-se+SEITE-ze)>0) {
      i=SL(s,z); st=SC(s,z,i);
      meld=2; BlockEinfuegen(s,z,s,z); meld=FALSE;
      if(i-st>=x) {
         RestLoeschen(exmeld); len=0;
      }
      else len=1;
      if(++z>SEITE) { z=0; s++; }
      if(len) {
         a[s] [z] [0] = EOS;
         BelAend(s,z, 0, 0, i);
      }
      else {
         r=Testen(stil); st=e=0;
         if(letztes!=0 && letztes!=8) { st=e=1; a[s] [z] [0] = letztes; }
         for(len=x+r-1; len<=i-1; len++, st++) {
            if(a[s] [z] [len] <9) e++;
            a[s] [z] [st] = a[s] [z] [len];
         }
         a[s] [z] [st] = EOS;
         BelAend(s, z, e, st, i);
      }
      if(exmeld==500) return();
      e=FALSE;
      if(Flagge) {
         if(Flagge==256)  StrEnde();
         else if(Flagge>500) x=Flagge-499;
         else { x=Flagge+1; e=TRUE; }
         Flagge=0;
      }
      else x=1;
      if(x<=off) { off=(x/MAX1)*OFF1; if(++y>MAY) y=MAY; SText(); }
      else {
         if(++y>MAY) { y=MAY; Hochrollen(); }
         Rollen();
      }
      if(x!=1) x--;
      meld=FALSE;
   }
   else {
      sprintf(buffer, "%s", FehlerText[2]);
      meld = TRUE;
   }
   return(e);
}
BSpace()
{
   int len, r, i, st, rs=0, rz, st2,e=0;
   UBYTE buf[512];

   if(s==0 && z==0 && x==1) {
      sprintf(buffer, "%s", FehlerText[0]);
      meld = TRUE;
   }
   else {
      i=SL(s,z); st=SC(s,z,i);
      if(x>1) {
         if(i-st>=x-1) {
            if(i-st==x-1) { x--; r=Testen(stil); x++;
               for(len=x+r-3; len>=0; len--) {
                  if(a[s] [z] [len]<9)    { rs++; st--; }
                  else if(a[s] [z] [len] == 32)  rs++;
                  else break;
               }
               a[s] [z] [i-1-rs] = EOS;
            }
            else {
               r=Testen(stil);
               if(r>1) { if(a[s] [z] [x+r-2]<9 && a[s] [z] [x+r-4]<9) {
                              r--; st--; rs=1; }}
               if(r && !rs) { if(a[s] [z] [x+r-2]<9) r--; }
               for(len=x+r-1; len<=i; len++)
                  a[s] [z] [len-1-rs] = a[s] [z] [len];
            }
            BelAend(s, z, st, i-1-rs, i);
            if(meld==100) return();
            i=i-1-rs; if((len=i-st-off)<0) len =0;
            SetAPen(RP, 0);
            RectFill(RP,len*8,y*8-8,RRAND,y*8-1);
            SetAPen(RP, 1);
            if(--x<=off)   { SText(); meld=FALSE; }
            else if(i-st>x-1) {
               if(st) SonderDruck(y, SL(s,z), x);
               else {
                  a[s] [z] +=(x-1-off);
                  NormalDruck(y, SL(s,z), x-1-off  );
                  a[s] [z] -=(x-1-off);
               }
            }
         }
         else XMinus();
      }
      else {
         rz=z-1; rs=s; spe=zpe=0;
         if(rz<0) { rz=SEITE; rs--; }
         len=SL(rs,rz); st2=SC(rs,rz,len);
         if(i-st+len-st2<=MAX) {
            if(st2) {
               SchnellTest(rs,rz,len-st2);
               if(a[s] [z] [0] >8 && letztes!=8) e=1;
            }
            x=len-st2+1; strcpy(buf, a[rs] [rz]);
            if(!(a[rs] [rz] = (UBYTE *) AllocMem(i+len+e+2, MEMF_CLEAR)))
               SpeicherMangel();
            strcpy(a[rs] [rz], buf);
            a[rs] [rz] [i+len+e+1] = st2+st+e;
            if(e) a[rs] [rz] [len] = 8;
            for(r=0; r<=i; r++) a[rs] [rz] [r+len+e] = a[s] [z] [r];
            if(x<=off)           { off=( x      /MAX1  )*OFF1; SText(); }
            else if(x-MAX1>=off) { off=((x-MAX1)/OFF1+1)*OFF1; SText(); }
            BSpaceII();
            s=rs; z=rz;
            if(--y<MIY) {
               y=MIY;
               Runterrollen();
            }
            else Rolling();
         }
      }
   }
}
BSpaceII()
{
   int i, r, rz, rs;

   rz=z; rs=s; i=y;
   meld=2; BlockLoeschen(s,z,s,z); meld=FALSE;
   if(blo>2) {
      if(BloPruef()) {
         if(bas==bes && baz==bez) blo=0;  /* Block gelöscht */
         else {
            if(--bez<0) { bez=SEITE; bes--; }
         }
      }
      else if(s<bas || (s==bas && z<baz)) {
         if(--baz<0) { baz=SEITE; bas--; }
         if(--bez<0) { bez=SEITE; bes--; }
      }
   }
   if(y<MAY) CrsrBot();
   ScrollRaster(RP, 0, 8, 0, i*8-8, RRAND, YKOD);
   if(meld == FALSE) Rolling();
   z=rz; s=rs; y=i; meld=FALSE;
}
BlockSchieben(n)
int n;
{
   int i, olds=s, oldz=z, oldMAX = MAX;

   MAX=ABSMAX;
   FOREVER {
      meld=100; s=bas; z=baz;
      if((!n || n==2) && x==MAX) break;
      else if((n==1 || n==3) && x==MIX) break;
      FOREVER {
         if(!n || n==2) {           /* Block nach rechts verschieben */
            if((i=SL(s,z))-SC(s,z,i)<MAX) {
               Buchstabe((UBYTE *)' ');
            }
         }
         else if(n==1 || n==3) {     /* Block nach links verschieben */
            if((i=SL(s,z))-SC(s,z,i)>=x-1) BSpace();
         }
         if(s==bes && z==bez) break;
         if(++z>SEITE) { z=0; s++; }
      }
      if(!n || n==2) {
         meld=FALSE; XPlus();
         if(n==2 && tab[x-off-1] == '1') break;
         else if(!n) break;
      }
      else {
         meld=FALSE; XMinus();
         if(n==3 && tab[x-off-1] == '1') break;
         else if(n==1) break;
      }
   }
   s=olds; z=oldz; MAX = oldMAX; if(x>MAX) x=MAX;
   SText(); ZSDisplay(); meld=FALSE;
}
JumpWord(n)
int n;
{
   int i, st, len=0, r, durch=0;
   if(n) {
      FOREVER {
         st=SC(s,z,(i=SL(s,z)));
         if(x<=off)           { off=( x      /MAX1  )*OFF1; SText(); }
         else if(x-MAX1>=off) { off=((x-MAX1)/OFF1+1)*OFF1; SText(); }
         if(x==MIX && x==i-st && durch) break;
         durch=1;
         if(x>=i-st) { r=0;
            if(y==MAY) r=1;
            YZPlus(); if(meld) break;
            if(r) {
               st=s; i=z;
               for(len=1; len<=(MAY-MIY)/2; len++) {
                  YZPlus(); if(meld) { meld=0; break; }
               }
               s=st; z=i; y=MAY-len+1;
            }
            x=MIX; len=1; continue;
         }
         if(a[s] [z] [x+Testen(stil)-1] == 32) len=1;
         else if(len) break;
         do {
            XPlus(); if(a[s] [z] [x+(r=Testen(stil))-1] == 32) len=1;
         } while(x<i-st && !(a[s] [z] [x+r-1] != 32 && len));
         if(a[s] [z] [x+r-1] != 32 && len) break;
      }
   }
   else {
      FOREVER {
         st=SC(s,z,(i=SL(s,z)));
         if(x>i-st) { x=i-st; if(x==MIX) break; if(!x) x=MIX; }
         if(x==MIX) { r=0;
            if(y==MIY) r=1;
            YZMinus(); if(meld) break;
            if(r) {
               st=s; i=z;
               for(len=1; len<=(MAY-MIY)/2; len++) {
                  YZMinus(); if(meld) { meld=0; break; }
               }
               s=st; z=i; y=MIY+len-1;
            }
            st=SC(s,z,(i=SL(s,z))); if(!(x=i-st)) { x=MIX; r=2; }
            if(x-MAX1>=off) { off=((x-MAX1)/OFF1+1)*OFF1; SText(); }
            len=1; if(r==2) continue; if(x==MIX) break;
         }
         do {
            XMinus(); if(a[s] [z] [x+(r=Testen(stil))-1] != 32) len=1;
         } while(x>MIX && !(a[s] [z] [x+r-1] == 32 && len));
         if(a[s] [z] [x+r-1] == 32 && len) { XPlus(); break; }
         else if(x==MIX && a[s] [z] [x+r-1] !=32) break;
      }
   }
}
CDel(n)
int n;
{
   int i, st, len, r=0, e, durch=1;
   if(n==2) {    /* Alles links vom Crsr löschen */
      for(len=x; len>MIX; len--) BSpace();
   }
   else {
      st=SC(s,z,(i=SL(s,z)));
      if(x<=i-st) {
         r=Testen(stil);
         if(n==1 || (!n && a[s] [z] [x+r-1] != 32)) {
            if(x>MIX) {
               x--; r=Testen(stil); x++;
               if(!(a[s] [z] [x+r-2] == 32 && !n)) {
                  if(a[s] [z] [x+r-2] != 32 && (e=a[s] [z] [x+Testen(stil)-1]) != 32) n=0;
                  if(e!=32) e=0;
                  for(len=x; len>MIX; len--) {
                     BSpace();
                     if(--x<MIX) { x++; break; }
                     else {
                        r=Testen(stil);
                        if(a[s] [z] [x+r-1] == 32 ) {
                           if(--x<MIX || e) { if(e==33) e++; x+=2; }
                           else {
                              r=Testen(stil); x+=2;
                              if(a[s] [z] [x+r-3] != 32) break;
                           }
                        }
                        else {
                           x++;
                           if(e==32) e++;
                           else if(e==34) break;
                        }
                     }
                  }
               }
            }
         }
         if(n!=1) {
            st=SC(s,z,(i=SL(s,z)));
            r=Testen(stil); e=1;
            if(!n && a[s] [z] [x+r-1] == 32) { e=durch=0; }
            for(len=x, r=0; len<=i-st; len++) {
               if(n) {
                  if(a[s] [z] [x+Testen(stil)-1] != 32) break;
               }
               Del();
               if(!n) {
                  if(a[s] [z] [x+Testen(stil)-1] != 32) e=1;
                  else if(e) r=1;
                  if(r && e) {
                     if(len>i-st-2) break;
                     else {
                        if(durch==2) break;
                        x++; r=Testen(stil); x--;
                        if(a[s] [z] [x+r] != 32) {
                           if(durch) durch=2;
                           else break;
                        }
                        r=1;
                     }
                  }
               }
            }
         }
      }
      else if(n==1 && i-st>MIX) {
         r=st; x=MIX;
         for(len=i-st; len>=MIX; len--) {
            if(a[s] [z] [len+r-1] <9) r--;
            if(a[s] [z] [len+r-1] == 32) { x=len; break; }
         }
         for(len=x; len<=i-st; len++) Del();
      }
   }
}
Del()
{
   int i, r, len, rs=0, st;

   i=SL(s,z); st=SC(s,z,i);
   if(i-st>=x) {
      r=Testen(stil);
      if(i-st==x) {
         for(len=x+r-2; len>=0; len--) {
            if(a[s] [z] [len]<9)    { rs++; st--; }
            else if(a[s] [z] [len] == 32)  rs++;
            else break;
         }
         a[s] [z] [i-1-rs] = EOS;
      }
      else {
         if(r) { if(a[s] [z] [x+r-2]<9 && a[s] [z] [x+r]<9) { st--; rs=1; }}
         for(len=x+r; len<=i; len++) a[s] [z] [len-1-rs] = a[s] [z] [len];
      }
      BelAend(s, z, st, i-1-rs, i);
      i=i-1-rs; if((len=i-st-off)<0) len =0;
      SetAPen(RP, 0);
      RectFill(RP,len*8,y*8-8,RRAND,y*8-1);
      SetAPen(RP, 1);
      if(i-st>=x || x<=off) {
         if(st) SonderDruck(y, SL(s,z), x);
         else {
            a[s] [z] +=(x-1-off);
            NormalDruck(y, SL(s,z), x-1-off);
            a[s] [z] -=(x-1-off);
         }
      }
   }
}
StrEnde()
{
   x=SL(s,z); x=x-SC(s,z,x)+1;
   if(x>MAX) x=MAX;
   if(x<MIX) x=MIX;
}
SL(b,c)
int b,c;
{
   return((!a[b] [c] [0]) ? 0:strlen(a[b] [c]));
}
SC(b,c,d)
int b,c,d;
{
   return(a[b] [c] [d+1]);
}
