-- TiogaDisplay2Impl.mesa, last edit by Paxton on June 20, 1983 1:22 pm
Last Edited by: Maxwell, January 6, 1983 2:02 pm
DIRECTORY
EditNotify USING [AddNotifyProc, Change, ChangeSet],
Rope USING [Equal, Fetch, ROPE],
RopeEdit USING [BlankChar],
RopeReader USING [GetRopeReader, FreeRopeReader, Get, Ref, SetPosition],
TextEdit USING [Offset, Size],
TiogaNode USING [Location, Offset, Ref, RefTextNode, Span],
TiogaNodeOps USING [BranchChild, Forward, ForwardClipped, LastLocWithin, LastWithin, Level, NarrowToTextNode, Next, Parent, StepForward],
TiogaDisplay,
TiogaDocument USING [GetViewerForRoot, LineTable, maxClip, TiogaDocumentData],
TiogaInput USING [currentEvent],
TiogaSelection USING [ComputePosLine, ComputeSpanLines, CannotFindIt],
UndoEvent USING [Note, Ref, SubEvent],
ViewerClasses USING [Viewer],
ViewerOps USING [SetNewVersion];
TiogaDisplay2Impl: CEDAR MONITOR
IMPORTS Rope, EditNotify, RopeEdit, RopeReader, TextEdit, TiogaNodeOps, TiogaDocument, TiogaInput, TiogaSelection, UndoEvent, ViewerOps
EXPORTS TiogaDisplay =
BEGIN OPEN TiogaSelection, TiogaDocument;
Change: TYPE = EditNotify.Change;
EstablishLine: PUBLIC ENTRY PROC [tdd: TiogaDocumentData, newLine: TiogaNode.Location,
position: INTEGER ← 0] = BEGIN
lines: TiogaDocument.LineTable = tdd.lineTable;
lines[position].pos ← newLine;
lines[position].valid ← FALSE;
END;
UndoChangeViewer: PROC [undoRef: REF Change, currentEvent: UndoEvent.Ref] = {
x: REF Change.ChangingView = NARROW[undoRef];
tdd: TiogaDocumentData = NARROW[x.viewer.data];
IF tdd = NIL THEN RETURN;
IF ~AlreadySaved[x.viewer] THEN SaveViewerPos[x.viewer, tdd.lineTable[0].pos];
[] ← EstablishLine[tdd, x.old] };
AlreadySaved: PROC [v: ViewerClasses.Viewer] RETURNS [BOOL] = {
IF TiogaInput.currentEvent = NIL THEN RETURN [TRUE];
FOR l: UndoEvent.SubEvent ← TiogaInput.currentEvent.subevents, l.next UNTIL l=NIL DO
IF l.undoRef # NIL THEN
WITH l.undoRef SELECT FROM
x: REF Change.ChangingView => IF x.viewer = v THEN RETURN[TRUE];
ENDCASE;
ENDLOOP;
RETURN [FALSE] };
SaveViewerPos: PROC [viewer: ViewerClasses.Viewer, oldLine: TiogaNode.Location] = {
notify: REF ChangingView Change ← NEW[ChangingView Change];
notify^ ← [ChangingView[viewer,oldLine]];
UndoEvent.Note[TiogaInput.currentEvent,UndoChangeViewer,notify] };
UpdateAfterTextEdit: PROC [viewer: ViewerClasses.Viewer,
text: TiogaNode.RefTextNode,
start, newlen, oldlen: TiogaNode.Offset,
oldrope: Rope.ROPE] = {
-- invalidate lines containing old text and 1 line before
-- update line offsets for other lines in same node that start beyond the replacement
Update: PROC [v: ViewerClasses.Viewer] = {
tdd: TiogaDocumentData = NARROW[v.data];
lines: LineTable = tdd.lineTable;
IF tdd = NIL THEN RETURN;
IF tdd.tsInfo=NIL THEN ViewerOps.SetNewVersion[v];
IF ~AlreadySaved[v] THEN SaveViewerPos[v, lines[0].pos]; -- record viewer start pos
FOR n: INTEGER IN [0..lines.lastLine] DO
where: TiogaNode.Offset;
IF lines[n].pos.node # text THEN LOOP;
SELECT where ← lines[n].pos.where FROM
<= start => IF n > 0
AND lines[n-1].pos.node=text AND lines[n-1].valid
AND where+lines[n].nChars>start
AND Rope.Fetch[text.rope,where-1] # 15C
AND (oldlen > 0 OR InsertContainsBlank[])
AND ~LineContainsPreviousBlank[n,lines] THEN
-- invalidate 1 line before start to take care of back ripple
lines[n-1].valid ← FALSE;
>= end => lines[n].pos.where ← where+delta;
ENDCASE => IF n=0 THEN {
lines[0].pos.where ← MIN[where, start+newlen];
tdd.fixStart ← TRUE };
ENDLOOP;
};
LineContainsPreviousBlank: PROC [n: INTEGER, lines: LineTable] RETURNS [yes: BOOL] = {
OPEN RopeReader;
rdr: Ref;
lineStart: TiogaNode.Offset = lines[n].pos.where;
IF lineStart>=start THEN RETURN [FALSE];
rdr ← GetRopeReader[];
yes ← FALSE;
SetPosition[rdr,text.rope,lineStart];
FOR i:INT ← lines[n].pos.where, i+1 UNTIL i=start DO
IF RopeEdit.BlankChar[Get[rdr]] THEN { yes ← TRUE; EXIT };
ENDLOOP;
FreeRopeReader[rdr] };
delta: TiogaNode.Offset = newlen-oldlen;
end: TiogaNode.Offset = start+oldlen;
checkedInsertForBlank: BOOLFALSE;
insertContainsBlank: BOOL;
InsertContainsBlank: PROC RETURNS [BOOL] = { OPEN RopeReader;
rdr: Ref;
IF newlen = 0 THEN RETURN [FALSE];
IF checkedInsertForBlank THEN RETURN [insertContainsBlank];
rdr ← GetRopeReader[];
insertContainsBlank ← FALSE;
SetPosition[rdr,text.rope,start];
FOR i:INT ← 0, i+1 UNTIL i=newlen DO
IF RopeEdit.BlankChar[Get[rdr]] THEN { insertContainsBlank ← TRUE; EXIT };
ENDLOOP;
checkedInsertForBlank ← TRUE;
FreeRopeReader[rdr];
RETURN [insertContainsBlank] };
InvalidateSpan[viewer,[[text,start],[text,start+oldlen]],FALSE];
-- can safely pass errorFlag=FALSE since if fail to find text node in viewer,
-- then nothing needs invalidation
Update[viewer];
IF viewer.link # NIL THEN
FOR v: ViewerClasses.Viewer ← viewer.link, v.link UNTIL v=viewer DO
Update[v];
ENDLOOP;
};
InvalidateSpan: PUBLIC PROC [viewer: ViewerClasses.Viewer, span: TiogaNode.Span,
errorFlag: BOOLTRUE, movingOut, unnesting: BOOLFALSE] = {
-- invalidate lines containing text from nodes in span
InvalidateLinesInSpan: PROC [v: ViewerClasses.Viewer] = {
start, end: INTEGER;
startClipped, endClipped: BOOLEAN;
tdd: TiogaDocumentData = NARROW[v.data];
lines: LineTable = tdd.lineTable;
IF tdd.movedOut AND tdd.movedIn THEN {
-- cannot trust the line table anymore so invalidate everything
start ← 0; end ← LAST[INTEGER];
startClipped ← endClipped ← FALSE }
ELSE [start, end, startClipped, endClipped] ← TiogaSelection.ComputeSpanLines[v, span !
TiogaSelection.CannotFindIt => {
-- we must have previously deleted either first/last node in viewer
-- and span start/end node not in the viewer line table
end ← LAST[INTEGER];
startClipped ← endClipped ← FALSE;
start ← IF errorFlag THEN 0 --invalidate all-- ELSE LAST[INTEGER] --invalidate none--;
CONTINUE } ];
IF end < 0 THEN RETURN; -- off screen
IF start = LAST[INTEGER] THEN
IF (unnesting AND tdd.clipLevel < maxClip) OR
(lines[lines.lastLine].pos.node = span.start.node AND
lines[lines.lastLine].pos.where+lines[lines.lastLine].nChars = span.start.where)
THEN start ← lines.lastLine -- may be visible now
ELSE RETURN;
IF start < 0 THEN start ← 0; -- begins above screen
IF end = LAST[INTEGER] THEN end ← tdd.lineTable.lastLine -- ends below screen
ELSE IF endClipped AND (end > start OR ~unnesting) THEN end ← end-1;
FOR n: INTEGER IN [start..end] DO
tdd.lineTable.lines[n].valid ← FALSE;
ENDLOOP;
IF movingOut THEN tdd.movedOut ← TRUE };
InvalidateLinesInSpan[viewer];
IF viewer.link # NIL THEN
FOR v: ViewerClasses.Viewer ← viewer.link, v.link UNTIL v=viewer DO
InvalidateLinesInSpan[v]; ENDLOOP };
InvalidateChildren: PUBLIC PROC [viewer: ViewerClasses.Viewer, node: TiogaNode.Ref] = {
-- invalidate lines for children of node
child: TiogaNode.Ref;
span: TiogaNode.Span;
IF (child ← TiogaNode.FirstChild[node])=NIL THEN RETURN;
span ← [[child,0],TiogaNodeOps.LastLocWithin[node]];
InvalidateSpan[viewer,span] };
InvalidateBranch: PUBLIC PROC [viewer: ViewerClasses.Viewer, node: TiogaNode.Ref] = {
-- invalidate lines for children of node
span: TiogaNode.Span ← [[node,0],TiogaNodeOps.LastLocWithin[node]];
InvalidateSpan[viewer,span] };
InvalidateBranchAndNext: PROC [viewer: ViewerClasses.Viewer, node: TiogaNode.Ref] = {
-- invalidate lines for children of node and one node beyond
OPEN TextNode;
last: Ref = LastWithin[node];
next: Ref = Forward[last].nx;
end: Ref = IF next=NIL THEN last ELSE next;
span: Span ← [[node,0], [end,0]];
InvalidateSpan[viewer,span] };
UpdateStartPos: PROC
[viewer: ViewerClasses.Viewer, first, last: TiogaNode.Ref] = {
-- this takes care of case in which something has been inserted in front of the first node
next: TiogaNode.Ref ← TiogaNodeOps.StepForwardNode[last];
UpdateViewerStartPos: PROC [v: ViewerClasses.Viewer] = {
tdd: TiogaDocumentData = NARROW[v.data];
lines: LineTable;
IF tdd = NIL THEN RETURN;
lines ← tdd.lineTable;
IF lines[0].pos.node # next THEN RETURN;
IF ~AlreadySaved[v] THEN SaveViewerPos[v, lines[0].pos]; -- record viewer start pos
lines[0].pos ← [first, 0];
lines[0].valid ← FALSE };
UpdateViewerStartPos[viewer];
IF viewer.link # NIL THEN
FOR v: ViewerClasses.Viewer ← viewer.link, v.link UNTIL v=viewer DO
UpdateViewerStartPos[v];
ENDLOOP;
};
InvalidateDest: PROC
[viewer: ViewerClasses.Viewer, node: TiogaNode.Ref, movingIn, afterDest: BOOL] = {
InvalidateDestLine: PROC [v: ViewerClasses.Viewer, dest: TiogaNode.Location] = {
line: INTEGER;
clipped, failed: BOOLEANFALSE;
tdd: TiogaDocumentData = NARROW[v.data];
IF tdd = NIL THEN RETURN;
IF movingIn THEN tdd.movedIn ← TRUE; -- remember that have moved nodes into this doc
[line, clipped] ← TiogaSelection.ComputePosLine[v, dest !
TiogaSelection.CannotFindIt => { failed ← TRUE; CONTINUE } ];
IF failed AND tdd.clipLevel < maxClip THEN { -- try adjusting for level clipping
n: TiogaNode.Ref ← dest.node;
delta: INTEGER ← TiogaNodeOps.Level[n]-tdd.clipLevel;
IF delta > 0 THEN {
FOR i:INTEGER IN [0..delta) DO n ← TiogaNodeOps.Parent[n]; ENDLOOP;
n ← TiogaNodeOps.ForwardClipped[n,tdd.clipLevel].nx;
IF n # NIL THEN {
dest ← [n,0];
afterDest ← failed ← FALSE;
[line, clipped] ← TiogaSelection.ComputePosLine[v, dest !
TiogaSelection.CannotFindIt => { failed ← TRUE; CONTINUE } ] }}};
IF failed OR line < 0 THEN RETURN; -- not visible
IF line = LAST[INTEGER] THEN -- beyond last thing on viewer
IF tdd.clipLevel < maxClip THEN line ← tdd.lineTable.lastLine
ELSE RETURN;
IF afterDest AND ~clipped AND line < tdd.lineTable.lastLine THEN line ← line+1;
tdd.lineTable[line].valid ← FALSE;
};
pos: TiogaNode.Location = [node,
IF afterDest THEN TextEdit.Size[TiogaNodeOps.NarrowToTextNode[node]] ELSE 0];
InvalidateDestLine[viewer,pos];
IF viewer.link # NIL THEN
FOR v: ViewerClasses.Viewer ← viewer.link, v.link UNTIL v=viewer DO
InvalidateDestLine[v,pos]; ENDLOOP };
FirstLevelParent: PROC [node: TiogaNode.Ref] RETURNS [TiogaNode.Ref] = {
prev1, prev2: TiogaNode.Ref;
UNTIL node=NIL DO
prev2 ← prev1;
node ← TiogaNodeOps.Parent[prev1 ← node];
ENDLOOP;
RETURN [prev2] };
SpanIsBranches: PROC [first, last: TiogaNode.Ref] RETURNS [BOOL] = {
parent: TiogaNode.Ref = TiogaNodeOps.Parent[first];
n: TiogaNode.Ref ← last;
IF TiogaNodeOps.FirstChild[n] # NIL THEN RETURN [FALSE];
DO
SELECT (n ← TiogaNodeOps.Parent[n]) FROM
parent => RETURN [TRUE];
NIL => EXIT;
ENDCASE;
IF TiogaNodeOps.Next[n] # NIL THEN RETURN [TiogaNodeOps.Parent[n] = parent];
ENDLOOP;
RETURN [FALSE] };
SpanFromNodes: PROC [first,last: TiogaNode.Ref] RETURNS [TiogaNode.Span] = {
RETURN [[[first,0],[last,MAX[TextEdit.Size[TiogaNodeOps.NarrowToTextNode[last]],1]-1]]] };
NotifyBeforeEdit: PROC [change: REF READONLY EditNotify.Change] = {
viewer: ViewerClasses.Viewer;
span: TiogaNode.Span;
WITH change SELECT FROM
x: REF READONLY Change.NodeNesting => {
IF (viewer ← TiogaDocument.GetViewerForRoot[x.root])=NIL THEN RETURN;
span ← SpanFromNodes[x.first,x.last];
IF ~SpanIsBranches[x.first,x.last] THEN
span.end ← TiogaNodeOps.LastLocWithin[FirstLevelParent[x.last]];
InvalidateSpan[viewer,span,TRUE,FALSE,(x.change<0)] };
x: REF READONLY Change.MovingNodes => {
IF (viewer ← TiogaDocument.GetViewerForRoot[x.sourceRoot]) # NIL THEN {
branches: BOOL ← SpanIsBranches[x.first,x.last];
span ← SpanFromNodes[x.first,x.last];
IF ~branches THEN span.end ← TiogaNodeOps.LastLocWithin[FirstLevelParent[x.last]];
-- want span.last to be the last thing that can be influenced by the move
-- this is a conservative but easy to compute span that is sure to get everything
InvalidateSpan[viewer,span,TRUE,TRUE];
ViewerOps.SetNewVersion[viewer] };
x.dest.dirty ← x.pred.dirty ← TRUE;
IF (viewer ← TiogaDocument.GetViewerForRoot[x.destRoot])=NIL THEN RETURN;
InvalidateDest[viewer,x.dest,TRUE,x.afterDest] };
x: REF READONLY Change.ChangingType => {
x.node.dirty ← TRUE;
IF (viewer ← TiogaDocument.GetViewerForRoot[x.root])=NIL THEN RETURN;
InvalidateBranchAndNext[viewer,x.node] };
x: REF READONLY Change.ChangingProp => {
x.node.dirty ← TRUE;
IF (viewer ← TiogaDocument.GetViewerForRoot[x.root])=NIL THEN RETURN;
IF ~Rope.Equal[x.propName,"Prefix"] AND ~Rope.Equal[x.propName,"Postfix"]
AND ~Rope.Equal[x.propName,"Comment"]
AND ~Rope.Equal[x.propName,"StyleDef"] THEN NULL
ELSE -- this is a property that can influence appearance
InvalidateBranchAndNext[viewer,x.node] };
ENDCASE;
IF viewer # NIL THEN ViewerOps.SetNewVersion[viewer];
};
NotifyAfterEdit: PROC [change: REF READONLY EditNotify.Change] = {
viewer: ViewerClasses.Viewer;
span: TiogaNode.Span;
WITH change SELECT FROM
x: REF READONLY Change.ChangingText => {
x.text.dirty ← TRUE;
IF (viewer ← TiogaDocument.GetViewerForRoot[x.root])=NIL THEN RETURN;
UpdateAfterTextEdit[viewer,x.text,x.start,x.newlen,x.oldlen,x.oldRope] };
x: REF READONLY Change.InsertingNode => {
span: TiogaNode.Span;
child: TiogaNode.Ref;
DiffCommentProps: PROC [n1, n2: TiogaNode.Ref] RETURNS [BOOL] = {
node1, node2: TiogaNode.RefTextNode;
IF (node1 ← TiogaNodeOps.NarrowToTextNode[n1]) = NIL THEN RETURN [TRUE];
IF (node2 ← TiogaNodeOps.NarrowToTextNode[n2]) = NIL THEN RETURN [TRUE];
RETURN [node1.comment # node2.comment] };
x.new.new ← x.dest.dirty ← TRUE;
IF (viewer ← TiogaDocument.GetViewerForRoot[x.root])=NIL THEN RETURN;
ViewerOps.SetNewVersion[viewer];
InvalidateDest[viewer,x.dest,FALSE,TRUE];
IF (child ← TiogaNodeOps.FirstChild[x.new]) # NIL -- new inherited children of old
AND (x.dest.next # x.new --different levels-- OR
x.dest.hasstyledef OR x.new.hasstyledef OR
x.dest.hasprefix OR x.new.hasprefix OR
x.dest.haspostfix OR x.new.haspostfix OR
x.dest.typename # x.new.typename OR
DiffCommentProps[x.dest, x.new]) THEN { -- must invalidate children too
span ← [[child,0],TiogaNodeOps.LastLocWithin[x.new]];
InvalidateSpan[viewer,span];
UpdateStartPos[viewer,x.new,x.new] }};
x: REF READONLY Change.MovingNodes => {
IF (viewer ← TiogaDocument.GetViewerForRoot[x.destRoot])=NIL THEN RETURN;
UpdateStartPos[viewer,x.first,x.last];
IF SpanIsBranches[x.first,x.last] THEN RETURN;
span.start ← [x.dest,0];
span.end ← TiogaNodeOps.LastLocWithin[FirstLevelParent[x.last]];
InvalidateSpan[viewer,span] };
ENDCASE;
};
Init: PROC = {
beforeEditChanges: EditNotify.ChangeSet ← ALL[TRUE];
afterEditChanges: EditNotify.ChangeSet ← ALL[FALSE];
beforeEditChanges[ChangingText] ← FALSE;
beforeEditChanges[InsertingNode] ← FALSE;
EditNotify.AddNotifyProc[proc: NotifyBeforeEdit, time: before, changeSet: beforeEditChanges];
afterEditChanges[MovingNodes] ← TRUE;
afterEditChanges[InsertingNode] ← TRUE;
afterEditChanges[ChangingText] ← TRUE;
EditNotify.AddNotifyProc[proc: NotifyAfterEdit, time: after, changeSet: afterEditChanges];
};
Init;
END.