Browse Source

Add support for clipboard

rexim 2 years ago
parent
commit
0498f885b8
3 changed files with 60 additions and 6 deletions
  1. 43 6
      src/editor.c
  2. 5 0
      src/editor.h
  3. 12 0
      src/main.c

+ 43 - 6
src/editor.c

@@ -37,6 +37,7 @@ void editor_delete(Editor *e)
 
 
 Errno editor_save_as(Editor *e, const char *file_path)
 Errno editor_save_as(Editor *e, const char *file_path)
 {
 {
+    printf("Saving as %s...\n", file_path);
     Errno err = write_entire_file(file_path, e->data.items, e->data.count);
     Errno err = write_entire_file(file_path, e->data.items, e->data.count);
     if (err != 0) return err;
     if (err != 0) return err;
     e->file_path.count = 0;
     e->file_path.count = 0;
@@ -48,6 +49,7 @@ Errno editor_save_as(Editor *e, const char *file_path)
 Errno editor_save(const Editor *e)
 Errno editor_save(const Editor *e)
 {
 {
     assert(e->file_path.count > 0);
     assert(e->file_path.count > 0);
+    printf("Saving as %s...\n", e->file_path.items);
     return write_entire_file(e->file_path.items, e->data.items, e->data.count);
     return write_entire_file(e->file_path.items, e->data.items, e->data.count);
 }
 }
 
 
@@ -117,20 +119,26 @@ void editor_move_char_right(Editor *e)
 }
 }
 
 
 void editor_insert_char(Editor *e, char x)
 void editor_insert_char(Editor *e, char x)
+{
+    editor_insert_buf(e, &x, 1);
+}
+
+void editor_insert_buf(Editor *e, char *buf, size_t buf_len)
 {
 {
     if (e->cursor > e->data.count) {
     if (e->cursor > e->data.count) {
         e->cursor = e->data.count;
         e->cursor = e->data.count;
     }
     }
 
 
-    da_append(&e->data, '\0');
+    for (size_t i = 0; i < buf_len; ++i) {
+        da_append(&e->data, '\0');
+    }
     memmove(
     memmove(
-        &e->data.items[e->cursor + 1],
+        &e->data.items[e->cursor + buf_len],
         &e->data.items[e->cursor],
         &e->data.items[e->cursor],
-        e->data.count - e->cursor - 1
+        e->data.count - e->cursor - buf_len
     );
     );
-    e->data.items[e->cursor] = x;
-    e->cursor += 1;
-
+    memcpy(&e->data.items[e->cursor], buf, buf_len);
+    e->cursor += buf_len;
     editor_retokenize(e);
     editor_retokenize(e);
 }
 }
 
 
@@ -350,3 +358,32 @@ void editor_update_selection(Editor *e, bool shift)
         }
         }
     }
     }
 }
 }
+
+void editor_clipboard_copy(Editor *e)
+{
+    if (e->selection) {
+        size_t begin = e->select_begin;
+        size_t end = e->cursor;
+        if (begin > end) SWAP(size_t, begin, end);
+
+        e->clipboard.count = 0;
+        sb_append_buf(&e->clipboard, &e->data.items[begin], end - begin + 1);
+        sb_append_null(&e->clipboard);
+
+        if (SDL_SetClipboardText(e->clipboard.items) < 0) {
+            fprintf(stderr, "ERROR: SDL ERROR: %s\n", SDL_GetError());
+        }
+    }
+}
+
+void editor_clipboard_paste(Editor *e)
+{
+    char *text = SDL_GetClipboardText();
+    size_t text_len = strlen(text);
+    if (text_len > 0) {
+        editor_insert_buf(e, text, text_len);
+    } else {
+        fprintf(stderr, "ERROR: SDL ERROR: %s\n", SDL_GetError());
+    }
+    SDL_free(text);
+}

+ 5 - 0
src/editor.h

@@ -39,6 +39,8 @@ typedef struct {
     size_t cursor;
     size_t cursor;
 
 
     Uint32 last_stroke;
     Uint32 last_stroke;
+
+    String_Builder clipboard;
 } Editor;
 } Editor;
 
 
 Errno editor_save_as(Editor *editor, const char *file_path);
 Errno editor_save_as(Editor *editor, const char *file_path);
@@ -53,8 +55,11 @@ void editor_move_line_down(Editor *e);
 void editor_move_char_left(Editor *e);
 void editor_move_char_left(Editor *e);
 void editor_move_char_right(Editor *e);
 void editor_move_char_right(Editor *e);
 void editor_insert_char(Editor *e, char x);
 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);
 void editor_retokenize(Editor *e);
 void editor_render(SDL_Window *window, Free_Glyph_Atlas *atlas, Simple_Renderer *sr, Editor *editor);
 void editor_render(SDL_Window *window, Free_Glyph_Atlas *atlas, Simple_Renderer *sr, Editor *editor);
 void editor_update_selection(Editor *e, bool shift);
 void editor_update_selection(Editor *e, bool shift);
+void editor_clipboard_copy(Editor *e);
+void editor_clipboard_paste(Editor *e);
 
 
 #endif // EDITOR_H_
 #endif // EDITOR_H_

+ 12 - 0
src/main.c

@@ -285,6 +285,18 @@ int main(int argc, char **argv)
                     }
                     }
                     break;
                     break;
 
 
+                    case SDLK_c: {
+                        if (event.key.keysym.mod & KMOD_CTRL) {
+                            editor_clipboard_copy(&editor);
+                        }
+                    } break;
+
+                    case SDLK_v: {
+                        if (event.key.keysym.mod & KMOD_CTRL) {
+                            editor_clipboard_paste(&editor);
+                        }
+                    } break;
+
                     case SDLK_UP: {
                     case SDLK_UP: {
                         editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
                         editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
                         editor_move_line_up(&editor);
                         editor_move_line_up(&editor);