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”. If given the servername of an existing Vim instance, it can control that instance without the need to start a new process.
A Client would be necessary as an actual interface, though it is possible to use a Server directly to invoke –remote commands on its Vim instance.
Constant Summary collapse
- VIMRC =
File.("../../../vim/vimrc", __FILE__)
- VIMRUNNER_RC =
File.("../../../vim/vimrunner.vim", __FILE__)
Instance Attribute Summary collapse
-
#executable ⇒ Object
readonly
Returns the value of attribute executable.
-
#gvimrc ⇒ Object
readonly
Returns the value of attribute gvimrc.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#vimrc ⇒ Object
readonly
Returns the value of attribute vimrc.
Instance Method Summary collapse
-
#connect(options = {}) ⇒ Object
Public: Connects to the running server by name, blocking if need be.
-
#connect!(options = {}) ⇒ Object
Public: Connects to the running server by name, blocking if need be.
-
#initialize(options = {}) ⇒ Server
constructor
Public: Initialize a Server.
-
#kill ⇒ Object
Public: Kills the Vim instance in the background.
-
#new_client ⇒ Object
Public: A convenience method that returns a new Client instance, connected to this server.
-
#remote_expr(expression) ⇒ Object
Public: Evaluates an expression in the Vim server and returns the result.
-
#remote_send(keys) ⇒ Object
Public: Sends the given keys A wrapper around –remote-send.
-
#running? ⇒ Boolean
Public: Checks if there’s a running Vim instance with the server’s name.
-
#serverlist ⇒ Object
Public: Retrieves a list of names of currently running Vim servers.
-
#start ⇒ Object
Public: Start a Server.
Constructor Details
#initialize(options = {}) ⇒ Server
Public: Initialize a Server
options - The Hash options used to define a server (default: {}):
:executable - The String Vim executable to use (optional)
(default: Platform.vim).
:name - The String name of the Vim server (optional)
(default: "VIMRUNNER#{rand}").
:vimrc - The String vimrc file to source in the client (optional)
(default: Server::VIMRC).
:foreground - Boolean, whether to start Vim with the -f option (optional)
(default: true).
37 38 39 40 41 42 43 |
# File 'lib/vimrunner/server.rb', line 37 def initialize( = {}) @executable = .fetch(:executable) { Platform.vim } @name = .fetch(:name) { "VIMRUNNER#{rand}" }.upcase @vimrc = .fetch(:vimrc) { VIMRC } @gvimrc = .fetch(:gvimrc) { "NONE" } @foreground = .fetch(:foreground, true) end |
Instance Attribute Details
#executable ⇒ Object (readonly)
Returns the value of attribute executable.
23 24 25 |
# File 'lib/vimrunner/server.rb', line 23 def executable @executable end |
#gvimrc ⇒ Object (readonly)
Returns the value of attribute gvimrc.
23 24 25 |
# File 'lib/vimrunner/server.rb', line 23 def gvimrc @gvimrc end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
23 24 25 |
# File 'lib/vimrunner/server.rb', line 23 def name @name end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
23 24 25 |
# File 'lib/vimrunner/server.rb', line 23 def pid @pid end |
#vimrc ⇒ Object (readonly)
Returns the value of attribute vimrc.
23 24 25 |
# File 'lib/vimrunner/server.rb', line 23 def vimrc @vimrc end |
Instance Method Details
#connect(options = {}) ⇒ Object
Public: Connects to the running server by name, blocking if need be. Returns nil if no server was found in the given time.
options - An optional Hash. For now, only used for specifying a timeout
(default: {}):
:timeout - The Integer timeout to wait for a running server (optional)
(default: 5).
Returns a new Client instance initialized with this Server.
83 84 85 86 87 |
# File 'lib/vimrunner/server.rb', line 83 def connect( = {}) connect!() rescue TimeoutError nil end |
#connect!(options = {}) ⇒ Object
Public: Connects to the running server by name, blocking if need be. Raises a TimeoutError if no server was found in the given time in seconds.
options - An optional Hash. For now, only used for specifying a timeout
(default: {}):
:timeout - The Integer timeout to wait for a running server
(default: 5).
Returns a new Client instance initialized with this Server.
100 101 102 103 104 105 106 |
# File 'lib/vimrunner/server.rb', line 100 def connect!( = {}) wait_until_running([:timeout] || 5) client = new_client client.source(VIMRUNNER_RC) client end |
#kill ⇒ Object
Public: Kills the Vim instance in the background.
Returns self.
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/vimrunner/server.rb', line 118 def kill @r.close @w.close begin Process.kill(9, @pid) rescue Errno::ESRCH end self end |
#new_client ⇒ Object
Public: A convenience method that returns a new Client instance, connected to this server.
Returns a Client.
134 135 136 |
# File 'lib/vimrunner/server.rb', line 134 def new_client Client.new(self) end |
#remote_expr(expression) ⇒ Object
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.
151 152 153 |
# File 'lib/vimrunner/server.rb', line 151 def remote_expr(expression) execute([executable, "--servername", name, "--remote-expr", expression]) end |
#remote_send(keys) ⇒ Object
Public: Sends the given keys A wrapper around –remote-send.
keys - a String with a sequence of Vim-compatible keystrokes.
Returns nothing.
161 162 163 |
# File 'lib/vimrunner/server.rb', line 161 def remote_send(keys) execute([executable, "--servername", name, "--remote-send", keys]) end |
#running? ⇒ Boolean
Public: Checks if there’s a running Vim instance with the server’s name.
Returns a Boolean
111 112 113 |
# File 'lib/vimrunner/server.rb', line 111 def running? serverlist.include?(name) end |
#serverlist ⇒ Object
Public: Retrieves a list of names of currently running Vim servers.
Returns an Array of String server names currently running.
141 142 143 |
# File 'lib/vimrunner/server.rb', line 141 def serverlist execute([executable, "--serverlist"]).split("\n") end |
#start ⇒ Object
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.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/vimrunner/server.rb', line 58 def start @r, @w, @pid = spawn if block_given? begin @result = yield(connect!) ensure kill end @result else connect! end end |