Class: Vimrunner::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/vimrunner/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ Client

Returns a new instance of Client.



8
9
10
# File 'lib/vimrunner/client.rb', line 8

def initialize(server)
  @server = server
end

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



6
7
8
# File 'lib/vimrunner/client.rb', line 6

def server
  @server
end

Instance Method Details

#add_plugin(dir, entry_script = nil) ⇒ Object

Public: Adds a plugin to Vim’s runtime. Initially, Vim is started without sourcing any plugins to ensure a clean state. This method can be used to populate the instance’s environment.

dir - The base directory of the plugin, the one that contains

its autoload, plugin, ftplugin, etc. directories.

entry_script - The Vim script that’s runtime’d to initialize the plugin

(optional).

Examples

vim.add_plugin 'rails', 'plugin/rails.vim'

Returns nothing.



26
27
28
29
30
31
32
# File 'lib/vimrunner/client.rb', line 26

def add_plugin(dir, entry_script = nil)
  append_runtimepath(dir)
  if entry_script
    entry_script_path = Path.new(entry_script)
    command("runtime #{entry_script_path}")
  end
end

#append_runtimepath(dir) ⇒ Object

Public: Appends a directory to Vim’s runtimepath

dir - The directory added to the path

Returns nothing.



53
54
55
56
# File 'lib/vimrunner/client.rb', line 53

def append_runtimepath(dir)
  dir_path = Path.new(dir)
  command("set runtimepath+=#{dir_path}")
end

#command(commands) ⇒ Object

Public: Executes the given command in the Vim instance and returns its output, stripping all surrounding whitespace.

Returns the String output. Raises InvalidCommandError if the command is not recognised by vim.



190
191
192
193
194
195
# File 'lib/vimrunner/client.rb', line 190

def command(commands)
  command = Command.new(commands)
  server.remote_expr("VimrunnerEvaluateCommandOutput('#{command}')").tap do |output|
    raise InvalidCommandError.new(output) if output =~ /^Vim(\(call\))?:E\d+:/
  end
end

#echo(*expressions) ⇒ Object

Public: Echo each expression with a space in between.

Returns the String output.



117
118
119
# File 'lib/vimrunner/client.rb', line 117

def echo(*expressions)
  command "echo #{expressions.join(' ')}"
end

#edit(filename) ⇒ Object

Public: Edits the file filename with Vim.

Note that this doesn’t use the ‘–remote’ Vim flag, it simply types in the command manually. This is necessary to avoid the Vim instance getting focus.

Returns the Client instance.



168
169
170
171
172
# File 'lib/vimrunner/client.rb', line 168

def edit(filename)
  file_path = Path.new(filename)
  command "edit #{file_path}"
  self
end

#edit!(filename) ⇒ Object

Public: Edits the file filename with Vim using edit!.

Similar to #edit, only discards any changes to the current buffer.

Returns the Client instance.



179
180
181
182
183
# File 'lib/vimrunner/client.rb', line 179

def edit!(filename)
  file_path = Path.new(filename)
  command "edit! #{file_path}"
  self
end

#feedkeys(string) ⇒ Object

Public: Send keys as if they come from a mapping or typed by a user.

Vim’s usual remote-send functionality to send keys to a server does not respect mappings. As a workaround, the feedkeys() function can be used to more closely simulate user input.

Any keys are sent in a double-quoted string so that special keys such as <CR> and <C-L> can be used. Note that, as per Vim documentation, such keys should be preceded by a backslash, e.g. ‘<CR>’ for a carriage return, ‘<CR>’ will send those four characters separately.

Examples

vim.command 'map <C-R> ihello'
vim.feedkeys '\<C-R>'

Returns nothing.



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

def feedkeys(string)
  string = string.gsub('"', '\"')
  server.remote_expr(%Q{feedkeys("#{string}")})
end

#foregroundObject

Bring the server to foreground



203
204
205
# File 'lib/vimrunner/client.rb', line 203

def foreground
  server.remote_expr("foreground()")
end

#insert(text) ⇒ Object

Public: Switches Vim to insert mode and types in the given text.

Returns the Client instance.



101
102
103
# File 'lib/vimrunner/client.rb', line 101

def insert(text)
  normal "i#{text}"
end

#killObject

Kills the server it’s connected to.



198
199
200
# File 'lib/vimrunner/client.rb', line 198

def kill
  server.kill
end

#normal(keys = "") ⇒ Object

Public: Switches Vim to normal mode and types in the given keys.

Returns the Client instance.



74
75
76
77
# File 'lib/vimrunner/client.rb', line 74

def normal(keys = "")
  server.remote_send("<C-\\><C-n>#{keys}")
  self
end

#prepend_runtimepath(dir) ⇒ Object

Public: Prepends a directory to Vim’s runtimepath. Use this instead of #append_runtimepath to give the directory higher priority when Vim runtime’s a file.

dir - The directory added to the path

Returns nothing.



65
66
67
68
69
# File 'lib/vimrunner/client.rb', line 65

def prepend_runtimepath(dir)
  dir_path = Path.new(dir)
  runtimepath = Path.new(echo('&runtimepath'))
  command("set runtimepath=#{dir_path},#{runtimepath}")
end

#search(text) ⇒ Object

Public: Starts a search in Vim for the given text. The result is that the cursor is positioned on its first occurrence.

Returns the Client instance.



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

def search(text)
  normal
  type "/#{text}<CR>"
end

#set(setting, value = nil) ⇒ Object

Public: Sets a setting in Vim. If value is nil, the setting is considered to be a boolean.

Examples

vim.set 'expandtab'  # invokes ":set expandtab"
vim.set 'tabstop', 3 # invokes ":set tabstop=3"

Returns the Client instance



152
153
154
155
156
157
158
159
# File 'lib/vimrunner/client.rb', line 152

def set(setting, value = nil)
  if value
    command "set #{setting}=#{value}"
  else
    command "set #{setting}"
  end
  self
end

#source(script) ⇒ Object

Public: source a script in Vim server

script - The Vim script to be sourced

Examples

vim.source '/path/to/plugin/rails.vim'

Returns nothing.



43
44
45
46
# File 'lib/vimrunner/client.rb', line 43

def source(script)
  script_path = Path.new(script)
  feedkeys(":\\<C-u>source #{script_path}\\<CR>")
end

#type(keys) ⇒ Object

Public: Invokes one of the basic actions the Vim server supports, sending a key sequence. The keys are sent as-is, so it’d probably be better to use the wrapper methods, #normal, #insert and so on.

Returns the Client instance.



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

def type(keys)
  server.remote_send(keys)
  self
end

#writeObject

Public: Writes the file being edited to disk. Note that you probably want to set the file’s name first by using Runner#edit.

Returns the Client instance.



109
110
111
112
# File 'lib/vimrunner/client.rb', line 109

def write
  command :write
  self
end