TEditDocumentsCacheImpl.mesa; Edited by Paxton on October 20, 1982 8:39 am
Last Edited by: Maxwell, January 6, 1983 11:10 am
Last Edited by: Plass, March 16, 1983 11:59 am
DIRECTORY
Process USING [Detach],
Rope USING [Concat, Equal, ROPE],
TEditDocumentsCache,
TEditDocumentPrivate,
TEditInput USING [FreeTree],
TEditProfile USING [unsavedDocumentCacheSize],
TextNode USING [pZone, Ref];
TEditDocumentsCacheImpl: CEDAR MONITOR
IMPORTS Process, Rope, TEditDocumentsCache, TEditInput, TEditProfile, TextNode
EXPORTS TEditDocumentsCache, TEditDocumentPrivate =
BEGIN OPEN TEditDocumentsCache, TEditDocumentPrivate;
list: LIST OF UnsavedDocumentInfo;
UnsavedDocumentInfo: TYPE = REF UnsavedDocumentInfoRec;
UnsavedDocumentInfoRec: TYPE = RECORD [fileName: ROPE, root: TextNode.Ref];
GetList: PUBLIC ENTRY PROC RETURNS [rope: ROPE] = {
FOR l: LIST OF UnsavedDocumentInfo ← list, l.rest UNTIL l=NIL DO
rope ← Rope.Concat[rope, Rope.Concat["\n", l.first.fileName]];
ENDLOOP };
RecordUnsavedDocument: PUBLIC ENTRY PROC [fileName: ROPE, root: TextNode.Ref] = {
ENABLE UNWIND => NULL;
Size: PROC RETURNS [size: INTEGER] = {
size ← 0;
IF list=NIL THEN RETURN;
FOR l: LIST OF UnsavedDocumentInfo ← list, l.rest UNTIL l=NIL DO
size ← size+1;
ENDLOOP };
info: UnsavedDocumentInfo;
oldRoot: TextNode.Ref;
IF (oldRoot ← SearchList[fileName]) # NIL THEN -- discard previous entry, if any
TEditInput.FreeTree[oldRoot];
info ← TextNode.pZone.NEW[UnsavedDocumentInfoRec ← [fileName, root]];
list ← TextNode.pZone.CONS[info, list];
WHILE Size[] > MAX[TEditProfile.unsavedDocumentCacheSize, 0] DO DiscardOldest[]; ENDLOOP;
TRUSTED {Process.Detach[FORK Update[]] }};
FindUnsavedDocument: PUBLIC ENTRY PROC [fileName: ROPE]
RETURNS [root: TextNode.Ref] = {
ENABLE UNWIND => NULL;
IF (root ← SearchList[fileName]) # NIL THEN TRUSTED {Process.Detach[FORK Update[]] }};
DiscardOldest: PROC = {
IF list=NIL THEN RETURN;
FOR l: LIST OF UnsavedDocumentInfo ← list, l.rest DO
IF l.rest # NIL THEN LOOP; -- spin until get to the oldest entry
TEditInput.FreeTree[SearchList[l.first.fileName]];
RETURN;
ENDLOOP };
SearchList: PROC [fileName: ROPE] RETURNS [root: TextNode.Ref] = { -- if find, then remove
prev: LIST OF UnsavedDocumentInfo;
IF list=NIL THEN RETURN [NIL];
IF Rope.Equal[list.first.fileName, fileName, FALSE] THEN {
root ← list.first.root;
list ← list.rest;
RETURN };
prev ← list;
FOR l: LIST OF UnsavedDocumentInfo ← list.rest, l.rest UNTIL l=NIL DO
IF Rope.Equal[l.first.fileName, fileName, FALSE] THEN {
root ← l.first.root;
prev.rest ← l.rest;
RETURN };
prev ← l;
ENDLOOP;
RETURN [NIL] };
END.