/* --------------------------- Ue5.c ------------------------- */

#include "uedit.h"

/* #define USEC 1 */

Fmenu()
{
register struct MenuItem   *item,**x;
struct MenuItem 	**y;
register  short 	menuNum,macNum,i,k;
register  long  	j;
register  USHORT	flags;
UCHAR				*name,*name2;
struct   IntuiText  *text,*stext;
struct   Menu		*mPtr;

	menuNum =  *longPtrArgs[2] - 1;
	macNum  =  *longPtrArgs[3];
	name = (UCHAR *)ptrArgs[0];
	if (name==NULL || *name==0x00) goto BRTN;
	i = (short)strlen(name) + 1;
	name2 = ptrArgs[1];
	if (name2!=NULL && *name2>' ') {
		j = (short)strlen(name2);
		if (++j > i) i = j;
	} else name2=NULL;

	if (i==1 || menuNum>=MAXMENUS || macNum<=0 || macNum>=MAXMACROS) goto BRTN;
	mPtr = edMenu[menuNum];
	if (mPtr==NULL) {						 /* allocate menu header struct */
		if (subflag) goto BRTN; 		/*  no submenus if no menu yet */
		if((mPtr = (struct Menu *)safeMem((long)sizeof(struct Menu)))
			== NULL) goto BRTN;
		i *= charWidth;
		edMenu[menuNum] = mPtr;
		mPtr->MenuName = (char *)name;

		mPtr->LeftEdge = menuInset;
		menuInset += (i + charWidth);
		mPtr->Height = charHeight;
		mPtr->Width = i;
		mPtr->Flags = MENUENABLED;
	} else {								 /* must be adding an item */
		if (cmdTable[macNum]==NULL) goto BRTN;
													/* find last item in chain */
		for (i=0; i<MAXITEMS; i++) {
			if (i==0) { x = &(mPtr->FirstItem); y = NULL; }
			else	{ y = &((*x)->SubItem); x = &((*x)->NextItem); }
			if (*x==NULL) break;			/* always true,  even for subitems */
		}
		if (*x!=NULL) goto BRTN;

		if (subflag) {  							/* find last subitem */
			if ((x=y)==NULL) goto BRTN; 	/* no subitem allowed here */
			i--;
			if (*x==NULL) k  = 0;
			else for (k=1; k<MAXSUBITEMS; k++) {
				x = &((*x)->NextItem);
				if (*x==NULL) break;
			}
			if (*x!=NULL) goto BRTN;
		} else k = 0;

		/* allocate the item */
		if ( (item = (struct MenuItem *)safeMem((long)sizeof(struct MenuItem)))
				== NULL) goto BRTN;
		if (
			(text =(struct IntuiText *)safeMem((long)sizeof(struct IntuiText)))
				== NULL) {
OUT2:
			FreeMem(item,(long)sizeof(struct MenuItem));
			goto BRTN;
		}
		if (name2!=NULL &&
			(stext =(struct IntuiText *)safeMem((long)sizeof(struct IntuiText)))
				== NULL) {
			FreeMem(text,(long)sizeof(struct IntuiText));
			goto OUT2;
		}

		j = (MAXMENUWIDTH * charWidth) /* + CHECKWIDTH */;
		if (subflag) item->LeftEdge = (j * menuOffset)/100;

		if ((menuInset + j + item->LeftEdge)>=horPixels)
				item->LeftEdge = horPixels - (menuInset + j);
		if (!subflag) item->TopEdge  = (i * charHeight);
		else item->TopEdge  = (k * charHeight);
		*x = item;  	/* new end of chain */
		menuMacroNum[(menuNum * MAXITEMS * MAXSUBITEMS) +
						(i * MAXSUBITEMS) + k] = macNum;
		item->Width = j;
		item->Height   = charHeight;
		flags = ITEMENABLED | ITEMTEXT;
		item->ItemFill = (APTR)text;

		if (depth==1) {
			text->FrontPen = 1;
			text->BackPen = 0;
		} else {
			j = (screenColor >> 20L) & 15;
			text->FrontPen = j >> 2;
			text->BackPen  = j & 3;
		}
		text->DrawMode = JAM2 /* | INVERSVID */;
		text->IText = (UBYTE *)name;
/*     	text->LeftEdge = CHECKWIDTH; */
		if (checkit) {
				j = text->FrontPen;
				text->FrontPen = text->BackPen;
				text->BackPen = j;
				flags |= (CHECKIT  /* | MENUTOGGLE */ );
				(UBYTE)item->Command = checkit;
		}
		if (name2!=NULL) {
			item->SelectFill = (APTR)stext;
			movdat((UCHAR *)text,(UCHAR *)stext,(short)sizeof(struct IntuiText));
			stext->IText = (UBYTE *)name2;
			/* flags |= HIGHIMAGE; */
		} else flags |=   HIGHCOMP;
		item->Flags = flags;
	}
	return(TRUE);
BRTN:
	return(FALSE);
}

static short   caseSense;

FsortLines()	/* sortLines(buf) */
{
register UCHAR  	**p,*pp;
register short  	bufNum,eoline,ch;
register long		lines,j;

UCHAR				**ps;
struct CB			*bufPtr;
short				rtn,iscurfile;


	rtn = iscurfile = FALSE;
	ps = NULL;
	bufPtr = bufPtrArgs[0];
	bufNum = *longPtrArgs[0];
	if (bufPtr==NULL) goto RTN;
	if (bufPtr==curBuf) iscurfile = TRUE;
	caseSense = (short)*longPtrArgs[1];
	oneMsg("Prep");
	if (bufPtr->Flags & NEEDPACK) packBuf(bufPtr,TRUE);
	eoline = eol;

	pushFast(bufPtr,&bufPtr->Block,&bufPtr->Ptr);

	/* see if eof needs an eol */
	eFileFast();
	decFast();
	if (*fastP!=eol) {
		incFast();
		stickChar(eol);
	}

	/* count lines */
	sFileFast();
	lines = 0L;
	ch = *fastP;
	eoline = eol;
	if (ch!=eoline && ch!=12) ch = *eLineFast();
	while (1) {
		if (ch==EOFILE) break;
		lines++;
		ch = *eLineFast();
	}
	if (lines<2L) goto RTN;
	passiveMsg("",NULL,lines);

	/* alloc line ptr array */
	ps = (UCHAR **)safeMem(lines << 2L);
	if (ps==NULL) goto RTN;

	/* store line beginnings */
	sFileFast();
	p = ps;
	j = 0L;
	while (1) {
		*(p++) = fastP;
		if (*fastP!=eoline && *fastP!=12) eLineFast();
		incFast();
		if (++j>=lines) break;
	}

	/* do gap sort on lines, swapping pointers, not lines themselves */
	if (!sortEm(lines,(long)ps)) goto RTN;

	/* copy lines into new buffer */
	oneMsg("...");
	if (!fixBuf(ALLBUFS)) goto RTN;
	bufPtr = buf[ALLBUFS];
	strcpy(bufPtr->FileName,buf[bufNum]->FileName);
	pushFast(bufPtr,&bufPtr->Block,&bufPtr->Ptr);
	eFileFast();

	if (!copyLines(ps,lines)) goto RTN;
	sFileFast();
	takeFast();

	oneMsg("Done");
	freeBuf(bufNum);
	buf[bufNum] = bufPtr;
	if (iscurfile) curBuf = bufPtr;
	buf[ALLBUFS] = NULL;
	bufPtr->BufNum = bufNum;
	bufPtr->PBlock = bufPtr->NextBlock;
	bufPtr->PPtr   = bufPtr->PBlock->Data;
	rtn = TRUE;

RTN:
	splitFlag(bufNum,PAGE_ | SHOWCURSOR_);
	if (ps) FreeMem(ps,(long)lines << 2L);
	if (buf[ALLBUFS]!=NULL) freeBuf(ALLBUFS);
	popFast();
	return(rtn);
}

#ifdef USEC

copyLines(ps,lines)
UCHAR			**ps;
register long	lines;
{
register UCHAR  **p,*pp;
register short  ch,eoline;
register long	j;

	p = ps;
	eoline = eol;
	for (j=0; j<lines; j++) {
		pp = *p;
		while (1) { 							   /* xfer line to new buf */
DOMORE:
			ch = *pp;
			if (ch<=EOFILE) {
				if (ch==EOB) {
					pp -= DATASIZE;
					pp = ( (struct DB *)pp)->NextBlock->Data;
					goto DOMORE;
				}
				if (ch==EOFILE) goto DOLESS;
			}
			if (!stickChar(ch)) return(FALSE);
			if (ch<=13) {
				if (ch==eoline || ch==12) goto DOLESS;
			}
			pp++;
		}
DOLESS:
		p++;
	}
	return(TRUE);
}

sortEm(lines,ps)
register long		lines,ps;
{
register UCHAR  	**p,**pp;
register short  	swapped;
register long		j,gap,ppp;

	gap = lines;
	while (1) {
		gap >>= 1L;
		if (gap==0L) break;
		passiveMsg("",NULL,gap);
		if (stop()) return(FALSE);
REPASS:
		swapped = FALSE;
		p = (UCHAR **)ps;
		pp = p + gap;
		j = lines - gap;
		do {
			if (cmpLines(*p,*pp)) { /* trade the lines */
				ppp = (long)*pp;
				*pp = *p;
				*p = (UCHAR *)ppp;
				swapped = TRUE;
			}
			p++;
			pp++;
		} while (--j>0L);
		if (swapped) goto REPASS;
	}
	return(TRUE);
}

cmpLines(p,pp)  	 /* returns TRUE if p>pp & lines should be traded */
register UCHAR  	 *p;
register UCHAR  	 *pp;
{
register short  i,ii,eoline,sense;

	eoline = eol;
	sense = caseSense;
	while (1) {
AGAIN1:
		i = *p;
		if (i<=13) {
			if (i==eoline || i==12) return(0);
			if (i==EOB)  {
				p -= DATASIZE;
				p = ( (struct DB *)p)->NextBlock->Data;
				goto AGAIN1;
			}
		}
AGAIN2:
		ii = *pp;
		if (ii<=13) {
			if (ii==eoline || ii==12) return(1);
			if (ii==EOB) {
				pp -= DATASIZE;
				pp = ( (struct DB *)pp)->NextBlock->Data;
				goto AGAIN2;
			}
		}
		if (sense==0) {
			if (i<='Z' && i>='A') i += 32;
			if (ii<='Z' && ii>='A') ii += 32;
		}
		ii -= i;
		if (ii>0) return(0);
		if (ii<0) return(1);
		p++;
		pp++;
	}
}

#endif

#ifndef USEC
#asm
	xref	_eol
	xref	_cmpLines
	xref	_eol
	xref	_passiveMsg
	xref	_stickChar
	xref	_stop

	xdef	_copyLines
_copyLines:
	movem.l	d2-d4/a2/a3,-(sp)			;RUCHAR**p,*ppRshortch,eolineRlongjp=ps
	move.l	24(sp),a3			;eoline=eol
	moveq	#0,d3
	move.b	_eol,d3
	moveq	#0,d4			;for(j=0j<linesj++){
	bra	ML10004
ML10003:			;pp=*p
	move.l	(a3),a2			;while(1){/*xferlinetonewbuf*/
ML10005:			;DOMORE:
ML10007:			;ch=*pp
	moveq	#0,d2
	move.b	(a2),d2
	cmp.w	#3,d2			;if(ch<=EOFILE){
	bgt	ML10008			;if(ch==EOB){
	cmp.w	#2,d2
	bne	ML10009			;pp-=DATASIZE
	lea	-244(a2),a2			;pp=((structDB*)pp)->NextBlock->Data
	move.l	252(a2),a2			;gotoDOMORE
	bra	ML10007			;}if(ch==EOFILE)gotoDOLESS
ML10009:
	cmp.w	#3,d2
	beq	ML10010			;}if(!stickChar(ch))return(FALSE)
ML10008:
	move.w	d2,-(sp)
	jsr	_stickChar
	tst.w	d0
	addq.w	#2,sp
	bne	ML10011
	moveq	#0,d0
ML2:
	movem.l	(sp)+,d2-d4/a2/a3
	rts			;if(ch<=13){
ML10011:
	cmp.w	#13,d2
	bgt	ML10012			;if(ch==eoline||ch==12)gotoDOLESS
	cmp.w	d3,d2
	beq	ML10010
	cmp.w	#12,d2
	beq	ML10010			;}pp++
ML10012:
	addq.l	#1,a2			;}
	bra	ML10005			;DOLESS:
ML10010:			;p++
	addq.l	#4,a3			;}
ML10001:
	addq.l	#1,d4
ML10004:
	cmp.l	28(sp),d4
	blt	ML10003
ML10002:			;return(TRUE)
	moveq	#1,d0
	bra	ML2			;}

	xdef	_sortEm
_sortEm:
	movem.l	d2-d7/a2/a3,-(sp)
	move.l	36(sp),d6
	move.l	40(sp),d7			;RUCHAR**p,**ppRshortswappedRlongj,gap,pppgap=lines
	move.l	d6,d3			;while(1){
SL10001:			;gap>>=1L
	asr.l	#1,d3			;if(gap==0L)break
	tst.l	d3
	beq	SL10002			;if(stop())return(FALSE)
	move.l	d3,-(sp)
	clr.l   -(sp)
	pea	SL1+0
	jsr	_passiveMsg						;passiveMsg("",NULL,gap)
	lea	12(sp),sp
	jsr	_stop
	tst.w	d0
	beq	SL10003
	moveq	#0,d0
SL2:
	movem.l	(sp)+,d2-d7/a2/a3
	rts			;REPASS:
SL10003:
SL10004:
	moveq	#0,d4			;swapped=FALSE p=(UCHAR**)ps
	move.l	d7,a2			;pp=p+gap
	move.l	d3,a3
;;	asl.l	#2,d0
	add.l	a3,a3
	add.l	a3,a3
	add.l	a2,a3			;j=lines-gap
	move.l	d6,d5
	sub.l	d3,d5			;do{
SL10007:			;if(cmpLines(*p,*pp)){/*tradethelines*/
	move.l	(a3),-(sp)
	move.l	(a2),-(sp)
	jsr	_cmpLines
	tst.w	d0
	addq.w	#8,sp
	beq	SL10008			;ppp=(long)*pp
	move.l	(a3),d2			;*pp=*p
	move.l	(a2),(a3)			;*p=(UCHAR*)ppp
	move.l	d2,(a2)			;swapped=TRUE
	moveq	#1,d4			;}p++
SL10008:
	addq.l	#4,a2			;pp++
	addq.l	#4,a3			;}while(--j>0L)
SL10005:
	subq.l	#1,d5
	bgt	SL10007
SL10006:			;if(swapped)gotoREPASS
	tst.w	d4
	bne	SL10004			;}
	bra	SL10001
SL10002:			;return(TRUE)
	moveq	#1,d0
	bra	SL2			;}
SL1:
	dc.b	0,119,0
	ds	0			;

	xdef	_cmpLines
_cmpLines:
	movem.l	d2-d5/a2/a3,-(sp)
	move.l	28(sp),a2
	move.l	32(sp),a3		;Rshorti,ii,eolineeoline=eol
	moveq	#0,d4
	move.b	_eol,d4
	moveq	#0,d5
	move.w  _caseSense,d5
SORT10001:		;AGAIN1:		;while(1){
SORT10003:		;i=*p
	moveq	#0,d3
	move.b	(a2),d3
	cmp.w	#13,d3		;if(i<=13){
	bgt	SORT10004		;if(i==eoline||i==12)return(0)
	cmp.w	d4,d3
	beq	SORT2
	cmp.w	#12,d3
	bne	SORT10005
SORT2:
	moveq	#0,d0
SORT3:
	movem.l	(sp)+,d2-d5/a2/a3
	rts		;if(i==EOB){
SORT10005:
	cmp.w	#2,d3
	bne	SORT10006		;p-=DATASIZE
	lea	-244(a2),a2		;p=((structDB*)p)->NextBlock->Data
	move.l	252(a2),a2		;gotoAGAIN1
	bra	SORT10003		;}}
SORT10006:		;AGAIN2:
SORT10004:
SORT10007:		;ii=*pp
	moveq	#0,d2
	move.b	(a3),d2
	cmp.w	#13,d2  		;if(ii<=13){
	bgt	SORT10008		;if(ii==eoline||ii==12)return(1)
	cmp.w	d4,d2
	beq	SORT4
	cmp.w	#12,d2
	bne	SORT10009
SORT4:
	moveq	#1,d0
	bra	SORT3		;if(ii==EOB){
SORT10009:
	cmp.w	#2,d2
	bne	SORT10010		;pp-=DATASIZE
	lea	-244(a3),a3		;pp=((structDB*)pp)->NextBlock->Data
	move.l	252(a3),a3		;gotoAGAIN2
	bra	SORT10007		;}}
SORT10010:
SORT10008:
	tst.w	d5  		; if (caseSense) {
	bne SORT10012
	cmp.w	#90,d3  	;if(i<='Z'&&i>='A')i+=32
	bgt	SORT10011
	cmp.w	#65,d3
	blt	SORT10011
	add.w	#32,d3		;if(ii<='Z'&&ii>='A')ii+=32
SORT10011:
	cmp.w	#90,d2
	bgt	SORT10012
	cmp.w	#65,d2
	blt	SORT10012
	add.w	#32,d2
SORT10012:		;ii-=i
	sub.w	d3,d2		;if(ii>0)return(0)
;;;	tst.w	d2
	ble	SORT10013
	moveq	#0,d0
	bra	SORT3		;if(ii<0)return(1)
SORT10013:
;;;	tst.w	d2
	bge	SORT10014
	moveq	#1,d0
	bra	SORT3		;p++
SORT10014:
	addq.l	#1,a2		;pp++
	addq.l	#1,a3		;}
	bra	SORT10001		;}
#endasm
#endif


FvScroll()
{
register UCHAR  **ovl;
register struct CB  *curB;
register short  i,j,amt,rtn;

	curB = curBuf;
	colTrack = -1;

	pushFast(curB,&(curB->PBlock),&(curB->PPtr));
	j = args[0];
	rtn = FALSE;
	if (j<=byteArg_) {
		amt = (short) *longPtrArgs[0];
		if (amt==0) goto ENDOUT;
DOAMT:
		i = 0;
		j = 0;
		do {
			ovl = (UCHAR **)fastP;
			if (amt<0) { upLineFast();   j--;}
			else	{ downLineFast(); j++;}
			if (fastP==(UCHAR *)ovl) break;
			i = j;
		} while (j!=amt);
		colTrack = -1;
		if (i) {
			takeFast();
			vscrollamt += i;
			displayFlag |= VSCROLL_;
			rtn=TRUE;
		}
		goto ENDOUT;
	}
	if (j==upLine_) { amt = -1; goto DOAMT; }
	if (j==downLine_) { amt = 1; goto DOAMT; }
	if (j<=mouseLoc_ && j>=atCursor_) {
		if (j==atCursor_) {
			cursorMid(TRUE);
			if (fastP!=curB->PPtr) rtn=TRUE;
			goto ENDOUT;
		}
		if (j==sPage_) { sPageFast(); goto TAKEIT; }
		if (j==ePage_) { ePageFast(); goto TAKEIT; }
		ovl = (UCHAR **)curB;
		fastB = (struct DB *)ovl[j - atCursor_ ];
		fastP = ovl[j - atCursor_ + CBOVL ];
CENTERLINE:
		amt = stopSplit[curSplit] - startSplit[curSplit];
		amt >>= 1;
		while (amt) { sLineFast(); amt--; }
	} else switch(j) {
		case  sFile_:	sFileFast(); break;
		case  eFile_:	eFileFast(); goto CENTERLINE;
		case  sForm_:	sFormFast(); goto CENTERLINE;
		case  eForm_:	eFormFast(); goto CENTERLINE;
	}
TAKEIT:
	if (fastP!=curB->PPtr) {
		rtn=TRUE;
		takeFast();
		displayFlag = (PAGE_ << curSplit);
	}
ENDOUT:
	popFast();
	return(rtn);
}

FhScroll()
{
register struct CB *curB;
register short rtn;
register long amt,i;

	rtn = FALSE;
	curB = curBuf;
	amt = *(longPtrArgs[0]);
	i = amt + curB->PageCol;
	hscrollamt += amt;

	if (i & 0XFFFF8000)
	{ i=0; hscrollamt = 0; displayFlag |= (PAGE_ << curSplit); }
	else if (amt) {
		displayFlag |= HSCROLL_;
		rtn = TRUE;
	}
RTN:
	curB->PageCol = i;
	return(rtn);
}

#ifdef USEC

FinsertRgn()
{
register UCHAR  	*fromP,*ffP;
register short  	inBuf,ch;
register ULONG  	bPtr,toP;
short					rtn,shifting;
struct DB				*fromB;
struct CB				*toBufPtr;

	rtn = FALSE;
	if (args[2]==buf_ ) {								/* block data */
		inBuf = *longPtrArgs[2];
		if (buf[inBuf]==NULL) goto RTN;
	} else inBuf = 255; 							/* literal data */
	ch = *longPtrArgs[0];							/* target buf num */
	if (inBuf==ch || !getBuf((USHORT)ch)) goto RTN;  /* may get NEW buf */
	bPtr = (ULONG)buf[ch];  	/* guaranteed existing TO buf here */
	toBufPtr = (struct CB *)bPtr;
	if (!canChange((struct CB *)bPtr)) goto RTN;

	if (inBuf!=255) {
		getBP(buf[inBuf],3);
		if (fB==NULL || gB==NULL) goto RTN;
		toP   = (ULONG)gP;
		fromP = fP;
		fromB = fB;
		if (toP==(ULONG)fromP || *fromP==EOFILE) goto RTN;
	}
	getBP((struct CB *)bPtr,1);  						/* the TO buf target loc */
	if (gB==NULL) goto RTN;
	ffP = gP;
	if (ffP==toBufPtr->PPtr) shifting = TRUE;	/* the TO insert location */
	else shifting = FALSE;
	splitFlag((short)ch,PAGE_ | SHOWCURSOR_);

	gB->eols = 255;
	fastBufPtr = (struct CB *)bPtr;
	fastB = gB;
	ggP = ffP;  					 /* WARNING:  stickChar looks for this! */
	stickMode = 0;

	if (inBuf==255)  {  								/* a literal */
		fromP = (UCHAR *)ptrArgs[2];
		if (*fromP==0x00) goto RTN;
		goto TAKE1;
		while (1) {
				if (ffP[1]) {
					fastP = ffP;
					if (!stickChar((short)ch)) goto BADOUT;
					ffP = fastP;
				} else {
					ffP[1] = *ffP;
					*(ffP++) = ch;
				}
TAKE1:
				ch = *(fromP++);
				if (ch==0) break;
		}
	} else {												/* block data */
		inBuf = EOFILE;
		while (1) {
			if (fromP==(UCHAR *)toP) break;
			ch = *(fromP++);
			if (ch<=inBuf) {
				if (ch==0x00) continue;
				if (ch==EOB) {
					fromB = fromB->NextBlock;
					if (fromB==NULL) break;
					fromP = fromB->Data;
					continue;
				}
				if (ch==inBuf) break;
			}
			if (ffP[1]==0) {
				ffP[1] = *ffP;
				*(ffP++) = ch;
			} else {
				fastP = ffP;
				if (!stickChar((short)ch)) goto BADOUT;
				ffP = fastP;
			}
		}
	}
	rtn = TRUE;
BADOUT:
	toBufPtr->Flags |= (NEEDPACK | CHANGED);
	matchPtrs((UCHAR **)bPtr,(ULONG)fastB,(ULONG)ggP,(ULONG)ffP);

	if (shifting) shiftit((struct CB *)bPtr);
				/* WARNING - must have POPFAST AFTER this */
	popFast();
RTN:
	return(rtn);
}

FclearRgn()
{
register UCHAR  	*gpReg,*ffP;
register short  	disp,ch;
register long		locpt,bufp;


	if (args[1]==all_) return(FclearBuf());
	bufp = (long)bufPtrArgs[0];
	if (bufp==0L || !canChange((struct CB *)bufp)) goto BRTN;

	getBP((struct CB *)bufp,1);
	gpReg = gP; 						/* the ending point */
	ffP = fP; fastB = fB;
	if (fB==NULL || gB==NULL || *ffP==EOFILE || gpReg==ffP) goto BRTN;
	disp = 0;
	goto TAKE;
	while (1) {
		if (gpReg==ffP) break;
		if (ch<14) {
			if (ch==eol || ch==12) {
				fastB->eols = 255;
				disp = PAGE_;
				goto TAKEIT;
			}
			if (ch==0) goto NEXT;
			if (ch==EOFILE) break;
		}
TAKEIT:
		*ffP = 0x00;
NEXT:
		ffP++;
TAKE:
		ch = *ffP;
		if (ch==EOB) { fastB = fastB->NextBlock; ffP = fastB->Data; goto TAKE; }
	}
	if (ch==EOFILE) {					/* move EOFILE bkwd to last data */
		*fP = EOFILE; *ffP = 0x00;
		gpReg = fP;    gB = fB;
	}

	((struct CB *)bufp)->Flags |= (NEEDPACK | CHANGED);
	fixPtrs((UCHAR **)bufp,(ULONG)gB,(ULONG)gpReg);
	fixSOF((struct CB *)bufp);
	if (bufp==(long)curBuf) {
		disp |= SHOWCURSOR_;
		locpt = loc(((struct CB *)bufp)->PBlock,((struct CB *)bufp)->PPtr);
		if (loc(fB,fP)<=locpt && loc(gB,gP)>=locpt) {
			disp = PAGE_;
			shiftit((struct CB *)bufp);
						/* WARNING - must have POPFAST AFTER this */
		}
	}
	popFast();
	splitFlag(((struct CB *)bufp)->BufNum,disp);
	return(TRUE);
BRTN:
	return(FALSE);
}

FclearBuf()
{
register struct CB  *bufPtr;
register struct DB  *b;

register short  	disp,i;
register long		bb;

	bufPtr = bufPtrArgs[0];
	if (bufPtr==NULL || !canChange(bufPtr)) return(FALSE);

	disp = PAGE_;
	b = bufPtr->NextBlock;
	if (b->Data[0]==EOFILE) goto RTNPT;
	bufPtr->Flags |= CHANGED;

	b = b->NextBlock;					 	/* delete chain, all but first */
	while (b) {
		bb = (long)b->NextBlock;
		FreeMem(b,(long)sizeof(struct DB));
		b = (struct DB *)bb;
	}

	b = bufPtr->NextBlock;
	b->NextBlock = NULL;
	bufPtr->PageCol = 0;

	i = DATASIZE >> 2;  				  /* null out the first data block */
TOP1:
		*( ((long *)b)++ ) = 0L;

		if (--i > 0) goto TOP1;

	b = bufPtr->NextBlock;
	b->Data[0] = EOFILE;
	b->eols = 0;

	i = CBOVL << 1; 				/* point CB ptrs to first block & byte */
TOP2:
		*(((UCHAR **)bufPtr)++) = (UCHAR *)b;
		if (--i>0) goto TOP2;

	bufPtr = bufPtrArgs[0];
	if (bufPtr == curBuf) {
		if (noDisplay==0) {
			i = startSplit[curSplit] + 1;
			if (linePtrs[i]!=NULL) {
				linePtrs[i]   = b->Data;
				lineBlocks[i] = b;
			}
		}
		disp |= SHOWCURSOR_;
	}
RTNPT:
	if (bufPtr==curBuf) popFast();
	splitFlag(bufPtr->BufNum,disp);
	return(TRUE);
}

#endif

#asm
	XREF	_args
	XREF	_bufPtrArgs
	XREF	_canChange
	XREF	_checkit
	XREF	_curBuf
	XREF	_CurrentDir
	XREF	_eol
	XREF	_fastB
	XREF	_fastBufPtr
	XREF	_fastP
	XREF	_fB
	XREF	_fixPtrs
	XREF	_fixString
	XREF	_Fmenu
	XREF	_fP
	XREF	_gB
	XREF	_getBP
	XREF	_getBuf
	XREF	_getCol
	XREF	_ggP
	XREF	_gP
	XREF	_Lock
	XREF	_longPtrArgs
	XREF	_matchPtrs
	XREF	_oldCursorCol
	XREF	_onScreen
	XREF	_packBuf
	XREF	_popFast
	XREF	_ptrArgs
	XREF	_pushFast
	XREF	_shiftit
	XREF	_splitFlag
	XREF	_stickChar
	XREF	_stickMode
	XREF	_subflag
	XREF	_fixSOF
	XREF	_lineBlocks
	XREF	_linePtrs
	XREF	_noDisplay

#endasm

#ifndef USEC
#asm

	xdef	_FclearBuf
_FclearBuf:
	movem.l	d2-d4/a2/a3/a6,-(sp) 		;RSCB*bufPtrRSDB*bRshortdisp,iRlongbbbufPtr=bufPtrArgs[0]
	move.l	_bufPtrArgs,a3  		;if(bufPtr==NULL||!canChange(bufPtr))return(FALSE)
	move.l	a3,d0
	beq	CB2
	move.l	a3,-(sp)
	jsr	_canChange
	tst.w	d0
	addq.w	#4,sp
	bne	CB10001
CB2:
	moveq	#0,d0
CB3:
	movem.l	(sp)+,d2-d4/a2/a3/a6
	rts 		;disp=PAGE_
CB10001:
	moveq	#16,d4  		;b=bufPtr->NextBlock
	move.l	160(a3),a2  		;if(b->Data[0]==EOFILE)gotoRTNPT
	cmp.b	#3,(a2)
	beq	CB10002 		;bufPtr->Flags|=CHANGED
	lea	168(a3),a0
	bset.b	#0,2(a0)			;b=b->NextBlock/*deletechain,allbutfirst*/
	move.l	252(a2),a2  		;while(b){
CB10003:
	move.l	a2,d0
	beq	CB10004 		;bb=(long)b->NextBlock
	move.l	252(a2),d3  		;FreeMem(b,(long)sizeof(SDB))
	move.l	#256,d0
	move.l	a2,a1
	move.l	_SysBase#,a6
	jsr	-210(a6)			;b=(SDB*)bb
	move.l	d3,a2			;}
	bra	CB10003
CB10004:			;b=bufPtr->NextBlock
	move.l	160(a3),a2  		;b->NextBlock=NULL
	clr.l	252(a2) 		;bufPtr->PageCol=0
	clr.w	164(a3) 		;i=DATASIZE>>2/*nulloutthefirstdatablock*/
	moveq	#61,d2  		;TOP1:
CB10005:			;*(((long*)b)++)=0L
	clr.l	(a2)+			;if(--i>0)gotoTOP1
	subq.w	#1,d2
	bgt	CB10005 		;b=bufPtr->NextBlock
	move.l	160(a3),a2  		;b->Data[0]=EOFILE
	move.b	#3,(a2) 		;b->eols=0
	clr.b	245(a2) 		;i=CBOVL<<1/*pointCBptrstofirstblock&byte*/
	moveq	#40,d2  		;TOP2:
CB10006:			;*(((UCHAR**)bufPtr)++)=(UCHAR*)b
	move.l	a2,(a3)+			;if(--i>0)gotoTOP2
	subq.w	#1,d2
	bgt	CB10006 		;bufPtr=bufPtrArgs[0]
	move.l	_bufPtrArgs,a3  		;if(bufPtr==curBuf){
	cmp.l	_curBuf,a3
	bne	CB10007 		;if(noDisplay==0){
	tst.w	_noDisplay
	bne	CB10008 		;i=startSplit[curSplit]+1
	move.w	_curSplit,d0
	add.w	d0,d0
	lea	_startSplit,a0
	move.w	(a0,d0.w),d2
	addq.w	#1,d2
	move.w	d2,d0
	asl.w	#2,d0
	lea	_linePtrs,a0
	tst.l	(a0,d0.w)			;if(linePtrs[i]!=NULL){
	beq	CB10009
;;;	lea	_linePtrs,a0
	move.l	a2,(a0,d0.w)		 ;linePtrs[i]=b->Data
	lea	_lineBlocks,a0  		  ;lineBlocks[i]=b
	move.l	a2,(a0,d0.w)			;}}
CB10009:			;disp|=SHOWCURSOR_
CB10008:
	bset.l	#0,d4			;}RTNPT:
CB10007:
CB10002:			;if(bufPtr==curBuf)popFast()
	cmp.l	_curBuf,a3
	bne	CB10010
	jsr	_popFast			;splitFlag(bufPtr->BufNum,disp)
CB10010:
	move.w	d4,-(sp)
	move.w	166(a3),-(sp)
	jsr	_splitFlag  		;return(TRUE)
	moveq	#1,d0
	addq.w	#4,sp
	bra	CB3 		;}

	xdef	_FclearRgn
_FclearRgn:
	movem.l	d2-d5/a2/a3,-(sp) 		;RUCHAR*gpReg,*ffPRshortdisp,chRlonglocpt,bufpbufp=(long)bufPtrArgs[0]

	cmp.b	#134,_args+1		;if(args[1]==all_)return(clearBuf())
	bne	CCRR
	jsr	_FclearBuf
	bra CR3
CCRR:
	move.l	_bufPtrArgs,d3  		;if(bufp==0L||!canChange((structCB*)bufp))gotoBRTN
	tst.l	d3
	beq	CL10011
	move.l	d3,-(sp)
	jsr	_canChange
	tst.w	d0
	addq.w	#4,sp
	bne CL10012
CR3:
	movem.l	(sp)+,d2-d5/a2/a3
	rts 		;getBP((structCB*)bufp,1)

CL10012:
	move.w	#1,-(sp)
	move.l	d3,-(sp)
	jsr	_getBP  		;gpReg=gP/*theendingpoint*/
	move.l	_gP,a3  		;ffP=fPfastB=fB
	move.l	_fP,a2
	move.l	_fB,_fastB  		;if(fB==NULL||gB==NULL||*ffP==EOFILE||gpReg==ffP)gotoBRTN
	tst.l	_fB
	addq.w	#6,sp
	beq	CL10011
	tst.l	_gB
	beq	CL10011
	cmp.b	#3,(a2)
	beq	CL10011
	cmp.l	a2,a3
	beq	CL10011 		;disp=0
	moveq	#0,d2
	moveq	#0,d4			;gotoTAKE
	bra	CL10013 		;while(1){
CL10014:			;if(gpReg==ffP)break
	cmp.l	a2,a3
	beq	CL10015 		;if(ch<14){
	cmp.b	#14,d2
	bge	CL10016 		;if(ch==eol||ch==12){
	cmp.b	_eol,d2
	beq	CR4
	cmp.b	#12,d2
	bne	CL10017
CR4:			;fastB->eols=255
	move.l	_fastB,a0
	move.b	#255,245(a0)			;disp=PAGE_
	moveq	#16,d4  		;gotoTAKEIT
	bra	CL10018 		;}if(ch==0)gotoNEXT
CL10017:
	tst.b	d2
	beq	CL10019 		;if(ch==EOFILE)break
	cmp.b	#3,d2
	beq	CL10015 		;}TAKEIT:
CL10016:
CL10018:			;*ffP=0x00
	clr.b	(a2)			;NEXT:
CL10019:			;ffP++
	addq.w	#1,a2			;TAKE:
CL10013:			;ch=*ffP
	move.b	(a2),d2 	   ;if(ch==EOB){fastB=fastB->NextBlockffP=fastB->DatagotoTAKE}
	cmp.b	#2,d2
	bne	CL10014
	move.l	_fastB,a0
	move.l	252(a0),a2
	move.l  a2,_fastB
	bra	CL10013 		;}
CL10015:			;if(ch==EOFILE){/*moveEOFILEbkwdtolastdata*/
	cmp.b	#3,d2
	bne	CL10021 		;*fP=EOFILE*ffP=0x00
	move.l	_fP,a0
	move.b	#3,(a0)
	clr.b	(a2)			;gpReg=fPgB=fB
	move.l	_fP,a3
	move.l	_fB,_gB 		;}((structCB*)bufp)->Flags|=(NEEDPACK|CHANGED)
CL10021:
	move.l	d3,a0
;;;	add.l	#168,a0
	or.l	#8448,168(a0)  		;fixPtrs((UCHAR**)bufp,(ULONG)gB,(ULONG)gpReg)
	move.l	a3,-(sp)
	move.l	_gB,-(sp)
	move.l	d3,-(sp)
	jsr	_fixPtrs			;fixSOF((structCB*)bufp)
	move.l	d3,-(sp)
	jsr	_fixSOF 		;if(bufp==(long)curBuf){
	move.l	_curBuf,d0
	cmp.l	d3,d0
	lea	16(sp),sp
	bne	CL10022 		;disp|=SHOWCURSOR_
	bset.l	#0,d4			;locpt=loc(((structCB*)bufp)->PBlock,((structCB*)bufp)->PPtr)
	move.l	d3,a0
	move.l	20(a0),a0
	moveq	#0,d0
	move.w	246(a0),d0
	swap	d0
;;;	moveq	#16,d1
;;;	asl.l	d1,d0
	move.l	d3,a0
	add.l	100(a0),d0
	move.l	d3,a0
	move.l	d0,d5
	sub.l	20(a0),d5			;if(loc(fB,fP)<=locpt&&loc(gB,gP)>=locpt){
	move.l	_fB,a0
	moveq	#0,d0
	move.w	246(a0),d0
	swap	d0
;;;	moveq	#16,d1
;;;	asl.l	d1,d0
	add.l	_fP,d0
	sub.l	_fB,d0
	cmp.l	d5,d0
	bhi	CL10023
	move.l	_gB,a0
	moveq	#0,d0
	move.w	246(a0),d0
	swap	d0
;;;	moveq	#16,d1
;;;	asl.l	d1,d0
	add.l	_gP,d0
	sub.l	_gB,d0
	cmp.l	d5,d0
	bcs	CL10023 		;disp=PAGE_
	moveq	#16,d4  		;shiftit((structCB*)bufp)
	move.l	d3,-(sp)
	jsr	_shiftit			;/*WARNING-musthavePOPFASTAFTERthis*/}}
	addq.w	#4,sp
CL10023:			;popFast()
CL10022:
	jsr	_popFast			;splitFlag(((structCB*)bufp)->BufNum,disp)
	move.w	d4,-(sp)
	move.l	d3,a0
	move.w	166(a0),-(sp)
	jsr	_splitFlag  		;return(TRUE)
	moveq	#1,d0
	addq.w	#4,sp
	bra	CR3 		;BRTN:
CL10011:			;return(FALSE)
	moveq	#0,d0
	bra	CR3 		;}

	xdef	_FinsertRgn
_FinsertRgn:
	link	a5,#I2
	movem.l	I3,-(sp) 			;rtn=FALSE
	clr.w	-2(a5) 			;if(args[2]==buf_){
	cmp.b	#1,_args+2
	bne	I4 			;inBuf=*longPtrArgs[2]
	move.l	_longPtrArgs+8,a0
	move.w	2(a0),d4 			;if(buf[inBuf]==NULL)gotoRTN
	move.w	d4,d0
			;			;			;	asl.w	#2,d0
	add.w	d0,d0
	add.w	d0,d0
	lea	_buf,a0
	tst.l	(a0,d0.w)
	beq	I5 			;}elseinBuf=255
	bra	I6
I4:
	move.w	#255,d4
I6: 			;ch=*longPtrArgs[0]
	move.l	_longPtrArgs,a0
	moveq	#0,d5
	move.b	3(a0),d5 			;if(inBuf==ch||!getBuf((USHORT)ch))gotoRTN
	cmp.w	d4,d5
	beq	I5
	move.w	d5,-(sp)
	jsr	_getBuf
	tst.w	d0
	addq.w	#2,sp
	beq	I5 			;bPtr=(ULONG)buf[ch]
	move.w	d5,d0
			;			;			;	asl.w	#2,d0
	add.w	d0,d0
	add.w	d0,d0
	lea	_buf,a0
	move.l	(a0,d0.w),d6 			;toBufPtr=(structCB*)bPtr
	move.l	d6,-12(a5) 			;if(!canChange(bPtr))gotoRTN
	move.l	d6,-(sp)
	jsr	_canChange
	tst.w	d0
	addq.w	#4,sp
	beq	I5 			;if(inBuf!=255){
	cmp.w	#255,d4
	beq	I7 			;getBP(buf[inBuf],3)
	move.w	#3,-(sp)
	move.w	d4,d0
			;			;			;	asl.w	#2,d0
	add.w	d0,d0
	add.w	d0,d0
	lea	_buf,a0
	move.l	(a0,d0.w),-(sp)
	jsr	_getBP 			;if(fB==NULL||gB==NULL)gotoRTN
	tst.l	_fB
	addq.w	#6,sp
	beq	I5
	tst.l	_gB
	beq	I5 			;toP=(ULONG)gP
	move.l	_gP,d7 			;fromP=fP
	move.l	_fP,a2 			;fromB=fB
	move.l	_fB,-8(a5) 			;if(toP==(ULONG)fromP||*fromP==EOFILE)gotoRTN
	cmp.l	d7,a2
	beq	I5
	cmp.b	#3,(a2)
	beq	I5 			;}getBP(bPtr,1)
I7:
	move.w	#1,-(sp)
	move.l	d6,-(sp)
	jsr	_getBP 			;if(gB==NULL)gotoRTN
	tst.l	_gB
	addq.w	#6,sp
	beq	I5 			;ffP=gP
	move.l	_gP,a3 			;if(ffP==toBufPtr->PPtr)shifting=TRUE
	move.l	-12(a5),a0
	move.l	100(a0),a1
	cmp.l	a3,a1
	bne	I8
	move.w	#1,-4(a5) 			;elseshifting=FALSE
	bra	I9
I8:
	clr.w	-4(a5)
I9: 			;splitFlag((short)ch,PAGE_|SHOWCURSOR_)
	move.w	#17,-(sp)
	move.w	d5,-(sp)
	jsr	_splitFlag 			;gB->eols=255
	move.l	_gB,a0
	move.b	#255,245(a0) 			;fastBufPtr=(structCB*)bPtr
	move.l	d6,_fastBufPtr 			;fastB=gB
	move.l	_gB,_fastB 			;ggP=ffP
	move.l	a3,_ggP 			;stickMode=0
	clr.w	_stickMode 			;if(inBuf==255){
	cmp.w	#255,d4
	addq.w	#4,sp
	bne	I10 			;fromP=(UCHAR*)ptrArgs[2]
	move.l	_ptrArgs+8,a2 			;gotoTAKE1
	tst.b	(a2)	; if *fromP==0x00 goto RTN
	beq I5
	bra	I11 			;while(1){
I12: 			;if(ffP[1]){
	tst.b	1(a3)
	beq	I14 			;fastP=ffP
	move.l	a3,_fastP 			;if(!stickChar((short)ch))gotoBADOUT
	move.w	d5,-(sp)
	jsr	_stickChar
	tst.w	d0
	addq.w	#2,sp
	beq	I15 			;ffP=fastP
	move.l	_fastP,a3 			;}else{
	bra	I16
I14: 			;ffP[1]=ffP[0]
	move.b	(a3),1(a3) 			;ffP[0]=ch
	move.b	d5,(a3)+ 			;ffP++
I16: 			;TAKE1:
I11: 			;ch=*fromP
	move.b	(a2)+,d5 			;fromP++
	bne	I12 			;if(ch==0)break
			;I13: 			;}else{
	bra	I17
I10: 			;inBuf=EOFILE
	moveq	#3,d4 			;while(1){
I18: 			;if(fromP==(UCHAR*)toP)break
	cmp.l	a2,d7
	beq	I19 			;ch=*fromP
	move.b	(a2)+,d5 			;fromP++
	cmp.w	d4,d5 			;if(ch<=inBuf){
	bhi	I20 			;if(ch==0x00)continue
	tst.b	d5
	beq	I18 			;if(ch==EOB){
	cmp.b	#2,d5
	bne	I21 			;fromB=fromB->NextBlock
	move.l	-8(a5),a0
	move.l	252(a0),d0  				;-8(a5) 			;if(fromB==NULL)break
	move.l  d0,-8(a5)
	beq	I19 			;fromP=fromB->Data
	move.l	d0,a2 			;continue
	bra	I18 			;}if(ch==inBuf)break
I21:
	cmp.w	d4,d5
	beq	I19
I20:
	tst.b	1(a3) 			;}if(ffP[1]==0){
	bne	I22
	move.b	(a3),1(a3) 			;ffP[1]=ffP[0];ffP[0]=ch
	move.b	d5,(a3)+ 			;ffP++
	bra	I18
I22:
	move.l	a3,_fastP 			;fastP=ffP;if(!stickChar((short)ch))gotoBADOUT
	move.w	d5,-(sp)
	jsr	_stickChar
	tst.w	d0
	addq.w	#2,sp
	beq	I15 			;ffP=fastP
	move.l	_fastP,a3 			;}else{
	bra	I18
I19: 			;}
I17: 			;rtn=TRUE
	move.w	#1,-2(a5) 			;BADOUT:
I15: 			;toBufPtr->Flags|=(NEEDPACK|CHANGED)
	move.l	-12(a5),a0
	or.l	#8448,168(a0) 			;matchPtrs(bPtr,fastB,ggP,ffP)
	move.l	a3,-(sp)
	move.l	_ggP,-(sp)
	move.l	_fastB,-(sp)
	move.l	d6,-(sp)
	jsr	_matchPtrs 			;if(shifting)shiftit(bPtr)
	tst.w	-4(a5)
	lea	16(sp),sp
	beq	I24
	move.l	d6,-(sp)
	jsr	_shiftit 			;popFast()
	addq.w	#4,sp
I24:
	jsr	_popFast 			;RTN:
I5: 			;return(rtn)
	move.w	-2(a5),d0
I25:
	movem.l	(sp)+,I3
	unlk	a5
	rts 			;}
I2:	equ	-12
I3:	reg	d4-d7/a2/a3

#endasm
#endif

FswapBuf()
{
register short  	i,j1,j2,j;
register struct CB  *b1,*b2;

	j1 = *longPtrArgs[0];
	j2 = *longPtrArgs[1];
	if (j1==j2 || !getBuf(j1) || !getBuf(j2)) goto BRTN;
	b1 = buf[j1];
	b2 = buf[j2];
	j = 0;
	if (b1==curBuf) { curBuf = b2; j = 1; }
	else if (b2==curBuf) { curBuf = b1; j = 1; }

	buf[j1]=b2;
	buf[j2]=b1;

	b1->BufNum=j2;
	b2->BufNum=j1;

	nameBuf(b1);
	nameBuf(b2);

	for (i=0; i<=nSplit; i++) {
		if (j1==splitBufNum[i])
			{ splitBufNum[i] = j2; splitFlag(j2,PAGE_); showTitle = TRUE; }
		else if (j2==splitBufNum[i])
			{ splitBufNum[i] = j1; splitFlag(j1,PAGE_); showTitle = TRUE; }
	}
	if (j) {
		splitBufNum[curSplit] = curBuf->BufNum;
		switchFast();
		/* FrefreshDisplay(); */
	}
	return(TRUE);
BRTN:
	return(FALSE);
}

FindexToLoc()		/* buf, loc, index */
{
register	struct DB	*b;
register	UCHAR		*p;

register	long		ndx;
register	short		rtn,j;
UCHAR					**ov;
struct CB				*bufPtr;

	rtn = FALSE;
	bufPtr=bufPtrArgs[0];
	if (bufPtr==NULL) goto RTN;
	if (bufPtr->Flags & NEEDPACK) packBuf(bufPtr,TRUE);
	b = bufPtr->NextBlock;
	rtn = TRUE;
	ndx = *longPtrArgs[2];
	if ( ndx > 0L ) {
		ndx--;
		while (ndx >= (long)DATASIZE) {
			ndx -= (long)DATASIZE;
			if (b->NextBlock == NULL) {
				rtn = FALSE;
				p = b->Data;
				while (*p != EOFILE) p++;
				goto DONE;
			}
			b = b->NextBlock;
		}
		p = b->Data;
		while (*p != EOFILE) {
			if (--ndx < 0L) break;
			p++;
		}
	} else {
		rtn = FALSE;
		p = b->Data;
	}
DONE:
	j = args[1];
	if (bufPtr==curBuf) {
		if (j==atCursor_) displayFlag |= SHOWCURSOR_;
		else displayFlag |= (PAGE_ << curSplit);
	}
	ov = (UCHAR **)bufPtr;
	if (j==mouseLoc_) mouseY=0;
	j -= atCursor_;
	ov[j]		 = (UCHAR *)b;
	ov[j+CBOVL]  = p;
RTN:
	return(rtn);
}

FcolToLoc() 										/* buf, loc, col */
{
register	struct CB	*bufPtr;
register	UCHAR		*fP;
register	long		col;
register	short		j,rtn;
UCHAR **ov;

	rtn = FALSE;
	bufPtr=bufPtrArgs[0];
	if (bufPtr==NULL) goto RTN;
	if ( (col = *longPtrArgs[2])<=0L) col = 1L;
	j = args[1];
	if (bufPtr == curBuf) {
		if (j==atCursor_) displayFlag |= SHOWCURSOR_;
		else displayFlag |= (PAGE_ << curSplit);
	}
	ov = (UCHAR **)bufPtr;
	if (j==mouseLoc_) mouseY=0;
	j -= atCursor_;
	pushFast(bufPtr,&(ov[j]), &(ov[j+CBOVL]));
	fP = fastP;
	if (*fP==EOFILE) {  							/* put onto SOL */
		fP = decFast();
		if (*fP==eol || *fP==12) fP = incFast();
		else goto CANDO;
	} else {
CANDO:
		incFast();
		sLineFast();
	}
	rtn = gotoColFast((short)col);
	rtn = (rtn<col) ? FALSE : TRUE;
	takeFast();
	popFast();
RTN:
	return(rtn);
}

#ifdef USEC

FlocToCol()  /* buf, col, loc */
{
register	struct CB	*bufPtr;

	bufPtr=bufPtrArgs[0];
	if (bufPtr==NULL) goto BRTN;
	getBP(bufPtr,2);
	if (gB==NULL) goto BRTN;
	pushFast(bufPtr,&gB,&gP);
	*longPtrArgs[1] = getCol();
	popFast();
	return(TRUE);
BRTN:
	return(FALSE);
}

FfileSize()
{
register struct  DB  *b;
register UCHAR  *p;
register long		i;
struct CB			*bufPtr;

	bufPtr=bufPtrArgs[0];
	if (bufPtr==NULL) goto BRTN;
	b = bufPtr->NextBlock;
	if (bufPtr->Flags & NEEDPACK) packBuf(bufPtr,TRUE);
	i = 0;
	while (b->NextBlock!=NULL) { b = b->NextBlock; i += DATASIZE; }
	p = b->Data;
	while (ISGOOD(*p)) { i++; p++; }
	*longPtrArgs[1]=i;
	return(TRUE);
BRTN:
	return(FALSE);
}

FchangeDir()
{
register char	*newLock;

	if ( (newLock =  (char *)Lock(fixString(' ',0,127),
					(ULONG)SHARED_LOCK) )==NULL) goto BRTN;
	CurrentDir(newLock);
	return(TRUE);

BRTN:
	return(FALSE);
}

FlocToIndex()											/* buf, index, loc */
{
register	struct CB	*bufPtr;
register	struct DB	*gbReg;

	bufPtr=bufPtrArgs[0];
	if (bufPtr==NULL) goto BRTN;
	if (bufPtr->Flags & NEEDPACK) packBuf(bufPtr,TRUE);
	getBP(bufPtr,2);
	if ((gbReg=gB)==NULL) { *longPtrArgs[1] = 0L; goto BRTN; }
	*longPtrArgs[1] = 1L + ((long)gbReg->BlockNum - 1L)*(long)DATASIZE +
							(long)(gP - gbReg->Data);
	return(TRUE);
BRTN:
	return(FALSE);
}

FonScreen() /* ONLY applies to curBuf!!! */
{
	getBP(curBuf,0);
	if (gB!=NULL) return(onScreen(gB,gP));
	return(FALSE);
}

	 /* WARNING - must have POPFAST AFTER this */
void shiftit(register struct CB *bPtr)
{
	pushFast(bPtr,&bPtr->PBlock,&bPtr->PPtr);
	incFast();
	sLineFast();
	takeFast();
	if (bPtr==curBuf) oldCursorCol=0;
}

FsubMenu()
{
register short    rtn;

	subflag = 1;
	rtn = Fmenu();
	subflag = 0;
	return(rtn);
}

FcheckedSubMenu()
{
register short    rtn;

	subflag = 1;
	rtn = FcheckedMenu();
	subflag = 0;
	return(rtn);
}

FcheckedMenu()
{
register short   rtn;

	checkit = args[4] - pmEsc_ + 128;
	rtn=Fmenu();
	checkit = 0;
	return(rtn);
}

#endif

#ifndef USEC
#asm

	xdef  _FlocToCol
_FlocToCol:
	move.l  a2,-(sp)   ;bufPtr=bufPtrArgs[0]
	move.l  _bufPtrArgs,d0   ;if(bufPtr==NULL)gotoBRTN
	beq E4   ;getBP(bufPtr,2)
	move.l  d0,a2
	move.w  #2,-(sp)
	move.l  a2,-(sp)
	jsr _getBP   ;if(gB==NULL)gotoBRTN
	tst.l	_gB
	addq.w  #6,sp
	beq E4   ;pushFast(bufPtr,&gB,&gP)
	pea _gP
	pea _gB
	move.l  a2,-(sp)
	jsr _pushFast	;*longPtrArgs[1]=getCol()
	jsr _getCol
	ext.l	d0
	move.l  _longPtrArgs+4,a0
	move.l  d0,(a0)    ;popFast()
	jsr _popFast	;return(TRUE)
	moveq	#1,d0
	lea 12(sp),sp
E5:
	move.l  (sp)+,a2
	rts    ;BRTN:
E4:    ;return(FALSE)
	moveq	#0,d0
	bra E5   ;}

	xdef  _FfileSize
_FfileSize:
	movem.l  d2/a2,-(sp)
	move.l  _bufPtrArgs,d1   ;bufPtrbufPtr=bufPtrArgs[0]
;	tst.l	d1   ;if(bufPtr==NULL)gotoBRTN
	beq E8   ;b=bufPtr->NextBlock
	move.l  d1,a0
	move.l  160(a0),a2   ;if(bufPtr->Flags&NEEDPACK)
	btst.b  #5,170(a0)   ;packBuf(bufPtr,TRUE)
	beq E9
	move.w  #1,-(sp)
	move.l  d1,-(sp)
	jsr _packBuf	;i=0
	addq.w  #6,sp
E9:
	moveq	#0,d2	;while(b->NextBlock!=NULL)
E10:				;{b=b->NextBlocki+=DATASIZE
	tst.l	252(a2)
	beq E11
	move.l  252(a2),a2    ;}
	add.l	#244,d2
	bra E10
E11:   ;p=b->Data
	move.l  a2,a1	;while(ISGOOD(*p)){i++p++}
E12:
	cmp.b	#3,(a1)+
	bls E13
	addq.l  #1,d2
	bra E12
E13:	;*longPtrArgs[1]=i
	move.l  _longPtrArgs+4,a0
	move.l  d2,(a0)   ;return(TRUE)
	moveq	#1,d0
E14:
	movem.l  (sp)+,d2/a2
	rts    ;BRTN:
E8:    ;return(FALSE)
	moveq	#0,d0
	bra E14

	xdef  _FchangeDir
_FchangeDir:		;if((newLock=(char*)
				;Lock(fixString('',0,127),(ULONG)SHARED_LOCK))
	pea -2  ;==NULL)gotoBRTN
	move.w  #127,-(sp)
	clr.w	-(sp)
	move.w  #32,-(sp)
	jsr _fixString
	addq.w  #6,sp
	move.l  d0,-(sp)
	jsr _Lock
	tst.l	d0
	addq.w  #8,sp
	beq E18    ;CurrentDir(newLock)
	move.l  d0,-(sp)
	jsr _CurrentDir   ;return(TRUE)
	moveq	#1,d0
	addq.w  #4,sp
E18:
	rts    ;BRTN:

	xdef  _FlocToIndex
_FlocToIndex:
	move.l  a2,-(sp)   ;bufPtr=bufPtrArgs[0]
	move.l  _bufPtrArgs,a2   ;if(bufPtr==NULL)gotoBRTN
	move.l  a2,d0
	beq E21   ;if(bufPtr->Flags&NEEDPACK)
	btst.b  #5,170(a2)   ;packBuf(bufPtr,TRUE)
	beq E22
	move.w  #1,-(sp)
	move.l  a2,-(sp)
	jsr _packBuf   ;getBP(bufPtr,2)
	addq.w  #6,sp
E22:
	move.w  #2,-(sp)
	move.l  a2,-(sp)
	jsr _getBP   ;if((gbReg=gB)==NULL)
	move.l  _gB,a2   ;{*longPtrArgs[1]=0LgotoBRTN}
	move.l  a2,d0
	addq.w  #6,sp
	bne E23
	move.l  _longPtrArgs+4,a0
	clr.l	(a0)
	bra E21   ;*longPtrArgs[1]=1L+((long)gbReg->BlockNum-1L)
E23:   ;*(long)DATASIZE+(long)(gP-gbReg->Data)
	move.l  _longPtrArgs+4,a0
	moveq	#0,d0
	move.w  246(a2),d0
	subq.l  #1,d0
	move.l  #244,d1
	jsr .mulu#
	move.l  _gP,a1
	sub.l	a2,a1
	add.l	a1,d0
	addq.l  #1,d0
	move.l  d0,(a0)   ;return(TRUE)
	moveq	#1,d0
E24:
	move.l  (sp)+,a2
	rts    ;BRTN:
E21:   ;return(FALSE)
	moveq	#0,d0
	bra E24    ;}

	xdef  _FsubMenu
_FsubMenu:
	move.w  #1,_subflag   ;subflag=1
	jsr _Fmenu   ;rtn=Fmenu()
	clr.w	_subflag   ;return(rtn)
	rts    ;subflag=0

	xdef  _FcheckedSubMenu
_FcheckedSubMenu:
	move.w  #1,_subflag   ;subflag=1
	jsr _FcheckedMenu	;rtn=FcheckedMenu()
	clr.w	_subflag   ;subflag=0
	rts   ;return(rtn)

	xdef  _FcheckedMenu
_FcheckedMenu:
	moveq	#0,d0	;checkit=args[3]-pmEsc_+128
	move.b  _args+4,d0
	add.w	#113,d0
	move.w  d0,_checkit
	jsr _Fmenu   ;rtn=Fmenu()
	clr.w	_checkit   ;checkit=0
	rts   ;return(rtn)

	xdef  _shiftit
_shiftit:
	move.l  a2,-(sp)
	move.l  8(sp),a2
	pea 100(a2)    ;pushFast(bPtr,&bPtr->PBlock,&bPtr->PPtr);
	pea 20(a2)
	move.l  a2,-(sp)
	jsr _pushFast
	lea 12(sp),sp
	jsr _incFast   ;incFast();
	jsr _sLineFast   ;sLineFast();
	jsr _takeFast	;takeFast();
	cmp.l	_curBuf,a2    ;if (bPtr==curBuf) oldCursorCol=0;
	bne SHFT58
	clr.w	_oldCursorCol
SHFT58:
	move.l  (sp)+,a2
	rts

	xdef  _FonScreen
_FonScreen:
	clr.w	-(sp)
	move.l  _curBuf,-(sp)
	jsr _getBP    ;if (gB!=NULL) return(onScreen(gB,gP));
	tst.l	_gB
	addq.w  #6,sp
	beq F4
	move.l  _gP,-(sp)
	move.l  _gB,-(sp)
	jsr _onScreen
	addq.w  #8,sp
F5:
	rts
F4:
	moveq	#0,d0
	bra F5
#endasm
#endif
