summaryrefslogtreecommitdiff
path: root/usr.bin/mg/paragraph.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/mg/paragraph.c')
-rw-r--r--usr.bin/mg/paragraph.c174
1 files changed, 102 insertions, 72 deletions
diff --git a/usr.bin/mg/paragraph.c b/usr.bin/mg/paragraph.c
index 8e1536dfe0d..f4e44042b91 100644
--- a/usr.bin/mg/paragraph.c
+++ b/usr.bin/mg/paragraph.c
@@ -2,29 +2,33 @@
* Code for dealing with paragraphs and filling. Adapted from MicroEMACS 3.6
* and GNU-ified by mwm@ucbvax. Several bug fixes by blarson@usc-oberon.
*/
+
#include "def.h"
-static int fillcol = 70;
+static int fillcol = 70;
+
#define MAXWORD 256
/*
- * go back to the begining of the current paragraph
- * here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
- * combination to delimit the begining of a paragraph
+ * Move to start of paragraph. Go back to the begining of the current
+ * paragraph here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
+ * combination to delimit the begining of a paragraph.
*/
/* ARGSUSED */
+int
gotobop(f, n)
+ int f, n;
{
- if (n < 0) /* the other way... */
+ /* the other way... */
+ if (n < 0)
return gotoeop(f, -n);
- while (n-- > 0) { /* for each one asked for */
-
+ while (n-- > 0) {
/* first scan back until we are in a word */
+ while (backchar(FFRAND, 1) && inword() == NULL);
- while (backchar(FFRAND, 1) && !inword()) {
- }
- curwp->w_doto = 0; /* and go to the B-O-Line */
+ /* and go to the B-O-Line */
+ curwp->w_doto = 0;
/*
* and scan back until we hit a <NL><SP> <NL><TAB> or
@@ -45,36 +49,43 @@ gotobop(f, n)
* beond end of buffer,
* cleanup time
*/
- curwp->w_dotp = lback(curwp->w_dotp);
- curwp->w_doto = llength(curwp->w_dotp);
+ curwp->w_dotp =
+ lback(curwp->w_dotp);
+ curwp->w_doto =
+ llength(curwp->w_dotp);
}
}
break;
}
}
- curwp->w_flag |= WFMOVE;/* force screen update */
+ /* force screen update */
+ curwp->w_flag |= WFMOVE;
return TRUE;
}
/*
- * go forword to the end of the current paragraph
- * here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
- * combination to delimit the begining of a paragraph
+ * Move to end of paragraph. Go forword to the end of the current paragraph
+ * here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE> combination to
+ * delimit the begining of a paragraph.
*/
/* ARGSUSED */
+int
gotoeop(f, n)
+ int f, n;
{
- if (n < 0) /* the other way... */
+ /* the other way... */
+ if (n < 0)
return gotobop(f, -n);
- while (n-- > 0) { /* for each one asked for */
-
+ /* for each one asked for */
+ while (n-- > 0) {
/* Find the first word on/after the current line */
curwp->w_doto = 0;
- while (forwchar(FFRAND, 1) && !inword()) {
- }
+ while (forwchar(FFRAND, 1) && inword() == NULL);
+
curwp->w_doto = 0;
curwp->w_dotp = lforw(curwp->w_dotp);
+
/* and scan forword until we hit a <NL><SP> or ... */
while (curwp->w_dotp != curbp->b_linep) {
if (llength(curwp->w_dotp)
@@ -92,43 +103,46 @@ gotoeop(f, n)
break;
}
}
- curwp->w_flag |= WFMOVE;/* force screen update */
+ /* force screen update */
+ curwp->w_flag |= WFMOVE;
return TRUE;
}
/*
- * Fill the current paragraph according to the current
- * fill column
+ * Justify a paragraph. Fill the current paragraph according to the current
+ * fill column.
*/
/* ARGSUSED */
+int
fillpara(f, n)
+ int f, n;
{
- register int c; /* current char durring scan */
- register int wordlen;/* length of current word */
- register int clength;/* position on line during fill */
- register int i; /* index during word copy */
- register int eopflag;/* Are we at the End-Of-Paragraph? */
- int firstflag; /* first word? (needs no space) */
- int newlength; /* tentative new line length */
- int eolflag;/* was at end of line */
- LINE *eopline;/* pointer to line just past EOP */
- char wbuf[MAXWORD]; /* buffer for current word */
+ int c; /* current char durring scan */
+ int wordlen; /* length of current word */
+ int clength; /* position on line during fill */
+ int i; /* index during word copy */
+ int eopflag; /* Are we at the End-Of-Paragraph? */
+ int firstflag; /* first word? (needs no space) */
+ int newlength; /* tentative new line length */
+ int eolflag; /* was at end of line */
+ LINE *eopline; /* pointer to line just past EOP */
+ char wbuf[MAXWORD]; /* buffer for current word */
/* record the pointer to the line just past the EOP */
- (VOID) gotoeop(FFRAND, 1);
+ (VOID)gotoeop(FFRAND, 1);
if (curwp->w_doto != 0) {
/* paragraph ends at end of buffer */
- (VOID) lnewline();
+ (VOID)lnewline();
eopline = lforw(curwp->w_dotp);
} else
eopline = curwp->w_dotp;
/* and back top the begining of the paragraph */
- (VOID) gotobop(FFRAND, 1);
+ (VOID)gotobop(FFRAND, 1);
/* initialize various info */
- while (!inword() && forwchar(FFRAND, 1)) {
- }
+ while (inword() == NULL && forwchar(FFRAND, 1));
+
clength = curwp->w_doto;
wordlen = 0;
@@ -136,8 +150,9 @@ fillpara(f, n)
firstflag = TRUE;
eopflag = FALSE;
while (!eopflag) {
+
/* get the next character in the paragraph */
- if (eolflag = (curwp->w_doto == llength(curwp->w_dotp))) {
+ if ((eolflag = (curwp->w_doto == llength(curwp->w_dotp)))) {
c = ' ';
if (lforw(curwp->w_dotp) == eopline)
eopflag = TRUE;
@@ -162,23 +177,26 @@ fillpara(f, n)
ewprintf("Word too long!");
}
} else if (wordlen) {
+
/* calculate tenatitive new length with word added */
newlength = clength + 1 + wordlen;
+
/*
* if at end of line or at doublespace and previous
* character was one of '.','?','!' doublespace here.
*/
- if ((eolflag || curwp->w_doto == llength(curwp->w_dotp)
- || (c = lgetc(curwp->w_dotp, curwp->w_doto)) == ' '
- || c == '\t')
- && ISEOSP(wbuf[wordlen - 1])
- && wordlen < MAXWORD - 1)
+ if ((eolflag ||
+ curwp->w_doto == llength(curwp->w_dotp) ||
+ (c = lgetc(curwp->w_dotp, curwp->w_doto)) == ' '
+ || c == '\t') && ISEOSP(wbuf[wordlen - 1]) &&
+ wordlen < MAXWORD - 1)
wbuf[wordlen++] = ' ';
+
/* at a word break with a word waiting */
if (newlength <= fillcol) {
/* add word to current line */
if (!firstflag) {
- (VOID) linsert(1, ' ');
+ (VOID)linsert(1, ' ');
++clength;
}
firstflag = FALSE;
@@ -186,77 +204,87 @@ fillpara(f, n)
if (curwp->w_doto > 0 &&
lgetc(curwp->w_dotp, curwp->w_doto - 1) == ' ') {
curwp->w_doto -= 1;
- (VOID) ldelete((RSIZE) 1, KNONE);
+ (VOID)ldelete((RSIZE) 1, KNONE);
}
/* start a new line */
- (VOID) lnewline();
+ (VOID)lnewline();
clength = 0;
}
/* and add the word in in either case */
for (i = 0; i < wordlen; i++) {
- (VOID) linsert(1, wbuf[i]);
+ (VOID)linsert(1, wbuf[i]);
++clength;
}
wordlen = 0;
}
}
/* and add a last newline for the end of our new paragraph */
- (VOID) lnewline();
+ (VOID)lnewline();
+
/*
* we realy should wind up where we started, (which is hard to keep
* track of) but I think the end of the last line is better than the
* begining of the blank line.
*/
- (VOID) backchar(FFRAND, 1);
+ (VOID)backchar(FFRAND, 1);
return TRUE;
}
-/* delete n paragraphs starting with the current one */
+/*
+ * Delete a paragraph. Delete n paragraphs starting with the current one.
+ */
/* ARGSUSED */
+int
killpara(f, n)
+ int f, n;
{
- register int status; /* returned status of functions */
+ int status; /* returned status of functions */
- while (n--) { /* for each paragraph to delete */
+ /* for each paragraph to delete */
+ while (n--) {
/* mark out the end and begining of the para to delete */
- (VOID) gotoeop(FFRAND, 1);
+ (VOID)gotoeop(FFRAND, 1);
/* set the mark here */
curwp->w_markp = curwp->w_dotp;
curwp->w_marko = curwp->w_doto;
/* go to the begining of the paragraph */
- (VOID) gotobop(FFRAND, 1);
- curwp->w_doto = 0; /* force us to the begining of line */
+ (VOID)gotobop(FFRAND, 1);
+
+ /* force us to the beginning of line */
+ curwp->w_doto = 0;
/* and delete it */
if ((status = killregion(FFRAND, 1)) != TRUE)
return status;
/* and clean up the 2 extra lines */
- (VOID) ldelete((RSIZE) 1, KFORW);
+ (VOID)ldelete((RSIZE) 1, KFORW);
}
return TRUE;
}
/*
- * check to see if we're past fillcol, and if so,
- * justify this line. As a last step, justify the line.
+ * Insert char with work wrap. Check to see if we're past fillcol, and if so,
+ * justify this line. As a last step, justify the line.
*/
/* ARGSUSED */
+int
fillword(f, n)
+ int f, n;
{
- register char c;
- register int col, i, nce;
+ char c;
+ int col, i, nce;
for (i = col = 0; col <= fillcol; ++i, ++col) {
if (i == curwp->w_doto)
return selfinsert(f, n);
c = lgetc(curwp->w_dotp, i);
if (c == '\t'
-#ifdef NOTAB
+#ifdef NOTAB
&& !(curbp->b_flag & BFNOTAB)
#endif
)
@@ -265,7 +293,7 @@ fillword(f, n)
++col;
}
if (curwp->w_doto != llength(curwp->w_dotp)) {
- (VOID) selfinsert(f, n);
+ (VOID)selfinsert(f, n);
nce = llength(curwp->w_dotp) - curwp->w_doto;
} else
nce = 0;
@@ -273,18 +301,18 @@ fillword(f, n)
if ((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' ' && c != '\t')
do {
- (VOID) backchar(FFRAND, 1);
+ (VOID)backchar(FFRAND, 1);
} while ((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' '
- && c != '\t' && curwp->w_doto > 0);
+ && c != '\t' && curwp->w_doto > 0);
if (curwp->w_doto == 0)
do {
- (VOID) forwchar(FFRAND, 1);
+ (VOID)forwchar(FFRAND, 1);
} while ((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' '
&& c != '\t' && curwp->w_doto < llength(curwp->w_dotp));
- (VOID) delwhite(FFRAND, 1);
- (VOID) lnewline();
+ (VOID)delwhite(FFRAND, 1);
+ (VOID)lnewline();
i = llength(curwp->w_dotp) - nce;
curwp->w_doto = i > 0 ? i : 0;
curwp->w_flag |= WFMOVE;
@@ -293,11 +321,13 @@ fillword(f, n)
return TRUE;
}
-/* Set fill column to n. */
+/*
+ * Set fill column to n for justify.
+ */
+int
setfillcol(f, n)
+ int f, n;
{
- extern int getcolpos();
-
fillcol = ((f & FFARG) ? n : getcolpos());
ewprintf("Fill column set to %d", fillcol);
return TRUE;