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_
#define DATA_H_
#include "../lisp-interpreter/dist/lisp.h"
#include <termios.h>
#include <time.h>
@@ -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
+3 -3
View File
@@ -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_
+3 -3
View File
@@ -8,10 +8,10 @@
#include <stdlib.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_
+1 -1
View File
@@ -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_
+3 -3
View File
@@ -19,15 +19,15 @@
// END \x1b[4~ || <esc>[8~ || <esc>[F || <esc>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_
+6 -6
View File
@@ -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_
+5 -6
View File
@@ -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_
+2 -2
View File
@@ -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();
+8 -8
View File
@@ -16,21 +16,21 @@
#include "include/output.h"
#include "include/terminal.h"
int main(int argc, char *argv[]) {
struct editorConfig E;
enableRawMode(&E);
initEditor(&E);
int main(int argc, char *argv[]) {
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;
}
+2
View File
@@ -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);
+27 -25
View File
@@ -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;
}
}
+22 -21
View File
@@ -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));
}
+19 -14
View File
@@ -1,19 +1,24 @@
#include "../include/init.h"
#include "data.h"
#include <stdio.h>
#include <stdlib.h>
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;
}
+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;
+50 -48
View File
@@ -4,24 +4,26 @@
#include <string.h>
#include <time.h>
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);
}
+27 -26
View File
@@ -3,6 +3,8 @@
#include <string.h>
#include <time.h>
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;
}
+6 -5
View File
@@ -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);