-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.vim
More file actions
109 lines (101 loc) · 3.01 KB
/
python.vim
File metadata and controls
109 lines (101 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
function! python#Error(msg)
execute 'normal! \<Esc>'
echohl ErrorMsg
echomsg 'python-vim: ' . a:msg
echohl None
endfunction
function! python#Success(msg)
execute 'normal! \<Esc>'
echohl Function
echomsg 'python-vim: ' . a:msg
echohl None
endfunction
function! python#SetArgs()
let args = input('Arguments: ', "", "file")
let g:pyargs = split(args, '\s+')
call python#Success('Arguments set')
endfunction
function! python#Run(...)
let cmd = ':terminal'
if filereadable('./venv/bin/python')
let cmd = cmd . " " . './venv/bin/python'
else
let cmd = cmd . " " . 'python'
endif
let cmd = cmd . " " . "%"
let i = 1
while i < argc()
echo "" . i . ": " . argv(i)
let cmd = cmd . " " . argv(i)
let i = i + 1
endwhile
if exists('g:pyargs') && len(g:pyargs) > 0
let extraargs = join(g:pyargs, " ")
let cmd = cmd . " " . extraargs
endif
call python#Success(cmd)
execute cmd
endfunction
function! python#Breakpoint()
let lineno = line('.')
let line = getline(lineno - 1)
if line =~ '^\s*import pdb; pdb.set_trace().*'
execute ':' . lineno - 1
d
call python#Success('Breakpoint removed')
else
if line =~ '^.*:\s*$'
let extra = ' '
else
let extra = ''
endif
call append(lineno - 1, substitute(line, '^\(\s*\).*$', '\1' . extra . 'import pdb; pdb.set_trace()', ''))
call python#Success('Breakpoint set')
endif
endfunction
function! python#Comment(visual)
if a:visual == 1
execute 'normal! gv'
let firstline = line("'<")
let lastline = line("'>")
else
let firstline = a:firstline
let lastline = a:lastline
endif
if firstline > lastline
let temp = firstline
let firstline = lastline
let lastline = temp
endif
let currentline = firstline
while currentline <= lastline
let line = getline(currentline)
if line =~ '^\s*#.*'
let changedline = substitute(line, '^\(\s*\)#', '\1', '')
else
let changedline = substitute(line, '^', '#', '')
endif
call setline(currentline, changedline)
let currentline = currentline + 1
endwhile
endfunction
function! python#SetArgsAndRun()
execute ':PySetArgs'
execute ':PyRun'
endfunction
command! PyRun call python#Run(<f-args>)
command! PyBreakpoint call python#Breakpoint()
command! -range -bar PyCommentVisual call python#Comment(1)
command! -range -bar PyCommentNormal call python#Comment(0)
command! PySetArgs call python#SetArgs()
command! PyRunWithArgs call python#SetArgsAndRun()
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" SUGGESTED MAPPINGS
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" autocmd BufNewFile,BufReadPost *.py nnoremap <F3> :PySetArgs <CR>
" autocmd BufNewFile,BufReadPost *.py nnoremap <F4> :PyBreakpoint <CR>
" autocmd BufNewFile,BufReadPost *.py nnoremap <F5> :PyRun <CR>
" autocmd BufNewFile,BufReadPost *.py nnoremap <F6> :PyRunWithArgs <CR>
" autocmd BufNewFile,BufReadPost *.py nnoremap <c-_> :PyCommentNormal <CR>
" autocmd BufNewFile,BufReadPost *.py xnoremap <c-_> :PyCommentVisual <CR>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""