Made Editor Config global

This commit is contained in:
Arthur Barraux
2025-09-19 14:31:12 +02:00
parent 91e247d1de
commit 8ce621dfde
17 changed files with 235 additions and 215 deletions
+45 -44
View File
@@ -8,11 +8,13 @@
#include <stdlib.h>
#include <unistd.h>
extern struct editorConfig E;
/**
* \fn char * editorPrompt(struct editorConfig *E, char *prompt)
* \brief Return user input in a prompt when enter is hit. */
char *editorPrompt(struct editorConfig *E, char *prompt) {
char *editorPrompt(char *prompt) {
size_t buf_size = 128;
char *buf = malloc(buf_size);
size_t buf_len = 0;
@@ -20,8 +22,8 @@ char *editorPrompt(struct editorConfig *E, char *prompt) {
buf[0] = '\0';
while (1) {
editorSetStatusMessage(E, prompt, buf);
editorRefreshScreen(E);
editorSetStatusMessage(prompt, buf);
editorRefreshScreen();
c = editorReadKey();
if (c == DEL_KEY || c == CTRL_KEY('h') || c == BACKSPACE) {
if (buf_len != 0) {
@@ -29,12 +31,12 @@ char *editorPrompt(struct editorConfig *E, char *prompt) {
}
} else if (c == ESCAPE) {
fprintf(stderr, "escape");
editorSetStatusMessage(E, "");
editorSetStatusMessage("");
free(buf);
return NULL;
} else if (c == '\r') {
if (buf_len != 0) {
editorSetStatusMessage(E, "");
editorSetStatusMessage("");
return buf;
}
} else if (!iscntrl(c) && c < 128) {
@@ -48,46 +50,46 @@ char *editorPrompt(struct editorConfig *E, char *prompt) {
}
}
void editorMoveCursor(struct editorConfig *E, int key) {
erow *row = (E->cursor_y >= E->numrows) ? NULL : &E->row[E->cursor_y];
void editorMoveCursor(int key) {
erow *row = (E.cursor_y >= E.numrows) ? NULL : &E.row[E.cursor_y];
int row_len;
switch (key) {
case ARROW_RIGHT:
if (row && E->cursor_x < row->size) {
++E->cursor_x;
} else if (row && E->cursor_x == row->size) {
E->cursor_y++;
E->cursor_x = 0;
if (row && E.cursor_x < row->size) {
++E.cursor_x;
} else if (row && E.cursor_x == row->size) {
E.cursor_y++;
E.cursor_x = 0;
}
break;
case ARROW_DOWN:
if (E->cursor_y < E->numrows) {
++E->cursor_y;
if (E.cursor_y < E.numrows) {
++E.cursor_y;
}
break;
case ARROW_UP:
if (E->cursor_y != 0) {
--E->cursor_y;
if (E.cursor_y != 0) {
--E.cursor_y;
}
break;
case ARROW_LEFT:
if (E->cursor_x != 0) {
--E->cursor_x;
} else if (E->cursor_y > 0) {
--E->cursor_y;
E->cursor_x = E->row[E->cursor_y].size;
if (E.cursor_x != 0) {
--E.cursor_x;
} else if (E.cursor_y > 0) {
--E.cursor_y;
E.cursor_x = E.row[E.cursor_y].size;
}
break;
}
row = (E->cursor_y >= E->numrows) ? NULL : &E->row[E->cursor_y];
row = (E.cursor_y >= E.numrows) ? NULL : &E.row[E.cursor_y];
row_len = row ? row->size : 0;
if (E->cursor_x > row_len) {
E->cursor_x = row_len;
if (E.cursor_x > row_len) {
E.cursor_x = row_len;
}
}
void editorProcessKeypress(struct editorConfig *E) {
void editorProcessKeypress() {
static int quit_times = QUIT_TIMES;
int c = editorReadKey();
int times;
@@ -95,33 +97,32 @@ void editorProcessKeypress(struct editorConfig *E) {
switch (c) {
case '\r':
editorInsertNewLine(E);
editorInsertNewLine();
break;
case CTRL_KEY('q'):
if (E->dirty && quit_times > 0) {
editorSetStatusMessage(E,
"WARNING! Changes hasn't been saved. Press Ctrl-Q "
if (E.dirty && quit_times > 0) {
editorSetStatusMessage("WARNING! Changes hasn't been saved. Press Ctrl-Q "
"another time to quit.");
--quit_times;
return;
}
write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, CURSOR_TOP_LEFT, 3);
disableRawMode(E);
disableRawMode();
exit(0);
break;
case CTRL_KEY('s'):
editorSave(E);
editorSave();
break;
case BEG_LINE:
E->cursor_x = 0;
E.cursor_x = 0;
break;
case END_LINE:
if (E->cursor_y < E->numrows) {
E->cursor_x = E->row[E->cursor_y].size;
if (E.cursor_y < E.numrows) {
E.cursor_x = E.row[E.cursor_y].size;
}
break;
@@ -129,24 +130,24 @@ void editorProcessKeypress(struct editorConfig *E) {
case CTRL_KEY('h'):
case DEL_KEY:
if (c == DEL_KEY) {
editorMoveCursor(E, ARROW_RIGHT);
editorMoveCursor(ARROW_RIGHT);
}
editorDelChar(E);
editorDelChar();
break;
case PAGE_UP:
case PAGE_DOWN: {
if (c == PAGE_UP) {
E->cursor_y = E->row_offset;
E.cursor_y = E.row_offset;
} else if (c == PAGE_DOWN) {
E->cursor_y = E->row_offset + E->screenrows - 1;
if (E->cursor_y > E->numrows) {
E->cursor_y = E->numrows;
E.cursor_y = E.row_offset + E.screenrows - 1;
if (E.cursor_y > E.numrows) {
E.cursor_y = E.numrows;
}
}
times = E->screenrows;
times = E.screenrows;
while (--times) {
editorMoveCursor(E, c == PAGE_UP ? ARROW_UP : ARROW_DOWN);
editorMoveCursor(c == PAGE_UP ? ARROW_UP : ARROW_DOWN);
}
} break;
@@ -154,14 +155,14 @@ void editorProcessKeypress(struct editorConfig *E) {
case ARROW_DOWN:
case ARROW_LEFT:
case ARROW_RIGHT:
editorMoveCursor(E, c);
editorMoveCursor(c);
break;
case CTRL_KEY('l'):
case '\x1b':
break;
default:
editorInsertChar(E, c);
editorInsertChar(c);
break;
}
quit_times = QUIT_TIMES;