📄 edit.c
字号:
/* Edit.c */
/* Edit test module */
#define U32 unsigned long
#define S32 long
#define U16 unsigned int
#define S16 int
#define U8 unsigned char
#define S8 char
#define TRUE 1
#define FALSE 0
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/* Includes for OS public calls and structures */
#include "MKernel.h"
#include "MMemory.h"
#include "MData.h"
#include "MTimer.h"
#include "MVid.h"
#include "MKbd.h"
#include "MJob.h"
#include "MFiles.h"
#define EDVID BRITEWHITE|BGBLUE
#define NORMVID WHITE|BGBLACK
#define MARKVID WHITE|BGRED
#define STATVID BLACK|BGCYAN
#define LFSHOW 0x1F /* Down Triangle */
#define CRSHOW 0x11 /* Left Triangle */
#define EMPTY 99999
#define NLINESMAX 26
struct EditRecType {
U32 PAD;
U8 *pBuf;
U8 *pBufWork; /* For copy and move */
U32 Line[NLINESMAX]; /* Offset in buf for 1st char in line */
U32 iBufMax; /* sBuf - 1 */
U32 iColMin; /* Screen coords */
U32 iRowMin;
U32 iColMax;
U32 iRowMax;
U32 sLine;
U8 bSpace;
U8 fVisible;
U32 iAttrMark;
U32 iAttrNorm;
U32 iTabNorm;
U32 oBufLine0 /* oBufLine0 */
U32 iCol; /* cursor, 0..sLine-1 */
U32 iLine; /* cursor, 0..cLines-1 */
U32 oBufInsert; /* offset of next char in */
U32 oBufLast; /* offset+1 of last char */
U32 oBufMark;
U32 oBufBound;
};
struct EditRecType EdRec;
struct EditRecType *pEdit;
char *pBuf1, *pBuf2;
unsigned char b, b1;
long erc, fh;
char fModified;
char fOvertype;
char aStat[80];
char aStat1[80];
char aCmd[80]; /* Get our command line */
long cbCmd = 0;
char *apParam[13]; /* Param 0 is cmd name */
long acbParam[13];
#define nParamsMax 13
char Filename[60];
long cbFilename;
unsigned char filler[100];
long CheckErc(long call, long erc)
{
char st[40];
long i;
if (erc) {
FillData(st, 40, 0);
Beep();
switch (call) {
case 1:
sprintf(st, "Error %05d occured on OpenFile", erc);
break;
case 2:
sprintf(st, "Error %05d occured on ReadBytes", erc);
break;
case 3:
sprintf(st, "Error %05d occured on WriteBytes", erc);
break;
case 4:
sprintf(st, "Error %05d occured on CreateFile", erc);
break;
case 5:
sprintf(st, "Error %05d occured on SetFileSize", erc);
break;
case 6:
sprintf(st, "Error %05d occured on SetFileLFA", erc);
break;
default:
sprintf(st, "Error %05d occured on last command", erc);
break;
}
for (i=0; i<40; i++)
if (!st[i])
st[i] = ' ';
PutVidChars (40, 24, st, 39, STATVID);
}
return (erc);
}
void ClearStatus(void)
{
char st[80];
FillData(st, 80, 0);
PutVidChars (0, 24, st, 80, NORMVID);
}
void Trace(long num)
{
char st[40];
sprintf(st, "Reached: %05d ", num);
PutVidChars (40, 24, st, 20, STATVID);
}
/*********************************************************
This Fills in the ptrs to and sizes of each parameter
found on the command line.
*********************************************************/
void ParseCmdLine(void)
{
long iCmd, iPrm, i; /* iCmd is index to aCmd */
iCmd = 0;
for (iPrm=0; iPrm<nParamsMax; iPrm++) {
acbParam[iPrm] = 0; /* default the param to empty */
apParam[iPrm] = 0;
if (iCmd < cbCmd) {
if (!isspace(aCmd[iCmd])) {
apParam[iPrm] = &aCmd[iCmd++]; /* ptr to param */
i = 1;
while ((iCmd < cbCmd) && (!isspace(aCmd[iCmd]))) {
i++;
iCmd++;
}
acbParam[iPrm] = i; /* size of param */
}
while ((iCmd < cbCmd) && (isspace(aCmd[iCmd])))
iCmd++;
}
}
}
void OpenAFile(void)
{
U32 filesize, dret, keycode;
erc = 0;
SetXY(0,24);
PutVidChars (0,24, "Filename: ", 10, BLACK|BGWHITE);
SetXY(10,24);
EditLine(Filename, 0, 60, &cbFilename, &b1, BLACK|BGCYAN);
SetXY(0,0);
if ((b1==0x0d) && (cbFilename)) {
erc = OpenFile(Filename, cbFilename, ModeModify, 1, &fh);
if (!erc) {
GetFileSize(fh, &filesize);
if (filesize > 65535)
filesize = 65535;
erc = ReadBytes (fh, pBuf1, filesize, &dret);
if (erc > 1)
erc = CheckErc(2, erc);
else erc = 0;
pEdit->oBufLast = dret; /* offset+1 of last char */
pBuf1[pEdit->oBufLast] = 0x0F; /* the SUN */
}
else if (erc == 203) { /* no such file */
Beep();
SetXY(50, 24);
TTYOut("Doesn't exist. Create?? (Y/N)", 29, BLACK|BGCYAN);
ReadKbd(&keycode, 1);
if (((keycode & 0xff) == 'Y') || ((keycode & 0xff) == 'y')) {
erc = CheckErc(4, CreateFile(Filename, cbFilename, 0));
if (!erc)
erc = CheckErc(1, OpenFile(Filename, cbFilename,
ModeModify, 1, &fh));
if (erc) {
fh = 0;
cbFilename = 0;
}
}
else {
cbFilename = 0;
ClearStatus();
}
}
else
CheckErc(1, erc);
}
if (!erc)
ClearStatus();
}
/************************************************************
This returns the index to the the last character in a line
upto a maximum of sLine-1. iBuf points to the beginning
point in the buffer to find the end of line for.
*/
unsigned long findEol (unsigned long iBuf)
{
unsigned long iEol, iEolMax;
unsigned char *pBuff;
pBuff = pEdit->pBuf;
/* Calculate the most it could be */
iEolMax = iBuf + pEdit->sLine-1;
/* Fix it if EOL is past end of data */
if (iEolMax > pEdit->oBufLast)
iEolMax = pEdit->oBufLast;
iEol = iBuf;
while ((pBuff[iEol] != 0x0A) && (iEol < iEolMax)) /* Find CR */
iEol++;
if ((iEol == iEolMax) && (pBuff[iEol] != 0x0A)) { /* if no CR... */
iEol = iEolMax;
if (iEolMax < pEdit->oBufLast) {
/* now work back to last space */
while ((pBuff[iEol] != pEdit->bSpace) && (iEol > iBuf))
iEol--;
/* now find first non-space - allows */
if ((iEol > iBuf) &&
(pBuff[iEol] == pEdit->bSpace) && /* wrap-around w/ double space */
(iEol == iEolMax)) {
if ((pBuff[iEol-1] == pEdit->bSpace) ||
((iEol == iEolMax) && (pBuff[iEol+1] == pEdit->bSpace))) {
while ((pBuff[iEol] == pEdit->bSpace) && (iEol > iBuf))
iEol--;
while ((pBuff[iEol] != pEdit->bSpace) && (iEol > iBuf))
iEol--;
}
}
if ((iEol == iBuf) &&
(pBuff[iBuf] > 0) &&
(pBuff[iEolMax] > 0)) /* handles "all-char" of full line */
iEol = iEolMax;
}
}
return(iEol);
}
unsigned long findPrevLine (unsigned long oBufStart)
{
unsigned long i, j;
char *pBuff;
pBuff = pEdit->pBuf;
i = 0;
if (oBufStart)
i = oBufStart - 1;
while ((i) && (pBuff[i] != 0x0A))
i--;
if (i > 0)
i--;
while ((i > 0) && (pBuff[i] != 0x0A))
i--;
if (i)
i++; /* Get to known start of line */
do {
j = i;
i = (findEol (j)) + 1;
}
while (i < oBufStart);
return(j);
}
void doMark (unsigned long iLn)
{
unsigned long iColStart, iColFinish, s, iMarkLoc, iBoundLoc;
if (pEdit->oBufMark < EMPTY) {
if (pEdit->oBufMark <= pEdit->oBufBound) {
iMarkLoc = pEdit->oBufMark;
iBoundLoc = pEdit->oBufBound;
}
else {
iMarkLoc = pEdit->oBufBound;
iBoundLoc = pEdit->oBufMark;
}
if ( ((iMarkLoc >= pEdit->Line[iLn]) && (iMarkLoc < pEdit->Line[iLn+1])) ||
((iBoundLoc >= pEdit->Line[iLn]) && (iBoundLoc < pEdit->Line[iLn+1])) ||
((iMarkLoc < pEdit->Line[iLn]) && (iBoundLoc >= pEdit->Line[iLn+1])) )
{
if (iMarkLoc >= pEdit->Line[iLn])
iColStart = pEdit->iColMin + iMarkLoc - pEdit->Line[iLn];
else
iColStart = pEdit->iColMin;
if (iBoundLoc < pEdit->Line[iLn+1])
iColFinish = pEdit->iColMin + iBoundLoc - pEdit->Line[iLn];
else
iColFinish = pEdit->iColMin + pEdit->Line[iLn+1] - pEdit->Line[iLn] - 1;
if (iColStart > pEdit->iColMin)
PutVidAttrs (pEdit->iColMin,
iLn,
iColStart-pEdit->iColMin,
pEdit->iAttrNorm);
PutVidAttrs (iColStart, iLn, iColFinish - iColStart +1, pEdit->iAttrMark);
if (iColFinish < pEdit->iColMax)
PutVidAttrs (iColFinish+1,
iLn,
pEdit->iColMax - iColFinish,
pEdit->iAttrNorm);
}
else /*buf col*/
PutVidAttrs (pEdit->iColMin,
iLn,
pEdit->sLine,
pEdit->iAttrNorm);
}
}
char putInBuf( unsigned char bPutIn,
char fOvertype,
char fSpecInsert)
{
unsigned long i, j, cb;
char fOK, *pBuff;
fModified = 1;
pBuff = pEdit->pBuf;
if ((pEdit->oBufInsert < pEdit->iBufMax) &&
((pEdit->oBufLast < pEdit->iBufMax) ||
((fOvertype) && (!fSpecInsert))))
{
fOK = 1;
if ((fOvertype) && (!fSpecInsert)) {
pBuff[pEdit->oBufInsert] = bPutIn;
if (pEdit->oBufLast == pEdit->oBufInsert)
pEdit->oBufLast++;
pEdit->oBufInsert++;
}
else {
cb = pEdit->oBufLast - pEdit->oBufInsert + 1;
CopyData (&pBuff[pEdit->oBufInsert], pEdit->pBufWork, cb);
pBuff[pEdit->oBufInsert] = bPutIn;
CopyData (pEdit->pBufWork, &pBuff[pEdit->oBufInsert+1], cb);
pEdit->oBufLast++;
pEdit->oBufInsert++;
if (pEdit->oBufMark < EMPTY) {
if (pEdit->oBufInsert-1 < pEdit->oBufMark)
pEdit->oBufMark++;
if (pEdit->oBufInsert-1 <= pEdit->oBufBound)
pEdit->oBufBound++;
}
}
}
else {
fOK = 0;
Beep();
erc = 40400;
}
return (fOK);
}
void moveData (void)
{
unsigned long i, iMk, iBd;
char *pBuff, *pBuffWork;
pBuff = pEdit->pBuf;
pBuffWork = pEdit->pBufWork;
if (pEdit->oBufMark < EMPTY) {
fModified = 1;
if (pEdit->oBufMark <= pEdit->oBufBound) {
iMk = pEdit->oBufMark;
iBd = pEdit->oBufBound;
}
else {
iBd = pEdit->oBufMark;
iMk = pEdit->oBufBound;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -