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
+6
View File
@@ -1,6 +1,7 @@
#ifndef DATA_H_ #ifndef DATA_H_
#define DATA_H_ #define DATA_H_
#include "../lisp-interpreter/dist/lisp.h"
#include <termios.h> #include <termios.h>
#include <time.h> #include <time.h>
@@ -35,6 +36,9 @@ struct editorConfig {
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 */
LispContext ctx; /** Lisp context */
Lisp ctx_data; /** Lisp data context */
LispError ctx_error; /** Lisp ctx error */
}; };
/** /**
@@ -47,4 +51,6 @@ struct abuf {
int len; /**< Length of the text */ int len; /**< Length of the text */
}; };
extern struct editorConfig E;
#endif #endif
+3 -3
View File
@@ -2,10 +2,10 @@
#define EDITOR_OP_H_ #define EDITOR_OP_H_
#include "data.h" #include "data.h"
void editorInsertChar(struct editorConfig *E, int c); void editorInsertChar(int c);
void editorInsertNewLine(struct editorConfig *E); void editorInsertNewLine();
void editorDelChar(struct editorConfig *E); void editorDelChar();
#endif // EDITOR_OP_H_ #endif // EDITOR_OP_H_
+3 -3
View File
@@ -8,10 +8,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
char *editorRowsToString(struct editorConfig *E, int *buffer_len); char *editorRowsToString(int *buffer_len);
void editorOpen(struct editorConfig *E, char *filename); void editorOpen(char *filename);
void editorSave(struct editorConfig *E); void editorSave();
#endif // FILE_IO_H_ #endif // FILE_IO_H_
+1 -1
View File
@@ -10,6 +10,6 @@
* \brief Job's function is to initialize all the fields of editorConfig. * \brief Job's function is to initialize all the fields of editorConfig.
* */ * */
void initEditor(struct editorConfig *E); void initEditor();
#endif // INIT_H_ #endif // INIT_H_
+3 -3
View File
@@ -19,15 +19,15 @@
// END \x1b[4~ || <esc>[8~ || <esc>[F || <esc>OF // END \x1b[4~ || <esc>[8~ || <esc>[F || <esc>OF
// DELETE \x1b[3~ // DELETE \x1b[3~
char *editorPrompt(struct editorConfig *E, char *prompt); char *editorPrompt(char *prompt);
void editorMoveCursor(struct editorConfig *E, int key); void editorMoveCursor(int key);
/** /**
* \fn void editorProcessKeypress() * \fn void editorProcessKeypress()
* \brief Get the last key input and do the proper action. * \brief Get the last key input and do the proper action.
*/ */
void editorProcessKeypress(struct editorConfig *E); void editorProcessKeypress();
#endif // INPUT_H_ #endif // INPUT_H_
+6 -6
View File
@@ -13,16 +13,16 @@
* \brief Draws left rows of the editor. * \brief Draws left rows of the editor.
*/ */
void editorDrawRows(struct editorConfig *E, struct abuf *ab); void editorDrawRows(struct abuf *ab);
void editorRefreshScreen(struct editorConfig *E); void editorRefreshScreen();
void editorScroll(struct editorConfig *E); void editorScroll();
void editorDrawStatusBar(struct editorConfig *E, struct abuf *ab); void editorDrawStatusBar(struct abuf *ab);
void editorDrawMessageBar(struct editorConfig *E, struct abuf *ab); void editorDrawMessageBar(struct abuf *ab);
void editorSetStatusMessage(struct editorConfig *E, const char *fmt, ...); void editorSetStatusMessage(const char *fmt, ...);
#endif // OUTPUT_H_ #endif // OUTPUT_H_
+5 -6
View File
@@ -12,17 +12,16 @@ int editorRowCxToRx(erow *row, int cursor_x);
void editorUpdateRow(erow *row); void editorUpdateRow(erow *row);
void editorInsertRow(struct editorConfig *E, int at, char *s, size_t len); void editorInsertRow(int at, char *s, size_t len);
void editorFreeRow(erow *row); void editorFreeRow(erow *row);
void editorDelRow(struct editorConfig *E, int at); void editorDelRow(int at);
void editorRowInsertChar(struct editorConfig *E, erow *row, int at, int c); void editorRowInsertChar(erow *row, int at, int c);
void editorRowAppendString(struct editorConfig *E, erow *row, char *s, void editorRowAppendString(erow *row, char *s, size_t len);
size_t len);
void editorRowDelchar(struct editorConfig *E, erow *row, int at); void editorRowDelchar(erow *row, int at);
#endif // ROW_OP_H_ #endif // ROW_OP_H_
+2 -2
View File
@@ -21,9 +21,9 @@
void die(const char *s); void die(const char *s);
void disableRawMode(struct editorConfig *E); void disableRawMode();
void enableRawMode(struct editorConfig *E); void enableRawMode();
int editorReadKey(); int editorReadKey();
+8 -8
View File
@@ -16,21 +16,21 @@
#include "include/output.h" #include "include/output.h"
#include "include/terminal.h" #include "include/terminal.h"
struct editorConfig E;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
struct editorConfig E; enableRawMode();
initEditor();
enableRawMode(&E);
initEditor(&E);
if (argc >= 2) { if (argc >= 2) {
editorOpen(&E, argv[1]); editorOpen(argv[1]);
} }
editorSetStatusMessage(&E, "HELP: Ctrl-S = save | Ctrl-Q = quit"); editorSetStatusMessage("HELP: Ctrl-S = save | Ctrl-Q = quit");
while (1) { while (1) {
editorRefreshScreen(&E); editorRefreshScreen();
editorProcessKeypress(&E); editorProcessKeypress();
} }
return 0; return 0;
} }
+2
View File
@@ -1,5 +1,7 @@
#include "../include/append_buffer.h" #include "../include/append_buffer.h"
extern struct editorConfig E;
void abAppend(struct abuf *ab, const char *s, int len) { void abAppend(struct abuf *ab, const char *s, int len) {
char *new = realloc(ab->b, ab->len + len); char *new = realloc(ab->b, ab->len + len);
+27 -25
View File
@@ -1,44 +1,46 @@
#include "../include/editor_op.h" #include "../include/editor_op.h"
#include "../include/row_op.h" #include "../include/row_op.h"
void editorInsertChar(struct editorConfig *E, int c) { extern struct editorConfig E;
if (E->cursor_y == E->numrows) {
editorInsertRow(E, E->numrows, "", 0); void editorInsertChar(int c) {
if (E.cursor_y == E.numrows) {
editorInsertRow(E.numrows, "", 0);
} }
editorRowInsertChar(E, &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(struct editorConfig *E) { void editorInsertNewLine() {
erow *row; erow *row;
if (!E->cursor_x) { if (!E.cursor_x) {
editorInsertRow(E, E->cursor_y, "", 0); editorInsertRow(E.cursor_y, "", 0);
} else { } else {
row = &E->row[E->cursor_y]; row = &E.row[E.cursor_y];
editorInsertRow(E, E->cursor_y + 1, &row->chars[E->cursor_x], editorInsertRow(E.cursor_y + 1, &row->chars[E.cursor_x],
row->size - E->cursor_x); row->size - E.cursor_x);
row = &E->row[E->cursor_y]; row = &E.row[E.cursor_y];
row->size = E->cursor_x; row->size = E.cursor_x;
row->chars[row->size] = '\0'; row->chars[row->size] = '\0';
editorUpdateRow(row); editorUpdateRow(row);
} }
++E->cursor_y; ++E.cursor_y;
E->cursor_x = 0; E.cursor_x = 0;
} }
void editorDelChar(struct editorConfig *E) { void editorDelChar() {
erow *row; erow *row;
if (E->cursor_y == E->numrows || !(E->cursor_x || E->cursor_y)) { if (E.cursor_y == E.numrows || !(E.cursor_x || E.cursor_y)) {
return; return;
} }
row = &E->row[E->cursor_y]; row = &E.row[E.cursor_y];
if (E->cursor_x > 0) { if (E.cursor_x > 0) {
editorRowDelchar(E, row, E->cursor_x - 1); editorRowDelchar(row, E.cursor_x - 1);
--E->cursor_x; --E.cursor_x;
} else { } else {
E->cursor_x = E->row[E->cursor_y - 1].size; E.cursor_x = E.row[E.cursor_y - 1].size;
editorRowAppendString(E, &E->row[E->cursor_y - 1], row->chars, row->size); editorRowAppendString(&E.row[E.cursor_y - 1], row->chars, row->size);
editorDelRow(E, E->cursor_y); editorDelRow(E.cursor_y);
--E->cursor_y; --E.cursor_y;
} }
} }
+22 -21
View File
@@ -12,22 +12,23 @@ extern char *strdup(const char *);
extern ssize_t getline(char **restrict lineptr, size_t *restrict n, extern ssize_t getline(char **restrict lineptr, size_t *restrict n,
FILE *restrict stream); FILE *restrict stream);
extern int ftruncate(int fd, off_t length); extern int ftruncate(int fd, off_t length);
extern struct editorConfig E;
char *editorRowsToString(struct editorConfig *E, int *buffer_len) { char *editorRowsToString(int *buffer_len) {
int tot_len = 0; int tot_len = 0;
int j; int j;
char *buf; char *buf;
char *p; char *p;
for (j = 0; j < E->numrows; ++j) { for (j = 0; j < E.numrows; ++j) {
tot_len += E->row[j].size + 1; tot_len += E.row[j].size + 1;
} }
*buffer_len = tot_len; *buffer_len = tot_len;
buf = malloc(tot_len); buf = malloc(tot_len);
p = buf; p = buf;
for (j = 0; j < E->numrows; ++j) { for (j = 0; j < E.numrows; ++j) {
memcpy(p, E->row[j].chars, E->row[j].size); memcpy(p, E.row[j].chars, E.row[j].size);
p += E->row[j].size; p += E.row[j].size;
*p = '\n'; *p = '\n';
p++; p++;
} }
@@ -35,11 +36,11 @@ char *editorRowsToString(struct editorConfig *E, int *buffer_len) {
return buf; return buf;
} }
void editorOpen(struct editorConfig *E, char *filename) { void editorOpen(char *filename) {
FILE *fp; FILE *fp;
free(E->filename); free(E.filename);
E->filename = strdup(filename); E.filename = strdup(filename);
fp = fopen(filename, "r"); fp = fopen(filename, "r");
if (!fp) if (!fp)
@@ -54,38 +55,38 @@ void editorOpen(struct editorConfig *E, char *filename) {
(line[line_len - 1] == '\n' || line[line_len - 1] == '\r')) { (line[line_len - 1] == '\n' || line[line_len - 1] == '\r')) {
--line_len; --line_len;
} }
editorInsertRow(E, E->numrows, line, line_len); editorInsertRow(E.numrows, line, line_len);
} }
free(line); free(line);
fclose(fp); fclose(fp);
E->dirty = 0; E.dirty = 0;
} }
void editorSave(struct editorConfig *E) { void editorSave() {
int len; int len;
char *buf; char *buf;
int fd; int fd;
if (E->filename == NULL) { if (E.filename == NULL) {
E->filename = editorPrompt(E, "Save as: %s (ESC to cancel)"); E.filename = editorPrompt("Save as: %s (ESC to cancel)");
if (E->filename == NULL) { if (E.filename == NULL) {
editorSetStatusMessage(E, "Save aborted"); editorSetStatusMessage("Save aborted");
return; return;
} }
} }
buf = editorRowsToString(E, &len); buf = editorRowsToString(&len);
fd = open(E->filename, O_RDWR | O_CREAT, 0644); fd = open(E.filename, O_RDWR | O_CREAT, 0644);
if (fd != -1) { if (fd != -1) {
if (ftruncate(fd, len) != -1) { if (ftruncate(fd, len) != -1) {
if (write(fd, buf, len) == len) { if (write(fd, buf, len) == len) {
close(fd); close(fd);
free(buf); free(buf);
E->dirty = 0; E.dirty = 0;
editorSetStatusMessage(E, "%d bytes written to disk", len); editorSetStatusMessage("%d bytes written to disk", len);
return; return;
} }
} }
close(fd); close(fd);
} }
free(buf); free(buf);
editorSetStatusMessage(E, "Can't save! I/O error: %s", strerror(errno)); editorSetStatusMessage("Can't save! I/O error: %s", strerror(errno));
} }
+19 -14
View File
@@ -1,19 +1,24 @@
#include "../include/init.h" #include "../include/init.h"
#include "data.h"
#include <stdio.h>
#include <stdlib.h>
void initEditor(struct editorConfig *E) { extern struct editorConfig;
E->cursor_x = 0;
E->cursor_y = 0; void initEditor() {
E->rx = 0; E.cursor_x = 0;
E->row_offset = 0; E.cursor_y = 0;
E->col_offset = 0; E.rx = 0;
E->numrows = 0; E.row_offset = 0;
E->row = NULL; E.col_offset = 0;
E->dirty = 0; E.numrows = 0;
E->filename = NULL; E.row = NULL;
E->status_msg[0] = '\0'; E.dirty = 0;
E->status_msg_time = 0; E.filename = NULL;
if (getWindowSize(&E->screenrows, &E->screencols) == -1) { E.status_msg[0] = '\0';
E.status_msg_time = 0;
if (getWindowSize(&E.screenrows, &E.screencols) == -1) {
die("getWindowSize"); die("getWindowSize");
} }
E->screenrows -= 2; E.screenrows -= 2;
} }
+45 -44
View File
@@ -8,11 +8,13 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
extern struct editorConfig E;
/** /**
* \fn char * editorPrompt(struct editorConfig *E, char *prompt) * \fn char * editorPrompt(struct editorConfig *E, char *prompt)
* \brief Return user input in a prompt when enter is hit. */ * \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; size_t buf_size = 128;
char *buf = malloc(buf_size); char *buf = malloc(buf_size);
size_t buf_len = 0; size_t buf_len = 0;
@@ -20,8 +22,8 @@ char *editorPrompt(struct editorConfig *E, char *prompt) {
buf[0] = '\0'; buf[0] = '\0';
while (1) { while (1) {
editorSetStatusMessage(E, prompt, buf); editorSetStatusMessage(prompt, buf);
editorRefreshScreen(E); editorRefreshScreen();
c = editorReadKey(); c = editorReadKey();
if (c == DEL_KEY || c == CTRL_KEY('h') || c == BACKSPACE) { if (c == DEL_KEY || c == CTRL_KEY('h') || c == BACKSPACE) {
if (buf_len != 0) { if (buf_len != 0) {
@@ -29,12 +31,12 @@ char *editorPrompt(struct editorConfig *E, char *prompt) {
} }
} else if (c == ESCAPE) { } else if (c == ESCAPE) {
fprintf(stderr, "escape"); fprintf(stderr, "escape");
editorSetStatusMessage(E, ""); editorSetStatusMessage("");
free(buf); free(buf);
return NULL; return NULL;
} else if (c == '\r') { } else if (c == '\r') {
if (buf_len != 0) { if (buf_len != 0) {
editorSetStatusMessage(E, ""); editorSetStatusMessage("");
return buf; return buf;
} }
} else if (!iscntrl(c) && c < 128) { } else if (!iscntrl(c) && c < 128) {
@@ -48,46 +50,46 @@ char *editorPrompt(struct editorConfig *E, char *prompt) {
} }
} }
void editorMoveCursor(struct editorConfig *E, int key) { void editorMoveCursor(int key) {
erow *row = (E->cursor_y >= E->numrows) ? NULL : &E->row[E->cursor_y]; erow *row = (E.cursor_y >= E.numrows) ? NULL : &E.row[E.cursor_y];
int row_len; int row_len;
switch (key) { switch (key) {
case ARROW_RIGHT: case ARROW_RIGHT:
if (row && E->cursor_x < row->size) { if (row && E.cursor_x < row->size) {
++E->cursor_x; ++E.cursor_x;
} else if (row && E->cursor_x == row->size) { } else if (row && E.cursor_x == row->size) {
E->cursor_y++; E.cursor_y++;
E->cursor_x = 0; E.cursor_x = 0;
} }
break; break;
case ARROW_DOWN: case ARROW_DOWN:
if (E->cursor_y < E->numrows) { if (E.cursor_y < E.numrows) {
++E->cursor_y; ++E.cursor_y;
} }
break; break;
case ARROW_UP: case ARROW_UP:
if (E->cursor_y != 0) { if (E.cursor_y != 0) {
--E->cursor_y; --E.cursor_y;
} }
break; break;
case ARROW_LEFT: case ARROW_LEFT:
if (E->cursor_x != 0) { if (E.cursor_x != 0) {
--E->cursor_x; --E.cursor_x;
} else if (E->cursor_y > 0) { } else if (E.cursor_y > 0) {
--E->cursor_y; --E.cursor_y;
E->cursor_x = E->row[E->cursor_y].size; E.cursor_x = E.row[E.cursor_y].size;
} }
break; 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; row_len = row ? row->size : 0;
if (E->cursor_x > row_len) { if (E.cursor_x > row_len) {
E->cursor_x = row_len; E.cursor_x = row_len;
} }
} }
void editorProcessKeypress(struct editorConfig *E) { void editorProcessKeypress() {
static int quit_times = QUIT_TIMES; static int quit_times = QUIT_TIMES;
int c = editorReadKey(); int c = editorReadKey();
int times; int times;
@@ -95,33 +97,32 @@ void editorProcessKeypress(struct editorConfig *E) {
switch (c) { switch (c) {
case '\r': case '\r':
editorInsertNewLine(E); editorInsertNewLine();
break; break;
case CTRL_KEY('q'): case CTRL_KEY('q'):
if (E->dirty && quit_times > 0) { if (E.dirty && quit_times > 0) {
editorSetStatusMessage(E, editorSetStatusMessage("WARNING! Changes hasn't been saved. Press Ctrl-Q "
"WARNING! Changes hasn't been saved. Press Ctrl-Q "
"another time to quit."); "another time to quit.");
--quit_times; --quit_times;
return; return;
} }
write(STDOUT_FILENO, "\x1b[2J", 4); write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, CURSOR_TOP_LEFT, 3); write(STDOUT_FILENO, CURSOR_TOP_LEFT, 3);
disableRawMode(E); disableRawMode();
exit(0); exit(0);
break; break;
case CTRL_KEY('s'): case CTRL_KEY('s'):
editorSave(E); editorSave();
break; break;
case BEG_LINE: case BEG_LINE:
E->cursor_x = 0; E.cursor_x = 0;
break; break;
case END_LINE: case END_LINE:
if (E->cursor_y < E->numrows) { if (E.cursor_y < E.numrows) {
E->cursor_x = E->row[E->cursor_y].size; E.cursor_x = E.row[E.cursor_y].size;
} }
break; break;
@@ -129,24 +130,24 @@ void editorProcessKeypress(struct editorConfig *E) {
case CTRL_KEY('h'): case CTRL_KEY('h'):
case DEL_KEY: case DEL_KEY:
if (c == DEL_KEY) { if (c == DEL_KEY) {
editorMoveCursor(E, ARROW_RIGHT); editorMoveCursor(ARROW_RIGHT);
} }
editorDelChar(E); editorDelChar();
break; break;
case PAGE_UP: case PAGE_UP:
case PAGE_DOWN: { case PAGE_DOWN: {
if (c == PAGE_UP) { if (c == PAGE_UP) {
E->cursor_y = E->row_offset; E.cursor_y = E.row_offset;
} else if (c == PAGE_DOWN) { } else if (c == PAGE_DOWN) {
E->cursor_y = E->row_offset + E->screenrows - 1; E.cursor_y = E.row_offset + E.screenrows - 1;
if (E->cursor_y > E->numrows) { if (E.cursor_y > E.numrows) {
E->cursor_y = E->numrows; E.cursor_y = E.numrows;
} }
} }
times = E->screenrows; times = E.screenrows;
while (--times) { while (--times) {
editorMoveCursor(E, c == PAGE_UP ? ARROW_UP : ARROW_DOWN); editorMoveCursor(c == PAGE_UP ? ARROW_UP : ARROW_DOWN);
} }
} break; } break;
@@ -154,14 +155,14 @@ void editorProcessKeypress(struct editorConfig *E) {
case ARROW_DOWN: case ARROW_DOWN:
case ARROW_LEFT: case ARROW_LEFT:
case ARROW_RIGHT: case ARROW_RIGHT:
editorMoveCursor(E, c); editorMoveCursor(c);
break; break;
case CTRL_KEY('l'): case CTRL_KEY('l'):
case '\x1b': case '\x1b':
break; break;
default: default:
editorInsertChar(E, c); editorInsertChar(c);
break; break;
} }
quit_times = QUIT_TIMES; quit_times = QUIT_TIMES;
+50 -48
View File
@@ -4,24 +4,26 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
void editorDrawRows(struct editorConfig *E, struct abuf *ab) { extern struct editorConfig E;
void editorDrawRows(struct abuf *ab) {
int y; int y;
char welcome[80]; char welcome[80];
int welcome_len; int welcome_len;
int padding; int padding;
int len; int len;
int file_row; int file_row;
for (y = 0; y < E->screenrows; ++y) { for (y = 0; y < E.screenrows; ++y) {
file_row = y + E->row_offset; file_row = y + E.row_offset;
if (file_row >= E->numrows) { if (file_row >= E.numrows) {
if (E->numrows == 0 && y == E->screenrows / 3) { if (E.numrows == 0 && y == E.screenrows / 3) {
welcome_len = welcome_len =
snprintf(welcome, sizeof(welcome), snprintf(welcome, sizeof(welcome),
"Beluga text editor -- version %s", BELUGA_VERSION); "Beluga text editor -- version %s", BELUGA_VERSION);
if (welcome_len > E->screencols) { if (welcome_len > E.screencols) {
welcome_len = E->screencols; welcome_len = E.screencols;
} }
padding = (E->screencols - welcome_len) / 2; padding = (E.screencols - welcome_len) / 2;
if (padding) { if (padding) {
abAppend(ab, "~", 1); abAppend(ab, "~", 1);
--padding; --padding;
@@ -34,54 +36,54 @@ void editorDrawRows(struct editorConfig *E, struct abuf *ab) {
abAppend(ab, "~", 1); abAppend(ab, "~", 1);
} }
} else { } else {
len = E->row[file_row].rsize - E->col_offset; len = E.row[file_row].rsize - E.col_offset;
if (len < 0) if (len < 0)
len = 0; len = 0;
if (len > E->screencols) if (len > E.screencols)
len = E->screencols; len = E.screencols;
abAppend(ab, &E->row[file_row].render[E->col_offset], len); abAppend(ab, &E.row[file_row].render[E.col_offset], len);
} }
abAppend(ab, ERASE_END_LINE, 3); abAppend(ab, ERASE_END_LINE, 3);
abAppend(ab, "\r\n", 2); abAppend(ab, "\r\n", 2);
} }
} }
void editorScroll(struct editorConfig *E) { void editorScroll() {
E->rx = E->cursor_x; E.rx = E.cursor_x;
if (E->cursor_y < E->numrows) { if (E.cursor_y < E.numrows) {
E->rx = editorRowCxToRx(&E->row[E->cursor_y], E->cursor_x); E.rx = editorRowCxToRx(&E.row[E.cursor_y], E.cursor_x);
} }
if (E->cursor_y < E->row_offset) { if (E.cursor_y < E.row_offset) {
E->row_offset = E->cursor_y; E.row_offset = E.cursor_y;
} }
if (E->cursor_y >= E->row_offset + E->screenrows) { if (E.cursor_y >= E.row_offset + E.screenrows) {
E->row_offset = E->cursor_y - E->screenrows + 1; E.row_offset = E.cursor_y - E.screenrows + 1;
} }
if (E->rx < E->col_offset) { if (E.rx < E.col_offset) {
E->col_offset = E->rx; E.col_offset = E.rx;
} }
if (E->rx >= E->col_offset + E->screencols) { if (E.rx >= E.col_offset + E.screencols) {
E->col_offset = E->rx - E->screencols + 1; E.col_offset = E.rx - E.screencols + 1;
} }
} }
void editorDrawStatusBar(struct editorConfig *E, struct abuf *ab) { void editorDrawStatusBar(struct abuf *ab) {
int len, render_len; int len, render_len;
char status[80], render_status[80]; char status[80], render_status[80];
abAppend(ab, "\x1b[7m", 4); // inverting colors abAppend(ab, "\x1b[7m", 4); // inverting colors
len = snprintf(status, sizeof(status), "%.20s - %d lines%s", len = snprintf(status, sizeof(status), "%.20s - %d lines%s",
E->filename ? E->filename : "[No Name]", E->numrows, E.filename ? E.filename : "[No Name]", E.numrows,
E->dirty ? "*" : ""); E.dirty ? "*" : "");
render_len = snprintf(render_status, sizeof(render_status), "%d/%d", render_len = snprintf(render_status, sizeof(render_status), "%d/%d",
E->cursor_y + 1, E->numrows); E.cursor_y + 1, E.numrows);
if (len > E->screencols) { if (len > E.screencols) {
len = E->screencols; len = E.screencols;
} }
abAppend(ab, status, len); abAppend(ab, status, len);
while (len < E->screencols) { while (len < E.screencols) {
if (E->screencols - len == render_len) { if (E.screencols - len == render_len) {
abAppend(ab, render_status, render_len); abAppend(ab, render_status, render_len);
break; break;
} else { } else {
@@ -93,31 +95,31 @@ void editorDrawStatusBar(struct editorConfig *E, struct abuf *ab) {
abAppend(ab, "\r\n", 2); abAppend(ab, "\r\n", 2);
} }
void editorDrawMessageBar(struct editorConfig *E, struct abuf *ab) { void editorDrawMessageBar(struct abuf *ab) {
int msg_len = strlen(E->status_msg); int msg_len = strlen(E.status_msg);
abAppend(ab, ERASE_END_LINE, 3); abAppend(ab, ERASE_END_LINE, 3);
if (msg_len > E->screencols) { if (msg_len > E.screencols) {
msg_len = E->screencols; msg_len = E.screencols;
} }
if (msg_len && time(NULL) - E->status_msg_time < 5) { if (msg_len && time(NULL) - E.status_msg_time < 5) {
abAppend(ab, E->status_msg, msg_len); abAppend(ab, E.status_msg, msg_len);
} }
} }
void editorRefreshScreen(struct editorConfig *E) { void editorRefreshScreen() {
editorScroll(E); editorScroll();
struct abuf ab = ABUF_INIT; struct abuf ab = ABUF_INIT;
char buf[32]; char buf[32];
abAppend(&ab, HIDE_CURSOR, 6); abAppend(&ab, HIDE_CURSOR, 6);
abAppend(&ab, CURSOR_TOP_LEFT, 3); abAppend(&ab, CURSOR_TOP_LEFT, 3);
editorDrawRows(E, &ab); editorDrawRows(&ab);
editorDrawStatusBar(E, &ab); editorDrawStatusBar(&ab);
editorDrawMessageBar(E, &ab); editorDrawMessageBar(&ab);
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E->cursor_y - E->row_offset) + 1, snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cursor_y - E.row_offset) + 1,
(E->rx - E->col_offset) + 1); (E.rx - E.col_offset) + 1);
abAppend(&ab, buf, strlen(buf)); abAppend(&ab, buf, strlen(buf));
abAppend(&ab, SHOW_CURSOR, 6); abAppend(&ab, SHOW_CURSOR, 6);
@@ -126,10 +128,10 @@ void editorRefreshScreen(struct editorConfig *E) {
abFree(&ab); abFree(&ab);
} }
void editorSetStatusMessage(struct editorConfig *E, const char *fmt, ...) { void editorSetStatusMessage(const char *fmt, ...) {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
vsnprintf(E->status_msg, sizeof(E->status_msg), fmt, ap); vsnprintf(E.status_msg, sizeof(E.status_msg), fmt, ap);
va_end(ap); va_end(ap);
E->status_msg_time = time(NULL); E.status_msg_time = time(NULL);
} }
+27 -26
View File
@@ -3,6 +3,8 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
extern struct editorConfig E;
int editorRowCxToRx(erow *row, int cursor_x) { int editorRowCxToRx(erow *row, int cursor_x) {
int render_x = 0; int render_x = 0;
int i; int i;
@@ -53,25 +55,25 @@ void editorUpdateRow(erow *row) {
row->rsize = i_render; row->rsize = i_render;
} }
void editorInsertRow(struct editorConfig *E, int at, char *s, size_t len) { void editorInsertRow(int at, char *s, size_t len) {
if (at < 0 || at > E->numrows) { if (at < 0 || at > E.numrows) {
return; return;
} }
E->row = realloc(E->row, sizeof(erow) * (E->numrows + 1)); E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1));
memmove(&E->row[at + 1], &E->row[at], sizeof(erow) * (E->numrows - at)); memmove(&E.row[at + 1], &E.row[at], sizeof(erow) * (E.numrows - at));
E->row[at].size = len; E.row[at].size = len;
E->row[at].chars = malloc(len + 1); E.row[at].chars = malloc(len + 1);
memcpy(E->row[at].chars, s, len); memcpy(E.row[at].chars, s, len);
E->row[at].chars[len] = '\0'; E.row[at].chars[len] = '\0';
E->row[at].rsize = 0; E.row[at].rsize = 0;
E->row[at].render = NULL; E.row[at].render = NULL;
editorUpdateRow(&E->row[at]); editorUpdateRow(&E.row[at]);
++E->numrows; ++E.numrows;
++E->dirty; ++E.dirty;
} }
void editorFreeRow(erow *row) { void editorFreeRow(erow *row) {
@@ -79,21 +81,21 @@ void editorFreeRow(erow *row) {
free(row->chars); free(row->chars);
} }
void editorDelRow(struct editorConfig *E, int at) { void editorDelRow(int at) {
if (at < 0 || at >= E->numrows) { if (at < 0 || at >= E.numrows) {
return; return;
} }
editorFreeRow(&E->row[at]); editorFreeRow(&E.row[at]);
memmove(&E->row[at], &E->row[at + 1], sizeof(erow) * (E->numrows - at - 1)); memmove(&E.row[at], &E.row[at + 1], sizeof(erow) * (E.numrows - at - 1));
--E->numrows; --E.numrows;
++E->dirty; ++E.dirty;
} }
/** /**
* \fn editorRowInsertChar(erow *row, int at, int c) * \fn editorRowInsertChar(erow *row, int at, int c)
* \param at Index of where we want to insert the char */ * \param at Index of where we want to insert the char */
void editorRowInsertChar(struct editorConfig *E, erow *row, int at, int c) { void editorRowInsertChar(erow *row, int at, int c) {
if (at < 0 || at > row->size) { if (at < 0 || at > row->size) {
at = row->size; at = row->size;
} }
@@ -102,17 +104,16 @@ void editorRowInsertChar(struct editorConfig *E, erow *row, int at, int c) {
++row->size; ++row->size;
row->chars[at] = c; row->chars[at] = c;
editorUpdateRow(row); editorUpdateRow(row);
++E->dirty; ++E.dirty;
} }
void editorRowAppendString(struct editorConfig *E, erow *row, char *s, void editorRowAppendString(erow *row, char *s, size_t len) {
size_t len) {
row->chars = realloc(row->chars, row->size + len + 1); row->chars = realloc(row->chars, row->size + len + 1);
memcpy(&row->chars[row->size], s, len); memcpy(&row->chars[row->size], s, len);
row->size += len; row->size += len;
row->chars[row->size] = '\0'; row->chars[row->size] = '\0';
editorUpdateRow(row); editorUpdateRow(row);
++E->dirty; ++E.dirty;
} }
/** /**
@@ -120,12 +121,12 @@ void editorRowAppendString(struct editorConfig *E, erow *row, char *s,
* \brief Delete the a char at the chosen position on the given row * \brief Delete the a char at the chosen position on the given row
* \param at Index of the char to delete * \param at Index of the char to delete
* \param row Row on operation is made */ * \param row Row on operation is made */
void editorRowDelchar(struct editorConfig *E, erow *row, int at) { void editorRowDelchar(erow *row, int at) {
if (at < 0 || at >= row->size) { if (at < 0 || at >= row->size) {
return; return;
} }
memmove(&row->chars[at], &row->chars[at + 1], row->size - at); memmove(&row->chars[at], &row->chars[at + 1], row->size - at);
--row->size; --row->size;
editorUpdateRow(row); editorUpdateRow(row);
++E->dirty; ++E.dirty;
} }
+6 -5
View File
@@ -1,4 +1,5 @@
#include "../include/terminal.h" #include "../include/terminal.h"
#include "data.h"
void die(const char *s) { void die(const char *s) {
write(STDOUT_FILENO, "\x1b[2J", 4); write(STDOUT_FILENO, "\x1b[2J", 4);
@@ -7,18 +8,18 @@ void die(const char *s) {
exit(1); exit(1);
} }
void disableRawMode(struct editorConfig *E) { void disableRawMode() {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E->orig_termios) == -1) { if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E.orig_termios) == -1) {
die("tcsetattr"); die("tcsetattr");
} }
} }
void enableRawMode(struct editorConfig *E) { void enableRawMode() {
if (tcgetattr(STDIN_FILENO, &E->orig_termios) == -1) { if (tcgetattr(STDIN_FILENO, &E.orig_termios) == -1) {
die("tcgetattr"); die("tcgetattr");
} }
struct termios raw = E->orig_termios; struct termios raw = E.orig_termios;
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
raw.c_oflag &= ~(OPOST); raw.c_oflag &= ~(OPOST);
raw.c_cflag |= (CS8); raw.c_cflag |= (CS8);