From 546a6c5f1a5159b06ffb5e9dddc67129c4bd59be Mon Sep 17 00:00:00 2001 From: night0721 Date: Sat, 9 Nov 2024 00:40:00 +0000 Subject: [PATCH] Remove old code --- include/bar.h | 8 -- include/editor.h | 16 --- include/io.h | 9 -- include/row.h | 25 ---- include/syntax.h | 26 ---- include/term.h | 12 -- include/vip.h | 75 ----------- src/bar.c | 115 ----------------- src/editor.c | 298 -------------------------------------------- src/io.c | 317 ----------------------------------------------- src/row.c | 156 ----------------------- src/syntax.c | 211 ------------------------------- src/term.c | 90 -------------- src/vip.c | 46 ------- 14 files changed, 1404 deletions(-) delete mode 100644 include/bar.h delete mode 100644 include/editor.h delete mode 100644 include/io.h delete mode 100644 include/row.h delete mode 100644 include/syntax.h delete mode 100644 include/term.h delete mode 100644 include/vip.h delete mode 100644 src/bar.c delete mode 100644 src/editor.c delete mode 100644 src/io.c delete mode 100644 src/row.c delete mode 100644 src/syntax.c delete mode 100644 src/term.c delete mode 100644 src/vip.c diff --git a/include/bar.h b/include/bar.h deleted file mode 100644 index 2345360..0000000 --- a/include/bar.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef BAR_H_ -#define BAR_H_ - -void draw_status_bar(struct abuf *ab); -void draw_message_bar(struct abuf *ab); -void set_status_bar_message(const char *fmt, ...); - -#endif diff --git a/include/editor.h b/include/editor.h deleted file mode 100644 index 60e861d..0000000 --- a/include/editor.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef EDITOR_H_ -#define EDITOR_H_ - -void refresh_screen(void); -void move_cursor(int key); -void scroll(void); -void insert_char(int c); -void insert_new_line(void); -void shift_new_line(void); -void del_char(void); -void init_editor(void); -void open_editor(char *filename); -char *prompt_editor(char *prompt, void (*callback)(char *, int)); -void find_editor(void); - -#endif diff --git a/include/io.h b/include/io.h deleted file mode 100644 index 15d0c31..0000000 --- a/include/io.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef IO_H_ -#define IO_H_ - -int read_key(void); -void save_file(void); -void process_key(void); -void draw_rows(struct abuf *ab); - -#endif diff --git a/include/row.h b/include/row.h deleted file mode 100644 index 1fed172..0000000 --- a/include/row.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef ROW_H_ -#define ROW_H_ - -typedef struct row { - int idx; - int size; - int render_size; - char *chars; - char *render; - unsigned char *hl; - int opened_comment; -} row; - -int row_cx_to_rx(row *row, int cx); -int row_rx_to_cx(row *row, int rx); -void update_row(row *row); -void insert_row(int at, char *s, size_t len); -void free_row(row *row); -void del_row(int at); -void row_insert_char(row *row, int at, int c); -void row_append_str(row *row, char *s, size_t len); -void row_del_char(row *row, int at); -char *rows_to_str(int *buflen); - -#endif diff --git a/include/syntax.h b/include/syntax.h deleted file mode 100644 index db6026f..0000000 --- a/include/syntax.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef SYNTAX_H_ -#define SYNTAX_H_ - -#include - -#include "row.h" - -#define HL_NUMBERS (1 << 0) -#define HL_STRINGS (1 << 1) - -typedef struct language { - char *filetype; - int flags; - char *singleline_comment_start; - char *multiline_comment_start; - char *multiline_comment_end; - char **keywords; - char **extensions; -} language; - -int is_separator(int c); -void update_highlight(row *row); -char *syntax_to_color(int hl, size_t *len); -void select_syntax_highlight(void); - -#endif diff --git a/include/term.h b/include/term.h deleted file mode 100644 index c4ea7d7..0000000 --- a/include/term.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef TERM_H_ -#define TERM_H_ - -#define CTRL_KEY(k) ((k) & 0x1f) - -void die(const char *s); -void reset_term(void); -void setup_term(void); -int get_cursor_position(int *rows, int *cols); -int get_window_size(int *rows, int *cols); - -#endif diff --git a/include/vip.h b/include/vip.h deleted file mode 100644 index d93501f..0000000 --- a/include/vip.h +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef VIP_H_ -#define VIP_H_ - -#include -#include - -#define VERSION "0.0.1" - -#define COLOR_LEN 19 - -enum keys { - BACKSPACE = 127, - ARROW_LEFT = 1000, - ARROW_RIGHT, - ARROW_UP, - ARROW_DOWN, - DEL_KEY, - HOME_KEY, - END_KEY, - PAGE_UP, - PAGE_DOWN -}; - -enum modes { - NORMAL, - INSERT, - VISUAL, - COMMAND -}; - -enum highlight { - DEFAULT = 0, - COMMENT, - MLCOMMENT, - KEYWORD1, /* default */ - KEYWORD2, /* types */ - STRING, - NUMBER, - MATCH, - RESET -}; - -#include "row.h" -#include "syntax.h" -#include "config.h" - -typedef struct editor { - int cx, cy; /* chars x, y */ - int rx; /* render x */ - int rowoff; - int coloff; - int screenrows, screencols; - int rows; - row *row; - int dirty; - int mode; - char *filename; - char statusmsg[80]; - time_t statusmsg_time; - language *syntax; - struct termios termios; -} editor; - -struct abuf { - char *b; - int len; -}; - -#define ABUF_INIT { NULL, 0 } -void abAppend(struct abuf *ab, const char *s, int len); -void abFree(struct abuf *ab); - -extern editor vip; - -#endif diff --git a/src/bar.c b/src/bar.c deleted file mode 100644 index be4efc2..0000000 --- a/src/bar.c +++ /dev/null @@ -1,115 +0,0 @@ -#include -#include -#include -#include - -#include "vip.h" - -extern editor vip; - -void draw_status_bar(struct abuf *ab) -{ - abAppend(ab, "\x1b[7m", 4); - abAppend(ab, "\x1b[1m", 4); - - int mode_len; - if (vip.mode == NORMAL) { - abAppend(ab, BLUE_BG, COLOR_LEN); - char mode[9] = " NORMAL "; - abAppend(ab, mode, 8); - mode_len = 8; - } else if (vip.mode == INSERT) { - abAppend(ab, GREEN_BG, COLOR_LEN); - char mode[9] = " INSERT "; - abAppend(ab, mode, 8); - mode_len = 8; - } else if (vip.mode == COMMAND) { - abAppend(ab, PEACH_BG, COLOR_LEN); - char mode[10] = " COMMAND "; - abAppend(ab, mode, 9); - mode_len = 9; - } - - abAppend(ab, "\x1b[22m", 5); - - char git_branch[80], git_diff[80], file[80], info[80], lines[80], coord[80]; - int gitb_len = snprintf(git_branch, sizeof(git_branch), " %s ", "master"); - int gitd_len = snprintf(git_diff, sizeof(git_diff), " %s ", "+1"); - int file_len = snprintf(file, sizeof(file), " %.20s %s", - vip.filename ? vip.filename : "[No Name]", vip.dirty ? "[+]" : ""); - int info_len = snprintf(info, sizeof(info), " %s ", vip.syntax ? vip.syntax->filetype : ""); - int lines_len; - if (vip.rows == 0 || vip.rows == vip.cy + 1) { - lines_len = snprintf(lines, sizeof(lines), " %s ", vip.rows == 0 ? "Top" : "Bot"); - } else { - lines_len = snprintf(lines, sizeof(lines), " %d%% ", ((vip.cy + 1) * 100 / vip.rows)); - } - int coord_len = snprintf(coord, sizeof(coord), " %d:%d ", vip.cy + 1, vip.rx + 1); - - abAppend(ab, SURFACE_1_BG, COLOR_LEN); /* background */ - if (vip.mode == NORMAL) { - abAppend(ab, BLUE_FG, COLOR_LEN); /* text */ - } else if (vip.mode == INSERT) { - abAppend(ab, GREEN_FG, COLOR_LEN); - } else if (vip.mode == COMMAND) { - abAppend(ab, PEACH_FG, COLOR_LEN); - } - abAppend(ab, git_branch, gitb_len); - abAppend(ab, "|", 1); - abAppend(ab, GREEN_FG, COLOR_LEN); - abAppend(ab, git_diff, gitd_len); - abAppend(ab, BLACK_BG, COLOR_LEN); - abAppend(ab, WHITE_FG, COLOR_LEN); - abAppend(ab, file, file_len); - - - while (file_len < vip.screencols) { - if (vip.screencols - mode_len - file_len - gitb_len - gitd_len - 1 == info_len + lines_len + coord_len) { - abAppend(ab, info, info_len); - abAppend(ab, SURFACE_1_BG, COLOR_LEN); - if (vip.mode == NORMAL) { - abAppend(ab, BLUE_FG, COLOR_LEN); - } else if (vip.mode == INSERT) { - abAppend(ab, GREEN_FG, COLOR_LEN); - } else if (vip.mode == COMMAND) { - abAppend(ab, PEACH_FG, COLOR_LEN); - } - abAppend(ab, lines, lines_len); - if (vip.mode == NORMAL) { - abAppend(ab, BLUE_BG, COLOR_LEN); - } else if (vip.mode == INSERT) { - abAppend(ab, GREEN_BG, COLOR_LEN); - } else if (vip.mode == COMMAND) { - abAppend(ab, PEACH_BG, COLOR_LEN); - } - abAppend(ab, BLACK_FG, COLOR_LEN); - abAppend(ab, "\x1b[1m", 4); - abAppend(ab, coord, coord_len); - abAppend(ab, "\x1b[22m", 5); - break; - } else { - abAppend(ab, " ", 1); - file_len++; - } - } - abAppend(ab, "\x1b[m", 3); - abAppend(ab, "\r\n", 2); -} - -void draw_message_bar(struct abuf *ab) -{ - abAppend(ab, "\x1b[K", 3); - int msglen = strlen(vip.statusmsg); - if (msglen > vip.screencols) msglen = vip.screencols; - if (msglen && time(NULL) - vip.statusmsg_time < 5) - abAppend(ab, vip.statusmsg, msglen); -} - -void set_status_bar_message(const char *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - vsnprintf(vip.statusmsg, sizeof(vip.statusmsg), fmt, ap); - va_end(ap); - vip.statusmsg_time = time(NULL); -} diff --git a/src/editor.c b/src/editor.c deleted file mode 100644 index dab4672..0000000 --- a/src/editor.c +++ /dev/null @@ -1,298 +0,0 @@ -#include -#include -#include -#include -#include - -#include "editor.h" -#include "vip.h" -#include "term.h" -#include "io.h" -#include "bar.h" - -extern editor vip; - -void refresh_screen(void) -{ - scroll(); - - struct abuf ab = ABUF_INIT; - - abAppend(&ab, "\x1b[?25l", 6); - abAppend(&ab, "\x1b[H", 3); - - draw_rows(&ab); - draw_status_bar(&ab); - draw_message_bar(&ab); - - char buf[32]; - snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (vip.cy - vip.rowoff) + 1, - (vip.rx - vip.coloff) + 1); - abAppend(&ab, buf, strlen(buf)); - - abAppend(&ab, "\x1b[?25h", 6); - - write(STDOUT_FILENO, ab.b, ab.len); - abFree(&ab); -} - -void move_cursor(int key) -{ - row *row = (vip.cy >= vip.rows) ? NULL : &vip.row[vip.cy]; - switch (key) { - case ARROW_LEFT: - if (vip.cx != 0) { - vip.cx--; - } - break; - case ARROW_RIGHT: - if (row && vip.cx < row->size) { - vip.cx++; - } - break; - case ARROW_UP: - if (vip.cy != 0) { - vip.cy--; - } - break; - case ARROW_DOWN: - if (vip.cy < vip.rows) { - vip.cy++; - } - break; - } - row = (vip.cy >= vip.rows) ? NULL : &vip.row[vip.cy]; - int rowlen = row ? row->size : 0; - if (vip.cx > rowlen) { - vip.cx = rowlen; - } -} - -void scroll(void) -{ - vip.rx = 0; - if (vip.cy < vip.rows) { - vip.rx = row_cx_to_rx(&vip.row[vip.cy], vip.cx); - } - if (vip.cy < vip.rowoff) { - vip.rowoff = vip.cy; - } - if (vip.cy >= vip.rowoff + vip.screenrows) { - vip.rowoff = vip.cy - vip.screenrows + 1; - } - if (vip.rx < vip.coloff) { - vip.coloff = vip.rx; - } - if (vip.rx >= vip.coloff + vip.screencols) { - vip.coloff = vip.rx - vip.screencols + 1; - } -} - -void insert_char(int c) -{ - if (vip.cy == vip.rows) { - insert_row(vip.rows, "", 0); - } - row_insert_char(&vip.row[vip.cy], vip.cx, c); - vip.cx++; -} - -void insert_new_line(void) -{ - if (vip.cx == 0) { - insert_row(vip.cy, "", 0); - } else { - row *row = &vip.row[vip.cy]; - insert_row(vip.cy + 1, &row->chars[vip.cx], row->size - vip.cx); - row = &vip.row[vip.cy]; - row->size = vip.cx; - row->chars[row->size] = '\0'; - update_row(row); - } - vip.cy++; - vip.cx = 0; -} - -/* - * 'o' in vim - */ -void shift_new_line(void) -{ - insert_row(vip.cy + 1, "", 0); - vip.cy++; - vip.cx = 0; -} - -void del_char(void) -{ - if (vip.cy == vip.rows) return; - if (vip.cx == 0 && vip.cy == 0) return; - - row *row = &vip.row[vip.cy]; - if (vip.cx > 0) { - row_del_char(row, vip.cx - 1); - vip.cx--; - } else { - vip.cx = vip.row[vip.cy - 1].size; - row_append_str(&vip.row[vip.cy - 1], row->chars, row->size); - del_row(vip.cy); - vip.cy--; - } -} - -void init_editor(void) -{ - vip.cx = 0; - vip.cy = 0; - vip.rx = 0; - vip.rowoff = 0; - vip.coloff = 0; - vip.rows = 0; - vip.row = NULL; - vip.dirty = 0; - vip.mode = NORMAL; - vip.filename = NULL; - vip.statusmsg[0] = '\0'; - vip.statusmsg_time = 0; - vip.syntax = NULL; - - if (get_window_size(&vip.screenrows, &vip.screencols) == -1) { - die("get_window_size"); - } - vip.screenrows -= 2; -} - -void open_editor(char *filename) -{ - free(vip.filename); - vip.filename = strdup(filename); - - select_syntax_highlight(); - - FILE *fp = fopen(filename, "r"); - if (!fp) { - die("fopen"); - } - char *line = NULL; - size_t linecap = 0; - ssize_t len; - while ((len = getline(&line, &linecap, fp)) != -1) { - /* remove new line and carriage return at end of line */ - while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')) { - len--; - } - insert_row(vip.rows, line, len); - } - free(line); - fclose(fp); - /* reset dirtiness as nothing is modified yet */ - vip.dirty = 0; -} - -char *prompt_editor(char *prompt, void (*callback)(char *, int)) -{ - size_t bufsize = 128; - char *buf = malloc(bufsize); - size_t buflen = 0; - buf[0] = '\0'; - while (1) { - set_status_bar_message(prompt, buf); - refresh_screen(); - int c = read_key(); - if (c == DEL_KEY || c == CTRL_KEY('h') || c == BACKSPACE) { - if (buflen != 0) { - buf[--buflen] = '\0'; - } - } else if (c == '\x1b') { - set_status_bar_message(""); - if (callback) callback(buf, c); - free(buf); - return NULL; - } else if (c == '\r') { - if (buflen != 0) { - set_status_bar_message(""); - if (callback) callback(buf, c); - return buf; - } - } else if (!iscntrl(c) && c < 128) { - if (buflen == bufsize - 1) { - bufsize *= 2; - buf = realloc(buf, bufsize); - } - buf[buflen++] = c; - buf[buflen] = '\0'; - } - - if (callback) callback(buf, c); - } -} - -void find_callback(char *query, int key) -{ - static int last_match = -1; - static int direction = 1; - - static int saved_hl_line; - static char *saved_hl = NULL; - if (saved_hl) { - memcpy(vip.row[saved_hl_line].hl, saved_hl, vip.row[saved_hl_line].render_size); - free(saved_hl); - saved_hl = NULL; - } - - if (key == '\r' || key == '\x1b') { - last_match = -1; - direction = 1; - return; - } else if (key == CTRL_KEY('n')) { - direction = 1; - } else if (key == CTRL_KEY('p')) { - direction = -1; - } else { - last_match = -1; - direction = 1; - } - - if (last_match == -1) direction = 1; - int current = last_match; - for (int i = 0; i < vip.rows; i++) { - current += direction; - - if (current == -1) current = vip.rows - 1; - else if (current == vip.rows) current = 0; - - row *row = &vip.row[current]; - char *match = strstr(row->render, query); - if (match) { - last_match = current; - vip.cy = current; - vip.cx = row_rx_to_cx(row, match - row->render); - vip.rowoff = vip.rows; - - saved_hl_line = current; - saved_hl = malloc(row->render_size); - memcpy(saved_hl, row->hl, row->render_size); - - memset(&row->hl[match - row->render], MATCH, strlen(query)); - memset(&row->hl[match - row->render + strlen(query)], RESET, row->render_size - (match - row->render + strlen(query))); - break; - } - } -} - -void find_editor(void) -{ - int tmp_cx = vip.cx; - int tmp_cy = vip.cy; - int tmp_coloff = vip.coloff; - int tmp_rowoff = vip.rowoff; - char *query = prompt_editor("/%s", find_callback); - if (query) { - free(query); - } else { - vip.cx = tmp_cx; - vip.cy = tmp_cy; - vip.coloff = tmp_coloff; - vip.rowoff = tmp_rowoff; - } -} diff --git a/src/io.c b/src/io.c deleted file mode 100644 index ee65050..0000000 --- a/src/io.c +++ /dev/null @@ -1,317 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "editor.h" -#include "term.h" -#include "vip.h" -#include "bar.h" - -int read_key(void) -{ - int nread; - char c; - while ((nread = read(STDIN_FILENO, &c, 1)) != 1) { - if (nread == -1 && errno != EAGAIN) { - die("read"); - } - } - - if (c == '\x1b') { - char seq[3]; - - if (read(STDIN_FILENO, &seq[0], 1) != 1) - return '\x1b'; - if (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 HOME_KEY; - case '3': return DEL_KEY; - case '4': return END_KEY; - case '5': return PAGE_UP; - case '6': return PAGE_DOWN; - case '7': return HOME_KEY; - case '8': return END_KEY; - } - } - } else { - switch (seq[1]) { - case 'A': return ARROW_UP; - case 'B': return ARROW_DOWN; - case 'C': return ARROW_RIGHT; - case 'D': return ARROW_LEFT; - case 'F': return END_KEY; - case 'H': return HOME_KEY; - } - } - } else if (seq[0] == 'O') { - switch (seq[1]) { - case 'F': return END_KEY; - case 'H': return HOME_KEY; - } - } - return '\x1b'; - } else { - return c; - } -} - -void save_file(void) -{ - if (vip.filename == NULL) { - vip.filename = prompt_editor("Save as: %s", NULL); - if (vip.filename == NULL) { - set_status_bar_message("Save aborted"); - return; - } - select_syntax_highlight(); - } - int len; - char *buf = rows_to_str(&len); - int fd = open(vip.filename, O_RDWR | O_CREAT, 0644); - if (fd != -1) { - if (ftruncate(fd, len) != -1) { - if (write(fd, buf, len) == len) { - close(fd); - free(buf); - vip.dirty = 0; - set_status_bar_message("\"%s\" %dL, %dB written", vip.filename, vip.rows, len); - return; - } - } - close(fd); - } - free(buf); - set_status_bar_message("Error saving: %s", strerror(errno)); -} - -void process_key(void) -{ - int c = read_key(); - switch (c) { - case '\r': - insert_new_line(); - break; - - case HOME_KEY: - vip.cx = 0; - break; - - case END_KEY: - if (vip.cy < vip.rows) { - vip.cx = vip.row[vip.cy].size; - } - break; - - case BACKSPACE: - case CTRL_KEY('h'): - case DEL_KEY: /* PASSTHROUGH */ - if (vip.mode == INSERT) { - if (c == DEL_KEY) - move_cursor(ARROW_RIGHT); - del_char(); - } - break; - - case PAGE_UP: - case PAGE_DOWN: - { - if (c == PAGE_UP) { - vip.cy = vip.rowoff; - } else if (c == PAGE_DOWN) { - vip.cy = vip.rowoff + vip.screenrows - 1; - if (vip.cy > vip.rows) vip.cy = vip.rows; - } - int times = vip.screenrows; - while (times--) - move_cursor(c == PAGE_UP ? ARROW_UP : ARROW_DOWN); - } - break; - - case ARROW_UP: - case ARROW_DOWN: - case ARROW_LEFT: - case ARROW_RIGHT: - if (vip.mode == INSERT) { - move_cursor(c); - } - break; - - case CTRL_KEY('l'): - case '\x1b': - if (vip.mode == INSERT) { - vip.mode = NORMAL; - move_cursor(ARROW_LEFT); - } - break; - - case 'i': /* PASSTHROUGH */ - if (vip.mode == NORMAL) { - vip.mode = INSERT; - break; - } - - case ':': /* PASSTHROUGH */ - if (vip.mode == NORMAL) { - vip.mode = COMMAND; - char *cmd = prompt_editor(":%s", NULL); - if (cmd == NULL) { - vip.mode = NORMAL; - return; - } - switch (cmd[0]) { - case 'q': - if (cmd[1] == '!') { - exit(0); - break; - } else { - if (vip.dirty) { - set_status_bar_message("No write since last change for buffer \"%s\"", vip.filename); - vip.mode = NORMAL; - return; - } - - exit(0); - break; - - } - case 'w': - save_file(); - vip.mode = NORMAL; - } - } - case '/': /* PASSTHROUGH */ - if (vip.mode == NORMAL) { - find_editor(); - break; - } - - case 'k': /* PASSTHROUGH */ - if (vip.mode != INSERT) { - move_cursor(ARROW_UP); - break; - } - - case 'j': /* PASSTHROUGH */ - if (vip.mode != INSERT) { - move_cursor(ARROW_DOWN); - break; - } - - case 'l': /* PASSTHROUGH */ - if (vip.mode != INSERT) { - move_cursor(ARROW_RIGHT); - break; - } - - case 'h': /* PASSTHROUGH */ - if (vip.mode != INSERT) { - move_cursor(ARROW_LEFT); - break; - } - - case 'o': /* PASSTHROUGH */ - if (vip.mode == NORMAL) { - shift_new_line(); - vip.mode = INSERT; - break; - } - - case '0': /* PASSTHROUGH */ - if (vip.mode == NORMAL) { - vip.cx = 0; - break; - } - - case '$': /* PASSTHROUGH */ - if (vip.mode == NORMAL) { - vip.cx = vip.row[vip.cy].size; - break; - } - - default: - if (vip.mode == INSERT) { - insert_char(c); - } - break; - } -} - -void draw_rows(struct abuf *ab) -{ - for (int y = 0; y < vip.screenrows; y++) { - int filerow = y + vip.rowoff; - if (filerow >= vip.rows) { - if (vip.rows == 0 && y == vip.screenrows / 3) { - char welcome[11]; - int welcomelen = snprintf(welcome, sizeof(welcome), - "VIP v%s", VERSION); - if (welcomelen > vip.screencols) - welcomelen = vip.screencols; - int padding = (vip.screencols - welcomelen) / 2; - if (padding) { - abAppend(ab, "~", 1); - padding--; - } - while (padding--) abAppend(ab, " ", 1); - abAppend(ab, welcome, welcomelen); - } else { - abAppend(ab, "~", 1); - } - } else { - int len = vip.row[filerow].render_size - vip.coloff; - if (len < 0) len = 0; - if (len > vip.screencols) len = vip.screencols; - char *c = &vip.row[filerow].render[vip.coloff]; - unsigned char *hl = &vip.row[filerow].hl[vip.coloff]; - - char *current_color = malloc(COLOR_LEN * 2); - int current_color_len = 0; - for (int j = 0; j < len; j++) { - if (iscntrl(c[j])) { - char sym = (c[j] <= 26) ? '@' + c[j] : '?'; - abAppend(ab, "\x1b[7m", 4); - abAppend(ab, &sym, 1); - abAppend(ab, "\x1b[m", 3); - if (strncmp(current_color, WHITE_BG, COLOR_LEN)) { - char *buf = malloc(COLOR_LEN * 2); - memcpy(buf, current_color, current_color_len); - abAppend(ab, buf, current_color_len); - free(buf); - } - } else if (hl[j] == NORMAL) { - if (strncmp(current_color, WHITE_BG, COLOR_LEN)) { - memcpy(current_color, WHITE_BG, COLOR_LEN); - current_color_len = COLOR_LEN; - abAppend(ab, WHITE_BG, COLOR_LEN); - } - abAppend(ab, &c[j], 1); - } else { - size_t len; - char *color = syntax_to_color(hl[j], &len); - if (strncmp(current_color, color, len)) { - memcpy(current_color, color, len); - current_color_len = len; - abAppend(ab, color, len); - } - free(color); - abAppend(ab, &c[j], 1); - } - } - free(current_color); - abAppend(ab, WHITE_BG, COLOR_LEN); - } - - abAppend(ab, "\x1b[K", 3); - abAppend(ab, "\r\n", 2); - } -} diff --git a/src/row.c b/src/row.c deleted file mode 100644 index 7bf9c2c..0000000 --- a/src/row.c +++ /dev/null @@ -1,156 +0,0 @@ -#include -#include -#include - -#include "vip.h" -#include "row.h" -#include "syntax.h" - -extern editor vip; - -int row_cx_to_rx(row *row, int cx) -{ - int rx = 0; - for (int j = 0; j < cx; j++) { - if (row->chars[j] == '\t') { - rx += (TAB_SIZE - 1) - (rx % TAB_SIZE); - } - rx++; - } - return rx; -} - -int row_rx_to_cx(row *row, int rx) -{ - int cur_rx = 0; - int cx; - for (cx = 0; cx < row->size; cx++) { - if (row->chars[cx] == '\t') { - cur_rx += (TAB_SIZE - 1) - (cur_rx % TAB_SIZE); - } - cur_rx++; - if (cur_rx > rx) - return cx; - } - return cx; -} - -void update_row(row *row) -{ - int tabs = 0; - for (int j = 0; j < row->size; j++) - if (row->chars[j] == '\t') tabs++; - free(row->render); - row->render = malloc(row->size + tabs * (TAB_SIZE - 1) + 1); - int idx = 0; - for (int j = 0; j < row->size; j++) { - if (row->chars[j] == '\t') { - row->render[idx++] = ' '; - while (idx % TAB_SIZE != 0) { - row->render[idx++] = ' '; - } - } - else { - row->render[idx++] = row->chars[j]; - } - } - row->render[idx] = '\0'; - row->render_size = idx; - update_highlight(row); -} - -void insert_row(int at, char *s, size_t len) -{ - if (at < 0 || at > vip.rows) return; - - vip.row = realloc(vip.row, sizeof(row) * (vip.rows + 1)); - memmove(&vip.row[at + 1], &vip.row[at], sizeof(row) * (vip.rows - at)); - for (int j = at + 1; j <= vip.rows; j++) { - vip.row[j].idx++; - } - - vip.row[at].idx = at; - - vip.row[at].size = len; - vip.row[at].chars = malloc(len + 1); - memcpy(vip.row[at].chars, s, len); - vip.row[at].chars[len] = '\0'; - - vip.row[at].render_size = 0; - vip.row[at].render = NULL; - vip.row[at].hl = NULL; - vip.row[at].opened_comment = 0; - update_row(&vip.row[at]); - - vip.rows++; - vip.dirty++; -} - -void free_row(row *row) -{ - free(row->render); - free(row->chars); - free(row->hl); -} - -void del_row(int at) -{ - if (at < 0 || at >= vip.rows) return; - free_row(&vip.row[at]); - memmove(&vip.row[at], &vip.row[at + 1], sizeof(row) * (vip.rows - at - 1)); - for (int j = at; j < vip.rows - 1; j++) { - vip.row[j].idx--; - } - vip.rows--; - vip.dirty++; -} - -void row_insert_char(row *row, int at, int c) -{ - if (at < 0 || at > row->size) { - at = row->size; - } - row->chars = realloc(row->chars, row->size + 2); - memmove(&row->chars[at + 1], &row->chars[at], row->size - at + 1); - row->size++; - row->chars[at] = c; - update_row(row); - vip.dirty++; -} - -void row_append_str(row *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'; - update_row(row); - vip.dirty++; -} - -void row_del_char(row *row, int at) -{ - if (at < 0 || at >= row->size) return; - memmove(&row->chars[at], &row->chars[at + 1], row->size - at); - row->size--; - update_row(row); - vip.dirty++; -} - -char *rows_to_str(int *buflen) -{ - int total_len = 0; - for (int j = 0; j < vip.rows; j++) { - total_len += vip.row[j].size + 1; - } - *buflen = total_len; - char *buf = malloc(total_len); - char *p = buf; - for (int j = 0; j < vip.rows; j++) { - memcpy(p, vip.row[j].chars, vip.row[j].size); - p += vip.row[j].size; - *p = '\n'; - p++; - } - return buf; -} diff --git a/src/syntax.c b/src/syntax.c deleted file mode 100644 index 642ea21..0000000 --- a/src/syntax.c +++ /dev/null @@ -1,211 +0,0 @@ -#include -#include -#include -#include - -#include "vip.h" -#include "row.h" -#include "syntax.h" - -char *c_keywords[] = { "switch", "if", "while", "for", "break", "continue", "return", "else", "struct", "union", "typedef", "static", "enum", "case", "sizeof", "int|", "long|", "double|", "float|", "char|", "unsigned|", "void|", NULL }; -char *c_ext[] = { ".c", ".h", ".cpp", NULL }; - -language langs[] = { - { - "c", - HL_NUMBERS | HL_STRINGS, - "//", - "/*", - "*/", - c_keywords, - c_ext, - }, -}; - -#define LANGS_LEN (sizeof(langs) / sizeof(langs[0])) - -int is_separator(int c) -{ - return isspace(c) || c == '\0' || strchr(",.()+-/*=~%<>[];", c) != NULL; -} - -void update_highlight(row *row) -{ - row->hl = realloc(row->hl, row->render_size); - memset(row->hl, NORMAL, row->render_size); - - if (vip.syntax == NULL) return; - - char **keywords = vip.syntax->keywords; - - char *scs = vip.syntax->singleline_comment_start; - char *mcs = vip.syntax->multiline_comment_start; - char *mce = vip.syntax->multiline_comment_end; - - int scs_len = scs ? strlen(scs) : 0; - int mcs_len = mcs ? strlen(mcs) : 0; - int mce_len = mce ? strlen(mce) : 0; - - int prev_sep = 1; - int in_string = 0; - int in_comment = (row->idx > 0 && vip.row[row->idx - 1].opened_comment); - - int i = 0; - while (i < row->render_size) { - char c = row->render[i]; - unsigned char prev_hl = (i > 0) ? row->hl[i - 1] : NORMAL; - - if (scs_len && !in_string && !in_comment) { - if (!strncmp(&row->render[i], scs, scs_len)) { - memset(&row->hl[i], COMMENT, row->render_size - i); - break; - } - } - - if (mcs_len && mce_len && !in_string) { - if (in_comment) { - row->hl[i] = MLCOMMENT; - if (!strncmp(&row->render[i], mce, mce_len)) { - memset(&row->hl[i], MLCOMMENT, mce_len); - i += mce_len; - in_comment = 0; - prev_sep = 1; - continue; - } else { - i++; - continue; - } - } else if (!strncmp(&row->render[i], mcs, mcs_len)) { - memset(&row->hl[i], MLCOMMENT, mcs_len); - i += mcs_len; - in_comment = 1; - continue; - } - } - - if (vip.syntax->flags & HL_STRINGS) { - if (in_string) { - row->hl[i] = STRING; - if (c == '\\' && i + 1 < row->render_size) { - row->hl[i + 1] = STRING; - i += 2; - continue; - } - if (c == in_string) in_string = 0; - i++; - prev_sep = 1; - continue; - } else { - if (c == '"') { - in_string = c; - row->hl[i] = STRING; - i++; - continue; - } - } - } - - if (vip.syntax->flags & HL_NUMBERS) { - if ((isdigit(c) && (prev_sep || prev_hl == NUMBER)) || - (c == '.' && prev_hl == NUMBER)) { - row->hl[i] = NUMBER; - i++; - prev_sep = 0; - continue; - } - } - - if (prev_sep) { - int j; - for (j = 0; keywords[j]; j++) { - int klen = strlen(keywords[j]); - int type_keyword = keywords[j][klen - 1] == '|'; - if (type_keyword) klen--; - if (!strncmp(&row->render[i], keywords[j], klen) && - is_separator(row->render[i + klen])) { - memset(&row->hl[i], type_keyword ? KEYWORD2 : KEYWORD1, klen); - i += klen; - break; - } - } - if (keywords[j] != NULL) { - prev_sep = 0; - continue; - } - } - - prev_sep = is_separator(c); - i++; - } - int changed = (row->opened_comment != in_comment); - row->opened_comment = in_comment; - if (changed && row->idx + 1 < vip.rows) - update_highlight(&vip.row[row->idx + 1]); -} - -char *syntax_to_color(int hl, size_t *len) -{ - switch (hl) { - case NUMBER: - *len = COLOR_LEN; - return strdup(PEACH_BG); - - case STRING: - *len = COLOR_LEN; - return strdup(GREEN_BG); - - case COMMENT: - case MLCOMMENT: - *len = COLOR_LEN; - return strdup(OVERLAY_0_BG); - - case KEYWORD1: - *len = COLOR_LEN; - return strdup(MAUVE_BG); - - case KEYWORD2: - *len = COLOR_LEN; - return strdup(YELLOW_BG); - - case MATCH:; - char *str = malloc(COLOR_LEN * 2 + 1); - snprintf(str, COLOR_LEN * 2 + 1, "%s%s", BLACK_BG, SKY_FG); - *len = COLOR_LEN * 2; - return str; - - case RESET:; - char *res = malloc(COLOR_LEN * 2 + 1); - snprintf(res, COLOR_LEN * 2 + 1, "%s%s", WHITE_BG, BLACK_FG); - *len = COLOR_LEN * 2; - return res; - - default: - *len = COLOR_LEN; - return strdup(WHITE_BG); - } -} - -void select_syntax_highlight(void) -{ - vip.syntax = NULL; - if (vip.filename == NULL) return; - char *ext = strrchr(vip.filename, '.'); - for (unsigned int j = 0; j < LANGS_LEN; j++) { - language *s = &langs[j]; - unsigned int i = 0; - while (s->extensions[i]) { - int is_ext = (s->extensions[i][0] == '.'); - if ((is_ext && ext && !strcmp(ext, s->extensions[i])) || - (!is_ext && strstr(vip.filename, s->extensions[i]))) { - vip.syntax = s; - - for (int row = 0; row < vip.rows; row++) { - update_highlight(&vip.row[row]); - } - return; - } - i++; - } - } -} - diff --git a/src/term.c b/src/term.c deleted file mode 100644 index 48f8e30..0000000 --- a/src/term.c +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include -#include -#include -#include - -#include "vip.h" - -extern editor vip; - -void die(const char *s) -{ - write(STDOUT_FILENO, "\x1b[2J\x1b[H", 7); - perror(s); - exit(1); -} - -void reset_term(void) -{ - if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &vip.termios) == -1) { - die("tcsetattr"); - } - write(STDOUT_FILENO, "\x1b[2J\x1b[?1049l", 12); -} - -/* - * Setup terminal - */ -void setup_term(void) -{ - write(STDOUT_FILENO, "\x1b[?1049h\x1b[2J", 12); - if (tcgetattr(STDIN_FILENO, &vip.termios) == -1) { - die("tcgetattr"); - } - atexit(reset_term); - - struct termios raw = vip.termios; - /* disbable echo, line output and signals */ - raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); - raw.c_oflag &= ~(OPOST); - raw.c_cflag |= (CS8); - raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); - raw.c_cc[VMIN] = 0; - raw.c_cc[VTIME] = 1; - if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) { - die("tcsetattr"); - } -} - -int get_cursor_position(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 get_window_size(int *rows, int *cols) -{ - struct winsize ws; - if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) { - /* Can't get window size */ - if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12) { - return -1; - } - return get_cursor_position(rows, cols); - } else { - *cols = ws.ws_col; - *rows = ws.ws_row; - return 0; - } -} diff --git a/src/vip.c b/src/vip.c deleted file mode 100644 index aca187b..0000000 --- a/src/vip.c +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "vip.h" -#include "term.h" -#include "editor.h" -#include "bar.h" -#include "io.h" - -editor vip; - -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); -} - -int main(int argc, char **argv) -{ - setup_term(); - init_editor(); - if (argc >= 2) { - open_editor(argv[1]); - } - - set_status_bar_message("By night0721 and gnucolas"); - - while (1) { - refresh_screen(); - process_key(); - } - return 0; -}