diff --git a/include/data.h b/include/data.h index 1effdf0..0b82ea3 100644 --- a/include/data.h +++ b/include/data.h @@ -1,6 +1,7 @@ #ifndef DATA_H_ #define DATA_H_ +#include "../lisp-interpreter/dist/lisp.h" #include #include @@ -35,6 +36,9 @@ struct editorConfig { char status_msg[80]; time_t status_msg_time; 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 */ }; +extern struct editorConfig E; + #endif diff --git a/include/editor_op.h b/include/editor_op.h index 93aa2a8..757b6c8 100644 --- a/include/editor_op.h +++ b/include/editor_op.h @@ -2,10 +2,10 @@ #define EDITOR_OP_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_ diff --git a/include/file_io.h b/include/file_io.h index 13eaa64..f93154d 100644 --- a/include/file_io.h +++ b/include/file_io.h @@ -8,10 +8,10 @@ #include #include -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_ diff --git a/include/init.h b/include/init.h index af1f94e..f538d94 100644 --- a/include/init.h +++ b/include/init.h @@ -10,6 +10,6 @@ * \brief Job's function is to initialize all the fields of editorConfig. * */ -void initEditor(struct editorConfig *E); +void initEditor(); #endif // INIT_H_ diff --git a/include/input.h b/include/input.h index 7003ff9..676be3d 100644 --- a/include/input.h +++ b/include/input.h @@ -19,15 +19,15 @@ // END \x1b[4~ || [8~ || [F || OF // 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() * \brief Get the last key input and do the proper action. */ -void editorProcessKeypress(struct editorConfig *E); +void editorProcessKeypress(); #endif // INPUT_H_ diff --git a/include/output.h b/include/output.h index b6db4f1..590f269 100644 --- a/include/output.h +++ b/include/output.h @@ -13,16 +13,16 @@ * \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_ diff --git a/include/row_op.h b/include/row_op.h index 911603f..9a486db 100644 --- a/include/row_op.h +++ b/include/row_op.h @@ -12,17 +12,16 @@ int editorRowCxToRx(erow *row, int cursor_x); 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 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, - size_t len); +void editorRowAppendString(erow *row, char *s, size_t len); -void editorRowDelchar(struct editorConfig *E, erow *row, int at); +void editorRowDelchar(erow *row, int at); #endif // ROW_OP_H_ diff --git a/include/terminal.h b/include/terminal.h index cefe14e..6ab9108 100644 --- a/include/terminal.h +++ b/include/terminal.h @@ -21,9 +21,9 @@ void die(const char *s); -void disableRawMode(struct editorConfig *E); +void disableRawMode(); -void enableRawMode(struct editorConfig *E); +void enableRawMode(); int editorReadKey(); diff --git a/main.c b/main.c index f720ceb..dbc2bd5 100644 --- a/main.c +++ b/main.c @@ -16,21 +16,21 @@ #include "include/output.h" #include "include/terminal.h" +struct editorConfig E; + int main(int argc, char *argv[]) { - struct editorConfig E; - - enableRawMode(&E); - initEditor(&E); + enableRawMode(); + initEditor(); 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) { - editorRefreshScreen(&E); - editorProcessKeypress(&E); + editorRefreshScreen(); + editorProcessKeypress(); } return 0; } diff --git a/src/append_buffer.c b/src/append_buffer.c index 38cdc05..00e14a3 100644 --- a/src/append_buffer.c +++ b/src/append_buffer.c @@ -1,5 +1,7 @@ #include "../include/append_buffer.h" +extern struct editorConfig E; + void abAppend(struct abuf *ab, const char *s, int len) { char *new = realloc(ab->b, ab->len + len); diff --git a/src/editor_op.c b/src/editor_op.c index d5aa1aa..258f197 100644 --- a/src/editor_op.c +++ b/src/editor_op.c @@ -1,44 +1,46 @@ #include "../include/editor_op.h" #include "../include/row_op.h" -void editorInsertChar(struct editorConfig *E, int c) { - if (E->cursor_y == E->numrows) { - editorInsertRow(E, E->numrows, "", 0); +extern struct editorConfig E; + +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); - E->cursor_x++; + editorRowInsertChar(&E.row[E.cursor_y], E.cursor_x, c); + E.cursor_x++; } -void editorInsertNewLine(struct editorConfig *E) { +void editorInsertNewLine() { erow *row; - if (!E->cursor_x) { - editorInsertRow(E, E->cursor_y, "", 0); + if (!E.cursor_x) { + editorInsertRow(E.cursor_y, "", 0); } else { - row = &E->row[E->cursor_y]; - editorInsertRow(E, E->cursor_y + 1, &row->chars[E->cursor_x], - row->size - E->cursor_x); - row = &E->row[E->cursor_y]; - row->size = E->cursor_x; + row = &E.row[E.cursor_y]; + editorInsertRow(E.cursor_y + 1, &row->chars[E.cursor_x], + row->size - E.cursor_x); + row = &E.row[E.cursor_y]; + row->size = E.cursor_x; row->chars[row->size] = '\0'; editorUpdateRow(row); } - ++E->cursor_y; - E->cursor_x = 0; + ++E.cursor_y; + E.cursor_x = 0; } -void editorDelChar(struct editorConfig *E) { +void editorDelChar() { 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; } - row = &E->row[E->cursor_y]; - if (E->cursor_x > 0) { - editorRowDelchar(E, row, E->cursor_x - 1); - --E->cursor_x; + row = &E.row[E.cursor_y]; + if (E.cursor_x > 0) { + editorRowDelchar(row, E.cursor_x - 1); + --E.cursor_x; } else { - E->cursor_x = E->row[E->cursor_y - 1].size; - editorRowAppendString(E, &E->row[E->cursor_y - 1], row->chars, row->size); - editorDelRow(E, E->cursor_y); - --E->cursor_y; + E.cursor_x = E.row[E.cursor_y - 1].size; + editorRowAppendString(&E.row[E.cursor_y - 1], row->chars, row->size); + editorDelRow(E.cursor_y); + --E.cursor_y; } } diff --git a/src/file_io.c b/src/file_io.c index 057848b..ac535ca 100644 --- a/src/file_io.c +++ b/src/file_io.c @@ -12,22 +12,23 @@ extern char *strdup(const char *); extern ssize_t getline(char **restrict lineptr, size_t *restrict n, FILE *restrict stream); 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 j; char *buf; char *p; - for (j = 0; j < E->numrows; ++j) { - tot_len += E->row[j].size + 1; + for (j = 0; j < E.numrows; ++j) { + tot_len += E.row[j].size + 1; } *buffer_len = tot_len; buf = malloc(tot_len); p = buf; - for (j = 0; j < E->numrows; ++j) { - memcpy(p, E->row[j].chars, E->row[j].size); - p += E->row[j].size; + for (j = 0; j < E.numrows; ++j) { + memcpy(p, E.row[j].chars, E.row[j].size); + p += E.row[j].size; *p = '\n'; p++; } @@ -35,11 +36,11 @@ char *editorRowsToString(struct editorConfig *E, int *buffer_len) { return buf; } -void editorOpen(struct editorConfig *E, char *filename) { +void editorOpen(char *filename) { FILE *fp; - free(E->filename); - E->filename = strdup(filename); + free(E.filename); + E.filename = strdup(filename); fp = fopen(filename, "r"); if (!fp) @@ -54,38 +55,38 @@ void editorOpen(struct editorConfig *E, char *filename) { (line[line_len - 1] == '\n' || line[line_len - 1] == '\r')) { --line_len; } - editorInsertRow(E, E->numrows, line, line_len); + editorInsertRow(E.numrows, line, line_len); } free(line); fclose(fp); - E->dirty = 0; + E.dirty = 0; } -void editorSave(struct editorConfig *E) { +void editorSave() { int len; char *buf; int fd; - if (E->filename == NULL) { - E->filename = editorPrompt(E, "Save as: %s (ESC to cancel)"); - if (E->filename == NULL) { - editorSetStatusMessage(E, "Save aborted"); + if (E.filename == NULL) { + E.filename = editorPrompt("Save as: %s (ESC to cancel)"); + if (E.filename == NULL) { + editorSetStatusMessage("Save aborted"); return; } } - buf = editorRowsToString(E, &len); - fd = open(E->filename, O_RDWR | O_CREAT, 0644); + buf = editorRowsToString(&len); + fd = open(E.filename, O_RDWR | O_CREAT, 0644); if (fd != -1) { if (ftruncate(fd, len) != -1) { if (write(fd, buf, len) == len) { close(fd); free(buf); - E->dirty = 0; - editorSetStatusMessage(E, "%d bytes written to disk", len); + E.dirty = 0; + editorSetStatusMessage("%d bytes written to disk", len); return; } } close(fd); } free(buf); - editorSetStatusMessage(E, "Can't save! I/O error: %s", strerror(errno)); + editorSetStatusMessage("Can't save! I/O error: %s", strerror(errno)); } diff --git a/src/init.c b/src/init.c index 9b06fac..8dc3d90 100644 --- a/src/init.c +++ b/src/init.c @@ -1,19 +1,24 @@ #include "../include/init.h" +#include "data.h" +#include +#include -void initEditor(struct editorConfig *E) { - 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; - if (getWindowSize(&E->screenrows, &E->screencols) == -1) { +extern struct editorConfig; + +void initEditor() { + 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; + if (getWindowSize(&E.screenrows, &E.screencols) == -1) { die("getWindowSize"); } - E->screenrows -= 2; + E.screenrows -= 2; } diff --git a/src/input.c b/src/input.c index 9fc3d12..57f749e 100644 --- a/src/input.c +++ b/src/input.c @@ -8,11 +8,13 @@ #include #include +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; diff --git a/src/output.c b/src/output.c index 28b508d..a8a896a 100644 --- a/src/output.c +++ b/src/output.c @@ -4,24 +4,26 @@ #include #include -void editorDrawRows(struct editorConfig *E, struct abuf *ab) { +extern struct editorConfig E; + +void editorDrawRows(struct abuf *ab) { int y; char welcome[80]; int welcome_len; int padding; int len; int file_row; - for (y = 0; y < E->screenrows; ++y) { - file_row = y + E->row_offset; - if (file_row >= E->numrows) { - if (E->numrows == 0 && y == E->screenrows / 3) { + for (y = 0; y < E.screenrows; ++y) { + file_row = y + E.row_offset; + if (file_row >= E.numrows) { + if (E.numrows == 0 && y == E.screenrows / 3) { welcome_len = snprintf(welcome, sizeof(welcome), "Beluga text editor -- version %s", BELUGA_VERSION); - if (welcome_len > E->screencols) { - welcome_len = E->screencols; + if (welcome_len > E.screencols) { + welcome_len = E.screencols; } - padding = (E->screencols - welcome_len) / 2; + padding = (E.screencols - welcome_len) / 2; if (padding) { abAppend(ab, "~", 1); --padding; @@ -34,54 +36,54 @@ void editorDrawRows(struct editorConfig *E, struct abuf *ab) { abAppend(ab, "~", 1); } } else { - len = E->row[file_row].rsize - E->col_offset; + len = E.row[file_row].rsize - E.col_offset; if (len < 0) len = 0; - if (len > E->screencols) - len = E->screencols; - abAppend(ab, &E->row[file_row].render[E->col_offset], len); + if (len > E.screencols) + len = E.screencols; + abAppend(ab, &E.row[file_row].render[E.col_offset], len); } abAppend(ab, ERASE_END_LINE, 3); abAppend(ab, "\r\n", 2); } } -void editorScroll(struct editorConfig *E) { - E->rx = E->cursor_x; - if (E->cursor_y < E->numrows) { - E->rx = editorRowCxToRx(&E->row[E->cursor_y], E->cursor_x); +void editorScroll() { + E.rx = E.cursor_x; + if (E.cursor_y < E.numrows) { + E.rx = editorRowCxToRx(&E.row[E.cursor_y], E.cursor_x); } - if (E->cursor_y < E->row_offset) { - E->row_offset = E->cursor_y; + if (E.cursor_y < E.row_offset) { + E.row_offset = E.cursor_y; } - if (E->cursor_y >= E->row_offset + E->screenrows) { - E->row_offset = E->cursor_y - E->screenrows + 1; + if (E.cursor_y >= E.row_offset + E.screenrows) { + E.row_offset = E.cursor_y - E.screenrows + 1; } - if (E->rx < E->col_offset) { - E->col_offset = E->rx; + if (E.rx < E.col_offset) { + E.col_offset = E.rx; } - if (E->rx >= E->col_offset + E->screencols) { - E->col_offset = E->rx - E->screencols + 1; + if (E.rx >= E.col_offset + E.screencols) { + 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; char status[80], render_status[80]; abAppend(ab, "\x1b[7m", 4); // inverting colors len = snprintf(status, sizeof(status), "%.20s - %d lines%s", - E->filename ? E->filename : "[No Name]", E->numrows, - E->dirty ? "*" : ""); + E.filename ? E.filename : "[No Name]", E.numrows, + E.dirty ? "*" : ""); render_len = snprintf(render_status, sizeof(render_status), "%d/%d", - E->cursor_y + 1, E->numrows); - if (len > E->screencols) { - len = E->screencols; + E.cursor_y + 1, E.numrows); + if (len > E.screencols) { + len = E.screencols; } abAppend(ab, status, len); - while (len < E->screencols) { - if (E->screencols - len == render_len) { + while (len < E.screencols) { + if (E.screencols - len == render_len) { abAppend(ab, render_status, render_len); break; } else { @@ -93,31 +95,31 @@ void editorDrawStatusBar(struct editorConfig *E, struct abuf *ab) { abAppend(ab, "\r\n", 2); } -void editorDrawMessageBar(struct editorConfig *E, struct abuf *ab) { - int msg_len = strlen(E->status_msg); +void editorDrawMessageBar(struct abuf *ab) { + int msg_len = strlen(E.status_msg); abAppend(ab, ERASE_END_LINE, 3); - if (msg_len > E->screencols) { - msg_len = E->screencols; + if (msg_len > E.screencols) { + msg_len = E.screencols; } - if (msg_len && time(NULL) - E->status_msg_time < 5) { - abAppend(ab, E->status_msg, msg_len); + if (msg_len && time(NULL) - E.status_msg_time < 5) { + abAppend(ab, E.status_msg, msg_len); } } -void editorRefreshScreen(struct editorConfig *E) { - editorScroll(E); +void editorRefreshScreen() { + editorScroll(); struct abuf ab = ABUF_INIT; char buf[32]; abAppend(&ab, HIDE_CURSOR, 6); abAppend(&ab, CURSOR_TOP_LEFT, 3); - editorDrawRows(E, &ab); - editorDrawStatusBar(E, &ab); - editorDrawMessageBar(E, &ab); + editorDrawRows(&ab); + editorDrawStatusBar(&ab); + editorDrawMessageBar(&ab); - snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E->cursor_y - E->row_offset) + 1, - (E->rx - E->col_offset) + 1); + snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cursor_y - E.row_offset) + 1, + (E.rx - E.col_offset) + 1); abAppend(&ab, buf, strlen(buf)); abAppend(&ab, SHOW_CURSOR, 6); @@ -126,10 +128,10 @@ void editorRefreshScreen(struct editorConfig *E) { abFree(&ab); } -void editorSetStatusMessage(struct editorConfig *E, const char *fmt, ...) { +void editorSetStatusMessage(const char *fmt, ...) { va_list ap; 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); - E->status_msg_time = time(NULL); + E.status_msg_time = time(NULL); } diff --git a/src/row_op.c b/src/row_op.c index 1d902c3..97c22af 100644 --- a/src/row_op.c +++ b/src/row_op.c @@ -3,6 +3,8 @@ #include #include +extern struct editorConfig E; + int editorRowCxToRx(erow *row, int cursor_x) { int render_x = 0; int i; @@ -53,25 +55,25 @@ void editorUpdateRow(erow *row) { row->rsize = i_render; } -void editorInsertRow(struct editorConfig *E, int at, char *s, size_t len) { - if (at < 0 || at > E->numrows) { +void editorInsertRow(int at, char *s, size_t len) { + if (at < 0 || at > E.numrows) { return; } - E->row = realloc(E->row, sizeof(erow) * (E->numrows + 1)); - memmove(&E->row[at + 1], &E->row[at], sizeof(erow) * (E->numrows - at)); + E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1)); + memmove(&E.row[at + 1], &E.row[at], sizeof(erow) * (E.numrows - at)); - E->row[at].size = len; - E->row[at].chars = malloc(len + 1); - memcpy(E->row[at].chars, s, len); - E->row[at].chars[len] = '\0'; + E.row[at].size = len; + E.row[at].chars = malloc(len + 1); + memcpy(E.row[at].chars, s, len); + E.row[at].chars[len] = '\0'; - E->row[at].rsize = 0; - E->row[at].render = NULL; - editorUpdateRow(&E->row[at]); + E.row[at].rsize = 0; + E.row[at].render = NULL; + editorUpdateRow(&E.row[at]); - ++E->numrows; - ++E->dirty; + ++E.numrows; + ++E.dirty; } void editorFreeRow(erow *row) { @@ -79,21 +81,21 @@ void editorFreeRow(erow *row) { free(row->chars); } -void editorDelRow(struct editorConfig *E, int at) { - if (at < 0 || at >= E->numrows) { +void editorDelRow(int at) { + if (at < 0 || at >= E.numrows) { return; } - editorFreeRow(&E->row[at]); - memmove(&E->row[at], &E->row[at + 1], sizeof(erow) * (E->numrows - at - 1)); - --E->numrows; - ++E->dirty; + editorFreeRow(&E.row[at]); + memmove(&E.row[at], &E.row[at + 1], sizeof(erow) * (E.numrows - at - 1)); + --E.numrows; + ++E.dirty; } /** * \fn editorRowInsertChar(erow *row, int at, int c) * \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) { at = row->size; } @@ -102,17 +104,16 @@ void editorRowInsertChar(struct editorConfig *E, erow *row, int at, int c) { ++row->size; row->chars[at] = c; editorUpdateRow(row); - ++E->dirty; + ++E.dirty; } -void editorRowAppendString(struct editorConfig *E, erow *row, char *s, - size_t len) { +void editorRowAppendString(erow *row, char *s, size_t len) { row->chars = realloc(row->chars, row->size + len + 1); memcpy(&row->chars[row->size], s, len); row->size += len; row->chars[row->size] = '\0'; 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 * \param at Index of the char to delete * \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) { return; } memmove(&row->chars[at], &row->chars[at + 1], row->size - at); --row->size; editorUpdateRow(row); - ++E->dirty; + ++E.dirty; } diff --git a/src/terminal.c b/src/terminal.c index 581deb1..0ac54ac 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -1,4 +1,5 @@ #include "../include/terminal.h" +#include "data.h" void die(const char *s) { write(STDOUT_FILENO, "\x1b[2J", 4); @@ -7,18 +8,18 @@ void die(const char *s) { exit(1); } -void disableRawMode(struct editorConfig *E) { - if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E->orig_termios) == -1) { +void disableRawMode() { + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E.orig_termios) == -1) { die("tcsetattr"); } } -void enableRawMode(struct editorConfig *E) { - if (tcgetattr(STDIN_FILENO, &E->orig_termios) == -1) { +void enableRawMode() { + if (tcgetattr(STDIN_FILENO, &E.orig_termios) == -1) { die("tcgetattr"); } - struct termios raw = E->orig_termios; + struct termios raw = E.orig_termios; raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); raw.c_oflag &= ~(OPOST); raw.c_cflag |= (CS8);