ref: 15c91dac1995fd5e93ed379b80d0ff7a7fd9fc72
parent: e8ccdf0b751754aae72e4765e556e9a1760443a9
author: Ali Gholami Rudi <ali@rudi.ir>
date: Tue Feb 19 14:08:04 EST 2013
adj: simplify line break handling after blank or indented lines
--- a/adj.c
+++ b/adj.c
@@ -8,28 +8,26 @@
int beg; /* word beginning offset */
int end; /* word ending offset */
int wid; /* word width */
- int blanks; /* blanks before word */
+ int gap; /* the space before this word */
};
struct adj {
char buf[LNLEN]; /* line buffer */
int len;
- struct word words[NWORDS]; /* words in buf */
+ struct word words[NWORDS]; /* words in buf */
int nwords;
- int wid; /* total width of buffer */
+ int wid; /* total width of buf */
struct word *word; /* current word */
int swid; /* current space width */
- int spaces; /* spaces before the next word */
- int nl_cnt; /* newlines before the next word */
- int nl_pre; /* previous newlines; for "\n\n" */
- int nl_ins; /* indented line after newline */
+ int gap; /* space before the next word */
+ int nls; /* newlines before the next word */
};
/* does the adjustment buffer need to be flushed without filling? */
static int adj_fullnf(struct adj *a)
{
- return a->nl_ins || a->nl_pre + a->nl_cnt > 1 ||
- (a->nl_cnt > 0 && !a->nwords);
+ /* blank lines; indented lines; newlines when buffer is empty */
+ return a->nls > 1 || (a->nls && a->gap) || (a->nls && !a->nwords);
}
/* does the adjustment buffer need to be flushed? */
@@ -36,7 +34,7 @@
int adj_full(struct adj *a, int mode, int linelen)
{
if (mode == ADJ_N)
- return a->nl_cnt;
+ return a->nls;
if (adj_fullnf(a))
return 1;
return !a->word && a->wid > linelen;
@@ -45,7 +43,7 @@
/* is the adjustment buffer empty? */
int adj_empty(struct adj *a, int mode)
{
- return mode == ADJ_N ? !a->nl_cnt : !a->nwords && !adj_fullnf(a);
+ return mode == ADJ_N ? !a->nls : !a->nwords && !adj_fullnf(a);
}
/* set space width */
@@ -54,7 +52,7 @@
adj->swid = swid;
}
-/* move n words from the adjustment buffer to the s buffer */
+/* move n words from the adjustment buffer to s */
static void adj_move(struct adj *a, int n, char *s)
{
struct word *cur;
@@ -63,10 +61,10 @@
int i;
for (i = 0; i < n; i++) {
cur = &a->words[i];
- s += sprintf(s, "\\h'%du'", cur->blanks);
+ s += sprintf(s, "\\h'%du'", cur->gap);
memcpy(s, a->buf + cur->beg, cur->end - cur->beg);
s += cur->end - cur->beg;
- w += cur->wid + cur->blanks;
+ w += cur->wid + cur->gap;
}
*s = '\0';
if (!n)
@@ -90,16 +88,14 @@
int w = 0;
int i, n;
if (mode == ADJ_N || adj_fullnf(a)) {
- a->nl_pre = 1;
- a->nl_cnt--;
- a->nl_ins = 0;
+ a->nls--;
adj_move(a, a->nwords, s);
return;
}
for (n = 0; n < a->nwords; n++) {
- if (n && w + a->words[n].wid + a->words[n].blanks > ll)
+ if (n && w + a->words[n].wid + a->words[n].gap > ll)
break;
- w += a->words[n].wid + a->words[n].blanks;
+ w += a->words[n].wid + a->words[n].gap;
}
if (mode == ADJ_B && n > 1 && n < a->nwords) {
adj_div = (ll - w) / (n - 1);
@@ -106,21 +102,21 @@
adj_rem = ll - w - adj_div * (n - 1);
a->wid += ll - w;
for (i = 0; i < n - 1; i++)
- a->words[i + 1].blanks += adj_div + (i < adj_rem);
+ a->words[i + 1].gap += adj_div + (i < adj_rem);
}
adj_move(a, n, s);
if (a->nwords)
- a->wid -= a->words[0].blanks;
- a->words[0].blanks = 0;
+ a->wid -= a->words[0].gap;
+ a->words[0].gap = 0;
}
-static void adj_wordbeg(struct adj *adj, int blanks)
+static void adj_wordbeg(struct adj *adj, int gap)
{
adj->word = &adj->words[adj->nwords++];
adj->word->beg = adj->len;
adj->word->wid = 0;
- adj->word->blanks = blanks;
- adj->wid += blanks;
+ adj->word->gap = gap;
+ adj->wid += gap;
}
static void adj_wordend(struct adj *adj)
@@ -136,28 +132,23 @@
va_list ap;
if (!strcmp(s, " ")) {
adj_wordend(adj);
- adj->spaces += wid;
+ adj->gap += wid;
adj->swid = wid;
- if (adj->nl_cnt)
- adj->nl_ins = 1;
return;
}
if (!strcmp(s, "\n")) {
adj_wordend(adj);
- adj->nl_cnt++;
- adj->spaces = 0;
- adj->nl_ins = 0;
+ adj->nls++;
+ adj->gap = 0;
adj->swid = wid;
return;
}
if (!adj->word) {
- if (adj->nl_cnt && !adj->spaces && adj->nwords >= 1)
- adj->spaces = adj->swid;
- adj_wordbeg(adj, adj->spaces);
- adj->nl_cnt = 0;
- adj->spaces = 0;
- adj->nl_ins = 0;
- adj->nl_pre = 0;
+ if (adj->nls && !adj->gap && adj->nwords >= 1)
+ adj->gap = adj->swid;
+ adj_wordbeg(adj, adj->gap);
+ adj->nls = 0;
+ adj->gap = 0;
}
va_start(ap, s);
adj->len += vsprintf(adj->buf + adj->len, s, ap);