vimrunner 0.0.4 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,55 +1,87 @@
1
- Using Vim's client/server functionality, this library exposes a way to spawn a
2
- Vim instance and control it programatically. Apart from being a fun party
3
- trick, this could be used to do integration testing on vimscript.
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
- [github issue tracker](https://github.com/AndrewRadev/Vimrunner/issues)
11
+ [GitHub issue tracker](https://github.com/AndrewRadev/Vimrunner/issues).
8
12
 
9
13
  ## Usage
10
14
 
11
- There are two objects that can be used to control Vim, a "server" and a
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
- A server can be started in two ways:
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
- - `Vimrunner::Server.start`: Starts a terminal instance. Since it's
18
- "invisible", it's nice for use in automated tests. If it's impossible to
19
- start a terminal instance due to missing requirements for the `vim` binary
20
- (see "Requirements" below), a GUI instance will be started.
21
- - `Vimrunner::Server.start(:gui => true)`: Starts a GUI instance of Vim. On
22
- Linux, it'll be a `gvim`, on Mac it defaults to `mvim`.
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
- Both methods return a `Server` instance. For a more comprehensive list of
25
- options you can start the server with, check out
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
- To be able to send commands to the server, you need a client that knows some
29
- details about it:
39
+ * `vim` if it supports headlessly creating servers;
40
+ * `mvim` if you are on Mac OS X;
41
+ * `gvim`.
30
42
 
31
- ``` ruby
32
- server = Vimrunner::Server.start
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
- client = Vimrunner::Client.new(server)
35
- # or,
36
- client = server.new_client
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
- There are also two convenience methods that start a server and return a
40
- connected client -- `Vimrunner.start_vim` and `Vimrunner.start_gui_vim`.
61
+ (You can also use the non-block form of `start` in both of the above
62
+ examples.)
41
63
 
42
- For a full list of methods you can invoke on the remote vim instance, check out
43
- the methods on the `Client` class in
44
- [the docs](http://rubydoc.info/gems/vimrunner/Vimrunner/Client).
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. The client/server functionality
50
- (regrettably) needs a running X server to function, even for a terminal vim.
51
- This means that if you're using it for automated tests on a remote server,
52
- you'll probably need to start it with xvfb.
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
@@ -5,6 +5,6 @@ $: << File.expand_path('../../lib', __FILE__)
5
5
  require 'irb'
6
6
  require 'vimrunner'
7
7
 
8
- $vim = Vimrunner.start_gui_vim
8
+ $vim = Vimrunner.start_gvim
9
9
 
10
10
  IRB.start
data/lib/vimrunner.rb CHANGED
@@ -1,16 +1,51 @@
1
- require 'vimrunner/client'
2
- require 'vimrunner/server'
1
+ require "vimrunner/server"
2
+ require "vimrunner/platform"
3
3
 
4
4
  module Vimrunner
5
- # Starts a new Server with a terminal vim instance and returns a client,
6
- # connected to it.
7
- def self.start_vim
8
- Client.new(Server.start)
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
- # Starts a new Server with a GUI vim instance and returns a client, connected
12
- # to it.
13
- def self.start_gui_vim
14
- Client.new(Server.start(:gui => true))
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
@@ -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 without
13
- # sourcing any plugins to ensure a clean state. This method can be used to
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
- # Optional.
15
+ # entry_script - The Vim script that's runtime'd to initialize the plugin
16
+ # (optional).
20
17
  #
21
- # Example:
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
- # Invokes one of the basic actions the Vim server supports, sending a key
31
- # sequence. The keys are sent as-is, so it'd probably be better to use the
32
- # wrapper methods, #normal, #insert and so on.
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
- invoke_vim '--remote-send', keys
41
+ server.remote_send(keys)
35
42
  end
36
43
 
37
- # Executes the given command in the Vim instance and returns its output,
38
- # stripping all surrounding whitespace.
39
- def command(vim_command)
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
- expression = "VimrunnerEvaluateCommandOutput('#{vim_command.to_s}')"
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
- invoke_vim('--remote-expr', expression).strip.tap do |output|
45
- raise InvalidCommandError if output =~ /^Vim:E\d+:/
46
- end
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
- # Starts a search in Vim for the given text. The result is that the cursor
50
- # is positioned on its first occurrence.
51
- def search(text)
52
- normal
53
- type "/#{text}<cr>"
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 considered to be
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 getting
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
- # Writes the file being edited to disk. Note that you probably want to set
82
- # the file's name first by using Runner#edit.
83
- def write
84
- command :write
85
- end
86
-
87
- # Switches Vim to insert mode and types in the given text.
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
- # Switches Vim to normal mode and types in the given keys.
93
- def normal(keys = '')
94
- type "<c-\\><c-n>#{keys}"
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
- @server.kill
115
+ server.kill
100
116
  end
101
117
 
102
118
  private
103
119
 
104
- def invoke_vim(*args)
105
- args = [@server.vim_path, '--servername', @server.name, *args]
106
- Shell.run *args
120
+ def escape(string)
121
+ string.to_s.gsub("'", "''")
107
122
  end
108
123
  end
109
124
  end
@@ -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
@@ -1,152 +1,122 @@
1
- require 'timeout'
2
- require 'rbconfig'
3
- require 'pty'
1
+ require "timeout"
2
+ require "pty"
4
3
 
5
- require 'vimrunner/errors'
6
- require 'vimrunner/shell'
7
- require 'vimrunner/client'
4
+ require "vimrunner/errors"
5
+ require "vimrunner/client"
8
6
 
9
7
  module Vimrunner
10
8
 
11
- # The Server is a wrapper around the Vim server process that is controlled by
12
- # clients. It will attempt to start the most appropriate Vim binary available
13
- # on the system, though there are some options that can control this
14
- # behaviour. See #initialize for more details.
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
- class << self
15
+ VIMRC = File.expand_path("../../../vim/vimrc", __FILE__)
17
16
 
18
- # A convenience method that initializes a new server and starts it.
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
- # Returns true if the given Vim binary is compiled with support for the
59
- # client/server functionality.
60
- def clientserver_enabled?(vim_path)
61
- vim_version = %x[#{vim_path} --version]
62
- vim_version.include? '+clientserver' and vim_version.include? '+xterm_clipboard'
63
- end
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
- attr_accessor :pid
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
- # :gui - Whether or not to start Vim with a GUI, either 'gvim' or 'mvim'
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
- # :vim_path - A path to a custom Vim binary. If this option is not set,
84
- # the server attempts to guess an appropriate one, given the
85
- # :gui option and the current OS.
31
+ # client = Vimrunner::Server.new("vim").start
32
+ # # => #<Vimrunner::Client>
86
33
  #
87
- # Note that simply initializing a Server doesn't start the binary. You need
88
- # to call the #start method to do that.
34
+ # Vimrunner::Server.new("vim").start do |client|
35
+ # client.edit("foo")
36
+ # end
89
37
  #
90
- # Examples:
91
- #
92
- # server = Server.new # Will start a 'vim' if possible
93
- # server = Server.new(:gui => true) # Will start a 'gvim' or 'mvim' depending on the OS
94
- # server = Server.new(:vim_path => '/opt/bin/vim') # Will start a server with the given vim instance
95
- #
96
- def initialize(options = {})
97
- @gui = options[:gui]
98
- @vim_path = options[:vim_path]
99
- @name = "VIMRUNNER#{rand.to_s}"
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
- if not @vim_path or not Server.clientserver_enabled? @vim_path
102
- @vim_path = Server.vim_path
103
- end
52
+ @result
53
+ else
54
+ @r, @w, @pid = spawn
55
+ wait_until_started
104
56
 
105
- if @gui or not Server.clientserver_enabled? @vim_path
106
- @gui = true
107
- @vim_path = Server.gui_vim_path
57
+ new_client
108
58
  end
109
59
  end
110
60
 
111
- # Starts a Vim server.
112
- def start
113
- command = "#{vim_path} -f -u #{Server.vimrc_path} --noplugin --servername #{name}"
114
-
115
- if gui?
116
- @pid = Kernel.spawn(command, [:in, :out, :err] => :close)
117
- else
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
- # Returns true if the server is a GUI version of Vim. This can be forced by
126
- # instantiating the server with :gui => true
127
- def gui?
128
- @gui
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
- # Kills the Vim instance in the background by sending it a TERM signal.
132
- def kill
133
- Shell.kill(@pid)
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
- # Retrieve a list of names of currently running Vim servers.
137
- def serverlist
138
- %x[#{vim_path} --serverlist].strip.split "\n"
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
- servers = serverlist
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
@@ -1,3 +1,3 @@
1
1
  module Vimrunner
2
- VERSION = '0.0.4'
2
+ VERSION = '0.1.0'
3
3
  end
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
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-04-22 00:00:00.000000000 Z
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: &20415700 !ruby/object:Gem::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: *20415700
24
+ version_requirements: *19072460
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rdoc
27
- requirement: &20415020 !ruby/object:Gem::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: *20415020
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: &20414500 !ruby/object:Gem::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: *20414500
47
- description: ! " Using vim's client/server functionality, this library exposes
48
- a way to\n spawn a vim instance and control it programatically. Apart from being
49
- a fun\n party trick, this could be used to do integration testing on vimscript.\n"
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: 507320625350919209
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 vim instance through ruby
104
+ summary: Lets you control a Vim instance through Ruby
94
105
  test_files: []
@@ -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