-- TiogaExecCommands.mesa
-- Last changed by Paxton, June 28, 1982 2:37 pm

DIRECTORY
TextEdit,
Directory,
TextNode,
TEditMesaOps,
GetTree,
PutGet,
File,
Rope,
IO,
TEditProfile USING [GetToken],
TEditInput,
WindowManager,
UserExec;

TiogaExecCommands: PROGRAM
IMPORTS Directory, GetTree, IO, PutGet, Rope, TEditMesaOps, TEditInput, TEditProfile, TextEdit, TextNode, UserExec, WindowManager = {

TiogaMesaLooks: UserExec.CommandProc = TRUSTED {
 DoTiogaMesa[exec, FALSE, TRUE] };

TiogaMesa: UserExec.CommandProc = TRUSTED {
 DoTiogaMesa[exec, TRUE, TRUE] };

ReadIndent: UserExec.CommandProc = TRUSTED {
  DoTiogaMesa[exec, TRUE, FALSE] };

DoTiogaMesa: PROC [exec: UserExec.ExecHandle, tree, mesaLooks: BOOLEAN] = {
 filename: Rope.ROPE;
 dollarFile: REF READONLY TEXT;
 dollarCap: File.Capability ← File.nullCapability;
 tabIndent: NAT ← 8;
 offset: INT ← 0;
 WindowManager.WaitCursor[];
DO-- process each file in command line
  root: TextNode.Ref;
  [filename,offset] ← TEditProfile.GetToken[exec.commandLine, offset];
  IF Rope.Size[filename]=0 THEN EXIT;
  IF Rope.Size[filename]=1 AND Rope.Fetch[filename,0] IN ['1..'9] THEN
   { tabIndent ← LOOPHOLE[Rope.Fetch[filename,0]-'1]; LOOP };
  IF Rope.Find[filename,"."] = -1 THEN -- add .mesa extension
  filename ← Rope.Concat[filename,".mesa"];
  { ENABLE Directory.Error => {
   IO.PutRope[exec.out, filename];
   IO.PutRope[exec.out, " not found.\n"];
   LOOP };
  root ← IF tree THEN GetTree.ReadIndent[filename, tabIndent]
   ELSE PutGet.FromFile[filename];
  };
  IO.PutChar[exec.out, '.];
  IF mesaLooks THEN {
   TextEdit.ChangeStyle[root, "Mesa"];
   [] ← TEditMesaOps.SetSpanMesaLooks[
   TextNode.MakeNodeSpan[root,TextNode.LastWithin[root]],NIL] };
  dollarFile ← Rope.ToRefText[Rope.Cat[filename, "$"]];
  dollarCap ← Directory.LookupUnlimited[fileName: LOOPHOLE[dollarFile]
   ! Directory.Error => CONTINUE];
  IF dollarCap#File.nullCapability THEN
   Directory.RemoveFile[LOOPHOLE[dollarFile], dollarCap];
  Directory.Rename[LOOPHOLE[Rope.ToRefText[filename]], LOOPHOLE[dollarFile]
   ! Directory.Error => CONTINUE];
  [] ← PutGet.ToFile[filename, root];
  TEditInput.FreeTree[root];
  ENDLOOP;
 WindowManager.UnWaitCursor[];
IO.PutRope[exec.out, " Done."] };


TiogaReadProfile: UserExec.CommandProc = TRUSTED {
 TEditInput.ReadUserProfile[] };

UserCategory: UserExec.CommandProc = TRUSTED {
 name: Rope.ROPE;
 offset: INT ← 0;
 [name,] ← TEditProfile.GetToken[exec.commandLine, offset];
 TEditInput.SetUserCategory[name] };

WritePlain: UserExec.CommandProc = TRUSTED {
 filename: Rope.ROPE;
 tabIndent: NAT ← 8;
 offset: INT ← 0;
 WindowManager.WaitCursor[];
DO-- process each file in command line
  root: TextNode.Ref;
  [filename,offset] ← TEditProfile.GetToken[exec.commandLine, offset];
  IF Rope.Size[filename]=0 THEN EXIT;
  IF Rope.Size[filename]=1 AND Rope.Fetch[filename,0] IN ['1..'9] THEN
   { tabIndent ← LOOPHOLE[Rope.Fetch[filename,0]-'1]; LOOP };
  IF Rope.Find[filename,"."] = -1 THEN -- add .mesa extension
  filename ← Rope.Concat[filename,".mesa"];
  { ENABLE Directory.Error => {
   IO.PutRope[exec.out, filename];
   IO.PutRope[exec.out, " not found.\n"];
   LOOP };
  root ← PutGet.FromFile[filename];
  };
  PutGet.WriteFilePlain[filename, root];
  TEditInput.FreeTree[root];
  ENDLOOP;
 WindowManager.UnWaitCursor[];
IO.PutRope[exec.out, " Done."] };

UserExec.RegisterCommand["TiogaMesa", TiogaMesa, "Convert Mesa files to Tioga tree format.
 Number in line gives spaces/tab -- must be in [1..9]; default is 8"];

UserExec.RegisterCommand["TiogaReadProfile", TiogaReadProfile, "Have Tioga read its part of User.Profile again."];

UserExec.RegisterCommand["UserCategory", UserCategory, "Set Tioga user category to one of
  Beginner, Intermediate, Advanced, or Expert."];

UserExec.RegisterCommand["ReadIndent", ReadIndent, "Convert to tree structure based on indenting."];

UserExec.RegisterCommand["WritePlain", WritePlain, "Convert Tioga file to unformatted file."];
}.