-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvimrc-to-json.py
91 lines (74 loc) · 2.13 KB
/
vimrc-to-json.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
import re
# Get the path of this file.
path = os.path.dirname(os.path.realpath(__file__)).replace("\\","/") + "/"
file = open(path + ".vimrc", "r", encoding='utf-8')
lines = file.read().split("\n")
file.close()
maptypes = {
"nmap":"normalModeKeyBindings",
"vmap":"visualModeKeyBindings",
"imap":"insertModeKeyBindings",
"nnoremap":"normalModeKeyBindingsNonRecursive",
"vnoremap":"visualModeKeyBindingsNonRecursive",
"inoremap":"insertModeKeyBindingsNonRecursive"
}
maplists = {
"nmap":[],
"vmap":[],
"imap":[],
"nnoremap":[],
"vnoremap":[],
"inoremap":[]
}
new_file = "{\n"
# Get all the mappings and place them in the correct category.
for item in lines:
matches = re.match("(^.*map)\s([\S]+)\s+([\S]+)$", item)
if matches:
maptype = matches.group(1)
before = matches.group(2)
after = matches.group(3)
if maptype in maplists:
maplists[maptype].append({"before" : before, "after" : after})
# Parses abc to ["a", "b", "c"] and :wq<CR> to [":wq"]
def mapToJSONList(mapstring, command):
map_json = "["
if command:
map_json += '"' + re.match("(:\w+)", mapstring).group(1) + '"]'
return map_json
else:
parts = re.findall("(<[^>]+>|.)", mapstring)
for part in parts:
if part == '"':
part = '\\"'
map_json += '"' + part + '", '
# Remove the last ', '
return map_json[:-2] + "]"
# Turn all the mappings into JSON format
for maptype in maplists:
maplist = maplists[maptype]
if len(maplist) > 0:
new_file += '"vim.' + maptypes[maptype] + '": [\n'
for item in maplist:
new_file += '{\n"before": '
new_file += mapToJSONList(item["before"], False)
# Check if it's a command
after = item["after"]
command = False
if after.startswith(":") and len(after) > 1:
new_file += ',\n"commands": '
command = True
else:
new_file += ',\n"after": '
new_file += mapToJSONList(item["after"], command)
new_file += '\n},\n'
# Remove the last ',\n'
new_file = new_file[:-2]
new_file += "\n],\n"
# Remove the last '],\n'
new_file = new_file[:-2] + "\n}"
# Write the JSON to settings.json in the same directory.
file = open(path + "settings.json", "w")
file.write(new_file)
file.close()