* 
* 
*                  Ibex Version of QED program             
*                    version 1.0 (09-20-92) 
*              
* 
     str rec.1000,file.90,line.1000,line2.1000,out.1000,out2.1000,savefile.90 
     str moveup.6,formbuf.10000 
     str num.6,red.4,temp.10 
     int index(200000,2),addindex(1000,2),tindex(200000,2),x4 
     int a,b,c,d,e,f,g,h,i,j,k,n
     int qpoint,inflag,filesize,ppdsize,lastline
     int breakflag,addsize,nplace 

     table X(200000) 

* 
     nplace = 5 
     breakflag = 0 
     red = chr(27) // "&dA" 
     moveup = chr(27) // "&a-1R" 
     putc Input file? 
     getc file
     file = trm(file) 
     savefile = file 
     if file = "" 
       putc No input file.       
       inflag = 1 
       qpoint = 0 
     else 
       open [1,1] file
       inflag = 0 
       loop for i = 1 to 200000 
         if breakflag = 1 
           stop 
         end 
         getf [1] rec 
         tput [X,i] ~rec 
         index(i,1) = i 
         index(i,2) = 0 
       repeat 
       putc File exceeds 200000 records 
       stop 

eof1:  qpoint = i - 1 
       close [1]
       if qpoint > 99999 
         nplace = 6 
       end 
       putc ~qpoint  records.  
     end
     filesize = qpoint
* 
*    X table has been constructed; inflag is set; index is constructed 
* 

* 
*  Case I: no input file
* 
     if inflag = 1
       loop 
         getc line
         line = line // pad(2)
         if line{1,2} = "/r"
           perform renumber 
           goto A 
         end
         if line{1,2} = "/e"
           goto ST
         end
         if addsize = 0 
           x4 = 1 
         else 
           addindex(addsize,2) = addsize + 1
         end
         ++addsize
         ++qpoint
         addindex(addsize,1) = qpoint 
         addindex(addsize,2) = 0
         tput [X,qpoint] ~line 
       repeat 
     end
* 
*  Case II: input file, or renumbered screen input
* 
A:   getc line
AA:  line = line // pad(nplace+2) 
     breakflag = 0 
* 
*   /e Command
* 
     if line{1,2} = "/e"
       goto ST
     end
* 
*   /l Command
* 
     if line{1,2} = "/l"
       perform getlistpar 
       if a = 0 
         temp = "000000"{1,nplace} 
         putc ~temp  ********  File Listing  ******** 
         if x4 > 0
           c = x4 
           perform listaddlines 
         end
         a = 1
       end
       loop for k = a to b
         if breakflag = 1 
           goto A 
         end 
         i = index(k,1) 
         if i > 0 
           tget [X,i] rec 
           n = k
           perform makenum
           putc ~num  ~rec
         end
         c = index(k,2) 
         if c > 0 
           perform listaddlines 
         end
       repeat 
       goto A 
     end
* 
*   /d Command
* 
     if line{1,2} = "/d"
       perform getlistpar 
       if a = 0 
         a = 1
         if b = 0 
           goto A 
         end
       end
       loop for k = a to b
         i = index(k,1) 
         if i > 0 
           index(k,1) = 0 - i 
         end
       repeat 
       goto A 
     end
* 
*   /U Command
* 
     if line{1,2} = "/U"
       perform getlistpar 
       if a = 0 
         a = 1
         if b = 0 
           goto A 
         end
       end
       loop for k = a to b
         i = index(k,1) 
         if i < 0 
           index(k,1) = 0 - i 
         end
         c = index(k,2) 
         if c > 0 
UC:        i = addindex(c,1)
           if i < 0 
             addindex(c,1) = 0 - i
           end
           c = addindex(c,2) 
           if c > 0 
             goto UC
           end
         end
       repeat 
       goto A 
     end
* 
*   /L Command
* 
     if line{1,2} = "/L"
       perform getlistpar 
       if a = 0 
         temp = "000000"{1,nplace} 
         putc ~temp  ********  File Listing  ******** 
         if x4 > 0
           c = x4 
           perform listaddlines 
         end
         a = 1
       end
       loop for k = a to b
         if breakflag = 1 
           goto A 
         end 
         i = index(k,1) 
         n = k 
         perform makenum 
         if i > 0 
           tget [X,i] rec 
           putc ~num  ~rec
         else 
           i = 0 - i
           tget [X,i] rec 
           putc ~num  ~rec 
         end
         c = index(k,2) 
         if c > 0 
           perform Listaddlines 
         end
       repeat 
       goto A 
     end
* 
*   /s Command
* 
     if line{1,2} = "/s"
       perform getlistpar 
       getc line
       if a = 0 
         if x4 > 0
           c = x4 
           perform schaddlines
         end
         a = 1
       end
       loop for k = a to b
         if breakflag = 1 
           goto A 
         end 
         i = index(k,1) 
         if i > 0 
           tget [X,i] rec 
           if rec con line
             n = k
             perform makenum
             putc ~num  ~rec
           end
         end
         c = index(k,2) 
         if c > 0 
           perform schaddlines
         end
       repeat 
       goto A 
     end
* 
*   /z Command
* 
     if line{1,2} = "/z"
       perform getlistpar 
       getc line
       getc line2 
       if a = 0 
         if x4 > 0
           c = x4 
           perform zaddlines
         end
         a = 1
       end
       loop for k = a to b
         if breakflag = 1 
           goto A 
         end 
         i = index(k,1) 
         if i > 0 
           tget [X,i] rec 
           f = len(line)
           j = len(rec) 
           if rec con line
             out = rec{1,mpt-1} 
             g = mpt + f
             if g > j 
               rec = "" 
             else 
               rec = rec{g..} 
               j = len(rec) 
             end
             n = k
             perform makenum
             putc ~num  ~out ~line2 ...  
ZZ:          if j > 0 
               if rec con line
                 out = rec{1,mpt-1} 
                 g = mpt + f
                 if g > j 
                   rec = "" 
                 else 
                   rec = rec{g..} 
                   j = len(rec) 
                 end
                 putc ~out ~line2 ... 
                 goto ZZ
               end
             end
             putc ~rec
           end
         end
         c = index(k,2) 
         if c > 0 
           perform zaddlines
         end
       repeat 
       goto A 
     end
* 
*   /r Command
* 
     if line{1,2} = "/r"
       perform renumber 
       goto A 
     end
* 
*   /c Command
* 
     if line{1,2} = "/c"
       perform getplace 
       perform getlistpar 
       perform copylines
       goto A 
     end
     if line{1,2} = "/m"
       perform getplace 
       perform getlistpar 
       perform copylines
       line = "/d," // chs(a) // "," // chs(b)
       goto AA
     end
* 
*   /f Command
* 
     if line{1,2} = "/f"
       formbuf = "" 
       perform getlistpar 
       if a = 0 
         goto BP
       end
*  Construct formbuf
       loop for k = a to b
         i = index(k,1) 
         if i > 0 
           tget [X,i] rec 
           rec = trm(rec) 
           if rec <> "" 
             if rec{..} = "." 
               rec = rec // " " 
             end
             formbuf = formbuf // rec // " "
           end
         end
         c = index(k,2) 
FFA:     if c > 0 
           i = addindex(c,1)
           if i > 0 
             tget [X,i] rec 
             rec = trm(rec) 
             if rec <> "" 
               if rec{..} = "." 
                 rec = rec // " " 
               end 
               formbuf = formbuf // rec // " " 
             end 
           end
           c = addindex(c,2) 
           goto FFA 
         end
       repeat 
*  Determine where to add lines 
       h = a - 1
       if index(h,2) = 0
         g = 0
       else 
         g = 1
         c = index(h,2) 
FFD:     h = c
         c = addindex(c,2) 
         if c > 0 
           goto FFD 
         end
       end
*   if g = 0, modify index(h,2) 
*   if g = 1, modify addindex(h,2) 
* 
*   Incertion loop (FFB)
* 
FFB:   if len(formbuf) < 69 
         line = formbuf 
         formbuf = "" 
       else 
         if len(formbuf) > 80 
           line = formbuf{1,80} 
         else 
           line = formbuf 
         end
FFC:     if formbuf con " " 
           if mpt < 68
             formbuf{mpt} = chr(0) 
             i = mpt
             goto FFC 
           else 
             line = line{1,i-1} 
             line = trm(line) 
             e = len(line)
*   Deal with situation where line ends with a period 
             if line{e} = "." and len(line) > 40 
FFE:           if formbuf{e} <> chr(0) 
                 --e
                 if e > 0 
                   goto FFE 
                 end 
               end 
               if e > 0 
                 i = e 
                 line = line{1,i-1} 
               end 
             end
*   Reset and fix formbuf 
             formbuf = formbuf{i+1..} 
FFG:         if formbuf con chr(0) 
               formbuf{mpt} = " " 
               goto FFG 
             end
FFF:         if formbuf{1} = " "
               formbuf = formbuf{2..} 
               goto FFF 
             end
* 
           end
         end
       end
*  Incert line
       ++qpoint
       tput [X,qpoint] ~line 
       if g = 0 
         g = 1
         if h = 0 
           x4 = addsize + 1 
         else 
           index(h,2) = addsize + 1 
         end
       else 
         addindex(addsize,2) = addsize + 1
       end
       ++addsize
       addindex(addsize,1) = qpoint 
       addindex(addsize,2) = 0
*  Test for termination 
       if formbuf = ""
         line = "/d," // chs(a) // "," // chs(b)
         goto AA
       else 
         goto FFB 
       end
     end

  /* Entering a line?  

     if line{1,nplace} in ['0'..'9'] 
       a = int(line{1,nplace}) 
       if a = 0 
         lastline = 0 
         goto A 
       end
       if a > filesize
         goto BP
       end
       i = index(a,1) 
       if i < 0 
         goto BP
       end
       if line{nplace+1} = "-" 
         index(a,1) = 0 - i 
         goto A 
       end
       if line{nplace+1} <> " " 
         goto BP
       end
       line = line{nplace+2..} 
       if line con red 
         out2 = line{1,mpt-1} 
         line = line{mpt+4..} 
         putc .b27 L...  
         putc ~moveup ... 
         putc .b27 &a6C.b27 K~out2 
         putc       ~line
       else 
         tput [X,i] ~line 
       end
       lastline = a 
       goto A 
     end
     if line{1} = "." 
       if line{nplace+1} <> " " 
         goto BP 
       end 
       a = 0
       if line{2} = "." 
         if line{3} = "." 
           if line{4} = "." 
             if line{5} = "." and nplace = 6 
               if line{6} in ['1'..'9'] 
                 a = int(line{6}) 
               end 
             end 
             if line{5..nplace} in ['1'..'9'] 
               a = int(line{5..nplace}) 
             end 
           else 
             if line{4..nplace} in ['1'..'9'] 
               a = int(line{4..nplace}) 
             end 
           end 
         else 
           if line{3..nplace} in ['1'..'9'] 
             a = int(line{3..nplace}) 
           end 
         end 
       else 
         if line{2..nplace} in ['1'..'9'] 
           a = int(line{2..nplace}) 
         end
       end
       if a = 0 
         goto BP
       end
       if a > addsize 
         goto BP
       end
       i = addindex(a,1)
       if i < 0 
         goto BP
       end
       if line{nplace+1} = "-" 
         addindex(a,1) = 0 - i
         goto A 
       end
       line = line{nplace+2..} 
       if line con red 
         out2 = line{1,mpt-1} 
         line = line{mpt+4..} 
         putc .b27 L...  
         putc ~moveup ... 
         putc .b27 &a6C.b27 K~out2 
         putc       ~line
       else 
         tput [X,i] ~line 
       end
       lastline = a + 200000 
       goto A 
     end
     if line{1,nplace+1} = "      " 
       ++addsize
       ++qpoint
       addindex(addsize,1) = qpoint 
       line = line{nplace+2..} 
       tput [X,qpoint] ~line 
       if lastline < 200000 
         if lastline = 0
           addindex(addsize,2) = x4 
           x4 = addsize 
         else 
           addindex(addsize,2) = index(lastline,2) 
           index(lastline,2) = addsize
         end
       else 
         lastline = lastline - 200000 
         addindex(addsize,2) = addindex(lastline,2) 
         addindex(lastline,2) = addsize 
       end
       lastline = addsize + 200000 
       putc ~moveup ... 
       n = addsize
       perform makedotnum 
       putc ~num
       goto A 
     end
* 
BP:  putc .b7 ...  
     goto A 
* 
ST:  perform renumber 
     if filesize = 0
       putc Nothing in, nothing out.   
       stop 
     end
     putc Output file? ($ = source file) 
     getc file
     file = trm(file) 
     if file = "$" 
       file = savefile 
     end 
     open [1,2] file
     loop for i = 1 to filesize 
       j = index(i,1) 
       tget [X,j] line 
       putf [1] ~line 
     repeat 
     stop 
* 
trap: 
     putc .b7 ...  
     goto A 
* 
* 
*   PROCEDURES
* 
*P0:  break 
* 
* 
      procedure break 
        breakflag = 1 
      return 
* 
*P1:  getlistpar
* 
*     Purpose:  get line numbers for /l, /d, /s, /z 
* 
*     Input: line 
* 
*     Output: a = first number, b = second number 
* 
     procedure getlistpar 
       line = line{4..} 
       if line{1} in ['0'..'9',' ','e'] 
       else 
         return 1 
       end
       if line{1} = "e" 
         a = filesize 
         b = filesize 
         return 
       end
       if line{1} = " " 
         a = 1
         b = filesize 
       else 
         a = int(line)
         if a > filesize
           return 1 
         end
         if line con ","
           line = line // " " 
           line = line{mpt+1..} 
           if line{1} = " " 
             b = a
           else 
             if line{1} = "e" 
               b = filesize 
             else 
               b = int(line)
             end
           end
           if b > filesize
             return 1 
           end
         else 
           b = a
         end
       end
       if b < a 
         return 1 
       end
     return 
* 
*P2:  listaddlines
* 
*     Purpose:  list added lines from point c in add-buffer 
* 
*     Input: c = pointer to first added line
* 
     procedure listaddlines 
PLA:  
       if breakflag = 1 
         return 
       end 
       i = addindex(c,1)
       if i > 0 
         tget [X,i] rec 
         n = c
         perform makedotnum 
         putc ~num  ~rec
       end
       c = addindex(c,2) 
       if c > 0 
         goto PLA 
       end
     return 
* 
*P3:  Listaddlines
* 
*     Purpose:  same as listaddlines, but include deleted lines 
* 
     procedure Listaddlines 
PLB: 
       if breakflag = 1 
         return 
       end 
       i = addindex(c,1) 
       n = c 
       perform makedotnum 
       if i > 0 
         tget [X,i] rec 
         putc ~num  ~rec
       else 
         i = 0 - i
         tget [X,i] rec 
         putc ~num  ~rec 
       end
       c = addindex(c,2) 
       if c > 0 
         goto PLB 
       end
     return 
* 
*P4:  schaddlines 
* 
*     Purpose:  search added lines from point c for string
*                 contained in line 
* 
     procedure schaddlines
PS: 
       if breakflag = 1 
         return 
       end 
       i = addindex(c,1) 
       if i > 0 
         tget [X,i] rec 
         if rec con line
           n = c
           perform makedotnum 
           putc ~num  ~rec
         end
       end
       c = addindex(c,2) 
       if c > 0 
         goto PS
       end
     return 
* 
*P5:  zaddlines 
* 
*     Purpose:  search for line and substitute line2 in addline buffer
* 
     procedure zaddlines
PZ: 
       if breakflag = 1 
         return 
       end 
       i = addindex(c,1) 
       if i > 0 
         tget [X,i] rec 
         f = len(line)
         j = len(line) 
         if rec con line
           out = rec{1,mpt-1} 
           g = mpt + f
           if g > j 
             rec = "" 
           else 
             rec = rec{g..} 
             j = len(rec) 
           end
           n = c
           perform makedotnum 
           putc ~num  ~out ~line2 ...  
PZ2:       if j > 0 
             if rec con line
               out = rec{1,mpt-1} 
               g = mpt + f
               if g > j 
                 rec = "" 
               else 
                 rec = rec{g..} 
                 j = len(rec) 
               end
               putc ~out ~line2 ... 
               goto PZ2 
             end
           end
           putc ~rec
         end
       end
       c = addindex(c,2) 
       if c > 0 
         goto PZ
       end
     return 
* 
*P6:  renumber
* 
*     Purpose:  renumber lines; eliminate deleted lines; clear addindex 
* 
     procedure renumber 
       if filesize > 0
         loop for i = 1 to filesize 
           tindex(i,1) = index(i,1) 
           tindex(i,2) = index(i,2) 
         repeat 
       end
       k = 0
       if x4 > 0
         c = x4 
         perform addnewlines
       end
       if filesize > 0
         loop for i = 1 to filesize 
           if tindex(i,1) > 0 
             k = k + 1
             index(k,1) = tindex(i,1) 
             index(k,2) = 0 
           end
           c = tindex(i,2) 
           if c > 0 
             perform addnewlines
           end
         repeat 
       end
       filesize = k 
       addsize = 0
       x4 = 0 
     return 
* 
*P7:  addnewlines 
* 
*     Purpose:  add new lines from add-buffer starting at point c 
*                  increment k
* 
     procedure addnewlines
PAD:   if addindex(c,1) > 0 
         k = k + 1
         index(k,1) = addindex(c,1) 
         index(k,2) = 0 
       end
       c = addindex(c,2) 
       if c > 0 
         goto PAD 
       end
     return 
* 
*P8:  makenum 
* 
*     Input: n
* 
*     Output: num 
* 
     procedure makenum
       int i 
       num = chs(n) 
       i = len(num) 
       num = "000000"{1,nplace-i} // num 
     return 
  
*P9:  makedotnum
* 
*     Input: n
* 
*     Output: num 
* 
     procedure makedotnum 
       int i 
       num = chs(n) 
       i = len(num) 
       num = "......"{1,nplace-i} // num 
     return 
* 
*P10: getplace
* 
*     Purpose: get location for move and copy commands
* 
*     Input: line 
* 
*     Output: h = line after which material will be incerted
* 
     procedure getplace 
       if line con "a"
         line2 = line 
CA:      if mpt = len(line2)
           return 1 
         end
         line2 = line2{mpt+1..} 
         if line2{1} = " "
           mpt = 1
           goto CA
         end
         if line2{1} = "e"
           h = filesize 
         else 
           if line2{1} in ['0'..'9']
             h = int(line2) 
           else 
             return 1 
           end
         end
       else 
         return 1 
       end
     return 
* 
*P11: copylines 
* 
*     Purpose: copy lines a through b (including incerts) after line h
*                 don't change a or b 
* 
     procedure copylines
       if h = 0 
         f = x4 
       else 
         f = index(h,2) 
       end
       g = 0
       loop for k = a to b
         i = index(k,1) 
         if i > 0 
           perform incert 
         end
         c = index(k,2) 
KKA:     if c > 0 
           i = addindex(c,1)
           if i > 0 
             perform incert 
           end
           c = addindex(c,2) 
           goto KKA 
         end
       repeat 
     return 
* 
*P12: incert
* 
*     Inputs:  f may be index(h,2) 
*              g = situation flag 
*              h = main point of incertion
*              i = address in Q1 of record to incert
*              qpoint = address in Q1 of last incertion 
*              addsize = last add-line number 
* 
     procedure incert 
       tget [X,i] rec 
       j = len(rec)  
       ++qpoint
       tput [X,qpoint] ~rec 
       if g = 0 
         g = 1
         if h = 0 
           x4 = addsize + 1 
         else 
           index(h,2) = addsize + 1 
         end
       else 
         f = addindex(addsize,2) 
         addindex(addsize,2) = addsize + 1
       end
       ++addsize
       addindex(addsize,1) = qpoint 
       addindex(addsize,2) = f 
     return 
* 
     run