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