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