From 35e14c526d870014e7a1b50fe47601e9c18cb697 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Thu, 10 Oct 2013 19:59:51 +0200 Subject: [PATCH] [align] Align all occurences in the selected lines --- align/plugin/align.vim | 52 +++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/align/plugin/align.vim b/align/plugin/align.vim index d9b589a..fe864c4 100644 --- a/align/plugin/align.vim +++ b/align/plugin/align.vim @@ -1,3 +1,7 @@ +if !exists('g:Align_StartIncrement') + let g:Align_StartIncrement = 4 +endif + if !exists('g:Align_DefaultSeq') let g:Align_DefaultSeq = {} endif @@ -9,53 +13,65 @@ 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 pattern 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] + let l:pattern = g:Align_DefaultSeq[&filetype] else - let l:sequence = a:1 + let l:pattern = a:1 endif - " First, find the rightmost occurence of sequence in all lines - let l:index = 0 - for i in range(a:firstline, a:lastline) + let l:nextstart = 0 + while l:nextstart >= 0 + let l:nextstart = s:AlignOnce(l:nextstart, l:pattern, a:firstline, a:lastline) + if l:nextstart != -1 + let l:nextstart = l:nextstart + g:Align_StartIncrement + endif + endwhile +endfunction + +function! s:AlignOnce(startpos, pattern, startline, endline) + " First, find the rightmost occurence of pattern in all lines + let l:index = -1 + for i in range(a:startline, a:endline) let l:line = getline(i) - let l:seqpos = match(l:line, l:sequence) - if l:seqpos > l:index - let l:index = l:seqpos + let l:pos = match(l:line, a:pattern, a:startpos) + if l:pos > l:index + let l:index = l:pos endif endfor - " 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) + " Now, for each line, find the first occurrence of pattern, and insert + " spaces until pattern was shifted to l:index + for i in range(a:startline, a:endline) let l:line = getline(i) - let l:seqpos = match(l:line, l:sequence) + let l:pos = match(l:line, a:pattern, a:startpos) - if l:seqpos == -1 + if l:pos == -1 continue endif " Build string of spaces let l:spaces = "" - for j in range(l:seqpos, l:index-1) + for j in range(l:pos, 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 :] + " Insert spaces right before the pattern + if l:pos > 0 + let l:line = l:line[: l:pos-1] . l:spaces . l:line[l:pos :] else let l:line = l:spaces . l:line endif call setline(i, l:line) endfor + + return l:index endfunction command! -nargs=? -range Align ,call s:Align()