Add mouse support
This commit is contained in:
@@ -6,8 +6,6 @@ A TUI IDE.
|
|||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
|
|
||||||
- [ ] Fix problem highlighting selection when line is empty.
|
|
||||||
- [ ] Add mouse support.
|
|
||||||
- [ ] Add struct as union of structs that can take input. or similar
|
- [ ] Add struct as union of structs that can take input. or similar
|
||||||
- then send input to them.
|
- then send input to them.
|
||||||
- Also background thread will run worker function for whatever struct is selected.
|
- Also background thread will run worker function for whatever struct is selected.
|
||||||
|
|||||||
@@ -115,8 +115,10 @@ void scroll_up(Editor *editor, uint32_t number);
|
|||||||
void scroll_down(Editor *editor, uint32_t number);
|
void scroll_down(Editor *editor, uint32_t number);
|
||||||
void ensure_cursor(Editor *editor);
|
void ensure_cursor(Editor *editor);
|
||||||
void ensure_scroll(Editor *editor);
|
void ensure_scroll(Editor *editor);
|
||||||
|
void handle_editor_event(Editor *editor, KeyEvent event);
|
||||||
void apply_edit(std::vector<Span> &spans, uint32_t x, int64_t y);
|
void apply_edit(std::vector<Span> &spans, uint32_t x, int64_t y);
|
||||||
void edit_erase(Editor *editor, Coord pos, int64_t len);
|
void edit_erase(Editor *editor, Coord pos, int64_t len);
|
||||||
void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len);
|
void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len);
|
||||||
|
Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -10,5 +10,6 @@
|
|||||||
#define RUNNER 3
|
#define RUNNER 3
|
||||||
|
|
||||||
extern std::atomic<bool> running;
|
extern std::atomic<bool> running;
|
||||||
|
extern uint8_t mode;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -73,13 +73,8 @@ void render_editor(Editor *editor) {
|
|||||||
editor->cursor.col;
|
editor->cursor.col;
|
||||||
uint32_t sel2 = line_to_byte(editor->root, editor->selection.row, nullptr) +
|
uint32_t sel2 = line_to_byte(editor->root, editor->selection.row, nullptr) +
|
||||||
editor->selection.col;
|
editor->selection.col;
|
||||||
if (sel1 <= sel2) {
|
sel_start = MIN(sel1, sel2);
|
||||||
sel_start = sel1;
|
sel_end = MAX(sel1, sel2);
|
||||||
sel_end = sel2;
|
|
||||||
} else {
|
|
||||||
sel_start = sel2;
|
|
||||||
sel_end = sel1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Coord cursor = {UINT32_MAX, UINT32_MAX};
|
Coord cursor = {UINT32_MAX, UINT32_MAX};
|
||||||
uint32_t line_index = editor->scroll.row;
|
uint32_t line_index = editor->scroll.row;
|
||||||
|
|||||||
@@ -2,191 +2,234 @@ extern "C" {
|
|||||||
#include "../libs/libgrapheme/grapheme.h"
|
#include "../libs/libgrapheme/grapheme.h"
|
||||||
}
|
}
|
||||||
#include "../include/editor.h"
|
#include "../include/editor.h"
|
||||||
|
#include "../include/main.h"
|
||||||
#include "../include/utils.h"
|
#include "../include/utils.h"
|
||||||
|
|
||||||
void cursor_down(Editor *editor, uint32_t number) {
|
void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||||
if (!editor || !editor->root || number == 0)
|
if (event.key_type == KEY_SPECIAL) {
|
||||||
return;
|
switch (event.special_key) {
|
||||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
case KEY_DOWN:
|
||||||
uint32_t len;
|
cursor_down(editor, 1);
|
||||||
char *line_content = next_line(it, &len);
|
|
||||||
if (line_content == nullptr)
|
|
||||||
return;
|
|
||||||
if (editor->cursor_preffered == UINT32_MAX)
|
|
||||||
editor->cursor_preffered =
|
|
||||||
get_visual_col_from_bytes(line_content, len, editor->cursor.col);
|
|
||||||
uint32_t visual_col = editor->cursor_preffered;
|
|
||||||
do {
|
|
||||||
free(line_content);
|
|
||||||
line_content = next_line(it, &len);
|
|
||||||
editor->cursor.row += 1;
|
|
||||||
if (editor->cursor.row >= editor->root->line_count) {
|
|
||||||
editor->cursor.row = editor->root->line_count - 1;
|
|
||||||
break;
|
break;
|
||||||
};
|
case KEY_UP:
|
||||||
if (editor->folded[editor->cursor.row] != 0)
|
cursor_up(editor, 1);
|
||||||
number++;
|
break;
|
||||||
} while (--number > 0);
|
case KEY_LEFT:
|
||||||
free(it);
|
cursor_left(editor, 1);
|
||||||
if (line_content == nullptr)
|
break;
|
||||||
return;
|
case KEY_RIGHT:
|
||||||
editor->cursor.col = get_bytes_from_visual_col(line_content, len, visual_col);
|
cursor_right(editor, 1);
|
||||||
free(line_content);
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (event.key_type == KEY_MOUSE) {
|
||||||
|
switch (event.mouse_state) {
|
||||||
|
case SCROLL:
|
||||||
|
switch (event.mouse_direction) {
|
||||||
|
case SCROLL_UP:
|
||||||
|
scroll_up(editor, 10);
|
||||||
|
ensure_cursor(editor);
|
||||||
|
break;
|
||||||
|
case SCROLL_DOWN:
|
||||||
|
scroll_down(editor, 10);
|
||||||
|
ensure_cursor(editor);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case PRESS:
|
||||||
|
if (event.mouse_button == LEFT_BTN) {
|
||||||
|
Coord p = editor_hit_test(editor, event.mouse_x, event.mouse_y);
|
||||||
|
editor->cursor = p;
|
||||||
|
editor->cursor_preffered = UINT32_MAX;
|
||||||
|
editor->selection = p;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case DRAG:
|
||||||
|
if (event.mouse_button == LEFT_BTN) {
|
||||||
|
Coord p = editor_hit_test(editor, event.mouse_x, event.mouse_y);
|
||||||
|
editor->cursor = p;
|
||||||
|
editor->cursor_preffered = UINT32_MAX;
|
||||||
|
editor->selection_active = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case RELEASE:
|
||||||
|
if (event.mouse_button == LEFT_BTN)
|
||||||
|
if (editor->cursor.row == editor->selection.row &&
|
||||||
|
editor->cursor.col == editor->selection.col)
|
||||||
|
editor->selection_active = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch (mode) {
|
||||||
|
case NORMAL:
|
||||||
|
if (event.key_type == KEY_CHAR && event.len == 1) {
|
||||||
|
switch (event.c[0]) {
|
||||||
|
case 'a':
|
||||||
|
case 'i':
|
||||||
|
mode = INSERT;
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
case 'v':
|
||||||
|
mode = SELECT;
|
||||||
|
editor->selection_active = true;
|
||||||
|
editor->selection = editor->cursor;
|
||||||
|
break;
|
||||||
|
case ';':
|
||||||
|
case ':':
|
||||||
|
mode = RUNNER;
|
||||||
|
break;
|
||||||
|
case 0x7F:
|
||||||
|
cursor_left(editor, 1);
|
||||||
|
break;
|
||||||
|
case ' ':
|
||||||
|
cursor_right(editor, 1);
|
||||||
|
break;
|
||||||
|
case '\r':
|
||||||
|
case '\n':
|
||||||
|
cursor_down(editor, 1);
|
||||||
|
break;
|
||||||
|
case '\\':
|
||||||
|
case '|':
|
||||||
|
cursor_up(editor, 1);
|
||||||
|
break;
|
||||||
|
case CTRL('d'):
|
||||||
|
scroll_down(editor, 1);
|
||||||
|
ensure_cursor(editor);
|
||||||
|
break;
|
||||||
|
case CTRL('u'):
|
||||||
|
scroll_up(editor, 1);
|
||||||
|
ensure_cursor(editor);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case INSERT:
|
||||||
|
if (event.key_type == KEY_CHAR) {
|
||||||
|
if (event.len == 1) {
|
||||||
|
if (event.c[0] == '\t') {
|
||||||
|
edit_insert(editor, editor->cursor, (char *)" ", 1);
|
||||||
|
cursor_right(editor, 2);
|
||||||
|
} else if (event.c[0] == '\n' || event.c[0] == '\r') {
|
||||||
|
edit_insert(editor, editor->cursor, (char *)"\n", 1);
|
||||||
|
cursor_right(editor, 1);
|
||||||
|
} else if (event.c[0] == 0x7F) {
|
||||||
|
edit_erase(editor, editor->cursor, -1);
|
||||||
|
} else if (isprint((unsigned char)(event.c[0]))) {
|
||||||
|
edit_insert(editor, editor->cursor, event.c, 1);
|
||||||
|
cursor_right(editor, 1);
|
||||||
|
} else if (event.c[0] == 0x1B) {
|
||||||
|
mode = NORMAL;
|
||||||
|
}
|
||||||
|
} else if (event.len > 1) {
|
||||||
|
edit_insert(editor, editor->cursor, event.c, event.len);
|
||||||
|
cursor_right(editor, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (event.key_type == KEY_SPECIAL && event.special_key == KEY_DELETE)
|
||||||
|
edit_erase(editor, editor->cursor, 1);
|
||||||
|
break;
|
||||||
|
case SELECT:
|
||||||
|
if (event.key_type == KEY_CHAR && event.len == 1) {
|
||||||
|
switch (event.c[0]) {
|
||||||
|
case 0x1B:
|
||||||
|
case 's':
|
||||||
|
case 'v':
|
||||||
|
editor->selection_active = false;
|
||||||
|
mode = NORMAL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case RUNNER:
|
||||||
|
if (event.key_type == KEY_CHAR && event.len == 1) {
|
||||||
|
switch (event.c[0]) {
|
||||||
|
case 0x1B:
|
||||||
|
mode = NORMAL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ensure_scroll(editor);
|
||||||
|
if (event.key_type == KEY_CHAR && event.c)
|
||||||
|
free(event.c);
|
||||||
}
|
}
|
||||||
|
|
||||||
void cursor_up(Editor *editor, uint32_t number) {
|
Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
|
||||||
if (!editor || !editor->root || number == 0)
|
uint32_t target_visual_row = y;
|
||||||
return;
|
uint32_t visual_row = 0;
|
||||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
uint32_t line_index = editor->scroll.row;
|
||||||
uint32_t len;
|
bool first_visual_line = true;
|
||||||
char *line_content = next_line(it, &len);
|
std::shared_lock knot_lock(editor->knot_mtx);
|
||||||
if (!line_content) {
|
LineIterator *it = begin_l_iter(editor->root, line_index);
|
||||||
|
if (!it)
|
||||||
|
return editor->scroll;
|
||||||
|
while (visual_row <= target_visual_row) {
|
||||||
|
if (editor->folded[line_index]) {
|
||||||
|
if (editor->folded[line_index] == 2) {
|
||||||
|
if (visual_row == target_visual_row) {
|
||||||
free(it);
|
free(it);
|
||||||
return;
|
return {line_index, 0};
|
||||||
}
|
}
|
||||||
if (editor->cursor_preffered == UINT32_MAX)
|
visual_row++;
|
||||||
editor->cursor_preffered =
|
}
|
||||||
get_visual_col_from_bytes(line_content, len, editor->cursor.col);
|
do {
|
||||||
uint32_t visual_col = editor->cursor_preffered;
|
char *l = next_line(it, nullptr);
|
||||||
free(line_content);
|
if (!l)
|
||||||
while (number > 0 && editor->cursor.row > 0) {
|
break;
|
||||||
editor->cursor.row--;
|
free(l);
|
||||||
if (editor->folded[editor->cursor.row] != 0)
|
line_index++;
|
||||||
|
} while (editor->folded[line_index] == 1);
|
||||||
continue;
|
continue;
|
||||||
number--;
|
|
||||||
}
|
}
|
||||||
free(it);
|
|
||||||
it = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
line_content = next_line(it, &len);
|
|
||||||
if (!line_content) {
|
|
||||||
free(it);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
editor->cursor.col = get_bytes_from_visual_col(line_content, len, visual_col);
|
|
||||||
free(line_content);
|
|
||||||
free(it);
|
|
||||||
}
|
|
||||||
|
|
||||||
void cursor_right(Editor *editor, uint32_t number) {
|
|
||||||
if (!editor || !editor->root || number == 0)
|
|
||||||
return;
|
|
||||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
uint32_t line_len;
|
uint32_t line_len;
|
||||||
char *line = next_line(it, &line_len);
|
char *line = next_line(it, &line_len);
|
||||||
free(it);
|
|
||||||
if (!line)
|
|
||||||
return;
|
|
||||||
if (line[line_len - 1] == '\n')
|
|
||||||
--line_len;
|
|
||||||
while (number > 0) {
|
|
||||||
if (editor->cursor.col >= line_len) {
|
|
||||||
free(line);
|
|
||||||
line = nullptr;
|
|
||||||
uint32_t next_row = editor->cursor.row + 1;
|
|
||||||
while (next_row < editor->root->line_count &&
|
|
||||||
editor->folded[next_row] != 0)
|
|
||||||
next_row++;
|
|
||||||
if (next_row >= editor->root->line_count) {
|
|
||||||
editor->cursor.col = line_len;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
editor->cursor.row = next_row;
|
|
||||||
editor->cursor.col = 0;
|
|
||||||
it = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
line = next_line(it, &line_len);
|
|
||||||
free(it);
|
|
||||||
if (!line)
|
if (!line)
|
||||||
break;
|
break;
|
||||||
if (line[line_len - 1] == '\n')
|
if (line_len && line[line_len - 1] == '\n')
|
||||||
--line_len;
|
line_len--;
|
||||||
} else {
|
uint32_t offset = first_visual_line ? editor->scroll.col : 0;
|
||||||
uint32_t inc = grapheme_next_character_break_utf8(
|
first_visual_line = false;
|
||||||
line + editor->cursor.col, line_len - editor->cursor.col);
|
while (offset < line_len || (line_len == 0 && offset == 0)) {
|
||||||
if (inc == 0)
|
uint32_t col = 0;
|
||||||
|
uint32_t advance = 0;
|
||||||
|
uint32_t left = line_len - offset;
|
||||||
|
uint32_t last_good_offset = offset;
|
||||||
|
while (left > 0 && col < editor->size.col) {
|
||||||
|
uint32_t g =
|
||||||
|
grapheme_next_character_break_utf8(line + offset + advance, left);
|
||||||
|
int w = display_width(line + offset + advance, g);
|
||||||
|
if (col + w > editor->size.col)
|
||||||
break;
|
break;
|
||||||
editor->cursor.col += inc;
|
if (visual_row == target_visual_row && x < col + w) {
|
||||||
}
|
|
||||||
number--;
|
|
||||||
}
|
|
||||||
LineIterator *it2 = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
uint32_t len2;
|
|
||||||
char *cur_line = next_line(it2, &len2);
|
|
||||||
free(it2);
|
|
||||||
if (cur_line) {
|
|
||||||
if (len2 > 0 && cur_line[len2 - 1] == '\n')
|
|
||||||
--len2;
|
|
||||||
editor->cursor_preffered =
|
|
||||||
get_visual_col_from_bytes(cur_line, len2, editor->cursor.col);
|
|
||||||
free(cur_line);
|
|
||||||
} else {
|
|
||||||
editor->cursor_preffered = UINT32_MAX;
|
|
||||||
}
|
|
||||||
if (line)
|
|
||||||
free(line);
|
free(line);
|
||||||
}
|
|
||||||
|
|
||||||
void cursor_left(Editor *editor, uint32_t number) {
|
|
||||||
if (!editor || !editor->root || number == 0)
|
|
||||||
return;
|
|
||||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
uint32_t len;
|
|
||||||
char *line = next_line(it, &len);
|
|
||||||
free(it);
|
free(it);
|
||||||
if (!line)
|
return {line_index, offset + advance + g};
|
||||||
return;
|
}
|
||||||
if (line[len - 1] == '\n')
|
advance += g;
|
||||||
line[--len] = '\0';
|
last_good_offset = offset + advance;
|
||||||
while (number > 0) {
|
left -= g;
|
||||||
if (editor->cursor.col == 0) {
|
col += w;
|
||||||
|
}
|
||||||
|
if (visual_row == target_visual_row) {
|
||||||
free(line);
|
free(line);
|
||||||
line = nullptr;
|
|
||||||
if (editor->cursor.row == 0)
|
|
||||||
break;
|
|
||||||
int32_t prev_row = editor->cursor.row - 1;
|
|
||||||
while (prev_row >= 0 && editor->folded[prev_row] != 0)
|
|
||||||
prev_row--;
|
|
||||||
if (prev_row < 0)
|
|
||||||
break;
|
|
||||||
editor->cursor.row = prev_row;
|
|
||||||
it = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
line = next_line(it, &len);
|
|
||||||
free(it);
|
free(it);
|
||||||
if (!line)
|
return {line_index, last_good_offset};
|
||||||
|
}
|
||||||
|
visual_row++;
|
||||||
|
if (visual_row > target_visual_row)
|
||||||
break;
|
break;
|
||||||
if (line[len - 1] == '\n')
|
if (advance == 0)
|
||||||
--len;
|
break;
|
||||||
editor->cursor.col = len;
|
offset += advance;
|
||||||
} else {
|
if (line_len == 0)
|
||||||
uint32_t col = editor->cursor.col;
|
|
||||||
uint32_t new_col = 0;
|
|
||||||
uint32_t visual_col = 0;
|
|
||||||
while (new_col < col) {
|
|
||||||
uint32_t inc =
|
|
||||||
grapheme_next_character_break_utf8(line + new_col, len - new_col);
|
|
||||||
if (new_col + inc >= col)
|
|
||||||
break;
|
break;
|
||||||
new_col += inc;
|
|
||||||
visual_col++;
|
|
||||||
}
|
}
|
||||||
editor->cursor.col = new_col;
|
|
||||||
}
|
|
||||||
number--;
|
|
||||||
}
|
|
||||||
LineIterator *it2 = begin_l_iter(editor->root, editor->cursor.row);
|
|
||||||
uint32_t len2;
|
|
||||||
char *cur_line = next_line(it2, &len2);
|
|
||||||
free(it2);
|
|
||||||
if (cur_line) {
|
|
||||||
if (len2 > 0 && cur_line[len2 - 1] == '\n')
|
|
||||||
--len2;
|
|
||||||
editor->cursor_preffered =
|
|
||||||
get_visual_col_from_bytes(cur_line, len2, editor->cursor.col);
|
|
||||||
free(cur_line);
|
|
||||||
} else {
|
|
||||||
editor->cursor_preffered = UINT32_MAX;
|
|
||||||
}
|
|
||||||
if (line)
|
|
||||||
free(line);
|
free(line);
|
||||||
|
line_index++;
|
||||||
|
}
|
||||||
|
free(it);
|
||||||
|
return editor->scroll;
|
||||||
}
|
}
|
||||||
|
|
||||||
Coord move_right(Editor *editor, Coord cursor, uint32_t number) {
|
Coord move_right(Editor *editor, Coord cursor, uint32_t number) {
|
||||||
@@ -294,6 +337,83 @@ Coord move_left(Editor *editor, Coord cursor, uint32_t number) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void cursor_down(Editor *editor, uint32_t number) {
|
||||||
|
if (!editor || !editor->root || number == 0)
|
||||||
|
return;
|
||||||
|
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||||
|
uint32_t len;
|
||||||
|
char *line_content = next_line(it, &len);
|
||||||
|
if (line_content == nullptr)
|
||||||
|
return;
|
||||||
|
if (editor->cursor_preffered == UINT32_MAX)
|
||||||
|
editor->cursor_preffered =
|
||||||
|
get_visual_col_from_bytes(line_content, len, editor->cursor.col);
|
||||||
|
uint32_t visual_col = editor->cursor_preffered;
|
||||||
|
do {
|
||||||
|
free(line_content);
|
||||||
|
line_content = next_line(it, &len);
|
||||||
|
editor->cursor.row += 1;
|
||||||
|
if (editor->cursor.row >= editor->root->line_count) {
|
||||||
|
editor->cursor.row = editor->root->line_count - 1;
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
if (editor->folded[editor->cursor.row] != 0)
|
||||||
|
number++;
|
||||||
|
} while (--number > 0);
|
||||||
|
free(it);
|
||||||
|
if (line_content == nullptr)
|
||||||
|
return;
|
||||||
|
editor->cursor.col = get_bytes_from_visual_col(line_content, len, visual_col);
|
||||||
|
free(line_content);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cursor_up(Editor *editor, uint32_t number) {
|
||||||
|
if (!editor || !editor->root || number == 0)
|
||||||
|
return;
|
||||||
|
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||||
|
uint32_t len;
|
||||||
|
char *line_content = next_line(it, &len);
|
||||||
|
if (!line_content) {
|
||||||
|
free(it);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (editor->cursor_preffered == UINT32_MAX)
|
||||||
|
editor->cursor_preffered =
|
||||||
|
get_visual_col_from_bytes(line_content, len, editor->cursor.col);
|
||||||
|
uint32_t visual_col = editor->cursor_preffered;
|
||||||
|
free(line_content);
|
||||||
|
while (number > 0 && editor->cursor.row > 0) {
|
||||||
|
editor->cursor.row--;
|
||||||
|
if (editor->folded[editor->cursor.row] != 0)
|
||||||
|
continue;
|
||||||
|
number--;
|
||||||
|
}
|
||||||
|
free(it);
|
||||||
|
it = begin_l_iter(editor->root, editor->cursor.row);
|
||||||
|
line_content = next_line(it, &len);
|
||||||
|
if (!line_content) {
|
||||||
|
free(it);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
editor->cursor.col = get_bytes_from_visual_col(line_content, len, visual_col);
|
||||||
|
free(line_content);
|
||||||
|
free(it);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cursor_right(Editor *editor, uint32_t number) {
|
||||||
|
if (!editor || !editor->root || number == 0)
|
||||||
|
return;
|
||||||
|
editor->cursor = move_right(editor, editor->cursor, number);
|
||||||
|
editor->cursor_preffered = UINT32_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cursor_left(Editor *editor, uint32_t number) {
|
||||||
|
if (!editor || !editor->root || number == 0)
|
||||||
|
return;
|
||||||
|
editor->cursor = move_left(editor, editor->cursor, number);
|
||||||
|
editor->cursor_preffered = UINT32_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
||||||
if (len == 0)
|
if (len == 0)
|
||||||
return;
|
return;
|
||||||
@@ -308,33 +428,13 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
|||||||
uint32_t start = line_to_byte(editor->root, point.row, nullptr) + point.col;
|
uint32_t start = line_to_byte(editor->root, point.row, nullptr) + point.col;
|
||||||
if (cursor_original > start && cursor_original <= byte_pos) {
|
if (cursor_original > start && cursor_original <= byte_pos) {
|
||||||
editor->cursor = point;
|
editor->cursor = point;
|
||||||
LineIterator *it = begin_l_iter(editor->root, point.row);
|
editor->cursor_preffered = UINT32_MAX;
|
||||||
if (!it)
|
|
||||||
return;
|
|
||||||
uint32_t line_len;
|
|
||||||
char *line = next_line(it, &line_len);
|
|
||||||
free(it);
|
|
||||||
if (!line)
|
|
||||||
return;
|
|
||||||
editor->cursor_preffered =
|
|
||||||
get_visual_col_from_bytes(line, line_len, point.col);
|
|
||||||
free(line);
|
|
||||||
} else if (cursor_original > byte_pos) {
|
} else if (cursor_original > byte_pos) {
|
||||||
uint32_t cursor_new = cursor_original - (byte_pos - start);
|
uint32_t cursor_new = cursor_original - (byte_pos - start);
|
||||||
uint32_t new_col;
|
uint32_t new_col;
|
||||||
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
|
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
|
||||||
editor->cursor = {new_row, new_col};
|
editor->cursor = {new_row, new_col};
|
||||||
LineIterator *it = begin_l_iter(editor->root, new_row);
|
editor->cursor_preffered = UINT32_MAX;
|
||||||
if (!it)
|
|
||||||
return;
|
|
||||||
uint32_t line_len;
|
|
||||||
char *line = next_line(it, &line_len);
|
|
||||||
free(it);
|
|
||||||
if (!line)
|
|
||||||
return;
|
|
||||||
editor->cursor_preffered =
|
|
||||||
get_visual_col_from_bytes(line, line_len, new_col);
|
|
||||||
free(line);
|
|
||||||
}
|
}
|
||||||
lock_1.unlock();
|
lock_1.unlock();
|
||||||
std::unique_lock lock_2(editor->knot_mtx);
|
std::unique_lock lock_2(editor->knot_mtx);
|
||||||
@@ -366,33 +466,13 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
|||||||
uint32_t end = line_to_byte(editor->root, point.row, nullptr) + point.col;
|
uint32_t end = line_to_byte(editor->root, point.row, nullptr) + point.col;
|
||||||
if (cursor_original > byte_pos && cursor_original <= end) {
|
if (cursor_original > byte_pos && cursor_original <= end) {
|
||||||
editor->cursor = pos;
|
editor->cursor = pos;
|
||||||
LineIterator *it = begin_l_iter(editor->root, pos.row);
|
editor->cursor_preffered = UINT32_MAX;
|
||||||
if (!it)
|
|
||||||
return;
|
|
||||||
uint32_t line_len;
|
|
||||||
char *line = next_line(it, &line_len);
|
|
||||||
free(it);
|
|
||||||
if (!line)
|
|
||||||
return;
|
|
||||||
editor->cursor_preffered =
|
|
||||||
get_visual_col_from_bytes(line, line_len, pos.col);
|
|
||||||
free(line);
|
|
||||||
} else if (cursor_original > end) {
|
} else if (cursor_original > end) {
|
||||||
uint32_t cursor_new = cursor_original - (end - byte_pos);
|
uint32_t cursor_new = cursor_original - (end - byte_pos);
|
||||||
uint32_t new_col;
|
uint32_t new_col;
|
||||||
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
|
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
|
||||||
editor->cursor = {new_row, new_col};
|
editor->cursor = {new_row, new_col};
|
||||||
LineIterator *it = begin_l_iter(editor->root, new_row);
|
editor->cursor_preffered = UINT32_MAX;
|
||||||
if (!it)
|
|
||||||
return;
|
|
||||||
uint32_t line_len;
|
|
||||||
char *line = next_line(it, &line_len);
|
|
||||||
free(it);
|
|
||||||
if (!line)
|
|
||||||
return;
|
|
||||||
editor->cursor_preffered =
|
|
||||||
get_visual_col_from_bytes(line, line_len, new_col);
|
|
||||||
free(line);
|
|
||||||
}
|
}
|
||||||
lock_1.unlock();
|
lock_1.unlock();
|
||||||
std::unique_lock lock_2(editor->knot_mtx);
|
std::unique_lock lock_2(editor->knot_mtx);
|
||||||
|
|||||||
@@ -163,6 +163,14 @@ void scroll_down(Editor *editor, uint32_t number) {
|
|||||||
free(line);
|
free(line);
|
||||||
line_index++;
|
line_index++;
|
||||||
}
|
}
|
||||||
|
if (q_size > 0) {
|
||||||
|
uint32_t idx;
|
||||||
|
if (q_size < max_visual_lines)
|
||||||
|
idx = (q_head + q_size - 1) % max_visual_lines;
|
||||||
|
else
|
||||||
|
idx = q_head;
|
||||||
|
editor->scroll = scroll_queue[idx];
|
||||||
|
}
|
||||||
free(it);
|
free(it);
|
||||||
free(scroll_queue);
|
free(scroll_queue);
|
||||||
}
|
}
|
||||||
@@ -216,13 +224,6 @@ void ensure_cursor(Editor *editor) {
|
|||||||
Coord current = {line_index, offset};
|
Coord current = {line_index, offset};
|
||||||
last_visible = current;
|
last_visible = current;
|
||||||
visual_rows++;
|
visual_rows++;
|
||||||
if (line_index == editor->cursor.row) {
|
|
||||||
if (editor->cursor.col >= offset) {
|
|
||||||
free(line);
|
|
||||||
free(it);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (visual_rows >= editor->size.row)
|
if (visual_rows >= editor->size.row)
|
||||||
break;
|
break;
|
||||||
uint32_t col = 0;
|
uint32_t col = 0;
|
||||||
@@ -238,6 +239,14 @@ void ensure_cursor(Editor *editor) {
|
|||||||
left -= g;
|
left -= g;
|
||||||
col += w;
|
col += w;
|
||||||
}
|
}
|
||||||
|
if (line_index == editor->cursor.row) {
|
||||||
|
if (editor->cursor.col >= offset &&
|
||||||
|
editor->cursor.col < offset + advance) {
|
||||||
|
free(line);
|
||||||
|
free(it);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (advance == 0)
|
if (advance == 0)
|
||||||
break;
|
break;
|
||||||
offset += advance;
|
offset += advance;
|
||||||
|
|||||||
154
src/main.cc
154
src/main.cc
@@ -4,17 +4,20 @@
|
|||||||
#include "../include/ui.h"
|
#include "../include/ui.h"
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <cstdint>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
std::atomic<bool> running{true};
|
std::atomic<bool> running{true};
|
||||||
Queue<KeyEvent> event_queue;
|
Queue<KeyEvent> event_queue;
|
||||||
|
std::vector<Editor *> editors;
|
||||||
|
|
||||||
|
uint8_t current_editor = 0;
|
||||||
uint8_t mode = NORMAL;
|
uint8_t mode = NORMAL;
|
||||||
|
|
||||||
void background_worker(Editor *editor) {
|
void background_worker() {
|
||||||
while (running) {
|
while (running) {
|
||||||
ts_collect_spans(editor);
|
ts_collect_spans(editors[current_editor]);
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(16));
|
std::this_thread::sleep_for(std::chrono::milliseconds(16));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -34,138 +37,61 @@ void input_listener() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_editor_event(Editor *editor, KeyEvent event) {
|
Editor *editor_at(uint8_t x, uint8_t y) {
|
||||||
if (event.key_type == KEY_SPECIAL) {
|
for (Editor *ed : editors) {
|
||||||
switch (event.special_key) {
|
Coord pos = ed->position;
|
||||||
case KEY_DOWN:
|
Coord size = ed->size;
|
||||||
cursor_down(editor, 1);
|
if (x >= pos.col && x < pos.col + size.col && y >= pos.row &&
|
||||||
break;
|
y < pos.row + size.row)
|
||||||
case KEY_UP:
|
return ed;
|
||||||
cursor_up(editor, 1);
|
|
||||||
break;
|
|
||||||
case KEY_LEFT:
|
|
||||||
cursor_left(editor, 1);
|
|
||||||
break;
|
|
||||||
case KEY_RIGHT:
|
|
||||||
cursor_right(editor, 1);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
switch (mode) {
|
|
||||||
case NORMAL:
|
uint8_t index_of(Editor *ed) {
|
||||||
if (event.key_type == KEY_CHAR && event.len == 1) {
|
for (uint8_t i = 0; i < editors.size(); i++) {
|
||||||
switch (event.c[0]) {
|
if (editors[i] == ed)
|
||||||
case 'a':
|
return i;
|
||||||
case 'i':
|
|
||||||
mode = INSERT;
|
|
||||||
break;
|
|
||||||
case 's':
|
|
||||||
case 'v':
|
|
||||||
mode = SELECT;
|
|
||||||
editor->selection_active = true;
|
|
||||||
editor->selection = editor->cursor;
|
|
||||||
break;
|
|
||||||
case ';':
|
|
||||||
case ':':
|
|
||||||
mode = RUNNER;
|
|
||||||
break;
|
|
||||||
case 0x7F:
|
|
||||||
cursor_left(editor, 1);
|
|
||||||
break;
|
|
||||||
case ' ':
|
|
||||||
cursor_right(editor, 1);
|
|
||||||
break;
|
|
||||||
case '\r':
|
|
||||||
case '\n':
|
|
||||||
cursor_down(editor, 1);
|
|
||||||
break;
|
|
||||||
case '\\':
|
|
||||||
case '|':
|
|
||||||
cursor_up(editor, 1);
|
|
||||||
break;
|
|
||||||
case CTRL('d'):
|
|
||||||
scroll_down(editor, 1);
|
|
||||||
ensure_cursor(editor);
|
|
||||||
break;
|
|
||||||
case CTRL('u'):
|
|
||||||
scroll_up(editor, 1);
|
|
||||||
ensure_cursor(editor);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
return 0;
|
||||||
break;
|
|
||||||
case INSERT:
|
|
||||||
if (event.key_type == KEY_CHAR) {
|
|
||||||
if (event.len == 1) {
|
|
||||||
if (event.c[0] == '\t') {
|
|
||||||
edit_insert(editor, editor->cursor, (char *)" ", 1);
|
|
||||||
cursor_right(editor, 2);
|
|
||||||
} else if (event.c[0] == '\n' || event.c[0] == '\r') {
|
|
||||||
edit_insert(editor, editor->cursor, (char *)"\n", 1);
|
|
||||||
cursor_right(editor, 1);
|
|
||||||
} else if (event.c[0] == 0x7F) {
|
|
||||||
edit_erase(editor, editor->cursor, -1);
|
|
||||||
} else if (isprint((unsigned char)(event.c[0]))) {
|
|
||||||
edit_insert(editor, editor->cursor, event.c, 1);
|
|
||||||
cursor_right(editor, 1);
|
|
||||||
} else if (event.c[0] == 0x1B) {
|
|
||||||
mode = NORMAL;
|
|
||||||
}
|
|
||||||
} else if (event.len > 1) {
|
|
||||||
edit_insert(editor, editor->cursor, event.c, event.len);
|
|
||||||
cursor_right(editor, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (event.key_type == KEY_SPECIAL && event.special_key == KEY_DELETE)
|
|
||||||
edit_erase(editor, editor->cursor, 1);
|
|
||||||
break;
|
|
||||||
case SELECT:
|
|
||||||
if (event.key_type == KEY_CHAR && event.len == 1) {
|
|
||||||
switch (event.c[0]) {
|
|
||||||
case 0x1B:
|
|
||||||
case 's':
|
|
||||||
case 'v':
|
|
||||||
editor->selection_active = false;
|
|
||||||
mode = NORMAL;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case RUNNER:
|
|
||||||
if (event.key_type == KEY_CHAR && event.len == 1) {
|
|
||||||
switch (event.c[0]) {
|
|
||||||
case 0x1B:
|
|
||||||
mode = NORMAL;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ensure_scroll(editor);
|
|
||||||
if (event.key_type == KEY_CHAR && event.c)
|
|
||||||
free(event.c);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
Coord screen = start_screen();
|
Coord screen = start_screen();
|
||||||
const char *filename = (argc > 1) ? argv[1] : "";
|
const char *filename = (argc > 1) ? argv[1] : "";
|
||||||
|
|
||||||
Editor *editor = new_editor(filename, {0, 0}, screen);
|
Editor *editor =
|
||||||
|
new_editor(filename, {10, 10}, {screen.row - 20, screen.col - 20});
|
||||||
if (!editor) {
|
if (!editor) {
|
||||||
end_screen();
|
end_screen();
|
||||||
fprintf(stderr, "Failed to load editor\n");
|
fprintf(stderr, "Failed to load editor\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
editors.push_back(editor);
|
||||||
|
current_editor = editors.size() - 1;
|
||||||
|
|
||||||
std::thread input_thread(input_listener);
|
std::thread input_thread(input_listener);
|
||||||
std::thread work_thread(background_worker, editor);
|
std::thread work_thread(background_worker);
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
KeyEvent event;
|
KeyEvent event;
|
||||||
while (event_queue.pop(event))
|
while (event_queue.pop(event)) {
|
||||||
handle_editor_event(editor, event);
|
if (event.key_type == KEY_MOUSE) {
|
||||||
|
Editor *target = editor_at(event.mouse_x, event.mouse_y);
|
||||||
|
if (!target)
|
||||||
|
continue;
|
||||||
|
if (event.mouse_state == PRESS)
|
||||||
|
current_editor = index_of(target);
|
||||||
|
event.mouse_x -= target->position.col;
|
||||||
|
event.mouse_y -= target->position.row;
|
||||||
|
handle_editor_event(target, event);
|
||||||
|
} else {
|
||||||
|
handle_editor_event(editors[current_editor], event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
render_editor(editor);
|
render_editor(editors[current_editor]);
|
||||||
render();
|
render();
|
||||||
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(8));
|
std::this_thread::sleep_for(std::chrono::milliseconds(8));
|
||||||
|
|||||||
Reference in New Issue
Block a user