Newsgroups: comp.editors
Path: utzoo!sq!lee
From: lee@sq.sq.com (Liam R. E. Quin)
Subject: Re: simple tricks in Vi
Message-ID: <1990Jul11.024601.28733@sq.sq.com>
Summary: alternatives to the fmt command
Organization: SoftQuad Inc.
References: <579@entec.Wichita.NCR.COM>
Date: Wed, 11 Jul 90 02:46:01 GMT
Lines: 84

I mailed this, and then decided to post it, too...

blair@entec.Wichita.NCR.COM (Brian Lair) writes:
>	sh: fmt: not found

fmt is on V7 Unix and then generally only on BSD-derived Unix...
so it's not on System V, for example...

Here are a couple of simple (completely public domain...) sketches for
replacements to the fmt command.  They both work, but have less functionality
than some newer (e.g. SunOS) versions of fmt.

You can simulate it easily, though.  Here are two approaches -- one
with nroff, and one with awk.

--- cut here for the nroff version
:
(
cat << 'xxx'
.pl 1 \" page length of 1 so we don't get page breaks
.ll 61 \" set line length to max number of characters wanted per line
.nh \" disable hyphenation
.ad l \" don't pad lines with spaces to make an even right margin
. \" The next three lines turn off nroff's special processing of lines
. \" starting with a . or ' and also disable the in-line escape character,
. \" normally the backslash (\) so that the input text is passed through
. \" with no changes except line filling...
.eo \" disable backslashes (and comments) from here on...
.c2 ^A\" change the ^A to be a real control-A and delete this comment
.cc ^B\" and a control-B here... and delete this comment (from the \)
xxx
cat ${@+"$@"}
) | nroff -
--- end here

If not, here's an awk script off the top of my head....
You could put it in a shell script.... and make the 61 a parameter.
I have tried this, and it works.

cut here for the awk version. You will need to put a wrapper shell-script
around this on System V.
-- -- -- snip -- -- --
#! /bin/awk -f
BEGIN {
    MaxLength = 61
}

{
    for (i = 1; i <= NF; i++) {
	l = length($i)
	if (LengthSoFar + l + SpaceWidth > MaxLength) {
	    printf "%s\n", LineSoFar
	    LineSoFar = ""; Space = ""; SpaceWidth = 0
	    LengthSoFar = 0
	}
	LineSoFar = LineSoFar Space $i
	LengthSoFar += SpaceWidth + l
	Space = " "; SpaceWidth = 1
    }
}

(NF == 0) {
    # blank line -- end paragraph
    if (LengthSoFar) {
	printf "%s\n", LineSoFar
	LineSoFar = ""; Space = ""; SpaceWidth = 0
	LengthSoFar = 0
    }
    printf "\n" # pass the blank line through to the output
}

END {
    printf "%s\n", LineSoFar
}
-- cut here.... ---

Again, one could do this in perl, snobol or C (but I won't).  Nroff and
awk are the most wide-spread of the Unix text processing tools, so you
are most likely to have them available.

Lee
-- 
Liam R. E. Quin,  lee@sq.com, {utai,utzoo}!sq!lee,  SoftQuad Inc., Toronto
``Sun. n.s.,  Any thing eminently splendid'' [Johnson's dictionary (1837)]
