- /*
- * This is a little bit complicated. Here we determine the
- * header the corresponds to a cell. We add headers dynamically
- * when need be or re-use them, otherwise. As an example, given
- * the following:
- *
- * 1 c || l
- * 2 | c | l
- * 3 l l
- * 3 || c | l |.
- *
- * We first add the new headers (as there are none) in (1); then
- * in (2) we insert the first spanner (as it doesn't match up
- * with the header); then we re-use the prior data headers,
- * skipping over the spanners; then we re-use everything and add
- * a last spanner. Note that VERT headers are made into DVERT
- * ones.
- */
-
- h = pp ? pp->head->next : tbl->first_head;
-
- if (h) {
- /* Re-use data header. */
- if (TBL_HEAD_DATA == h->pos &&
- (TBL_CELL_VERT != p->pos &&
- TBL_CELL_DVERT != p->pos)) {
- p->head = h;
- return(p);
- }
-
- /* Re-use spanner header. */
- if (TBL_HEAD_DATA != h->pos &&
- (TBL_CELL_VERT == p->pos ||
- TBL_CELL_DVERT == p->pos)) {
- head_adjust(p, h);
- p->head = h;
- return(p);
- }
-
- /* Right-shift headers with a new spanner. */
- if (TBL_HEAD_DATA == h->pos &&
- (TBL_CELL_VERT == p->pos ||
- TBL_CELL_DVERT == p->pos)) {
- hp = mandoc_calloc(1, sizeof(struct tbl_head));
- hp->ident = tbl->opts.cols++;
- hp->prev = h->prev;
- if (h->prev)
- h->prev->next = hp;
- if (h == tbl->first_head)
- tbl->first_head = hp;
- h->prev = hp;
- hp->next = h;
- head_adjust(p, hp);
- p->head = hp;
- return(p);
- }
-
- if (NULL != (h = h->next)) {
- head_adjust(p, h);
- p->head = h;
- return(p);
- }
-
- /* Fall through to default case... */
- }
-
- hp = mandoc_calloc(1, sizeof(struct tbl_head));
- hp->ident = tbl->opts.cols++;
-
- if (tbl->last_head) {
- hp->prev = tbl->last_head;
- tbl->last_head->next = hp;
- tbl->last_head = hp;