Compare commits

...

11 commits

2 changed files with 150 additions and 49 deletions

View file

@ -24,6 +24,10 @@
#define YELLOW_BG "\033[38;2;249;226;175m"
#define RED_FG "\033[48;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 */
#define ERROR "\033[38;2;243;139;168m\033[1m\033[3m"
@ -52,12 +56,15 @@ enum highlight {
DEFAULT = 0,
SYMBOL,
COMMENT,
TERMINATOR,
MLCOMMENT,
KW,
KW_TYPE,
KW_FN,
KW_BRACKET,
STRING,
CHAR,
ESCAPE,
NUMBER,
MATCH,
RESET
@ -105,7 +112,7 @@ language_t langs[] = {
"//",
"/*",
"*/",
{ "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 },
{ "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 },
{ ".c", ".h", ".cpp", NULL },
},
};

190
vip.c
View file

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