TEditDocumentsCacheImpl.mesa
Copyright © 1985, 1986 by Xerox Corporation. All rights reserved.
Edited by Paxton on October 20, 1982 8:39 am
Last Edited by: Maxwell, January 6, 1983 11:10 am
Last Edited by: Plass, April 10, 1985 10:17:55 am PST
Doug Wyatt, September 2, 1986 3:05:27 pm PDT
DIRECTORY
Process USING [Detach],
Rope USING [Concat, Equal, ROPE],
TEditDocumentsCache USING [Update],
TEditDocumentPrivate USING [],
TEditInput USING [FreeTree],
TEditProfile USING [unsavedDocumentCacheSize],
TextNode USING [Node];
TEditDocumentsCacheImpl: CEDAR MONITOR
IMPORTS Process, Rope, TEditDocumentsCache, TEditInput, TEditProfile
EXPORTS TEditDocumentsCache, TEditDocumentPrivate
= BEGIN
ROPE: TYPE ~ Rope.ROPE;
list: LIST OF UnsavedDocumentInfo;
UnsavedDocumentInfo: TYPE = REF UnsavedDocumentInfoRec;
UnsavedDocumentInfoRec: TYPE = RECORD [fileName: ROPE, root: TextNode.Node];
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.Node] = {
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.Node;
IF (oldRoot ← SearchList[fileName]) #
NIL
THEN
-- discard previous entry, if any
TEditInput.FreeTree[oldRoot];
info ← NEW[UnsavedDocumentInfoRec ← [fileName, root]];
list ← CONS[info, list];
WHILE Size[] >
MAX[TEditProfile.unsavedDocumentCacheSize, 0]
DO
DiscardOldest[];
ENDLOOP;
TRUSTED { Process.Detach[FORK TEditDocumentsCache.Update[]] };
};
FindUnsavedDocument: PUBLIC ENTRY PROC [fileName: ROPE]
RETURNS [root: TextNode.Node] = {
ENABLE UNWIND => NULL;
IF (root ← SearchList[fileName]) # NIL THEN TRUSTED {Process.Detach[FORK TEditDocumentsCache.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.Node] = {
-- 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.