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