import os import sys if os.path.exists('C:\\apps\\Vim\\vim74\\gvim.exe'): vimdir = 'C:\\apps\\Vim\\vim74' elif os.path.exists('C:\\apps\\Vim\\vim73\\gvim.exe'): vimdir = 'C:\\apps\\Vim\\vim73' else: raise "Could not find Vim directory, update script" config = { # path to directory containing vim.exe and gvim.exe 'vimdir': vimdir, # list of (path, name) tuples defining gvim contexts 'servers': [], # default server name to use if no server paths match 'default_server': 'GVIM', } def read_config_file(config, path): if os.path.exists(path): fh = open(path, 'r') script = fh.read() fh.close() try: exec(script, config) except: sys.stderr.write('Configuration file error in "%s":\n' % path) traceback.print_exception(sys.exc_info()[0], sys.exc_info()[1], None) tb = traceback.extract_tb(sys.exc_info()[2]) for ent in tb[1:]: lineno, fn = ent[1:3] sys.stderr.write(' File "%s", line %d, in %s\n' % (path, lineno, fn)) def path_contains_file(path, fname): path = path.replace('\\', '/') fname = fname.replace('\\', '/') if path.endswith('/'): path = path[:-1] return fname.startswith(path + '/') def get_server_name(config, fname): for path, name in config['servers']: path = path.replace('\\', '/') if path.endswith('/*'): path = path[:-2] try: for dirent in os.listdir(path): query_path = os.path.join(path, dirent) if os.path.isdir(query_path) and path_contains_file(query_path, fname): return name.replace('*', dirent) except: pass else: if path_contains_file(path, fname): return name.replace('*', os.path.basename(path)) return config['default_server'] def main(argv): read_config_file(config, os.path.expanduser('~/.gvim-wrapper')) fname = ' '.join(argv[1:]) if len(argv) >= 2 else '' servername = get_server_name(config, fname if fname != '' else os.getcwd() + '/dum') cmd = ['gvim', '--servername', '"%s"' % servername] if fname != '': cmd += ['--remote-tab-silent', '"%s"' % fname] os.execv(config['vimdir'] + '\\gvim.exe', cmd) if __name__ == '__main__': sys.exit(main(sys.argv))