{Main program calling section for IE on APM}

%include "IE:IE.INC"
%include "IE:Terminal.Inc"

%include "Inc:UTIL.Imp"
!%include "Inc:DICT.Imp"

%conststring(15) memory file name = ".IE_Memory"
%conststring(15) last   file name = ".IE_Last"

%conststring(15) previous memory file = ".IE_Memory:-1"
%conststring(15) previous last   file = ".IE_Last:-1"

%externalroutinespec L delete file(%record(*)%name access, %string(255) file)

%begin
   %own %string (255) In File = "",
                      Preload List = "",
                      Out File = "",
                      Def Profile = "",
                      Profile File = "",
                      Keyboard File = ""
   %string (255) Orig Keyboard File
   !@ 16_3FA0 %byte rows, cols
   %constinteger rows = 24, cols = 80

   %integer SL = 1,
            SP = 1,
            IE Bits = 0,
            Memory Used = 0
   %constant %integer Memory     Flag = 2_10000000000000000000000000000000,
                      Last       Flag = 2_01000000000000000000000000000000,
                      Top        Flag = 2_00100000000000000000000000000000,
                      Save       Flag = 2_00010000000000000000000000000000,
                      Read       Flag = 2_00001000000000000000000000000000,
                      Create     Flag = 2_00000100000000000000000000000000,
                      Confirm    Flag = 2_00000010000000000000000000000000,
                      Silent     Flag = 2_00000001000000000000000000000000,
                      Recover    Flag = 2_00000000100000000000000000000000,
                      Ignore     Flag = 2_00000000010000000000000000000000,
                      Adopt      Flag = 2_00000000001000000000000000000000,
                      No Profile Flag = 2_00000000000100000000000000000000

   %constant %integer Default Flags = Confirm Flag!Save Flag

   %own %integer Bits = Default Flags

   !%record %format IE Stuff fm (%string (127) Memory,
   !                                           Last,
   !                                           Journal File,
   !                                           Profile File,
   !                                           Keyboard File,
   !                             %byte Terminal,
   !                             %integer Line, Position)
   !
   !%record(IE stuff fm) TEMP IE STUFF
   !
   !%record (IE Stuff fm) %name IE Stuff
   !%integer Stuff At

   %string(255) from memory = "", last file = ""
   %integer start line = 0, start column = 0
   %integer last start line = 0, last start column = 0

   %on 9, Abort %start
      {Event 9 comes from PAM}
      select input(0);  select output(0)
      %if Event_Event = Abort %start
         Reset Terminal
         %if Event_Message = "" %start
            Event_Message = "(to provide an error report)" 
         %finish
         Print String ("IE fails: " . Event_Message)
      %finish
      New Line
      %signal 0, 1;  ! IMN_trap
   %finish

   %routine Include Buffers {A quick GMC hack}
     %string(255) next

     %routine add buffer (%string(255)%name next)
        %record(Preload Fm)%name pF

        %return %if next = "" %or next = " "

        %if %not exists(next) %start
           %signal abort, 0, 0, "Preload file ".next." not found" 
        %finish

        pF == nil
        pF == new(pF)
        pF_Buffer Name = next
        pF_File Name = next

        %if Preloads == nil %start
          Preloads == pF
          Preloads_next == nil
        %else
          pF_next == Preloads
          Preloads == pF
        %finish
     %end

     Preloads == nil

     %if Preload List # "" %start

       next = ""
       %while Preload List -> next.(" ").Preload List %cycle
         add buffer(next)
       %repeat

       add buffer(Preload List)
     %finish

   %end

   select input(0);  select output(0)

   !Stuff At = Ref Name ("IE_Stuff", Sys Dict)
   !%if Stuff At = 0 %start
   !   Stuff At = Def Name("IE_Stuff", Sys Dict, Size Of (IE Stuff))
   !   IE Stuff == Record (Stuff At)
   !   IE Stuff =  0
   !%else
   !  IE Stuff == Record (Stuff At)
   !%finish
   !IE stuff == TEMP IE stuff;  IE stuff = 0

!M    %begin
!M       ! Read the memory file (if any)
!M       %on 3, 9 %start
!M          select input(0)
!M          start line = 1
!M          start column = 1
!M          from memory = ""
!M          %return
!M       %finish
!M       open input(3, memory file name)
!M       select input(3)
!M       read(start line)
!M       read(start column)
!M       read(from memory)
!M       close input
!M       select input(0)
!M    %end
!M
!M    %begin
!M       ! Read the "last" file (if any)
!M       %on 3, 9 %start
!M          select input(0)
!M          start line = 1
!M          start column = 1
!M          from memory = ""
!M          %return
!M       %finish
!M       open input(3, last file name)
!M       select input(3)
!M       read(last start line)
!M       read(last start column)
!M       read(last file)
!M       close input
!M       select input(0)
!M    %end

   Define Param ("INPUT",
                 In File,
                 PAM Major)

   Define Param ("PREload list -- list of files for inclusion as buffers",
                 Preload List,
                 0)

   Define Param ("OUTPUT",
                 Out File,
                 PAM New Group!PAM Major)

   Def Profile = Default Profile
   Profile File = Def Profile

   Define Param ("Profile",
                 Profile File,
                 0)

   Journal File = Default Journal
   Define Param ("Journal",
                 Journal File,
                 0)

   Define Boolean Params ( %c
"Memory,Last,Top,SAve_position,Read,CReate,COnfirm,SIlent,RECover,IGNore,ADOPT,NOProfile",
                          Bits,
                          0)

   Terminal Model = Default Terminal
   Define Enum Param ("V200,V55,VT100,VT220,Freedom100,WY50,WY75",
                      Terminal Model,
                      PAM MAjor)

   Terminal Width = Cols
   Define Int Param ("Width",
                     Terminal Width,
                     0)

   Orig Keyboard File = Default Keyboard
   Keyboard File = Orig Keyboard File
   Define Param ("Keyboard",
                 Keyboard File,
                 0)

   Process Parameters (CLI Param)

   %if Bits&Last Flag # 0 %start
      %if in file # "" %start
         Print String (" Input file and " . To String (PAM_Key Flag) . "LAST?")
         New Line
         %stop
      %else
         %if last file = "" %start
            Print String ("No Last File")
            New Line
            %stop
         %finish
         In File = last file
      %finish
   %else
      %if in file = "" %start
         %if from Memory = "" %start
            Print String ("No Memory - please give file name")
            New Line
            %stop
         %finish
         In File = from memory
         %if start line # 0 %start
            SL = start line
            SP = start column
         %finish
         %if Bits&Top Flag # 0 %start
            SL = 1
            SP = 1
         %finish
         Memory Used = 1
      %finish
   %finish

   out file = in file %if out file = ""

   out file = "" %if Bits&Read Flag # 0
   in file  = "" %if Bits&Create Flag # 0

   %if out file = "" = in file %start
      Print String (To String (PAM_Key Flag) . "READ and " . -
                    To String (PAM_Key Flag) . "CREATE?")
      New Line
      %stop
   %finish

   IE Bits = IE Bits!Confirm %if Bits&Confirm Flag # 0
   IE Bits = IE Bits!Silent  %if Bits&Silent  Flag # 0
   IE Bits = IE Bits!Recover Edit %if Bits&Recover Flag # 0
   IE Bits = IE Bits!Ignore Differences %if Bits&Ignore Flag # 0
   IE Bits = IE Bits!Adopt Defaults %if Bits&Adopt Flag # 0

   %if Keyboard File = Orig Keyboard File %and %c
       Terminal Model # Visual 200 %start
      Keyboard File = Default Keyboard
   %finish

   !IE Stuff_Journal File = Journal File
   %if Profile File # Def Profile %start
      {in the case the qualifier -Profile=... has been used to set it}
      !IE Stuff_Profile File = Profile File
   %finish
   !IE Stuff_Terminal = Terminal Model
   !IE Stuff_Keyboard File = Keyboard File

   Profile File = "" %if Bits&NoProfile Flag # 0

   Set Up Terminal

   Include Buffers

   !! printstring("About to call IE: ");  printstring(in file)
   !! space;  printstring(out file)
   !! space;  printstring(profile file)
   !! space;  printstring(keyboard file)
   !! print symbol(13);  newline
   IE Editor (In File, Out File,
              0, Rows - 1,
              SL, SP,
              Profile File,
              Keyboard File,
              IE Bits)
   Reset Terminal

!M %if (Bits & (memory flag ! Save Flag) # 0 %or Memory Used # 0) %c
!M       %and bits & read flag = 0 %start
!M    %begin
!M       %on 3, 9 %start
!M          select output(0)
!M          printstring("IE fails: cannot write memory file")
!M          newline
!M          %return
!M       %finish
!M       open output(3, memory file name)
!M       select output(3)
!M       write(SL, 1)
!M       write(SP, 1)
!M       space
!M       printstring(out file)
!M       newline
!M       close output
!M       select output(0)
!M    %end
!M %finish
!M
!M %begin
!M    %on 3, 9 %start
!M       select output(0)
!M       printstring("IE fails: cannot write last file")
!M       newline
!M       %return
!M    %finish
!M    open output(3, last file name)
!M    select output(3)
!M    write(SL, 1)
!M    write(SP, 1)
!M    space
!M    printstring(out file)
!M    newline
!M    close output
!M    select output(0)
!M %end
!M
!M L delete file(nil, previous last file) %while exists(previous last file)
!M L delete file(nil, previous memory file) %while exists(previous memory file)

   Select Output (0)
   New Line
%end

%end %of %file
