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