Class: Vimrunner::Server

Inherits:
Object
  • Object
show all
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.expand_path("../../../vim/vimrc", __FILE__)
VIMRUNNER_RC =
File.expand_path("../../../vim/vimrunner_rc", __FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

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).


36
37
38
39
40
41
42
# File 'lib/vimrunner/server.rb', line 36

def initialize(options = {})
  @executable = options.fetch(:executable) { Platform.vim }
  @name       = options.fetch(:name) { "VIMRUNNER#{rand}" }.upcase
  @vimrc      = options.fetch(:vimrc) { VIMRC }
  @gvimrc     = options.fetch(:gvimrc) { "NONE" }
  @foreground = options.fetch(:foreground, true)
end

Instance Attribute Details

#executableObject (readonly)

Returns the value of attribute executable.



22
23
24
# File 'lib/vimrunner/server.rb', line 22

def executable
  @executable
end

#gvimrcObject (readonly)

Returns the value of attribute gvimrc.



22
23
24
# File 'lib/vimrunner/server.rb', line 22

def gvimrc
  @gvimrc
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/vimrunner/server.rb', line 22

def name
  @name
end

#vimrcObject (readonly)

Returns the value of attribute vimrc.



22
23
24
# File 'lib/vimrunner/server.rb', line 22

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.



84
85
86
87
88
# File 'lib/vimrunner/server.rb', line 84

def connect(options = {})
  connect!(options)
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.



101
102
103
104
105
106
107
# File 'lib/vimrunner/server.rb', line 101

def connect!(options = {})
  wait_until_running(options[:timeout] || 5)

  client = new_client
  client.source(VIMRUNNER_RC)
  client
end

#killObject

Public: Kills the Vim instance in the background.

Returns self.



119
120
121
122
123
124
125
# File 'lib/vimrunner/server.rb', line 119

def kill
  @r.close
  @w.close
  Process.kill(9, @pid) rescue Errno::ESRCH

  self
end

#new_clientObject

Public: A convenience method that returns a new Client instance, connected to this server.

Returns a Client.



131
132
133
# File 'lib/vimrunner/server.rb', line 131

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.



148
149
150
# File 'lib/vimrunner/server.rb', line 148

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.



158
159
160
# File 'lib/vimrunner/server.rb', line 158

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

Returns:

  • (Boolean)


112
113
114
# File 'lib/vimrunner/server.rb', line 112

def running?
  serverlist.include?(name)
end

#serverlistObject

Public: Retrieves a list of names of currently running Vim servers.

Returns an Array of String server names currently running.



138
139
140
# File 'lib/vimrunner/server.rb', line 138

def serverlist
  execute([executable, "--serverlist"]).split("\n")
end

#startObject

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.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vimrunner/server.rb', line 57

def start
  @r, @w, @pid = spawn

  if block_given?
    begin
      @result = yield(connect!)
    ensure
      @r.close
      @w.close
      Process.kill(9, @pid) rescue Errno::ESRCH
    end
    @result
  else
    connect!
  end
end