xref: /minix/external/bsd/nvi/dist/vi/vs_split.c (revision 84d9c625)
1 /*	$NetBSD: vs_split.c,v 1.6 2013/12/01 02:34:54 christos Exp $ */
2 /*-
3  * Copyright (c) 1993, 1994
4  *	The Regents of the University of California.  All rights reserved.
5  * Copyright (c) 1993, 1994, 1995, 1996
6  *	Keith Bostic.  All rights reserved.
7  *
8  * See the LICENSE file for redistribution information.
9  */
10 
11 #include "config.h"
12 
13 #ifndef lint
14 static const char sccsid[] = "Id: vs_split.c,v 10.42 2001/06/25 15:19:38 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:38 ";
15 #endif /* not lint */
16 
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/time.h>
20 
21 #include <bitstring.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "../common/common.h"
29 #include "vi.h"
30 
31 typedef enum { HORIZ_FOLLOW, HORIZ_PRECEDE, VERT_FOLLOW, VERT_PRECEDE } jdir_t;
32 
33 static SCR	*vs_getbg __P((SCR *, const char *));
34 static void      vs_insert __P((SCR *sp, WIN *wp));
35 static int	 vs_join __P((SCR *, SCR **, jdir_t *));
36 
37 /*
38  * vs_split --
39  *	Create a new screen, horizontally.
40  *
41  * PUBLIC: int vs_split __P((SCR *, SCR *, int));
42  */
43 int
44 vs_split(SCR *sp, SCR *new, int ccl)
45 
46 	        		/* Colon-command line split. */
47 {
48 	GS *gp;
49 	SMAP *smp;
50 	size_t half;
51 	int issmallscreen, splitup;
52 
53 	gp = sp->gp;
54 
55 	/* Check to see if it's possible. */
56 	/* XXX: The IS_ONELINE fix will change this, too. */
57 	if (sp->rows < 4) {
58 		msgq(sp, M_ERR,
59 		    "222|Screen must be larger than %d lines to split", 4 - 1);
60 		return (1);
61 	}
62 
63 	/* Wait for any messages in the screen. */
64 	vs_resolve(sp, NULL, 1);
65 
66 	/* Get a new screen map. */
67 	CALLOC(sp, _HMAP(new), SMAP *, SIZE_HMAP(sp), sizeof(SMAP));
68 	if (_HMAP(new) == NULL)
69 		return (1);
70 	_HMAP(new)->lno = sp->lno;
71 	_HMAP(new)->coff = 0;
72 	_HMAP(new)->soff = 1;
73 
74 	/* Split the screen in half. */
75 	half = sp->rows / 2;
76 	if (ccl && half > 6)
77 		half = 6;
78 
79 	/*
80 	 * Small screens: see vs_refresh.c section 6a.  Set a flag so
81 	 * we know to fix the screen up later.
82 	 */
83 	issmallscreen = IS_SMALL(sp);
84 
85 	/* The columns in the screen don't change. */
86 	new->coff = sp->coff;
87 	new->cols = sp->cols;
88 
89 	/*
90 	 * Split the screen, and link the screens together.  If creating a
91 	 * screen to edit the colon command line or the cursor is in the top
92 	 * half of the current screen, the new screen goes under the current
93 	 * screen.  Else, it goes above the current screen.
94 	 *
95 	 * Recalculate current cursor position based on sp->lno, we're called
96 	 * with the cursor on the colon command line.  Then split the screen
97 	 * in half and update the shared information.
98 	 */
99 	splitup =
100 	    !ccl && (vs_sm_cursor(sp, &smp) ? 0 : (size_t)(smp - HMAP) + 1) >= half;
101 	if (splitup) {				/* Old is bottom half. */
102 		new->rows = sp->rows - half;	/* New. */
103 		new->roff = sp->roff;
104 		sp->rows = half;		/* Old. */
105 		sp->roff += new->rows;
106 
107 		/*
108 		 * If the parent is the bottom half of the screen, shift
109 		 * the map down to match on-screen text.
110 		 */
111 		memcpy(_HMAP(sp), _HMAP(sp) + new->rows,
112 		    (sp->t_maxrows - new->rows) * sizeof(SMAP));
113 	} else {				/* Old is top half. */
114 		new->rows = half;		/* New. */
115 		sp->rows -= half;		/* Old. */
116 		new->roff = sp->roff + sp->rows;
117 	}
118 
119 	/* Adjust maximum text count. */
120 	sp->t_maxrows = IS_ONELINE(sp) ? 1 : sp->rows - 1;
121 	new->t_maxrows = IS_ONELINE(new) ? 1 : new->rows - 1;
122 
123 	/*
124 	 * Small screens: see vs_refresh.c, section 6a.
125 	 *
126 	 * The child may have different screen options sizes than the parent,
127 	 * so use them.  Guarantee that text counts aren't larger than the
128 	 * new screen sizes.
129 	 */
130 	if (issmallscreen) {
131 		/* Fix the text line count for the parent. */
132 		if (splitup)
133 			sp->t_rows -= new->rows;
134 
135 		/* Fix the parent screen. */
136 		if (sp->t_rows > sp->t_maxrows)
137 			sp->t_rows = sp->t_maxrows;
138 		if (sp->t_minrows > sp->t_maxrows)
139 			sp->t_minrows = sp->t_maxrows;
140 
141 		/* Fix the child screen. */
142 		new->t_minrows = new->t_rows = O_VAL(sp, O_WINDOW);
143 		if (new->t_rows > new->t_maxrows)
144 			new->t_rows = new->t_maxrows;
145 		if (new->t_minrows > new->t_maxrows)
146 			new->t_minrows = new->t_maxrows;
147 	} else {
148 		sp->t_minrows = sp->t_rows = IS_ONELINE(sp) ? 1 : sp->rows - 1;
149 
150 		/*
151 		 * The new screen may be a small screen, even if the parent
152 		 * was not.  Don't complain if O_WINDOW is too large, we're
153 		 * splitting the screen so the screen is much smaller than
154 		 * normal.
155 		 */
156 		new->t_minrows = new->t_rows = O_VAL(sp, O_WINDOW);
157 		if (new->t_rows > new->rows - 1)
158 			new->t_minrows = new->t_rows =
159 			    IS_ONELINE(new) ? 1 : new->rows - 1;
160 	}
161 
162 	/* Adjust the ends of the new and old maps. */
163 	_TMAP(sp) = IS_ONELINE(sp) ?
164 	    _HMAP(sp) : _HMAP(sp) + (sp->t_rows - 1);
165 	_TMAP(new) = IS_ONELINE(new) ?
166 	    _HMAP(new) : _HMAP(new) + (new->t_rows - 1);
167 
168 	/* Reset the length of the default scroll. */
169 	if ((sp->defscroll = sp->t_maxrows / 2) == 0)
170 		sp->defscroll = 1;
171 	if ((new->defscroll = new->t_maxrows / 2) == 0)
172 		new->defscroll = 1;
173 
174 	/* Fit the screen into the logical chain. */
175 	vs_insert(new, sp->wp);
176 
177 	/* Tell the display that we're splitting. */
178 	(void)gp->scr_split(sp, new);
179 
180 	/*
181 	 * Initialize the screen flags:
182 	 *
183 	 * If we're in vi mode in one screen, we don't have to reinitialize.
184 	 * This isn't just a cosmetic fix.  The path goes like this:
185 	 *
186 	 *	return into vi(), SC_SSWITCH set
187 	 *	call vs_refresh() with SC_STATUS set
188 	 *	call vs_resolve to display the status message
189 	 *	call vs_refresh() because the SC_SCR_VI bit isn't set
190 	 *
191 	 * Things go downhill at this point.
192 	 *
193 	 * Draw the new screen from scratch, and add a status line.
194 	 */
195 	F_SET(new,
196 	    SC_SCR_REFORMAT | SC_STATUS |
197 	    F_ISSET(sp, SC_EX | SC_VI | SC_SCR_VI | SC_SCR_EX));
198 	return (0);
199 }
200 
201 /*
202  * vs_vsplit --
203  *	Create a new screen, vertically.
204  *
205  * PUBLIC: int vs_vsplit __P((SCR *, SCR *));
206  */
207 int
208 vs_vsplit(SCR *sp, SCR *new)
209 {
210 	GS *gp;
211 	size_t cols;
212 
213 	gp = sp->gp;
214 
215 	/* Check to see if it's possible. */
216 	if (sp->cols / 2 <= MINIMUM_SCREEN_COLS) {
217 		msgq(sp, M_ERR,
218 		    "288|Screen must be larger than %d columns to split",
219 		    MINIMUM_SCREEN_COLS * 2);
220 		return (1);
221 	}
222 
223 	/* Wait for any messages in the screen. */
224 	vs_resolve(sp, NULL, 1);
225 
226 	/* Get a new screen map. */
227 	CALLOC(sp, _HMAP(new), SMAP *, SIZE_HMAP(sp), sizeof(SMAP));
228 	if (_HMAP(new) == NULL)
229 		return (1);
230 	_HMAP(new)->lno = sp->lno;
231 	_HMAP(new)->coff = 0;
232 	_HMAP(new)->soff = 1;
233 
234 	/*
235 	 * Split the screen in half; we have to sacrifice a column to delimit
236 	 * the screens.
237 	 *
238 	 * XXX
239 	 * We always split to the right... that makes more sense to me, and
240 	 * I don't want to play the stupid games that I play when splitting
241 	 * horizontally.
242 	 *
243 	 * XXX
244 	 * We reserve a column for the screen, "knowing" that curses needs
245 	 * one.  This should be worked out with the display interface.
246 	 */
247 	cols = sp->cols / 2;
248 	new->cols = sp->cols - cols - 1;
249 	sp->cols = cols;
250 	new->coff = sp->coff + cols + 1;
251 	sp->cno = 0;
252 
253 	/* Nothing else changes. */
254 	new->rows = sp->rows;
255 	new->t_rows = sp->t_rows;
256 	new->t_maxrows = sp->t_maxrows;
257 	new->t_minrows = sp->t_minrows;
258 	new->roff = sp->roff;
259 	new->defscroll = sp->defscroll;
260 	_TMAP(new) = _HMAP(new) + (new->t_rows - 1);
261 
262 	/* Fit the screen into the logical chain. */
263 	vs_insert(new, sp->wp);
264 
265 	/* Tell the display that we're splitting. */
266 	(void)gp->scr_split(sp, new);
267 
268 	/* Redraw the old screen from scratch. */
269 	F_SET(sp, SC_SCR_REFORMAT | SC_STATUS);
270 
271 	/*
272 	 * Initialize the screen flags:
273 	 *
274 	 * If we're in vi mode in one screen, we don't have to reinitialize.
275 	 * This isn't just a cosmetic fix.  The path goes like this:
276 	 *
277 	 *	return into vi(), SC_SSWITCH set
278 	 *	call vs_refresh() with SC_STATUS set
279 	 *	call vs_resolve to display the status message
280 	 *	call vs_refresh() because the SC_SCR_VI bit isn't set
281 	 *
282 	 * Things go downhill at this point.
283 	 *
284 	 * Draw the new screen from scratch, and add a status line.
285 	 */
286 	F_SET(new,
287 	    SC_SCR_REFORMAT | SC_STATUS |
288 	    F_ISSET(sp, SC_EX | SC_VI | SC_SCR_VI | SC_SCR_EX));
289 	return (0);
290 }
291 
292 /*
293  * vs_insert --
294  *	Insert the new screen into the correct place in the logical
295  *	chain.
296  */
297 static void
298 vs_insert(SCR *sp, WIN *wp)
299 {
300 	SCR *tsp;
301 
302 	sp->wp = wp;
303 
304 	/* Move past all screens with lower row numbers. */
305 	TAILQ_FOREACH(tsp, &wp->scrq, q)
306 		if (tsp->roff >= sp->roff)
307 			break;
308 	/*
309 	 * Move past all screens with the same row number and lower
310 	 * column numbers.
311 	 */
312 	for (; tsp != NULL; tsp = TAILQ_NEXT(tsp, q))
313 		if (tsp->roff != sp->roff || tsp->coff > sp->coff)
314 			break;
315 
316 	/*
317 	 * If we reached the end, this screen goes there.  Otherwise,
318 	 * put it before or after the screen where we stopped.
319 	 */
320 	if (tsp == NULL) {
321 		TAILQ_INSERT_TAIL(&wp->scrq, sp, q);
322 	} else if (tsp->roff < sp->roff ||
323 	    (tsp->roff == sp->roff && tsp->coff < sp->coff)) {
324 		TAILQ_INSERT_AFTER(&wp->scrq, tsp, sp, q);
325 	} else
326 		TAILQ_INSERT_BEFORE(tsp, sp, q);
327 }
328 
329 /*
330  * vs_discard --
331  *	Discard the screen, folding the real-estate into a related screen,
332  *	if one exists, and return that screen.
333  *
334  * PUBLIC: int vs_discard __P((SCR *, SCR **));
335  */
336 int
337 vs_discard(SCR *sp, SCR **spp)
338 {
339 	GS *gp;
340 	SCR *tsp, **lp, *list[100];
341 	jdir_t jdir;
342 
343 	gp = sp->gp;
344 
345 	/*
346 	 * Save the old screen's cursor information.
347 	 *
348 	 * XXX
349 	 * If called after file_end(), and the underlying file was a tmp
350 	 * file, it may have gone away.
351 	 */
352 	if (sp->frp != NULL) {
353 		sp->frp->lno = sp->lno;
354 		sp->frp->cno = sp->cno;
355 		F_SET(sp->frp, FR_CURSORSET);
356 	}
357 
358 	/* If no other screens to join, we're done. */
359 	if (!IS_SPLIT(sp)) {
360 		(void)gp->scr_discard(sp, NULL);
361 
362 		if (spp != NULL)
363 			*spp = NULL;
364 		return (0);
365 	}
366 
367 	/*
368 	 * Find a set of screens that cover one of the screen's borders.
369 	 * Check the vertical axis first, for no particular reason.
370 	 *
371 	 * XXX
372 	 * It's possible (I think?), to create a screen that shares no full
373 	 * border with any other set of screens, so we can't discard it.  We
374 	 * just complain at the user until they clean it up.
375 	 */
376 	if (vs_join(sp, list, &jdir))
377 		return (1);
378 
379 	/*
380 	 * Modify the affected screens.  Redraw the modified screen(s) from
381 	 * scratch, setting a status line.  If this is ever a performance
382 	 * problem we could play games with the map, but I wrote that code
383 	 * before and it was never clean or easy.
384 	 *
385 	 * Don't clean up the discarded screen's information.  If the screen
386 	 * isn't exiting, we'll do the work when the user redisplays it.
387 	 */
388 	switch (jdir) {
389 	case HORIZ_FOLLOW:
390 	case HORIZ_PRECEDE:
391 		for (lp = &list[0]; (tsp = *lp) != NULL; ++lp) {
392 			/*
393 			 * Small screens: see vs_refresh.c section 6a.  Adjust
394 			 * text line info, unless it's a small screen.
395 			 *
396 			 * Reset the length of the default scroll.
397 			 *
398 			 * Reset the map references.
399 			 */
400 			tsp->rows += sp->rows;
401 			if (!IS_SMALL(tsp))
402 				tsp->t_rows = tsp->t_minrows = tsp->rows - 1;
403 			tsp->t_maxrows = tsp->rows - 1;
404 
405 			tsp->defscroll = tsp->t_maxrows / 2;
406 
407 			*(_HMAP(tsp) + (tsp->t_rows - 1)) = *_TMAP(tsp);
408 			_TMAP(tsp) = _HMAP(tsp) + (tsp->t_rows - 1);
409 
410 			switch (jdir) {
411 			case HORIZ_FOLLOW:
412 				tsp->roff = sp->roff;
413 				vs_sm_fill(tsp, OOBLNO, P_TOP);
414 				break;
415 			case HORIZ_PRECEDE:
416 				vs_sm_fill(tsp, OOBLNO, P_BOTTOM);
417 				break;
418 			default:
419 				abort();
420 			}
421 			F_SET(tsp, SC_STATUS);
422 		}
423 		break;
424 	case VERT_FOLLOW:
425 	case VERT_PRECEDE:
426 		for (lp = &list[0]; (tsp = *lp) != NULL; ++lp) {
427 			if (jdir == VERT_FOLLOW)
428 				tsp->coff = sp->coff;
429 			tsp->cols += sp->cols + 1;	/* XXX: DIVIDER */
430 			vs_sm_fill(tsp, OOBLNO, P_TOP);
431 			F_SET(tsp, SC_STATUS);
432 		}
433 		break;
434 	default:
435 		abort();
436 	}
437 
438 	/* Find the closest screen that changed and move to it. */
439 	tsp = list[0];
440 	if (spp != NULL)
441 		*spp = tsp;
442 
443 	/* Tell the display that we're discarding a screen. */
444 	(void)gp->scr_discard(sp, list);
445 
446 	return (0);
447 }
448 
449 /*
450  * vs_join --
451  *	Find a set of screens that covers a screen's border.
452  */
453 static int
454 vs_join(SCR *sp, SCR **listp, jdir_t *jdirp)
455 {
456 	WIN *wp;
457 	SCR **lp, *tsp;
458 	int first;
459 	size_t tlen;
460 
461 	wp = sp->wp;
462 
463 	/* Check preceding vertical. */
464 	lp = listp;
465 	tlen = sp->rows;
466 	TAILQ_FOREACH(tsp, &wp->scrq, q) {
467 		if (sp == tsp)
468 			continue;
469 		/* Test if precedes the screen vertically. */
470 		if (tsp->coff + tsp->cols + 1 != sp->coff)
471 			continue;
472 		/*
473 		 * Test if a subset on the vertical axis.  If overlaps the
474 		 * beginning or end, we can't join on this axis at all.
475 		 */
476 		if (tsp->roff > sp->roff + sp->rows)
477 			continue;
478 		if (tsp->roff < sp->roff) {
479 			if (tsp->roff + tsp->rows >= sp->roff)
480 				break;
481 			continue;
482 		}
483 		if (tsp->roff + tsp->rows > sp->roff + sp->rows)
484 			break;
485 #ifdef DEBUG
486 		if (tlen < tsp->rows)
487 			abort();
488 #endif
489 		tlen -= tsp->rows;
490 		*lp++ = tsp;
491 	}
492 	if (tlen == 0) {
493 		*lp = NULL;
494 		*jdirp = VERT_PRECEDE;
495 		return (0);
496 	}
497 
498 	/* Check following vertical. */
499 	lp = listp;
500 	tlen = sp->rows;
501 	TAILQ_FOREACH(tsp, &wp->scrq, q) {
502 		if (sp == tsp)
503 			continue;
504 		/* Test if follows the screen vertically. */
505 		if (tsp->coff != sp->coff + sp->cols + 1)
506 			continue;
507 		/*
508 		 * Test if a subset on the vertical axis.  If overlaps the
509 		 * beginning or end, we can't join on this axis at all.
510 		 */
511 		if (tsp->roff > sp->roff + sp->rows)
512 			continue;
513 		if (tsp->roff < sp->roff) {
514 			if (tsp->roff + tsp->rows >= sp->roff)
515 				break;
516 			continue;
517 		}
518 		if (tsp->roff + tsp->rows > sp->roff + sp->rows)
519 			break;
520 #ifdef DEBUG
521 		if (tlen < tsp->rows)
522 			abort();
523 #endif
524 		tlen -= tsp->rows;
525 		*lp++ = tsp;
526 	}
527 	if (tlen == 0) {
528 		*lp = NULL;
529 		*jdirp = VERT_FOLLOW;
530 		return (0);
531 	}
532 
533 	/* Check preceding horizontal. */
534 	first = 0;
535 	lp = listp;
536 	tlen = sp->cols;
537 	TAILQ_FOREACH(tsp, &wp->scrq, q) {
538 		if (sp == tsp)
539 			continue;
540 		/* Test if precedes the screen horizontally. */
541 		if (tsp->roff + tsp->rows != sp->roff)
542 			continue;
543 		/*
544 		 * Test if a subset on the horizontal axis.  If overlaps the
545 		 * beginning or end, we can't join on this axis at all.
546 		 */
547 		if (tsp->coff > sp->coff + sp->cols)
548 			continue;
549 		if (tsp->coff < sp->coff) {
550 			if (tsp->coff + tsp->cols >= sp->coff)
551 				break;
552 			continue;
553 		}
554 		if (tsp->coff + tsp->cols > sp->coff + sp->cols)
555 			break;
556 #ifdef DEBUG
557 		if (tlen < tsp->cols)
558 			abort();
559 #endif
560 		tlen -= tsp->cols + first;
561 		first = 1;
562 		*lp++ = tsp;
563 	}
564 	if (tlen == 0) {
565 		*lp = NULL;
566 		*jdirp = HORIZ_PRECEDE;
567 		return (0);
568 	}
569 
570 	/* Check following horizontal. */
571 	first = 0;
572 	lp = listp;
573 	tlen = sp->cols;
574 	TAILQ_FOREACH(tsp, &wp->scrq, q) {
575 		if (sp == tsp)
576 			continue;
577 		/* Test if precedes the screen horizontally. */
578 		if (tsp->roff != sp->roff + sp->rows)
579 			continue;
580 		/*
581 		 * Test if a subset on the horizontal axis.  If overlaps the
582 		 * beginning or end, we can't join on this axis at all.
583 		 */
584 		if (tsp->coff > sp->coff + sp->cols)
585 			continue;
586 		if (tsp->coff < sp->coff) {
587 			if (tsp->coff + tsp->cols >= sp->coff)
588 				break;
589 			continue;
590 		}
591 		if (tsp->coff + tsp->cols > sp->coff + sp->cols)
592 			break;
593 #ifdef DEBUG
594 		if (tlen < tsp->cols)
595 			abort();
596 #endif
597 		tlen -= tsp->cols + first;
598 		first = 1;
599 		*lp++ = tsp;
600 	}
601 	if (tlen == 0) {
602 		*lp = NULL;
603 		*jdirp = HORIZ_FOLLOW;
604 		return (0);
605 	}
606 	return (1);
607 }
608 
609 /*
610  * vs_fg --
611  *	Background the current screen, and foreground a new one.
612  *
613  * PUBLIC: int vs_fg __P((SCR *, SCR **, CHAR_T *, int));
614  */
615 int
616 vs_fg(SCR *sp, SCR **nspp, CHAR_T *name, int newscreen)
617 {
618 	GS *gp;
619 	WIN *wp;
620 	SCR *nsp;
621 	const char *np;
622 	size_t nlen;
623 
624 	gp = sp->gp;
625 	wp = sp->wp;
626 
627 	if (name)
628 	    INT2CHAR(sp, name, STRLEN(name) + 1, np, nlen);
629 	else
630 	    np = NULL;
631 	if (newscreen)
632 		/* Get the specified background screen. */
633 		nsp = vs_getbg(sp, np);
634 	else
635 		/* Swap screens. */
636 		if (vs_swap(sp, &nsp, np))
637 			return (1);
638 
639 	if ((*nspp = nsp) == NULL) {
640 		msgq_wstr(sp, M_ERR, name,
641 		    name == NULL ?
642 		    "223|There are no background screens" :
643 		    "224|There's no background screen editing a file named %s");
644 		return (1);
645 	}
646 
647 	if (newscreen) {
648 		/* Remove the new screen from the background queue. */
649 		TAILQ_REMOVE(&gp->hq, nsp, q);
650 
651 		/* Split the screen; if we fail, hook the screen back in. */
652 		if (vs_split(sp, nsp, 0)) {
653 			TAILQ_INSERT_TAIL(&gp->hq, nsp, q);
654 			return (1);
655 		}
656 	} else {
657 		/* Move the old screen to the background queue. */
658 		TAILQ_REMOVE(&wp->scrq, sp, q);
659 		TAILQ_INSERT_TAIL(&gp->hq, sp, q);
660 	}
661 	return (0);
662 }
663 
664 /*
665  * vs_bg --
666  *	Background the screen, and switch to the next one.
667  *
668  * PUBLIC: int vs_bg __P((SCR *));
669  */
670 int
671 vs_bg(SCR *sp)
672 {
673 	GS *gp;
674 	WIN *wp;
675 	SCR *nsp;
676 
677 	gp = sp->gp;
678 	wp = sp->wp;
679 
680 	/* Try and join with another screen. */
681 	if (vs_discard(sp, &nsp))
682 		return (1);
683 	if (nsp == NULL) {
684 		msgq(sp, M_ERR,
685 		    "225|You may not background your only displayed screen");
686 		return (1);
687 	}
688 
689 	/* Move the old screen to the background queue. */
690 	TAILQ_REMOVE(&wp->scrq, sp, q);
691 	TAILQ_INSERT_TAIL(&gp->hq, sp, q);
692 
693 	/* Toss the screen map. */
694 	free(_HMAP(sp));
695 	_HMAP(sp) = NULL;
696 
697 	/* Switch screens. */
698 	sp->nextdisp = nsp;
699 	F_SET(sp, SC_SSWITCH);
700 
701 	return (0);
702 }
703 
704 /*
705  * vs_swap --
706  *	Swap the current screen with a backgrounded one.
707  *
708  * PUBLIC: int vs_swap __P((SCR *, SCR **, const char *));
709  */
710 int
711 vs_swap(SCR *sp, SCR **nspp, const char *name)
712 {
713 	GS *gp;
714 	WIN *wp;
715 	SCR *nsp, *list[2];
716 
717 	gp = sp->gp;
718 	wp = sp->wp;
719 
720 	/* Get the specified background screen. */
721 	if ((*nspp = nsp = vs_getbg(sp, name)) == NULL)
722 		return (0);
723 
724 	/*
725 	 * Save the old screen's cursor information.
726 	 *
727 	 * XXX
728 	 * If called after file_end(), and the underlying file was a tmp
729 	 * file, it may have gone away.
730 	 */
731 	if (sp->frp != NULL) {
732 		sp->frp->lno = sp->lno;
733 		sp->frp->cno = sp->cno;
734 		F_SET(sp->frp, FR_CURSORSET);
735 	}
736 
737 	/* Switch screens. */
738 	sp->nextdisp = nsp;
739 	F_SET(sp, SC_SSWITCH);
740 
741 	/* Initialize terminal information. */
742 	VIP(nsp)->srows = VIP(sp)->srows;
743 
744 	/* Initialize screen information. */
745 	nsp->cols = sp->cols;
746 	nsp->rows = sp->rows;	/* XXX: Only place in vi that sets rows. */
747 	nsp->roff = sp->roff;
748 
749 	/*
750 	 * Small screens: see vs_refresh.c, section 6a.
751 	 *
752 	 * The new screens may have different screen options sizes than the
753 	 * old one, so use them.  Make sure that text counts aren't larger
754 	 * than the new screen sizes.
755 	 */
756 	if (IS_SMALL(nsp)) {
757 		nsp->t_minrows = nsp->t_rows = O_VAL(nsp, O_WINDOW);
758 		if (nsp->t_rows > sp->t_maxrows)
759 			nsp->t_rows = nsp->t_maxrows;
760 		if (nsp->t_minrows > sp->t_maxrows)
761 			nsp->t_minrows = nsp->t_maxrows;
762 	} else
763 		nsp->t_rows = nsp->t_maxrows = nsp->t_minrows = nsp->rows - 1;
764 
765 	/* Reset the length of the default scroll. */
766 	nsp->defscroll = nsp->t_maxrows / 2;
767 
768 	/* Allocate a new screen map. */
769 	CALLOC_RET(nsp, _HMAP(nsp), SMAP *, SIZE_HMAP(nsp), sizeof(SMAP));
770 	_TMAP(nsp) = _HMAP(nsp) + (nsp->t_rows - 1);
771 
772 	/* Fill the map. */
773 	nsp->wp = sp->wp;
774 	if (vs_sm_fill(nsp, nsp->lno, P_FILL))
775 		return (1);
776 
777 	/*
778 	 * The new screen replaces the old screen in the parent/child list.
779 	 * We insert the new screen after the old one.  If we're exiting,
780 	 * the exit will delete the old one, if we're foregrounding, the fg
781 	 * code will move the old one to the background queue.
782 	 */
783 	TAILQ_REMOVE(&gp->hq, nsp, q);
784 	TAILQ_INSERT_AFTER(&wp->scrq, sp, nsp, q);
785 
786 	/*
787 	 * Don't change the screen's cursor information other than to
788 	 * note that the cursor is wrong.
789 	 */
790 	F_SET(VIP(nsp), VIP_CUR_INVALID);
791 
792 	/* Draw the new screen from scratch, and add a status line. */
793 	F_SET(nsp, SC_SCR_REDRAW | SC_STATUS);
794 
795 	list[0] = nsp; list[1] = NULL;
796 	(void)gp->scr_discard(sp, list);
797 
798 	return (0);
799 }
800 
801 /*
802  * vs_resize --
803  *	Change the absolute size of the current screen.
804  *
805  * PUBLIC: int vs_resize __P((SCR *, long, adj_t));
806  */
807 int
808 vs_resize(SCR *sp, long int count, adj_t adj)
809 {
810 	GS *gp;
811 	SCR *g, *s, *prev, *next, *list[3] = {NULL, NULL, NULL};
812 	size_t g_off, s_off;
813 
814 	gp = sp->gp;
815 
816 	/*
817 	 * Figure out which screens will grow, which will shrink, and
818 	 * make sure it's possible.
819 	 */
820 	if (count == 0)
821 		return (0);
822 	if (adj == A_SET) {
823 		if (sp->t_maxrows == (size_t)count)
824 			return (0);
825 		if (sp->t_maxrows > (size_t)count) {
826 			adj = A_DECREASE;
827 			count = sp->t_maxrows - count;
828 		} else {
829 			adj = A_INCREASE;
830 			count = count - sp->t_maxrows;
831 		}
832 	}
833 
834 	/* Find first overlapping screen */
835 	for (next = TAILQ_NEXT(sp, q);
836 	     next != NULL &&
837 	     (next->coff >= sp->coff + sp->cols ||
838 	      next->coff + next->cols <= sp->coff);
839 	     next = TAILQ_NEXT(next, q))
840 		continue;
841 	/* See if we can use it */
842 	if (next != NULL &&
843 	    (sp->coff != next->coff || sp->cols != next->cols))
844 		next = NULL;
845 	for (prev = TAILQ_PREV(sp, _scrh, q);
846 	     prev != NULL &&
847 	     (prev->coff >= sp->coff + sp->cols ||
848 	      prev->coff + prev->cols <= sp->coff);
849 	     prev = TAILQ_PREV(prev, _scrh, q))
850 		continue;
851 	if (prev != NULL &&
852 	    (sp->coff != prev->coff || sp->cols != prev->cols))
853 		prev = NULL;
854 
855 	g_off = s_off = 0;
856 	if (adj == A_DECREASE) {
857 		if (count < 0)
858 			count = -count;
859 		s = sp;
860 		if (s->t_maxrows < MINIMUM_SCREEN_ROWS + (size_t)count)
861 			goto toosmall;
862 		if ((g = prev) == NULL) {
863 			if ((g = next) == NULL)
864 				goto toobig;
865 			g_off = -count;
866 		} else
867 			s_off = count;
868 	} else {
869 		g = sp;
870 		if ((s = next) != NULL &&
871 		    s->t_maxrows >= MINIMUM_SCREEN_ROWS + (size_t)count)
872 				s_off = count;
873 		else
874 			s = NULL;
875 		if (s == NULL) {
876 			if ((s = prev) == NULL) {
877 toobig:				msgq(sp, M_BERR, adj == A_DECREASE ?
878 				    "227|The screen cannot shrink" :
879 				    "228|The screen cannot grow");
880 				return (1);
881 			}
882 			if (s->t_maxrows < MINIMUM_SCREEN_ROWS + (size_t)count) {
883 toosmall:			msgq(sp, M_BERR,
884 				    "226|The screen can only shrink to %d rows",
885 				    MINIMUM_SCREEN_ROWS);
886 				return (1);
887 			}
888 			g_off = -count;
889 		}
890 	}
891 
892 	/*
893 	 * Fix up the screens; we could optimize the reformatting of the
894 	 * screen, but this isn't likely to be a common enough operation
895 	 * to make it worthwhile.
896 	 */
897 	s->rows += -count;
898 	s->roff += s_off;
899 	g->rows += count;
900 	g->roff += g_off;
901 
902 	g->t_rows += count;
903 	if (g->t_minrows == g->t_maxrows)
904 		g->t_minrows += count;
905 	g->t_maxrows += count;
906 	_TMAP(g) += count;
907 	F_SET(g, SC_SCR_REFORMAT | SC_STATUS);
908 
909 	s->t_rows -= count;
910 	s->t_maxrows -= count;
911 	if (s->t_minrows > s->t_maxrows)
912 		s->t_minrows = s->t_maxrows;
913 	_TMAP(s) -= count;
914 	F_SET(s, SC_SCR_REFORMAT | SC_STATUS);
915 
916 	/* XXXX */
917 	list[0] = g; list[1] = s;
918 	gp->scr_discard(0, list);
919 
920 	return (0);
921 }
922 
923 /*
924  * vs_getbg --
925  *	Get the specified background screen, or, if name is NULL, the first
926  *	background screen.
927  */
928 static SCR *
929 vs_getbg(SCR *sp, const char *name)
930 {
931 	GS *gp;
932 	SCR *nsp;
933 	char *p;
934 
935 	gp = sp->gp;
936 
937 	/* If name is NULL, return the first background screen on the list. */
938 	if (name == NULL) {
939 		return TAILQ_FIRST(&gp->hq);
940 	}
941 
942 	/* Search for a full match. */
943 	TAILQ_FOREACH(nsp, &gp->hq, q)
944 		if (!strcmp(nsp->frp->name, name))
945 			break;
946 	if (nsp != NULL)
947 		return (nsp);
948 
949 	/* Search for a last-component match. */
950 	TAILQ_FOREACH(nsp, &gp->hq, q) {
951 		if ((p = strrchr(nsp->frp->name, '/')) == NULL)
952 			p = nsp->frp->name;
953 		else
954 			++p;
955 		if (!strcmp(p, name))
956 			break;
957 	}
958 	return nsp;
959 }
960