From da2c9f0261e4e13738057869eae71e2665ded59a Mon Sep 17 00:00:00 2001 From: khamidou Date: Fri, 15 May 2009 14:25:49 +0200 Subject: [PATCH] added things here and there --- buffer.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- buffer.h | 8 +++++--- config.h | 6 ++++++ main.c | 2 +- screen.c | 34 +++++++++++++++++++++------------- screen.h | 2 ++ 6 files changed, 85 insertions(+), 19 deletions(-) create mode 100644 config.h diff --git a/buffer.c b/buffer.c index 8636a34..3e8241f 100644 --- a/buffer.c +++ b/buffer.c @@ -21,6 +21,7 @@ struct te_buffer *alloc_and_insert_buffer(void) b->x = 0; b->y = 0; + b->point = 0; return b; } @@ -65,23 +66,70 @@ struct te_buffer* load_buffer(char *filename) return b; } -void prev_char(struct te_buffer *buf) +/* Compute the length of a string between two '\n' */ + +int line_length(bstring b, int point) +{ + if (b == NULL) + return -1; + + int c_offset = bstrchrp(b, '\n', point); + if (c_offset == BSTR_ERR) + c_offset = 0; + + int p_offset = bstrrchrp(b, '\n', c_offset - 1); + if (p_offset == BSTR_ERR) + p_offset = 0; + + return c_offset - p_offset; +} + +/* + The length of a line as displayed on the screen. + */ +int screen_line_length(bstring b, int point) +{ + if (b == NULL) + return -1; + + int len = line_length(b, point); + int i = 0; + int count = 0; + + for (i = 0; i < len; i++) { + if (bchar(b, point - i) == '\t') + count += 8; + else + count += 1; + } + + return count; +} + +int prev_char(struct te_buffer *buf) { if (buf == NULL) return; if (buf->point > 0) buf->point--; + else + return ERR; + + return OK; } -void next_char(struct te_buffer *buf) +int next_char(struct te_buffer *buf) { if (buf == NULL) return; if (buf->point < blength(buf->contents)) buf->point++; + else + return ERR; + return OK; } diff --git a/buffer.h b/buffer.h index e66e49d..72d5b8d 100644 --- a/buffer.h +++ b/buffer.h @@ -4,7 +4,7 @@ #include #include "bstrlib.h" #include "queue.h" - +#include "config.h" struct te_buffer { char *name; @@ -28,8 +28,10 @@ void free_buffer(struct te_buffer *b); struct te_buffer* load_buffer(char *filename); void init_buffers(void); -void prev_char(struct te_buffer *buf); -void next_char(struct te_buffer *buf); +int line_length(bstring b, int point); +int screen_line_length(bstring b, int point); +int prev_char(struct te_buffer *buf); +int next_char(struct te_buffer *buf); diff --git a/config.h b/config.h new file mode 100644 index 0000000..c4274cc --- /dev/null +++ b/config.h @@ -0,0 +1,6 @@ +#ifndef __CONFIG_H__ +#define __CONFIG_H__ + +#define TAB_LEN 8 + +#endif diff --git a/main.c b/main.c index 80a909a..ba19fbe 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,6 @@ /* te, a small text editor for unix. - */ +*/ #include #include diff --git a/screen.c b/screen.c index 28b98ac..aac2adf 100644 --- a/screen.c +++ b/screen.c @@ -61,19 +61,18 @@ void move_left(struct te_buffer *buf) return; if (buf->x > 0) - buf->x--; + if (bchar(buf->contents, buf->point) == '\t') + buf->x -= TAB_LEN; + else + buf->x--; + else if (buf->y > 0) { buf->y--; - /* Move the cursor to the last character of the previous string */ - int c_offset = bstrrchrp(buf->contents, '\n', buf->point); - int p_offset = bstrrchrp(buf->contents, '\n', c_offset); - - /* We compute the difference between the two numbers to get the row number to move the cursor to */ - buf->x = c_offset - p_offset; + buf->x = screen_line_length(buf->contents, buf->point); } prev_char(buf); - wmove(buffer_win, buf->y, buf->x); + move(buf->y, buf->x); } void move_right(struct te_buffer *buf) @@ -81,16 +80,25 @@ void move_right(struct te_buffer *buf) if (buf == NULL) return; - if (buf->x < COLS) - buf->x++; - else if (buf->y < LINES) { + if (next_char(buf) == ERR) /* last char of the document */ + return; + + 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++; + } + else if (buf->y < LINES - 3) { buf->y++; buf->x = 0; } - next_char(buf); - wmove(buffer_win, buf->y, 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_up(struct te_buffer *buf) diff --git a/screen.h b/screen.h index 5731e10..1feac12 100644 --- a/screen.h +++ b/screen.h @@ -6,6 +6,8 @@ #include #include "buffer.h" +#include "config.h" + WINDOW *buffer_win, *status_win, *minibuffer_win; -- 2.43.0