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. 6 * 7 * See the LICENSE file for redistribution information. 8 */ 9 10 #include "config.h" 11 12 #include <sys/types.h> 13 #include <sys/queue.h> 14 #include <sys/time.h> 15 16 #include <bitstring.h> 17 #include <errno.h> 18 #include <limits.h> 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <string.h> 22 23 #include "../common/common.h" 24 #include "vi.h" 25 26 typedef enum { HORIZ_FOLLOW, HORIZ_PRECEDE, VERT_FOLLOW, VERT_PRECEDE } jdir_t; 27 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 *); 31 32 /* 33 * vs_split -- 34 * Create a new screen, horizontally. 35 * 36 * PUBLIC: int vs_split(SCR *, SCR *, int); 37 */ 38 int 39 vs_split( 40 SCR *sp, 41 SCR *new, 42 int ccl) /* Colon-command line split. */ 43 { 44 GS *gp; 45 SMAP *smp; 46 size_t half; 47 int issmallscreen, splitup; 48 49 gp = sp->gp; 50 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); 57 } 58 59 /* Wait for any messages in the screen. */ 60 vs_resolve(sp, NULL, 1); 61 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; 69 70 /* Split the screen in half. */ 71 half = sp->rows / 2; 72 if (ccl && half > 6) 73 half = 6; 74 75 /* 76 * Small screens: see vs_refresh.c section 6a. Set a flag so 77 * we know to fix the screen up later. 78 */ 79 issmallscreen = IS_SMALL(sp); 80 81 /* The columns in the screen don't change. */ 82 new->coff = sp->coff; 83 new->cols = sp->cols; 84 85 /* 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. 90 * 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. 94 */ 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; 102 103 /* 104 * If the parent is the bottom half of the screen, shift 105 * the map down to match on-screen text. 106 */ 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; 113 } 114 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; 118 119 /* 120 * Small screens: see vs_refresh.c, section 6a. 121 * 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. 125 */ 126 if (issmallscreen) { 127 /* Fix the text line count for the parent. */ 128 if (splitup) 129 sp->t_rows -= new->rows; 130 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; 136 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; 145 146 /* 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. 151 */ 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; 156 } 157 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); 163 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; 169 170 /* Fit the screen into the logical chain. */ 171 vs_insert(new, sp->gp); 172 173 /* Tell the display that we're splitting. */ 174 (void)gp->scr_split(sp, new); 175 176 /* 177 * Initialize the screen flags: 178 * 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: 181 * 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 186 * 187 * Things go downhill at this point. 188 * 189 * Draw the new screen from scratch, and add a status line. 190 */ 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); 195 } 196 197 /* 198 * vs_vsplit -- 199 * Create a new screen, vertically. 200 * 201 * PUBLIC: int vs_vsplit(SCR *, SCR *); 202 */ 203 int 204 vs_vsplit(SCR *sp, SCR *new) 205 { 206 GS *gp; 207 size_t cols; 208 209 gp = sp->gp; 210 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); 217 } 218 219 /* Wait for any messages in the screen. */ 220 vs_resolve(sp, NULL, 1); 221 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; 229 230 /* 231 * Split the screen in half; we have to sacrifice a column to delimit 232 * the screens. 233 * 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. 238 * 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. 242 */ 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; 248 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); 257 258 /* Fit the screen into the logical chain. */ 259 vs_insert(new, sp->gp); 260 261 /* Tell the display that we're splitting. */ 262 (void)gp->scr_split(sp, new); 263 264 /* Redraw the old screen from scratch. */ 265 F_SET(sp, SC_SCR_REFORMAT | SC_STATUS); 266 267 /* 268 * Initialize the screen flags: 269 * 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: 272 * 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 277 * 278 * Things go downhill at this point. 279 * 280 * Draw the new screen from scratch, and add a status line. 281 */ 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); 286 } 287 288 /* 289 * vs_insert -- 290 * Insert the new screen into the correct place in the logical 291 * chain. 292 */ 293 static void 294 vs_insert(SCR *sp, GS *gp) 295 { 296 SCR *tsp; 297 298 gp = sp->gp; 299 300 /* Move past all screens with lower row numbers. */ 301 TAILQ_FOREACH(tsp, gp->dq, q) 302 if (tsp->roff >= sp->roff) 303 break; 304 /* 305 * Move past all screens with the same row number and lower 306 * column numbers. 307 */ 308 for (; tsp != NULL; tsp = TAILQ_NEXT(tsp, q)) 309 if (tsp->roff != sp->roff || tsp->coff > sp->coff) 310 break; 311 312 /* 313 * If we reached the end, this screen goes there. Otherwise, 314 * put it before or after the screen where we stopped. 315 */ 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); 323 } 324 325 /* 326 * vs_discard -- 327 * Discard the screen, folding the real-estate into a related screen, 328 * if one exists, and return that screen. 329 * 330 * PUBLIC: int vs_discard(SCR *, SCR **); 331 */ 332 int 333 vs_discard(SCR *sp, SCR **spp) 334 { 335 GS *gp; 336 SCR *tsp, **lp, *list[100]; 337 jdir_t jdir; 338 339 gp = sp->gp; 340 341 /* 342 * Save the old screen's cursor information. 343 * 344 * XXX 345 * If called after file_end(), and the underlying file was a tmp 346 * file, it may have gone away. 347 */ 348 if (sp->frp != NULL) { 349 sp->frp->lno = sp->lno; 350 sp->frp->cno = sp->cno; 351 F_SET(sp->frp, FR_CURSORSET); 352 } 353 354 /* If no other screens to join, we're done. */ 355 if (!IS_SPLIT(sp)) { 356 (void)gp->scr_discard(sp, NULL); 357 358 if (spp != NULL) 359 *spp = NULL; 360 return (0); 361 } 362 363 /* 364 * Find a set of screens that cover one of the screen's borders. 365 * Check the vertical axis first, for no particular reason. 366 * 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. 371 */ 372 if (vs_join(sp, list, &jdir)) 373 return (1); 374 375 /* 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. 380 * 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. 383 */ 384 switch (jdir) { 385 case HORIZ_FOLLOW: 386 case HORIZ_PRECEDE: 387 for (lp = &list[0]; (tsp = *lp) != NULL; ++lp) { 388 /* 389 * Small screens: see vs_refresh.c section 6a. Adjust 390 * text line info, unless it's a small screen. 391 * 392 * Reset the length of the default scroll. 393 * 394 * Reset the map references. 395 */ 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; 400 401 tsp->defscroll = tsp->t_maxrows / 2; 402 403 *(_HMAP(tsp) + (tsp->t_rows - 1)) = *_TMAP(tsp); 404 _TMAP(tsp) = _HMAP(tsp) + (tsp->t_rows - 1); 405 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(); 416 } 417 F_SET(tsp, SC_STATUS); 418 } 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); 428 } 429 break; 430 default: 431 abort(); 432 } 433 434 /* Find the closest screen that changed and move to it. */ 435 tsp = list[0]; 436 if (spp != NULL) 437 *spp = tsp; 438 439 /* Tell the display that we're discarding a screen. */ 440 (void)gp->scr_discard(sp, list); 441 442 return (0); 443 } 444 445 /* 446 * vs_join -- 447 * Find a set of screens that covers a screen's border. 448 */ 449 static int 450 vs_join(SCR *sp, SCR **listp, jdir_t *jdirp) 451 { 452 GS *gp; 453 SCR **lp, *tsp; 454 int first; 455 size_t tlen; 456 457 gp = sp->gp; 458 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; 468 /* 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. 471 */ 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; 478 } 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; 487 } 488 if (tlen == 0) { 489 *lp = NULL; 490 *jdirp = VERT_PRECEDE; 491 return (0); 492 } 493 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; 503 /* 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. 506 */ 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; 513 } 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; 522 } 523 if (tlen == 0) { 524 *lp = NULL; 525 *jdirp = VERT_FOLLOW; 526 return (0); 527 } 528 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; 538 /* 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. 541 */ 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; 548 } 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; 558 } 559 if (tlen == 0) { 560 *lp = NULL; 561 *jdirp = HORIZ_PRECEDE; 562 return (0); 563 } 564 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; 574 /* 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. 577 */ 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; 584 } 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; 594 } 595 if (tlen == 0) { 596 *lp = NULL; 597 *jdirp = HORIZ_FOLLOW; 598 return (0); 599 } 600 return (1); 601 } 602 603 /* 604 * vs_fg -- 605 * Background the current screen, and foreground a new one. 606 * 607 * PUBLIC: int vs_fg(SCR *, SCR **, CHAR_T *, int); 608 */ 609 int 610 vs_fg(SCR *sp, SCR **nspp, CHAR_T *name, int newscreen) 611 { 612 GS *gp; 613 SCR *nsp; 614 char *np; 615 size_t nlen; 616 617 gp = sp->gp; 618 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); 630 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); 637 } 638 639 if (newscreen) { 640 /* Remove the new screen from the background queue. */ 641 TAILQ_REMOVE(gp->hq, nsp, q); 642 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); 647 } 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); 652 } 653 return (0); 654 } 655 656 /* 657 * vs_bg -- 658 * Background the screen, and switch to the next one. 659 * 660 * PUBLIC: int vs_bg(SCR *); 661 */ 662 int 663 vs_bg(SCR *sp) 664 { 665 GS *gp; 666 SCR *nsp; 667 668 gp = sp->gp; 669 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); 677 } 678 679 /* Move the old screen to the background queue. */ 680 TAILQ_REMOVE(gp->dq, sp, q); 681 TAILQ_INSERT_TAIL(gp->hq, sp, q); 682 683 /* Toss the screen map. */ 684 free(_HMAP(sp)); 685 _HMAP(sp) = NULL; 686 687 /* Switch screens. */ 688 sp->nextdisp = nsp; 689 F_SET(sp, SC_SSWITCH); 690 691 return (0); 692 } 693 694 /* 695 * vs_swap -- 696 * Swap the current screen with a backgrounded one. 697 * 698 * PUBLIC: int vs_swap(SCR *, SCR **, char *); 699 */ 700 int 701 vs_swap(SCR *sp, SCR **nspp, char *name) 702 { 703 GS *gp; 704 SCR *nsp, *list[2]; 705 706 gp = sp->gp; 707 708 /* Get the specified background screen. */ 709 if ((*nspp = nsp = vs_getbg(sp, name)) == NULL) 710 return (0); 711 712 /* 713 * Save the old screen's cursor information. 714 * 715 * XXX 716 * If called after file_end(), and the underlying file was a tmp 717 * file, it may have gone away. 718 */ 719 if (sp->frp != NULL) { 720 sp->frp->lno = sp->lno; 721 sp->frp->cno = sp->cno; 722 F_SET(sp->frp, FR_CURSORSET); 723 } 724 725 /* Switch screens. */ 726 sp->nextdisp = nsp; 727 F_SET(sp, SC_SSWITCH); 728 729 /* Initialize terminal information. */ 730 VIP(nsp)->srows = VIP(sp)->srows; 731 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; 736 737 /* 738 * Small screens: see vs_refresh.c, section 6a. 739 * 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. 743 */ 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; 752 753 /* Reset the length of the default scroll. */ 754 nsp->defscroll = nsp->t_maxrows / 2; 755 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); 759 760 /* Fill the map. */ 761 nsp->gp = sp->gp; 762 if (vs_sm_fill(nsp, nsp->lno, P_FILL)) 763 return (1); 764 765 /* 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. 770 */ 771 TAILQ_REMOVE(gp->hq, nsp, q); 772 TAILQ_INSERT_AFTER(gp->dq, sp, nsp, q); 773 774 /* 775 * Don't change the screen's cursor information other than to 776 * note that the cursor is wrong. 777 */ 778 F_SET(VIP(nsp), VIP_CUR_INVALID); 779 780 /* Draw the new screen from scratch, and add a status line. */ 781 F_SET(nsp, SC_SCR_REDRAW | SC_STATUS); 782 783 list[0] = nsp; list[1] = NULL; 784 (void)gp->scr_discard(sp, list); 785 786 return (0); 787 } 788 789 /* 790 * vs_resize -- 791 * Change the absolute size of the current screen. 792 * 793 * PUBLIC: int vs_resize(SCR *, long, adj_t); 794 */ 795 int 796 vs_resize(SCR *sp, long int count, adj_t adj) 797 { 798 GS *gp; 799 SCR *g, *s, *prev, *next, *list[3] = {NULL, NULL, NULL}; 800 size_t g_off, s_off; 801 802 gp = sp->gp; 803 804 /* 805 * Figure out which screens will grow, which will shrink, and 806 * make sure it's possible. 807 */ 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; 819 } 820 } 821 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; 838 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); 865 } 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); 871 } 872 g_off = -count; 873 } 874 } 875 876 /* 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. 880 */ 881 s->rows += -count; 882 s->roff += s_off; 883 g->rows += count; 884 g->roff += g_off; 885 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); 892 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); 899 900 /* XXXX */ 901 list[0] = g; list[1] = s; 902 gp->scr_discard(0, list); 903 904 return (0); 905 } 906 907 /* 908 * vs_getbg -- 909 * Get the specified background screen, or, if name is NULL, the first 910 * background screen. 911 */ 912 static SCR * 913 vs_getbg(SCR *sp, char *name) 914 { 915 GS *gp; 916 SCR *nsp; 917 char *p; 918 919 gp = sp->gp; 920 921 /* If name is NULL, return the first background screen on the list. */ 922 if (name == NULL) 923 return (TAILQ_FIRST(gp->hq)); 924 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); 931 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; 940 } 941 if (nsp != NULL) 942 return (nsp); 943 944 return (NULL); 945 } 946