Cleanup the as_text_generic macro

Also allow adding a \r at the end of each visual line
This commit is contained in:
Kovid Goyal 2018-05-18 23:06:06 +05:30
parent 2a713cab60
commit bb06bfa627
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 15 additions and 9 deletions

View File

@ -208,7 +208,7 @@ static PyObject*
as_text(HistoryBuf *self, PyObject *args) {
Line l = {.xnum=self->xnum};
#define gl(self, y) get_line(self, y, &l);
as_text_generic(args, self, gl, self->count, self->xnum, callback, as_ansi);
as_text_generic(args, self, gl, self->count, self->xnum);
#undef gl
}

View File

@ -414,7 +414,7 @@ get_line(LineBuf *self, index_type y) {
static PyObject*
as_text(LineBuf *self, PyObject *args) {
as_text_generic(args, self, get_line, self->ynum, self->xnum, callback, as_ansi);
as_text_generic(args, self, get_line, self->ynum, self->xnum);
}

View File

@ -81,14 +81,15 @@ void historybuf_refresh_sprite_positions(HistoryBuf *self);
void historybuf_clear(HistoryBuf *self);
#define as_text_generic(args, container, get_line, lines, columns, callback, as_ansi) { \
#define as_text_generic(args, container, get_line, lines, columns) { \
PyObject *callback; \
int as_ansi = 0; \
if (!PyArg_ParseTuple(args, "O|p", &callback, &as_ansi)) return NULL; \
int as_ansi = 0, insert_wrap_markers = 0; \
if (!PyArg_ParseTuple(args, "O|pp", &callback, &as_ansi, &insert_wrap_markers)) return NULL; \
PyObject *ret = NULL, *t = NULL; \
Py_UCS4 *buf = NULL; \
PyObject *nl = PyUnicode_FromString("\n"); \
if (nl == NULL) goto end; \
PyObject *cr = PyUnicode_FromString("\r"); \
if (nl == NULL || cr == NULL) goto end; \
if (as_ansi) { \
buf = malloc(sizeof(Py_UCS4) * columns * 100); \
if (buf == NULL) { PyErr_NoMemory(); goto end; } \
@ -109,9 +110,14 @@ void historybuf_clear(HistoryBuf *self);
if (t == NULL) goto end; \
ret = PyObject_CallFunctionObjArgs(callback, t, NULL); \
Py_DECREF(t); if (ret == NULL) goto end; Py_DECREF(ret); \
if (insert_wrap_markers) { \
ret = PyObject_CallFunctionObjArgs(callback, cr, NULL); \
if (ret == NULL) goto end; \
Py_CLEAR(ret); \
}\
} \
end: \
Py_CLEAR(nl); free(buf); \
Py_CLEAR(nl); Py_CLEAR(cr); free(buf); \
if (PyErr_Occurred()) return NULL; \
Py_RETURN_NONE; \
}

View File

@ -1462,12 +1462,12 @@ screen_open_url(Screen *self) {
static PyObject*
as_text(Screen *self, PyObject *args) {
as_text_generic(args, self, visual_line_, self->lines, self->columns, callback, as_ansi);
as_text_generic(args, self, visual_line_, self->lines, self->columns);
}
static PyObject*
as_text_non_visual(Screen *self, PyObject *args) {
as_text_generic(args, self, range_line_, self->lines, self->columns, callback, as_ansi);
as_text_generic(args, self, range_line_, self->lines, self->columns);
}
static PyObject*