r/o

Newline! e510b0df parent f1208672

authored by Yuki Izumi

authored by Yuki Izumi <yuki@kivikakk.ee> 10 years ago

Newline!

TODO | 2 +-
qb.c | 18 ++++++++++++++++++
2 files changed, 19 insertions(+), 1 deletion(-)
diff --git a/TODO b/TODO
index 0ab0984..7416d18 100644
--- a/TODO
+++ b/TODO
@@ -1,5 +1,5 @@
* model a text editor
- * newlines, backspace
+ * backspace
* virtual space entry
* scrolling
* model the application GUI
diff --git a/qb.c b/qb.c
index 0fd4f66..cd2753c 100644
--- a/qb.c
+++ b/qb.c
@@ -75,6 +75,20 @@ static void insert_character(doc_line_t *d, int offset, char c) {
++d->stored;
}
+static void split_line(doc_line_t *d, int offset) {
+ doc_line_t *n = create_doc_line();
+ n->next = d->next;
+ d->next = n;
+
+ n->stored = d->stored - offset;
+ ensure_available(n, n->stored);
+ memcpy(n->line, d->line + offset, n->stored);
+
+ d->stored -= n->stored;
+
+ ++total_lines;
+}
+
static char get_character(SDL_Keycode sym, Uint16 mod) {
if (sym >= SDLK_a && sym <= SDLK_z) {
if (mod & (KMOD_SHIFT | KMOD_CAPS)) {
@@ -134,6 +148,10 @@ void qb_keypress(SDL_Keycode sym, Uint16 mod) {
cursor_x,
get_character(sym, mod));
++cursor_x;
+ } else if (sym == SDLK_RETURN) {
+ split_line(get_current_doc_line(), cursor_x);
+ cursor_x = 0;
+ ++cursor_y;
}
qb_render();