From 9157b94398291ac4f4a368fb1097f0f2f0deb8b0 Mon Sep 17 00:00:00 2001 From: Arthur Barraux Date: Thu, 2 Oct 2025 14:59:28 +0200 Subject: [PATCH] Adding usefull keybinds --- config/init.el | 13 ------------- config/init.lisp | 29 +++++++++++++++++++++++++++++ include/builtins.h | 8 +++++++- src/builtins.c | 37 +++++++++++++++++++++++++++++++++++-- src/init.c | 7 ++++--- src/input.c | 4 +++- 6 files changed, 78 insertions(+), 20 deletions(-) delete mode 100644 config/init.el create mode 100644 config/init.lisp diff --git a/config/init.el b/config/init.el deleted file mode 100644 index 20546f6..0000000 --- a/config/init.el +++ /dev/null @@ -1,13 +0,0 @@ -(define TAB-LENGTH 2) -(define QUIT-TIMES 1) - -(map-key "CTRL-q" editor-quit) -(map-key "CTRL-s" editor-save) - -(map-key "ARROW-UP" '(move-cursor "up")) -(map-key "ARROW-DOWN" '(move-cursor "down")) -(map-key "ARROW-RIGHT" '(move-cursor "right")) -(map-key "ARROW-LEFT" '(move-cursor "left")) -(map-key "ENTER" editor-insert-new-line) -(map-key "CTRL-a" move-cursor-beg-line) -(map-key "CTRL-e" move-cursor-end-line) diff --git a/config/init.lisp b/config/init.lisp new file mode 100644 index 0000000..08704c4 --- /dev/null +++ b/config/init.lisp @@ -0,0 +1,29 @@ +(define TAB-LENGTH 2) +(define QUIT-TIMES 1) + +(map-key "CTRL-q" editor-quit) +(map-key "CTRL-s" editor-save) + +(define editor-delete-next-char (lambda () ( + (move-cursor "right") + (editor-delete-previous-char) + ) + ) + ) + + +(map-key "ARROW-UP" '(move-cursor "up")) +(map-key "ARROW-DOWN" '(move-cursor "down")) +(map-key "ARROW-RIGHT" '(move-cursor "right")) +(map-key "ARROW-LEFT" '(move-cursor "left")) +(map-key "ENTER" editor-insert-new-line) +(map-key "CTRL-a" move-cursor-beg-line) +(map-key "CTRL-e" move-cursor-end-line) +(map-key "BACKSPACE" editor-delete-previous-char) +(map-key "DEL" editor-delete-next-char) +(map-key "PAGE-UP" move-cursor-page-up) +(map-key "PAGE-DOWN" move-cursor-page-down) + +; Key binds + + diff --git a/include/builtins.h b/include/builtins.h index d5ba0f1..44d26a2 100644 --- a/include/builtins.h +++ b/include/builtins.h @@ -17,6 +17,12 @@ Lisp l_editorInsertNewLine(Lisp args, LispError* e, LispContext ctx); Lisp moveCursorBeginLine(Lisp args, LispError *e, LispContext ctx); -Lisp moveCursorEndLine(Lisp args, LispError* e, LispContext ctx); +Lisp moveCursorEndLine(Lisp args, LispError *e, LispContext ctx); + +Lisp deletePreviousChar(Lisp args, LispError *e, LispContext ctx); + +Lisp editorMoveCursorPageUp(Lisp args, LispError* e, LispContext ctx); + +Lisp editorMoveCursorPageDown(Lisp args, LispError* e, LispContext ctx); #endif diff --git a/src/builtins.c b/src/builtins.c index a5b5d53..48fcb1f 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -5,11 +5,12 @@ #include "../include/editor_op.h" #include "../include/data.h" +#include #include #include Lisp mapKey(Lisp args, LispError *e, LispContext ctx) { - const char* key_sequence = lisp_string(lisp_car(args)); + const char *key_sequence = lisp_string(lisp_car(args)); args = lisp_cdr(args); // second argument Lisp func = lisp_car(args); @@ -25,7 +26,8 @@ Lisp mapKey(Lisp args, LispError *e, LispContext ctx) { return lisp_null(); } -Lisp moveCursor(Lisp args, LispError* e, LispContext ctx) { +Lisp moveCursor(Lisp args, LispError *e, LispContext ctx) { + fprintf(stderr, "Cursor is moving\n"); const char *direction = lisp_string(lisp_car(args)); switch (direction[0]) { case 'u': @@ -93,3 +95,34 @@ Lisp moveCursorEndLine(Lisp args, LispError* e, LispContext ctx) { } return lisp_null(); } + + +Lisp deletePreviousChar(Lisp args, LispError* e, LispContext ctx) { + editorDelChar(); + return lisp_null(); +} + +Lisp editorMoveCursorPageUp(Lisp args, LispError* e, LispContext ctx) { + E.cursor_y = E.row_offset; + int times = E.screenrows; + while (--times) { + editorMoveCursor(ARROW_UP); + } + return lisp_null(); +} + +Lisp editorMoveCursorPageDown(Lisp args, LispError* e, LispContext ctx) { + E.cursor_y = E.row_offset + E.screenrows - 1; + if (E.cursor_y > E.numrows) { + E.cursor_y = E.numrows; + } + int times = E.screenrows; + while (--times) { + editorMoveCursor(ARROW_DOWN); + } + + return lisp_null(); +} + + + diff --git a/src/init.c b/src/init.c index 46f0e30..855cc5b 100644 --- a/src/init.c +++ b/src/init.c @@ -26,8 +26,9 @@ void initBuiltins() { registerBuiltin("EDITOR-INSERT-NEW-LINE", l_editorInsertNewLine); registerBuiltin("MOVE-CURSOR-BEG-LINE", moveCursorBeginLine); registerBuiltin("MOVE-CURSOR-END-LINE", moveCursorEndLine); - - + registerBuiltin("EDITOR-DELETE-PREVIOUS-CHAR", deletePreviousChar); + registerBuiltin("MOVE-CURSOR-PAGE-UP", editorMoveCursorPageUp); + registerBuiltin("MOVE-CURSOR-PAGE-DOWN", editorMoveCursorPageDown); } void initEditor() { @@ -50,7 +51,7 @@ void initEditor() { E.number_of_keybinds = 0; - E.fd_init_file = fopen("config/init.el", "r"); + E.fd_init_file = fopen("config/init.lisp", "r"); E.ctx = lisp_init(); E.env = lisp_env(E.ctx); lisp_lib_load(E.ctx); diff --git a/src/input.c b/src/input.c index ed7e215..dcb579e 100644 --- a/src/input.c +++ b/src/input.c @@ -2,7 +2,6 @@ #include "../include/editor_op.h" #include "../include/output.h" #include "../include/define.h" -#include "data.h" #include #include #include @@ -80,12 +79,15 @@ char *key_to_string(int key) { break; case PAGE_UP: strcpy(key_str, "PAGE-UP"); + fprintf(stderr, "pagr up\n"); break; case PAGE_DOWN: strcpy(key_str, "PAGE-DOWN"); break; case DEL_KEY: + fprintf(stderr, "delete key\n"); strcpy(key_str, "DEL"); + break; case BACKSPACE: strcpy(key_str, "BACKSPACE");