Compare commits

..

No commits in common. "ef4e93abb97fc76d3478c346b44c82ba8db1a4b9" and "a3147570114ca252975ce9b0175ddc98509e1512" have entirely different histories.

2 changed files with 49 additions and 150 deletions

View file

@ -24,10 +24,6 @@
#define YELLOW_BG "\033[38;2;249;226;175m" #define YELLOW_BG "\033[38;2;249;226;175m"
#define RED_FG "\033[48;2;243;139;168m" #define RED_FG "\033[48;2;243;139;168m"
#define RED_BG "\033[38;2;243;139;168m" #define RED_BG "\033[38;2;243;139;168m"
#define TEAL_FG "\033[48;2;148;226;213m"
#define TEAL_BG "\033[38;2;148;226;213m"
#define PINK_FG "\033[48;2;245;194;231m"
#define PINK_BG "\033[38;2;245;194;231m"
/* ERROR is red with bold and italic */ /* ERROR is red with bold and italic */
#define ERROR "\033[38;2;243;139;168m\033[1m\033[3m" #define ERROR "\033[38;2;243;139;168m\033[1m\033[3m"
@ -56,15 +52,12 @@ enum highlight {
DEFAULT = 0, DEFAULT = 0,
SYMBOL, SYMBOL,
COMMENT, COMMENT,
TERMINATOR,
MLCOMMENT, MLCOMMENT,
KW, KW,
KW_TYPE, KW_TYPE,
KW_FN, KW_FN,
KW_BRACKET, KW_BRACKET,
STRING, STRING,
CHAR,
ESCAPE,
NUMBER, NUMBER,
MATCH, MATCH,
RESET RESET
@ -112,7 +105,7 @@ language_t langs[] = {
"//", "//",
"/*", "/*",
"*/", "*/",
{ "const", "switch", "if", "while", "for", "break", "continue", "return", "else", "struct", "union", "typedef", "static", "enum", "case", "sizeof", "#include", "#define", "#if", "#elseif", "#endif", "int|", "long|", "double|", "float|", "char|", "unsigned|", "void|", "size_t|", "uint8_t|", NULL }, { "switch", "if", "while", "for", "break", "continue", "return", "else", "struct", "union", "typedef", "static", "enum", "case", "sizeof", "#include", "#define", "#if", "#elseif", "#endif", "int|", "long|", "double|", "float|", "char|", "unsigned|", "void|", "size_t|", "uint8_t|", NULL },
{ ".c", ".h", ".cpp", NULL }, { ".c", ".h", ".cpp", NULL },
}, },
}; };

130
vip.c
View file

@ -17,7 +17,6 @@
#include "config.h" #include "config.h"
int cat_mode = 0;
struct termios newt, oldt; struct termios newt, oldt;
int rows, cols; int rows, cols;
editor_t editor[9]; editor_t editor[9];
@ -42,8 +41,6 @@ void update_row(row_t *row);
void insert_row(int at, char *s, size_t len); void insert_row(int at, char *s, size_t len);
void del_row(int at); void del_row(int at);
char *export_buffer(int *buflen); char *export_buffer(int *buflen);
void replace_home(char *path);
int is_symbol(int c);
int is_separator(int c); int is_separator(int c);
void update_highlight(row_t *row); void update_highlight(row_t *row);
void select_syntax(void); void select_syntax(void);
@ -171,21 +168,18 @@ void refresh_screen(void)
if (cur_editor->rx < cur_editor->coloff) { if (cur_editor->rx < cur_editor->coloff) {
cur_editor->coloff = cur_editor->rx; cur_editor->coloff = cur_editor->rx;
} }
if (!cat_mode) {
if (cur_editor->rx >= cur_editor->coloff + cols) { if (cur_editor->rx >= cur_editor->coloff + cols) {
cur_editor->coloff = cur_editor->rx - cols + 1; cur_editor->coloff = cur_editor->rx - cols + 1;
} }
bprintf("\033H\033[2 q"); bprintf("\033H\033[2 q");
}
draw_rows(); draw_rows();
if (!cat_mode) {
draw_status_bar(); draw_status_bar();
move_cursor((cur_editor->y - cur_editor->rowoff) + 1, move_cursor((cur_editor->y - cur_editor->rowoff) + 1,
(cur_editor->rx - cur_editor->coloff) + 1); (cur_editor->rx - cur_editor->coloff) + 1);
} }
}
void move_xy(int key) void move_xy(int key)
{ {
@ -564,9 +558,8 @@ void save_file(void)
void draw_rows(void) void draw_rows(void)
{ {
for (int y = 0; y < cur_editor->rows; y++) { for (int y = 0; y < rows - 1; y++) {
if (!cat_mode && y > rows - 2) return; move_cursor(y + 1, 1);
if (!cat_mode) move_cursor(y + 1, 1);
int filerow = y + cur_editor->rowoff; int filerow = y + cur_editor->rowoff;
if (filerow >= cur_editor->rows) { if (filerow >= cur_editor->rows) {
if (cur_editor->rows == 0 && y == rows / 2) { if (cur_editor->rows == 0 && y == rows / 2) {
@ -581,7 +574,7 @@ void draw_rows(void)
} else { } else {
int len = cur_editor->row[filerow].render_size - cur_editor->coloff; int len = cur_editor->row[filerow].render_size - cur_editor->coloff;
if (len < 0) len = 0; if (len < 0) len = 0;
if (!cat_mode && len > cols) len = cols; if (len > cols) len = cols;
char *c = &cur_editor->row[filerow].render[cur_editor->coloff]; char *c = &cur_editor->row[filerow].render[cur_editor->coloff];
unsigned char *hl = &cur_editor->row[filerow].hl[cur_editor->coloff]; unsigned char *hl = &cur_editor->row[filerow].hl[cur_editor->coloff];
@ -600,22 +593,10 @@ void draw_rows(void)
bprintf(GREEN_BG); bprintf(GREEN_BG);
break; break;
case CHAR:
bprintf(TEAL_BG);
break;
case ESCAPE:
bprintf(PINK_BG);
break;
case SYMBOL: case SYMBOL:
bprintf(SKY_BG); bprintf(SKY_BG);
break; break;
case TERMINATOR:
bprintf(OVERLAY_2_BG);
break;
case COMMENT: case COMMENT:
case MLCOMMENT: case MLCOMMENT:
bprintf("\033[3m"); bprintf("\033[3m");
@ -660,7 +641,7 @@ void draw_rows(void)
bprintf(WHITE_BG); bprintf(WHITE_BG);
} }
bprintf("\033[K\n"); bprintf("\033[K");
} }
} }
@ -748,18 +729,6 @@ char *export_buffer(int *buflen)
return buf; return buf;
} }
void replace_home(char *path)
{
char *home = getenv("HOME");
if (home == NULL) {
wpprintw("$HOME not defined");
return;
}
/* replace ~ with home */
snprintf(path, strlen(path) + strlen(home), "%s%s", home, path + 1);
return;
}
int is_separator(int c) int is_separator(int c)
{ {
return isspace(c) || c == '\0' || strchr(",.()+-/*=~%<>[];", c) != NULL; return isspace(c) || c == '\0' || strchr(",.()+-/*=~%<>[];", c) != NULL;
@ -767,7 +736,7 @@ int is_separator(int c)
int is_symbol(int c) int is_symbol(int c)
{ {
return strchr("+-/*=~%><:?&|.", c) != NULL; return strchr("+-/*=~%><:?", c) != NULL;
} }
void update_highlight(row_t *row) void update_highlight(row_t *row)
@ -789,9 +758,7 @@ void update_highlight(row_t *row)
int prev_sep = 1; int prev_sep = 1;
int in_string = 0; int in_string = 0;
int in_char = 0;
int in_include = 0; int in_include = 0;
int in_escape = 0;
int in_comment = row->idx > 0 && cur_editor->row[row->idx - 1].opened_comment; int in_comment = row->idx > 0 && cur_editor->row[row->idx - 1].opened_comment;
int i = 0; int i = 0;
@ -827,24 +794,6 @@ void update_highlight(row_t *row)
} }
} }
if (in_escape) {
if ((c > 47 && c < 58) || c == 'n' || c == 't' || c == 'r') {
row->hl[i] = ESCAPE;
i++;
prev_sep = 0;
continue;
} else {
in_escape = 0;
}
} else {
if (c == '\\') {
in_escape = 1;
row->hl[i] = ESCAPE;
i++;
continue;
}
}
if (in_string) { if (in_string) {
row->hl[i] = STRING; row->hl[i] = STRING;
if (c == '\\' && i + 1 < row->render_size) { if (c == '\\' && i + 1 < row->render_size) {
@ -865,26 +814,6 @@ void update_highlight(row_t *row)
} }
} }
if (in_char) {
row->hl[i] = CHAR;
if (c == '\\' && i + 1 < row->render_size) {
row->hl[i + 1] = CHAR;
i += 2;
continue;
}
if (c == in_char) in_char = 0;
i++;
prev_sep = 1;
continue;
} else {
if (c == '\'') {
in_char = c;
row->hl[i] = CHAR;
i++;
continue;
}
}
if (in_include) { if (in_include) {
row->hl[i] = STRING; row->hl[i] = STRING;
if (c == '>') in_include = 0; if (c == '>') in_include = 0;
@ -926,13 +855,6 @@ void update_highlight(row_t *row)
continue; continue;
} }
if (c == ';' || c == ',') {
row->hl[i] = TERMINATOR;
prev_sep = 1;
i++;
continue;
}
if (prev_sep) { if (prev_sep) {
int j; int j;
for (j = 0; keywords[j]; j++) { for (j = 0; keywords[j]; j++) {
@ -953,11 +875,13 @@ void update_highlight(row_t *row)
continue; continue;
} }
/* Check for function */ /* Check for function */
/* Assume maximum function name is 128 characters */
char word[128];
int word_len = 0; int word_len = 0;
while (!is_separator(row->render[i])) { while (!is_separator(row->render[i])) {
word_len++; word[word_len++] = row->render[i++];
i++;
} }
word[word_len] = '\0';
if (row->render[i] == '(') { if (row->render[i] == '(') {
memset(&row->hl[i - word_len], KW_FN, word_len); memset(&row->hl[i - word_len], KW_FN, word_len);
prev_sep = 1; prev_sep = 1;
@ -1000,6 +924,7 @@ void select_syntax(void)
void die(const char *s) void die(const char *s)
{ {
cleanup();
perror(s); perror(s);
exit(1); exit(1);
} }
@ -1015,9 +940,10 @@ void handle_sigwinch(int ignore)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if (argc > 2 && !strcmp(argv[1], "-c")) { if (get_window_size(&rows, &cols) == -1) {
cat_mode = 1; die("get_window_size");
} else { }
struct sigaction sa; struct sigaction sa;
sa.sa_handler = handle_sigwinch; sa.sa_handler = handle_sigwinch;
sa.sa_flags = SA_RESTART; sa.sa_flags = SA_RESTART;
@ -1028,14 +954,10 @@ int main(int argc, char **argv)
exit(1); exit(1);
} }
bprintf("\033[?1049h\033[2J\033[2q");
if (tcgetattr(STDIN_FILENO, &oldt) == -1) { if (tcgetattr(STDIN_FILENO, &oldt) == -1) {
die("tcgetattr"); die("tcgetattr");
} }
if (get_window_size(&rows, &cols) == -1) {
die("get_window_size");
}
bprintf("\033[?1049h\033[2J\033[2q");
newt = oldt; newt = oldt;
/* Disable canonical mode and echo */ /* Disable canonical mode and echo */
newt.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); newt.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
@ -1046,19 +968,10 @@ int main(int argc, char **argv)
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &newt) == -1) { if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &newt) == -1) {
die("tcsetattr"); die("tcsetattr");
} }
}
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
/* Create tabs for each arg */ /* Create tabs for each arg */
if (argv[i][0] != '-') {
init_editor(i == 0 ? NULL : argv[i]); init_editor(i == 0 ? NULL : argv[i]);
} }
}
if (cat_mode) {
refresh_screen();
return 0;
}
while (1) { while (1) {
refresh_screen(); refresh_screen();
@ -1260,10 +1173,7 @@ int main(int argc, char **argv)
} else if (pid > 0) { } else if (pid > 0) {
/* Parent process */ /* Parent process */
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
char fpath[PATH_MAX]; FILE *f = fopen("/home/night/.cache/ccc/opened_file", "r");
strcpy(fpath, "~/.cache/ccc/opened_file");
replace_home(fpath);
FILE *f = fopen(fpath, "r");
char opened_file[PATH_MAX]; char opened_file[PATH_MAX];
fread(opened_file, sizeof(char), PATH_MAX, f); fread(opened_file, sizeof(char), PATH_MAX, f);
opened_file[strcspn(opened_file, "\n")] = 0; opened_file[strcspn(opened_file, "\n")] = 0;
@ -1370,9 +1280,5 @@ void bprintf(const char *fmt, ...)
vsnprintf(buffer, sizeof(buffer), fmt, args); vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args); va_end(args);
if (cat_mode) {
printf("%s", buffer);
} else {
write(STDOUT_FILENO, buffer, strlen(buffer)); write(STDOUT_FILENO, buffer, strlen(buffer));
} }
}