diff --git a/kitty/data-types.c b/kitty/data-types.c index ce1663504..0610e6bae 100644 --- a/kitty/data-types.c +++ b/kitty/data-types.c @@ -9,6 +9,7 @@ extern PyTypeObject LineBuf_Type; extern PyTypeObject Cursor_Type; +extern PyTypeObject Line_Type; static PyMethodDef module_methods[] = { {NULL, NULL, 0, NULL} /* Sentinel */ @@ -29,6 +30,7 @@ PyInit_fast_data_types(void) { if (PyType_Ready(&LineBuf_Type) < 0) return NULL; if (PyType_Ready(&Cursor_Type) < 0) return NULL; + if (PyType_Ready(&Line_Type) < 0) return NULL; m = PyModule_Create(&module); if (m == NULL) return NULL; @@ -37,6 +39,8 @@ PyInit_fast_data_types(void) { PyModule_AddObject(m, "LineBuf", (PyObject *)&LineBuf_Type); Py_INCREF(&Cursor_Type); PyModule_AddObject(m, "Cursor", (PyObject *)&Cursor_Type); + Py_INCREF(&Line_Type); + PyModule_AddObject(m, "Line", (PyObject *)&Line_Type); } return m; diff --git a/kitty/data-types.h b/kitty/data-types.h index 5b2c7d8d3..8f3b1c88a 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -37,6 +37,16 @@ typedef unsigned int index_type; #define COL_SHIFT 32 #define HAS_BG_MASK (0xFF << COL_SHIFT) +typedef struct { + PyObject_HEAD + + char_type *chars; + color_type *colors; + decoration_type *decoration_fg; + combining_type *combining_chars; + index_type xnum, ynum; +} Line; + typedef struct { PyObject_HEAD @@ -45,6 +55,7 @@ typedef struct { index_type xnum, ynum, *line_map; index_type block_size; uint8_t *continued_map; + Line *line; // Pointers into buf char_type *chars; diff --git a/kitty/line-buf.c b/kitty/line-buf.c index 60a58ad3c..03c124d9f 100644 --- a/kitty/line-buf.c +++ b/kitty/line-buf.c @@ -6,6 +6,7 @@ */ #include "data-types.h" +extern PyTypeObject Line_Type; static inline void clear_chars_to_space(LineBuf* linebuf, index_type y) { @@ -38,9 +39,10 @@ LineBuf_new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) { self->buf = PyMem_Calloc(xnum * ynum, CELL_SIZE); self->line_map = PyMem_Calloc(ynum, sizeof(index_type)); self->continued_map = PyMem_Calloc(ynum, sizeof(uint8_t)); - if (self->buf == NULL || self->line_map == NULL || self->continued_map == NULL) { + self->line = (Line*)PyType_GenericAlloc(&Line_Type, 0); + if (self->buf == NULL || self->line_map == NULL || self->continued_map == NULL || self->line == NULL) { PyErr_NoMemory(); - PyMem_Free(self->buf); PyMem_Free(self->line_map); PyMem_Free(self->continued_map); + PyMem_Free(self->buf); PyMem_Free(self->line_map); PyMem_Free(self->continued_map); Py_XDECREF(self->line); Py_DECREF(self); self = NULL; } else { @@ -48,6 +50,7 @@ LineBuf_new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) { self->colors = (color_type*)(self->chars + self->block_size); self->decoration_fg = (decoration_type*)(self->colors + self->block_size); self->combining_chars = (combining_type*)(self->decoration_fg + self->block_size); + self->line->xnum = xnum; for(index_type i = 0; i < ynum; i++) { self->line_map[i] = i; clear_chars_to_space(self, i); @@ -61,44 +64,31 @@ LineBuf_new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) { static void LineBuf_dealloc(LineBuf* self) { PyMem_Free(self->buf); PyMem_Free(self->line_map); PyMem_Free(self->continued_map); + Py_XDECREF(self->line); Py_TYPE(self)->tp_free((PyObject*)self); } -static PyObject * -text_at(LineBuf* self, PyObject *args) { - index_type ynum, xnum, idx; - char_type ch; - combining_type cc; - PyObject * ans; - - if (!PyArg_ParseTuple(args, "II", &ynum, &xnum)) return NULL; - if (ynum >= self->ynum) { PyErr_SetString(PyExc_ValueError, "Line number out of bounds"); return NULL; } - if (xnum >= self->xnum) { PyErr_SetString(PyExc_ValueError, "Column number out of bounds"); return NULL; } - - idx = xnum + ynum * self->xnum; - ch = self->chars[idx] & CHAR_MASK; - cc = self->combining_chars[idx]; - if (cc == 0) { - ans = PyUnicode_New(1, ch); - if (ans == NULL) return PyErr_NoMemory(); - PyUnicode_WriteChar(ans, 0, ch); - } else { - Py_UCS4 cc1 = cc & 0xFFFF, cc2 = cc >> 16; - Py_UCS4 maxc = (ch > cc1) ? MAX(ch, cc2) : MAX(cc1, cc2); - ans = PyUnicode_New(cc2 ? 3 : 2, maxc); - if (ans == NULL) return PyErr_NoMemory(); - PyUnicode_WriteChar(ans, 0, ch); - PyUnicode_WriteChar(ans, 1, cc1); - if (cc2) PyUnicode_WriteChar(ans, 2, cc2); +static PyObject* +line(LineBuf *self, PyObject *y) { + unsigned long idx = PyLong_AsUnsignedLong(y); + if (idx >= self->ynum) { + PyErr_SetString(PyExc_ValueError, "Line number too large"); + return NULL; } - - return ans; + self->line->ynum = self->line_map[idx]; + size_t off = self->line->ynum * self->xnum; + self->line->chars = self->chars + off; + self->line->colors = self->colors + off; + self->line->decoration_fg = self->decoration_fg + off; + self->line->combining_chars = self->combining_chars + off; + Py_INCREF(self->line); + return (PyObject*)self->line; } // Boilerplate {{{ static PyMethodDef LineBuf_methods[] = { - {"text_at", (PyCFunction)text_at, METH_VARARGS, - "Return the text in the specified cell" + {"line", (PyCFunction)line, METH_O, + "Return the specified line as a Line object. Note the Line Object is a live view into the underlying buffer. And only a single line object can be used at a time." }, {NULL} /* Sentinel */ }; diff --git a/kitty/line.c b/kitty/line.c new file mode 100644 index 000000000..7047de72f --- /dev/null +++ b/kitty/line.c @@ -0,0 +1,100 @@ +/* + * line.c + * Copyright (C) 2016 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +#include "data-types.h" + + +static PyObject * +new(PyTypeObject UNUSED *type, PyObject UNUSED *args, PyObject UNUSED *kwds) { + PyErr_SetString(PyExc_TypeError, "Line objects cannot be instantiated directly, create them using LineBuf.line()"); + return NULL; +} + +static void +dealloc(LineBuf* self) { + Py_TYPE(self)->tp_free((PyObject*)self); +} + +static PyObject * +text_at(Line* self, PyObject *x) { + unsigned long xval = PyLong_AsUnsignedLong(x); + char_type ch; + combining_type cc; + PyObject *ans; + + if (xval >= self->xnum) { PyErr_SetString(PyExc_ValueError, "Column number out of bounds"); return NULL; } + + ch = self->chars[xval] & CHAR_MASK; + cc = self->combining_chars[xval]; + if (cc == 0) { + ans = PyUnicode_New(1, ch); + if (ans == NULL) return PyErr_NoMemory(); + PyUnicode_WriteChar(ans, 0, ch); + } else { + Py_UCS4 cc1 = cc & 0xFFFF, cc2 = cc >> 16; + Py_UCS4 maxc = (ch > cc1) ? MAX(ch, cc2) : MAX(cc1, cc2); + ans = PyUnicode_New(cc2 ? 3 : 2, maxc); + if (ans == NULL) return PyErr_NoMemory(); + PyUnicode_WriteChar(ans, 0, ch); + PyUnicode_WriteChar(ans, 1, cc1); + if (cc2) PyUnicode_WriteChar(ans, 2, cc2); + } + + return ans; +} + + +// Boilerplate {{{ +static PyMethodDef methods[] = { + {"text_at", (PyCFunction)text_at, METH_O, + "Return the text in the specified cell" + }, + {NULL} /* Sentinel */ +}; + +PyTypeObject Line_Type = { + PyVarObject_HEAD_INIT(NULL, 0) + "fast_data_types.Line", + sizeof(Line), + 0, /* tp_itemsize */ + (destructor)dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + "Lines", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + new, /* tp_new */ +}; +// }} + diff --git a/kitty_tests/datatypes.py b/kitty_tests/datatypes.py index 8b9cf4baf..ec85958d5 100644 --- a/kitty_tests/datatypes.py +++ b/kitty_tests/datatypes.py @@ -16,12 +16,13 @@ class TestDataTypes(BaseTest): def test_line_buf(self): lb = LineBuf(2, 3) for y in range(2): + line = lb.line(y) for x in range(3): - self.ae(lb.text_at(y, x), ' ') + self.ae(line.text_at(x), ' ') with self.assertRaises(ValueError): - lb.text_at(1, 5) + lb.line(5) with self.assertRaises(ValueError): - lb.text_at(5, 1) + lb.line(0).text_at(5) def test_line_ops(self): t = 'Testing with simple text' diff --git a/setup.py b/setup.py index 929f7b417..7457df332 100644 --- a/setup.py +++ b/setup.py @@ -68,4 +68,4 @@ if __name__ == '__main__': if sys.version_info < (3, 5): raise SystemExit('python >= 3.5 required') init_env() - compile_c_extension('kitty/fast_data_types', 'kitty/data-types.c', 'kitty/line-buf.c', 'kitty/cursor.c') + compile_c_extension('kitty/fast_data_types', 'kitty/line.c', 'kitty/data-types.c', 'kitty/line-buf.c', 'kitty/cursor.c')