[net.sources] WordStar-like reformat for vi

cmi@dartvax.UUCP (Theo Pozzy/R. Green) (11/05/85)

Here is the source for the "WordStar" style reformating filter
printed in the November, 1985 issue of Unix/World magazine.  It
simulates the ^B command available in the popular WordStar (tm Micropro)
editor.  It is used to reformat the lines in a paragraph of text
so that no line is longer than 70 characters, and short lines
are joined where possible.

To compile it, simply "cc" it:

	cc -O -o rf rf.c

I have renamed it from "reform" to "rf" to reduce the number
of keystrokes necessary to invoke it from vi, although if you
define a "map" for it, that won't matter.  To invoke it, simply
position your cursor at the start of the paragraph you want to
reformat, and type:
	
	!}rf
	
For more information, see the magazine article.

Theo Pozzy
...!decvax!dartvax!cmi (USENET)
cmi@dartmouth (CSNET)

---------------------------- cut here ---------------------------------------
#!/bin/sh
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	rf.c
sed 's/^X//' << 'SHAR_EOF' > rf.c
X/*
X * rf.c - a "WordStar"-like paragraph reformatter for vi.  From the
X * "Wizard's Grabbag" article (Dr. Rebecca Thomas) in the Nov. '85
X * issue of Unix/World.
X *
X */
X
X#include <stdio.h>
X#define YES 1
X#define NO 0
X#define LINELENGTH 70
X
Xmain()
X{
X
Xint endchar;			/* does this word end a sentence? */
Xint getword();			/* get next word on stdin */
Xint length;			/* length of word read by getword */
Xint count = 0;			/* number of characters on line so far */
Xchar word[81];			/* word read from stdin */
X
Xwhile ((endchar = getword(word)) != EOF) {
X	count += length = strlen(word);
X	if (count > LINELENGTH) {	/* split the line here */
X		putchar('\n');
X		count = length;	/* beginning count for next line */
X		printf("%s ", word);
X		if (endchar == YES)
X			putchar(' ');	/* end of sentence space */
X	}
X	else	if (endchar == YES) {
X			printf("%s  ", word);	/* extra space */
X			count++;	/* and account for it */
X		}
X		else
X			printf("%s ", word);
X	count++;		/* to account for the extra space */
X}
X
Xif (strlen(word) > 0)
X	printf("%s", word);	/* print any left over word */
Xputchar('\n');
Xexit(0);
X
X}
X
Xint getword(word)
Xchar *word;			/* storage for word read */
X{
Xchar c;
Xint endflag = NO;		/* does this word end a sentence? */
Xint beginflag = YES;		/* is this the beginning of new word? */
X
Xwhile((c = getchar()) != EOF) {
X	switch(c) {
X	case '.':
X	case '!':		/* end of sentence characters */
X	case '?':
X		endflag = YES;
X		*word++ = c;
X		break;
X	case ' ':
X	case '\t':		/* word delimiters - white space */
X	case '\n':
X		if (beginflag == YES)
X			continue;	/* skip leading white space */
X		*word = '\0';		/* terminate word */
X		return(endflag);	/* non-EOF return */
X	default:		/* just another character */
X		endflag = NO;
X		*word++ = c;
X	}
X	beginflag = NO;		/* no longer at beginning of word */
X}
X
X*word = '\0';			/* terminate the word */
Xreturn(EOF);
X
X}
SHAR_EOF
exit

schwartz@uw-beaver (Michael Schwartz) (11/07/85)

> Here is the source for the "WordStar" style reformating filter
> printed in the November, 1985 issue of Unix/World magazine.  It
> simulates the ^B command available in the popular WordStar (tm Micropro)
> editor.  It is used to reformat the lines in a paragraph of text
> so that no line is longer than 70 characters, and short lines
> are joined where possible.
> 

There is a command to do this available on standard Berkeley Unix:
fmt(1).
 - Mike Schwartz