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/* build/*
bin/*
doc/* doc/*
beluga.wiki/*
+10 -11
View File
@@ -3,19 +3,18 @@
#*=----------------------------=*#* #*=----------------------------=*#*
#*------------------------------------*# #*------------------------------------*#
%+----------------------------------------=#* %+----------------------------------------=#*
#+---------------------------------------------## #+---------------------------------------------##
*#-------------------------------------------------=## *#-------------------------------------------------=##
*#----------------------------------------------------:-## *#----------------------------------------------------:-##
#----------------------------------------------------------## #----------------------------------------------------------##
#=-------------------------------------------------------------## #=--------------------------------------------------------------##
#----------------------------==----------------------------------=#*
+--------------------------+@#-%*-----------------------------------#* +--------------------------+@#-%*-----------------------------------#*
+--------------------------%@@@@#-------------------------------------** +--------------------------%@@@@#-------------------------------------** 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) (define QUIT-TIMES 1)
(map-key "CTRL-q" editor-quit) ;; FUNCTIONS
(map-key "CTRL-s" editor-save)
(define editor-delete-next-char (lambda () ( (define editor-delete-next-char (lambda () (
(move-cursor "right") (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-UP" '(move-cursor "up"))
(map-key "ARROW-DOWN" '(move-cursor "down")) (map-key "ARROW-DOWN" '(move-cursor "down"))
(map-key "ARROW-RIGHT" '(move-cursor "right")) (map-key "ARROW-RIGHT" '(move-cursor "right"))
@@ -23,7 +28,7 @@
(map-key "DEL" editor-delete-next-char) (map-key "DEL" editor-delete-next-char)
(map-key "PAGE-UP" move-cursor-page-up) (map-key "PAGE-UP" move-cursor-page-up)
(map-key "PAGE-DOWN" move-cursor-page-down) (map-key "PAGE-DOWN" move-cursor-page-down)
(map-key "CTRL-o" editor-open-file)
; Key binds
+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 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 #endif
+6
View File
@@ -20,6 +20,11 @@ typedef struct erow {
char *render; /**< The actual line we will print */ char *render; /**< The actual line we will print */
} erow; } erow;
enum editorStatus_e {
IDLE,
READ_ONLY,
READ_AND_WRITE,
};
struct const_t { struct const_t {
int TAB_LENGTH; int TAB_LENGTH;
@@ -47,6 +52,7 @@ struct editorConfig {
erow *row; /**< Store all the rows printed */ erow *row; /**< Store all the rows printed */
int dirty; int dirty;
char *filename; char *filename;
enum editorStatus_e state;
char status_msg[80]; char status_msg[80];
time_t status_msg_time; time_t status_msg_time;
struct termios orig_termios; /**< Terminal communication interface */ struct termios orig_termios; /**< Terminal communication interface */
+3
View File
@@ -10,6 +10,9 @@
char *editorRowsToString(int *buffer_len); char *editorRowsToString(int *buffer_len);
void editorCloseFile(void);
void editorOpen(char *filename); void editorOpen(char *filename);
void editorSave(); void editorSave();
+3
View File
@@ -24,7 +24,10 @@ int main(int argc, char *argv[]) {
enableRawMode(); enableRawMode();
initEditor(); initEditor();
if (argc >= 2) { if (argc >= 2) {
E.state = READ_AND_WRITE;
editorOpen(argv[1]); editorOpen(argv[1]);
} else {
editorOpen("assets/beluga.txt");
} }
editorSetStatusMessage("HELP: Ctrl-S = save | Ctrl-Q = quit"); 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(); 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/editor_op.h"
#include "../include/row_op.h" #include "../include/row_op.h"
#include <stdio.h> #include "data.h"
extern struct editorConfig E; extern struct editorConfig E;
void editorInsertChar(int c) { void editorInsertChar(int c) {
if (E.cursor_y == E.numrows) { if (E.cursor_y == E.numrows) {
editorInsertRow(E.numrows, "", 0); editorInsertRow(E.numrows, "", 0);
} }
editorRowInsertChar(&E.row[E.cursor_y], E.cursor_x, c); editorRowInsertChar(&E.row[E.cursor_y], E.cursor_x, c);
E.cursor_x++; E.cursor_x++;
} }
void editorInsertNewLine() { void editorInsertNewLine() {
+21
View File
@@ -1,6 +1,7 @@
#include "../include/file_io.h" #include "../include/file_io.h"
#include "../include/input.h" #include "../include/input.h"
#include "../include/output.h" #include "../include/output.h"
#include "data.h"
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -36,9 +37,29 @@ char *editorRowsToString(int *buffer_len) {
return buf; 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) { void editorOpen(char *filename) {
FILE *fp; FILE *fp;
// Test if a file is already open
if (E.filename != NULL) {
editorCloseFile();
}
free(E.filename); free(E.filename);
E.filename = strdup(filename); E.filename = strdup(filename);
+3
View File
@@ -29,6 +29,8 @@ void initBuiltins() {
registerBuiltin("EDITOR-DELETE-PREVIOUS-CHAR", deletePreviousChar); registerBuiltin("EDITOR-DELETE-PREVIOUS-CHAR", deletePreviousChar);
registerBuiltin("MOVE-CURSOR-PAGE-UP", editorMoveCursorPageUp); registerBuiltin("MOVE-CURSOR-PAGE-UP", editorMoveCursorPageUp);
registerBuiltin("MOVE-CURSOR-PAGE-DOWN", editorMoveCursorPageDown); registerBuiltin("MOVE-CURSOR-PAGE-DOWN", editorMoveCursorPageDown);
registerBuiltin("EDITOR-OPEN-FILE", editorOpenFile);
registerBuiltin("EDITOR-INSERT-CHAR", editorPrintC);
} }
void initEditor() { void initEditor() {
@@ -41,6 +43,7 @@ void initEditor() {
E.row = NULL; E.row = NULL;
E.dirty = 0; E.dirty = 0;
E.filename = NULL; E.filename = NULL;
E.state = READ_ONLY;
E.status_msg[0] = '\0'; E.status_msg[0] = '\0';
E.status_msg_time = 0; E.status_msg_time = 0;
if (getWindowSize(&E.screenrows, &E.screencols) == -1) { 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 */ * \param at Index of where we want to insert the char */
void editorRowInsertChar(erow *row, int at, int c) { void editorRowInsertChar(erow *row, int at, int c) {
if (E.state == READ_ONLY)
return;
if (at < 0 || at > row->size) { if (at < 0 || at > row->size) {
at = row->size; at = row->size;
} }