2024-06-22 21:50:38 +02:00
|
|
|
#include <stdio.h>
|
2024-06-25 02:10:38 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2024-07-02 23:56:09 +02:00
|
|
|
#include <fcntl.h>
|
2024-06-25 02:10:38 +02:00
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
2024-06-30 20:55:59 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
2024-06-22 21:50:38 +02:00
|
|
|
|
2024-06-30 20:55:59 +02:00
|
|
|
#include "vip.h"
|
|
|
|
#include "term.h"
|
|
|
|
#include "bar.h"
|
2024-07-01 21:40:07 +02:00
|
|
|
#include "editor.h"
|
2024-07-03 03:41:28 +02:00
|
|
|
#include "row.h"
|
2024-07-03 14:12:41 +02:00
|
|
|
#include "syntax.h"
|
2024-06-25 02:10:38 +02:00
|
|
|
|
2024-06-30 20:55:59 +02:00
|
|
|
editor vip;
|
2024-06-25 02:10:38 +02:00
|
|
|
|
|
|
|
int read_key()
|
|
|
|
{
|
|
|
|
int nread;
|
|
|
|
char c;
|
|
|
|
while ((nread = read(STDIN_FILENO, &c, 1)) != 1) {
|
2024-06-30 20:55:59 +02:00
|
|
|
if (nread == -1 && errno != EAGAIN) {
|
|
|
|
die("read");
|
|
|
|
}
|
2024-06-25 02:10:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '\x1b') {
|
|
|
|
char seq[3];
|
|
|
|
|
2024-06-30 20:55:59 +02:00
|
|
|
if (read(STDIN_FILENO, &seq[0], 1) != 1)
|
|
|
|
return '\x1b';
|
|
|
|
if (read(STDIN_FILENO, &seq[1], 1) != 1)
|
|
|
|
return '\x1b';
|
2024-06-25 02:10:38 +02:00
|
|
|
|
|
|
|
if (seq[0] == '[') {
|
|
|
|
if (seq[1] >= '0' && seq[1] <= '9') {
|
2024-06-30 20:55:59 +02:00
|
|
|
if (read(STDIN_FILENO, &seq[2], 1) != 1)
|
|
|
|
return '\x1b';
|
2024-06-25 02:10:38 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-02 23:56:09 +02:00
|
|
|
void save_file()
|
|
|
|
{
|
2024-07-03 04:00:14 +02:00
|
|
|
if (vip.filename == NULL) {
|
2024-07-03 12:11:48 +02:00
|
|
|
vip.filename = prompt_editor("Save as: %s", NULL);
|
2024-07-03 04:00:14 +02:00
|
|
|
if (vip.filename == NULL) {
|
|
|
|
set_status_bar_message("Save aborted");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2024-07-02 23:56:09 +02:00
|
|
|
int len;
|
|
|
|
char *buf = rows_to_str(&len);
|
|
|
|
int fd = open(vip.filename, O_RDWR | O_CREAT, 0644);
|
|
|
|
if (fd != -1) {
|
|
|
|
if (ftruncate(fd, len) != -1) {
|
|
|
|
if (write(fd, buf, len) == len) {
|
|
|
|
close(fd);
|
|
|
|
free(buf);
|
|
|
|
vip.dirty = 0;
|
|
|
|
set_status_bar_message("\"%s\" %dL, %dB written", vip.filename, vip.rows, len);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
set_status_bar_message("Error saving: %s", strerror(errno));
|
2024-06-30 20:55:59 +02:00
|
|
|
}
|
2024-06-25 02:10:38 +02:00
|
|
|
|
|
|
|
void abAppend(struct abuf *ab, const char *s, int len)
|
|
|
|
{
|
|
|
|
char *new = realloc(ab->b, ab->len + len);
|
|
|
|
if (new == NULL) return;
|
|
|
|
memcpy(&new[ab->len], s, len);
|
|
|
|
ab->b = new;
|
|
|
|
ab->len += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void abFree(struct abuf *ab)
|
2024-06-22 21:50:38 +02:00
|
|
|
{
|
2024-06-25 02:10:38 +02:00
|
|
|
free(ab->b);
|
|
|
|
}
|
|
|
|
|
2024-06-30 20:55:59 +02:00
|
|
|
void scroll()
|
|
|
|
{
|
|
|
|
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 draw_rows(struct abuf *ab)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < vip.screenrows; y++) {
|
|
|
|
int filerow = y + vip.rowoff;
|
|
|
|
if (filerow >= vip.rows) {
|
|
|
|
if (vip.rows == 0 && y == vip.screenrows / 3) {
|
|
|
|
char welcome[11];
|
|
|
|
int welcomelen = snprintf(welcome, sizeof(welcome),
|
|
|
|
"VIP v%s", VERSION);
|
|
|
|
if (welcomelen > vip.screencols)
|
|
|
|
welcomelen = vip.screencols;
|
|
|
|
int padding = (vip.screencols - welcomelen) / 2;
|
|
|
|
if (padding) {
|
|
|
|
abAppend(ab, "~", 1);
|
|
|
|
padding--;
|
|
|
|
}
|
|
|
|
while (padding--) abAppend(ab, " ", 1);
|
|
|
|
abAppend(ab, welcome, welcomelen);
|
|
|
|
} else {
|
2024-06-25 02:10:38 +02:00
|
|
|
abAppend(ab, "~", 1);
|
|
|
|
}
|
|
|
|
} else {
|
2024-06-30 20:55:59 +02:00
|
|
|
int len = vip.row[filerow].render_size - vip.coloff;
|
|
|
|
if (len < 0) len = 0;
|
|
|
|
if (len > vip.screencols) len = vip.screencols;
|
2024-07-03 14:12:41 +02:00
|
|
|
char *c = &vip.row[filerow].render[vip.coloff];
|
|
|
|
unsigned char *hl = &vip.row[filerow].hl[vip.coloff];
|
|
|
|
|
|
|
|
char *current_color = malloc(COLOR_LEN * 2);
|
|
|
|
for (int j = 0; j < len; j++) {
|
|
|
|
if (hl[j] == HL_NORMAL) {
|
|
|
|
if (strncmp(current_color, WHITE_BG, COLOR_LEN)) {
|
|
|
|
abAppend(ab, WHITE_BG, COLOR_LEN);
|
|
|
|
memcpy(current_color, WHITE_BG, COLOR_LEN);
|
|
|
|
}
|
|
|
|
abAppend(ab, &c[j], 1);
|
|
|
|
} else {
|
|
|
|
size_t len;
|
|
|
|
char *color = syntax_to_color(hl[j], &len);
|
|
|
|
FILE *f = fopen("/home/night/a", "a");
|
|
|
|
fprintf(f, "len: %d\n", len);
|
|
|
|
if (strncmp(current_color, color, len)) {
|
|
|
|
fprintf(f, "color: %s\n", color);
|
|
|
|
memcpy(current_color, color, len);
|
|
|
|
abAppend(ab, color, len);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
free(color);
|
|
|
|
abAppend(ab, &c[j], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
abAppend(ab, WHITE_BG, COLOR_LEN);
|
2024-06-25 02:10:38 +02:00
|
|
|
}
|
2024-06-30 20:55:59 +02:00
|
|
|
|
|
|
|
abAppend(ab, "\x1b[K", 3);
|
|
|
|
abAppend(ab, "\r\n", 2);
|
2024-06-25 02:10:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void refresh_screen()
|
|
|
|
{
|
2024-06-30 20:55:59 +02:00
|
|
|
scroll();
|
|
|
|
|
2024-06-25 02:10:38 +02:00
|
|
|
struct abuf ab = ABUF_INIT;
|
|
|
|
|
|
|
|
abAppend(&ab, "\x1b[?25l", 6);
|
|
|
|
abAppend(&ab, "\x1b[H", 3);
|
|
|
|
|
|
|
|
draw_rows(&ab);
|
2024-06-30 20:55:59 +02:00
|
|
|
draw_status_bar(&ab);
|
|
|
|
draw_message_bar(&ab);
|
2024-06-25 02:10:38 +02:00
|
|
|
|
|
|
|
char buf[32];
|
2024-06-30 20:55:59 +02:00
|
|
|
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (vip.cy - vip.rowoff) + 1,
|
|
|
|
(vip.rx - vip.coloff) + 1);
|
2024-06-25 02:10:38 +02:00
|
|
|
abAppend(&ab, buf, strlen(buf));
|
|
|
|
|
|
|
|
abAppend(&ab, "\x1b[?25h", 6);
|
|
|
|
|
|
|
|
write(STDOUT_FILENO, ab.b, ab.len);
|
|
|
|
abFree(&ab);
|
|
|
|
}
|
|
|
|
|
2024-06-30 20:55:59 +02:00
|
|
|
void move_cursor(int key)
|
|
|
|
{
|
|
|
|
row *row = (vip.cy >= vip.rows) ? NULL : &vip.row[vip.cy];
|
2024-06-25 02:10:38 +02:00
|
|
|
switch (key) {
|
|
|
|
case ARROW_LEFT:
|
2024-06-30 20:55:59 +02:00
|
|
|
if (vip.cx != 0) {
|
|
|
|
vip.cx--;
|
2024-06-25 02:10:38 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ARROW_RIGHT:
|
2024-06-30 20:55:59 +02:00
|
|
|
if (row && vip.cx < row->size) {
|
|
|
|
vip.cx++;
|
2024-06-25 02:10:38 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ARROW_UP:
|
2024-06-30 20:55:59 +02:00
|
|
|
if (vip.cy != 0) {
|
|
|
|
vip.cy--;
|
2024-06-25 02:10:38 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ARROW_DOWN:
|
2024-06-30 20:55:59 +02:00
|
|
|
if (vip.cy < vip.rows) {
|
|
|
|
vip.cy++;
|
2024-06-25 02:10:38 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-06-30 20:55:59 +02:00
|
|
|
row = (vip.cy >= vip.rows) ? NULL : &vip.row[vip.cy];
|
|
|
|
int rowlen = row ? row->size : 0;
|
|
|
|
if (vip.cx > rowlen) {
|
|
|
|
vip.cx = rowlen;
|
|
|
|
}
|
2024-06-25 02:10:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void process_key()
|
|
|
|
{
|
2024-07-03 03:41:28 +02:00
|
|
|
|
2024-06-25 02:10:38 +02:00
|
|
|
int c = read_key();
|
|
|
|
switch (c) {
|
2024-07-01 21:40:07 +02:00
|
|
|
case '\r':
|
2024-07-03 03:41:28 +02:00
|
|
|
insert_new_line();
|
2024-07-01 21:40:07 +02:00
|
|
|
break;
|
|
|
|
|
2024-06-25 02:10:38 +02:00
|
|
|
case HOME_KEY:
|
2024-06-30 20:55:59 +02:00
|
|
|
vip.cx = 0;
|
2024-06-25 02:10:38 +02:00
|
|
|
break;
|
2024-07-01 21:40:07 +02:00
|
|
|
|
2024-06-25 02:10:38 +02:00
|
|
|
case END_KEY:
|
2024-06-30 20:55:59 +02:00
|
|
|
if (vip.cy < vip.rows) {
|
|
|
|
vip.cx = vip.row[vip.cy].size;
|
|
|
|
}
|
2024-06-25 02:10:38 +02:00
|
|
|
break;
|
2024-07-01 21:40:07 +02:00
|
|
|
|
|
|
|
case BACKSPACE:
|
|
|
|
case CTRL_KEY('h'):
|
2024-07-03 10:58:51 +02:00
|
|
|
case DEL_KEY: /* PASSTHROUGH */
|
|
|
|
if (vip.mode == INSERT) {
|
|
|
|
if (c == DEL_KEY)
|
|
|
|
move_cursor(ARROW_RIGHT);
|
|
|
|
del_char();
|
|
|
|
}
|
2024-07-01 21:40:07 +02:00
|
|
|
break;
|
|
|
|
|
2024-06-25 02:10:38 +02:00
|
|
|
case PAGE_UP:
|
|
|
|
case PAGE_DOWN:
|
|
|
|
{
|
2024-06-30 20:55:59 +02:00
|
|
|
if (c == PAGE_UP) {
|
|
|
|
vip.cy = vip.rowoff;
|
|
|
|
} else if (c == PAGE_DOWN) {
|
|
|
|
vip.cy = vip.rowoff + vip.screenrows - 1;
|
|
|
|
if (vip.cy > vip.rows) vip.cy = vip.rows;
|
|
|
|
}
|
|
|
|
int times = vip.screenrows;
|
2024-06-25 02:10:38 +02:00
|
|
|
while (times--)
|
|
|
|
move_cursor(c == PAGE_UP ? ARROW_UP : ARROW_DOWN);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARROW_UP:
|
|
|
|
case ARROW_DOWN:
|
|
|
|
case ARROW_LEFT:
|
|
|
|
case ARROW_RIGHT:
|
2024-07-03 10:58:51 +02:00
|
|
|
if (vip.mode == INSERT) {
|
|
|
|
move_cursor(c);
|
|
|
|
}
|
2024-06-25 02:10:38 +02:00
|
|
|
break;
|
2024-07-01 21:40:07 +02:00
|
|
|
|
|
|
|
case CTRL_KEY('l'):
|
|
|
|
case '\x1b':
|
2024-07-03 10:58:51 +02:00
|
|
|
if (vip.mode == INSERT) {
|
|
|
|
vip.mode = NORMAL;
|
|
|
|
move_cursor(ARROW_LEFT);
|
|
|
|
}
|
2024-07-01 21:40:07 +02:00
|
|
|
break;
|
|
|
|
|
2024-07-03 10:58:51 +02:00
|
|
|
case 'i':
|
|
|
|
if (vip.mode == NORMAL) {
|
|
|
|
vip.mode = INSERT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ':': /* PASSTHROUGH */
|
|
|
|
if (vip.mode == NORMAL) {
|
2024-07-03 14:12:41 +02:00
|
|
|
vip.mode = COMMAND;
|
2024-07-03 12:11:48 +02:00
|
|
|
char *cmd = prompt_editor(":%s", NULL);
|
|
|
|
if (cmd == NULL) {
|
2024-07-03 14:12:41 +02:00
|
|
|
vip.mode = NORMAL;
|
2024-07-03 12:11:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-07-03 10:58:51 +02:00
|
|
|
switch (cmd[0]) {
|
|
|
|
case 'q':
|
|
|
|
if (cmd[1] == '!') {
|
|
|
|
write(STDOUT_FILENO, "\x1b[2J", 4);
|
|
|
|
write(STDOUT_FILENO, "\x1b[H", 3);
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (vip.dirty) {
|
|
|
|
set_status_bar_message("No write since last change for buffer \"%s\"", vip.filename);
|
2024-07-03 14:12:41 +02:00
|
|
|
vip.mode = NORMAL;
|
2024-07-03 10:58:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
write(STDOUT_FILENO, "\x1b[2J", 4);
|
|
|
|
write(STDOUT_FILENO, "\x1b[H", 3);
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
case 'w':
|
|
|
|
save_file();
|
2024-07-03 14:12:41 +02:00
|
|
|
vip.mode = NORMAL;
|
2024-07-03 10:58:51 +02:00
|
|
|
}
|
|
|
|
}
|
2024-07-03 12:11:48 +02:00
|
|
|
case '/': /* PASSTHROUGH */
|
|
|
|
if (vip.mode == NORMAL) {
|
|
|
|
find_editor();
|
|
|
|
break;
|
|
|
|
}
|
2024-07-03 10:58:51 +02:00
|
|
|
|
|
|
|
case 'k': /* PASSTHROUGH */
|
|
|
|
if (vip.mode != INSERT) {
|
|
|
|
move_cursor(ARROW_UP);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'j': /* PASSTHROUGH */
|
|
|
|
if (vip.mode != INSERT) {
|
|
|
|
move_cursor(ARROW_DOWN);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'l': /* PASSTHROUGH */
|
|
|
|
if (vip.mode != INSERT) {
|
|
|
|
move_cursor(ARROW_RIGHT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'h': /* PASSTHROUGH */
|
|
|
|
if (vip.mode != INSERT) {
|
|
|
|
move_cursor(ARROW_LEFT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'o': /* PASSTHROUGH */
|
|
|
|
if (vip.mode == NORMAL) {
|
|
|
|
shift_new_line();
|
|
|
|
vip.mode = INSERT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-07-01 21:40:07 +02:00
|
|
|
default:
|
2024-07-03 10:58:51 +02:00
|
|
|
if (vip.mode == INSERT) {
|
|
|
|
insert_char(c);
|
|
|
|
}
|
2024-07-01 21:40:07 +02:00
|
|
|
break;
|
2024-06-25 02:10:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_editor()
|
|
|
|
{
|
2024-06-30 20:55:59 +02:00
|
|
|
vip.cx = 0;
|
|
|
|
vip.cy = 0;
|
|
|
|
vip.rx = 0;
|
|
|
|
vip.rowoff = 0;
|
|
|
|
vip.coloff = 0;
|
|
|
|
vip.rows = 0;
|
|
|
|
vip.row = NULL;
|
2024-07-02 23:56:09 +02:00
|
|
|
vip.dirty = 0;
|
2024-07-03 03:41:28 +02:00
|
|
|
vip.mode = NORMAL;
|
2024-06-30 20:55:59 +02:00
|
|
|
vip.filename = NULL;
|
|
|
|
vip.statusmsg[0] = '\0';
|
|
|
|
vip.statusmsg_time = 0;
|
|
|
|
|
|
|
|
if (get_window_size(&vip.screenrows, &vip.screencols) == -1) {
|
2024-06-25 02:10:38 +02:00
|
|
|
die("get_window_size");
|
2024-06-30 20:55:59 +02:00
|
|
|
}
|
|
|
|
vip.screenrows -= 2;
|
2024-06-25 02:10:38 +02:00
|
|
|
}
|
|
|
|
|
2024-06-30 20:55:59 +02:00
|
|
|
int main(int argc, char **argv)
|
2024-06-25 02:10:38 +02:00
|
|
|
{
|
2024-06-30 20:55:59 +02:00
|
|
|
setup_term();
|
2024-06-25 02:10:38 +02:00
|
|
|
init_editor();
|
2024-06-30 20:55:59 +02:00
|
|
|
if (argc >= 2) {
|
|
|
|
open_editor(argv[1]);
|
|
|
|
}
|
|
|
|
|
2024-07-03 12:11:48 +02:00
|
|
|
set_status_bar_message("By night0721 and gnucolas");
|
2024-06-25 02:10:38 +02:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
refresh_screen();
|
|
|
|
process_key();
|
|
|
|
}
|
2024-06-22 21:50:38 +02:00
|
|
|
return 0;
|
|
|
|
}
|