From 4c1f4bb8f008881300ee1f55d35237977b72074b Mon Sep 17 00:00:00 2001 From: khamidou Date: Mon, 8 Jun 2009 12:00:35 +0200 Subject: [PATCH] fixed python-related compilation bug --- Makefile | 9 +++-- buffer.c | 34 ++++++++++++++++-- buffer.h | 7 +++- keyb.c | 17 +++++++-- main.c | 4 ++- screen.c | 103 +++++++++++++++++++++++++++++++------------------------ screen.h | 8 ++--- 7 files changed, 122 insertions(+), 60 deletions(-) diff --git a/Makefile b/Makefile index 2924294..781371e 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=gcc -CFLAGS=-c -g -LDFLAGS=-lcurses -OBJS=main.o errors.o screen.o buffer.o keyb.o bstrlib.o +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 te: $(OBJS) $(CC) $(LDFLAGS) -o te $(OBJS) @@ -24,6 +24,9 @@ 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) rm $(OBJS) rm te \ No newline at end of file diff --git a/buffer.c b/buffer.c index 76b9fcd..44ae165 100644 --- a/buffer.c +++ b/buffer.c @@ -65,7 +65,7 @@ int line_length(bstring b, int point) int c_offset = bstrchrp(b, '\n', point); if (c_offset == BSTR_ERR) - c_offset = 0; + c_offset = blength(b); int p_offset = bstrrchrp(b, '\n', c_offset - 1); if (p_offset == BSTR_ERR) @@ -93,10 +93,37 @@ int screen_line_length(bstring b, int point) count += 1; } - return count; } +/* + accessor and mutators for the contents of the buffer. + + */ int prev_char(struct te_buffer *buf) +{ + if (buf == NULL) + return; + + return bchar(buf->contents, buf->point - 1); +} + +int curr_char(struct te_buffer *buf) +{ + if (buf == NULL) + return; + + return bchar(buf->contents, buf->point); +} + +int next_char(struct te_buffer *buf) +{ + if (buf == NULL) + return; + + return bchar(buf->contents, buf->point + 1); +} + +int move_left(struct te_buffer *buf) { if (buf == NULL) return; @@ -109,7 +136,8 @@ int prev_char(struct te_buffer *buf) return OK; } -int next_char(struct te_buffer *buf) + +int move_right(struct te_buffer *buf) { if (buf == NULL) return; diff --git a/buffer.h b/buffer.h index 72d5b8d..cd3dde7 100644 --- a/buffer.h +++ b/buffer.h @@ -2,6 +2,7 @@ #define __BUFFER_H__ #include +#include #include "bstrlib.h" #include "queue.h" #include "config.h" @@ -14,6 +15,8 @@ struct te_buffer { int point; /* offset in "contents" */ struct te_char *scr_top, *scr_bottom; /* the first and last char of the screen */ + PyObject *pyBuffer; /* pointer to the buffer object */ + TAILQ_HEAD(te_chars_head, te_char) chars_head; TAILQ_ENTRY(te_buffer) buffers; }; @@ -31,8 +34,10 @@ void init_buffers(void); int line_length(bstring b, int point); int screen_line_length(bstring b, int point); int prev_char(struct te_buffer *buf); +int curr_char(struct te_buffer *buf); int next_char(struct te_buffer *buf); - +int move_right(struct te_buffer *buf); +int move_left(struct te_buffer *buf); #endif diff --git a/keyb.c b/keyb.c index 13a741f..30ee644 100644 --- a/keyb.c +++ b/keyb.c @@ -3,7 +3,7 @@ void input_loop(void) { while(1) { - char c = getch(); + int c = getch(); switch(c) { case 'q': @@ -11,16 +11,27 @@ void input_loop(void) case 'k': case KEY_LEFT: - move_left(current_buf); + screen_move_left(current_buf); break; case 'l': case KEY_RIGHT: - move_right(current_buf); + screen_move_right(current_buf); + break; + + case 'd': + case KEY_DOWN: + screen_move_down(current_buf); break; } 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); + } } diff --git a/main.c b/main.c index ba19fbe..1a050ba 100644 --- a/main.c +++ b/main.c @@ -4,16 +4,18 @@ #include #include +#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("screen.c"); + current_buf = load_buffer("README"); paint_buffer(current_buf); refresh(); input_loop(); diff --git a/screen.c b/screen.c index 3606918..31018cb 100644 --- a/screen.c +++ b/screen.c @@ -60,9 +60,10 @@ void screen_prev_line(struct te_buffer *buf) if (buf == NULL) return; - buf->x = 0; - if (buf->y > 0) + if (buf->y > 0) { buf->y--; + buf->x = screen_line_length(buf->contents, buf->point); + } } void screen_next_line(struct te_buffer *buf) @@ -75,78 +76,90 @@ void screen_next_line(struct te_buffer *buf) buf->y++; } -void move_left(struct te_buffer *buf) +void screen_move_left(struct te_buffer *buf) { if (buf == NULL) return; - if (bchar(buf->contents, buf->point) == '\n') { - screen_prev_line(buf); - prev_char(buf); + if(move_left(buf) == ERR) return; - } - - if(prev_char(buf) == ERR) - return; - - if (buf->x > 0) - if (bchar(buf->contents, buf->point) == '\t') - buf->x -= TAB_LEN; - else { - buf->x--; - } - - else if (buf->y > 0) { - buf->y--; - buf->x = screen_line_length(buf->contents, buf->point); + + if (curr_char(buf) == '\n') { + screen_prev_line(buf); + } else { + if (buf->x > 0) + if (curr_char(buf) == '\t') { + buf->x -= TAB_LEN; + } else { + buf->x--; + } } move(buf->y, buf->x); - refresh(); - miniprintf("%c, y: %d, x%d", bchar(buf->contents, buf->point), buf->y, buf->x); } -void move_right(struct te_buffer *buf) +void screen_move_right(struct te_buffer *buf) { if (buf == NULL) return; - if (bchar(buf->contents, buf->point) == '\n') { - screen_next_line(buf); - next_char(buf); - return; - } - - if (next_char(buf) == ERR) /* last char of the document */ + if (move_right(buf) == ERR) /* last char of the document */ return; - if (buf->x < screen_line_length(buf->contents, buf->point)) { + if (prev_char(buf) == '\n') { + screen_next_line(buf); + } else { + if (buf->x <= screen_line_length(buf->contents, buf->point)) { /* tab is the only character larger than 1 */ - if (bchar(buf->contents, buf->point) == '\t') - buf->x += TAB_LEN; - else { - buf->x++; + if (prev_char(buf) == '\t') { + buf->x += TAB_LEN; + } else { + buf->x++; + } } - } - else if (buf->y < LINES - 3) { - buf->y++; - buf->x = 0; - } + else { /* FIXME: scroll the screen instead of testing this + condition + */ + screen_next_line(buf); + } + } move(buf->y, buf->x); - refresh(); - miniprintf("%c, y: %d, x%d", bchar(buf->contents, buf->point), buf->y, buf->x); } -void move_up(struct te_buffer *buf) +void screen_move_up(struct te_buffer *buf) { + if (buf == NULL) + return; + } -void move_down(struct te_buffer *buf) +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; + + do { + /* move until the end of line */ + if(move_right(buf) == ERR) + break; + + } while(curr_char(buf) != '\n'); + + screen_next_line(buf); + + for(count = 0; count < line_len && curr_char(buf) != '\n'; count++) { + move_right(buf); + screen_move_right(buf); + } + return; } void statusprintf(char *fmt, ...) diff --git a/screen.h b/screen.h index 1feac12..5c3d4f1 100644 --- a/screen.h +++ b/screen.h @@ -18,10 +18,10 @@ void paint_buffer(struct te_buffer *buf); void scroll_up(struct te_buffer *buf); void scroll_down(struct te_buffer *buf); -void move_left(struct te_buffer *buf); -void move_right(struct te_buffer *buf); -void move_up(struct te_buffer *buf); -void move_down(struct te_buffer *buf); +void screen_move_left(struct te_buffer *buf); +void screen_move_right(struct te_buffer *buf); +void screen_move_up(struct te_buffer *buf); +void screen_move_down(struct te_buffer *buf); void statusprintf(char *fmt, ...); void miniprintf(char *fmt, ...); -- 2.43.0