Skip to content

Commit

Permalink
Added 1 configuration variable and 2 new commands: (#45)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Giraut and Pierre Coupard authored Sep 26, 2021
1 parent ca45c7d commit 115e260
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
12 changes: 12 additions & 0 deletions doc/ghost.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand Down
2 changes: 2 additions & 0 deletions plugin/vim_compat.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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')
6 changes: 6 additions & 0 deletions pythonx/ghost_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
23 changes: 22 additions & 1 deletion rplugin/python3/ghost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 115e260

Please sign in to comment.