vimrunner 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,5 +1,24 @@
1
- ## Very not ready yet, please ignore
2
-
3
1
  Using Vim's client/server functionality, this library exposes a way to spawn a
4
2
  Vim instance and control it programatically. Apart from being a fun party
5
3
  trick, this could be used to do integration testing on vimscript.
4
+
5
+ The `vimrunner` executable opens up an irb session with `$vim` set to a running
6
+ `gvim` instance. A few things you can try:
7
+
8
+ ``` ruby
9
+ $vim.edit 'some_file_name' # edit a file
10
+ $vim.insert 'Hello, World!' # enter insert mode and write some text
11
+ $vim.normal 'T,' # go back to the nearest comma
12
+ $vim.type 'a<cr>' # append a newline after the comma
13
+ $vim.write # write file to disk
14
+ ```
15
+
16
+ For more examples of what you can do, you could take a look at the specs, they
17
+ should be fairly readable.
18
+
19
+ Note that this should work on a Linux box, but probably won't on a Mac. I'm
20
+ assuming you'd need to change the binary to `mvim` at the very least.
21
+
22
+ This is still fairly experimental, so use with caution. Any issue reports or
23
+ contributions are very welcome on the
24
+ [github issue tracker](https://github.com/AndrewRadev/Vimrunner/issues)
@@ -9,8 +9,12 @@ module Vimrunner
9
9
  #
10
10
  # Use Runner#kill to manually destroy the background process.
11
11
  class Runner
12
+ attr_reader :servername
13
+
12
14
  class << self
13
15
  def start_gvim
16
+ servername = "VIMRUNNER#{rand.to_s.gsub '.', ''}"
17
+
14
18
  child_stdin, parent_stdin = IO::pipe
15
19
  parent_stdout, child_stdout = IO::pipe
16
20
  parent_stderr, child_stderr = IO::pipe
@@ -24,21 +28,26 @@ module Vimrunner
24
28
 
25
29
  [child_stdin, child_stdout, child_stderr].each { |io| io.close }
26
30
 
27
- exec 'gvim', '-f', '-u', vimrc_path, '--noplugin', '--servername', 'VIMRUNNER'
31
+ exec 'gvim', '-f', '-u', vimrc_path, '--noplugin', '--servername', servername
28
32
  end
29
33
 
30
34
  [child_stdin, child_stdout, child_stderr].each { |io| io.close }
31
35
 
32
- new(pid)
36
+ new(pid, servername)
33
37
  end
34
38
 
35
39
  def vimrc_path
36
40
  File.join(File.expand_path('../../..', __FILE__), 'vim', 'vimrc')
37
41
  end
42
+
43
+ def serverlist
44
+ %x[vim --serverlist].strip.split "\n"
45
+ end
38
46
  end
39
47
 
40
- def initialize(pid)
41
- @pid = pid
48
+ def initialize(pid, servername)
49
+ @pid = pid
50
+ @servername = servername
42
51
  wait_until_started
43
52
  end
44
53
 
@@ -59,16 +68,21 @@ module Vimrunner
59
68
  command("runtime #{entry_script}")
60
69
  end
61
70
 
71
+ # Invokes one of the basic actions the vim server supports, sending a key
72
+ # sequence. The keys are sent as-is, so it'd probably be better to use the
73
+ # wrapper methods, #normal, #insert and so on.
62
74
  def type(keys)
63
75
  invoke_vim '--remote-send', keys
64
76
  end
65
77
 
66
- # Executes +vim_command+ in the vim instance and returns its output,
78
+ # Executes the given command in the vim instance and returns its output,
67
79
  # stripping all surrounding whitespace.
68
80
  def command(vim_command)
69
81
  normal
70
82
 
71
- invoke_vim('--remote-expr', "VimrunnerEvaluateCommandOutput('#{vim_command.to_s}')").strip.tap do |output|
83
+ expression = "VimrunnerEvaluateCommandOutput('#{vim_command.to_s}')"
84
+
85
+ invoke_vim('--remote-expr', expression).strip.tap do |output|
72
86
  raise InvalidCommandError if output =~ /^Vim:E\d+:/
73
87
  end
74
88
  end
@@ -97,21 +111,23 @@ module Vimrunner
97
111
  end
98
112
 
99
113
  # Edits the file +filename+ with Vim.
114
+ #
115
+ # Note that this doesn't use the '--remote' vim flag, it simply types in
116
+ # the command manually. This is necessary to avoid the vim instance getting
117
+ # focus.
100
118
  def edit(filename)
101
- invoke_vim '--remote', filename
119
+ command "edit #{filename}"
102
120
  end
103
121
 
104
122
  # Writes the file being edited to disk. Note that you need to set the
105
123
  # file's name first by using Runner#edit.
106
124
  def write
107
- normal
108
- type ':w<cr>'
125
+ command :write
109
126
  end
110
127
 
111
128
  # Switches vim to insert mode and types in the given text.
112
129
  def insert(text = '')
113
- normal
114
- type "i#{text}"
130
+ normal "i#{text}"
115
131
  end
116
132
 
117
133
  # Switches vim to insert mode and types in the given keys.
@@ -119,44 +135,23 @@ module Vimrunner
119
135
  type "<c-\\><c-n>#{keys}"
120
136
  end
121
137
 
122
- def quit
123
- normal 'ZZ'
124
- end
125
-
126
138
  # Kills the vim instance in the background by sending it a TERM signal.
127
139
  def kill
128
140
  Shell.kill(@pid)
129
141
  end
130
142
 
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
143
  private
147
144
 
148
- def serverlist
149
- %x[vim --serverlist].strip.split '\n'
150
- end
151
-
152
145
  def invoke_vim(*args)
153
- args = ['vim', '--servername', 'VIMRUNNER', *args]
146
+ args = ['vim', '--servername', @servername, *args]
154
147
  Shell.run *args
155
148
  end
156
149
 
157
150
  def wait_until_started
158
- while serverlist.empty? or not serverlist.include? 'VIMRUNNER'
151
+ serverlist = Runner.serverlist
152
+ while serverlist.empty? or not serverlist.include? @servername
159
153
  sleep 0.1
154
+ serverlist = Runner.serverlist
160
155
  end
161
156
  end
162
157
  end
@@ -1,3 +1,3 @@
1
1
  module Vimrunner
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vimrunner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-28 00:00:00.000000000Z
12
+ date: 2011-10-29 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &10798240 !ruby/object:Gem::Requirement
16
+ requirement: &22761920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *10798240
24
+ version_requirements: *22761920
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rdoc
27
- requirement: &10796660 !ruby/object:Gem::Requirement
27
+ requirement: &22761500 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *10796660
35
+ version_requirements: *22761500
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &10794840 !ruby/object:Gem::Requirement
38
+ requirement: &22761000 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 2.0.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *10794840
46
+ version_requirements: *22761000
47
47
  description: ! " Using vim's client/server functionality, this library exposes
48
48
  a way to\n spawn a vim instance and control it programatically. Apart from being
49
49
  a fun\n party trick, this could be used to do integration testing on vimscript.\n"
@@ -77,7 +77,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
77
  version: '0'
78
78
  segments:
79
79
  - 0
80
- hash: -1926415317328212253
80
+ hash: -4179387824099540343
81
81
  required_rubygems_version: !ruby/object:Gem::Requirement
82
82
  none: false
83
83
  requirements: