Kaynağa Gözat

Implement more navigation abilities

rexim 2 yıl önce
ebeveyn
işleme
7f3638d25e
3 değiştirilmiş dosya ile 98 ekleme ve 9 silme
  1. 52 0
      src/editor.c
  2. 10 0
      src/editor.h
  3. 36 9
      src/main.c

+ 52 - 0
src/editor.c

@@ -490,3 +490,55 @@ bool editor_search_matches_at(Editor *e, size_t pos)
     }
     return true;
 }
+
+void editor_move_to_begin(Editor *e)
+{
+    editor_stop_search(e);
+    e->cursor = 0;
+}
+
+void editor_move_to_end(Editor *e)
+{
+    editor_stop_search(e);
+    e->cursor = e->data.count;
+}
+
+void editor_move_to_line_begin(Editor *e)
+{
+    editor_stop_search(e);
+    size_t row = editor_cursor_row(e);
+    e->cursor = e->lines.items[row].begin;
+}
+
+void editor_move_to_line_end(Editor *e)
+{
+    editor_stop_search(e);
+    size_t row = editor_cursor_row(e);
+    e->cursor = e->lines.items[row].end;
+}
+
+void editor_move_paragraph_up(Editor *e)
+{
+    editor_stop_search(e);
+    size_t row = editor_cursor_row(e);
+    while (row > 0 && e->lines.items[row].end - e->lines.items[row].begin <= 1) {
+        row -= 1;
+    }
+    while (row > 0 && e->lines.items[row].end - e->lines.items[row].begin > 1) {
+        row -= 1;
+    }
+    e->cursor = e->lines.items[row].begin;
+}
+
+void editor_move_paragraph_down(Editor *e)
+{
+    editor_stop_search(e);
+    size_t row = editor_cursor_row(e);
+    while (row + 1 < e->lines.count && e->lines.items[row].end - e->lines.items[row].begin <= 1) {
+        row += 1;
+    }
+    while (row + 1 < e->lines.count && e->lines.items[row].end - e->lines.items[row].begin > 1) {
+        row += 1;
+    }
+    e->cursor = e->lines.items[row].begin;
+}

+ 10 - 0
src/editor.h

@@ -53,12 +53,22 @@ Errno editor_load_from_file(Editor *editor, const char *file_path);
 void editor_backspace(Editor *editor);
 void editor_delete(Editor *editor);
 size_t editor_cursor_row(const Editor *e);
+
 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_move_to_begin(Editor *e);
+void editor_move_to_end(Editor *e);
+void editor_move_to_line_begin(Editor *e);
+void editor_move_to_line_end(Editor *e);
+
+void editor_move_paragraph_up(Editor *e);
+void editor_move_paragraph_down(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);

+ 36 - 9
src/main.c

@@ -26,10 +26,9 @@
 // Needed when ded is ran without any file so it does not know where to save.
 
 // TODO: An ability to create a new file
-// TODO: Jump up/down by paragraph
 // TODO: Delete a word
 // TODO: Delete selection
-// TODO: Jump to the beginning/end of the line
+// TODO: Undo/redo system
 
 void MessageCallback(GLenum source,
                      GLenum type,
@@ -84,8 +83,6 @@ int main(int argc, char **argv)
     }
 
     FT_UInt pixel_size = FREE_GLYPH_FONT_SIZE;
-    // TODO: FT_Set_Pixel_Sizes does not produce good looking results
-    // We need to use something like FT_Set_Char_Size and properly set the device resolution
     error = FT_Set_Pixel_Sizes(face, 0, pixel_size);
     if (error) {
         fprintf(stderr, "ERROR: Could not set pixel size to %u\n", pixel_size);
@@ -235,6 +232,26 @@ int main(int argc, char **argv)
                     }
                 } else {
                     switch (event.key.keysym.sym) {
+                    case SDLK_HOME: {
+                        editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
+                        if (event.key.keysym.mod & KMOD_CTRL) {
+                            editor_move_to_begin(&editor);
+                        } else {
+                            editor_move_to_line_begin(&editor);
+                        }
+                        editor.last_stroke = SDL_GetTicks();
+                    } break;
+
+                    case SDLK_END: {
+                        editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
+                        if (event.key.keysym.mod & KMOD_CTRL) {
+                            editor_move_to_end(&editor);
+                        } else {
+                            editor_move_to_line_end(&editor);
+                        }
+                        editor.last_stroke = SDL_GetTicks();
+                    } break;
+
                     case SDLK_BACKSPACE: {
                         editor_backspace(&editor);
                         editor.last_stroke = SDL_GetTicks();
@@ -284,11 +301,14 @@ int main(int argc, char **argv)
                         if (event.key.keysym.mod & KMOD_CTRL) {
                             editor_start_search(&editor);
                         }
-                    } break;
+                    }
+                    break;
 
                     case SDLK_ESCAPE: {
                         editor_stop_search(&editor);
-                    } break;
+                        editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
+                    }
+                    break;
 
                     case SDLK_a: {
                         if (event.key.keysym.mod & KMOD_CTRL) {
@@ -329,14 +349,22 @@ int main(int argc, char **argv)
 
                     case SDLK_UP: {
                         editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
-                        editor_move_line_up(&editor);
+                        if (event.key.keysym.mod & KMOD_CTRL) {
+                            editor_move_paragraph_up(&editor);
+                        } else {
+                            editor_move_line_up(&editor);
+                        }
                         editor.last_stroke = SDL_GetTicks();
                     }
                     break;
 
                     case SDLK_DOWN: {
                         editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
-                        editor_move_line_down(&editor);
+                        if (event.key.keysym.mod & KMOD_CTRL) {
+                            editor_move_paragraph_down(&editor);
+                        } else {
+                            editor_move_line_down(&editor);
+                        }
                         editor.last_stroke = SDL_GetTicks();
                     }
                     break;
@@ -415,5 +443,4 @@ int main(int argc, char **argv)
 
 // TODO: ability to search within file browser
 // Very useful when you have a lot of files
-// TODO: ability to search with the text editor
 // TODO: ability to remove trailing whitespaces