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 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"
@ -56,15 +52,12 @@ enum highlight {
DEFAULT = 0,
SYMBOL,
COMMENT,
TERMINATOR,
MLCOMMENT,
KW,
KW_TYPE,
KW_FN,
KW_BRACKET,
STRING,
CHAR,
ESCAPE,
NUMBER,
MATCH,
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 },
},
};

188
vip.c
View file

@ -17,7 +17,6 @@
#include "config.h"
int cat_mode = 0;
struct termios newt, oldt;
int rows, cols;
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 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);
@ -171,20 +168,17 @@ void refresh_screen(void)
if (cur_editor->rx < cur_editor->coloff) {
cur_editor->coloff = cur_editor->rx;
}
if (!cat_mode) {
if (cur_editor->rx >= cur_editor->coloff + cols) {
cur_editor->coloff = cur_editor->rx - cols + 1;
}
bprintf("\033H\033[2 q");
if (cur_editor->rx >= cur_editor->coloff + cols) {
cur_editor->coloff = cur_editor->rx - cols + 1;
}
bprintf("\033H\033[2 q");
draw_rows();
if (!cat_mode) {
draw_status_bar();
move_cursor((cur_editor->y - cur_editor->rowoff) + 1,
(cur_editor->rx - cur_editor->coloff) + 1);
}
draw_status_bar();
move_cursor((cur_editor->y - cur_editor->rowoff) + 1,
(cur_editor->rx - cur_editor->coloff) + 1);
}
void move_xy(int key)
@ -564,9 +558,8 @@ void save_file(void)
void draw_rows(void)
{
for (int y = 0; y < cur_editor->rows; y++) {
if (!cat_mode && y > rows - 2) return;
if (!cat_mode) move_cursor(y + 1, 1);
for (int y = 0; y < rows - 1; y++) {
move_cursor(y + 1, 1);
int filerow = y + cur_editor->rowoff;
if (filerow >= cur_editor->rows) {
if (cur_editor->rows == 0 && y == rows / 2) {
@ -581,7 +574,7 @@ void draw_rows(void)
} else {
int len = cur_editor->row[filerow].render_size - cur_editor->coloff;
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];
unsigned char *hl = &cur_editor->row[filerow].hl[cur_editor->coloff];
@ -600,22 +593,10 @@ 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");
@ -660,7 +641,7 @@ void draw_rows(void)
bprintf(WHITE_BG);
}
bprintf("\033[K\n");
bprintf("\033[K");
}
}
@ -748,18 +729,6 @@ 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;
@ -767,7 +736,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)
@ -789,9 +758,7 @@ 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;
@ -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) {
row->hl[i] = STRING;
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) {
row->hl[i] = STRING;
if (c == '>') in_include = 0;
@ -926,13 +855,6 @@ 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++) {
@ -953,11 +875,13 @@ 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_len++;
i++;
word[word_len++] = row->render[i++];
}
word[word_len] = '\0';
if (row->render[i] == '(') {
memset(&row->hl[i - word_len], KW_FN, word_len);
prev_sep = 1;
@ -1000,6 +924,7 @@ void select_syntax(void)
void die(const char *s)
{
cleanup();
perror(s);
exit(1);
}
@ -1015,49 +940,37 @@ void handle_sigwinch(int ignore)
int main(int argc, char **argv)
{
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");
}
if (get_window_size(&rows, &cols) == -1) {
die("get_window_size");
}
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 */
if (argv[i][0] != '-') {
init_editor(i == 0 ? NULL : argv[i]);
}
}
if (cat_mode) {
refresh_screen();
return 0;
init_editor(i == 0 ? NULL : argv[i]);
}
while (1) {
@ -1260,10 +1173,7 @@ int main(int argc, char **argv)
} else if (pid > 0) {
/* Parent process */
waitpid(pid, NULL, 0);
char fpath[PATH_MAX];
strcpy(fpath, "~/.cache/ccc/opened_file");
replace_home(fpath);
FILE *f = fopen(fpath, "r");
FILE *f = fopen("/home/night/.cache/ccc/opened_file", "r");
char opened_file[PATH_MAX];
fread(opened_file, sizeof(char), PATH_MAX, f);
opened_file[strcspn(opened_file, "\n")] = 0;
@ -1370,9 +1280,5 @@ void bprintf(const char *fmt, ...)
vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
if (cat_mode) {
printf("%s", buffer);
} else {
write(STDOUT_FILENO, buffer, strlen(buffer));
}
write(STDOUT_FILENO, buffer, strlen(buffer));
}