Vi

Leo de Wit leo at philmds.UUCP
Mon Jun 12 21:10:34 AEST 1989


In article <1128 at vsi.COM> friedl at vsi.COM (Stephen J. Friedl) writes:
|In article <19890 at adm.BRL.MIL> thoyt at ddn-wms.arpa (Thomas Hoyt) writes:
|>   Can Vi reformat a paragraph, justifying it properly....
|
|If you don't have nroff then I'm sure somebody will come up with a
|sed script for it :-).

Ok Stephen, I could not let you be called a liar, so here's a sed
script that does basically what 'fmt' does. It's called 4mt, and like
fmt it expands tab to spaces, word wraps at 72 columns, and left aligns
to the first line's indentation.

If your system doesn't support #! <interpreter> it is easy to wrap a
shell script around it. A small explanation is perhaps welcome:
1) the first part /     /{ ... } expands tabs to spaces. Newline is
used as a marker.
2) the second part /..../!{ ... } joins lines into the pattern space
until it is at least 72 characters long.
3) A newline is put into the pattern space after 72 characters, serving
as a marker.
4) If this action splits up the first word of the line, this word is
obviously too long. The branch 2long is taken (the line will contain the
complete word).
5) If not, the newline is placed before the word it is possibly dividing.
6) After either 4) or 5), the initial space part is copied after the
newline, so that subsequent lines will have the same initial spacing.
7) The first line of the pattern space is printed and the next cycle
started.

    Leo.

P.S. It was kind of fun to experience that sed was capable of doing this.

---------->8 cut here 8<-------
#! /bin/sed -f

: tab1
/	/{
	s/^/\
/
	: tab2
	s/\(\n\)	/        \1/
	s/\(\n\)\([^	]\)	/\2       \1/
	s/\(\n\)\([^	][^	]\)	/\2      \1/
	s/\(\n\)\([^	][^	][^	]\)	/\2     \1/
	s/\(\n\)\([^	][^	][^	][^	]\)	/\2    \1/
	s/\(\n\)\([^	][^	][^	][^	][^	]\)	/\2   \1/
	s/\(\n\)\([^	][^	][^	][^	][^	][^	]\)	/\2  \1/
	s/\(\n\)\([^	][^	][^	][^	][^	][^	][^	]\)	/\2 \1/
	s/\(\n\)\([^	][^	][^	][^	][^	][^	][^	][^	]\)/\2\1/
	/	/b tab2
	s/\n//
}
/......................................................................../!{
	$p
	$d
	N
	s/\([^ 	]\)\(\n\)/\1 \2/
	s/\n[ 	]*//
	b tab1
}
s/\(........................................................................\)/\1\
/
/^ *[^ \n]*\n/b 2long
s/  *\([^ \n]*\)\(\n\)/\2\1/
: cpspac
s/^\( *\)\([^\n]*\n\)/\1\2\1/
P
D
: 2long
s/\(\n\)\([^ ]*\) */\2\1/
b cpspac
---------->8 cut here 8<-------



More information about the Comp.unix.questions mailing list