forked from prigaux/remote-emacsclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emacsclient.py
executable file
·43 lines (35 loc) · 1.1 KB
/
emacsclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
import re
import os
import socket
import sys
quote_map = {'&': '&&', '-': '&-', '\n': '&n', ' ': '&_'}
reverse_quote_map = {v: k for k, v in quote_map.iteritems()}
def quote(s):
return re.sub(r'[-&\n ]', lambda m: quote_map[m.group()], s)
def unquote(s):
return re.sub(r'&[-&n_]', lambda m: reverse_quote_map[m.group()], s)
nowait = False
if sys.argv[1:2] == ['-n']:
del sys.argv[1:2]
nowait = True
args = sys.argv[1:] or ['.']
with open(os.environ['remote_emacs_auth']) as infile:
host, client_auth, tramp_prefix = infile.read().splitlines()
host, port = host.split()
sock = socket.socket()
sock.connect((host, int(port)))
pwd = os.getcwd()
tramp_args = [client_auth, '-dir', tramp_prefix + quote(pwd)]
if nowait:
tramp_args.append('-nowait')
for arg in args:
if arg.startswith('+'):
tramp_args.extend(['-position', quote(arg)])
else:
tramp_args.extend(
['-file', tramp_prefix + quote(os.path.join(pwd, arg))])
sock.sendall(' '.join(tramp_args) + '\n')
buffer = ''
for line in sock.makefile():
print unquote(line.rstrip('\n'))