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