From b3af1454642f11d459f549bceac0a95c85501a33 Mon Sep 17 00:00:00 2001 From: khamidou Date: Wed, 8 Jul 2009 19:56:06 +0200 Subject: [PATCH] implemented backspace support --- buffer.c | 24 ++++++++++++++++++++++++ buffer.h | 1 + keyb.c | 4 ++++ screen.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/buffer.c b/buffer.c index 0d20423..a0276af 100644 --- a/buffer.c +++ b/buffer.c @@ -207,8 +207,32 @@ void insert_char(struct te_buffer *buf, char c) return; binsertch(buf->contents, buf->point, 1, c); + return; +} + +void delete_char(struct te_buffer *buf) +{ + if (buf == NULL) + return; + bdelete(buf->contents, buf->point, 1); + move_left(buf); return; } +void write_buffer(struct te_buffer *buf) +{ + if (buf == NULL) + return; + + char *c = bstr2cstr(buf->contents, '\0'); + FILE *fp = fopen(buf->name, "w+"); + + if (fp == NULL) + notifyprintf("Unable to open %s", buf->name); + + fwrite(c, strlen(c), sizeof(char), fp); + + free(c); +} diff --git a/buffer.h b/buffer.h index 98f6e17..d5aa5b4 100644 --- a/buffer.h +++ b/buffer.h @@ -33,5 +33,6 @@ 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); +void delete_char(struct te_buffer *buf); #endif diff --git a/keyb.c b/keyb.c index d0089b6..e14c061 100644 --- a/keyb.c +++ b/keyb.c @@ -25,6 +25,10 @@ void input_loop(void) screen_move_up(current_buf); break; + case KEY_BACKSPACE: + screen_delete_char(current_buf); + break; + case 'u': wscrl(buffer_win, -10); touchwin(stdscr); diff --git a/screen.c b/screen.c index 358bed7..acb7f81 100644 --- a/screen.c +++ b/screen.c @@ -251,6 +251,19 @@ void screen_insert_char(struct te_buffer *buf, char c) } +void screen_delete_char(struct te_buffer *buf) +{ + if (buf == NULL) + return; + + 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 */ +} + /* display a message in the status bar. */ @@ -300,6 +313,21 @@ void miniprintf(char *fmt, ...) restoreyx(); } +/* + notifyprintf : display a message in the minibuffer and wait that the user + presses a key. + */ +void notifyprintf(char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + miniprintf(fmt, ap); + va_end(ap); + + getch(); /* discard input */ +} + + void console_signal_handler(int sig) { switch(sig) { -- 2.43.0