From 4fd8c7cfaaa7a2a1a5d84d221fbf6ec8bf7d3462 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 28 Aug 2017 11:09:54 +0530 Subject: [PATCH] DRYer --- kitty/data-types.h | 3 ++- kitty/history.c | 2 +- kitty/line-buf.c | 8 ++++---- kitty/line.c | 10 +++++----- kitty/rewrap.h | 2 +- kitty/screen.c | 8 ++++---- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/kitty/data-types.h b/kitty/data-types.h index a1cb61556..a87f5fe69 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -38,6 +38,7 @@ typedef unsigned int index_type; #define DATA_CELL_SIZE 6 +#define BLANK_CHAR 32 #define CHAR_MASK 0xFFFFFF #define ATTRS_SHIFT 24 #define ATTRS_MASK_WITHOUT_WIDTH 0xFC000000 @@ -312,7 +313,7 @@ PyTypeObject ChildMonitor_Type; for(index_type __i__ = (at); __i__ < (line)->xnum - (num); __i__++) { \ COPY_CELL(line, __i__ + (num), line, __i__) \ } \ - if ((((line)->cells[(at)].ch >> ATTRS_SHIFT) & WIDTH_MASK) != 1) (line)->cells[(at)].ch = (1 << ATTRS_SHIFT) | 32; + if ((((line)->cells[(at)].ch >> ATTRS_SHIFT) & WIDTH_MASK) != 1) (line)->cells[(at)].ch = (1 << ATTRS_SHIFT) | BLANK_CHAR; // Global functions diff --git a/kitty/history.c b/kitty/history.c index a086553e9..14bb3e8d1 100644 --- a/kitty/history.c +++ b/kitty/history.c @@ -43,7 +43,7 @@ new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) { self->line->xnum = xnum; for(index_type y = 0; y < self->ynum; y++) { self->line->chars = start_of(self, y) + 1; - for (index_type i = 0; i < self->xnum; i++) self->line->chars[i] = (1 << ATTRS_SHIFT) | 32; + for (index_type i = 0; i < self->xnum; i++) self->line->chars[i] = (1 << ATTRS_SHIFT) | BLANK_CHAR; } } } diff --git a/kitty/line-buf.c b/kitty/line-buf.c index 72e9a1f7b..db127fdcb 100644 --- a/kitty/line-buf.c +++ b/kitty/line-buf.c @@ -37,7 +37,7 @@ void linebuf_clear(LineBuf *self, char_type ch) { static PyObject* clear(LineBuf *self) { #define clear_doc "Clear all lines in this LineBuf" - linebuf_clear(self, ' '); + linebuf_clear(self, BLANK_CHAR); Py_RETURN_NONE; } @@ -75,7 +75,7 @@ new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) { self->line->xnum = xnum; for(index_type i = 0; i < ynum; i++) { self->line_map[i] = i; - clear_chars_to(self, i, ' '); + clear_chars_to(self, i, BLANK_CHAR); } } } @@ -157,7 +157,7 @@ allocate_line_storage(Line *line, bool initialize) { if (initialize) { line->cells = PyMem_Calloc(line->xnum, sizeof(Cell)); if (line->cells == NULL) { PyErr_NoMemory(); return false; } - clear_chars_in_line(line->cells, line->xnum, ' '); + clear_chars_in_line(line->cells, line->xnum, BLANK_CHAR); } else { line->cells = PyMem_Malloc(line->xnum * sizeof(Cell)); if (line->cells == NULL) { PyErr_NoMemory(); return false; } @@ -453,7 +453,7 @@ linebuf_rewrap(LineBuf *self, LineBuf *other, int *cursor_y_out, HistoryBuf *his for (first = self->ynum - 1; true; first--) { Cell *cells = lineptr(self, first); for(i = 0; i < self->xnum; i++) { - if ((cells[i].ch & CHAR_MASK) != 32) { is_empty = false; break; } + if ((cells[i].ch & CHAR_MASK) != BLANK_CHAR) { is_empty = false; break; } } if (!is_empty || !first) break; } diff --git a/kitty/line.c b/kitty/line.c index e0fc7dc22..6234045a1 100644 --- a/kitty/line.c +++ b/kitty/line.c @@ -26,7 +26,7 @@ unsigned int line_length(Line *self) { index_type last = self->xnum - 1; for (index_type i = 0; i < self->xnum; i++) { - if ((self->cells[last - i].ch & CHAR_MASK) != ' ') return self->xnum - i; + if ((self->cells[last - i].ch & CHAR_MASK) != BLANK_CHAR) return self->xnum - i; } return 0; } @@ -138,7 +138,7 @@ line_as_ansi(Line *self, Py_UCS4 *buf, index_type buflen) { int r; if (!self->continued) { // Trim trailing spaces for(r = self->xnum - 1; r >= 0; r--) { - if ((self->cells[r].ch & CHAR_MASK) != 32) break; + if ((self->cells[r].ch & CHAR_MASK) != BLANK_CHAR) break; } limit = r + 1; } @@ -312,7 +312,7 @@ static PyObject* clear_text(Line* self, PyObject *args) { #define clear_text_doc "clear_text(at, num, ch=' ') -> Clear characters in the specified range, preserving formatting." unsigned int at, num; - int ch = 32; + int ch = BLANK_CHAR; if (!PyArg_ParseTuple(args, "II|C", &at, &num, &ch)) return NULL; line_clear_text(self, at, num, ch); Py_RETURN_NONE; @@ -327,7 +327,7 @@ line_apply_cursor(Line *self, Cursor *cursor, unsigned int at, unsigned int num, for (index_type i = at; i < self->xnum && i < at + num; i++) { if (clear_char) { - self->cells[i].ch = 32 | attrs; + self->cells[i].ch = BLANK_CHAR | attrs; self->cells[i].cc = 0; } else { char_type w = ((self->cells[i].ch >> ATTRS_SHIFT) & WIDTH_MASK) << ATTRS_SHIFT; @@ -355,7 +355,7 @@ void line_right_shift(Line *self, unsigned int at, unsigned int num) { } // Check if a wide character was split at the right edge char_type w = (self->cells[self->xnum - 1].ch >> ATTRS_SHIFT) & WIDTH_MASK; - if (w != 1) self->cells[self->xnum - 1].ch = (1 << ATTRS_SHIFT) | 32; + if (w != 1) self->cells[self->xnum - 1].ch = (1 << ATTRS_SHIFT) | BLANK_CHAR; } static PyObject* diff --git a/kitty/rewrap.h b/kitty/rewrap.h index 761ce3865..6de30ffe4 100644 --- a/kitty/rewrap.h +++ b/kitty/rewrap.h @@ -60,7 +60,7 @@ rewrap_inner(BufType *src, BufType *dest, const index_type src_limit, HistoryBuf src_x_limit = src->xnum; if (!src_line_is_continued) { // Trim trailing white-space since there is a hard line break at the end of this line - while(src_x_limit && (src->line->cells[src_x_limit - 1].ch & CHAR_MASK) == 32) src_x_limit--; + while(src_x_limit && (src->line->cells[src_x_limit - 1].ch & CHAR_MASK) == BLANK_CHAR) src_x_limit--; } while (src_x < src_x_limit) { diff --git a/kitty/screen.c b/kitty/screen.c index d7f40f458..25a39ddcf 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -70,7 +70,7 @@ new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) { void screen_reset(Screen *self) { if (self->linebuf == self->alt_linebuf) screen_toggle_screen_buffer(self); - linebuf_clear(self->linebuf, ' '); + linebuf_clear(self->linebuf, BLANK_CHAR); self->modes = empty_modes; self->default_fg = 0; self->default_bg = 0; self->highlight_fg = 0; self->highlight_bg = 0; @@ -369,7 +369,7 @@ void screen_toggle_screen_buffer(Screen *self) { bool to_alt = self->linebuf == self->main_linebuf; if (to_alt) { - linebuf_clear(self->alt_linebuf, ' '); + linebuf_clear(self->alt_linebuf, BLANK_CHAR); screen_save_cursor(self); self->linebuf = self->alt_linebuf; self->tabstops = self->alt_tabstops; @@ -777,7 +777,7 @@ void screen_erase_in_line(Screen *self, unsigned int how, bool private) { if (n > 0) { linebuf_init_line(self->linebuf, self->cursor->y); if (private) { - line_clear_text(self->linebuf->line, s, n, ' '); + line_clear_text(self->linebuf->line, s, n, BLANK_CHAR); } else { line_apply_cursor(self->linebuf->line, self->cursor, s, n, true); } @@ -813,7 +813,7 @@ void screen_erase_in_display(Screen *self, unsigned int how, bool private) { for (unsigned int i=a; i < b; i++) { linebuf_init_line(self->linebuf, i); if (private) { - line_clear_text(self->linebuf->line, 0, self->columns, ' '); + line_clear_text(self->linebuf->line, 0, self->columns, BLANK_CHAR); } else { line_apply_cursor(self->linebuf->line, self->cursor, 0, self->columns, true); }