From 9acb8cc3d14ddb6ed636e8c13b3decae4892d3c7 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Thu, 10 Oct 2013 18:46:20 +0200 Subject: [PATCH] [align] Regex support and default patterns --- align/plugin/align.vim | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/align/plugin/align.vim b/align/plugin/align.vim index 0611ef9..d9b589a 100644 --- a/align/plugin/align.vim +++ b/align/plugin/align.vim @@ -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 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) + let l:seqpos = match(l:line, l: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) + let l:seqpos = match(l:line, l:sequence) if l:seqpos == -1 continue @@ -37,4 +58,4 @@ function! s:Align(sequence) range endfor endfunction -command! -nargs=1 -range Align ,call s:Align() +command! -nargs=? -range Align ,call s:Align()