File: vs_split.c

package info (click to toggle)
nvi 1.81.6-15
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 11,852 kB
  • sloc: ansic: 43,645; sh: 11,207; makefile: 604; perl: 262; tcl: 234; awk: 19
file content (968 lines) | stat: -rw-r--r-- 23,289 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
/*-
 * Copyright (c) 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
 *
 * See the LICENSE file for redistribution information.
 */

#include "config.h"

#ifndef lint
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 $";
#endif /* not lint */

#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"

typedef enum { HORIZ_FOLLOW, HORIZ_PRECEDE, VERT_FOLLOW, VERT_PRECEDE } jdir_t;

static SCR	*vs_getbg __P((SCR *, char *));
static void      vs_insert __P((SCR *sp, WIN *wp));
static int	 vs_join __P((SCR *, SCR **, jdir_t *));

/*
 * vs_split --
 *	Create a new screen, horizontally.
 *
 * PUBLIC: int vs_split __P((SCR *, SCR *, int));
 */
int
vs_split(SCR *sp, SCR *new, int ccl)
	              
	        		/* Colon-command line split. */
{
	GS *gp;
	SMAP *smp;
	size_t half;
	int issmallscreen, splitup;

	gp = sp->gp;

	/* Check to see if it's possible. */
	/* XXX: The IS_ONELINE fix will change this, too. */
	if (sp->rows < 4) {
		msgq(sp, M_ERR,
		    "222|Screen must be larger than %d lines to split", 4 - 1);
		return (1);
	}

	/* Wait for any messages in the screen. */
	vs_resolve(sp, NULL, 1);

	/* Get a new screen map. */
	CALLOC(sp, _HMAP(new), SMAP *, SIZE_HMAP(sp), sizeof(SMAP));
	if (_HMAP(new) == NULL)
		return (1);
	_HMAP(new)->lno = sp->lno;
	_HMAP(new)->coff = 0;
	_HMAP(new)->soff = 1;

	/* Split the screen in half. */
	half = sp->rows / 2;
	if (ccl && half > 6)
		half = 6;

	/*
	 * Small screens: see vs_refresh.c section 6a.  Set a flag so
	 * we know to fix the screen up later.
	 */
	issmallscreen = IS_SMALL(sp);

	/* The columns in the screen don't change. */
	new->coff = sp->coff;
	new->cols = sp->cols;

	/*
	 * Split the screen, and link the screens together.  If creating a
	 * screen to edit the colon command line or the cursor is in the top
	 * half of the current screen, the new screen goes under the current
	 * screen.  Else, it goes above the current screen.
	 *
	 * Recalculate current cursor position based on sp->lno, we're called
	 * with the cursor on the colon command line.  Then split the screen
	 * in half and update the shared information.
	 */
	splitup =
	    !ccl && (vs_sm_cursor(sp, &smp) ? 0 : (smp - HMAP) + 1) >= half;
	if (splitup) {				/* Old is bottom half. */
		new->rows = sp->rows - half;	/* New. */
		new->roff = sp->roff;
		sp->rows = half;		/* Old. */
		sp->roff += new->rows;

		/*
		 * If the parent is the bottom half of the screen, shift
		 * the map down to match on-screen text.
		 */
		memcpy(_HMAP(sp), _HMAP(sp) + new->rows,
		    (sp->t_maxrows - new->rows) * sizeof(SMAP));
	} else {				/* Old is top half. */
		new->rows = half;		/* New. */
		sp->rows -= half;		/* Old. */
		new->roff = sp->roff + sp->rows;
	}

	/* Adjust maximum text count. */
	sp->t_maxrows = IS_ONELINE(sp) ? 1 : sp->rows - 1;
	new->t_maxrows = IS_ONELINE(new) ? 1 : new->rows - 1;

	/*
	 * Small screens: see vs_refresh.c, section 6a.
	 *
	 * The child may have different screen options sizes than the parent,
	 * so use them.  Guarantee that text counts aren't larger than the
	 * new screen sizes.
	 */
	if (issmallscreen) {
		/* Fix the text line count for the parent. */
		if (splitup)
			sp->t_rows -= new->rows;

		/* Fix the parent screen. */
		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;

		/* Fix the child screen. */
		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;

		/*
		 * The new screen may be a small screen, even if the parent
		 * was not.  Don't complain if O_WINDOW is too large, we're
		 * splitting the screen so the screen is much smaller than
		 * normal.
		 */
		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;
	}

	/* Adjust the ends of the new and old maps. */
	_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);

	/* Reset the length of the default scroll. */
	if ((sp->defscroll = sp->t_maxrows / 2) == 0)
		sp->defscroll = 1;
	if ((new->defscroll = new->t_maxrows / 2) == 0)
		new->defscroll = 1;

	/* Fit the screen into the logical chain. */
	vs_insert(new, sp->wp);

	/* Tell the display that we're splitting. */
	(void)gp->scr_split(sp, new);

	/*
	 * Initialize the screen flags:
	 *
	 * If we're in vi mode in one screen, we don't have to reinitialize.
	 * This isn't just a cosmetic fix.  The path goes like this:
	 *
	 *	return into vi(), SC_SSWITCH set
	 *	call vs_refresh() with SC_STATUS set
	 *	call vs_resolve to display the status message
	 *	call vs_refresh() because the SC_SCR_VI bit isn't set
	 *
	 * Things go downhill at this point.
	 *
	 * Draw the new screen from scratch, and add a status line.
	 */
	F_SET(new,
	    SC_SCR_REFORMAT | SC_STATUS |
	    F_ISSET(sp, SC_EX | SC_VI | SC_SCR_VI | SC_SCR_EX));
	return (0);
}

/*
 * vs_vsplit --
 *	Create a new screen, vertically.
 *
 * PUBLIC: int vs_vsplit __P((SCR *, SCR *));
 */
int
vs_vsplit(SCR *sp, SCR *new)
{
	GS *gp;
	size_t cols;

	gp = sp->gp;

	/* Check to see if it's possible. */
	if (sp->cols / 2 <= MINIMUM_SCREEN_COLS) {
		msgq(sp, M_ERR,
		    "288|Screen must be larger than %d columns to split",
		    MINIMUM_SCREEN_COLS * 2);
		return (1);
	}

	/* Wait for any messages in the screen. */
	vs_resolve(sp, NULL, 1);

	/* Get a new screen map. */
	CALLOC(sp, _HMAP(new), SMAP *, SIZE_HMAP(sp), sizeof(SMAP));
	if (_HMAP(new) == NULL)
		return (1);
	_HMAP(new)->lno = sp->lno;
	_HMAP(new)->coff = 0;
	_HMAP(new)->soff = 1;

	/*
	 * Split the screen in half; we have to sacrifice a column to delimit
	 * the screens.
	 *
	 * XXX
	 * We always split to the right... that makes more sense to me, and
	 * I don't want to play the stupid games that I play when splitting
	 * horizontally.
	 *
	 * XXX
	 * We reserve a column for the screen, "knowing" that curses needs
	 * one.  This should be worked out with the display interface.
	 */
	cols = sp->cols / 2;
	new->cols = sp->cols - cols - 1;
	sp->cols = cols;
	new->coff = sp->coff + cols + 1;
	sp->cno = 0;

	/* Nothing else changes. */
	new->rows = sp->rows;
	new->t_rows = sp->t_rows;
	new->t_maxrows = sp->t_maxrows;
	new->t_minrows = sp->t_minrows;
	new->roff = sp->roff;
	new->defscroll = sp->defscroll;
	_TMAP(new) = _HMAP(new) + (new->t_rows - 1);

	/* Fit the screen into the logical chain. */
	vs_insert(new, sp->wp);

	/* Tell the display that we're splitting. */
	(void)gp->scr_split(sp, new);

	/* Redraw the old screen from scratch. */
	F_SET(sp, SC_SCR_REFORMAT | SC_STATUS);

	/*
	 * Initialize the screen flags:
	 *
	 * If we're in vi mode in one screen, we don't have to reinitialize.
	 * This isn't just a cosmetic fix.  The path goes like this:
	 *
	 *	return into vi(), SC_SSWITCH set
	 *	call vs_refresh() with SC_STATUS set
	 *	call vs_resolve to display the status message
	 *	call vs_refresh() because the SC_SCR_VI bit isn't set
	 *
	 * Things go downhill at this point.
	 *
	 * Draw the new screen from scratch, and add a status line.
	 */
	F_SET(new,
	    SC_SCR_REFORMAT | SC_STATUS |
	    F_ISSET(sp, SC_EX | SC_VI | SC_SCR_VI | SC_SCR_EX));
	return (0);
}

/*
 * vs_insert --
 *	Insert the new screen into the correct place in the logical
 *	chain.
 */
static void
vs_insert(SCR *sp, WIN *wp)
{
	GS *gp;
	SCR *tsp;

	gp = sp->gp;

	sp->wp = wp;

	/* Move past all screens with lower row numbers. */
	for (tsp = wp->scrq.cqh_first;
	    tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
		if (tsp->roff >= sp->roff)
			break;
	/*
	 * Move past all screens with the same row number and lower
	 * column numbers.
	 */
	for (; tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
		if (tsp->roff != sp->roff || tsp->coff > sp->coff)
			break;

	/*
	 * If we reached the end, this screen goes there.  Otherwise,
	 * put it before or after the screen where we stopped.
	 */
	if (tsp == (void *)&wp->scrq) {
		CIRCLEQ_INSERT_TAIL(&wp->scrq, sp, q);
	} else if (tsp->roff < sp->roff ||
	    (tsp->roff == sp->roff && tsp->coff < sp->coff)) {
		CIRCLEQ_INSERT_AFTER(&wp->scrq, tsp, sp, q);
	} else
		CIRCLEQ_INSERT_BEFORE(&wp->scrq, tsp, sp, q);
}

/*
 * vs_discard --
 *	Discard the screen, folding the real-estate into a related screen,
 *	if one exists, and return that screen.
 *
 * PUBLIC: int vs_discard __P((SCR *, SCR **));
 */
int
vs_discard(SCR *sp, SCR **spp)
{
	GS *gp;
	SCR *tsp, **lp, *list[100];
	jdir_t jdir;

	gp = sp->gp;

	/*
	 * Save the old screen's cursor information.
	 *
	 * XXX
	 * If called after file_end(), and the underlying file was a tmp
	 * file, it may have gone away.
	 */
	if (sp->frp != NULL) {
		sp->frp->lno = sp->lno;
		sp->frp->cno = sp->cno;
		F_SET(sp->frp, FR_CURSORSET);
	}

	/* If no other screens to join, we're done. */
	if (!IS_SPLIT(sp)) {
		(void)gp->scr_discard(sp, NULL);

		if (spp != NULL)
			*spp = NULL;
		return (0);
	}

	/*
	 * Find a set of screens that cover one of the screen's borders.
	 * Check the vertical axis first, for no particular reason.
	 *
	 * XXX
	 * It's possible (I think?), to create a screen that shares no full
	 * border with any other set of screens, so we can't discard it.  We
	 * just complain at the user until they clean it up.
	 */
	if (vs_join(sp, list, &jdir))
		return (1);

	/*
	 * Modify the affected screens.  Redraw the modified screen(s) from
	 * scratch, setting a status line.  If this is ever a performance
	 * problem we could play games with the map, but I wrote that code
	 * before and it was never clean or easy.
	 *
	 * Don't clean up the discarded screen's information.  If the screen
	 * isn't exiting, we'll do the work when the user redisplays it.
	 */
	switch (jdir) {
	case HORIZ_FOLLOW:
	case HORIZ_PRECEDE:
		for (lp = &list[0]; (tsp = *lp) != NULL; ++lp) {
			/*
			 * Small screens: see vs_refresh.c section 6a.  Adjust
			 * text line info, unless it's a small screen.
			 *
			 * Reset the length of the default scroll.
			 *
			 * Reset the map references.
			 */
			tsp->rows += sp->rows;
			if (!IS_SMALL(tsp))
				tsp->t_rows = tsp->t_minrows = tsp->rows - 1;
			tsp->t_maxrows = tsp->rows - 1;

			tsp->defscroll = tsp->t_maxrows / 2;

			*(_HMAP(tsp) + (tsp->t_rows - 1)) = *_TMAP(tsp);
			_TMAP(tsp) = _HMAP(tsp) + (tsp->t_rows - 1);

			switch (jdir) {
			case HORIZ_FOLLOW:
				tsp->roff = sp->roff;
				vs_sm_fill(tsp, OOBLNO, P_TOP);
				break;
			case HORIZ_PRECEDE:
				vs_sm_fill(tsp, OOBLNO, P_BOTTOM);
				break;
			default:
				abort();
			}
			F_SET(tsp, SC_STATUS);
		}
		break;
	case VERT_FOLLOW:
	case VERT_PRECEDE:
		for (lp = &list[0]; (tsp = *lp) != NULL; ++lp) {
			if (jdir == VERT_FOLLOW)
				tsp->coff = sp->coff;
			tsp->cols += sp->cols + 1;	/* XXX: DIVIDER */
			vs_sm_fill(tsp, OOBLNO, P_TOP);
			F_SET(tsp, SC_STATUS);
		}
		break;
	default:
		abort();
	}

	/* Find the closest screen that changed and move to it. */
	tsp = list[0];
	if (spp != NULL)
		*spp = tsp;

	/* Tell the display that we're discarding a screen. */
	(void)gp->scr_discard(sp, list);

	return (0);
}

/*
 * vs_join --
 *	Find a set of screens that covers a screen's border.
 */
static int
vs_join(SCR *sp, SCR **listp, jdir_t *jdirp)
{
	GS *gp;
	WIN *wp;
	SCR **lp, *tsp;
	int first;
	size_t tlen;

	gp = sp->gp;
	wp = sp->wp;

	/* Check preceding vertical. */
	for (lp = listp, tlen = sp->rows,
	    tsp = wp->scrq.cqh_first;
	    tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next) {
		if (sp == tsp)
			continue;
		/* Test if precedes the screen vertically. */
		if (tsp->coff + tsp->cols + 1 != sp->coff)
			continue;
		/*
		 * Test if a subset on the vertical axis.  If overlaps the
		 * beginning or end, we can't join on this axis at all.
		 */
		if (tsp->roff > sp->roff + sp->rows)
			continue;
		if (tsp->roff < sp->roff) {
			if (tsp->roff + tsp->rows >= sp->roff)
				break;
			continue;
		}
		if (tsp->roff + tsp->rows > sp->roff + sp->rows)
			break;
#ifdef DEBUG
		if (tlen < tsp->rows)
			abort();
#endif
		tlen -= tsp->rows;
		*lp++ = tsp;
	}
	if (tlen == 0) {
		*lp = NULL;
		*jdirp = VERT_PRECEDE;
		return (0);
	}

	/* Check following vertical. */
	for (lp = listp, tlen = sp->rows,
	    tsp = wp->scrq.cqh_first;
	    tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next) {
		if (sp == tsp)
			continue;
		/* Test if follows the screen vertically. */
		if (tsp->coff != sp->coff + sp->cols + 1)
			continue;
		/*
		 * Test if a subset on the vertical axis.  If overlaps the
		 * beginning or end, we can't join on this axis at all.
		 */
		if (tsp->roff > sp->roff + sp->rows)
			continue;
		if (tsp->roff < sp->roff) {
			if (tsp->roff + tsp->rows >= sp->roff)
				break;
			continue;
		}
		if (tsp->roff + tsp->rows > sp->roff + sp->rows)
			break;
#ifdef DEBUG
		if (tlen < tsp->rows)
			abort();
#endif
		tlen -= tsp->rows;
		*lp++ = tsp;
	}
	if (tlen == 0) {
		*lp = NULL;
		*jdirp = VERT_FOLLOW;
		return (0);
	}

	/* Check preceding horizontal. */
	for (first = 0, lp = listp, tlen = sp->cols,
	    tsp = wp->scrq.cqh_first;
	    tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next) {
		if (sp == tsp)
			continue;
		/* Test if precedes the screen horizontally. */
		if (tsp->roff + tsp->rows != sp->roff)
			continue;
		/*
		 * Test if a subset on the horizontal axis.  If overlaps the
		 * beginning or end, we can't join on this axis at all.
		 */
		if (tsp->coff > sp->coff + sp->cols)
			continue;
		if (tsp->coff < sp->coff) {
			if (tsp->coff + tsp->cols >= sp->coff)
				break;
			continue;
		}
		if (tsp->coff + tsp->cols > sp->coff + sp->cols)
			break;
#ifdef DEBUG
		if (tlen < tsp->cols)
			abort();
#endif
		tlen -= tsp->cols + first;
		first = 1;
		*lp++ = tsp;
	}
	if (tlen == 0) {
		*lp = NULL;
		*jdirp = HORIZ_PRECEDE;
		return (0);
	}

	/* Check following horizontal. */
	for (first = 0, lp = listp, tlen = sp->cols,
	    tsp = wp->scrq.cqh_first;
	    tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next) {
		if (sp == tsp)
			continue;
		/* Test if precedes the screen horizontally. */
		if (tsp->roff != sp->roff + sp->rows)
			continue;
		/*
		 * Test if a subset on the horizontal axis.  If overlaps the
		 * beginning or end, we can't join on this axis at all.
		 */
		if (tsp->coff > sp->coff + sp->cols)
			continue;
		if (tsp->coff < sp->coff) {
			if (tsp->coff + tsp->cols >= sp->coff)
				break;
			continue;
		}
		if (tsp->coff + tsp->cols > sp->coff + sp->cols)
			break;
#ifdef DEBUG
		if (tlen < tsp->cols)
			abort();
#endif
		tlen -= tsp->cols + first;
		first = 1;
		*lp++ = tsp;
	}
	if (tlen == 0) {
		*lp = NULL;
		*jdirp = HORIZ_FOLLOW;
		return (0);
	}
	return (1);
}

/*
 * vs_fg --
 *	Background the current screen, and foreground a new one.
 *
 * PUBLIC: int vs_fg __P((SCR *, SCR **, CHAR_T *, int));
 */
int
vs_fg(SCR *sp, SCR **nspp, CHAR_T *name, int newscreen)
{
	GS *gp;
	WIN *wp;
	SCR *nsp;
	char *np;
	size_t nlen;

	gp = sp->gp;
	wp = sp->wp;

	if (name)
	    INT2CHAR(sp, name, STRLEN(name) + 1, np, nlen);
	else
	    np = NULL;
	if (newscreen)
		/* Get the specified background screen. */
		nsp = vs_getbg(sp, np);
	else
		/* Swap screens. */
		if (vs_swap(sp, &nsp, np))
			return (1);

	if ((*nspp = nsp) == NULL) {
		msgq_wstr(sp, M_ERR, name,
		    name == NULL ?
		    "223|There are no background screens" :
		    "224|There's no background screen editing a file named %s");
		return (1);
	}

	if (newscreen) {
		/* Remove the new screen from the background queue. */
		CIRCLEQ_REMOVE(&gp->hq, nsp, q);

		/* Split the screen; if we fail, hook the screen back in. */
		if (vs_split(sp, nsp, 0)) {
			CIRCLEQ_INSERT_TAIL(&gp->hq, nsp, q);
			return (1);
		}
	} else {
		/* Move the old screen to the background queue. */
		CIRCLEQ_REMOVE(&wp->scrq, sp, q);
		CIRCLEQ_INSERT_TAIL(&gp->hq, sp, q);
	}
	return (0);
}

/*
 * vs_bg --
 *	Background the screen, and switch to the next one.
 *
 * PUBLIC: int vs_bg __P((SCR *));
 */
int
vs_bg(SCR *sp)
{
	GS *gp;
	WIN *wp;
	SCR *nsp;

	gp = sp->gp;
	wp = sp->wp;

	/* Try and join with another screen. */
	if (vs_discard(sp, &nsp))
		return (1);
	if (nsp == NULL) {
		msgq(sp, M_ERR,
		    "225|You may not background your only displayed screen");
		return (1);
	}

	/* Move the old screen to the background queue. */
	CIRCLEQ_REMOVE(&wp->scrq, sp, q);
	CIRCLEQ_INSERT_TAIL(&gp->hq, sp, q);

	/* Toss the screen map. */
	free(_HMAP(sp));
	_HMAP(sp) = NULL;

	/* Switch screens. */
	sp->nextdisp = nsp;
	F_SET(sp, SC_SSWITCH);

	return (0);
}

/*
 * vs_swap --
 *	Swap the current screen with a backgrounded one.
 *
 * PUBLIC: int vs_swap __P((SCR *, SCR **, char *));
 */
int
vs_swap(SCR *sp, SCR **nspp, char *name)
{
	GS *gp;
	WIN *wp;
	SCR *nsp, *list[2];

	gp = sp->gp;
	wp = sp->wp;

	/* Get the specified background screen. */
	if ((*nspp = nsp = vs_getbg(sp, name)) == NULL)
		return (0);

	/*
	 * Save the old screen's cursor information.
	 *
	 * XXX
	 * If called after file_end(), and the underlying file was a tmp
	 * file, it may have gone away.
	 */
	if (sp->frp != NULL) {
		sp->frp->lno = sp->lno;
		sp->frp->cno = sp->cno;
		F_SET(sp->frp, FR_CURSORSET);
	}

	/* Switch screens. */
	sp->nextdisp = nsp;
	F_SET(sp, SC_SSWITCH);

	/* Initialize terminal information. */
	VIP(nsp)->srows = VIP(sp)->srows;

	/* Initialize screen information. */
	nsp->cols = sp->cols;
	nsp->rows = sp->rows;	/* XXX: Only place in vi that sets rows. */
	nsp->roff = sp->roff;

	/*
	 * Small screens: see vs_refresh.c, section 6a.
	 *
	 * The new screens may have different screen options sizes than the
	 * old one, so use them.  Make sure that text counts aren't larger
	 * than the new screen sizes.
	 */
	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;

	/* Reset the length of the default scroll. */
	nsp->defscroll = nsp->t_maxrows / 2;

	/* Allocate a new screen map. */
	CALLOC_RET(nsp, _HMAP(nsp), SMAP *, SIZE_HMAP(nsp), sizeof(SMAP));
	_TMAP(nsp) = _HMAP(nsp) + (nsp->t_rows - 1);

	/* Fill the map. */
	nsp->wp = sp->wp;
	if (vs_sm_fill(nsp, nsp->lno, P_FILL))
		return (1);

	/*
	 * The new screen replaces the old screen in the parent/child list.
	 * We insert the new screen after the old one.  If we're exiting,
	 * the exit will delete the old one, if we're foregrounding, the fg
	 * code will move the old one to the background queue.
	 */
	CIRCLEQ_REMOVE(&gp->hq, nsp, q);
	CIRCLEQ_INSERT_AFTER(&wp->scrq, sp, nsp, q);

	/*
	 * Don't change the screen's cursor information other than to
	 * note that the cursor is wrong.
	 */
	F_SET(VIP(nsp), VIP_CUR_INVALID);

	/* Draw the new screen from scratch, and add a status line. */
	F_SET(nsp, SC_SCR_REDRAW | SC_STATUS);

	list[0] = nsp; list[1] = NULL;
	(void)gp->scr_discard(sp, list);

	return (0);
}

/*
 * vs_resize --
 *	Change the absolute size of the current screen.
 *
 * PUBLIC: int vs_resize __P((SCR *, long, adj_t));
 */
int
vs_resize(SCR *sp, long int count, adj_t adj)
{
	GS *gp;
	WIN *wp;
	SCR *g, *s, *prev, *next, *list[3] = {NULL, NULL, NULL};
	size_t g_off, s_off;

	gp = sp->gp;
	wp = sp->wp;

	/*
	 * Figure out which screens will grow, which will shrink, and
	 * make sure it's possible.
	 */
	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;
		}
	}

	/* Find first overlapping screen */
	for (next = sp->q.cqe_next; 
	     next != (void *)&wp->scrq && 
	     (next->coff >= sp->coff + sp->cols || 
	      next->coff + next->cols <= sp->coff); 
	     next = next->q.cqe_next);
	/* See if we can use it */
	if (next != (void *)&wp->scrq && 
	    (sp->coff != next->coff || sp->cols != next->cols))
		next = (void *)&wp->scrq;
	for (prev = sp->q.cqe_prev; 
	     prev != (void *)&wp->scrq && 
	     (prev->coff >= sp->coff + sp->cols || 
	      prev->coff + prev->cols <= sp->coff); 
	     prev = prev->q.cqe_prev);
	if (prev != (void *)&wp->scrq && 
	    (sp->coff != prev->coff || sp->cols != prev->cols))
		prev = (void *)&wp->scrq;

	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 = prev) == (void *)&wp->scrq) {
			if ((g = next) == (void *)&wp->scrq)
				goto toobig;
			g_off = -count;
		} else
			s_off = count;
	} else {
		g = sp;
		if ((s = next) != (void *)&wp->scrq &&
		    s->t_maxrows >= MINIMUM_SCREEN_ROWS + count)
				s_off = count;
		else
			s = NULL;
		if (s == NULL) {
			if ((s = prev) == (void *)&wp->scrq) {
toobig:				msgq(sp, M_BERR, adj == A_DECREASE ?
				    "227|The screen cannot shrink" :
				    "228|The screen cannot grow");
				return (1);
			}
			if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count) {
toosmall:			msgq(sp, M_BERR,
				    "226|The screen can only shrink to %d rows",
				    MINIMUM_SCREEN_ROWS);
				return (1);
			}
			g_off = -count;
		}
	}

	/*
	 * Fix up the screens; we could optimize the reformatting of the
	 * screen, but this isn't likely to be a common enough operation
	 * to make it worthwhile.
	 */
	s->rows += -count;
	s->roff += s_off;
	g->rows += count;
	g->roff += 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);

	/* XXXX */
	list[0] = g; list[1] = s;
	gp->scr_discard(0, list);

	return (0);
}

/*
 * vs_getbg --
 *	Get the specified background screen, or, if name is NULL, the first
 *	background screen.
 */
static SCR *
vs_getbg(SCR *sp, char *name)
{
	GS *gp;
	SCR *nsp;
	char *p;

	gp = sp->gp;

	/* If name is NULL, return the first background screen on the list. */
	if (name == NULL) {
		nsp = gp->hq.cqh_first;
		return (nsp == (void *)&gp->hq ? NULL : nsp);
	}

	/* Search for a full match. */
	for (nsp = gp->hq.cqh_first;
	    nsp != (void *)&gp->hq; nsp = nsp->q.cqe_next)
		if (!strcmp(nsp->frp->name, name))
			break;
	if (nsp != (void *)&gp->hq)
		return (nsp);

	/* Search for a last-component match. */
	for (nsp = gp->hq.cqh_first;
	    nsp != (void *)&gp->hq; nsp = nsp->q.cqe_next) {
		if ((p = strrchr(nsp->frp->name, '/')) == NULL)
			p = nsp->frp->name;
		else
			++p;
		if (!strcmp(p, name))
			break;
	}
	if (nsp != (void *)&gp->hq)
		return (nsp);

	return (NULL);
}