xref: /freebsd/contrib/one-true-awk/testdir/tt.15 (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
1# fmt - format
2#    input:  text
3#    output: text formatted into lines of <= 72 characters
4
5BEGIN {
6        maxlen = 72
7}
8
9/^[ \t]/ { printline(); print; next }      # verbatim
10###/^ +/ { printline();  }			# whitespace == break
11
12/./  { for (i = 1; i <= NF; i++) addword($i); next }
13
14/^$/ { printline(); print "" }
15END  { printline() }
16
17function addword(w) {
18    print "adding [", w, "] ", length(w), length(line), maxlen
19    if (length(line) + length(w) > maxlen)
20        printline()
21    if (length(w) > 2 && ( w ~ /[\.!]["?)]?$/ || w ~ /[?!]"?$/) &&
22		w !~ /^(Mr|Dr|Ms|Mrs|vs|Ph.D)\.$/)
23        w = w " "
24    line = line " " w
25}
26
27function printline() {
28    if (length(line) > 0) {
29        sub(/ +$/, "", line)
30        print substr(line, 2)   # removes leading blank
31        line = ""
32    }
33}
34