]> khamidou — GitHub mirrors - te.git/commitdiff
fixed a cursor position bug
authorkhamidou <karim@lambda-expr.org>
Wed, 17 Jun 2009 18:52:17 +0000 (20:52 +0200)
committerkhamidou <karim@lambda-expr.org>
Wed, 17 Jun 2009 18:52:17 +0000 (20:52 +0200)
README
buffer.c
buffer.h
keyb.c
main.c
screen.c
screen.h
util.h

diff --git a/README b/README
index b8fd027d12452c054be44902731262adbcfa80d3..76f0517f02f6814df0bc8048e177ed530ea4d526 100644 (file)
--- 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.
index 3941995392c2cb3a2c9e1b4e4a158b3c8c889b1a..387055d054fde75bc8b568d32ace17f2974f6a4e 100644 (file)
--- 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)
index cd3dde797cb5b340193c3775ebe5dc31105d87e3..85aa93f55ec335a29b7babfc8695b4bad12ca06f 100644 (file)
--- 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 30ee644666171ace0c9f475d06bdb986d397a749..ae20df4ee7b8069db151ba7318deb14f8f0c112f 100644 (file)
--- 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 1a050ba28a683f8fc6f64c4aa992f42a95049d8f..0d92e8a26f00f8ec74a29f3ea25e7b1f27023ceb 100644 (file)
--- 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();
index 31018cb747eeef76dfa8901f1c8563b1053d4334..f172a56937473599175d97029ebe591649703b6d 100644 (file)
--- 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)
 
        }
 }
+
index 5c3d4f12dff7088022c57cc0decb2af3f7d9b39d..f84335eef906146d19d11704af6b46c30e2950cd 100644 (file)
--- 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 01428243e954f4e3945bac260c7f1a629ff2be1d..0f1687765f557ec5b75e97a879b06229a5384182 100644 (file)
--- 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