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

Annotation of src/usr.bin/tmux/layout-string.c, Revision 1.5

1.5     ! nicm        1: /* $OpenBSD: layout-string.c,v 1.4 2010/01/08 17:44:27 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 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:
                     21: #include <stdlib.h>
                     22: #include <string.h>
                     23:
                     24: #include "tmux.h"
                     25:
                     26: /*
                     27:  * Figure out the pane position based on a description. Fairly simple right
                     28:  * now, just understands a set of strings: left, right, top, bottom, top-left
                     29:  * top-right, bottom-left, bottom-right.
                     30:  */
                     31:
                     32: struct layout_cell *layout_find_top(struct layout_cell *);
                     33: struct layout_cell *layout_find_bottom(struct layout_cell *);
                     34: struct layout_cell *layout_find_left(struct layout_cell *);
                     35: struct layout_cell *layout_find_right(struct layout_cell *);
                     36: struct layout_cell *layout_find_topleft(struct layout_cell *);
                     37: struct layout_cell *layout_find_topright(struct layout_cell *);
                     38: struct layout_cell *layout_find_bottomleft(struct layout_cell *);
                     39:
                     40: /* Find the cell; returns NULL if string not understood. */
                     41: struct layout_cell *
                     42: layout_find_string(struct window *w, const char *s)
                     43: {
1.2       nicm       44:        struct layout_cell      *lc;
                     45:
                     46:        lc = NULL;
1.1       nicm       47:
                     48:        if (strcasecmp(s, "top") == 0)
1.3       nicm       49:                lc = layout_find_top(w->layout_root);
1.1       nicm       50:        else if (strcasecmp(s, "bottom") == 0)
1.3       nicm       51:                lc = layout_find_bottom(w->layout_root);
1.1       nicm       52:        else if (strcasecmp(s, "left") == 0)
1.3       nicm       53:                lc = layout_find_left(w->layout_root);
1.1       nicm       54:        else if (strcasecmp(s, "right") == 0)
1.3       nicm       55:                lc = layout_find_right(w->layout_root);
1.1       nicm       56:        else if (strcasecmp(s, "top-left") == 0)
1.3       nicm       57:                lc = layout_find_topleft(w->layout_root);
1.1       nicm       58:        else if (strcasecmp(s, "top-right") == 0)
1.3       nicm       59:                lc = layout_find_topright(w->layout_root);
1.1       nicm       60:        else if (strcasecmp(s, "bottom-left") == 0)
1.3       nicm       61:                lc = layout_find_bottomleft(w->layout_root);
1.1       nicm       62:        else if (strcasecmp(s, "bottom-right") == 0)
1.3       nicm       63:                lc = layout_find_bottomright(w->layout_root);
1.1       nicm       64:
                     65:        if (lc == NULL || lc->type != LAYOUT_WINDOWPANE)
                     66:                return (NULL);
                     67:        return (lc);
                     68: }
                     69:
                     70: /*
                     71:  * Find the top cell. Because splits in the same direction are stored as a
                     72:  * list, this is just the first in the list. Return NULL if no topmost cell.
                     73:  * For an unnested cell (not split), the top cell is always itself.
                     74:  */
                     75: struct layout_cell *
                     76: layout_find_top(struct layout_cell *lc)
                     77: {
                     78:        if (lc->type == LAYOUT_WINDOWPANE)
                     79:                return (lc);
                     80:        else if (lc->type == LAYOUT_TOPBOTTOM)
                     81:                return (TAILQ_FIRST(&lc->cells));
                     82:        return (NULL);
                     83: }
                     84:
                     85: /*
                     86:  * Find the bottom cell. Similarly to the top cell, this is just the last in
                     87:  * the list.
                     88:  */
                     89: struct layout_cell *
                     90: layout_find_bottom(struct layout_cell *lc)
                     91: {
                     92:        if (lc->type == LAYOUT_WINDOWPANE)
                     93:                return (lc);
                     94:        else if (lc->type == LAYOUT_TOPBOTTOM)
                     95:                return (TAILQ_LAST(&lc->cells, layout_cells));
                     96:        return (NULL);
                     97: }
                     98:
                     99: /* Find the left cell. */
                    100: struct layout_cell *
                    101: layout_find_left(struct layout_cell *lc)
                    102: {
                    103:        if (lc->type == LAYOUT_WINDOWPANE)
                    104:                return (lc);
                    105:        else if (lc->type == LAYOUT_LEFTRIGHT)
                    106:                return (TAILQ_FIRST(&lc->cells));
                    107:        return (NULL);
                    108: }
                    109:
                    110: /* Find the right cell. */
                    111: struct layout_cell *
                    112: layout_find_right(struct layout_cell *lc)
                    113: {
                    114:        if (lc->type == LAYOUT_WINDOWPANE)
                    115:                return (lc);
                    116:        else if (lc->type == LAYOUT_LEFTRIGHT)
                    117:                return (TAILQ_LAST(&lc->cells, layout_cells));
                    118:        return (NULL);
                    119: }
                    120:
                    121: /*
                    122:  * Find the top-left cell. This means recursing until there are no more moves
                    123:  * to be made.
                    124:  */
                    125: struct layout_cell *
                    126: layout_find_topleft(struct layout_cell *lc)
                    127: {
                    128:        if (lc->type == LAYOUT_WINDOWPANE)
                    129:                return (lc);
                    130:        lc = TAILQ_FIRST(&lc->cells);
                    131:        return (layout_find_topleft(lc));
                    132: }
                    133:
                    134: /* Find the top-right cell. */
                    135: struct layout_cell *
                    136: layout_find_topright(struct layout_cell *lc)
                    137: {
                    138:        if (lc->type == LAYOUT_WINDOWPANE)
                    139:                return (lc);
                    140:        if (lc->type == LAYOUT_LEFTRIGHT)
                    141:                lc = TAILQ_LAST(&lc->cells, layout_cells);
                    142:        else
                    143:                lc = TAILQ_FIRST(&lc->cells);
                    144:        return (layout_find_topright(lc));
                    145: }
                    146:
                    147: /* Find the bottom-left cell. */
                    148: struct layout_cell *
                    149: layout_find_bottomleft(struct layout_cell *lc)
                    150: {
                    151:        if (lc->type == LAYOUT_WINDOWPANE)
                    152:                return (lc);
                    153:        if (lc->type == LAYOUT_LEFTRIGHT)
                    154:                lc = TAILQ_FIRST(&lc->cells);
                    155:        else
                    156:                lc = TAILQ_LAST(&lc->cells, layout_cells);
                    157:        return (layout_find_bottomleft(lc));
                    158: }
                    159:
                    160: /* Find the bottom-right cell. */
                    161: struct layout_cell *
                    162: layout_find_bottomright(struct layout_cell *lc)
                    163: {
                    164:        if (lc->type == LAYOUT_WINDOWPANE)
                    165:                return (lc);
                    166:        lc = TAILQ_LAST(&lc->cells, layout_cells);
                    167:        return (layout_find_bottomright(lc));
                    168: }