-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendwise.vim
131 lines (114 loc) · 4.55 KB
/
endwise.vim
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
" endwise.vim - EndWise
" Author: Tim Pope <[email protected]>
" Version: 1.0
" Distributable under the same terms as Vim itself (see :help license)
" Exit quickly when:
" - this plugin was already loaded (or disabled)
" - when 'compatible' is set
if (exists("g:loaded_endwise") && g:loaded_endwise) || &cp
finish
endif
let g:loaded_endwise = 1
let s:cpo_save = &cpo
set cpo&vim
augroup endwise " {{{1
au!
autocmd FileType ruby
\ let b:endwise_addition = '\=submatch(0)=="{" ? "}" : "end"' |
\ let b:endwise_words = 'module,class,def,if,unless,case,while,until,begin,do' |
\ let b:endwise_pattern = '^\s*\zs\%(module\|class\|def\|if\|unless\|case\|while\|until\|for\|\|begin\)\>\%(.*[^.:@$]\<end\>\)\@!\|\<do\ze\%(\s*|.*|\)\=\s*$' |
\ let b:endwise_syngroups = 'rubyModule,rubyClass,rubyDefine,rubyControl,rubyConditional,rubyRepeat'
autocmd FileType vb,vbnet,aspvbs
\ let b:endwise_addition = 'End &' |
\ let b:endwise_words = 'Function,Sub,Class,Module,Enum,Namespace' |
\ let b:endwise_pattern = '\%(\<End\>.*\)\@<!\<&\>' |
\ let b:endwise_syngroups = 'vbStatement,vbnetStorage,vbnetProcedure,vbnet.*Words,AspVBSStatement'
autocmd FileType vim
\ let b:endwise_addition = 'end&' |
\ let b:endwise_words = 'fu\%[nction],wh\%[ile],if,for,try' |
\ let b:endwise_syngroups = 'vimFuncKey,vimNotFunc,vimCommand'
augroup END " }}}1
" Maps {{{1
if maparg("<Plug>DiscretionaryEnd") == ""
inoremap <silent> <SID>DiscretionaryEnd <C-R>=<SID>crend(0)<CR>
inoremap <silent> <SID>AlwaysEnd <C-R>=<SID>crend(1)<CR>
imap <script> <Plug>DiscretionaryEnd <SID>DiscretionaryEnd
imap <script> <Plug>AlwaysEnd <SID>AlwaysEnd
endif
if maparg('<CR>','i') =~# '<C-R>=.*crend(.)<CR>\|<\%(Plug\|SID\)>.*End'
" Already mapped
elseif maparg('<CR>','i') =~ '<CR>'
exe "imap <script> <C-X><CR> ".maparg('<CR>','i')."<SID>AlwaysEnd"
exe "imap <script> <CR> ".maparg('<CR>','i')."<SID>DiscretionaryEnd"
else
imap <C-X><CR> <CR><Plug>AlwaysEnd
imap <CR> <CR><Plug>DiscretionaryEnd
endif
if maparg('<M-o>','i') == ''
inoremap <M-o> <C-O>o
endif
" }}}1
" Code {{{1
function! s:mysearchpair(beginpat,endpat,synpat)
let g:endwise_syntaxes = ""
let s:lastline = line('.')
call s:synname()
let line = searchpair(a:beginpat,'',a:endpat,'Wn','<SID>synname() !~# "^'.substitute(a:synpat,'\\','\\\\','g').'$"',line('.')+50)
return line
endfunction
function! s:crend(always)
let n = ""
if !exists("b:endwise_addition") || !exists("b:endwise_words") || !exists("b:endwise_syngroups")
return n
end
let synpat = '\%('.substitute(b:endwise_syngroups,',','\\|','g').'\)'
let wordchoice = '\%('.substitute(b:endwise_words,',','\\|','g').'\)'
if exists("b:endwise_pattern")
let beginpat = substitute(b:endwise_pattern,'&',substitute(wordchoice,'\\','\\&','g'),'g')
else
let beginpat = '\<'.wordchoice.'\>'
endif
let lnum = line('.') - 1
let space = matchstr(getline(lnum),'^\s*')
let col = match(getline(lnum),beginpat) + 1
let word = matchstr(getline(lnum),beginpat)
let endpat = substitute(word,'.*',b:endwise_addition,'')
let y = n.endpat."\<C-O>O"
let endpat = '\<'.substitute(wordchoice,'.*',b:endwise_addition,'').'\>'
if a:always
return y
elseif col <= 0 || synIDattr(synID(lnum,col,1),'name') !~ '^'.synpat.'$'
return n
elseif getline('.') !~ '^\s*#\=$'
return n
endif
let line = s:mysearchpair(beginpat,endpat,synpat)
" even is false if no end was found, or if the end found was less
" indented than the current line
let even = strlen(matchstr(getline(line),'^\s*')) >= strlen(space)
if line == 0
let even = 0
endif
let g:endwise_debug = line . "(" . even . ")"
if !even && line == line('.') + 1
return y
endif
if even
return n
endif
return y
endfunction
function! s:synname()
" Checking this helps to force things to stay in sync
while s:lastline < line('.')
let s = synIDattr(synID(s:lastline,indent(s:lastline)+1,1),'name')
let s:lastline = nextnonblank(s:lastline + 1)
endwhile
let s = synIDattr(synID(line('.'),col('.'),1),'name')
let g:endwise_syntaxes = g:endwise_syntaxes . line('.').','.col('.')."=".s."\n"
let s:lastline = line('.')
return s
endfunction
" }}}1
let &cpo = s:cpo_save
" vim:set ft=vim ff=unix ts=8 sw=4 sts=4: