commit 14ba265c61a9e084d8c47ad0800fb3e1614b5883
parent 759c47633d142ba37c0fc3620d472ffb40851cd7
Author: rexim <reximkut@gmail.com>
Date: Fri, 17 Feb 2023 11:24:00 +0700
Jump forward backward by word
Diffstat:
3 files changed, 34 insertions(+), 3 deletions(-)
diff --git a/src/editor.c b/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);
diff --git a/src/editor.h b/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);
diff --git a/src/main.c b/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;