rewrap.icn: Procedures for advanced line rewrap

link rewrap
March 3, 1996; Richard L. Goerwitz
See also: wrap.icn
This file is in the public domain.

The procedure rewrap(s,i), included in this file, reformats text
fed to it into strings < i in length.  Rewrap utilizes a static
buffer, so it can be called repeatedly with different s arguments,
and still produce homogenous output.  This buffer is flushed by
calling rewrap with a null first argument.  The default for
argument 2 (i) is 70.
____________________________________________________________

Here's a simple example of how rewrap could be used.  The following
program reads the standard input, producing fully rewrapped output.

procedure main()
    every write(rewrap(!&input))
    write(rewrap())
end

Naturally, in practice you would want to do things like check for in-
dentation or blank lines in order to wrap only on a paragraph-by para-
graph basis, as in

procedure main()
    while line := read(&input) do {
        if line == "" then {
            write("" ~== rewrap())
            write(line)
        } else {
            if match("\t", line) then {
                write(rewrap())
                write(rewrap(line))
            } else {
                write(rewrap(line))
            }
        }
    }
end

Fill-prefixes can be implemented simply by prepending them to the
output of rewrap:

    i := 70; fill_prefix := " > "
    while line := read(input_file) do {
        line ?:= (f_bit := tab(many('> ')) | "", tab(0))
        write(fill_prefix || f_bit || rewrap(line, i - *fill_prefix))
        etc.

Obviously, these examples are fairly simplistic.  Putting them to
actual use would certainly require a few environment-specific
modifications and/or extensions.  Still, I hope they offer some
indication of the kinds of applications rewrap might be used in.

Note:  If you want leading and trailing tabs removed, map them to
spaces first.  Rewrap only fools with spaces, leaving tabs intact.
This can be changed easily enough, by running its input through the
Icon detab() function.

Source code | Program Library Page | Icon Home Page