[BACK]Return to screen.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/screen.c, Revision 1.68

1.68    ! nicm        1: /* $OpenBSD: screen.c,v 1.67 2020/05/16 15:49:20 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.37      nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20:
1.3       nicm       21: #include <stdlib.h>
1.1       nicm       22: #include <string.h>
1.16      nicm       23: #include <unistd.h>
1.47      nicm       24: #include <vis.h>
1.1       nicm       25:
                     26: #include "tmux.h"
                     27:
1.52      nicm       28: /* Selected area in screen. */
                     29: struct screen_sel {
                     30:        int              hidden;
                     31:        int              rectangle;
                     32:        int              modekeys;
                     33:
                     34:        u_int            sx;
                     35:        u_int            sy;
                     36:
                     37:        u_int            ex;
                     38:        u_int            ey;
                     39:
                     40:        struct grid_cell cell;
                     41: };
                     42:
                     43: /* Entry on title stack. */
1.48      nicm       44: struct screen_title_entry {
                     45:        char                            *text;
                     46:
                     47:        TAILQ_ENTRY(screen_title_entry)  entry;
                     48: };
                     49: TAILQ_HEAD(screen_titles, screen_title_entry);
                     50:
1.66      nicm       51: static void    screen_resize_y(struct screen *, u_int, int, u_int *);
1.67      nicm       52: static void    screen_reflow(struct screen *, u_int, u_int *, u_int *, int);
1.42      nicm       53:
1.48      nicm       54: /* Free titles stack. */
                     55: static void
                     56: screen_free_titles(struct screen *s)
                     57: {
                     58:        struct screen_title_entry       *title_entry;
                     59:
                     60:        if (s->titles == NULL)
                     61:                return;
                     62:
                     63:        while ((title_entry = TAILQ_FIRST(s->titles)) != NULL) {
                     64:                TAILQ_REMOVE(s->titles, title_entry, entry);
                     65:                free(title_entry->text);
                     66:                free(title_entry);
                     67:        }
                     68:
                     69:        free(s->titles);
                     70:        s->titles = NULL;
                     71: }
                     72:
1.1       nicm       73: /* Create a new screen. */
                     74: void
                     75: screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
                     76: {
                     77:        s->grid = grid_create(sx, sy, hlimit);
1.59      nicm       78:        s->saved_grid = NULL;
                     79:
1.34      nicm       80:        s->title = xstrdup("");
1.48      nicm       81:        s->titles = NULL;
1.68    ! nicm       82:        s->path = NULL;
1.1       nicm       83:
1.21      nicm       84:        s->cstyle = 0;
1.20      nicm       85:        s->ccolour = xstrdup("");
1.3       nicm       86:        s->tabs = NULL;
1.52      nicm       87:        s->sel = NULL;
1.3       nicm       88:
1.64      nicm       89:        s->write_list = NULL;
                     90:
1.1       nicm       91:        screen_reinit(s);
                     92: }
                     93:
                     94: /* Reinitialise screen. */
                     95: void
                     96: screen_reinit(struct screen *s)
                     97: {
                     98:        s->cx = 0;
                     99:        s->cy = 0;
                    100:
                    101:        s->rupper = 0;
                    102:        s->rlower = screen_size_y(s) - 1;
                    103:
1.16      nicm      104:        s->mode = MODE_CURSOR | MODE_WRAP;
1.12      nicm      105:
1.59      nicm      106:        if (s->saved_grid != NULL)
                    107:                screen_alternate_off(s, NULL, 0);
                    108:        s->saved_cx = UINT_MAX;
                    109:        s->saved_cy = UINT_MAX;
                    110:
1.3       nicm      111:        screen_reset_tabs(s);
1.1       nicm      112:
1.44      nicm      113:        grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy, 8);
1.1       nicm      114:
                    115:        screen_clear_selection(s);
1.48      nicm      116:        screen_free_titles(s);
1.1       nicm      117: }
                    118:
                    119: /* Destroy a screen. */
                    120: void
                    121: screen_free(struct screen *s)
                    122: {
1.52      nicm      123:        free(s->sel);
1.23      nicm      124:        free(s->tabs);
1.68    ! nicm      125:        free(s->path);
1.23      nicm      126:        free(s->title);
                    127:        free(s->ccolour);
1.48      nicm      128:
1.64      nicm      129:        if (s->write_list != NULL)
                    130:                screen_write_free_list(s);
                    131:
1.59      nicm      132:        if (s->saved_grid != NULL)
                    133:                grid_destroy(s->saved_grid);
1.1       nicm      134:        grid_destroy(s->grid);
1.48      nicm      135:
                    136:        screen_free_titles(s);
1.1       nicm      137: }
                    138:
1.3       nicm      139: /* Reset tabs to default, eight spaces apart. */
                    140: void
                    141: screen_reset_tabs(struct screen *s)
                    142: {
                    143:        u_int   i;
                    144:
1.23      nicm      145:        free(s->tabs);
1.3       nicm      146:
                    147:        if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
                    148:                fatal("bit_alloc failed");
                    149:        for (i = 8; i < screen_size_x(s); i += 8)
                    150:                bit_set(s->tabs, i);
1.21      nicm      151: }
                    152:
                    153: /* Set screen cursor style. */
                    154: void
                    155: screen_set_cursor_style(struct screen *s, u_int style)
                    156: {
1.22      nicm      157:        if (style <= 6)
1.21      nicm      158:                s->cstyle = style;
1.20      nicm      159: }
                    160:
                    161: /* Set screen cursor colour. */
                    162: void
1.36      nicm      163: screen_set_cursor_colour(struct screen *s, const char *colour)
1.20      nicm      164: {
1.23      nicm      165:        free(s->ccolour);
1.36      nicm      166:        s->ccolour = xstrdup(colour);
1.3       nicm      167: }
                    168:
1.1       nicm      169: /* Set screen title. */
1.57      nicm      170: int
1.1       nicm      171: screen_set_title(struct screen *s, const char *title)
                    172: {
1.57      nicm      173:        if (!utf8_isvalid(title))
                    174:                return (0);
1.23      nicm      175:        free(s->title);
1.57      nicm      176:        s->title = xstrdup(title);
                    177:        return (1);
1.56      nicm      178: }
                    179:
                    180: /* Set screen path. */
                    181: void
                    182: screen_set_path(struct screen *s, const char *path)
                    183: {
                    184:        free(s->path);
                    185:        utf8_stravis(&s->path, path, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
1.48      nicm      186: }
                    187:
                    188: /* Push the current title onto the stack. */
                    189: void
                    190: screen_push_title(struct screen *s)
                    191: {
                    192:        struct screen_title_entry *title_entry;
                    193:
                    194:        if (s->titles == NULL) {
                    195:                s->titles = xmalloc(sizeof *s->titles);
                    196:                TAILQ_INIT(s->titles);
                    197:        }
                    198:        title_entry = xmalloc(sizeof *title_entry);
                    199:        title_entry->text = xstrdup(s->title);
                    200:        TAILQ_INSERT_HEAD(s->titles, title_entry, entry);
                    201: }
                    202:
                    203: /*
                    204:  * Pop a title from the stack and set it as the screen title. If the stack is
                    205:  * empty, do nothing.
                    206:  */
                    207: void
                    208: screen_pop_title(struct screen *s)
                    209: {
                    210:        struct screen_title_entry *title_entry;
                    211:
                    212:        if (s->titles == NULL)
                    213:                return;
                    214:
                    215:        title_entry = TAILQ_FIRST(s->titles);
                    216:        if (title_entry != NULL) {
                    217:                screen_set_title(s, title_entry->text);
                    218:
                    219:                TAILQ_REMOVE(s->titles, title_entry, entry);
                    220:                free(title_entry->text);
                    221:                free(title_entry);
                    222:        }
1.1       nicm      223: }
                    224:
1.67      nicm      225: /* Resize screen with options. */
1.1       nicm      226: void
1.62      nicm      227: screen_resize_cursor(struct screen *s, u_int sx, u_int sy, int reflow,
1.67      nicm      228:     int eat_empty, int cursor)
1.1       nicm      229: {
1.67      nicm      230:        u_int   cx = s->cx, cy = s->grid->hsize + s->cy;
1.64      nicm      231:
1.65      nicm      232:        if (s->write_list != NULL)
1.64      nicm      233:                screen_write_free_list(s);
1.62      nicm      234:
1.63      nicm      235:        log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)",
                    236:            __func__, sx, sy, screen_size_x(s), screen_size_y(s), s->cx, s->cy,
1.67      nicm      237:            cx, cy);
1.62      nicm      238:
1.1       nicm      239:        if (sx < 1)
                    240:                sx = 1;
                    241:        if (sy < 1)
                    242:                sy = 1;
                    243:
1.3       nicm      244:        if (sx != screen_size_x(s)) {
1.54      nicm      245:                s->grid->sx = sx;
1.3       nicm      246:                screen_reset_tabs(s);
1.49      nicm      247:        } else
                    248:                reflow = 0;
1.3       nicm      249:
1.1       nicm      250:        if (sy != screen_size_y(s))
1.67      nicm      251:                screen_resize_y(s, sy, eat_empty, &cy);
1.24      nicm      252:
                    253:        if (reflow)
1.67      nicm      254:                screen_reflow(s, sx, &cx, &cy, cursor);
1.62      nicm      255:
1.67      nicm      256:        if (cy >= s->grid->hsize) {
                    257:                s->cx = cx;
                    258:                s->cy = cy - s->grid->hsize;
1.62      nicm      259:        } else {
                    260:                s->cx = 0;
                    261:                s->cy = 0;
                    262:        }
1.67      nicm      263:
1.63      nicm      264:        log_debug("%s: cursor finished at %u,%u = %u,%u", __func__, s->cx,
1.67      nicm      265:            s->cy, cx, cy);
1.65      nicm      266:
                    267:        if (s->write_list != NULL)
                    268:                screen_write_make_list(s);
1.62      nicm      269: }
                    270:
                    271: /* Resize screen. */
                    272: void
                    273: screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
                    274: {
1.67      nicm      275:        screen_resize_cursor(s, sx, sy, reflow, 1, 1);
1.1       nicm      276: }
                    277:
1.41      nicm      278: static void
1.66      nicm      279: screen_resize_y(struct screen *s, u_int sy, int eat_empty, u_int *cy)
1.1       nicm      280: {
                    281:        struct grid     *gd = s->grid;
1.4       nicm      282:        u_int            needed, available, oldy, i;
1.1       nicm      283:
                    284:        if (sy == 0)
                    285:                fatalx("zero size");
1.4       nicm      286:        oldy = screen_size_y(s);
                    287:
1.12      nicm      288:        /*
1.4       nicm      289:         * When resizing:
                    290:         *
                    291:         * If the height is decreasing, delete lines from the bottom until
                    292:         * hitting the cursor, then push lines from the top into the history.
1.12      nicm      293:         *
1.40      nicm      294:         * When increasing, pull as many lines as possible from scrolled
                    295:         * history (not explicitly cleared from view) to the top, then fill the
                    296:         * remaining with blanks at the bottom.
1.4       nicm      297:         */
1.1       nicm      298:
                    299:        /* Size decreasing. */
1.4       nicm      300:        if (sy < oldy) {
                    301:                needed = oldy - sy;
1.1       nicm      302:
1.4       nicm      303:                /* Delete as many lines as possible from the bottom. */
1.62      nicm      304:                if (eat_empty) {
                    305:                        available = oldy - 1 - s->cy;
                    306:                        if (available > 0) {
                    307:                                if (available > needed)
                    308:                                        available = needed;
                    309:                                grid_view_delete_lines(gd, oldy - available,
                    310:                                    available, 8);
                    311:                        }
                    312:                        needed -= available;
1.1       nicm      313:                }
                    314:
1.4       nicm      315:                /*
1.8       nicm      316:                 * Now just increase the history size, if possible, to take
                    317:                 * over the lines which are left. If history is off, delete
                    318:                 * lines from the top.
1.4       nicm      319:                 */
1.8       nicm      320:                available = s->cy;
1.40      nicm      321:                if (gd->flags & GRID_HISTORY) {
                    322:                        gd->hscrolled += needed;
1.8       nicm      323:                        gd->hsize += needed;
1.40      nicm      324:                } else if (needed > 0 && available > 0) {
1.8       nicm      325:                        if (available > needed)
                    326:                                available = needed;
1.44      nicm      327:                        grid_view_delete_lines(gd, 0, available, 8);
1.66      nicm      328:                        (*cy) -= available;
1.8       nicm      329:                }
1.12      nicm      330:        }
1.1       nicm      331:
1.51      nicm      332:        /* Resize line array. */
                    333:        grid_adjust_lines(gd, gd->hsize + sy);
1.1       nicm      334:
                    335:        /* Size increasing. */
1.4       nicm      336:        if (sy > oldy) {
                    337:                needed = sy - oldy;
                    338:
1.8       nicm      339:                /*
1.41      nicm      340:                 * Try to pull as much as possible out of scrolled history, if
                    341:                 * is is enabled.
1.8       nicm      342:                 */
1.40      nicm      343:                available = gd->hscrolled;
1.8       nicm      344:                if (gd->flags & GRID_HISTORY && available > 0) {
1.4       nicm      345:                        if (available > needed)
                    346:                                available = needed;
1.40      nicm      347:                        gd->hscrolled -= available;
1.4       nicm      348:                        gd->hsize -= available;
1.8       nicm      349:                } else
                    350:                        available = 0;
1.4       nicm      351:                needed -= available;
                    352:
                    353:                /* Then fill the rest in with blanks. */
1.11      nicm      354:                for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
1.61      nicm      355:                        grid_empty_line(gd, i, 8);
1.1       nicm      356:        }
                    357:
1.4       nicm      358:        /* Set the new size, and reset the scroll region. */
1.1       nicm      359:        gd->sy = sy;
                    360:        s->rupper = 0;
                    361:        s->rlower = screen_size_y(s) - 1;
                    362: }
                    363:
                    364: /* Set selection. */
                    365: void
1.14      nicm      366: screen_set_selection(struct screen *s, u_int sx, u_int sy,
1.52      nicm      367:     u_int ex, u_int ey, u_int rectangle, int modekeys, struct grid_cell *gc)
1.1       nicm      368: {
1.52      nicm      369:        if (s->sel == NULL)
                    370:                s->sel = xcalloc(1, sizeof *s->sel);
1.1       nicm      371:
1.52      nicm      372:        memcpy(&s->sel->cell, gc, sizeof s->sel->cell);
                    373:        s->sel->hidden = 0;
                    374:        s->sel->rectangle = rectangle;
                    375:        s->sel->modekeys = modekeys;
                    376:
                    377:        s->sel->sx = sx;
                    378:        s->sel->sy = sy;
                    379:        s->sel->ex = ex;
                    380:        s->sel->ey = ey;
1.1       nicm      381: }
                    382:
                    383: /* Clear selection. */
                    384: void
                    385: screen_clear_selection(struct screen *s)
                    386: {
1.52      nicm      387:        free(s->sel);
                    388:        s->sel = NULL;
1.1       nicm      389: }
                    390:
1.45      nicm      391: /* Hide selection. */
                    392: void
                    393: screen_hide_selection(struct screen *s)
                    394: {
1.52      nicm      395:        if (s->sel != NULL)
                    396:                s->sel->hidden = 1;
1.45      nicm      397: }
                    398:
1.1       nicm      399: /* Check if cell in selection. */
                    400: int
                    401: screen_check_selection(struct screen *s, u_int px, u_int py)
                    402: {
1.52      nicm      403:        struct screen_sel       *sel = s->sel;
1.28      nicm      404:        u_int                    xx;
1.1       nicm      405:
1.52      nicm      406:        if (sel == NULL || sel->hidden)
1.1       nicm      407:                return (0);
                    408:
1.52      nicm      409:        if (sel->rectangle) {
1.14      nicm      410:                if (sel->sy < sel->ey) {
                    411:                        /* start line < end line -- downward selection. */
                    412:                        if (py < sel->sy || py > sel->ey)
                    413:                                return (0);
                    414:                } else if (sel->sy > sel->ey) {
                    415:                        /* start line > end line -- upward selection. */
                    416:                        if (py > sel->sy || py < sel->ey)
                    417:                                return (0);
                    418:                } else {
                    419:                        /* starting line == ending line. */
                    420:                        if (py != sel->sy)
                    421:                                return (0);
                    422:                }
                    423:
                    424:                /*
                    425:                 * Need to include the selection start row, but not the cursor
                    426:                 * row, which means the selection changes depending on which
                    427:                 * one is on the left.
                    428:                 */
                    429:                if (sel->ex < sel->sx) {
                    430:                        /* Cursor (ex) is on the left. */
1.17      nicm      431:                        if (px < sel->ex)
1.14      nicm      432:                                return (0);
                    433:
                    434:                        if (px > sel->sx)
                    435:                                return (0);
                    436:                } else {
                    437:                        /* Selection start (sx) is on the left. */
                    438:                        if (px < sel->sx)
                    439:                                return (0);
                    440:
1.17      nicm      441:                        if (px > sel->ex)
1.14      nicm      442:                                return (0);
                    443:                }
                    444:        } else {
                    445:                /*
                    446:                 * Like emacs, keep the top-left-most character, and drop the
                    447:                 * bottom-right-most, regardless of copy direction.
                    448:                 */
                    449:                if (sel->sy < sel->ey) {
                    450:                        /* starting line < ending line -- downward selection. */
                    451:                        if (py < sel->sy || py > sel->ey)
                    452:                                return (0);
                    453:
1.28      nicm      454:                        if (py == sel->sy && px < sel->sx)
1.29      nicm      455:                                return (0);
1.28      nicm      456:
1.53      nicm      457:                        if (sel->modekeys == MODEKEY_EMACS)
                    458:                                xx = (sel->ex == 0 ? 0 : sel->ex - 1);
                    459:                        else
                    460:                                xx = sel->ex;
                    461:                        if (py == sel->ey && px > xx)
1.14      nicm      462:                                return (0);
                    463:                } else if (sel->sy > sel->ey) {
                    464:                        /* starting line > ending line -- upward selection. */
                    465:                        if (py > sel->sy || py < sel->ey)
                    466:                                return (0);
                    467:
1.28      nicm      468:                        if (py == sel->ey && px < sel->ex)
                    469:                                return (0);
                    470:
                    471:                        if (sel->modekeys == MODEKEY_EMACS)
                    472:                                xx = sel->sx - 1;
                    473:                        else
                    474:                                xx = sel->sx;
1.46      nicm      475:                        if (py == sel->sy && (sel->sx == 0 || px > xx))
1.14      nicm      476:                                return (0);
                    477:                } else {
                    478:                        /* starting line == ending line. */
                    479:                        if (py != sel->sy)
                    480:                                return (0);
                    481:
                    482:                        if (sel->ex < sel->sx) {
                    483:                                /* cursor (ex) is on the left */
1.28      nicm      484:                                if (sel->modekeys == MODEKEY_EMACS)
                    485:                                        xx = sel->sx - 1;
                    486:                                else
                    487:                                        xx = sel->sx;
                    488:                                if (px > xx || px < sel->ex)
1.14      nicm      489:                                        return (0);
                    490:                        } else {
                    491:                                /* selection start (sx) is on the left */
1.53      nicm      492:                                if (sel->modekeys == MODEKEY_EMACS)
                    493:                                        xx = (sel->ex == 0 ? 0 : sel->ex - 1);
                    494:                                else
                    495:                                        xx = sel->ex;
                    496:                                if (px < sel->sx || px > xx)
1.14      nicm      497:                                        return (0);
                    498:                        }
                    499:                }
1.1       nicm      500:        }
                    501:
                    502:        return (1);
1.43      nicm      503: }
                    504:
                    505: /* Get selected grid cell. */
                    506: void
                    507: screen_select_cell(struct screen *s, struct grid_cell *dst,
                    508:     const struct grid_cell *src)
                    509: {
1.52      nicm      510:        if (s->sel == NULL || s->sel->hidden)
1.43      nicm      511:                return;
                    512:
1.52      nicm      513:        memcpy(dst, &s->sel->cell, sizeof *dst);
1.43      nicm      514:
                    515:        utf8_copy(&dst->data, &src->data);
                    516:        dst->attr = dst->attr & ~GRID_ATTR_CHARSET;
                    517:        dst->attr |= src->attr & GRID_ATTR_CHARSET;
                    518:        dst->flags = src->flags;
1.24      nicm      519: }
                    520:
                    521: /* Reflow wrapped lines. */
1.42      nicm      522: static void
1.67      nicm      523: screen_reflow(struct screen *s, u_int new_x, u_int *cx, u_int *cy, int cursor)
1.24      nicm      524: {
1.62      nicm      525:        u_int   wx, wy;
1.54      nicm      526:
1.67      nicm      527:        if (cursor) {
                    528:                grid_wrap_position(s->grid, *cx, *cy, &wx, &wy);
                    529:                log_debug("%s: cursor %u,%u is %u,%u", __func__, *cx, *cy, wx,
                    530:                    wy);
                    531:        }
1.54      nicm      532:
                    533:        grid_reflow(s->grid, new_x);
                    534:
1.67      nicm      535:        if (cursor) {
                    536:                grid_unwrap_position(s->grid, cx, cy, wx, wy);
                    537:                log_debug("%s: new cursor is %u,%u", __func__, *cx, *cy);
                    538:        }
                    539:        else {
                    540:                *cx = 0;
                    541:                *cy = s->grid->hsize;
                    542:        }
1.59      nicm      543: }
                    544:
                    545: /*
                    546:  * Enter alternative screen mode. A copy of the visible screen is saved and the
                    547:  * history is not updated.
                    548:  */
                    549: void
                    550: screen_alternate_on(struct screen *s, struct grid_cell *gc, int cursor)
                    551: {
                    552:        u_int   sx, sy;
                    553:
                    554:        if (s->saved_grid != NULL)
                    555:                return;
                    556:        sx = screen_size_x(s);
                    557:        sy = screen_size_y(s);
                    558:
                    559:        s->saved_grid = grid_create(sx, sy, 0);
                    560:        grid_duplicate_lines(s->saved_grid, 0, s->grid, screen_hsize(s), sy);
                    561:        if (cursor) {
                    562:                s->saved_cx = s->cx;
                    563:                s->saved_cy = s->cy;
                    564:        }
                    565:        memcpy(&s->saved_cell, gc, sizeof s->saved_cell);
                    566:
                    567:        grid_view_clear(s->grid, 0, 0, sx, sy, 8);
                    568:
1.60      nicm      569:        s->saved_flags = s->grid->flags;
1.59      nicm      570:        s->grid->flags &= ~GRID_HISTORY;
                    571: }
                    572:
                    573: /* Exit alternate screen mode and restore the copied grid. */
                    574: void
                    575: screen_alternate_off(struct screen *s, struct grid_cell *gc, int cursor)
                    576: {
                    577:        u_int   sx, sy;
                    578:
                    579:        /*
                    580:         * Restore the cursor position and cell. This happens even if not
                    581:         * currently in the alternate screen.
                    582:         */
                    583:        if (cursor && s->saved_cx != UINT_MAX && s->saved_cy != UINT_MAX) {
                    584:                s->cx = s->saved_cx;
                    585:                if (s->cx > screen_size_x(s) - 1)
                    586:                        s->cx = screen_size_x(s) - 1;
                    587:                s->cy = s->saved_cy;
                    588:                if (s->cy > screen_size_y(s) - 1)
                    589:                        s->cy = screen_size_y(s) - 1;
                    590:                if (gc != NULL)
                    591:                        memcpy(gc, &s->saved_cell, sizeof *gc);
                    592:        }
                    593:
                    594:        if (s->saved_grid == NULL)
                    595:                return;
                    596:        sx = screen_size_x(s);
                    597:        sy = screen_size_y(s);
                    598:
                    599:        /*
                    600:         * If the current size is bigger, temporarily resize to the old size
                    601:         * before copying back.
                    602:         */
                    603:        if (sy > s->saved_grid->sy)
                    604:                screen_resize(s, sx, s->saved_grid->sy, 1);
                    605:
                    606:        /* Restore the saved grid. */
                    607:        grid_duplicate_lines(s->grid, screen_hsize(s), s->saved_grid, 0, sy);
                    608:
                    609:        /*
                    610:         * Turn history back on (so resize can use it) and then resize back to
                    611:         * the current size.
                    612:         */
1.60      nicm      613:        if (s->saved_flags & GRID_HISTORY)
                    614:                s->grid->flags |= GRID_HISTORY;
1.59      nicm      615:        if (sy > s->saved_grid->sy || sx != s->saved_grid->sx)
                    616:                screen_resize(s, sx, sy, 1);
                    617:
                    618:        grid_destroy(s->saved_grid);
                    619:        s->saved_grid = NULL;
1.1       nicm      620: }