Skip to content

atkenny15/vim-ghosttext

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GhostText for Vim

Vim plugin for communicating with the GhostText browser plugin.

Usage

To install the plugin it is recommended to use a Vim plugin manager such as vim-plug, Vundle or Pathogen. Add the following line to your .vimrc:

Plug 'atkenny15/vim-ghosttext'

The following commands are provided:

  • GhostStart: Start the GhostText HTTP server
  • GhostStop: Stop the GhostText HTTP server (this is called on QuitPre if GhostStart has been called)

A detailed log can be found at: <temp>/ghosttext-vim-log.txt. Under Unix like systems this is typically /tmp/ghosttext-vim-log.txt. Please provide this log if you run into any issues.

To use the plugin:

  1. Launch vim/gvim
  2. Start the HTTP server with :GhostStart
  3. Change to the Vim buffer you want to work with
    • All text in this buffer will be replaced
    • Each connection from GhostText will communicate with the current buffer at the time the connection is made
  4. Navigate to a web page supported by GhostText and click the ghost button to connect to the editor
  5. Once the connection is made begin typing in either the browser or Vim and both should update in real-time with changes from the other application
  6. When done editing click the GhostText button in the browser to close the connection
    • Due to a bug in GhostText, do not click the button again until reloading the page
  7. At this point Vim can be left open for additional connections or closed.

Here is a demo made using Peek:

Demo screencast

Design

GhostText Interface

Launching GhostText from the web browser triggers a GET request to port 4001 (by default, configurable in the extension). It expects a response containing the JSON structure:

{
    "ProtocolVersion": 1,
    "WebSocketPort": <port>,
}

GhostText then acts as a WebSocket client on port <port> exchanging the following JSON packets:

{
    'text': <text>,
    'selections': [{'start': <start>, 'end': <end>}],
    'title': '',
    'url': '',
    'syntax': '',
}

<text> is the Unicode text data to be displayed. This plugin sets <start> and <end> to len(<text>), title to ghosttext-vim, and does not change the other values. The other values seem to be specific to Sublime Text. Some details are in the code here:

changed_text = view.substr(sublime.Region(0, view.size()))
selections = OnSelectionModifiedListener._get_selections(view)
response = json.dumps({
    'title': view.name(),
    'text':  changed_text,
    'syntax': view.scope_name(0),
    'selections': selections
})

GhostText for Vim Design

TODO

Requirements

  • Vim compiled with +python support
  • Python2

Rationale

The highly useful it's All Text! browser plugin is unsupported on Firefox Quantum. A couple other projects provide similar features, but for following reasons did not quite work for my use.

(N)Vim Ghost uses Neo-Vim specific features, and patching for Vim support is pretty much equivalent to a complete re-write. Additionally, this requires the SimpleWebSocket module installed, whereas this plugin has no non-core dependencies.

Ghost Text Vim worked initially, but seems to have issues recently. Rather than try to modify a TCL program, it seemed easier and more interesting to just re-write the plugin in Python.

Known Issues

  • GhostText currently has problems if it is stopped and restarted, these are being addressed in a complete re-write of the browser plugin. To work around this, re-load the web page before running GhostText a second time. The two issues encountered so far are:
    • Multiple WebSocket connections are opened, but data is only sent on a single one
      • This plugin will cleanup any unused WebSockets that receive no post-handshake data within 3s, but the issue below prevents any further action
    • After the handshake and receiving valid data GhostText sends a close frame and no longer responds to any data sent to it
      • Sending data after receiving a valid frame does not do anything to mitigate this
      • There is no workaround available for this, a browser update is needed

TODO

  • Lock updates to a single buffer
  • Fix logging
  • Test with multiple connections (from different browser tabs)
  • Test python3 support
  • Fix bug when sending large amounts of data from browser
  • Vim Documentation
  • Improve comments
  • Allow HTTP port to be modified
  • Better WebSocket port selection
  • Write tests
  • Add better support for WebSocket
    • Support all valid frame opcodes
    • Client side support
      • Support mask generation in a Frame
    • Test payload lengths > 16-bit
    • Better validation of handshake header

References

About

GhostText Plugin for Vim

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published