From 119d4d708ce752ad8a89d9bede22c555d2601193 Mon Sep 17 00:00:00 2001 From: khamidou Date: Wed, 17 Jun 2009 20:52:17 +0200 Subject: [PATCH] fixed a cursor position bug --- README | 28 ++++++++++++++++++++++++++ buffer.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++--------- buffer.h | 4 +++- keyb.c | 7 ++++++- main.c | 2 +- screen.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++---- screen.h | 1 + util.h | 3 ++- 8 files changed, 144 insertions(+), 17 deletions(-) diff --git a/README b/README index b8fd027..76f0517 100644 --- a/README +++ b/README @@ -1,3 +1,31 @@ +te, Copyright (c) 2008-2009 Karim Hamidou +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. Portions Copyright (c) 2002-2008 Paul Hsieh All rights reserved. diff --git a/buffer.c b/buffer.c index 3941995..387055d 100644 --- a/buffer.c +++ b/buffer.c @@ -4,6 +4,7 @@ #include "zalloc.h" #include "error.h" #include "buffer.h" +#include "util.h" void init_buffers(void) { @@ -64,15 +65,29 @@ 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 = blength(b); + int s_offset = bstrrchrp(b, '\n', point - 1); + if (s_offset == BSTR_ERR) + s_offset = 0; - int p_offset = bstrrchrp(b, '\n', c_offset - 1); - if (p_offset == BSTR_ERR) - p_offset = 0; + int e_offset = bstrchrp(b, '\n', point); + if (e_offset == BSTR_ERR) + e_offset = blength(b); - return c_offset - p_offset; + + return e_offset - s_offset; +} + +bstring current_line_as_bstring(bstring b, int point) +{ + int s_offset = bstrrchrp(b, '\n', point - 1); + if (s_offset == BSTR_ERR) + s_offset = 0; + + int e_offset = bstrchrp(b, '\n', point); + if (e_offset == BSTR_ERR) + e_offset = blength(b); + + return bmidstr(b, s_offset, e_offset - s_offset); } /* @@ -100,6 +115,7 @@ int screen_line_length(bstring b, int point) accessor and mutators for the contents of the buffer. */ + int prev_char(struct te_buffer *buf) { if (buf == NULL) @@ -129,8 +145,11 @@ int move_left(struct te_buffer *buf) if (buf == NULL) return; - if (buf->point > 0) + if (buf->point > 0) { buf->point--; + if (bchar(buf->contents, buf->point) == '\n') + decrement_or_nullify(buf->lineno); + } else return ERR; @@ -143,14 +162,38 @@ int move_right(struct te_buffer *buf) if (buf == NULL) return; - if (buf->point < blength(buf->contents)) + if (buf->point < blength(buf->contents)) { buf->point++; + if (bchar(buf->contents, buf->point) == '\n') + buf->lineno++; + } else return ERR; return OK; } +void move_to_prev_line(struct te_buffer *buf) +{ + if (buf == NULL) + return; + + while(curr_char(buf) != '\n') + move_left(buf); + +} + +void move_to_next_line(struct te_buffer *buf) +{ + if (buf == NULL) + return; + + while(curr_char(buf) != '\n') + move_right(buf); + + move_right(buf); +} + void insert_char(struct te_buffer *buf, char c) { if (buf == NULL) diff --git a/buffer.h b/buffer.h index cd3dde7..85aa93f 100644 --- a/buffer.h +++ b/buffer.h @@ -11,6 +11,7 @@ struct te_buffer { char *name; int dirty; int x, y; + int lineno; /* the number of '\n' seen so far */ bstring contents; int point; /* offset in "contents" */ struct te_char *scr_top, *scr_bottom; /* the first and last char of the screen */ @@ -33,11 +34,12 @@ struct te_buffer* load_buffer(char *filename); void init_buffers(void); int line_length(bstring b, int point); int screen_line_length(bstring b, int point); +bstring current_line_as_bstring(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); - +void insert_char(struct te_buffer *buf, char c); #endif diff --git a/keyb.c b/keyb.c index 30ee644..ae20df4 100644 --- a/keyb.c +++ b/keyb.c @@ -23,8 +23,13 @@ void input_loop(void) case KEY_DOWN: screen_move_down(current_buf); break; + + case 'v': + screen_insert_char(current_buf, 'X'); + paint_buffer(current_buf); + break; } - + refresh(); miniprintf("%c, line length: %d, y: %d, x%d", diff --git a/main.c b/main.c index 1a050ba..0d92e8a 100644 --- a/main.c +++ b/main.c @@ -15,7 +15,7 @@ int main(int argc, char **argv) init_buffers(); init_windows(); - current_buf = load_buffer("README"); + current_buf = load_buffer("main.c"); paint_buffer(current_buf); refresh(); input_loop(); diff --git a/screen.c b/screen.c index 31018cb..f172a56 100644 --- a/screen.c +++ b/screen.c @@ -1,5 +1,8 @@ #include "screen.h" +static int y, x; + + void init_windows(void) { initscr(); @@ -34,16 +37,32 @@ void paint_buffer(struct te_buffer *buf) wmove(buffer_win, 0, 0); - char c; + bstring s; int i = 0; + int pos = 0; - for(i = 0; i < (LINES - 3) * COLS; i++) - waddch(buffer_win, bchar(buf->contents, i)); + for(i = 0; i < (LINES - 3); i++) { + s = current_line_as_bstring(buf->contents, pos); + draw_line(s, i); + pos += line_length(s, pos); + } wmove(buffer_win, 0, 0); refresh(); } +void draw_line(bstring s, int y) +{ + int i; + int j; + /* 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)); +} void scroll_up(struct te_buffer *buf) { @@ -153,7 +172,8 @@ void screen_move_down(struct te_buffer *buf) } while(curr_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); screen_move_right(buf); @@ -162,8 +182,28 @@ void screen_move_down(struct te_buffer *buf) return; } +void screen_insert_char(struct te_buffer *buf, char c) +{ + if (buf == NULL) + return; + + statusprintf("buf x : %d, y : %d", buf->x, buf->y); + insert_char(buf, c); + + bstring s = current_line_as_bstring(buf->contents, buf->point); + draw_line(s, buf->y); + + +} + +/* + display a message in the status bar. + */ void statusprintf(char *fmt, ...) { + + getyx(stdscr, y, x); + werase(status_win); wmove(status_win, 0, 0); @@ -184,11 +224,15 @@ void statusprintf(char *fmt, ...) } wrefresh(status_win); + + move(y, x); } void miniprintf(char *fmt, ...) { + getyx(stdscr, y, x); + va_list ap; va_start(ap, fmt); werase(minibuffer_win); @@ -196,6 +240,8 @@ void miniprintf(char *fmt, ...) va_end(ap); wrefresh(minibuffer_win); + + move(y, x); } void console_signal_handler(int sig) @@ -208,3 +254,4 @@ void console_signal_handler(int sig) } } + diff --git a/screen.h b/screen.h index 5c3d4f1..f84335e 100644 --- a/screen.h +++ b/screen.h @@ -15,6 +15,7 @@ void init_windows(void); void cleanup_windows(void); void paint_buffer(struct te_buffer *buf); +void draw_line(bstring s, int y); void scroll_up(struct te_buffer *buf); void scroll_down(struct te_buffer *buf); diff --git a/util.h b/util.h index 0142824..0f16877 100644 --- a/util.h +++ b/util.h @@ -1,6 +1,7 @@ #ifndef __TE_UTIL_H__ #define __TE_UTIL_H__ -#define decrement_or_nullify(x) (x > 0) ? x-- : x = 0; +#define decrement_or_nullify(x) (x = (x > 0) ? x - 1 : 0) +#define increment_or_maximize(x, max) (x = (x > max) ? x + 1 : max) #endif -- 2.43.0