Add more keywords, and change filename to stack, only allow 9 tabs

This commit is contained in:
Night Kaly 2024-11-11 11:46:39 +00:00
parent 8a6dd74ac0
commit 8a49a9b255
Signed by: night0721
SSH key fingerprint: SHA256:B/hgVwUoBpx5vdNsXl9w8XwZljA9766uk6T4ubZp5HM
2 changed files with 29 additions and 19 deletions

View file

@ -87,7 +87,8 @@ typedef struct {
row_t *row; row_t *row;
int dirty; int dirty;
int mode; int mode;
char *filename; char filename[PATH_MAX];
char cwd[PATH_MAX];
language_t *syntax; language_t *syntax;
} editor_t; } editor_t;
@ -101,7 +102,7 @@ language_t langs[] = {
"//", "//",
"/*", "/*",
"*/", "*/",
{ "switch", "if", "while", "for", "break", "continue", "return", "else", "struct", "union", "typedef", "static", "enum", "case", "sizeof", "#include", "int|", "long|", "double|", "float|", "char|", "unsigned|", "void|", 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|", NULL },
{ ".c", ".h", ".cpp", NULL }, { ".c", ".h", ".cpp", NULL },
}, },
}; };

43
vip.c
View file

@ -9,6 +9,7 @@
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <libgen.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/types.h> #include <sys/types.h>
@ -20,6 +21,7 @@ struct termios newt, oldt;
int screenrows, screencols; int screenrows, screencols;
editor_t editor[9]; editor_t editor[9];
int editor_idx = 0; int editor_idx = 0;
int editors = 0;
editor_t *cur_editor; editor_t *cur_editor;
void draw_status_bar(void); void draw_status_bar(void);
@ -87,8 +89,8 @@ void draw_status_bar(void)
char git[80], file[80], info[80], lines[80], coord[80]; char git[80], file[80], info[80], lines[80], coord[80];
int git_len = snprintf(git, sizeof(git), " %s | %s ", "master", "+1"); int git_len = snprintf(git, sizeof(git), " %s | %s ", "master", "+1");
int file_len = snprintf(file, sizeof(file), " %.20s %s", int file_len = snprintf(file, sizeof(file), " %s %s",
cur_editor->filename ? cur_editor->filename : "[No Name]", cur_editor->dirty ? "[+]" : ""); strlen(cur_editor->filename) > 0 ? cur_editor->filename : "[No Name]", cur_editor->dirty ? "[+]" : "");
int info_len = snprintf(info, sizeof(info), " %s ", cur_editor->syntax ? cur_editor->syntax->filetype : ""); int info_len = snprintf(info, sizeof(info), " %s ", cur_editor->syntax ? cur_editor->syntax->filetype : "");
int lines_len; int lines_len;
if (cur_editor->rows == 0 || cur_editor->rows == cur_editor->y + 1) { if (cur_editor->rows == 0 || cur_editor->rows == cur_editor->y + 1) {
@ -301,7 +303,12 @@ void del_char(void)
void init_editor(char *filename) void init_editor(char *filename)
{ {
cur_editor = &editor[editor_idx]; if (editors > 8) {
wpprintw("\033[38;2;255;0;0m\033[1mOnly 9 tabs are allowed");
readch();
return;
}
cur_editor = &editor[editors++];
cur_editor->x = 0; cur_editor->x = 0;
cur_editor->y = 0; cur_editor->y = 0;
cur_editor->rx = 0; cur_editor->rx = 0;
@ -311,18 +318,17 @@ void init_editor(char *filename)
cur_editor->row = NULL; cur_editor->row = NULL;
cur_editor->dirty = 0; cur_editor->dirty = 0;
cur_editor->mode = NORMAL; cur_editor->mode = NORMAL;
cur_editor->filename = NULL;
cur_editor->syntax = NULL; cur_editor->syntax = NULL;
char cwd[PATH_MAX];
getcwd(cwd, PATH_MAX);
strcpy(cur_editor->cwd, cwd);
if (get_window_size(&screenrows, &screencols) == -1) { if (get_window_size(&screenrows, &screencols) == -1) {
die("get_window_size"); die("get_window_size");
} }
if (cur_editor->filename) {
free(cur_editor->filename);
}
if (filename) { if (filename) {
cur_editor->filename = strdup(filename); strcpy(cur_editor->filename, basename(filename));
FILE *fp = fopen(filename, "r"); FILE *fp = fopen(filename, "r");
if (!fp) { if (!fp) {
@ -345,6 +351,8 @@ void init_editor(char *filename)
fclose(fp); fclose(fp);
/* Reset dirtiness as nothing is modified yet */ /* Reset dirtiness as nothing is modified yet */
cur_editor->dirty = 0; cur_editor->dirty = 0;
} else {
strcpy(cur_editor->filename, "");
} }
} }
@ -526,12 +534,13 @@ int readch(void)
void save_file(void) void save_file(void)
{ {
if (cur_editor->filename == NULL) { if (strlen(cur_editor->filename) == 0) {
cur_editor->filename = prompt_editor("Save as: %s", NULL); char *new_filename = prompt_editor("Save as: %s", NULL);
if (cur_editor->filename == NULL) { if (new_filename == NULL) {
wpprintw("Save aborted"); wpprintw("Save aborted");
return; return;
} }
strcpy(cur_editor->filename, new_filename);
select_syntax(); select_syntax();
} }
int fd = open(cur_editor->filename, O_RDWR | O_CREAT, 0644); int fd = open(cur_editor->filename, O_RDWR | O_CREAT, 0644);
@ -856,7 +865,7 @@ char *syntax_to_color(int hl, size_t *len)
void select_syntax(void) void select_syntax(void)
{ {
if (cur_editor->filename == NULL) return; if (strlen(cur_editor->filename) == 0) return;
cur_editor->syntax = NULL; cur_editor->syntax = NULL;
char *ext = strrchr(cur_editor->filename, '.'); char *ext = strrchr(cur_editor->filename, '.');
for (uint8_t i = 0; i < LANGS_LEN; i++) { for (uint8_t i = 0; i < LANGS_LEN; i++) {
@ -878,7 +887,7 @@ void select_syntax(void)
void die(const char *s) void die(const char *s)
{ {
bprintf("\033[2J\033[H"); cleanup();
perror(s); perror(s);
exit(1); exit(1);
} }
@ -1096,13 +1105,10 @@ int main(int argc, char **argv)
if (c == 'p') { if (c == 'p') {
c = readch(); c = readch();
if (c == 'v') { if (c == 'v') {
char cwd[PATH_MAX];
getcwd(cwd, PATH_MAX);
pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) { if (pid == 0) {
/* Child process */ /* Child process */
execlp("ccc", "ccc", cwd, "-p", NULL); execlp("ccc", "ccc", cur_editor->cwd, "-p", NULL);
_exit(1); /* Exit if exec fails */ _exit(1); /* Exit if exec fails */
} else if (pid > 0) { } else if (pid > 0) {
/* Parent process */ /* Parent process */
@ -1110,6 +1116,9 @@ int main(int argc, char **argv)
FILE *f = fopen("/home/night/.cache/ccc/opened_file", "r"); FILE *f = fopen("/home/night/.cache/ccc/opened_file", "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;
init_editor(opened_file);
} else { } else {
/* Fork failed */ /* Fork failed */
} }