-- TiogaFileOpsImpl.mesa
-- written by Bill Paxton, June 1982
-- last written by Paxton. June 7, 1982 2:37 pm

DIRECTORY
TiogaFileOps,
NameSymbolTable,
PutGet,
TextEdit,
TextLooks,
TextNode,
Rope;

TiogaFileOpsImpl: PROGRAM
	IMPORTS NameSymbolTable, PutGet, Rope, TextEdit, TextNode
	EXPORTS TiogaFileOps =
BEGIN
OPEN TiogaFileOps;

CreateRoot: PUBLIC PROC RETURNS [root: Ref] = {
	root ← TextNode.NewTextNode[];
	root.last ← TRUE };

InsertNode: PUBLIC PROC [x: Ref, child: BOOL ← FALSE] RETURNS [new: Ref] = {
	-- if ~child then new is sibling of x
	-- else new is first child of x
	new ← TextNode.NewTextNode[];
	IF child THEN {
		IF x.child # NIL THEN { new.next ← x.child; new.last ← FALSE }
		ELSE { new.next ← x; new.last ← TRUE };
		x.child ← new }
	ELSE { new.next ← x.next; new.last ← x.last; x.next ← new; x.last ← FALSE }};

SetContents: PUBLIC PROC [x: Ref, txt: Rope.Ref] = { x.rope ← txt; x.runs ← NIL };

AddLooks: PUBLIC PROC [x: Ref, start, len: INT, look: CHAR ['a..'z], root: Ref ← NIL] = {
	looks: TextLooks.Looks ← TextLooks.noLooks;
	looks[look] ← TRUE;
	IF root = NIL THEN root ← TextNode.NarrowToTextNode[TextNode.Root[x]];
	TextEdit.AddLooks[root, x, looks, start, len] };

SetFormat: PUBLIC PROC [x: Ref, format: Rope.Ref] = {
	name: TextNode.TypeName ← NameSymbolTable.MakeName[
		LOOPHOLE[Rope.Flatten[format]]];
	TextEdit.ChangeType[x, name] };

SetStyle: PUBLIC PROC [x: Ref, style: Rope.Ref] = { TextEdit.ChangeStyle[x, style] };

Store: PUBLIC PROC [x: Ref, filename: Rope.Ref] = { [] ← PutGet.ToFile[filename, x] };

END.