// raid.sr
// Carol Hankins
// May 14, 1976

get "bravo1.df"
get "com.df"
get "doc.df"
get "char.df"
get "msg.df"

//incoming routines
external
	[
	setsel		//params are selection,cpfirst,cplast
	MakeCurrentBuf
	invalidatedisplay
	replacea
	InsertBuf
	updatedisplay
	FInsertCom
	umin
	ult
	mapcp
	move
	inserta
	SetVab
	SetRegionSys
	]

//incoming statics
external
	[
	rgmaccp
	vdlhint
	chcom
	selmain
	vmapstatus
	parsacred
	vlook1
	vlook2
	doctxcurrent
	vesccom
	vundocom
	ttblsacred
	dcpendofdoc
	]

//outgoing routines
external
	[
	editrepeat
	EditCheck
	deletez
	replacez
	insertz
	FAdjustSelEod
	]


let editrepeat(cf) = valof
[

// code for a repeat of insert, append, replace

unless cf>>CF.w0 do
	[
// Command terminated
	SetRegionSys(risyspast, 50)
	resultis abmsg
	]

let sel = cf>>CF.sel
let doc = sel>>SEL.doc
FAdjustSelEod(sel, cf>>CF.chcom)
let cpfirst = sel>>SEL.cpfirst
let cplast = sel>>SEL.cplast
let cp = nil
let dcp = cplast - cpfirst + 1

cp = cf>>CF.chcom eq $a ? cplast+1, cpfirst

invalidatedisplay(doc, cp, vdlhint)

// should check for improper return from replacea
let doctx = doctxcurrent
switchon cf>>CF.chcom into
	[
case $a:
case $i:	if vundocom then
		doctx = 1
	replacea(doc, cp, 0, doctx, 0, rgmaccp ! doctx)
	endcase;
case $r:	if vundocom then
		[
		replacea(doctx2, 0, rgmaccp ! doctx2, doctx1, 0, rgmaccp ! doctx1)
		InsertBuf(2, doctx, 0, rgmaccp ! doctx)
		doctx = 2
		]
	replacea(doctx1, 0, rgmaccp ! doctx1, doc, cp, dcp)
	InsertBuf(1, doc, cp, dcp)
	replacea(doc, cp, dcp, doctx, 0, rgmaccp ! doctx)
	endcase;
	]

let cpMac = rgmaccp ! doctx
setsel(selmain, cp, cp + (cpMac ne 0 ? cpMac-1, 0))
resultis abcomplete

]


and EditCheck(cf) =
	vesccom % vundocom ? editrepeat(cf), abnil


// delete command
and deletez(cf) = valof
[

if (vesccom % vundocom) & not cf>>CF.w0 then
	[
// Command terminated
	SetRegionSys(risyspast, 50)
	resultis abmsg
	]

let sel = cf>>CF.sel
let doc = sel>>SEL.doc
FAdjustSelEod(sel, $d)
let cpfirst = sel>>SEL.cpfirst
let dcp = sel>>SEL.cplast - cpfirst + 1
invalidatedisplay(doc, cpfirst, vdlhint)

replacea(doctx1, 0, rgmaccp ! doctx1, doc, cpfirst, dcp)
InsertBuf(1, doc, cpfirst, dcp)
replacea(doc, cpfirst, dcp, 0, 0, 0)
MakeCurrentBuf(1)
setsel(selmain, cpfirst, cpfirst)
cf>>CF.w0 = true
resultis abnil

]


// replace
and replacez(cf) = valof
[

let sel = cf>>CF.sel
let cplastOld = sel>>SEL.cplast
FAdjustSelEod(sel, $r)
let doc = sel>>SEL.doc
let ww = sel>>SEL.ww
let cpfirst = sel>>SEL.cpfirst
let dcp = sel>>SEL.cplast - cpfirst + 1

vmapstatus = statusblind; mapcp(doc, cpfirst, parneeded)
let parLook = vec parovhd; move(parsacred, parLook, parovhd)
let ttblLook = vec lnttblMax; move(ttblsacred, ttblLook, lnttblMax)
let look1 = vlook1; let look2 = vlook2

invalidatedisplay(doc, cpfirst, vdlhint)

// do delete first, then store in doctx5, then insert
replacea(doctx5, 0, rgmaccp ! doctx5, doc, cpfirst, dcp)

replacea(doc, cpfirst, dcp, 0, 0, 0)

unless FInsertCom(ww, doc, cpfirst, look1, look2, parLook, ttblLook) do
	[
	if dcp ne 0 then
		[
		invalidatedisplay(doc, cpfirst, vdlhint);
		inserta(doc, cpfirst, doctx5, 0, dcp-1);
		]
// REPLACE Command terminated
	SetVab(abmsg, false, 2, 50)
	setsel(sel, cpfirst, cplastOld)
	cf>>CF.w0 = false
	resultis abmsg
	]

cf>>CF.w0 = true
resultis abcomplete

]


// insert, append
and insertz(cf) = valof
[

let sel = cf>>CF.sel
let cplastOld = sel>>SEL.cplast
FAdjustSelEod(sel, cf>>CF.chcom)
let doc = sel>>SEL.doc
let tcp = nil
let tcpLook = nil
test cf>>CF.chcom eq $a ifso
	[
	tcp = sel>>SEL.cplast+1
	tcpLook = tcp eq 0 ? tcp, tcp-1
	]
ifnot	[
	tcp = sel>>SEL.cpfirst
	tcpLook = tcp
	]

vmapstatus = statusblind; mapcp(doc, tcpLook, parneeded)
let parLook = vec parovhd; move(parsacred, parLook, parovhd)
let ttblLook = vec lnttblMax; move(ttblsacred, ttblLook, lnttblMax)
let look1 = vlook1; let look2 = vlook2

unless FInsertCom(sel>>SEL.ww, doc, tcp, look1, look2, parLook, ttblLook) do
	[
// {APPEND, INSERT} Command terminated
	SetVab(abmsg, false, (cf>>CF.chcom eq $a ? 1, 0), 50)
	setsel(sel, sel>>SEL.cpfirst, cplastOld)
	cf>>CF.w0 = false
	resultis abmsg
	]

cf>>CF.w0 = true
resultis abcomplete
]

and FAdjustSelEod(sel, chCom) = valof
[
let doc = sel>>SEL.doc
let fDelete = ((chCom eq $d) % (chCom eq $r)) ? true, false
let cpFirst = sel>>SEL.cpfirst
let cpMac = rgmaccp ! doc - dcpendofdoc
sel>>SEL.cplast = umin(sel>>SEL.cplast+1, cpMac) - 1
sel >> SEL.cpfirst = umin(sel >> SEL.cpfirst,sel >> SEL.cplast+1)
resultis not (fDelete & (cpFirst eq cpMac))
]