Following my thoughts yesterday, here are some VIM python scripts to add python breakpoint and debugging features to VIM. With this set up the F7 key will set a breakpoint on a line of code, Shift-F7 will remove all breakpoints and Shift-F12 will execute a script in the python debugger. This only runs on windows as far as I know, because it uses the 'start' command to launch the debugger in a seperate process without VIM waiting for it to finish. This allows you to look through the source code (and fix it) while the debugging is still in progress.
This goes in a python block in _vimrc:
1 def SetBreakpoint():
2 import re
3
4 nLine = int( vim.eval( 'line(".")'))
5
6 strLine = vim.current.line
7 strWhite = re.search( '^(/s*)', strLine).group(1)
8
9 vim.current.buffer.append(
10 "%(space)spdb.set_trace() %(mark)s Breakpoint %(mark)s" %
11 {'space':strWhite, 'mark': '#' * 30}, nLine - 1)
12
13 for strLine in vim.current.buffer:
14 if strLine == "import pdb":
15 break
16 else:
17 vim.current.buffer.append( 'import pdb', 0)
18 vim.command( 'normal j1')
19
20 vim.command( 'map <f7> :py SetBreakpoint()<cr>')
21
22 def RemoveBreakpoints():
23 import re
24
25 nCurrentLine = int( vim.eval( 'line(".")'))
26
27 nLines = []
28 nLine = 1
29 for strLine in vim.current.buffer:
30 if strLine == 'import pdb' or strLine.lstrip()[:15] == 'pdb.set_trace()':
31 nLines.append( nLine)
32 nLine += 1
33
34 nLines.reverse()
35
36 for nLine in nLines:
37 vim.command( 'normal %dG' % nLine)
38 vim.command( 'normal dd')
39 if nLine < nCurrentLine:
40 nCurrentLine -= 1
41
42 vim.command( 'normal %dG' % nCurrentLine)
43
44 vim.command( 'map <s-f7> :py RemoveBreakpoints()<cr>')
45
46 def RunDebugger():
47 vim.command( 'wall')
48 strFile = vim.eval( "g:mainfile")
49 vim.command( "!start python -m pdb %s" % strFile)
50
51 vim.command( 'map <s-f12> :py RunDebugger()<cr>')
This relies on using the runscript plugin, and modifying the function 'SetMainScript' as follows:
function s:SetMainScript()
let s:mainfile = bufname('%')
let g:mainfile = bufname('%')
echo s:mainfile . ' set as the starting program.'
endfunction
This allows F11 to be used to select which file is executed to start debugging. F12 can still be used to launch the script outside of the debugger.
Now I don't need WingIDE or a pc upgrade.
What do you mean by "a python block in _vimrc"? I was unaware of any way of embedding Python in a vimrc, myself...