! STAMPS - Programme five                     J.G.H. Summer 1978
! FORMAT programme to insert newlines into the output.
!
!
!
!        This programme is designed to read an input stream and if
! the line is more than 70 characters long it will force a newline.
!       If the programme contains newline characters before the 70th
! characters then these are printed and the character count restarts,
! until either the next NL is met or 70 characters are output.
!   The reason for this is that output for the Feranti Master Plotter
! will contain no NLs so that Input Buffer Store  may be used  and if
! the output is to be sent to a line printer, or viewed on a terminal
! it must have these newlines inserted.
!     So that the programme is of more general use, it can be 
! applied to text files. If it is forced to break a line then
! it breaks it at the last space output on the line (if there
! was one). The master plotter input contains no spaces (except
! in 'Comment Modes', and these are treated as normal text).
!
!
! The format of the command (on ISYS) is -
!
!   EDWIN.FORMAT <input stream> / <output stream>
!
!
!
!
%begin

   %CONST %BYTE %INTEGER LINE LENGTH = 70
   %CONST %BYTE %INTEGER SPACE = ' '

   %INTEGER SYM,I,K
   %BYTE %INTEGER %ARRAY STORE (1:LINE LENGTH)
   %OWN %INTEGER END LINE PTR = 1
   
   %ROUTINE OUTPUT STORE
      %for K=1,1,END LINE PTR %cycle
         PRINT SYMBOL (STORE(K))
      %REPEAT
      NEWLINE %IF STORE(K)#NL
   %END

   %ON %EVENT 9 %START
       OUTPUT STORE %IF I > 1
       %STOP
   %FINISH

   SELECT INPUT (1)
   SELECT OUTPUT (1)
   %for I=1,1,LINE LENGTH %cycle
      READ SYMBOL (SYM)
      STORE (I) = SYM
      END LINE PTR = I %IF SYM = SPACE
      %IF I=LINE LENGTH  %OR  SYM = NL %START
          END LINE PTR = I %IF END LINE PTR = 1  %OR SYM = NL
          ! We want to output all of the line if there has
          ! been no sutiable break point.
          OUTPUT STORE
          %IF END LINE PTR<I %START
              %for K=END LINE PTR+1,1,I %cycle
                 STORE (K-END LINE PTR) = STORE (K)
              %REPEAT
              ! This moves remaining text to start of next line.
              I = K - END LINE PTR
              ! Next free position on line.
          %FINISH  %ELSE  I = 0
          END LINE PTR = 1
      %FINISH
   %REPEAT
%END %OF %PROGRAM
