-
Notifications
You must be signed in to change notification settings - Fork 13
Improve connect behavior #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5d23456
e693cb4
89cc63b
b070aaf
e1ae8dc
d6a0796
b2286ea
671526d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,9 @@ module Vimrunner | |
| # to use a Server directly to invoke --remote commands on its Vim instance. | ||
| class Server | ||
| VIMRC = File.expand_path("../../../vim/vimrc", __FILE__) | ||
| VIMRUNNER_RC = File.expand_path("../../../vim/vimrunner_rc", __FILE__) | ||
|
|
||
| attr_reader :name, :executable | ||
| attr_reader :name, :executable, :vimrc | ||
|
|
||
| # Public: Initialize a Server | ||
| # | ||
|
|
@@ -30,6 +31,8 @@ class Server | |
| def initialize(options = {}) | ||
| @executable = options.fetch(:executable) { Platform.vim } | ||
| @name = options.fetch(:name) { "VIMRUNNER#{rand}" } | ||
| @vimrc = options.fetch(:vimrc) { VIMRC } | ||
| @foreground = options.fetch(:foreground, true) | ||
| end | ||
|
|
||
| # Public: Start a Server. This spawns a background process. | ||
|
|
@@ -46,32 +49,34 @@ def initialize(options = {}) | |
| # Returns a new Client instance initialized with this Server. | ||
| # Yields a new Client instance initialized with this Server. | ||
| def start | ||
| @r, @w, @pid = spawn | ||
| if block_given? | ||
| spawn do |r, w, pid| | ||
| begin | ||
| @result = yield(connect) | ||
| ensure | ||
| r.close | ||
| w.close | ||
| Process.kill(9, pid) rescue Errno::ESRCH | ||
| end | ||
| begin | ||
| @result = yield(connect) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should be |
||
| ensure | ||
| @r.close | ||
| @w.close | ||
| Process.kill(9, @pid) rescue Errno::ESRCH | ||
| end | ||
|
|
||
| @result | ||
| else | ||
| @r, @w, @pid = spawn | ||
|
|
||
| connect | ||
| end | ||
| end | ||
|
|
||
| # Public: Connects to the running server by name, blocking if need be. | ||
| # | ||
| # Returns a new Client instance initialized with this Server. | ||
| def connect | ||
| def connect(options = {}) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rather not have # if you want to spawn a new server if it's not already there:
server = Server.new(:name => 'FOO')
if not server.connected?
server.start
end
# if you want to wait until someone spawns the server:
server = Server.new(:name => 'FOO')
server.connectDo you have a use case that this doesn't cover?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have add the # if you want to spawn a new server if it's not already there:
client = Server.new(:name => 'FOO').connect(:spawn => true)
# if you want to wait until someone spawns the server:
client = Server.new(:name => 'FOO').connect |
||
| if !connected? && options[:spawn] | ||
| @r, @w, @pid = spawn | ||
| end | ||
| wait_until_started | ||
|
|
||
| new_client | ||
| client = new_client | ||
| client.source(VIMRUNNER_RC) | ||
|
|
||
| return client | ||
| end | ||
|
|
||
| # Public: Checks if the server is connected to a running Vim instance. | ||
|
|
@@ -129,12 +134,16 @@ def remote_send(keys) | |
|
|
||
| private | ||
|
|
||
| def foreground_option | ||
| @foreground ? '-f' : '' | ||
| end | ||
|
|
||
| def execute(command) | ||
| IO.popen(command) { |io| io.read.strip } | ||
| end | ||
|
|
||
| def spawn(&blk) | ||
| PTY.spawn(executable, "-f", "--servername", name, "-u", VIMRC, &blk) | ||
| def spawn | ||
| PTY.spawn("#{executable} #{foreground_option} --servername #{name} -u #{vimrc}") | ||
| end | ||
|
|
||
| def wait_until_started | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| function! VimrunnerEvaluateCommandOutput(command) | ||
| let output = '' | ||
|
|
||
| try | ||
| redir => output | ||
| silent exe a:command | ||
| redir END | ||
|
|
||
| let output = s:StripSilencedErrors(output) | ||
| catch | ||
| let output = v:exception | ||
| endtry | ||
|
|
||
| return output | ||
| endfunction | ||
|
|
||
| " Remove errors from the output that have been silenced by :silent!. These are | ||
| " visible in the captured output since all messages are captured by :redir. | ||
| function! s:StripSilencedErrors(output) | ||
| let processed_output = [] | ||
|
|
||
| for line in reverse(split(a:output, "\n")) | ||
| if line =~ '^E\d\+:' | ||
| break | ||
| endif | ||
|
|
||
| call add(processed_output, line) | ||
| endfor | ||
|
|
||
| return join(reverse(processed_output), "\n") | ||
| endfunction | ||
|
|
||
| " vim: set ft=vim |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to make the
foregroundan option? To be honest, I just kept it around, since I needed it before for gvim spawning, but I don't think it's even necessary now. On my linux, both with and without-f, it works the same way.@mudge, could you experiment with removing
-fon your mac? I may make a separate issue for this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No issue with removing
-f, this works fine:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I do not optional remove
-f, the spawn vim server would quit after my script quit. The purpose is to send some command for vim to do it for me. I do not want to spawn the vim server each time. For example, I use Vim to detect and fix file encoding, dir diff, batch rename files, etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that makes a lot of sense. I'm okay with leaving the foreground optional then. I'll make a separate issue for this, though, because I want to see if I can get consistently similar behaviour for the headless version as well. Maybe a different method may make sense, like
detach.