Compare commits

..

No commits in common. "51688b6ad6c581048a3c53e80b734ce7107352a2" and "8a6dd74ac0c833a617a5bec36bf762da5cf06093" have entirely different histories.

2 changed files with 40 additions and 75 deletions

View file

@ -22,11 +22,6 @@
#define MAUVE_FG "\033[48;2;203;166;247m"
#define MAUVE_BG "\033[38;2;203;166;247m"
#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"
/* ERROR is red with bold and italic */
#define ERROR "\033[38;2;243;139;168m\033[1m\033[3m"
enum keys {
BACKSPACE = 127,
@ -92,8 +87,7 @@ typedef struct {
row_t *row;
int dirty;
int mode;
char filename[PATH_MAX];
char cwd[PATH_MAX];
char *filename;
language_t *syntax;
} editor_t;
@ -107,7 +101,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|", NULL },
{ "switch", "if", "while", "for", "break", "continue", "return", "else", "struct", "union", "typedef", "static", "enum", "case", "sizeof", "#include", "int|", "long|", "double|", "float|", "char|", "unsigned|", "void|", NULL },
{ ".c", ".h", ".cpp", NULL },
},
};

105
vip.c
View file

@ -9,7 +9,6 @@
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/types.h>
@ -18,10 +17,9 @@
#include "config.h"
struct termios newt, oldt;
int rows, cols;
int screenrows, screencols;
editor_t editor[9];
int editor_idx = 0;
int editors = 0;
editor_t *cur_editor;
void draw_status_bar(void);
@ -60,7 +58,7 @@ void bprintf(const char *fmt, ...);
*/
void draw_status_bar(void)
{
move_cursor(rows - 1, 1);
move_cursor(screenrows - 1, 1);
/* Reverse and bold */
bprintf("\033[7m\033[1m");
@ -89,8 +87,8 @@ void draw_status_bar(void)
char git[80], file[80], info[80], lines[80], coord[80];
int git_len = snprintf(git, sizeof(git), " %s | %s ", "master", "+1");
int file_len = snprintf(file, sizeof(file), " %s %s",
strlen(cur_editor->filename) > 0 ? cur_editor->filename : "[No Name]", cur_editor->dirty ? "[+]" : "");
int file_len = snprintf(file, sizeof(file), " %.20s %s",
cur_editor->filename ? cur_editor->filename : "[No Name]", cur_editor->dirty ? "[+]" : "");
int info_len = snprintf(info, sizeof(info), " %s ", cur_editor->syntax ? cur_editor->syntax->filetype : "");
int lines_len;
if (cur_editor->rows == 0 || cur_editor->rows == cur_editor->y + 1) {
@ -118,8 +116,8 @@ void draw_status_bar(void)
}
bprintf("%s%s%s%s", git, BLACK_BG, WHITE_FG, file);
while (file_len < cols) {
if (cols - mode_len - git_len - file_len == info_len + lines_len + coord_len) {
while (file_len < screencols) {
if (screencols - mode_len - git_len - file_len == info_len + lines_len + coord_len) {
bprintf("%s%s", info, SURFACE_1_BG);
if (cur_editor->mode == NORMAL) {
bprintf(BLUE_FG);
@ -147,7 +145,7 @@ void draw_status_bar(void)
file_len++;
}
}
bprintf("\033[m");
bprintf("\033[0m");
}
void refresh_screen(void)
@ -165,14 +163,14 @@ void refresh_screen(void)
if (cur_editor->y < cur_editor->rowoff) {
cur_editor->rowoff = cur_editor->y;
}
if (cur_editor->y >= cur_editor->rowoff + rows - 3) {
cur_editor->rowoff = cur_editor->y - rows + 3;
if (cur_editor->y >= cur_editor->rowoff + screenrows - 3) {
cur_editor->rowoff = cur_editor->y - screenrows + 3;
}
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 (cur_editor->rx >= cur_editor->coloff + screencols) {
cur_editor->coloff = cur_editor->rx - screencols + 1;
}
bprintf("\033H\033[2 q");
@ -303,13 +301,7 @@ void del_char(void)
void init_editor(char *filename)
{
if (editors > 8) {
wpprintw("%sOnly 9 tabs are allowed", ERROR);
readch();
wpprintw("");
return;
}
cur_editor = &editor[editors++];
cur_editor = &editor[editor_idx];
cur_editor->x = 0;
cur_editor->y = 0;
cur_editor->rx = 0;
@ -319,17 +311,18 @@ void init_editor(char *filename)
cur_editor->row = NULL;
cur_editor->dirty = 0;
cur_editor->mode = NORMAL;
cur_editor->filename = NULL;
cur_editor->syntax = NULL;
char cwd[PATH_MAX];
getcwd(cwd, PATH_MAX);
strcpy(cur_editor->cwd, cwd);
if (get_window_size(&rows, &cols) == -1) {
if (get_window_size(&screenrows, &screencols) == -1) {
die("get_window_size");
}
if (cur_editor->filename) {
free(cur_editor->filename);
}
if (filename) {
strcpy(cur_editor->filename, basename(filename));
cur_editor->filename = strdup(filename);
FILE *fp = fopen(filename, "r");
if (!fp) {
@ -352,8 +345,6 @@ void init_editor(char *filename)
fclose(fp);
/* Reset dirtiness as nothing is modified yet */
cur_editor->dirty = 0;
} else {
strcpy(cur_editor->filename, "");
}
}
@ -366,7 +357,7 @@ char *prompt_editor(char *prompt, void (*callback)(char *, int))
buf[0] = '\0';
while (1) {
wpprintw(prompt, buf);
move_cursor(rows, prompt_len - 1 + strlen(buf));
move_cursor(screenrows, prompt_len - 1 + strlen(buf));
int c = readch();
if (c == DEL_KEY || c == CTRL_KEY('h') || c == BACKSPACE) {
if (buflen != 0) {
@ -535,13 +526,12 @@ int readch(void)
void save_file(void)
{
if (strlen(cur_editor->filename) == 0) {
char *new_filename = prompt_editor("Save as: %s", NULL);
if (new_filename == NULL) {
if (cur_editor->filename == NULL) {
cur_editor->filename = prompt_editor("Save as: %s", NULL);
if (cur_editor->filename == NULL) {
wpprintw("Save aborted");
return;
}
strcpy(cur_editor->filename, new_filename);
select_syntax();
}
int fd = open(cur_editor->filename, O_RDWR | O_CREAT, 0644);
@ -565,15 +555,15 @@ void save_file(void)
void draw_rows(void)
{
for (int y = 0; y < rows - 1; y++) {
for (int y = 0; y < screenrows - 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) {
if (cur_editor->rows == 0 && y == screenrows / 2) {
char welcome[11];
snprintf(welcome, sizeof(welcome), "VIP v%s", VERSION);
/* Length of welcome message must be 10 */
int padding = (cols - 10) / 2;
int padding = (screencols - 10) / 2;
while (padding--)
bprintf(" ");
bprintf(welcome);
@ -581,7 +571,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 (len > screencols) len = screencols;
char *c = &cur_editor->row[filerow].render[cur_editor->coloff];
unsigned char *hl = &cur_editor->row[filerow].hl[cur_editor->coloff];
@ -866,7 +856,7 @@ char *syntax_to_color(int hl, size_t *len)
void select_syntax(void)
{
if (strlen(cur_editor->filename) == 0) return;
if (cur_editor->filename == NULL) return;
cur_editor->syntax = NULL;
char *ext = strrchr(cur_editor->filename, '.');
for (uint8_t i = 0; i < LANGS_LEN; i++) {
@ -888,32 +878,13 @@ void select_syntax(void)
void die(const char *s)
{
cleanup();
bprintf("\033[2J\033[H");
perror(s);
exit(1);
}
void handle_sigwinch(int ignore)
{
get_window_size(&rows, &cols);
if (cols < 80 || rows < 24) {
die("vip: Terminal size needs to be at least 80x24");
}
refresh_screen();
}
int main(int argc, char **argv)
{
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");
@ -968,10 +939,10 @@ int main(int argc, char **argv)
if (c == PAGE_UP) {
cur_editor->y = cur_editor->rowoff;
} else if (c == PAGE_DOWN) {
cur_editor->y = cur_editor->rowoff + rows - 1;
cur_editor->y = cur_editor->rowoff + screenrows - 1;
if (cur_editor->y > cur_editor->rows) cur_editor->y = cur_editor->rows;
}
int times = rows;
int times = screenrows;
while (times--)
move_xy(c == PAGE_UP ? ARROW_UP : ARROW_DOWN);
break;
@ -1046,7 +1017,7 @@ int main(int argc, char **argv)
break;
} else {
if (cur_editor->dirty) {
wpprintw("%sNo write since last change for buffer \"%s\"", ERROR, cur_editor->filename);
wpprintw("\033[1m\033[38;2;255;0;0mNo write since last change for buffer \"%s\"\033[0m", cur_editor->filename);
cur_editor->mode = NORMAL;
break;
}
@ -1125,10 +1096,13 @@ int main(int argc, char **argv)
if (c == 'p') {
c = readch();
if (c == 'v') {
char cwd[PATH_MAX];
getcwd(cwd, PATH_MAX);
pid_t pid = fork();
if (pid == 0) {
/* Child process */
execlp("ccc", "ccc", cur_editor->cwd, "-p", NULL);
execlp("ccc", "ccc", cwd, "-p", NULL);
_exit(1); /* Exit if exec fails */
} else if (pid > 0) {
/* Parent process */
@ -1136,9 +1110,6 @@ int main(int argc, char **argv)
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;
init_editor(opened_file);
} else {
/* Fork failed */
}
@ -1173,16 +1144,16 @@ void cleanup(void)
*/
void wpprintw(const char *fmt, ...)
{
char buffer[cols];
char buffer[screencols];
va_list args;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
move_cursor(rows, 1);
move_cursor(screenrows, 1);
/* Clear line and print formatted string */
bprintf("\033[K%s\033[0m", buffer);
bprintf("\033[K%s", buffer);
}
void move_cursor(int row, int col)