]> pd.if.org Git - mmurtl/commitdiff
autocommit for file dated 1995-02-09 15:56:34
authorRichard Burgess <>
Thu, 9 Feb 1995 15:56:34 +0000 (15:56 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Mon, 17 Oct 2016 14:03:48 +0000 (14:03 +0000)
msamples/editor/edit.c [new file with mode: 0644]

diff --git a/msamples/editor/edit.c b/msamples/editor/edit.c
new file mode 100644 (file)
index 0000000..cfb1d13
--- /dev/null
@@ -0,0 +1,1380 @@
+/* Edit.c  A simple editor using MMURTL file system and keyboard services\r
+   Use MakeIT.Bat to build this MMURTL samples application, or\r
+   use CM32 and DASM spearately:\r
+   CM32 Edit.c\r
+   DASM Edit.bat\r
+*/\r
+\r
+#define U32 unsigned long\r
+#define S32 long\r
+#define U16 unsigned int\r
+#define S16 int\r
+#define U8 unsigned char\r
+#define S8 char\r
+\r
+#define TRUE 1\r
+#define FALSE 0\r
+\r
+#include <stdio.h>\r
+#include <string.h>\r
+#include <ctype.h>\r
+\r
+/* Includes for OS public calls and structures */\r
+\r
+#include "\OSSOURCE\MKernel.h"\r
+#include "\OSSOURCE\MMemory.h"\r
+#include "\OSSOURCE\MData.h"\r
+#include "\OSSOURCE\MTimer.h"\r
+#include "\OSSOURCE\MVid.h"\r
+#include "\OSSOURCE\MKbd.h"\r
+#include "\OSSOURCE\MJob.h"\r
+#include "\OSSOURCE\MFiles.h"\r
+\r
+#define EDVID   BRITEWHITE|BGBLUE\r
+#define NORMVID WHITE|BGBLACK\r
+#define MARKVID WHITE|BGRED\r
+#define STATVID BLACK|BGCYAN\r
+\r
+#define EMPTY  99999\r
+#define NLINESMAX 26\r
+\r
+struct EditRecType {\r
+       U8      *pBuf;\r
+    U8         *pBufWork;                      /* For copy and move */\r
+       U32             Line[NLINESMAX];        /* Offset in buf for 1st char in line */\r
+       U32     iBufMax;                        /* sBuf - 1 */\r
+       U32     iColMin;                        /* Screen coords */\r
+       U32     iRowMin;\r
+       U32     iColMax;\r
+       U32     iRowMax;\r
+       U32     sLine;\r
+       U8      bSpace;\r
+       U8              fVisible;\r
+       U32     iAttrMark;\r
+       U32     iAttrNorm;\r
+       U32     iTabNorm;\r
+    U32     oBufLine0   /* oBufLine0 */\r
+       U32     iCol;           /* cursor, 0..sLine-1 */\r
+       U32     iLine;      /* cursor, 0..cLines-1 */\r
+       U32     oBufInsert; /* offset of next char in */\r
+       U32     oBufLast;   /* offset+1 of last char */\r
+       U32     oBufMark;\r
+       U32     oBufBound;\r
+       };\r
+\r
+struct EditRecType  EdRec;\r
+struct EditRecType  *pEdit;\r
+char *pBuf1, *pBuf2;\r
+unsigned char b, b1;\r
+long  erc, fh;\r
+char fModified;\r
+char fOvertype;\r
+char aStat[80];\r
+char aStat1[80];\r
+char aCmd[80];                 /* Get our command line */\r
+long cbCmd = 0;\r
+char *apParam[13];             /* Param 0 is cmd name */\r
+long acbParam[13];\r
+#define nParamsMax 13\r
+char Filename[60];\r
+long cbFilename;\r
+unsigned char filler[100];\r
+\r
+\r
+void clearbuf(void);   /* prototype for forward usage */\r
+\r
+/*********************************************************\r
+  Displays errors if they occur for certain file operations.\r
+*********************************************************/\r
+\r
+long CheckErc(long call, long erc)\r
+{\r
+char st[40];\r
+long i;\r
+\r
+       if (erc) {\r
+               FillData(st, 40, 0);\r
+               Beep();\r
+               switch (call) {\r
+               case 1:\r
+                       sprintf(st, "Error %05d occurred on OpenFile", erc);\r
+                       break;\r
+               case 2:\r
+                       sprintf(st, "Error %05d occurred on ReadBytes", erc);\r
+                       break;\r
+               case 3:\r
+                       sprintf(st, "Error %05d occurred on WriteBytes", erc);\r
+                       break;\r
+               case 4:\r
+                       sprintf(st, "Error %05d occurred on CreateFile", erc);\r
+                       break;\r
+               case 5:\r
+                       sprintf(st, "Error %05d occurred on SetFileSize", erc);\r
+                       break;\r
+               case 6:\r
+                       sprintf(st, "Error %05d occurred on SetFileLFA", erc);\r
+                       break;\r
+               case 7:\r
+                       sprintf(st, "Error %05d occurred on ReadKbd", erc);\r
+                       break;\r
+               default:\r
+                       sprintf(st, "Error %05d occurred on last command", erc);\r
+                       break;\r
+               }\r
+               for (i=0; i<40; i++)\r
+                       if (!st[i])\r
+                               st[i] = ' ';\r
+\r
+           PutVidChars (40, 24, st, 39, STATVID);\r
+       }\r
+    return (erc);\r
+}\r
+\r
+/*********************************************************\r
+  Clears the status line with 80 blank chars.\r
+*********************************************************/\r
+void ClearStatus(void)\r
+{\r
+char st[80];\r
+       FillData(st, 80, 0);\r
+    PutVidChars (0, 24, st, 80, NORMVID);\r
+}\r
+\r
+\r
+/*************************************************************\r
+   Saves a file you are editing. If fPrompt is true, this\r
+   will prompt you to save. If fClose is true, the file will be \r
+   closed and the buffer will be closed.\r
+**************************************************************/\r
+\r
+void SaveFile(int fPrompt, int fClose)\r
+{\r
+U32 i, keycode, fYes;\r
+unsigned char  *pBuff;\r
+\r
+  pBuff = pEdit->pBuf;\r
+\r
+       if ((fh) && (fModified))\r
+       {\r
+               if (pEdit->fVisible)\r
+               {    /* fix visible characters */\r
+                    for (i=0; i <=pEdit->iBufMax; i++)\r
+                        if (pBuff[i] == 0x07)\r
+                                 pBuff[i] = 0x20;\r
+                 pEdit->fVisible = FALSE;\r
+           }\r
+        fYes = 1;\r
+               if (fPrompt)\r
+               {\r
+                       ClearStatus();\r
+                       SetXY(0, 24);\r
+                   TTYOut("This file has been modified. SAVE IT? (Y/N)", 43, BLACK|BGCYAN);\r
+                       ReadKbd(&keycode, 1);\r
+                       if (((keycode & 0xff) == 'N') || ((keycode & 0xff) == 'n'))\r
+                       {\r
+                               fYes = 0;\r
+                               ClearStatus();\r
+                       }\r
+               }\r
+\r
+               if (fYes)\r
+               {\r
+                       erc = CheckErc(6, SetFileLFA(fh, 0));\r
+                       if (!erc)\r
+                               erc = CheckErc(5, SetFileSize(fh,\r
+                                                       pEdit->oBufLast));\r
+                       if (!erc)\r
+                               erc = CheckErc(3, WriteBytes (fh, pBuf1,\r
+                                                          pEdit->oBufLast, &i));\r
+                       fModified = 0;\r
+                       ClearStatus();\r
+                   PutVidChars (0, 24, "DONE...   ", 10, STATVID);\r
+                       Sleep(150);\r
+                       ClearStatus();\r
+               }\r
+\r
+       }\r
+\r
+       if (fh && fClose)\r
+       {\r
+               CloseFile(fh);\r
+               fh = 0;\r
+               cbFilename = 0;\r
+               clearbuf();\r
+       }\r
+}\r
+\r
+/*************************************************************\r
+   This prompts for a filename to open and opens it if it\r
+   exists. If not, it will prompts to create.\r
+**************************************************************/\r
+\r
+void OpenAFile(char *name)\r
+{\r
+U32 filesize, dret, keycode;\r
+\r
+       erc = 0;\r
+       cbFilename = 0;\r
+       if (!name)\r
+       {\r
+               SetXY(0,24);\r
+           PutVidChars (0,24, "Filename: ", 10, BLACK|BGWHITE);\r
+               SetXY(10,24);\r
+               EditLine(Filename, 0, 60, &cbFilename, &b1, BLACK|BGCYAN);\r
+               SetXY(0,0);\r
+       }\r
+       else\r
+       {\r
+           b1=0x0d;\r
+           strncpy(Filename, name, 13);\r
+           cbFilename = strlen(Filename);\r
+       }\r
+       if ((b1==0x0d) && (cbFilename)) {\r
+               erc = OpenFile(Filename, cbFilename, ModeModify, 1, &fh);\r
+               if (!erc) {\r
+                       GetFileSize(fh, &filesize);\r
+                       if (filesize < 131000)  /* Buf is 131071 */\r
+                       {\r
+                               erc = ReadBytes (fh, pBuf1, filesize, &dret);\r
+                               if (erc > 1)\r
+                                       erc = CheckErc(2, erc);\r
+                               else erc = 0;\r
+                           pEdit->oBufLast     = dret;      /* offset+1 of last char  */\r
+                               pBuf1[pEdit->oBufLast] = 0x0F;          /* the SUN */\r
+                       }\r
+                       else\r
+                       {\r
+                               CloseFile(fh);\r
+                               fh = 0;\r
+                               Beep();\r
+                               SetXY(50, 24);\r
+                           TTYOut("File is too large to edit.", 26, BLACK|BGCYAN);\r
+                               ReadKbd(&keycode, 1);\r
+                       }\r
+               }\r
+               else if (erc == 203) {  /* no such file */\r
+                       Beep();\r
+                       SetXY(50, 24);\r
+                   TTYOut("Doesn't exist. Create?? (Y/N)", 29, BLACK|BGCYAN);\r
+                       ReadKbd(&keycode, 1);\r
+                       if (((keycode & 0xff) == 'Y') || ((keycode & 0xff) == 'y')) {\r
+                               erc = CheckErc(4, CreateFile(Filename, cbFilename, 0));\r
+\r
+                               if (!erc)\r
+                                       erc = CheckErc(1, OpenFile(Filename, cbFilename,\r
+                                                                       ModeModify, 1, &fh));\r
+\r
+                               if (erc) {\r
+                                       fh = 0;\r
+                                       cbFilename = 0;\r
+                               }\r
+\r
+                       }\r
+                       else {\r
+                               cbFilename = 0;\r
+                               ClearStatus();\r
+                       }\r
+               }\r
+               else\r
+                       CheckErc(1, erc);\r
+       }\r
+\r
+       if (!erc)\r
+               ClearStatus();\r
+}\r
+\r
+/************************************************************\r
+   This counts ABSOLUTE LINES from the begining of the buffer\r
+   up to point of oBufLine0 (which is the first char displayed\r
+   in the window. ABSOLUTE means LFs were found, even though\r
+   we word wrap always.\r
+*************************************************************/\r
+\r
+unsigned long CountEols (void)\r
+{\r
+unsigned long  nEols, i;\r
+unsigned char  *pBuff;\r
+\r
+  pBuff = pEdit->pBuf;\r
+  nEols = 0;\r
+  i = 0;\r
+\r
+  while (i < pEdit->oBufLine0) /* count LFs */\r
+       if (pBuff[i++] == 0x0A)\r
+               nEols++;\r
+\r
+  return(nEols);\r
+}\r
+\r
+\r
+\r
+/************************************************************\r
+   This returns the index to the the last character in a line\r
+   upto        a maximum of sLine-1. iBuf points to the beginning\r
+   point in the buffer to find the end of line for.\r
+*************************************************************/\r
+\r
+unsigned long findEol (unsigned long iBuf)\r
+{\r
+\r
+unsigned long  iEol, iEolMax;\r
+unsigned char  *pBuff;\r
+\r
+  pBuff = pEdit->pBuf;\r
+\r
+       /* Calculate the most it could be */\r
+\r
+  iEolMax = iBuf + pEdit->sLine-1;\r
+\r
+       /* Fix it if EOL is past end of data */\r
+\r
+  if (iEolMax > pEdit->oBufLast)\r
+     iEolMax = pEdit->oBufLast;\r
+\r
+  iEol = iBuf;\r
+  while ((pBuff[iEol] != 0x0A) && (iEol < iEolMax)) /* Find CR */\r
+    iEol++;\r
+  if ((iEol == iEolMax) && (pBuff[iEol] != 0x0A)) {  /* if no CR... */\r
+    iEol = iEolMax;\r
+    if (iEolMax < pEdit->oBufLast) {\r
+\r
+      /* now work back to last space */\r
+      while ((pBuff[iEol] != pEdit->bSpace) && (iEol > iBuf))\r
+         iEol--;\r
+\r
+      /*  now find first non-space - allows */\r
+      if ((iEol > iBuf) &&\r
+          (pBuff[iEol] == pEdit->bSpace) &&  /* wrap-around w/ double space */\r
+          (iEol == iEolMax)) {\r
+\r
+        if ((pBuff[iEol-1] == pEdit->bSpace) ||\r
+            ((iEol == iEolMax) && (pBuff[iEol+1] == pEdit->bSpace))) {\r
+               while ((pBuff[iEol] == pEdit->bSpace) && (iEol > iBuf))\r
+               iEol--;\r
+               while ((pBuff[iEol] != pEdit->bSpace) && (iEol > iBuf))\r
+               iEol--;\r
+        }\r
+         }\r
+      if ((iEol == iBuf) &&\r
+          (pBuff[iBuf] > 0) &&\r
+          (pBuff[iEolMax] > 0))        /* handles "all-char" of full line */\r
+        iEol = iEolMax;\r
+    }\r
+  }\r
+  return(iEol);\r
+}\r
+\r
+/************************************************************\r
+   This walks back through the buffer looking for the\r
+   logical end of a line.\r
+*************************************************************/\r
+\r
+\r
+unsigned long findPrevLine (unsigned long oBufStart)\r
+{\r
+unsigned long  i, j;\r
+char *pBuff;\r
+\r
+  pBuff = pEdit->pBuf;\r
+\r
+  i = 0;\r
+  if (oBufStart)\r
+        i = oBufStart - 1;\r
+  while ((i) && (pBuff[i] != 0x0A))\r
+        i--;\r
+  if (i > 0)\r
+       i--;\r
+  while ((i > 0) && (pBuff[i] != 0x0A))\r
+        i--;\r
+  if (i)\r
+       i++;                    /*  Get to known start of line */\r
+  do {\r
+       j = i;\r
+       i = (findEol (j)) + 1;\r
+  }\r
+  while (i < oBufStart);\r
+  return(j);\r
+}\r
+\r
+/************************************************************\r
+   This executes the BEGIN BLOCK (Mark) command.\r
+*************************************************************/\r
+\r
+\r
+void doMark (unsigned long iLn)\r
+{\r
+unsigned long  iColStart, iColFinish, iMarkLoc, iBoundLoc;\r
+\r
+  if (pEdit->oBufMark < EMPTY) {\r
+      if (pEdit->oBufMark <= pEdit->oBufBound) {\r
+      iMarkLoc = pEdit->oBufMark;\r
+      iBoundLoc = pEdit->oBufBound;\r
+       }\r
+       else {\r
+      iMarkLoc = pEdit->oBufBound;\r
+      iBoundLoc = pEdit->oBufMark;\r
+       }\r
+    if ( ((iMarkLoc >= pEdit->Line[iLn]) && (iMarkLoc < pEdit->Line[iLn+1]))   ||\r
+         ((iBoundLoc >= pEdit->Line[iLn]) && (iBoundLoc < pEdit->Line[iLn+1])) ||\r
+         ((iMarkLoc < pEdit->Line[iLn]) && (iBoundLoc >= pEdit->Line[iLn+1])) )\r
+       {\r
+      if (iMarkLoc >= pEdit->Line[iLn])\r
+        iColStart = pEdit->iColMin + iMarkLoc - pEdit->Line[iLn];\r
+      else\r
+        iColStart = pEdit->iColMin;\r
+\r
+      if (iBoundLoc < pEdit->Line[iLn+1])\r
+         iColFinish = pEdit->iColMin + iBoundLoc - pEdit->Line[iLn];\r
+      else\r
+         iColFinish = pEdit->iColMin + pEdit->Line[iLn+1] - pEdit->Line[iLn] - 1;\r
+\r
+      if (iColStart > pEdit->iColMin)\r
+          PutVidAttrs (pEdit->iColMin,\r
+                               iLn,\r
+                               iColStart-pEdit->iColMin,\r
+                               pEdit->iAttrNorm);\r
+      PutVidAttrs (iColStart, iLn,     iColFinish - iColStart +1, pEdit->iAttrMark);\r
+\r
+      if (iColFinish < pEdit->iColMax)\r
+        PutVidAttrs (iColFinish+1,\r
+                         iLn,\r
+                         pEdit->iColMax - iColFinish,\r
+                         pEdit->iAttrNorm);\r
+    }\r
+    else   /*buf col*/\r
+      PutVidAttrs (pEdit->iColMin,\r
+                       iLn,\r
+                       pEdit->sLine,\r
+                       pEdit->iAttrNorm);\r
+  }\r
+}\r
+\r
+\r
+/************************************************************\r
+   This inserts data into the main editing buffer.\r
+*************************************************************/\r
+\r
+\r
+char putInBuf( unsigned char bPutIn,\r
+                               char fOvertype,\r
+                char fSpecInsert)\r
+{\r
+unsigned long  cb;\r
+char  fOK, *pBuff;\r
+\r
+       fModified = 1;\r
+       pBuff = pEdit->pBuf;\r
+\r
+  if ((pEdit->oBufInsert < pEdit->iBufMax) &&\r
+    ((pEdit->oBufLast < pEdit->iBufMax) ||\r
+     ((fOvertype) && (!fSpecInsert))))\r
+  {\r
+    fOK = 1;\r
+    if ((fOvertype) && (!fSpecInsert)) {\r
+      pBuff[pEdit->oBufInsert] = bPutIn;\r
+      if (pEdit->oBufLast == pEdit->oBufInsert)\r
+         pEdit->oBufLast++;\r
+      pEdit->oBufInsert++;\r
+    }\r
+    else {\r
+      cb = pEdit->oBufLast - pEdit->oBufInsert + 1;\r
+      CopyData (&pBuff[pEdit->oBufInsert], pEdit->pBufWork, cb);\r
+      pBuff[pEdit->oBufInsert] = bPutIn;\r
+      CopyData (pEdit->pBufWork, &pBuff[pEdit->oBufInsert+1], cb);\r
+      pEdit->oBufLast++;\r
+      pEdit->oBufInsert++;\r
+      if (pEdit->oBufMark < EMPTY) {\r
+        if (pEdit->oBufInsert-1 < pEdit->oBufMark)\r
+          pEdit->oBufMark++;\r
+        if (pEdit->oBufInsert-1 <= pEdit->oBufBound)\r
+          pEdit->oBufBound++;\r
+      }\r
+    }\r
+  }\r
+  else {\r
+       fOK = 0;\r
+    Beep();\r
+    erc = 40400;\r
+  }\r
+  return (fOK);\r
+}\r
+\r
+/************************************************************\r
+   This executes the MOVE command which moves a marked\r
+   block to the cursor's current location in the file.\r
+*************************************************************/\r
+\r
+void moveData (void)\r
+{\r
+unsigned long  i, iMk, iBd;\r
+char *pBuff, *pBuffWork;\r
+\r
+       pBuff = pEdit->pBuf;\r
+       pBuffWork = pEdit->pBufWork;\r
+\r
+if (pEdit->oBufMark < EMPTY) {\r
+       fModified = 1;\r
+       if (pEdit->oBufMark <= pEdit->oBufBound) {\r
+           iMk = pEdit->oBufMark;\r
+           iBd = pEdit->oBufBound;\r
+       }\r
+       else {\r
+           iBd = pEdit->oBufMark;\r
+           iMk = pEdit->oBufBound;\r
+       }\r
+       if ((pEdit->oBufInsert < iMk) || (pEdit->oBufInsert > iBd)) {\r
+               for (i=0; i <= pEdit->oBufLast; i++)\r
+               pBuffWork[i] = pBuff[i];\r
+           if (pEdit->oBufInsert < iMk) {\r
+         for (i=0; i<=iBd-iMk; i++)                /* Move mk/bd */\r
+                pBuff[pEdit->oBufInsert+i] = pBuffWork[iMk+i];\r
+         for (i=0; i<=iMk - pEdit->oBufInsert - 1; i++) /* Shift overwritten ahead */\r
+                       pBuff[pEdit->oBufInsert+iBd-iMk+1+i] =\r
+               pBuffWork[pEdit->oBufInsert+i];\r
+       }\r
+       if (pEdit->oBufInsert > iBd) {\r
+               for (i=0; pEdit->oBufInsert - iBd - 1; i++)\r
+                       pBuff[iMk+i] = pBuffWork[iBd+1+i];\r
+               pEdit->oBufInsert = pEdit->oBufInsert - iBd + iMk - 1;\r
+               for (i=0; i <=iBd-iMk; i++)\r
+                       pBuff[pEdit->oBufInsert+i] = pBuffWork[iMk+i];\r
+       }\r
+       iBd = pEdit->oBufInsert + iBd - iMk;\r
+       iMk = pEdit->oBufInsert;\r
+       if (pEdit->oBufBound > pEdit->oBufMark) {\r
+               pEdit->oBufBound = iBd;\r
+               pEdit->oBufMark = iMk;\r
+       }\r
+       else {\r
+               pEdit->oBufMark = iBd;\r
+               pEdit->oBufBound = iMk;\r
+       }\r
+       }\r
+}\r
+ else Beep();\r
+}\r
+\r
+/************************************************************\r
+   This executes the COPY command which copies a marked\r
+   block to the cursor's current location in the file.\r
+*************************************************************/\r
+\r
+void CopyIt (void)\r
+{\r
+unsigned long iMk, iBd;\r
+char *pBuff, *pBuffWork;\r
+\r
+       pBuff = pEdit->pBuf;\r
+       pBuffWork = pEdit->pBufWork;\r
+\r
+if (pEdit->oBufMark < EMPTY) {\r
+       fModified = 1;\r
+\r
+  if (pEdit->oBufMark <= pEdit->oBufBound) {\r
+    iMk = pEdit->oBufMark;\r
+    iBd = pEdit->oBufBound;\r
+  } else {\r
+    iBd = pEdit->oBufMark;\r
+    iMk = pEdit->oBufBound;\r
+  }\r
+  if (pEdit->oBufLast+iBd-iMk+1 < pEdit->iBufMax) {\r
+       CopyData(pBuff, pBuffWork, pEdit->oBufLast+1);\r
+       CopyData(&pBuffWork[iMk], &pBuff[pEdit->oBufInsert], iBd-iMk+1);\r
+    if (pEdit->oBufLast >= pEdit->oBufInsert)\r
+       CopyData(&pBuffWork[pEdit->oBufInsert],\r
+                &pBuff[pEdit->oBufInsert+iBd-iMk+1],\r
+                pEdit->oBufLast - pEdit->oBufInsert+1);\r
+    iBd = pEdit->oBufInsert + iBd - iMk;\r
+    iMk = pEdit->oBufInsert;\r
+    pEdit->oBufInsert = pEdit->oBufInsert + iBd - iMk + 1;\r
+    pEdit->oBufLast = pEdit->oBufLast + iBd - iMk + 1;\r
+    if (pEdit->oBufBound > pEdit->oBufMark) {\r
+      pEdit->oBufBound = iBd;\r
+      pEdit->oBufMark = iMk;\r
+    }\r
+    else {\r
+      pEdit->oBufMark = iBd;\r
+      pEdit->oBufBound = iMk;\r
+    }\r
+  }\r
+}\r
+else Beep();\r
+}\r
+\r
+/************************************************************\r
+  This sets the characters on the screen to normal attributes\r
+*************************************************************/\r
+\r
+void normAttr (void)\r
+{\r
+unsigned long  i;\r
+\r
+  for (i = pEdit->iRowMin; i <= pEdit->iRowMax; i++)\r
+     PutVidAttrs (pEdit->iColMin, i, pEdit->sLine, pEdit->iAttrNorm);\r
+}\r
+\r
+/************************************************************\r
+   This unmarks a selected block. (hides it).\r
+*************************************************************/\r
+\r
+void nullMarkBound (void)\r
+{\r
+  pEdit->oBufMark = EMPTY;\r
+  pEdit->oBufBound = EMPTY;\r
+  normAttr ();\r
+}\r
+\r
+/************************************************************\r
+   This DELETES a selected block.\r
+*************************************************************/\r
+\r
+void deleteData (void)\r
+{\r
+unsigned long  i, iMk, iBd;\r
+char  fProb;\r
+char *pBuff, *pBuffWork;\r
+\r
+  pBuff = pEdit->pBuf;\r
+  pBuffWork = pEdit->pBufWork;\r
+\r
+if (pEdit->oBufMark < EMPTY) {\r
+  fModified = 1;\r
+  if (pEdit->oBufMark <= pEdit->oBufBound)  {\r
+    iMk = pEdit->oBufMark;\r
+    iBd = pEdit->oBufBound;\r
+  } else {\r
+    iBd = pEdit->oBufMark;\r
+    iMk = pEdit->oBufBound;\r
+  }\r
+  if ((pEdit->oBufLine0 >= iMk) && (pEdit->oBufLine0 <= iBd))\r
+     fProb = TRUE;\r
+  else fProb = FALSE;\r
+  CopyData(&pBuff[iBd+1], &pBuff[iMk], pEdit->oBufLast-iBd);\r
+  pEdit->oBufLast = pEdit->oBufLast - iBd + iMk - 1;\r
+  if (pEdit->oBufInsert > iBd)\r
+         pEdit->oBufInsert = pEdit->oBufInsert - iBd + iMk;\r
+  else if ((pEdit->oBufInsert > iMk) && (pEdit->oBufInsert <= iBd))\r
+      pEdit->oBufInsert = iMk;\r
+  if (pEdit->oBufInsert > pEdit->oBufLast)\r
+     pEdit->oBufInsert = pEdit->oBufLast;\r
+  if (fProb)  {\r
+    i = findPrevLine (pEdit->oBufInsert);\r
+    pEdit->oBufLine0 = i;\r
+  }\r
+  nullMarkBound ();\r
+}\r
+}\r
+\r
+/************************************************************\r
+   After screen movement (such as scrolling), this\r
+   finds the proper location of the cursor in relationship\r
+   to the portion of the file currently displayed.\r
+*************************************************************/\r
+\r
+void findCursor (void)\r
+{\r
+       /* locates cursor based on oBufInsert\r
+          - might be off screen - if it is, this\r
+       will adjust screen */\r
+\r
+unsigned long  i, j;\r
+\r
+  i = pEdit->iRowMin;\r
+  while ((i <= pEdit->iRowMax) && (pEdit->oBufInsert >= pEdit->Line[i]))\r
+       i++;\r
+  pEdit->iLine = i - 1;\r
+  if (pEdit->iLine < pEdit->iRowMin)\r
+     pEdit->iLine = pEdit->iRowMin;\r
+\r
+  j = pEdit->iLine;\r
+  if ((pEdit->Line[j+1] < EMPTY) &&\r
+      (pEdit->oBufInsert >= pEdit->Line[j+1]))\r
+       pEdit->iLine = pEdit->iLine + 1;\r
+  j = pEdit->iLine;\r
+  pEdit->iCol = pEdit->oBufInsert - pEdit->Line[j] + pEdit->iColMin;\r
+  if (pEdit->iLine > pEdit->iRowMax + 1)\r
+     pEdit->iLine = pEdit->iRowMax;\r
+}\r
+\r
+/************************************************************\r
+   This readjusts iCol & iLine to get them back in sync with\r
+   oBufInsert if oBufInsert is on screen.  If oBufInsert is\r
+   not onscreen, this makes it so it is!\r
+*************************************************************/\r
+\r
+void coordCursor_oBuf (void)\r
+{\r
+unsigned long  oBuf, i;\r
+\r
+  i = pEdit->iRowMax+1;\r
+  if ((pEdit->oBufInsert >= pEdit->oBufLine0) &&\r
+      (pEdit->oBufInsert < pEdit->Line[i]))  {\r
+\r
+    /* if bogus line, guarantee end of good */\r
+\r
+       i = pEdit->iLine;\r
+    if (pEdit->Line[i] == EMPTY)\r
+       pEdit->iCol = pEdit->iColMax;\r
+\r
+     /* if bogus line, find last good line */\r
+\r
+    while ((pEdit->Line[i] == EMPTY) &&\r
+           (i > pEdit->iRowMin)) {\r
+        pEdit->iLine--;\r
+               i = pEdit->iLine;\r
+       }\r
+\r
+       i = pEdit->iLine;\r
+    pEdit->oBufInsert =\r
+       pEdit->Line[i] + pEdit->iCol - pEdit->iColMin;\r
+    if (pEdit->oBufInsert > pEdit->oBufLast)\r
+       pEdit->oBufInsert = pEdit->oBufLast;\r
+    oBuf = pEdit->Line[i+1];\r
+    if (pEdit->oBufInsert > oBuf)\r
+       pEdit->oBufInsert = oBuf;   /* get to potential insert - is, if\r
+                                          prev char <> CR, is not if prev = CR */\r
+    if (pEdit->oBufInsert == oBuf)            /* if at EOL */\r
+       if (pEdit->pBuf[oBuf-1] == 0x0A)\r
+         pEdit->oBufInsert--;\r
+    pEdit->iCol = pEdit->oBufInsert + pEdit->iColMin -\r
+                         pEdit->Line[i];\r
+  }\r
+}\r
+\r
+\r
+/************************************************************\r
+ Adjusts oBufLine0 to make sure that oBufInsert is on the screen.\r
+ This also sets all of the Line array values (Line[n])\r
+*************************************************************/\r
+\r
+void makeOnScreen (void)\r
+{\r
+unsigned long  i, j, k;\r
+\r
+  /* If oBufInsert is not on screen (above current display)\r
+     then find the previous line beginning and make that\r
+     the new first line 1 until it is!\r
+  */\r
+\r
+  while (pEdit->oBufInsert < pEdit->oBufLine0)\r
+       pEdit->oBufLine0 = findPrevLine (pEdit->oBufLine0);\r
+\r
+  /* Set Line[iRowMin] to match oBufLine0 */\r
+\r
+  k = pEdit->iRowMin;\r
+  pEdit->Line[k] = pEdit->oBufLine0;\r
+\r
+  /* Set all subsequent Line[s] by calling findEol for each. */\r
+\r
+  for (i = k; i <= pEdit->iRowMax; i++) {\r
+    if (pEdit->Line[i] < EMPTY) {\r
+      j = findEol (pEdit->Line[i]);\r
+      if (j < pEdit->oBufLast)       /* j = offset of last char of line */\r
+        pEdit->Line[i+1] = j + 1;\r
+      else\r
+       for (j=i+1; j<NLINESMAX; j++)\r
+                       pEdit->Line[j] = EMPTY;\r
+       }\r
+  }\r
+\r
+       /* If the InsertPoint (your cursor position) is past\r
+       the last line then do this junk to fix it */\r
+\r
+  j = pEdit->iRowMin;\r
+  k = pEdit->iRowMax;\r
+  while (pEdit->oBufInsert >= pEdit->Line[k+1]) {\r
+    for (i=j; i<=k; i++)\r
+      pEdit->Line[i] = pEdit->Line[i+1];\r
+    pEdit->oBufLine0 = pEdit->Line[j];\r
+\r
+    i = findEol (pEdit->Line[k]);                /* EOL of iRowMax */\r
+    if (i < pEdit->oBufLast)              /* i = offset of last char of line */\r
+       pEdit->Line[k+1] = i + 1;\r
+    else pEdit->Line[k+1] = EMPTY;\r
+  }\r
+}\r
+\r
+/************************************************************\r
+ Redisplay all data on the screen.\r
+*************************************************************/\r
+\r
+void showScreen (char *pFiller)\r
+{\r
+unsigned long i, iLn, cb, oBuf;\r
+char *pBuff;\r
+\r
+       pBuff = pEdit->pBuf;\r
+\r
+       makeOnScreen ();    /* oBufInsert on screen - Line correct */\r
+\r
+       for (iLn = pEdit->iRowMin; iLn <= pEdit->iRowMax; iLn++) {\r
+\r
+               /* i = offset in buf of last char on line */\r
+               /* cb = nchars in line */\r
+\r
+               cb = 0;\r
+               oBuf = pEdit->Line[iLn];\r
+               if (oBuf < EMPTY) {\r
+                       if (pEdit->Line[iLn+1] < EMPTY)\r
+                               i = pEdit->Line[iLn+1] - 1;\r
+                       else\r
+                       i = pEdit->oBufLast;\r
+                   cb = i - oBuf + 1;          /* Make size, not offset */\r
+\r
+\r
+                   if ((!pEdit->fVisible) &&\r
+                       (pBuff[i] == 0x0A) &&\r
+                       (cb))\r
+                               cb--;\r
+               }\r
+\r
+               if ((cb) && (oBuf < EMPTY))\r
+                       PutVidChars (pEdit->iColMin, iLn, pBuff+oBuf, cb,\r
+                                         pEdit->iAttrNorm);\r
+\r
+               if (cb < pEdit->sLine)\r
+                       PutVidChars (pEdit->iColMin+cb, iLn, pFiller, pEdit->sLine-cb,\r
+                                         pEdit->iAttrNorm);\r
+\r
+               doMark (iLn);\r
+       }\r
+}\r
+\r
+/************************************************************\r
+ Resets all variables for the editor and clears the\r
+ buffers. Called before use and after closure.\r
+*************************************************************/\r
+\r
+void clearbuf (void)\r
+{\r
+unsigned long i;\r
+char *pBuff;\r
+\r
+       pBuff = pEdit->pBuf;\r
+       FillData(filler, 80, 0x20);\r
+       for (i=0; i<NLINESMAX; i++)\r
+       pEdit->Line[i] = EMPTY;\r
+       i = pEdit->iRowMin;\r
+       pEdit->Line[i] = 0;\r
+\r
+    pEdit->iCol = pEdit->iColMin;\r
+    pEdit->iLine = pEdit->iRowMin;\r
+       pEdit->bSpace = 0x20;\r
+       pEdit->fVisible = FALSE;\r
+       fModified = 0;\r
+       fOvertype = FALSE;\r
+       pEdit->oBufLast  = 0;\r
+    pEdit->oBufInsert = 0;\r
+    pEdit->oBufLine0 = 0;\r
+       pBuff[pEdit->oBufLast] = 0x0F;          /* the SUN */\r
+       nullMarkBound();\r
+       normAttr ();\r
+}\r
+\r
+/************************************************************\r
+ This is the main editing function. It is a HUGE while\r
+ loop which reads keystrokes and processes them.\r
+*************************************************************/\r
+\r
+void Editor(char *pbExitRet)\r
+{\r
+unsigned long  i, j, k, key;\r
+char  fSpecInsert;     /* TRUE = insert no matter what fOvertype is */\r
+char  fScreen;            /* TRUE = display entire screen */\r
+char  fDone;\r
+unsigned char b;\r
+char *pBuff, *pBuffWork;\r
+long exch;\r
+\r
+char testkey[10];\r
+\r
+  pBuff = pEdit->pBuf;\r
+  pBuffWork = pEdit->pBufWork;\r
+  fDone = FALSE;\r
+\r
+  if (pEdit->fVisible)\r
+  {\r
+    pEdit->bSpace = 0x07;\r
+    for (i=0; i <=pEdit->oBufLast; i++)\r
+      if (pBuff[i] == 0x20)\r
+         pBuff[i] = pEdit->bSpace;\r
+  } else\r
+       pEdit->bSpace = 0x20;\r
+\r
+\r
+  normAttr ();\r
+  fScreen = TRUE;\r
+\r
+  pBuff[pEdit->oBufLast] = 0x0F;               /* the SUN */\r
+\r
+  erc = AllocExch(&exch);\r
+\r
+  while (!fDone)\r
+  {\r
+    if (fScreen)\r
+    {\r
+      showScreen (filler);    /* we know oBufInsert on screen */\r
+      findCursor ();\r
+      fScreen = FALSE;\r
+    }\r
+    SetXY (pEdit->iCol, pEdit->iLine);\r
+       FillData(aStat, 80, 0x20);\r
+       i= CountEols() + pEdit->iLine;\r
+       sprintf(aStat,"C: %02d  L: %05d  nChars: %05d",\r
+                       pEdit->iCol, i, pEdit->oBufLast);\r
+       if (cbFilename)\r
+               CopyData(Filename, &aStat[40], cbFilename);\r
+       if (fOvertype)\r
+               CopyData("OVR", &aStat[77], 3);\r
+       else\r
+               CopyData("INS", &aStat[77], 3);\r
+    PutVidChars (0, 0, aStat, 80, STATVID);\r
+\r
+    fSpecInsert = FALSE;  /* True if char should be inserted even if fOver */\r
+\r
+    CheckErc(7, ReadKbd (&key, 1));      /* Wait for char */\r
+\r
+    b = key & 0xff;\r
+\r
+       if (key & 0x3000)               /* ALT key is down */\r
+       {\r
+               switch (b)\r
+               {\r
+\r
+               case 0x42:              /* ALT-B -- Beginning of Text */\r
+               case 0x62:              /* ALT-b  */\r
+                   pEdit->oBufLine0 = 0;\r
+                   pEdit->oBufInsert = 0;\r
+                   pEdit->iCol = pEdit->iColMin;\r
+                   pEdit->iLine = pEdit->iRowMin;\r
+                   fScreen = TRUE;\r
+                                break;\r
+\r
+               case 0x43:              /* ALT-C -- CloseFile */\r
+               case 0x63:              /* ALT-c  */\r
+                SaveFile(TRUE, TRUE);\r
+                       fScreen = TRUE;\r
+                               break;\r
+               case 0x45:              /* ALT-E -- End of Text */\r
+               case 0x65:              /* ALT-e  */\r
+                   pEdit->oBufInsert = pEdit->oBufLast;\r
+                   coordCursor_oBuf ();\r
+                                  fScreen = TRUE;\r
+                                break;\r
+               case 0x01:              /* ALT-UP Arrow - Cursor to top of screen */\r
+                   pEdit->iLine = pEdit->iRowMin;\r
+                   pEdit->iCol = pEdit->iColMin;\r
+                                break;\r
+               case 0x02:      /* ALT Down Arrow - Cursor to bottom of screen */\r
+                   pEdit->iLine = pEdit->iRowMin;\r
+                                  i = pEdit->iLine;\r
+                   while ((pEdit->Line[i+1] < EMPTY) &&\r
+                          (i < pEdit->iRowMax)) {\r
+                       pEdit->iLine++;\r
+                                          i = pEdit->iLine;\r
+                                  }\r
+                   pEdit->iCol = pEdit->iColMax;\r
+                   coordCursor_oBuf ();\r
+                                break;\r
+               case 0x03:              /* ALT-Left Arrow - Cursor to BOL */\r
+                   pEdit->iCol = pEdit->iColMin;\r
+                                break;\r
+               case 0x04:              /* ALT-Right Arrow - Cursor to EOL */\r
+                                  i = pEdit->iLine;\r
+                   if (pEdit->Line[i+1] < EMPTY) {\r
+                      pEdit->iCol =\r
+                       pEdit->iColMin +\r
+                       pEdit->Line[i+1] -\r
+                       pEdit->Line[i];\r
+                       i = pEdit->iLine+1;\r
+                      if ((pBuff[pEdit->Line[i]-1] == 0x0A)\r
+                         && (pEdit->iCol > pEdit->iColMin))\r
+                       pEdit->iCol--;\r
+                   }\r
+                   else pEdit->iCol = pEdit->iColMin +\r
+                               pEdit->oBufLast - pEdit->Line[i];\r
+                                break;\r
+\r
+               case 0x56:              /* ALT-V   Make nontext chars Visible/Invisible */\r
+               case 0x76:              /* ALT-v  */\r
+                   if (pEdit->fVisible)  {\r
+                     for (i=0; i <= pEdit->oBufLast; i++)\r
+                          if (pBuff[i] == 0x07)\r
+                               pBuff[i] = 0x20;\r
+                     pEdit->fVisible = FALSE;\r
+                     pEdit->bSpace = 0x20;\r
+                   } else {\r
+                     for (i=0; i<=pEdit->oBufLast; i++)\r
+                          if (pBuff[i] == 0x20)\r
+                               pBuff[i] = 0x07;\r
+                     pEdit->fVisible = TRUE;\r
+                     pEdit->bSpace = 0x07;\r
+                   }\r
+                   fScreen = TRUE;\r
+                               break;\r
+               case 0x4F:              /* ALT-O   OpenFile */\r
+               case 0x6F:              /* ALT-o  */\r
+                               if (!fh) {\r
+                                       clearbuf();\r
+                                       OpenAFile(0);\r
+                    fScreen = TRUE;\r
+                               }\r
+                               break;\r
+               case 0x51:      /* ALT Q - Quit */\r
+               case 0x71:\r
+                SaveFile(TRUE, TRUE);\r
+                fDone = TRUE;\r
+                               break;\r
+               case 0x53:              /* ALT S - SaveFile */\r
+               case 0x73:              /* ALT s  */\r
+                SaveFile(FALSE, FALSE);\r
+                       fScreen = TRUE;\r
+                               break;\r
+               case 0x7F:              /* ALT Delete (Delete Marked Block)  */\r
+                       if (pEdit->oBufMark < EMPTY)  {\r
+                   deleteData ();\r
+                   fScreen = TRUE;\r
+                 }\r
+                                break;\r
+                       default:\r
+                               break;\r
+               }\r
+       }\r
+\r
+       else if (key & 0x0300)          /* CTRL key is down */\r
+       {\r
+\r
+\r
+       }\r
+\r
+       /* Standard editing keys including LF */\r
+\r
+       else if (((b >= 0x20) && (b <= 0x7E)) || (b == 0x0D))\r
+       {\r
+               coordCursor_oBuf ();\r
+                       if (b == 0x20)\r
+               b = pEdit->bSpace;\r
+                       if (b == 0x0D)\r
+               b = 0x0A;\r
+                            /* Don't overwrite CR */\r
+            if (pBuff[pEdit->oBufInsert] == 0x0A)\r
+              fSpecInsert = TRUE;\r
+            if (!(putInBuf (b, fOvertype, fSpecInsert)))\r
+               Beep();\r
+                       findCursor ();\r
+            fScreen = TRUE;\r
+\r
+       }\r
+\r
+       else if (key & 0x0C00) {        /* SHIFT key is down & NOT letter keys */\r
+               switch (b)\r
+               {\r
+                       case 0x03:      /* SHIFT Left */\r
+                    if (pEdit->iCol > pEdit->iColMin + 5)\r
+                               pEdit->iCol -= 5;\r
+                 else\r
+                       if (pEdit->iCol > pEdit->iColMin)\r
+                               pEdit->iCol--;\r
+                                break;\r
+                       case 0x04:      /* SHIFT Right */\r
+                    if (pEdit->iCol < pEdit->iColMax - 5)\r
+                               pEdit->iCol += 5;\r
+                 else\r
+                       if (pEdit->iCol < pEdit->iColMax)\r
+                               pEdit->iCol++;\r
+                                break;\r
+                        default:\r
+                               break;\r
+               }\r
+    }\r
+\r
+       else {                  /* Unshifted editing keys */\r
+               switch (b) {\r
+               case 0x08:      /* Backspace */\r
+                       if (pEdit->oBufLast)  {\r
+                   coordCursor_oBuf ();\r
+                   if (pEdit->oBufInsert)  {\r
+                     pEdit->oBufInsert = pEdit->oBufInsert - 1;\r
+                     if (!fOvertype)  {\r
+                       CopyData(pBuff,\r
+                                               pBuffWork,\r
+                                               pEdit->oBufLast+1);\r
+                       CopyData(&pBuffWork[pEdit->oBufInsert+1],\r
+                                       &pBuff[pEdit->oBufInsert],\r
+                                       pEdit->oBufLast-pEdit->oBufInsert);\r
+                       pBuff[pEdit->oBufLast] = 0;\r
+                       pEdit->oBufLast = pEdit->oBufLast - 1;\r
+                       if ((pEdit->oBufMark == pEdit->oBufBound) &&\r
+                           (pEdit->oBufMark == pEdit->oBufInsert))\r
+                          nullMarkBound ();\r
+                       if (pEdit->oBufMark < EMPTY)  {\r
+                         if (pEdit->oBufInsert <= pEdit->oBufMark)\r
+                            pEdit->oBufMark--;\r
+                         if (pEdit->oBufInsert <= pEdit->oBufBound)\r
+                            pEdit->oBufBound--;\r
+                       }\r
+                     }\r
+                   }\r
+                   if (pEdit->oBufInsert < pEdit->oBufLine0)\r
+                      pEdit->oBufLine0 = findPrevLine (pEdit->oBufLine0);\r
+                   fScreen = TRUE;\r
+                                  fModified = TRUE;\r
+                }\r
+                               break;\r
+               case 0x06:              /* Home - Cursor to BOL */\r
+                pEdit->iCol = pEdit->iColMin;\r
+                               break;\r
+               case 0x09:              /* Tab */\r
+                       if (pEdit->oBufLast + pEdit->iTabNorm < pEdit->iBufMax)\r
+                       {\r
+                   coordCursor_oBuf();\r
+                   j = pEdit->iTabNorm - (pEdit->iCol % pEdit->iTabNorm);\r
+                   for (i=1; i <=j; i++)\r
+                        putInBuf (pEdit->bSpace, FALSE, FALSE);\r
+                   fScreen = TRUE;\r
+                }\r
+                               break;\r
+               case 0x10:   /* F2 -- UNMARK BLOCK */\r
+                               nullMarkBound ();\r
+                                break;\r
+               case 0x11:              /* F3 -- Begin Block */\r
+                        if (pEdit->oBufLast > 0)  {\r
+                   coordCursor_oBuf ();\r
+                   pEdit->oBufMark = pEdit->oBufInsert;\r
+                   i = pEdit->iLine;\r
+                   if (pEdit->oBufMark >= pEdit->Line[i+1])\r
+                      pEdit->oBufMark = pEdit->oBufMark - 1;\r
+                   if (pEdit->oBufMark == pEdit->oBufLast)\r
+                      pEdit->oBufMark = pEdit->oBufLast - 1;\r
+                   pEdit->oBufBound = pEdit->oBufMark;\r
+                   fScreen = TRUE;\r
+                 }\r
+                 break;\r
+               case 0x12:              /* F4 -- End Block */\r
+                       if (pEdit->oBufMark < EMPTY)  {\r
+                   coordCursor_oBuf ();\r
+                   pEdit->oBufBound = pEdit->oBufInsert;\r
+                   i = pEdit->iLine;\r
+                   if (pEdit->oBufBound >= pEdit->Line[i+1])\r
+                      pEdit->oBufBound--;\r
+                   if (pEdit->oBufBound == pEdit->oBufLast)\r
+                      pEdit->oBufBound = pEdit->oBufLast - 1;\r
+                   fScreen = TRUE;\r
+                 }\r
+                 break;\r
+               case 0x17:      /* F9 - MOVE */\r
+                   coordCursor_oBuf ();\r
+                   moveData ();\r
+                   if (pEdit->oBufInsert < pEdit->oBufLine0)\r
+                      pEdit->oBufLine0 = pEdit->oBufInsert;\r
+                   fScreen = TRUE;\r
+                                break;\r
+               case 0x18:      /* F10 - COPY */\r
+                   coordCursor_oBuf ();\r
+                   CopyIt ();\r
+                   coordCursor_oBuf ();\r
+                   fScreen = TRUE;\r
+                                break;\r
+               case 0x0C:      /* Page Down */\r
+                   coordCursor_oBuf ();\r
+                   i = pEdit->iRowMax;\r
+                   while ((pEdit->Line[i] == EMPTY) && (i > pEdit->iRowMin))\r
+                        i--;\r
+                   pEdit->oBufLine0 = pEdit->Line[i];\r
+                                /*always keep onScreen*/\r
+                   if (pEdit->oBufInsert < pEdit->oBufLine0)\r
+                      pEdit->oBufInsert = pEdit->oBufLine0;\r
+                   pEdit->iLine = pEdit->iRowMin;\r
+                   pEdit->iCol = pEdit->iColMin;\r
+                   fScreen = TRUE;\r
+                                break;\r
+               case 0x05:      /* Page Up */\r
+                        if (pEdit->oBufLine0)  {\r
+                   coordCursor_oBuf ();\r
+                   j = pEdit->iRowMax - pEdit->iRowMin;\r
+                   i = pEdit->oBufLine0;\r
+                   k = pEdit->iLine;   /*fix for scrolling when iLine=iRowMax */\r
+                   do {\r
+                     i = findPrevLine (i);\r
+                     j--;\r
+                     k--;\r
+                                  }\r
+                   while ((j > 0) && (i > 0));\r
+                   pEdit->oBufLine0 = i;\r
+                          /*fix for scroll when iLine=iRowMax*/\r
+                   if (pEdit->iLine == pEdit->iRowMax)\r
+                      pEdit->oBufInsert = pEdit->Line[k];\r
+                            /*keep on screen*/\r
+                                  i = pEdit->iRowMax;\r
+                   if (pEdit->oBufInsert >= pEdit->Line[i+1])\r
+                      pEdit->oBufInsert = pEdit->Line[i];\r
+                   fScreen = TRUE;\r
+                 }\r
+                                break;\r
+               case 0x01:      /* Up */\r
+                       if (pEdit->iLine > pEdit->iRowMin)  {\r
+                   pEdit->iLine--;\r
+                 } else {              /* scroll screen down if we can */\r
+                                       i = pEdit->oBufLine0;\r
+                               if (i > 0)  {\r
+                               i = findPrevLine (i);\r
+                               pEdit->oBufLine0 = i;\r
+                       pEdit->oBufInsert = i;\r
+                               fScreen = TRUE;\r
+                                   }\r
+                 }\r
+                                break;\r
+               case 0x02:      /* Down */\r
+                       i = pEdit->iLine;\r
+                       if ((pEdit->Line[i+1] < EMPTY) &&   /*Down Arrow*/\r
+                       (i < pEdit->iRowMax))  {\r
+                   pEdit->iLine++;\r
+                }\r
+                               else {                  /* ELSE scroll screen UP if we can */\r
+                                       i = pEdit->iRowMax;\r
+                               if (pEdit->Line[i+1] < EMPTY)  {\r
+                               pEdit->oBufInsert = pEdit->Line[i+1];\r
+                                               i = pEdit->iCol;\r
+                                               j = pEdit->iLine;\r
+                                               coordCursor_oBuf ();\r
+                                               pEdit->iCol = i;\r
+                                               pEdit->iLine = j;\r
+                               fScreen = TRUE;\r
+                                       }\r
+                }\r
+                               break;\r
+               case 0x03:      /* Left */\r
+                       if (pEdit->iCol > pEdit->iColMin)  {          /*Left Arrow*/\r
+                   pEdit->iCol--;\r
+                 }\r
+                                break;\r
+               case 0x04:      /* Right */\r
+                       if (pEdit->iCol < pEdit->iColMax)  {\r
+                   pEdit->iCol++;\r
+                 }\r
+                                break;\r
+               case 0x0E:      /* Insert */\r
+                   if (fOvertype)\r
+                      fOvertype = FALSE;\r
+                     else fOvertype = TRUE;\r
+                                break;\r
+               case 0x7F:      /* Delete */\r
+                   coordCursor_oBuf ();\r
+                   if ((pEdit->oBufLast) &&\r
+                       (pEdit->oBufLast > pEdit->oBufInsert))  {\r
+                     CopyData(pBuff,\r
+                              pBuffWork,\r
+                              pEdit->oBufLast+1);\r
+                     CopyData(&pBuffWork[pEdit->oBufInsert+1],\r
+                              &pBuff[pEdit->oBufInsert],\r
+                              pEdit->oBufLast-pEdit->oBufInsert);\r
+                     pBuff[pEdit->oBufLast] = 0;\r
+                     pEdit->oBufLast--;\r
+                     if ((pEdit->oBufInsert == pEdit->oBufMark) &&\r
+                         (pEdit->oBufMark == pEdit->oBufBound))\r
+                        nullMarkBound ();\r
+                     if (pEdit->oBufMark < EMPTY)  {\r
+                       if (pEdit->oBufInsert < pEdit->oBufMark)\r
+                          pEdit->oBufMark--;\r
+                       if (pEdit->oBufInsert < pEdit->oBufBound)\r
+                          pEdit->oBufBound--;\r
+                       if (pEdit->oBufMark == pEdit->oBufLast)\r
+                          pEdit->oBufMark--;\r
+                       if (pEdit->oBufBound == pEdit->oBufLast)\r
+                          pEdit->oBufBound--;\r
+                     }\r
+                     fScreen = TRUE;\r
+                     fModified = TRUE;\r
+                   }\r
+                                  break;\r
+                       default:\r
+                               break;\r
+               }\r
+       }\r
+  } /* Not fDone */\r
+\r
+\r
+  for (i=pEdit->iRowMin; i <=pEdit->iRowMax; i++)\r
+    PutVidAttrs (pEdit->iColMin, i, pEdit->sLine+1, 0); /* REM buffer column */\r
+\r
+  if (fh) {\r
+       if (pEdit->fVisible)     /* fix visible characters */\r
+            for (i=0; i <=pEdit->iBufMax; i++)\r
+                if (pBuff[i] == 0x07)\r
+                         pBuff[i] = 0x20;\r
+       pBuff[pEdit->oBufLast] = 0;\r
+       if (fModified) {\r
+                       erc = CheckErc(6, SetFileLFA(fh, 0));\r
+                       if (!erc)\r
+                               erc = CheckErc(5, SetFileSize(fh,pEdit->oBufLast));\r
+                       if (!erc)\r
+                               erc = CheckErc(3, WriteBytes (fh, pBuf1, pEdit->oBufLast, &i));\r
+                       fModified = 0;\r
+       }\r
+       CloseFile(fh);\r
+       cbFilename = 0;\r
+  }\r
+\r
+  *pbExitRet = b;\r
+  return;\r
+}\r
+\r
+/************************************************************\r
+ This is the main entry point for the editor. It aloocates\r
+ two buffers of equal size(a main and a working buffer),\r
+ and then checks for a single parameter which should be the\r
+ name of the file to edit.\r
+*************************************************************/\r
+\r
+void main(U32 argc, U8 *argv[])\r
+{\r
+long i;\r
+\r
+       ClrScr();\r
+       SetJobName("Editor", 6);\r
+       fh = 0;\r
+\r
+    pEdit = &EdRec;\r
+       erc = AllocPage(32, &pBuf1);    /* 32 pages = 128K */\r
+    erc = AllocPage(32, &pBuf2);\r
+\r
+    pEdit->pBuf                        = pBuf1;\r
+    pEdit->pBufWork            = pBuf2;\r
+    pEdit->iBufMax             = 131071;    /*sBuf - 1 */\r
+    pEdit->iColMin     = 0;        /*Screen coordinates*/\r
+    pEdit->iColMax     = 79;\r
+    pEdit->iRowMin     = 1;\r
+    pEdit->iRowMax     = 23;\r
+    pEdit->sLine       = 80;       /* iColMax-iColMin+1 */\r
+    pEdit->bSpace      = 0x20;\r
+    pEdit->fVisible    = FALSE;\r
+    pEdit->iAttrMark   = MARKVID;  /* Rev Vid*/\r
+    pEdit->iAttrNorm   = EDVID;    /* Rev Vid Half Bright    */\r
+    pEdit->iTabNorm    = 4;        /* Tabs every 4th column  */\r
+    pEdit->oBufLine0   = 0;        /* oBufLine0 */\r
+    pEdit->iCol        = 0;        /* cursor, 0..sLine-1  */\r
+    pEdit->iLine       = 0;        /* cursor, 0..cLines-1    */\r
+    pEdit->oBufInsert   = 0;        /* offset of next char in */\r
+    pEdit->oBufLast    = 0;        /* offset+1 of last char  */\r
+    pEdit->oBufMark    = EMPTY;\r
+    pEdit->oBufBound   = EMPTY;\r
+\r
+       SetNormVid(NORMVID);\r
+\r
+       FillData(filler, 80, 0x20);\r
+       for (i=0; i<NLINESMAX; i++)\r
+       pEdit->Line[i] = EMPTY;\r
+       i = pEdit->iRowMin;\r
+       pEdit->Line[i] = 0;\r
+\r
+       fModified = 0;\r
+       fOvertype = FALSE;    /* Set Overtype OFF */\r
+\r
+       if (argc > 1)\r
+       {\r
+               OpenAFile(argv[1]);\r
+       }\r
+\r
+       Editor(&b);\r
+       ExitJob(0);\r
+}\r