{Routine to call the editor of choice depending on value of symbol EDITOR}

{Richard M. Marshall - 12-February-1985}

%external %string (255) Copyright %alias "NEWS_EDIT_(C)_RMM_85" = -
   "Copyright (C) 1985 Richard M. Marshall"

%include "IE:IE.Inc"
%include "IE:Terminal.Inc"
%include "Inc:Dict.Imp"
%include "inc:ecce.imp"
%include "Inc:util.Imp"
%include "inc:vtlib.imp"
%include "News.Inc"

{EDIT}

%dynamic %integer %function Edit %alias "NEWS_EDIT" -
                                 (%string (255) In, Second, Out,
                                  %integer X, Y)
   %constant %integer Unknown = 0,
                      IE = 1,
                      VECCE = 2
   %own %integer Which Editor = Unknown
   %own %string (255) Profile File,
                      Keyboard File
   %integer Success = Yes

   {IDENTIFY EDITOR}

   %routine Identify Editor
      %integer At
      %string (255) S
      
      Which Editor = IE
      At = Ref Name ("EDITOR", Com Dict)
      %if At # 0 %start
          Trans Name (Integer (At), S)
         To Upper (S)
         Which Editor = VECCE %if S = "VECCE" %or %c
                                  S = "ECCE"
      %finish
      %if Which Editor = IE %start
         Profile File  = Default Profile
         Keyboard File = Default Keyboard 
         Journal File  = Default Journal
      %finish
   %end {Identify Editor}

   {IE IT}

   %routine IE It
      @ 16_3FA0 %byte rows
      %record (Preload fm) Preload
   
      %on Abort %start
         Success = No
         %return
      %finish

      %if Second = "" %start
         Preloads == Nil
      %else
         Preload_File Name = Second
         Preload_Buffer Name = "ORIGINAL"
         Preload_Next == Nil
         Preloads == Preload
      %finish

      IE Editor (In, Out,
                 0, Rows - 1,
                 Y, X,
                 Profile File,
                 Keyboard File,
                 Confirm!Silent!Reset Heap)
   %end {IE It}

   {VECCE IT}

   %routine VECCE It

      ! This version stollen from CLEAN

      !ECCExx:    Implementation of ECCE for 2900/EMAS, VAX/VMS and APM
      !  Revised specification (1981/82) including video support.
      !  Hamish Dewar   Edinburgh University Computer Science Department
      !
      
      %constinteger MAXNAME=127
      
         %string (255) Param Line 
         %integer i, same, FP, L Beg
         %string(255) heading

         %ownrecord(edfile) MAIN=0,SEC=0
         %ownstring(maxname) OUTNAME

         %on 0 %start
            select input (0)
            select output (0)
            pop frame; pop frame
            set mode(0)
            Success = No
            Set Up Terminal
            %return
         %finish

      main_name = In
      outname = Out
      sec_name = Second
         same = 0;  same = 1 %if outname = main_name
         main_flag = 32768;  !plus extra
         connect edfile(main)
         %return %if main_flag # 0

      {Now try and set FP}
      %unless X = 1 = Y %start
         FP = Main_Start 2
         Main_Line = Y
         Y = Y - 1
         L Beg = FP
         %cycle
            %if Y = 0 %start
               X = X - 1
               %exit %if X = 0
            %finish
            %if Byte Integer (FP) = NL %start
               Y = Y - 1
               FP = FP + 1  
               L Beg = FP
            %else 
               FP = FP + 1
            %finish   
         %repeat %until FP = Main_Lim 2
         Main_FP = FP
         Main_L Beg = L Beg
      %finish

         sec_flag = 0
         connect edfile (sec)

         %if outname = "" %start
            main_change = -1
         %finish

         main_lim1 = main_start1;  sec_lim1 = sec_start1
         {main_fp = main_start2;}  main_change = 16_7FFFFFFF
      Heading = ""
      Reset Terminal
      Select Input (0)
         edi(main,sec,heading)
      Set Up Terminal
      
         !MAIN_FLAG is negative if edit abandoned
         !MAIN_CHANGE is untouched (neg or zero) if no changes

         %if main_flag < 0 %or (main_change = 16_7FFFFFFF %and same # 0) %start
           main_change = -1
         %finish
 
         %if main_change >= 0 %start;  !file to be written
           sec_name = main_name;  main_name = outname
           disconnect edfile(main)
         %finish

      Success = No %if Main_Flag < 0
   %end {VECCE It}

   {MAIN CODE OF EDIT}

   %if Which Editor = Unknown %start
      Identify Editor
   %finish
   %if Which Editor = IE %start
      IE It
   %else
      VECCE It
   %finish
   %result = Success
%end {Edit}

%end %of %file
