the script used to extract a release
[nvi.git] / vi / vs_split.c
blob1d03f7e5c698538d16a03397f5451daec3b31c9f
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.39 2000/07/17 18:53:35 skimo Exp $ (Berkeley) $Date: 2000/07/17 18:53:35 $";
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(sp, new, ccl)
44 SCR *sp, *new;
45 int ccl; /* 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(sp, new)
208 SCR *sp, *new;
210 GS *gp;
211 size_t cols;
213 gp = sp->gp;
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);
223 /* Wait for any messages in the screen. */
224 vs_resolve(sp, NULL, 1);
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;
235 * Split the screen in half; we have to sacrifice a column to delimit
236 * the screens.
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.
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.
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;
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);
262 /* Fit the screen into the logical chain. */
263 vs_insert(new, sp->wp);
265 /* Tell the display that we're splitting. */
266 (void)gp->scr_split(sp, new);
268 /* Redraw the old screen from scratch. */
269 F_SET(sp, SC_SCR_REFORMAT | SC_STATUS);
272 * Initialize the screen flags:
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:
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
282 * Things go downhill at this point.
284 * Draw the new screen from scratch, and add a status line.
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);
293 * vs_insert --
294 * Insert the new screen into the correct place in the logical
295 * chain.
297 static void
298 vs_insert(SCR *sp, WIN *wp)
300 GS *gp;
301 SCR *tsp;
303 gp = sp->gp;
305 sp->wp = wp;
307 /* Move past all screens with lower row numbers. */
308 for (tsp = wp->scrq.cqh_first;
309 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
310 if (tsp->roff >= sp->roff)
311 break;
313 * Move past all screens with the same row number and lower
314 * column numbers.
316 for (; tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
317 if (tsp->roff != sp->roff || tsp->coff > sp->coff)
318 break;
321 * If we reached the end, this screen goes there. Otherwise,
322 * put it before or after the screen where we stopped.
324 if (tsp == (void *)&wp->scrq) {
325 CIRCLEQ_INSERT_TAIL(&wp->scrq, sp, q);
326 } else if (tsp->roff < sp->roff ||
327 (tsp->roff == sp->roff && tsp->coff < sp->coff)) {
328 CIRCLEQ_INSERT_AFTER(&wp->scrq, tsp, sp, q);
329 } else
330 CIRCLEQ_INSERT_BEFORE(&wp->scrq, tsp, sp, q);
334 * vs_discard --
335 * Discard the screen, folding the real-estate into a related screen,
336 * if one exists, and return that screen.
338 * PUBLIC: int vs_discard __P((SCR *, SCR **));
341 vs_discard(sp, spp)
342 SCR *sp, **spp;
344 GS *gp;
345 SCR *tsp, **lp, *list[100];
346 jdir_t jdir;
348 gp = sp->gp;
351 * Save the old screen's cursor information.
353 * XXX
354 * If called after file_end(), and the underlying file was a tmp
355 * file, it may have gone away.
357 if (sp->frp != NULL) {
358 sp->frp->lno = sp->lno;
359 sp->frp->cno = sp->cno;
360 F_SET(sp->frp, FR_CURSORSET);
363 /* If no other screens to join, we're done. */
364 if (!IS_SPLIT(sp)) {
365 (void)gp->scr_discard(sp, NULL);
367 if (spp != NULL)
368 *spp = NULL;
369 return (0);
373 * Find a set of screens that cover one of the screen's borders.
374 * Check the vertical axis first, for no particular reason.
376 * XXX
377 * It's possible (I think?), to create a screen that shares no full
378 * border with any other set of screens, so we can't discard it. We
379 * just complain at the user until they clean it up.
381 if (vs_join(sp, list, &jdir))
382 return (1);
385 * Modify the affected screens. Redraw the modified screen(s) from
386 * scratch, setting a status line. If this is ever a performance
387 * problem we could play games with the map, but I wrote that code
388 * before and it was never clean or easy.
390 * Don't clean up the discarded screen's information. If the screen
391 * isn't exiting, we'll do the work when the user redisplays it.
393 switch (jdir) {
394 case HORIZ_FOLLOW:
395 case HORIZ_PRECEDE:
396 for (lp = &list[0]; (tsp = *lp) != NULL; ++lp) {
398 * Small screens: see vs_refresh.c section 6a. Adjust
399 * text line info, unless it's a small screen.
401 * Reset the length of the default scroll.
403 * Reset the map references.
405 tsp->rows += sp->rows;
406 if (!IS_SMALL(tsp))
407 tsp->t_rows = tsp->t_minrows = tsp->rows - 1;
408 tsp->t_maxrows = tsp->rows - 1;
410 tsp->defscroll = tsp->t_maxrows / 2;
412 *(_HMAP(tsp) + (tsp->t_rows - 1)) = *_TMAP(tsp);
413 _TMAP(tsp) = _HMAP(tsp) + (tsp->t_rows - 1);
415 switch (jdir) {
416 case HORIZ_FOLLOW:
417 tsp->roff = sp->roff;
418 vs_sm_fill(tsp, OOBLNO, P_TOP);
419 break;
420 case HORIZ_PRECEDE:
421 vs_sm_fill(tsp, OOBLNO, P_BOTTOM);
422 break;
423 default:
424 abort();
426 F_SET(tsp, SC_STATUS);
428 break;
429 case VERT_FOLLOW:
430 case VERT_PRECEDE:
431 for (lp = &list[0]; (tsp = *lp) != NULL; ++lp) {
432 if (jdir == VERT_FOLLOW)
433 tsp->coff = sp->coff;
434 tsp->cols += sp->cols + 1; /* XXX: DIVIDER */
435 vs_sm_fill(tsp, OOBLNO, P_TOP);
436 F_SET(tsp, SC_STATUS);
438 break;
439 default:
440 abort();
443 /* Find the closest screen that changed and move to it. */
444 tsp = list[0];
445 if (spp != NULL)
446 *spp = tsp;
448 /* Tell the display that we're discarding a screen. */
449 (void)gp->scr_discard(sp, list);
451 return (0);
455 * vs_join --
456 * Find a set of screens that covers a screen's border.
458 static int
459 vs_join(sp, listp, jdirp)
460 SCR *sp, **listp;
461 jdir_t *jdirp;
463 GS *gp;
464 WIN *wp;
465 SCR **lp, *tsp;
466 int first;
467 size_t tlen;
469 gp = sp->gp;
470 wp = sp->wp;
472 /* Check preceding vertical. */
473 for (lp = listp, tlen = sp->rows,
474 tsp = wp->scrq.cqh_first;
475 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next) {
476 if (sp == tsp)
477 continue;
478 /* Test if precedes the screen vertically. */
479 if (tsp->coff + tsp->cols + 1 != sp->coff)
480 continue;
482 * Test if a subset on the vertical axis. If overlaps the
483 * beginning or end, we can't join on this axis at all.
485 if (tsp->roff > sp->roff + sp->rows)
486 continue;
487 if (tsp->roff < sp->roff) {
488 if (tsp->roff + tsp->rows >= sp->roff)
489 break;
490 continue;
492 if (tsp->roff + tsp->rows > sp->roff + sp->rows)
493 break;
494 #ifdef DEBUG
495 if (tlen < tsp->rows)
496 abort();
497 #endif
498 tlen -= tsp->rows;
499 *lp++ = tsp;
501 if (tlen == 0) {
502 *lp = NULL;
503 *jdirp = VERT_PRECEDE;
504 return (0);
507 /* Check following vertical. */
508 for (lp = listp, tlen = sp->rows,
509 tsp = wp->scrq.cqh_first;
510 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next) {
511 if (sp == tsp)
512 continue;
513 /* Test if follows the screen vertically. */
514 if (tsp->coff != sp->coff + sp->cols + 1)
515 continue;
517 * Test if a subset on the vertical axis. If overlaps the
518 * beginning or end, we can't join on this axis at all.
520 if (tsp->roff > sp->roff + sp->rows)
521 continue;
522 if (tsp->roff < sp->roff) {
523 if (tsp->roff + tsp->rows >= sp->roff)
524 break;
525 continue;
527 if (tsp->roff + tsp->rows > sp->roff + sp->rows)
528 break;
529 #ifdef DEBUG
530 if (tlen < tsp->rows)
531 abort();
532 #endif
533 tlen -= tsp->rows;
534 *lp++ = tsp;
536 if (tlen == 0) {
537 *lp = NULL;
538 *jdirp = VERT_FOLLOW;
539 return (0);
542 /* Check preceding horizontal. */
543 for (first = 0, lp = listp, tlen = sp->cols,
544 tsp = wp->scrq.cqh_first;
545 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next) {
546 if (sp == tsp)
547 continue;
548 /* Test if precedes the screen horizontally. */
549 if (tsp->roff + tsp->rows != sp->roff)
550 continue;
552 * Test if a subset on the horizontal axis. If overlaps the
553 * beginning or end, we can't join on this axis at all.
555 if (tsp->coff > sp->coff + sp->cols)
556 continue;
557 if (tsp->coff < sp->coff) {
558 if (tsp->coff + tsp->cols >= sp->coff)
559 break;
560 continue;
562 if (tsp->coff + tsp->cols > sp->coff + sp->cols)
563 break;
564 #ifdef DEBUG
565 if (tlen < tsp->cols)
566 abort();
567 #endif
568 tlen -= tsp->cols + first;
569 first = 1;
570 *lp++ = tsp;
572 if (tlen == 0) {
573 *lp = NULL;
574 *jdirp = HORIZ_PRECEDE;
575 return (0);
578 /* Check following horizontal. */
579 for (first = 0, lp = listp, tlen = sp->cols,
580 tsp = wp->scrq.cqh_first;
581 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next) {
582 if (sp == tsp)
583 continue;
584 /* Test if precedes the screen horizontally. */
585 if (tsp->roff != sp->roff + sp->rows)
586 continue;
588 * Test if a subset on the horizontal axis. If overlaps the
589 * beginning or end, we can't join on this axis at all.
591 if (tsp->coff > sp->coff + sp->cols)
592 continue;
593 if (tsp->coff < sp->coff) {
594 if (tsp->coff + tsp->cols >= sp->coff)
595 break;
596 continue;
598 if (tsp->coff + tsp->cols > sp->coff + sp->cols)
599 break;
600 #ifdef DEBUG
601 if (tlen < tsp->cols)
602 abort();
603 #endif
604 tlen -= tsp->cols + first;
605 first = 1;
606 *lp++ = tsp;
608 if (tlen == 0) {
609 *lp = NULL;
610 *jdirp = HORIZ_FOLLOW;
611 return (0);
613 return (1);
617 * vs_fg --
618 * Background the current screen, and foreground a new one.
620 * PUBLIC: int vs_fg __P((SCR *, SCR **, CHAR_T *, int));
623 vs_fg(sp, nspp, name, newscreen)
624 SCR *sp, **nspp;
625 CHAR_T *name;
626 int newscreen;
628 GS *gp;
629 WIN *wp;
630 SCR *nsp;
631 char *np;
632 size_t nlen;
634 gp = sp->gp;
635 wp = sp->wp;
637 if (name)
638 INT2CHAR(sp, name, v_strlen(name) + 1, np, nlen);
639 else
640 np = NULL;
641 if (newscreen)
642 /* Get the specified background screen. */
643 nsp = vs_getbg(sp, np);
644 else
645 /* Swap screens. */
646 if (vs_swap(sp, &nsp, np))
647 return (1);
649 if ((*nspp = nsp) == NULL) {
650 msgq_wstr(sp, M_ERR, name,
651 name == NULL ?
652 "223|There are no background screens" :
653 "224|There's no background screen editing a file named %s");
654 return (1);
657 if (newscreen) {
658 /* Remove the new screen from the background queue. */
659 CIRCLEQ_REMOVE(&gp->hq, nsp, q);
661 /* Split the screen; if we fail, hook the screen back in. */
662 if (vs_split(sp, nsp, 0)) {
663 CIRCLEQ_INSERT_TAIL(&gp->hq, nsp, q);
664 return (1);
666 } else {
667 /* Move the old screen to the background queue. */
668 CIRCLEQ_REMOVE(&wp->scrq, sp, q);
669 CIRCLEQ_INSERT_TAIL(&gp->hq, sp, q);
670 sp->wp = 0;
672 return (0);
676 * vs_bg --
677 * Background the screen, and switch to the next one.
679 * PUBLIC: int vs_bg __P((SCR *));
682 vs_bg(sp)
683 SCR *sp;
685 GS *gp;
686 WIN *wp;
687 SCR *nsp;
689 gp = sp->gp;
690 wp = sp->wp;
692 /* Try and join with another screen. */
693 if (vs_discard(sp, &nsp))
694 return (1);
695 if (nsp == NULL) {
696 msgq(sp, M_ERR,
697 "225|You may not background your only displayed screen");
698 return (1);
701 /* Move the old screen to the background queue. */
702 CIRCLEQ_REMOVE(&wp->scrq, sp, q);
703 CIRCLEQ_INSERT_TAIL(&gp->hq, sp, q);
704 sp->wp = 0;
706 /* Toss the screen map. */
707 free(_HMAP(sp));
708 _HMAP(sp) = NULL;
710 /* Switch screens. */
711 sp->nextdisp = nsp;
712 F_SET(sp, SC_SSWITCH);
714 return (0);
718 * vs_swap --
719 * Swap the current screen with a backgrounded one.
721 * PUBLIC: int vs_swap __P((SCR *, SCR **, char *));
724 vs_swap(sp, nspp, name)
725 SCR *sp, **nspp;
726 char *name;
728 GS *gp;
729 WIN *wp;
730 SCR *nsp, *list[2];
732 gp = sp->gp;
733 wp = sp->wp;
735 /* Get the specified background screen. */
736 if ((*nspp = nsp = vs_getbg(sp, name)) == NULL)
737 return (0);
740 * Save the old screen's cursor information.
742 * XXX
743 * If called after file_end(), and the underlying file was a tmp
744 * file, it may have gone away.
746 if (sp->frp != NULL) {
747 sp->frp->lno = sp->lno;
748 sp->frp->cno = sp->cno;
749 F_SET(sp->frp, FR_CURSORSET);
752 /* Switch screens. */
753 sp->nextdisp = nsp;
754 F_SET(sp, SC_SSWITCH);
756 /* Initialize terminal information. */
757 VIP(nsp)->srows = VIP(sp)->srows;
759 /* Initialize screen information. */
760 nsp->cols = sp->cols;
761 nsp->rows = sp->rows; /* XXX: Only place in vi that sets rows. */
762 nsp->roff = sp->roff;
765 * Small screens: see vs_refresh.c, section 6a.
767 * The new screens may have different screen options sizes than the
768 * old one, so use them. Make sure that text counts aren't larger
769 * than the new screen sizes.
771 if (IS_SMALL(nsp)) {
772 nsp->t_minrows = nsp->t_rows = O_VAL(nsp, O_WINDOW);
773 if (nsp->t_rows > sp->t_maxrows)
774 nsp->t_rows = nsp->t_maxrows;
775 if (nsp->t_minrows > sp->t_maxrows)
776 nsp->t_minrows = nsp->t_maxrows;
777 } else
778 nsp->t_rows = nsp->t_maxrows = nsp->t_minrows = nsp->rows - 1;
780 /* Reset the length of the default scroll. */
781 nsp->defscroll = nsp->t_maxrows / 2;
783 /* Allocate a new screen map. */
784 CALLOC_RET(nsp, _HMAP(nsp), SMAP *, SIZE_HMAP(nsp), sizeof(SMAP));
785 _TMAP(nsp) = _HMAP(nsp) + (nsp->t_rows - 1);
787 /* Fill the map. */
788 if (vs_sm_fill(nsp, nsp->lno, P_FILL))
789 return (1);
792 * The new screen replaces the old screen in the parent/child list.
793 * We insert the new screen after the old one. If we're exiting,
794 * the exit will delete the old one, if we're foregrounding, the fg
795 * code will move the old one to the background queue.
797 CIRCLEQ_REMOVE(&gp->hq, nsp, q);
798 CIRCLEQ_INSERT_AFTER(&wp->scrq, sp, nsp, q);
799 nsp->wp = sp->wp;
802 * Don't change the screen's cursor information other than to
803 * note that the cursor is wrong.
805 F_SET(VIP(nsp), VIP_CUR_INVALID);
807 /* Draw the new screen from scratch, and add a status line. */
808 F_SET(nsp, SC_SCR_REDRAW | SC_STATUS);
810 list[0] = nsp; list[1] = NULL;
811 (void)gp->scr_discard(sp, list);
813 return (0);
817 * vs_resize --
818 * Change the absolute size of the current screen.
820 * PUBLIC: int vs_resize __P((SCR *, long, adj_t));
823 vs_resize(sp, count, adj)
824 SCR *sp;
825 long count;
826 adj_t adj;
828 GS *gp;
829 WIN *wp;
830 SCR *g, *s, *prev, *next, *list[3] = {NULL, NULL, NULL};
831 size_t g_off, s_off;
833 gp = sp->gp;
834 wp = sp->wp;
837 * Figure out which screens will grow, which will shrink, and
838 * make sure it's possible.
840 if (count == 0)
841 return (0);
842 if (adj == A_SET) {
843 if (sp->t_maxrows == count)
844 return (0);
845 if (sp->t_maxrows > count) {
846 adj = A_DECREASE;
847 count = sp->t_maxrows - count;
848 } else {
849 adj = A_INCREASE;
850 count = count - sp->t_maxrows;
854 /* Find first overlapping screen */
855 for (next = sp->q.cqe_next;
856 next != (void *)&wp->scrq &&
857 (next->coff >= sp->coff + sp->cols ||
858 next->coff + next->cols <= sp->coff);
859 next = next->q.cqe_next);
860 /* See if we can use it */
861 if (next != (void *)&wp->scrq &&
862 (sp->coff != next->coff || sp->cols != next->cols))
863 next = (void *)&wp->scrq;
864 for (prev = sp->q.cqe_prev;
865 prev != (void *)&wp->scrq &&
866 (prev->coff >= sp->coff + sp->cols ||
867 prev->coff + prev->cols <= sp->coff);
868 prev = prev->q.cqe_prev);
869 if (prev != (void *)&wp->scrq &&
870 (sp->coff != prev->coff || sp->cols != prev->cols))
871 prev = (void *)&wp->scrq;
873 g_off = s_off = 0;
874 if (adj == A_DECREASE) {
875 if (count < 0)
876 count = -count;
877 s = sp;
878 if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count)
879 goto toosmall;
880 if ((g = prev) == (void *)&wp->scrq) {
881 if ((g = next) == (void *)&wp->scrq)
882 goto toobig;
883 g_off = -count;
884 } else
885 s_off = count;
886 } else {
887 g = sp;
888 if ((s = next) != (void *)&wp->scrq &&
889 s->t_maxrows >= MINIMUM_SCREEN_ROWS + count)
890 s_off = count;
891 else
892 s = NULL;
893 if (s == NULL) {
894 if ((s = prev) == (void *)&wp->scrq) {
895 toobig: msgq(sp, M_BERR, adj == A_DECREASE ?
896 "227|The screen cannot shrink" :
897 "228|The screen cannot grow");
898 return (1);
900 if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count) {
901 toosmall: msgq(sp, M_BERR,
902 "226|The screen can only shrink to %d rows",
903 MINIMUM_SCREEN_ROWS);
904 return (1);
906 g_off = -count;
911 * Fix up the screens; we could optimize the reformatting of the
912 * screen, but this isn't likely to be a common enough operation
913 * to make it worthwhile.
915 s->rows += -count;
916 s->roff += s_off;
917 g->rows += count;
918 g->roff += g_off;
920 g->t_rows += count;
921 if (g->t_minrows == g->t_maxrows)
922 g->t_minrows += count;
923 g->t_maxrows += count;
924 _TMAP(g) += count;
925 F_SET(g, SC_SCR_REFORMAT | SC_STATUS);
927 s->t_rows -= count;
928 s->t_maxrows -= count;
929 if (s->t_minrows > s->t_maxrows)
930 s->t_minrows = s->t_maxrows;
931 _TMAP(s) -= count;
932 F_SET(s, SC_SCR_REFORMAT | SC_STATUS);
934 /* XXXX */
935 list[0] = g; list[1] = s;
936 gp->scr_discard(0, list);
938 return (0);
942 * vs_getbg --
943 * Get the specified background screen, or, if name is NULL, the first
944 * background screen.
946 static SCR *
947 vs_getbg(sp, name)
948 SCR *sp;
949 char *name;
951 GS *gp;
952 SCR *nsp;
953 char *p;
955 gp = sp->gp;
957 /* If name is NULL, return the first background screen on the list. */
958 if (name == NULL) {
959 nsp = gp->hq.cqh_first;
960 return (nsp == (void *)&gp->hq ? NULL : nsp);
963 /* Search for a full match. */
964 for (nsp = gp->hq.cqh_first;
965 nsp != (void *)&gp->hq; nsp = nsp->q.cqe_next)
966 if (!strcmp(nsp->frp->name, name))
967 break;
968 if (nsp != (void *)&gp->hq)
969 return (nsp);
971 /* Search for a last-component match. */
972 for (nsp = gp->hq.cqh_first;
973 nsp != (void *)&gp->hq; nsp = nsp->q.cqe_next) {
974 if ((p = strrchr(nsp->frp->name, '/')) == NULL)
975 p = nsp->frp->name;
976 else
977 ++p;
978 if (!strcmp(p, name))
979 break;
981 if (nsp != (void *)&gp->hq)
982 return (nsp);
984 return (NULL);