From 0b3602f764cb3ec9fb5f2ccbd07995f1b6601bf5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 11 Jan 2020 09:29:32 +0530 Subject: [PATCH] Code to add markers to Screen --- kitty/data-types.h | 2 ++ kitty/screen.c | 34 +++++++++++++++++++++++++++++++++- kitty/screen.h | 5 +++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/kitty/data-types.h b/kitty/data-types.h index 1dc24980a..7de045222 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -68,6 +68,8 @@ typedef enum { NONE, MENUBAR, WINDOW, ALL } WindowTitleIn; #define REVERSE_SHIFT 6 #define STRIKE_SHIFT 7 #define DIM_SHIFT 8 +#define MARK_SHIFT 9 +#define MARK_MASK 3 #define COL_MASK 0xFFFFFFFF #define UTF8_ACCEPT 0 #define UTF8_REJECT 1 diff --git a/kitty/screen.c b/kitty/screen.c index b09ea6a49..7a991be80 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -105,6 +105,7 @@ new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) { self->callbacks = callbacks; Py_INCREF(callbacks); self->test_child = test_child; Py_INCREF(test_child); self->cursor = alloc_cursor(); + self->markers.callbacks = PyList_New(0); self->color_profile = alloc_color_profile(); self->main_linebuf = alloc_linebuf(lines, columns); self->alt_linebuf = alloc_linebuf(lines, columns); self->linebuf = self->main_linebuf; @@ -115,7 +116,11 @@ new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) { self->pending_mode.wait_time = 2.0; self->disable_ligatures = OPT(disable_ligatures); self->main_tabstops = PyMem_Calloc(2 * self->columns, sizeof(bool)); - if (self->cursor == NULL || self->main_linebuf == NULL || self->alt_linebuf == NULL || self->main_tabstops == NULL || self->historybuf == NULL || self->main_grman == NULL || self->alt_grman == NULL || self->color_profile == NULL) { + if ( + self->cursor == NULL || self->main_linebuf == NULL || self->alt_linebuf == NULL || + self->main_tabstops == NULL || self->historybuf == NULL || self->main_grman == NULL || + self->alt_grman == NULL || self->color_profile == NULL || self->markers.callbacks == NULL + ) { Py_CLEAR(self); return NULL; } self->alt_tabstops = self->main_tabstops + self->columns * sizeof(bool); @@ -278,6 +283,7 @@ dealloc(Screen* self) { Py_CLEAR(self->alt_linebuf); Py_CLEAR(self->historybuf); Py_CLEAR(self->color_profile); + Py_CLEAR(self->markers.callbacks); PyMem_Free(self->overlay_line.cpu_cells); PyMem_Free(self->overlay_line.gpu_cells); PyMem_Free(self->main_tabstops); @@ -2241,6 +2247,30 @@ send_escape_code_to_child(Screen *self, PyObject *args) { Py_RETURN_NONE; } +static PyObject* +add_marker(Screen *self, PyObject *marker) { + if (!PyCallable_Check(marker)) { + PyErr_SetString(PyExc_TypeError, "marker must be a callable"); + return NULL; + } + if (!PySequence_Contains(self->markers.callbacks, marker)) { + if (PyList_Append(self->markers.callbacks, marker) != 0) return NULL; + self->markers.dirty = true; + } + Py_RETURN_NONE; +} + +static PyObject* +remove_marker(Screen *self, PyObject *marker) { + Py_ssize_t idx = PySequence_Index(self->markers.callbacks, marker); + if (idx > -1) { + PySequence_DelItem(self->markers.callbacks, idx); + self->markers.dirty = true; + Py_RETURN_TRUE; + } + Py_RETURN_FALSE; +} + static PyObject* paste(Screen *self, PyObject *bytes) { if (!PyBytes_Check(bytes)) { PyErr_SetString(PyExc_TypeError, "Must paste() bytes"); return NULL; } @@ -2340,6 +2370,8 @@ static PyMethodDef methods[] = { MND(paste, METH_O) MND(paste_bytes, METH_O) MND(copy_colors_from, METH_O) + MND(add_marker, METH_O) + MND(remove_marker, METH_O) {"select_graphic_rendition", (PyCFunction)_select_graphic_rendition, METH_VARARGS, ""}, {NULL} /* Sentinel */ diff --git a/kitty/screen.h b/kitty/screen.h index a189ab760..84362ec87 100644 --- a/kitty/screen.h +++ b/kitty/screen.h @@ -109,6 +109,11 @@ typedef struct { } pending_mode; DisableLigature disable_ligatures; + struct { + PyObject *callbacks; + bool dirty; + } markers; + } Screen;