21.3. Alternatives to fmtfmt (Section 21.2) is hard to do without once you've learned about it. Unfortunately, it's not available in some versions of Unix. You can get the GNU version from the CD-ROM [see http://examples.oreilly.com/upt3], but it's also relatively easy to emulate with sed (Section 37.4) and nroff. Using those two utilities also lets you take advantage of the more sophisticated formatting and flexibility that sed and nroff macros can give you. (If you're doing anything really fancy, like tables with tbl,[59] you might need col or colcrt to clean up nroff's output.)
Here's the script: Go to http://examples.oreilly.com/upt3 for more information on: fmt.sh #!/bin/sh sed '1i\ .ll 72\ .na\ .hy 0\ .pl 1' $* | nroff The reason this is so complicated is that, by default, nroff makes some assumptions you need to change. For example, it assumes an 11-inch page (66 lines) and will add blank lines to a short file (or the end of a long file). The quick-and-dirty workaround to this is to manually put the nroff request .pl 1 (page length 1 line) at the top of the text you want to reformat. nroff also tends to justify lines; you want to turn this off with the .na request. You also want to turn off hyphenation (.hy 0), and you may want to set the line length to 72 instead of nroff's default 65, if only for consistency with the real fmt program. All these nroff requests get inserted before the first line of input by the sed 1i command. A fancier script would take a -nn line-length option and turn it into a .ll request for nroff, etc. Another solution to consider is Damian Conway's Text::Autoformat Perl module. It has some very sophisticated heurestics to try to figure out how text should be formatted, including bulleted and numbered lists. In its simplest form, it can be used to read from stdin and write to stdout, just as a standard Unix utility would do. You can invoke this module from the command line like this: % perl -MText::Autoformat -e 'autoformat' < your_file_here By default, autoformat formats only one paragraph at a time. This behavior can be changed by altering the invocation slightly: % perl -MText::Autoformat -e 'autoformat({all =>1})' The manpage for this module even suggests a way into integrate this into vi: map f !Gperl -MText::Autoformat -e'autoformat' --TOR and JJ Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|