Skip to content

Commit

Permalink
Optimizations and Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Feb 19, 2022
1 parent a91393f commit e926be6
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 62 deletions.
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ lib:
cp libvim/src/libvim.a lib/libvim.a

bin/svim: $(SRC)/main.m $(OBJ) | $(ODIR)
$(CC) $(CFLAGS) $^ -o $(ODIR)/svim $(LIBS)
$(CC) $(CFLAGS) $^ -o $@ $(LIBS)

$(ODIR)/%.o: $(SRC)/%.c $(SRC)/%.h | $(ODIR)
$(CC) -c -o $@ $< $(CFLAGS)
Expand Down
16 changes: 8 additions & 8 deletions src/ax.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void ax_begin(struct ax* ax) {
assert(ax->system_element != NULL);
}

bool ax_get_text(struct ax* ax) {
static inline bool ax_get_text(struct ax* ax) {
CFTypeRef text_ref;
AXError error = AXUIElementCopyAttributeValue(ax->selected_element,
kAXValueAttribute,
Expand All @@ -52,7 +52,7 @@ bool ax_get_text(struct ax* ax) {
return error == kAXErrorSuccess;
}

bool ax_get_cursor(struct ax* ax) {
static inline bool ax_get_cursor(struct ax* ax) {
CFTypeRef text_range_ref = NULL;
CFRange text_range = CFRangeMake(0, 0);
AXError error = AXUIElementCopyAttributeValue(ax->selected_element,
Expand All @@ -74,9 +74,9 @@ bool ax_get_cursor(struct ax* ax) {
return error == kAXErrorSuccess;
}

bool ax_set_text(struct ax* ax) {
static inline bool ax_set_text(struct ax* ax) {
if (!ax->is_supported || !ax->buffer.raw) return false;
if (!ax->buffer.lines_changed) return true;
if (!ax->buffer.did_change) return true;

CFStringRef text_ref = CFStringCreateWithCString(NULL,
ax->buffer.raw,
Expand All @@ -90,15 +90,15 @@ bool ax_set_text(struct ax* ax) {
return error == kAXErrorSuccess;
}

bool ax_set_cursor(struct ax* ax) {
static inline bool ax_set_cursor(struct ax* ax) {
if (!ax->is_supported) return false;

CFRange text_range = CFRangeMake(ax->buffer.cursor.position,
ax->buffer.cursor.selection);
AXValueRef value = AXValueCreate(kAXValueCFRangeType, &text_range);
// HACK: This is needed when the text has been set to give the
// HACK: AX API some time to breathe...
if (ax->buffer.lines_changed) usleep(15000);
if (ax->buffer.did_change) usleep(15000);

AXError error = AXUIElementSetAttributeValue(ax->selected_element,
kAXSelectedTextRangeAttribute,
Expand All @@ -108,12 +108,12 @@ bool ax_set_cursor(struct ax* ax) {
return error == kAXErrorSuccess;
}

bool ax_set_buffer(struct ax* ax) {
static inline bool ax_set_buffer(struct ax* ax) {
return ax_set_text(ax)
&& ax_set_cursor(ax);
}

bool ax_get_selected_element(struct ax* ax) {
static inline bool ax_get_selected_element(struct ax* ax) {
CFTypeRef selected_element = NULL;
AXError error = AXUIElementCopyAttributeValue(ax->system_element,
kAXFocusedUIElementAttribute,
Expand Down
38 changes: 23 additions & 15 deletions src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void buffer_begin(struct buffer* buffer) {
buffer_loadrc(buffer);
}

void buffer_update_raw_text(struct buffer* buffer) {
static inline bool buffer_update_raw_text(struct buffer* buffer) {
uint32_t lines = vimBufferGetLineCount(buffer->vbuf);

char_u* raw = NULL;
Expand All @@ -42,12 +42,18 @@ void buffer_update_raw_text(struct buffer* buffer) {
}
raw[len - 1] = '\0';

if (raw && buffer->raw && strcmp(buffer->raw, raw) == 0) {
free(raw);
return false;
}

if (buffer->raw) free(buffer->raw);
buffer->raw = raw;
return true;
}

void buffer_sync_text(struct buffer* buffer) {
buffer_update_raw_text(buffer);
static inline void buffer_sync_text(struct buffer* buffer) {
buffer->did_change = buffer_update_raw_text(buffer);

uint32_t lines = vimBufferGetLineCount(buffer->vbuf);
uint32_t old_count = buffer->line_count;
Expand All @@ -60,24 +66,23 @@ void buffer_sync_text(struct buffer* buffer) {
buffer->lines = realloc(buffer->lines, sizeof(struct line*) * lines);
buffer->line_count = lines;

buffer->lines_changed = 0;
uint32_t cursor_pos_cummulative = 0;
for (int i = 1; i <= lines; i++) {
char_u* line = vimBufferGetLine(buffer->vbuf, i);
if (i > old_count) buffer->lines[i - 1] = line_create();
line_set_text(buffer->lines[i - 1], line);
buffer->lines[i - 1]->cursor_offset = cursor_pos_cummulative;
cursor_pos_cummulative += buffer->lines[i - 1]->length + 1;
buffer->lines_changed += buffer->lines[i - 1]->changed;
}
}

void buffer_sync_cursor(struct buffer* buffer) {
static inline void buffer_sync_cursor(struct buffer* buffer) {
pos_T cursor_pos = vimCursorGetPosition();
uint32_t pos = line_get_position_from_raw_position(buffer->lines[cursor_pos.lnum - 1],
cursor_pos.col);

buffer->cursor.position = buffer->lines[cursor_pos.lnum - 1]->cursor_offset + pos;
buffer->cursor.position = buffer->lines[cursor_pos.lnum - 1]->cursor_offset
+ pos;

if (buffer->cursor.mode & NORMAL) {
buffer->cursor.selection = 1;
Expand Down Expand Up @@ -121,20 +126,23 @@ void buffer_sync_cursor(struct buffer* buffer) {
buffer->cursor.selection = 0;
}

bool buffer_sync_mode(struct buffer* buffer) {
static inline bool buffer_sync_mode(struct buffer* buffer) {
uint32_t mode = vimGetMode();
if (mode & buffer->cursor.mode) return false;
buffer->cursor.mode = mode;

return true;
}

bool buffer_sync_cmdline(struct buffer* buffer) {
char* old = buffer->command_line.raw ? string_copy(buffer->command_line.raw) : NULL;
static inline bool buffer_sync_cmdline(struct buffer* buffer) {
char* old = buffer->command_line.raw
? string_copy(buffer->command_line.raw)
: NULL;

line_set_text(&buffer->command_line, vimCommandLineGetText());

if (buffer->command_line.raw && old && strcmp(old, buffer->command_line.raw) == 0) {
if (buffer->command_line.raw && old
&& strcmp(old, buffer->command_line.raw) == 0) {

free(old);
return false;
Expand Down Expand Up @@ -199,9 +207,9 @@ void buffer_input(struct buffer* buffer, UniChar key, UniCharCount count) {
vimKey(NORMAL_MODE);
}
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);
char_u* key_str = malloc(sizeof(char_u) * (sizeof(UniChar) * count + 1));
memset(key_str, 0, (sizeof(UniChar) * count + 1));
snprintf(key_str, sizeof(UniChar) * count + 1, "%lc", key);

vimInput(key_str);
free(key_str);
Expand Down Expand Up @@ -252,7 +260,7 @@ void buffer_clear(struct buffer* buffer) {
line_clear(&buffer->command_line);

buffer->cursor = (struct cursor){0, 0, 0};
buffer->lines_changed = 0;
buffer->did_change = false;
buffer->line_count = 0;
buffer->lines = NULL;
buffer->raw = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ struct cursor {
struct buffer {
buf_T* vbuf;
char_u* raw;
bool did_change;

uint32_t lines_changed;
struct line command_line;
char cmd_line_mode;

Expand Down
52 changes: 17 additions & 35 deletions src/line.c
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
#include "line.h"
#include <string.h>

struct line* line_create() {
struct line* line = malloc(sizeof(struct line));
memset(line, 0, sizeof(struct line));
return line;
}

bool line_compare_raw(struct line* line, char* raw) {
if (line->raw && raw && strcmp(line->raw, raw) == 0)
return true;
return false;
}

void line_set_text(struct line* line, char* text) {
if (!text) return;

if (line_compare_raw(line, text)) {
line->changed = false;
return;
}

uint32_t len = strlen(text);
line->text = realloc(line->text, sizeof(wchar_t) * (len + 1));
swprintf(line->text, len + 1, L"%s", text);
Expand All @@ -29,7 +19,6 @@ void line_set_text(struct line* line, char* text) {
line->raw = realloc(line->raw, sizeof(char) * (len + 1));
memcpy(line->raw, text, sizeof(char) * (len + 1));
line->raw_length = len;
line->changed = true;
}

void line_clear(struct line* line) {
Expand All @@ -47,38 +36,31 @@ void line_destroy(struct line* line) {
free(line);
}

uint32_t line_get_position_from_raw_position(struct line* line, uint32_t raw_pos) {
uint32_t line_get_position_from_raw_position(struct line* line,
uint32_t raw_pos) {
if (line->raw_length >= raw_pos) {
char* tmp = malloc(sizeof(char) * (raw_pos + 1));
memcpy(tmp, line->raw, sizeof(char) * raw_pos);
tmp[raw_pos] = '\0';
char raw[raw_pos + 1];
wchar_t wide[raw_pos + 1];

memcpy(raw, line->raw, sizeof(char) * raw_pos);
raw[raw_pos] = '\0';

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", tmp);
tmp2[raw_pos] = '\0';
uint32_t len = wcslen(tmp2);
free(tmp);
free(tmp2);
return len;
swprintf(wide, raw_pos + 1, L"%s", raw);
return wcslen(wide);
}
return 0;
}

uint32_t line_get_raw_position_from_position(struct line* line, uint32_t pos) {
if (line->length >= pos) {
wchar_t* tmp = malloc(sizeof(wchar_t) * (pos + 1));
memcpy(tmp, line->text, sizeof(wchar_t) * pos);
tmp[pos] = '\0';
wchar_t wide[pos + 1];
char raw[2*pos + 1];

memcpy(wide, line->text, sizeof(wchar_t) * pos);
wide[pos] = '\0';

char* tmp2 = malloc(sizeof(char) * (2*pos + 1));
memset(tmp2, 0, sizeof(char) * (2*pos + 1));
snprintf(tmp2, 2*pos + 1, "%S", tmp);
tmp2[2*pos] = '\0';
uint32_t len = strlen(tmp2);
free(tmp);
free(tmp2);
return len;
snprintf(raw, 2*pos + 1, "%S", wide);
return strlen(raw);
}
return 0;
}
Expand Down
2 changes: 0 additions & 2 deletions src/line.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <stdbool.h>

struct line {
bool changed;

uint32_t length;
uint32_t raw_length;
uint32_t cursor_offset;
Expand Down

0 comments on commit e926be6

Please sign in to comment.