-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvim-follow.py
79 lines (71 loc) · 2.37 KB
/
vim-follow.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
#
# Copyright (c) 2021 Vasile Vilvoiu <vasi@vilvoiu.ro>
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the MIT license. See LICENSE for details.
#
import os
global server_name
server_name = 'gdb'
global follow
follow = False
def vim_goto_location(file, line):
if file is None or line is None:
return
keystrokes = '<Esc>:edit {}<CR>:{}<CR>zz'.format(file, line)
if server_name[0] == '!':
# nvim
os.system('nvim --server "{}" --remote-send "{}"'.format(os.path.expanduser(server_name[1:]), keystrokes))
else:
# vim
os.system('vim --servername "{}" --remote-send "{}"'.format(server_name, keystrokes))
def get_location():
try:
sal = gdb.selected_frame().find_sal()
return sal.symtab.fullname(), sal.line
except:
return None, None
def prompt_handler():
f, l = get_location()
vim_goto_location(f, l)
class Vim(gdb.Command):
def __init__(self):
super(Vim, self).__init__('vim', gdb.COMMAND_FILES)
def complete(self, text, word):
CMDS = ['server', 'follow', 'unfollow']
return [k for k in CMDS if k.startswith(text)]
def invoke(self, argument, from_tty):
global server_name
global follow
tok = argument.split()
if len(tok) == 0:
f, l = get_location()
vim_goto_location(f, l)
elif tok[0] == 'server':
if len(tok) != 2:
gdb.write('Usage: vim server <server_name>\n')
else:
server_name = tok[1]
elif tok[0] == 'follow':
if len(tok) != 1:
gdb.write('vim follow takes no parameters')
else:
try:
if not follow:
gdb.events.before_prompt.connect(prompt_handler)
follow = True
except:
pass
elif tok[0] == 'unfollow':
if len(tok) != 1:
gdb.write('vim unfollow takes no parameters\n')
else:
try:
if follow:
gdb.events.before_prompt.disconnect(prompt_handler)
follow = False
except:
pass
else:
gdb.write('Bad argument; try "server", "follow", "unfollow" or without arguments\n')
Vim()