commit 3e73558cfd28618457cb6fc98a35a6f627381964
parent bc29ff001086358d5dbc2c6a8429f5aaf7410ee3
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Wed, 31 Aug 2022 16:54:29 +0200
Extract connection code form split functions
CONNECTION_FORMULA contains code for connecting the panes together
Diffstat:
M | src/pane.c | | | 62 | ++++++++++++++++++++++++++------------------------------------ |
1 file changed, 26 insertions(+), 36 deletions(-)
diff --git a/src/pane.c b/src/pane.c
@@ -120,6 +120,30 @@ void draw_border(T self)
} \
}
+#define wrap_or_slap(dir, index) \
+ dir(child) = (dir(self) && dir(self) != self) ? dir(self) : \
+ self->children[index];
+
+#define CONNECTION_FORMULA(af, ab, s1, s2) \
+ { \
+ T child; \
+ for (i = 0; i < self->children_num; i++) { \
+ child = self->children[i]; \
+ wrap_or_slap(s1, i); \
+ wrap_or_slap(s2, i); \
+ } \
+ \
+ for (i = 1; i < self->children_num; i++) \
+ ab(self->children[i]) = self->children[i - 1]; \
+ child = self->children[0]; \
+ wrap_or_slap(ab, self->children_num - 1); \
+ \
+ for (i = 0; i < self->children_num - 1; i++) \
+ af(self->children[i]) = self->children[i + 1]; \
+ child = self->children[self->children_num - 1]; \
+ wrap_or_slap(af, 0); \
+ }
+
int calc_children(T self)
{
if (self->horizontal)
@@ -213,10 +237,6 @@ void split(T self, size_t count, va_list ap)
pane_resize(self);
}
-#define wrap_or_slap(dir, index) \
- dir(child) = (dir(self) && dir(self) != self) ? dir(self) : \
- self->children[index];
-
T *pane_split(T self, size_t count, ...)
{
size_t i;
@@ -227,22 +247,7 @@ T *pane_split(T self, size_t count, ...)
split(self, count, ap);
va_end(ap);
- T child;
- for (i = 0; i < self->children_num; i++) {
- child = self->children[i];
- wrap_or_slap(UP, i);
- wrap_or_slap(DOWN, i);
- }
-
- for (i = 1; i < self->children_num; i++)
- LEFT(self->children[i]) = self->children[i - 1];
- child = self->children[0];
- wrap_or_slap(LEFT, self->children_num - 1);
-
- for (i = 0; i < self->children_num - 1; i++)
- RIGHT(self->children[i]) = self->children[i + 1];
- child = self->children[self->children_num - 1];
- wrap_or_slap(RIGHT, 0);
+ CONNECTION_FORMULA(RIGHT, LEFT, UP, DOWN)
return self->children;
}
@@ -257,22 +262,7 @@ T *pane_vsplit(T self, size_t count, ...)
split(self, count, ap);
va_end(ap);
- T child;
- for (i = 0; i < self->children_num; i++) {
- child = self->children[i];
- wrap_or_slap(LEFT, i);
- wrap_or_slap(RIGHT, i);
- }
-
- for (i = 1; i < self->children_num; i++)
- UP(self->children[i]) = self->children[i - 1];
- child = self->children[0];
- wrap_or_slap(UP, self->children_num - 1);
-
- for (i = 0; i < self->children_num - 1; i++)
- DOWN(self->children[i]) = self->children[i + 1];
- child = self->children[self->children_num - 1];
- wrap_or_slap(DOWN, 0);
+ CONNECTION_FORMULA(DOWN, UP, LEFT, RIGHT)
return self->children;
}