Class: Vimrunner::Client
- Inherits:
-
Object
- Object
- Vimrunner::Client
- Defined in:
- lib/vimrunner/client.rb
Instance Attribute Summary (collapse)
-
- (Object) server
readonly
Returns the value of attribute server.
Instance Method Summary (collapse)
-
- (Object) add_plugin(dir, entry_script = nil)
Public: Adds a plugin to Vim's runtime.
-
- (Object) append_runtimepath(dir)
Public: Appends a directory to Vim's runtimepath.
-
- (Object) command(commands)
Public: Executes the given command in the Vim instance and returns its output, stripping all surrounding whitespace.
-
- (Object) echo(*expressions)
Public: Echo each expression with a space in between.
-
- (Object) edit(filename)
Public: Edits the file filename with Vim.
-
- (Object) edit!(filename)
Public: Edits the file filename with Vim using edit!.
-
- (Client) initialize(server)
constructor
A new instance of Client.
-
- (Object) insert(text)
Public: Switches Vim to insert mode and types in the given text.
-
- (Object) kill
Kills the server it's connected to.
-
- (Object) normal(keys = "")
Public: Switches Vim to normal mode and types in the given keys.
-
- (Object) prepend_runtimepath(dir)
Public: Prepends a directory to Vim's runtimepath.
-
- (Object) search(text)
Public: Starts a search in Vim for the given text.
-
- (Object) set(setting, value = nil)
Public: Sets a setting in Vim.
-
- (Object) type(keys)
Public: Invokes one of the basic actions the Vim server supports, sending a key sequence.
-
- (Object) write
Public: Writes the file being edited to disk.
Constructor Details
- (Client) initialize(server)
A new instance of Client
5 6 7 |
# File 'lib/vimrunner/client.rb', line 5 def initialize(server) @server = server end |
Instance Attribute Details
- (Object) server (readonly)
Returns the value of attribute server
3 4 5 |
# File 'lib/vimrunner/client.rb', line 3 def server @server end |
Instance Method Details
- (Object) add_plugin(dir, entry_script = nil)
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.
23 24 25 26 |
# File 'lib/vimrunner/client.rb', line 23 def add_plugin(dir, entry_script = nil) append_runtimepath(dir) command("runtime #{entry_script}") if entry_script end |
- (Object) append_runtimepath(dir)
Public: Appends a directory to Vim's runtimepath
dir - The directory added to the path
Returns nothing.
33 34 35 |
# File 'lib/vimrunner/client.rb', line 33 def append_runtimepath(dir) command("set runtimepath+=#{dir}") end |
- (Object) command(commands)
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.
144 145 146 147 148 149 150 |
# File 'lib/vimrunner/client.rb', line 144 def command(commands) expression = "VimrunnerEvaluateCommandOutput('#{escape(commands)}')" server.remote_expr(expression).tap do |output| raise InvalidCommandError.new(output) if output =~ /^Vim:E\d+:/ end end |
- (Object) echo(*expressions)
Public: Echo each expression with a space in between.
Returns the String output.
95 96 97 |
# File 'lib/vimrunner/client.rb', line 95 def echo(*expressions) command "echo #{expressions.join(' ')}" end |
- (Object) edit(filename)
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.
124 125 126 127 |
# File 'lib/vimrunner/client.rb', line 124 def edit(filename) command "edit #{filename}" self end |
- (Object) edit!(filename)
Public: Edits the file filename with Vim using edit!.
Similar to #edit, only discards any changes to the current buffer.
Returns the Client instance.
134 135 136 137 |
# File 'lib/vimrunner/client.rb', line 134 def edit!(filename) command "edit! #{filename}" self end |
- (Object) insert(text)
Public: Switches Vim to insert mode and types in the given text.
Returns the Client instance.
79 80 81 |
# File 'lib/vimrunner/client.rb', line 79 def insert(text) normal "i#{text}" end |
- (Object) kill
Kills the server it's connected to.
153 154 155 |
# File 'lib/vimrunner/client.rb', line 153 def kill server.kill end |
- (Object) normal(keys = "")
Public: Switches Vim to normal mode and types in the given keys.
Returns the Client instance.
52 53 54 55 |
# File 'lib/vimrunner/client.rb', line 52 def normal(keys = "") server.remote_send("<C-\\><C-n>#{keys}") self end |
- (Object) prepend_runtimepath(dir)
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.
44 45 46 47 |
# File 'lib/vimrunner/client.rb', line 44 def prepend_runtimepath(dir) runtimepath = echo '&runtimepath' command("set runtimepath=#{dir},#{runtimepath}") end |
- (Object) search(text)
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.
71 72 73 74 |
# File 'lib/vimrunner/client.rb', line 71 def search(text) normal type "/#{text}<CR>" end |
- (Object) set(setting, value = nil)
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
108 109 110 111 112 113 114 115 |
# File 'lib/vimrunner/client.rb', line 108 def set(setting, value = nil) if value command "set #{setting}=#{value}" else command "set #{setting}" end self end |
- (Object) type(keys)
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.
62 63 64 65 |
# File 'lib/vimrunner/client.rb', line 62 def type(keys) server.remote_send(keys) self end |
- (Object) write
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.
87 88 89 90 |
# File 'lib/vimrunner/client.rb', line 87 def write command :write self end |