Compare commits
5 Commits
38839408b1
...
5153770ee9
| Author | SHA1 | Date | |
|---|---|---|---|
|
5153770ee9
|
|||
|
a59111162c
|
|||
|
d8c281d7d7
|
|||
|
d9ebf83588
|
|||
|
e83cc6cf02
|
10
README.md
10
README.md
@@ -6,18 +6,9 @@ A TUI IDE.
|
||||
|
||||
# TODO
|
||||
|
||||
- [ ] Fix problem highlighting selection when line is empty.
|
||||
- [ ] Add ui api for setting cursor types.
|
||||
- [ ] Use that api to set cursor for selection/insertion/normal etc properly.
|
||||
- Sorta unrelated but check kutuwm + kitty problem not keeping cursor mode properly.
|
||||
- [ ] Add line numbers.
|
||||
- [ ] Add bg highlight for current line.
|
||||
- [ ] Make function to get selected text. (selection itself is done)
|
||||
- [ ] Add support for copy/cut/paste.
|
||||
- [ ] Add support for ctrl + arrow key for moving words.
|
||||
- [ ] Add support for ctrl + backspace / delete for deleting words.
|
||||
- [ ] Add underline highlight for current word and all occurences.
|
||||
- [ ] Add mouse support.
|
||||
- [ ] Add `hooks` in files that can be set/unset/jumped to.
|
||||
- [ ] Add folding support at tree-sitter level (basic folding is done).
|
||||
- [ ] Add feature where doing enter uses tree-sitter to add newline with indentation.
|
||||
@@ -30,3 +21,4 @@ A TUI IDE.
|
||||
- [ ] Add support for LSP & autocomplete / snippets.
|
||||
- [ ] Add codeium/copilot support.
|
||||
- [ ] Add git stuff.
|
||||
- [ ] Add splash screen / minigame jumping.
|
||||
|
||||
@@ -109,11 +109,19 @@ void render_editor(Editor *editor);
|
||||
void fold(Editor *editor, uint32_t start_line, uint32_t end_line);
|
||||
void cursor_up(Editor *editor, uint32_t number);
|
||||
void cursor_down(Editor *editor, uint32_t number);
|
||||
Coord move_left(Editor *editor, Coord cursor, uint32_t number);
|
||||
Coord move_right(Editor *editor, Coord cursor, uint32_t number);
|
||||
void cursor_left(Editor *editor, uint32_t number);
|
||||
void cursor_right(Editor *editor, uint32_t number);
|
||||
void scroll_up(Editor *editor, uint32_t number);
|
||||
void scroll_down(Editor *editor, uint32_t number);
|
||||
void ensure_cursor(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 edit_erase(Editor *editor, Coord pos, int64_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);
|
||||
char *get_selection(Editor *editor, uint32_t *out_len);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -10,5 +10,6 @@
|
||||
#define RUNNER 3
|
||||
|
||||
extern std::atomic<bool> running;
|
||||
extern uint8_t mode;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -46,6 +46,11 @@
|
||||
#define CNTRL_ALT 3
|
||||
#define SHIFT 4
|
||||
|
||||
#define DEFAULT 0
|
||||
#define BLOCK 2
|
||||
#define UNDERLINE 4
|
||||
#define CURSOR 6
|
||||
|
||||
enum CellFlags : uint8_t {
|
||||
CF_NONE = 0,
|
||||
CF_ITALIC = 1 << 0,
|
||||
@@ -86,7 +91,7 @@ Coord start_screen();
|
||||
void end_screen();
|
||||
void update(uint32_t row, uint32_t col, const char *utf8, uint32_t fg,
|
||||
uint32_t bg, uint8_t flags);
|
||||
void set_cursor(int row, int col, int show_cursor_param);
|
||||
void set_cursor(int row, int col, int type, bool show_cursor_param);
|
||||
void render();
|
||||
Coord get_size();
|
||||
|
||||
|
||||
@@ -34,9 +34,21 @@ template <typename T> struct Queue {
|
||||
struct Coord {
|
||||
uint32_t row;
|
||||
uint32_t col;
|
||||
|
||||
bool operator<(const Coord &other) const {
|
||||
return row < other.row || (row == other.row && col < other.col);
|
||||
}
|
||||
bool operator<=(const Coord &other) const {
|
||||
return *this < other || *this == other;
|
||||
}
|
||||
bool operator==(const Coord &other) const {
|
||||
return row == other.row && col == other.col;
|
||||
}
|
||||
bool operator!=(const Coord &other) const { return !(*this == other); }
|
||||
bool operator>(const Coord &other) const { return other < *this; }
|
||||
bool operator>=(const Coord &other) const { return !(*this < other); }
|
||||
};
|
||||
|
||||
// std::vector<uint32_t> visual_width(const char *s, uint32_t max_width);
|
||||
int display_width(const char *str, size_t len);
|
||||
uint32_t get_visual_col_from_bytes(const char *line, uint32_t len,
|
||||
uint32_t byte_limit);
|
||||
@@ -47,5 +59,7 @@ std::string get_exe_dir();
|
||||
char *load_file(const char *path, uint32_t *out_len);
|
||||
char *detect_file_type(const char *filename);
|
||||
Language language_for_file(const char *filename);
|
||||
void copy_to_clipboard(const char *text, size_t len);
|
||||
char *get_from_clipboard(uint32_t *out_len);
|
||||
|
||||
#endif
|
||||
|
||||
278
src/editor.cc
278
src/editor.cc
@@ -2,8 +2,10 @@ extern "C" {
|
||||
#include "../libs/libgrapheme/grapheme.h"
|
||||
}
|
||||
#include "../include/editor.h"
|
||||
#include "../include/main.h"
|
||||
#include "../include/ts.h"
|
||||
#include "../include/utils.h"
|
||||
#include <cmath>
|
||||
|
||||
Editor *new_editor(const char *filename, Coord position, Coord size) {
|
||||
Editor *editor = new Editor();
|
||||
@@ -48,148 +50,6 @@ void free_editor(Editor *editor) {
|
||||
delete editor;
|
||||
}
|
||||
|
||||
void ensure_scroll(Editor *editor) {
|
||||
std::shared_lock knot_lock(editor->knot_mtx);
|
||||
if (editor->cursor.row < editor->scroll.row ||
|
||||
(editor->cursor.row == editor->scroll.row &&
|
||||
editor->cursor.col <= editor->scroll.col)) {
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t len;
|
||||
char *line = next_line(it, &len);
|
||||
if (!line) {
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
--len;
|
||||
uint32_t rows = 1;
|
||||
uint32_t cols = 0;
|
||||
uint32_t offset = 0;
|
||||
uint32_t old_offset = 0;
|
||||
while (offset < len) {
|
||||
uint32_t inc =
|
||||
grapheme_next_character_break_utf8(line + offset, len - offset);
|
||||
int width = display_width(line + offset, inc);
|
||||
if (cols + width > editor->size.col) {
|
||||
rows++;
|
||||
cols = 0;
|
||||
if (editor->cursor.col > old_offset && editor->cursor.col <= offset) {
|
||||
editor->scroll.row = editor->cursor.row;
|
||||
editor->scroll.col = old_offset;
|
||||
free(line);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
old_offset = offset;
|
||||
}
|
||||
cols += width;
|
||||
offset += inc;
|
||||
}
|
||||
free(line);
|
||||
free(it);
|
||||
editor->scroll.row = editor->cursor.row;
|
||||
editor->scroll.col = (editor->cursor.col == 0) ? 0 : old_offset;
|
||||
} else {
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
LineIterator *it = begin_l_iter(editor->root, line_index);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t max_visual_lines = editor->size.row;
|
||||
Coord *scroll_queue = (Coord *)malloc(sizeof(Coord) * max_visual_lines);
|
||||
uint32_t q_head = 0;
|
||||
uint32_t q_size = 0;
|
||||
bool first_visual_line = true;
|
||||
while (true) {
|
||||
if (editor->folded[line_index]) {
|
||||
if (editor->folded[line_index] == 2) {
|
||||
Coord fold_coord = {line_index, 0};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = fold_coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = fold_coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
if (line_index == editor->cursor.row) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
break;
|
||||
}
|
||||
}
|
||||
do {
|
||||
char *line = next_line(it, nullptr);
|
||||
if (!line)
|
||||
break;
|
||||
free(line);
|
||||
line_index++;
|
||||
} while (line_index < editor->size.row &&
|
||||
editor->folded[line_index] == 1);
|
||||
continue;
|
||||
}
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
break;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
uint32_t current_byte_offset = 0;
|
||||
if (first_visual_line) {
|
||||
current_byte_offset += editor->scroll.col;
|
||||
first_visual_line = false;
|
||||
}
|
||||
while (current_byte_offset < line_len ||
|
||||
(line_len == 0 && current_byte_offset == 0)) {
|
||||
Coord current_coord = {line_index, current_byte_offset};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = current_coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = current_coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
uint32_t col = 0;
|
||||
uint32_t local_render_offset = 0;
|
||||
uint32_t line_left = line_len - current_byte_offset;
|
||||
while (line_left > 0 && col < editor->size.col) {
|
||||
uint32_t cluster_len = grapheme_next_character_break_utf8(
|
||||
line + current_byte_offset + local_render_offset, line_left);
|
||||
int width = display_width(
|
||||
line + current_byte_offset + local_render_offset, cluster_len);
|
||||
if (col + width > editor->size.col)
|
||||
break;
|
||||
local_render_offset += cluster_len;
|
||||
line_left -= cluster_len;
|
||||
col += width;
|
||||
}
|
||||
if (line_index == editor->cursor.row) {
|
||||
bool cursor_found = false;
|
||||
if (editor->cursor.col >= current_byte_offset &&
|
||||
editor->cursor.col < current_byte_offset + local_render_offset)
|
||||
cursor_found = true;
|
||||
else if (editor->cursor.col == line_len &&
|
||||
current_byte_offset + local_render_offset == line_len)
|
||||
cursor_found = true;
|
||||
if (cursor_found) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
free(line);
|
||||
free(scroll_queue);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
current_byte_offset += local_render_offset;
|
||||
if (line_len == 0)
|
||||
break;
|
||||
}
|
||||
line_index++;
|
||||
free(line);
|
||||
}
|
||||
free(scroll_queue);
|
||||
free(it);
|
||||
}
|
||||
}
|
||||
|
||||
void fold(Editor *editor, uint32_t start_line, uint32_t end_line) {
|
||||
if (!editor)
|
||||
return;
|
||||
@@ -208,43 +68,23 @@ void update_render_fold_marker(uint32_t row, uint32_t cols) {
|
||||
update(row, i, " ", 0xc6c6c6, 0, 0);
|
||||
}
|
||||
|
||||
void apply_edit(std::vector<Span> &spans, uint32_t x, int64_t y) {
|
||||
Span key{.start = x, .end = 0, .hl = nullptr};
|
||||
auto it = std::lower_bound(
|
||||
spans.begin(), spans.end(), key,
|
||||
[](const Span &a, const Span &b) { return a.start < b.start; });
|
||||
size_t idx = std::distance(spans.begin(), it);
|
||||
while (idx > 0 && spans.at(idx - 1).end > x)
|
||||
--idx;
|
||||
for (size_t i = idx; i < spans.size();) {
|
||||
Span &s = spans.at(i);
|
||||
if (s.start < x && s.end > x) {
|
||||
s.end += y;
|
||||
} else if (s.start > x) {
|
||||
s.start += y;
|
||||
s.end += y;
|
||||
}
|
||||
if (s.end <= s.start)
|
||||
spans.erase(spans.begin() + i);
|
||||
else
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
void render_editor(Editor *editor) {
|
||||
uint32_t sel_start = 0, sel_end = 0;
|
||||
uint32_t numlen =
|
||||
2 + static_cast<int>(std::log10(editor->root->line_count + 1));
|
||||
uint32_t render_width = editor->size.col - numlen;
|
||||
uint32_t render_x = editor->position.col + numlen;
|
||||
if (editor->selection_active) {
|
||||
uint32_t sel1 = line_to_byte(editor->root, editor->cursor.row, nullptr) +
|
||||
editor->cursor.col;
|
||||
uint32_t sel2 = line_to_byte(editor->root, editor->selection.row, nullptr) +
|
||||
editor->selection.col;
|
||||
if (sel1 <= sel2) {
|
||||
sel_start = sel1;
|
||||
sel_end = sel2;
|
||||
Coord start, end;
|
||||
if (editor->cursor >= editor->selection) {
|
||||
start = editor->selection;
|
||||
end = move_right(editor, editor->cursor, 1);
|
||||
} else {
|
||||
sel_start = sel2;
|
||||
sel_end = sel1;
|
||||
start = editor->cursor;
|
||||
end = move_right(editor, editor->selection, 1);
|
||||
}
|
||||
sel_start = line_to_byte(editor->root, start.row, nullptr) + start.col;
|
||||
sel_end = line_to_byte(editor->root, end.row, nullptr) + end.col;
|
||||
}
|
||||
Coord cursor = {UINT32_MAX, UINT32_MAX};
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
@@ -259,7 +99,7 @@ void render_editor(Editor *editor) {
|
||||
while (rendered_rows < editor->size.row) {
|
||||
if (editor->folded[line_index]) {
|
||||
if (editor->folded[line_index] == 2) {
|
||||
update_render_fold_marker(rendered_rows, editor->size.col);
|
||||
update_render_fold_marker(rendered_rows, render_width);
|
||||
rendered_rows++;
|
||||
}
|
||||
do {
|
||||
@@ -287,14 +127,28 @@ void render_editor(Editor *editor) {
|
||||
if (rendered_rows == 0)
|
||||
current_byte_offset += editor->scroll.col;
|
||||
while (current_byte_offset < line_len && rendered_rows < editor->size.row) {
|
||||
uint32_t color = editor->cursor.row == line_index ? 0x222222 : 0;
|
||||
if (current_byte_offset == 0 || rendered_rows == 0) {
|
||||
char buf[16];
|
||||
int len = snprintf(buf, sizeof(buf), "%*u", numlen - 1, line_index + 1);
|
||||
uint32_t num_color =
|
||||
editor->cursor.row == line_index ? 0xFFFFFF : 0x555555;
|
||||
for (int i = 0; i < len; i++)
|
||||
update(editor->position.row + rendered_rows, editor->position.col + i,
|
||||
std::string(1, buf[i]).c_str(), num_color, 0, 0);
|
||||
} else {
|
||||
for (uint32_t i = 0; i < numlen; i++)
|
||||
update(editor->position.row + rendered_rows, editor->position.col + i,
|
||||
" ", 0, 0, 0);
|
||||
}
|
||||
uint32_t col = 0;
|
||||
uint32_t local_render_offset = 0;
|
||||
uint32_t line_left = line_len - current_byte_offset;
|
||||
while (line_left > 0 && col < editor->size.col) {
|
||||
while (line_left > 0 && col < render_width) {
|
||||
if (line_index == editor->cursor.row &&
|
||||
editor->cursor.col == (current_byte_offset + local_render_offset)) {
|
||||
cursor.row = editor->position.row + rendered_rows;
|
||||
cursor.col = editor->position.col + col;
|
||||
cursor.col = render_x + col;
|
||||
}
|
||||
uint32_t absolute_byte_pos =
|
||||
global_byte_offset + current_byte_offset + local_render_offset;
|
||||
@@ -310,33 +164,32 @@ void render_editor(Editor *editor) {
|
||||
std::string cluster(line + current_byte_offset + local_render_offset,
|
||||
cluster_len);
|
||||
int width = display_width(cluster.c_str(), cluster_len);
|
||||
if (col + width > editor->size.col)
|
||||
if (col + width > render_width)
|
||||
break;
|
||||
update(editor->position.row + rendered_rows, editor->position.col + col,
|
||||
cluster.c_str(), fg, bg, fl);
|
||||
update(editor->position.row + rendered_rows, render_x + col,
|
||||
cluster.c_str(), fg, bg | color, fl);
|
||||
local_render_offset += cluster_len;
|
||||
line_left -= cluster_len;
|
||||
col += width;
|
||||
while (width-- > 1)
|
||||
update(editor->position.row + rendered_rows,
|
||||
editor->position.col + col - width, "\x1b", fg, bg, fl);
|
||||
update(editor->position.row + rendered_rows, render_x + col - width,
|
||||
"\x1b", fg, bg | color, fl);
|
||||
}
|
||||
if (line_index == editor->cursor.row &&
|
||||
editor->cursor.col == (current_byte_offset + local_render_offset)) {
|
||||
cursor.row = editor->position.row + rendered_rows;
|
||||
cursor.col = editor->position.col + col;
|
||||
cursor.col = render_x + col;
|
||||
}
|
||||
if (editor->selection_active &&
|
||||
global_byte_offset + line_len + 1 > sel_start &&
|
||||
global_byte_offset + line_len + 1 <= sel_end &&
|
||||
col < editor->size.col) {
|
||||
update(editor->position.row + rendered_rows, editor->position.col + col,
|
||||
" ", 0, 0x555555, 0);
|
||||
global_byte_offset + line_len + 1 <= sel_end && col < render_width) {
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
|
||||
0x555555 | color, 0);
|
||||
col++;
|
||||
}
|
||||
while (col < editor->size.col) {
|
||||
update(editor->position.row + rendered_rows, editor->position.col + col,
|
||||
" ", 0xFFFFFF, 0, 0);
|
||||
while (col < render_width) {
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
|
||||
0 | color, 0);
|
||||
col++;
|
||||
}
|
||||
rendered_rows++;
|
||||
@@ -344,21 +197,29 @@ void render_editor(Editor *editor) {
|
||||
}
|
||||
if (line_len == 0 ||
|
||||
(current_byte_offset >= line_len && rendered_rows == 0)) {
|
||||
uint32_t color = editor->cursor.row == line_index ? 0x222222 : 0;
|
||||
char buf[16];
|
||||
int len = snprintf(buf, sizeof(buf), "%*u", numlen - 1, line_index + 1);
|
||||
uint32_t num_color =
|
||||
editor->cursor.row == line_index ? 0xFFFFFF : 0x555555;
|
||||
for (int i = 0; i < len; i++)
|
||||
update(editor->position.row + rendered_rows, editor->position.col + i,
|
||||
std::string(1, buf[i]).c_str(), num_color, 0, 0);
|
||||
if (editor->cursor.row == line_index) {
|
||||
cursor.row = editor->position.row + rendered_rows;
|
||||
cursor.col = editor->position.col;
|
||||
cursor.col = render_x;
|
||||
}
|
||||
uint32_t col = 0;
|
||||
if (editor->selection_active &&
|
||||
global_byte_offset + line_len + 1 > sel_start &&
|
||||
global_byte_offset + line_len + 1 <= sel_end) {
|
||||
update(editor->position.row + rendered_rows, editor->position.col + col,
|
||||
" ", 0, 0x555555, 0);
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
|
||||
0x555555 | color, 0);
|
||||
col++;
|
||||
}
|
||||
while (col < editor->size.col) {
|
||||
update(editor->position.row + rendered_rows, editor->position.col + col,
|
||||
" ", 0xFFFFFF, 0, 0);
|
||||
while (col < render_width) {
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ", 0,
|
||||
0 | color, 0);
|
||||
col++;
|
||||
}
|
||||
rendered_rows++;
|
||||
@@ -367,12 +228,25 @@ void render_editor(Editor *editor) {
|
||||
line_index++;
|
||||
free(line);
|
||||
}
|
||||
if (cursor.row != UINT32_MAX && cursor.col != UINT32_MAX)
|
||||
set_cursor(cursor.row, cursor.col, 1);
|
||||
while (rendered_rows < editor->size.row) {
|
||||
for (uint32_t col = 0; col < editor->size.col; col++)
|
||||
update(editor->position.row + rendered_rows, editor->position.col + col,
|
||||
" ", 0xFFFFFF, 0, 0);
|
||||
if (cursor.row != UINT32_MAX && cursor.col != UINT32_MAX) {
|
||||
int type = 0;
|
||||
switch (mode) {
|
||||
case NORMAL:
|
||||
type = BLOCK;
|
||||
break;
|
||||
case INSERT:
|
||||
type = CURSOR;
|
||||
break;
|
||||
case SELECT:
|
||||
type = UNDERLINE;
|
||||
break;
|
||||
}
|
||||
set_cursor(cursor.row, cursor.col, type, true);
|
||||
}
|
||||
while (rendered_rows < render_width) {
|
||||
for (uint32_t col = 0; col < render_width; col++)
|
||||
update(editor->position.row + rendered_rows, render_x + col, " ",
|
||||
0xFFFFFF, 0, 0);
|
||||
rendered_rows++;
|
||||
}
|
||||
free(it);
|
||||
|
||||
@@ -1,192 +1,315 @@
|
||||
#include <cstdint>
|
||||
extern "C" {
|
||||
#include "../libs/libgrapheme/grapheme.h"
|
||||
}
|
||||
#include "../include/editor.h"
|
||||
#include "../include/main.h"
|
||||
#include "../include/utils.h"
|
||||
#include <cmath>
|
||||
|
||||
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;
|
||||
void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
static std::chrono::steady_clock::time_point last_click_time =
|
||||
std::chrono::steady_clock::now();
|
||||
static Coord last_click_pos = {UINT32_MAX, UINT32_MAX};
|
||||
if (event.key_type == KEY_SPECIAL) {
|
||||
switch (event.special_key) {
|
||||
case KEY_DOWN:
|
||||
cursor_down(editor, 1);
|
||||
break;
|
||||
case KEY_UP:
|
||||
cursor_up(editor, 1);
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
cursor_left(editor, 1);
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
cursor_right(editor, 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);
|
||||
}
|
||||
if (event.key_type == KEY_MOUSE) {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
now - last_click_time)
|
||||
.count();
|
||||
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;
|
||||
case SCROLL_LEFT:
|
||||
cursor_left(editor, 10);
|
||||
break;
|
||||
case SCROLL_RIGHT:
|
||||
cursor_right(editor, 10);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case PRESS:
|
||||
if (event.mouse_button == LEFT_BTN) {
|
||||
if (duration < 250 &&
|
||||
last_click_pos == (Coord){event.mouse_x, event.mouse_y}) {
|
||||
mode = SELECT;
|
||||
editor->selection_active = true;
|
||||
} else {
|
||||
Coord p = editor_hit_test(editor, event.mouse_x, event.mouse_y);
|
||||
editor->cursor = p;
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
editor->selection = p;
|
||||
if (mode == SELECT) {
|
||||
mode = NORMAL;
|
||||
editor->selection_active = false;
|
||||
}
|
||||
last_click_pos = (Coord){event.mouse_x, event.mouse_y};
|
||||
last_click_time = now;
|
||||
}
|
||||
}
|
||||
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;
|
||||
mode = SELECT;
|
||||
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) {
|
||||
mode = NORMAL;
|
||||
editor->selection_active = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
switch (mode) {
|
||||
case NORMAL:
|
||||
if (event.key_type == KEY_CHAR && event.len == 1) {
|
||||
switch (event.c[0]) {
|
||||
case 'a':
|
||||
mode = INSERT;
|
||||
cursor_right(editor, 1);
|
||||
break;
|
||||
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;
|
||||
cursor_left(editor, 1);
|
||||
}
|
||||
} else if (event.len > 1) {
|
||||
edit_insert(editor, editor->cursor, event.c, event.len);
|
||||
cursor_right(editor, 1);
|
||||
}
|
||||
} else 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) {
|
||||
uint32_t len;
|
||||
char *line_content = next_line(it, &len);
|
||||
if (!line_content) {
|
||||
free(it);
|
||||
return;
|
||||
char *text;
|
||||
switch (event.c[0]) {
|
||||
case 0x1B:
|
||||
case 's':
|
||||
case 'v':
|
||||
editor->selection_active = false;
|
||||
mode = NORMAL;
|
||||
break;
|
||||
case 'y':
|
||||
text = get_selection(editor, &len);
|
||||
copy_to_clipboard(text, len);
|
||||
free(text);
|
||||
editor->selection_active = false;
|
||||
mode = NORMAL;
|
||||
break;
|
||||
case 'x':
|
||||
text = get_selection(editor, &len);
|
||||
copy_to_clipboard(text, len);
|
||||
edit_erase(editor, MIN(editor->cursor, editor->selection), len);
|
||||
free(text);
|
||||
editor->selection_active = false;
|
||||
mode = NORMAL;
|
||||
break;
|
||||
case 'p':
|
||||
text = get_from_clipboard(&len);
|
||||
if (text) {
|
||||
Coord start, end;
|
||||
if (editor->cursor >= editor->selection) {
|
||||
start = editor->selection;
|
||||
end = move_right(editor, editor->cursor, 1);
|
||||
} else {
|
||||
start = editor->cursor;
|
||||
end = move_right(editor, editor->selection, 1);
|
||||
}
|
||||
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--;
|
||||
uint32_t start_byte =
|
||||
line_to_byte(editor->root, start.row, nullptr) + start.col;
|
||||
uint32_t end_byte =
|
||||
line_to_byte(editor->root, end.row, nullptr) + end.col;
|
||||
edit_erase(editor, start, end_byte - start_byte);
|
||||
edit_insert(editor, editor->cursor, text, len);
|
||||
free(text);
|
||||
}
|
||||
free(it);
|
||||
it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
line_content = next_line(it, &len);
|
||||
if (!line_content) {
|
||||
free(it);
|
||||
return;
|
||||
editor->selection_active = false;
|
||||
mode = NORMAL;
|
||||
break;
|
||||
}
|
||||
editor->cursor.col = get_bytes_from_visual_col(line_content, len, visual_col);
|
||||
free(line_content);
|
||||
free(it);
|
||||
}
|
||||
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_right(Editor *editor, uint32_t number) {
|
||||
if (!editor || !editor->root || number == 0)
|
||||
return;
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) {
|
||||
if (mode == INSERT)
|
||||
x++;
|
||||
uint32_t numlen =
|
||||
2 + static_cast<int>(std::log10(editor->root->line_count + 1));
|
||||
uint32_t render_width = editor->size.col - numlen;
|
||||
x = MAX(x, numlen) - numlen;
|
||||
uint32_t target_visual_row = y;
|
||||
uint32_t visual_row = 0;
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
bool first_visual_line = true;
|
||||
std::shared_lock knot_lock(editor->knot_mtx);
|
||||
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);
|
||||
return {line_index, 0};
|
||||
}
|
||||
visual_row++;
|
||||
}
|
||||
do {
|
||||
char *l = next_line(it, nullptr);
|
||||
if (!l)
|
||||
break;
|
||||
free(l);
|
||||
line_index++;
|
||||
} while (editor->folded[line_index] == 1);
|
||||
continue;
|
||||
}
|
||||
uint32_t 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)
|
||||
break;
|
||||
if (line[line_len - 1] == '\n')
|
||||
--line_len;
|
||||
} else {
|
||||
uint32_t inc = grapheme_next_character_break_utf8(
|
||||
line + editor->cursor.col, line_len - editor->cursor.col);
|
||||
if (inc == 0)
|
||||
if (line_len && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
uint32_t offset = first_visual_line ? editor->scroll.col : 0;
|
||||
first_visual_line = false;
|
||||
while (offset < line_len || (line_len == 0 && offset == 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 < render_width) {
|
||||
uint32_t g =
|
||||
grapheme_next_character_break_utf8(line + offset + advance, left);
|
||||
int w = display_width(line + offset + advance, g);
|
||||
if (col + w > render_width)
|
||||
break;
|
||||
editor->cursor.col += inc;
|
||||
}
|
||||
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)
|
||||
if (visual_row == target_visual_row && x < col + w) {
|
||||
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);
|
||||
if (!line)
|
||||
return;
|
||||
if (line[len - 1] == '\n')
|
||||
line[--len] = '\0';
|
||||
while (number > 0) {
|
||||
if (editor->cursor.col == 0) {
|
||||
return {line_index, offset + advance};
|
||||
}
|
||||
advance += g;
|
||||
last_good_offset = offset + advance;
|
||||
left -= g;
|
||||
col += w;
|
||||
}
|
||||
if (visual_row == target_visual_row) {
|
||||
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);
|
||||
if (!line)
|
||||
return {line_index, last_good_offset};
|
||||
}
|
||||
visual_row++;
|
||||
if (visual_row > target_visual_row)
|
||||
break;
|
||||
if (line[len - 1] == '\n')
|
||||
--len;
|
||||
editor->cursor.col = len;
|
||||
} else {
|
||||
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)
|
||||
if (advance == 0)
|
||||
break;
|
||||
offset += advance;
|
||||
if (line_len == 0)
|
||||
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);
|
||||
line_index++;
|
||||
}
|
||||
free(it);
|
||||
return editor->scroll;
|
||||
}
|
||||
|
||||
Coord move_right(Editor *editor, Coord cursor, uint32_t number) {
|
||||
@@ -294,6 +417,83 @@ Coord move_left(Editor *editor, Coord cursor, uint32_t number) {
|
||||
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) {
|
||||
if (len == 0)
|
||||
return;
|
||||
@@ -308,33 +508,13 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
||||
uint32_t start = line_to_byte(editor->root, point.row, nullptr) + point.col;
|
||||
if (cursor_original > start && cursor_original <= byte_pos) {
|
||||
editor->cursor = point;
|
||||
LineIterator *it = begin_l_iter(editor->root, point.row);
|
||||
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);
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
} else if (cursor_original > byte_pos) {
|
||||
uint32_t cursor_new = cursor_original - (byte_pos - start);
|
||||
uint32_t new_col;
|
||||
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
|
||||
editor->cursor = {new_row, new_col};
|
||||
LineIterator *it = begin_l_iter(editor->root, new_row);
|
||||
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);
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
}
|
||||
lock_1.unlock();
|
||||
std::unique_lock lock_2(editor->knot_mtx);
|
||||
@@ -366,33 +546,13 @@ void edit_erase(Editor *editor, Coord pos, int64_t len) {
|
||||
uint32_t end = line_to_byte(editor->root, point.row, nullptr) + point.col;
|
||||
if (cursor_original > byte_pos && cursor_original <= end) {
|
||||
editor->cursor = pos;
|
||||
LineIterator *it = begin_l_iter(editor->root, pos.row);
|
||||
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);
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
} else if (cursor_original > end) {
|
||||
uint32_t cursor_new = cursor_original - (end - byte_pos);
|
||||
uint32_t new_col;
|
||||
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
|
||||
editor->cursor = {new_row, new_col};
|
||||
LineIterator *it = begin_l_iter(editor->root, new_row);
|
||||
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);
|
||||
editor->cursor_preffered = UINT32_MAX;
|
||||
}
|
||||
lock_1.unlock();
|
||||
std::unique_lock lock_2(editor->knot_mtx);
|
||||
@@ -462,3 +622,45 @@ void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
|
||||
if (editor->spans.mid_parse)
|
||||
editor->spans.edits.push({byte_pos, len});
|
||||
}
|
||||
|
||||
char *get_selection(Editor *editor, uint32_t *out_len) {
|
||||
std::shared_lock lock(editor->knot_mtx);
|
||||
Coord start, end;
|
||||
if (editor->cursor >= editor->selection) {
|
||||
start = editor->selection;
|
||||
end = move_right(editor, editor->cursor, 1);
|
||||
} else {
|
||||
start = editor->cursor;
|
||||
end = move_right(editor, editor->selection, 1);
|
||||
}
|
||||
uint32_t start_byte =
|
||||
line_to_byte(editor->root, start.row, nullptr) + start.col;
|
||||
uint32_t end_byte = line_to_byte(editor->root, end.row, nullptr) + end.col;
|
||||
char *text = read(editor->root, start_byte, end_byte - start_byte);
|
||||
if (out_len)
|
||||
*out_len = end_byte - start_byte;
|
||||
return text;
|
||||
}
|
||||
|
||||
void apply_edit(std::vector<Span> &spans, uint32_t x, int64_t y) {
|
||||
Span key{.start = x, .end = 0, .hl = nullptr};
|
||||
auto it = std::lower_bound(
|
||||
spans.begin(), spans.end(), key,
|
||||
[](const Span &a, const Span &b) { return a.start < b.start; });
|
||||
size_t idx = std::distance(spans.begin(), it);
|
||||
while (idx > 0 && spans.at(idx - 1).end > x)
|
||||
--idx;
|
||||
for (size_t i = idx; i < spans.size();) {
|
||||
Span &s = spans.at(i);
|
||||
if (s.start < x && s.end > x) {
|
||||
s.end += y;
|
||||
} else if (s.start > x) {
|
||||
s.start += y;
|
||||
s.end += y;
|
||||
}
|
||||
if (s.end <= s.start)
|
||||
spans.erase(spans.begin() + i);
|
||||
else
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
412
src/editor_scroll.cc
Normal file
412
src/editor_scroll.cc
Normal file
@@ -0,0 +1,412 @@
|
||||
#include <cstdint>
|
||||
extern "C" {
|
||||
#include "../libs/libgrapheme/grapheme.h"
|
||||
}
|
||||
#include "../include/editor.h"
|
||||
#include "../include/utils.h"
|
||||
#include <cmath>
|
||||
|
||||
void scroll_up(Editor *editor, uint32_t number) {
|
||||
number++;
|
||||
uint32_t numlen =
|
||||
2 + static_cast<int>(std::log10(editor->root->line_count + 1));
|
||||
uint32_t render_width = editor->size.col - numlen;
|
||||
Coord *scroll_queue = (Coord *)malloc(sizeof(Coord) * number);
|
||||
uint32_t q_head = 0;
|
||||
uint32_t q_size = 0;
|
||||
int line_index = editor->scroll.row - number;
|
||||
line_index = line_index < 0 ? 0 : line_index;
|
||||
LineIterator *it = begin_l_iter(editor->root, line_index);
|
||||
if (!it) {
|
||||
free(scroll_queue);
|
||||
return;
|
||||
}
|
||||
for (uint32_t i = line_index; i <= editor->scroll.row; i++) {
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
break;
|
||||
if (line[line_len - 1] == '\n')
|
||||
--line_len;
|
||||
uint32_t offset = 0;
|
||||
uint32_t col = 0;
|
||||
if (q_size < number) {
|
||||
scroll_queue[(q_head + q_size) % number] = {i, 0};
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = {i, 0};
|
||||
q_head = (q_head + 1) % number;
|
||||
}
|
||||
if (i == editor->scroll.row && 0 == editor->scroll.col) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
free(line);
|
||||
free(it);
|
||||
free(scroll_queue);
|
||||
return;
|
||||
}
|
||||
while (offset < line_len) {
|
||||
uint32_t inc =
|
||||
grapheme_next_character_break_utf8(line + offset, line_len - offset);
|
||||
int width = display_width(line + offset, inc);
|
||||
if (col + width > render_width) {
|
||||
if (q_size < number) {
|
||||
scroll_queue[(q_head + q_size) % number] = {i, offset};
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = {i, offset};
|
||||
q_head = (q_head + 1) % number;
|
||||
}
|
||||
if (i == editor->scroll.row && offset >= editor->scroll.col) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
free(line);
|
||||
free(it);
|
||||
free(scroll_queue);
|
||||
return;
|
||||
}
|
||||
col = 0;
|
||||
}
|
||||
col += width;
|
||||
offset += inc;
|
||||
}
|
||||
free(line);
|
||||
}
|
||||
editor->scroll = {0, 0};
|
||||
free(scroll_queue);
|
||||
free(it);
|
||||
}
|
||||
|
||||
void scroll_down(Editor *editor, uint32_t number) {
|
||||
if (!editor || number == 0)
|
||||
return;
|
||||
uint32_t numlen =
|
||||
2 + static_cast<int>(std::log10(editor->root->line_count + 1));
|
||||
uint32_t render_width = editor->size.col - numlen;
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
LineIterator *it = begin_l_iter(editor->root, line_index);
|
||||
if (!it)
|
||||
return;
|
||||
const uint32_t max_visual_lines = editor->size.row;
|
||||
Coord *scroll_queue = (Coord *)malloc(sizeof(Coord) * max_visual_lines);
|
||||
uint32_t q_head = 0;
|
||||
uint32_t q_size = 0;
|
||||
uint32_t visual_seen = 0;
|
||||
bool first_visual_line = true;
|
||||
while (true) {
|
||||
if (editor->folded[line_index]) {
|
||||
if (editor->folded[line_index] == 2) {
|
||||
Coord fold_coord = {line_index, 0};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = fold_coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = fold_coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
visual_seen++;
|
||||
if (visual_seen >= number + max_visual_lines) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
break;
|
||||
}
|
||||
}
|
||||
do {
|
||||
char *line = next_line(it, nullptr);
|
||||
if (!line) {
|
||||
free(scroll_queue);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
free(line);
|
||||
line_index++;
|
||||
} while (editor->folded[line_index] == 1);
|
||||
continue;
|
||||
}
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
break;
|
||||
if (line_len && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
uint32_t current_byte_offset = 0;
|
||||
if (first_visual_line) {
|
||||
current_byte_offset += editor->scroll.col;
|
||||
first_visual_line = false;
|
||||
}
|
||||
while (current_byte_offset < line_len ||
|
||||
(line_len == 0 && current_byte_offset == 0)) {
|
||||
Coord coord = {line_index, current_byte_offset};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
visual_seen++;
|
||||
if (visual_seen >= number + max_visual_lines) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
free(line);
|
||||
free(scroll_queue);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
uint32_t col = 0;
|
||||
uint32_t local_render_offset = 0;
|
||||
uint32_t left = line_len - current_byte_offset;
|
||||
while (left > 0 && col < render_width) {
|
||||
uint32_t cluster_len = grapheme_next_character_break_utf8(
|
||||
line + current_byte_offset + local_render_offset, left);
|
||||
int width = display_width(
|
||||
line + current_byte_offset + local_render_offset, cluster_len);
|
||||
if (col + width > render_width)
|
||||
break;
|
||||
local_render_offset += cluster_len;
|
||||
left -= cluster_len;
|
||||
col += width;
|
||||
}
|
||||
current_byte_offset += local_render_offset;
|
||||
if (line_len == 0)
|
||||
break;
|
||||
}
|
||||
free(line);
|
||||
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(scroll_queue);
|
||||
}
|
||||
|
||||
void ensure_cursor(Editor *editor) {
|
||||
std::shared_lock knot_lock(editor->knot_mtx);
|
||||
if (editor->cursor < editor->scroll) {
|
||||
editor->cursor = editor->scroll;
|
||||
return;
|
||||
}
|
||||
uint32_t numlen =
|
||||
2 + static_cast<int>(std::log10(editor->root->line_count + 1));
|
||||
uint32_t render_width = editor->size.col - numlen;
|
||||
uint32_t visual_rows = 0;
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
bool first_visual_line = true;
|
||||
LineIterator *it = begin_l_iter(editor->root, line_index);
|
||||
if (!it)
|
||||
return;
|
||||
Coord last_visible = editor->scroll;
|
||||
while (true) {
|
||||
if (visual_rows >= editor->size.row)
|
||||
break;
|
||||
if (editor->folded[line_index]) {
|
||||
if (editor->folded[line_index] == 2) {
|
||||
Coord c = {line_index, 0};
|
||||
last_visible = c;
|
||||
visual_rows++;
|
||||
if (line_index == editor->cursor.row) {
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
do {
|
||||
char *line = next_line(it, nullptr);
|
||||
if (!line)
|
||||
break;
|
||||
free(line);
|
||||
line_index++;
|
||||
} while (editor->folded[line_index] == 1);
|
||||
continue;
|
||||
}
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
break;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
uint32_t offset = first_visual_line ? editor->scroll.col : 0;
|
||||
first_visual_line = false;
|
||||
while (offset < line_len || (line_len == 0 && offset == 0)) {
|
||||
Coord current = {line_index, offset};
|
||||
last_visible = current;
|
||||
visual_rows++;
|
||||
if (visual_rows >= editor->size.row)
|
||||
break;
|
||||
uint32_t col = 0;
|
||||
uint32_t advance = 0;
|
||||
uint32_t left = line_len - offset;
|
||||
while (left > 0 && col < render_width) {
|
||||
uint32_t g =
|
||||
grapheme_next_character_break_utf8(line + offset + advance, left);
|
||||
int w = display_width(line + offset + advance, g);
|
||||
if (col + w > render_width)
|
||||
break;
|
||||
advance += g;
|
||||
left -= g;
|
||||
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)
|
||||
break;
|
||||
offset += advance;
|
||||
if (line_len == 0)
|
||||
break;
|
||||
}
|
||||
free(line);
|
||||
line_index++;
|
||||
}
|
||||
editor->cursor = last_visible;
|
||||
free(it);
|
||||
}
|
||||
|
||||
void ensure_scroll(Editor *editor) {
|
||||
std::shared_lock knot_lock(editor->knot_mtx);
|
||||
uint32_t numlen =
|
||||
2 + static_cast<int>(std::log10(editor->root->line_count + 1));
|
||||
uint32_t render_width = editor->size.col - numlen;
|
||||
if (editor->cursor < editor->scroll) {
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t len;
|
||||
char *line = next_line(it, &len);
|
||||
if (!line) {
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
--len;
|
||||
uint32_t rows = 1;
|
||||
uint32_t cols = 0;
|
||||
uint32_t offset = 0;
|
||||
uint32_t old_offset = 0;
|
||||
while (offset < len) {
|
||||
uint32_t inc =
|
||||
grapheme_next_character_break_utf8(line + offset, len - offset);
|
||||
int width = display_width(line + offset, inc);
|
||||
if (cols + width > render_width) {
|
||||
rows++;
|
||||
cols = 0;
|
||||
if (editor->cursor.col > old_offset && editor->cursor.col <= offset) {
|
||||
editor->scroll.row = editor->cursor.row;
|
||||
editor->scroll.col = old_offset;
|
||||
free(line);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
old_offset = offset;
|
||||
}
|
||||
cols += width;
|
||||
offset += inc;
|
||||
}
|
||||
free(line);
|
||||
free(it);
|
||||
editor->scroll.row = editor->cursor.row;
|
||||
editor->scroll.col = (editor->cursor.col == 0) ? 0 : old_offset;
|
||||
} else {
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
LineIterator *it = begin_l_iter(editor->root, line_index);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t max_visual_lines = editor->size.row;
|
||||
Coord *scroll_queue = (Coord *)malloc(sizeof(Coord) * max_visual_lines);
|
||||
uint32_t q_head = 0;
|
||||
uint32_t q_size = 0;
|
||||
bool first_visual_line = true;
|
||||
while (true) {
|
||||
if (editor->folded[line_index]) {
|
||||
if (editor->folded[line_index] == 2) {
|
||||
Coord fold_coord = {line_index, 0};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = fold_coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = fold_coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
if (line_index == editor->cursor.row) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
break;
|
||||
}
|
||||
}
|
||||
do {
|
||||
char *line = next_line(it, nullptr);
|
||||
if (!line)
|
||||
break;
|
||||
free(line);
|
||||
line_index++;
|
||||
} while (line_index < editor->size.row &&
|
||||
editor->folded[line_index] == 1);
|
||||
continue;
|
||||
}
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
break;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
uint32_t current_byte_offset = 0;
|
||||
if (first_visual_line) {
|
||||
current_byte_offset += editor->scroll.col;
|
||||
first_visual_line = false;
|
||||
}
|
||||
while (current_byte_offset < line_len ||
|
||||
(line_len == 0 && current_byte_offset == 0)) {
|
||||
Coord current_coord = {line_index, current_byte_offset};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = current_coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = current_coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
uint32_t col = 0;
|
||||
uint32_t local_render_offset = 0;
|
||||
uint32_t line_left = line_len - current_byte_offset;
|
||||
while (line_left > 0 && col < render_width) {
|
||||
uint32_t cluster_len = grapheme_next_character_break_utf8(
|
||||
line + current_byte_offset + local_render_offset, line_left);
|
||||
int width = display_width(
|
||||
line + current_byte_offset + local_render_offset, cluster_len);
|
||||
if (col + width > render_width)
|
||||
break;
|
||||
local_render_offset += cluster_len;
|
||||
line_left -= cluster_len;
|
||||
col += width;
|
||||
}
|
||||
if (line_index == editor->cursor.row) {
|
||||
bool cursor_found = false;
|
||||
if (editor->cursor.col >= current_byte_offset &&
|
||||
editor->cursor.col < current_byte_offset + local_render_offset)
|
||||
cursor_found = true;
|
||||
else if (editor->cursor.col == line_len &&
|
||||
current_byte_offset + local_render_offset == line_len)
|
||||
cursor_found = true;
|
||||
if (cursor_found) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
free(line);
|
||||
free(scroll_queue);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
current_byte_offset += local_render_offset;
|
||||
if (line_len == 0)
|
||||
break;
|
||||
}
|
||||
line_index++;
|
||||
free(line);
|
||||
}
|
||||
free(scroll_queue);
|
||||
free(it);
|
||||
}
|
||||
}
|
||||
141
src/main.cc
141
src/main.cc
@@ -4,17 +4,20 @@
|
||||
#include "../include/ui.h"
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <sys/ioctl.h>
|
||||
#include <thread>
|
||||
|
||||
std::atomic<bool> running{true};
|
||||
Queue<KeyEvent> event_queue;
|
||||
std::vector<Editor *> editors;
|
||||
|
||||
uint8_t current_editor = 0;
|
||||
uint8_t mode = NORMAL;
|
||||
|
||||
void background_worker(Editor *editor) {
|
||||
void background_worker() {
|
||||
while (running) {
|
||||
ts_collect_spans(editor);
|
||||
ts_collect_spans(editors[current_editor]);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(16));
|
||||
}
|
||||
}
|
||||
@@ -24,107 +27,33 @@ void input_listener() {
|
||||
KeyEvent event = read_key();
|
||||
if (event.key_type == KEY_NONE)
|
||||
continue;
|
||||
if (event.key_type == KEY_CHAR && *event.c == CTRL('q'))
|
||||
if (event.key_type == KEY_CHAR && event.len == 1 && *event.c == CTRL('q')) {
|
||||
free(event.c);
|
||||
running = false;
|
||||
return;
|
||||
}
|
||||
event_queue.push(event);
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
||||
}
|
||||
}
|
||||
|
||||
void handle_editor_event(Editor *editor, KeyEvent event) {
|
||||
if (event.key_type == KEY_SPECIAL) {
|
||||
switch (event.special_key) {
|
||||
case KEY_DOWN:
|
||||
cursor_down(editor, 1);
|
||||
break;
|
||||
case KEY_UP:
|
||||
cursor_up(editor, 1);
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
cursor_left(editor, 1);
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
cursor_right(editor, 1);
|
||||
break;
|
||||
Editor *editor_at(uint8_t x, uint8_t y) {
|
||||
for (Editor *ed : editors) {
|
||||
Coord pos = ed->position;
|
||||
Coord size = ed->size;
|
||||
if (x >= pos.col && x < pos.col + size.col && y >= pos.row &&
|
||||
y < pos.row + size.row)
|
||||
return ed;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
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;
|
||||
|
||||
uint8_t index_of(Editor *ed) {
|
||||
for (uint8_t i = 0; i < editors.size(); i++) {
|
||||
if (editors[i] == ed)
|
||||
return i;
|
||||
}
|
||||
}
|
||||
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);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
@@ -132,21 +61,37 @@ int main(int argc, char *argv[]) {
|
||||
const char *filename = (argc > 1) ? argv[1] : "";
|
||||
|
||||
Editor *editor = new_editor(filename, {0, 0}, screen);
|
||||
|
||||
if (!editor) {
|
||||
end_screen();
|
||||
fprintf(stderr, "Failed to load editor\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
editors.push_back(editor);
|
||||
current_editor = editors.size() - 1;
|
||||
|
||||
std::thread input_thread(input_listener);
|
||||
std::thread work_thread(background_worker, editor);
|
||||
std::thread work_thread(background_worker);
|
||||
|
||||
while (running) {
|
||||
KeyEvent event;
|
||||
while (event_queue.pop(event))
|
||||
handle_editor_event(editor, event);
|
||||
while (event_queue.pop(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();
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(8));
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "../include/ui.h"
|
||||
|
||||
uint32_t rows, cols;
|
||||
int show_cursor = 0;
|
||||
bool show_cursor = 0;
|
||||
std::vector<ScreenCell> screen;
|
||||
std::vector<ScreenCell> old_screen;
|
||||
std::mutex screen_mutex;
|
||||
@@ -187,9 +187,10 @@ void render() {
|
||||
}
|
||||
}
|
||||
|
||||
void set_cursor(int row, int col, int show_cursor_param) {
|
||||
void set_cursor(int row, int col, int type, bool show_cursor_param) {
|
||||
char buf[32];
|
||||
int n = snprintf(buf, sizeof(buf), "\x1b[%d;%dH\x1b[5 q", row + 1, col + 1);
|
||||
int n = snprintf(buf, sizeof(buf), "\x1b[%d;%dH\x1b[%d q", row + 1, col + 1,
|
||||
type);
|
||||
show_cursor = show_cursor_param;
|
||||
write(STDOUT_FILENO, buf, n);
|
||||
}
|
||||
|
||||
16
src/ts.cc
16
src/ts.cc
@@ -1,7 +1,6 @@
|
||||
#include "../include/ts.h"
|
||||
#include "../include/editor.h"
|
||||
#include "../include/knot.h"
|
||||
#include "../include/main.h"
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
@@ -11,9 +10,8 @@
|
||||
std::unordered_map<std::string, pcre2_code *> regex_cache;
|
||||
|
||||
void clear_regex_cache() {
|
||||
for (auto &kv : regex_cache) {
|
||||
for (auto &kv : regex_cache)
|
||||
pcre2_code_free(kv.second);
|
||||
}
|
||||
regex_cache.clear();
|
||||
}
|
||||
|
||||
@@ -168,10 +166,6 @@ static inline bool ts_predicate(TSQuery *query, const TSQueryMatch &match,
|
||||
|
||||
const char *read_ts(void *payload, uint32_t byte_index, TSPoint,
|
||||
uint32_t *bytes_read) {
|
||||
if (!running) {
|
||||
*bytes_read = 0;
|
||||
return "";
|
||||
}
|
||||
Editor *editor = (Editor *)payload;
|
||||
if (byte_index >= editor->root->char_count) {
|
||||
*bytes_read = 0;
|
||||
@@ -201,8 +195,6 @@ void ts_collect_spans(Editor *editor) {
|
||||
if (editor->tree)
|
||||
copy = ts_tree_copy(editor->tree);
|
||||
knot_mtx.unlock();
|
||||
if (!running)
|
||||
return;
|
||||
std::vector<TSInputEdit> edits;
|
||||
TSInputEdit edit;
|
||||
if (copy)
|
||||
@@ -234,13 +226,9 @@ void ts_collect_spans(Editor *editor) {
|
||||
new_spans.reserve(4096);
|
||||
TSQueryMatch match;
|
||||
while (ts_query_cursor_next_match(cursor, &match)) {
|
||||
if (!running)
|
||||
break;
|
||||
if (!ts_predicate(editor->query, match, editor->root))
|
||||
continue;
|
||||
for (uint32_t i = 0; i < match.capture_count; i++) {
|
||||
if (!running)
|
||||
break;
|
||||
TSQueryCapture cap = match.captures[i];
|
||||
uint32_t start = ts_node_start_byte(cap.node);
|
||||
uint32_t end = ts_node_end_byte(cap.node);
|
||||
@@ -251,8 +239,6 @@ void ts_collect_spans(Editor *editor) {
|
||||
}
|
||||
ts_query_cursor_delete(cursor);
|
||||
ts_tree_delete(copy);
|
||||
if (!running)
|
||||
return;
|
||||
std::sort(new_spans.begin(), new_spans.end());
|
||||
std::pair<uint32_t, int64_t> span_edit;
|
||||
while (editor->spans.edits.pop(span_edit))
|
||||
|
||||
50
src/utils.cc
50
src/utils.cc
@@ -7,6 +7,7 @@ extern "C" {
|
||||
#include <cstdarg>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <limits.h>
|
||||
@@ -16,6 +17,55 @@ extern "C" {
|
||||
#include <unistd.h>
|
||||
#include <unordered_map>
|
||||
|
||||
char *get_from_clipboard(uint32_t *out_len) {
|
||||
FILE *pipe = popen("xclip -selection clipboard -o", "r");
|
||||
if (!pipe) {
|
||||
*out_len = 0;
|
||||
return nullptr;
|
||||
}
|
||||
size_t capacity = 4096;
|
||||
size_t length = 0;
|
||||
char *buffer = (char *)malloc(capacity);
|
||||
if (!buffer) {
|
||||
pclose(pipe);
|
||||
*out_len = 0;
|
||||
return nullptr;
|
||||
}
|
||||
size_t n;
|
||||
while ((n = fread(buffer + length, 1, capacity - length, pipe)) > 0) {
|
||||
length += n;
|
||||
if (length == capacity) {
|
||||
capacity *= 2;
|
||||
char *tmp = (char *)realloc(buffer, capacity);
|
||||
if (!tmp) {
|
||||
free(buffer);
|
||||
pclose(pipe);
|
||||
*out_len = 0;
|
||||
return nullptr;
|
||||
}
|
||||
buffer = tmp;
|
||||
}
|
||||
}
|
||||
pclose(pipe);
|
||||
char *result = (char *)realloc(buffer, length + 1);
|
||||
if (result) {
|
||||
result[length] = '\0';
|
||||
buffer = result;
|
||||
} else {
|
||||
buffer[length] = '\0';
|
||||
}
|
||||
*out_len = length;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void copy_to_clipboard(const char *text, size_t len) {
|
||||
FILE *pipe = popen("xclip -selection clipboard", "w");
|
||||
if (!pipe)
|
||||
return;
|
||||
fwrite(text, sizeof(char), len, pipe);
|
||||
pclose(pipe);
|
||||
}
|
||||
|
||||
int display_width(const char *str, size_t len) {
|
||||
if (!str || !*str)
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user