From 9d64e45d8df6af646b99a443da9dace7dd4d312f Mon Sep 17 00:00:00 2001 From: "Raymond W. Ko" Date: Sun, 6 Jan 2013 16:48:38 -0500 Subject: [PATCH] Add option of using if_python if if_ruby is not available --- autoload/nrepl/foreplay_connection.vim | 64 ++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/autoload/nrepl/foreplay_connection.vim b/autoload/nrepl/foreplay_connection.vim index 807811c..76b7a98 100644 --- a/autoload/nrepl/foreplay_connection.vim +++ b/autoload/nrepl/foreplay_connection.vim @@ -195,10 +195,7 @@ let s:nrepl = { \ 'path': s:function('s:nrepl_path'), \ 'process': s:function('s:nrepl_process')} -if !has('ruby') - finish -endif - +if has('ruby') ruby << require 'timeout' require 'socket' @@ -239,4 +236,63 @@ function! s:nrepl_call(payload) dict abort throw 'nREPL: '.err endfunction +finish +endif + +if has('python') +python << EOF +import vim +import socket +import string +import re + +def foreplay_string_encode(input): + str_list = [] + for c in input: + if (000 <= ord(c) and ord(c) <= 037) or c == '"' or c == "\\": + str_list.append("\\{0:03o}".format(ord(c))) + else: + str_list.append(c) + return '"' + ''.join(str_list) + '"' + +def foreplay_let(var, value): + return vim.command('let ' + var + " = " + foreplay_string_encode(value)) + +def foreplay_repl_interact(): + buffer = '' + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.settimeout(16.0) + try: + host = vim.eval('self.host') + port = int(vim.eval('self.port')) + s.connect((host, port)) + s.sendall(vim.eval('payload')) + while True: + body = s.recv(8192) + if re.search("=> $", body) != None: + raise Exception("not an nREPL server: upgrade to Leiningen 2") + buffer += body + if string.find(body, '6:statusl4:done') != -1: + break + foreplay_let('out', buffer) + except Exception, e: + foreplay_let('err', str(e)) + finally: + s.close() +EOF + +function! s:nrepl_call(payload) dict abort + let payload = nrepl#foreplay_connection#bencode(a:payload) + python << EOF +foreplay_repl_interact() +EOF + if !exists('err') + return nrepl#foreplay_connection#bdecode('l'.out.'e') + endif + throw 'nREPL: '.err +endfunction + +finish +endif + " vim:set et sw=2: