organisation des des fichiers
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
#include "../include/append_buffer.h"
|
||||
|
||||
void abAppend(struct abuf *ab, const char *s, int len) {
|
||||
char *new = realloc(ab->b, ab->len + len);
|
||||
|
||||
if (new == NULL) {
|
||||
return;
|
||||
}
|
||||
memcpy(&new[ab->len], s, len);
|
||||
ab->b = new;
|
||||
ab->len += len;
|
||||
}
|
||||
|
||||
void abFree(struct abuf *ab) { free(ab->b); }
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "../include/file_io.h"
|
||||
|
||||
void editorOpen(struct editorConfig *E, char *filename) {
|
||||
FILE *fp = fopen(filename, "r");
|
||||
if (!fp)
|
||||
die("fopen");
|
||||
|
||||
char *line = NULL;
|
||||
size_t line_cap = 0;
|
||||
ssize_t line_len;
|
||||
|
||||
while ((line_len = getline(&line, &line_cap, fp)) != -1) {
|
||||
while (line_len > 0 &&
|
||||
(line[line_len - 1] == '\n' || line[line_len - 1] == '\r')) {
|
||||
--line_len;
|
||||
}
|
||||
editorAppendRow(E, line, line_len);
|
||||
}
|
||||
free(line);
|
||||
fclose(fp);
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
#include "../include/init.h"
|
||||
|
||||
void initEditor(struct editorConfig *E) {
|
||||
E->cursor_x = 0;
|
||||
E->cursor_y = 0;
|
||||
E->row_offset = 0;
|
||||
E->numrows = 0;
|
||||
E->row = NULL;
|
||||
if (getWindowSize(&E->screenrows, &E->screencols) == -1) {
|
||||
die("getWindowSize");
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
#include "../include/input.h"
|
||||
|
||||
void editorMoveCursor(struct editorConfig *E, int key) {
|
||||
switch (key) {
|
||||
case CURSOR_LEFT:
|
||||
if (E->cursor_x != 0) {
|
||||
--E->cursor_x;
|
||||
}
|
||||
break;
|
||||
case CURSOR_DOWN:
|
||||
if (E->cursor_x != E->screencols - 1) {
|
||||
++E->cursor_y;
|
||||
}
|
||||
break;
|
||||
case CURSOR_UP:
|
||||
if (E->cursor_y != 0) {
|
||||
--E->cursor_y;
|
||||
}
|
||||
break;
|
||||
case CURSOR_RIGHT:
|
||||
if (E->cursor_y != E->screenrows - 1) {
|
||||
++E->cursor_x;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void editorProcessKeypress(struct editorConfig *E) {
|
||||
int c = editorReadKey();
|
||||
int times;
|
||||
|
||||
switch (c) {
|
||||
case CTRL_KEY('q'):
|
||||
write(STDOUT_FILENO, "\x1b[2J", 4);
|
||||
write(STDOUT_FILENO, CURSOR_TOP_LEFT, 3);
|
||||
disableRawMode(E);
|
||||
exit(0);
|
||||
break;
|
||||
|
||||
case BEG_LINE:
|
||||
E->cursor_x = 0;
|
||||
break;
|
||||
|
||||
case END_LINE:
|
||||
E->cursor_x = E->screencols - 1;
|
||||
break;
|
||||
|
||||
case PAGE_UP:
|
||||
case PAGE_DOWN: {
|
||||
times = E->screenrows;
|
||||
while (--times) {
|
||||
editorMoveCursor(E, c == PAGE_UP ? CURSOR_UP : CURSOR_DOWN);
|
||||
}
|
||||
} break;
|
||||
|
||||
case CURSOR_UP:
|
||||
case CURSOR_DOWN:
|
||||
case CURSOR_LEFT:
|
||||
case CURSOR_RIGHT:
|
||||
editorMoveCursor(E, c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
#include "../include/output.h"
|
||||
|
||||
void editorDrawRows(struct editorConfig *E, 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) {
|
||||
welcome_len =
|
||||
snprintf(welcome, sizeof(welcome),
|
||||
"Beluga text editor -- version %s", BELUGA_VERSION);
|
||||
if (welcome_len > E->screencols) {
|
||||
welcome_len = E->screencols;
|
||||
}
|
||||
padding = (E->screencols - welcome_len) / 2;
|
||||
if (padding) {
|
||||
abAppend(ab, "~", 1);
|
||||
--padding;
|
||||
}
|
||||
while (padding--) {
|
||||
abAppend(ab, " ", 1);
|
||||
}
|
||||
abAppend(ab, welcome, welcome_len);
|
||||
} else {
|
||||
abAppend(ab, "~", 1);
|
||||
}
|
||||
} else {
|
||||
len = E->row[file_row].size > E->screencols ? E->screencols
|
||||
: E->row[y].size;
|
||||
abAppend(ab, E->row[file_row].chars, len);
|
||||
}
|
||||
abAppend(ab, ERASE_END_LINE, 3);
|
||||
if (y < E->screenrows - 1) {
|
||||
abAppend(ab, "\r\n", 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void editorRefreshScreen(struct editorConfig *E) {
|
||||
struct abuf ab = ABUF_INIT;
|
||||
char buf[32];
|
||||
|
||||
abAppend(&ab, HIDE_CURSOR, 6);
|
||||
abAppend(&ab, CURSOR_TOP_LEFT, 3);
|
||||
|
||||
editorDrawRows(E, &ab);
|
||||
|
||||
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", E->cursor_y + 1, E->cursor_x + 1);
|
||||
abAppend(&ab, buf, strlen(buf));
|
||||
|
||||
abAppend(&ab, SHOW_CURSOR, 6);
|
||||
|
||||
write(STDOUT_FILENO, ab.b, ab.len);
|
||||
abFree(&ab);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "../include/row_op.h"
|
||||
|
||||
void editorAppendRow(struct editorConfig *E, char *s, size_t len) {
|
||||
int at;
|
||||
E->row = realloc(E->row, sizeof(erow) * (E->numrows + 1));
|
||||
|
||||
at = E->numrows;
|
||||
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->numrows;
|
||||
}
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
#include "../include/terminal.h"
|
||||
|
||||
void die(const char *s) {
|
||||
write(STDOUT_FILENO, "\x1b[2J", 4);
|
||||
write(STDOUT_FILENO, CURSOR_TOP_LEFT, 3);
|
||||
perror(s);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void disableRawMode(struct editorConfig *E) {
|
||||
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E->orig_termios) == -1) {
|
||||
die("tcsetattr");
|
||||
}
|
||||
}
|
||||
|
||||
void enableRawMode(struct editorConfig *E) {
|
||||
if (tcgetattr(STDIN_FILENO, &E->orig_termios) == -1) {
|
||||
die("tcgetattr");
|
||||
}
|
||||
|
||||
struct termios raw = E->orig_termios;
|
||||
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
|
||||
raw.c_oflag &= ~(OPOST);
|
||||
raw.c_cflag |= (CS8);
|
||||
raw.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
|
||||
raw.c_cc[VMIN] = 0;
|
||||
raw.c_cc[VTIME] = 1;
|
||||
|
||||
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) {
|
||||
die("tcgetattr");
|
||||
}
|
||||
}
|
||||
|
||||
int editorReadKey() {
|
||||
int nread;
|
||||
char c;
|
||||
char seq[3];
|
||||
while ((nread = read(STDIN_FILENO, &c, 1)) != 1) {
|
||||
if (nread == -1 && errno != EAGAIN) {
|
||||
die("read");
|
||||
}
|
||||
}
|
||||
|
||||
if (c == '\x1b') {
|
||||
if (read(STDIN_FILENO, &seq[0], 1) != 1 ||
|
||||
read(STDIN_FILENO, &seq[1], 1) != 1) {
|
||||
return '\x1b';
|
||||
}
|
||||
if (seq[0] == '[') {
|
||||
if (seq[1] >= '0' && seq[1] <= '9') {
|
||||
if (read(STDIN_FILENO, &seq[2], 1) != 1) {
|
||||
return '\x1b';
|
||||
}
|
||||
if (seq[2] == '~') {
|
||||
switch (seq[1]) {
|
||||
case '1':
|
||||
return BEG_LINE;
|
||||
case '3':
|
||||
return DELETE;
|
||||
case '4':
|
||||
return END_LINE;
|
||||
case '5':
|
||||
return PAGE_UP;
|
||||
case '6':
|
||||
return PAGE_DOWN;
|
||||
case '7':
|
||||
return BEG_LINE;
|
||||
case '8':
|
||||
return END_LINE;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
switch (seq[1]) {
|
||||
case 'A':
|
||||
return CURSOR_UP;
|
||||
case 'B':
|
||||
return CURSOR_DOWN;
|
||||
case 'C':
|
||||
return CURSOR_LEFT;
|
||||
case 'D':
|
||||
return CURSOR_RIGHT;
|
||||
case 'H':
|
||||
return BEG_LINE;
|
||||
case 'F':
|
||||
return END_LINE;
|
||||
}
|
||||
}
|
||||
} else if (seq[0] == 'O') {
|
||||
switch (seq[1]) {
|
||||
case 'H':
|
||||
return BEG_LINE;
|
||||
case 'F':
|
||||
return END_LINE;
|
||||
}
|
||||
}
|
||||
return '\x1b';
|
||||
} else {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
int getCursorPosition(int *rows, int *cols) {
|
||||
char buf[32];
|
||||
unsigned int i = 0;
|
||||
|
||||
if (write(STDOUT_FILENO, "\x1b[6n", 4) != 4) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (i < sizeof(buf) - 1) {
|
||||
if (read(STDIN_FILENO, &buf[i], 1) != 1) {
|
||||
break;
|
||||
}
|
||||
if (buf[i] == 'R') {
|
||||
break;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
buf[i] = '\0';
|
||||
|
||||
if (buf[0] != '\x1b' || buf[1] != '[') {
|
||||
return -1;
|
||||
}
|
||||
if (sscanf(&buf[2], "%d;%d", rows, cols) != 2) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getWindowSize(int *rows, int *cols) {
|
||||
struct winsize ws;
|
||||
|
||||
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
|
||||
if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12) {
|
||||
return -1;
|
||||
}
|
||||
return getCursorPosition(rows, cols);
|
||||
} else {
|
||||
*cols = ws.ws_col;
|
||||
*rows = ws.ws_row;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user