/* ----------------------------- ue6.c -------------------------------- */

#include "uedit.h"

static long 	   locSH,locEH,locSI,locEI;
static short	   hInset,hWidth,iInset,iWidth,mayMode,madeCursor;
static UCHAR	   css[6];

void display()  		/* can calc amt v/h scroll, don't believe amt */
{
register UCHAR  	*cs;
register struct CB  *curb;
register ULONG  	locp,loc1;
register short  	k,flag;
unsigned long		loc2;
short				split;
struct  DB  		**b;
UCHAR				**p;

	if (noDisplay) goto NODISPLAY;
	suppressClear = FALSE;
	wptr = lineBuf;
	k  = nSplit;
	split = k;
	if (displayFlag==VSCROLL_) k = curSplit;
TOPAGAIN:
	curb = buf[splitBufNum[k]];
	if (curb==NULL) {
		fixSplit();
		curb = buf[splitBufNum[k]];
		if (curb==NULL) goto ENDOUT;
	}
	localColor = curb->colors;
	setColors(normalVid);
	startLine = startSplit[k] + 1;   /* first print line */
	stopLine = stopSplit[k]; /* last print line */

	if (displayFlag==VSCROLL_ && scrollType && scrolling()) {
			didScroll = TRUE;
			return;
	}
	locSH = loc(curb->SHBlock,curb->SHPtr);
	locEH = loc(curb->EHBlock,curb->EHPtr);
	locSI = loc(curb->SIBlock,curb->SIPtr);
	locEI = loc(curb->EIBlock,curb->EIPtr);
	madeCursor = FALSE;  /* important:  Keep this here! */

BLASTAGAIN:
	cs = curb->Ptr;
	flag = (displayFlag >> k) & PAGE_;
	if (k==curSplit) flag |= (displayFlag & 15);
	if (flag==0) goto ENDOUT;
	if (k==curSplit && (flag & SHOWCURSOR_) && !onScreen(curb->Block,cs)) {
		cursorMid(FALSE);
		flag |= PAGE_;
	}

	if (flag==VSCROLL_ || flag==HSCROLL_) {
		scrolling();
		didScroll = TRUE;
		flag = displayFlag >> k;
	}
	if (k!=curSplit || (locSH>=locEH && locSI>=locEI)) mayMode = 0;
	else {
		locp = loc(curb->PBlock,curb->PPtr);
		if (locp>=locEH && locp>=locEI) mayMode = 0;
		else { mayMode = 1; if (columnar) { mayMode=2; colCalc(); } }
	}

	if (mayMode) inTrace(); 	 /* maymode */
	else if (ISGOOD(*cs)) { *css = *cs; *cs = TRACE; }

	if (flag & PAGE_) {
DOPAGE:
		line = startLine;
		displayLine(k,stopLine);
		goto DONE;
	}
	if (flag) { 									  /* cursor or showeos */
		line = oldCursorLine;

		if (line<startLine) goto DOPAGE;
		if (line>=stopLine) line = stopLine - 1;
		if (flag & SHOWEOS_) {
			displayLine(k,stopLine);
			goto DONE;
		}
		displayLine(k,line+1);  			   /* wipe out old cursor line */
		if (madeCursor) goto DONE;
		locp = loc(curb->Block,cs);
		k = startLine;
		p = &linePtrs[k];
		b = &lineBlocks[k];
		loc1 = loc(*b,*p);
		while (k<stopLine) {
			b++;
			p++;
			if (*p==NULL) goto TAKELINE;
			loc2 = loc(*b,*p);
			if (locp>=loc1) {
				if (*cs==EOFILE && locp<=loc2) goto TAKELINE;
				if (locp<loc2 ) {
TAKELINE:			line = k;
					displayLine(split,k+1);
					goto DONE;
				}
			}
			loc1 = loc2;
			k++;
		}
	}
DONE:
	if (mayMode) outTrace();
	else if (*cs==TRACE) *cs = *css;
	if (!madeCursor && (flag & SHOWCURSOR_)) {
		cursorMid(TRUE);
		madeCursor = TRUE;
		k = split;
		goto BLASTAGAIN;
	}
ENDOUT:
	k = --split;
	if (k>=0) goto TOPAGAIN;
NODISPLAY:
	if (curBuf) localColor = curBuf->colors;
	vscrollamt = hscrollamt = 0;
	displayFlag = 0;
}

void displayLine(	short split,
					register short stop)
{
register UCHAR  	*p,*q,CH;
register short  	col,byte,oldMode,mode;
short				pageCol,k,lastLine;
UCHAR				*table;
unsigned long		locp;
struct CB			*curb;

	col = split;
	mode = startSplit[col]+1;
	lastLine = stopSplit[col];
	if (line>=stop) return;
	curb = buf[splitBufNum[col]];
	pageCol = curb->PageCol;
	if (line==mode) {
		if(col!=curSplit && curb==curBuf) {
			p = curb->LSPPtr;
			q = (UCHAR *)curb->LSPBlock;
		} else {
			p = curb->PPtr;
			q = (UCHAR *)curb->PBlock;
		}
		linePtrs[mode]   = p;
		lineBlocks[mode] = (struct DB *)q;
	}
	saveFast();
DOLINE:
	mode = line;
	q = linePtrs[mode];
	fastB = lineBlocks[mode];
	fastP = q;
	if (q==NULL) goto LINENULL;
	table = tabTables[curb->tabTable];
	if (columnar) { table = (UCHAR *)""; spooks = 1; }
	mode = 0;
	if (mayMode) {
		locp = loc(fastB,q);
		if (locp<locEH && locp>=locSH) mode |= hiliteVid;
		if (locp<locEI && locp>=locSI) mode |= invertVid;
		if (mayMode==2) {
			if ((mode & hiliteVid) && (hWidth<=0 || hInset>0))
						mode -= hiliteVid;
			if ((mode & invertVid) && (iWidth<=0 || iInset>0))
						mode -= invertVid;
		}
	}
	oldMode = mode;
	col 	= -pageCol;
	CH = *q;
	if (CH==EOFILE) {
		mode = line;
MAKECURSOR:
		curb->EPtr   = q;
		curb->EBlock = fastB;
		move(0,mode);
		if (q==curb->Ptr && mode<stop) {
			if (col>=0 && col<=maxLineChars) {
				if (oldColors!=cursorVid) setColors(cursorVid);
				*lineBuf = ' ';
				text(1);
				cursorMade(col,mode);
			}
		}
		if (mode<stop) {
			if (oldColors!=normalVid) setColors(normalVid);
			cleareol();
		}
		mode++;
		q = NULL;
LINENULL:
		CH = stop;
		while (mode <= CH) {
			if (mode<CH && primitive!=2) {
				if (oldColors!=normalVid) setColors(normalVid);
				move(0,mode);
				if (mode<maxLines) cleareol();
				else {
					if (warpUp) Move(rp,(long)destx,(long)lineHeight[mode]);
					ClearScreen(rp);
				}
			}
			linePtrs[mode]  = q;
			lineBlocks[mode]   = fastB;
			mode++;
		}
RTNPOINT:
		line = mode;
		if (q==NULL || mode>=stop) { unsaveFast(); return; }
		goto DOLINE;
	}
	byte = 0;
	p = lineBuf;
	move(0,line);
	if (CH==0) goto BUMP;
	while (1) {
		if (ISWEIRD(CH) || (mode & cursorVid)) {
			if (q==curb->Ptr) {
				if (*q==TRACE) *q = *css;
				mode |= cursorVid;
			} else if (mode & cursorVid) mode -= cursorVid;
			if (mayMode) {
				if (q==curb->SHPtr) {
					if (locSH<locEH) mode |=  hiliteVid;
					if (*q==TRACE) *q=css[1];
				} else if (q==curb->EHPtr) {
					if (locSH<locEH) mode &= ~hiliteVid;
					if (*q==TRACE) *q=css[2];
				}
				if (q==curb->SIPtr) {
					if (locSI<locEI) mode |=  invertVid;
					if (*q==TRACE) *q=css[3];
				} else if (q==curb->EIPtr) {
					if (locSI<locEI) mode &= ~invertVid;
					if (*q==TRACE) *q=css[4];
				}
			}
			CH = *q;
		}
		if (mayMode==2) {
			if ((locp=loc(fastB,q))<locEH && locp>=locSH) {
				if (col==hInset)			mode |=  hiliteVid;
				if (hWidth<0 || col==hWidth) mode &= ~hiliteVid;
			}
			if (locp<locEI && locp>=locSI) {
				if (col==iInset)			mode |=  invertVid;
				if (iWidth<0 || col==iWidth) mode &= ~invertVid;
			}
		}

		if (oldMode!=mode || CH==eol || CH==12 || CH==EOFILE) {
CURSORMAYBE:
			if (byte) {
				if (oldMode & cursorVid) cursorMade(col,line);
				if (oldColors!=oldMode) setColors(oldMode);
				p=lineBuf;
				text(byte);
				byte = 0;
			}
			if (CH==eol || CH==12 || CH==EOFILE) {
				if (mode & cursorVid) {
					if (col>=0 && col<=maxLineChars) {
						setColors(cursorVid);
						*lineBuf = ' ';
						text(1);
						cursorMade(col,line);
					}
				}
				if (oldColors!=normalVid) setColors(normalVid);
				cleareol();
				if (CH==12) {
					if (depth!=1) setColors(ffVid);
					if (CH==12) {
						if (warpUp) Move(rp,(long)destx,(long)lineHeight[line]);
						if (!MENUDRAWN)
						   Draw(rp,(long)horPixels - 12L,(long)lineHeight[line]);
					}
					if (oldColors!=normalVid) setColors(normalVid);
				}
				mode = line;
				mode++;
				if (CH!=EOFILE) {		/* was eoline or formfeed */
					col = -pageCol;
					fastP = q;
					q = incFast();
				} else {
					curb->EPtr  = q;
					curb->EBlock = fastB;
					q = NULL;			/* definitely was EOFILE */
				}
				linePtrs[mode]  =	q;  /* store for sure, could be EOFILE */
				lineBlocks[mode] =  fastB;
				if (q==NULL) goto LINENULL; /* store ALL nulls */
				if (CH==EOFILE) goto MAKECURSOR;
				if (mode>=lastLine) {
					curb->EPtr  = q;
					curb->EBlock = fastB;
				}
				goto RTNPOINT;
			}
			oldMode = mode;
		}
NOTYET:
		if (ISPRINT(CH)) goto SHOWIT;
		if (CH==TAB) {
			if (*table) {
				k = 1 + col + pageCol;
				while (*table && *table<=k) table++;
				if (*table==0) goto CHECKSPOOK;
				while (k++ < *table) {
					if (col>=0 && col<=maxLineChars) {
						*(p++) = ' ';
						byte++;
					}
					col++;
				}
				goto BUMP;
			}
CHECKSPOOK:
			if (spooks) goto SHOWIT;					/* show spooks */
			goto BUMP;
		}
		if (CH==eol || CH==12) goto BUMP;
		if (col>10000 && CH>EOFILE) { CH = EOFILE; goto CURSORMAYBE; }
		if (spooks && ISGOOD(CH)) {
SHOWIT:
			if (col>=0 && col<=maxLineChars) {
				*(p++) = CH;
				byte++;
			}
			col++;
		}
BUMP:	q++;
TEST:	CH = *q;
		if (CH==0) goto BUMP;
		if (CH==EOB) {
			fastB = fastB->NextBlock;
			q = fastP = fastB->Data;
			goto TEST;
		}
	} /* while (1) */
}

#ifdef USEC

void setColors(register short  i)
{
register long	 j,k,i1,i2;

	oldColors = i;
	if (depth==1) {
		i1 = 1;
		i2 = 0;
		/* FIX this for tab ruler, requester, etc!!!! */
		if (i && i!=9) { i1 = 0; i2 = 1; }
	} else {
		if (i<8) j  = localColor;
		else { j = screenColor; i -= 8; }
		k = 7 - i;
		k <<= 2;
		j >>= k;
		i1 = j >> 2;
		i1 &= 3;
		i2 = j & 3;
	}
	SetAPen(rp,i1);
	SetBPen(rp,i2);
	mask1 = rp->minterms[0];
	mask2 = rp->minterms[1];
}

#endif

#ifdef USEC

void cursorMid(short flag)
{
register struct CB  *curB;
register short  	i,j,k,curSp;
struct CB			*saveBufPtr;
struct DB			*saveB;
UCHAR				*saveP;

/* ===================
		if (midCursor==0) {
				displayFlag = (PAGE_ << curSplit);
				return;
		}
================== */
	curB  = curBuf;
	curSp = curSplit;
	saveB = fastB;   saveP = fastP;  saveBufPtr = fastBufPtr;
	fastBufPtr = curB; fastP = curB->Ptr; fastB = curB->Block;

	i = getCol();
	j = curB->PageCol;
	k = maxLineChars;
	if (i<=k) j = 0;
	else {
		if (i>(k + j) || i<=j) j = i - (k >> 1);
		if (j<0) j = 0;
	}
	curB->PageCol = j;
	oldCursorCol = i;

	if (flag || !onScreen(fastB,fastP)) {
		i = stopSplit[curSp];
		j = startSplit[curSp];
		i -= j;
		i >>= 1;
		j += i;
		i = startSplit[curSp];
		incFast();
		sLineFast();
		while (i<j) { i++; sLineFast(); }
		curB->PBlock = fastB;
		curB->PPtr   = fastP;
	}
	displayFlag = (PAGE_ << curSp) | SHOWCURSOR_ ;
NOMID:
	fastB = saveB; fastP = saveP; fastBufPtr = saveBufPtr;
}

void colCalc()
{
register struct CB *curB;
register UCHAR     *sP;
register ULONG     sB;
register ULONG     ss;
register short     col;

	sB = (ULONG)fastB; sP = fastP; ss = (ULONG)fastBufPtr;
	curB = curBuf;
	fastBufPtr = curB;

	if (locSH>=locEH) goto SKIPH;
	fastP = curB->SHPtr; fastB = curB->SHBlock;
	hInset = getCol() - 1 - curB->PageCol;

	fastP  = curB->EHPtr; fastB = curB->EHBlock;
	col = getCol() - 1 - curB->PageCol;

	if (col<=hInset) SKIPH: hWidth = hInset = -1;
	else hWidth = col;

	if (locSI >= locEI) goto SKIPI;
	fastP = curB->SIPtr; fastB = curB->SIBlock;
	iInset = getCol() - 1 - curB->PageCol;

	fastP = curB->EIPtr; fastB = curB->EIBlock;
	col = getCol() - 1 - curB->PageCol;

	if (col<=iInset) {
SKIPI:
		iWidth = iInset = -1;
		if (hWidth<0) mayMode = 0;
	} else iWidth = col;

	fastB = (struct DB *)sB; fastP = sP; fastBufPtr = (struct CB *)ss;
}

void cursorMade(	short col,
					short cline)
{
	madeCursor = TRUE;
	oldCursorCol = col + 1;
	oldCursorLine = cline;
}

FgetPageRowCol()
{
	displayFlag |= SHOWCURSOR_;
	popFast();
	showRow(FALSE);
	*longPtrArgs[0] = gPage;
	*longPtrArgs[1] = gRow;
	*longPtrArgs[2] = gColm;
	return(TRUE);
}

void showRow(short	flag)
{
register struct CB  *curB;
register UCHAR  	*rrc;
register long		i,lines,cols,pages;

	curB = curBuf;
	rrc = rc;
	breakOk = TRUE;
	lines = locToLines(curB,curB->Block,curB->Ptr);
	if (lines == -1L) goto RTN;
	i = curB->PageLines;
	if (i>1) {
			pages = 1L + ((lines-1)/i);
			lines %= i;
			if (lines==0L) lines = i;
			stci_d(rrc,pages);
			strcat(rrc,":");
			catNumber(rrc,lines,30);
	} else { stci_d(rrc,lines); pages = 0; }
	untakeFast();
	cols = getCol();
	gPage = pages; gRow = lines; gColm = cols;
	if (flag) {
		strcat(rrc,",");
		catNumber(rrc,cols,30);
		rowMsg();
	}
RTN:
	breakOk = FALSE;
}

void rowMsg()
{
register UCHAR  *rrc,*y;
register short  j;

	rrc = rc;
	if (noDisplay==0 && !primitive && rowCol && *rrc!=0x00) {
		setColors(msgVid);
		wptr = rrc;
		y = rrc;
		while (*rrc) rrc++;
		j = rrc - y;
		move(maxLineChars - 16,0);
		text(j);
		cleareol();
		return;
	}
}

long	locToLines( struct  CB  *bufPtr,
					struct  DB  *block,
					UCHAR		*ptr)
{
register UCHAR  		*p;
register struct DB  	*b;
register short  		eols;
register long			j,locSeek,lines,PageLines;
register UCHAR  		ch;
USHORT  				i;

	locSeek = (long)ptr;
	i = block->BlockNum;
	b = bufPtr->NextBlock;
	p = b->Data;
	lines = 1L;
	PageLines = bufPtr->PageLines;
TOP:
	if (i<1) goto BOTTOM;
	if (b->eols == 255 || i==1) {
		eols=0;
		while (1) {
			if ((long)p==locSeek) locSeek=0L;
			ch = *p;
			if (ch>13 || ch==0x00) { p++; continue; }
			if (ch==EOB) break;
			if (ch==EOFILE) break;
			if (ch==eol) {
PLAYEOL:
					if (locSeek) lines++;
					if (eols<255) eols++;
			} else if (ch==12) {
					if (PageLines<2) goto PLAYEOL;
					eols = 255;
					if (locSeek) {
						j = (lines % PageLines);
						lines++;
						if (j) lines += (PageLines - j);
					}
			}
			p++;
		}
		b->eols = eols;
	} else lines += b->eols;
	b = b->NextBlock;
	p = b->Data;
	i--;
	if (breakOk && FinputWaiting()) {
				lines = -1L;
				goto BOTTOM;
	}
	goto TOP;
BOTTOM:
	return(lines);
}

void inTrace()
{
register UCHAR  **ovr,*cs,ch;
register short  i;

	ovr = (UCHAR **)&(curBuf->Ptr); cs = css; i = 5;
	while (1) {
		ch = **ovr;
		if (ISGOOD(ch)) { *cs = ch; **ovr = TRACE; }
		else *cs = TRACE;
		cs++;
		ovr++;
		if (--i==0) break;
	}
}

void outTrace()
{
register UCHAR  **ovr,*cs,ch,chs;
register short  i;

	ovr = (UCHAR **)&(curBuf->Ptr); cs = css; i = 5;
	while (1) {
		chs = *cs;
		ch = **ovr;
		if (ch==TRACE && ISGOOD(chs)) **ovr = chs;
		ovr++;
		cs++;
		if (--i==0) break;
	}
}

#endif

#ifndef USEC
#asm
	XREF	_breakOk
	XREF	_catNumber
	XREF	_cleareol
	XREF	_curBuf
	XREF	_curSplit
	XREF	_depth
	XREF	_displayFlag
	XREF	_eol
	XREF	_fastB
	XREF	_fastBufPtr
	XREF	_fastP
	XREF	_FinputWaiting
	XREF	_gColm
	XREF	_getCol
	XREF	_gPage
	XREF	_gRow
	XREF	_hInset
	XREF	_hWidth
	XREF	_iInset
	XREF	_incFast
	XREF	_iWidth
	XREF	_locEH
	XREF	_locEI
	xref	_saveFast
	xref	_spooks
	xref	_tabTables
	xref	_oldColors
	xref	_maxLines
	xref	_warpUp
	xref	_lineHeight
	xref	_destx
	xref	_rp
	xref	_GfxBase
	xref	_unsaveFast
	xref	_menuDrawn
	xref	_horPixels
	xref	_suppressClear
	xref	_lineBuf
	xref	_nSplit
	xref	_splitBufNum
	xref	_buf
	xref	_fixSplit
	xref	_localColor
	xref	_startLine
	xref	_stopLine
	xref	_scrollType
	xref	_scrolling
	xref	_didScroll
	xref	_columnar
	xref	_line
	xref	_linePtrs
	xref	_lineBlocks
	xref	_hscrollamt
	xref	_vscrollamt
	xref	_screenColor
	XREF	_locSH
	XREF	_locSI
	XREF	_longPtrArgs
	XREF	_madeCursor
	XREF	_mask1
	XREF	_mask2
	XREF	_maxLineChars
	XREF	_mayMode
	XREF	_move
	XREF	_noDisplay
	XREF	_oldCursorCol
	XREF	_oldCursorLine
	XREF	_onScreen
	XREF	_popFast
	XREF	_primitive
	XREF	_rc
	XREF	_rowCol
	XREF	_rowMsg
	XREF	_SetAPen
	XREF	_SetBPen
	XREF	_sLineFast
	XREF	_startSplit
	XREF	_stci_d
	XREF	_stopSplit
	XREF	_strcat
	XREF	_text
	XREF	_untakeFast
	XREF	_wptr

	xdef _inTrace
_inTrace:
	move.l  a2,-(sp)
							;register UCHAR **ovr,*cs,ch;
							;register short i;
	move.l  _curBuf,a1  ;	ovr = (UCHAR **)&(curBuf->Ptr); cs = css; i = 5;
	add.w	#80,a1
	lea _css,a2
	moveq	#5,d1
YYYY19: 				;	while (1) {
	move.l  (a1),a0 	;		ch = **ovr;
	move.b  (a0),d0
	cmp.b	#3,d0		;	if (ISGOOD(ch)) { *cs = ch; **ovr = TRACE; }
	bls YYYY21
	move.b  d0,(a2)
	move.l  (a1),a0
	move.b  #1,(a0)
	bra YYYY22  		;		else *cs = TRACE;
YYYY21:
	move.b  #1,(a2)
YYYY22:
	addq.w  #1,a2		;		cs++;
	addq.w  #4,a1		;		ovr++;
	subq.w  #1,d1		;		i--;
;;;	tst.w	d1  	;;; 	;	if (i==0) break;
	bne YYYY19  		;	}
	move.l  (sp)+,a2
	rts

	xdef  _outTrace
_outTrace:
	move.l  a2,-(sp)
							;register UCHAR **ovr,*cs,ch,chs;
							;register short i;
	move.l  _curBuf,a2  ;	ovr = (UCHAR **)&(curBuf->Ptr); cs = css; i = 5;
	add.w	#80,a2
	lea _css,a1
	moveq	#5,d0
YYYY26: 				;	while (1) {
	move.b  (a1),d1 	;		chs = *cs;
	move.l  (a2),a0 	;		ch = **ovr;
	cmp.b	#1,(a0) 	;		if (ch==TRACE && ISGOOD(chs)) **ovr = chs;
	bne YYYY28
	cmp.b	#3,d1
	bls YYYY28
	move.l  (a2),a0
	move.b  d1,(a0)
YYYY28:
	addq.w  #4,a2			;		ovr++;
	addq.w  #1,a1			;		cs++;
	subq.w  #1,d0			;		i--;
;;;	tst.w	d0  		;;;
	bne YYYY26  		;	}
	move.l  (sp)+,a2
	rts

	xdef	_cursorMid
_cursorMid:
	movem.l	d2-d7/a2/a3,-(sp)
	move.l	_curBuf,a2  		;curSp=curSplit
	move.w	_curSplit,d4			;saveB=fastBsaveP=fastPsaveBufPtr=fastBufPtr
	move.l	_fastB,d6
	move.l	_fastP,d7
	move.l	_fastBufPtr,a3  		;fastBufPtr=curBfastP=curB->PtrfastB=curB->Block
	move.l	a2,_fastBufPtr
	move.l	80(a2),_fastP
	move.l	(a2),_fastB 		;i=getCol()
	jsr	_getCol
	move.w	d0,d2			;j=curB->PageCol
	move.w	164(a2),d3  		;k=maxLineChars
	move.w	_maxLineChars,d5			;if(i<=k)j=0
	cmp.w	d5,d2
	bgt	DS10001
	moveq	#0,d3			;else{
	bra	DS10002
DS10001:			;if(i>(k+j)||i<=j)j=i-(k>>1)
	move.w	d5,d0
	add.w	d3,d0
	cmp.w	d2,d0
	blt	DS2
	cmp.w	d3,d2
	bgt	DS10003
DS2:
	move.w	d5,d0
	asr.w	#1,d0
	move.w	d2,d3
	sub.w	d0,d3			;if(j<0)j=0
DS10003:
	tst.w	d3
	bge	DS10004
	moveq	#0,d3			;}
DS10004:
DS10002:			;curB->PageCol=j
	move.w	d3,164(a2)  		;oldCursorCol=i
	move.w	d2,_oldCursorCol			;if(flag||!onScreen(fastB,fastP)){
	tst.w	36(sp)
	bne	DS3
	move.l	_fastP,-(sp)
	move.l	_fastB,-(sp)
	jsr	_onScreen
	tst.w	d0
	addq.w	#8,sp
	bne	DS10005
DS3:			;i=stopSplit[curSp]
	move.w	d4,d0
	add.w	d0,d0
	lea	_stopSplit,a0
	move.w	(a0,d0.w),d2			;j=startSplit[curSp]
	lea	_startSplit,a0
	move.w	(a0,d0.w),d3			;i-=j
	sub.w	d3,d2			;i>>=1
	asr.w	#1,d2			;j+=i
	add.w	d2,d3			;i=startSplit[curSp]
	lea	_startSplit,a0
	move.w	(a0,d0.w),d2			;incFast()
	jsr	_incFast			;sLineFast()
	jsr	_sLineFast  		;while(i<j){i++sLineFast()}
DS10006:
	cmp.w	d3,d2
	bge	DS10007
	addq.w	#1,d2
	jsr	_sLineFast
	bra	DS10006
DS10007:			;curB->PBlock=fastB
	move.l	_fastB,20(a2)			;curB->PPtr=fastP
	move.l	_fastP,100(a2)  		;}displayFlag=(PAGE_<<curSp)|SHOWCURSOR_
DS10005:
	moveq	#16,d0
	asl.w	d4,d0
	bset.l	#0,d0
	move.w	d0,_displayFlag 		;NOMID:
DS10008:			;fastB=saveBfastP=savePfastBufPtr=saveBufPtr
	move.l	d6,_fastB
	move.l	d7,_fastP
	move.l	a3,_fastBufPtr  		;}
DS4:
	movem.l	(sp)+,d2-d7/a2/a3
	rts 		;voidcolCalc(){

	xdef	_colCalc
_colCalc:
	movem.l	d2-d4/a2/a3,-(sp)			;ss=(U)fastBufPtr
	move.l	_fastB,d3
	move.l	_fastP,a3
	move.l	_fastBufPtr,d4  		;curB=curBuf
	move.l	_curBuf,a2  		;fastBufPtr=curB
	move.l	a2,_fastBufPtr  		;if(locSH>=locEH)gotoSKIPH
	move.l	_locSH,d0
	cmp.l	_locEH,d0
	bge	DS10009 		;fastP=curB->SHPtrfastB=curB->SHBlock
	move.l	84(a2),_fastP
	move.l	4(a2),_fastB			;hInset=getCol()-1-curB->PageCol
	jsr	_getCol
	subq.w	#1,d0
	sub.w	164(a2),d0
	move.w	d0,_hInset  		;fastP=curB->EHPtrfastB=curB->EHBlock
	move.l	88(a2),_fastP
	move.l	8(a2),_fastB			;col=getCol()-1-curB->PageCol
	jsr	_getCol
	subq.w	#1,d0
	move.w	d0,d2
	sub.w	164(a2),d2  		;if(col<=hInset)SKIPH:hWidth=hInset=-1
	cmp.w	_hInset,d2
	bgt	DS10010
DS10009:
	move.w	#-1,_hInset
	move.w	#-1,_hWidth 		;elsehWidth=col
	bra	DS10011
DS10010:
	move.w	d2,_hWidth
DS10011:			;if(locSI>=locEI)gotoSKIPI
	move.l	_locSI,d0
	cmp.l	_locEI,d0
	bge	DS10012 		;fastP=curB->SIPtrfastB=curB->SIBlock
	move.l	92(a2),_fastP
	move.l	12(a2),_fastB			;iInset=getCol()-1-curB->PageCol
	jsr	_getCol
	subq.w	#1,d0
	sub.w	164(a2),d0
	move.w	d0,_iInset  		;fastP=curB->EIPtrfastB=curB->EIBlock
	move.l	96(a2),_fastP
	move.l	16(a2),_fastB			;col=getCol()-1-curB->PageCol
	jsr	_getCol
	subq.w	#1,d0
	move.w	d0,d2
	sub.w	164(a2),d2  		;if(col<=iInset){
	cmp.w	_iInset,d2
	bgt	DS10013 		;SKIPI:
DS10012:			;iWidth=iInset=-1
	move.w	#-1,_iInset
	move.w	#-1,_iWidth 		;if(hWidth<0)mayMode=0
	tst.w	_hWidth
	bge	DS10014
	clr.w	_mayMode			;}elseiWidth=col
DS10014:
	bra	DS10015
DS10013:
	move.w	d2,_iWidth
DS10015:			;fastB=(SDB*)sBfastP=sPfastBufPtr=(SCB*)ss
	move.l	d3,_fastB
	move.l	a3,_fastP
	move.l	d4,_fastBufPtr  		;}
DS5:
	movem.l	(sp)+,d2-d4/a2/a3
	rts 		;voidinTrace(){

	xdef	_cursorMade
_cursorMade:			;madeCursor=TRUE
	move.w	#1,_madeCursor  		;oldCursorCol=col+1
	move.w	4(sp),d0
	addq.w	#1,d0
	move.w	d0,_oldCursorCol			;oldCursorLine=cline
	move.w	6(sp),_oldCursorLine			;}
DS8:
	rts 

	xdef	_FgetPageRowCol
_FgetPageRowCol:
	bset.b	#0,_displayFlag+1			; displayFlag|=SHOWCURSOR_ popFast()
	jsr	_popFast			;showRow(FALSE)
	clr.w	-(sp)
	jsr	_showRow			;*longPtrArgs[0]=gPage
	move.w	_gPage,a0
	move.l	_longPtrArgs,a1
	move.l	a0,(a1) 		;*longPtrArgs[1]=gRow
	move.w	_gRow,a0
	move.l	_longPtrArgs+4,a1
	move.l	a0,(a1) 		;*longPtrArgs[2]=gColm
	move.w	_gColm,a0
	move.l	_longPtrArgs+8,a1
	move.l	a0,(a1) 		;return(TRUE)
	moveq	#1,d0
	addq.w	#2,sp
DS12:
	rts 		;}voidshowRow(flag){

	xdef	_showRow
_showRow:
	movem.l	d2-d5/a2/a3,-(sp) 		 ;curB=curBuf
	move.l	_curBuf,a3  		;rrc=rc
	lea	_rc,a2  		 ;breakOk=TRUE
	move.w	#1,_breakOk 		;lines=locToLines(curB,curB->Block,curB->Ptr)
	move.l	80(a3),-(sp)
	move.l	(a3),-(sp)
	move.l	a3,-(sp)
	jsr	_locToLines
	move.l	d0,d2			;if(lines==-1L)gotoRTN
	cmp.l	#-1,d2
	lea	12(sp),sp
	beq	DS10026 		;i=curB->PageLines
	moveq	#0,d0
	move.w	174(a3),d0
	move.l	d0,d3			;if(i>1){
	cmp.l	#1,d3
	ble	DS10027 		;pages=1L+((lines-1)/i)
	move.l	d2,d0
	subq.l	#1,d0
	move.l	d3,d1
	jsr	.divs#
	move.l	d0,d4
	addq.l	#1,d4			;lines%=i
	move.l	d2,d0
	move.l	d3,d1
	jsr	.mods#
	move.l	d0,d2			;if(lines==0L)lines=i
;;;	tst.l	d2
	bne	DS10028
	move.l	d3,d2			;stci_d(rrc,pages)
DS10028:
	move.l	d4,-(sp)
	move.l	a2,-(sp)
	jsr	_stci_d 		;strcat(rrc,":")
	pea	DS1+0
	move.l	a2,-(sp)
	jsr	_strcat 		;catNumber(rrc,lines,30)
	move.w	#30,-(sp)
	move.l	d2,-(sp)
	move.l	a2,-(sp)
	jsr	_catNumber  		;}else{stci_d(rrc,lines)pages=0}
	lea	26(sp),sp
	bra	DS10029
DS10027:
	move.l	d2,-(sp)
	move.l	a2,-(sp)
	jsr	_stci_d
	moveq	#0,d4
	addq.w	#8,sp
DS10029:			;untakeFast()
	jsr	_untakeFast 		;cols=getCol()
	jsr	_getCol
	ext.l	d0
	move.l	d0,d5			;gPage=pagesgRow=linesgColm=cols
	move.w	d4,_gPage
	move.w	d2,_gRow
	move.w	d5,_gColm			;if(flag){
	tst.w	32(sp)
	beq	DS10030 		;strcat(rrc,",")
	pea	DS1+2
	move.l	a2,-(sp)
	jsr	_strcat 		;catNumber(rrc,cols,30)
	move.w	#30,-(sp)
	move.l	d5,-(sp)
	move.l	a2,-(sp)
	jsr	_catNumber  		;rowMsg()
	jsr	_rowMsg 		;}RTN:
	lea	18(sp),sp
DS10030:
DS10026:			;breakOk=FALSE
	clr.w	_breakOk			;}
DS13:
	movem.l	(sp)+,d2-d5/a2/a3
	rts
DS1:
	dc.b	58,0,44,0
	ds	0			;voidrowMsg(){

	xdef	_rowMsg
_rowMsg:
	movem.l	d2/a2/a3,-(sp)  		 ;RUCHAR*rrc,*yRjrrc=rc
	lea	_rc,a2  		 ;if(noDisplay==0&&!primitive&&rowCol&&*rrc!=0x00){
	tst.w	_noDisplay
	bne	DS15
	tst.w	_primitive
	bne	DS15
	tst.l	_rowCol
	beq	DS15
	tst.b	(a2)
	beq	DS15		 ;j=9
;;;	moveq	#9,d2			;if(depth==1)j=0
;;;	cmp.w	#1,_depth
;;;	bne	DS10032
;;;	moveq	#0,d2			;setColors(msgVid)
;;;DS10032:
	move.w	#9,-(sp)
	jsr	_setColors  		;wptr=rrc
	move.l	a2,_wptr			;y=rrc
	move.l	a2,a3			;while(*rrc)rrc++
	addq.w	#2,sp
DS10033:
	tst.b	(a2)
	beq	DS10034
	addq.l	#1,a2
	bra	DS10033
DS10034:			;j=rrc-y
	move.l	a2,d0
	sub.l	a3,d0
	move.w	d0,d2			;move(maxLineChars-16,0)
	clr.w	-(sp)
	move.w	_maxLineChars,d0
	sub.w	#16,d0
	move.w	d0,-(sp)
	jsr	_move			;text(j)
	move.w	d2,-(sp)
	jsr	_text			;cleareol()
	jsr	_cleareol			;return
	addq.w	#6,sp
DS15:
	movem.l	(sp)+,d2/a2/a3
	rts 		;}}
	bra	DS15			;locToLines(SCB*bufPtr,SDB*block,UCHAR*ptr){

	xdef	_locToLines
_locToLines:
	link	a5,#DS16
	movem.l	d2-d7/a2/a3,-(sp)
	move.l	16(a5),d4			;i=block->BlockNum
	move.l	12(a5),a0
	move.w	246(a0),-2(a5)  		;b=bufPtr->NextBlock
	move.l	8(a5),a0
	move.l	160(a0),a3  		;p=b->Data
	move.l	a3,a2			;lines=1L
	moveq	#1,d3			;PageLines=bufPtr->PageLines
;;;	move.l	8(a5),a0
	moveq	#0,d0
	move.w	174(a0),d0
	move.l	d0,d6			;TOP:
DS10035:			;if(i<1)gotoBOTTOM
	cmp.w	#1,-2(a5)
	bcs	DS10036 		;if(b->eols==255||i==1){
	cmp.b	#255,245(a3)
	beq	DS17
	cmp.w	#1,-2(a5)
	bne	DS10037
DS17:			;eols=0
	moveq	#0,d5			;while(1){
DS10038:			;if(()p==locSeek)locSeek=0L
	cmp.l	d4,a2
	bne	DS10040
	moveq	#0,d4			;ch=*p
DS10040:
	move.b	(a2),d2 		;if(ch>13||ch==0x00){p++continue}
	cmp.b	#13,d2
	bhi	DS18
	tst.b	d2
	bne	DS10041
DS18:
	addq.l	#1,a2
	bra	DS10038 		;if(ch==EOB)break
DS10041:
	cmp.b	#2,d2
	beq	DS10039 		;if(ch==EOFILE)break
	cmp.b	#3,d2
	beq	DS10039 		;if(ch==eol){
	cmp.b	_eol,d2
	bne	DS10042 		;PLAYEOL:
DS10043:			;if(locSeek)lines++
	tst.l	d4
	beq	DS10044
	addq.l	#1,d3			;if(eols<255)eols++
DS10044:
	cmp.w	#255,d5
	bge	DS10046
	addq.w	#1,d5			;}elseif(ch==12){
	bra	DS10046
DS10042:
	cmp.b	#12,d2
	bne	DS10047 		;if(PageLines<2)gotoPLAYEOL
	cmp.l	#2,d6
	blt	DS10043 		;eols=255
	move.w	#255,d5 		;if(locSeek){
	tst.l	d4
	beq	DS10048 		;j=(lines%PageLines)
	move.l	d3,d0
	move.l	d6,d1
	jsr	.mods#
	addq.l	#1,d3			;if(j)lines+=(PageLines-j)
	move.l	d0,d7			;lines++
;;;	tst.l	d7
	beq	DS10049
	move.l	d6,d0
	sub.l	d7,d0
	add.l	d0,d3			;}
DS10049:			;}
DS10048:			;p++
DS10047:
DS10046:
	addq.l	#1,a2			;}
	bra	DS10038
DS10039:			;b->eols=eols
	move.b	d5,245(a3)  		;}elselines+=b->eols
	bra	DS10050
DS10037:
	moveq	#0,d0
	move.b	245(a3),d0
	add.l	d0,d3
DS10050:			;b=b->NextBlock
	move.l	252(a3),a3  		;p=b->Data
	move.l	a3,a2			;i--
	add.w	#-1,-2(a5)  		;if(breakOk&&FinputWaiting()){
	tst.w	_breakOk
	beq	DS10035
	jsr	_FinputWaiting
	tst.w	d0
	beq	DS10035 		;lines=-1L
	moveq	#-1,d3  		;gotoBOTTOM
	bra	DS10036 		;}gotoTOP
;;;DS10051:
;;;	bra	DS10035 		;BOTTOM:
DS10036:			;return(lines)
	move.l	d3,d0
DS19:
	movem.l	(sp)+,d2-d7/a2/a3
	unlk	a5
	rts 		;}
DS16:	equ	-2  		;

#endasm
#endif

#ifndef USEC
#asm
	xdef	_setColors
_setColors:
	movem.l	d2-d6/a6,-(sp)
	move.w	28(sp),d2			;Rlongj,k,i1,i2oldColors=i
	move.w	d2,_oldColors			;if(depth==1){
	cmp.w	#1,_depth
	bne	NV10127 		;i1=1
	moveq	#1,d4			;i2=0
	moveq	#0,d5			;if(i&&i!=9){i1=0i2=1}
	tst.w	d2
	beq	NV10129
	cmp.w	#9,d2
	beq	NV10129
	moveq	#0,d4
	moveq	#1,d5			;}else{
;;;NV10128:
	bra	NV10129
NV10127:			;if(i<8)j=localColor
	cmp.w	#8,d2
	bge	NV10130
	move.l	_localColor,d3  		;else{j=screenColori-=8}
	bra	NV10131
NV10130:
	move.l	_screenColor,d3
	subq.w	#8,d2
NV10131:			;k=7-i
	moveq	#7,d0
	sub.w	d2,d0
;;;	ext.l	d0
	move.l	d0,d6			;k<<=2
	asl.l	#2,d6			;j>>=k
	asr.l	d6,d3			;i1=j>>2
	move.l	d3,d4
	asr.l	#2,d4			;i1&=3
	and.l	#3,d4			;i2=j&3
	move.l	d3,d5
	and.l	#3,d5			;}
NV10129:			;SetAPen(rp,i1)
	move.l	d4,d0
	move.l	_rp,a1
	move.l	_GfxBase,a6
	jsr	-342(a6)			;SetBPen(rp,i2)
	move.l	d5,d0
	move.l	_rp,a1
	move.l	_GfxBase,a6
	jsr	-348(a6)			;mask1=rp->minterms[0]
	move.l	_rp,a0
	moveq	#0,d0
	move.b	40(a0),d0
	move.l	d0,_mask1			;mask2=rp->minterms[1]
;;;	move.l	_rp,a0
;;;	moveq	#0,d0
	move.b	41(a0),d0
	move.l	d0,_mask2			;}
NV18:
	movem.l	(sp)+,d2-d6/a6
	rts 		;/*-ue16.c-*/staticlongasize,chipHupChip(){

#endasm
#endif

