From 9348ae668a9db4cd8ae3a1e75000365f1b4b9fc9 Mon Sep 17 00:00:00 2001 From: Arthur Barraux Date: Sun, 5 Oct 2025 21:44:58 +0200 Subject: [PATCH] Add open file key-bind --- .gitignore | 4 ++-- assets/beluga.txt | 25 ++++++++++++------------- config/init.lisp | 15 ++++++++++----- include/builtins.h | 6 +++++- include/data.h | 6 ++++++ include/file_io.h | 3 +++ main.c | 3 +++ src/builtins.c | 13 +++++++++++++ src/editor_op.c | 13 +++++++------ src/file_io.c | 23 ++++++++++++++++++++++- src/init.c | 3 +++ src/row_op.c | 2 ++ 12 files changed, 88 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 5d7c2b0..3b2fbce 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -tmp/* -bin/* +build/* doc/* +beluga.wiki/* diff --git a/assets/beluga.txt b/assets/beluga.txt index 28fe0e0..8aee859 100644 --- a/assets/beluga.txt +++ b/assets/beluga.txt @@ -1,21 +1,20 @@ - **#%#*****###%** + **#%#*****###%** *##+--------------------=##* #*=----------------------------=*#* #*------------------------------------*# %+----------------------------------------=#* - #+---------------------------------------------## - *#-------------------------------------------------=## - *#----------------------------------------------------:-## - #----------------------------------------------------------## - #=-------------------------------------------------------------## - #----------------------------==----------------------------------=#* + #+---------------------------------------------## + *#-------------------------------------------------=## + *#----------------------------------------------------:-## + #----------------------------------------------------------## + #=--------------------------------------------------------------## +--------------------------+@#-%*-----------------------------------#* - +--------------------------%@@@@#-------------------------------------** - *-=-------------------------#@@*---------------------------------------=% - %*#==--------------------------------------------------------------------+# - *%%=-=--------------------------------------------------------------------=# - %=--------------------------------------------------------------------------#* - %-----------------------------------------------------------------------------** + +--------------------------%@@@@#-------------------------------------** BELUGA - VERSION 1.1 + *-=-------------------------#@@*---------------------------------------=% + %*#==--------------------------------------------------------------------+# ----- KEY-BINDS ----- + *%%=-=--------------------------------------------------------------------=# CTRL-q leave + %=--------------------------------------------------------------------------#* CTRL-s save + %-----------------------------------------------------------------------------** CTRL-o open-file *+--=---===----=---------------=*-----------------------------------------------** #--=## *#%#*+==----==+**+----------------------= ***=---------------------% *%**=-----------==== ==---------------------------------=+#+-----------------=# diff --git a/config/init.lisp b/config/init.lisp index 08704c4..3f11aa5 100644 --- a/config/init.lisp +++ b/config/init.lisp @@ -1,8 +1,9 @@ -(define TAB-LENGTH 2) +;; MACROS + +(define TAB-LENGTH 4) (define QUIT-TIMES 1) -(map-key "CTRL-q" editor-quit) -(map-key "CTRL-s" editor-save) +;; FUNCTIONS (define editor-delete-next-char (lambda () ( (move-cursor "right") @@ -12,6 +13,10 @@ ) +;; KEY MAPPING + +(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")) @@ -23,7 +28,7 @@ (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 +(map-key "CTRL-o" editor-open-file) + diff --git a/include/builtins.h b/include/builtins.h index 44d26a2..0bae402 100644 --- a/include/builtins.h +++ b/include/builtins.h @@ -23,6 +23,10 @@ Lisp deletePreviousChar(Lisp args, LispError *e, LispContext ctx); Lisp editorMoveCursorPageUp(Lisp args, LispError* e, LispContext ctx); -Lisp editorMoveCursorPageDown(Lisp args, LispError* e, LispContext ctx); +Lisp editorMoveCursorPageDown(Lisp args, LispError *e, LispContext ctx); + +Lisp editorOpenFile(Lisp args, LispError *e, LispContext ctx); + +Lisp editorPrintC(Lisp args, LispError *e, LispContext ctx); #endif diff --git a/include/data.h b/include/data.h index 1d2e824..d3e7c6a 100644 --- a/include/data.h +++ b/include/data.h @@ -20,6 +20,11 @@ typedef struct erow { char *render; /**< The actual line we will print */ } erow; +enum editorStatus_e { + IDLE, + READ_ONLY, + READ_AND_WRITE, +}; struct const_t { int TAB_LENGTH; @@ -47,6 +52,7 @@ struct editorConfig { erow *row; /**< Store all the rows printed */ int dirty; char *filename; + enum editorStatus_e state; char status_msg[80]; time_t status_msg_time; struct termios orig_termios; /**< Terminal communication interface */ diff --git a/include/file_io.h b/include/file_io.h index f93154d..771ef08 100644 --- a/include/file_io.h +++ b/include/file_io.h @@ -10,6 +10,9 @@ char *editorRowsToString(int *buffer_len); + +void editorCloseFile(void); + void editorOpen(char *filename); void editorSave(); diff --git a/main.c b/main.c index b88fba8..922533c 100644 --- a/main.c +++ b/main.c @@ -24,7 +24,10 @@ int main(int argc, char *argv[]) { enableRawMode(); initEditor(); if (argc >= 2) { + E.state = READ_AND_WRITE; editorOpen(argv[1]); + } else { + editorOpen("assets/beluga.txt"); } editorSetStatusMessage("HELP: Ctrl-S = save | Ctrl-Q = quit"); diff --git a/src/builtins.c b/src/builtins.c index 48fcb1f..8ff9d7c 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -124,5 +124,18 @@ Lisp editorMoveCursorPageDown(Lisp args, LispError* e, LispContext ctx) { return lisp_null(); } +Lisp editorOpenFile(Lisp args, LispError *e, LispContext ctx) { + char *filename = editorPrompt("Path : %s"); + if (filename) + editorOpen(filename); + + return lisp_null(); +} +Lisp editorPrintC(Lisp args, LispError *e, LispContext ctx) { + char c = lisp_char(lisp_car(args)); + editorInsertChar(c); + return lisp_null(); +} + diff --git a/src/editor_op.c b/src/editor_op.c index 91948e5..4d261d9 100644 --- a/src/editor_op.c +++ b/src/editor_op.c @@ -1,15 +1,16 @@ #include "../include/editor_op.h" #include "../include/row_op.h" -#include +#include "data.h" + extern struct editorConfig E; void editorInsertChar(int c) { - if (E.cursor_y == E.numrows) { - editorInsertRow(E.numrows, "", 0); - } - editorRowInsertChar(&E.row[E.cursor_y], E.cursor_x, c); - E.cursor_x++; + if (E.cursor_y == E.numrows) { + editorInsertRow(E.numrows, "", 0); + } + editorRowInsertChar(&E.row[E.cursor_y], E.cursor_x, c); + E.cursor_x++; } void editorInsertNewLine() { diff --git a/src/file_io.c b/src/file_io.c index 6a141ae..fc9b64f 100644 --- a/src/file_io.c +++ b/src/file_io.c @@ -1,6 +1,7 @@ #include "../include/file_io.h" #include "../include/input.h" #include "../include/output.h" +#include "data.h" #include #include #include @@ -36,16 +37,36 @@ char *editorRowsToString(int *buffer_len) { return buf; } +void editorCloseFile(void) { + E.cursor_x = 0; + E.cursor_y = 0; + E.rx = 0; + E.row_offset = 0; + E.col_offset = 0; + E.numrows = 0; + E.row = NULL; + E.dirty = 0; + E.filename = NULL; + E.status_msg[0] = '\0'; + E.status_msg_time = 0; + +} + void editorOpen(char *filename) { FILE *fp; + // Test if a file is already open + if (E.filename != NULL) { + editorCloseFile(); + } + free(E.filename); E.filename = strdup(filename); fp = fopen(filename, "a+"); if (!fp) die("fopen"); - + char *line = NULL; size_t line_cap = 0; ssize_t line_len; diff --git a/src/init.c b/src/init.c index 855cc5b..6119254 100644 --- a/src/init.c +++ b/src/init.c @@ -29,6 +29,8 @@ void initBuiltins() { registerBuiltin("EDITOR-DELETE-PREVIOUS-CHAR", deletePreviousChar); registerBuiltin("MOVE-CURSOR-PAGE-UP", editorMoveCursorPageUp); registerBuiltin("MOVE-CURSOR-PAGE-DOWN", editorMoveCursorPageDown); + registerBuiltin("EDITOR-OPEN-FILE", editorOpenFile); + registerBuiltin("EDITOR-INSERT-CHAR", editorPrintC); } void initEditor() { @@ -41,6 +43,7 @@ void initEditor() { E.row = NULL; E.dirty = 0; E.filename = NULL; + E.state = READ_ONLY; E.status_msg[0] = '\0'; E.status_msg_time = 0; if (getWindowSize(&E.screenrows, &E.screencols) == -1) { diff --git a/src/row_op.c b/src/row_op.c index 64515c5..817abc9 100644 --- a/src/row_op.c +++ b/src/row_op.c @@ -102,6 +102,8 @@ void editorDelRow(int at) { * \param at Index of where we want to insert the char */ void editorRowInsertChar(erow *row, int at, int c) { + if (E.state == READ_ONLY) + return; if (at < 0 || at > row->size) { at = row->size; }