SED.RED 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. %
  2. % This program makes some systematic edits, and some that are clearly
  3. % ad hoc. The syematic changes include expanding tabs and cleaning up
  4. % trailing whitespace, plus processing of some escapes that "info" format
  5. % uses. The ad hoc ones appear to relate to "words" that have embedded
  6. % apostrophes.
  7. %
  8. % It replaces a sequence of Unix-specific commands, which included use of
  9. % expand to dispose of tabs and several uses of sed to perform the other
  10. % transformations.
  11. % It transforms from redhelp.x into redhelp.y
  12. % It will also be MUCH slower than direct use of Unix utilities, in part
  13. % because it has been coded here with simplicity, portability and clarity
  14. % as objectives above speed. After all the help file is not re-created
  15. % by end-users and not altered very often anyway.
  16. %
  17. % A C Norman. December 1995
  18. symbolic;
  19. on comp;
  20. fluid '(edits n_edits);
  21. edits := for each p in '(
  22. ("Euler's" . "Euler")
  23. ("EULER'S" . "EULER")
  24. ("Euler constant can" . "Euler's constant can")
  25. ("Catalan's" . "Catalan")
  26. ("CATALAN'S" . "CATALAN")
  27. ("Khinchin's" . "Khinchin")
  28. ("KHINCHIN'S" . "KHINCHIN")
  29. ("Khinchin book" . "Khinchin's book")
  30. ("Jacobi's" . "Jacobi")
  31. ("@$" . "$")
  32. ("@key" . "(Key)")
  33. ("@%" . "%")
  34. ("@_" . "_")
  35. ) collect (explode2 car p . explode2 cdr p);
  36. symbolic procedure matches(x, p);
  37. if null p then list x
  38. else if null x then nil
  39. else if car x eq car p then matches(cdr x, cdr p)
  40. else nil;
  41. symbolic procedure make_edits();
  42. begin
  43. scalar fi, fo, c, line, n, p, w, t0, n_edits,
  44. !*echo, !*raise, !*lower;
  45. t0 := time();
  46. fi := open("redhelp.x", 'input);
  47. if null fi then error(0, "Input file not available");
  48. fo := open("redhelp.y", 'output);
  49. if null fo then error(0, "Output file not available");
  50. fi := rds fi;
  51. fo := wrs fo;
  52. linelength 1000; % Output must never wrap.
  53. n_edits := 0;
  54. c := !$eol!$;
  55. while not (c = !$eof!$) do <<
  56. line := nil;
  57. n := 0;
  58. % While reading a line in I will process tabs, counting a tab as at least
  59. % one blank and then enough to pad out to the next multiple of 8.
  60. while not ((c := readch()) = !$eol!$ or c = !$eof!$) do <<
  61. if c = tab!* then <<
  62. repeat << line := '! . line;
  63. n := n + 1 >> until zerop remainder(n, 8) >>
  64. else << line := c . line; n := n + 1 >> >>;
  65. % Next I act on 's/ *$//g', which discards trailing blanks.
  66. while eqcar(line, '! ) do line := cdr line;
  67. line := reversip line;
  68. while line do <<
  69. for each p in edits do
  70. if (w := matches(line, car p)) then <<
  71. n_edits := n_edits + 1;
  72. line := append(cdr p, car w) >>;
  73. prin2 car line;
  74. line := cdr line >>;
  75. terpri() >>;
  76. rds fi;
  77. wrs fo;
  78. close fi;
  79. close fo;
  80. prin2 n_edits; prin2t " edits performed";
  81. return (time() - t0)/1000.0
  82. end;
  83. make_edits();
  84. quit;
  85.