TiogaDisplays.Mesa
Last Edited by: Spreitzer, March 17, 1985 10:22:10 am PST
DIRECTORY Ascii, Atom, Basics, CharDisplays, IO, IOClasses, InputFocus, Menus, MessageWindow, Rope, TiogaMenuOps, TiogaOps, TIPUser, ViewerClasses, ViewerOps;
TiogaDisplays: CEDAR PROGRAM
IMPORTS Ascii, Atom, Basics, CharDisplays, IO, IOClasses, InputFocus, Rope, Menus, MessageWindow, TiogaMenuOps, TiogaOps, TIPUser, ViewerOps
= {OPEN CharDisplays;
REFTEXT: TYPE = REF TEXT;
Viewer: TYPE = ViewerClasses.Viewer;
ViewerClass: TYPE = ViewerClasses.ViewerClass;
TiogaDisplay: TYPE = REF TiogaDisplayRep;
TiogaDisplayRep: TYPE = RECORD [
TORoot, TONode: TiogaOps.Ref,
eatChars, spitChars: IO.STREAMNIL,
inserting: BOOLFALSE,
looks: ROPE ← "f",
spaces: ROPENIL,
lineS: SEQUENCE length: NAT OF Line
];
Line: TYPE = RECORD [
tiogaCount: NAT,
key: ATOM];
tiogaClass, tiogaDisplayViewerClass: ViewerClass ← NIL;
tiogaDisplayViewerClassFlavor: ATOM ← $TiogaCharDisplay;
tdProp: ATOM ← $TiogaDisplayData;
tiogaCharDisplayClass: CharDisplayClass ← NEW [CharDisplayClassRep ← [
name: "Tioga",
Init: TInit,
DeleteChar: DeleteChar,
TakeChar: TTakeChar,
CursorMove: TCursorMove,
Line: TLine,
ClearTo: ClearTo,
ClearAll: ClearAll,
SetMode: SetMode,
Emphasize: Emphasize,
Flash: Flash]];
InitTiogaDisplayClass: PROC = {
tdTIP: TIPUser.TIPTable ← TIPUser.InstantiateNewTIPTable["TiogaDisplay.TIP"];
menu: Menus.Menu ← Menus.CreateMenu[];
tiogaClass ← ViewerOps.FetchViewerClass[$Text];
tiogaDisplayViewerClass ← NEW [ViewerClasses.ViewerClassRec ← tiogaClass^];
tiogaDisplayViewerClass.notify ← NotifyTiogaDisplay;
tiogaDisplayViewerClass.icon ← typescript;
tdTIP.mouseTicks ← MIN[tdTIP.mouseTicks, tiogaClass.tipTable.mouseTicks];
tdTIP.opaque ← FALSE;
tdTIP.link ← tiogaClass.tipTable;
tiogaDisplayViewerClass.tipTable ← tdTIP;
menu.AppendMenuEntry[Menus.CreateEntry["Find", Find]];
menu.AppendMenuEntry[Menus.CreateEntry["Word", Word]];
menu.AppendMenuEntry[Menus.CreateEntry["Def", Def]];
menu.AppendMenuEntry[Menus.CreateEntry["Position", Position]];
menu.AppendMenuEntry[Menus.CreateEntry["Normalize", Normalize]];
menu.AppendMenuEntry[Menus.CreateEntry["PrevPlace", PrevPlace]];
menu.AppendMenuEntry[Menus.CreateEntry["Reselect", Reselect]];
tiogaDisplayViewerClass.menu ← menu;
ViewerOps.RegisterViewerClass[tiogaDisplayViewerClassFlavor, tiogaDisplayViewerClass];
RegClass[tiogaCharDisplayClass];
};
Find: Menus.MenuProc = {
v: Viewer ← NARROW[parent];
[] ← TiogaOps.FindText[viewer: v, whichDir: SearchDir[mouseButton], case: NOT shift];
};
Word: Menus.MenuProc = {
v: Viewer ← NARROW[parent];
[] ← TiogaOps.FindWord[viewer: v, whichDir: SearchDir[mouseButton], case: NOT shift];
};
Def: Menus.MenuProc = {
v: Viewer ← NARROW[parent];
[] ← TiogaOps.FindDef[viewer: v, whichDir: SearchDir[mouseButton], case: NOT shift];
};
SearchDir: PROC [mb: Menus.MouseButton] RETURNS [sd: TiogaOps.SearchDir] =
{sd ← SELECT mb FROM red => forwards, yellow => anywhere, blue => backwards, ENDCASE => ERROR};
Position: Menus.MenuProc = {
v: Viewer ← NARROW[parent];
TiogaMenuOps.Position[v]};
Normalize: Menus.MenuProc = {
v: Viewer ← NARROW[parent];
TiogaMenuOps.Normalize[v]};
PrevPlace: Menus.MenuProc = {
v: Viewer ← NARROW[parent];
TiogaMenuOps.PrevPlace[v]};
Reselect: Menus.MenuProc = {
v: Viewer ← NARROW[parent];
TiogaMenuOps.Reselect[v]};
NotifyTiogaDisplay: PROC [self: Viewer, input: LIST OF REF ANY] --ViewerClasses.NotifyProc-- = {
td: TiogaDisplay ← GetDisplay[self];
WITH input.first SELECT FROM
a: ATOM => SELECT a FROM
$TDInput => {
r: ROPE;
ctl, shift, meta: BOOLFALSE;
FOR input ← input.rest, input.rest WHILE input # NIL DO
WITH input.first SELECT FROM
R: ROPE => r ← R;
t: REFTEXT => r ← Rope.FromRefText[t];
b: ATOM => SELECT b FROM
$Ctl => ctl ← TRUE;
$Shift => shift ← TRUE;
$Meta => meta ← TRUE;
ENDCASE => ERROR;
ENDCASE => ERROR;
ENDLOOP;
FOR i: INT IN [0 .. r.Length[]) DO
c: CHAR ← r.Fetch[i];
IF NOT shift THEN c ← Ascii.Lower[c];
IF ctl THEN c ← Control[c];
IF meta THEN c ← c + 128;
td.eatChars.PutChar[c];
ENDLOOP;
RETURN;
};
ENDCASE;
r: ROPE => {
td.eatChars.PutRope[r];
RETURN;
};
ENDCASE;
tiogaClass.notify[self, input];
};
Control: PROC [c: CHAR] RETURNS [cc: CHAR] = {
d: NAT ← c - 0C;
cd: NAT ← Basics.BITAND[d, 31];
cc ← 0C + cd;
};
GetDisplay: PROC [self: Viewer] RETURNS [td: TiogaDisplay] = INLINE
{td ← NARROW[ViewerOps.FetchProp[viewer: self, prop: tdProp]]};
TInit: PROC [cd: CharDisplay, initData: REF ANYNIL] = {
td: TiogaDisplay ← NEW [TiogaDisplayRep[cd.det.lines]];
InitDoc: PROC [root: TiogaOps.Ref] = {
IF root # td.TORoot THEN ERROR;
TiogaOps.SelectPoint[viewer: cd.viewer, caret: [td.TONode, 0]];
TiogaOps.SetLooks["f", caret];
FOR l: NAT IN [0 .. cd.det.lines) DO
TiogaOps.InsertChar['\n];
td.lineS[l] ← [
tiogaCount: 0,
key: Atom.MakeAtom[IO.PutFR["Before%g", IO.int[l]]]
];
ENDLOOP;
};
cd.otherInstanceData ← td;
cd.viewer ← ViewerOps.CreateViewer[flavor: tiogaDisplayViewerClassFlavor, info: [name: cd.name]];
cd.viewer.tipTable ← tiogaDisplayViewerClass.tipTable;
cd.viewer.menu ← tiogaDisplayViewerClass.menu.CopyMenu[];
ViewerOps.AddProp[viewer: cd.viewer, prop: tdProp, val: td];
td.TORoot ← TiogaOps.ViewerDoc[cd.viewer];
td.TONode ← TiogaOps.LastChild[td.TORoot];
[push: td.eatChars, pull: td.spitChars] ← IOClasses.CreatePipe[];
cd.fromDisplay ← td.spitChars;
td.spaces ← " ";
FOR i: INT IN [0 .. cd.det.columns) DO
td.spaces ← td.spaces.Cat[" "];
ENDLOOP;
TiogaOps.CallWithLocks[InitDoc, td.TORoot];
InputFocus.SetInputFocus[cd.viewer];
SetLines[cd, td];
};
SetLines: PROC [cd: CharDisplay, td: TiogaDisplay, recursed: BOOLFALSE] = {
IsBegin: PROC [ci: INT] RETURNS [is: BOOL] =
INLINE {is ← ci = 0 OR nr.Fetch[ci-1] = '\n};
nr: ROPE;
lineNum, prevCI: INT;
td.TONode ← TiogaOps.LastChild[td.TORoot];
nr ← TiogaOps.GetRope[td.TONode];
lineNum ← cd.det.lines;
prevCI ← nr.Length[];
FOR ci: INT ← nr.Length[]-1, ci-1 WHILE lineNum > 0 DO
IF ci < 0 THEN {
IF recursed THEN ERROR;
TiogaOps.SelectPoint[viewer: cd.viewer, caret: [td.TONode, 0]];
FOR i: INT IN [0 .. lineNum] DO TiogaOps.InsertChar['\n] ENDLOOP;
SetLines[cd, td, TRUE];
EXIT;
};
IF IsBegin[ci] THEN {
lineNum ← lineNum - 1;
TiogaOps.PutTextKey[node: td.TONode, where: ci, key: td.lineS[lineNum].key];
td.lineS[lineNum].tiogaCount ← prevCI - ci - 1;
prevCI ← ci;
};
ENDLOOP;
};
DeleteChar: PROC [cd: CharDisplay] = {
td: TiogaDisplay ← NARROW[cd.otherInstanceData];
base: INT ← TiogaOps.GetTextKey[node: td.TONode, key: td.lineS[cd.line].key].where;
WithLocks: PROC [root: TiogaOps.Ref] = {
IF root # td.TORoot THEN ERROR;
TiogaOps.SelectPoint[viewer: cd.viewer, caret: [td.TONode, base+cd.col]];
TiogaOps.DeleteNextCharacter[];
td.lineS[cd.line].tiogaCount ← td.lineS[cd.line].tiogaCount - 1;
};
IF cd.col >= td.lineS[cd.line].tiogaCount THEN RETURN;
TiogaOps.CallWithLocks[WithLocks, td.TORoot];
};
TTakeChar: PROC [cd: CharDisplay, char: CHAR] = {
td: TiogaDisplay ← NARROW[cd.otherInstanceData];
base: INT ← TiogaOps.GetTextKey[node: td.TONode, key: td.lineS[cd.line].key].where;
wasEmpty: BOOL ← td.lineS[cd.line].tiogaCount = 0;
WithLocks: PROC [root: TiogaOps.Ref] = {
IF root # td.TORoot THEN ERROR;
TiogaOps.SelectPoint[viewer: cd.viewer, caret: [td.TONode, base+MIN[cd.col, td.lineS[cd.line].tiogaCount]]];
IF td.lineS[cd.line].tiogaCount < cd.col THEN {
TiogaOps.SetLooks[looks: "f", which: caret];
WHILE td.lineS[cd.line].tiogaCount < cd.col DO
TiogaOps.InsertChar[' ];
td.lineS[cd.line].tiogaCount ← td.lineS[cd.line].tiogaCount + 1;
ENDLOOP;
};
IF (NOT td.inserting) AND td.lineS[cd.line].tiogaCount > cd.col THEN {
TiogaOps.DeleteNextCharacter[];
td.lineS[cd.line].tiogaCount ← td.lineS[cd.line].tiogaCount - 1};
TiogaOps.SetLooks[looks: td.looks, which: caret];
TiogaOps.InsertChar[char];
td.lineS[cd.line].tiogaCount ← td.lineS[cd.line].tiogaCount + 1;
};
TiogaOps.CallWithLocks[WithLocks, td.TORoot];
IF wasEmpty OR cd.col = 0 THEN SetLines[cd, td];
TCursorMove[cd, 0, 1, TRUE];
};
TCursorMove: PROC [cd: CharDisplay, line, col: INT, relative: BOOLFALSE, doLine, doCol: BOOLTRUE] = {
td: TiogaDisplay ← NARROW[cd.otherInstanceData];
WithLocks: PROC [root: TiogaOps.Ref] = {
colDelta: INT;
IF root # td.TORoot THEN ERROR;
IF line >= cd.det.lines THEN {
delta: INT ← line - (cd.det.lines-1);
base: INT ← TiogaOps.GetTextKey[node: td.TONode, key: td.lineS[cd.det.lines-1].key].where;
line ← line - delta;
TiogaOps.SelectPoint[viewer: cd.viewer, caret: [td.TONode, base+td.lineS[cd.det.lines-1].tiogaCount]];
FOR i: INT IN [0 .. delta) DO TiogaOps.InsertChar['\n] ENDLOOP;
SetLines[cd, td];
};
cd.line ← line;
cd.col ← col;
colDelta ← cd.col - td.lineS[cd.line].tiogaCount;
TiogaOps.SelectPoint[
viewer: cd.viewer,
caret: [
td.TONode,
TiogaOps.GetTextKey[node: td.TONode, key: td.lineS[cd.line].key].where + MIN[cd.col, td.lineS[cd.line].tiogaCount]]
];
IF colDelta > 0 THEN {
wasEmpty: BOOL ← td.lineS[cd.line].tiogaCount = 0;
TiogaOps.SetLooks["f", caret];
TiogaOps.InsertRope[td.spaces.Substr[0, colDelta]];
td.lineS[cd.line].tiogaCount ← td.lineS[cd.line].tiogaCount + colDelta;
IF wasEmpty THEN SetLines[cd, td];
};
};
IF relative THEN {line ← line + cd.line; col ← col + cd.col};
IF NOT doLine THEN line ← cd.line;
IF NOT doCol THEN col ← cd.col;
IF cd.det.autoMargins THEN {
dl: INT ← col / cd.det.columns;
line ← line + dl;
col ← col - dl * cd.det.columns;
}
ELSE col ← MIN[col, cd.det.columns-1];
WHILE col < 0 DO col ← col + cd.det.columns; line ← line - 1 ENDLOOP;
IF line < 0 THEN line ← 0;
TiogaOps.CallWithLocks[WithLocks, td.TORoot];
};
TLine: PROC [cd: CharDisplay, insert: BOOL] = {
td: TiogaDisplay ← NARROW[cd.otherInstanceData];
endBase: INT ← TiogaOps.GetTextKey[node: td.TONode, key: td.lineS[cd.det.lines-1].key].where;
endEnd: INT ← endBase + td.lineS[cd.det.lines-1].tiogaCount;
base: INT ← TiogaOps.GetTextKey[node: td.TONode, key: td.lineS[cd.line].key].where;
WithLocks: PROC [root: TiogaOps.Ref] = {
IF root # td.TORoot THEN ERROR;
IF insert THEN {
TiogaOps.SetSelection[
viewer: cd.viewer,
start: [td.TONode, endBase],
end: [td.TONode, endEnd]];
TiogaOps.Delete[];
TiogaOps.SelectPoint[viewer: cd.viewer, caret: [td.TONode, base]];
TiogaOps.InsertChar['\n];
}
ELSE {
TiogaOps.SelectPoint[viewer: cd.viewer, caret: [td.TONode, endEnd+1]];
TiogaOps.InsertChar['\n];
TiogaOps.SetSelection[
viewer: cd.viewer,
start: [td.TONode, base],
end: [td.TONode, base + td.lineS[cd.line].tiogaCount]];
TiogaOps.Delete[];
};
};
TiogaOps.CallWithLocks[WithLocks, td.TORoot];
SetLines[cd, td];
};
ClearTo: PROC [cd: CharDisplay, where: Where] = {
td: TiogaDisplay ← NARROW[cd.otherInstanceData];
lastLine: INTSELECT where FROM
EndOfLine => cd.line,
EndOfScreen => cd.det.lines-1,
ENDCASE => ERROR;
beginBase: INT ← TiogaOps.GetTextKey[node: td.TONode, key: td.lineS[cd.line].key].where;
endBase: INT ← TiogaOps.GetTextKey[node: td.TONode, key: td.lineS[lastLine].key].where;
Inner: PROC [root: TiogaOps.Ref] = {
IF root # td.TORoot THEN ERROR;
TiogaOps.SetSelection[
viewer: cd.viewer,
start: [td.TONode, beginBase + MIN[cd.col, td.lineS[cd.line].tiogaCount]],
end: [td.TONode, endBase + td.lineS[lastLine].tiogaCount],
pendingDelete: TRUE];
FOR l: INT IN [cd.line .. lastLine] DO
TiogaOps.InsertChar['\n];
ENDLOOP;
};
TiogaOps.CallWithLocks[Inner, td.TORoot];
SetLines[cd, td];
};
ClearAll: PROC [cd: CharDisplay] = {
td: TiogaDisplay ← NARROW[cd.otherInstanceData];
begin: INT ← TiogaOps.GetTextKey[node: td.TONode, key: td.lineS[0].key].where;
end: INT ← TiogaOps.GetTextKey[node: td.TONode, key: td.lineS[cd.det.lines-1].key].where + td.lineS[cd.det.lines-1].tiogaCount;
WithLocks: PROC [root: TiogaOps.Ref] = {
IF root # td.TORoot THEN ERROR;
TiogaOps.SetSelection[
viewer: cd.viewer,
start: [td.TONode, begin],
end: [td.TONode, end],
pendingDelete: TRUE];
FOR l: INT IN [0 .. cd.det.lines) DO
TiogaOps.InsertChar['\n];
ENDLOOP;
};
TiogaOps.CallWithLocks[WithLocks, td.TORoot];
SetLines[cd, td];
};
SetMode: PROC [cd: CharDisplay, mode: Mode, on: BOOL] = {
td: TiogaDisplay ← NARROW[cd.otherInstanceData];
cd.modes[mode] ← on;
SELECT mode FROM
insertChar => td.inserting ← on;
IN Emph => ComputeLooks[cd, td];
ENDCASE => ERROR;
};
ComputeLooks: PROC [cd: CharDisplay, td: TiogaDisplay] = {
td.looks ← "f";
IF cd.modes[underline] THEN td.looks ← td.looks.Cat["z"];
IF cd.modes[standout] THEN td.looks ← td.looks.Cat["b"];
};
Emphasize: PROC [cd: CharDisplay, emph: Emph, on: BOOL] = {
td: TiogaDisplay ← NARROW[cd.otherInstanceData];
loc: INT ← TiogaOps.GetTextKey[node: td.TONode, key: td.lineS[cd.line].key].where + cd.col;
looks: ROPESELECT emph FROM
underline => "z",
standout => "b",
ENDCASE => ERROR;
WithLocks: PROC [root: TiogaOps.Ref] = {
IF root # td.TORoot THEN ERROR;
TiogaOps.SetSelection[
viewer: cd.viewer,
start: [td.TONode, loc],
end: [td.TONode, loc]];
(IF on THEN TiogaOps.AddLooks ELSE TiogaOps.SubtractLooks)[looks];
};
IF cd.col > td.lineS[cd.line].tiogaCount THEN RETURN;
TiogaOps.CallWithLocks[WithLocks, td.TORoot];
};
Flash: PROC [cd: CharDisplay] = {
MessageWindow.Append["Beep", TRUE];
MessageWindow.Blink[];
MessageWindow.Clear[];
};
InitTiogaDisplayClass[];
}.