forked from python-mode/python-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotion.vim
More file actions
97 lines (84 loc) · 2.61 KB
/
motion.vim
File metadata and controls
97 lines (84 loc) · 2.61 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
" Python-mode motion functions
fun! pymode#motion#move(pattern, flags, ...) "{{{
let cnt = v:count1 - 1
let [line, column] = searchpos(a:pattern, a:flags . 'sW')
let indent = indent(line)
while cnt && line
let [line, column] = searchpos(a:pattern, a:flags . 'W')
if indent(line) == indent
let cnt = cnt - 1
endif
endwhile
return [line, column]
endfunction "}}}
fun! pymode#motion#vmove(pattern, flags) range "{{{
call cursor(a:lastline, 0)
let end = pymode#motion#move(a:pattern, a:flags)
call cursor(a:firstline, 0)
normal! v
call cursor(end)
endfunction "}}}
fun! pymode#motion#pos_le(pos1, pos2) "{{{
return ((a:pos1[0] < a:pos2[0]) || (a:pos1[0] == a:pos2[0] && a:pos1[1] <= a:pos2[1]))
endfunction "}}}
fun! pymode#motion#select(pattern, inner) "{{{
let cnt = v:count1 - 1
let orig = getpos('.')[1:2]
let snum = s:BlockStart(orig[0], a:pattern)
if getline(snum) !~ a:pattern
return 0
endif
let enum = s:BlockEnd(snum, indent(snum))
while cnt
let lnum = search(a:pattern, 'nW')
if lnum
let enum = s:BlockEnd(lnum, indent(lnum))
call cursor(enum, 1)
endif
let cnt = cnt - 1
endwhile
if pymode#motion#pos_le([snum, 0], orig) && pymode#motion#pos_le(orig, [enum, 1])
if a:inner
let snum = snum + 1
let enum = prevnonblank(enum)
endif
call cursor(snum, 1)
normal! v
call cursor(enum, len(getline(enum)))
endif
endfunction "}}}
fun! s:BlockStart(lnum, ...) "{{{
let pattern = a:0 ? a:1 : '^\s*\(@\|class\s.*:\|def\s\)'
let lnum = a:lnum + 1
let indent = 100
while lnum
let lnum = prevnonblank(lnum - 1)
let test = indent(lnum)
let line = getline(lnum)
if line =~ '^\s*#' " Skip comments
continue
elseif !test " Zero-level regular line
return lnum
elseif test >= indent " Skip deeper or equal lines
continue
" Indent is strictly less at this point: check for def/class
elseif line =~ pattern && line !~ '^\s*@'
return lnum
endif
let indent = indent(lnum)
endwhile
return 0
endfunction "}}}
fun! s:BlockEnd(lnum, ...) "{{{
let indent = a:0 ? a:1 : indent(a:lnum)
let lnum = a:lnum
while lnum
let lnum = nextnonblank(lnum + 1)
if getline(lnum) =~ '^\s*#' | continue
elseif lnum && indent(lnum) <= indent
return lnum - 1
endif
endwhile
return line('$')
endfunction "}}}
" vim: fdm=marker:fdl=0