62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
import os
|
|
import sys
|
|
|
|
config = {
|
|
# path to directory containing vim.exe and gvim.exe
|
|
'vimdir': 'C:\\apps\\Vim\\vim73',
|
|
# 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]
|
|
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)
|
|
else:
|
|
if path_contains_file(path, fname):
|
|
return name.replace('*', os.path.basename(path))
|
|
return config['default_server']
|
|
|
|
def main(argv):
|
|
if (len(argv) < 2):
|
|
return -1
|
|
read_config_file(config, os.path.expanduser('~/.gvim-wrapper'))
|
|
fname = ' '.join(argv[1:])
|
|
servername = get_server_name(config, fname)
|
|
os.execv(config['vimdir'] + '\\gvim.exe',
|
|
['gvim', '--servername', servername, '--remote-tab-silent', '"' + fname + '"'])
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main(sys.argv))
|