Overwrite breakpoints when launching pygdb from vim, updating vim also after quit
[pygdb.git] / pygdb.vim
1 "shuber, 2008-06-08
2
3 if ! exists("g:pygdb")
4
5 let g:pygdb = 1
6 let s:ScriptLocation = expand("<sfile>")
7
8
9 python << >>
10
11 import gtk
12 import os
13 import string
14 import sys
15 import threading
16 import vim
17
18 import Configuration
19 import DbgTerminal
20
21
22
23 #Breakpoint positions: List of dictionaries of form
24 #{"signnum" : , "file" : , "lineno":, "cond" : }
25 gdbBps = []
26 signnum = 0
27 clientcmd = ""
28 execsign = None
29
30 def gdbLaunch():
31 global gdbterm, mainctrlwnd, statuswnd, gdbBps, clientcmd, gdbthread
32
33 clientcmd = vim.eval("input('Client commando: ', '%s')" % clientcmd).strip()
34
35 if clientcmd.strip()=="":
36 print "No command given!"
37 return
38
39 #Add the breakpoints to the configuration
40 conf = Configuration.Configuration()
41 conf.load(".pygdb.conf")
42 conf.breakpoints = []
43 for bp in gdbBps:
44 conf.addBreak(bp["file"], bp["lineno"], bp["cond"])
45 conf.store(".pygdb.conf")
46
47 vim.command("!python %s/pygdb.py %s &\n" % (pygdbdir, clientcmd))
48
49
50 def gdbToggleBreakpoint(lineno=None, file=None):
51 global gdbBps
52
53 #Set lineno and file if not already set
54 if lineno==None:
55 lineno = vim.current.window.cursor[0]
56 if file==None:
57 file = getCurrentFile()
58
59 #Determine index of breakpoint
60 bpidx = gdbGetBreakpoint( file, lineno )
61
62 if bpidx != None:
63 removeBreakpoint(bpidx)
64 else:
65 addBreakpoint(file, lineno)
66
67
68 def setExecutionLine(file, lineno):
69 global execsign
70
71
72 #Open that file!
73 if file != getCurrentFile():
74 try:
75 os.stat(file)
76 vim.command(":e %s" % file)
77 except OSError:
78 print "Warning: file '%s' does not exist! (Wrong client command?)" % file
79 return
80
81 #Jump to line
82 vim.command(":%d" % lineno)
83
84 #Remove old execsign
85 if execsign != None:
86 delExecutionLine()
87
88 #Set the sign
89 execsign = gdbNewSignnum()
90 vim.command("sign place %d line=%s name=ExecutionLine file=%s"%(execsign, lineno, file))
91
92
93 def delExecutionLine():
94 global execsign
95
96 #Remove old execsign
97 if execsign != None:
98 vim.command("sign unplace %d" % execsign)
99 execsign = None
100
101
102 def addBreakpoint(file, lineno, cond=None):
103 global gdbBps, cmdset
104
105 #If file is not open, open it
106 if not file in [b.name for b in vim.buffers]:
107 try:
108 os.stat(file)
109 vim.command(":e %s" % file)
110 except OSError:
111 print "Warning: file '%s' does not exist! (Wrong client command?)" % file
112 cmdset = False
113 return
114
115
116 #Determine a sign number
117 signnum = gdbNewSignnum()
118
119 #Create breakpoint and add sign
120 b = {"signnum" : signnum, "lineno" : lineno, "file" : file, "cond" : cond}
121 gdbBps += [b]
122
123 if cond == None:
124 vim.command("sign place %(signnum)d line=%(lineno)d name=BreakPoint file=%(file)s" % b)
125 else:
126 vim.command("sign place %(signnum)d line=%(lineno)d name=CondBreakPoint file=%(file)s" % b)
127
128
129
130 def removeBreakpoint(idx):
131 global gdbBps
132
133 vim.command("sign unplace %(signnum)d" % gdbBps[idx])
134 del gdbBps[idx]
135
136
137 def gdbBreakpointCond(lineno=None, file=None, cond=None):
138 global gdbBps
139
140 #Set lineno and file if not already set
141 if lineno==None:
142 lineno = vim.current.window.cursor[0]
143 if file==None:
144 file = getCurrentFile()
145
146 #Determine index of breakpoint
147 bpidx = gdbGetBreakpoint( file, lineno )
148
149 #Alter condition
150 if bpidx != None:
151 gdbBps[bpidx]["cond"] = vim.eval("input('Breakpoint condition: ', '%s')" % gdbBps[bpidx]["cond"])
152 #Set new breakpoint
153 else:
154 #Get condition
155 cond = vim.eval("input('Breakpoint condition: ', '')")
156 #Add the breakpoint
157 addBreakpoint(file, lineno, cond)
158
159
160 def getCurrentFile():
161 return vim.current.window.buffer.name
162
163
164
165 def gdbNewSignnum():
166 global signnum
167 signnum += 1
168 return signnum
169
170
171 def gdbGetBreakpoint(file, lineno):
172 for i in range(len(gdbBps)):
173 if [gdbBps[i]["file"], gdbBps[i]["lineno"]] == [file,lineno]:
174 return i
175 return None
176
177 def gdbShowBreakpoints():
178 global gdbBps
179
180 if len(gdbBps) == 0:
181 print "No breakpoints set."
182 else:
183 print "%d breakpoints set:" % len(gdbBps)
184
185 for bp in gdbBps:
186 if bp["cond"] != None:
187 print "%(file)s:%(lineno)d if %(cond)s" % bp
188 else:
189 print "%(file)s:%(lineno)d" % bp
190
191
192
193 def toAbsPath(path):
194 global clientcmd, cmdset
195
196 #Not a absolute path --> make one
197 if path[0] != os.sep:
198
199 #We need the client command to expand the paths...
200 while clientcmd == "" or not cmdset:
201 clientcmd = vim.eval("input('Client commando: ', '%s')" % clientcmd).strip()
202 cmdset = True
203
204 #Get the dirs where executeable is in
205 relcmd = string.split(clientcmd)[0]
206 abscmd = DbgTerminal.relToAbsPath(getCurrentFile(), relcmd)
207 path = DbgTerminal.relToAbsPath(abscmd, path)
208
209 assert(path[0] == "/")
210
211 return path
212
213
214 def gdbLoadConfig():
215 global clientcmd, gdbBps, cmdset
216
217
218
219
220 #Load configuration
221 conf = Configuration.Configuration()
222 conf.load(".pygdb.conf")
223
224 #Remove all breakpoints
225 while len(gdbBps)>0:
226 removeBreakpoint(0)
227
228 #Add breakpoints from configuration
229 for bp in conf.breakpoints:
230 bp["file"] = toAbsPath( bp["file"] )
231 addBreakpoint(bp["file"], bp["lineno"], bp["cond"])
232
233 #Set current execution line
234 if conf.isCurrposSet():
235 file = toAbsPath(conf.currfile)
236 setExecutionLine(file, conf.currlineno)
237 else:
238 delExecutionLine()
239 >>
240
241 highlight ExecutionLine term=bold ctermbg=DarkGreen ctermfg=Black guibg=LightGreen guifg=Black
242 highlight BreakPoint term=inverse ctermbg=DarkRed ctermfg=Black guibg=LightRed guifg=Black
243
244 sign define ExecutionLine text==> texthl=ExecutionLine linehl=ExecutionLine
245 sign define BreakPoint text=! texthl=BreakPoint linehl=BreakPoint
246 sign define CondBreakPoint text=? texthl=BreakPoint linehl=BreakPoint
247
248
249 command! GDBLaunch :python gdbLaunch()
250 command! GDBToggleBreakpoint :python gdbToggleBreakpoint()
251 command! GDBBreakpointCond :python gdbBreakpointCond()
252 command! GDBShowBreakpoints :python gdbShowBreakpoints()
253 command! GDBLoadConfig :python gdbLoadConfig()
254
255
256
257 function! GDBMapDefaults()
258 nmap <silent> <F5> :GDBLaunch<CR>
259 nmap <silent> <F8> :GDBToggleBreakpoint<CR>
260 nmap <silent> <S-F8> :GDBBreakpointCond<CR>
261 nmap <silent> <F9> :GDBShowBreakpoints<CR>
262 nmap <silent> <S-F9> :GDBLoadConfig<CR>
263 endfunction
264
265
266
267 endif
268
269