Class: Vimrunner::Server
- Inherits:
-
Object
- Object
- Vimrunner::Server
- Defined in:
- lib/vimrunner/server.rb
Overview
Public: A Server has the responsibility of starting a Vim process and communicating with it through the clientserver interface. The process can be started with "start" and stopped with "kill". A Client would be necessary as the actual interface, though it is possible to use a Server directly to invoke --remote commands on its Vim instance.
Constant Summary
- VIMRC =
File.("../../../vim/vimrc", __FILE__)
Instance Attribute Summary (collapse)
-
- (Object) executable
readonly
Returns the value of attribute executable.
-
- (Object) name
readonly
Returns the value of attribute name.
Instance Method Summary (collapse)
-
- (Server) initialize(executable)
constructor
Public: Initialize a Server.
-
- (Object) kill
Public: Kills the Vim instance in the background.
-
- (Object) new_client
Public: A convenience method that returns a new Client instance, connected to this server.
-
- (Object) remote_expr(expression)
Public: Evaluates an expression in the Vim server and returns the result.
-
- (Object) remote_send(keys)
Public: Sends the given keys A wrapper around --remote-expr.
-
- (Object) serverlist
Public: Retrieves a list of names of currently running Vim servers.
-
- (Object) start
Public: Start a Server.
Constructor Details
- (Server) initialize(executable)
Public: Initialize a Server
executable - a String representing a Vim executable.
22 23 24 25 |
# File 'lib/vimrunner/server.rb', line 22 def initialize(executable) @executable = executable @name = "VIMRUNNER#{rand}" end |
Instance Attribute Details
- (Object) executable (readonly)
Returns the value of attribute executable
17 18 19 |
# File 'lib/vimrunner/server.rb', line 17 def executable @executable end |
- (Object) name (readonly)
Returns the value of attribute name
17 18 19 |
# File 'lib/vimrunner/server.rb', line 17 def name @name end |
Instance Method Details
- (Object) kill
Public: Kills the Vim instance in the background.
Returns self.
65 66 67 68 69 70 71 |
# File 'lib/vimrunner/server.rb', line 65 def kill @r.close @w.close Process.kill(9, @pid) rescue Errno::ESRCH self end |
- (Object) new_client
Public: A convenience method that returns a new Client instance, connected to this server.
Returns a Client.
77 78 79 |
# File 'lib/vimrunner/server.rb', line 77 def new_client Client.new(self) end |
- (Object) remote_expr(expression)
Public: Evaluates an expression in the Vim server and returns the result. A wrapper around --remote-expr.
expression - a String with a Vim expression to evaluate.
Returns the String output of the expression.
94 95 96 |
# File 'lib/vimrunner/server.rb', line 94 def remote_expr(expression) execute([executable, "--servername", name, "--remote-expr", expression]) end |
- (Object) remote_send(keys)
Public: Sends the given keys A wrapper around --remote-expr.
keys - a String with a sequence of Vim-compatible keystrokes.
Returns nothing.
104 105 106 |
# File 'lib/vimrunner/server.rb', line 104 def remote_send(keys) execute([executable, "--servername", name, "--remote-send", keys]) end |
- (Object) serverlist
Public: Retrieves a list of names of currently running Vim servers.
Returns an Array of String server names currently running.
84 85 86 |
# File 'lib/vimrunner/server.rb', line 84 def serverlist execute([executable, "--serverlist"]).split("\n") end |
- (Object) start
Public: Start a Server. This spawns a background process.
Examples
client = Vimrunner::Server.new("vim").start
# => #<Vimrunner::Client>
Vimrunner::Server.new("vim").start do |client|
client.edit("foo")
end
Returns a new Client instance initialized with this Server. Yields a new Client instance initialized with this Server.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/vimrunner/server.rb', line 40 def start if block_given? spawn do |r, w, pid| begin wait_until_started @result = yield(new_client) ensure r.close w.close Process.kill(9, pid) rescue Errno::ESRCH end end @result else @r, @w, @pid = spawn wait_until_started new_client end end |