2016-07-09 15:04:09 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
2018-02-02 12:31:34 +03:00
|
|
|
#import <Foundation/Foundation.h>
|
2016-10-22 18:25:43 +03:00
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
#import "Logging.h"
|
2018-03-20 09:53:46 +03:00
|
|
|
#import "server_ui.h"
|
2017-12-20 23:35:36 +03:00
|
|
|
#import "NvimServer.h"
|
2016-08-26 16:31:30 +03:00
|
|
|
#import "CocoaCategories.h"
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2017-06-06 23:39:41 +03:00
|
|
|
// FileInfo and Boolean are #defined by Carbon and NeoVim:
|
|
|
|
// Since we don't need the Carbon versions of them, we rename
|
2016-07-09 15:04:09 +03:00
|
|
|
// them.
|
|
|
|
#define FileInfo CarbonFileInfo
|
|
|
|
#define Boolean CarbonBoolean
|
|
|
|
|
|
|
|
#import <nvim/vim.h>
|
|
|
|
#import <nvim/api/vim.h>
|
|
|
|
#import <nvim/ui.h>
|
|
|
|
#import <nvim/ui_bridge.h>
|
2016-08-19 00:11:33 +03:00
|
|
|
#import <nvim/fileio.h>
|
|
|
|
#import <nvim/undo.h>
|
2017-05-30 23:13:52 +03:00
|
|
|
#import <nvim/mouse.h>
|
2017-06-01 23:15:59 +03:00
|
|
|
#import <nvim/screen.h>
|
2017-05-30 23:13:52 +03:00
|
|
|
#import <nvim/edit.h>
|
2017-06-25 18:35:51 +03:00
|
|
|
#import <nvim/syntax.h>
|
2017-09-07 19:24:06 +03:00
|
|
|
#import <nvim/aucmd.h>
|
2018-05-10 20:33:50 +03:00
|
|
|
#import <nvim/msgpack_rpc/helpers.h>
|
|
|
|
#import <msgpack.h>
|
|
|
|
#import <nvim/api/private/helpers.h>
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
#define let __auto_type const
|
|
|
|
#define var __auto_type
|
2016-09-02 19:22:40 +03:00
|
|
|
#define pun_type(t, x) (*((t *) (&(x))))
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2017-05-27 10:31:00 +03:00
|
|
|
|
2017-05-27 18:22:47 +03:00
|
|
|
static NSInteger _default_foreground = 0xFF000000;
|
|
|
|
static NSInteger _default_background = 0xFFFFFFFF;
|
|
|
|
static NSInteger _default_special = 0xFFFF0000;
|
2017-05-27 10:31:00 +03:00
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
typedef struct {
|
2018-05-10 20:33:50 +03:00
|
|
|
UIBridgeData *bridge;
|
|
|
|
Loop *loop;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
bool stop;
|
2016-07-09 15:04:09 +03:00
|
|
|
} ServerUiData;
|
|
|
|
|
|
|
|
// We declare nvim_main because it's not declared in any header files of neovim
|
|
|
|
extern int nvim_main(int argc, char **argv);
|
|
|
|
|
|
|
|
|
|
|
|
// The thread in which neovim's main runs
|
|
|
|
static uv_thread_t _nvim_thread;
|
|
|
|
|
2017-06-06 23:39:41 +03:00
|
|
|
// Condition variable used by the XPC's init to wait till our custom UI initialization
|
|
|
|
// is finished inside neovim
|
2016-07-09 15:04:09 +03:00
|
|
|
static bool _is_ui_launched = false;
|
|
|
|
static uv_mutex_t _mutex;
|
|
|
|
static uv_cond_t _condition;
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
static ServerUiData *_server_ui_data;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
static NSString *_marked_text = nil;
|
|
|
|
|
2017-05-11 09:08:58 +03:00
|
|
|
static NSInteger _marked_row = 0;
|
|
|
|
static NSInteger _marked_column = 0;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2017-06-06 23:39:41 +03:00
|
|
|
// for 하 -> hanja popup, Cocoa first inserts 하, then sets marked text,
|
|
|
|
// cf docs/notes-on-cocoa-text-input.md
|
2017-05-11 09:08:58 +03:00
|
|
|
static NSInteger _marked_delta = 0;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2017-05-11 09:08:58 +03:00
|
|
|
static NSInteger _put_row = -1;
|
|
|
|
static NSInteger _put_column = -1;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
static NSString *_backspace = nil;
|
|
|
|
|
2017-05-06 10:39:42 +03:00
|
|
|
static bool _dirty = false;
|
|
|
|
|
2017-06-12 20:28:28 +03:00
|
|
|
static NSInteger _initialWidth = 30;
|
|
|
|
static NSInteger _initialHeight = 15;
|
|
|
|
|
2018-05-11 11:41:08 +03:00
|
|
|
static msgpack_sbuffer msg_sbuffer;
|
|
|
|
static msgpack_sbuffer flush_sbuffer;
|
2018-05-10 20:33:50 +03:00
|
|
|
static msgpack_packer *flush_packer;
|
2018-01-20 20:18:12 +03:00
|
|
|
|
2017-01-06 12:16:55 +03:00
|
|
|
#pragma mark Helper functions
|
2018-05-10 20:33:50 +03:00
|
|
|
|
2016-11-10 19:33:14 +03:00
|
|
|
static inline String vim_string_from(NSString *str) {
|
2018-05-10 20:33:50 +03:00
|
|
|
return (String) {.data = (char *) str.cstr, .size = str.clength};
|
2016-11-10 19:33:14 +03:00
|
|
|
}
|
|
|
|
|
2017-06-10 11:16:32 +03:00
|
|
|
static void refresh_ui_screen(int type) {
|
|
|
|
update_screen(type);
|
|
|
|
setcursor();
|
|
|
|
ui_flush();
|
|
|
|
}
|
|
|
|
|
2017-01-06 12:16:55 +03:00
|
|
|
static bool has_dirty_docs() {
|
|
|
|
FOR_ALL_BUFFERS(buffer) {
|
|
|
|
if (bufIsChanged(buffer)) {
|
|
|
|
return true;
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
2017-01-06 12:16:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
typedef void (^pack_block)(msgpack_packer *packer);
|
|
|
|
|
|
|
|
static void send_msg_packing(NvimServerMsgId msgid, pack_block body) {
|
|
|
|
msgpack_packer packer;
|
2018-05-11 11:41:08 +03:00
|
|
|
msgpack_packer_init(&packer, &msg_sbuffer, msgpack_sbuffer_write);
|
2018-05-10 20:33:50 +03:00
|
|
|
|
|
|
|
body(&packer);
|
|
|
|
|
2018-05-11 11:41:08 +03:00
|
|
|
var data = [[NSData alloc] initWithBytesNoCopy:msg_sbuffer.data length:msg_sbuffer.size freeWhenDone:false];
|
2018-05-10 20:33:50 +03:00
|
|
|
[_neovim_server sendMessageWithId:msgid data:data];
|
|
|
|
[data release];
|
2018-05-11 11:41:08 +03:00
|
|
|
|
|
|
|
msgpack_sbuffer_clear(&msg_sbuffer);
|
2018-05-10 20:33:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pack_flush_data(RenderDataType type, pack_block body) {
|
|
|
|
msgpack_pack_array(flush_packer, 2);
|
|
|
|
msgpack_pack_int64(flush_packer, type);
|
|
|
|
body(flush_packer);
|
|
|
|
}
|
|
|
|
|
2017-05-06 10:39:42 +03:00
|
|
|
static void send_dirty_status() {
|
2018-05-10 20:33:50 +03:00
|
|
|
var new_dirty_status = has_dirty_docs();
|
2017-05-06 10:39:42 +03:00
|
|
|
DLOG("dirty status: %d vs. %d", _dirty, new_dirty_status);
|
|
|
|
if (_dirty == new_dirty_status) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_dirty = new_dirty_status;
|
|
|
|
DLOG("sending dirty status: %d", _dirty);
|
2018-05-10 20:33:50 +03:00
|
|
|
|
|
|
|
send_msg_packing(NvimServerMsgIdDirtyStatusChanged, ^(msgpack_packer *packer) {
|
|
|
|
if (_dirty) {
|
|
|
|
msgpack_pack_true(packer);
|
|
|
|
} else {
|
|
|
|
msgpack_pack_false(packer);
|
|
|
|
}
|
|
|
|
});
|
2017-05-06 10:39:42 +03:00
|
|
|
}
|
|
|
|
|
2017-06-09 02:15:45 +03:00
|
|
|
static void send_cwd() {
|
2018-05-10 20:33:50 +03:00
|
|
|
var temp = xmalloc(MAXPATHL);
|
2017-06-09 02:15:45 +03:00
|
|
|
if (os_dirname(temp, MAXPATHL) == FAIL) {
|
|
|
|
xfree(temp);
|
2018-01-23 22:40:20 +03:00
|
|
|
[_neovim_server sendMessageWithId:NvimServerMsgIdCwdChanged];
|
2017-06-09 02:15:45 +03:00
|
|
|
}
|
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
send_msg_packing(NvimServerMsgIdCwdChanged, ^(msgpack_packer *packer) {
|
|
|
|
let value = cstr_to_string((const char *) temp);
|
|
|
|
msgpack_rpc_from_string(value, packer);
|
|
|
|
api_free_string(value);
|
|
|
|
xfree(temp);
|
|
|
|
});
|
2017-06-09 02:15:45 +03:00
|
|
|
}
|
|
|
|
|
2017-06-25 18:35:51 +03:00
|
|
|
static HlAttrs HlAttrsFromAttrCode(int attr_code) {
|
2018-05-10 20:33:50 +03:00
|
|
|
HlAttrs rgb_attrs = {false, false, false, false, false, -1, -1, -1};
|
|
|
|
let aep = syn_cterm_attr2entry(attr_code);
|
2017-06-25 18:35:51 +03:00
|
|
|
|
|
|
|
rgb_attrs.foreground = aep->rgb_fg_color;
|
|
|
|
rgb_attrs.background = aep->rgb_bg_color;
|
|
|
|
rgb_attrs.special = aep->rgb_sp_color;
|
2017-06-27 20:37:54 +03:00
|
|
|
rgb_attrs.reverse = (bool) (aep->rgb_ae_attr & HL_INVERSE);
|
2017-06-25 18:35:51 +03:00
|
|
|
|
|
|
|
return rgb_attrs;
|
|
|
|
}
|
|
|
|
|
2017-06-28 00:09:10 +03:00
|
|
|
static int foreground_for(HlAttrs attrs) {
|
2018-05-10 20:33:50 +03:00
|
|
|
return attrs.reverse ? attrs.background : attrs.foreground;
|
2017-06-28 00:09:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int background_for(HlAttrs attrs) {
|
2018-05-10 20:33:50 +03:00
|
|
|
return attrs.reverse ? attrs.foreground : attrs.background;
|
2017-06-28 00:09:10 +03:00
|
|
|
}
|
|
|
|
|
2017-06-25 18:35:51 +03:00
|
|
|
static void send_colorscheme() {
|
2017-06-27 20:37:54 +03:00
|
|
|
// It seems that the highlight groupt only gets updated when the screen is redrawn.
|
|
|
|
// Since there's a guard var, probably it's safe to call it here...
|
|
|
|
if (need_highlight_changed) {
|
|
|
|
highlight_changed();
|
|
|
|
}
|
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
let visualAttrs = HlAttrsFromAttrCode(highlight_attr[HLF_V]);
|
|
|
|
let dirAttrs = HlAttrsFromAttrCode(highlight_attr[HLF_D]);
|
2017-06-27 20:37:54 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
send_msg_packing(NvimServerMsgIdColorSchemeChanged, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_pack_array(packer, 5);
|
|
|
|
msgpack_pack_int64(packer, normal_fg);
|
|
|
|
msgpack_pack_int64(packer, normal_bg);
|
|
|
|
msgpack_pack_int64(packer, foreground_for(visualAttrs));
|
|
|
|
msgpack_pack_int64(packer, background_for(visualAttrs));
|
|
|
|
msgpack_pack_int64(packer, foreground_for(dirAttrs));
|
|
|
|
});
|
2017-06-25 18:35:51 +03:00
|
|
|
}
|
|
|
|
|
2017-01-06 12:16:55 +03:00
|
|
|
static void insert_marked_text(NSString *markedText) {
|
|
|
|
_marked_text = [markedText retain]; // release when the final text is input in -vimInput
|
|
|
|
|
|
|
|
nvim_input(vim_string_from(markedText));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delete_marked_text() {
|
2018-05-10 20:33:50 +03:00
|
|
|
let length = [_marked_text lengthOfBytesUsingEncoding:NSUTF32StringEncoding] / 4;
|
2017-01-06 12:16:55 +03:00
|
|
|
|
|
|
|
[_marked_text release];
|
|
|
|
_marked_text = nil;
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
nvim_input(vim_string_from(_backspace));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-26 10:01:12 +03:00
|
|
|
static void run_neovim(void *arg) {
|
2018-03-20 09:50:16 +03:00
|
|
|
int argc;
|
2017-06-12 01:30:33 +03:00
|
|
|
char **argv;
|
2017-01-06 12:16:55 +03:00
|
|
|
|
2017-06-12 01:30:33 +03:00
|
|
|
@autoreleasepool {
|
2018-05-10 20:33:50 +03:00
|
|
|
let nvimArgs = (NSArray<NSString *> *) arg;
|
2017-06-12 01:30:33 +03:00
|
|
|
|
|
|
|
argc = (int) nvimArgs.count + 1;
|
|
|
|
argv = (char **) malloc((argc + 1) * sizeof(char *));
|
|
|
|
|
|
|
|
argv[0] = "nvim";
|
2018-05-10 20:33:50 +03:00
|
|
|
for (var i = 0; i < nvimArgs.count; i++) {
|
2017-06-12 01:30:33 +03:00
|
|
|
argv[i + 1] = (char *) nvimArgs[(NSUInteger) i].cstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
[nvimArgs release]; // retained in start_neovim()
|
|
|
|
}
|
|
|
|
|
|
|
|
nvim_main(argc, argv);
|
|
|
|
|
|
|
|
free(argv);
|
2016-11-10 19:33:14 +03:00
|
|
|
}
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
static void set_ui_size(UIBridgeData *bridge, int width, int height) {
|
|
|
|
bridge->ui->width = width;
|
|
|
|
bridge->ui->height = height;
|
|
|
|
bridge->bridge.width = width;
|
|
|
|
bridge->bridge.height = height;
|
|
|
|
}
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
static void server_ui_scheduler(Event event, void *d) {
|
2016-07-09 15:04:09 +03:00
|
|
|
UI *ui = d;
|
|
|
|
ServerUiData *data = ui->data;
|
|
|
|
loop_schedule(data->loop, event);
|
|
|
|
}
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
static void server_ui_main(UIBridgeData *bridge, UI *ui) {
|
2018-05-11 11:41:08 +03:00
|
|
|
msgpack_sbuffer_init(&msg_sbuffer);
|
|
|
|
msgpack_sbuffer_init(&flush_sbuffer);
|
|
|
|
flush_packer = msgpack_packer_new(&flush_sbuffer, msgpack_sbuffer_write);
|
2018-01-20 20:18:12 +03:00
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
Loop loop;
|
|
|
|
loop_init(&loop, NULL);
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
_server_ui_data = xcalloc(1, sizeof(ServerUiData));
|
|
|
|
ui->data = _server_ui_data;
|
|
|
|
_server_ui_data->bridge = bridge;
|
|
|
|
_server_ui_data->loop = &loop;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2017-06-12 20:28:28 +03:00
|
|
|
set_ui_size(bridge, (int) _initialWidth, (int) _initialHeight);
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
_server_ui_data->stop = false;
|
2016-07-09 15:04:09 +03:00
|
|
|
CONTINUE(bridge);
|
|
|
|
|
|
|
|
uv_mutex_lock(&_mutex);
|
|
|
|
_is_ui_launched = true;
|
|
|
|
uv_cond_signal(&_condition);
|
|
|
|
uv_mutex_unlock(&_mutex);
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
while (!_server_ui_data->stop) {
|
2016-07-09 15:04:09 +03:00
|
|
|
loop_poll_events(&loop, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui_bridge_stopped(bridge);
|
2016-10-09 22:25:34 +03:00
|
|
|
loop_close(&loop, false);
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
xfree(_server_ui_data);
|
2016-07-09 15:04:09 +03:00
|
|
|
xfree(ui);
|
2018-01-20 20:18:12 +03:00
|
|
|
|
2018-05-11 11:41:08 +03:00
|
|
|
msgpack_sbuffer_clear(&flush_sbuffer);
|
2018-05-10 20:33:50 +03:00
|
|
|
msgpack_packer_free(flush_packer);
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 17:16:16 +03:00
|
|
|
#pragma mark NeoVim's UI callbacks
|
|
|
|
|
2018-01-20 20:18:12 +03:00
|
|
|
static void server_ui_flush(UI *ui __unused) {
|
|
|
|
@autoreleasepool {
|
2018-05-11 11:41:08 +03:00
|
|
|
if (flush_sbuffer.size == 0) {
|
2018-01-20 20:18:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-11 11:41:08 +03:00
|
|
|
let data = [[NSData alloc] initWithBytesNoCopy:flush_sbuffer.data length:flush_sbuffer.size freeWhenDone:false];
|
2018-05-10 20:33:50 +03:00
|
|
|
[_neovim_server sendMessageWithId:NvimServerMsgIdFlush data:data];
|
|
|
|
[data release];
|
|
|
|
|
2018-05-11 11:41:08 +03:00
|
|
|
msgpack_sbuffer_clear(&flush_sbuffer);
|
2018-05-10 20:33:50 +03:00
|
|
|
msgpack_packer_free(flush_packer);
|
2018-05-11 11:41:08 +03:00
|
|
|
flush_packer = msgpack_packer_new(&flush_sbuffer, msgpack_sbuffer_write);
|
2018-01-20 20:18:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-11 09:08:58 +03:00
|
|
|
static void server_ui_resize(UI *ui __unused, Integer width, Integer height) {
|
2017-12-08 13:02:56 +03:00
|
|
|
@autoreleasepool {
|
2018-01-20 20:18:12 +03:00
|
|
|
server_ui_flush(NULL);
|
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
send_msg_packing(NvimServerMsgIdResize, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_pack_array(packer, 2);
|
|
|
|
msgpack_pack_int64(packer, width);
|
|
|
|
msgpack_pack_int64(packer, height);
|
|
|
|
});
|
2017-12-08 13:02:56 +03:00
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
static void server_ui_clear(UI *ui __unused) {
|
2018-01-20 20:18:12 +03:00
|
|
|
@autoreleasepool {
|
|
|
|
server_ui_flush(NULL);
|
|
|
|
}
|
|
|
|
|
2018-01-23 22:40:20 +03:00
|
|
|
[_neovim_server sendMessageWithId:NvimServerMsgIdClear];
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
static void server_ui_eol_clear(UI *ui __unused) {
|
2018-05-10 20:33:50 +03:00
|
|
|
pack_flush_data(RenderDataTypeEolClear, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_pack_nil(packer);
|
|
|
|
});
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2017-05-11 09:08:58 +03:00
|
|
|
static void server_ui_cursor_goto(UI *ui __unused, Integer row, Integer col) {
|
2018-05-10 20:33:50 +03:00
|
|
|
_put_row = row;
|
|
|
|
_put_column = col;
|
2017-01-06 12:21:40 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
DLOG("%d:%d - %d:%d - %d:%d", row, col, curwin->w_cursor.lnum, curwin->w_cursor.col + 1);
|
2017-01-07 17:53:32 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
pack_flush_data(RenderDataTypeGoto, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_pack_array(packer, 4);
|
|
|
|
msgpack_pack_int64(packer, row);
|
|
|
|
msgpack_pack_int64(packer, col);
|
|
|
|
msgpack_pack_int64(packer, curwin->w_cursor.lnum);
|
|
|
|
msgpack_pack_int64(packer, curwin->w_cursor.col + 1);
|
|
|
|
});
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
static void server_ui_update_menu(UI *ui __unused) {
|
2018-01-23 22:40:20 +03:00
|
|
|
[_neovim_server sendMessageWithId:NvimServerMsgIdSetMenu];
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
static void server_ui_busy_start(UI *ui __unused) {
|
2018-01-23 22:40:20 +03:00
|
|
|
[_neovim_server sendMessageWithId:NvimServerMsgIdBusyStart];
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
static void server_ui_busy_stop(UI *ui __unused) {
|
2018-01-23 22:40:20 +03:00
|
|
|
[_neovim_server sendMessageWithId:NvimServerMsgIdBusyStop];
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
static void server_ui_mouse_on(UI *ui __unused) {
|
2018-01-23 22:40:20 +03:00
|
|
|
[_neovim_server sendMessageWithId:NvimServerMsgIdMouseOn];
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
static void server_ui_mouse_off(UI *ui __unused) {
|
2018-01-23 22:40:20 +03:00
|
|
|
[_neovim_server sendMessageWithId:NvimServerMsgIdMouseOff];
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
static void server_ui_mode_info_set(UI *ui __unused, Boolean enabled __unused, Array cursor_styles __unused) {
|
2017-04-07 12:26:31 +03:00
|
|
|
// yet noop
|
|
|
|
}
|
|
|
|
|
2017-05-11 09:08:58 +03:00
|
|
|
static void server_ui_mode_change(UI *ui __unused, String mode_str __unused, Integer mode) {
|
2017-12-08 13:02:56 +03:00
|
|
|
@autoreleasepool {
|
2018-05-10 20:33:50 +03:00
|
|
|
send_msg_packing(NvimServerMsgIdModeChange, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_pack_int64(packer, mode);
|
|
|
|
});
|
2017-12-08 13:02:56 +03:00
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
static void server_ui_set_scroll_region(UI *ui __unused, Integer top, Integer bot, Integer left, Integer right) {
|
2017-12-08 13:02:56 +03:00
|
|
|
@autoreleasepool {
|
2018-01-20 20:18:12 +03:00
|
|
|
server_ui_flush(NULL);
|
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
send_msg_packing(NvimServerMsgIdSetScrollRegion, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_pack_array(packer, 4);
|
|
|
|
msgpack_pack_int64(packer, top);
|
|
|
|
msgpack_pack_int64(packer, bot);
|
|
|
|
msgpack_pack_int64(packer, left);
|
|
|
|
msgpack_pack_int64(packer, right);
|
|
|
|
});
|
2017-12-08 13:02:56 +03:00
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2017-05-11 09:08:58 +03:00
|
|
|
static void server_ui_scroll(UI *ui __unused, Integer count) {
|
2017-12-08 13:02:56 +03:00
|
|
|
@autoreleasepool {
|
2018-01-21 18:02:24 +03:00
|
|
|
server_ui_flush(NULL);
|
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
send_msg_packing(NvimServerMsgIdScroll, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_pack_int64(packer, count);
|
|
|
|
});
|
2017-12-08 13:02:56 +03:00
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
static void server_ui_highlight_set(UI *ui __unused, HlAttrs attrs) {
|
2018-05-10 20:33:50 +03:00
|
|
|
var trait = FontTraitNone;
|
2017-01-06 12:21:40 +03:00
|
|
|
if (attrs.italic) {
|
|
|
|
trait |= FontTraitItalic;
|
|
|
|
}
|
|
|
|
if (attrs.bold) {
|
|
|
|
trait |= FontTraitBold;
|
|
|
|
}
|
|
|
|
if (attrs.underline) {
|
|
|
|
trait |= FontTraitUnderline;
|
|
|
|
}
|
|
|
|
if (attrs.undercurl) {
|
|
|
|
trait |= FontTraitUndercurl;
|
|
|
|
}
|
|
|
|
CellAttributes cellAttrs;
|
|
|
|
cellAttrs.fontTrait = trait;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
let fg = attrs.foreground == -1 ? _default_foreground : attrs.foreground;
|
|
|
|
let bg = attrs.background == -1 ? _default_background : attrs.background;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2017-01-06 12:21:40 +03:00
|
|
|
cellAttrs.foreground = attrs.reverse ? bg : fg;
|
|
|
|
cellAttrs.background = attrs.reverse ? fg : bg;
|
2018-05-10 20:33:50 +03:00
|
|
|
cellAttrs.special = attrs.special == -1 ? _default_special : pun_type(unsigned int, attrs.special);
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
pack_flush_data(RenderDataTypeHighlight, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_pack_bin(packer, sizeof(cellAttrs));
|
|
|
|
msgpack_pack_bin_body(packer, &cellAttrs, sizeof(cellAttrs));
|
|
|
|
});
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2017-05-11 09:08:58 +03:00
|
|
|
static void server_ui_put(UI *ui __unused, String str) {
|
2018-05-10 20:33:50 +03:00
|
|
|
if (_marked_text != nil
|
|
|
|
&& _marked_row == _put_row
|
|
|
|
&& _marked_column == _put_column) {
|
2017-12-08 13:02:56 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
DLOG("putting marked text: '%s'", str.data);
|
|
|
|
pack_flush_data(RenderDataTypePutMarked, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_rpc_from_string(str, packer);
|
|
|
|
});
|
2017-06-05 16:22:01 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
} else if (_marked_text != nil
|
|
|
|
&& str.size == 0
|
|
|
|
&& _marked_row == _put_row
|
|
|
|
&& _marked_column == _put_column - 1) {
|
2017-06-05 16:22:01 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
DLOG("putting marked text cuz zero");
|
|
|
|
pack_flush_data(RenderDataTypePutMarked, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_rpc_from_string(str, packer);
|
|
|
|
});
|
2017-06-05 16:22:01 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
} else {
|
2017-06-05 16:22:01 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
DLOG("putting non-marked text: '%s'", str.data);
|
|
|
|
pack_flush_data(RenderDataTypePut, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_rpc_from_string(str, packer);
|
|
|
|
});
|
2016-08-15 21:30:44 +03:00
|
|
|
|
2017-12-08 13:02:56 +03:00
|
|
|
}
|
2018-05-10 20:33:50 +03:00
|
|
|
|
|
|
|
_put_column += 1;
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
static void server_ui_bell(UI *ui __unused) {
|
2018-01-23 22:40:20 +03:00
|
|
|
[_neovim_server sendMessageWithId:NvimServerMsgIdBell];
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
static void server_ui_visual_bell(UI *ui __unused) {
|
2018-01-23 22:40:20 +03:00
|
|
|
[_neovim_server sendMessageWithId:NvimServerMsgIdVisualBell];
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2017-05-11 09:08:58 +03:00
|
|
|
static void server_ui_update_fg(UI *ui __unused, Integer fg) {
|
2017-12-08 13:02:56 +03:00
|
|
|
@autoreleasepool {
|
2018-05-10 20:33:50 +03:00
|
|
|
if (fg != -1) {
|
|
|
|
_default_foreground = fg;
|
2017-12-08 13:02:56 +03:00
|
|
|
}
|
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
send_msg_packing(NvimServerMsgIdSetForeground, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_pack_int64(packer, _default_foreground);
|
|
|
|
});
|
2017-01-06 12:21:40 +03:00
|
|
|
}
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2017-05-11 09:08:58 +03:00
|
|
|
static void server_ui_update_bg(UI *ui __unused, Integer bg) {
|
2017-12-08 13:02:56 +03:00
|
|
|
@autoreleasepool {
|
2018-05-10 20:33:50 +03:00
|
|
|
if (bg != -1) {
|
|
|
|
_default_background = bg;
|
2017-12-08 13:02:56 +03:00
|
|
|
}
|
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
send_msg_packing(NvimServerMsgIdSetBackground, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_pack_int64(packer, _default_background);
|
|
|
|
});
|
2017-01-06 12:21:40 +03:00
|
|
|
}
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2017-05-11 09:08:58 +03:00
|
|
|
static void server_ui_update_sp(UI *ui __unused, Integer sp) {
|
2017-12-08 13:02:56 +03:00
|
|
|
@autoreleasepool {
|
2018-05-10 20:33:50 +03:00
|
|
|
if (sp != -1) {
|
|
|
|
_default_special = sp;
|
2017-12-08 13:02:56 +03:00
|
|
|
}
|
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
|
|
|
|
send_msg_packing(NvimServerMsgIdSetSpecial, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_pack_int64(packer, _default_special);
|
|
|
|
});
|
2017-01-06 12:21:40 +03:00
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2017-05-11 09:08:58 +03:00
|
|
|
static void server_ui_set_title(UI *ui __unused, String title) {
|
2017-12-08 13:02:56 +03:00
|
|
|
@autoreleasepool {
|
|
|
|
if (title.size == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-19 00:11:33 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
send_msg_packing(NvimServerMsgIdSetTitle, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_rpc_from_string(title, packer);
|
|
|
|
});
|
2017-12-08 13:02:56 +03:00
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2017-05-11 09:08:58 +03:00
|
|
|
static void server_ui_set_icon(UI *ui __unused, String icon) {
|
2017-12-08 13:02:56 +03:00
|
|
|
@autoreleasepool {
|
|
|
|
if (icon.size == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-19 00:11:33 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
send_msg_packing(NvimServerMsgIdSetTitle, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_rpc_from_string(icon, packer);
|
|
|
|
});
|
2017-12-08 13:02:56 +03:00
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 12:46:00 +03:00
|
|
|
static void server_ui_stop(UI *ui __unused) {
|
2018-01-23 22:40:20 +03:00
|
|
|
[_neovim_server sendMessageWithId:NvimServerMsgIdStop];
|
2016-09-02 19:22:40 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
let data = (ServerUiData *) ui->data;
|
2017-01-06 12:21:40 +03:00
|
|
|
data->stop = true;
|
2016-07-10 12:46:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark Public
|
2017-01-06 12:16:55 +03:00
|
|
|
// called by neovim
|
2016-07-10 12:46:00 +03:00
|
|
|
|
|
|
|
void custom_ui_start(void) {
|
|
|
|
UI *ui = xcalloc(1, sizeof(UI));
|
|
|
|
|
|
|
|
ui->rgb = true;
|
|
|
|
ui->stop = server_ui_stop;
|
|
|
|
ui->resize = server_ui_resize;
|
|
|
|
ui->clear = server_ui_clear;
|
|
|
|
ui->eol_clear = server_ui_eol_clear;
|
|
|
|
ui->cursor_goto = server_ui_cursor_goto;
|
|
|
|
ui->update_menu = server_ui_update_menu;
|
|
|
|
ui->busy_start = server_ui_busy_start;
|
|
|
|
ui->busy_stop = server_ui_busy_stop;
|
|
|
|
ui->mouse_on = server_ui_mouse_on;
|
|
|
|
ui->mouse_off = server_ui_mouse_off;
|
2017-04-23 12:33:06 +03:00
|
|
|
ui->mode_info_set = server_ui_mode_info_set;
|
2016-07-10 12:46:00 +03:00
|
|
|
ui->mode_change = server_ui_mode_change;
|
|
|
|
ui->set_scroll_region = server_ui_set_scroll_region;
|
|
|
|
ui->scroll = server_ui_scroll;
|
|
|
|
ui->highlight_set = server_ui_highlight_set;
|
|
|
|
ui->put = server_ui_put;
|
|
|
|
ui->bell = server_ui_bell;
|
|
|
|
ui->visual_bell = server_ui_visual_bell;
|
|
|
|
ui->update_fg = server_ui_update_fg;
|
|
|
|
ui->update_bg = server_ui_update_bg;
|
|
|
|
ui->update_sp = server_ui_update_sp;
|
|
|
|
ui->flush = server_ui_flush;
|
2016-09-09 21:26:50 +03:00
|
|
|
ui->suspend = NULL;
|
2016-07-10 12:46:00 +03:00
|
|
|
ui->set_title = server_ui_set_title;
|
|
|
|
ui->set_icon = server_ui_set_icon;
|
|
|
|
|
|
|
|
ui_bridge_attach(ui, server_ui_main, server_ui_scheduler);
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:11:33 +03:00
|
|
|
void custom_ui_autocmds_groups(
|
2017-05-11 09:08:58 +03:00
|
|
|
event_T event,
|
|
|
|
char_u *fname __unused,
|
|
|
|
char_u *fname_io __unused,
|
|
|
|
int group __unused,
|
|
|
|
bool force __unused,
|
|
|
|
buf_T *buf,
|
|
|
|
exarg_T *eap __unused
|
2016-08-19 00:11:33 +03:00
|
|
|
) {
|
2017-06-06 23:39:41 +03:00
|
|
|
// We don't need these events in the UI (yet) and they slow down scrolling: Enable them,
|
|
|
|
// if necessary, only after optimizing the scrolling.
|
2016-12-20 01:57:42 +03:00
|
|
|
if (event == EVENT_CURSORMOVED || event == EVENT_CURSORMOVEDI) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-06 19:53:07 +03:00
|
|
|
@autoreleasepool {
|
|
|
|
DLOG("got event %d for file %s in group %d.", event, fname, group);
|
2016-11-22 00:14:45 +03:00
|
|
|
|
2017-06-09 02:15:45 +03:00
|
|
|
if (event == EVENT_DIRCHANGED) {
|
|
|
|
send_cwd();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-25 18:35:51 +03:00
|
|
|
if (event == EVENT_COLORSCHEME) {
|
|
|
|
send_colorscheme();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-06 10:39:42 +03:00
|
|
|
if (event == EVENT_TEXTCHANGED
|
2018-05-10 20:33:50 +03:00
|
|
|
|| event == EVENT_TEXTCHANGEDI
|
|
|
|
|| event == EVENT_BUFWRITEPOST
|
|
|
|
|| event == EVENT_BUFLEAVE) {
|
2017-05-06 10:39:42 +03:00
|
|
|
send_dirty_status();
|
|
|
|
}
|
2017-01-07 18:06:39 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
send_msg_packing(NvimServerMsgIdAutoCommandEvent, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_pack_array(packer, 2);
|
|
|
|
msgpack_pack_int64(packer, (NSInteger) event);
|
|
|
|
if (buf == NULL) {
|
|
|
|
msgpack_pack_int64(packer, -1);
|
|
|
|
} else {
|
|
|
|
msgpack_pack_int64(packer, (NSInteger) buf->handle);
|
|
|
|
}
|
|
|
|
});
|
2016-08-19 00:11:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 12:16:55 +03:00
|
|
|
#pragma mark Other help functions
|
2018-05-10 20:33:50 +03:00
|
|
|
|
2017-06-12 20:28:28 +03:00
|
|
|
void start_neovim(NSInteger width, NSInteger height, NSArray<NSString *> *args) {
|
2018-05-11 11:41:08 +03:00
|
|
|
// The caller has an @autoreleasepool.
|
2017-06-12 20:28:28 +03:00
|
|
|
_initialWidth = width;
|
|
|
|
_initialHeight = height;
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
// set $VIMRUNTIME to ${RESOURCE_PATH_OF_XPC_BUNDLE}/runtime
|
2018-05-10 20:33:50 +03:00
|
|
|
let bundlePath = [NSBundle bundleForClass:[NvimServer class]].bundlePath;
|
|
|
|
let resourcesPath = [bundlePath.stringByDeletingLastPathComponent stringByAppendingPathComponent:@"Resources"];
|
|
|
|
let runtimePath = [resourcesPath stringByAppendingPathComponent:@"runtime"];
|
2016-07-09 15:04:09 +03:00
|
|
|
setenv("VIMRUNTIME", runtimePath.fileSystemRepresentation, true);
|
|
|
|
|
2016-11-13 16:45:30 +03:00
|
|
|
// Set $LANG to en_US.UTF-8 such that the copied text to the system clipboard is not garbled.
|
|
|
|
setenv("LANG", "en_US.UTF-8", true);
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
uv_mutex_init(&_mutex);
|
|
|
|
uv_cond_init(&_condition);
|
|
|
|
|
2017-06-12 01:30:33 +03:00
|
|
|
uv_thread_create(&_nvim_thread, run_neovim, [args retain]); // released in run_neovim()
|
2016-08-26 16:31:30 +03:00
|
|
|
DLOG("NeoVim started");
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
// continue only after our UI main code for neovim has been fully initialized
|
|
|
|
uv_mutex_lock(&_mutex);
|
|
|
|
while (!_is_ui_launched) {
|
|
|
|
uv_cond_wait(&_condition, &_mutex);
|
|
|
|
}
|
|
|
|
uv_mutex_unlock(&_mutex);
|
|
|
|
|
|
|
|
uv_cond_destroy(&_condition);
|
|
|
|
uv_mutex_destroy(&_mutex);
|
|
|
|
|
|
|
|
_backspace = [[NSString alloc] initWithString:@"<BS>"];
|
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
send_msg_packing(NvimServerMsgIdNvimReady, ^(msgpack_packer *packer) {
|
|
|
|
if (msg_didany > 0) {
|
|
|
|
msgpack_pack_true(packer);
|
|
|
|
} else {
|
|
|
|
msgpack_pack_false(packer);
|
|
|
|
}
|
|
|
|
});
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2017-01-06 12:16:55 +03:00
|
|
|
#pragma mark Functions for neovim's main loop
|
2016-07-09 23:52:59 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
typedef void (^async_work_block)(NSData *);
|
2016-07-09 23:52:59 +03:00
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
static void work_async(void **argv, async_work_block block) {
|
2017-01-06 16:06:27 +03:00
|
|
|
@autoreleasepool {
|
|
|
|
NSData *data = argv[0];
|
2018-05-10 20:33:50 +03:00
|
|
|
block(data);
|
2017-01-06 16:06:27 +03:00
|
|
|
[data release]; // retained in local_server_callback
|
|
|
|
}
|
2016-07-09 23:52:59 +03:00
|
|
|
}
|
|
|
|
|
2017-05-30 23:13:52 +03:00
|
|
|
void neovim_scroll(void **argv) {
|
2018-05-10 20:33:50 +03:00
|
|
|
work_async(argv, ^(NSData *data) {
|
|
|
|
let values = (NSInteger *) data.bytes;
|
2017-06-24 15:12:11 +03:00
|
|
|
int horiz = (int) values[0];
|
|
|
|
int vert = (int) values[1];
|
|
|
|
int row = (int) values[2];
|
|
|
|
int column = (int) values[3];
|
2017-05-30 23:13:52 +03:00
|
|
|
|
|
|
|
if (horiz == 0 && vert == 0) {
|
2018-05-10 20:33:50 +03:00
|
|
|
return;
|
2017-05-30 23:13:52 +03:00
|
|
|
}
|
|
|
|
|
2017-06-24 12:33:37 +03:00
|
|
|
if (row < 0 || column < 0) {
|
|
|
|
row = 0;
|
|
|
|
column = 0;
|
|
|
|
}
|
|
|
|
|
2017-05-30 23:13:52 +03:00
|
|
|
// value > 0 => down or right
|
|
|
|
int horizDir;
|
|
|
|
int vertDir;
|
|
|
|
if (horiz != 0) {
|
2018-05-10 20:33:50 +03:00
|
|
|
horizDir = horiz > 0 ? MSCR_RIGHT : MSCR_LEFT;
|
2017-06-24 15:12:11 +03:00
|
|
|
custom_ui_scroll(horizDir, ABS(horiz), row, column);
|
2017-05-30 23:13:52 +03:00
|
|
|
}
|
|
|
|
if (vert != 0) {
|
2018-05-10 20:33:50 +03:00
|
|
|
vertDir = vert > 0 ? MSCR_DOWN : MSCR_UP;
|
2017-06-24 15:12:11 +03:00
|
|
|
custom_ui_scroll(vertDir, ABS(vert), row, column);
|
2017-05-30 23:13:52 +03:00
|
|
|
}
|
|
|
|
|
2017-06-10 11:16:32 +03:00
|
|
|
refresh_ui_screen(VALID);
|
2017-05-30 23:13:52 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-01 23:59:31 +03:00
|
|
|
void neovim_resize(void **argv) {
|
2018-05-10 20:33:50 +03:00
|
|
|
work_async(argv, ^(NSData *data) {
|
2018-01-21 16:16:07 +03:00
|
|
|
const NSInteger *values = data.bytes;
|
2018-05-10 20:33:50 +03:00
|
|
|
let width = values[0];
|
|
|
|
let height = values[1];
|
2017-12-01 23:59:31 +03:00
|
|
|
|
2018-01-21 16:16:07 +03:00
|
|
|
set_ui_size(_server_ui_data->bridge, (int) width, (int) height);
|
2017-12-01 23:59:31 +03:00
|
|
|
ui_refresh();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-06 12:06:47 +03:00
|
|
|
void neovim_vim_input(void **argv) {
|
2018-05-10 20:33:50 +03:00
|
|
|
work_async(argv, ^(NSData *data) {
|
|
|
|
let input = [[[NSString alloc] initWithData:data
|
|
|
|
encoding:NSUTF8StringEncoding] autorelease];
|
2017-01-06 12:06:47 +03:00
|
|
|
|
|
|
|
if (_marked_text == nil) {
|
|
|
|
nvim_input(vim_string_from(input));
|
2018-05-10 20:33:50 +03:00
|
|
|
return;
|
2016-11-10 21:28:09 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 23:39:41 +03:00
|
|
|
// Handle cases like ㅎ -> arrow key: The previously marked text is the same as the finalized
|
|
|
|
// text which should inserted. Neovim's drawing code is optimized such that it does not call
|
|
|
|
// put in this case again, thus, we have to manually unmark the cells in the main app.
|
2017-01-06 12:06:47 +03:00
|
|
|
if ([_marked_text isEqualToString:input]) {
|
|
|
|
DLOG("unmarking text: '%s'\t now at %d:%d", input.cstr, _put_row, _put_column);
|
|
|
|
const char *str = _marked_text.cstr;
|
|
|
|
size_t cellCount = mb_string2cells((const char_u *) str);
|
2016-11-10 22:42:42 +03:00
|
|
|
|
2017-01-06 12:06:47 +03:00
|
|
|
for (int i = 1; i <= cellCount; i++) {
|
|
|
|
DLOG("unmarking at %d:%d", _put_row, _put_column - i);
|
2018-05-10 20:33:50 +03:00
|
|
|
send_msg_packing(NvimServerMsgIdUnmark, ^(msgpack_packer *packer) {
|
|
|
|
msgpack_pack_array(packer, 2);
|
|
|
|
msgpack_pack_int64(packer, _put_row);
|
|
|
|
msgpack_pack_int64(packer, MAX(_put_column - i, 0));
|
|
|
|
});
|
2017-01-06 12:06:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete_marked_text();
|
|
|
|
nvim_input(vim_string_from(input));
|
|
|
|
});
|
2016-11-10 19:33:14 +03:00
|
|
|
}
|
|
|
|
|
2017-01-06 12:06:47 +03:00
|
|
|
void neovim_vim_input_marked_text(void **argv) {
|
2018-05-10 20:33:50 +03:00
|
|
|
work_async(argv, ^(NSData *data) {
|
|
|
|
let markedText = [[[NSString alloc] initWithData:data
|
|
|
|
encoding:NSUTF8StringEncoding] autorelease];
|
2017-01-06 12:06:47 +03:00
|
|
|
|
|
|
|
if (_marked_text == nil) {
|
|
|
|
_marked_row = _put_row;
|
|
|
|
_marked_column = _put_column + _marked_delta;
|
2017-06-06 23:39:41 +03:00
|
|
|
DLOG(
|
2018-05-10 20:33:50 +03:00
|
|
|
"marking position: %d:%d(%d + %d)", _put_row, _marked_column, _put_column, _marked_delta
|
2017-06-06 23:39:41 +03:00
|
|
|
);
|
2017-01-06 12:06:47 +03:00
|
|
|
_marked_delta = 0;
|
|
|
|
} else {
|
|
|
|
delete_marked_text();
|
|
|
|
}
|
2016-11-10 22:42:42 +03:00
|
|
|
|
2017-01-06 12:06:47 +03:00
|
|
|
DLOG("inserting marked text '%s' at %d:%d", markedText.cstr, _put_row, _put_column);
|
|
|
|
insert_marked_text(markedText);
|
2016-11-10 21:28:09 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-06 12:06:47 +03:00
|
|
|
void neovim_delete(void **argv) {
|
2018-05-10 20:33:50 +03:00
|
|
|
work_async(argv, ^(NSData *data) {
|
2017-01-06 12:27:08 +03:00
|
|
|
const NSInteger *values = data.bytes;
|
2017-01-06 12:06:47 +03:00
|
|
|
NSInteger count = values[0];
|
|
|
|
|
|
|
|
_marked_delta = 0;
|
|
|
|
|
2017-06-06 23:39:41 +03:00
|
|
|
// Very ugly: When we want to have the Hanja for 하, Cocoa first finalizes 하, then sets
|
|
|
|
// the Hanja as marked text. The main app will call this method when this happens,
|
|
|
|
// thus compute how many cell we have to go backward to correctly
|
|
|
|
// mark the will-be-soon-inserted Hanja... See also docs/notes-on-cocoa-text-input.md
|
2017-01-06 12:06:47 +03:00
|
|
|
int emptyCounter = 0;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
_marked_delta -= 1;
|
|
|
|
|
2017-06-06 23:39:41 +03:00
|
|
|
// TODO: -1 because we assume that the cursor is one cell ahead,
|
|
|
|
// probably not always correct...
|
2017-01-06 12:06:47 +03:00
|
|
|
schar_T character = ScreenLines[_put_row * screen_Rows + _put_column - i - emptyCounter - 1];
|
|
|
|
if (character == 0x00 || character == ' ') {
|
|
|
|
// FIXME: dunno yet, why we have to also match ' '...
|
|
|
|
_marked_delta -= 1;
|
|
|
|
emptyCounter += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DLOG("put cursor: %d:%d, count: %li, delta: %d", _put_row, _put_column, count, _marked_delta);
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
nvim_input(vim_string_from(_backspace));
|
|
|
|
}
|
|
|
|
});
|
2016-08-12 19:00:05 +03:00
|
|
|
}
|
2017-01-07 15:55:53 +03:00
|
|
|
|
2017-09-07 19:24:06 +03:00
|
|
|
void neovim_focus_gained(void **argv) {
|
2018-05-10 20:33:50 +03:00
|
|
|
work_async(argv, ^(NSData *data) {
|
2017-09-07 19:24:06 +03:00
|
|
|
const bool *values = data.bytes;
|
|
|
|
|
|
|
|
aucmd_schedule_focusgained(values[0]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-25 18:35:51 +03:00
|
|
|
void neovim_debug1(void **argv) {
|
2018-05-10 20:33:50 +03:00
|
|
|
work_async(argv, ^(NSData *data) {
|
2017-06-25 18:35:51 +03:00
|
|
|
NSLog(@"normal fg: %#08X", normal_fg);
|
|
|
|
NSLog(@"normal bg: %#08X", normal_bg);
|
|
|
|
NSLog(@"normal sp: %#08X", normal_sp);
|
|
|
|
|
|
|
|
for (int i = 0; i < HLF_COUNT; i++) {
|
|
|
|
NSLog(@"%s: %#08X", hlf_names[i], HlAttrsFromAttrCode(highlight_attr[i]).foreground);
|
|
|
|
}
|
|
|
|
});
|
2017-08-24 01:37:48 +03:00
|
|
|
}
|