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