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