From: khamidou Date: Fri, 26 Jun 2009 19:22:48 +0000 (+0200) Subject: implementing up/down motion X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=c388c7c9782d3bbb50e23200ad95ddf965dff0f3;p=te.git implementing up/down motion --- diff --git a/buffer.c b/buffer.c index f2e7418..00d18ac 100644 --- a/buffer.c +++ b/buffer.c @@ -65,7 +65,7 @@ int line_length(bstring b, int point) if (b == NULL) return -1; - int s_offset = bstrrchrp(b, '\n', point - 1); + int s_offset = bstrrchrp(b, '\n', point -1); if (s_offset == BSTR_ERR) s_offset = 0; @@ -74,23 +74,9 @@ int line_length(bstring b, int point) e_offset = blength(b); - return e_offset - s_offset; -} - -bstring current_line_as_bstring(bstring b, int point) -{ - int s_offset = bstrrchrp(b, '\n', max(point, 0)); - if (s_offset == BSTR_ERR) - s_offset = 0; - - int e_offset = bstrchrp(b, '\n', point + 1); - if (e_offset == BSTR_ERR) - e_offset = blength(b); - - if (s_offset > 0) - return bmidstr(b, s_offset + 1, e_offset - s_offset); - else - return bmidstr(b, s_offset, e_offset - s_offset); + return (e_offset - (s_offset + 1)); /* s_offset + 1 because the first '\n' is not a part + of the line + */ } /* @@ -114,6 +100,22 @@ int screen_line_length(bstring b, int point) } +bstring current_line_as_bstring(bstring b, int point) +{ + int s_offset = bstrrchrp(b, '\n', max(point, 0)); + if (s_offset == BSTR_ERR) + s_offset = 0; + + int e_offset = bstrchrp(b, '\n', point + 1); + if (e_offset == BSTR_ERR) + e_offset = blength(b); + + if (s_offset > 0) + return bmidstr(b, s_offset + 1, e_offset - s_offset); + else + return bmidstr(b, s_offset, e_offset - s_offset); +} + /* accessor and mutators for the contents of the buffer. diff --git a/keyb.c b/keyb.c index df14d38..4acd83b 100644 --- a/keyb.c +++ b/keyb.c @@ -20,7 +20,11 @@ void input_loop(void) case KEY_DOWN: screen_move_down(current_buf); break; - + + case KEY_UP: + screen_move_up(current_buf); + break; + default: screen_insert_char(current_buf, c); refresh(); @@ -29,10 +33,10 @@ void input_loop(void) refresh(); -/* miniprintf("%c, line length: %d, y: %d, x%d", */ -/* (bchar(current_buf->contents, current_buf->point) == '\n') ? 'N' : bchar(current_buf->contents, current_buf->point), */ -/* screen_line_length(current_buf->contents, current_buf->point), */ -/* current_buf->y, current_buf->x); */ + miniprintf("%c, line length: %d, y: %d, x%d", + (bchar(current_buf->contents, current_buf->point) == '\n') ? 'N' : bchar(current_buf->contents, current_buf->point), + screen_line_length(current_buf->contents, current_buf->point), + current_buf->y, current_buf->x); } diff --git a/python-bindings.c b/python-bindings.c new file mode 100644 index 0000000..eb7eaf7 --- /dev/null +++ b/python-bindings.c @@ -0,0 +1,150 @@ +#include "python-bindings.h" + +PyMODINIT_FUNC init_python(void) +{ + Py_Initialize(); + + if (PyType_Ready(&binding_BufferType) < 0) + return; + + PyObject *m = Py_InitModule3("te", TeMethods, + "te main python api."); + + if (m == NULL) + return; + + Py_INCREF(&binding_BufferType); + PyModule_AddObject(m, "Buffer", (PyObject *)&binding_BufferType); + +} + +PyObject *Buffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + TeBuffer *self; + + self = (TeBuffer *)type->tp_alloc(type, 0); + if (self != NULL) { + self->buf = alloc_and_insert_buffer(); + if (self->buf == NULL) + { + Py_DECREF(self); + return NULL; + } + + self->buf->pyBuffer = (PyObject *) self; + } + + return (PyObject *)self; +} + +void Buffer_dealloc(TeBuffer *self) +{ + if (self == NULL) + return; + + free_buffer(self->buf); + self->ob_type->tp_free((PyObject*)self); +} + +int Buffer_init(TeBuffer *self, PyObject *args, PyObject *kwds) +{ + PyObject *tmp; + + static char *kwlist[] = {"name", /* "file" */ NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|S", kwlist, + &tmp)) + return -1; + + + if (self != NULL) + self->buf->name = PyString_AsString(tmp); + + Py_XDECREF(tmp); + +} + +PyObject *Buffer_getname(TeBuffer *self, void *closure) +{ + if (self->buf == NULL) + return NULL; + + PyObject *s = PyString_FromString(self->buf->name); + Py_INCREF(s); + return s; +} + +int Buffer_setname(TeBuffer *self, PyObject *value, void *closure) +{ + if (self->buf == NULL) + return -1; + + if (value == NULL) { + PyErr_SetString(PyExc_TypeError, "Cannot change buffer name"); + return -1; + } + + char *s = PyString_AsString(value); + free(self->buf->name); + self->buf->name = strdup(s); +} + +PyObject *Buffer_getpoint(TeBuffer *self, void *closure) +{ + if (self->buf == NULL) + return NULL; + + PyObject *i = PyInt_FromLong((long) self->buf->point); + Py_INCREF(i); + return i; +} + +int Buffer_setpoint(TeBuffer *self, PyObject *value, void *closure) +{ + if (self->buf == NULL) + return -1; + + if (value == NULL) { + PyErr_SetString(PyExc_TypeError, "Invalid object"); + return -1; + } + + if (!PyInt_Check(value)) { + PyErr_SetString(PyExc_TypeError, "Parameter is not of integer type"); + return -1; + } + + self->buf->point = (int) PyInt_AsLong(value); +} + +PyObject *Buffer_getcontents(TeBuffer *self, void *closure) +{ + if (self->buf == NULL) + return NULL; + + char *cstr = bstr2cstr(self->buf->contents, '\0'); + PyObject *s = PyString_FromString(cstr); + + free(cstr); + + Py_INCREF(s); + return s; +} + +int Buffer_setcontents(TeBuffer *self, PyObject *value, void *closure) +{ + if (self->buf == NULL) + return -1; + + if (value == NULL) { + PyErr_SetString(PyExc_TypeError, "Invalid object"); + return -1; + } + + if (!PyInt_Check(value)) { + PyErr_SetString(PyExc_TypeError, "Parameter is not of integer type"); + return -1; + } + + self->buf->point = (int) PyInt_AsLong(value); +} diff --git a/python-bindings.h b/python-bindings.h new file mode 100644 index 0000000..3cbbea5 --- /dev/null +++ b/python-bindings.h @@ -0,0 +1,112 @@ +/* + conventions : the declarations using mixed case always refer to elements of the python binding. + the names of methods are prefixed with the name of their class (ex: Buffer_getname for Buffer.getname()) +*/ + +#ifndef __TE_PYTHON_BINDINGS__ +#define __TE_PYTHON_BINDINGS__ + +#include +#include +#include "buffer.h" + +void init_interp(void); + +typedef struct { + PyObject_HEAD + struct te_buffer *buf; +} TeBuffer; + +int Buffer_init(TeBuffer *self, PyObject *args, PyObject *kwds); +void Buffer_dealloc(TeBuffer *self); +PyObject *Buffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds); + +PyObject *Buffer_getname(TeBuffer *self, void *closure); +int Buffer_setname(TeBuffer *self, PyObject *value, void *closure); + +PyObject *Buffer_getpoint(TeBuffer *self, void *closure); +int Buffer_setpoint(TeBuffer *self, PyObject *value, void *closure); + +PyObject *Buffer_getcontents(TeBuffer *self, void *closure); +int Buffer_setcontents(TeBuffer *self, PyObject *value, void *closure); + +static PyMemberDef Buffer_members[] = { + {NULL} /* Sentinel */ +}; + +static PyMethodDef Buffer_methods[] = { + {NULL} /* Sentinel */ +}; + +static PyGetSetDef Buffer_getsetters[] = { + {"point", + (getter)Buffer_getpoint, (setter)Buffer_setpoint, + "point of a buffer", + NULL}, + + {"name", + (getter)Buffer_getname, (setter)Buffer_setname, + "name of a buffer", + NULL}, + + {"contents", + (getter)Buffer_getcontents, (setter)Buffer_setcontents, + "contents of a buffer", + NULL}, + + {NULL} +}; + +static PyTypeObject binding_BufferType = { + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "te.Buffer", /*tp_name*/ + sizeof(TeBuffer), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + 0, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 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*/ + "A representation of a buffer in memory.", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + Buffer_methods, /* tp_methods */ + Buffer_members, /* tp_members */ + Buffer_getsetters, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)Buffer_init, /* tp_init */ + 0, /* tp_alloc */ + Buffer_new, /* tp_new */ + +}; + +/* the methods of the te module */ +static PyMethodDef TeMethods[] = { +// {"system", spam_system, METH_VARARGS, +// "Execute a shell command."}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +PyMODINIT_FUNC init_python(void); + +#endif diff --git a/screen.c b/screen.c index a03bed5..88da079 100644 --- a/screen.c +++ b/screen.c @@ -1,4 +1,5 @@ #include "screen.h" +#include "util.h" static int scr_y, scr_x; @@ -62,18 +63,21 @@ void draw_line(bstring s, int y) { int i; int j; + int screen_abs = 0; saveyx(); move(y, 0); clrtoeol(); /* FIXME : wrap line if we go beyond COLS */ - for (i = 0; i < blength(s); i++) -/* if (bchar(s, i) == '\t') */ -/* for (j = 0; j < TAB_LEN; j++) */ -/* mvwaddch(buffer_win, y, i++, ' '); */ -/* else */ - mvwaddch(buffer_win, y, i, bchar(s, i)); - + for (i = 0; i < blength(s); i++) { + if (bchar(s, i) == '\t') { + for (j = 0; j < TAB_LEN; j++) + mvwaddch(buffer_win, y, screen_abs++, ' '); + } else { + mvwaddch(buffer_win, y, screen_abs, bchar(s, i)); + screen_abs++; + } + } restoreyx(); } @@ -167,7 +171,37 @@ void screen_move_up(struct te_buffer *buf) if (buf == NULL) return; + int i = 0; + int old_x = buf->x; + int curr_y; + int curr_x = 0; + + /* move until the last character of the previous line (before the '\n') + * unless we're already there. + */ + if (curr_char(buf) == '\n' && prev_char(buf) == '\n') { + /* special case for a blank line */ + move_left(buf); + move_left(buf); + } else { + do { + if(move_left(buf) == ERR) + break; + + } while(next_char(buf) != '\n'); + } + + screen_prev_line(buf); + /* mimic emacs' behaviour of moving the user to the exact offset we were on */ + + + while(buf->x > old_x && prev_char(buf) != '\n') { + screen_move_left(buf); + } + + move(buf->y, buf->x); + return; } void screen_move_down(struct te_buffer *buf) @@ -175,25 +209,28 @@ void screen_move_down(struct te_buffer *buf) if (buf == NULL) return; - int line_len = line_length(buf->contents, buf->point); - int sline_len = screen_line_length(buf->contents, buf->point); - int count = 0; + int i = 0; + int old_x = buf->x; + int curr_y; + int curr_x = 0; do { - /* move until the end of line */ + /* move until the first char of the next line */ if(move_right(buf) == ERR) break; - } while(curr_char(buf) != '\n'); + } while(prev_char(buf) != '\n'); screen_next_line(buf); - + /* mimic emacs' behaviour of moving the user to the exact offset we were on */ - for(count = 0; count < line_len && curr_char(buf) != '\n'; count++) { - move_right(buf); + + while(buf->x < old_x && curr_char(buf) != '\n') { screen_move_right(buf); + statusprintf("curr_x : %d, old_x : %d", curr_x, old_x); } + move(buf->y, buf->x); return; } diff --git a/screen.h b/screen.h index f84335e..6a3bc4d 100644 --- a/screen.h +++ b/screen.h @@ -7,7 +7,7 @@ #include "buffer.h" #include "config.h" - +#include "util.h" WINDOW *buffer_win, *status_win, *minibuffer_win; diff --git a/util.h b/util.h index f80b65c..4f58e7c 100644 --- a/util.h +++ b/util.h @@ -5,4 +5,6 @@ #define increment_or_maximize(x, max) (x = (x > max) ? x + 1 : max) #define max(a, b) (a > b) ? a : b +#define min(a, b) (a < b) ? a : b + #endif