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