-- Saving to a file info
-- Save.info

SaveStyleProc: Menus.MenuProc = BEGIN
 -- Save the style currently in the style tool in a file names <StyleName>.style where
 -- <StyleName> is the name found in the style name viewer.
 styleFileName: Rope.ROPE;
 handle: StyleToolHandle ← NARROW[clientData];
 IF ~handle.outer.newVersion THEN RETURN;
 styleFileName ← ViewerTools.GetContents[handle.styleNameViewer];
 IF Rope.Size[styleFileName] = 0 THEN {
  MessageWindow.Append[message: "Enter style name prior to loading.", clearFirst: TRUE];
  MessageWindow.Blink[];
  RETURN };
 -- Check if ".style" already appended
 IF Rope.Find[s1: styleFileName, s2: ".style", case: FALSE] = -1 THEN
  styleFileName ← Rope.Cat[styleFileName, ".style"];
 IF ~MessageWindow.Confirm[Rope.Cat["Confirm write to ", styleFileName, "..."]] THEN RETURN
 ELSE {
  SaveStyle[styleFileName];
  MessageWindow.Append[message: Rope.Cat["Current style saved in ", styleFileName], clearFirst: TRUE];
  handle.outer.newVersion ← FALSE;
  ViewerOps.PaintViewer[handle.outer, header];
  };
  
 END;
 
SaveStyle: PROC [filename: Rope.ROPE] = BEGIN
 file: IO.Handle;
 file ← IO.CreateFileStream[fileName: filename, accessOptions: overwrite, createOptions: none
  ! IO.Error => SELECT ec FROM
       IllegalFileName => {MessageWindow.Append[message: Rope.Cat[
             filename, " is not a valid file name"], clearFirst: TRUE];
             MessageWindow.Blink[];
             CONTINUE};
       ENDCASE => ERROR -- shouldn't happen
  ]; -- Write beginning of file:
 IO.PutRope[file, Rope.Cat["% ", filename]];
 IO.PutChar[file, IO.CR]; IO.PutChar[file, IO.CR];
 IO.PutRope[file, "BeginStyle"];
 IO.PutChar[file, IO.CR]; IO.PutChar[file, IO.CR];
 
 -- Write ending of file
 IO.PutRope[file, "EndStyle"];
 
 IO.Flush[file];
 IO.Close[file];
 END;