-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_vim_fxns.vim
68 lines (54 loc) · 1.58 KB
/
python_vim_fxns.vim
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
" check for python support
if !has('python')
"echom "Error: Requires vim compiled with +python" " causes hang on start
finish
endif
python << EOF
booltable = {'True':'False', 'true':'false', 't':'f', 'T':'F', '1':'0', 'TRUE':'FALSE'}
for key in booltable.keys():
booltable[booltable[key]] = key
EOF
function! ToggleBool()
python << EOF
import vim
def main():
def toggle_bool(bool):
'''Given a bool (as a string), this returns its negation.'''
table = booltable
return table[bool]
# get current location
row, col = vim.current.window.cursor
line = vim.current.buffer[row-1]
# find location of word to be replaced in buffer
EOW, pos = False, col
while not EOW and pos < len(line):
char = line[pos]
if char in [' ', "'", '"', '+', '-', '=', ',', '.', ')', '(']:
EOW = True
else:
pos += 1
try:
notbool = toggle_bool(line[col:pos])
except:
print('Boolean "{}" not recognized.'.format(line[col:pos]))
return
# replace
replacement = list(line)
replacement[col:pos] = list(notbool)
vim.current.buffer[row-1] = "".join(replacement)
return
main()
EOF
endfunction
function! GotoBool()
python << EOF
import vim, re
row, col = vim.current.window.cursor
line = vim.current.buffer[row-1]
matchstrings = booltable.keys()
matchstrings.sort(key=lambda a: len(a), reverse=True)
boolre = 'inevermatchbecauseimnotdone'
match = re.search(boolre, line)
vim.current.window.cursor = (row, match.start())
EOF
endfunction