Add open file key-bind
Build and Deploy Docs / build (push) Failing after 40s

This commit is contained in:
Arthur Barraux
2025-10-05 21:44:58 +02:00
parent 9157b94398
commit 9348ae668a
12 changed files with 88 additions and 28 deletions
+2 -2
View File
@@ -1,3 +1,3 @@
tmp/*
bin/*
build/*
doc/*
beluga.wiki/*
+12 -13
View File
@@ -1,21 +1,20 @@
**#%#*****###%**
**#%#*****###%**
*##+--------------------=##*
#*=----------------------------=*#*
#*------------------------------------*#
%+----------------------------------------=#*
#+---------------------------------------------##
*#-------------------------------------------------=##
*#----------------------------------------------------:-##
#----------------------------------------------------------##
#=-------------------------------------------------------------##
#----------------------------==----------------------------------=#*
#+---------------------------------------------##
*#-------------------------------------------------=##
*#----------------------------------------------------:-##
#----------------------------------------------------------##
#=--------------------------------------------------------------##
+--------------------------+@#-%*-----------------------------------#*
+--------------------------%@@@@#-------------------------------------**
*-=-------------------------#@@*---------------------------------------=%
%*#==--------------------------------------------------------------------+#
*%%=-=--------------------------------------------------------------------=#
%=--------------------------------------------------------------------------#*
%-----------------------------------------------------------------------------**
+--------------------------%@@@@#-------------------------------------** BELUGA - VERSION 1.1
*-=-------------------------#@@*---------------------------------------=%
%*#==--------------------------------------------------------------------+# ----- KEY-BINDS -----
*%%=-=--------------------------------------------------------------------=# CTRL-q leave
%=--------------------------------------------------------------------------#* CTRL-s save
%-----------------------------------------------------------------------------** CTRL-o open-file
*+--=---===----=---------------=*-----------------------------------------------**
#--=## *#%#*+==----==+**+----------------------= ***=---------------------%
*%**=-----------==== ==---------------------------------=+#+-----------------=#
+10 -5
View File
@@ -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)
+5 -1
View File
@@ -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
+6
View File
@@ -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 */
+3
View File
@@ -10,6 +10,9 @@
char *editorRowsToString(int *buffer_len);
void editorCloseFile(void);
void editorOpen(char *filename);
void editorSave();
+3
View File
@@ -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");
+13
View File
@@ -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();
}
+7 -6
View File
@@ -1,15 +1,16 @@
#include "../include/editor_op.h"
#include "../include/row_op.h"
#include <stdio.h>
#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() {
+22 -1
View File
@@ -1,6 +1,7 @@
#include "../include/file_io.h"
#include "../include/input.h"
#include "../include/output.h"
#include "data.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
@@ -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;
+3
View File
@@ -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) {
+2
View File
@@ -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;
}