Vi

alan.j.greenberger hopewell at cbnewsm.ATT.COM
Sat Jun 10 06:59:00 AEST 1989


In article <19890 at adm.BRL.MIL> thoyt at ddn-wms.arpa (Thomas Hoyt) writes:
>Folks:
>   Can Vi reformat a paragraph, justifying it properly(left, right, or center-
>ed)?  I hate editing text when I have to rejustify the right margin by hand
>after deleting or adding to a line at the top of a paragraph.  If it is possible,
>how can I do it?  Thanks much.
>******
>thoyt at ddn-wms.arpa      |  "Oh no...it's written in COBOL..."
>Thomas Hoyt             |  "Government Computers for Government business..."
>CRC Systems, Inc., Fairfax, VA -- 703-359-9400       |  "NO FUN ALLOWED..."
>******

	I use the statement "map ^A !}fill" in .exrc to pipe from the current
line to the end of paragraph out to the following filter:

/* @(#)fill.c	1.3 */
/* This program is a filter for text.  It fills whole words up to LINELEN
columns */

#include	<stdio.h>

#define		LINELEN			79
#define		NOT_SENTENCE_END	0
#define		SENTENCE_END		1
#define		FIRST_WORD		0
#define		NOT_FIRST_WORD		1
#define		SPACES_IN_TAB		8

main()
{
	char	firstchar;
	int	linelen;
	int	lastword = NOT_SENTENCE_END;
	char	word[2*LINELEN];
	int	wordlen;
	int	wordnum = FIRST_WORD;

	if((firstchar = getchar()) != ' ')
	{	/* special case to retain a leading TAB */
		if(firstchar == '\t')
		{
			linelen = SPACES_IN_TAB;
			putchar(firstchar);
		}
		else
		{
			linelen = 1;
			ungetc(firstchar,stdin);
		}
	}

	while(fscanf(stdin,"%s",word) == 1)
	{	/* got a new word from standard input */
		wordlen = strlen(word);
		if((linelen + wordlen) < LINELEN)
		{	/* append to this line */
			if(lastword == NOT_SENTENCE_END || (word[0] < 65 || word[0] > 90 /* not capital letter */))
			{
				if(wordnum == NOT_FIRST_WORD)
				{
					printf(" %s",word);
				}
				else
				{
					printf("%s",word);
				}
				linelen += wordlen + 1;
			}
			else
			{	/* SENTENCE_END */
				if((linelen + wordlen + 1) < LINELEN)
				{
					printf("  %s",word);
					linelen += wordlen + 2;
				}
				else
				{	/* put on next line */
					printf("\n%s",word);
					linelen = wordlen;
				}
			}
		}
		else
		{	/* put on next line */
			printf("\n%s",word);
			linelen = wordlen;
		}
		if(word[wordlen - 1] == '.' || word[wordlen - 1] == '?' || word[wordlen - 1] == '!')
		{	/* looks like end of sentence */
			if((strchr(word,'(') != NULL && strchr(word,')') == NULL) ||
			(strchr(word,'[') != NULL && strchr(word,']') == NULL))
			{	/* inside parenthesis */
				lastword = NOT_SENTENCE_END;
			}
			else
			{
				lastword = SENTENCE_END;
			}
		}
		else if(word[wordlen - 1] == ']' || word[wordlen - 1] == ')')
		{
			if(strchr(word,'.') != NULL)
			{
				lastword = SENTENCE_END;
			}
			else
			{
				lastword = NOT_SENTENCE_END;
			}
		}
		else
		{
			lastword = NOT_SENTENCE_END;
		}
		wordnum = NOT_FIRST_WORD;
	}
	putchar('\n');
}



More information about the Comp.unix.questions mailing list