diff --git a/doc/ghost.txt b/doc/ghost.txt index 6c344e8..08fa425 100644 --- a/doc/ghost.txt +++ b/doc/ghost.txt @@ -150,6 +150,18 @@ Syntax Highlighting autocmd BufNewFile,BufRead *stackoverflow.com* set filetype=markdown augroup END +-------------------------------------------------------------------------------- + *g:ghost_enable_sync* +Enable real-time syncing of the browser's textarea. + + By default, anything you type in vim appears immediately in the browser's textarea. + If disabled, manual syncing of the textarea is done with the :GhostSync command. + Syncing may be disabled / enabled on the fly using the :GhostToggleSync command. +> + let g:ghost_enable_sync = 1 +< +Default: 1 + ================================================================================ Contributing *GhostContributing* diff --git a/plugin/vim_compat.vim b/plugin/vim_compat.vim index 00f1471..bc9a5ae 100644 --- a/plugin/vim_compat.vim +++ b/plugin/vim_compat.vim @@ -10,3 +10,5 @@ endfunc com! -nargs=0 GhostStart call s:ghost.call('server_start') com! -nargs=0 GhostStop call s:ghost.call('server_stop') +com! -nargs=0 GhostToggleSync call s:ghost.call('ghost_toggle_sync') +com! -nargs=0 GhostSync call s:ghost.call('ghost_sync') diff --git a/pythonx/ghost_wrapper.py b/pythonx/ghost_wrapper.py index b847b58..ee62df6 100644 --- a/pythonx/ghost_wrapper.py +++ b/pythonx/ghost_wrapper.py @@ -9,5 +9,11 @@ def server_start(*args): def server_stop(*args): return _obj.server_stop(args, '') +def ghost_toggle_sync(*args): + return _obj.ghost_toggle_sync(args, '') + +def ghost_sync(*args): + return _obj.ghost_sync(args, '') + def ghost_notify(*args): return _obj.ghost_notify(args) diff --git a/rplugin/python3/ghost.py b/rplugin/python3/ghost.py index 1b70cbb..89aa056 100644 --- a/rplugin/python3/ghost.py +++ b/rplugin/python3/ghost.py @@ -121,6 +121,7 @@ def __init__(self, vim): self.winapp = None self.darwin_app = None self.linux_window_id = None + self.syncghost = True self.cmd = 'ed' def echo(self, message, *args): @@ -151,6 +152,9 @@ def start_http_server(): else: self.nvim.vars["ghost_port"] = self.port + if "ghost_enable_sync" in self.nvim.vars: + self.syncghost = self.nvim.vars["ghost_enable_sync"] != 0 + if "ghost_cmd" in self.nvim.vars: self.cmd = self.nvim.vars["ghost_cmd"] else: @@ -192,6 +196,23 @@ def server_stop(self, args, range): self.echo("Ghost server stopped") self.server_started = False + @neovim.command('GhostToggleSync', range='', nargs='0', sync=True) + def ghost_toggle_sync(self, args, range): + self.syncghost = not self.syncghost + self.echo("Ghost sync: {}".format("ON" if self.syncghost else "OFF")) + if self.syncghost: + self.ghost_sync(args, range) + + @neovim.command('GhostSync', range='', nargs='0', sync=True) + def ghost_sync(self, args, range): + bufnr = self.nvim.current.buffer.number + wsclient, req = buffer_handler_map[bufnr] + logger.info("sending message to client ") + text = "\n".join(self.nvim.buffers[bufnr][:]) + req["text"] = text + # self.nvim.command("echo '%s'" % text) + wsclient.sendMessage(json.dumps(req)) + @neovim.function("GhostNotify") def ghost_notify(self, args): logger.info(args) @@ -200,7 +221,7 @@ def ghost_notify(self, args): return wsclient, req = buffer_handler_map[bufnr] logger.debug('event recd: %s, buffer: %d', event, bufnr) - if event == "text_changed": + if event == "text_changed" and self.syncghost: logger.info("sending message to client ") text = "\n".join(self.nvim.buffers[bufnr][:]) req["text"] = text