From 2afd025a55e9bae301d4f327f075a8dcbe9048d3 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Wed, 9 Oct 2013 21:56:19 +0200 Subject: [PATCH] The first script: align --- align/plugin/align.vim | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 align/plugin/align.vim diff --git a/align/plugin/align.vim b/align/plugin/align.vim new file mode 100644 index 0000000..0611ef9 --- /dev/null +++ b/align/plugin/align.vim @@ -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 ,call s:Align()