#include "config.h"
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/time.h>
#include <bitstring.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../common/common.h"
#include "vi.h"
static SCR *vs_getbg(SCR *, char *);
int
vs_split(SCR *sp, SCR *new, int ccl)
{
GS *gp;
SMAP *smp;
size_t half;
int issmallscreen, splitup;
gp = sp->gp;
if (sp->rows < 4) {
msgq(sp, M_ERR,
"Screen must be larger than %d lines to split", 4 - 1);
return (1);
}
vs_resolve(sp, NULL, 1);
half = sp->rows / 2;
if (ccl && half > 6)
half = 6;
CALLOC(sp, _HMAP(new), SIZE_HMAP(sp), sizeof(SMAP));
if (_HMAP(new) == NULL)
return (1);
_HMAP(new)->lno = sp->lno;
_HMAP(new)->coff = 0;
_HMAP(new)->soff = 1;
issmallscreen = IS_SMALL(sp);
new->cols = sp->cols;
splitup =
!ccl && (vs_sm_cursor(sp, &smp) ? 0 : (smp - HMAP) + 1) >= half;
if (splitup) {
new->rows = sp->rows - half;
new->woff = sp->woff;
sp->rows = half;
sp->woff += new->rows;
TAILQ_INSERT_BEFORE(sp, new, q);
memmove(_HMAP(sp), _HMAP(sp) + new->rows,
(sp->t_maxrows - new->rows) * sizeof(SMAP));
} else {
new->rows = half;
sp->rows -= half;
new->woff = sp->woff + sp->rows;
TAILQ_INSERT_AFTER(&gp->dq, sp, new, q);
}
sp->t_maxrows = IS_ONELINE(sp) ? 1 : sp->rows - 1;
new->t_maxrows = IS_ONELINE(new) ? 1 : new->rows - 1;
if (issmallscreen) {
if (splitup)
sp->t_rows -= new->rows;
if (sp->t_rows > sp->t_maxrows)
sp->t_rows = sp->t_maxrows;
if (sp->t_minrows > sp->t_maxrows)
sp->t_minrows = sp->t_maxrows;
new->t_minrows = new->t_rows = O_VAL(sp, O_WINDOW);
if (new->t_rows > new->t_maxrows)
new->t_rows = new->t_maxrows;
if (new->t_minrows > new->t_maxrows)
new->t_minrows = new->t_maxrows;
} else {
sp->t_minrows = sp->t_rows = IS_ONELINE(sp) ? 1 : sp->rows - 1;
new->t_minrows = new->t_rows = O_VAL(sp, O_WINDOW);
if (new->t_rows > new->rows - 1)
new->t_minrows = new->t_rows =
IS_ONELINE(new) ? 1 : new->rows - 1;
}
_TMAP(sp) = IS_ONELINE(sp) ?
_HMAP(sp) : _HMAP(sp) + (sp->t_rows - 1);
_TMAP(new) = IS_ONELINE(new) ?
_HMAP(new) : _HMAP(new) + (new->t_rows - 1);
if ((sp->defscroll = sp->t_maxrows / 2) == 0)
sp->defscroll = 1;
if ((new->defscroll = new->t_maxrows / 2) == 0)
new->defscroll = 1;
F_SET(new,
SC_SCR_REFORMAT | SC_STATUS |
F_ISSET(sp, SC_EX | SC_VI | SC_SCR_VI | SC_SCR_EX));
return (0);
}
int
vs_discard(SCR *sp, SCR **spp)
{
SCR *nsp;
dir_t dir;
if (sp->frp != NULL) {
sp->frp->lno = sp->lno;
sp->frp->cno = sp->cno;
F_SET(sp->frp, FR_CURSORSET);
}
if ((nsp = TAILQ_PREV(sp, _dqh, q))) {
nsp->rows += sp->rows;
sp = nsp;
dir = FORWARD;
} else if ((nsp = TAILQ_NEXT(sp, q))) {
nsp->woff = sp->woff;
nsp->rows += sp->rows;
sp = nsp;
dir = BACKWARD;
} else {
sp = NULL;
dir = 0;
}
if (spp != NULL)
*spp = sp;
if (sp == NULL)
return (0);
if (!IS_SMALL(sp))
sp->t_rows = sp->t_minrows = sp->rows - 1;
sp->t_maxrows = sp->rows - 1;
sp->defscroll = sp->t_maxrows / 2;
*(HMAP + (sp->t_rows - 1)) = *TMAP;
TMAP = HMAP + (sp->t_rows - 1);
switch (dir) {
case FORWARD:
vs_sm_fill(sp, OOBLNO, P_TOP);
break;
case BACKWARD:
vs_sm_fill(sp, OOBLNO, P_BOTTOM);
break;
default:
abort();
}
F_SET(sp, SC_STATUS);
return (0);
}
int
vs_fg(SCR *sp, SCR **nspp, CHAR_T *name, int newscreen)
{
GS *gp;
SCR *nsp;
gp = sp->gp;
if (newscreen)
nsp = vs_getbg(sp, name);
else
if (vs_swap(sp, &nsp, name))
return (1);
if ((*nspp = nsp) == NULL) {
msgq_str(sp, M_ERR, name,
name == NULL ?
"There are no background screens" :
"There's no background screen editing a file named %s");
return (1);
}
if (newscreen) {
TAILQ_REMOVE(&gp->hq, nsp, q);
if (vs_split(sp, nsp, 0)) {
TAILQ_INSERT_TAIL(&gp->hq, nsp, q);
return (1);
}
} else {
TAILQ_REMOVE(&gp->dq, sp, q);
TAILQ_INSERT_TAIL(&gp->hq, sp, q);
}
return (0);
}
int
vs_bg(SCR *sp)
{
GS *gp;
SCR *nsp;
gp = sp->gp;
if (vs_discard(sp, &nsp))
return (1);
if (nsp == NULL) {
msgq(sp, M_ERR,
"You may not background your only displayed screen");
return (1);
}
TAILQ_REMOVE(&gp->dq, sp, q);
TAILQ_INSERT_TAIL(&gp->hq, sp, q);
free(_HMAP(sp));
_HMAP(sp) = NULL;
sp->nextdisp = nsp;
F_SET(sp, SC_SSWITCH);
return (0);
}
int
vs_swap(SCR *sp, SCR **nspp, char *name)
{
GS *gp;
SCR *nsp;
gp = sp->gp;
if ((*nspp = nsp = vs_getbg(sp, name)) == NULL)
return (0);
if (sp->frp != NULL) {
sp->frp->lno = sp->lno;
sp->frp->cno = sp->cno;
F_SET(sp->frp, FR_CURSORSET);
}
sp->nextdisp = nsp;
F_SET(sp, SC_SSWITCH);
VIP(nsp)->srows = VIP(sp)->srows;
nsp->cols = sp->cols;
nsp->rows = sp->rows;
nsp->woff = sp->woff;
if (IS_SMALL(nsp)) {
nsp->t_minrows = nsp->t_rows = O_VAL(nsp, O_WINDOW);
if (nsp->t_rows > sp->t_maxrows)
nsp->t_rows = nsp->t_maxrows;
if (nsp->t_minrows > sp->t_maxrows)
nsp->t_minrows = nsp->t_maxrows;
} else
nsp->t_rows = nsp->t_maxrows = nsp->t_minrows = nsp->rows - 1;
nsp->defscroll = nsp->t_maxrows / 2;
CALLOC_RET(nsp, _HMAP(nsp), SIZE_HMAP(nsp), sizeof(SMAP));
_TMAP(nsp) = _HMAP(nsp) + (nsp->t_rows - 1);
if (vs_sm_fill(nsp, nsp->lno, P_FILL))
return (1);
TAILQ_REMOVE(&gp->hq, nsp, q);
TAILQ_INSERT_AFTER(&gp->dq, sp, nsp, q);
F_SET(VIP(nsp), VIP_CUR_INVALID);
F_SET(nsp, SC_SCR_REDRAW | SC_STATUS);
return (0);
}
int
vs_resize(SCR *sp, long count, adj_t adj)
{
GS *gp;
SCR *g, *s;
size_t g_off, s_off;
gp = sp->gp;
if (count == 0)
return (0);
if (adj == A_SET) {
if (sp->t_maxrows == count)
return (0);
if (sp->t_maxrows > count) {
adj = A_DECREASE;
count = sp->t_maxrows - count;
} else {
adj = A_INCREASE;
count = count - sp->t_maxrows;
}
}
g_off = s_off = 0;
if (adj == A_DECREASE) {
if (count < 0)
count = -count;
s = sp;
if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count)
goto toosmall;
if ((g = TAILQ_PREV(sp, _dqh, q)) == NULL) {
if ((g = TAILQ_NEXT(sp, q)) == NULL)
goto toobig;
g_off = -count;
} else
s_off = count;
} else {
g = sp;
if ((s = TAILQ_NEXT(sp, q)))
if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count)
s = NULL;
else
s_off = count;
else
s = NULL;
if (s == NULL) {
if ((s = TAILQ_PREV(sp, _dqh, q)) == NULL) {
toobig: msgq(sp, M_BERR, adj == A_DECREASE ?
"The screen cannot shrink" :
"The screen cannot grow");
return (1);
}
if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count) {
toosmall: msgq(sp, M_BERR,
"The screen can only shrink to %d rows",
MINIMUM_SCREEN_ROWS);
return (1);
}
g_off = -count;
}
}
s->rows += -count;
s->woff += s_off;
g->rows += count;
g->woff += g_off;
g->t_rows += count;
if (g->t_minrows == g->t_maxrows)
g->t_minrows += count;
g->t_maxrows += count;
_TMAP(g) += count;
F_SET(g, SC_SCR_REFORMAT | SC_STATUS);
s->t_rows -= count;
s->t_maxrows -= count;
if (s->t_minrows > s->t_maxrows)
s->t_minrows = s->t_maxrows;
_TMAP(s) -= count;
F_SET(s, SC_SCR_REFORMAT | SC_STATUS);
return (0);
}
static SCR *
vs_getbg(SCR *sp, char *name)
{
GS *gp;
SCR *nsp;
char *p;
gp = sp->gp;
if (name == NULL)
return (TAILQ_FIRST(&gp->hq));
TAILQ_FOREACH(nsp, &gp->hq, q) {
if (!strcmp(nsp->frp->name, name))
return(nsp);
}
TAILQ_FOREACH(nsp, &gp->hq, q) {
if ((p = strrchr(nsp->frp->name, '/')) == NULL)
p = nsp->frp->name;
else
++p;
if (!strcmp(p, name))
return(nsp);
}
return (NULL);
}