]> khamidou — GitHub mirrors - te.git/commitdiff
added things here and there
authorkhamidou <karim@lambda-expr.org>
Fri, 15 May 2009 12:25:49 +0000 (14:25 +0200)
committerkhamidou <karim@lambda-expr.org>
Fri, 15 May 2009 12:25:49 +0000 (14:25 +0200)
buffer.c
buffer.h
config.h [new file with mode: 0644]
main.c
screen.c
screen.h

index 8636a344be5c626cad6c294b100d4cd5a6acc343..3e8241f3c39582177b953aad5d5c037a1b064813 100644 (file)
--- 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;
 }
 
 
index e66e49d86a9718917e20fb86daa1b4f1b13b3f04..72d5b8d56215ed679906ebb9947a60575e27fbd8 100644 (file)
--- a/buffer.h
+++ b/buffer.h
@@ -4,7 +4,7 @@
 #include <stdio.h>
 #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 (file)
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 80a909a4957169688ee85afc385a18548edcd5e0..ba19fbefc138fa8a1fef906190f1694d941d285a 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,6 +1,6 @@
 /*
   te, a small text editor for unix.
- */
+*/
 
 #include <stdio.h>
 #include <curses.h>
index 28b98acb514aed009cbe77ffe131e8a58c5fbfa6..aac2adf5b0d5e6ec430cf4a4e0b5c52146b16bf8 100644 (file)
--- 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)
index 5731e1099d2bee9c234d3d4de6475ab0af7221ed..1feac12d38cdee36f013a8dd7a5dcb6ccd19d5cf 100644 (file)
--- a/screen.h
+++ b/screen.h
@@ -6,6 +6,8 @@
 #include <curses.h>
 
 #include "buffer.h"
+#include "config.h"
+
 
 WINDOW *buffer_win, *status_win, *minibuffer_win;