[align] Regex support and default patterns

This commit is contained in:
Thomas Kolb 2013-10-10 18:46:20 +02:00
parent 2afd025a55
commit 9acb8cc3d1

View file

@ -1,20 +1,41 @@
function! s:Align(sequence) range if !exists('g:Align_DefaultSeq')
let g:Align_DefaultSeq = {}
endif
let g:Align_DefaultSeq['c'] = '='
let g:Align_DefaultSeq['cpp'] = '='
let g:Align_DefaultSeq['python'] = '='
let g:Align_DefaultSeq['vhdl'] = '<=\|:\|:=\|=>'
let g:Align_DefaultSeq['vim'] = '='
function! s:Align(...) range
" If sequence is not set, find the default pattern for the current filetype
if !exists("a:1")
if !exists("g:Align_DefaultSeq[&filetype]")
echoerr "No Pattern was given and no default pattern could be found. Doing nothing."
return
endif
let l:sequence = g:Align_DefaultSeq[&filetype]
else
let l:sequence = a:1
endif
" First, find the rightmost occurence of sequence in all lines " First, find the rightmost occurence of sequence in all lines
let l:index = 0 let l:index = 0
for i in range(a:firstline, a:lastline) for i in range(a:firstline, a:lastline)
let l:line = getline(i) let l:line = getline(i)
let l:seqpos = stridx(l:line, a:sequence) let l:seqpos = match(l:line, l:sequence)
if l:seqpos > l:index if l:seqpos > l:index
let l:index = l:seqpos let l:index = l:seqpos
endif endif
endfor endfor
echo "Rightmost position: " . l:index
" Now, for each line, find the first occurrence of sequence, and insert " Now, for each line, find the first occurrence of sequence, and insert
" spaces until sequence was shifted to l:index " spaces until sequence was shifted to l:index
for i in range(a:firstline, a:lastline) for i in range(a:firstline, a:lastline)
let l:line = getline(i) let l:line = getline(i)
let l:seqpos = stridx(l:line, a:sequence) let l:seqpos = match(l:line, l:sequence)
if l:seqpos == -1 if l:seqpos == -1
continue continue
@ -37,4 +58,4 @@ function! s:Align(sequence) range
endfor endfor
endfunction endfunction
command! -nargs=1 -range Align <line1>,<line2>call s:Align(<args>) command! -nargs=? -range Align <line1>,<line2>call s:Align(<args>)