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". 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.expand_path("../../../vim/vimrc", __FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(executable) ⇒ Server

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

#executableObject (readonly)

Returns the value of attribute executable



17
18
19
# File 'lib/vimrunner/server.rb', line 17

def executable
  @executable
end

#nameObject (readonly)

Returns the value of attribute name



17
18
19
# File 'lib/vimrunner/server.rb', line 17

def name
  @name
end

Instance Method Details

#killObject

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

#new_clientObject

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

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



94
95
96
# File 'lib/vimrunner/server.rb', line 94

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

#serverlistObject

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

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



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