vimrunner 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,5 @@
1
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
2
+
3
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
4
+
5
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,5 @@
1
+ ## Very not ready yet, please ignore
2
+
3
+ Using Vim's client/server functionality, this library exposes a way to spawn a
4
+ Vim instance and control it programatically. Apart from being a fun party
5
+ trick, this could be used to do integration testing on vimscript.
data/bin/vimrunner ADDED
@@ -0,0 +1,10 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ $: << File.expand_path('../../lib', __FILE__)
4
+
5
+ require 'irb'
6
+ require 'vimrunner'
7
+
8
+ $vim = Vimrunner::Runner.start_gvim
9
+
10
+ IRB.start
@@ -0,0 +1,3 @@
1
+ module Vimrunner
2
+ class InvalidCommandError < RuntimeError; end
3
+ end
@@ -0,0 +1,163 @@
1
+ require 'vimrunner/shell'
2
+ require 'vimrunner/errors'
3
+
4
+ module Vimrunner
5
+
6
+ # The Runner class acts as the actual proxy to a vim instance. Upon
7
+ # initialization, a vim process is started in the background. The Runner
8
+ # instance's public methods correspond to actions the instance will perform.
9
+ #
10
+ # Use Runner#kill to manually destroy the background process.
11
+ class Runner
12
+ class << self
13
+ def start_gvim
14
+ child_stdin, parent_stdin = IO::pipe
15
+ parent_stdout, child_stdout = IO::pipe
16
+ parent_stderr, child_stderr = IO::pipe
17
+
18
+ pid = Kernel.fork do
19
+ [parent_stdin, parent_stdout, parent_stderr].each { |io| io.close }
20
+
21
+ STDIN.reopen(child_stdin)
22
+ STDOUT.reopen(child_stdout)
23
+ STDERR.reopen(child_stderr)
24
+
25
+ [child_stdin, child_stdout, child_stderr].each { |io| io.close }
26
+
27
+ exec 'gvim', '-f', '-u', vimrc_path, '--noplugin', '--servername', 'VIMRUNNER'
28
+ end
29
+
30
+ [child_stdin, child_stdout, child_stderr].each { |io| io.close }
31
+
32
+ new(pid)
33
+ end
34
+
35
+ def vimrc_path
36
+ File.join(File.expand_path('../../..', __FILE__), 'vim', 'vimrc')
37
+ end
38
+ end
39
+
40
+ def initialize(pid)
41
+ @pid = pid
42
+ wait_until_started
43
+ end
44
+
45
+ # Adds a plugin to Vim's runtime. Initially, Vim is started without
46
+ # sourcing any plugins to ensure a clean state. This method can be used to
47
+ # populate the instance's environment.
48
+ #
49
+ # dir - The base directory of the plugin, the one that contains
50
+ # its autoload, plugin, ftplugin, etc. directories.
51
+ # entry_script - The vim script that's runtime'd to initialize the plugin.
52
+ #
53
+ # Example:
54
+ #
55
+ # vim.add_plugin 'rails', 'plugin/rails.vim'
56
+ #
57
+ def add_plugin(dir, entry_script)
58
+ command("set runtimepath+=#{dir}")
59
+ command("runtime #{entry_script}")
60
+ end
61
+
62
+ def type(keys)
63
+ invoke_vim '--remote-send', keys
64
+ end
65
+
66
+ # Executes +vim_command+ in the vim instance and returns its output,
67
+ # stripping all surrounding whitespace.
68
+ def command(vim_command)
69
+ normal
70
+
71
+ invoke_vim('--remote-expr', "VimrunnerEvaluateCommandOutput('#{vim_command.to_s}')").strip.tap do |output|
72
+ raise InvalidCommandError if output =~ /^Vim:E\d+:/
73
+ end
74
+ end
75
+
76
+ # Starts a search in vim for the given text. The result is that the cursor
77
+ # is positioned on its first occurrence.
78
+ def search(text)
79
+ normal
80
+ type "/#{text}<cr>"
81
+ end
82
+
83
+ # Sets a setting in vim. If +value+ is nil, the setting is considered to be
84
+ # a boolean.
85
+ #
86
+ # Examples:
87
+ #
88
+ # vim.set 'expandtab' # invokes ":set expandtab"
89
+ # vim.set 'tabstop', 3 # invokes ":set tabstop=3"
90
+ #
91
+ def set(setting, value = nil)
92
+ if value
93
+ command "set #{setting}=#{value}"
94
+ else
95
+ command "set #{setting}"
96
+ end
97
+ end
98
+
99
+ # Edits the file +filename+ with Vim.
100
+ def edit(filename)
101
+ invoke_vim '--remote', filename
102
+ end
103
+
104
+ # Writes the file being edited to disk. Note that you need to set the
105
+ # file's name first by using Runner#edit.
106
+ def write
107
+ normal
108
+ type ':w<cr>'
109
+ end
110
+
111
+ # Switches vim to insert mode and types in the given text.
112
+ def insert(text = '')
113
+ normal
114
+ type "i#{text}"
115
+ end
116
+
117
+ # Switches vim to insert mode and types in the given keys.
118
+ def normal(keys = '')
119
+ type "<c-\\><c-n>#{keys}"
120
+ end
121
+
122
+ def quit
123
+ normal 'ZZ'
124
+ end
125
+
126
+ # Kills the vim instance in the background by sending it a TERM signal.
127
+ def kill
128
+ Shell.kill(@pid)
129
+ end
130
+
131
+ # Ensures that vim has finished with its previous action. This is useful
132
+ # when a command has been sent to the vim instance that might take a little
133
+ # while, and we need to check the results of the command.
134
+ #
135
+ # Example:
136
+ #
137
+ # runner.write
138
+ # runner.wait_until_ready
139
+ # # Provided there was no error, the file should now be written
140
+ # # successfully
141
+ #
142
+ def wait_until_ready
143
+ command :echo
144
+ end
145
+
146
+ private
147
+
148
+ def serverlist
149
+ %x[vim --serverlist].strip.split '\n'
150
+ end
151
+
152
+ def invoke_vim(*args)
153
+ args = ['vim', '--servername', 'VIMRUNNER', *args]
154
+ Shell.run *args
155
+ end
156
+
157
+ def wait_until_started
158
+ while serverlist.empty? or not serverlist.include? 'VIMRUNNER'
159
+ sleep 0.1
160
+ end
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,32 @@
1
+ module Vimrunner
2
+
3
+ # The Shell module contains functions that interact directly with the shell.
4
+ # They're general utilities that help with some of the specific use cases
5
+ # that pop up with the vim runner.
6
+ module Shell
7
+ extend self
8
+
9
+ # Executes a shell command, waits until it's finished, and returns the
10
+ # output
11
+ def run(*command)
12
+ IO.popen(command) { |io| io.read.strip }
13
+ end
14
+
15
+ # Sends a TERM signal to the given PID if it corresponds to a running
16
+ # process.
17
+ def kill(pid)
18
+ Process.kill(Signal.list['TERM'], pid) if running?(pid)
19
+ end
20
+
21
+ private
22
+
23
+ # Checks if the given PID corresponds to a running process
24
+ def running?(pid)
25
+ return false if pid.nil?
26
+ Process.getpgid(pid)
27
+ true
28
+ rescue Errno::ESRCH
29
+ false
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Vimrunner
2
+ VERSION = '0.0.1'
3
+ end
data/lib/vimrunner.rb ADDED
@@ -0,0 +1 @@
1
+ require 'vimrunner/runner'
data/vim/vimrc ADDED
@@ -0,0 +1,19 @@
1
+ set nocompatible
2
+
3
+ filetype plugin on
4
+ filetype indent on
5
+ syntax on
6
+
7
+ function! VimrunnerEvaluateCommandOutput(command)
8
+ let base_command = split(a:command, '\s\+')[0]
9
+
10
+ if !exists(':'.base_command)
11
+ let output = 'Vim:E492: Not an editor command: '.base_command
12
+ else
13
+ redir => output
14
+ silent exe a:command
15
+ redir END
16
+ endif
17
+
18
+ return output
19
+ endfunction
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vimrunner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Radev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-28 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &10798240 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *10798240
25
+ - !ruby/object:Gem::Dependency
26
+ name: rdoc
27
+ requirement: &10796660 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *10796660
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &10794840 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 2.0.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *10794840
47
+ description: ! " Using vim's client/server functionality, this library exposes
48
+ a way to\n spawn a vim instance and control it programatically. Apart from being
49
+ a fun\n party trick, this could be used to do integration testing on vimscript.\n"
50
+ email:
51
+ - andrey.radev@gmail.com
52
+ executables:
53
+ - vimrunner
54
+ extensions: []
55
+ extra_rdoc_files: []
56
+ files:
57
+ - lib/vimrunner/shell.rb
58
+ - lib/vimrunner/version.rb
59
+ - lib/vimrunner/runner.rb
60
+ - lib/vimrunner/errors.rb
61
+ - lib/vimrunner.rb
62
+ - vim/vimrc
63
+ - bin/vimrunner
64
+ - LICENSE
65
+ - README.md
66
+ homepage: http://github.com/AndrewRadev/vimrunner
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ segments:
79
+ - 0
80
+ hash: -1926415317328212253
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: 1.3.6
87
+ requirements: []
88
+ rubyforge_project: vimrunner
89
+ rubygems_version: 1.8.11
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Lets you control a vim instance through ruby
93
+ test_files: []