From 115e2600481c92c0bfb69d82ccbd8af7dc052a03 Mon Sep 17 00:00:00 2001 From: Giraut Date: Sun, 26 Sep 2021 09:23:16 +0300 Subject: [PATCH] Added 1 configuration variable and 2 new commands: (#45) g:ghost_enable_sync (default: 1) :GhostToggleSync :GhostSync By default, Ghost synchronizes the content of the Vim buffer with the browser's textarea (normal behavior). If ghost_enable_sync is set to 0, Ghost only updates the textarea when the :GhostSync command is issued. While editing, :GhostToggleSync may be used to disable / enable syncing. The purpose of this modification is to allow the user to control exactly when the textarea should receive an update. This was done specifically for Discourse forums, in which other forum users can see someone is in the process of replying. Discourse does not allow users to disable this. Using vim-ghost with syncing disabled, nobody on a Discourse forum is made aware that a reply is being typed until the very last syncing of the reply's textarea. Co-authored-by: Pierre Coupard --- doc/ghost.txt | 12 ++++++++++++ plugin/vim_compat.vim | 2 ++ pythonx/ghost_wrapper.py | 6 ++++++ rplugin/python3/ghost.py | 23 ++++++++++++++++++++++- 4 files changed, 42 insertions(+), 1 deletion(-) 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