Skip to content

Commit

Permalink
better reverse syncronization
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Feb 13, 2022
1 parent 1aa42ea commit 2f72801
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = "1.0.1"
VERSION = "1.0.2"
CC = clang
DEFINES = -DHAVE_CONFIG_H -DMACOS_X -DMACOS_X_DARWIN
LIBS = lib/libvim.a -lm -lncurses -liconv -framework Carbon -framework Cocoa
Expand Down
3 changes: 3 additions & 0 deletions src/ax.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ CGEventRef ax_process_event(struct ax* ax, CGEventRef event) {

// Insert mode is passed and only synced later
if (was_insert && ax->buffer.cursor.mode & INSERT) return event;
else if (was_insert) {
if (!ax_get_text(ax) || !ax_get_cursor(ax)) return event;
}

ax_set_buffer(ax);

Expand Down
22 changes: 14 additions & 8 deletions src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ void buffer_sync_cursor(struct buffer* buffer) {
}

uint32_t pos = line_get_position_from_raw_position(buffer->lines[cursor_pos.lnum - 1], cursor_pos.col);

buffer->cursor.position = cursor_pos_cummulative + pos;

if (buffer->cursor.mode & NORMAL)
if (buffer->cursor.mode & NORMAL) {
buffer->cursor.selection = 1;
}
else if (buffer->cursor.mode & VISUAL) {
pos_T start, end;
vimVisualGetRange(&start, &end);
Expand Down Expand Up @@ -192,12 +194,13 @@ void buffer_sync(struct buffer* buffer) {
void buffer_input(struct buffer* buffer, UniChar key, UniCharCount count) {
if (key == 0x1B) {
vimKey(NORMAL_MODE);
} else {
}
else {
char_u* key_str = malloc(sizeof(char_u) * (2 * count + 1));
memset(key_str, 0, (2 * count + 1));
snprintf(key_str, 2 * count + 1, "%lc", key);

vimInput(key_str);
vimInput(key_str);
free(key_str);
}

Expand All @@ -220,19 +223,22 @@ void buffer_revsync_cursor(struct buffer* buffer) {
uint32_t line_character_count = 0;
for (int i = 0; i < buffer->line_count; i++) {
line_character_count = buffer->lines[i]->length;
character_count += buffer->lines[i]->length + (i == 0 ? 0 : 1);
character_count += buffer->lines[i]->length + 1;
line = i + 1;
if (character_count > buffer->cursor.position) break;
}

pos_T pos = {0, 0, 0};
pos.lnum = line;
pos.col = line_get_raw_position_from_position(buffer->lines[line - 1],
buffer->cursor.position
+ line_character_count
- character_count);
if (line > 0) {
pos.col = line_get_raw_position_from_position(buffer->lines[line - 1],
buffer->cursor.position
+ line_character_count
- character_count);
}

vimCursorSetPosition(pos);
buffer_sync_cursor(buffer);
}

void buffer_clear(struct buffer* buffer) {
Expand Down
3 changes: 2 additions & 1 deletion src/line.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ uint32_t line_get_position_from_raw_position(struct line* line, uint32_t raw_pos

wchar_t* tmp2 = malloc(sizeof(wchar_t) * (raw_pos + 1));
memset(tmp2, 0, sizeof(wchar_t) * (raw_pos + 1));
swprintf(tmp2, raw_pos + 1, L"%s\0", tmp);
swprintf(tmp2, raw_pos + 1, L"%s", tmp);
tmp2[raw_pos] = '\0';
uint32_t len = wcslen(tmp2);
free(tmp);
free(tmp2);
Expand Down

0 comments on commit 2f72801

Please sign in to comment.