The first script: align

This commit is contained in:
Thomas Kolb 2013-10-09 21:56:19 +02:00
commit 2afd025a55

40
align/plugin/align.vim Normal file
View file

@ -0,0 +1,40 @@
function! s:Align(sequence) range
" First, find the rightmost occurence of sequence in all lines
let l:index = 0
for i in range(a:firstline, a:lastline)
let l:line = getline(i)
let l:seqpos = stridx(l:line, a:sequence)
if l:seqpos > l:index
let l:index = l:seqpos
endif
endfor
echo "Rightmost position: " . l:index
" Now, for each line, find the first occurrence of sequence, and insert
" spaces until sequence was shifted to l:index
for i in range(a:firstline, a:lastline)
let l:line = getline(i)
let l:seqpos = stridx(l:line, a:sequence)
if l:seqpos == -1
continue
endif
" Build string of spaces
let l:spaces = ""
for j in range(l:seqpos, l:index-1)
let l:spaces = l:spaces . ' '
endfor
" Insert spaces right before the sequence
if l:seqpos > 0
let l:line = l:line[: l:seqpos-1] . l:spaces . l:line[l:seqpos :]
else
let l:line = l:spaces . l:line
endif
call setline(i, l:line)
endfor
endfunction
command! -nargs=1 -range Align <line1>,<line2>call s:Align(<args>)