Remove old code

This commit is contained in:
Night Kaly 2024-11-09 00:40:00 +00:00
parent 86dce61030
commit 546a6c5f1a
Signed by: night0721
SSH key fingerprint: SHA256:B/hgVwUoBpx5vdNsXl9w8XwZljA9766uk6T4ubZp5HM
14 changed files with 0 additions and 1404 deletions

View file

@ -1,8 +0,0 @@
#ifndef BAR_H_
#define BAR_H_
void draw_status_bar(struct abuf *ab);
void draw_message_bar(struct abuf *ab);
void set_status_bar_message(const char *fmt, ...);
#endif

View file

@ -1,16 +0,0 @@
#ifndef EDITOR_H_
#define EDITOR_H_
void refresh_screen(void);
void move_cursor(int key);
void scroll(void);
void insert_char(int c);
void insert_new_line(void);
void shift_new_line(void);
void del_char(void);
void init_editor(void);
void open_editor(char *filename);
char *prompt_editor(char *prompt, void (*callback)(char *, int));
void find_editor(void);
#endif

View file

@ -1,9 +0,0 @@
#ifndef IO_H_
#define IO_H_
int read_key(void);
void save_file(void);
void process_key(void);
void draw_rows(struct abuf *ab);
#endif

View file

@ -1,25 +0,0 @@
#ifndef ROW_H_
#define ROW_H_
typedef struct row {
int idx;
int size;
int render_size;
char *chars;
char *render;
unsigned char *hl;
int opened_comment;
} row;
int row_cx_to_rx(row *row, int cx);
int row_rx_to_cx(row *row, int rx);
void update_row(row *row);
void insert_row(int at, char *s, size_t len);
void free_row(row *row);
void del_row(int at);
void row_insert_char(row *row, int at, int c);
void row_append_str(row *row, char *s, size_t len);
void row_del_char(row *row, int at);
char *rows_to_str(int *buflen);
#endif

View file

@ -1,26 +0,0 @@
#ifndef SYNTAX_H_
#define SYNTAX_H_
#include <stdio.h>
#include "row.h"
#define HL_NUMBERS (1 << 0)
#define HL_STRINGS (1 << 1)
typedef struct language {
char *filetype;
int flags;
char *singleline_comment_start;
char *multiline_comment_start;
char *multiline_comment_end;
char **keywords;
char **extensions;
} language;
int is_separator(int c);
void update_highlight(row *row);
char *syntax_to_color(int hl, size_t *len);
void select_syntax_highlight(void);
#endif

View file

@ -1,12 +0,0 @@
#ifndef TERM_H_
#define TERM_H_
#define CTRL_KEY(k) ((k) & 0x1f)
void die(const char *s);
void reset_term(void);
void setup_term(void);
int get_cursor_position(int *rows, int *cols);
int get_window_size(int *rows, int *cols);
#endif

View file

@ -1,75 +0,0 @@
#ifndef VIP_H_
#define VIP_H_
#include <termios.h>
#include <time.h>
#define VERSION "0.0.1"
#define COLOR_LEN 19
enum keys {
BACKSPACE = 127,
ARROW_LEFT = 1000,
ARROW_RIGHT,
ARROW_UP,
ARROW_DOWN,
DEL_KEY,
HOME_KEY,
END_KEY,
PAGE_UP,
PAGE_DOWN
};
enum modes {
NORMAL,
INSERT,
VISUAL,
COMMAND
};
enum highlight {
DEFAULT = 0,
COMMENT,
MLCOMMENT,
KEYWORD1, /* default */
KEYWORD2, /* types */
STRING,
NUMBER,
MATCH,
RESET
};
#include "row.h"
#include "syntax.h"
#include "config.h"
typedef struct editor {
int cx, cy; /* chars x, y */
int rx; /* render x */
int rowoff;
int coloff;
int screenrows, screencols;
int rows;
row *row;
int dirty;
int mode;
char *filename;
char statusmsg[80];
time_t statusmsg_time;
language *syntax;
struct termios termios;
} editor;
struct abuf {
char *b;
int len;
};
#define ABUF_INIT { NULL, 0 }
void abAppend(struct abuf *ab, const char *s, int len);
void abFree(struct abuf *ab);
extern editor vip;
#endif

115
src/bar.c
View file

@ -1,115 +0,0 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include "vip.h"
extern editor vip;
void draw_status_bar(struct abuf *ab)
{
abAppend(ab, "\x1b[7m", 4);
abAppend(ab, "\x1b[1m", 4);
int mode_len;
if (vip.mode == NORMAL) {
abAppend(ab, BLUE_BG, COLOR_LEN);
char mode[9] = " NORMAL ";
abAppend(ab, mode, 8);
mode_len = 8;
} else if (vip.mode == INSERT) {
abAppend(ab, GREEN_BG, COLOR_LEN);
char mode[9] = " INSERT ";
abAppend(ab, mode, 8);
mode_len = 8;
} else if (vip.mode == COMMAND) {
abAppend(ab, PEACH_BG, COLOR_LEN);
char mode[10] = " COMMAND ";
abAppend(ab, mode, 9);
mode_len = 9;
}
abAppend(ab, "\x1b[22m", 5);
char git_branch[80], git_diff[80], file[80], info[80], lines[80], coord[80];
int gitb_len = snprintf(git_branch, sizeof(git_branch), " %s ", "master");
int gitd_len = snprintf(git_diff, sizeof(git_diff), " %s ", "+1");
int file_len = snprintf(file, sizeof(file), " %.20s %s",
vip.filename ? vip.filename : "[No Name]", vip.dirty ? "[+]" : "");
int info_len = snprintf(info, sizeof(info), " %s ", vip.syntax ? vip.syntax->filetype : "");
int lines_len;
if (vip.rows == 0 || vip.rows == vip.cy + 1) {
lines_len = snprintf(lines, sizeof(lines), " %s ", vip.rows == 0 ? "Top" : "Bot");
} else {
lines_len = snprintf(lines, sizeof(lines), " %d%% ", ((vip.cy + 1) * 100 / vip.rows));
}
int coord_len = snprintf(coord, sizeof(coord), " %d:%d ", vip.cy + 1, vip.rx + 1);
abAppend(ab, SURFACE_1_BG, COLOR_LEN); /* background */
if (vip.mode == NORMAL) {
abAppend(ab, BLUE_FG, COLOR_LEN); /* text */
} else if (vip.mode == INSERT) {
abAppend(ab, GREEN_FG, COLOR_LEN);
} else if (vip.mode == COMMAND) {
abAppend(ab, PEACH_FG, COLOR_LEN);
}
abAppend(ab, git_branch, gitb_len);
abAppend(ab, "|", 1);
abAppend(ab, GREEN_FG, COLOR_LEN);
abAppend(ab, git_diff, gitd_len);
abAppend(ab, BLACK_BG, COLOR_LEN);
abAppend(ab, WHITE_FG, COLOR_LEN);
abAppend(ab, file, file_len);
while (file_len < vip.screencols) {
if (vip.screencols - mode_len - file_len - gitb_len - gitd_len - 1 == info_len + lines_len + coord_len) {
abAppend(ab, info, info_len);
abAppend(ab, SURFACE_1_BG, COLOR_LEN);
if (vip.mode == NORMAL) {
abAppend(ab, BLUE_FG, COLOR_LEN);
} else if (vip.mode == INSERT) {
abAppend(ab, GREEN_FG, COLOR_LEN);
} else if (vip.mode == COMMAND) {
abAppend(ab, PEACH_FG, COLOR_LEN);
}
abAppend(ab, lines, lines_len);
if (vip.mode == NORMAL) {
abAppend(ab, BLUE_BG, COLOR_LEN);
} else if (vip.mode == INSERT) {
abAppend(ab, GREEN_BG, COLOR_LEN);
} else if (vip.mode == COMMAND) {
abAppend(ab, PEACH_BG, COLOR_LEN);
}
abAppend(ab, BLACK_FG, COLOR_LEN);
abAppend(ab, "\x1b[1m", 4);
abAppend(ab, coord, coord_len);
abAppend(ab, "\x1b[22m", 5);
break;
} else {
abAppend(ab, " ", 1);
file_len++;
}
}
abAppend(ab, "\x1b[m", 3);
abAppend(ab, "\r\n", 2);
}
void draw_message_bar(struct abuf *ab)
{
abAppend(ab, "\x1b[K", 3);
int msglen = strlen(vip.statusmsg);
if (msglen > vip.screencols) msglen = vip.screencols;
if (msglen && time(NULL) - vip.statusmsg_time < 5)
abAppend(ab, vip.statusmsg, msglen);
}
void set_status_bar_message(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(vip.statusmsg, sizeof(vip.statusmsg), fmt, ap);
va_end(ap);
vip.statusmsg_time = time(NULL);
}

View file

@ -1,298 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include "editor.h"
#include "vip.h"
#include "term.h"
#include "io.h"
#include "bar.h"
extern editor vip;
void refresh_screen(void)
{
scroll();
struct abuf ab = ABUF_INIT;
abAppend(&ab, "\x1b[?25l", 6);
abAppend(&ab, "\x1b[H", 3);
draw_rows(&ab);
draw_status_bar(&ab);
draw_message_bar(&ab);
char buf[32];
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (vip.cy - vip.rowoff) + 1,
(vip.rx - vip.coloff) + 1);
abAppend(&ab, buf, strlen(buf));
abAppend(&ab, "\x1b[?25h", 6);
write(STDOUT_FILENO, ab.b, ab.len);
abFree(&ab);
}
void move_cursor(int key)
{
row *row = (vip.cy >= vip.rows) ? NULL : &vip.row[vip.cy];
switch (key) {
case ARROW_LEFT:
if (vip.cx != 0) {
vip.cx--;
}
break;
case ARROW_RIGHT:
if (row && vip.cx < row->size) {
vip.cx++;
}
break;
case ARROW_UP:
if (vip.cy != 0) {
vip.cy--;
}
break;
case ARROW_DOWN:
if (vip.cy < vip.rows) {
vip.cy++;
}
break;
}
row = (vip.cy >= vip.rows) ? NULL : &vip.row[vip.cy];
int rowlen = row ? row->size : 0;
if (vip.cx > rowlen) {
vip.cx = rowlen;
}
}
void scroll(void)
{
vip.rx = 0;
if (vip.cy < vip.rows) {
vip.rx = row_cx_to_rx(&vip.row[vip.cy], vip.cx);
}
if (vip.cy < vip.rowoff) {
vip.rowoff = vip.cy;
}
if (vip.cy >= vip.rowoff + vip.screenrows) {
vip.rowoff = vip.cy - vip.screenrows + 1;
}
if (vip.rx < vip.coloff) {
vip.coloff = vip.rx;
}
if (vip.rx >= vip.coloff + vip.screencols) {
vip.coloff = vip.rx - vip.screencols + 1;
}
}
void insert_char(int c)
{
if (vip.cy == vip.rows) {
insert_row(vip.rows, "", 0);
}
row_insert_char(&vip.row[vip.cy], vip.cx, c);
vip.cx++;
}
void insert_new_line(void)
{
if (vip.cx == 0) {
insert_row(vip.cy, "", 0);
} else {
row *row = &vip.row[vip.cy];
insert_row(vip.cy + 1, &row->chars[vip.cx], row->size - vip.cx);
row = &vip.row[vip.cy];
row->size = vip.cx;
row->chars[row->size] = '\0';
update_row(row);
}
vip.cy++;
vip.cx = 0;
}
/*
* 'o' in vim
*/
void shift_new_line(void)
{
insert_row(vip.cy + 1, "", 0);
vip.cy++;
vip.cx = 0;
}
void del_char(void)
{
if (vip.cy == vip.rows) return;
if (vip.cx == 0 && vip.cy == 0) return;
row *row = &vip.row[vip.cy];
if (vip.cx > 0) {
row_del_char(row, vip.cx - 1);
vip.cx--;
} else {
vip.cx = vip.row[vip.cy - 1].size;
row_append_str(&vip.row[vip.cy - 1], row->chars, row->size);
del_row(vip.cy);
vip.cy--;
}
}
void init_editor(void)
{
vip.cx = 0;
vip.cy = 0;
vip.rx = 0;
vip.rowoff = 0;
vip.coloff = 0;
vip.rows = 0;
vip.row = NULL;
vip.dirty = 0;
vip.mode = NORMAL;
vip.filename = NULL;
vip.statusmsg[0] = '\0';
vip.statusmsg_time = 0;
vip.syntax = NULL;
if (get_window_size(&vip.screenrows, &vip.screencols) == -1) {
die("get_window_size");
}
vip.screenrows -= 2;
}
void open_editor(char *filename)
{
free(vip.filename);
vip.filename = strdup(filename);
select_syntax_highlight();
FILE *fp = fopen(filename, "r");
if (!fp) {
die("fopen");
}
char *line = NULL;
size_t linecap = 0;
ssize_t len;
while ((len = getline(&line, &linecap, fp)) != -1) {
/* remove new line and carriage return at end of line */
while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')) {
len--;
}
insert_row(vip.rows, line, len);
}
free(line);
fclose(fp);
/* reset dirtiness as nothing is modified yet */
vip.dirty = 0;
}
char *prompt_editor(char *prompt, void (*callback)(char *, int))
{
size_t bufsize = 128;
char *buf = malloc(bufsize);
size_t buflen = 0;
buf[0] = '\0';
while (1) {
set_status_bar_message(prompt, buf);
refresh_screen();
int c = read_key();
if (c == DEL_KEY || c == CTRL_KEY('h') || c == BACKSPACE) {
if (buflen != 0) {
buf[--buflen] = '\0';
}
} else if (c == '\x1b') {
set_status_bar_message("");
if (callback) callback(buf, c);
free(buf);
return NULL;
} else if (c == '\r') {
if (buflen != 0) {
set_status_bar_message("");
if (callback) callback(buf, c);
return buf;
}
} else if (!iscntrl(c) && c < 128) {
if (buflen == bufsize - 1) {
bufsize *= 2;
buf = realloc(buf, bufsize);
}
buf[buflen++] = c;
buf[buflen] = '\0';
}
if (callback) callback(buf, c);
}
}
void find_callback(char *query, int key)
{
static int last_match = -1;
static int direction = 1;
static int saved_hl_line;
static char *saved_hl = NULL;
if (saved_hl) {
memcpy(vip.row[saved_hl_line].hl, saved_hl, vip.row[saved_hl_line].render_size);
free(saved_hl);
saved_hl = NULL;
}
if (key == '\r' || key == '\x1b') {
last_match = -1;
direction = 1;
return;
} else if (key == CTRL_KEY('n')) {
direction = 1;
} else if (key == CTRL_KEY('p')) {
direction = -1;
} else {
last_match = -1;
direction = 1;
}
if (last_match == -1) direction = 1;
int current = last_match;
for (int i = 0; i < vip.rows; i++) {
current += direction;
if (current == -1) current = vip.rows - 1;
else if (current == vip.rows) current = 0;
row *row = &vip.row[current];
char *match = strstr(row->render, query);
if (match) {
last_match = current;
vip.cy = current;
vip.cx = row_rx_to_cx(row, match - row->render);
vip.rowoff = vip.rows;
saved_hl_line = current;
saved_hl = malloc(row->render_size);
memcpy(saved_hl, row->hl, row->render_size);
memset(&row->hl[match - row->render], MATCH, strlen(query));
memset(&row->hl[match - row->render + strlen(query)], RESET, row->render_size - (match - row->render + strlen(query)));
break;
}
}
}
void find_editor(void)
{
int tmp_cx = vip.cx;
int tmp_cy = vip.cy;
int tmp_coloff = vip.coloff;
int tmp_rowoff = vip.rowoff;
char *query = prompt_editor("/%s", find_callback);
if (query) {
free(query);
} else {
vip.cx = tmp_cx;
vip.cy = tmp_cy;
vip.coloff = tmp_coloff;
vip.rowoff = tmp_rowoff;
}
}

317
src/io.c
View file

@ -1,317 +0,0 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include "editor.h"
#include "term.h"
#include "vip.h"
#include "bar.h"
int read_key(void)
{
int nread;
char c;
while ((nread = read(STDIN_FILENO, &c, 1)) != 1) {
if (nread == -1 && errno != EAGAIN) {
die("read");
}
}
if (c == '\x1b') {
char seq[3];
if (read(STDIN_FILENO, &seq[0], 1) != 1)
return '\x1b';
if (read(STDIN_FILENO, &seq[1], 1) != 1)
return '\x1b';
if (seq[0] == '[') {
if (seq[1] >= '0' && seq[1] <= '9') {
if (read(STDIN_FILENO, &seq[2], 1) != 1)
return '\x1b';
if (seq[2] == '~') {
switch (seq[1]) {
case '1': return HOME_KEY;
case '3': return DEL_KEY;
case '4': return END_KEY;
case '5': return PAGE_UP;
case '6': return PAGE_DOWN;
case '7': return HOME_KEY;
case '8': return END_KEY;
}
}
} else {
switch (seq[1]) {
case 'A': return ARROW_UP;
case 'B': return ARROW_DOWN;
case 'C': return ARROW_RIGHT;
case 'D': return ARROW_LEFT;
case 'F': return END_KEY;
case 'H': return HOME_KEY;
}
}
} else if (seq[0] == 'O') {
switch (seq[1]) {
case 'F': return END_KEY;
case 'H': return HOME_KEY;
}
}
return '\x1b';
} else {
return c;
}
}
void save_file(void)
{
if (vip.filename == NULL) {
vip.filename = prompt_editor("Save as: %s", NULL);
if (vip.filename == NULL) {
set_status_bar_message("Save aborted");
return;
}
select_syntax_highlight();
}
int len;