2014-06-08 10:13:29 +04:00
|
|
|
/* This file is part of ToaruOS and is released under the terms
|
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2013-2014 Kevin Lange
|
|
|
|
* Copyright (C) 2014 Lioncash
|
|
|
|
*/
|
2012-09-08 12:47:19 +04:00
|
|
|
/*
|
2012-11-16 11:33:45 +04:00
|
|
|
* bim
|
|
|
|
*
|
|
|
|
* Bim is a Bad IMitation of Vim.
|
|
|
|
*
|
|
|
|
* The 'standard' text editor for とあるOS.
|
2012-09-08 12:47:19 +04:00
|
|
|
*
|
|
|
|
*/
|
2012-11-16 11:33:45 +04:00
|
|
|
#define _XOPEN_SOURCE 1
|
2012-09-08 12:47:19 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
2012-09-09 01:37:48 +04:00
|
|
|
#include <unistd.h>
|
2013-03-26 09:24:26 +04:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <termios.h>
|
2012-09-09 01:37:48 +04:00
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
#include <stdio_ext.h>
|
|
|
|
#define BACKSPACE_KEY 0x7F
|
|
|
|
#else
|
2012-09-08 12:47:19 +04:00
|
|
|
#include <syscall.h>
|
2012-09-09 01:37:48 +04:00
|
|
|
#define BACKSPACE_KEY 0x08
|
|
|
|
#endif
|
2012-09-08 12:47:19 +04:00
|
|
|
|
2012-09-09 03:47:43 +04:00
|
|
|
#include <wchar.h>
|
|
|
|
|
2012-09-08 12:47:19 +04:00
|
|
|
#include "lib/utf8decode.h"
|
|
|
|
|
|
|
|
#define BLOCK_SIZE 256
|
2012-09-09 01:37:48 +04:00
|
|
|
#define ENTER_KEY '\n'
|
|
|
|
|
2012-09-09 07:20:38 +04:00
|
|
|
typedef struct {
|
|
|
|
uint8_t display_width;
|
|
|
|
uint16_t codepoint;
|
|
|
|
} __attribute__((packed)) char_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t available;
|
|
|
|
uint32_t actual;
|
|
|
|
char_t text[0];
|
|
|
|
} line_t;
|
|
|
|
|
2012-09-09 09:25:32 +04:00
|
|
|
int term_width, term_height;
|
|
|
|
int csr_x_actual, csr_y_actual;
|
2012-09-09 07:20:38 +04:00
|
|
|
|
|
|
|
typedef struct _env {
|
2012-09-08 12:47:19 +04:00
|
|
|
int bottom_size;
|
|
|
|
short lineno_width;
|
|
|
|
char * file_name;
|
|
|
|
int offset;
|
2012-09-09 09:25:32 +04:00
|
|
|
int coffset;
|
2012-09-08 12:47:19 +04:00
|
|
|
int line_no;
|
|
|
|
int line_count;
|
|
|
|
int line_avail;
|
|
|
|
int col_no;
|
|
|
|
short modified;
|
2012-09-09 07:20:38 +04:00
|
|
|
line_t ** lines;
|
|
|
|
} buffer_t;
|
2012-09-08 12:47:19 +04:00
|
|
|
|
2012-09-09 07:20:38 +04:00
|
|
|
buffer_t * env;
|
2012-09-08 12:47:19 +04:00
|
|
|
|
2012-09-09 07:20:38 +04:00
|
|
|
uint32_t buffers_len;
|
|
|
|
uint32_t buffers_avail;
|
|
|
|
buffer_t ** buffers;
|
2012-09-08 12:47:19 +04:00
|
|
|
|
2012-09-09 07:20:38 +04:00
|
|
|
buffer_t * buffer_new() {
|
|
|
|
if (buffers_len == buffers_avail) {
|
|
|
|
buffers_avail *= 2;
|
|
|
|
buffers = realloc(buffers, sizeof(buffer_t *) * buffers_avail);
|
|
|
|
}
|
|
|
|
buffers[buffers_len] = malloc(sizeof(buffer_t));
|
|
|
|
memset(buffers[buffers_len], 0x00, sizeof(buffer_t));
|
|
|
|
buffers_len++;
|
|
|
|
|
|
|
|
return buffers[buffers_len-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_t * buffer_close(buffer_t * buf) {
|
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < buffers_len; i++) {
|
|
|
|
if (buf == buffers[i])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == buffers_len) {
|
|
|
|
return env; /* wtf */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != buffers_len - 1) {
|
|
|
|
memmove(&buffers[i], &buffers[i+1], buffers_len - i);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffers_len--;
|
|
|
|
if (!buffers_len) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (i == buffers_len) {
|
|
|
|
return buffers[buffers_len-1];
|
|
|
|
}
|
|
|
|
return buffers[buffers_len];
|
|
|
|
}
|
2012-09-08 12:47:19 +04:00
|
|
|
|
|
|
|
line_t * line_insert(line_t * line, char_t c, uint32_t offset) {
|
|
|
|
if (line->actual == line->available) {
|
|
|
|
line->available *= 2;
|
|
|
|
line = realloc(line, sizeof(line_t) + sizeof(char_t) * line->available);
|
|
|
|
}
|
|
|
|
if (offset < line->actual) {
|
|
|
|
memmove(&line->text[offset+1], &line->text[offset], sizeof(char_t) * (line->actual - offset));
|
|
|
|
}
|
|
|
|
line->text[offset] = c;
|
|
|
|
line->actual += 1;
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
void line_delete(line_t * line, uint32_t offset) {
|
|
|
|
if (offset == 0) return;
|
|
|
|
if (offset < line->actual) {
|
|
|
|
memmove(&line->text[offset-1], &line->text[offset], sizeof(char_t) * (line->actual - offset - 1));
|
|
|
|
}
|
|
|
|
line->actual -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
line_t ** add_line(line_t ** lines, uint32_t offset) {
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->line_count == env->line_avail) {
|
|
|
|
env->line_avail *= 2;
|
|
|
|
lines = realloc(lines, sizeof(line_t *) * env->line_avail);
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
2012-09-09 07:20:38 +04:00
|
|
|
if (offset < env->line_count) {
|
|
|
|
memmove(&lines[offset+1], &lines[offset], sizeof(line_t *) * (env->line_count - offset));
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
|
|
|
lines[offset] = malloc(sizeof(line_t) + sizeof(char_t) * 32);
|
|
|
|
lines[offset]->available = 32;
|
|
|
|
lines[offset]->actual = 0;
|
2012-09-09 07:20:38 +04:00
|
|
|
env->line_count += 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2012-09-09 02:23:10 +04:00
|
|
|
line_t ** split_line(line_t ** lines, uint32_t line, uint32_t split) {
|
|
|
|
if (split == 0) {
|
|
|
|
return add_line(lines, line - 1);
|
|
|
|
}
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->line_count == env->line_avail) {
|
|
|
|
env->line_avail *= 2;
|
|
|
|
lines = realloc(lines, sizeof(line_t *) * env->line_avail);
|
2012-09-09 02:23:10 +04:00
|
|
|
}
|
2012-09-09 07:20:38 +04:00
|
|
|
if (line < env->line_count) {
|
|
|
|
memmove(&lines[line+1], &lines[line], sizeof(line_t *) * (env->line_count - line));
|
2012-09-09 02:23:10 +04:00
|
|
|
}
|
|
|
|
uint32_t remaining = lines[line-1]->actual - split;
|
|
|
|
|
|
|
|
uint32_t v = remaining;
|
|
|
|
v--;
|
|
|
|
v |= v >> 1;
|
|
|
|
v |= v >> 2;
|
|
|
|
v |= v >> 4;
|
|
|
|
v |= v >> 8;
|
|
|
|
v |= v >> 16;
|
|
|
|
v++;
|
|
|
|
|
|
|
|
lines[line] = malloc(sizeof(line_t) + sizeof(char_t) * v);
|
|
|
|
lines[line]->available = v;
|
|
|
|
lines[line]->actual = remaining;
|
|
|
|
|
|
|
|
memmove(lines[line]->text, &lines[line-1]->text[split], sizeof(char_t) * remaining);
|
|
|
|
lines[line-1]->actual = split;
|
|
|
|
|
2012-09-09 07:20:38 +04:00
|
|
|
env->line_count += 1;
|
2012-09-09 02:23:10 +04:00
|
|
|
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2012-09-09 07:20:38 +04:00
|
|
|
void setup_buffer(buffer_t * env) {
|
|
|
|
if (env->lines) {
|
|
|
|
for (int i = 0; i < env->line_count; ++i) {
|
|
|
|
free(env->lines[i]);
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
2012-09-09 07:20:38 +04:00
|
|
|
free(env->lines);
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
2012-09-09 07:20:38 +04:00
|
|
|
env->line_no = 1;
|
|
|
|
env->col_no = 1;
|
|
|
|
env->line_count = 1; /* XXX */
|
|
|
|
env->modified = 0;
|
|
|
|
env->bottom_size = 2;
|
|
|
|
env->offset = 0;
|
|
|
|
env->line_avail = 8;
|
2012-09-08 12:47:19 +04:00
|
|
|
|
2012-09-09 07:20:38 +04:00
|
|
|
env->lines = malloc(sizeof(line_t *) * env->line_avail);
|
|
|
|
env->lines[0] = malloc(sizeof(line_t) + sizeof(char_t) * 32);
|
|
|
|
env->lines[0]->available = 32;
|
|
|
|
env->lines[0]->actual = 0;
|
2012-09-08 12:47:19 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#define COLOR_FG 230
|
|
|
|
#define COLOR_BG 235
|
|
|
|
#define COLOR_CURSOR 15
|
|
|
|
#define COLOR_ALT_FG 244
|
|
|
|
#define COLOR_ALT_BG 236
|
2012-12-01 03:50:06 +04:00
|
|
|
#define COLOR_NUMBER_BG 232
|
2012-09-08 12:47:19 +04:00
|
|
|
#define COLOR_NUMBER_FG 101
|
|
|
|
#define COLOR_STATUS_BG 238
|
|
|
|
#define COLOR_TABBAR_BG 230
|
|
|
|
#define COLOR_TAB_BG 248
|
|
|
|
#define COLOR_ERROR_FG 15
|
|
|
|
#define COLOR_ERROR_BG 196
|
|
|
|
|
|
|
|
uint32_t codepoint;
|
2012-09-09 01:37:48 +04:00
|
|
|
uint32_t codepoint_r;
|
2012-09-08 12:47:19 +04:00
|
|
|
uint32_t state = 0;
|
|
|
|
uint32_t istate = 0;
|
|
|
|
|
2012-09-09 01:37:48 +04:00
|
|
|
struct termios old;
|
2012-09-08 12:47:19 +04:00
|
|
|
|
|
|
|
void set_unbuffered() {
|
2012-09-09 01:37:48 +04:00
|
|
|
tcgetattr(fileno(stdin), &old);
|
|
|
|
struct termios new = old;
|
|
|
|
new.c_lflag &= (~ICANON & ~ECHO);
|
|
|
|
tcsetattr(fileno(stdin), TCSAFLUSH, &new);
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_buffered() {
|
2013-11-23 09:54:47 +04:00
|
|
|
tcsetattr(fileno(stdin), TCSAFLUSH, &old);
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
|
|
|
|
2014-04-29 11:29:19 +04:00
|
|
|
int to_eight(uint32_t codepoint, uint8_t * out) {
|
|
|
|
memset(out, 0x00, 7);
|
2012-09-08 12:47:19 +04:00
|
|
|
|
|
|
|
if (codepoint < 0x0080) {
|
|
|
|
out[0] = (uint8_t)codepoint;
|
|
|
|
} else if (codepoint < 0x0800) {
|
|
|
|
out[0] = 0xC0 | (codepoint >> 6);
|
|
|
|
out[1] = 0x80 | (codepoint & 0x3F);
|
2014-04-29 11:29:19 +04:00
|
|
|
} else if (codepoint < 0x10000) {
|
2012-09-08 12:47:19 +04:00
|
|
|
out[0] = 0xE0 | (codepoint >> 12);
|
|
|
|
out[1] = 0x80 | ((codepoint >> 6) & 0x3F);
|
|
|
|
out[2] = 0x80 | (codepoint & 0x3F);
|
2014-04-29 11:29:19 +04:00
|
|
|
} else if (codepoint < 0x200000) {
|
|
|
|
out[0] = 0xF0 | (codepoint >> 18);
|
|
|
|
out[1] = 0x80 | ((codepoint >> 12) & 0x3F);
|
|
|
|
out[2] = 0x80 | ((codepoint >> 6) & 0x3F);
|
|
|
|
out[3] = 0x80 | ((codepoint) & 0x3F);
|
|
|
|
} else if (codepoint < 0x4000000) {
|
|
|
|
out[0] = 0xF8 | (codepoint >> 24);
|
|
|
|
out[1] = 0x80 | (codepoint >> 18);
|
|
|
|
out[2] = 0x80 | ((codepoint >> 12) & 0x3F);
|
|
|
|
out[3] = 0x80 | ((codepoint >> 6) & 0x3F);
|
|
|
|
out[4] = 0x80 | ((codepoint) & 0x3F);
|
|
|
|
} else {
|
|
|
|
out[0] = 0xF8 | (codepoint >> 30);
|
|
|
|
out[1] = 0x80 | ((codepoint >> 24) & 0x3F);
|
|
|
|
out[2] = 0x80 | ((codepoint >> 18) & 0x3F);
|
|
|
|
out[3] = 0x80 | ((codepoint >> 12) & 0x3F);
|
|
|
|
out[4] = 0x80 | ((codepoint >> 6) & 0x3F);
|
|
|
|
out[5] = 0x80 | ((codepoint) & 0x3F);
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return strlen(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
int codepoint_width(uint16_t codepoint) {
|
|
|
|
if (codepoint < 32) {
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
if (codepoint > 256) {
|
2013-05-12 00:20:20 +04:00
|
|
|
return wcwidth(codepoint);
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void place_cursor(int x, int y) {
|
|
|
|
printf("\033[%d;%dH", y, x);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void place_cursor_h(int h) {
|
|
|
|
printf("\033[%dG", h);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_colors(int fg, int bg) {
|
|
|
|
printf("\033[48;5;%dm", bg);
|
|
|
|
printf("\033[38;5;%dm", fg);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear_to_end() {
|
|
|
|
printf("\033[K");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_bold() {
|
|
|
|
printf("\033[1m");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2012-09-09 07:20:38 +04:00
|
|
|
void set_underline() {
|
|
|
|
printf("\033[4m");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2012-09-08 12:47:19 +04:00
|
|
|
void reset() {
|
|
|
|
printf("\033[0m");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear_screen() {
|
|
|
|
printf("\033[H\033[2J");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void redraw_tabbar() {
|
|
|
|
place_cursor(1,1);
|
2012-09-09 07:20:38 +04:00
|
|
|
for (uint32_t i = 0; i < buffers_len; i++) {
|
|
|
|
buffer_t * _env = buffers[i];
|
|
|
|
if (_env == env) {
|
|
|
|
reset();
|
|
|
|
set_colors(COLOR_FG, COLOR_BG);
|
|
|
|
set_bold();
|
|
|
|
} else {
|
|
|
|
reset();
|
|
|
|
set_colors(COLOR_FG, COLOR_TAB_BG);
|
|
|
|
set_underline();
|
|
|
|
}
|
|
|
|
if (_env->modified) {
|
|
|
|
printf(" +");
|
|
|
|
}
|
|
|
|
if (_env->file_name) {
|
|
|
|
printf(" %s ", _env->file_name);
|
|
|
|
} else {
|
|
|
|
printf(" [No Name] ");
|
|
|
|
}
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
|
|
|
reset();
|
|
|
|
set_colors(COLOR_FG, COLOR_TABBAR_BG);
|
|
|
|
clear_to_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
int log_base_10(unsigned int v) {
|
|
|
|
int r = (v >= 1000000000) ? 9 : (v >= 100000000) ? 8 : (v >= 10000000) ? 7 :
|
|
|
|
(v >= 1000000) ? 6 : (v >= 100000) ? 5 : (v >= 10000) ? 4 :
|
|
|
|
(v >= 1000) ? 3 : (v >= 100) ? 2 : (v >= 10) ? 1 : 0;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-09-09 09:25:32 +04:00
|
|
|
void render_line(line_t * line, int width, int offset) {
|
2012-09-08 12:47:19 +04:00
|
|
|
uint32_t i = 0;
|
|
|
|
uint32_t j = 0;
|
|
|
|
set_colors(COLOR_FG, COLOR_BG);
|
|
|
|
while (i < line->actual) {
|
|
|
|
char_t c = line->text[i];
|
2012-09-09 09:25:32 +04:00
|
|
|
if (j >= offset) {
|
|
|
|
if (j - offset + c.display_width >= width) {
|
|
|
|
set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
|
|
|
|
while (j - offset < width - 1) {
|
|
|
|
printf("-");
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
printf(">");
|
|
|
|
break;
|
2012-09-09 01:37:48 +04:00
|
|
|
}
|
2012-09-09 09:25:32 +04:00
|
|
|
if (c.codepoint == '\t') {
|
|
|
|
set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
|
|
|
|
printf("»···");
|
|
|
|
set_colors(COLOR_FG, COLOR_BG);
|
|
|
|
} else if (c.codepoint < 32) {
|
|
|
|
set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
|
|
|
|
printf("<%02x>", c.codepoint);
|
|
|
|
set_colors(COLOR_FG, COLOR_BG);
|
|
|
|
} else {
|
2014-04-29 11:29:19 +04:00
|
|
|
char tmp[8];
|
2012-09-09 09:25:32 +04:00
|
|
|
to_eight(c.codepoint, tmp);
|
|
|
|
printf("%s", tmp);
|
|
|
|
}
|
|
|
|
} else if (j + c.display_width == offset + 1) {
|
2012-09-08 12:47:19 +04:00
|
|
|
set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
|
2012-09-09 09:25:32 +04:00
|
|
|
printf("<");
|
2012-09-08 12:47:19 +04:00
|
|
|
set_colors(COLOR_FG, COLOR_BG);
|
|
|
|
}
|
2012-09-09 09:25:32 +04:00
|
|
|
j += c.display_width;
|
2012-09-08 12:47:19 +04:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-09 09:25:32 +04:00
|
|
|
void realign_cursor() {
|
|
|
|
line_t * line = env->lines[env->line_no-1];
|
|
|
|
int x = -env->coffset;
|
|
|
|
int i = 0;
|
|
|
|
for (; i < env->col_no - 1; ++i) {
|
|
|
|
if (x + 12 > term_width) {
|
|
|
|
env->col_no = i + 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char_t * c = &env->lines[env->line_no-1]->text[i];
|
|
|
|
x += c->display_width;
|
|
|
|
}
|
|
|
|
while (x < 0) {
|
|
|
|
env->col_no += 1;
|
|
|
|
i++;
|
|
|
|
char_t * c = &env->lines[env->line_no-1]->text[i];
|
|
|
|
x += c->display_width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-08 12:47:19 +04:00
|
|
|
void redraw_text() {
|
2012-09-09 07:20:38 +04:00
|
|
|
int l = term_height - env->bottom_size - 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
int j = 0;
|
|
|
|
|
2012-09-09 07:20:38 +04:00
|
|
|
int num_size = log_base_10(env->line_count) + 2;
|
|
|
|
for (int x = env->offset; j < l && x < env->line_count; x++) {
|
2012-09-08 12:47:19 +04:00
|
|
|
place_cursor(1,2 + j);
|
|
|
|
/* draw line number */
|
|
|
|
set_colors(COLOR_NUMBER_FG, COLOR_ALT_FG);
|
|
|
|
printf(" ");
|
|
|
|
set_colors(COLOR_NUMBER_FG, COLOR_NUMBER_BG);
|
|
|
|
for (int y = 0; y < num_size - log_base_10(x + 1); ++y) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
printf("%d ", x + 1);
|
|
|
|
set_colors(COLOR_FG, COLOR_BG);
|
|
|
|
clear_to_end();
|
2012-09-09 09:25:32 +04:00
|
|
|
render_line(env->lines[x], term_width - 3 - num_size, env->coffset);
|
2012-09-08 12:47:19 +04:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
for (; j < l; ++j) {
|
|
|
|
place_cursor(1,2 + j);
|
|
|
|
set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
|
|
|
|
printf("~");
|
|
|
|
clear_to_end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void redraw_statusbar() {
|
2012-09-09 07:20:38 +04:00
|
|
|
place_cursor(1, term_height - 1);
|
2012-09-08 12:47:19 +04:00
|
|
|
set_colors(COLOR_FG, COLOR_STATUS_BG);
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->file_name) {
|
|
|
|
printf("%s", env->file_name);
|
2012-09-08 12:47:19 +04:00
|
|
|
} else {
|
|
|
|
printf("[No Name]");
|
|
|
|
}
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->modified) {
|
2012-09-08 12:47:19 +04:00
|
|
|
printf(" [+]");
|
|
|
|
}
|
|
|
|
clear_to_end();
|
|
|
|
char right_hand[1024];
|
2012-09-09 07:20:38 +04:00
|
|
|
sprintf(right_hand, "Line %d/%d Col: %d ", env->line_no, env->line_count, env->col_no);
|
|
|
|
place_cursor_h(term_width - strlen(right_hand));
|
2012-09-09 01:37:48 +04:00
|
|
|
printf("%s",right_hand);
|
2012-09-08 12:47:19 +04:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void redraw_commandline() {
|
2012-09-09 07:20:38 +04:00
|
|
|
place_cursor(1, term_height);
|
2012-09-08 12:47:19 +04:00
|
|
|
set_colors(COLOR_FG, COLOR_BG);
|
|
|
|
clear_to_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void redraw_all() {
|
|
|
|
redraw_tabbar();
|
|
|
|
redraw_text();
|
|
|
|
redraw_statusbar();
|
|
|
|
redraw_commandline();
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_title() {
|
|
|
|
char cwd[1024] = {'/',0};
|
2012-09-09 01:37:48 +04:00
|
|
|
getcwd(cwd, 1024);
|
2012-09-09 07:20:38 +04:00
|
|
|
printf("\033]1;%s%s (%s) - BIM\007", env->file_name, env->modified ? " +" : "", cwd);
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_modified() {
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->modified) return;
|
2012-09-08 12:47:19 +04:00
|
|
|
|
|
|
|
update_title();
|
2012-09-09 07:20:38 +04:00
|
|
|
env->modified = 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
redraw_tabbar();
|
|
|
|
redraw_statusbar();
|
|
|
|
}
|
|
|
|
|
|
|
|
void render_error(char * message) {
|
|
|
|
redraw_commandline();
|
|
|
|
set_colors(COLOR_ERROR_FG, COLOR_ERROR_BG);
|
|
|
|
printf("%s", message);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void render_cursor() {
|
|
|
|
printf("\033[1z");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void place_cursor_actual() {
|
2012-09-09 07:20:38 +04:00
|
|
|
int num_size = log_base_10(env->line_count) + 5;
|
2012-09-09 09:25:32 +04:00
|
|
|
int x = num_size + 1 - env->coffset;
|
2012-09-09 07:20:38 +04:00
|
|
|
for (int i = 0; i < env->col_no - 1; ++i) {
|
|
|
|
char_t * c = &env->lines[env->line_no-1]->text[i];
|
2012-09-08 12:47:19 +04:00
|
|
|
x += c->display_width;
|
|
|
|
}
|
2012-09-09 07:20:38 +04:00
|
|
|
int y = env->line_no - env->offset + 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
|
|
|
|
place_cursor(x,y);
|
2012-09-09 09:25:32 +04:00
|
|
|
csr_x_actual = x;
|
|
|
|
csr_y_actual = y;
|
|
|
|
|
2012-09-09 01:37:48 +04:00
|
|
|
#ifndef __linux__
|
2012-09-08 12:47:19 +04:00
|
|
|
render_cursor();
|
2012-09-09 01:37:48 +04:00
|
|
|
#endif
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void initialize() {
|
2012-09-09 07:20:38 +04:00
|
|
|
buffers_avail = 4;
|
|
|
|
buffers = malloc(sizeof(buffer_t *) * buffers_avail);
|
|
|
|
|
2012-09-09 01:37:48 +04:00
|
|
|
struct winsize w;
|
|
|
|
ioctl(0, TIOCGWINSZ, &w);
|
2012-09-09 07:20:38 +04:00
|
|
|
term_width = w.ws_col;
|
|
|
|
term_height = w.ws_row;
|
2012-09-08 12:47:19 +04:00
|
|
|
set_unbuffered();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void goto_line(int line) {
|
|
|
|
if (line < 1) line = 1;
|
2012-09-09 07:20:38 +04:00
|
|
|
if (line > env->line_count) line = env->line_count;
|
|
|
|
env->offset = line - 1;
|
|
|
|
env->line_no = line;
|
|
|
|
env->col_no = 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
redraw_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_buffer(uint8_t * buf, int size) {
|
|
|
|
for (int i = 0; i < size; ++i) {
|
2012-09-09 01:37:48 +04:00
|
|
|
if (!decode(&state, &codepoint_r, buf[i])) {
|
|
|
|
uint32_t c = codepoint_r;
|
2012-09-08 12:47:19 +04:00
|
|
|
if (c == '\n') {
|
2012-09-09 07:20:38 +04:00
|
|
|
env->lines = add_line(env->lines, env->line_no);
|
|
|
|
env->col_no = 1;
|
|
|
|
env->line_no += 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
} else {
|
|
|
|
char_t _c;
|
2012-09-09 01:37:48 +04:00
|
|
|
_c.codepoint = (uint16_t)c;
|
|
|
|
_c.display_width = codepoint_width((uint16_t)c);
|
2012-09-09 07:20:38 +04:00
|
|
|
line_t * line = env->lines[env->line_no - 1];
|
|
|
|
line_t * nline = line_insert(line, _c, env->col_no - 1);
|
2012-09-08 12:47:19 +04:00
|
|
|
if (line != nline) {
|
2012-09-09 07:20:38 +04:00
|
|
|
env->lines[env->line_no - 1] = nline;
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
2012-09-09 07:20:38 +04:00
|
|
|
env->col_no += 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
|
|
|
} else if (state == UTF8_REJECT) {
|
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void open_file(char * file) {
|
2012-09-09 07:20:38 +04:00
|
|
|
env = buffer_new();
|
2012-09-08 12:47:19 +04:00
|
|
|
|
2012-09-09 07:20:38 +04:00
|
|
|
env->file_name = malloc(strlen(file) + 1);
|
|
|
|
memcpy(env->file_name, file, strlen(file) + 1);
|
2012-09-08 12:47:19 +04:00
|
|
|
|
2012-09-09 07:20:38 +04:00
|
|
|
setup_buffer(env);
|
2012-09-08 12:47:19 +04:00
|
|
|
|
|
|
|
FILE * f = fopen(file, "r");
|
|
|
|
|
|
|
|
if (!f) {
|
|
|
|
char buf[1024];
|
2014-05-22 05:03:45 +04:00
|
|
|
snprintf(buf, 1024, "Could not open %s", file);
|
2012-09-08 12:47:19 +04:00
|
|
|
render_error(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
length = ftell(f);
|
|
|
|
fseek(f, 0, SEEK_SET);
|
|
|
|
|
|
|
|
uint8_t buf[BLOCK_SIZE];
|
|
|
|
|
|
|
|
while (length > BLOCK_SIZE) {
|
2012-09-09 01:37:48 +04:00
|
|
|
fread(buf, BLOCK_SIZE, 1, f);
|
2012-09-08 12:47:19 +04:00
|
|
|
add_buffer(buf, BLOCK_SIZE);
|
|
|
|
length -= BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
if (length > 0) {
|
|
|
|
fread(buf, 1, length, f);
|
|
|
|
add_buffer((uint8_t *)buf, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
update_title();
|
|
|
|
goto_line(0);
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void quit() {
|
|
|
|
set_buffered();
|
|
|
|
reset();
|
|
|
|
clear_screen();
|
|
|
|
printf("Thanks for flying bim!\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2012-09-09 07:20:38 +04:00
|
|
|
void try_quit() {
|
|
|
|
for (uint32_t i = 0; i < buffers_len; i++ ) {
|
|
|
|
buffer_t * _env = buffers[i];
|
|
|
|
if (_env->modified) {
|
|
|
|
char msg[100];
|
|
|
|
snprintf(msg, 100, "Modifications made to file `%s` in tab %d. Aborting.", _env->file_name, i+1);
|
|
|
|
render_error(msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void previous_tab() {
|
|
|
|
buffer_t * last = NULL;
|
|
|
|
for (uint32_t i = 0; i < buffers_len; i++) {
|
|
|
|
buffer_t * _env = buffers[i];
|
|
|
|
if (_env == env) {
|
|
|
|
if (last) {
|
|
|
|
env = last;
|
|
|
|
redraw_all();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
env = buffers[buffers_len-1];
|
|
|
|
redraw_all();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last = _env;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void next_tab() {
|
|
|
|
for (uint32_t i = 0; i < buffers_len; i++) {
|
|
|
|
buffer_t * _env = buffers[i];
|
|
|
|
if (_env == env) {
|
|
|
|
if (i != buffers_len - 1) {
|
|
|
|
env = buffers[i+1];
|
|
|
|
redraw_all();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
env = buffers[0];
|
|
|
|
redraw_all();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-08 12:47:19 +04:00
|
|
|
int isnumeric(char * str) {
|
|
|
|
char *p = str;
|
|
|
|
while (*p) {
|
|
|
|
if (*p < '0' || *p > '9') return 0;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-09-09 02:53:15 +04:00
|
|
|
void write_file(char * file) {
|
|
|
|
if (!file) {
|
|
|
|
render_error("Need a file to write to.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE * f = fopen(file, "w");
|
|
|
|
|
|
|
|
if (!f) {
|
|
|
|
render_error("Failed to open file for writing.");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t i, j;
|
2012-09-09 07:20:38 +04:00
|
|
|
for (i = 0; i < env->line_count; ++i) {
|
|
|
|
line_t * line = env->lines[i];
|
2012-09-09 02:53:15 +04:00
|
|
|
for (j = 0; j < line->actual; j++) {
|
|
|
|
char_t c = line->text[j];
|
2012-09-10 07:48:43 +04:00
|
|
|
if (c.codepoint == 0) {
|
|
|
|
char buf[1] = {0};
|
|
|
|
fwrite(buf, 1, 1, f);
|
|
|
|
} else {
|
|
|
|
char tmp[4];
|
|
|
|
int i = to_eight(c.codepoint, tmp);
|
|
|
|
fwrite(tmp, i, 1, f);
|
|
|
|
}
|
2012-09-09 02:53:15 +04:00
|
|
|
}
|
2012-09-09 07:20:38 +04:00
|
|
|
if (i + 1 < env->line_count) {
|
2012-09-10 07:48:43 +04:00
|
|
|
fputc('\n', f);
|
2012-09-09 02:53:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
2012-09-09 07:20:38 +04:00
|
|
|
env->modified = 0;
|
|
|
|
if (!env->file_name) {
|
|
|
|
env->file_name = malloc(strlen(file) + 1);
|
|
|
|
memcpy(env->file_name, file, strlen(file) + 1);
|
2012-09-09 02:53:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
redraw_all();
|
|
|
|
}
|
|
|
|
|
2012-09-08 12:47:19 +04:00
|
|
|
void process_command(char * cmd) {
|
|
|
|
char *p, *argv[512], *last;
|
|
|
|
int argc = 0;
|
|
|
|
for ((p = strtok_r(cmd, " ", &last)); p;
|
|
|
|
(p = strtok_r(NULL, " ", &last)), argc++) {
|
|
|
|
if (argc < 511) argv[argc] = p;
|
|
|
|
}
|
|
|
|
argv[argc] = NULL;
|
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
/* no op */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[0], "e")) {
|
|
|
|
if (argc > 1) {
|
|
|
|
open_file(argv[1]);
|
|
|
|
} else {
|
|
|
|
render_error("Expected a file to open...");
|
|
|
|
}
|
2012-09-09 02:53:15 +04:00
|
|
|
} else if (!strcmp(argv[0], "w")) {
|
|
|
|
if (argc > 1) {
|
|
|
|
write_file(argv[1]);
|
|
|
|
} else {
|
2012-09-09 07:20:38 +04:00
|
|
|
write_file(env->file_name);
|
2012-09-09 02:53:15 +04:00
|
|
|
}
|
2012-09-08 12:47:19 +04:00
|
|
|
} else if (!strcmp(argv[0], "q")) {
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->modified) {
|
2012-09-08 12:47:19 +04:00
|
|
|
render_error("No write since last change. Use :q! to force exit.");
|
|
|
|
} else {
|
2012-09-09 07:20:38 +04:00
|
|
|
buffer_t * previous_env = env;
|
|
|
|
buffer_t * new_env = buffer_close(env);
|
|
|
|
if (new_env == previous_env) {
|
|
|
|
render_error("lolwat");
|
|
|
|
}
|
|
|
|
if (!new_env) {
|
|
|
|
quit();
|
|
|
|
}
|
|
|
|
free(previous_env);
|
|
|
|
env = new_env;
|
|
|
|
redraw_all();
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
2012-09-09 07:20:38 +04:00
|
|
|
} else if (!strcmp(argv[0], "qall")) {
|
|
|
|
try_quit();
|
2012-09-08 12:47:19 +04:00
|
|
|
} else if (!strcmp(argv[0], "q!")) {
|
|
|
|
quit();
|
2012-09-09 07:20:38 +04:00
|
|
|
} else if (!strcmp(argv[0], "tabp")) {
|
|
|
|
previous_tab();
|
|
|
|
} else if (!strcmp(argv[0], "tabn")) {
|
|
|
|
next_tab();
|
2012-09-08 12:47:19 +04:00
|
|
|
} else if (isnumeric(argv[0])) {
|
|
|
|
goto_line(atoi(argv[0]));
|
|
|
|
} else {
|
|
|
|
char buf[512];
|
|
|
|
snprintf(buf, 512, "Not an editor command: %s", argv[0]);
|
|
|
|
render_error(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void command_mode() {
|
|
|
|
char c;
|
|
|
|
char buffer[1024] = {0};
|
|
|
|
int buffer_len = 0;
|
|
|
|
|
|
|
|
redraw_commandline();
|
|
|
|
printf(":");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
while (c = fgetc(stdin)) {
|
|
|
|
if (c == '\033') {
|
|
|
|
break;
|
2012-09-09 01:37:48 +04:00
|
|
|
} else if (c == ENTER_KEY) {
|
2012-09-08 12:47:19 +04:00
|
|
|
process_command(buffer);
|
|
|
|
break;
|
2012-09-09 01:37:48 +04:00
|
|
|
} else if (c == BACKSPACE_KEY) {
|
2012-09-08 12:47:19 +04:00
|
|
|
if (buffer_len > 0) {
|
|
|
|
buffer_len -= 1;
|
2012-09-09 01:37:48 +04:00
|
|
|
buffer[buffer_len] = '\0';
|
|
|
|
redraw_commandline();
|
|
|
|
printf(":%s", buffer);
|
2012-09-08 12:47:19 +04:00
|
|
|
fflush(stdout);
|
|
|
|
} else {
|
|
|
|
redraw_commandline();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buffer[buffer_len] = c;
|
|
|
|
buffer_len++;
|
|
|
|
printf("%c", c);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void insert_mode() {
|
2012-09-09 02:53:15 +04:00
|
|
|
uint8_t cin;
|
|
|
|
uint32_t c;
|
2012-09-08 12:47:19 +04:00
|
|
|
redraw_commandline();
|
|
|
|
set_bold();
|
|
|
|
printf("-- INSERT --");
|
|
|
|
reset();
|
|
|
|
place_cursor_actual();
|
|
|
|
set_colors(COLOR_FG, COLOR_BG);
|
|
|
|
while (cin = fgetc(stdin)) {
|
|
|
|
if (!decode(&istate, &c, cin)) {
|
|
|
|
switch (c) {
|
|
|
|
case '\033':
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->col_no > env->lines[env->line_no-1]->actual) {
|
|
|
|
env->col_no = env->lines[env->line_no-1]->actual;
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->col_no == 0) env->col_no = 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
redraw_commandline();
|
|
|
|
return;
|
2012-09-09 01:37:48 +04:00
|
|
|
case BACKSPACE_KEY:
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->col_no > 1) {
|
|
|
|
line_delete(env->lines[env->line_no - 1], env->col_no - 1);
|
|
|
|
env->col_no -= 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
redraw_text();
|
|
|
|
set_modified();
|
2012-09-08 13:39:45 +04:00
|
|
|
redraw_statusbar();
|
2012-09-08 12:47:19 +04:00
|
|
|
place_cursor_actual();
|
|
|
|
}
|
|
|
|
break;
|
2012-09-09 01:37:48 +04:00
|
|
|
case ENTER_KEY:
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->col_no == env->lines[env->line_no - 1]->actual + 1) {
|
|
|
|
env->lines = add_line(env->lines, env->line_no);
|
2012-09-08 12:47:19 +04:00
|
|
|
} else {
|
|
|
|
/* oh oh god we're all gonna die */
|
2012-09-09 07:20:38 +04:00
|
|
|
env->lines = split_line(env->lines, env->line_no, env->col_no - 1);
|
2012-09-09 02:23:10 +04:00
|
|
|
}
|
2012-09-09 07:20:38 +04:00
|
|
|
env->col_no = 1;
|
|
|
|
env->line_no += 1;
|
|
|
|
if (env->line_no > env->offset + term_height - env->bottom_size - 1) {
|
|
|
|
env->offset += 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
2012-09-09 02:23:10 +04:00
|
|
|
redraw_text();
|
|
|
|
set_modified();
|
|
|
|
redraw_statusbar();
|
|
|
|
place_cursor_actual();
|
2012-09-08 12:47:19 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
char_t _c;
|
|
|
|
_c.codepoint = c;
|
|
|
|
_c.display_width = codepoint_width(c);
|
2012-09-09 07:20:38 +04:00
|
|
|
line_t * line = env->lines[env->line_no - 1];
|
|
|
|
line_t * nline = line_insert(line, _c, env->col_no - 1);
|
2012-09-08 12:47:19 +04:00
|
|
|
if (line != nline) {
|
2012-09-09 07:20:38 +04:00
|
|
|
env->lines[env->line_no - 1] = nline;
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
|
|
|
redraw_text(); /* XXX */
|
2012-09-09 07:20:38 +04:00
|
|
|
env->col_no += 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
set_modified();
|
2012-09-08 13:39:45 +04:00
|
|
|
redraw_statusbar();
|
2012-09-08 12:47:19 +04:00
|
|
|
place_cursor_actual();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (istate == UTF8_REJECT) {
|
|
|
|
istate = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char * argv[]) {
|
|
|
|
initialize();
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
open_file(argv[1]);
|
2012-09-09 07:20:38 +04:00
|
|
|
} else {
|
|
|
|
env = buffer_new();
|
|
|
|
update_title();
|
|
|
|
setup_buffer(env);
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
redraw_all();
|
|
|
|
place_cursor_actual();
|
|
|
|
char buf[1];
|
|
|
|
char c;
|
|
|
|
while (c = fgetc(stdin)) {
|
|
|
|
switch (c) {
|
|
|
|
case '\033':
|
|
|
|
redraw_all();
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
/* Switch to command mode */
|
|
|
|
command_mode();
|
|
|
|
break;
|
|
|
|
case 'j':
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->line_no < env->line_count) {
|
|
|
|
env->line_no += 1;
|
|
|
|
if (env->col_no > env->lines[env->line_no-1]->actual) {
|
|
|
|
env->col_no = env->lines[env->line_no-1]->actual;
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->col_no == 0) env->col_no = 1;
|
|
|
|
if (env->line_no > env->offset + term_height - env->bottom_size - 1) {
|
|
|
|
env->offset += 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
redraw_text();
|
|
|
|
}
|
2012-09-08 13:39:45 +04:00
|
|
|
redraw_statusbar();
|
2012-09-08 12:47:19 +04:00
|
|
|
place_cursor_actual();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'k':
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->line_no > 1) {
|
|
|
|
env->line_no -= 1;
|
|
|
|
if (env->col_no > env->lines[env->line_no-1]->actual) {
|
|
|
|
env->col_no = env->lines[env->line_no-1]->actual;
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->col_no == 0) env->col_no = 1;
|
|
|
|
if (env->line_no <= env->offset) {
|
|
|
|
env->offset -= 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
redraw_text();
|
|
|
|
}
|
2012-09-08 13:39:45 +04:00
|
|
|
redraw_statusbar();
|
2012-09-08 12:47:19 +04:00
|
|
|
place_cursor_actual();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'h':
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->col_no > 1) {
|
|
|
|
env->col_no -= 1;
|
2012-09-08 13:39:45 +04:00
|
|
|
redraw_statusbar();
|
2012-09-08 12:47:19 +04:00
|
|
|
place_cursor_actual();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'l':
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->col_no < env->lines[env->line_no-1]->actual) {
|
|
|
|
env->col_no += 1;
|
2012-09-08 13:39:45 +04:00
|
|
|
redraw_statusbar();
|
2012-09-08 12:47:19 +04:00
|
|
|
place_cursor_actual();
|
|
|
|
}
|
|
|
|
break;
|
2012-09-09 01:37:48 +04:00
|
|
|
case ' ':
|
2012-09-09 07:20:38 +04:00
|
|
|
goto_line(env->line_no + term_height - 6);
|
2012-09-09 01:37:48 +04:00
|
|
|
break;
|
2012-09-08 12:47:19 +04:00
|
|
|
case 'O':
|
|
|
|
{
|
2012-09-09 07:20:38 +04:00
|
|
|
env->lines = add_line(env->lines, env->line_no-1);
|
|
|
|
env->col_no = 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
redraw_text();
|
|
|
|
set_modified();
|
|
|
|
place_cursor_actual();
|
|
|
|
goto _insert;
|
|
|
|
}
|
|
|
|
case 'o':
|
|
|
|
{
|
2012-09-09 07:20:38 +04:00
|
|
|
env->lines = add_line(env->lines, env->line_no);
|
|
|
|
env->col_no = 1;
|
|
|
|
env->line_no += 1;
|
|
|
|
if (env->line_no > env->offset + term_height - env->bottom_size - 1) {
|
|
|
|
env->offset += 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
|
|
|
redraw_text();
|
|
|
|
set_modified();
|
|
|
|
place_cursor_actual();
|
|
|
|
goto _insert;
|
|
|
|
}
|
2012-09-09 09:25:32 +04:00
|
|
|
case ',':
|
|
|
|
if (env->coffset > 5) {
|
|
|
|
env->coffset -= 5;
|
|
|
|
} else {
|
|
|
|
env->coffset = 0;
|
|
|
|
}
|
|
|
|
realign_cursor();
|
|
|
|
redraw_all();
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
env->coffset += 5;
|
|
|
|
realign_cursor();
|
|
|
|
redraw_all();
|
|
|
|
break;
|
2012-09-08 12:47:19 +04:00
|
|
|
case 'a':
|
2012-09-09 07:20:38 +04:00
|
|
|
if (env->col_no < env->lines[env->line_no-1]->actual + 1) {
|
|
|
|
env->col_no += 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
}
|
|
|
|
goto _insert;
|
|
|
|
case '$':
|
2012-09-09 07:20:38 +04:00
|
|
|
env->col_no = env->lines[env->line_no-1]->actual+1;
|
2012-09-08 12:47:19 +04:00
|
|
|
break;
|
|
|
|
case '0':
|
2012-09-09 07:20:38 +04:00
|
|
|
env->col_no = 1;
|
2012-09-08 12:47:19 +04:00
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
_insert:
|
|
|
|
insert_mode();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
place_cursor_actual();
|
|
|
|
}
|
|
|
|
_continue:
|
|
|
|
printf("%c", c);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (argc < 2) {
|
|
|
|
fprintf(stderr, "%s: argument expected\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Writing out file again:\n\n");
|
|
|
|
|
|
|
|
for (int i = 0; i < file_len_unicode; ++i) {
|
|
|
|
uint8_t buf[4];
|
|
|
|
int len = to_eight(file_buffer[i], buf);
|
|
|
|
fwrite(buf, len, 1, stdout);
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|