From 4a9a021b2ccb9f14ca7bc0fe198a5fe2e6c1d0fe Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 12 Jul 2021 18:34:43 +0530 Subject: [PATCH] DRYer --- kitty/history.c | 9 +++------ kitty/line-buf.c | 17 ++++------------- kitty/lineops.h | 16 ++++++++++++++++ kitty/rewrap.h | 4 ++-- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/kitty/history.c b/kitty/history.c index 793ba5bac..1fc161240 100644 --- a/kitty/history.c +++ b/kitty/history.c @@ -147,10 +147,7 @@ init_line(HistoryBuf *self, index_type num, Line *l) { // Initialize the line l, setting its pointer to the offsets for the line at index (buffer position) num l->cpu_cells = cpu_lineptr(self, num); l->gpu_cells = gpu_lineptr(self, num); - l->continued = *attrptr(self, num) & CONTINUED_MASK; - l->has_dirty_text = *attrptr(self, num) & TEXT_DIRTY_MASK ? true : false; - l->is_output_start = *attrptr(self, num) & OUTPUT_START_MASK ? true : false; - l->is_prompt_start = *attrptr(self, num) & PROMPT_START_MASK ? true : false; + copy_line_attrs_to_line(*attrptr(self, num), l); } void @@ -250,7 +247,7 @@ void historybuf_add_line(HistoryBuf *self, const Line *line, ANSIBuf *as_ansi_buf) { index_type idx = historybuf_push(self, as_ansi_buf); copy_line(line, self->line); - *attrptr(self, idx) = (line->continued & CONTINUED_MASK) | (line->has_dirty_text ? TEXT_DIRTY_MASK : 0) | (line->is_output_start ? OUTPUT_START_MASK : 0) | (line->is_output_start ? PROMPT_START_MASK : 0); + *attrptr(self, idx) = line_attrs_from_line(line); } bool @@ -532,7 +529,7 @@ HistoryBuf *alloc_historybuf(unsigned int lines, unsigned int columns, unsigned #define is_src_line_continued(src_y) (map_src_index(src_y) < src->ynum - 1 ? (*attrptr(src, map_src_index(src_y + 1)) & CONTINUED_MASK) : false) -#define next_dest_line(cont) *attrptr(dest, historybuf_push(dest, as_ansi_buf)) = (cont ? CONTINUED_MASK : 0) | (src->line->is_output_start ? OUTPUT_START_MASK : 0) | (src->line->is_prompt_start ? PROMPT_START_MASK : 0); dest->line->continued = cont; +#define next_dest_line(cont) *attrptr(dest, historybuf_push(dest, as_ansi_buf)) = (cont ? CONTINUED_MASK : 0) | line_attrs_from_line(src->line); dest->line->continued = cont; #define first_dest_line next_dest_line(false); diff --git a/kitty/line-buf.c b/kitty/line-buf.c index d075b145a..35bb9aae2 100644 --- a/kitty/line-buf.c +++ b/kitty/line-buf.c @@ -141,10 +141,7 @@ void linebuf_init_line(LineBuf *self, index_type idx) { self->line->ynum = idx; self->line->xnum = self->xnum; - self->line->continued = self->line_attrs[idx] & CONTINUED_MASK ? true : false; - self->line->has_dirty_text = self->line_attrs[idx] & TEXT_DIRTY_MASK ? true : false; - self->line->is_prompt_start = self->line_attrs[idx] & PROMPT_START_MASK ? true : false; - self->line->is_output_start = self->line_attrs[idx] & OUTPUT_START_MASK ? true : false; + copy_line_attrs_to_line(self->line_attrs[idx], self->line); init_line(self, self->line, self->line_map[idx]); } @@ -232,10 +229,7 @@ create_line_copy_inner(LineBuf* self, index_type y) { src.xnum = self->xnum; line->xnum = self->xnum; if (!allocate_line_storage(line, 0)) { Py_CLEAR(line); return PyErr_NoMemory(); } line->ynum = y; - line->continued = self->line_attrs[y] & CONTINUED_MASK ? true : false; - line->has_dirty_text = self->line_attrs[y] & TEXT_DIRTY_MASK ? true : false; - line->is_output_start = self->line_attrs[y] & OUTPUT_START_MASK ? true : false; - line->is_prompt_start = self->line_attrs[y] & PROMPT_START_MASK ? true : false; + copy_line_attrs_to_line(self->line_attrs[y], line); init_line(self, &src, self->line_map[y]); copy_line(&src, line); return (PyObject*)line; @@ -257,10 +251,7 @@ copy_line_to(LineBuf *self, PyObject *args) { if (!PyArg_ParseTuple(args, "IO!", &y, &Line_Type, &dest)) return NULL; src.xnum = self->xnum; dest->xnum = self->xnum; dest->ynum = y; - dest->continued = self->line_attrs[y] & CONTINUED_MASK; - dest->has_dirty_text = self->line_attrs[y] & TEXT_DIRTY_MASK; - dest->is_output_start = self->line_attrs[y] & OUTPUT_START_MASK; - dest->is_prompt_start = self->line_attrs[y] & PROMPT_START_MASK; + copy_line_attrs_to_line(self->line_attrs[y], dest); init_line(self, &src, self->line_map[y]); copy_line(&src, dest); Py_RETURN_NONE; @@ -419,7 +410,7 @@ void linebuf_copy_line_to(LineBuf *self, Line *line, index_type where) { init_line(self, self->line, self->line_map[where]); copy_line(line, self->line); - self->line_attrs[where] = TEXT_DIRTY_MASK | (line->continued ? CONTINUED_MASK : 0) | (line->is_output_start ? OUTPUT_START_MASK : 0) | (line->is_prompt_start ? PROMPT_START_MASK : 0); + self->line_attrs[where] = TEXT_DIRTY_MASK | line_attrs_from_line(line); } static PyObject* diff --git a/kitty/lineops.h b/kitty/lineops.h index 589877e8a..a06553a19 100644 --- a/kitty/lineops.h +++ b/kitty/lineops.h @@ -67,6 +67,22 @@ left_shift_line(Line *line, index_type at, index_type num) { } } +static inline void +copy_line_attrs_to_line(const line_attrs_type attrs, Line *line) { +#define S(a, m) line->a = (attrs & m) ? true: false + S(continued, CONTINUED_MASK); S(has_dirty_text, TEXT_DIRTY_MASK); + S(is_prompt_start, PROMPT_START_MASK); S(is_output_start, OUTPUT_START_MASK); +#undef S +} + +static inline line_attrs_type +line_attrs_from_line(const Line *line) { +#define S(a, m) (line->a ? m : 0) + return S(continued, CONTINUED_MASK) | S(has_dirty_text, TEXT_DIRTY_MASK) | S(is_prompt_start, PROMPT_START_MASK) | S(is_output_start, OUTPUT_START_MASK); +#undef S +} + + typedef Line*(get_line_func)(void *, int); void line_clear_text(Line *self, unsigned int at, unsigned int num, char_type ch); void line_apply_cursor(Line *self, Cursor *cursor, unsigned int at, unsigned int num, bool clear_char); diff --git a/kitty/rewrap.h b/kitty/rewrap.h index d331d39bf..ff49752d9 100644 --- a/kitty/rewrap.h +++ b/kitty/rewrap.h @@ -16,10 +16,10 @@ #endif #ifndef init_dest_line -#define init_dest_line(dest_y) init_line(dest, dest->line, dest->line_map[dest_y]); dest->line->continued = dest->line_attrs[dest_y] & CONTINUED_MASK ? true : false; dest->line->is_output_start = dest->line_attrs[dest_y] & OUTPUT_START_MASK ? true : false; dest->line->is_prompt_start = dest->line_attrs[dest_y] & PROMPT_START_MASK ? true : false; +#define init_dest_line(dest_y) init_line(dest, dest->line, dest->line_map[dest_y]); copy_line_attrs_to_line(dest->line_attrs[dest_y], dest->line); #endif -#define set_dest_line_attrs(dest_y, continued) dest->line_attrs[dest_y] = (continued ? CONTINUED_MASK : 0) | (src->line->is_output_start ? OUTPUT_START_MASK : 0) | (src->line->is_prompt_start ? PROMPT_START_MASK : 0); +#define set_dest_line_attrs(dest_y, continued) dest->line_attrs[dest_y] = (continued ? CONTINUED_MASK : 0) | line_attrs_from_line(src->line); #ifndef first_dest_line #define first_dest_line init_dest_line(0); set_dest_line_attrs(0, false)