add vi_send(), vi_translate() to list of exported functions
[nvi.git] / vi / vs_split.c
blobb6e74b60c7f0e170141991fd558b859c75d435f1
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.34 1996/12/18 10:24:31 bostic Exp $ (Berkeley) $Date: 1996/12/18 10:24:31 $";
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 *));
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);
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 = 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);
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(sp)
299 SCR *sp;
301 GS *gp;
302 SCR *tsp;
304 gp = sp->gp;
306 /* Move past all screens with lower row numbers. */
307 for (tsp = gp->dq.cqh_first;
308 tsp != (void *)&gp->dq; 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 *)&gp->dq; 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 *)&gp->dq) {
324 CIRCLEQ_INSERT_TAIL(&gp->dq, sp, q);
325 } else if (tsp->roff < sp->roff ||
326 tsp->roff == sp->roff && tsp->coff < sp->coff) {
327 CIRCLEQ_INSERT_BEFORE(&gp->dq, tsp, sp, q);
328 } else
329 CIRCLEQ_INSERT_AFTER(&gp->dq, 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(sp, spp)
341 SCR *sp, **spp;
343 GS *gp;
344 SCR *tsp, **lp, *list[100];
345 jdir_t jdir;
347 gp = sp->gp;
350 * Save the old screen's cursor information.
352 * XXX
353 * If called after file_end(), and the underlying file was a tmp
354 * file, it may have gone away.
356 if (sp->frp != NULL) {
357 sp->frp->lno = sp->lno;
358 sp->frp->cno = sp->cno;
359 F_SET(sp->frp, FR_CURSORSET);
362 /* If no other screens to join, we're done. */
363 if (!IS_SPLIT(sp)) {
364 (void)gp->scr_discard(sp, NULL);
366 if (spp != NULL)
367 *spp = NULL;
368 return (0);
372 * Find a set of screens that cover one of the screen's borders.
373 * Check the vertical axis first, for no particular reason.
375 * XXX
376 * It's possible (I think?), to create a screen that shares no full
377 * border with any other set of screens, so we can't discard it. We
378 * just complain at the user until they clean it up.
380 if (vs_join(sp, list, &jdir))
381 return (1);
384 * Modify the affected screens. Redraw the modified screen(s) from
385 * scratch, setting a status line. If this is ever a performance
386 * problem we could play games with the map, but I wrote that code
387 * before and it was never clean or easy.
389 * Don't clean up the discarded screen's information. If the screen
390 * isn't exiting, we'll do the work when the user redisplays it.
392 switch (jdir) {
393 case HORIZ_FOLLOW:
394 case HORIZ_PRECEDE:
395 for (lp = &list[0]; (tsp = *lp) != NULL; ++lp) {
397 * Small screens: see vs_refresh.c section 6a. Adjust
398 * text line info, unless it's a small screen.
400 * Reset the length of the default scroll.
402 * Reset the map references.
404 tsp->rows += sp->rows;
405 if (!IS_SMALL(tsp))
406 tsp->t_rows = tsp->t_minrows = tsp->rows - 1;
407 tsp->t_maxrows = tsp->rows - 1;
409 tsp->defscroll = tsp->t_maxrows / 2;
411 *(_HMAP(tsp) + (tsp->t_rows - 1)) = *_TMAP(tsp);
412 _TMAP(tsp) = _HMAP(tsp) + (tsp->t_rows - 1);
414 switch (jdir) {
415 case HORIZ_FOLLOW:
416 tsp->roff = sp->roff;
417 vs_sm_fill(tsp, OOBLNO, P_TOP);
418 break;
419 case HORIZ_PRECEDE:
420 vs_sm_fill(tsp, OOBLNO, P_BOTTOM);
421 break;
422 default:
423 abort();
425 F_SET(tsp, SC_STATUS);
427 break;
428 case VERT_FOLLOW:
429 case VERT_PRECEDE:
430 for (lp = &list[0]; (tsp = *lp) != NULL; ++lp) {
431 if (jdir == VERT_FOLLOW)
432 tsp->coff = sp->coff;
433 tsp->cols += sp->cols + 1; /* XXX: DIVIDER */
434 vs_sm_fill(tsp, OOBLNO, P_TOP);
435 F_SET(tsp, SC_STATUS);
437 break;
438 default:
439 abort();
442 /* Find the closest screen that changed and move to it. */
443 tsp = list[0];
444 if (spp != NULL)
445 *spp = tsp;
447 /* Tell the display that we're discarding a screen. */
448 (void)gp->scr_discard(sp, tsp);
450 return (0);
454 * vs_join --
455 * Find a set of screens that covers a screen's border.
457 static int
458 vs_join(sp, listp, jdirp)
459 SCR *sp, **listp;
460 jdir_t *jdirp;
462 GS *gp;
463 SCR **lp, *tsp;
464 int first;
465 size_t tlen;
467 gp = sp->gp;
469 /* Check preceding vertical. */
470 for (lp = listp, tlen = sp->rows,
471 tsp = gp->dq.cqh_first;
472 tsp != (void *)&gp->dq; tsp = tsp->q.cqe_next) {
473 if (sp == tsp)
474 continue;
475 /* Test if precedes the screen vertically. */
476 if (tsp->coff + tsp->cols + 1 != sp->coff)
477 continue;
479 * Test if a subset on the vertical axis. If overlaps the
480 * beginning or end, we can't join on this axis at all.
482 if (tsp->roff > sp->roff + sp->rows)
483 continue;
484 if (tsp->roff < sp->roff) {
485 if (tsp->roff + tsp->rows >= sp->roff)
486 break;
487 continue;
489 if (tsp->roff + tsp->rows > sp->roff + sp->rows)
490 break;
491 #ifdef DEBUG
492 if (tlen < tsp->rows)
493 abort();
494 #endif
495 tlen -= tsp->rows;
496 *lp++ = tsp;
498 if (tlen == 0) {
499 *lp = NULL;
500 *jdirp = VERT_PRECEDE;
501 return (0);
504 /* Check following vertical. */
505 for (lp = listp, tlen = sp->rows,
506 tsp = gp->dq.cqh_first;
507 tsp != (void *)&gp->dq; tsp = tsp->q.cqe_next) {
508 if (sp == tsp)
509 continue;
510 /* Test if follows the screen vertically. */
511 if (tsp->coff != sp->coff + sp->cols + 1)
512 continue;
514 * Test if a subset on the vertical axis. If overlaps the
515 * beginning or end, we can't join on this axis at all.
517 if (tsp->roff > sp->roff + sp->rows)
518 continue;
519 if (tsp->roff < sp->roff) {
520 if (tsp->roff + tsp->rows >= sp->roff)
521 break;
522 continue;
524 if (tsp->roff + tsp->rows > sp->roff + sp->rows)
525 break;
526 #ifdef DEBUG
527 if (tlen < tsp->rows)
528 abort();
529 #endif
530 tlen -= tsp->rows;
531 *lp++ = tsp;
533 if (tlen == 0) {
534 *lp = NULL;
535 *jdirp = VERT_FOLLOW;
536 return (0);
539 /* Check preceding horizontal. */
540 for (first = 0, lp = listp, tlen = sp->cols,
541 tsp = gp->dq.cqh_first;
542 tsp != (void *)&gp->dq; tsp = tsp->q.cqe_next) {
543 if (sp == tsp)
544 continue;
545 /* Test if precedes the screen horizontally. */
546 if (tsp->roff + tsp->rows != sp->roff)
547 continue;
549 * Test if a subset on the horizontal axis. If overlaps the
550 * beginning or end, we can't join on this axis at all.
552 if (tsp->coff > sp->coff + sp->cols)
553 continue;
554 if (tsp->coff < sp->coff) {
555 if (tsp->coff + tsp->cols >= sp->coff)
556 break;
557 continue;
559 if (tsp->coff + tsp->cols > sp->coff + sp->cols)
560 break;
561 #ifdef DEBUG
562 if (tlen < tsp->cols)
563 abort();
564 #endif
565 tlen -= tsp->cols + first;
566 first = 1;
567 *lp++ = tsp;
569 if (tlen == 0) {
570 *lp = NULL;
571 *jdirp = HORIZ_PRECEDE;
572 return (0);
575 /* Check following horizontal. */
576 for (first = 0, lp = listp, tlen = sp->cols,
577 tsp = gp->dq.cqh_first;
578 tsp != (void *)&gp->dq; tsp = tsp->q.cqe_next) {
579 if (sp == tsp)
580 continue;
581 /* Test if precedes the screen horizontally. */
582 if (tsp->roff != sp->roff + sp->rows)
583 continue;
585 * Test if a subset on the horizontal axis. If overlaps the
586 * beginning or end, we can't join on this axis at all.
588 if (tsp->coff > sp->coff + sp->cols)
589 continue;
590 if (tsp->coff < sp->coff) {
591 if (tsp->coff + tsp->cols >= sp->coff)
592 break;
593 continue;
595 if (tsp->coff + tsp->cols > sp->coff + sp->cols)
596 break;
597 #ifdef DEBUG
598 if (tlen < tsp->cols)
599 abort();
600 #endif
601 tlen -= tsp->cols + first;
602 first = 1;
603 *lp++ = tsp;
605 if (tlen == 0) {
606 *lp = NULL;
607 *jdirp = HORIZ_FOLLOW;
608 return (0);
610 return (1);
614 * vs_fg --
615 * Background the current screen, and foreground a new one.
617 * PUBLIC: int vs_fg __P((SCR *, SCR **, CHAR_T *, int));
620 vs_fg(sp, nspp, name, newscreen)
621 SCR *sp, **nspp;
622 CHAR_T *name;
623 int newscreen;
625 GS *gp;
626 SCR *nsp;
628 gp = sp->gp;
630 if (newscreen)
631 /* Get the specified background screen. */
632 nsp = vs_getbg(sp, name);
633 else
634 /* Swap screens. */
635 if (vs_swap(sp, &nsp, name))
636 return (1);
638 if ((*nspp = nsp) == NULL) {
639 msgq_str(sp, M_ERR, name,
640 name == NULL ?
641 "223|There are no background screens" :
642 "224|There's no background screen editing a file named %s");
643 return (1);
646 if (newscreen) {
647 /* Remove the new screen from the background queue. */
648 CIRCLEQ_REMOVE(&gp->hq, nsp, q);
650 /* Split the screen; if we fail, hook the screen back in. */
651 if (vs_split(sp, nsp, 0)) {
652 CIRCLEQ_INSERT_TAIL(&gp->hq, nsp, q);
653 return (1);
655 } else {
656 /* Move the old screen to the background queue. */
657 CIRCLEQ_REMOVE(&gp->dq, sp, q);
658 CIRCLEQ_INSERT_TAIL(&gp->hq, sp, q);
660 return (0);
664 * vs_bg --
665 * Background the screen, and switch to the next one.
667 * PUBLIC: int vs_bg __P((SCR *));
670 vs_bg(sp)
671 SCR *sp;
673 GS *gp;
674 SCR *nsp;
676 gp = sp->gp;
678 /* Try and join with another screen. */
679 if (vs_discard(sp, &nsp))
680 return (1);
681 if (nsp == NULL) {
682 msgq(sp, M_ERR,
683 "225|You may not background your only displayed screen");
684 return (1);
687 /* Move the old screen to the background queue. */
688 CIRCLEQ_REMOVE(&gp->dq, sp, q);
689 CIRCLEQ_INSERT_TAIL(&gp->hq, sp, q);
691 /* Toss the screen map. */
692 free(_HMAP(sp));
693 _HMAP(sp) = NULL;
695 /* Switch screens. */
696 sp->nextdisp = nsp;
697 F_SET(sp, SC_SSWITCH);
699 return (0);
703 * vs_swap --
704 * Swap the current screen with a backgrounded one.
706 * PUBLIC: int vs_swap __P((SCR *, SCR **, char *));
709 vs_swap(sp, nspp, name)
710 SCR *sp, **nspp;
711 char *name;
713 GS *gp;
714 SCR *nsp;
716 gp = sp->gp;
718 /* Get the specified background screen. */
719 if ((*nspp = nsp = vs_getbg(sp, name)) == NULL)
720 return (0);
723 * Save the old screen's cursor information.
725 * XXX
726 * If called after file_end(), and the underlying file was a tmp
727 * file, it may have gone away.
729 if (sp->frp != NULL) {
730 sp->frp->lno = sp->lno;
731 sp->frp->cno = sp->cno;
732 F_SET(sp->frp, FR_CURSORSET);
735 /* Switch screens. */
736 sp->nextdisp = nsp;
737 F_SET(sp, SC_SSWITCH);
739 /* Initialize terminal information. */
740 VIP(nsp)->srows = VIP(sp)->srows;
742 /* Initialize screen information. */
743 nsp->cols = sp->cols;
744 nsp->rows = sp->rows; /* XXX: Only place in vi that sets rows. */
745 nsp->roff = sp->roff;
748 * Small screens: see vs_refresh.c, section 6a.
750 * The new screens may have different screen options sizes than the
751 * old one, so use them. Make sure that text counts aren't larger
752 * than the new screen sizes.
754 if (IS_SMALL(nsp)) {
755 nsp->t_minrows = nsp->t_rows = O_VAL(nsp, O_WINDOW);
756 if (nsp->t_rows > sp->t_maxrows)
757 nsp->t_rows = nsp->t_maxrows;
758 if (nsp->t_minrows > sp->t_maxrows)
759 nsp->t_minrows = nsp->t_maxrows;
760 } else
761 nsp->t_rows = nsp->t_maxrows = nsp->t_minrows = nsp->rows - 1;
763 /* Reset the length of the default scroll. */
764 nsp->defscroll = nsp->t_maxrows / 2;
766 /* Allocate a new screen map. */
767 CALLOC_RET(nsp, _HMAP(nsp), SMAP *, SIZE_HMAP(nsp), sizeof(SMAP));
768 _TMAP(nsp) = _HMAP(nsp) + (nsp->t_rows - 1);
770 /* Fill the map. */
771 if (vs_sm_fill(nsp, nsp->lno, P_FILL))
772 return (1);
775 * The new screen replaces the old screen in the parent/child list.
776 * We insert the new screen after the old one. If we're exiting,
777 * the exit will delete the old one, if we're foregrounding, the fg
778 * code will move the old one to the background queue.
780 CIRCLEQ_REMOVE(&gp->hq, nsp, q);
781 CIRCLEQ_INSERT_AFTER(&gp->dq, sp, nsp, q);
784 * Don't change the screen's cursor information other than to
785 * note that the cursor is wrong.
787 F_SET(VIP(nsp), VIP_CUR_INVALID);
789 /* Draw the new screen from scratch, and add a status line. */
790 F_SET(nsp, SC_SCR_REDRAW | SC_STATUS);
791 return (0);
795 * vs_resize --
796 * Change the absolute size of the current screen.
798 * PUBLIC: int vs_resize __P((SCR *, long, adj_t));
801 vs_resize(sp, count, adj)
802 SCR *sp;
803 long count;
804 adj_t adj;
806 GS *gp;
807 SCR *g, *s;
808 size_t g_off, s_off;
810 gp = sp->gp;
813 * Figure out which screens will grow, which will shrink, and
814 * make sure it's possible.
816 if (count == 0)
817 return (0);
818 if (adj == A_SET) {
819 if (sp->t_maxrows == count)
820 return (0);
821 if (sp->t_maxrows > count) {
822 adj = A_DECREASE;
823 count = sp->t_maxrows - count;
824 } else {
825 adj = A_INCREASE;
826 count = count - sp->t_maxrows;
830 g_off = s_off = 0;
831 if (adj == A_DECREASE) {
832 if (count < 0)
833 count = -count;
834 s = sp;
835 if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count)
836 goto toosmall;
837 if ((g = sp->q.cqe_prev) == (void *)&gp->dq) {
838 if ((g = sp->q.cqe_next) == (void *)&gp->dq)
839 goto toobig;
840 g_off = -count;
841 } else
842 s_off = count;
843 } else {
844 g = sp;
845 if ((s = sp->q.cqe_next) != (void *)&gp->dq)
846 if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count)
847 s = NULL;
848 else
849 s_off = count;
850 else
851 s = NULL;
852 if (s == NULL) {
853 if ((s = sp->q.cqe_prev) == (void *)&gp->dq) {
854 toobig: msgq(sp, M_BERR, adj == A_DECREASE ?
855 "227|The screen cannot shrink" :
856 "228|The screen cannot grow");
857 return (1);
859 if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count) {
860 toosmall: msgq(sp, M_BERR,
861 "226|The screen can only shrink to %d rows",
862 MINIMUM_SCREEN_ROWS);
863 return (1);
865 g_off = -count;
870 * Fix up the screens; we could optimize the reformatting of the
871 * screen, but this isn't likely to be a common enough operation
872 * to make it worthwhile.
874 s->rows += -count;
875 s->roff += s_off;
876 g->rows += count;
877 g->roff += g_off;
879 g->t_rows += count;
880 if (g->t_minrows == g->t_maxrows)
881 g->t_minrows += count;
882 g->t_maxrows += count;
883 _TMAP(g) += count;
884 F_SET(g, SC_SCR_REFORMAT | SC_STATUS);
886 s->t_rows -= count;
887 s->t_maxrows -= count;
888 if (s->t_minrows > s->t_maxrows)
889 s->t_minrows = s->t_maxrows;
890 _TMAP(s) -= count;
891 F_SET(s, SC_SCR_REFORMAT | SC_STATUS);
893 return (0);
897 * vs_getbg --
898 * Get the specified background screen, or, if name is NULL, the first
899 * background screen.
901 static SCR *
902 vs_getbg(sp, name)
903 SCR *sp;
904 char *name;
906 GS *gp;
907 SCR *nsp;
908 char *p;
910 gp = sp->gp;
912 /* If name is NULL, return the first background screen on the list. */
913 if (name == NULL) {
914 nsp = gp->hq.cqh_first;
915 return (nsp == (void *)&gp->hq ? NULL : nsp);
918 /* Search for a full match. */
919 for (nsp = gp->hq.cqh_first;
920 nsp != (void *)&gp->hq; nsp = nsp->q.cqe_next)
921 if (!strcmp(nsp->frp->name, name))
922 break;
923 if (nsp != (void *)&gp->hq)
924 return (nsp);
926 /* Search for a last-component match. */
927 for (nsp = gp->hq.cqh_first;
928 nsp != (void *)&gp->hq; nsp = nsp->q.cqe_next) {
929 if ((p = strrchr(nsp->frp->name, '/')) == NULL)
930 p = nsp->frp->name;
931 else
932 ++p;
933 if (!strcmp(p, name))
934 break;
936 if (nsp != (void *)&gp->hq)
937 return (nsp);
939 return (NULL);