home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / PASCAL / PT03.ZIP / TED.AR < prev    next >
Encoding:
Text File  |  1983-09-01  |  35.1 KB  |  1,513 lines

  1. -h- altpatsi.ted 398
  2. { patsize -- returns size of pattern entry at pat[n] }
  3. function patsize (var pat : string; n : integer) : integer;
  4. begin
  5.  if (pat[n] = LITCHAR) then
  6.   patsize := 2
  7.  else if (pat[n] in [BOL, EOL, ANY]) then
  8.   patsize := 1
  9.  else if (pat[n] = CCL) or (pat[n] = NCCL) then
  10.   patsize := pat[n+1] + 2
  11.  else if (pat[n] = CLOSURE) then
  12.   patsize := CLOSIZE
  13.  else
  14.   error('in patsize: can''t happen')
  15. end;
  16.  
  17. -h- amatch.ted 1225
  18. { amatch -- look for match of pat[j]... at lin[offset]... }
  19. function amatch (var lin : string; offset : integer;
  20.   var pat : string; j : integer) : integer;
  21. var
  22.  i, k : integer;
  23.  done : boolean;
  24. {$include:'locate.ted'}
  25. {$include:'omatch.ted' }
  26. {$include:'patsize.ted'}
  27. begin
  28.  done := false;
  29.  while (not done) and (pat[j] <> ENDSTR) do
  30.   if (pat[j] = CLOSURE) then begin
  31.    j := j + patsize(pat, j); { step over CLOSURE }
  32.    i := offset;
  33.    { match as many as possible }
  34.    while (not done) and (lin[i] <> ENDSTR) do
  35.     if (not omatch(lin, i, pat, j)) then
  36.      done := true;
  37.    { i points to input character that made us fail }
  38.    { match rest of pattern against rest of input }
  39.    { shrink closure by 1 after each failure }
  40.    done := false;
  41.    while (not done) and (i >= offset) do begin
  42.     k := amatch(lin, i, pat, j+patsize(pat,j));
  43.     if (k > 0) then { matched rest of pattern }
  44.      done := true
  45.     else
  46.      i := i - 1
  47.    end;
  48.    offset := k; { if k = 0 failure else success }
  49.    done := true
  50.   end
  51.   else if (not omatch(lin, offset, pat, j)) then begin
  52.    offset := 0;  { non-closure }
  53.    done := true
  54.   end
  55.   else { omatch succeeded on this pattern element }
  56.    j := j + patsize(pat, j);
  57.  amatch := offset
  58. end;
  59.  
  60. -h- amatch0.ted 296
  61. { amatch -- with no metacharacters }
  62. function amatch (var lin : string; i : integer;
  63.   var pat : string; j : integer) : integer;
  64. begin
  65.  while (pat[j] <> ENDSTR) and (i > 0) do
  66.   if (lin[i] <> pat[j]) then
  67.    i := 0 { no match }
  68.   else begin
  69.    i := i + 1;
  70.    j := j + 1
  71.   end;
  72.  amatch := i
  73. end;
  74.  
  75. -h- amatch1.ted 320
  76. { amatch -- with some metacharacters }
  77. function amatch (var lin : string; i : integer;
  78.   var pat : string; j : integer) : integer;
  79. #include "omatch.p"
  80. begin
  81.  while (pat[j] <> ENDSTR) and (i > 0) do
  82.   if (omatch(lin, i, pat, j)) then
  83.    j := j + patsize(pat, j)
  84.   else
  85.    i := 0; { no match possible }
  86.  amatch := i
  87. end;
  88.  
  89. -h- append.ted 524
  90. { append -- append lines after "line" }
  91. function append (line : integer; glob : boolean) : stcode;
  92. var
  93.  inline : string;
  94.  stat : stcode;
  95.  done : boolean;
  96. begin
  97.  if (glob) then
  98.   stat := ERR
  99.  else begin
  100.   curln := line;
  101.   stat := OK;
  102.   done := false;
  103.   while (not done) and (stat = OK) do
  104.    if (not getline(inline, STDIN, MAXSTR)) then
  105.     stat := ENDDATA
  106.    else if (inline[1] = PERIOD)
  107.      and (inline[2] = NEWLINE) then
  108.     done := true
  109.    else if (puttxt(inline) = ERR) then
  110.     stat := ERR
  111.  end;
  112.  append := stat
  113. end;
  114.  
  115. -h- blkmove.ted 295
  116. { blkmove -- move block of lines n1..n2 to after n3 }
  117. procedure blkmove (n1, n2, n3 : integer);
  118. begin
  119.  if (n3 < n1-1) then begin
  120.   reverse(n3+1, n1-1);
  121.   reverse(n1, n2);
  122.   reverse(n3+1, n2)
  123.  end
  124.  else if (n3 > n2) then begin
  125.   reverse(n1, n2);
  126.   reverse(n2+1, n3);
  127.   reverse(n1, n3)
  128.  end
  129. end;
  130.  
  131. -h- catsub.ted 437
  132. { catsub -- add replacement text to end of new }
  133. procedure catsub (var lin : string; s1, s2 : integer;
  134.   var sub : string; var new : string;
  135.   var k : integer; maxnew : integer);
  136. var
  137.  i, j : integer;
  138.  junk : boolean;
  139. begin
  140.  i := 1;
  141.  while (sub[i] <> ENDSTR) do begin
  142.   if (sub[i] = DITTO) then
  143.    for j := s1 to s2-1 do
  144.     junk := addstr(lin[j], new, k, maxnew)
  145.   else
  146.    junk := addstr(sub[i], new, k, maxnew);
  147.   i := i + 1
  148.  end
  149. end;
  150.  
  151. -h- change.ted 570
  152. { change -- change "from" into "to" on each line }
  153. procedure change;
  154. {$include:'findcons.fnd'}
  155.  DITTO = 1;
  156. var
  157.  lin, pat, sub, arg : string;
  158. {$include:'getpat.ted'}
  159. {$include:'getsub.ted'}
  160. {$include:'subline.ted'}
  161. begin
  162.  if (not getarg(1, arg, MAXSTR)) then
  163.   error('usage: change from [to]');
  164.  if (not getpat(arg, pat)) then
  165.   error('change: illegal "from" pattern');
  166.  if (not getarg(2, arg, MAXSTR)) then
  167.   arg[1] := ENDSTR;
  168.  if (not getsub(arg, sub)) then
  169.   error('change: illegal "to" string');
  170.  while (getline(lin, STDIN, MAXSTR)) do
  171.   subline(lin, pat, sub)
  172. end;
  173.  
  174. -h- ckglob.ted 755
  175. { ckglob -- if global prefix, mark lines to be affected }
  176. function ckglob (var lin : string; var i : integer;
  177.   var status : stcode) : stcode;
  178. var
  179.  n : integer;
  180.  gflag : boolean;
  181.  temp : string;
  182. begin
  183.  if (lin[i] <> GCMD) and (lin[i] <> XCMD) then
  184.   status := ENDDATA
  185.  else begin
  186.   gflag := (lin[i] = GCMD);
  187.   i := i + 1;
  188.   if (optpat(lin, i) = ERR) then
  189.    status := ERR
  190.   else if (default(1,lastln,status) <> ERR) then begin
  191.    i := i + 1; { mark affected lines }
  192.    for n := line1 to line2 do begin
  193.     gettxt(n, temp);
  194.     putmark(n, (match(temp, pat) = gflag))
  195.    end;
  196.    for n := 1 to line1-1 do { erase other marks }
  197.     putmark(n, false);
  198.    for n := line2+1 to lastln do
  199.     putmark(n, false);
  200.    status := OK
  201.   end
  202.  end;
  203.  ckglob := status
  204. end;
  205.  
  206. -h- ckp.ted 338
  207. { ckp -- check for "p" after command }
  208. function ckp (var lin : string; i : integer;
  209.   var pflag : boolean; var status : stcode) : stcode;
  210. begin
  211.  skipbl(lin, i);
  212.  if (lin[i] = PCMD) then begin
  213.   i := i + 1;
  214.   pflag := true
  215.  end
  216.  else
  217.   pflag := false;
  218.  if (lin[i] = NEWLINE) then
  219.   status := OK
  220.  else
  221.   status := ERR;
  222.  ckp := status
  223. end;
  224.  
  225. -h- clrbuf1.ted 176
  226. { clrbuf (in memory) -- initialize for new file }
  227. procedure clrbuf;
  228. var
  229.  i : integer;
  230. begin
  231.  for i := 0 to MAXLINES do
  232.   if (buf[i].txt <> NIL) then dispose(buf[i].txt);
  233. end;
  234.  
  235. -h- clrbuf2.ted 134
  236. { clrbuf (scratch file) -- dispose of scratch file }
  237. procedure clrbuf;
  238. begin
  239.  xclose(scrin);
  240.  xclose(scrout);
  241.  remove(edittemp)
  242. end;
  243.  
  244. -h- default.ted 292
  245. { default -- set defaulted line numbers }
  246. function default (def1, def2 : integer;
  247.   var status : stcode) : stcode;
  248. begin
  249.  if (nlines = 0) then begin
  250.   line1 := def1;
  251.   line2 := def2
  252.  end;
  253.  if (line1 > line2) or (line1 <= 0) then
  254.   status := ERR
  255.  else
  256.   status := OK;
  257.  default := status
  258. end;
  259.  
  260. -h- docmd.ted 2897
  261. { docmd -- handle all commands except globals }
  262. function docmd (var lin : string; var i : integer;
  263.   glob : boolean; var status : stcode) : stcode;
  264. var
  265.  fil, sub : string;
  266.  line3 : integer;
  267.  gflag, pflag : boolean;
  268. begin
  269.  pflag := false; { may be set by d, m, s }
  270.  status := ERR;
  271.  if (lin[i] = PCMD) then begin
  272.   if (lin[i+1] = NEWLINE) then
  273.     if (default(curln, curln, status) = OK) then
  274.    status := doprint(line1, line2)
  275.  end
  276.  else if (lin[i] = NEWLINE) then begin
  277.   if (nlines = 0) then
  278.    line2 := nextln(curln);
  279.   status := doprint(line2, line2)
  280.  end
  281.  else if (lin[i] = QCMD) then begin
  282.   if (lin[i+1]=NEWLINE) and (nlines=0) and (not glob) then
  283.    status := ENDDATA
  284.  end
  285.  else if (lin[i] = ACMD) then begin
  286.   if (lin[i+1] = NEWLINE) then
  287.    status := append(line2, glob)
  288.  end
  289.  else if (lin[i] = CCMD) then begin
  290.   if (lin[i+1] = NEWLINE) then
  291.     if (default(curln, curln, status) = OK) then
  292.     if (lndelete(line1, line2, status) = OK) then
  293.    status := append(prevln(line1), glob)
  294.  end
  295.  else if (lin[i] = DCMD) then begin
  296.   if (ckp(lin, i+1, pflag, status) = OK) then
  297.     if (default(curln, curln, status) = OK) then
  298.     if (lndelete(line1, line2, status) = OK) then
  299.     if (nextln(curln) <> 0) then
  300.    curln := nextln(curln)
  301.  end
  302.  else if (lin[i] = ICMD) then begin
  303.   if (lin[i+1] = NEWLINE) then begin
  304.    if (line2 = 0) then
  305.     status := append(0, glob)
  306.    else
  307.     status := append(prevln(line2), glob)
  308.   end
  309.  end
  310.  else if (lin[i] = EQCMD) then begin
  311.   if (ckp(lin, i+1, pflag, status) = OK) then begin
  312.    putdec(line2, 1);
  313.    putc(NEWLINE)
  314.   end
  315.  end
  316.  else if (lin[i] = MCMD) then begin
  317.   i := i + 1;
  318.   if (getone(lin, i, line3, status) = ENDDATA) then
  319.    status := ERR;
  320.   if (status = OK) then
  321.     if (ckp(lin, i, pflag, status) = OK) then
  322.     if (default(curln, curln, status) = OK) then
  323.    status := move(line3)
  324.  end
  325.  else if (lin[i] = SCMD) then begin
  326.   i := i + 1;
  327.   if (optpat(lin, i) = OK) then
  328.     if (getrhs(lin, i, sub, gflag) = OK) then
  329.     if (ckp(lin, i+1, pflag, status) = OK) then
  330.     if (default(curln, curln, status) = OK) then
  331.    status := subst(sub, gflag, glob)
  332.  end
  333.  else if (lin[i] = ECMD) then begin
  334.   if (nlines = 0) then
  335.     if (getfn(lin, i, fil) = OK) then begin
  336.    scopy(fil, 1, savefile, 1);
  337.    clrbuf;
  338.    setbuf;
  339.    status := doread(0, fil)
  340.   end
  341.  end
  342.  else if (lin[i] = FCMD) then begin
  343.   if (nlines = 0) then
  344.     if (getfn(lin, i, fil) = OK) then begin
  345.    scopy(fil, 1, savefile, 1);
  346.    putstr(savefile, STDOUT);
  347.    putc(NEWLINE);
  348.    status := OK
  349.   end
  350.  end
  351.  else if (lin[i] = RCMD) then begin
  352.   if (getfn(lin, i, fil) = OK) then
  353.    status := doread(line2, fil)
  354.  end
  355.  else if (lin[i] = WCMD) then begin
  356.   if (getfn(lin, i, fil) = OK) then
  357.     if (default(1, lastln, status) = OK) then
  358.    status := dowrite(line1, line2, fil)
  359.  end;
  360.  { else status is ERR }
  361.  
  362.  if (status = OK) and (pflag) then
  363.   status := doprint(curln, curln);
  364.  docmd := status
  365. end;
  366.  
  367. -h- dodash.ted 818
  368. { dodash - expand set at src[i] into dest[j], stop at delim }
  369. procedure dodash (delim : character; var src : string;
  370.   var i : integer; var dest : string;
  371.   var j : integer; maxset : integer);
  372. var
  373.  k : integer;
  374.  junk : boolean;
  375. begin
  376.  while (src[i] <> delim) and (src[i] <> ENDSTR) do begin
  377.   if (src[i] = ESCAPE) then
  378.    junk := addstr(esc(src, i), dest, j, maxset)
  379.   else if (src[i] <> DASH) then
  380.    junk := addstr(src[i], dest, j, maxset)
  381.   else if (j <= 1) or (src[i+1] = ENDSTR) then
  382.    junk := addstr(DASH,dest,j,maxset) { literal - }
  383.   else if (isalphanum(src[i-1]))
  384.     and (isalphanum(src[i+1]))
  385.     and (src[i-1] <= src[i+1]) then begin
  386.    for k := src[i-1]+1 to src[i+1] do
  387.     junk := addstr(k, dest, j, maxset);
  388.    i := i + 1
  389.   end
  390.   else
  391.    junk := addstr(DASH, dest, j, maxset);
  392.   i := i + 1
  393.  end
  394. end;
  395.  
  396. -h- doglob.ted 592
  397. { doglob -- do command at lin[i] on all marked lines }
  398. function doglob (var lin : string; var i, cursave : integer;
  399.   var status : stcode) : stcode;
  400. var
  401.  count, istart, n : integer;
  402. begin
  403.  status := OK;
  404.  count := 0;
  405.  n := line1;
  406.  istart := i;
  407.  repeat
  408.   if (getmark(n)) then begin
  409.    putmark(n, false);
  410.    curln := n;
  411.    cursave := curln;
  412.    i := istart;
  413.    if (getlist(lin, i, status) = OK) then
  414.      if (docmd(lin, i, true, status) = OK) then
  415.     count := 0
  416.   end
  417.   else begin
  418.    n := nextln(n);
  419.    count := count + 1
  420.   end
  421.  until (count > lastln) or (status <> OK);
  422.  doglob := status
  423. end;
  424.  
  425. -h- doprint.ted 297
  426. { doprint -- print lines n1 through n2 }
  427. function doprint (n1, n2 : integer) : stcode;
  428. var
  429.  i : integer;
  430.  line : string;
  431. begin
  432.  if (n1 <= 0) then
  433.   doprint := ERR
  434.  else begin
  435.   for i := n1 to n2 do begin
  436.    gettxt(i, line);
  437.    putstr(line, STDOUT)
  438.   end;
  439.   curln := n2;
  440.   doprint := OK
  441.  end
  442. end;
  443.  
  444. -h- doread.ted 573
  445. { doread -- read "fil" after line n }
  446. function doread (n : integer; var fil : string) : stcode;
  447. var
  448.  count : integer;
  449.  t : boolean;
  450.  stat : stcode;
  451.  fd : filedesc;
  452.  inline : string;
  453. begin
  454.  fd := open(fil, IOREAD);
  455.  if (fd = IOERROR) then
  456.   stat := ERR
  457.  else begin
  458.   curln := n;
  459.   stat := OK;
  460.   count := 0;
  461.   repeat
  462.    t := getline(inline, fd, MAXSTR);
  463.    if (t) then begin
  464.     stat := puttxt(inline);
  465.     if (stat <> ERR) then
  466.      count := count + 1
  467.    end
  468.   until (stat <> OK) or (t = false);
  469.   xclose(fd);
  470.   putdec(count, 1);
  471.   putc(NEWLINE)
  472.  end;
  473.  doread := stat
  474. end;
  475.  
  476. -h- dowrite.ted 402
  477. { dowrite -- write lines n1..n2 into file }
  478. function dowrite (n1, n2 : integer; var fil : string) : stcode;
  479. var
  480.  i : integer;
  481.  fd : filedesc;
  482.  line : string;
  483. begin
  484.  fd := create(fil, IOWRITE);
  485.  if (fd = IOERROR) then
  486.   dowrite := ERR
  487.  else begin
  488.   for i := n1 to n2 do begin
  489.    gettxt(i, line);
  490.    putstr(line, fd)
  491.   end;
  492.   xclose(fd);
  493.   putdec(n2-n1+1, 1);
  494.   putc(NEWLINE);
  495.   dowrite := OK
  496.  end
  497. end;
  498.  
  499. -h- editcons.ted 659
  500. { editcons -- const declarations for edit }
  501. const
  502.  MAXLINES = 2000; {a file this big would require about 200k on the heap}
  503.  MAXPAT = MAXSTR;
  504.  CLOSIZE = 1; { size of a closure entry }
  505.  DITTO = ENDFILE;
  506.  CLOSURE = STAR;
  507.  BOL = PERCENT;
  508.  EOL = DOLLAR;
  509.   ANY = QUESTION;
  510.  CCL = LBRACK;
  511.  CCLEND = RBRACK;
  512.  NEGATE = CARET;
  513.  NCCL = EXCLAM;
  514.  LITCHAR = LETC;
  515.  CURLINE = PERIOD;
  516.  LASTLINE = DOLLAR;
  517.  SCAN = SLASH;
  518.   BACKSCAN = BACKSLASH;
  519.  
  520.  ACMD = LETA; { = ord('a') }
  521.  CCMD = LETC;
  522.  DCMD = LETD;
  523.  ECMD = LETE;
  524.  EQCMD = EQUALS;
  525.  FCMD = LETF;
  526.  GCMD = LETG;
  527.  ICMD = LETI;
  528.  MCMD = LETM;
  529.  PCMD = LETP;
  530.  QCMD = LETQ;
  531.  RCMD = LETR;
  532.  SCMD = LETS;
  533.  WCMD = LETW;
  534.   XCMD = LETX;
  535.  
  536. -h- editproc.ted 767
  537. { editproc -- procedures for edit }
  538. {$include:'edprim.ted'  } { editor buffer primitives }
  539. {$include:'amatch.ted'  }
  540. {$include:'match.ted'   }
  541. {$include:'skipbl.ted'  }
  542. {$include:'optpat.ted'  }
  543. {$include:'nextln.ted'  }
  544. {$include:'prevln.ted'  }
  545. {$include:'patscan.ted' }
  546. {$include:'getnum.ted'  }
  547. {$include:'getone.ted'  }
  548. {$include:'getlist.ted' }
  549. {$include:'append.ted'  }
  550. {$include:'lndelete.ted'}
  551. {$include:'doprint.ted' }
  552. {$include:'doread.ted'  }
  553. {$include:'dowrite.ted' }
  554. {$include:'move.ted'    }
  555. {$include:'makesub.ted' }
  556. {$include:'getrhs.ted'  }
  557. {$include:'catsub.ted'  }
  558. {$include:'subst.ted'   }
  559. {$include:'ckp.ted'     }
  560. {$include:'default.ted' }
  561. {$include:'getfn.ted'   }
  562. {$include:'docmd.ted'   }
  563. {$include:'ckglob.ted'  }
  564. {$include:'doglob.ted'  }
  565. -h- edittype.ted 26
  566. {$include:'edtype1.ted'}
  567.  
  568. -h- editvar.ted 25
  569. {$include:'edvar1.ted'}
  570.  
  571. -h- edprim.ted 26
  572. {$include:'edprim1.ted'}
  573.  
  574. -h- edprim1.ted 201
  575. {$include:'setbuf1.ted'}
  576. {$include:'clrbuf1.ted'}
  577. {$include:'getmark.ted'}
  578. {$include:'putmark.ted'}
  579. {$include:'gettxt1.ted'}
  580. {$include:'reverse.ted'}
  581. {$include:'blkmove.ted'}
  582. {$include:'puttxt1.ted'}
  583.  
  584. -h- edprim2.ted 226
  585. {$include:'seek.ted'   }
  586. {$include:'setbuf2.ted'}
  587. {$include:'clrbuf2.ted'}
  588. {$include:'getmark.ted'}
  589. {$include:'putmark.ted'}
  590. {$include:'gettxt2.ted'}
  591. {$include:'reverse.ted'}
  592. {$include:'blkmove.ted'}
  593. {$include:'puttxt2.ted'}
  594.  
  595. -h- edtype1.ted 255
  596. { edittype -- types for in-memory version of edit }
  597. type
  598.  stcode = (ENDDATA, ERR, OK); { status returns }
  599.  strptr = ^string;
  600.  buftype = { in-memory edit buffer entry }
  601.   record
  602.    txt : strptr; { text of line }
  603.    mark : boolean { mark for line }
  604.   end;
  605.  
  606. -h- edtype2.ted 189
  607. { edittype -- types for scratch-file version of edit }
  608. type
  609.  stcode = (ENDDATA, ERR, OK);
  610.  buftype =
  611.   record
  612.    txt : integer; { text of line }
  613.    mark : boolean { mark for line }
  614.   end;
  615.  
  616. -h- edvar1.ted 412
  617. { editvar -- variables for edit }
  618. var
  619.  buf : array [0..MAXLINES] of buftype;
  620.  
  621.  line1 : integer; { first line number }
  622.  line2 : integer; { second line number }
  623.  nlines : integer; { # of line numbers specified }
  624.  curln : integer; { current line -- value of dot }
  625.  lastln : integer; { last line -- value of $ }
  626.  
  627.  pat : string;    { pattern }
  628.  lin : string;    { input line }
  629.  savefile : string; { remembered file name }
  630.  
  631. -h- edvar2.ted 649
  632. { editvar -- variables for edit }
  633. var
  634.  buf : array [0..MAXLINES] of buftype;
  635.  scrout : filedesc; { scratch input fd }
  636.  scrin : filedesc; { scratch output fd }
  637.  recin : integer; { next record to read from scrin }
  638.  recout : integer; { next record to write on scrout }
  639.  edittemp : string; { temp file name 'edtemp' }
  640.  
  641.  line1 : integer; { first line number }
  642.  line2 : integer; { second line number }
  643.  nlines : integer; { # of line numbers specified }
  644.  curln : integer; { current line -- value of dot }
  645.  lastln : integer; { last line -- value of $ }
  646.  
  647.  pat : string;    { pattern }
  648.  lin : string;    { input line }
  649.  savefile : string; { remembered file name }
  650.  
  651. -h- getccl.ted 545
  652. { getccl -- expand char class at arg[i] into pat[j] }
  653. function getccl (var arg : string; var i : integer;
  654.    var pat : string; var j : integer) : boolean;
  655. var
  656.  jstart : integer;
  657.  junk : boolean;
  658. begin
  659.  i := i + 1; { skip over '[' }
  660.  if (arg[i] = NEGATE) then begin
  661.   junk := addstr(NCCL, pat, j, MAXPAT);
  662.   i := i + 1
  663.  end
  664.  else
  665.   junk := addstr(CCL, pat, j, MAXPAT);
  666.  jstart := j;
  667.  junk := addstr(0, pat, j, MAXPAT); { room for count }
  668.  dodash(CCLEND, arg, i, pat, j, MAXPAT);
  669.  pat[jstart] := j - jstart - 1;
  670.  getccl := (arg[i] = CCLEND)
  671. end;
  672.  
  673. -h- getfn.ted 599
  674. { getfn -- get file name from lin[i]... }
  675. function getfn (var lin : string; var i : integer;
  676.   var fil : string) : stcode;
  677. var
  678.  k : integer;
  679.  stat : stcode;
  680. {$include:'getword.ted'}
  681. begin
  682.  stat := ERR;
  683.  if (lin[i+1] = BLANK) then begin
  684.   k := getword(lin, i+2, fil); { get new filename }
  685.   if (k > 0) then
  686.    if (lin[k] = NEWLINE) then
  687.     stat := OK
  688.  end
  689.  else if (lin[i+1] = NEWLINE)
  690.    and (savefile[1] <> ENDSTR) then begin
  691.   scopy(savefile, 1, fil, 1);
  692.   stat := OK
  693.  end;
  694.  if (stat = OK) and (savefile[1] = ENDSTR) then
  695.   scopy(fil, 1, savefile, 1); { save if no old one }
  696.  getfn := stat
  697. end;
  698.  
  699. -h- getlist.ted 719
  700. { getlist -- get list of line nums at lin[i], increment i }
  701. function getlist (var lin : string; var i : integer;
  702.  var status : stcode) : stcode;
  703. var
  704.  num : integer;
  705.  done : boolean;
  706. begin
  707.  line2 := 0;
  708.  nlines := 0;
  709.  done := (getone(lin, i, num, status) <> OK);
  710.  while (not done) do begin
  711.   line1 := line2;
  712.   line2 := num;
  713.   nlines := nlines + 1;
  714.   if (lin[i] = SEMICOL) then
  715.    curln := num;
  716.   if (lin[i] = COMMA) or (lin[i] = SEMICOL) then begin
  717.    i := i + 1;
  718.    done := (getone(lin, i, num, status) <> OK)
  719.   end
  720.   else
  721.    done := true
  722.  end;
  723.  nlines := imin(nlines, 2);
  724.  if (nlines = 0) then
  725.   line2 := curln;
  726.  if (nlines <= 1) then
  727.   line1 := line2;
  728.  if (status <> ERR) then
  729.   status := OK;
  730.  getlist := status
  731. end;
  732.  
  733. -h- getmark.ted 116
  734. { getmark -- get mark from nth line }
  735. function getmark (n : integer) : boolean;
  736. begin
  737.  getmark := buf[n].mark
  738. end;
  739.  
  740. -h- getnum.ted 681
  741. { getnum -- get single line number component }
  742. function getnum (var lin : string;  var i, num : integer;
  743.   var status : stcode) : stcode;
  744. begin
  745.  status := OK;
  746.  skipbl(lin, i);
  747.  if (isdigit(lin[i])) then begin
  748.   num := ctoi(lin, i);
  749.   i := i - 1 { move back; to be advanced at end }
  750.  end
  751.  else if (lin[i] = CURLINE) then
  752.   num := curln
  753.  else if (lin[i] = LASTLINE) then
  754.   num := lastln
  755.  else if (lin[i] = SCAN) or (lin[i] = BACKSCAN) then begin
  756.   if (optpat(lin, i) = ERR) then  { build pattern }
  757.    status := ERR
  758.   else
  759.    status := patscan(lin[i], num)
  760.  end
  761.  else
  762.   status := ENDDATA;
  763.  if (status = OK) then
  764.   i := i + 1; { next character to be examined }
  765.  getnum := status
  766. end;
  767.  
  768. -h- getone.ted 815
  769. { getone -- get one line number expression }
  770. function getone (var lin : string; var i, num : integer;
  771.   var status : stcode) : stcode;
  772. var
  773.  istart, mul, pnum : integer;
  774. begin
  775.  istart := i;
  776.  num := 0;
  777.  if (getnum(lin, i, num, status) = OK) then  { 1st term }
  778.   repeat { + or - terms }
  779.    skipbl(lin, i);
  780.    if (lin[i] <> PLUS) and (lin[i] <> MINUS) then
  781.     status := ENDDATA
  782.    else begin
  783.     if (lin[i] = PLUS) then
  784.      mul := +1
  785.     else
  786.      mul := -1;
  787.     i := i + 1;
  788.     if (getnum(lin, i, pnum, status) = OK) then
  789.      num := num + mul * pnum;
  790.     if (status = ENDDATA) then
  791.      status := ERR
  792.    end
  793.   until (status <> OK);
  794.  if (num < 0) or (num > lastln) then
  795.   status := ERR;
  796.  if (status <> ERR) then begin
  797.   if (i <= istart) then
  798.    status := ENDDATA
  799.   else
  800.    status := OK
  801.  end;
  802.  getone := status
  803. end;
  804.  
  805. -h- getpat.ted 178
  806. { getpat -- convert argument into pattern }
  807. function getpat (var arg, pat : string) : boolean;
  808. {$include:'makepat.ted'}
  809. begin
  810.  getpat := (makepat(arg, 1, ENDSTR, pat) > 0)
  811. end;
  812.  
  813. -h- getrhs.ted 470
  814. { getrhs -- get right hand side of "s" command }
  815. function getrhs (var lin : string; var i : integer;
  816.    var sub : string; var gflag : boolean) : stcode;
  817. begin
  818.  getrhs := OK;
  819.  if (lin[i] = ENDSTR) then
  820.   getrhs := ERR
  821.  else if (lin[i+1] = ENDSTR) then
  822.   getrhs := ERR
  823.  else begin
  824.   i := makesub(lin, i+1, lin[i], sub);
  825.   if (i = 0) then
  826.    getrhs := ERR
  827.   else if (lin[i+1] = ord('g')) then begin
  828.    i := i + 1;
  829.    gflag := true
  830.   end
  831.   else
  832.    gflag := false
  833.  end
  834. end;
  835.  
  836. -h- getsub.ted 181
  837. { getsub -- get substitution string into sub }
  838. function getsub (var arg, sub : string) : boolean;
  839. {$include:'makesub.ted'}
  840. begin
  841.  getsub := (makesub(arg, 1, ENDSTR, sub) > 0)
  842. end;
  843.  
  844. -h- gettxt1.ted 196
  845. { gettxt (in memory) -- get text from line n into s }
  846. procedure gettxt (n : integer; var s : string);
  847. begin
  848.  if (buf[n].txt <> NIL) then
  849.   scopy(buf[n].txt^, 1, s, 1)
  850.  else
  851.   s[1] := ENDSTR
  852. end;
  853.  
  854. -h- gettxt2.ted 275
  855. { gettxt (scratch file) -- get text from line n into s }
  856. procedure gettxt (n : integer; var s : string);
  857. var
  858.  junk : boolean;
  859. begin
  860.  if (n = 0) then
  861.   s[1] := ENDSTR
  862.  else begin
  863.   xseek(buf[n].txt, scrin);
  864.   recin := recin + 1;
  865.   junk := getline(s, scrin, MAXSTR)
  866.  end
  867. end;
  868.  
  869. -h- getword.ted 407
  870. { getword -- get word from s[i] into out }
  871. function getword (var s : string; i : integer;
  872.    var out : string) : integer;
  873. var
  874.  j : integer;
  875. begin
  876.  while (s[i] in [BLANK, TAB, NEWLINE]) do
  877.   i := i + 1;
  878.  j := 1;
  879.  while (not (s[i] in [ENDSTR,BLANK,TAB,NEWLINE])) do begin
  880.   out[j] := s[i];
  881.   i := i + 1;
  882.   j := j + 1
  883.  end;
  884.  out[j] := ENDSTR;
  885.  if (s[i] = ENDSTR) then
  886.   getword := 0
  887.  else
  888.   getword := i
  889. end;
  890.  
  891. -h- lndelete.ted 299
  892. { lndelete -- delete lines n1 through n2 }
  893. function lndelete (n1, n2 : integer; var status : stcode)
  894.   : stcode;
  895. begin
  896.  if (n1 <= 0) then
  897.   status := ERR
  898.  else begin
  899.   blkmove(n1, n2, lastln);
  900.   lastln := lastln - (n2 - n1 + 1);
  901.   curln := prevln(n1);
  902.   status := OK
  903.  end;
  904.  lndelete := status
  905. end;
  906.  
  907. -h- locate.ted 430
  908. { locate -- look for c in character class at pat[offset] }
  909. function locate (c : character; var pat : string;
  910.   offset : integer) : boolean;
  911. var
  912.  i : integer;
  913. begin
  914.  { size of class is at pat[offset], characters follow }
  915.  locate := false;
  916.  i := offset + pat[offset]; { last position }
  917.  while (i > offset) do
  918.   if (c = pat[i]) then begin
  919.    locate := true;
  920.    i := offset { force loop termination }
  921.   end
  922.   else
  923.    i := i - 1
  924. end;
  925.  
  926. -h- makepat.ted 1339
  927. { makepat -- make pattern from arg[i], terminate at delim }
  928. function makepat (var arg : string; start : integer;
  929.   delim : character; var pat : string) : integer;
  930. var
  931.  i, j, lastj, lj : integer;
  932.  done, junk : boolean;
  933. {$include:'dodash.ted'}
  934. {$include:'getccl.ted' }
  935. {$include:'stclose.ted'}
  936. begin
  937.  j := 1; { pat index }
  938.  i := start; { arg index }
  939.  lastj := 1;
  940.  done := false;
  941.  while (not done) and (arg[i] <> delim)
  942.    and (arg[i] <> ENDSTR) do begin
  943.   lj := j;
  944.   if (arg[i] = ANY) then
  945.    junk := addstr(ANY, pat, j, MAXPAT)
  946.   else if (arg[i] = BOL) and (i = start) then
  947.    junk := addstr(BOL, pat, j, MAXPAT)
  948.   else if (arg[i] = EOL) and (arg[i+1] = delim) then
  949.    junk := addstr(EOL, pat, j, MAXPAT)
  950.   else if (arg[i] = CCL) then
  951.    done := (getccl(arg, i, pat, j) = false)
  952.   else if (arg[i] = CLOSURE) and (i > start) then begin
  953.    lj := lastj;
  954.    if (pat[lj] in [BOL, EOL, CLOSURE]) then
  955.     done := true { force loop termination }
  956.    else
  957.     stclose(pat, j, lastj)
  958.   end
  959.   else begin
  960.    junk := addstr(LITCHAR, pat, j, MAXPAT);
  961.    junk := addstr(esc(arg, i), pat, j, MAXPAT)
  962.   end;
  963.   lastj := lj;
  964.   if (not done) then
  965.    i := i + 1
  966.  end;
  967.  if (done) or (arg[i] <> delim) then  { finished early }
  968.   makepat := 0
  969.  else if (not addstr(ENDSTR, pat, j, MAXPAT)) then
  970.   makepat := 0    { no room }
  971.  else
  972.   makepat := i    { all is well }
  973. end;
  974.  
  975. -h- makesub.ted 583
  976. { makesub -- make substitution string from arg in sub }
  977. function makesub (var arg : string; from : integer;
  978.   delim : character; var sub : string) : integer;
  979. var
  980.  i, j : integer;
  981.  junk : boolean;
  982. begin
  983.  j := 1;
  984.  i := from;
  985.  while (arg[i] <> delim) and (arg[i] <> ENDSTR) do begin
  986.   if (arg[i] = ord('&')) then
  987.    junk := addstr(DITTO, sub, j, MAXPAT)
  988.   else
  989.    junk := addstr(esc(arg, i), sub, j, MAXPAT);
  990.   i := i + 1
  991.  end;
  992.  if (arg[i] <> delim) then  { missing delimiter }
  993.   makesub := 0
  994.  else if (not addstr(ENDSTR, sub, j, MAXPAT)) then
  995.   makesub := 0
  996.  else
  997.   makesub := i
  998. end;
  999.  
  1000. -h- match.ted 291
  1001. { match -- find match anywhere on line }
  1002. function match (var lin, pat : string) : boolean;
  1003. var
  1004.  i, pos : integer;
  1005. {$include:'amatch.ted'}
  1006. begin
  1007.  pos := 0;
  1008.  i := 1;
  1009.  while (lin[i] <> ENDSTR) and (pos = 0) do begin
  1010.   pos := amatch(lin, i, pat, 1);
  1011.   i := i + 1
  1012.  end;
  1013.  match := (pos > 0)
  1014. end;
  1015.  
  1016. -h- move.ted 329
  1017. { move -- move line1 through line2 after line3 }
  1018. function move (line3 : integer) : stcode;
  1019. begin
  1020.  if (line1<=0) or ((line3>=line1) and (line3<line2)) then
  1021.   move := ERR
  1022.  else begin
  1023.   blkmove(line1, line2, line3);
  1024.   if (line3 > line1) then
  1025.    curln := line3
  1026.   else
  1027.    curln := line3 + (line2 - line1 + 1);
  1028.   move := OK
  1029.  end
  1030. end;
  1031.  
  1032. -h- nextln.ted 145
  1033. { nextln -- get line after n }
  1034. function nextln (n : integer) : integer;
  1035. begin
  1036.  if (n >= lastln) then
  1037.   nextln := 0
  1038.  else
  1039.   nextln := n + 1
  1040. end;
  1041.  
  1042. -h- omatch.ted 877
  1043. { omatch -- match one pattern element at pat[j] }
  1044. function omatch (var lin : string; var i : integer;
  1045.   var pat : string; j : integer) : boolean;
  1046. var
  1047.  advance : -1..1;
  1048. begin
  1049.  advance := -1;
  1050.  if (lin[i] = ENDSTR) then
  1051.   omatch := false
  1052.  else if (not (pat[j] in
  1053.    [LITCHAR, BOL, EOL, ANY, CCL, NCCL, CLOSURE])) then
  1054.   error('in omatch: can''t happen')
  1055.  else
  1056.   case pat[j] of
  1057.    LITCHAR:
  1058.     if (lin[i] = pat[j+1]) then
  1059.      advance := 1;
  1060.    BOL:
  1061.     if (i = 1) then
  1062.      advance := 0;
  1063.    ANY:
  1064.     if (lin[i] <> NEWLINE) then
  1065.      advance := 1;
  1066.    EOL:
  1067.     if (lin[i] = NEWLINE) then
  1068.      advance := 0;
  1069.    CCL:
  1070.     if (locate(lin[i], pat, j+1)) then
  1071.      advance := 1;
  1072.    NCCL:
  1073.     if (lin[i] <> NEWLINE)
  1074.        and (not locate(lin[i], pat, j+1)) then
  1075.      advance := 1
  1076.   end;
  1077.  if (advance >= 0) then begin
  1078.   i := i + advance;
  1079.   omatch := true
  1080.  end
  1081.  else
  1082.   omatch := false
  1083. end;
  1084.  
  1085. -h- optpat.ted 509
  1086. { optpat -- get optional pattern from lin[i], increment i }
  1087. function optpat (var lin : string; var i : integer) : stcode;
  1088. {$include:'makepat.ted'}
  1089. begin
  1090.  if (lin[i] = ENDSTR) then
  1091.   i := 0
  1092.  else if (lin[i+1] = ENDSTR) then
  1093.   i := 0
  1094.  else if (lin[i+1] = lin[i]) then  { repeated delimiter }
  1095.   i := i + 1 { leave existing pattern alone }
  1096.  else
  1097.   i := makepat(lin, i+1, lin[i], pat);
  1098.  if (pat[1] = ENDSTR) then
  1099.   i := 0;
  1100.  if (i = 0) then begin
  1101.   pat[1] := ENDSTR;
  1102.   optpat := ERR
  1103.  end
  1104.  else
  1105.   optpat := OK
  1106. end;
  1107.  
  1108. -h- patscan.ted 415
  1109. { patscan -- find next occurrence of pattern after line n }
  1110. function patscan (way : character; var n : integer) : stcode;
  1111. var
  1112.  done : boolean;
  1113.  line : string;
  1114. begin
  1115.  n := curln;
  1116.  patscan := ERR;
  1117.  done := false;
  1118.  repeat
  1119.   if (way = SCAN) then
  1120.    n := nextln(n)
  1121.   else
  1122.    n := prevln(n);
  1123.   gettxt(n, line);
  1124.   if (match(line, pat)) then begin
  1125.    patscan := OK;
  1126.    done := true
  1127.   end
  1128.  until (n = curln) or (done)
  1129. end;
  1130.  
  1131. -h- patsize.ted 412
  1132. { patsize -- returns size of pattern entry at pat[n] }
  1133. function patsize (var pat : string; n : integer) : integer;
  1134. begin
  1135.  if (not (pat[n] in
  1136.    [LITCHAR, BOL, EOL, ANY, CCL, NCCL, CLOSURE])) then
  1137.   error('in patsize: can''t happen')
  1138.  else
  1139.   case pat[n] of
  1140.    LITCHAR:
  1141.     patsize := 2;
  1142.    BOL, EOL, ANY:
  1143.     patsize := 1;
  1144.    CCL, NCCL:
  1145.     patsize := pat[n+1] + 2;
  1146.    CLOSURE:
  1147.     patsize := CLOSIZE
  1148.   end
  1149. end;
  1150.  
  1151. -h- prevln.ted 146
  1152. { prevln -- get line before n }
  1153. function prevln (n : integer) : integer;
  1154. begin
  1155.  if (n <= 0) then
  1156.   prevln := lastln
  1157.  else
  1158.   prevln := n - 1
  1159. end;
  1160.  
  1161. -h- putmark.ted 113
  1162. { putmark -- put mark m on nth line }
  1163. procedure putmark(n : integer; m : boolean);
  1164. begin
  1165.  buf[n].mark := m
  1166. end;
  1167.  
  1168. -h- putsub.ted 320
  1169. { putsub -- output substitution text }
  1170. procedure putsub (var lin : string; s1, s2 : integer;
  1171.   var sub : string);
  1172. var
  1173.  i, j : integer;
  1174.  junk : boolean;
  1175. begin
  1176.  i := 1;
  1177.  while (sub[i] <> ENDSTR) do begin
  1178.   if (sub[i] = DITTO) then
  1179.    for j := s1 to s2-1 do
  1180.     putc(lin[j])
  1181.   else
  1182.    putc(sub[i]);
  1183.   i := i + 1
  1184.  end
  1185. end;
  1186.  
  1187. -h- puttxt1.ted 384
  1188. { puttxt (in memory) -- put text from lin after curln }
  1189. function puttxt (var lin : string) : stcode;
  1190. begin
  1191.  puttxt := ERR;
  1192.  if (lastln < MAXLINES) then begin
  1193.   lastln := lastln + 1;
  1194.   if (buf[lastln].txt = NIL) then new(buf[lastln].txt);
  1195.   scopy(lin, 1, buf[lastln].txt^, 1);
  1196.   putmark(lastln, false);
  1197.   blkmove(lastln, lastln, curln);
  1198.   curln := curln + 1;
  1199.   puttxt := OK
  1200.  end
  1201. end;
  1202.  
  1203. -h- puttxt2.ted 369
  1204. { puttxt (scratch file) -- put text from lin after curln }
  1205. function puttxt (var lin : string) : stcode;
  1206. begin
  1207.  puttxt := ERR;
  1208.  if (lastln < MAXLINES) then begin
  1209.   lastln := lastln + 1;
  1210.   putstr(lin, scrout);
  1211.   putmark(lastln, false);
  1212.   buf[lastln].txt := recout;
  1213.   recout := recout + 1;
  1214.   blkmove(lastln, lastln, curln);
  1215.   curln := curln + 1;
  1216.   puttxt := OK
  1217.  end
  1218. end;
  1219.  
  1220. -h- reverse.ted 234
  1221. { reverse -- reverse buf[n1]...buf[n2] }
  1222. procedure reverse (n1, n2 : integer);
  1223. var
  1224.  temp : buftype;
  1225. begin
  1226.  while (n1 < n2) do begin
  1227.   temp := buf[n1];
  1228.   buf[n1] := buf[n2];
  1229.   buf[n2] := temp;
  1230.   n1 := n1 + 1;
  1231.   n2 := n2 - 1
  1232.  end
  1233. end;
  1234.  
  1235. -h- seek.ted 479
  1236. { xseek (PC) -- special version of primitive for edit }
  1237. procedure xseek (recno : integer; var fd : filedesc);
  1238. var
  1239.  junk : boolean;
  1240.  temp : string;
  1241. begin
  1242.  flush(scrout);
  1243.  {This could be REAL slow unless just moving sequentially thru file}
  1244.  if (recno < recin) then begin
  1245.   xclose(fd);
  1246.   { cheat: open scratch file by name }
  1247.   fd := mustopen(edittemp, IOREAD);
  1248.   recin := 1;
  1249.  end;
  1250.  while (recin < recno) do begin
  1251.   junk := getline(temp, fd, MAXSTR);
  1252.   recin := recin + 1
  1253.  end
  1254. end;
  1255.  
  1256. -h- setbuf1.ted 222
  1257. { setbuf (in memory) -- initialize line storage buffer }
  1258. procedure setbuf;
  1259. var
  1260.  i : integer;
  1261. begin
  1262.  new(buf[0].txt);
  1263.  buf[0].txt^[1] := ENDSTR;
  1264.  for i := 1 to MAXLINES do buf[i].txt := NIL;
  1265.  curln := 0;
  1266.  lastln := 0
  1267. end;
  1268.  
  1269. -h- setbuf2.ted 450
  1270. { setbuf (scratch file) -- create scratch file, set up line 0 }
  1271. procedure setbuf;
  1272. begin
  1273.  { setstring(edittemp, 'edtemp'); }
  1274.   edittemp[1] := ord('e');
  1275.   edittemp[2] := ord('d');
  1276.   edittemp[3] := ord('t');
  1277.   edittemp[4] := ord('e');
  1278.   edittemp[5] := ord('m');
  1279.   edittemp[6] := ord('p');
  1280.   edittemp[7] := ENDSTR;
  1281.  scrout := mustcreate(edittemp, IOWRITE);
  1282.  scrin := mustopen(edittemp, IOREAD);
  1283.  recout := 1;
  1284.  recin := 1;
  1285.  curln := 0;
  1286.  lastln := 0
  1287. end;
  1288.  
  1289. -h- skipbl.ted 164
  1290. { skipbl -- skip blanks and tabs at s[i]... }
  1291. procedure skipbl (var s : string; var i : integer);
  1292. begin
  1293.  while (s[i] = BLANK) or (s[i] = TAB) do
  1294.   i := i + 1
  1295. end;
  1296.  
  1297. -h- stclose.ted 355
  1298. { stclose -- insert closure entry at pat[j] }
  1299. procedure stclose (var pat : string; var j : integer;
  1300.   lastj : integer);
  1301. var
  1302.  jp, jt : integer;
  1303.  junk : boolean;
  1304. begin
  1305.  for jp := j-1 downto lastj do begin
  1306.   jt := jp + CLOSIZE;
  1307.   junk := addstr(pat[jp], pat, jt, MAXPAT)
  1308.  end;
  1309.  j := j + CLOSIZE;
  1310.  pat[lastj] := CLOSURE    { where original pattern began }
  1311. end;
  1312.  
  1313. -h- subline.ted 559
  1314. { subline -- substitute sub for pat in lin and print }
  1315. procedure subline (var lin, pat, sub : string);
  1316. var
  1317.  i, lastm, m : integer;
  1318.  junk : boolean;
  1319. {$include:'amatch.ted'}
  1320. {$include:'putsub.ted'}
  1321. begin
  1322.  lastm := 0;
  1323.  i := 1;
  1324.  while (lin[i] <> ENDSTR) do begin
  1325.   m := amatch(lin, i, pat, 1);
  1326.   if (m > 0) and (lastm <> m) then begin
  1327.    { replace matched text }
  1328.    putsub(lin, i, m, sub);
  1329.    lastm := m
  1330.   end;
  1331.   if (m = 0) or (m = i) then begin
  1332.    { no match or null match }
  1333.    putc(lin[i]);
  1334.    i := i + 1
  1335.   end
  1336.   else { skip matched text }
  1337.    i := m
  1338.  end
  1339. end;
  1340.  
  1341. -h- subst.ted 1284
  1342. { subst -- substitute "sub" for occurrences of pattern }
  1343. function subst (var sub : string; gflag, glob : boolean) : stcode;
  1344. var
  1345.  new, old : string;
  1346.  j, k, lastm, line, m : integer;
  1347.  stat : stcode;
  1348.  done, subbed, junk : boolean;
  1349. begin
  1350.  if (glob) then
  1351.   stat := OK
  1352.  else
  1353.   stat := ERR;
  1354.  done := (line1 <= 0);
  1355.  line := line1;
  1356.  while (not done) and (line <= line2) do begin
  1357.   j := 1;
  1358.   subbed := false;
  1359.   gettxt(line, old);
  1360.   lastm := 0;
  1361.   k := 1;
  1362.   while (old[k] <> ENDSTR) do begin
  1363.    if (gflag) or (not subbed) then
  1364.     m := amatch(old, k, pat, 1)
  1365.    else
  1366.     m := 0;
  1367.    if (m > 0) and (lastm <> m) then begin
  1368.     { replace matched text }
  1369.     subbed := true;
  1370.     catsub(old, k, m, sub, new, j, MAXSTR);
  1371.     lastm := m
  1372.    end;
  1373.    if (m = 0) or (m = k) then begin
  1374.     { no match or null match }
  1375.     junk := addstr(old[k], new, j, MAXSTR);
  1376.     k := k + 1
  1377.    end
  1378.    else { skip matched text }
  1379.     k := m
  1380.   end;
  1381.   if (subbed) then begin
  1382.    if (not addstr(ENDSTR, new, j, MAXSTR)) then begin
  1383.     stat := ERR;
  1384.     done := true
  1385.    end
  1386.    else begin
  1387.     stat := lndelete(line, line, status);
  1388.     stat := puttxt(new);
  1389.     line2 := line2+curln-line;
  1390.     line := curln;
  1391.     if (stat = ERR) then
  1392.      done := true
  1393.     else
  1394.      stat := OK
  1395.    end
  1396.   end;
  1397.   line := line + 1
  1398.  end;
  1399.  subst := stat
  1400. end;
  1401. -h- ted.ted 936
  1402. { edit -- main routine for text editor }
  1403. procedure edit;
  1404. {$include:'editcons.ted'}
  1405. {$include:'edittype.ted'}
  1406. {$include:'editvar.ted' }
  1407.  cursave, i : integer;
  1408.  status : stcode;
  1409.  more : boolean;
  1410. {$include:'editproc.ted'}
  1411. begin
  1412.  setbuf;
  1413.  pat[1] := ENDSTR;
  1414.  savefile[1] := ENDSTR;
  1415.  if (getarg(1, savefile, MAXSTR)) then
  1416.   if (doread(0, savefile) = ERR) then
  1417.    message('?');
  1418.  more := getline(lin, STDIN, MAXSTR);
  1419.  while (more) do begin
  1420.   i := 1;
  1421.   cursave := curln;
  1422.   if (getlist(lin, i, status) = OK) then begin
  1423.    if (ckglob(lin, i, status) = OK) then
  1424.     status := doglob(lin, i, cursave, status)
  1425.    else if (status <> ERR) then
  1426.     status := docmd(lin, i, false, status)
  1427.    { else ERR, do nothing }
  1428.   end;
  1429.   if (status = ERR) then begin
  1430.    message('?');
  1431.    curln := imin(cursave, lastln)
  1432.   end
  1433.   else if (status = ENDDATA) then
  1434.    more := false;
  1435.   { else OK }
  1436.   if (more) then
  1437.    more := getline(lin, STDIN, MAXSTR)
  1438.  end;
  1439.  clrbuf
  1440. end;
  1441.  
  1442. -h- ted.pas 740
  1443. {$debug-}
  1444. program outer (input,output);
  1445.  
  1446. {$include:'globcons.inc'}
  1447. {$include:'globtyps.inc'}
  1448.  
  1449. {$include:'initio.dcl'}
  1450. {$include:'flush.dcl' }
  1451.  
  1452. {$include:'isdigit.dcl' }
  1453. {$include:'isalphan.dcl'}
  1454. {$include:'ctoi.dcl'    }
  1455. {$include:'addstr.dcl'  }
  1456. {$include:'esc.dcl'     }
  1457. {$include:'error.dcl'   }
  1458. {$include:'message.dcl' }
  1459. {$include:'open.dcl'    }
  1460. {$include:'close.dcl'   }
  1461. {$include:'create.dcl'  }
  1462. {$include:'mustopen.dcl'}
  1463. {$include:'mustcrea.dcl'}
  1464. {$include:'remove.dcl'  }
  1465. {$include:'getline.dcl' }
  1466. {$include:'putstr.dcl'  }
  1467. {$include:'putdec.dcl'  }
  1468. {$include:'putc.dcl'    }
  1469. {$include:'imin.dcl'    }
  1470. {$include:'scopy.dcl'   }
  1471. {$include:'getarg.dcl'  }
  1472.  
  1473. {$include:'ted.ted'     }
  1474. BEGIN
  1475.   minitio; initio;
  1476.   edit;
  1477.   flush(0);
  1478. END.
  1479. -h- ted.mak 190
  1480. ted+initio+getfcb+flush+isdigit+ctoi+itoc+addstr+esc+
  1481. error+message+mustopen+mustcrea+open+create+close+
  1482. remove+getline+getcf+getc+putstr+putdec+putcf+putc+
  1483. imin+scopy+getarg+nargs+isalphan
  1484. -h- change.pas 430
  1485. {$debug-}
  1486. program outer (input,output);
  1487.  
  1488. {$include:'globcons.inc'}
  1489. {$include:'globtyps.inc'}
  1490.  
  1491. {$include:'initio.dcl'}
  1492. {$include:'flush.dcl' }
  1493.  
  1494. {$include:'isalphan.dcl'}
  1495. {$include:'addstr.dcl'  }
  1496. {$include:'esc.dcl'     }
  1497. {$include:'error.dcl'   }
  1498. {$include:'getline.dcl' }
  1499. {$include:'putstr.dcl'  }
  1500. {$include:'putc.dcl'    }
  1501. {$include:'getarg.dcl'  }
  1502.  
  1503. {$include:'change.ted'  }
  1504. BEGIN
  1505.   minitio; initio;
  1506.   change;
  1507.   flush(0);
  1508. END.
  1509. -h- change.mak 105
  1510. change+initio+getfcb+flush+addstr+esc+
  1511. error+getline+getcf+getc+isalphan+getarg+nargs+
  1512. putstr+putcf+putc
  1513.