vimrunner 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +65 -33
- data/bin/vimrunner +1 -1
- data/lib/vimrunner.rb +45 -10
- data/lib/vimrunner/client.rb +65 -50
- data/lib/vimrunner/errors.rb +6 -0
- data/lib/vimrunner/platform.rb +77 -0
- data/lib/vimrunner/server.rb +87 -117
- data/lib/vimrunner/version.rb +1 -1
- metadata +25 -14
- data/lib/vimrunner/shell.rb +0 -24
data/README.md
CHANGED
@@ -1,55 +1,87 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
# Vimrunner [![Build Status](https://secure.travis-ci.org/AndrewRadev/vimrunner.png?branch=master)](http://travis-ci.org/AndrewRadev/vimrunner)
|
2
|
+
|
3
|
+
Using Vim's
|
4
|
+
[client/server](http://vimdoc.sourceforge.net/htmldoc/remote.html#clientserver)
|
5
|
+
functionality, this library exposes a way to spawn a Vim instance and control
|
6
|
+
it programatically. Apart from being a fun party trick, this can be used to do
|
7
|
+
integration testing on Vimscript.
|
4
8
|
|
5
9
|
This is still fairly experimental, so use with caution. Any issue reports or
|
6
10
|
contributions are very welcome on the
|
7
|
-
[
|
11
|
+
[GitHub issue tracker](https://github.com/AndrewRadev/Vimrunner/issues).
|
8
12
|
|
9
13
|
## Usage
|
10
14
|
|
11
|
-
|
12
|
-
"client". The server takes care of spawning a server vim instance that will be
|
13
|
-
controlled, and the client is its interface.
|
15
|
+
Vimrunner can be used in one of two main ways:
|
14
16
|
|
15
|
-
|
17
|
+
```ruby
|
18
|
+
# Vim will automatically be started and killed.
|
19
|
+
Vimrunner.start do |vim|
|
20
|
+
vim.edit "file.txt"
|
21
|
+
vim.insert "Hello"
|
22
|
+
vim.write
|
23
|
+
end
|
24
|
+
```
|
16
25
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
26
|
+
```ruby
|
27
|
+
# Vim will automatically be started but you must manually kill it when you are
|
28
|
+
# finished.
|
29
|
+
vim = Vimrunner.start
|
30
|
+
vim.edit "file.txt"
|
31
|
+
vim.insert "Hello"
|
32
|
+
vim.write
|
33
|
+
vim.kill
|
34
|
+
```
|
23
35
|
|
24
|
-
|
25
|
-
|
26
|
-
[the docs](http://rubydoc.info/gems/vimrunner/Vimrunner/Server).
|
36
|
+
Vimrunner will attempt to start up the most suitable version of Vim available,
|
37
|
+
meaning:
|
27
38
|
|
28
|
-
|
29
|
-
|
39
|
+
* `vim` if it supports headlessly creating servers;
|
40
|
+
* `mvim` if you are on Mac OS X;
|
41
|
+
* `gvim`.
|
30
42
|
|
31
|
-
|
32
|
-
|
43
|
+
If you wish to always start a GUI Vim (viz. skip using a headless `vim`) then
|
44
|
+
you can use `start_gvim` like so:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
Vimrunner.start_gvim do |vim|
|
48
|
+
# ...
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
If you require an even more specific version of Vim, you can pass the path to
|
53
|
+
it by instantiating your own `Server` instance like so:
|
33
54
|
|
34
|
-
|
35
|
-
|
36
|
-
|
55
|
+
```ruby
|
56
|
+
Vimrunner::Server.new("/path/to/my/specific/vim").start do |vim|
|
57
|
+
vim.edit "file.txt"
|
58
|
+
end
|
37
59
|
```
|
38
60
|
|
39
|
-
|
40
|
-
|
61
|
+
(You can also use the non-block form of `start` in both of the above
|
62
|
+
examples.)
|
41
63
|
|
42
|
-
|
43
|
-
|
44
|
-
|
64
|
+
Calling `start` (or `start_gvim`) will return a `Client` instance with which
|
65
|
+
you can control Vim. For a full list of methods you can invoke on the remote
|
66
|
+
vim instance, check out the [`Client`
|
67
|
+
documentation](http://rubydoc.info/gems/vimrunner/Vimrunner/Client).
|
45
68
|
|
46
69
|
## Requirements
|
47
70
|
|
48
71
|
Vim needs to be compiled with `+clientserver`. This should be available with
|
49
|
-
the `normal`, `big` and `huge` featuresets
|
50
|
-
(
|
51
|
-
|
52
|
-
|
72
|
+
the `normal`, `big` and `huge` featuresets or by using
|
73
|
+
[MacVim](http://code.google.com/p/macvim/) on Mac OS X. In order to start a
|
74
|
+
server without a GUI, you will also need `+xterm-clipboard` [as described in
|
75
|
+
the Vim
|
76
|
+
manual](http://vimdoc.sourceforge.net/htmldoc/remote.html#x11-clientserver).
|
77
|
+
|
78
|
+
The client/server functionality (regrettably) needs a running X server to
|
79
|
+
function, even without a GUI. This means that if you're using it for
|
80
|
+
automated tests on a remote server, you'll probably need to start it with
|
81
|
+
xvfb.
|
82
|
+
|
83
|
+
If you are using MacVim, note that you will need the `mvim` binary in your
|
84
|
+
`PATH` in order to start and communicate with Vim servers.
|
53
85
|
|
54
86
|
## Experimenting
|
55
87
|
|
data/bin/vimrunner
CHANGED
data/lib/vimrunner.rb
CHANGED
@@ -1,16 +1,51 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "vimrunner/server"
|
2
|
+
require "vimrunner/platform"
|
3
3
|
|
4
4
|
module Vimrunner
|
5
|
-
|
6
|
-
#
|
7
|
-
|
8
|
-
|
5
|
+
|
6
|
+
# Public: Start a Vim process and return a Client through which it can be
|
7
|
+
# controlled.
|
8
|
+
#
|
9
|
+
# vim - The String path to the Vim you wish to use (default: the most
|
10
|
+
# appropriate Vim for your system).
|
11
|
+
# blk - An optional block which will be passed a Client with which you can
|
12
|
+
# communicate with the Vim process. Upon exiting the block, the Vim
|
13
|
+
# process will be terminated.
|
14
|
+
#
|
15
|
+
# Examples
|
16
|
+
#
|
17
|
+
# client = Vimrunner.start
|
18
|
+
# # => #<Vimrunner::Client>
|
19
|
+
#
|
20
|
+
# Vimrunner.start do |client|
|
21
|
+
# client.command("version")
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# Returns a Client for the started Server.
|
25
|
+
def self.start(vim = Platform.vim, &blk)
|
26
|
+
Server.new(vim).start(&blk)
|
9
27
|
end
|
10
28
|
|
11
|
-
#
|
12
|
-
#
|
13
|
-
|
14
|
-
|
29
|
+
# Public: Start a Vim process with a GUI and return a Client through which
|
30
|
+
# it can be controlled.
|
31
|
+
#
|
32
|
+
# vim - The String path to the Vim you wish to use (default: the most
|
33
|
+
# appropriate Vim for your system).
|
34
|
+
# blk - An optional block which will be passed a Client with which you can
|
35
|
+
# communicate with the Vim process. Upon exiting the block, the Vim
|
36
|
+
# process will be terminated.
|
37
|
+
#
|
38
|
+
# Examples
|
39
|
+
#
|
40
|
+
# client = Vimrunner.start
|
41
|
+
# # => #<Vimrunner::Client>
|
42
|
+
#
|
43
|
+
# Vimrunner.start do |client|
|
44
|
+
# client.command("version")
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# Returns a Client for the started Server.
|
48
|
+
def self.start_gvim(&blk)
|
49
|
+
Server.new(Platform.gvim).start(&blk)
|
15
50
|
end
|
16
51
|
end
|
data/lib/vimrunner/client.rb
CHANGED
@@ -1,62 +1,79 @@
|
|
1
|
-
require 'vimrunner/shell'
|
2
|
-
|
3
1
|
module Vimrunner
|
4
|
-
|
5
|
-
# A Client is simply a proxy to a Vim server. It's initialized with a Server
|
6
|
-
# instance and sends commands, keys and signals to it.
|
7
2
|
class Client
|
3
|
+
attr_reader :server
|
4
|
+
|
8
5
|
def initialize(server)
|
9
6
|
@server = server
|
10
7
|
end
|
11
8
|
|
12
|
-
# Adds a plugin to Vim's runtime. Initially, Vim is started
|
13
|
-
# sourcing any plugins to ensure a clean state. This method can be
|
14
|
-
# populate the instance's environment.
|
9
|
+
# Public: Adds a plugin to Vim's runtime. Initially, Vim is started
|
10
|
+
# without sourcing any plugins to ensure a clean state. This method can be
|
11
|
+
# used to populate the instance's environment.
|
15
12
|
#
|
16
13
|
# dir - The base directory of the plugin, the one that contains
|
17
14
|
# its autoload, plugin, ftplugin, etc. directories.
|
18
|
-
# entry_script - The Vim script that's runtime'd to initialize the plugin
|
19
|
-
#
|
15
|
+
# entry_script - The Vim script that's runtime'd to initialize the plugin
|
16
|
+
# (optional).
|
20
17
|
#
|
21
|
-
#
|
18
|
+
# Examples
|
22
19
|
#
|
23
20
|
# vim.add_plugin 'rails', 'plugin/rails.vim'
|
24
21
|
#
|
22
|
+
# Returns nothing.
|
25
23
|
def add_plugin(dir, entry_script = nil)
|
26
24
|
command("set runtimepath+=#{dir}")
|
27
25
|
command("runtime #{entry_script}") if entry_script
|
28
26
|
end
|
29
27
|
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
28
|
+
# Public: Switches Vim to normal mode and types in the given keys.
|
29
|
+
#
|
30
|
+
# Returns nothing.
|
31
|
+
def normal(keys = "")
|
32
|
+
server.remote_send("<C-\\><C-n>#{keys}")
|
33
|
+
end
|
34
|
+
|
35
|
+
# Public: Invokes one of the basic actions the Vim server supports,
|
36
|
+
# sending a key sequence. The keys are sent as-is, so it'd probably be
|
37
|
+
# better to use the wrapper methods, #normal, #insert and so on.
|
38
|
+
#
|
39
|
+
# Returns nothing.
|
33
40
|
def type(keys)
|
34
|
-
|
41
|
+
server.remote_send(keys)
|
35
42
|
end
|
36
43
|
|
37
|
-
#
|
38
|
-
#
|
39
|
-
|
44
|
+
# Public: Starts a search in Vim for the given text. The result is that
|
45
|
+
# the cursor is positioned on its first occurrence.
|
46
|
+
#
|
47
|
+
# Returns nothing.
|
48
|
+
def search(text)
|
40
49
|
normal
|
50
|
+
type "/#{text}<CR>"
|
51
|
+
end
|
41
52
|
|
42
|
-
|
53
|
+
# Public: Switches Vim to insert mode and types in the given text.
|
54
|
+
#
|
55
|
+
# Returns nothing.
|
56
|
+
def insert(text)
|
57
|
+
normal "i#{text}"
|
58
|
+
end
|
43
59
|
|
44
|
-
|
45
|
-
|
46
|
-
|
60
|
+
# Public: Writes the file being edited to disk. Note that you probably
|
61
|
+
# want to set the file's name first by using Runner#edit.
|
62
|
+
def write
|
63
|
+
command :write
|
47
64
|
end
|
48
65
|
|
49
|
-
#
|
50
|
-
#
|
51
|
-
|
52
|
-
|
53
|
-
|
66
|
+
# Public: Echo each expression with a space in between.
|
67
|
+
#
|
68
|
+
# Returns the String output.
|
69
|
+
def echo(*expressions)
|
70
|
+
command "echo #{expressions.join(' ')}"
|
54
71
|
end
|
55
72
|
|
56
|
-
# Sets a setting in Vim. If +value+ is nil, the setting is
|
57
|
-
# a boolean.
|
73
|
+
# Public: Sets a setting in Vim. If +value+ is nil, the setting is
|
74
|
+
# considered to be a boolean.
|
58
75
|
#
|
59
|
-
# Examples
|
76
|
+
# Examples
|
60
77
|
#
|
61
78
|
# vim.set 'expandtab' # invokes ":set expandtab"
|
62
79
|
# vim.set 'tabstop', 3 # invokes ":set tabstop=3"
|
@@ -69,41 +86,39 @@ module Vimrunner
|
|
69
86
|
end
|
70
87
|
end
|
71
88
|
|
72
|
-
# Edits the file +filename+ with Vim.
|
89
|
+
# Public: Edits the file +filename+ with Vim.
|
73
90
|
#
|
74
91
|
# Note that this doesn't use the '--remote' Vim flag, it simply types in
|
75
|
-
# the command manually. This is necessary to avoid the Vim instance
|
76
|
-
# focus.
|
92
|
+
# the command manually. This is necessary to avoid the Vim instance
|
93
|
+
# getting focus.
|
94
|
+
#
|
95
|
+
# Returns nothing.
|
77
96
|
def edit(filename)
|
78
97
|
command "edit #{filename}"
|
79
98
|
end
|
80
99
|
|
81
|
-
#
|
82
|
-
#
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
def insert(text = '')
|
89
|
-
normal "i#{text}"
|
90
|
-
end
|
100
|
+
# Public: Executes the given command in the Vim instance and returns its
|
101
|
+
# output, stripping all surrounding whitespace.
|
102
|
+
#
|
103
|
+
# Returns the string output.
|
104
|
+
# Raises InvalidCommandError if the command is not recognised by vim.
|
105
|
+
def command(commands)
|
106
|
+
expression = "VimrunnerEvaluateCommandOutput('#{escape(commands)}')"
|
91
107
|
|
92
|
-
|
93
|
-
|
94
|
-
|
108
|
+
server.remote_expr(expression).tap do |output|
|
109
|
+
raise InvalidCommandError if output =~ /^Vim:E\d+:/
|
110
|
+
end
|
95
111
|
end
|
96
112
|
|
97
113
|
# Kills the server it's connected to.
|
98
114
|
def kill
|
99
|
-
|
115
|
+
server.kill
|
100
116
|
end
|
101
117
|
|
102
118
|
private
|
103
119
|
|
104
|
-
def
|
105
|
-
|
106
|
-
Shell.run *args
|
120
|
+
def escape(string)
|
121
|
+
string.to_s.gsub("'", "''")
|
107
122
|
end
|
108
123
|
end
|
109
124
|
end
|
data/lib/vimrunner/errors.rb
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
module Vimrunner
|
2
2
|
class InvalidCommandError < RuntimeError; end
|
3
3
|
|
4
|
+
class NoSuitableVimError < RuntimeError
|
5
|
+
def message
|
6
|
+
"No suitable Vim executable could be found for this system."
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
4
10
|
class TimeoutError < RuntimeError
|
5
11
|
def message
|
6
12
|
"Timed out while waiting for serverlist. Is an X11 server running?"
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require "rbconfig"
|
2
|
+
|
3
|
+
require "vimrunner/errors"
|
4
|
+
|
5
|
+
module Vimrunner
|
6
|
+
|
7
|
+
# Public: The Platform module contains logic for finding a Vim executable
|
8
|
+
# that supports the clientserver functionality on the current system. Its
|
9
|
+
# methods can be used to fetch a Vim path for initializing a Server.
|
10
|
+
#
|
11
|
+
# Examples
|
12
|
+
#
|
13
|
+
# Vimrunner::Platform.vim
|
14
|
+
# # => "gvim"
|
15
|
+
module Platform
|
16
|
+
extend self
|
17
|
+
|
18
|
+
# Public: Looks for a Vim executable that's suitable for the current
|
19
|
+
# platform. Also attempts to find an appropriate GUI vim if terminal ones
|
20
|
+
# are unsuitable.
|
21
|
+
#
|
22
|
+
# Returns the String Vim executable.
|
23
|
+
# Raises NoSuitableVimError if no suitable Vim can be found.
|
24
|
+
def vim
|
25
|
+
vims.find { |vim| suitable?(vim) } or raise NoSuitableVimError
|
26
|
+
end
|
27
|
+
|
28
|
+
# Public: Looks for a GUI Vim executable that's suitable for the current
|
29
|
+
# platform.
|
30
|
+
#
|
31
|
+
# Returns the String Vim executable.
|
32
|
+
# Raises NoSuitableVimError if no suitable Vim can be found.
|
33
|
+
def gvim
|
34
|
+
gvims.find { |gvim| suitable?(gvim) } or raise NoSuitableVimError
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def gvims
|
40
|
+
if mac?
|
41
|
+
%w( mvim gvim )
|
42
|
+
else
|
43
|
+
%w( gvim )
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def vims
|
48
|
+
%w( vim ) + gvims
|
49
|
+
end
|
50
|
+
|
51
|
+
def suitable?(vim)
|
52
|
+
features = features(vim)
|
53
|
+
|
54
|
+
if gui?(vim)
|
55
|
+
features.include?("+clientserver")
|
56
|
+
else
|
57
|
+
features.include?("+clientserver") && features.include?("+xterm_clipboard")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def gui?(vim)
|
62
|
+
executable = File.basename(vim)
|
63
|
+
|
64
|
+
executable[0, 1] == "g" || executable[0, 1] == "m"
|
65
|
+
end
|
66
|
+
|
67
|
+
def features(vim)
|
68
|
+
IO.popen([vim, "--version"]) { |io| io.read.strip }
|
69
|
+
rescue Errno::ENOENT
|
70
|
+
""
|
71
|
+
end
|
72
|
+
|
73
|
+
def mac?
|
74
|
+
RbConfig::CONFIG["host_os"] =~ /darwin/
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/lib/vimrunner/server.rb
CHANGED
@@ -1,152 +1,122 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require 'pty'
|
1
|
+
require "timeout"
|
2
|
+
require "pty"
|
4
3
|
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require 'vimrunner/client'
|
4
|
+
require "vimrunner/errors"
|
5
|
+
require "vimrunner/client"
|
8
6
|
|
9
7
|
module Vimrunner
|
10
8
|
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
9
|
+
# Public: A Server has the responsibility of starting a Vim process and
|
10
|
+
# communicating with it through the clientserver interface. The process can
|
11
|
+
# be started with "start" and stopped with "kill". A Client would be
|
12
|
+
# necessary as the actual interface, though it is possible to use a Server
|
13
|
+
# directly to invoke --remote commands on its Vim instance.
|
15
14
|
class Server
|
16
|
-
|
15
|
+
VIMRC = File.expand_path("../../../vim/vimrc", __FILE__)
|
17
16
|
|
18
|
-
|
19
|
-
def start(options = {})
|
20
|
-
server = new(options)
|
21
|
-
server.start
|
22
|
-
end
|
23
|
-
|
24
|
-
# A convenience method that returns a new Client instance, connected to
|
25
|
-
# the server.
|
26
|
-
def new_client
|
27
|
-
Client.new(self)
|
28
|
-
end
|
29
|
-
|
30
|
-
# The default path to use when starting a server with a terminal Vim. If
|
31
|
-
# the "vim" executable is not compiled with clientserver capabilities,
|
32
|
-
# the GUI version is started instead.
|
33
|
-
def vim_path
|
34
|
-
'vim'
|
35
|
-
end
|
36
|
-
|
37
|
-
# The default path to use when starting a server with the GUI version of
|
38
|
-
# Vim. Defaults to "mvim" on a mac and "gvim" on linux.
|
39
|
-
def gui_vim_path
|
40
|
-
if mac?
|
41
|
-
'mvim'
|
42
|
-
else
|
43
|
-
'gvim'
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
# The path to a vimrc file containing some required vimscript. The server
|
48
|
-
# is started with no settings or a vimrc, apart from this one.
|
49
|
-
def vimrc_path
|
50
|
-
File.join(File.expand_path('../../..', __FILE__), 'vim', 'vimrc')
|
51
|
-
end
|
52
|
-
|
53
|
-
# Returns true if the current operating system is Mac OS X.
|
54
|
-
def mac?
|
55
|
-
host_os =~ /darwin/
|
56
|
-
end
|
17
|
+
attr_reader :name, :executable
|
57
18
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
private
|
66
|
-
|
67
|
-
def host_os
|
68
|
-
RbConfig::CONFIG['host_os']
|
69
|
-
end
|
19
|
+
# Public: Initialize a Server
|
20
|
+
#
|
21
|
+
# executable - a String representing a Vim executable.
|
22
|
+
def initialize(executable)
|
23
|
+
@executable = executable
|
24
|
+
@name = "VIMRUNNER#{rand}"
|
70
25
|
end
|
71
26
|
|
72
|
-
|
73
|
-
attr_reader :name, :vim_path
|
74
|
-
|
75
|
-
# A Server is initialized with two options that control its behaviour:
|
27
|
+
# Public: Start a Server. This spawns a background process.
|
76
28
|
#
|
77
|
-
#
|
78
|
-
# depending on the OS. The default is false, which means that
|
79
|
-
# the server will start itself as a terminal instance. Note
|
80
|
-
# that, if the terminal Vim doesn't have client/server
|
81
|
-
# support, a GUI version will be started anyway.
|
29
|
+
# Examples
|
82
30
|
#
|
83
|
-
#
|
84
|
-
#
|
85
|
-
# :gui option and the current OS.
|
31
|
+
# client = Vimrunner::Server.new("vim").start
|
32
|
+
# # => #<Vimrunner::Client>
|
86
33
|
#
|
87
|
-
#
|
88
|
-
#
|
34
|
+
# Vimrunner::Server.new("vim").start do |client|
|
35
|
+
# client.edit("foo")
|
36
|
+
# end
|
89
37
|
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
38
|
+
# Returns a new Client instance initialized with this Server.
|
39
|
+
# Yields a new Client instance initialized with this Server.
|
40
|
+
def start
|
41
|
+
if block_given?
|
42
|
+
spawn do |r, w, pid|
|
43
|
+
begin
|
44
|
+
wait_until_started
|
45
|
+
@result = yield(new_client)
|
46
|
+
ensure
|
47
|
+
r.close
|
48
|
+
w.close
|
49
|
+
end
|
50
|
+
end
|
100
51
|
|
101
|
-
|
102
|
-
|
103
|
-
|
52
|
+
@result
|
53
|
+
else
|
54
|
+
@r, @w, @pid = spawn
|
55
|
+
wait_until_started
|
104
56
|
|
105
|
-
|
106
|
-
@gui = true
|
107
|
-
@vim_path = Server.gui_vim_path
|
57
|
+
new_client
|
108
58
|
end
|
109
59
|
end
|
110
60
|
|
111
|
-
#
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
_out, _in, @pid = PTY.spawn(command)
|
119
|
-
end
|
61
|
+
# Public: Kills the Vim instance in the background.
|
62
|
+
#
|
63
|
+
# Returns self.
|
64
|
+
def kill
|
65
|
+
@r.close
|
66
|
+
@w.close
|
67
|
+
Process.detach(@pid)
|
120
68
|
|
121
|
-
wait_until_started
|
122
69
|
self
|
123
70
|
end
|
124
71
|
|
125
|
-
#
|
126
|
-
#
|
127
|
-
|
128
|
-
|
72
|
+
# Public: A convenience method that returns a new Client instance,
|
73
|
+
# connected to this server.
|
74
|
+
#
|
75
|
+
# Returns a Client.
|
76
|
+
def new_client
|
77
|
+
Client.new(self)
|
129
78
|
end
|
130
79
|
|
131
|
-
#
|
132
|
-
|
133
|
-
|
80
|
+
# Public: Retrieves a list of names of currently running Vim servers.
|
81
|
+
#
|
82
|
+
# Returns an Array of String server names currently running.
|
83
|
+
def serverlist
|
84
|
+
execute([executable, "--serverlist"]).split("\n")
|
134
85
|
end
|
135
86
|
|
136
|
-
#
|
137
|
-
|
138
|
-
|
87
|
+
# Public: Evaluates an expression in the Vim server and returns the result.
|
88
|
+
# A wrapper around --remote-expr.
|
89
|
+
#
|
90
|
+
# expression - a String with a Vim expression to evaluate.
|
91
|
+
#
|
92
|
+
# Returns the String output of the expression.
|
93
|
+
def remote_expr(expression)
|
94
|
+
execute([executable, "--servername", name, "--remote-expr", expression])
|
95
|
+
end
|
96
|
+
|
97
|
+
# Public: Sends the given keys
|
98
|
+
# A wrapper around --remote-expr.
|
99
|
+
#
|
100
|
+
# keys - a String with a sequence of Vim-compatible keystrokes.
|
101
|
+
#
|
102
|
+
# Returns nothing.
|
103
|
+
def remote_send(keys)
|
104
|
+
execute([executable, "--servername", name, "--remote-send", keys])
|
139
105
|
end
|
140
106
|
|
141
107
|
private
|
142
108
|
|
109
|
+
def execute(command)
|
110
|
+
IO.popen(command) { |io| io.read.strip }
|
111
|
+
end
|
112
|
+
|
113
|
+
def spawn(&blk)
|
114
|
+
PTY.spawn(executable, "-f", "--servername", name, "-u", VIMRC, &blk)
|
115
|
+
end
|
116
|
+
|
143
117
|
def wait_until_started
|
144
118
|
Timeout.timeout(5, TimeoutError) do
|
145
|
-
|
146
|
-
while servers.empty? or not servers.include? name
|
147
|
-
sleep 0.1
|
148
|
-
servers = serverlist
|
149
|
-
end
|
119
|
+
sleep 0.1 while !serverlist.include?(name)
|
150
120
|
end
|
151
121
|
end
|
152
122
|
end
|
data/lib/vimrunner/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vimrunner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-05-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &19072460 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *19072460
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rdoc
|
27
|
-
requirement: &
|
27
|
+
requirement: &19071860 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,21 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *19071860
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: simplecov
|
38
|
+
requirement: &19071280 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *19071280
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: rspec
|
38
|
-
requirement: &
|
49
|
+
requirement: &19069980 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ! '>='
|
@@ -43,10 +54,10 @@ dependencies:
|
|
43
54
|
version: 2.0.0
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
47
|
-
description: ! " Using
|
48
|
-
a way to\n spawn a
|
49
|
-
a fun\n party trick, this
|
57
|
+
version_requirements: *19069980
|
58
|
+
description: ! " Using Vim's client/server functionality, this library exposes
|
59
|
+
a way to\n spawn a Vim instance and control it programatically. Apart from being
|
60
|
+
a fun\n party trick, this can be used to integration test Vim script.\n"
|
50
61
|
email:
|
51
62
|
- andrey.radev@gmail.com
|
52
63
|
executables:
|
@@ -56,8 +67,8 @@ extra_rdoc_files: []
|
|
56
67
|
files:
|
57
68
|
- lib/vimrunner.rb
|
58
69
|
- lib/vimrunner/errors.rb
|
59
|
-
- lib/vimrunner/shell.rb
|
60
70
|
- lib/vimrunner/server.rb
|
71
|
+
- lib/vimrunner/platform.rb
|
61
72
|
- lib/vimrunner/version.rb
|
62
73
|
- lib/vimrunner/client.rb
|
63
74
|
- vim/vimrc
|
@@ -78,7 +89,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
78
89
|
version: '0'
|
79
90
|
segments:
|
80
91
|
- 0
|
81
|
-
hash:
|
92
|
+
hash: 3979564230760095821
|
82
93
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
94
|
none: false
|
84
95
|
requirements:
|
@@ -90,5 +101,5 @@ rubyforge_project: vimrunner
|
|
90
101
|
rubygems_version: 1.8.15
|
91
102
|
signing_key:
|
92
103
|
specification_version: 3
|
93
|
-
summary: Lets you control a
|
104
|
+
summary: Lets you control a Vim instance through Ruby
|
94
105
|
test_files: []
|
data/lib/vimrunner/shell.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
module Vimrunner
|
2
|
-
|
3
|
-
# The Shell module contains functions that interact directly with the shell.
|
4
|
-
# They're general utilities that help with some of the specific use cases
|
5
|
-
# that pop up with the vim runner.
|
6
|
-
module Shell
|
7
|
-
extend self
|
8
|
-
|
9
|
-
# Executes a shell command, waits until it's finished, and returns the
|
10
|
-
# output
|
11
|
-
def run(*command)
|
12
|
-
IO.popen(command) { |io| io.read.strip }
|
13
|
-
end
|
14
|
-
|
15
|
-
# Sends a TERM signal to the given PID. Returns true if the process was
|
16
|
-
# found and killed, false otherwise.
|
17
|
-
def kill(pid)
|
18
|
-
Process.kill('TERM', pid)
|
19
|
-
true
|
20
|
-
rescue Errno::ESRCH
|
21
|
-
false
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|