]> khamidou — GitHub mirrors - te.git/commitdiff
backspace and enter work at last...
authorkhamidou <karim@lambda-expr.org>
Fri, 17 Jul 2009 08:28:55 +0000 (10:28 +0200)
committerkhamidou <karim@lambda-expr.org>
Fri, 17 Jul 2009 08:28:55 +0000 (10:28 +0200)
Makefile
README
buffer.c
buffer.h
keyb.c
keyb.h
main.c
screen.c
screen.h

index 222c065a25cc561100e74dace71ab24b92515fa7..d28f336f89d9e57dc70e57a37ecd4d9f552785fa 100644 (file)
--- 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
+LDFLAGS=-lcurses -lslang
+OBJS=main.o errors.o screen.o buffer.o keyb.o interp.o bstrlib.o
 
 te: $(OBJS)
        $(CC) $(LDFLAGS) -o te $(OBJS)
@@ -21,6 +21,9 @@ buffer.o: buffer.c
 keyb.o: keyb.c
        $(CC) $(CFLAGS) keyb.c
 
+interp.o: interp.c
+       $(CC) $(CFLAGS) interp.c
+
 bstrlib.o: bstrlib.c
        $(CC) $(CFLAGS) bstrlib.c
 
diff --git a/README b/README
index 76f0517f02f6814df0bc8048e177ed530ea4d526..ef21b4003b05437be87f71ba0c471b5d8dc783c1 100644 (file)
--- a/README
+++ b/README
@@ -1,6 +1,5 @@
 te, Copyright (c) 2008-2009 Karim Hamidou
-All rights reserved.
-
+gAll rights reserved.
 Redistribution and use in source and binary forms, with or without 
 modification, are permitted provided that the following conditions are met:
 
index b38ae7ef83b6fb19ffde609d85e7215b0ba49272..a7dd84ee7cdbaf818244e9896d9622253019510a 100644 (file)
--- a/buffer.c
+++ b/buffer.c
@@ -23,6 +23,7 @@ struct te_buffer *alloc_and_insert_buffer(void)
        b->x = 0;
        b->y = 0;
        b->point = 0;
+       b->top_char = 0;
 
        return b;
 }
@@ -37,6 +38,18 @@ void free_buffer(struct te_buffer *b)
        free(b);
 }
 
+struct te_buffer* search_buffer(char *name)
+{
+       if (name == NULL)
+               return;
+
+       struct te_buffer *b;
+       TAILQ_FOREACH(b, &buffers_head, buffers) {
+               if (strcmp(name, b->name) == 0)
+                       return b;
+       }
+}
+
 struct te_buffer* load_buffer(char *filename)
 {
        FILE *fp = fopen(filename, "r");
index d5aa5b4b2750e3216cd133c3ce6ab280f57188ee..95bad3a3d700e5dc0506a13c10884263aacf3bca 100644 (file)
--- a/buffer.h
+++ b/buffer.h
@@ -13,7 +13,7 @@ struct te_buffer {
        int lineno; /* the number of '\n' seen so far */
        bstring contents;
        int point;              /* offset in "contents" */
-       int top_char, bottom_char; /* the char at the top and at the bottom of the screen */
+       int top_char; /* the position of the char at the top of the screen */
        TAILQ_ENTRY(te_buffer) buffers;
 };
 
@@ -21,6 +21,7 @@ TAILQ_HEAD(te_buffers_head, te_buffer) buffers_head;
 
 struct te_buffer               *alloc_and_insert_buffer(void);
 void                           free_buffer(struct te_buffer *b);
+struct te_buffer* search_buffer(char *name);
 struct te_buffer*              load_buffer(char *filename);
 
 void                           init_buffers(void);
diff --git a/keyb.c b/keyb.c
index 52c8c7a9fe558407d5bb94793b32941dea792f84..674d9e99a740cce35e6b5b58b3c1e326fda3b5f8 100644 (file)
--- a/keyb.c
+++ b/keyb.c
@@ -15,10 +15,10 @@ void input_loop(void)
 
                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); */
+               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);
 
        }
 
@@ -55,6 +55,10 @@ void process_command(int c)
                                write_buffer(current_buf);
                                break;
 
+                       case 'x':
+                               screen_delete_char(current_buf);
+                               break;
+
                        case 'i':
                        case KEY_END:
                                /* switch between command and input mode */
@@ -62,10 +66,8 @@ void process_command(int c)
                                break;
 
                        case 'v':
-                               str = read_user_input();
-                               miniprintf("you entered : %s", str);
-                               break;
 
+                       case 'b':
                        default:
                                miniprintf("%c - invalid command", c);
                                break;
@@ -75,6 +77,10 @@ void process_command(int c)
 void process_input(int c)
 {
                        switch(c) {
+                       case KEY_ENTER:
+                               screen_insert_char(current_buf, '\n');
+                               break;
+
                        case KEY_BACKSPACE:
                                screen_delete_char(current_buf);
                                break;
diff --git a/keyb.h b/keyb.h
index eec90caf5caad679d15b92906b11708c7784e094..5dc5ece144b08ff8caab53d648138a39c8e081ec 100644 (file)
--- a/keyb.h
+++ b/keyb.h
@@ -10,5 +10,7 @@ void process_command(int c); /* process a command */
 void process_input(int c);   /* process a normal char - that is, not a command */
 
 struct te_buffer *current_buf;
+struct te_buffer *buf1;
+struct te_buffer *buf2;
 
 #endif
diff --git a/main.c b/main.c
index d52a57da45e8d8da9e7b2741146b7633164b2fa2..7c129ec077417bfa401c7a33885ba0c18f23537f 100644 (file)
--- a/main.c
+++ b/main.c
@@ -4,6 +4,7 @@
 
 #include <stdio.h>
 #include <curses.h>
+#include "interp.h"
 #include "screen.h"
 
 extern struct te_buffer *current_buf;
@@ -11,9 +12,11 @@ extern struct te_buffer *current_buf;
 int main(int argc, char **argv)
 {
        init_buffers();
+       init_interp();
        init_windows();
 
-       current_buf = load_buffer("README");
+       current_buf = load_buffer("tests/bufferfuncs.c");
+
        paint_buffer(current_buf);
        refresh();
        input_loop();
index ac1cebd655d005b83900e7e5bb5901b093a7c29a..b2eba9d7150b6521026154ed6d364a82217cb12e 100644 (file)
--- a/screen.c
+++ b/screen.c
@@ -1,6 +1,7 @@
 #include "zalloc.h"
 #include "screen.h"
 #include "util.h"
+#include "keyb.h"
 
 static int scr_y, scr_x;
 
@@ -37,6 +38,12 @@ void cleanup_windows(void)
 }
 
 void paint_buffer(struct te_buffer *buf)
+{
+       paint_buffer_nlines(buf, LINES - 2);
+}
+
+/* paint the first nlines of a buffer */
+void paint_buffer_nlines(struct te_buffer *buf, int nlines)
 {
        if (buf == NULL)
                return;
@@ -46,9 +53,9 @@ void paint_buffer(struct te_buffer *buf)
 
        bstring s;
        int i = 0;
-       int count = 0;
+       int count = buf->top_char;
 
-       for(i = 0; i < (LINES - 2); i++) {
+       for(i = 0; i < nlines; i++) {
                s = current_line_as_bstring(buf->contents, count);
                draw_line(s, i);
                count += blength(s);
@@ -59,6 +66,45 @@ void paint_buffer(struct te_buffer *buf)
        
        restoreyx();
        refresh();
+
+}
+
+void paint_nthline(struct te_buffer *buf, int n, int y)
+{
+       if (buf == NULL)
+               return;
+
+       saveyx();
+       wmove(buffer_win, 0, 0);
+
+       bstring s;
+       int i = 0;
+       int count = buf->top_char;
+
+       for(i = 0; i < n; i++) {
+               s = current_line_as_bstring(buf->contents, count);
+               count += blength(s);
+
+               if (count >= blength(buf->contents))
+                   break;
+       }
+
+       draw_line(s, y);
+
+       restoreyx();
+}
+
+void clear_nfirst_lines(WINDOW *w, int n)
+{
+       int i = 0;
+       saveyx();
+
+       for (i = 0; i < n; i++) {
+               wmove(w, i, 0);
+               wclrtoeol(w);
+       }
+
+       restoreyx();
 }
 
 void draw_line(bstring s, int y)
@@ -75,7 +121,12 @@ void draw_line(bstring s, int y)
                if (bchar(s, i) == '\t') {
                        for (j = 0; j < TAB_LEN; j++)
                                mvwaddch(buffer_win, y, screen_abs++, ' ');
-               } else {
+               }
+/*             else if (bchar(s, i) == '\n') { */
+/*                             mvwaddstr(buffer_win, y, screen_abs++, "\\n"); */
+/*                             screen_abs++; */
+/*                             mvwaddch(buffer_win, y, screen_abs++, '\n'); */
+/*             } */ else {
                        mvwaddch(buffer_win, y, screen_abs, bchar(s, i));
                        screen_abs++;
                }
@@ -83,14 +134,20 @@ void draw_line(bstring s, int y)
        restoreyx();
 }
 
-void scroll_up(struct te_buffer *buf)
+void scroll_up(WINDOW *w)
 {
-
+       scrollok(w, TRUE);
+       wscrl(w, 1);
+       touchwin(stdscr); /* We have to use it to scroll the window. */
+       scrollok(w, FALSE);
 }
 
-void scroll_down(struct te_buffer *buf)
+void scroll_down(WINDOW *w)
 {
-
+       scrollok(w, TRUE);
+       wscrl(w, -1);
+       touchwin(stdscr); /* We have to use it to scroll the window. */
+       scrollok(w, FALSE);
 }
 
 void screen_prev_line(struct te_buffer *buf)
@@ -102,14 +159,20 @@ void screen_prev_line(struct te_buffer *buf)
                buf->y--;
                buf->x = screen_line_length(buf->contents, buf->point);
        } else {
-               /* scroll up */
-               scrollok(buffer_win, TRUE);
-               wscrl(buffer_win, -1);
-               touchwin(stdscr); /* We have to use it to scroll the window. */
-               scrollok(buffer_win, FALSE);
+
+               if (bstrrchrp(buf->contents, '\n', buf->point) == BSTR_ERR) /* is it the first line of the file ? */
+                       return;
+
+               scroll_down(buffer_win);
                bstring s = current_line_as_bstring(buf->contents, buf->point - 1);
                draw_line(s, 0);
                bdestroy(s);
+
+               /* update buf->top_char */
+               s = current_line_as_bstring(buf->contents, buf->top_char);
+               buf->top_char -= blength(s);
+               buf->top_char = max(buf->top_char, 0);
+               bdestroy(s);
        }
 }
 
@@ -122,13 +185,16 @@ void screen_next_line(struct te_buffer *buf)
        if (buf->y < LINES - 4)
                buf->y++;
        else {
-               scrollok(buffer_win, TRUE);
-               wscrl(buffer_win, 1);
-               touchwin(stdscr); /* We have to use it to scroll the window. */
-               scrollok(buffer_win, FALSE);
+               scroll_up(buffer_win);
                bstring s = current_line_as_bstring(buf->contents, buf->point - 1);
                draw_line(s, LINES - 4);
                bdestroy(s);
+
+               /* update buf->top_char */
+               s = current_line_as_bstring(buf->contents, buf->top_char);
+               buf->top_char += blength(s);
+               buf->top_char = max(buf->top_char, blength(buf->contents));
+               bdestroy(s);
        }
 }
 
@@ -249,12 +315,23 @@ void screen_insert_char(struct te_buffer *buf, char c)
        if (buf == NULL)
                return;
 
+       if (buf->dirty < 1)
+               buf->dirty = 1;
+
        insert_char(buf, c);
-       
-       bstring s = current_line_as_bstring(buf->contents, buf->point);
-       draw_line(s, buf->y);
-       screen_move_right(buf);
-       
+
+       if (c == '\n') {
+               clrtoeol();
+               clear_nfirst_lines(buffer_win, buf->y - 1);
+               scroll_down(buffer_win);
+               paint_buffer_nlines(buf, buf->y + 1);
+               paint_nthline(buf, buf->y + 2, buf->y + 1);
+               screen_move_right(buf);
+       } else {
+               bstring s = current_line_as_bstring(buf->contents, buf->point);
+               draw_line(s, buf->y);
+               screen_move_right(buf);
+       }
 }
 
 void screen_delete_char(struct te_buffer *buf)
@@ -262,17 +339,49 @@ void screen_delete_char(struct te_buffer *buf)
        if (buf == NULL)
                return;
 
+       move_left(buf);
+       bstring s = current_line_as_bstring(buf->contents, max(buf->point - 1, 0));
+       char c = curr_char(buf);
        delete_char(buf);
-       
-       bstring s = current_line_as_bstring(buf->contents, buf->point);
-       draw_line(s, buf->y);
-       screen_move_left(buf);
-       move_right(buf); /* yes it's ugly but I don't feel like recoding screen_move_right atm */
+       move_right(buf);
+
+       if (buf->dirty < 1)
+               buf->dirty = 1;
+
+       if (c == '\n') {
+               bstring s2 = current_line_as_bstring(buf->contents, max(buf->point - blength(s), 0));
+               statusprintf("s : %s", bstr2cstr(s, '\0'));
+               clrtoeol();
+               clear_nfirst_lines(buffer_win, buf->y);
+               scroll_up(buffer_win);
+               paint_buffer_nlines(buf, buf->y + 1); 
+//             screen_prev_line(buf);
+               screen_prev_line(buf);
+               buf->x = screen_line_length(s, 0);
+               move(buf->y, buf->x);
+       } else {
+               bstring s = current_line_as_bstring(buf->contents, buf->point);
+               draw_line(s, buf->y);
+               screen_move_left(buf);
+               move_right(buf); /* yes it's ugly but I don't feel like recoding screen_move_right atm */
+       }
+
 }
 
 void screen_switch_buffer(struct te_buffer *buf)
 {
-       
+       if (buf == NULL)
+               return;
+
+       wclear(buffer_win);
+       wclear(status_win);
+       statusprintf("%s", buf->name);
+
+       paint_buffer(buf);
+       move(buf->y, buf->x);
+       current_buf = buf;
+
+       return;
 }
 
 /*
@@ -338,25 +447,28 @@ void notifyprintf(char *fmt, ...)
        getch(); /* discard input */
 }
 
-char *read_user_input(void)
+char *ask_user(char *fmt, ...)
 {
+       va_list ap;
+       va_start(ap, fmt);
+       miniprintf(fmt, ap);
+       va_end(ap);
+
        char *s = zalloc(255);
        int c = 0;
        int i = 0;
 
-       for (i = 0; i < 255 && c != KEY_ENTER; i++) {
-               c = getch();
-               waddch(minibuffer_win, c);
-               *(s + i) = c;
-       }
-
-       if ((void *) c == NULL)
+       if (s == NULL)
                fail("Unable to allocate memory at __FILE__:__LINE__\n");
 
-       if (getnstr(c, 255) == ERR)
-               return (char *) ERR;
+       do {
+               c = wgetch(minibuffer_win);
+               wechochar(minibuffer_win, c);
+               s[i] = c;
+               i++;
+       } while (i < 255 && c != '\n');
 
-       return (char *) c;
+       return s;
 }
 
 void console_signal_handler(int sig)
index bcb31a3d3268ddc3534c55cf86d8158823779022..71d988fc01e6a835cb83ceab427b3ca2b1635608 100644 (file)
--- a/screen.h
+++ b/screen.h
@@ -15,10 +15,13 @@ void init_windows(void);
 void cleanup_windows(void);
 
 void paint_buffer(struct te_buffer *buf);
+void paint_buffer_nlines(struct te_buffer *buf, int nlines);
+void paint_nthline(struct te_buffer *buf, int n, int y);
+void clear_nfirst_lines(WINDOW *w, int n);
 void draw_line(bstring s, int y);
+void scroll_up(WINDOW *w);
+void scroll_down(WINDOW *w);
 
-void scroll_up(struct te_buffer *buf);
-void scroll_down(struct te_buffer *buf);
 int  screen_move_left(struct te_buffer *buf);
 int  screen_move_right(struct te_buffer *buf);
 void screen_move_up(struct te_buffer *buf);
@@ -27,7 +30,7 @@ void screen_move_down(struct te_buffer *buf);
 void statusprintf(char *fmt, ...);
 void miniprintf(char *fmt, ...);
 void notifyprintf(char *fmt, ...);
-char *read_user_input(void);
+char *ask_user(char *fmt, ...);
 
 void console_signal_handler(int sig);