-- TiogaBufferedInputImpl.mesa Edited by Paxton on November 24, 1982 9:50 am
Last Edited by: Maxwell, January 6, 1983 11:39 am
DIRECTORY
ImplErrors USING [UserErrorQuery],
Process USING [Detach, GetCurrent, MsecToTicks, SetTimeout, Ticks],
Rope USING [Concat, Fetch, FromProc, Length, ROPE],
TextEdit USING [InsertRope, InsertString],
TiogaNode USING [Location, RefBranchNode],
TiogaNodeOps USING [NarrowToTextNode],
TiogaDocument USING [Selection],
TiogaInput USING [currentEvent],
TiogaInputOps,
TiogaLocks USING [Lock, Unlock],
TiogaRefresh USING [ScrollToEndOfSel],
TiogaSelection USING [Alloc, Free, CaretVisible, Copy, InsertionPoint, LockSel, MakePointSelection, pSel, SelectionRoot, UnlockSel];
TiogaBufferedInputImpl: CEDAR MONITOR
IMPORTS ImplErrors, Process, Rope, TextEdit, TiogaNodeOps, TiogaInput, TiogaInputOps, TiogaLocks, TiogaRefresh, TiogaSelection
EXPORTS TiogaInputOps
SHARES TiogaInputOps = BEGIN
OPEN TiogaSelection;
editRepaintProcess: PROCESSNIL;
repaintDone: CONDITION;
bufferMaxlen: CARDINAL ← 32;
inputBuffer: REF TEXTNEW[TEXT[bufferMaxlen]];
inputRope: Rope.ROPE;
BufferedInsertChar: PUBLIC ENTRY PROCEDURE [char: CHARACTER] = BEGIN
ENABLE UNWIND => NULL; -- release lock
count: INTEGER ← 0;
WHILE inputBuffer.length >= bufferMaxlen DO-- buffer full
IF (count ← count+1) > 10 THEN { -- waited long enough
i: CARDINAL ← 0;
Char: PROC RETURNS [c: CHAR] = { c ← inputBuffer[i]; i ← i+1 };
rope: Rope.ROPE ← Rope.FromProc[inputBuffer.length, Char];
inputRope ← Rope.Concat[inputRope, rope];
inputBuffer.length ← 0;
EXIT };
BROADCAST untilTimesOutInALittleWhile; -- wake up the repaint process
WAIT bufferClear; -- this will time out if repaint fails to empty the buffer
ENDLOOP;
inputBuffer[inputBuffer.length] ← char;
inputBuffer.length ← inputBuffer.length + 1;
IF editRepaintProcess=NIL THEN TRUSTED {
Process.Detach[editRepaintProcess ← FORK Repaint[]] };
END;
BufferedInsertText: PUBLIC PROCEDURE [text: Rope.ROPE] = BEGIN
FOR n: LONG INTEGER IN [0..Rope.Length[text]) DO
BufferedInsertChar[Rope.Fetch[text, n]];
ENDLOOP;
END;
WaitForInsertToFinish: PUBLIC ENTRY PROCEDURE = BEGIN
ENABLE UNWIND => NULL; -- release lock
WHILE editRepaintProcess # NIL DO WAIT repaintDone; ENDLOOP;
END;
Repaint: PROCEDURE = BEGIN
Cleanup: PROC = {
TiogaSelection.UnlockSel[primary];
IF docLock THEN TiogaLocks.Unlock[root];
IF tSel # NIL THEN Free[tSel];
NotifyDone };
root: TiogaNode.RefBranchNode;
docLock, caretVisible: BOOLFALSE;
tSel: TiogaDocument.Selection;
BEGIN
ENABLE BEGIN
UNWIND => Cleanup;
ABORTED => GOTO Quit;
ANY => IF ImplErrors.UserErrorQuery[] THEN CONTINUE;
END;
WaitForMoreInput: ENTRY PROCEDURE = BEGIN
ENABLE UNWIND => NULL; -- release lock
WAIT untilTimesOutInALittleWhile;
END;
TiogaSelection.LockSel[primary, "TiogaBufferedInputImplRepaint"];
caretVisible ← TiogaSelection.CaretVisible[];
IF ~TiogaInputOps.CheckReadonly[pSel]
OR (root ← TiogaSelection.SelectionRoot[pSel])=NIL THEN { Cleanup[]; RETURN };
[] ← TiogaLocks.Lock[root, "TiogaBufferedInputImplMakeEdits"];
docLock ← TRUE;
tSel ← Alloc[];
WHILE MakeEdits[root, tSel, caretVisible] DO
IF inputRope=NIL AND inputBuffer.length<bufferMaxlen THEN WaitForMoreInput[];
ENDLOOP;
Cleanup;
EXITS Quit => Cleanup;
END END;
NotifyDone: ENTRY PROC = TRUSTED INLINE {
IF Process.GetCurrent[]=editRepaintProcess THEN editRepaintProcess ← NIL;
BROADCAST repaintDone};
MakeEdits: ENTRY PROCEDURE [
root: TiogaNode.RefBranchNode, tSel: TiogaDocument.Selection, caretVisible: BOOL]
Get document and selection locks outside of monitor to avoid deadlock with someone doing an insert char while holding a lock.
RETURNS [BOOLEAN] = BEGIN
ENABLE UNWIND => NULL;
pos: TiogaNode.Location ← InsertionPoint[pSel];
IF root=NIL OR pos.node=NIL OR (inputRope=NIL AND inputBuffer.length=0) THEN
{editRepaintProcess ← NIL; RETURN[FALSE]}; -- done
Copy[source: pSel, dest: tSel];
[] ← TextEdit.InsertString[
root: root,
dest: TiogaNodeOps.NarrowToTextNode[pos.node],
string: inputBuffer, destLoc: pos.where,
inherit: FALSE, looks: tSel.looks,
event: TiogaInput.currentEvent];
IF inputRope#NIL THEN -- insert it in front of the inputBuffer string
[] ← TextEdit.InsertRope[
root: root,
dest: TiogaNodeOps.NarrowToTextNode[pos.node],
rope: inputRope, destLoc: pos.where,
inherit: FALSE, looks: tSel.looks,
event: TiogaInput.currentEvent];
pos.where ← pos.where+inputBuffer.length+Rope.Length[inputRope];
TiogaSelection.MakePointSelection[tSel, pos];
IF caretVisible THEN -- after repaint finishes, do an autoscroll
TiogaRefresh.ScrollToEndOfSel[tSel.viewer, TRUE];
inputBuffer.length ← 0;
inputRope ← NIL;
BROADCAST bufferClear;
RETURN[TRUE];
END;
aLittleWhile: Process.Ticks = Process.MsecToTicks[50];
untilTimesOutInALittleWhile, bufferClear: CONDITION;
TRUSTED {Process.SetTimeout[@untilTimesOutInALittleWhile, aLittleWhile]};
TRUSTED {Process.SetTimeout[@bufferClear, aLittleWhile]};
END.