From 204916e7762f52193a7a3108020b380f36ddbbb0 Mon Sep 17 00:00:00 2001 From: Raghu Date: Sun, 30 Sep 2018 21:53:24 +0530 Subject: [PATCH] Vim compat (#20) Vim compatibility! --- README.adoc | 29 ++++++++++++++++++++--------- plugin/vim_compat.vim | 12 ++++++++++++ pythonx/ghost_wrapper.py | 13 +++++++++++++ rplugin/python3/ghost.py | 17 ++++++++--------- 4 files changed, 53 insertions(+), 18 deletions(-) create mode 100644 plugin/vim_compat.vim create mode 100644 pythonx/ghost_wrapper.py diff --git a/README.adoc b/README.adoc index 6f8feff..451f892 100644 --- a/README.adoc +++ b/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%] @@ -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 @@ -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 diff --git a/plugin/vim_compat.vim b/plugin/vim_compat.vim new file mode 100644 index 0000000..00f1471 --- /dev/null +++ b/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') diff --git a/pythonx/ghost_wrapper.py b/pythonx/ghost_wrapper.py new file mode 100644 index 0000000..b847b58 --- /dev/null +++ b/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) diff --git a/rplugin/python3/ghost.py b/rplugin/python3/ghost.py index c797921..9087425 100644 --- a/rplugin/python3/ghost.py +++ b/rplugin/python3/ghost.py @@ -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) @@ -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"