version 1.202, 2012/05/27 18:02:49 |
version 1.208, 2013/08/05 23:36:42 |
|
|
#include "term.h" |
#include "term.h" |
#include "main.h" |
#include "main.h" |
|
|
|
static size_t cond_width(const struct termp *, int, int *); |
static void adjbuf(struct termp *p, int); |
static void adjbuf(struct termp *p, int); |
static void bufferc(struct termp *, char); |
static void bufferc(struct termp *, char); |
static void encode(struct termp *, const char *, size_t); |
static void encode(struct termp *, const char *, size_t); |
|
|
term_flushln(struct termp *p) |
term_flushln(struct termp *p) |
{ |
{ |
int i; /* current input position in p->buf */ |
int i; /* current input position in p->buf */ |
|
int ntab; /* number of tabs to prepend */ |
size_t vis; /* current visual position on output */ |
size_t vis; /* current visual position on output */ |
size_t vbl; /* number of blanks to prepend to output */ |
size_t vbl; /* number of blanks to prepend to output */ |
size_t vend; /* end of word visual position on output */ |
size_t vend; /* end of word visual position on output */ |
Line 143 term_flushln(struct termp *p) |
|
Line 145 term_flushln(struct termp *p) |
|
* Handle literal tab characters: collapse all |
* Handle literal tab characters: collapse all |
* subsequent tabs into a single huge set of spaces. |
* subsequent tabs into a single huge set of spaces. |
*/ |
*/ |
|
ntab = 0; |
while (i < p->col && '\t' == p->buf[i]) { |
while (i < p->col && '\t' == p->buf[i]) { |
vend = (vis / p->tabwidth + 1) * p->tabwidth; |
vend = (vis / p->tabwidth + 1) * p->tabwidth; |
vbl += vend - vis; |
vbl += vend - vis; |
vis = vend; |
vis = vend; |
|
ntab++; |
i++; |
i++; |
} |
} |
|
|
Line 158 term_flushln(struct termp *p) |
|
Line 162 term_flushln(struct termp *p) |
|
*/ |
*/ |
|
|
for (j = i, jhy = 0; j < p->col; j++) { |
for (j = i, jhy = 0; j < p->col; j++) { |
if ((j && ' ' == p->buf[j]) || '\t' == p->buf[j]) |
if (' ' == p->buf[j] || '\t' == p->buf[j]) |
break; |
break; |
|
|
/* Back over the the last printed character. */ |
/* Back over the the last printed character. */ |
Line 191 term_flushln(struct termp *p) |
|
Line 195 term_flushln(struct termp *p) |
|
} else |
} else |
vbl = p->offset; |
vbl = p->offset; |
|
|
|
/* use pending tabs on the new line */ |
|
|
|
if (0 < ntab) |
|
vbl += ntab * p->tabwidth; |
|
|
/* Remove the p->overstep width. */ |
/* Remove the p->overstep width. */ |
|
|
bp += (size_t)p->overstep; |
bp += (size_t)p->overstep; |
Line 264 term_flushln(struct termp *p) |
|
Line 273 term_flushln(struct termp *p) |
|
p->overstep = (int)(vis - maxvis + (*p->width)(p, ' ')); |
p->overstep = (int)(vis - maxvis + (*p->width)(p, ' ')); |
|
|
/* |
/* |
* Behave exactly the same way as groff: |
|
* If we have overstepped the margin, temporarily move |
* If we have overstepped the margin, temporarily move |
* it to the right and flag the rest of the line to be |
* it to the right and flag the rest of the line to be |
* shorter. |
* shorter. |
* If we landed right at the margin, be happy. |
|
* If we are one step before the margin, temporarily |
|
* move it one step LEFT and flag the rest of the line |
|
* to be longer. |
|
*/ |
*/ |
if (p->overstep < -1) |
if (p->overstep < 0) |
p->overstep = 0; |
p->overstep = 0; |
return; |
return; |
|
|
|
|
term_fontpopq(struct termp *p, const void *key) |
term_fontpopq(struct termp *p, const void *key) |
{ |
{ |
|
|
while (p->fonti >= 0 && key != &p->fontq[p->fonti]) |
while (p->fonti >= 0 && key < (void *)(p->fontq + p->fonti)) |
p->fonti--; |
p->fonti--; |
assert(p->fonti >= 0); |
assert(p->fonti >= 0); |
} |
} |
Line 402 term_word(struct termp *p, const char *word) |
|
Line 406 term_word(struct termp *p, const char *word) |
|
|
|
if ( ! (TERMP_NOSPACE & p->flags)) { |
if ( ! (TERMP_NOSPACE & p->flags)) { |
if ( ! (TERMP_KEEP & p->flags)) { |
if ( ! (TERMP_KEEP & p->flags)) { |
if (TERMP_PREKEEP & p->flags) |
|
p->flags |= TERMP_KEEP; |
|
bufferc(p, ' '); |
bufferc(p, ' '); |
if (TERMP_SENTENCE & p->flags) |
if (TERMP_SENTENCE & p->flags) |
bufferc(p, ' '); |
bufferc(p, ' '); |
} else |
} else |
bufferc(p, ASCII_NBRSP); |
bufferc(p, ASCII_NBRSP); |
} |
} |
|
if (TERMP_PREKEEP & p->flags) |
|
p->flags |= TERMP_KEEP; |
|
|
if ( ! (p->flags & TERMP_NONOSPACE)) |
if ( ! (p->flags & TERMP_NONOSPACE)) |
p->flags &= ~TERMP_NOSPACE; |
p->flags &= ~TERMP_NOSPACE; |
Line 419 term_word(struct termp *p, const char *word) |
|
Line 423 term_word(struct termp *p, const char *word) |
|
p->flags &= ~(TERMP_SENTENCE | TERMP_IGNDELIM); |
p->flags &= ~(TERMP_SENTENCE | TERMP_IGNDELIM); |
|
|
while ('\0' != *word) { |
while ('\0' != *word) { |
if ((ssz = strcspn(word, "\\")) > 0) |
if ('\\' != *word) { |
|
if (TERMP_SKIPCHAR & p->flags) { |
|
p->flags &= ~TERMP_SKIPCHAR; |
|
word++; |
|
continue; |
|
} |
|
ssz = strcspn(word, "\\"); |
encode(p, word, ssz); |
encode(p, word, ssz); |
|
word += (int)ssz; |
word += (int)ssz; |
|
if ('\\' != *word) |
|
continue; |
continue; |
|
} |
|
|
word++; |
word++; |
esc = mandoc_escape(&word, &seq, &sz); |
esc = mandoc_escape(&word, &seq, &sz); |
Line 480 term_word(struct termp *p, const char *word) |
|
Line 489 term_word(struct termp *p, const char *word) |
|
term_fontlast(p); |
term_fontlast(p); |
break; |
break; |
case (ESCAPE_NOSPACE): |
case (ESCAPE_NOSPACE): |
if ('\0' == *word) |
if (TERMP_SKIPCHAR & p->flags) |
|
p->flags &= ~TERMP_SKIPCHAR; |
|
else if ('\0' == *word) |
p->flags |= TERMP_NOSPACE; |
p->flags |= TERMP_NOSPACE; |
break; |
break; |
|
case (ESCAPE_SKIPCHAR): |
|
p->flags |= TERMP_SKIPCHAR; |
|
break; |
default: |
default: |
break; |
break; |
} |
} |
Line 522 encode1(struct termp *p, int c) |
|
Line 536 encode1(struct termp *p, int c) |
|
{ |
{ |
enum termfont f; |
enum termfont f; |
|
|
|
if (TERMP_SKIPCHAR & p->flags) { |
|
p->flags &= ~TERMP_SKIPCHAR; |
|
return; |
|
} |
|
|
if (p->col + 4 >= p->maxcols) |
if (p->col + 4 >= p->maxcols) |
adjbuf(p, p->col + 4); |
adjbuf(p, p->col + 4); |
|
|
Line 545 encode(struct termp *p, const char *word, size_t sz) |
|
Line 564 encode(struct termp *p, const char *word, size_t sz) |
|
enum termfont f; |
enum termfont f; |
int i, len; |
int i, len; |
|
|
|
if (TERMP_SKIPCHAR & p->flags) { |
|
p->flags &= ~TERMP_SKIPCHAR; |
|
return; |
|
} |
|
|
/* LINTED */ |
/* LINTED */ |
len = sz; |
len = sz; |
|
|
Line 593 term_len(const struct termp *p, size_t sz) |
|
Line 617 term_len(const struct termp *p, size_t sz) |
|
return((*p->width)(p, ' ') * sz); |
return((*p->width)(p, ' ') * sz); |
} |
} |
|
|
|
static size_t |
|
cond_width(const struct termp *p, int c, int *skip) |
|
{ |
|
|
|
if (*skip) { |
|
(*skip) = 0; |
|
return(0); |
|
} else |
|
return((*p->width)(p, c)); |
|
} |
|
|
size_t |
size_t |
term_strlen(const struct termp *p, const char *cp) |
term_strlen(const struct termp *p, const char *cp) |
{ |
{ |
size_t sz, rsz, i; |
size_t sz, rsz, i; |
int ssz, c; |
int ssz, skip, c; |
const char *seq, *rhs; |
const char *seq, *rhs; |
enum mandoc_esc esc; |
enum mandoc_esc esc; |
static const char rej[] = { '\\', ASCII_HYPH, ASCII_NBRSP, '\0' }; |
static const char rej[] = { '\\', ASCII_HYPH, ASCII_NBRSP, '\0' }; |
Line 610 term_strlen(const struct termp *p, const char *cp) |
|
Line 644 term_strlen(const struct termp *p, const char *cp) |
|
*/ |
*/ |
|
|
sz = 0; |
sz = 0; |
|
skip = 0; |
while ('\0' != *cp) { |
while ('\0' != *cp) { |
rsz = strcspn(cp, rej); |
rsz = strcspn(cp, rej); |
for (i = 0; i < rsz; i++) |
for (i = 0; i < rsz; i++) |
sz += (*p->width)(p, *cp++); |
sz += cond_width(p, *cp++, &skip); |
|
|
c = 0; |
c = 0; |
switch (*cp) { |
switch (*cp) { |
Line 630 term_strlen(const struct termp *p, const char *cp) |
|
Line 665 term_strlen(const struct termp *p, const char *cp) |
|
(seq + 1, ssz - 1); |
(seq + 1, ssz - 1); |
if ('\0' == c) |
if ('\0' == c) |
break; |
break; |
sz += (*p->width)(p, c); |
sz += cond_width(p, c, &skip); |
continue; |
continue; |
case (ESCAPE_SPECIAL): |
case (ESCAPE_SPECIAL): |
c = mchars_spec2cp |
c = mchars_spec2cp |
(p->symtab, seq, ssz); |
(p->symtab, seq, ssz); |
if (c <= 0) |
if (c <= 0) |
break; |
break; |
sz += (*p->width)(p, c); |
sz += cond_width(p, c, &skip); |
continue; |
continue; |
default: |
default: |
break; |
break; |
Line 647 term_strlen(const struct termp *p, const char *cp) |
|
Line 682 term_strlen(const struct termp *p, const char *cp) |
|
|
|
switch (esc) { |
switch (esc) { |
case (ESCAPE_UNICODE): |
case (ESCAPE_UNICODE): |
sz += (*p->width)(p, '?'); |
sz += cond_width(p, '?', &skip); |
break; |
break; |
case (ESCAPE_NUMBERED): |
case (ESCAPE_NUMBERED): |
c = mchars_num2char(seq, ssz); |
c = mchars_num2char(seq, ssz); |
if ('\0' != c) |
if ('\0' != c) |
sz += (*p->width)(p, c); |
sz += cond_width(p, c, &skip); |
break; |
break; |
case (ESCAPE_SPECIAL): |
case (ESCAPE_SPECIAL): |
rhs = mchars_spec2str |
rhs = mchars_spec2str |
Line 664 term_strlen(const struct termp *p, const char *cp) |
|
Line 699 term_strlen(const struct termp *p, const char *cp) |
|
rhs = seq; |
rhs = seq; |
rsz = ssz; |
rsz = ssz; |
break; |
break; |
|
case (ESCAPE_SKIPCHAR): |
|
skip = 1; |
|
break; |
default: |
default: |
break; |
break; |
} |
} |
Line 671 term_strlen(const struct termp *p, const char *cp) |
|
Line 709 term_strlen(const struct termp *p, const char *cp) |
|
if (NULL == rhs) |
if (NULL == rhs) |
break; |
break; |
|
|
|
if (skip) { |
|
skip = 0; |
|
break; |
|
} |
|
|
for (i = 0; i < rsz; i++) |
for (i = 0; i < rsz; i++) |
sz += (*p->width)(p, *rhs++); |
sz += (*p->width)(p, *rhs++); |
break; |
break; |
case (ASCII_NBRSP): |
case (ASCII_NBRSP): |
sz += (*p->width)(p, ' '); |
sz += cond_width(p, ' ', &skip); |
cp++; |
cp++; |
break; |
break; |
case (ASCII_HYPH): |
case (ASCII_HYPH): |
sz += (*p->width)(p, '-'); |
sz += cond_width(p, '-', &skip); |
cp++; |
cp++; |
break; |
break; |
default: |
default: |