[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.14

1.14    ! nicm        1: /* $OpenBSD: screen.c,v 1.13 2010/01/03 17:12:04 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
                      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.2       nicm       23: #include <vis.h>
1.1       nicm       24:
                     25: #include "tmux.h"
                     26:
                     27: void   screen_resize_x(struct screen *, u_int);
                     28: void   screen_resize_y(struct screen *, u_int);
                     29:
                     30: /* Create a new screen. */
                     31: void
                     32: screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
                     33: {
                     34:        s->grid = grid_create(sx, sy, hlimit);
                     35:
                     36:        s->title = xstrdup("");
                     37:
1.3       nicm       38:        s->tabs = NULL;
                     39:
1.1       nicm       40:        screen_reinit(s);
                     41: }
                     42:
                     43: /* Reinitialise screen. */
                     44: void
                     45: screen_reinit(struct screen *s)
                     46: {
                     47:        s->cx = 0;
                     48:        s->cy = 0;
                     49:
                     50:        s->rupper = 0;
                     51:        s->rlower = screen_size_y(s) - 1;
                     52:
                     53:        s->mode = MODE_CURSOR;
1.12      nicm       54:
1.3       nicm       55:        screen_reset_tabs(s);
1.1       nicm       56:
1.6       nicm       57:        grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy);
1.1       nicm       58:
                     59:        screen_clear_selection(s);
                     60: }
                     61:
                     62: /* Destroy a screen. */
                     63: void
                     64: screen_free(struct screen *s)
                     65: {
1.10      nicm       66:        if (s->tabs != NULL)
                     67:                xfree(s->tabs);
1.1       nicm       68:        xfree(s->title);
                     69:        grid_destroy(s->grid);
                     70: }
                     71:
1.3       nicm       72: /* Reset tabs to default, eight spaces apart. */
                     73: void
                     74: screen_reset_tabs(struct screen *s)
                     75: {
                     76:        u_int   i;
                     77:
                     78:        if (s->tabs != NULL)
                     79:                xfree(s->tabs);
                     80:
                     81:        if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
                     82:                fatal("bit_alloc failed");
                     83:        for (i = 8; i < screen_size_x(s); i += 8)
                     84:                bit_set(s->tabs, i);
                     85: }
                     86:
1.1       nicm       87: /* Set screen title. */
                     88: void
                     89: screen_set_title(struct screen *s, const char *title)
                     90: {
1.2       nicm       91:        char    tmp[BUFSIZ];
                     92:
                     93:        strnvis(tmp, title, sizeof tmp, VIS_OCTAL|VIS_TAB|VIS_NL);
                     94:
1.1       nicm       95:        xfree(s->title);
1.2       nicm       96:        s->title = xstrdup(tmp);
1.1       nicm       97: }
                     98:
                     99: /* Resize screen. */
                    100: void
                    101: screen_resize(struct screen *s, u_int sx, u_int sy)
                    102: {
                    103:        if (sx < 1)
                    104:                sx = 1;
                    105:        if (sy < 1)
                    106:                sy = 1;
                    107:
1.3       nicm      108:        if (sx != screen_size_x(s)) {
1.1       nicm      109:                screen_resize_x(s, sx);
1.3       nicm      110:
                    111:                /*
                    112:                 * It is unclear what should happen to tabs on resize. xterm
                    113:                 * seems to try and maintain them, rxvt resets them. Resetting
                    114:                 * is simpler and more reliable so let's do that.
                    115:                 */
                    116:                screen_reset_tabs(s);
                    117:        }
                    118:
1.1       nicm      119:        if (sy != screen_size_y(s))
                    120:                screen_resize_y(s, sy);
                    121: }
                    122:
                    123: void
                    124: screen_resize_x(struct screen *s, u_int sx)
                    125: {
                    126:        struct grid             *gd = s->grid;
                    127:
                    128:        if (sx == 0)
                    129:                fatalx("zero size");
                    130:
1.7       nicm      131:        /*
                    132:         * Treat resizing horizontally simply: just ensure the cursor is
                    133:         * on-screen and change the size. Don't bother to truncate any lines -
                    134:         * then the data should be accessible if the size is then incrased.
                    135:         *
                    136:         * The only potential wrinkle is if UTF-8 double-width characters are
                    137:         * left in the last column, but UTF-8 terminals should deal with this
                    138:         * sanely.
                    139:         */
1.1       nicm      140:        if (s->cx >= sx)
                    141:                s->cx = sx - 1;
                    142:        gd->sx = sx;
                    143: }
                    144:
                    145: void
                    146: screen_resize_y(struct screen *s, u_int sy)
                    147: {
                    148:        struct grid     *gd = s->grid;
1.4       nicm      149:        u_int            needed, available, oldy, i;
1.1       nicm      150:
                    151:        if (sy == 0)
                    152:                fatalx("zero size");
1.4       nicm      153:        oldy = screen_size_y(s);
                    154:
1.12      nicm      155:        /*
1.4       nicm      156:         * When resizing:
                    157:         *
                    158:         * If the height is decreasing, delete lines from the bottom until
                    159:         * hitting the cursor, then push lines from the top into the history.
1.12      nicm      160:         *
1.4       nicm      161:         * When increasing, pull as many lines as possible from the history to
                    162:         * the top, then fill the remaining with blanks at the bottom.
                    163:         */
1.1       nicm      164:
                    165:        /* Size decreasing. */
1.4       nicm      166:        if (sy < oldy) {
                    167:                needed = oldy - sy;
1.1       nicm      168:
1.4       nicm      169:                /* Delete as many lines as possible from the bottom. */
                    170:                available = oldy - 1 - s->cy;
                    171:                if (available > 0) {
                    172:                        if (available > needed)
                    173:                                available = needed;
                    174:                        grid_view_delete_lines(gd, oldy - available, available);
1.1       nicm      175:                }
1.4       nicm      176:                needed -= available;
1.1       nicm      177:
1.4       nicm      178:                /*
1.8       nicm      179:                 * Now just increase the history size, if possible, to take
                    180:                 * over the lines which are left. If history is off, delete
                    181:                 * lines from the top.
                    182:                 *
                    183:                 * XXX Should apply history limit?
1.4       nicm      184:                 */
1.8       nicm      185:                available = s->cy;
                    186:                if (gd->flags & GRID_HISTORY)
                    187:                        gd->hsize += needed;
1.9       nicm      188:                else if (needed > 0 && available > 0) {
1.8       nicm      189:                        if (available > needed)
                    190:                                available = needed;
                    191:                        grid_view_delete_lines(gd, 0, available);
                    192:                }
1.4       nicm      193:                s->cy -= needed;
1.12      nicm      194:        }
1.1       nicm      195:
                    196:        /* Resize line arrays. */
1.11      nicm      197:        gd->linedata = xrealloc(
                    198:            gd->linedata, gd->hsize + sy, sizeof *gd->linedata);
1.1       nicm      199:
                    200:        /* Size increasing. */
1.4       nicm      201:        if (sy > oldy) {
                    202:                needed = sy - oldy;
                    203:
1.8       nicm      204:                /*
                    205:                 * Try to pull as much as possible out of the history, if is
                    206:                 * is enabled.
                    207:                 */
1.4       nicm      208:                available = gd->hsize;
1.8       nicm      209:                if (gd->flags & GRID_HISTORY && available > 0) {
1.4       nicm      210:                        if (available > needed)
                    211:                                available = needed;
                    212:                        gd->hsize -= available;
                    213:                        s->cy += available;
1.8       nicm      214:                } else
                    215:                        available = 0;
1.4       nicm      216:                needed -= available;
                    217:
                    218:                /* Then fill the rest in with blanks. */
1.11      nicm      219:                for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
                    220:                        memset(&gd->linedata[i], 0, sizeof gd->linedata[i]);
1.1       nicm      221:        }
                    222:
1.4       nicm      223:        /* Set the new size, and reset the scroll region. */
1.1       nicm      224:        gd->sy = sy;
                    225:        s->rupper = 0;
                    226:        s->rlower = screen_size_y(s) - 1;
                    227: }
                    228:
                    229: /* Set selection. */
                    230: void
1.14    ! nicm      231: screen_set_selection(struct screen *s, u_int sx, u_int sy,
        !           232:     u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc)
1.1       nicm      233: {
                    234:        struct screen_sel       *sel = &s->sel;
                    235:
                    236:        memcpy(&sel->cell, gc, sizeof sel->cell);
1.13      nicm      237:        sel->flag = 1;
1.14    ! nicm      238:        sel->rectflag = rectflag;
1.13      nicm      239:
1.14    ! nicm      240:        sel->sx = sx; sel->sy = sy;
        !           241:        sel->ex = ex; sel->ey = ey;
1.1       nicm      242: }
                    243:
                    244: /* Clear selection. */
                    245: void
                    246: screen_clear_selection(struct screen *s)
                    247: {
                    248:        struct screen_sel       *sel = &s->sel;
                    249:
                    250:        sel->flag = 0;
                    251: }
                    252:
                    253: /* Check if cell in selection. */
                    254: int
                    255: screen_check_selection(struct screen *s, u_int px, u_int py)
                    256: {
                    257:        struct screen_sel       *sel = &s->sel;
                    258:
1.14    ! nicm      259:        if (!sel->flag)
1.1       nicm      260:                return (0);
                    261:
1.14    ! nicm      262:        if (sel->rectflag) {
        !           263:                if (sel->sy < sel->ey) {
        !           264:                        /* start line < end line -- downward selection. */
        !           265:                        if (py < sel->sy || py > sel->ey)
        !           266:                                return (0);
        !           267:                } else if (sel->sy > sel->ey) {
        !           268:                        /* start line > end line -- upward selection. */
        !           269:                        if (py > sel->sy || py < sel->ey)
        !           270:                                return (0);
        !           271:                } else {
        !           272:                        /* starting line == ending line. */
        !           273:                        if (py != sel->sy)
        !           274:                                return (0);
        !           275:                }
        !           276:
        !           277:                /*
        !           278:                 * Need to include the selection start row, but not the cursor
        !           279:                 * row, which means the selection changes depending on which
        !           280:                 * one is on the left.
        !           281:                 */
        !           282:                if (sel->ex < sel->sx) {
        !           283:                        /* Cursor (ex) is on the left. */
        !           284:                        if (px <= sel->ex)
        !           285:                                return (0);
        !           286:
        !           287:                        if (px > sel->sx)
        !           288:                                return (0);
        !           289:                } else {
        !           290:                        /* Selection start (sx) is on the left. */
        !           291:                        if (px < sel->sx)
        !           292:                                return (0);
        !           293:
        !           294:                        if (px >= sel->ex)
        !           295:                                return (0);
        !           296:                }
        !           297:        } else {
        !           298:                /*
        !           299:                 * Like emacs, keep the top-left-most character, and drop the
        !           300:                 * bottom-right-most, regardless of copy direction.
        !           301:                 */
        !           302:                if (sel->sy < sel->ey) {
        !           303:                        /* starting line < ending line -- downward selection. */
        !           304:                        if (py < sel->sy || py > sel->ey)
        !           305:                                return (0);
        !           306:
        !           307:                        if ((py == sel->sy && px < sel->sx)
        !           308:                            || (py == sel->ey && px > sel->ex))
        !           309:                                return (0);
        !           310:                } else if (sel->sy > sel->ey) {
        !           311:                        /* starting line > ending line -- upward selection. */
        !           312:                        if (py > sel->sy || py < sel->ey)
        !           313:                                return (0);
        !           314:
        !           315:                        if ((py == sel->sy && px >= sel->sx)
        !           316:                            || (py == sel->ey && px < sel->ex))
        !           317:                                return (0);
        !           318:                } else {
        !           319:                        /* starting line == ending line. */
        !           320:                        if (py != sel->sy)
        !           321:                                return (0);
        !           322:
        !           323:                        if (sel->ex < sel->sx) {
        !           324:                                /* cursor (ex) is on the left */
        !           325:                                if (px > sel->sx || px < sel->ex)
        !           326:                                        return (0);
        !           327:                        } else {
        !           328:                                /* selection start (sx) is on the left */
        !           329:                                if (px < sel->sx || px > sel->ex)
        !           330:                                        return (0);
        !           331:                        }
        !           332:                }
1.1       nicm      333:        }
                    334:
                    335:        return (1);
                    336: }