home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / REWRAP.ICN < prev    next >
Text File  |  1991-07-13  |  4KB  |  147 lines

  1. ############################################################################
  2. #
  3. #    Name:     rewrap.icn
  4. #
  5. #    Title:     advanced line rewrap utility
  6. #
  7. #    Author:     Richard L. Goerwitz
  8. #
  9. #    Date:     June 3, 1991
  10. #
  11. #    Version: 1.3
  12. #
  13. ############################################################################
  14. #
  15. #  The procedure rewrap(s,i), included in this file, reformats text
  16. #  fed to it into strings < i in length.  Rewrap utilizes a static
  17. #  buffer, so it can be called repeatedly with different s arguments,
  18. #  and still produce homogenous output.  This buffer is flushed by
  19. #  calling rewrap with a null first argument.  The default for
  20. #  argument 2 (i) is 70.
  21. #
  22. #  Here's a simple example of how rewrap could be used.  The following
  23. #  program reads the standard input, producing fully rewrapped output.
  24. #
  25. #  procedure main()
  26. #      every write(rewrap(!&input))
  27. #      write(rewrap())
  28. #  end
  29. #
  30. #  Naturally, in practice you would want to do things like check for in-
  31. #  dentation or blank lines in order to wrap only on a paragraph-by para-
  32. #  graph basis, as in
  33. #
  34. #  procedure main()
  35. #      while line := read(&input) do {
  36. #          if line == "" then {
  37. #              "" ~== write(rewrap())
  38. #              write(line)
  39. #          } else {
  40. #              if match("\t", line) then {
  41. #                  write(rewrap())
  42. #                  write(rewrap(line))
  43. #              } else {
  44. #                  write(rewrap(line))
  45. #              }
  46. #          }
  47. #      }
  48. #  end
  49. #
  50. #  Fill-prefixes can be implemented simply by prepending them to the
  51. #  output of rewrap:
  52. #
  53. #      i := 70; fill_prefix := " > "
  54. #      while line := read(input_file) do {
  55. #          line ?:= (f_bit := tab(many('> ')) | "", tab(0))
  56. #          write(fill_prefix || f_bit || rewrap(line, i - *fill_prefix))
  57. #          etc.
  58. #
  59. #  Obviously, these examples are fairly simplistic.  Putting them to
  60. #  actual use would certainly require a few environment-specific
  61. #  modifications and/or extensions.  Still, I hope they offer some
  62. #  indication of the kinds of applications rewrap might be used in.
  63. #  Note:  If you want leading and trailing tabs removed, map them to
  64. #  spaces first.  Rewrap only fools with spaces, leaving tabs intact.
  65. #  This can be changed easily enough, by running its input through the
  66. #  Icon detab() function.
  67. #
  68. ############################################################################
  69. #
  70. #  See also:  wrap.icn
  71. #
  72. ############################################################################
  73.  
  74.  
  75. procedure rewrap(s,i)
  76.  
  77.     local extra_bit, line
  78.     static old_line
  79.     initial old_line := ""
  80.  
  81.     # Default column to wrap on is 70.
  82.     /i := 70
  83.     # Flush buffer on null first argument.
  84.     if /s then {
  85.     extra_bit := old_line
  86.     old_line := ""
  87.     return "" ~== extra_bit
  88.     }
  89.  
  90.     # Prepend to s anything that is in the buffer (leftovers from the last s).
  91.     s ?:= { tab(many(' ')); old_line || trim(tab(0)) }
  92.  
  93.     # If the line isn't long enough, just add everything to old_line.
  94.     if *s < i then old_line := s || " " & fail
  95.  
  96.     s ? {
  97.  
  98.     # While it is possible to find places to break s, do so.
  99.     while any(' -',line := EndToFront(i),-1) do {
  100.         # Clean up and suspend the last piece of s tabbed over.
  101.         line ?:= (tab(many(' ')), trim(tab(0)))
  102.             if *&subject - &pos + *line > i
  103.         then suspend line
  104.         else {
  105.         old_line := ""
  106.         return line || tab(0)
  107.         }
  108.     }
  109.  
  110.     # Keep the extra section of s in a buffer.
  111.     old_line := tab(0)
  112.  
  113.     # If the reason the remaining section of s was unrewrapable was
  114.     # that it was too long, and couldn't be broken up, then just return
  115.     # the thing as-is.
  116.     if *old_line > i then {
  117.         old_line ? {
  118.         if extra_bit := tab(upto(' -')+1) || (tab(many(' ')) | "")
  119.         then old_line := tab(0)
  120.         else extra_bit := old_line & old_line := ""
  121.         return trim(extra_bit)
  122.         }
  123.     }
  124.     # Otherwise, clean up the buffer for prepending to the next s.
  125.     else {
  126.         # If old_line is blank, then don't mess with it.  Otherwise,
  127.         # add whatever is needed in order to link it with the next s.
  128.         if old_line ~== "" then {
  129.         # If old_line ends in a dash, then there's no need to add a
  130.         # space to it.
  131.         if old_line[-1] ~== "-"
  132.         then old_line ||:= " "
  133.         }
  134.     }
  135.     }
  136.     
  137. end
  138.  
  139.  
  140.  
  141. procedure EndToFront(i)
  142.     # Goes with rewrap(s,i)
  143.     *&subject+1 - &pos >= i | fail
  144.     suspend &subject[.&pos:&pos <- &pos+i to &pos by -1]
  145. end
  146.