less(1): Regenerate defines.h and update Makefile
[dragonfly.git] / contrib / nvi2 / vi / vs_split.c
blobf1c9f8cd1dd6ab2689695d1c39545b601523a5ce
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 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
16 #include <bitstring.h>
17 #include <errno.h>
18 #include <limits.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "../common/common.h"
24 #include "vi.h"
26 typedef enum { HORIZ_FOLLOW, HORIZ_PRECEDE, VERT_FOLLOW, VERT_PRECEDE } jdir_t;
28 static SCR *vs_getbg(SCR *, char *);
29 static void vs_insert(SCR *sp, GS *gp);
30 static int vs_join(SCR *, SCR **, jdir_t *);
33 * vs_split --
34 * Create a new screen, horizontally.
36 * PUBLIC: int vs_split(SCR *, SCR *, int);
38 int
39 vs_split(
40 SCR *sp,
41 SCR *new,
42 int ccl) /* Colon-command line split. */
44 GS *gp;
45 SMAP *smp;
46 size_t half;
47 int issmallscreen, splitup;
49 gp = sp->gp;
51 /* Check to see if it's possible. */
52 /* XXX: The IS_ONELINE fix will change this, too. */
53 if (sp->rows < 4) {
54 msgq(sp, M_ERR,
55 "222|Screen must be larger than %d lines to split", 4 - 1);
56 return (1);
59 /* Wait for any messages in the screen. */
60 vs_resolve(sp, NULL, 1);
62 /* Get a new screen map. */
63 CALLOC(sp, _HMAP(new), SIZE_HMAP(sp), sizeof(SMAP));
64 if (_HMAP(new) == NULL)
65 return (1);
66 _HMAP(new)->lno = sp->lno;
67 _HMAP(new)->coff = 0;
68 _HMAP(new)->soff = 1;
70 /* Split the screen in half. */
71 half = sp->rows / 2;
72 if (ccl && half > 6)
73 half = 6;
76 * Small screens: see vs_refresh.c section 6a. Set a flag so
77 * we know to fix the screen up later.
79 issmallscreen = IS_SMALL(sp);
81 /* The columns in the screen don't change. */
82 new->coff = sp->coff;
83 new->cols = sp->cols;
86 * Split the screen, and link the screens together. If creating a
87 * screen to edit the colon command line or the cursor is in the top
88 * half of the current screen, the new screen goes under the current
89 * screen. Else, it goes above the current screen.
91 * Recalculate current cursor position based on sp->lno, we're called
92 * with the cursor on the colon command line. Then split the screen
93 * in half and update the shared information.
95 splitup =
96 !ccl && (vs_sm_cursor(sp, &smp) ? 0 : (smp - HMAP) + 1) >= half;
97 if (splitup) { /* Old is bottom half. */
98 new->rows = sp->rows - half; /* New. */
99 new->roff = sp->roff;
100 sp->rows = half; /* Old. */
101 sp->roff += new->rows;
104 * If the parent is the bottom half of the screen, shift
105 * the map down to match on-screen text.
107 memcpy(_HMAP(sp), _HMAP(sp) + new->rows,
108 (sp->t_maxrows - new->rows) * sizeof(SMAP));
109 } else { /* Old is top half. */
110 new->rows = half; /* New. */
111 sp->rows -= half; /* Old. */
112 new->roff = sp->roff + sp->rows;
115 /* Adjust maximum text count. */
116 sp->t_maxrows = IS_ONELINE(sp) ? 1 : sp->rows - 1;
117 new->t_maxrows = IS_ONELINE(new) ? 1 : new->rows - 1;
120 * Small screens: see vs_refresh.c, section 6a.
122 * The child may have different screen options sizes than the parent,
123 * so use them. Guarantee that text counts aren't larger than the
124 * new screen sizes.
126 if (issmallscreen) {
127 /* Fix the text line count for the parent. */
128 if (splitup)
129 sp->t_rows -= new->rows;
131 /* Fix the parent screen. */
132 if (sp->t_rows > sp->t_maxrows)
133 sp->t_rows = sp->t_maxrows;
134 if (sp->t_minrows > sp->t_maxrows)
135 sp->t_minrows = sp->t_maxrows;
137 /* Fix the child screen. */
138 new->t_minrows = new->t_rows = O_VAL(sp, O_WINDOW);
139 if (new->t_rows > new->t_maxrows)
140 new->t_rows = new->t_maxrows;
141 if (new->t_minrows > new->t_maxrows)
142 new->t_minrows = new->t_maxrows;
143 } else {
144 sp->t_minrows = sp->t_rows = IS_ONELINE(sp) ? 1 : sp->rows - 1;
147 * The new screen may be a small screen, even if the parent
148 * was not. Don't complain if O_WINDOW is too large, we're
149 * splitting the screen so the screen is much smaller than
150 * normal.
152 new->t_minrows = new->t_rows = O_VAL(sp, O_WINDOW);
153 if (new->t_rows > new->rows - 1)
154 new->t_minrows = new->t_rows =
155 IS_ONELINE(new) ? 1 : new->rows - 1;
158 /* Adjust the ends of the new and old maps. */
159 _TMAP(sp) = IS_ONELINE(sp) ?
160 _HMAP(sp) : _HMAP(sp) + (sp->t_rows - 1);
161 _TMAP(new) = IS_ONELINE(new) ?
162 _HMAP(new) : _HMAP(new) + (new->t_rows - 1);
164 /* Reset the length of the default scroll. */
165 if ((sp->defscroll = sp->t_maxrows / 2) == 0)
166 sp->defscroll = 1;
167 if ((new->defscroll = new->t_maxrows / 2) == 0)
168 new->defscroll = 1;
170 /* Fit the screen into the logical chain. */
171 vs_insert(new, sp->gp);
173 /* Tell the display that we're splitting. */
174 (void)gp->scr_split(sp, new);
177 * Initialize the screen flags:
179 * If we're in vi mode in one screen, we don't have to reinitialize.
180 * This isn't just a cosmetic fix. The path goes like this:
182 * return into vi(), SC_SSWITCH set
183 * call vs_refresh() with SC_STATUS set
184 * call vs_resolve to display the status message
185 * call vs_refresh() because the SC_SCR_VI bit isn't set
187 * Things go downhill at this point.
189 * Draw the new screen from scratch, and add a status line.
191 F_SET(new,
192 SC_SCR_REFORMAT | SC_STATUS |
193 F_ISSET(sp, SC_EX | SC_VI | SC_SCR_VI | SC_SCR_EX | SC_READONLY));
194 return (0);
198 * vs_vsplit --
199 * Create a new screen, vertically.
201 * PUBLIC: int vs_vsplit(SCR *, SCR *);
204 vs_vsplit(SCR *sp, SCR *new)
206 GS *gp;
207 size_t cols;
209 gp = sp->gp;
211 /* Check to see if it's possible. */
212 if (sp->cols / 2 <= MINIMUM_SCREEN_COLS) {
213 msgq(sp, M_ERR,
214 "288|Screen must be larger than %d columns to split",
215 MINIMUM_SCREEN_COLS * 2);
216 return (1);
219 /* Wait for any messages in the screen. */
220 vs_resolve(sp, NULL, 1);
222 /* Get a new screen map. */
223 CALLOC(sp, _HMAP(new), SIZE_HMAP(sp), sizeof(SMAP));
224 if (_HMAP(new) == NULL)
225 return (1);
226 _HMAP(new)->lno = sp->lno;
227 _HMAP(new)->coff = 0;
228 _HMAP(new)->soff = 1;
231 * Split the screen in half; we have to sacrifice a column to delimit
232 * the screens.
234 * XXX
235 * We always split to the right... that makes more sense to me, and
236 * I don't want to play the stupid games that I play when splitting
237 * horizontally.
239 * XXX
240 * We reserve a column for the screen, "knowing" that curses needs
241 * one. This should be worked out with the display interface.
243 cols = sp->cols / 2;
244 new->cols = sp->cols - cols - 1;
245 sp->cols = cols;
246 new->coff = sp->coff + cols + 1;
247 sp->cno = 0;
249 /* Nothing else changes. */
250 new->rows = sp->rows;
251 new->t_rows = sp->t_rows;
252 new->t_maxrows = sp->t_maxrows;
253 new->t_minrows = sp->t_minrows;
254 new->roff = sp->roff;
255 new->defscroll = sp->defscroll;
256 _TMAP(new) = _HMAP(new) + (new->t_rows - 1);
258 /* Fit the screen into the logical chain. */
259 vs_insert(new, sp->gp);
261 /* Tell the display that we're splitting. */
262 (void)gp->scr_split(sp, new);
264 /* Redraw the old screen from scratch. */
265 F_SET(sp, SC_SCR_REFORMAT | SC_STATUS);
268 * Initialize the screen flags:
270 * If we're in vi mode in one screen, we don't have to reinitialize.
271 * This isn't just a cosmetic fix. The path goes like this:
273 * return into vi(), SC_SSWITCH set
274 * call vs_refresh() with SC_STATUS set
275 * call vs_resolve to display the status message
276 * call vs_refresh() because the SC_SCR_VI bit isn't set
278 * Things go downhill at this point.
280 * Draw the new screen from scratch, and add a status line.
282 F_SET(new,
283 SC_SCR_REFORMAT | SC_STATUS |
284 F_ISSET(sp, SC_EX | SC_VI | SC_SCR_VI | SC_SCR_EX | SC_READONLY));
285 return (0);
289 * vs_insert --
290 * Insert the new screen into the correct place in the logical
291 * chain.
293 static void
294 vs_insert(SCR *sp, GS *gp)
296 SCR *tsp;
298 gp = sp->gp;
300 /* Move past all screens with lower row numbers. */
301 TAILQ_FOREACH(tsp, gp->dq, q)
302 if (tsp->roff >= sp->roff)
303 break;
305 * Move past all screens with the same row number and lower
306 * column numbers.
308 for (; tsp != NULL; tsp = TAILQ_NEXT(tsp, q))
309 if (tsp->roff != sp->roff || tsp->coff > sp->coff)
310 break;
313 * If we reached the end, this screen goes there. Otherwise,
314 * put it before or after the screen where we stopped.
316 if (tsp == NULL) {
317 TAILQ_INSERT_TAIL(gp->dq, sp, q);
318 } else if (tsp->roff < sp->roff ||
319 (tsp->roff == sp->roff && tsp->coff < sp->coff)) {
320 TAILQ_INSERT_AFTER(gp->dq, tsp, sp, q);
321 } else
322 TAILQ_INSERT_BEFORE(tsp, sp, q);
326 * vs_discard --
327 * Discard the screen, folding the real-estate into a related screen,
328 * if one exists, and return that screen.
330 * PUBLIC: int vs_discard(SCR *, SCR **);
333 vs_discard(SCR *sp, SCR **spp)
335 GS *gp;
336 SCR *tsp, **lp, *list[100];
337 jdir_t jdir;
339 gp = sp->gp;
342 * Save the old screen's cursor information.
344 * XXX
345 * If called after file_end(), and the underlying file was a tmp
346 * file, it may have gone away.
348 if (sp->frp != NULL) {
349 sp->frp->lno = sp->lno;
350 sp->frp->cno = sp->cno;
351 F_SET(sp->frp, FR_CURSORSET);
354 /* If no other screens to join, we're done. */
355 if (!IS_SPLIT(sp)) {
356 (void)gp->scr_discard(sp, NULL);
358 if (spp != NULL)
359 *spp = NULL;
360 return (0);
364 * Find a set of screens that cover one of the screen's borders.
365 * Check the vertical axis first, for no particular reason.
367 * XXX
368 * It's possible (I think?), to create a screen that shares no full
369 * border with any other set of screens, so we can't discard it. We
370 * just complain at the user until they clean it up.
372 if (vs_join(sp, list, &jdir))
373 return (1);
376 * Modify the affected screens. Redraw the modified screen(s) from
377 * scratch, setting a status line. If this is ever a performance
378 * problem we could play games with the map, but I wrote that code
379 * before and it was never clean or easy.
381 * Don't clean up the discarded screen's information. If the screen
382 * isn't exiting, we'll do the work when the user redisplays it.
384 switch (jdir) {
385 case HORIZ_FOLLOW:
386 case HORIZ_PRECEDE:
387 for (lp = &list[0]; (tsp = *lp) != NULL; ++lp) {
389 * Small screens: see vs_refresh.c section 6a. Adjust
390 * text line info, unless it's a small screen.
392 * Reset the length of the default scroll.
394 * Reset the map references.
396 tsp->rows += sp->rows;
397 if (!IS_SMALL(tsp))
398 tsp->t_rows = tsp->t_minrows = tsp->rows - 1;
399 tsp->t_maxrows = tsp->rows - 1;
401 tsp->defscroll = tsp->t_maxrows / 2;
403 *(_HMAP(tsp) + (tsp->t_rows - 1)) = *_TMAP(tsp);
404 _TMAP(tsp) = _HMAP(tsp) + (tsp->t_rows - 1);
406 switch (jdir) {
407 case HORIZ_FOLLOW:
408 tsp->roff = sp->roff;
409 vs_sm_fill(tsp, OOBLNO, P_TOP);
410 break;
411 case HORIZ_PRECEDE:
412 vs_sm_fill(tsp, OOBLNO, P_BOTTOM);
413 break;
414 default:
415 abort();
417 F_SET(tsp, SC_STATUS);
419 break;
420 case VERT_FOLLOW:
421 case VERT_PRECEDE:
422 for (lp = &list[0]; (tsp = *lp) != NULL; ++lp) {
423 if (jdir == VERT_FOLLOW)
424 tsp->coff = sp->coff;
425 tsp->cols += sp->cols + 1; /* XXX: DIVIDER */
426 vs_sm_fill(tsp, OOBLNO, P_TOP);
427 F_SET(tsp, SC_STATUS);
429 break;
430 default:
431 abort();
434 /* Find the closest screen that changed and move to it. */
435 tsp = list[0];
436 if (spp != NULL)
437 *spp = tsp;
439 /* Tell the display that we're discarding a screen. */
440 (void)gp->scr_discard(sp, list);
442 return (0);
446 * vs_join --
447 * Find a set of screens that covers a screen's border.
449 static int
450 vs_join(SCR *sp, SCR **listp, jdir_t *jdirp)
452 GS *gp;
453 SCR **lp, *tsp;
454 int first;
455 size_t tlen;
457 gp = sp->gp;
459 /* Check preceding vertical. */
460 for (lp = listp, tlen = sp->rows,
461 tsp = TAILQ_FIRST(gp->dq);
462 tsp != NULL; tsp = TAILQ_NEXT(tsp, q)) {
463 if (sp == tsp)
464 continue;
465 /* Test if precedes the screen vertically. */
466 if (tsp->coff + tsp->cols + 1 != sp->coff)
467 continue;
469 * Test if a subset on the vertical axis. If overlaps the
470 * beginning or end, we can't join on this axis at all.
472 if (tsp->roff > sp->roff + sp->rows)
473 continue;
474 if (tsp->roff < sp->roff) {
475 if (tsp->roff + tsp->rows >= sp->roff)
476 break;
477 continue;
479 if (tsp->roff + tsp->rows > sp->roff + sp->rows)
480 break;
481 #ifdef DEBUG
482 if (tlen < tsp->rows)
483 abort();
484 #endif
485 tlen -= tsp->rows;
486 *lp++ = tsp;
488 if (tlen == 0) {
489 *lp = NULL;
490 *jdirp = VERT_PRECEDE;
491 return (0);
494 /* Check following vertical. */
495 for (lp = listp, tlen = sp->rows,
496 tsp = TAILQ_FIRST(gp->dq);
497 tsp != NULL; tsp = TAILQ_NEXT(tsp, q)) {
498 if (sp == tsp)
499 continue;
500 /* Test if follows the screen vertically. */
501 if (tsp->coff != sp->coff + sp->cols + 1)
502 continue;
504 * Test if a subset on the vertical axis. If overlaps the
505 * beginning or end, we can't join on this axis at all.
507 if (tsp->roff > sp->roff + sp->rows)
508 continue;
509 if (tsp->roff < sp->roff) {
510 if (tsp->roff + tsp->rows >= sp->roff)
511 break;
512 continue;
514 if (tsp->roff + tsp->rows > sp->roff + sp->rows)
515 break;
516 #ifdef DEBUG
517 if (tlen < tsp->rows)
518 abort();
519 #endif
520 tlen -= tsp->rows;
521 *lp++ = tsp;
523 if (tlen == 0) {
524 *lp = NULL;
525 *jdirp = VERT_FOLLOW;
526 return (0);
529 /* Check preceding horizontal. */
530 for (first = 0, lp = listp, tlen = sp->cols,
531 tsp = TAILQ_FIRST(gp->dq);
532 tsp != NULL; tsp = TAILQ_NEXT(tsp, q)) {
533 if (sp == tsp)
534 continue;
535 /* Test if precedes the screen horizontally. */
536 if (tsp->roff + tsp->rows != sp->roff)
537 continue;
539 * Test if a subset on the horizontal axis. If overlaps the
540 * beginning or end, we can't join on this axis at all.
542 if (tsp->coff > sp->coff + sp->cols)
543 continue;
544 if (tsp->coff < sp->coff) {
545 if (tsp->coff + tsp->cols >= sp->coff)
546 break;
547 continue;
549 if (tsp->coff + tsp->cols > sp->coff + sp->cols)
550 break;
551 #ifdef DEBUG
552 if (tlen < tsp->cols)
553 abort();
554 #endif
555 tlen -= tsp->cols + first;
556 first = 1;
557 *lp++ = tsp;
559 if (tlen == 0) {
560 *lp = NULL;
561 *jdirp = HORIZ_PRECEDE;
562 return (0);
565 /* Check following horizontal. */
566 for (first = 0, lp = listp, tlen = sp->cols,
567 tsp = TAILQ_FIRST(gp->dq);
568 tsp != NULL; tsp = TAILQ_NEXT(tsp, q)) {
569 if (sp == tsp)
570 continue;
571 /* Test if precedes the screen horizontally. */
572 if (tsp->roff != sp->roff + sp->rows)
573 continue;
575 * Test if a subset on the horizontal axis. If overlaps the
576 * beginning or end, we can't join on this axis at all.
578 if (tsp->coff > sp->coff + sp->cols)
579 continue;
580 if (tsp->coff < sp->coff) {
581 if (tsp->coff + tsp->cols >= sp->coff)
582 break;
583 continue;
585 if (tsp->coff + tsp->cols > sp->coff + sp->cols)
586 break;
587 #ifdef DEBUG
588 if (tlen < tsp->cols)
589 abort();
590 #endif
591 tlen -= tsp->cols + first;
592 first = 1;
593 *lp++ = tsp;
595 if (tlen == 0) {
596 *lp = NULL;
597 *jdirp = HORIZ_FOLLOW;
598 return (0);
600 return (1);
604 * vs_fg --
605 * Background the current screen, and foreground a new one.
607 * PUBLIC: int vs_fg(SCR *, SCR **, CHAR_T *, int);
610 vs_fg(SCR *sp, SCR **nspp, CHAR_T *name, int newscreen)
612 GS *gp;
613 SCR *nsp;
614 char *np;
615 size_t nlen;
617 gp = sp->gp;
619 if (name)
620 INT2CHAR(sp, name, STRLEN(name) + 1, np, nlen);
621 else
622 np = NULL;
623 if (newscreen)
624 /* Get the specified background screen. */
625 nsp = vs_getbg(sp, np);
626 else
627 /* Swap screens. */
628 if (vs_swap(sp, &nsp, np))
629 return (1);
631 if ((*nspp = nsp) == NULL) {
632 msgq_wstr(sp, M_ERR, name,
633 name == NULL ?
634 "223|There are no background screens" :
635 "224|There's no background screen editing a file named %s");
636 return (1);
639 if (newscreen) {
640 /* Remove the new screen from the background queue. */
641 TAILQ_REMOVE(gp->hq, nsp, q);
643 /* Split the screen; if we fail, hook the screen back in. */
644 if (vs_split(sp, nsp, 0)) {
645 TAILQ_INSERT_TAIL(gp->hq, nsp, q);
646 return (1);
648 } else {
649 /* Move the old screen to the background queue. */
650 TAILQ_REMOVE(gp->dq, sp, q);
651 TAILQ_INSERT_TAIL(gp->hq, sp, q);
653 return (0);
657 * vs_bg --
658 * Background the screen, and switch to the next one.
660 * PUBLIC: int vs_bg(SCR *);
663 vs_bg(SCR *sp)
665 GS *gp;
666 SCR *nsp;
668 gp = sp->gp;
670 /* Try and join with another screen. */
671 if (vs_discard(sp, &nsp))
672 return (1);
673 if (nsp == NULL) {
674 msgq(sp, M_ERR,
675 "225|You may not background your only displayed screen");
676 return (1);
679 /* Move the old screen to the background queue. */
680 TAILQ_REMOVE(gp->dq, sp, q);
681 TAILQ_INSERT_TAIL(gp->hq, sp, q);
683 /* Toss the screen map. */
684 free(_HMAP(sp));
685 _HMAP(sp) = NULL;
687 /* Switch screens. */
688 sp->nextdisp = nsp;
689 F_SET(sp, SC_SSWITCH);
691 return (0);
695 * vs_swap --
696 * Swap the current screen with a backgrounded one.
698 * PUBLIC: int vs_swap(SCR *, SCR **, char *);
701 vs_swap(SCR *sp, SCR **nspp, char *name)
703 GS *gp;
704 SCR *nsp, *list[2];
706 gp = sp->gp;
708 /* Get the specified background screen. */
709 if ((*nspp = nsp = vs_getbg(sp, name)) == NULL)
710 return (0);
713 * Save the old screen's cursor information.
715 * XXX
716 * If called after file_end(), and the underlying file was a tmp
717 * file, it may have gone away.
719 if (sp->frp != NULL) {
720 sp->frp->lno = sp->lno;
721 sp->frp->cno = sp->cno;
722 F_SET(sp->frp, FR_CURSORSET);
725 /* Switch screens. */
726 sp->nextdisp = nsp;
727 F_SET(sp, SC_SSWITCH);
729 /* Initialize terminal information. */
730 VIP(nsp)->srows = VIP(sp)->srows;
732 /* Initialize screen information. */
733 nsp->cols = sp->cols;
734 nsp->rows = sp->rows; /* XXX: Only place in vi that sets rows. */
735 nsp->roff = sp->roff;
738 * Small screens: see vs_refresh.c, section 6a.
740 * The new screens may have different screen options sizes than the
741 * old one, so use them. Make sure that text counts aren't larger
742 * than the new screen sizes.
744 if (IS_SMALL(nsp)) {
745 nsp->t_minrows = nsp->t_rows = O_VAL(nsp, O_WINDOW);
746 if (nsp->t_rows > sp->t_maxrows)
747 nsp->t_rows = nsp->t_maxrows;
748 if (nsp->t_minrows > sp->t_maxrows)
749 nsp->t_minrows = nsp->t_maxrows;
750 } else
751 nsp->t_rows = nsp->t_maxrows = nsp->t_minrows = nsp->rows - 1;
753 /* Reset the length of the default scroll. */
754 nsp->defscroll = nsp->t_maxrows / 2;
756 /* Allocate a new screen map. */
757 CALLOC_RET(nsp, _HMAP(nsp), SIZE_HMAP(nsp), sizeof(SMAP));
758 _TMAP(nsp) = _HMAP(nsp) + (nsp->t_rows - 1);
760 /* Fill the map. */
761 nsp->gp = sp->gp;
762 if (vs_sm_fill(nsp, nsp->lno, P_FILL))
763 return (1);
766 * The new screen replaces the old screen in the parent/child list.
767 * We insert the new screen after the old one. If we're exiting,
768 * the exit will delete the old one, if we're foregrounding, the fg
769 * code will move the old one to the background queue.
771 TAILQ_REMOVE(gp->hq, nsp, q);
772 TAILQ_INSERT_AFTER(gp->dq, sp, nsp, q);
775 * Don't change the screen's cursor information other than to
776 * note that the cursor is wrong.
778 F_SET(VIP(nsp), VIP_CUR_INVALID);
780 /* Draw the new screen from scratch, and add a status line. */
781 F_SET(nsp, SC_SCR_REDRAW | SC_STATUS);
783 list[0] = nsp; list[1] = NULL;
784 (void)gp->scr_discard(sp, list);
786 return (0);
790 * vs_resize --
791 * Change the absolute size of the current screen.
793 * PUBLIC: int vs_resize(SCR *, long, adj_t);
796 vs_resize(SCR *sp, long int count, adj_t adj)
798 GS *gp;
799 SCR *g, *s, *prev, *next, *list[3] = {NULL, NULL, NULL};
800 size_t g_off, s_off;
802 gp = sp->gp;
805 * Figure out which screens will grow, which will shrink, and
806 * make sure it's possible.
808 if (count == 0)
809 return (0);
810 if (adj == A_SET) {
811 if (sp->t_maxrows == count)
812 return (0);
813 if (sp->t_maxrows > count) {
814 adj = A_DECREASE;
815 count = sp->t_maxrows - count;
816 } else {
817 adj = A_INCREASE;
818 count = count - sp->t_maxrows;
822 /* Find first overlapping screen */
823 for (next = TAILQ_NEXT(sp, q); next != NULL &&
824 (next->coff >= sp->coff + sp->cols ||
825 next->coff + next->cols <= sp->coff);
826 next = TAILQ_NEXT(next, q));
827 /* See if we can use it */
828 if (next != NULL &&
829 (sp->coff != next->coff || sp->cols != next->cols))
830 next = NULL;
831 for (prev = TAILQ_PREV(sp, _dqh, q); prev != NULL &&
832 (prev->coff >= sp->coff + sp->cols ||
833 prev->coff + prev->cols <= sp->coff);
834 prev = TAILQ_PREV(prev, _dqh, q));
835 if (prev != NULL &&
836 (sp->coff != prev->coff || sp->cols != prev->cols))
837 prev = NULL;
839 g_off = s_off = 0;
840 if (adj == A_DECREASE) {
841 if (count < 0)
842 count = -count;
843 s = sp;
844 if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count)
845 goto toosmall;
846 if ((g = prev) == NULL) {
847 if ((g = next) == NULL)
848 goto toobig;
849 g_off = -count;
850 } else
851 s_off = count;
852 } else {
853 g = sp;
854 if ((s = next) != NULL &&
855 s->t_maxrows >= MINIMUM_SCREEN_ROWS + count)
856 s_off = count;
857 else
858 s = NULL;
859 if (s == NULL) {
860 if ((s = prev) == NULL) {
861 toobig: msgq(sp, M_BERR, adj == A_DECREASE ?
862 "227|The screen cannot shrink" :
863 "228|The screen cannot grow");
864 return (1);
866 if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count) {
867 toosmall: msgq(sp, M_BERR,
868 "226|The screen can only shrink to %d rows",
869 MINIMUM_SCREEN_ROWS);
870 return (1);
872 g_off = -count;
877 * Fix up the screens; we could optimize the reformatting of the
878 * screen, but this isn't likely to be a common enough operation
879 * to make it worthwhile.
881 s->rows += -count;
882 s->roff += s_off;
883 g->rows += count;
884 g->roff += g_off;
886 g->t_rows += count;
887 if (g->t_minrows == g->t_maxrows)
888 g->t_minrows += count;
889 g->t_maxrows += count;
890 _TMAP(g) += count;
891 F_SET(g, SC_SCR_REFORMAT | SC_STATUS);
893 s->t_rows -= count;
894 s->t_maxrows -= count;
895 if (s->t_minrows > s->t_maxrows)
896 s->t_minrows = s->t_maxrows;
897 _TMAP(s) -= count;
898 F_SET(s, SC_SCR_REFORMAT | SC_STATUS);
900 /* XXXX */
901 list[0] = g; list[1] = s;
902 gp->scr_discard(0, list);
904 return (0);
908 * vs_getbg --
909 * Get the specified background screen, or, if name is NULL, the first
910 * background screen.
912 static SCR *
913 vs_getbg(SCR *sp, char *name)
915 GS *gp;
916 SCR *nsp;
917 char *p;
919 gp = sp->gp;
921 /* If name is NULL, return the first background screen on the list. */
922 if (name == NULL)
923 return (TAILQ_FIRST(gp->hq));
925 /* Search for a full match. */
926 TAILQ_FOREACH(nsp, gp->hq, q)
927 if (!strcmp(nsp->frp->name, name))
928 break;
929 if (nsp != NULL)
930 return (nsp);
932 /* Search for a last-component match. */
933 TAILQ_FOREACH(nsp, gp->hq, q) {
934 if ((p = strrchr(nsp->frp->name, '/')) == NULL)
935 p = nsp->frp->name;
936 else
937 ++p;
938 if (!strcmp(p, name))
939 break;
941 if (nsp != NULL)
942 return (nsp);
944 return (NULL);