Browse Source

Jump forward backward by word

rexim 2 years ago
parent
commit
14ba265c61
3 changed files with 34 additions and 3 deletions
  1. 20 0
      src/editor.c
  2. 2 0
      src/editor.h
  3. 12 3
      src/main.c

+ 20 - 0
src/editor.c

@@ -118,6 +118,26 @@ void editor_move_char_right(Editor *e)
     if (e->cursor < e->data.count) e->cursor += 1;
 }
 
+void editor_move_word_left(Editor *e)
+{
+    while (e->cursor > 0 && !isalnum(e->data.items[e->cursor - 1])) {
+        e->cursor -= 1;
+    }
+    while (e->cursor > 0 && isalnum(e->data.items[e->cursor - 1])) {
+        e->cursor -= 1;
+    }
+}
+
+void editor_move_word_right(Editor *e)
+{
+    while (e->cursor < e->data.count && !isalnum(e->data.items[e->cursor])) {
+        e->cursor += 1;
+    }
+    while (e->cursor < e->data.count && isalnum(e->data.items[e->cursor])) {
+        e->cursor += 1;
+    }
+}
+
 void editor_insert_char(Editor *e, char x)
 {
     editor_insert_buf(e, &x, 1);

+ 2 - 0
src/editor.h

@@ -54,6 +54,8 @@ void editor_move_line_up(Editor *e);
 void editor_move_line_down(Editor *e);
 void editor_move_char_left(Editor *e);
 void editor_move_char_right(Editor *e);
+void editor_move_word_left(Editor *e);
+void editor_move_word_right(Editor *e);
 void editor_insert_char(Editor *e, char x);
 void editor_insert_buf(Editor *e, char *buf, size_t buf_len);
 void editor_retokenize(Editor *e);

+ 12 - 3
src/main.c

@@ -24,8 +24,9 @@
 // TODO: Save file dialog
 // Needed when ded is ran without any file so it does not know where to save.
 
-// TODO: Jump forward/backward by a word
+// TODO: Jump up/down by paragraph
 // TODO: Delete a word
+// TODO: Delete selection
 
 void MessageCallback(GLenum source,
                      GLenum type,
@@ -318,14 +319,22 @@ int main(int argc, char **argv)
 
                     case SDLK_LEFT: {
                         editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
-                        editor_move_char_left(&editor);
+                        if (event.key.keysym.mod & KMOD_CTRL) {
+                            editor_move_word_left(&editor);
+                        } else {
+                            editor_move_char_left(&editor);
+                        }
                         editor.last_stroke = SDL_GetTicks();
                     }
                     break;
 
                     case SDLK_RIGHT: {
                         editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
-                        editor_move_char_right(&editor);
+                        if (event.key.keysym.mod & KMOD_CTRL) {
+                            editor_move_word_right(&editor);
+                        } else {
+                            editor_move_char_right(&editor);
+                        }
                         editor.last_stroke = SDL_GetTicks();
                     }
                     break;