vi/vs_split: vs_swap: initialize wp of new window before calling vs_sm_fill
[nvi.git] / vi / vs_split.c
blob155e70444cc6bd430d4fd3bf45049224f429179d
1 /*-
2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 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 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "../common/common.h"
28 #include "vi.h"
30 typedef enum { HORIZ_FOLLOW, HORIZ_PRECEDE, VERT_FOLLOW, VERT_PRECEDE } jdir_t;
32 static SCR *vs_getbg __P((SCR *, char *));
33 static void vs_insert __P((SCR *sp, WIN *wp));
34 static int vs_join __P((SCR *, SCR **, jdir_t *));
37 * vs_split --
38 * Create a new screen, horizontally.
40 * PUBLIC: int vs_split __P((SCR *, SCR *, int));
42 int
43 vs_split(SCR *sp, SCR *new, int ccl)
45 /* Colon-command line split. */
47 GS *gp;
48 SMAP *smp;
49 size_t half;
50 int issmallscreen, splitup;
52 gp = sp->gp;
54 /* Check to see if it's possible. */
55 /* XXX: The IS_ONELINE fix will change this, too. */
56 if (sp->rows < 4) {
57 msgq(sp, M_ERR,
58 "222|Screen must be larger than %d lines to split", 4 - 1);
59 return (1);
62 /* Wait for any messages in the screen. */
63 vs_resolve(sp, NULL, 1);
65 /* Get a new screen map. */
66 CALLOC(sp, _HMAP(new), SMAP *, SIZE_HMAP(sp), sizeof(SMAP));
67 if (_HMAP(new) == NULL)
68 return (1);
69 _HMAP(new)->lno = sp->lno;
70 _HMAP(new)->coff = 0;
71 _HMAP(new)->soff = 1;
73 /* Split the screen in half. */
74 half = sp->rows / 2;
75 if (ccl && half > 6)
76 half = 6;
79 * Small screens: see vs_refresh.c section 6a. Set a flag so
80 * we know to fix the screen up later.
82 issmallscreen = IS_SMALL(sp);
84 /* The columns in the screen don't change. */
85 new->coff = sp->coff;
86 new->cols = sp->cols;
89 * Split the screen, and link the screens together. If creating a
90 * screen to edit the colon command line or the cursor is in the top
91 * half of the current screen, the new screen goes under the current
92 * screen. Else, it goes above the current screen.
94 * Recalculate current cursor position based on sp->lno, we're called
95 * with the cursor on the colon command line. Then split the screen
96 * in half and update the shared information.
98 splitup =
99 !ccl && (vs_sm_cursor(sp, &smp) ? 0 : (smp - HMAP) + 1) >= half;
100 if (splitup) { /* Old is bottom half. */
101 new->rows = sp->rows - half; /* New. */
102 new->roff = sp->roff;
103 sp->rows = half; /* Old. */
104 sp->roff += new->rows;
107 * If the parent is the bottom half of the screen, shift
108 * the map down to match on-screen text.
110 memcpy(_HMAP(sp), _HMAP(sp) + new->rows,
111 (sp->t_maxrows - new->rows) * sizeof(SMAP));
112 } else { /* Old is top half. */
113 new->rows = half; /* New. */
114 sp->rows -= half; /* Old. */
115 new->roff = sp->roff + sp->rows;
118 /* Adjust maximum text count. */
119 sp->t_maxrows = IS_ONELINE(sp) ? 1 : sp->rows - 1;
120 new->t_maxrows = IS_ONELINE(new) ? 1 : new->rows - 1;
123 * Small screens: see vs_refresh.c, section 6a.
125 * The child may have different screen options sizes than the parent,
126 * so use them. Guarantee that text counts aren't larger than the
127 * new screen sizes.
129 if (issmallscreen) {
130 /* Fix the text line count for the parent. */
131 if (splitup)
132 sp->t_rows -= new->rows;
134 /* Fix the parent screen. */
135 if (sp->t_rows > sp->t_maxrows)
136 sp->t_rows = sp->t_maxrows;
137 if (sp->t_minrows > sp->t_maxrows)
138 sp->t_minrows = sp->t_maxrows;
140 /* Fix the child screen. */
141 new->t_minrows = new->t_rows = O_VAL(sp, O_WINDOW);
142 if (new->t_rows > new->t_maxrows)
143 new->t_rows = new->t_maxrows;
144 if (new->t_minrows > new->t_maxrows)
145 new->t_minrows = new->t_maxrows;
146 } else {
147 sp->t_minrows = sp->t_rows = IS_ONELINE(sp) ? 1 : sp->rows - 1;
150 * The new screen may be a small screen, even if the parent
151 * was not. Don't complain if O_WINDOW is too large, we're
152 * splitting the screen so the screen is much smaller than
153 * normal.
155 new->t_minrows = new->t_rows = O_VAL(sp, O_WINDOW);
156 if (new->t_rows > new->rows - 1)
157 new->t_minrows = new->t_rows =
158 IS_ONELINE(new) ? 1 : new->rows - 1;
161 /* Adjust the ends of the new and old maps. */
162 _TMAP(sp) = IS_ONELINE(sp) ?
163 _HMAP(sp) : _HMAP(sp) + (sp->t_rows - 1);
164 _TMAP(new) = IS_ONELINE(new) ?
165 _HMAP(new) : _HMAP(new) + (new->t_rows - 1);
167 /* Reset the length of the default scroll. */
168 if ((sp->defscroll = sp->t_maxrows / 2) == 0)
169 sp->defscroll = 1;
170 if ((new->defscroll = new->t_maxrows / 2) == 0)
171 new->defscroll = 1;
173 /* Fit the screen into the logical chain. */
174 vs_insert(new, sp->wp);
176 /* Tell the display that we're splitting. */
177 (void)gp->scr_split(sp, new);
180 * Initialize the screen flags:
182 * If we're in vi mode in one screen, we don't have to reinitialize.
183 * This isn't just a cosmetic fix. The path goes like this:
185 * return into vi(), SC_SSWITCH set
186 * call vs_refresh() with SC_STATUS set
187 * call vs_resolve to display the status message
188 * call vs_refresh() because the SC_SCR_VI bit isn't set
190 * Things go downhill at this point.
192 * Draw the new screen from scratch, and add a status line.
194 F_SET(new,
195 SC_SCR_REFORMAT | SC_STATUS |
196 F_ISSET(sp, SC_EX | SC_VI | SC_SCR_VI | SC_SCR_EX));
197 return (0);
201 * vs_vsplit --
202 * Create a new screen, vertically.
204 * PUBLIC: int vs_vsplit __P((SCR *, SCR *));
207 vs_vsplit(SCR *sp, SCR *new)
209 GS *gp;
210 size_t cols;
212 gp = sp->gp;
214 /* Check to see if it's possible. */
215 if (sp->cols / 2 <= MINIMUM_SCREEN_COLS) {
216 msgq(sp, M_ERR,
217 "288|Screen must be larger than %d columns to split",
218 MINIMUM_SCREEN_COLS * 2);
219 return (1);
222 /* Wait for any messages in the screen. */
223 vs_resolve(sp, NULL, 1);
225 /* Get a new screen map. */
226 CALLOC(sp, _HMAP(new), SMAP *, SIZE_HMAP(sp), sizeof(SMAP));
227 if (_HMAP(new) == NULL)
228 return (1);
229 _HMAP(new)->lno = sp->lno;
230 _HMAP(new)->coff = 0;
231 _HMAP(new)->soff = 1;
234 * Split the screen in half; we have to sacrifice a column to delimit
235 * the screens.
237 * XXX
238 * We always split to the right... that makes more sense to me, and
239 * I don't want to play the stupid games that I play when splitting
240 * horizontally.
242 * XXX
243 * We reserve a column for the screen, "knowing" that curses needs
244 * one. This should be worked out with the display interface.
246 cols = sp->cols / 2;
247 new->cols = sp->cols - cols - 1;
248 sp->cols = cols;
249 new->coff = sp->coff + cols + 1;
250 sp->cno = 0;
252 /* Nothing else changes. */
253 new->rows = sp->rows;
254 new->t_rows = sp->t_rows;
255 new->t_maxrows = sp->t_maxrows;
256 new->t_minrows = sp->t_minrows;
257 new->roff = sp->roff;
258 new->defscroll = sp->defscroll;
259 _TMAP(new) = _HMAP(new) + (new->t_rows - 1);
261 /* Fit the screen into the logical chain. */
262 vs_insert(new, sp->wp);
264 /* Tell the display that we're splitting. */
265 (void)gp->scr_split(sp, new);
267 /* Redraw the old screen from scratch. */
268 F_SET(sp, SC_SCR_REFORMAT | SC_STATUS);
271 * Initialize the screen flags:
273 * If we're in vi mode in one screen, we don't have to reinitialize.
274 * This isn't just a cosmetic fix. The path goes like this:
276 * return into vi(), SC_SSWITCH set
277 * call vs_refresh() with SC_STATUS set
278 * call vs_resolve to display the status message
279 * call vs_refresh() because the SC_SCR_VI bit isn't set
281 * Things go downhill at this point.
283 * Draw the new screen from scratch, and add a status line.
285 F_SET(new,
286 SC_SCR_REFORMAT | SC_STATUS |
287 F_ISSET(sp, SC_EX | SC_VI | SC_SCR_VI | SC_SCR_EX));
288 return (0);
292 * vs_insert --
293 * Insert the new screen into the correct place in the logical
294 * chain.
296 static void
297 vs_insert(SCR *sp, WIN *wp)
299 GS *gp;
300 SCR *tsp;
302 gp = sp->gp;
304 sp->wp = wp;
306 /* Move past all screens with lower row numbers. */
307 for (tsp = wp->scrq.cqh_first;
308 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
309 if (tsp->roff >= sp->roff)
310 break;
312 * Move past all screens with the same row number and lower
313 * column numbers.
315 for (; tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
316 if (tsp->roff != sp->roff || tsp->coff > sp->coff)
317 break;
320 * If we reached the end, this screen goes there. Otherwise,
321 * put it before or after the screen where we stopped.
323 if (tsp == (void *)&wp->scrq) {
324 CIRCLEQ_INSERT_TAIL(&wp->scrq, sp, q);
325 } else if (tsp->roff < sp->roff ||
326 (tsp->roff == sp->roff && tsp->coff < sp->coff)) {
327 CIRCLEQ_INSERT_AFTER(&wp->scrq, tsp, sp, q);
328 } else
329 CIRCLEQ_INSERT_BEFORE(&wp->scrq, tsp, sp, q);
333 * vs_discard --
334 * Discard the screen, folding the real-estate into a related screen,
335 * if one exists, and return that screen.
337 * PUBLIC: int vs_discard __P((SCR *, SCR **));
340 vs_discard(SCR *sp, SCR **spp)
342 GS *gp;
343 SCR *tsp, **lp, *list[100];
344 jdir_t jdir;
346 gp = sp->gp;
349 * Save the old screen's cursor information.
351 * XXX
352 * If called after file_end(), and the underlying file was a tmp
353 * file, it may have gone away.
355 if (sp->frp != NULL) {
356 sp->frp->lno = sp->lno;
357 sp->frp->cno = sp->cno;
358 F_SET(sp->frp, FR_CURSORSET);
361 /* If no other screens to join, we're done. */
362 if (!IS_SPLIT(sp)) {
363 (void)gp->scr_discard(sp, NULL);
365 if (spp != NULL)
366 *spp = NULL;
367 return (0);
371 * Find a set of screens that cover one of the screen's borders.
372 * Check the vertical axis first, for no particular reason.
374 * XXX
375 * It's possible (I think?), to create a screen that shares no full
376 * border with any other set of screens, so we can't discard it. We
377 * just complain at the user until they clean it up.
379 if (vs_join(sp, list, &jdir))
380 return (1);
383 * Modify the affected screens. Redraw the modified screen(s) from
384 * scratch, setting a status line. If this is ever a performance
385 * problem we could play games with the map, but I wrote that code
386 * before and it was never clean or easy.
388 * Don't clean up the discarded screen's information. If the screen
389 * isn't exiting, we'll do the work when the user redisplays it.
391 switch (jdir) {
392 case HORIZ_FOLLOW:
393 case HORIZ_PRECEDE:
394 for (lp = &list[0]; (tsp = *lp) != NULL; ++lp) {
396 * Small screens: see vs_refresh.c section 6a. Adjust
397 * text line info, unless it's a small screen.
399 * Reset the length of the default scroll.
401 * Reset the map references.
403 tsp->rows += sp->rows;
404 if (!IS_SMALL(tsp))
405 tsp->t_rows = tsp->t_minrows = tsp->rows - 1;
406 tsp->t_maxrows = tsp->rows - 1;
408 tsp->defscroll = tsp->t_maxrows / 2;
410 *(_HMAP(tsp) + (tsp->t_rows - 1)) = *_TMAP(tsp);
411 _TMAP(tsp) = _HMAP(tsp) + (tsp->t_rows - 1);
413 switch (jdir) {
414 case HORIZ_FOLLOW:
415 tsp->roff = sp->roff;
416 vs_sm_fill(tsp, OOBLNO, P_TOP);
417 break;
418 case HORIZ_PRECEDE:
419 vs_sm_fill(tsp, OOBLNO, P_BOTTOM);
420 break;
421 default:
422 abort();
424 F_SET(tsp, SC_STATUS);
426 break;
427 case VERT_FOLLOW:
428 case VERT_PRECEDE:
429 for (lp = &list[0]; (tsp = *lp) != NULL; ++lp) {
430 if (jdir == VERT_FOLLOW)
431 tsp->coff = sp->coff;
432 tsp->cols += sp->cols + 1; /* XXX: DIVIDER */
433 vs_sm_fill(tsp, OOBLNO, P_TOP);
434 F_SET(tsp, SC_STATUS);
436 break;
437 default:
438 abort();
441 /* Find the closest screen that changed and move to it. */
442 tsp = list[0];
443 if (spp != NULL)
444 *spp = tsp;
446 /* Tell the display that we're discarding a screen. */
447 (void)gp->scr_discard(sp, list);
449 return (0);
453 * vs_join --
454 * Find a set of screens that covers a screen's border.
456 static int
457 vs_join(SCR *sp, SCR **listp, jdir_t *jdirp)
459 GS *gp;
460 WIN *wp;
461 SCR **lp, *tsp;
462 int first;
463 size_t tlen;
465 gp = sp->gp;
466 wp = sp->wp;
468 /* Check preceding vertical. */
469 for (lp = listp, tlen = sp->rows,
470 tsp = wp->scrq.cqh_first;
471 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next) {
472 if (sp == tsp)
473 continue;
474 /* Test if precedes the screen vertically. */
475 if (tsp->coff + tsp->cols + 1 != sp->coff)
476 continue;
478 * Test if a subset on the vertical axis. If overlaps the
479 * beginning or end, we can't join on this axis at all.
481 if (tsp->roff > sp->roff + sp->rows)
482 continue;
483 if (tsp->roff < sp->roff) {
484 if (tsp->roff + tsp->rows >= sp->roff)
485 break;
486 continue;
488 if (tsp->roff + tsp->rows > sp->roff + sp->rows)
489 break;
490 #ifdef DEBUG
491 if (tlen < tsp->rows)
492 abort();
493 #endif
494 tlen -= tsp->rows;
495 *lp++ = tsp;
497 if (tlen == 0) {
498 *lp = NULL;
499 *jdirp = VERT_PRECEDE;
500 return (0);
503 /* Check following vertical. */
504 for (lp = listp, tlen = sp->rows,
505 tsp = wp->scrq.cqh_first;
506 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next) {
507 if (sp == tsp)
508 continue;
509 /* Test if follows the screen vertically. */
510 if (tsp->coff != sp->coff + sp->cols + 1)
511 continue;
513 * Test if a subset on the vertical axis. If overlaps the
514 * beginning or end, we can't join on this axis at all.
516 if (tsp->roff > sp->roff + sp->rows)
517 continue;
518 if (tsp->roff < sp->roff) {
519 if (tsp->roff + tsp->rows >= sp->roff)
520 break;
521 continue;
523 if (tsp->roff + tsp->rows > sp->roff + sp->rows)
524 break;
525 #ifdef DEBUG
526 if (tlen < tsp->rows)
527 abort();
528 #endif
529 tlen -= tsp->rows;
530 *lp++ = tsp;
532 if (tlen == 0) {
533 *lp = NULL;
534 *jdirp = VERT_FOLLOW;
535 return (0);
538 /* Check preceding horizontal. */
539 for (first = 0, lp = listp, tlen = sp->cols,
540 tsp = wp->scrq.cqh_first;
541 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next) {
542 if (sp == tsp)
543 continue;
544 /* Test if precedes the screen horizontally. */
545 if (tsp->roff + tsp->rows != sp->roff)
546 continue;
548 * Test if a subset on the horizontal axis. If overlaps the
549 * beginning or end, we can't join on this axis at all.
551 if (tsp->coff > sp->coff + sp->cols)
552 continue;
553 if (tsp->coff < sp->coff) {
554 if (tsp->coff + tsp->cols >= sp->coff)
555 break;
556 continue;
558 if (tsp->coff + tsp->cols > sp->coff + sp->cols)
559 break;
560 #ifdef DEBUG
561 if (tlen < tsp->cols)
562 abort();
563 #endif
564 tlen -= tsp->cols + first;
565 first = 1;
566 *lp++ = tsp;
568 if (tlen == 0) {
569 *lp = NULL;
570 *jdirp = HORIZ_PRECEDE;
571 return (0);
574 /* Check following horizontal. */
575 for (first = 0, lp = listp, tlen = sp->cols,
576 tsp = wp->scrq.cqh_first;
577 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next) {
578 if (sp == tsp)
579 continue;
580 /* Test if precedes the screen horizontally. */
581 if (tsp->roff != sp->roff + sp->rows)
582 continue;
584 * Test if a subset on the horizontal axis. If overlaps the
585 * beginning or end, we can't join on this axis at all.
587 if (tsp->coff > sp->coff + sp->cols)
588 continue;
589 if (tsp->coff < sp->coff) {
590 if (tsp->coff + tsp->cols >= sp->coff)
591 break;
592 continue;
594 if (tsp->coff + tsp->cols > sp->coff + sp->cols)
595 break;
596 #ifdef DEBUG
597 if (tlen < tsp->cols)
598 abort();
599 #endif
600 tlen -= tsp->cols + first;
601 first = 1;
602 *lp++ = tsp;
604 if (tlen == 0) {
605 *lp = NULL;
606 *jdirp = HORIZ_FOLLOW;
607 return (0);
609 return (1);
613 * vs_fg --
614 * Background the current screen, and foreground a new one.
616 * PUBLIC: int vs_fg __P((SCR *, SCR **, CHAR_T *, int));
619 vs_fg(SCR *sp, SCR **nspp, CHAR_T *name, int newscreen)
621 GS *gp;
622 WIN *wp;
623 SCR *nsp;
624 char *np;
625 size_t nlen;
627 gp = sp->gp;
628 wp = sp->wp;
630 if (name)
631 INT2CHAR(sp, name, STRLEN(name) + 1, np, nlen);
632 else
633 np = NULL;
634 if (newscreen)
635 /* Get the specified background screen. */
636 nsp = vs_getbg(sp, np);
637 else
638 /* Swap screens. */
639 if (vs_swap(sp, &nsp, np))
640 return (1);
642 if ((*nspp = nsp) == NULL) {
643 msgq_wstr(sp, M_ERR, name,
644 name == NULL ?
645 "223|There are no background screens" :
646 "224|There's no background screen editing a file named %s");
647 return (1);
650 if (newscreen) {
651 /* Remove the new screen from the background queue. */
652 CIRCLEQ_REMOVE(&gp->hq, nsp, q);
654 /* Split the screen; if we fail, hook the screen back in. */
655 if (vs_split(sp, nsp, 0)) {
656 CIRCLEQ_INSERT_TAIL(&gp->hq, nsp, q);
657 return (1);
659 } else {
660 /* Move the old screen to the background queue. */
661 CIRCLEQ_REMOVE(&wp->scrq, sp, q);
662 CIRCLEQ_INSERT_TAIL(&gp->hq, sp, q);
664 return (0);
668 * vs_bg --
669 * Background the screen, and switch to the next one.
671 * PUBLIC: int vs_bg __P((SCR *));
674 vs_bg(SCR *sp)
676 GS *gp;
677 WIN *wp;
678 SCR *nsp;
680 gp = sp->gp;
681 wp = sp->wp;
683 /* Try and join with another screen. */
684 if (vs_discard(sp, &nsp))
685 return (1);
686 if (nsp == NULL) {
687 msgq(sp, M_ERR,
688 "225|You may not background your only displayed screen");
689 return (1);
692 /* Move the old screen to the background queue. */
693 CIRCLEQ_REMOVE(&wp->scrq, sp, q);
694 CIRCLEQ_INSERT_TAIL(&gp->hq, sp, q);
696 /* Toss the screen map. */
697 free(_HMAP(sp));
698 _HMAP(sp) = NULL;
700 /* Switch screens. */
701 sp->nextdisp = nsp;
702 F_SET(sp, SC_SSWITCH);
704 return (0);
708 * vs_swap --
709 * Swap the current screen with a backgrounded one.
711 * PUBLIC: int vs_swap __P((SCR *, SCR **, char *));
714 vs_swap(SCR *sp, SCR **nspp, char *name)
716 GS *gp;
717 WIN *wp;
718 SCR *nsp, *list[2];
720 gp = sp->gp;
721 wp = sp->wp;
723 /* Get the specified background screen. */
724 if ((*nspp = nsp = vs_getbg(sp, name)) == NULL)
725 return (0);
728 * Save the old screen's cursor information.
730 * XXX
731 * If called after file_end(), and the underlying file was a tmp
732 * file, it may have gone away.
734 if (sp->frp != NULL) {
735 sp->frp->lno = sp->lno;
736 sp->frp->cno = sp->cno;
737 F_SET(sp->frp, FR_CURSORSET);
740 /* Switch screens. */
741 sp->nextdisp = nsp;
742 F_SET(sp, SC_SSWITCH);
744 /* Initialize terminal information. */
745 VIP(nsp)->srows = VIP(sp)->srows;
747 /* Initialize screen information. */
748 nsp->cols = sp->cols;
749 nsp->rows = sp->rows; /* XXX: Only place in vi that sets rows. */
750 nsp->roff = sp->roff;
753 * Small screens: see vs_refresh.c, section 6a.
755 * The new screens may have different screen options sizes than the
756 * old one, so use them. Make sure that text counts aren't larger
757 * than the new screen sizes.
759 if (IS_SMALL(nsp)) {
760 nsp->t_minrows = nsp->t_rows = O_VAL(nsp, O_WINDOW);
761 if (nsp->t_rows > sp->t_maxrows)
762 nsp->t_rows = nsp->t_maxrows;
763 if (nsp->t_minrows > sp->t_maxrows)
764 nsp->t_minrows = nsp->t_maxrows;
765 } else
766 nsp->t_rows = nsp->t_maxrows = nsp->t_minrows = nsp->rows - 1;
768 /* Reset the length of the default scroll. */
769 nsp->defscroll = nsp->t_maxrows / 2;
771 /* Allocate a new screen map. */
772 CALLOC_RET(nsp, _HMAP(nsp), SMAP *, SIZE_HMAP(nsp), sizeof(SMAP));
773 _TMAP(nsp) = _HMAP(nsp) + (nsp->t_rows - 1);
775 /* Fill the map. */
776 nsp->wp = sp->wp;
777 if (vs_sm_fill(nsp, nsp->lno, P_FILL))
778 return (1);
781 * The new screen replaces the old screen in the parent/child list.
782 * We insert the new screen after the old one. If we're exiting,
783 * the exit will delete the old one, if we're foregrounding, the fg
784 * code will move the old one to the background queue.
786 CIRCLEQ_REMOVE(&gp->hq, nsp, q);
787 CIRCLEQ_INSERT_AFTER(&wp->scrq, sp, nsp, q);
790 * Don't change the screen's cursor information other than to
791 * note that the cursor is wrong.
793 F_SET(VIP(nsp), VIP_CUR_INVALID);
795 /* Draw the new screen from scratch, and add a status line. */
796 F_SET(nsp, SC_SCR_REDRAW | SC_STATUS);
798 list[0] = nsp; list[1] = NULL;
799 (void)gp->scr_discard(sp, list);
801 return (0);
805 * vs_resize --
806 * Change the absolute size of the current screen.
808 * PUBLIC: int vs_resize __P((SCR *, long, adj_t));
811 vs_resize(SCR *sp, long int count, adj_t adj)
813 GS *gp;
814 WIN *wp;
815 SCR *g, *s, *prev, *next, *list[3] = {NULL, NULL, NULL};
816 size_t g_off, s_off;
818 gp = sp->gp;
819 wp = sp->wp;
822 * Figure out which screens will grow, which will shrink, and
823 * make sure it's possible.
825 if (count == 0)
826 return (0);
827 if (adj == A_SET) {
828 if (sp->t_maxrows == count)
829 return (0);
830 if (sp->t_maxrows > count) {
831 adj = A_DECREASE;
832 count = sp->t_maxrows - count;
833 } else {
834 adj = A_INCREASE;
835 count = count - sp->t_maxrows;
839 /* Find first overlapping screen */
840 for (next = sp->q.cqe_next;
841 next != (void *)&wp->scrq &&
842 (next->coff >= sp->coff + sp->cols ||
843 next->coff + next->cols <= sp->coff);
844 next = next->q.cqe_next);
845 /* See if we can use it */
846 if (next != (void *)&wp->scrq &&
847 (sp->coff != next->coff || sp->cols != next->cols))
848 next = (void *)&wp->scrq;
849 for (prev = sp->q.cqe_prev;
850 prev != (void *)&wp->scrq &&
851 (prev->coff >= sp->coff + sp->cols ||
852 prev->coff + prev->cols <= sp->coff);
853 prev = prev->q.cqe_prev);
854 if (prev != (void *)&wp->scrq &&
855 (sp->coff != prev->coff || sp->cols != prev->cols))
856 prev = (void *)&wp->scrq;
858 g_off = s_off = 0;
859 if (adj == A_DECREASE) {
860 if (count < 0)
861 count = -count;
862 s = sp;
863 if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count)
864 goto toosmall;
865 if ((g = prev) == (void *)&wp->scrq) {
866 if ((g = next) == (void *)&wp->scrq)
867 goto toobig;
868 g_off = -count;
869 } else
870 s_off = count;
871 } else {
872 g = sp;
873 if ((s = next) != (void *)&wp->scrq &&
874 s->t_maxrows >= MINIMUM_SCREEN_ROWS + count)
875 s_off = count;
876 else
877 s = NULL;
878 if (s == NULL) {
879 if ((s = prev) == (void *)&wp->scrq) {
880 toobig: msgq(sp, M_BERR, adj == A_DECREASE ?
881 "227|The screen cannot shrink" :
882 "228|The screen cannot grow");
883 return (1);
885 if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count) {
886 toosmall: msgq(sp, M_BERR,
887 "226|The screen can only shrink to %d rows",
888 MINIMUM_SCREEN_ROWS);
889 return (1);
891 g_off = -count;
896 * Fix up the screens; we could optimize the reformatting of the
897 * screen, but this isn't likely to be a common enough operation
898 * to make it worthwhile.
900 s->rows += -count;
901 s->roff += s_off;
902 g->rows += count;
903 g->roff += g_off;
905 g->t_rows += count;
906 if (g->t_minrows == g->t_maxrows)
907 g->t_minrows += count;
908 g->t_maxrows += count;
909 _TMAP(g) += count;
910 F_SET(g, SC_SCR_REFORMAT | SC_STATUS);
912 s->t_rows -= count;
913 s->t_maxrows -= count;
914 if (s->t_minrows > s->t_maxrows)
915 s->t_minrows = s->t_maxrows;
916 _TMAP(s) -= count;
917 F_SET(s, SC_SCR_REFORMAT | SC_STATUS);
919 /* XXXX */
920 list[0] = g; list[1] = s;
921 gp->scr_discard(0, list);
923 return (0);
927 * vs_getbg --
928 * Get the specified background screen, or, if name is NULL, the first
929 * background screen.
931 static SCR *
932 vs_getbg(SCR *sp, char *name)
934 GS *gp;
935 SCR *nsp;
936 char *p;
938 gp = sp->gp;
940 /* If name is NULL, return the first background screen on the list. */
941 if (name == NULL) {
942 nsp = gp->hq.cqh_first;
943 return (nsp == (void *)&gp->hq ? NULL : nsp);
946 /* Search for a full match. */
947 for (nsp = gp->hq.cqh_first;
948 nsp != (void *)&gp->hq; nsp = nsp->q.cqe_next)
949 if (!strcmp(nsp->frp->name, name))
950 break;
951 if (nsp != (void *)&gp->hq)
952 return (nsp);
954 /* Search for a last-component match. */
955 for (nsp = gp->hq.cqh_first;
956 nsp != (void *)&gp->hq; nsp = nsp->q.cqe_next) {
957 if ((p = strrchr(nsp->frp->name, '/')) == NULL)
958 p = nsp->frp->name;
959 else
960 ++p;
961 if (!strcmp(p, name))
962 break;
964 if (nsp != (void *)&gp->hq)
965 return (nsp);
967 return (NULL);