]> khamidou — GitHub mirrors - te.git/commitdiff
fixed this bug at last (hope so)
authorkhamidou <karim@lambda-expr.org>
Tue, 7 Jul 2009 20:07:37 +0000 (22:07 +0200)
committerkhamidou <karim@lambda-expr.org>
Tue, 7 Jul 2009 20:07:37 +0000 (22:07 +0200)
Makefile
buffer.c
buffer.h
keyb.c
main.c
python-bindings.c [deleted file]
python-bindings.h [deleted file]
screen.c

index 781371ef43d13832712757f40345ab5cda8eb237..222c065a25cc561100e74dace71ab24b92515fa7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
 CC=gcc
-CFLAGS=-c -g `python2.5-config --includes`
-LDFLAGS=-lcurses `python2.5-config --ldflags`
-OBJS=main.o errors.o screen.o buffer.o keyb.o bstrlib.o python-bindings.o
+CFLAGS=-c -g
+LDFLAGS=-lcurses
+OBJS=main.o errors.o screen.o buffer.o keyb.o bstrlib.o
 
 te: $(OBJS)
        $(CC) $(LDFLAGS) -o te $(OBJS)
@@ -24,9 +24,7 @@ keyb.o: keyb.c
 bstrlib.o: bstrlib.c
        $(CC) $(CFLAGS) bstrlib.c
 
-python-bindings.o: python-bindings.c python-bindings.h
-       $(CC) $(CFLAGS) python-bindings.c
-
-clean: $(OBJS)
+clean: 
        rm $(OBJS)
+       rm *~
        rm te
\ No newline at end of file
index d054683e233fcd144d915cbf7c135093f9c96099..0d204237c613509d84b7f45ab03ce67ea2fd6f12 100644 (file)
--- a/buffer.c
+++ b/buffer.c
@@ -43,7 +43,7 @@ struct te_buffer* load_buffer(char *filename)
        
        if (fp == NULL)
                fail("Unable to open %s", filename);
-
+       
        struct te_buffer *b = alloc_and_insert_buffer();
 
        b->name = strdup(filename);
@@ -90,7 +90,7 @@ int screen_line_length(bstring b, int point)
        int i = 0;
        int count = 0;
 
-       int s_offset = bstrrchrp(b, '\n', point - 1);
+       int s_offset = bstrrchrp(b, '\n', max(point - 1, 0));
 
        for (i = s_offset + 1; bchar(b, i) != '\n'; i++) {
                if (bchar(b, i) == '\t')
@@ -112,16 +112,16 @@ bstring current_line_as_bstring(bstring b, int point)
        if (e_offset == BSTR_ERR)
                e_offset = blength(b);
 
-       if (s_offset > 0)
-               return bmidstr(b, s_offset + 1, e_offset - s_offset);
-       else 
+       if (s_offset == 0) /* the first line of the file */
                return bmidstr(b, s_offset, e_offset - s_offset);
+       else 
+               return bmidstr(b, s_offset + 1, e_offset - s_offset);
 }
 
 /*
-  accessor and mutators for the contents of the buffer.
+  accessors and mutators for the contents of the buffer.
 
- */
+*/
 
 int prev_char(struct te_buffer *buf)
 {
@@ -171,7 +171,7 @@ int move_right(struct te_buffer *buf)
 
        if (buf->point < blength(buf->contents)) {
                buf->point++;
-               if (bchar(buf->contents, buf->point) == '\n')
+               if (prev_char(buf) == '\n')
                        buf->lineno++;
        }
        else
index 70a59a3736bbbe46bc5a578675b8f01213e51bce..98f6e175601e98d8b068e297ddd095572eccee24 100644 (file)
--- a/buffer.h
+++ b/buffer.h
@@ -2,7 +2,6 @@
 #define __BUFFER_H__
 
 #include <stdio.h>
-#include <Python.h>
 #include "bstrlib.h"
 #include "queue.h"
 #include "config.h"
@@ -15,9 +14,6 @@ struct te_buffer {
        bstring contents;
        int point;              /* offset in "contents" */
        int top_char, bottom_char; /* the char at the top and at the bottom of the screen */
-
-       PyObject *pyBuffer;     /* pointer to the buffer object */
-
        TAILQ_ENTRY(te_buffer) buffers;
 };
 
diff --git a/keyb.c b/keyb.c
index 4acd83bbae77c0037ddcebc090de57f32a0779e7..d0089b6b09cfa572ae15459a6f6490e158809efa 100644 (file)
--- a/keyb.c
+++ b/keyb.c
@@ -25,6 +25,10 @@ void input_loop(void)
                        screen_move_up(current_buf);
                        break;
 
+               case 'u':
+                       wscrl(buffer_win, -10);
+                       touchwin(stdscr);
+                       break;
                default:
                        screen_insert_char(current_buf, c);
                        refresh();
diff --git a/main.c b/main.c
index 0d92e8a26f00f8ec74a29f3ea25e7b1f27023ceb..0802e5c24e05e3391b6f7a9b95ac8bdd788946d2 100644 (file)
--- a/main.c
+++ b/main.c
@@ -4,18 +4,16 @@
 
 #include <stdio.h>
 #include <curses.h>
-#include "python-bindings.h"
 #include "screen.h"
 
 extern struct te_buffer *current_buf;
 
 int main(int argc, char **argv)
 {
-       init_python();
        init_buffers();
        init_windows();
 
-       current_buf = load_buffer("main.c");
+       current_buf = load_buffer("buffer.c");
        paint_buffer(current_buf);
        refresh();
        input_loop();
diff --git a/python-bindings.c b/python-bindings.c
deleted file mode 100644 (file)
index eb7eaf7..0000000
+++ /dev/null
@@ -1,150 +0,0 @@
-#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
deleted file mode 100644 (file)
index 3cbbea5..0000000
+++ /dev/null
@@ -1,112 +0,0 @@
-/*
-  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 <Python.h>
-#include <structmember.h>
-#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
index e1f84e5cc2f5e355ace0ba6011b46497de9bcc58..358bed72968cdd260f3d9462202d87631664eabb 100644 (file)
--- a/screen.c
+++ b/screen.c
@@ -13,8 +13,9 @@ void init_windows(void)
        cbreak();
        noecho();
        keypad(stdscr, TRUE);
+       scrollok(stdscr, FALSE);
 
-       buffer_win = subwin(stdscr, LINES - 2, COLS, 0, 0);
+       buffer_win = subwin(stdscr, LINES - 3, COLS, 0, 0);
        idlok(stdscr, TRUE);
        scrollok(buffer_win, FALSE);
 
@@ -46,7 +47,7 @@ void paint_buffer(struct te_buffer *buf)
        int i = 0;
        int count = 0;
 
-       for(i = 0; i < (LINES - 3); i++) {
+       for(i = 0; i < (LINES - 2); i++) {
                s = current_line_as_bstring(buf->contents, count);
                draw_line(s, i);
                count += blength(s);
@@ -99,6 +100,15 @@ void screen_prev_line(struct te_buffer *buf)
        if (buf->y > 0) {
                buf->y--;
                buf->x = screen_line_length(buf->contents, buf->point);
+       } else {
+               /* scroll up */
+               scrollok(buffer_win, TRUE);
+               wscrl(buffer_win, -1);
+               touchwin(stdscr); /* We have to use it to scroll the window. */
+               scrollok(buffer_win, FALSE);
+               bstring s = current_line_as_bstring(buf->contents, buf->point - 1);
+               draw_line(s, 0);
+               bdestroy(s);
        }
 }
 
@@ -108,10 +118,17 @@ void screen_next_line(struct te_buffer *buf)
                return;
 
        buf->x = 0;
-       if (buf->y < LINES)
+       if (buf->y < LINES - 4)
                buf->y++;
-
-       /* FIXME: scroll the screen if possible */
+       else {
+               scrollok(buffer_win, TRUE);
+               wscrl(buffer_win, 1);
+               touchwin(stdscr); /* We have to use it to scroll the window. */
+               scrollok(buffer_win, FALSE);
+               bstring s = current_line_as_bstring(buf->contents, buf->point - 1);
+               draw_line(s, LINES - 4);
+               bdestroy(s);
+       }
 }
 
 void screen_move_left(struct te_buffer *buf)
@@ -154,12 +171,6 @@ void screen_move_right(struct te_buffer *buf)
                        } else {
                                buf->x++;
                        }
-
-/*             else { /\* FIXME: scroll the screen instead of testing this */
-/*                       condition */
-/*                    *\/ */
-/*                     screen_next_line(buf); */
-/*             } */
        }
 
        move(buf->y, buf->x);
@@ -174,25 +185,26 @@ void screen_move_up(struct te_buffer *buf)
        int     old_x = buf->x;
 
        /* move until the first character of the line */
-       do {
+       while(prev_char(buf) != '\n') {
+
                if(move_left(buf) == ERR)
                        break;
-               
-       } while(prev_char(buf) != '\n');
+       } 
+       move_left(buf);
 
        /* then, move to the beginning of the previous line */
-       do {
+       while(prev_char(buf) != '\n') {
+
                if(move_left(buf) == ERR)
                        break;
-               
-       } while(prev_char(buf) != '\n');
+       }
 
-       screen_prev_line(buf);
-       buf->x = 0;
+       screen_prev_line(buf);
+       buf->x = 0; 
 
        /* mimic emacs' behaviour of moving the user to the exact offset we were on */
                
-       while(buf->x < old_x && next_char(buf) != '\n') {
+       while(buf->x < old_x && next_char(buf) != '\n' && curr_char(buf) != '\n') {
                screen_move_right(buf);
        }
 
@@ -210,16 +222,15 @@ void screen_move_down(struct te_buffer *buf)
 
        do {
                /* move until the first char of the next line */
-               if(move_right(buf) == ERR)
+               if (move_right(buf) == ERR)
                        break;
 
-       } while(prev_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 */
-
-       while(buf->x < old_x && curr_char(buf) != '\n') {
+       while (buf->x < old_x && curr_char(buf) != '\n') {
                screen_move_right(buf);
        }
 
@@ -235,7 +246,6 @@ void screen_insert_char(struct te_buffer *buf, char c)
        insert_char(buf, c);
        
        bstring s = current_line_as_bstring(buf->contents, buf->point);
-       miniprintf("%s", bstr2cstr(s, '\0'));
        draw_line(s, buf->y);
        screen_move_right(buf);