home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / BIPL.ZIP / PROCS.ZIP / REWRAP.ICN < prev    next >
Encoding:
Text File  |  1992-09-28  |  4.5 KB  |  149 lines

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