diff --git a/makefile b/makefile index 9eef06d..e0d7ce9 100644 --- a/makefile +++ b/makefile @@ -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 diff --git a/src/ax.c b/src/ax.c index 8e65ac3..6163796 100644 --- a/src/ax.c +++ b/src/ax.c @@ -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); diff --git a/src/buffer.c b/src/buffer.c index f3a678d..a06aa64 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -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); @@ -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); } @@ -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) { diff --git a/src/line.c b/src/line.c index 10c7d6c..ff828dd 100644 --- a/src/line.c +++ b/src/line.c @@ -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);