// insert.sr


get "BRAVO1.DF"
get "CHAR.DF"
get "MEASURE.DF"
get "DOC.DF"
get "SELECT.DF"
get "COM.DF"
get "MSG.DF"
get "VM.DF"


// Incoming procedures

external	[
	replacea
	InsertBuf
	updatedisplay
	setsel
	bravochar
	stcopy
	inserta
	umin
	InsertK
	deleted
	MakeCurrentBuf
	underline
	updateunderline
	selectwholedoc
	addbt
	invalidatedisplay
	select
	move
	PutSbScr
	insertd
	SetVab
	invalidateband
	endofkeystream
	]


// Incoming statics

external	[
	selarg
	vesccom
	vselcaret
	selmain
	sbpast
	chcom
	vdlhint
	rgmaccp
	doctxcurrent
	vmeasurestatus
	vbttoti
	vundocom
	deltacp
	cpscrt
	dcpendofdoc
	]


// Outgoing procedures

external	[
	FInsertCom
	]


// F   I N S E R T   C O M
// 
let FInsertCom(wwinsert, docinsert, cpinsert, look1, look2, par, ttbl) = valof
[
let parLook = vec parovhd
move(par, parLook, parovhd)
let ttblLook = vec lnttblMax
move(ttbl, ttblLook, lnttblMax)
let tchar = nil;
let tdoc = nil; let maccp = nil; let tcplast = nil;
selarg>>SEL.type = snone;
cpinsert = umin(cpinsert, rgmaccp ! docinsert-dcpendofdoc)
let dcpTrailerInsert = 0
if selmain>>SEL.type eq sph then
	[
	let sbtrler = "*032*c"
	deltacp = 0
	if cpinsert eq rgmaccp ! docinsert-dcpendofdoc then
		look1 = look1 & not visiblebit
	let bifr = PutSbScr(sbtrler, look1 % trailerbits, look2, parLook, ttblLook);
	invalidateband(docinsert, cpinsert, cpinsert+3);
	insertd(docinsert, cpinsert, deltacp, fnscr, cpscrt, bifr)
	cpscrt = cpscrt+deltacp
	dcpTrailerInsert = deltacp
	if endofkeystream() then
		updatedisplay()
	]
deltacp = 0
// test vesccom % vundocom ifnot
// 	[
vselcaret>>SEL.type = scaret;
vselcaret>>SEL.doc = docinsert;
vselcaret>>SEL.ww = wwinsert;
setsel(vselcaret, cpinsert, cpinsert);
vselcaret>>SEL.toggle = 0;
if endofkeystream() then
	updatedisplay();
select(selarg, vselcaret);
tchar = bravochar();
// 	]
// ifso	tchar = chesc;

// if (selarg>>SEL.doc eq docsys) & (selarg>>SEL.type ne snone) then
//	[
//	resultis false;	// no insertion in sys window
//	]

if tchar eq chdel then 
	[ if deltacp eq 0 then
		UndoTrailerInsert(docinsert,cpinsert,deltacp,lv dcpTrailerInsert)
	resultis false
	] 
let tsiz = nil;
test tchar eq chesc ifso
	[ UndoTrailerInsert(docinsert,cpinsert,deltacp,lv dcpTrailerInsert)
	invalidatedisplay(docinsert, cpinsert, vdlhint);
// use buffer #2's contents for insertion
	test selarg>>SEL.type eq snone ifso
		[
		tsiz = rgmaccp ! doctxcurrent;
		replacea(docinsert, cpinsert, 0, doctxcurrent, 0, tsiz);
		]
// use secondary selection
	ifnot	[
		tcplast = selarg>>SEL.cplast;
		tdoc = selarg>>SEL.doc;
		maccp = rgmaccp ! tdoc;
		selarg>>SEL.cplast = umin(tcplast, maccp-dcpendofdoc-1)
		selarg>>SEL.cpfirst = umin(selarg>>SEL.cpfirst, 
			maccp-dcpendofdoc-1)
		tsiz = selarg>>SEL.cplast-selarg>>SEL.cpfirst+1;
		replacea(docinsert, cpinsert, 0, tdoc, selarg>>SEL.cpfirst, tsiz);
		];
	]
// type in insertions
ifnot	[
	vselcaret>>SEL.type = scaret;
	vselcaret>>SEL.toggle = 0;
	tsiz = InsertK(wwinsert, cpinsert, look1, look2, parLook, ttblLook, tchar);
	];

tsiz = tsiz+dcpTrailerInsert 

//make buffer number 2 the current buffer
test tsiz eq 0 ifnot
	[
	setsel(selmain, cpinsert, cpinsert+tsiz-1);
	replacea(doctx2, 0, rgmaccp ! doctx2, docinsert, selmain>>SEL.cpfirst, tsiz);
	InsertBuf(2, selmain>>SEL.doc, selmain>>SEL.cpfirst, tsiz);
	MakeCurrentBuf(2);
	if chcom eq $r then 
		[
		replacea(doctx1, 0, rgmaccp ! doctx1, doctx5, 0, rgmaccp ! doctx5);
		InsertBuf(1, doctx1, 0, rgmaccp ! doctx1)
		]
	resultis true
	]
ifso	[
// No insertion - Command terminated
	SetVab(abmsg, false, 117, 50)
	resultis false
	]
]

// U N D O T R A I L E R I N S E R T
//
and UndoTrailerInsert(docInsert,cpInsert,dcpInsert,lvDcpTrailerInsert) be
[ replacea(docInsert, cpInsert+dcpInsert, @lvDcpTrailerInsert, 0, 0, 0);
@lvDcpTrailerInsert = 0
invalidatedisplay(docInsert, cpInsert+dcpInsert, vdlhint);
if endofkeystream() then
	updatedisplay()
]