Skip to content

Commit

Permalink
Vim compat (#20)
Browse files Browse the repository at this point in the history
Vim compatibility!
  • Loading branch information
raghur committed Sep 30, 2018
1 parent 4e06bc2 commit 204916e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
29 changes: 20 additions & 9 deletions README.adoc
@@ -1,6 +1,6 @@
= (N)Vim Ghost
= Vim Ghost

Edit browser textarea content in Neovim!
Edit browser textarea content in Vim/Neovim!

[.center.text-center]
image::https://i.imgur.com/yitnBrU.gif[demo,100%]
Expand All @@ -12,18 +12,32 @@ image::https://i.imgur.com/yitnBrU.gif[demo,100%]
" Vim-plug
Plug 'raghur/vim-ghost', {'do': ':GhostInstall'}
```
. Ensure you have a neovim instance open. Run `:GhostStart` to start the server.
. On any textara, click the ghost icon - switch to neovim and edit!. Content is sync'ed real time. Works both ways - edits in the browser window are pushed to neovim as well.
. Ensure you have a vim/neovim instance open. Run `:GhostStart` to start the server.
. On any textarea, click the ghost icon - switch to neovim and edit!. Content is sync'ed real time. Works both ways - edits in the browser window are pushed to neovim as well.
. When done, just delete the buffer with `:bd!` - you'll be disconnected

== Requirements

* Recent neovim
* Recent neovim/vim
* Python 3.4+
* Python plugin host - `python3 -c 'import neovim'` should not error
* Python websocket server https://github.com/dpallot/simple-websocket-server.git
** This is automatically installed as a `pip --user` dependency when `:GhostInstall` runs.

=== additional requirements for vim

* https://github.com/roxma/vim-hug-neovim-rpc[vim-hug-neovim-rpc]
* https://github.com/roxma/nvim-yarp[nvim-yarp]

[source,vimscript]
----
Plug 'roxma/nvim-yarp', {'cond': v:version == 800 && !has('nvim')}
Plug 'roxma/vim-hug-neovim-rpc', {'cond': v:version == 800 && !has('nvim')}
----

== Auto switching to vim:

._Optional, but highly recommended - Switching focus to Neovim_
* Linux: The `xdotool` command - if available, will be used to focus the nvim window.
Works in console, tmux or guis like neovim-qt
Expand All @@ -47,14 +61,11 @@ With Firefox moving to webextensions, *It's all text* is dead. Typing in text ar

My vimscript fu is pretty limited - so I thought I'd write a python plugin for neovim.

*PS: I know the repo name of vim-ghost is misleading.*

== TODOS

PR's welcome. Some areas:

- vim compatibility
- py2 support
- [.line-through]#vim compatibility# - DONE

Love it or just find it as useful as I do? Star this repo to let me know

Expand Down
12 changes: 12 additions & 0 deletions plugin/vim_compat.vim
@@ -0,0 +1,12 @@
if has('nvim')
finish
endif

let s:ghost = yarp#py3('ghost_wrapper')

func! GhostNotify(event, buffer)
return s:ghost.call('ghost_notify', a:event, a:buffer)
endfunc

com! -nargs=0 GhostStart call s:ghost.call('server_start')
com! -nargs=0 GhostStop call s:ghost.call('server_stop')
13 changes: 13 additions & 0 deletions pythonx/ghost_wrapper.py
@@ -0,0 +1,13 @@
from ghost import Ghost as _Ghost
import vim

_obj = _Ghost(vim)

def server_start(*args):
return _obj.server_start(args, '')

def server_stop(*args):
return _obj.server_stop(args, '')

def ghost_notify(*args):
return _obj.ghost_notify(args)
17 changes: 8 additions & 9 deletions rplugin/python3/ghost.py
Expand Up @@ -102,15 +102,15 @@ def server_start(self, args, range):
logger.info("server already running on port %d", self.port)
return

if self.nvim.funcs.exists("g:ghost_port") == 1:
self.port = self.nvim.api.get_var("ghost_port")
if "ghost_port" in self.nvim.vars:
self.port = self.nvim.vars["ghost_port"]
else:
self.nvim.api.set_var("ghost_port", self.port)
self.nvim.vars["ghost_port"] = self.port

if self.nvim.funcs.exists("g:ghost_cmd") == 1:
self.cmd = self.nvim.api.get_var("ghost_cmd")
if "ghost_cmd" in self.nvim.vars:
self.cmd = self.nvim.vars["ghost_cmd"]
else:
self.nvim.api.set_var("ghost_cmd", self.cmd)
self.nvim.vars["ghost_cmd"] = self.cmd

self.httpserver = MyHTTPServer(self, ('127.0.0.1', self.port),
WebRequestHandler)
Expand All @@ -130,10 +130,9 @@ def server_start(self, args, range):
self.winapp.process.real)
except ProcessNotFoundError as pne:
logger.warning("No process called nvim-qt found: %s", pne)
elif self.nvim.funcs.exists("g:ghost_nvim_window_id") == 1:
elif "ghost_nvim_window_id" in self.nvim.vars:
# for linux
self.linux_window_id = self.nvim.api.get_var(
"ghost_nvim_window_id").strip()
self.linux_window_id = self.nvim.vars["ghost_nvim_window_id"].strip()
elif sys.platform.startswith('darwin'):
if os.getenv('ITERM_PROFILE', None):
self.darwinapp = "iTerm2"
Expand Down

0 comments on commit 204916e

Please sign in to comment.